29 I((InitList && End) ? InitList->getNumOperands() : 0) {
33 assert(InitList ==
Other.InitList &&
"Incomparable iterators.");
38 return !(*
this ==
Other);
54 assert(CS &&
"Unrecognized type in llvm.global_ctors/llvm.global_dtors");
61 if (
Function *
F = dyn_cast_or_null<Function>(FuncC)) {
64 }
else if (
ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
66 FuncC = CE->getOperand(0);
75 auto *Priority = cast<ConstantInt>(CS->
getOperand(0));
79 return Element(Priority->getZExtValue(), Func,
Data);
83 const GlobalVariable *CtorsList = M.getNamedGlobal(
"llvm.global_ctors");
89 const GlobalVariable *DtorsList = M.getNamedGlobal(
"llvm.global_dtors");
94bool StaticInitGVIterator::isStaticInitGlobal(
GlobalValue &GV) {
99 GV.
getName() ==
"llvm.global_dtors"))
115 if (CtorDtors.
empty())
120 (*CtorDtors.
begin()).Func->getParent()->getDataLayout());
122 for (
auto CtorDtor : CtorDtors) {
123 assert(CtorDtor.Func && CtorDtor.Func->hasName() &&
124 "Ctor/Dtor function must be named to be runnable under the JIT");
127 if (CtorDtor.Func->hasLocalLinkage()) {
132 if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
133 dbgs() <<
" Skipping because why now?\n";
137 CtorDtorsByPriority[CtorDtor.Priority].push_back(
138 Mangle(CtorDtor.Func->getName()));
143 using CtorDtorTy = void (*)();
146 for (
auto &KV : CtorDtorsByPriority)
147 for (
auto &
Name : KV.second)
150 "Ctor/Dtor list contains duplicates");
153 if (
auto CtorDtorMap = ES.lookup(
155 std::move(LookupSet))) {
156 for (
auto &KV : CtorDtorsByPriority) {
157 for (
auto &
Name : KV.second) {
158 assert(CtorDtorMap->count(
Name) &&
"No entry for Name");
159 auto CtorDtor =
reinterpret_cast<CtorDtorTy
>(
160 static_cast<uintptr_t
>((*CtorDtorMap)[
Name].getAddress()));
164 CtorDtorsByPriority.clear();
167 return CtorDtorMap.takeError();
172 for (
auto &
P : CXXDestructorDataPairs)
174 CXXDestructorDataPairs.clear();
180 auto& CXXDestructorDataPairs =
182 CXXDestructorDataPairs.push_back(std::make_pair(Destructor,
Arg));
189 RuntimeInterposes[Mangle(
"__dso_handle")] =
192 RuntimeInterposes[Mangle(
"__cxa_atexit")] =
201 std::lock_guard<std::mutex> Lock(AtExitsMutex);
202 AtExitRecords[DSOHandle].push_back({
F, Ctx});
206 std::vector<AtExitRecord> AtExitsToRun;
209 std::lock_guard<std::mutex> Lock(AtExitsMutex);
210 auto I = AtExitRecords.find(DSOHandle);
211 if (
I != AtExitRecords.end()) {
212 AtExitsToRun = std::move(
I->second);
213 AtExitRecords.erase(
I);
217 while (!AtExitsToRun.empty()) {
218 AtExitsToRun.back().F(AtExitsToRun.back().Ctx);
219 AtExitsToRun.pop_back();
235 return std::make_unique<DynamicLibrarySearchGenerator>(
244 bool HasGlobalPrefix = (GlobalPrefix !=
'\0');
246 for (
auto &KV : Symbols) {
247 auto &
Name = KV.first;
252 if (Allow && !Allow(
Name))
255 if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
258 std::string Tmp((*Name).data() + HasGlobalPrefix,
259 (*Name).size() - HasGlobalPrefix);
267 if (NewSymbols.
empty())
283 if (isa<object::Archive>(
B->getBinary())) {
284 auto [Archive, ArchiveBuffer] =
B->takeBinary();
285 return Create(L, std::move(ArchiveBuffer),
286 std::unique_ptr<object::Archive>(
288 std::move(GetObjFileInterface));
293 if (
auto *UB = cast<object::MachOUniversalBinary>(
B->getBinary())) {
295 const auto &TT = L.getExecutionSession().getTargetTriple();
297 auto SliceRange = getSliceRangeForArch(*UB, TT);
299 return SliceRange.takeError();
304 return make_error<StringError>(
305 Twine(
"Could not create buffer for ") + TT.str() +
" slice of " +
306 FileName +
": [ " +
formatv(
"{0:x}", SliceRange->first) +
" .. " +
307 formatv(
"{0:x}", SliceRange->first + SliceRange->second) +
": " +
308 SliceBuffer.getError().message(),
309 SliceBuffer.getError());
311 return Create(L, std::move(*SliceBuffer), std::move(GetObjFileInterface));
314 return make_error<StringError>(
Twine(
"Unrecognized file type for ") +
321 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
322 std::unique_ptr<object::Archive> Archive,
327 std::unique_ptr<StaticLibraryDefinitionGenerator> ADG(
329 L, std::move(ArchiveBuffer), std::move(Archive),
330 std::move(GetObjFileInterface), Err));
333 return std::move(Err);
335 return std::move(ADG);
340 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
345 return B.takeError();
348 if (isa<object::Archive>(*
B))
349 return Create(L, std::move(ArchiveBuffer),
350 std::unique_ptr<object::Archive>(
352 std::move(GetObjFileInterface));
356 if (
auto *UB = cast<object::MachOUniversalBinary>(
B->get())) {
358 const auto &TT = L.getExecutionSession().getTargetTriple();
360 auto SliceRange = getSliceRangeForArch(*UB, TT);
362 return SliceRange.takeError();
365 StringRef(ArchiveBuffer->getBufferStart() + SliceRange->first,
367 ArchiveBuffer->getBufferIdentifier());
371 return Archive.takeError();
373 return Create(L, std::move(ArchiveBuffer), std::move(*Archive),
374 std::move(GetObjFileInterface));
377 return make_error<StringError>(
Twine(
"Unrecognized file type for ") +
378 ArchiveBuffer->getBufferIdentifier(),
396 for (
const auto &KV : Symbols) {
397 const auto &
Name = KV.first;
398 if (!ObjectFilesMap.count(
Name))
400 auto ChildBuffer = ObjectFilesMap[
Name];
402 {ChildBuffer.getBuffer(), ChildBuffer.getBufferIdentifier()});
405 for (
auto ChildBufferInfo : ChildBufferInfos) {
407 ChildBufferInfo.second);
411 return I.takeError();
421Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
425 for (
auto &S : Archive->symbols()) {
427 auto Member = S.getMember();
429 return Member.takeError();
430 auto DataOffset = Member->getDataOffset();
431 if (!Visited.
count(DataOffset)) {
432 Visited.
insert(DataOffset);
433 auto Child = Member->getAsBinary();
435 return Child.takeError();
436 if ((*Child)->isCOFFImportFile()) {
437 ImportedDynamicLibraries.insert((*Child)->getFileName().str());
438 Excluded.
insert(DataOffset);
441 MemoryBuffers[DataOffset] = (*Child)->getMemoryBufferRef();
443 if (!Excluded.
count(DataOffset))
445 MemoryBuffers[DataOffset];
451Expected<std::pair<size_t, size_t>>
452StaticLibraryDefinitionGenerator::getSliceRangeForArch(
453 object::MachOUniversalBinary &UB,
const Triple &TT) {
455 for (
const auto &Obj : UB.objects()) {
456 auto ObjTT = Obj.getTriple();
457 if (ObjTT.getArch() ==
TT.getArch() &&
458 ObjTT.getSubArch() ==
TT.getSubArch() &&
460 ObjTT.getVendor() ==
TT.getVendor())) {
462 return std::make_pair(Obj.getOffset(), Obj.getSize());
466 return make_error<StringError>(Twine(
"Universal binary ") + UB.getFileName() +
467 " does not contain a slice for " +
472StaticLibraryDefinitionGenerator::StaticLibraryDefinitionGenerator(
473 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
474 std::unique_ptr<object::Archive> Archive,
475 GetObjectFileInterface GetObjFileInterface, Error &Err)
476 :
L(
L), GetObjFileInterface(
std::
move(GetObjFileInterface)),
477 ArchiveBuffer(
std::
move(ArchiveBuffer)), Archive(
std::
move(Archive)) {
478 ErrorAsOutParameter
_(&Err);
479 if (!this->GetObjFileInterface)
482 Err = buildObjectFilesMap();
485std::unique_ptr<DLLImportDefinitionGenerator>
488 return std::unique_ptr<DLLImportDefinitionGenerator>(
497 LinkOrder.reserve(LO.size());
498 for (
auto &KV : LO) {
501 LinkOrder.push_back(KV);
510 for (
auto &KV : Symbols) {
515 if (ToLookUpSymbols.
count(Deinterned) &&
518 ToLookUpSymbols[Deinterned] = KV.second;
521 for (
auto &KV : ToLookUpSymbols)
522 LookupSet.
add(ES.
intern(KV.first), KV.second);
531 return G.takeError();
532 return L.add(JD, std::move(*
G));
536DLLImportDefinitionGenerator::getTargetPointerSize(
const Triple &TT) {
537 switch (TT.getArch()) {
541 return make_error<StringError>(
542 "architecture unsupported by DLLImportDefinitionGenerator",
547Expected<support::endianness>
548DLLImportDefinitionGenerator::getTargetEndianness(
const Triple &TT) {
549 switch (TT.getArch()) {
551 return support::endianness::little;
553 return make_error<StringError>(
554 "architecture unsupported by DLLImportDefinitionGenerator",
559Expected<std::unique_ptr<jitlink::LinkGraph>>
569 auto G = std::make_unique<jitlink::LinkGraph>(
570 "<DLLIMPORT_STUBS>", TT, *PointerSize, *Endianness,
572 jitlink::Section &Sec =
576 jitlink::Symbol &
Target =
G->addAbsoluteSymbol(
577 *KV.first, ExecutorAddr(KV.second.getAddress()), *PointerSize,
581 jitlink::Symbol &
Ptr =
583 auto NameCopy =
G->allocateContent(Twine(getImpPrefix()) + *KV.first);
584 StringRef NameCopyRef = StringRef(NameCopy.data(), NameCopy.size());
585 Ptr.setName(NameCopyRef);
591 jitlink::Block &StubBlock =
593 G->addDefinedSymbol(StubBlock, 0, *KV.first, StubBlock.getSize(),
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ConstantArray - Constant Array Declarations.
A constant value that is initialized with an expression using other constant values.
This is an important base class in LLVM.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Implements a dense probed hash-table based set.
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.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
StringRef getSection() const
@ HiddenVisibility
The GV is hidden.
@ ExternalLinkage
Externally visible function.
Represents a symbol that has been evaluated to an address already.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a MemoryBuffer.
A Module instance is used to store all the information related to an LLVM module.
StringRef - Represent a constant reference to a string, i.e.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
bool startswith(StringRef Prefix) const
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
StringRef getName() const
Return a constant reference to the value's name.
std::pair< iterator, bool > insert(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
A range adaptor for a pair of iterators.
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
This iterator provides a convenient way to iterate over the elements of an llvm.global_ctors/llvm....
bool operator!=(const CtorDtorIterator &Other) const
Test iterators for inequality.
Element operator*() const
Dereference iterator.
CtorDtorIterator(const GlobalVariable *GV, bool End)
Construct an iterator instance.
CtorDtorIterator & operator++()
Pre-increment iterator.
bool operator==(const CtorDtorIterator &Other) const
Test iterators for equality.
void add(iterator_range< CtorDtorIterator > CtorDtors)
A utility class to create COFF dllimport GOT symbols (__imp_*) and PLT stubs.
static std::unique_ptr< DLLImportDefinitionGenerator > Create(ExecutionSession &ES, ObjectLinkingLayer &L)
Creates a DLLImportDefinitionGenerator instance.
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
std::function< bool(const SymbolStringPtr &)> SymbolPredicate
DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow=SymbolPredicate())
Create a DynamicLibrarySearchGenerator that searches for symbols in the given sys::DynamicLibrary.
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
static Expected< std::unique_ptr< DynamicLibrarySearchGenerator > > Load(const char *FileName, char GlobalPrefix, SymbolPredicate Allow=SymbolPredicate())
Permanently loads the library at the given path and, on success, returns a DynamicLibrarySearchGenera...
An ExecutionSession represents a running JIT program.
const Triple & getTargetTriple() const
Return the triple for the executor.
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
void runAtExits(void *DSOHandle)
void registerAtExit(void(*F)(void *), void *Ctx, void *DSOHandle)
Represents a JIT'd dynamic library.
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
void(*)(void *) DestructorPtr
JITTargetAddress toTargetAddress(PtrTy *P)
static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg, void *DSOHandle)
std::vector< CXXDestructorDataPair > CXXDestructorDataPairList
CXXDestructorDataPairList DSOHandleOverride
void runDestructors()
Run any destructors recorded by the overriden __cxa_atexit function (CXAAtExitOverride).
Error enable(JITDylib &JD, MangleAndInterner &Mangler)
Wraps state for a lookup-in-progress.
Mangles symbol names then uniques them in the context of an ExecutionSession.
Interface for Layers that accept object files.
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the JITDylib for the giv...
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
An ObjectLayer implementation built on JITLink.
A utility class to expose symbols from a static library.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibraryDefinitionGenerator from the given path.
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > ArchiveBuffer, std::unique_ptr< object::Archive > Archive, GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibrarySearchGenerator from the given memory buffer and Archive object.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
bool containsDuplicates()
Returns true if this set contains any duplicates.
This class provides a portable interface to dynamic libraries which also might be known as shared lib...
static DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path using the library load operatio...
void * getAddressOfSymbol(const char *symbolName)
Searches through the library for the symbol symbolName.
constexpr uint64_t PointerSize
aarch64 pointer size.
Symbol & createAnonymousPointer(LinkGraph &G, Section &PointerSection, Symbol *InitialTarget=nullptr, uint64_t InitialAddend=0)
Creates a new pointer block in the given section and returns an anonymous symbol pointing to it.
Block & createPointerJumpStubBlock(LinkGraph &G, Section &StubSection, Symbol &PointerSymbol)
Create a jump stub block that jumps via the pointer at the given symbol.
const char * getGenericEdgeKindName(Edge::Kind K)
Returns the string name of the given generic edge kind, or "unknown" otherwise.
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
DenseMap< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Expected< MaterializationUnit::Interface > getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer)
Returns a MaterializationUnit::Interface for the object file contained in the given buffer,...
LookupKind
Describes the kind of lookup being performed.
@ Resolved
Queried, materialization begun.
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto dyn_cast_or_null(const Y &Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Accessor for an element of the global_ctors/global_dtors array.