30#define DEBUG_TYPE "dyld"
34enum RuntimeDyldErrorCode {
35 GenericRTDyldError = 1
41class RuntimeDyldErrorCategory :
public std::error_category {
43 const char *
name()
const noexcept override {
return "runtimedyld"; }
45 std::string message(
int Condition)
const override {
46 switch (
static_cast<RuntimeDyldErrorCode
>(Condition)) {
47 case GenericRTDyldError:
return "Generic RuntimeDyld error";
62 static RuntimeDyldErrorCategory RTDyldErrorCategory;
63 return std::error_code(GenericRTDyldError, RTDyldErrorCategory);
77 MemMgr.deregisterEHFrames();
82 dbgs() <<
"----- Contents of section " << S.
getName() <<
" " << State
86 dbgs() <<
"\n <section not emitted>\n";
90 const unsigned ColsPerRow = 16;
95 unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
96 unsigned BytesRemaining = S.
getSize();
100 LoadAddr & ~(
uint64_t)(ColsPerRow - 1)) <<
":";
101 while (StartPadding--)
105 while (BytesRemaining > 0) {
106 if ((LoadAddr & (ColsPerRow - 1)) == 0)
107 dbgs() <<
"\n" <<
format(
"0x%016" PRIx64, LoadAddr) <<
":";
122 std::lock_guard<sys::Mutex> locked(lock);
131 if (
auto Err = resolveExternalSymbols()) {
133 ErrorStr =
toString(std::move(Err));
136 resolveLocalRelocations();
147 for (
const auto &Rel : Relocations) {
151 unsigned Idx = Rel.first;
155 resolveRelocationList(Rel.second,
Addr);
162 std::lock_guard<sys::Mutex> locked(lock);
163 for (
unsigned i = 0, e = Sections.size(); i != e; ++i) {
164 if (Sections[i].
getAddress() == LocalAddress) {
165 reassignSectionAddress(i, TargetAddress);
183 std::lock_guard<sys::Mutex> locked(lock);
192 if (MemMgr.needsToReserveAllocationSpace()) {
193 uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
194 Align CodeAlign, RODataAlign, RWDataAlign;
195 if (
auto Err = computeTotalAllocSize(Obj, CodeSize, CodeAlign, RODataSize,
196 RODataAlign, RWDataSize, RWDataAlign))
197 return std::move(Err);
198 MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
199 RWDataSize, RWDataAlign);
221 if ((*FlagsOrErr & SymbolRef::SF_Common) ||
222 (*FlagsOrErr & SymbolRef::SF_Weak)) {
224 if (
auto NameOrErr =
Sym.getName())
225 Symbols.insert(*NameOrErr);
227 return NameOrErr.takeError();
231 if (
auto ResultOrErr =
Resolver.getResponsibilitySet(Symbols))
232 ResponsibilitySet = std::move(*ResultOrErr);
234 return ResultOrErr.takeError();
247 if (*FlagsOrErr & SymbolRef::SF_Undefined)
252 if (
auto SymTypeOrErr =
I->getType())
253 SymType = *SymTypeOrErr;
255 return SymTypeOrErr.takeError();
259 if (
auto NameOrErr =
I->getName())
262 return NameOrErr.takeError();
265 auto JITSymFlags = getJITSymbolFlags(*
I);
267 return JITSymFlags.takeError();
273 if (JITSymFlags->isWeak() || JITSymFlags->isCommon()) {
275 if (GlobalSymbolTable.count(
Name))
279 if (!ResponsibilitySet.count(
Name))
284 if (JITSymFlags->isWeak())
286 if (JITSymFlags->isCommon()) {
293 CommonSymbolsToAllocate.push_back(*
I);
297 if (*FlagsOrErr & SymbolRef::SF_Absolute &&
300 if (
auto AddrOrErr =
I->getAddress())
303 return AddrOrErr.takeError();
305 unsigned SectionID = AbsoluteSymbolSection;
308 <<
" SID: " << SectionID
309 <<
" Offset: " <<
format(
"%p", (uintptr_t)
Addr)
310 <<
" flags: " << *FlagsOrErr <<
"\n");
313 auto Result = GlobalSymbolTable.insert_or_assign(
315 processNewSymbol(*
I, Result.first->getValue());
323 if (
auto SIOrErr =
I->getSection())
326 return SIOrErr.takeError();
333 if (
auto Err =
getOffset(*
I, *SI, SectOffset))
334 return std::move(Err);
336 bool IsCode = SI->isText();
338 if (
auto SectionIDOrErr =
339 findOrEmitSection(Obj, *SI, IsCode, LocalSections))
340 SectionID = *SectionIDOrErr;
342 return SectionIDOrErr.takeError();
345 <<
" SID: " << SectionID
346 <<
" Offset: " <<
format(
"%p", (uintptr_t)SectOffset)
347 <<
" flags: " << *FlagsOrErr <<
"\n");
350 auto Result = GlobalSymbolTable.insert_or_assign(
352 processNewSymbol(*
I, Result.first->getValue());
358 if (
auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize,
360 return std::move(Err);
373 if (RelocatedSection == SE)
379 if (
I ==
E && !ProcessAllSections)
382 bool IsCode = RelocatedSection->
isText();
383 unsigned SectionID = 0;
384 if (
auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
386 SectionID = *SectionIDOrErr;
388 return SectionIDOrErr.takeError();
393 if (
auto IOrErr = processRelocationRef(SectionID,
I, Obj, LocalSections, Stubs))
396 return IOrErr.takeError();
400 if (NotifyStubEmitted) {
403 for (
auto &KV : Stubs) {
410 NotifyStubEmitted(FileName,
SectionName, VR.SymbolName, SectionID,
416 for (
auto &GSTMapEntry : GlobalSymbolTable) {
417 StringRef SymbolName = GSTMapEntry.first();
418 auto &GSTEntry = GSTMapEntry.second;
419 if (GSTEntry.getSectionID() == VR.SectionID &&
420 GSTEntry.getOffset() == VR.Offset) {
421 NotifyStubEmitted(FileName,
SectionName, SymbolName, SectionID,
431 if (ProcessAllSections) {
437 if (LocalSections.find(*SI) != LocalSections.end())
440 bool IsCode = SI->isText();
441 if (
auto SectionIDOrErr =
442 findOrEmitSection(Obj, *SI, IsCode, LocalSections))
443 LLVM_DEBUG(
dbgs() <<
"\tSectionID: " << (*SectionIDOrErr) <<
"\n");
445 return SectionIDOrErr.takeError();
450 if (
auto Err = finalizeLoad(Obj, LocalSections))
451 return std::move(Err);
456 return LocalSections;
466 for (
uint64_t SectionSize : SectionSizes)
467 TotalSize +=
alignTo(SectionSize, Alignment);
473 if (isa<object::ELFObjectFileBase>(Obj))
475 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
476 const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
487 return HasContent && !IsDiscardable;
490 assert(isa<MachOObjectFile>(Obj));
496 if (isa<object::ELFObjectFileBase>(Obj))
499 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
500 return ((COFFObj->getCOFFSection(Section)->Characteristics &
508 assert(isa<MachOObjectFile>(Obj));
514 if (isa<object::ELFObjectFileBase>(Obj))
516 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
517 return COFFObj->getCOFFSection(Section)->Characteristics &
520 auto *MachO = cast<MachOObjectFile>(Obj);
521 unsigned SectionType = MachO->getSectionType(Section);
528 if (isa<object::ELFObjectFileBase>(Obj))
538 Align &RWDataAlign) {
540 std::vector<uint64_t> CodeSectionSizes;
541 std::vector<uint64_t> ROSectionSizes;
542 std::vector<uint64_t> RWSectionSizes;
554 uint64_t DataSize = Section.getSize();
555 Align Alignment = Section.getAlignment();
556 bool IsCode = Section.isText();
558 bool IsTLS =
isTLS(Section);
565 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
568 if (
Name ==
".eh_frame")
570 if (StubBufSize != 0)
571 PaddingSize += getStubAlignment().value() - 1;
573 uint64_t SectionSize = DataSize + PaddingSize + StubBufSize;
580 if (
Name ==
".eh_frame")
587 CodeAlign = std::max(CodeAlign, Alignment);
588 CodeSectionSizes.push_back(SectionSize);
589 }
else if (IsReadOnly) {
590 RODataAlign = std::max(RODataAlign, Alignment);
591 ROSectionSizes.push_back(SectionSize);
593 RWDataAlign = std::max(RWDataAlign, Alignment);
594 RWSectionSizes.push_back(SectionSize);
602 if (
unsigned GotSize = computeGOTSize(Obj)) {
603 RWSectionSizes.push_back(GotSize);
604 RWDataAlign = std::max(RWDataAlign,
Align(getGOTEntrySize()));
616 if (*FlagsOrErr & SymbolRef::SF_Common) {
623 CommonAlign = Alignment;
624 CommonSize =
alignTo(CommonSize, Alignment) +
Size;
627 if (CommonSize != 0) {
628 RWSectionSizes.push_back(CommonSize);
629 RWDataAlign = std::max(RWDataAlign, CommonAlign);
632 if (!CodeSectionSizes.empty()) {
634 CodeSectionSizes.push_back(64);
651 size_t GotEntrySize = getGOTEntrySize();
660 if (relocationNeedsGot(Reloc))
661 GotSize += GotEntrySize;
670 if (!MemMgr.allowStubAllocation()) {
674 unsigned StubSize = getMaxStubSize();
681 unsigned StubBufSize = 0;
690 if (!(RelSecI == Section))
694 if (relocationNeedsStub(Reloc))
695 StubBufSize += StubSize;
699 uint64_t DataSize = Section.getSize();
700 Align Alignment = Section.getAlignment();
703 Align StubAlignment = getStubAlignment();
705 if (StubAlignment > EndAlignment)
706 StubBufSize += StubAlignment.
value() - EndAlignment.
value();
711 unsigned Size)
const {
713 if (IsTargetLittleEndian) {
716 Result = (Result << 8) | *Src--;
719 Result = (Result << 8) | *Src++;
725 unsigned Size)
const {
726 if (IsTargetLittleEndian) {
728 *Dst++ =
Value & 0xFF;
734 *Dst-- =
Value & 0xFF;
749 if (SymbolsToAllocate.empty())
753 unsigned SectionID = Sections.size();
754 uint8_t *
Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
755 "<common symbols>",
false);
761 memset(
Addr, 0, CommonSize);
765 <<
" DataSize: " << CommonSize <<
"\n");
768 for (
auto &
Sym : SymbolsToAllocate) {
772 if (
auto NameOrErr =
Sym.getName())
775 return NameOrErr.takeError();
783 auto JITSymFlags = getJITSymbolFlags(
Sym);
786 return JITSymFlags.takeError();
791 GlobalSymbolTable[
Name] =
805 Align Alignment = Section.getAlignment();
807 unsigned PaddingSize = 0;
808 unsigned StubBufSize = 0;
810 bool IsVirtual = Section.isVirtual();
813 bool IsTLS =
isTLS(Section);
814 uint64_t DataSize = Section.getSize();
821 StubBufSize = computeSectionStubBufSize(Obj, Section);
826 if (
Name ==
".eh_frame")
830 unsigned SectionID = Sections.size();
833 const char *pData =
nullptr;
837 if (!IsVirtual && !IsZeroInit) {
843 return E.takeError();
850 if (StubBufSize != 0) {
851 Alignment = std::max(Alignment, getStubAlignment());
852 PaddingSize += getStubAlignment().value() - 1;
857 if (IsRequired || ProcessAllSections) {
858 Allocate = DataSize + PaddingSize + StubBufSize;
862 auto TLSSection = MemMgr.allocateTLSSection(Allocate, Alignment.
value(),
864 Addr = TLSSection.InitializationImage;
865 LoadAddress = TLSSection.Offset;
867 Addr = MemMgr.allocateCodeSection(Allocate, Alignment.
value(), SectionID,
870 Addr = MemMgr.allocateDataSection(Allocate, Alignment.
value(), SectionID,
877 if (IsZeroInit || IsVirtual)
878 memset(
Addr, 0, DataSize);
880 memcpy(
Addr, pData, DataSize);
883 if (PaddingSize != 0) {
884 memset(
Addr + DataSize, 0, PaddingSize);
886 DataSize += PaddingSize;
891 DataSize &= -(
uint64_t)getStubAlignment().value();
894 LLVM_DEBUG(
dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: "
895 <<
Name <<
" obj addr: " <<
format(
"%p", pData)
896 <<
" new addr: " <<
format(
"%p",
Addr) <<
" DataSize: "
897 << DataSize <<
" StubBufSize: " << StubBufSize
898 <<
" Allocate: " << Allocate <<
"\n");
906 dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: " <<
Name
907 <<
" obj addr: " <<
format(
"%p", data.
data()) <<
" new addr: 0"
908 <<
" DataSize: " << DataSize <<
" StubBufSize: " << StubBufSize
909 <<
" Allocate: " << Allocate <<
"\n");
918 Sections.back().setLoadAddress(LoadAddress);
921 Sections.back().setLoadAddress(0);
932 unsigned SectionID = 0;
933 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
934 if (i != LocalSections.end())
935 SectionID = i->second;
937 if (
auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
938 SectionID = *SectionIDOrErr;
940 return SectionIDOrErr.takeError();
941 LocalSections[Section] = SectionID;
947 unsigned SectionID) {
948 Relocations[SectionID].push_back(RE);
957 if (Loc == GlobalSymbolTable.end()) {
958 ExternalSymbolRelocations[SymbolName].push_back(RE);
960 assert(!SymbolName.empty() &&
961 "Empty symbol should not be in GlobalSymbolTable");
964 const auto &
SymInfo = Loc->second;
966 Relocations[
SymInfo.getSectionID()].push_back(RECopy);
971 unsigned AbiVariant) {
978 writeBytesUnaligned(0xd2e00010,
Addr, 4);
979 writeBytesUnaligned(0xf2c00010,
Addr+4, 4);
980 writeBytesUnaligned(0xf2a00010,
Addr+8, 4);
981 writeBytesUnaligned(0xf2800010,
Addr+12, 4);
982 writeBytesUnaligned(0xd61f0200,
Addr+16, 4);
988 writeBytesUnaligned(0xe51ff004,
Addr, 4);
990 }
else if (IsMipsO32ABI || IsMipsN32ABI) {
995 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
996 const unsigned NopInstr = 0x0;
997 unsigned JrT9Instr = 0x03200008;
1000 JrT9Instr = 0x03200009;
1002 writeBytesUnaligned(LuiT9Instr,
Addr, 4);
1003 writeBytesUnaligned(AdduiT9Instr,
Addr + 4, 4);
1004 writeBytesUnaligned(JrT9Instr,
Addr + 8, 4);
1005 writeBytesUnaligned(NopInstr,
Addr + 12, 4);
1007 }
else if (IsMipsN64ABI) {
1016 const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
1017 DsllT9Instr = 0x19CC38;
1018 const unsigned NopInstr = 0x0;
1019 unsigned JrT9Instr = 0x03200008;
1021 JrT9Instr = 0x03200009;
1023 writeBytesUnaligned(LuiT9Instr,
Addr, 4);
1024 writeBytesUnaligned(DaddiuT9Instr,
Addr + 4, 4);
1025 writeBytesUnaligned(DsllT9Instr,
Addr + 8, 4);
1026 writeBytesUnaligned(DaddiuT9Instr,
Addr + 12, 4);
1027 writeBytesUnaligned(DsllT9Instr,
Addr + 16, 4);
1028 writeBytesUnaligned(DaddiuT9Instr,
Addr + 20, 4);
1029 writeBytesUnaligned(JrT9Instr,
Addr + 24, 4);
1030 writeBytesUnaligned(NopInstr,
Addr + 28, 4);
1036 writeInt32BE(
Addr, 0x3D800000);
1037 writeInt32BE(
Addr+4, 0x618C0000);
1038 writeInt32BE(
Addr+8, 0x798C07C6);
1039 writeInt32BE(
Addr+12, 0x658C0000);
1040 writeInt32BE(
Addr+16, 0x618C0000);
1041 if (AbiVariant == 2) {
1044 writeInt32BE(
Addr+20, 0xF8410018);
1045 writeInt32BE(
Addr+24, 0x7D8903A6);
1046 writeInt32BE(
Addr+28, 0x4E800420);
1051 writeInt32BE(
Addr+20, 0xF8410028);
1052 writeInt32BE(
Addr+24, 0xE96C0000);
1053 writeInt32BE(
Addr+28, 0xE84C0008);
1054 writeInt32BE(
Addr+32, 0x7D6903A6);
1055 writeInt32BE(
Addr+36, 0xE96C0010);
1056 writeInt32BE(
Addr+40, 0x4E800420);
1060 writeInt16BE(
Addr, 0xC418);
1061 writeInt16BE(
Addr+2, 0x0000);
1062 writeInt16BE(
Addr+4, 0x0004);
1063 writeInt16BE(
Addr+6, 0x07F1);
1091 dbgs() <<
"Reassigning address for section " << SectionID <<
" ("
1092 << Sections[SectionID].
getName() <<
"): "
1093 <<
format(
"0x%016" PRIx64, Sections[SectionID].getLoadAddress())
1094 <<
" -> " <<
format(
"0x%016" PRIx64,
Addr) <<
"\n");
1095 Sections[SectionID].setLoadAddress(
Addr);
1102 if (RE.SectionID != AbsoluteSymbolSection &&
1103 Sections[RE.SectionID].getAddress() ==
nullptr)
1111 for (
auto &RelocKV : ExternalSymbolRelocations) {
1114 if (
Name.size() == 0) {
1118 resolveRelocationList(Relocs, 0);
1123 if (Loc == GlobalSymbolTable.end()) {
1124 auto RRI = ExternalSymbolMap.
find(
Name);
1125 assert(RRI != ExternalSymbolMap.
end() &&
"No result for symbol");
1126 Addr = RRI->second.getAddress();
1127 Flags = RRI->second.getFlags();
1131 const auto &
SymInfo = Loc->second;
1132 Addr = getSectionLoadAddress(
SymInfo.getSectionID()) +
1140 "' which could not be resolved!");
1149 Addr = modifyAddressBasedOnFlags(
Addr, Flags);
1153 resolveRelocationList(Relocs,
Addr);
1157 ExternalSymbolRelocations.clear();
1171 for (
auto &RelocKV : ExternalSymbolRelocations) {
1173 if (!
Name.empty() && !GlobalSymbolTable.count(
Name) &&
1174 !ResolvedSymbols.count(
Name))
1175 NewSymbols.insert(
Name);
1178 if (NewSymbols.empty())
1182 using ExpectedLookupResult =
1188 auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>();
1189 auto NewSymbolsF = NewSymbolsP->get_future();
1192 NewSymbolsP->set_value(std::move(Result));
1195 auto NewResolverResults = NewSymbolsF.get();
1197 if (!NewResolverResults)
1198 return NewResolverResults.takeError();
1200 assert(NewResolverResults->size() == NewSymbols.size() &&
1201 "Should have errored on unresolved symbols");
1203 for (
auto &RRKV : *NewResolverResults) {
1204 assert(!ResolvedSymbols.count(RRKV.first) &&
"Redundant resolution?");
1205 ExternalSymbolMap.
insert(RRKV);
1206 ResolvedSymbols.insert(RRKV.first);
1211 applyExternalSymbolRelocations(ExternalSymbolMap);
1217 std::unique_ptr<RuntimeDyldImpl> This,
1219 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>,
Error)>
1222 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
Info) {
1224 auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This));
1225 auto PostResolveContinuation =
1226 [SharedThis, OnEmitted = std::move(OnEmitted), O = std::move(O),
1230 OnEmitted(std::move(O), std::move(
Info), Result.takeError());
1236 for (
auto &KV : *Result)
1237 Resolved[KV.first] = KV.second;
1239 SharedThis->applyExternalSymbolRelocations(Resolved);
1240 SharedThis->resolveLocalRelocations();
1241 SharedThis->registerEHFrames();
1243 if (SharedThis->MemMgr.finalizeMemory(&ErrMsg))
1244 OnEmitted(std::move(O), std::move(
Info),
1245 make_error<StringError>(std::move(ErrMsg),
1253 for (
auto &RelocKV : SharedThis->ExternalSymbolRelocations) {
1257 assert(!SharedThis->GlobalSymbolTable.count(
Name) &&
1258 "Name already processed. RuntimeDyld instances can not be re-used "
1259 "when finalizing with finalizeAsync.");
1260 Symbols.insert(
Name);
1263 if (!Symbols.empty()) {
1264 SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation));
1266 PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>());
1275 auto I = ObjSecToIDMap.find(Sec);
1276 if (
I != ObjSecToIDMap.end())
1277 return RTDyld.Sections[
I->second].getLoadAddress();
1290void RuntimeDyld::MemoryManager::anchor() {}
1291void JITSymbolResolver::anchor() {}
1292void LegacyJITSymbolResolver::anchor() {}
1304 ProcessAllSections =
false;
1309static std::unique_ptr<RuntimeDyldCOFF>
1314 std::unique_ptr<RuntimeDyldCOFF> Dyld =
1316 Dyld->setProcessAllSections(ProcessAllSections);
1317 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1321static std::unique_ptr<RuntimeDyldELF>
1325 std::unique_ptr<RuntimeDyldELF> Dyld =
1327 Dyld->setProcessAllSections(ProcessAllSections);
1328 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1332static std::unique_ptr<RuntimeDyldMachO>
1336 bool ProcessAllSections,
1338 std::unique_ptr<RuntimeDyldMachO> Dyld =
1340 Dyld->setProcessAllSections(ProcessAllSections);
1341 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1345std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1351 MemMgr,
Resolver, ProcessAllSections,
1352 std::move(NotifyStubEmitted));
1356 ProcessAllSections, std::move(NotifyStubEmitted));
1360 ProcessAllSections, std::move(NotifyStubEmitted));
1365 if (!Dyld->isCompatibleFile(Obj))
1368 auto LoadedObjInfo = Dyld->loadObject(Obj);
1370 return LoadedObjInfo;
1376 return Dyld->getSymbolLocalAddress(
Name);
1380 assert(Dyld &&
"No RuntimeDyld instance attached");
1381 return Dyld->getSymbolSectionID(
Name);
1387 return Dyld->getSymbol(
Name);
1392 return std::map<StringRef, JITEvaluatedSymbol>();
1393 return Dyld->getSymbolTable();
1399 Dyld->reassignSectionAddress(SectionID,
Addr);
1404 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1412 bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1413 MemMgr.FinalizationLocked =
true;
1416 if (!MemoryFinalizationLocked) {
1418 MemMgr.FinalizationLocked =
false;
1423 assert(Dyld &&
"No Dyld instance attached");
1424 return Dyld->getSectionContent(SectionID);
1428 assert(Dyld &&
"No Dyld instance attached");
1429 return Dyld->getSectionLoadAddress(SectionID);
1434 Dyld->registerEHFrames();
1439 Dyld->deregisterEHFrames();
1447 bool ProcessAllSections,
1450 std::map<StringRef, JITEvaluatedSymbol>)>
1453 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>,
Error)>
1462 OnEmitted(std::move(O), std::move(
Info),
1469 OnEmitted(std::move(O), std::move(
Info), std::move(Err));
1474 std::move(O), std::move(
Info));
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const Value * getAddress(const DbgVariableIntrinsic *DVI)
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.
Represents a symbol that has been evaluated to an address already.
Flags for symbols in the JIT.
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Symbol resolution interface.
std::set< StringRef > LookupSet
RelocationEntry - used to represent relocations internally in the dynamic linker.
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
static std::unique_ptr< RuntimeDyldCOFF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
std::map< SectionRef, unsigned > ObjSectionToIDMap
void applyExternalSymbolRelocations(const StringMap< JITEvaluatedSymbol > ExternalSymbolMap)
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value)
Resolves relocations from Relocs list with address from Value.
std::map< RelocationValueRef, uintptr_t > StubMap
void deregisterEHFrames()
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
virtual void registerEHFrames()
virtual ~RuntimeDyldImpl()
static void finalizeAsync(std::unique_ptr< RuntimeDyldImpl > This, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted, object::OwningBinary< object::ObjectFile > O, std::unique_ptr< RuntimeDyld::LoadedObjectInfo > Info)
Error emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols, uint64_t CommonSize, uint32_t CommonAlign)
Given the common symbols discovered in the object file, emit a new section for them and update the sy...
Expected< unsigned > emitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode)
Emits section data from the object file to the MemoryManager.
std::vector< SymbolRef > CommonSymbolList
void resolveLocalRelocations()
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
unsigned computeGOTSize(const ObjectFile &Obj)
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
Error computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize, Align &CodeAlign, uint64_t &RODataSize, Align &RODataAlign, uint64_t &RWDataSize, Align &RWDataAlign)
unsigned computeSectionStubBufSize(const ObjectFile &Obj, const SectionRef &Section)
void resolveRelocations()
virtual Expected< JITSymbolFlags > getJITSymbolFlags(const SymbolRef &Sym)
Generate JITSymbolFlags from a libObject symbol.
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
Error resolveExternalSymbols()
Resolve relocations to external symbols.
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
Information about the loaded object.
uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override
Obtain the Load Address of a section by SectionRef.
virtual void notifyObjectLoaded(RuntimeDyld &RTDyld, const object::ObjectFile &Obj)
This method is called after an object has been loaded into memory but before relocations are applied ...
virtual TLSSection allocateTLSSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName)
Allocate a memory block of (at least) the given size to be used for thread-local storage (TLS).
virtual bool finalizeMemory(std::string *ErrMsg=nullptr)=0
This method is called when object loading is complete and section page permissions can be applied.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
Map a section to its target address space value.
void setProcessAllSections(bool ProcessAllSections)
By default, only sections that are "required for execution" are passed to the RTDyldMemoryManager,...
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
uint64_t getSectionLoadAddress(unsigned SectionID) const
If the section was loaded, return the section's load address, otherwise return std::nullopt.
void * getSymbolLocalAddress(StringRef Name) const
Get the address of our local copy of the symbol.
std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
Returns a copy of the symbol table.
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
void finalizeWithMemoryManagerLocking()
Perform all actions needed to make the code owned by this RuntimeDyld instance executable:
void deregisterEHFrames()
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
StringRef getSectionContent(unsigned SectionID) const
Returns the section's working memory.
JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Construct a RuntimeDyld instance.
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated.
StringRef getErrorString()
unsigned getSymbolSectionID(StringRef Name) const
Get the section ID for the section containing the given symbol.
std::function< void(StringRef FileName, StringRef SectionName, StringRef SymbolName, unsigned SectionID, uint32_t StubOffset)> NotifyStubEmittedFunction
SectionEntry - represents a section emitted into memory by the dynamic linker.
StringRef getName() const
uint8_t * getAddress() const
uint64_t getLoadAddress() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
iterator find(StringRef Key)
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
StringRef - Represent a constant reference to a string, i.e.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Symbol info for RuntimeDyld.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
bool isLittleEndian() const
StringRef getFileName() const
uint64_t getFlags() const
This class is the base class for all object file types.
virtual section_iterator section_end() const =0
symbol_iterator_range symbols() const
virtual Triple::ArchType getArch() const =0
virtual section_iterator section_begin() const =0
This is a value type class that represents a single relocation in the list of relocations in the obje...
This is a value type class that represents a single section in the list of sections in the object fil...
uint64_t getAddress() const
bool isText() const
Whether this section contains instructions.
This is a value type class that represents a single symbol in the list of symbols in the object file.
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
This class implements an extremely fast bulk output stream that can only output to a stream.
unique_function is a type-erasing functor similar to std::function.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
@ IMAGE_SCN_MEM_DISCARDABLE
@ IMAGE_SCN_CNT_INITIALIZED_DATA
@ S_GB_ZEROFILL
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
@ S_ZEROFILL
S_ZEROFILL - Zero fill on demand section.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
static std::unique_ptr< RuntimeDyldMachO > createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
static bool isTLS(const SectionRef Section)
static void dumpSectionMemory(const SectionEntry &S, StringRef State)
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
static std::unique_ptr< RuntimeDyldCOFF > createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
static bool isReadOnlyData(const SectionRef Section)
void jitLinkForORC(object::OwningBinary< object::ObjectFile > O, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(const object::ObjectFile &Obj, RuntimeDyld::LoadedObjectInfo &, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
static std::unique_ptr< RuntimeDyldELF > createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static bool isZeroInit(const SectionRef Section)
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
static uint64_t computeAllocationSizeForSections(std::vector< uint64_t > &SectionSizes, Align Alignment)
static bool isRequiredForExecution(const SectionRef Section)
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
uint64_t value() const
This is a hole in the type system and should not be abused.
An allocated TLS section.
support::ulittle32_t VirtualSize
support::ulittle32_t Characteristics
support::ulittle32_t SizeOfRawData