18 #include "llvm/Config/llvm-config.h" 35 #include "llvm/Support/VCSRevision.h" 43 using namespace irsymtab;
46 #define HANDLE_LIBCALL(code, name) name, 47 #include "llvm/IR/RuntimeLibcalls.def" 53 const char *getExpectedProducerName() {
54 static char DefaultName[] = LLVM_VERSION_STRING
61 if (
char *OverrideName = getenv(
"LLVM_OVERRIDE_PRODUCER"))
66 const char *kExpectedProducerName = getExpectedProducerName();
79 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {}
85 std::vector<storage::Comdat> Comdats;
86 std::vector<storage::Module> Mods;
87 std::vector<storage::Symbol> Syms;
88 std::vector<storage::Uncommon> Uncommons;
90 std::string COFFLinkerOpts;
101 R.
Size = Objs.size();
102 Symtab.
insert(Symtab.
end(),
reinterpret_cast<const char *
>(Objs.data()),
103 reinterpret_cast<const char *>(Objs.data() + Objs.size()));
118 return make_error<StringError>(
"input module has no datalayout",
128 Mod.
Begin = Syms.size();
129 Mod.
End = Syms.size() + Msymtab.
symbols().size();
133 if (TT.isOSBinFormatCOFF()) {
139 for (
const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands())
140 COFFLinkerOptsOS <<
" " << cast<MDString>(MDOption)->getString();
145 if (
Error Err = addSymbol(Msymtab, Used, Msym))
152 auto P = ComdatMap.insert(std::make_pair(C, Comdats.size()));
155 if (TT.isOSBinFormatCOFF()) {
158 return make_error<StringError>(
"Could not find leader",
163 P.first->second = -1;
167 Mang.getNameWithPrefix(OS, GV,
false);
173 setStr(Comdat.
Name, Saver.save(Name));
174 Comdats.push_back(Comdat);
177 return P.first->second;
192 Uncommons.emplace_back();
193 Unc = &Uncommons.back();
227 if (Flags & object::BasicSymbolRef::SF_Undefined)
233 setStr(Sym.
IRName, GV->getName());
235 bool IsBuiltinFunc =
false;
237 for (
const char *LibcallName : LibcallRoutineNames)
238 if (GV->getName() == LibcallName)
239 IsBuiltinFunc =
true;
241 if (Used.
count(GV) || IsBuiltinFunc)
243 if (GV->isThreadLocal())
245 if (GV->hasGlobalUnnamedAddr())
247 if (GV->canBeOmittedFromSymbolTable())
251 if (Flags & object::BasicSymbolRef::SF_Common) {
252 Uncommon().CommonSize = GV->getParent()->getDataLayout().getTypeAllocSize(
253 GV->getType()->getElementType());
254 Uncommon().CommonAlign = GV->getAlignment();
259 return make_error<StringError>(
"Unable to determine comdat of alias!",
262 Expected<int> ComdatIndexOrErr = getComdatIndex(C, GV->getParent());
263 if (!ComdatIndexOrErr)
268 if (TT.isOSBinFormatCOFF()) {
271 if ((Flags & object::BasicSymbolRef::SF_Weak) &&
272 (Flags & object::BasicSymbolRef::SF_Indirect)) {
274 cast<GlobalAlias>(GV)->getAliasee()->stripPointerCasts());
276 return make_error<StringError>(
"Invalid weak external",
278 std::string FallbackName;
282 setStr(Uncommon().COFFWeakExternFallbackName, Saver.save(FallbackName));
287 setStr(Uncommon().SectionName, Saver.save(Base->
getSection()));
297 setStr(Hdr.
Producer, kExpectedProducerName);
300 TT =
Triple(IRMods[0]->getTargetTriple());
302 for (
auto *M : IRMods)
303 if (
Error Err = addModule(M))
306 COFFLinkerOptsOS.flush();
313 writeRange(Hdr.
Comdats, Comdats);
326 return Builder(Symtab, StrtabBuilder, Alloc).build(Mods);
335 std::vector<Module *> Mods;
336 std::vector<std::unique_ptr<Module>> OwnedMods;
337 for (
auto BM : BMs) {
339 BM.getLazyModule(Ctx,
true,
344 Mods.push_back(MOrErr->
get());
345 OwnedMods.push_back(std::move(*MOrErr));
359 return std::move(FC);
363 if (BFC.
Mods.empty())
364 return make_error<StringError>(
"Bitcode file does not contain any modules",
376 unsigned Version = Hdr->Version;
379 Producer != kExpectedProducerName)
393 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. ...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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.
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).
A reference to a range of objects in the symbol table.
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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.
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
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)...
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...
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
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...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
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)
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 pointer types, with the discriminator in the low bit of the pointer...
static const char * LibcallRoutineNames[]
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.