LLVM 18.0.0git
ExecutionUtils.cpp
Go to the documentation of this file.
1//===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
21#include <string>
22
23namespace llvm {
24namespace orc {
25
27 : InitList(
28 GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
29 I((InitList && End) ? InitList->getNumOperands() : 0) {
30}
31
33 assert(InitList == Other.InitList && "Incomparable iterators.");
34 return I == Other.I;
35}
36
38 return !(*this == Other);
39}
40
42 ++I;
43 return *this;
44}
45
47 CtorDtorIterator Temp = *this;
48 ++I;
49 return Temp;
50}
51
53 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
54 assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors");
55
56 Constant *FuncC = CS->getOperand(1);
57 Function *Func = nullptr;
58
59 // Extract function pointer, pulling off any casts.
60 while (FuncC) {
61 if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
62 Func = F;
63 break;
64 } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
65 if (CE->isCast())
66 FuncC = CE->getOperand(0);
67 else
68 break;
69 } else {
70 // This isn't anything we recognize. Bail out with Func left set to null.
71 break;
72 }
73 }
74
75 auto *Priority = cast<ConstantInt>(CS->getOperand(0));
76 Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
77 if (Data && !isa<GlobalValue>(Data))
78 Data = nullptr;
79 return Element(Priority->getZExtValue(), Func, Data);
80}
81
83 const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
84 return make_range(CtorDtorIterator(CtorsList, false),
85 CtorDtorIterator(CtorsList, true));
86}
87
89 const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
90 return make_range(CtorDtorIterator(DtorsList, false),
91 CtorDtorIterator(DtorsList, true));
92}
93
94bool StaticInitGVIterator::isStaticInitGlobal(GlobalValue &GV) {
95 if (GV.isDeclaration())
96 return false;
97
98 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
99 GV.getName() == "llvm.global_dtors"))
100 return true;
101
102 if (ObjFmt == Triple::MachO) {
103 // FIXME: These section checks are too strict: We should match first and
104 // second word split by comma.
105 if (GV.hasSection() &&
106 (GV.getSection().startswith("__DATA,__objc_classlist") ||
107 GV.getSection().startswith("__DATA,__objc_selrefs")))
108 return true;
109 }
110
111 return false;
112}
113
115 if (CtorDtors.empty())
116 return;
117
118 MangleAndInterner Mangle(
120 (*CtorDtors.begin()).Func->getParent()->getDataLayout());
121
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");
125
126 // FIXME: Maybe use a symbol promoter here instead.
127 if (CtorDtor.Func->hasLocalLinkage()) {
128 CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage);
129 CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility);
130 }
131
132 if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
133 dbgs() << " Skipping because why now?\n";
134 continue;
135 }
136
137 CtorDtorsByPriority[CtorDtor.Priority].push_back(
138 Mangle(CtorDtor.Func->getName()));
139 }
140}
141
143 using CtorDtorTy = void (*)();
144
145 SymbolLookupSet LookupSet;
146 for (auto &KV : CtorDtorsByPriority)
147 for (auto &Name : KV.second)
148 LookupSet.add(Name);
149 assert(!LookupSet.containsDuplicates() &&
150 "Ctor/Dtor list contains duplicates");
151
152 auto &ES = JD.getExecutionSession();
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 = (*CtorDtorMap)[Name].getAddress().toPtr<CtorDtorTy>();
160 CtorDtor();
161 }
162 }
163 CtorDtorsByPriority.clear();
164 return Error::success();
165 } else
166 return CtorDtorMap.takeError();
167}
168
170 auto& CXXDestructorDataPairs = DSOHandleOverride;
171 for (auto &P : CXXDestructorDataPairs)
172 P.first(P.second);
173 CXXDestructorDataPairs.clear();
174}
175
177 void *Arg,
178 void *DSOHandle) {
179 auto& CXXDestructorDataPairs =
180 *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
181 CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
182 return 0;
183}
184
186 MangleAndInterner &Mangle) {
187 SymbolMap RuntimeInterposes;
188 RuntimeInterposes[Mangle("__dso_handle")] = {
190 RuntimeInterposes[Mangle("__cxa_atexit")] = {
192
193 return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
194}
195
196void ItaniumCXAAtExitSupport::registerAtExit(void (*F)(void *), void *Ctx,
197 void *DSOHandle) {
198 std::lock_guard<std::mutex> Lock(AtExitsMutex);
199 AtExitRecords[DSOHandle].push_back({F, Ctx});
200}
201
203 std::vector<AtExitRecord> AtExitsToRun;
204
205 {
206 std::lock_guard<std::mutex> Lock(AtExitsMutex);
207 auto I = AtExitRecords.find(DSOHandle);
208 if (I != AtExitRecords.end()) {
209 AtExitsToRun = std::move(I->second);
210 AtExitRecords.erase(I);
211 }
212 }
213
214 while (!AtExitsToRun.empty()) {
215 AtExitsToRun.back().F(AtExitsToRun.back().Ctx);
216 AtExitsToRun.pop_back();
217 }
218}
219
222 : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
224
227 SymbolPredicate Allow) {
228 std::string ErrMsg;
229 auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
230 if (!Lib.isValid())
231 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
232 return std::make_unique<DynamicLibrarySearchGenerator>(
233 std::move(Lib), GlobalPrefix, std::move(Allow));
234}
235
237 LookupState &LS, LookupKind K, JITDylib &JD,
238 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
239 orc::SymbolMap NewSymbols;
240
241 bool HasGlobalPrefix = (GlobalPrefix != '\0');
242
243 for (auto &KV : Symbols) {
244 auto &Name = KV.first;
245
246 if ((*Name).empty())
247 continue;
248
249 if (Allow && !Allow(Name))
250 continue;
251
252 if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
253 continue;
254
255 std::string Tmp((*Name).data() + HasGlobalPrefix,
256 (*Name).size() - HasGlobalPrefix);
257 if (void *P = Dylib.getAddressOfSymbol(Tmp.c_str()))
259 }
260
261 if (NewSymbols.empty())
262 return Error::success();
263
264 return JD.define(absoluteSymbols(std::move(NewSymbols)));
265}
266
269 ObjectLayer &L, const char *FileName,
270 GetObjectFileInterface GetObjFileInterface) {
271
272 auto B = object::createBinary(FileName);
273 if (!B)
274 return createFileError(FileName, B.takeError());
275
276 // If this is a regular archive then create an instance from it.
277 if (isa<object::Archive>(B->getBinary())) {
278 auto [Archive, ArchiveBuffer] = B->takeBinary();
279 return Create(L, std::move(ArchiveBuffer),
280 std::unique_ptr<object::Archive>(
281 static_cast<object::Archive *>(Archive.release())),
282 std::move(GetObjFileInterface));
283 }
284
285 // If this is a universal binary then search for a slice matching the given
286 // Triple.
287 if (auto *UB = cast<object::MachOUniversalBinary>(B->getBinary())) {
288
289 const auto &TT = L.getExecutionSession().getTargetTriple();
290
291 auto SliceRange = getSliceRangeForArch(*UB, TT);
292 if (!SliceRange)
293 return SliceRange.takeError();
294
295 auto SliceBuffer = MemoryBuffer::getFileSlice(FileName, SliceRange->second,
296 SliceRange->first);
297 if (!SliceBuffer)
298 return make_error<StringError>(
299 Twine("Could not create buffer for ") + TT.str() + " slice of " +
300 FileName + ": [ " + formatv("{0:x}", SliceRange->first) + " .. " +
301 formatv("{0:x}", SliceRange->first + SliceRange->second) + ": " +
302 SliceBuffer.getError().message(),
303 SliceBuffer.getError());
304
305 return Create(L, std::move(*SliceBuffer), std::move(GetObjFileInterface));
306 }
307
308 return make_error<StringError>(Twine("Unrecognized file type for ") +
309 FileName,
311}
312
315 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
316 std::unique_ptr<object::Archive> Archive,
317 GetObjectFileInterface GetObjFileInterface) {
318
319 Error Err = Error::success();
320
321 std::unique_ptr<StaticLibraryDefinitionGenerator> ADG(
323 L, std::move(ArchiveBuffer), std::move(Archive),
324 std::move(GetObjFileInterface), Err));
325
326 if (Err)
327 return std::move(Err);
328
329 return std::move(ADG);
330}
331
334 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
335 GetObjectFileInterface GetObjFileInterface) {
336
337 auto B = object::createBinary(ArchiveBuffer->getMemBufferRef());
338 if (!B)
339 return B.takeError();
340
341 // If this is a regular archive then create an instance from it.
342 if (isa<object::Archive>(*B))
343 return Create(L, std::move(ArchiveBuffer),
344 std::unique_ptr<object::Archive>(
345 static_cast<object::Archive *>(B->release())),
346 std::move(GetObjFileInterface));
347
348 // If this is a universal binary then search for a slice matching the given
349 // Triple.
350 if (auto *UB = cast<object::MachOUniversalBinary>(B->get())) {
351
352 const auto &TT = L.getExecutionSession().getTargetTriple();
353
354 auto SliceRange = getSliceRangeForArch(*UB, TT);
355 if (!SliceRange)
356 return SliceRange.takeError();
357
358 MemoryBufferRef SliceRef(
359 StringRef(ArchiveBuffer->getBufferStart() + SliceRange->first,
360 SliceRange->second),
361 ArchiveBuffer->getBufferIdentifier());
362
363 auto Archive = object::Archive::create(SliceRef);
364 if (!Archive)
365 return Archive.takeError();
366
367 return Create(L, std::move(ArchiveBuffer), std::move(*Archive),
368 std::move(GetObjFileInterface));
369 }
370
371 return make_error<StringError>(Twine("Unrecognized file type for ") +
372 ArchiveBuffer->getBufferIdentifier(),
374}
375
377 LookupState &LS, LookupKind K, JITDylib &JD,
378 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
379 // Don't materialize symbols from static archives unless this is a static
380 // lookup.
381 if (K != LookupKind::Static)
382 return Error::success();
383
384 // Bail out early if we've already freed the archive.
385 if (!Archive)
386 return Error::success();
387
389
390 for (const auto &KV : Symbols) {
391 const auto &Name = KV.first;
392 if (!ObjectFilesMap.count(Name))
393 continue;
394 auto ChildBuffer = ObjectFilesMap[Name];
395 ChildBufferInfos.insert(
396 {ChildBuffer.getBuffer(), ChildBuffer.getBufferIdentifier()});
397 }
398
399 for (auto ChildBufferInfo : ChildBufferInfos) {
400 MemoryBufferRef ChildBufferRef(ChildBufferInfo.first,
401 ChildBufferInfo.second);
402
403 auto I = GetObjFileInterface(L.getExecutionSession(), ChildBufferRef);
404 if (!I)
405 return I.takeError();
406
407 if (auto Err = L.add(JD, MemoryBuffer::getMemBuffer(ChildBufferRef, false),
408 std::move(*I)))
409 return Err;
410 }
411
412 return Error::success();
413}
414
415Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
417 DenseSet<uint64_t> Visited;
418 DenseSet<uint64_t> Excluded;
419 for (auto &S : Archive->symbols()) {
420 StringRef SymName = S.getName();
421 auto Member = S.getMember();
422 if (!Member)
423 return Member.takeError();
424 auto DataOffset = Member->getDataOffset();
425 if (!Visited.count(DataOffset)) {
426 Visited.insert(DataOffset);
427 auto Child = Member->getAsBinary();
428 if (!Child)
429 return Child.takeError();
430 if ((*Child)->isCOFFImportFile()) {
431 ImportedDynamicLibraries.insert((*Child)->getFileName().str());
432 Excluded.insert(DataOffset);
433 continue;
434 }
435 MemoryBuffers[DataOffset] = (*Child)->getMemoryBufferRef();
436 }
437 if (!Excluded.count(DataOffset))
438 ObjectFilesMap[L.getExecutionSession().intern(SymName)] =
439 MemoryBuffers[DataOffset];
440 }
441
442 return Error::success();
443}
444
445Expected<std::pair<size_t, size_t>>
446StaticLibraryDefinitionGenerator::getSliceRangeForArch(
447 object::MachOUniversalBinary &UB, const Triple &TT) {
448
449 for (const auto &Obj : UB.objects()) {
450 auto ObjTT = Obj.getTriple();
451 if (ObjTT.getArch() == TT.getArch() &&
452 ObjTT.getSubArch() == TT.getSubArch() &&
453 (TT.getVendor() == Triple::UnknownVendor ||
454 ObjTT.getVendor() == TT.getVendor())) {
455 // We found a match. Return the range for the slice.
456 return std::make_pair(Obj.getOffset(), Obj.getSize());
457 }
458 }
459
460 return make_error<StringError>(Twine("Universal binary ") + UB.getFileName() +
461 " does not contain a slice for " +
462 TT.str(),
464}
465
466StaticLibraryDefinitionGenerator::StaticLibraryDefinitionGenerator(
467 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
468 std::unique_ptr<object::Archive> Archive,
469 GetObjectFileInterface GetObjFileInterface, Error &Err)
470 : L(L), GetObjFileInterface(std::move(GetObjFileInterface)),
471 ArchiveBuffer(std::move(ArchiveBuffer)), Archive(std::move(Archive)) {
472 ErrorAsOutParameter _(&Err);
473 if (!this->GetObjFileInterface)
474 this->GetObjFileInterface = getObjectFileInterface;
475 if (!Err)
476 Err = buildObjectFilesMap();
477}
478
479std::unique_ptr<DLLImportDefinitionGenerator>
482 return std::unique_ptr<DLLImportDefinitionGenerator>(
484}
485
487 LookupState &LS, LookupKind K, JITDylib &JD,
488 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
489 JITDylibSearchOrder LinkOrder;
490 JD.withLinkOrderDo([&](const JITDylibSearchOrder &LO) {
491 LinkOrder.reserve(LO.size());
492 for (auto &KV : LO) {
493 if (KV.first == &JD)
494 continue;
495 LinkOrder.push_back(KV);
496 }
497 });
498
499 // FIXME: if regular symbol name start with __imp_ we have to issue lookup of
500 // both __imp_ and stripped name and use the lookup information to resolve the
501 // real symbol name.
502 SymbolLookupSet LookupSet;
504 for (auto &KV : Symbols) {
505 StringRef Deinterned = *KV.first;
506 if (Deinterned.startswith(getImpPrefix()))
507 Deinterned = Deinterned.drop_front(StringRef(getImpPrefix()).size());
508 // Don't degrade the required state
509 if (ToLookUpSymbols.count(Deinterned) &&
510 ToLookUpSymbols[Deinterned] == SymbolLookupFlags::RequiredSymbol)
511 continue;
512 ToLookUpSymbols[Deinterned] = KV.second;
513 }
514
515 for (auto &KV : ToLookUpSymbols)
516 LookupSet.add(ES.intern(KV.first), KV.second);
517
518 auto Resolved =
519 ES.lookup(LinkOrder, LookupSet, LookupKind::DLSym, SymbolState::Resolved);
520 if (!Resolved)
521 return Resolved.takeError();
522
523 auto G = createStubsGraph(*Resolved);
524 if (!G)
525 return G.takeError();
526 return L.add(JD, std::move(*G));
527}
528
530DLLImportDefinitionGenerator::getTargetPointerSize(const Triple &TT) {
531 switch (TT.getArch()) {
532 case Triple::x86_64:
533 return 8;
534 default:
535 return make_error<StringError>(
536 "architecture unsupported by DLLImportDefinitionGenerator",
538 }
539}
540
541Expected<llvm::endianness>
542DLLImportDefinitionGenerator::getTargetEndianness(const Triple &TT) {
543 switch (TT.getArch()) {
544 case Triple::x86_64:
546 default:
547 return make_error<StringError>(
548 "architecture unsupported by DLLImportDefinitionGenerator",
550 }
551}
552
553Expected<std::unique_ptr<jitlink::LinkGraph>>
554DLLImportDefinitionGenerator::createStubsGraph(const SymbolMap &Resolved) {
555 Triple TT = ES.getTargetTriple();
556 auto PointerSize = getTargetPointerSize(TT);
557 if (!PointerSize)
558 return PointerSize.takeError();
559 auto Endianness = getTargetEndianness(TT);
560 if (!Endianness)
561 return Endianness.takeError();
562
563 auto G = std::make_unique<jitlink::LinkGraph>(
564 "<DLLIMPORT_STUBS>", TT, *PointerSize, *Endianness,
566 jitlink::Section &Sec =
567 G->createSection(getSectionName(), MemProt::Read | MemProt::Exec);
568
569 for (auto &KV : Resolved) {
570 jitlink::Symbol &Target = G->addAbsoluteSymbol(
571 *KV.first, KV.second.getAddress(), *PointerSize,
573
574 // Create __imp_ symbol
575 jitlink::Symbol &Ptr =
577 auto NameCopy = G->allocateContent(Twine(getImpPrefix()) + *KV.first);
578 StringRef NameCopyRef = StringRef(NameCopy.data(), NameCopy.size());
579 Ptr.setName(NameCopyRef);
580 Ptr.setLinkage(jitlink::Linkage::Strong);
582
583 // Create PLT stub
584 // FIXME: check PLT stub of data symbol is not accessed
585 jitlink::Block &StubBlock =
587 G->addDefinedSymbol(StubBlock, 0, *KV.first, StubBlock.getSize(),
589 false);
590 }
591
592 return std::move(G);
593}
594
595} // End namespace orc.
596} // End namespace llvm.
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
@ GlobalPrefix
Definition: AsmWriter.cpp:369
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...
std::string Name
bool End
Definition: ELF_riscv.cpp:478
#define _
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
Module.h This file contains the declarations for the Module class.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1003
This is an important base class in LLVM.
Definition: Constant.h:41
bool empty() const
Definition: DenseMap.h:98
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:273
StringRef getSection() const
Definition: Globals.cpp:173
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
bool hasSection() const
Definition: GlobalValue.h:286
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
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.
Definition: Module.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:613
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ UnknownVendor
Definition: Triple.h:169
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
LLVM Value Representation.
Definition: Value.h:74
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
A range adaptor for a pair of iterators.
IteratorT begin() const
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:669
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.
Definition: Core.h:1389
const Triple & getTargetTriple() const
Return the triple for the executor.
Definition: Core.h:1435
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1446
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:2121
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
void registerAtExit(void(*F)(void *), void *Ctx, void *DSOHandle)
Represents a JIT'd dynamic library.
Definition: Core.h:958
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1849
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:977
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition: Core.h:1842
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.
Definition: Core.h:890
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
Interface for Layers that accept object files.
Definition: Layer.h:133
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...
Definition: Layer.cpp:170
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:141
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.
Definition: Core.h:183
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:244
bool containsDuplicates()
Returns true if this set contains any duplicates.
Definition: Core.h:371
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 llvm::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:24
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
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...
Definition: Core.h:166
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.
Definition: Core.h:162
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:760
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.
Definition: Core.h:135
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:121
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.
Definition: Core.h:157
@ Resolved
Queried, materialization begun.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Definition: Error.h:1325
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.
Definition: STLExtras.h:1684
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:90
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto dyn_cast_or_null(const Y &Val)
Definition: Casting.h:759
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1853
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Accessor for an element of the global_ctors/global_dtors array.