16#include "llvm/Config/llvm-config.h"
32#include "llvm/Support/VCSRevision.h"
45 cl::desc(
"Disable automatic bitcode upgrade for version mismatch"));
49const char *getExpectedProducerName() {
50 static char DefaultName[] = LLVM_VERSION_STRING
57 if (
char *OverrideName = getenv(
"LLVM_OVERRIDE_PRODUCER"))
62const char *kExpectedProducerName = getExpectedProducerName();
66 SmallVector<char, 0> &Symtab;
67 StringTableBuilder &StrtabBuilder;
73 Builder(SmallVector<char, 0> &Symtab, StringTableBuilder &StrtabBuilder,
75 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(
Alloc), TT(TT) {}
77 DenseMap<const Comdat *, int> ComdatMap;
81 std::vector<storage::Comdat> Comdats;
82 std::vector<storage::Module> Mods;
83 std::vector<storage::Symbol> Syms;
84 std::vector<storage::Uncommon> Uncommons;
86 std::string COFFLinkerOpts;
87 raw_string_ostream COFFLinkerOptsOS{COFFLinkerOpts};
89 std::vector<storage::Str> DependentLibraries;
91 void setStr(storage::Str &S, StringRef
Value) {
97 void writeRange(storage::Range<T> &R,
const std::vector<T> &Objs) {
98 R.Offset = Symtab.size();
100 Symtab.insert(Symtab.end(),
reinterpret_cast<const char *
>(Objs.data()),
101 reinterpret_cast<const char *
>(Objs.data() + Objs.size()));
104 Expected<int> getComdatIndex(
const Comdat *
C,
const Module *M);
108 const SmallPtrSet<GlobalValue *, 4> &Used,
115 if (
M->getDataLayoutStr().empty())
135 ModuleSymbolTable Msymtab;
139 Mod.Begin = Syms.size();
140 Mod.End = Syms.size() + Msymtab.
symbols().size();
141 Mod.UncBegin = Uncommons.size();
144 if (
TT.isOSBinFormatCOFF()) {
145 if (
auto E =
M->materializeMetadata())
147 if (NamedMDNode *LinkerOptions =
148 M->getNamedMetadata(
"llvm.linker.options")) {
149 for (MDNode *MDOptions : LinkerOptions->operands())
150 for (
const MDOperand &MDOption :
cast<MDNode>(MDOptions)->operands())
155 if (
TT.isOSBinFormatELF()) {
156 if (
auto E =
M->materializeMetadata())
158 if (NamedMDNode *
N =
M->getNamedMetadata(
"llvm.dependent-libraries")) {
159 for (MDNode *MDOptions :
N->operands()) {
160 const auto OperandStr =
163 setStr(Specifier, OperandStr);
164 DependentLibraries.emplace_back(Specifier);
176Expected<int> Builder::getComdatIndex(
const Comdat *
C,
const Module *M) {
177 auto P = ComdatMap.
insert(std::make_pair(
C, Comdats.size()));
180 if (
TT.isOSBinFormatCOFF()) {
181 const GlobalValue *GV =
M->getNamedValue(
C->getName());
188 P.first->second = -1;
191 llvm::raw_string_ostream OS(Name);
194 Name = std::string(
C->getName());
197 storage::Comdat Comdat;
198 setStr(Comdat.
Name, Saver.
save(Name));
200 Comdats.push_back(Comdat);
203 return P.first->second;
206Error Builder::addSymbol(
const ModuleSymbolTable &Msymtab,
207 const SmallPtrSet<GlobalValue *, 4> &Used,
210 storage::Symbol &Sym = Syms.back();
213 storage::Uncommon *Unc =
nullptr;
214 auto Uncommon = [&]() -> storage::Uncommon & {
218 Uncommons.emplace_back();
219 Unc = &Uncommons.back();
226 SmallString<64>
Name;
228 raw_svector_ostream OS(Name);
259 StringRef GVName = GV->
getName();
260 setStr(Sym.
IRName, GVName);
277 Uncommon().CommonSize = GVar->getGlobalSize(GV->
getDataLayout());
278 Uncommon().CommonAlign = GVar->getAlign() ? GVar->getAlign()->value() : 0;
290 Expected<int> ComdatIndexOrErr = getComdatIndex(
C, GV->
getParent());
291 if (!ComdatIndexOrErr)
296 if (
TT.isOSBinFormatCOFF()) {
306 std::string FallbackName;
307 raw_string_ostream OS(FallbackName);
309 setStr(Uncommon().COFFWeakExternFallbackName, Saver.
save(FallbackName));
324 setStr(Hdr.
Producer, kExpectedProducerName);
325 setStr(Hdr.
TargetTriple, IRMods[0]->getTargetTriple().str());
328 for (
auto *M : IRMods)
329 if (
Error Err = addModule(M))
336 Symtab.
resize(
sizeof(storage::Header));
338 writeRange(Hdr.
Comdats, Comdats);
342 *
reinterpret_cast<storage::Header *
>(Symtab.
data()) = Hdr;
351 const Triple &TT = Mods[0]->getTargetTriple();
352 return Builder(Symtab, StrtabBuilder,
Alloc, TT).build(Mods);
361 std::vector<Module *> Mods;
362 std::vector<std::unique_ptr<Module>> OwnedMods;
363 for (
auto BM : BMs) {
365 BM.getLazyModule(Ctx,
true,
370 Mods.push_back(MOrErr->
get());
371 OwnedMods.push_back(std::move(*MOrErr));
380 FC.Strtab.resize(StrtabBuilder.
getSize());
383 FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()},
384 {FC.Strtab.data(), FC.Strtab.size()}};
385 return std::move(FC);
389 if (BFC.
Mods.empty())
406 Producer != kExpectedProducerName)
418 if (FC.TheReader.getNumModules() != BFC.
Mods.size())
421 return std::move(FC);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
static cl::opt< bool > DisableBitcodeVersionUpgrade("disable-bitcode-version-upgrade", cl::Hidden, cl::desc("Disable automatic bitcode upgrade for version mismatch"))
static Expected< FileContents > upgrade(ArrayRef< BitcodeModule > BMs)
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
This file defines the SmallPtrSet class.
This file defines the SmallString class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
Check if the array is empty.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
StringRef getSection() const
Get the custom section of this global if it has one.
const Comdat * getComdat() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
VisibilityTypes getVisibility() const
bool hasLocalLinkage() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI const GlobalObject * getAliaseeObject() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
bool hasGlobalUnnamedAddr() const
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
This is an important class for using LLVM in a threaded context.
LLVM_ABI void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
LLVM_ABI void addModule(Module *M)
LLVM_ABI void printSymbolName(raw_ostream &OS, Symbol S) const
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
LLVM_ABI uint32_t getSymbolFlags(Symbol S) const
ArrayRef< Symbol > symbols() const
A Module instance is used to store all the information related to an LLVM module.
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
Get the string size.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
StringRef save(const char *S)
Utility for building string tables with deduplicated suffixes.
LLVM_ABI void finalizeInOrder()
Finalize the string table without reording it.
LLVM_ABI void write(raw_ostream &OS) const
Triple - Helper class for working with autoconf configuration names.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
LLVM_ABI Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
LLVM_ABI 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.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
constexpr from_range_t from_range
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
StringRef StrtabForSymtab
std::vector< BitcodeModule > Mods
The contents of the irsymtab in a bitcode file.
StringRef get(StringRef Strtab) const
Str Name
The mangled symbol name.
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
Str SectionName
Specified section name, if any.
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.