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()) {
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;
696 if (relocationNeedsDLLImportStub(Reloc))
697 StubBufSize = sizeAfterAddingDLLImportStub(StubBufSize);
702 uint64_t DataSize = Section.getSize();
703 Align Alignment = Section.getAlignment();
706 Align StubAlignment = getStubAlignment();
708 if (StubAlignment > EndAlignment)
709 StubBufSize += StubAlignment.
value() - EndAlignment.
value();
714 unsigned Size)
const {
716 if (IsTargetLittleEndian) {
719 Result = (Result << 8) | *Src--;
722 Result = (Result << 8) | *Src++;
728 unsigned Size)
const {
729 if (IsTargetLittleEndian) {
731 *Dst++ =
Value & 0xFF;
737 *Dst-- =
Value & 0xFF;
752 if (SymbolsToAllocate.empty())
756 unsigned SectionID = Sections.size();
757 uint8_t *
Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
758 "<common symbols>",
false);
764 memset(
Addr, 0, CommonSize);
768 <<
" DataSize: " << CommonSize <<
"\n");
771 for (
auto &
Sym : SymbolsToAllocate) {
775 if (
auto NameOrErr =
Sym.getName())
778 return NameOrErr.takeError();
786 auto JITSymFlags = getJITSymbolFlags(
Sym);
789 return JITSymFlags.takeError();
794 GlobalSymbolTable[
Name] =
808 Align Alignment = Section.getAlignment();
810 unsigned PaddingSize = 0;
811 unsigned StubBufSize = 0;
813 bool IsVirtual = Section.isVirtual();
816 bool IsTLS =
isTLS(Section);
817 uint64_t DataSize = Section.getSize();
824 StubBufSize = computeSectionStubBufSize(Obj, Section);
829 if (
Name ==
".eh_frame")
833 unsigned SectionID = Sections.size();
836 const char *pData =
nullptr;
840 if (!IsVirtual && !IsZeroInit) {
846 return E.takeError();
853 if (StubBufSize != 0) {
854 Alignment = std::max(Alignment, getStubAlignment());
855 PaddingSize += getStubAlignment().value() - 1;
860 if (IsRequired || ProcessAllSections) {
861 Allocate = DataSize + PaddingSize + StubBufSize;
865 auto TLSSection = MemMgr.allocateTLSSection(Allocate, Alignment.
value(),
867 Addr = TLSSection.InitializationImage;
868 LoadAddress = TLSSection.Offset;
870 Addr = MemMgr.allocateCodeSection(Allocate, Alignment.
value(), SectionID,
873 Addr = MemMgr.allocateDataSection(Allocate, Alignment.
value(), SectionID,
880 if (IsZeroInit || IsVirtual)
881 memset(
Addr, 0, DataSize);
883 memcpy(
Addr, pData, DataSize);
886 if (PaddingSize != 0) {
887 memset(
Addr + DataSize, 0, PaddingSize);
889 DataSize += PaddingSize;
894 DataSize &= -getStubAlignment().value();
897 LLVM_DEBUG(
dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: "
898 <<
Name <<
" obj addr: " <<
format(
"%p", pData)
899 <<
" new addr: " <<
format(
"%p",
Addr) <<
" DataSize: "
900 << DataSize <<
" StubBufSize: " << StubBufSize
901 <<
" Allocate: " << Allocate <<
"\n");
909 dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: " <<
Name
910 <<
" obj addr: " <<
format(
"%p",
data.data()) <<
" new addr: 0"
911 <<
" DataSize: " << DataSize <<
" StubBufSize: " << StubBufSize
912 <<
" Allocate: " << Allocate <<
"\n");
921 Sections.back().setLoadAddress(LoadAddress);
924 Sections.back().setLoadAddress(0);
935 unsigned SectionID = 0;
936 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
937 if (i != LocalSections.end())
938 SectionID = i->second;
940 if (
auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
941 SectionID = *SectionIDOrErr;
943 return SectionIDOrErr.takeError();
944 LocalSections[Section] = SectionID;
950 unsigned SectionID) {
951 Relocations[SectionID].push_back(RE);
960 if (Loc == GlobalSymbolTable.end()) {
961 ExternalSymbolRelocations[SymbolName].push_back(RE);
963 assert(!SymbolName.empty() &&
964 "Empty symbol should not be in GlobalSymbolTable");
967 const auto &
SymInfo = Loc->second;
969 Relocations[
SymInfo.getSectionID()].push_back(RECopy);
974 unsigned AbiVariant) {
981 writeBytesUnaligned(0xd2e00010,
Addr, 4);
982 writeBytesUnaligned(0xf2c00010,
Addr+4, 4);
983 writeBytesUnaligned(0xf2a00010,
Addr+8, 4);
984 writeBytesUnaligned(0xf2800010,
Addr+12, 4);
985 writeBytesUnaligned(0xd61f0200,
Addr+16, 4);
991 writeBytesUnaligned(0xe51ff004,
Addr, 4);
999 writeBytesUnaligned(0x1400000c,
Addr, 4);
1000 writeBytesUnaligned(0x0380018c,
Addr + 4, 4);
1001 writeBytesUnaligned(0x1600000c,
Addr + 8, 4);
1002 writeBytesUnaligned(0x0300018c,
Addr + 12, 4);
1003 writeBytesUnaligned(0x4c000180,
Addr + 16, 4);
1005 }
else if (IsMipsO32ABI || IsMipsN32ABI) {
1010 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
1011 const unsigned NopInstr = 0x0;
1012 unsigned JrT9Instr = 0x03200008;
1015 JrT9Instr = 0x03200009;
1017 writeBytesUnaligned(LuiT9Instr,
Addr, 4);
1018 writeBytesUnaligned(AdduiT9Instr,
Addr + 4, 4);
1019 writeBytesUnaligned(JrT9Instr,
Addr + 8, 4);
1020 writeBytesUnaligned(NopInstr,
Addr + 12, 4);
1022 }
else if (IsMipsN64ABI) {
1031 const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
1032 DsllT9Instr = 0x19CC38;
1033 const unsigned NopInstr = 0x0;
1034 unsigned JrT9Instr = 0x03200008;
1036 JrT9Instr = 0x03200009;
1038 writeBytesUnaligned(LuiT9Instr,
Addr, 4);
1039 writeBytesUnaligned(DaddiuT9Instr,
Addr + 4, 4);
1040 writeBytesUnaligned(DsllT9Instr,
Addr + 8, 4);
1041 writeBytesUnaligned(DaddiuT9Instr,
Addr + 12, 4);
1042 writeBytesUnaligned(DsllT9Instr,
Addr + 16, 4);
1043 writeBytesUnaligned(DaddiuT9Instr,
Addr + 20, 4);
1044 writeBytesUnaligned(JrT9Instr,
Addr + 24, 4);
1045 writeBytesUnaligned(NopInstr,
Addr + 28, 4);
1051 writeInt32BE(
Addr, 0x3D800000);
1052 writeInt32BE(
Addr+4, 0x618C0000);
1053 writeInt32BE(
Addr+8, 0x798C07C6);
1054 writeInt32BE(
Addr+12, 0x658C0000);
1055 writeInt32BE(
Addr+16, 0x618C0000);
1056 if (AbiVariant == 2) {
1059 writeInt32BE(
Addr+20, 0xF8410018);
1060 writeInt32BE(
Addr+24, 0x7D8903A6);
1061 writeInt32BE(
Addr+28, 0x4E800420);
1066 writeInt32BE(
Addr+20, 0xF8410028);
1067 writeInt32BE(
Addr+24, 0xE96C0000);
1068 writeInt32BE(
Addr+28, 0xE84C0008);
1069 writeInt32BE(
Addr+32, 0x7D6903A6);
1070 writeInt32BE(
Addr+36, 0xE96C0010);
1071 writeInt32BE(
Addr+40, 0x4E800420);
1075 writeInt16BE(
Addr, 0xC418);
1076 writeInt16BE(
Addr+2, 0x0000);
1077 writeInt16BE(
Addr+4, 0x0004);
1078 writeInt16BE(
Addr+6, 0x07F1);
1106 dbgs() <<
"Reassigning address for section " << SectionID <<
" ("
1107 << Sections[SectionID].
getName() <<
"): "
1108 <<
format(
"0x%016" PRIx64, Sections[SectionID].getLoadAddress())
1109 <<
" -> " <<
format(
"0x%016" PRIx64,
Addr) <<
"\n");
1110 Sections[SectionID].setLoadAddress(
Addr);
1117 if (RE.SectionID != AbsoluteSymbolSection &&
1118 Sections[RE.SectionID].getAddress() ==
nullptr)
1126 for (
auto &RelocKV : ExternalSymbolRelocations) {
1129 if (
Name.size() == 0) {
1133 resolveRelocationList(Relocs, 0);
1138 if (Loc == GlobalSymbolTable.end()) {
1139 auto RRI = ExternalSymbolMap.
find(
Name);
1140 assert(RRI != ExternalSymbolMap.
end() &&
"No result for symbol");
1141 Addr = RRI->second.getAddress();
1142 Flags = RRI->second.getFlags();
1146 const auto &
SymInfo = Loc->second;
1147 Addr = getSectionLoadAddress(
SymInfo.getSectionID()) +
1155 "' which could not be resolved!");
1164 Addr = modifyAddressBasedOnFlags(
Addr, Flags);
1168 resolveRelocationList(Relocs,
Addr);
1172 ExternalSymbolRelocations.clear();
1186 for (
auto &RelocKV : ExternalSymbolRelocations) {
1188 if (!
Name.empty() && !GlobalSymbolTable.count(
Name) &&
1189 !ResolvedSymbols.count(
Name))
1190 NewSymbols.insert(
Name);
1193 if (NewSymbols.empty())
1197 using ExpectedLookupResult =
1203 auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>();
1204 auto NewSymbolsF = NewSymbolsP->get_future();
1207 NewSymbolsP->set_value(std::move(Result));
1210 auto NewResolverResults = NewSymbolsF.get();
1212 if (!NewResolverResults)
1213 return NewResolverResults.takeError();
1215 assert(NewResolverResults->size() == NewSymbols.size() &&
1216 "Should have errored on unresolved symbols");
1218 for (
auto &RRKV : *NewResolverResults) {
1219 assert(!ResolvedSymbols.count(RRKV.first) &&
"Redundant resolution?");
1220 ExternalSymbolMap.
insert(RRKV);
1221 ResolvedSymbols.insert(RRKV.first);
1226 applyExternalSymbolRelocations(ExternalSymbolMap);
1232 std::unique_ptr<RuntimeDyldImpl> This,
1234 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>,
Error)>
1237 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
Info) {
1239 auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This));
1240 auto PostResolveContinuation =
1241 [SharedThis, OnEmitted = std::move(OnEmitted), O = std::move(O),
1245 OnEmitted(std::move(O), std::move(
Info), Result.takeError());
1251 for (
auto &KV : *Result)
1252 Resolved[KV.first] = KV.second;
1254 SharedThis->applyExternalSymbolRelocations(Resolved);
1255 SharedThis->resolveLocalRelocations();
1256 SharedThis->registerEHFrames();
1258 if (SharedThis->MemMgr.finalizeMemory(&ErrMsg))
1259 OnEmitted(std::move(O), std::move(
Info),
1260 make_error<StringError>(std::move(ErrMsg),
1268 for (
auto &RelocKV : SharedThis->ExternalSymbolRelocations) {
1272 assert(!SharedThis->GlobalSymbolTable.count(
Name) &&
1273 "Name already processed. RuntimeDyld instances can not be re-used "
1274 "when finalizing with finalizeAsync.");
1275 Symbols.insert(
Name);
1278 if (!Symbols.empty()) {
1279 SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation));
1281 PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>());
1290 auto I = ObjSecToIDMap.find(Sec);
1291 if (
I != ObjSecToIDMap.end())
1292 return RTDyld.Sections[
I->second].getLoadAddress();
1305void RuntimeDyld::MemoryManager::anchor() {}
1306void JITSymbolResolver::anchor() {}
1307void LegacyJITSymbolResolver::anchor() {}
1319 ProcessAllSections =
false;
1324static std::unique_ptr<RuntimeDyldCOFF>
1329 std::unique_ptr<RuntimeDyldCOFF> Dyld =
1331 Dyld->setProcessAllSections(ProcessAllSections);
1332 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1336static std::unique_ptr<RuntimeDyldELF>
1340 std::unique_ptr<RuntimeDyldELF> Dyld =
1342 Dyld->setProcessAllSections(ProcessAllSections);
1343 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1347static std::unique_ptr<RuntimeDyldMachO>
1351 bool ProcessAllSections,
1353 std::unique_ptr<RuntimeDyldMachO> Dyld =
1355 Dyld->setProcessAllSections(ProcessAllSections);
1356 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1360std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1366 std::move(NotifyStubEmitted));
1370 std::move(NotifyStubEmitted));
1374 std::move(NotifyStubEmitted));
1379 if (!Dyld->isCompatibleFile(Obj))
1382 auto LoadedObjInfo = Dyld->loadObject(Obj);
1384 return LoadedObjInfo;
1390 return Dyld->getSymbolLocalAddress(
Name);
1394 assert(Dyld &&
"No RuntimeDyld instance attached");
1395 return Dyld->getSymbolSectionID(
Name);
1401 return Dyld->getSymbol(
Name);
1406 return std::map<StringRef, JITEvaluatedSymbol>();
1407 return Dyld->getSymbolTable();
1413 Dyld->reassignSectionAddress(SectionID,
Addr);
1418 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1426 bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1427 MemMgr.FinalizationLocked =
true;
1430 if (!MemoryFinalizationLocked) {
1432 MemMgr.FinalizationLocked =
false;
1437 assert(Dyld &&
"No Dyld instance attached");
1438 return Dyld->getSectionContent(SectionID);
1442 assert(Dyld &&
"No Dyld instance attached");
1443 return Dyld->getSectionLoadAddress(SectionID);
1448 Dyld->registerEHFrames();
1453 Dyld->deregisterEHFrames();
1461 bool ProcessAllSections,
1464 std::map<StringRef, JITEvaluatedSymbol>)>
1467 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>,
Error)>
1476 OnEmitted(std::move(O), std::move(
Info),
1483 OnEmitted(std::move(O), std::move(
Info), std::move(Err));
1488 std::move(O), std::move(
Info));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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)
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 LLVM_ABI 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.
LLVM_ABI 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,...
LLVM_ABI void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
LLVM_ABI uint64_t getSectionLoadAddress(unsigned SectionID) const
If the section was loaded, return the section's load address, otherwise return std::nullopt.
LLVM_ABI void * getSymbolLocalAddress(StringRef Name) const
Get the address of our local copy of the symbol.
LLVM_ABI std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
Returns a copy of the symbol table.
LLVM_ABI void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
LLVM_ABI void finalizeWithMemoryManagerLocking()
Perform all actions needed to make the code owned by this RuntimeDyld instance executable:
LLVM_ABI void deregisterEHFrames()
LLVM_ABI void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
LLVM_ABI StringRef getSectionContent(unsigned SectionID) const
Returns the section's working memory.
LLVM_ABI JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
LLVM_ABI RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Construct a RuntimeDyld instance.
LLVM_ABI std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated.
LLVM_ABI StringRef getErrorString()
LLVM_ABI 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.
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.
LLVM_ABI uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI 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)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
static bool isReadOnlyData(const SectionRef Section)
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...
LLVM_ABI 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)
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.
const char * toString(DWARFSectionKind Kind)
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