35 #include "llvm/Support/VCSRevision.h"
43 using namespace irsymtab;
47 const char *getExpectedProducerName() {
48 static char DefaultName[] = LLVM_VERSION_STRING
55 if (
char *OverrideName = getenv(
"LLVM_OVERRIDE_PRODUCER"))
60 const char *kExpectedProducerName = getExpectedProducerName();
73 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {}
79 std::vector<storage::Comdat> Comdats;
80 std::vector<storage::Module> Mods;
81 std::vector<storage::Symbol> Syms;
82 std::vector<storage::Uncommon> Uncommons;
84 std::string COFFLinkerOpts;
88 S.
Offset = StrtabBuilder.add(Value);
96 Symtab.insert(Symtab.end(),
reinterpret_cast<const char *
>(Objs.data()),
97 reinterpret_cast<const char *>(Objs.data() + Objs.size()));
110 return make_error<StringError>(
"input module has no datalayout",
120 Mod.
Begin = Syms.size();
121 Mod.
End = Syms.size() + Msymtab.
symbols().size();
125 if (TT.isOSBinFormatCOFF()) {
131 for (
const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands())
132 COFFLinkerOptsOS <<
" " << cast<MDString>(MDOption)->getString();
137 if (
Error Err = addSymbol(Msymtab, Used, Msym))
155 Uncommons.emplace_back();
156 Unc = &Uncommons.back();
189 if (Flags & object::BasicSymbolRef::SF_Undefined)
195 setStr(Sym.
IRName, GV->getName());
199 if (GV->isThreadLocal())
201 if (GV->hasGlobalUnnamedAddr())
207 if (Flags & object::BasicSymbolRef::SF_Common) {
208 Uncommon().CommonSize = GV->getParent()->getDataLayout().getTypeAllocSize(
209 GV->getType()->getElementType());
210 Uncommon().CommonAlign = GV->getAlignment();
215 return make_error<StringError>(
"Unable to determine comdat of alias!",
218 auto P = ComdatMap.insert(std::make_pair(
C, Comdats.size()));
223 setStr(Comdat.
Name,
C->getName());
224 Comdats.push_back(Comdat);
228 if (TT.isOSBinFormatCOFF()) {
231 if ((Flags & object::BasicSymbolRef::SF_Weak) &&
232 (Flags & object::BasicSymbolRef::SF_Indirect)) {
233 std::string FallbackName;
236 OS, cast<GlobalValue>(
237 cast<GlobalAlias>(GV)->getAliasee()->stripPointerCasts()));
239 setStr(Uncommon().COFFWeakExternFallbackName, Saver.save(FallbackName));
251 setStr(Hdr.
Producer, kExpectedProducerName);
254 TT =
Triple(IRMods[0]->getTargetTriple());
256 for (
auto *M : IRMods)
257 if (
Error Err = addModule(M))
260 COFFLinkerOptsOS.flush();
267 writeRange(Hdr.
Comdats, Comdats);
280 return Builder(Symtab, StrtabBuilder, Alloc).build(Mods);
289 std::vector<Module *> Mods;
290 std::vector<std::unique_ptr<Module>> OwnedMods;
291 for (
auto BM : BMs) {
293 BM.getLazyModule(Ctx,
true,
298 Mods.push_back(MOrErr->
get());
299 OwnedMods.push_back(std::move(*MOrErr));
313 return std::move(FC);
317 if (BFC.
Mods.empty())
318 return make_error<StringError>(
"Bitcode file does not contain any modules",
333 Producer != kExpectedProducerName)
347 return std::move(FC);
Tracking metadata reference owned by Metadata.
static Expected< FileContents > upgrade(ArrayRef< BitcodeModule > BMs)
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue * > &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Contains the information needed by linkers for symbol resolution, as well as by the LTO implementatio...
A Module instance is used to store all the information related to an LLVM module. ...
bool canBeOmittedFromSymbolTable(const GlobalValue *GV)
True if GV can be left out of the object symbol table.
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
A raw_ostream that writes to an SmallVector or SmallString.
Error takeError()
Take ownership of the stored error.
StringRef StrtabForSymtab
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
A reference to a range of objects in the symbol table.
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
The contents of the irsymtab in a bitcode file.
Tagged union holding either a T or a Error.
void addModule(Module *M)
Error build(ArrayRef< Module * > Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
Utility for building string tables with deduplicated suffixes.
SmallVector< char, 0 > Symtab
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
A reference to a string in the string table.
Describes the range of a particular module's symbols within the symbol table.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void finalizeInOrder()
Finalize the string table without reording it.
This is an important class for using LLVM in a threaded context.
Allocate memory in an ever growing pool, as if by bump-pointer.
const Comdat * getComdat() const
Str Name
The mangled symbol name.
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
bool empty() const
empty - Check if the array is empty.
This data structure contains rarely used symbol fields and is optionally referenced by a Symbol...
SmallVector< char, 0 > Strtab
Triple - Helper class for working with autoconf configuration names.
static ErrorSuccess success()
Create a success value.
void write(raw_ostream &OS) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Module.h This file contains the declarations for the Module class.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
reference get()
Returns a reference to the stored T value.
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
size_t getNumModules() const
uint32_t getSymbolFlags(Symbol S) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
static std::vector< std::string > Flags
Saves strings in the inheritor's stable storage and returns a StringRef with a stable character point...
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
pointer data()
Return a pointer to the vector's buffer, even if empty().
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
ArrayRef< Symbol > symbols() const
op_range operands() const
StringRef get(StringRef Strtab) const
void printSymbolName(raw_ostream &OS, Symbol S) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
LLVM Value Representation.
This is equivalent to an IR comdat.
Lightweight error class with error context and mandatory checking.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef - Represent a constant reference to a string, i.e.
const std::string & getDataLayoutStr() const
Get the data layout string for the module's target platform.
Word UncBegin
The index of the first Uncommon for this Module.
llvm::Error materializeMetadata()
std::vector< BitcodeModule > Mods
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.