31 #define DEBUG_TYPE "dyld" 36 GenericRTDyldError = 1
44 const char *
name()
const noexcept
override {
return "runtimedyld"; }
46 std::string message(
int Condition)
const override {
47 switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
48 case GenericRTDyldError:
return "Generic RuntimeDyld error";
65 return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
79 MemMgr.deregisterEHFrames();
84 dbgs() <<
"----- Contents of section " << S.
getName() <<
" " << State
88 dbgs() <<
"\n <section not emitted>\n";
92 const unsigned ColsPerRow = 16;
97 unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
98 unsigned BytesRemaining = S.
getSize();
102 LoadAddr & ~(uint64_t)(ColsPerRow - 1)) <<
":";
103 while (StartPadding--)
107 while (BytesRemaining > 0) {
108 if ((LoadAddr & (ColsPerRow - 1)) == 0)
109 dbgs() <<
"\n" <<
format(
"0x%016" PRIx64, LoadAddr) <<
":";
124 std::lock_guard<sys::Mutex> locked(lock);
127 LLVM_DEBUG(
for (
int i = 0,
e = Sections.size(); i !=
e; ++i)
131 if (
auto Err = resolveExternalSymbols()) {
133 ErrorStr =
toString(std::move(Err));
136 resolveLocalRelocations();
139 LLVM_DEBUG(
for (
int i = 0,
e = Sections.size(); i !=
e; ++i)
145 for (
auto it = Relocations.begin(),
e = Relocations.end(); it !=
e; ++it) {
150 uint64_t Addr = Sections[Idx].getLoadAddress();
151 LLVM_DEBUG(
dbgs() <<
"Resolving relocations Section #" << Idx <<
"\t" 152 <<
format(
"%p", (uintptr_t)Addr) <<
"\n");
153 resolveRelocationList(it->second, Addr);
159 uint64_t TargetAddress) {
160 std::lock_guard<sys::Mutex> locked(lock);
161 for (
unsigned i = 0,
e = Sections.size(); i !=
e; ++i) {
162 if (Sections[i].getAddress() == LocalAddress) {
163 reassignSectionAddress(i, TargetAddress);
181 std::lock_guard<sys::Mutex> locked(lock);
190 if (MemMgr.needsToReserveAllocationSpace()) {
191 uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
192 uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
193 if (
auto Err = computeTotalAllocSize(Obj,
195 RODataSize, RODataAlign,
196 RWDataSize, RWDataAlign))
197 return std::move(Err);
198 MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
199 RWDataSize, RWDataAlign);
208 uint64_t CommonSize = 0;
216 for (
auto &Sym : Obj.
symbols()) {
220 if (
auto NameOrErr = Sym.getName())
221 Symbols.insert(*NameOrErr);
223 return NameOrErr.takeError();
227 if (
auto ResultOrErr =
Resolver.getResponsibilitySet(Symbols))
228 ResponsibilitySet = std::move(*ResultOrErr);
230 return ResultOrErr.takeError();
245 if (
auto SymTypeOrErr =
I->getType())
246 SymType = *SymTypeOrErr;
248 return SymTypeOrErr.takeError();
252 if (
auto NameOrErr =
I->getName())
255 return NameOrErr.takeError();
258 auto JITSymFlags = getJITSymbolFlags(*
I);
260 return JITSymFlags.takeError();
266 if (JITSymFlags->isWeak() || JITSymFlags->isCommon()) {
268 if (GlobalSymbolTable.count(Name))
272 if (!ResponsibilitySet.count(Name))
277 if (JITSymFlags->isWeak())
279 if (JITSymFlags->isCommon()) {
282 uint64_t
Size =
I->getCommonSize();
286 CommonSymbolsToAllocate.push_back(*
I);
293 if (
auto AddrOrErr =
I->getAddress())
296 return AddrOrErr.takeError();
298 unsigned SectionID = AbsoluteSymbolSection;
300 LLVM_DEBUG(
dbgs() <<
"\tType: " << SymType <<
" (absolute) Name: " << Name
301 <<
" SID: " << SectionID
302 <<
" Offset: " <<
format(
"%p", (uintptr_t)Addr)
303 <<
" flags: " << Flags <<
"\n");
311 if (
auto SIOrErr =
I->getSection())
314 return SIOrErr.takeError();
321 if (
auto Err =
getOffset(*
I, *SI, SectOffset))
322 return std::move(Err);
324 bool IsCode = SI->isText();
326 if (
auto SectionIDOrErr =
327 findOrEmitSection(Obj, *SI, IsCode, LocalSections))
328 SectionID = *SectionIDOrErr;
330 return SectionIDOrErr.takeError();
333 <<
" SID: " << SectionID
334 <<
" Offset: " <<
format(
"%p", (uintptr_t)SectOffset)
335 <<
" flags: " << Flags <<
"\n");
336 GlobalSymbolTable[
Name] =
342 if (
auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize,
344 return std::move(Err);
357 if (RelocatedSection == SE)
363 if (I == E && !ProcessAllSections)
366 bool IsCode = RelocatedSection->isText();
367 unsigned SectionID = 0;
368 if (
auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
370 SectionID = *SectionIDOrErr;
372 return SectionIDOrErr.takeError();
377 if (
auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
380 return IOrErr.takeError();
384 if (NotifyStubEmitted) {
387 for (
auto &KV : Stubs) {
390 uint64_t StubAddr = KV.second;
394 NotifyStubEmitted(FileName, SectionName, VR.SymbolName, SectionID,
400 for (
auto &GSTMapEntry : GlobalSymbolTable) {
402 auto &GSTEntry = GSTMapEntry.second;
403 if (GSTEntry.getSectionID() == VR.SectionID &&
404 GSTEntry.getOffset() == VR.Offset) {
405 NotifyStubEmitted(FileName, SectionName, SymbolName, SectionID,
415 if (ProcessAllSections) {
421 if (LocalSections.find(*
SI) != LocalSections.end())
424 bool IsCode =
SI->isText();
425 if (
auto SectionIDOrErr =
426 findOrEmitSection(Obj, *
SI, IsCode, LocalSections))
427 LLVM_DEBUG(
dbgs() <<
"\tSectionID: " << (*SectionIDOrErr) <<
"\n");
429 return SectionIDOrErr.takeError();
434 if (
auto Err = finalizeLoad(Obj, LocalSections))
435 return std::move(Err);
440 return LocalSections;
448 uint64_t Alignment) {
449 uint64_t TotalSize = 0;
450 for (
size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
451 uint64_t AlignedSize =
452 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
453 TotalSize += AlignedSize;
460 if (isa<object::ELFObjectFileBase>(Obj))
462 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
463 const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
474 return HasContent && !IsDiscardable;
477 assert(isa<MachOObjectFile>(Obj));
483 if (isa<object::ELFObjectFileBase>(Obj))
486 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
487 return ((COFFObj->getCOFFSection(Section)->Characteristics &
495 assert(isa<MachOObjectFile>(Obj));
501 if (isa<object::ELFObjectFileBase>(Obj))
503 if (
auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
504 return COFFObj->getCOFFSection(Section)->Characteristics &
507 auto *MachO = cast<MachOObjectFile>(Obj);
508 unsigned SectionType = MachO->getSectionType(Section);
518 uint64_t &RODataSize,
520 uint64_t &RWDataSize,
523 std::vector<uint64_t> CodeSectionSizes;
524 std::vector<uint64_t> ROSectionSizes;
525 std::vector<uint64_t> RWSectionSizes;
537 uint64_t DataSize = Section.
getSize();
539 unsigned Alignment = (
unsigned)Alignment64 & 0xffffffffL;
540 bool IsCode = Section.
isText();
548 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
550 uint64_t PaddingSize = 0;
551 if (Name ==
".eh_frame")
553 if (StubBufSize != 0)
554 PaddingSize += getStubAlignment() - 1;
556 uint64_t
SectionSize = DataSize + PaddingSize + StubBufSize;
563 if (Name ==
".eh_frame")
570 CodeAlign =
std::max(CodeAlign, Alignment);
571 CodeSectionSizes.push_back(SectionSize);
572 }
else if (IsReadOnly) {
573 RODataAlign =
std::max(RODataAlign, Alignment);
574 ROSectionSizes.push_back(SectionSize);
576 RWDataAlign =
std::max(RWDataAlign, Alignment);
577 RWSectionSizes.push_back(SectionSize);
585 if (
unsigned GotSize = computeGOTSize(Obj)) {
586 RWSectionSizes.push_back(GotSize);
587 RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize());
591 uint64_t CommonSize = 0;
598 uint64_t
Size =
I->getCommonSize();
607 if (CommonSize != 0) {
608 RWSectionSizes.push_back(CommonSize);
609 RWDataAlign =
std::max(RWDataAlign, CommonAlign);
626 size_t GotEntrySize = getGOTEntrySize();
635 if (relocationNeedsGot(Reloc))
636 GotSize += GotEntrySize;
645 unsigned StubSize = getMaxStubSize();
652 unsigned StubBufSize = 0;
661 if (!(RelSecI == Section))
665 if (relocationNeedsStub(Reloc))
666 StubBufSize += StubSize;
670 uint64_t DataSize = Section.
getSize();
674 unsigned Alignment = (
unsigned)Alignment64 & 0xffffffffL;
675 unsigned StubAlignment = getStubAlignment();
676 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
677 if (StubAlignment > EndAlignment)
678 StubBufSize += StubAlignment - EndAlignment;
683 unsigned Size)
const {
685 if (IsTargetLittleEndian) {
688 Result = (Result << 8) | *Src--;
691 Result = (Result << 8) | *Src++;
697 unsigned Size)
const {
698 if (IsTargetLittleEndian) {
700 *Dst++ = Value & 0xFF;
706 *Dst-- = Value & 0xFF;
721 if (SymbolsToAllocate.empty())
725 unsigned SectionID = Sections.size();
726 uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
727 "<common symbols>",
false);
732 SectionEntry(
"<common symbols>", Addr, CommonSize, CommonSize, 0));
733 memset(Addr, 0, CommonSize);
736 <<
" new addr: " <<
format(
"%p", Addr)
737 <<
" DataSize: " << CommonSize <<
"\n");
740 for (
auto &Sym : SymbolsToAllocate) {
741 uint32_t Alignment = Sym.getAlignment();
742 uint64_t
Size = Sym.getCommonSize();
744 if (
auto NameOrErr = Sym.getName())
747 return NameOrErr.takeError();
750 uint64_t AlignOffset =
753 Offset += AlignOffset;
755 auto JITSymFlags = getJITSymbolFlags(Sym);
758 return JITSymFlags.takeError();
760 LLVM_DEBUG(
dbgs() <<
"Allocating common symbol " << Name <<
" address " 761 <<
format(
"%p", Addr) <<
"\n");
762 GlobalSymbolTable[
Name] =
778 unsigned Alignment = (
unsigned)Alignment64 & 0xffffffffL;
779 unsigned PaddingSize = 0;
780 unsigned StubBufSize = 0;
785 uint64_t DataSize = Section.
getSize();
790 Alignment =
std::max(1u, Alignment);
797 StubBufSize = computeSectionStubBufSize(Obj, Section);
802 if (Name ==
".eh_frame")
806 unsigned SectionID = Sections.size();
808 const char *pData =
nullptr;
812 if (!IsVirtual && !IsZeroInit) {
818 return E.takeError();
825 if (StubBufSize != 0) {
826 Alignment =
std::max(Alignment, getStubAlignment());
827 PaddingSize += getStubAlignment() - 1;
832 if (IsRequired || ProcessAllSections) {
833 Allocate = DataSize + PaddingSize + StubBufSize;
836 Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
838 : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
844 if (IsZeroInit || IsVirtual)
845 memset(Addr, 0, DataSize);
847 memcpy(Addr, pData, DataSize);
850 if (PaddingSize != 0) {
851 memset(Addr + DataSize, 0, PaddingSize);
853 DataSize += PaddingSize;
858 DataSize &= -(uint64_t)getStubAlignment();
861 LLVM_DEBUG(
dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: " 862 << Name <<
" obj addr: " <<
format(
"%p", pData)
863 <<
" new addr: " <<
format(
"%p", Addr) <<
" DataSize: " 864 << DataSize <<
" StubBufSize: " << StubBufSize
865 <<
" Allocate: " << Allocate <<
"\n");
873 dbgs() <<
"emitSection SectionID: " << SectionID <<
" Name: " << Name
874 <<
" obj addr: " <<
format(
"%p", data.
data()) <<
" new addr: 0" 875 <<
" DataSize: " << DataSize <<
" StubBufSize: " << StubBufSize
876 <<
" Allocate: " << Allocate <<
"\n");
880 SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
884 Sections.back().setLoadAddress(0);
895 unsigned SectionID = 0;
896 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
897 if (i != LocalSections.end())
898 SectionID = i->second;
900 if (
auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
901 SectionID = *SectionIDOrErr;
903 return SectionIDOrErr.takeError();
904 LocalSections[
Section] = SectionID;
910 unsigned SectionID) {
911 Relocations[SectionID].push_back(RE);
920 if (Loc == GlobalSymbolTable.end()) {
921 ExternalSymbolRelocations[
SymbolName].push_back(RE);
925 const auto &
SymInfo = Loc->second;
927 Relocations[
SymInfo.getSectionID()].push_back(RECopy);
932 unsigned AbiVariant) {
939 writeBytesUnaligned(0xd2e00010, Addr, 4);
940 writeBytesUnaligned(0xf2c00010, Addr+4, 4);
941 writeBytesUnaligned(0xf2a00010, Addr+8, 4);
942 writeBytesUnaligned(0xf2800010, Addr+12, 4);
943 writeBytesUnaligned(0xd61f0200, Addr+16, 4);
949 writeBytesUnaligned(0xe51ff004, Addr, 4);
951 }
else if (IsMipsO32ABI || IsMipsN32ABI) {
956 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
957 const unsigned NopInstr = 0x0;
958 unsigned JrT9Instr = 0x03200008;
961 JrT9Instr = 0x03200009;
963 writeBytesUnaligned(LuiT9Instr, Addr, 4);
964 writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4);
965 writeBytesUnaligned(JrT9Instr, Addr + 8, 4);
966 writeBytesUnaligned(NopInstr, Addr + 12, 4);
968 }
else if (IsMipsN64ABI) {
977 const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
978 DsllT9Instr = 0x19CC38;
979 const unsigned NopInstr = 0x0;
980 unsigned JrT9Instr = 0x03200008;
982 JrT9Instr = 0x03200009;
984 writeBytesUnaligned(LuiT9Instr, Addr, 4);
985 writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4);
986 writeBytesUnaligned(DsllT9Instr, Addr + 8, 4);
987 writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4);
988 writeBytesUnaligned(DsllT9Instr, Addr + 16, 4);
989 writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4);
990 writeBytesUnaligned(JrT9Instr, Addr + 24, 4);
991 writeBytesUnaligned(NopInstr, Addr + 28, 4);
997 writeInt32BE(Addr, 0x3D800000);
998 writeInt32BE(Addr+4, 0x618C0000);
999 writeInt32BE(Addr+8, 0x798C07C6);
1000 writeInt32BE(Addr+12, 0x658C0000);
1001 writeInt32BE(Addr+16, 0x618C0000);
1002 if (AbiVariant == 2) {
1005 writeInt32BE(Addr+20, 0xF8410018);
1006 writeInt32BE(Addr+24, 0x7D8903A6);
1007 writeInt32BE(Addr+28, 0x4E800420);
1012 writeInt32BE(Addr+20, 0xF8410028);
1013 writeInt32BE(Addr+24, 0xE96C0000);
1014 writeInt32BE(Addr+28, 0xE84C0008);
1015 writeInt32BE(Addr+32, 0x7D6903A6);
1016 writeInt32BE(Addr+36, 0xE96C0010);
1017 writeInt32BE(Addr+40, 0x4E800420);
1021 writeInt16BE(Addr, 0xC418);
1022 writeInt16BE(Addr+2, 0x0000);
1023 writeInt16BE(Addr+4, 0x0004);
1024 writeInt16BE(Addr+6, 0x07F1);
1052 dbgs() <<
"Reassigning address for section " << SectionID <<
" (" 1053 << Sections[SectionID].
getName() <<
"): " 1054 <<
format(
"0x%016" PRIx64, Sections[SectionID].getLoadAddress())
1055 <<
" -> " <<
format(
"0x%016" PRIx64, Addr) <<
"\n");
1056 Sections[SectionID].setLoadAddress(Addr);
1061 for (
unsigned i = 0,
e = Relocs.
size(); i !=
e; ++i) {
1064 if (Sections[RE.
SectionID].getAddress() ==
nullptr)
1066 resolveRelocation(RE, Value);
1072 while (!ExternalSymbolRelocations.empty()) {
1077 if (Name.
size() == 0) {
1082 resolveRelocationList(Relocs, 0);
1087 if (Loc == GlobalSymbolTable.end()) {
1088 auto RRI = ExternalSymbolMap.
find(Name);
1089 assert(RRI != ExternalSymbolMap.
end() &&
"No result for symbol");
1090 Addr = RRI->second.getAddress();
1091 Flags = RRI->second.getFlags();
1098 i = ExternalSymbolRelocations.find(Name);
1102 const auto &
SymInfo = Loc->second;
1103 Addr = getSectionLoadAddress(
SymInfo.getSectionID()) +
1111 "' which could not be resolved!");
1120 Addr = modifyAddressBasedOnFlags(Addr, Flags);
1122 LLVM_DEBUG(
dbgs() <<
"Resolving relocations Name: " << Name <<
"\t" 1123 <<
format(
"0x%lx", Addr) <<
"\n");
1127 resolveRelocationList(Relocs, Addr);
1131 ExternalSymbolRelocations.erase(i);
1146 for (
auto &RelocKV : ExternalSymbolRelocations) {
1148 if (!Name.
empty() && !GlobalSymbolTable.count(Name) &&
1149 !ResolvedSymbols.count(Name))
1150 NewSymbols.insert(Name);
1153 if (NewSymbols.empty())
1157 using ExpectedLookupResult =
1163 auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>();
1164 auto NewSymbolsF = NewSymbolsP->get_future();
1167 NewSymbolsP->set_value(std::move(Result));
1170 auto NewResolverResults = NewSymbolsF.get();
1172 if (!NewResolverResults)
1173 return NewResolverResults.takeError();
1175 assert(NewResolverResults->size() == NewSymbols.size() &&
1176 "Should have errored on unresolved symbols");
1178 for (
auto &RRKV : *NewResolverResults) {
1179 assert(!ResolvedSymbols.count(RRKV.first) &&
"Redundant resolution?");
1180 ExternalSymbolMap.
insert(RRKV);
1181 ResolvedSymbols.insert(RRKV.first);
1186 applyExternalSymbolRelocations(ExternalSymbolMap);
1192 std::unique_ptr<RuntimeDyldImpl> This,
1194 std::unique_ptr<MemoryBuffer> UnderlyingBuffer) {
1196 auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This));
1197 auto PostResolveContinuation =
1198 [SharedThis, OnEmitted = std::move(OnEmitted),
1199 UnderlyingBuffer = std::move(UnderlyingBuffer)](
1202 OnEmitted(Result.takeError());
1208 for (
auto &KV : *Result)
1209 Resolved[KV.first] = KV.second;
1211 SharedThis->applyExternalSymbolRelocations(Resolved);
1212 SharedThis->resolveLocalRelocations();
1213 SharedThis->registerEHFrames();
1215 if (SharedThis->MemMgr.finalizeMemory(&ErrMsg))
1216 OnEmitted(make_error<StringError>(std::move(ErrMsg),
1224 for (
auto &RelocKV : SharedThis->ExternalSymbolRelocations) {
1227 assert(!SharedThis->GlobalSymbolTable.count(Name) &&
1228 "Name already processed. RuntimeDyld instances can not be re-used " 1229 "when finalizing with finalizeAsync.");
1230 Symbols.insert(Name);
1233 if (!Symbols.empty()) {
1234 SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation));
1236 PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>());
1245 auto I = ObjSecToIDMap.find(Sec);
1246 if (
I != ObjSecToIDMap.end())
1247 return RTDyld.Sections[
I->second].getLoadAddress();
1252 void RuntimeDyld::MemoryManager::anchor() {}
1253 void JITSymbolResolver::anchor() {}
1254 void LegacyJITSymbolResolver::anchor() {}
1258 : MemMgr(MemMgr), Resolver(Resolver) {
1266 ProcessAllSections =
false;
1271 static std::unique_ptr<RuntimeDyldCOFF>
1276 std::unique_ptr<RuntimeDyldCOFF> Dyld =
1278 Dyld->setProcessAllSections(ProcessAllSections);
1279 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1283 static std::unique_ptr<RuntimeDyldELF>
1287 std::unique_ptr<RuntimeDyldELF> Dyld =
1289 Dyld->setProcessAllSections(ProcessAllSections);
1290 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1294 static std::unique_ptr<RuntimeDyldMachO>
1298 bool ProcessAllSections,
1300 std::unique_ptr<RuntimeDyldMachO> Dyld =
1302 Dyld->setProcessAllSections(ProcessAllSections);
1303 Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1307 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1313 MemMgr,
Resolver, ProcessAllSections,
1314 std::move(NotifyStubEmitted));
1318 ProcessAllSections, std::move(NotifyStubEmitted));
1322 ProcessAllSections, std::move(NotifyStubEmitted));
1327 if (!Dyld->isCompatibleFile(Obj))
1330 auto LoadedObjInfo = Dyld->loadObject(Obj);
1332 return LoadedObjInfo;
1338 return Dyld->getSymbolLocalAddress(Name);
1342 assert(Dyld &&
"No RuntimeDyld instance attached");
1343 return Dyld->getSymbolSectionID(Name);
1349 return Dyld->getSymbol(Name);
1354 return std::map<StringRef, JITEvaluatedSymbol>();
1355 return Dyld->getSymbolTable();
1361 Dyld->reassignSectionAddress(SectionID, Addr);
1365 uint64_t TargetAddress) {
1366 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1374 bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1375 MemMgr.FinalizationLocked =
true;
1378 if (!MemoryFinalizationLocked) {
1380 MemMgr.FinalizationLocked =
false;
1385 assert(Dyld &&
"No Dyld instance attached");
1386 return Dyld->getSectionContent(SectionID);
1390 assert(Dyld &&
"No Dyld instance attached");
1391 return Dyld->getSectionLoadAddress(SectionID);
1396 Dyld->registerEHFrames();
1401 Dyld->deregisterEHFrames();
1407 std::unique_ptr<MemoryBuffer> UnderlyingBuffer,
1411 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObj,
1412 std::map<StringRef, JITEvaluatedSymbol>)>
1428 OnEmitted(std::move(Err));
1431 std::move(UnderlyingBuffer));
static std::unique_ptr< RuntimeDyldCOFF > createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated. ...
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringRef getFileName() const
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
StringRef getErrorString()
unsigned computeGOTSize(const ObjectFile &Obj)
static bool isReadOnlyData(const SectionRef Section)
static std::unique_ptr< RuntimeDyldELF > createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
iterator find(StringRef Key)
This class is the base class for all object file types.
Expected< StringRef > getContents() const
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
uint8_t * getAddress() const
Error takeError()
Take ownership of the stored error.
std::set< StringRef > LookupSet
JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override
Obtain the Load Address of a section by SectionRef.
void deregisterEHFrames()
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 > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
virtual basic_symbol_iterator symbol_begin() const =0
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
unsigned SectionID
SectionID - the section this relocation points to.
std::string toString(Error E)
Write all error messages (if any) in E to a string.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
uint64_t getAddress() const
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value)
Resolves relocations from Relocs list with address from Value.
std::function< void(StringRef FileName, StringRef SectionName, StringRef SymbolName, unsigned SectionID, uint32_t StubOffset)> NotifyStubEmittedFunction
This is a value type class that represents a single relocation in the list of relocations in the obje...
static StringRef getName(Value *V)
bool isText() const
Whether this section contains instructions.
static bool isRequiredForExecution(const SectionRef Section)
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
virtual void registerEHFrames()
uint64_t getSectionLoadAddress(unsigned SectionID) const
If the section was loaded, return the section's load address, otherwise return None.
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
support::ulittle32_t VirtualSize
static uint64_t computeAllocationSizeForSections(std::vector< uint64_t > &SectionSizes, uint64_t Alignment)
friend void jitLinkForORC(object::ObjectFile &Obj, std::unique_ptr< MemoryBuffer > UnderlyingBuffer, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(std::unique_ptr< LoadedObjectInfo >, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(Error)> OnEmitted)
static std::unique_ptr< RuntimeDyldMachO > createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted)
virtual ~RuntimeDyldImpl()
LLVM_NODISCARD size_t size() const
size - Get the string size.
virtual Expected< JITSymbolFlags > getJITSymbolFlags(const SymbolRef &Sym)
Generate JITSymbolFlags from a libObject symbol.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
const ObjectFile * getObject() const
void setProcessAllSections(bool ProcessAllSections)
By default, only sections that are "required for execution" are passed to the RTDyldMemoryManager, and other sections are discarded.
Analysis containing CSE Info
RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Construct a RuntimeDyld instance.
StringRef getSectionContent(unsigned SectionID) const
Returns the section's working memory.
support::ulittle32_t Characteristics
uint64_t getFlags() const
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
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 * getSymbolLocalAddress(StringRef Name) const
Get the address of our local copy of the symbol.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
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 ...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
static ManagedStatic< _object_error_category > error_category
Error computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize, uint32_t &CodeAlign, uint64_t &RODataSize, uint32_t &RODataAlign, uint64_t &RWDataSize, uint32_t &RWDataAlign)
Flags for symbols in the JIT.
Symbol resolution interface.
std::vector< SymbolRef > CommonSymbolList
virtual basic_symbol_iterator symbol_end() const =0
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
support::ulittle32_t SizeOfRawData
Expected< uint64_t > getAddress() const
Returns the symbol virtual address (i.e.
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...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isLittleEndian() const
This struct is a compact representation of a valid (non-zero power of two) alignment.
void applyExternalSymbolRelocations(const StringMap< JITEvaluatedSymbol > ExternalSymbolMap)
static void dumpSectionMemory(const SectionEntry &S, StringRef State)
static ErrorSuccess success()
Create a success value.
Align max(MaybeAlign Lhs, Align Rhs)
unsigned computeSectionStubBufSize(const ObjectFile &Obj, const SectionRef &Section)
virtual section_iterator section_begin() const =0
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
virtual Triple::ArchType getArch() const =0
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Expected< StringRef > getName() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Symbol info for RuntimeDyld.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
Represents a symbol that has been evaluated to an address already.
This is a value type class that represents a single symbol in the list of symbols in the object file...
static bool isZeroInit(const SectionRef Section)
std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
Returns a copy of the symbol table.
symbol_iterator_range symbols() const
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
unsigned getSymbolSectionID(StringRef Name) const
Get the section ID for the section containing the given symbol.
static void finalizeAsync(std::unique_ptr< RuntimeDyldImpl > This, unique_function< void(Error)> OnEmitted, std::unique_ptr< MemoryBuffer > UnderlyingBuffer)
virtual section_iterator section_end() const =0
std::map< SectionRef, unsigned > ObjSectionToIDMap
void resolveRelocations()
void finalizeWithMemoryManagerLocking()
Perform all actions needed to make the code owned by this RuntimeDyld instance executable: ...
static std::unique_ptr< RuntimeDyldCOFF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
void resolveLocalRelocations()
uint64_t getLoadAddress() const
Expected< unsigned > emitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode)
Emits section data from the object file to the MemoryManager.
uint64_t getAlignment() const
Get the alignment of this section as the actual value (not log 2).
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
StringRef getName() const
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
void deregisterEHFrames()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
StringRef - Represent a constant reference to a string, i.e.
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
S_ZEROFILL - Zero fill on demand section.
Error resolveExternalSymbols()
Resolve relocations to external symbols.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
Map a section to its target address space value.
SectionType
These are the section type and attributes fields.
This is a value type class that represents a single section in the list of sections in the object fil...
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...