17 #include "llvm/Config/llvm-config.h" 34 #include "llvm/Support/VCSRevision.h" 42 using namespace irsymtab;
45 #define HANDLE_LIBCALL(code, name) name, 46 #include "llvm/IR/RuntimeLibcalls.def" 52 const char *getExpectedProducerName() {
53 static char DefaultName[] = LLVM_VERSION_STRING
60 if (
char *OverrideName = getenv(
"LLVM_OVERRIDE_PRODUCER"))
65 const char *kExpectedProducerName = getExpectedProducerName();
78 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {}
84 std::vector<storage::Comdat> Comdats;
85 std::vector<storage::Module> Mods;
86 std::vector<storage::Symbol> Syms;
87 std::vector<storage::Uncommon> Uncommons;
89 std::string COFFLinkerOpts;
92 std::vector<storage::Str> DependentLibraries;
102 R.
Size = Objs.size();
103 Symtab.
insert(Symtab.
end(),
reinterpret_cast<const char *
>(Objs.data()),
104 reinterpret_cast<const char *>(Objs.data() + Objs.size()));
119 return make_error<StringError>(
"input module has no datalayout",
129 Mod.
Begin = Syms.size();
130 Mod.
End = Syms.size() + Msymtab.
symbols().size();
134 if (
TT.isOSBinFormatCOFF()) {
140 for (
const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands())
141 COFFLinkerOptsOS <<
" " << cast<MDString>(MDOption)->getString();
145 if (
TT.isOSBinFormatELF()) {
149 for (
MDNode *MDOptions :
N->operands()) {
150 const auto OperandStr =
151 cast<MDString>(cast<MDNode>(MDOptions)->getOperand(0))->getString();
153 setStr(Specifier, OperandStr);
154 DependentLibraries.emplace_back(Specifier);
160 if (
Error Err = addSymbol(Msymtab, Used, Msym))
167 auto P = ComdatMap.insert(std::make_pair(C, Comdats.size()));
170 if (
TT.isOSBinFormatCOFF()) {
173 return make_error<StringError>(
"Could not find leader",
178 P.first->second = -1;
182 Mang.getNameWithPrefix(OS, GV,
false);
188 setStr(Comdat.
Name, Saver.save(Name));
189 Comdats.push_back(Comdat);
192 return P.first->second;
207 Uncommons.emplace_back();
208 Unc = &Uncommons.back();
242 if (Flags & object::BasicSymbolRef::SF_Undefined)
248 setStr(Sym.
IRName, GV->getName());
250 bool IsBuiltinFunc =
false;
252 for (
const char *LibcallName : LibcallRoutineNames)
253 if (GV->getName() == LibcallName)
254 IsBuiltinFunc =
true;
256 if (Used.
count(GV) || IsBuiltinFunc)
258 if (GV->isThreadLocal())
260 if (GV->hasGlobalUnnamedAddr())
262 if (GV->canBeOmittedFromSymbolTable())
266 if (Flags & object::BasicSymbolRef::SF_Common) {
267 Uncommon().CommonSize = GV->getParent()->getDataLayout().getTypeAllocSize(
268 GV->getType()->getElementType());
269 Uncommon().CommonAlign = GV->getAlignment();
274 return make_error<StringError>(
"Unable to determine comdat of alias!",
277 Expected<int> ComdatIndexOrErr = getComdatIndex(C, GV->getParent());
278 if (!ComdatIndexOrErr)
283 if (
TT.isOSBinFormatCOFF()) {
286 if ((Flags & object::BasicSymbolRef::SF_Weak) &&
287 (Flags & object::BasicSymbolRef::SF_Indirect)) {
289 cast<GlobalAlias>(GV)->getAliasee()->stripPointerCasts());
291 return make_error<StringError>(
"Invalid weak external",
293 std::string FallbackName;
297 setStr(Uncommon().COFFWeakExternFallbackName, Saver.save(FallbackName));
302 setStr(Uncommon().SectionName, Saver.save(Base->
getSection()));
312 setStr(Hdr.
Producer, kExpectedProducerName);
315 TT =
Triple(IRMods[0]->getTargetTriple());
317 for (
auto *M : IRMods)
318 if (
Error Err = addModule(M))
321 COFFLinkerOptsOS.flush();
328 writeRange(Hdr.
Comdats, Comdats);
341 return Builder(Symtab, StrtabBuilder, Alloc).build(Mods);
350 std::vector<Module *> Mods;
351 std::vector<std::unique_ptr<Module>> OwnedMods;
352 for (
auto BM : BMs) {
354 BM.getLazyModule(Ctx,
true,
359 Mods.push_back(MOrErr->
get());
360 OwnedMods.push_back(std::move(*MOrErr));
374 return std::move(FC);
378 if (BFC.
Mods.empty())
379 return make_error<StringError>(
"Bitcode file does not contain any modules",
391 unsigned Version = Hdr->Version;
394 Producer != kExpectedProducerName)
408 return std::move(FC);
StringRef getSection() const
Get the custom section of this global if it has one.
Tracking metadata reference owned by Metadata.
bool hasLocalLinkage() const
uint32_t getSymbolFlags(Symbol S) const
static Expected< FileContents > upgrade(ArrayRef< BitcodeModule > BMs)
This class represents lattice values for constants.
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. ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
A raw_ostream that writes to an SmallVector or SmallString.
const std::string & getDataLayoutStr() const
Get the data layout string for the module's target platform.
Str SectionName
Specified section name, if any.
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.
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
size_t getNumModules() const
The contents of the irsymtab in a bitcode file.
ArrayRef< Symbol > symbols() const
Tagged union holding either a T or a Error.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
void addModule(Module *M)
void write(raw_ostream &OS) const
Utility for building string tables with deduplicated suffixes.
op_range operands() const
size_t add(CachedHashStringRef S)
Add a string to the builder.
SmallVector< char, 0 > Symtab
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
A reference to a string in the string table.
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.
Describes the range of a particular module's symbols within the symbol table.
StringRef getName() const
* 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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Str Name
The mangled symbol name.
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
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.
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 ...
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.
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.
The access may modify the value stored in memory.
void printSymbolName(raw_ostream &OS, Symbol S) const
iterator insert(iterator I, T &&Elt)
const Comdat * getComdat() const
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
pointer data()
Return a pointer to the vector's buffer, even if empty().
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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.
StringRef - Represent a constant reference to a string, i.e.
Word UncBegin
The index of the first Uncommon for this Module.
llvm::Error materializeMetadata()
std::vector< BitcodeModule > Mods
bool empty() const
empty - Check if the array is empty.
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 or more pointer types, with the discriminator in the low bit of the poin...
static const char * LibcallRoutineNames[]
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.