30 using namespace llvm::object;
31 using namespace llvm::support::endian;
33 #define DEBUG_TYPE "dyld"
38 or32le(L, (Imm & 0xFFF) << 10);
41 template <
class T>
static void write(
bool isBE,
void *
P,
T V) {
42 isBE ? write<T, support::big>(
P, V) : write<T, support::little>(P, V);
47 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
48 uint64_t
Mask = (0x3 << 29) | (0x1FFFFC << 3);
54 static uint64_t
getBits(uint64_t Val,
int Start,
int End) {
55 uint64_t
Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
56 return (Val >> Start) &
Mask;
61 template <
class ELFT>
class DyldELFObject :
public ELFObjectFile<ELFT> {
96 DyldELFObject<ELFT>::DyldELFObject(
MemoryBufferRef Wrapper, std::error_code &EC)
98 this->isDyldELFObject =
true;
101 template <
class ELFT>
102 void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
106 const_cast<Elf_Shdr *
>(
reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
110 shdr->sh_addr =
static_cast<addr_type
>(Addr);
113 template <
class ELFT>
114 void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
117 Elf_Sym *sym =
const_cast<Elf_Sym *
>(
122 sym->st_value =
static_cast<addr_type
>(Addr);
125 class LoadedELFObjectInfo final
127 RuntimeDyld::LoadedObjectInfo> {
129 LoadedELFObjectInfo(
RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
133 getObjectForDebug(
const ObjectFile &Obj)
const override;
136 template <
typename ELFT>
137 std::unique_ptr<DyldELFObject<ELFT>>
140 const LoadedELFObjectInfo &
L,
141 std::error_code &ec) {
145 std::unique_ptr<DyldELFObject<ELFT>> Obj =
146 llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec);
150 for (
const auto &Sec : Obj->sections()) {
153 if (SectionName !=
"") {
155 Elf_Shdr *shdr =
const_cast<Elf_Shdr *
>(
156 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
158 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
161 shdr->sh_addr =
static_cast<addr_type
>(SecLoadAddr);
171 const LoadedELFObjectInfo &L) {
174 std::unique_ptr<MemoryBuffer> Buffer =
179 std::unique_ptr<ObjectFile> DebugObj;
182 DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj,
L,
186 DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj,
L,
190 DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj,
L,
194 DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj,
L,
199 assert(!ec &&
"Could not construct copy ELF object file");
205 LoadedELFObjectInfo::getObjectForDebug(
const ObjectFile &Obj)
const {
206 return createELFDebugObject(Obj, *
this);
215 :
RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
219 for (
int i = 0, e = UnregisteredEHFrameSections.
size(); i != e; ++i) {
220 SID EHFrameSID = UnregisteredEHFrameSections[i];
221 uint8_t *EHFrameAddr =
Sections[EHFrameSID].getAddress();
222 uint64_t EHFrameLoadAddr =
Sections[EHFrameSID].getLoadAddress();
223 size_t EHFrameSize =
Sections[EHFrameSID].getSize();
226 UnregisteredEHFrameSections.
clear();
229 std::unique_ptr<RuntimeDyldELF>
235 return make_unique<RuntimeDyldELF>(MemMgr, Resolver);
240 return make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
244 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
247 return llvm::make_unique<LoadedELFObjectInfo>(*
this, *ObjSectionToIDOrErr);
259 uint64_t SymOffset) {
264 case ELF::R_X86_64_NONE:
266 case ELF::R_X86_64_64: {
273 case ELF::R_X86_64_32:
274 case ELF::R_X86_64_32S: {
276 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
277 (Type == ELF::R_X86_64_32S &&
278 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
279 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
286 case ELF::R_X86_64_PC8: {
288 int64_t RealOffset = Value + Addend - FinalAddress;
290 int8_t TruncOffset = (RealOffset & 0xFF);
294 case ELF::R_X86_64_PC32: {
296 int64_t RealOffset = Value + Addend - FinalAddress;
298 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
303 case ELF::R_X86_64_PC64: {
305 int64_t RealOffset = Value + Addend - FinalAddress;
313 void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
317 case ELF::R_386_32: {
322 case ELF::R_386_PC32: {
325 uint32_t RealOffset = Value + Addend - FinalAddress;
338 void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
339 uint64_t Offset, uint64_t Value,
347 DEBUG(
dbgs() <<
"resolveAArch64Relocation, LocalAddress: 0x"
349 <<
" FinalAddress: 0x" <<
format(
"%llx", FinalAddress)
350 <<
" Value: 0x" <<
format(
"%llx", Value) <<
" Type: 0x"
351 <<
format(
"%x", Type) <<
" Addend: 0x" <<
format(
"%llx", Addend)
358 case ELF::R_AARCH64_ABS64:
359 write(isBE, TargetPtr, Value + Addend);
361 case ELF::R_AARCH64_PREL32: {
362 uint64_t Result = Value + Addend - FinalAddress;
363 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
364 static_cast<int64_t>(Result) <= UINT32_MAX);
365 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
368 case ELF::R_AARCH64_PREL64:
369 write(isBE, TargetPtr, Value + Addend - FinalAddress);
371 case ELF::R_AARCH64_CALL26:
372 case ELF::R_AARCH64_JUMP26: {
375 uint64_t BranchImm = Value + Addend - FinalAddress;
378 assert(isInt<28>(BranchImm));
379 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
382 case ELF::R_AARCH64_MOVW_UABS_G3:
383 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
385 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
386 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
388 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
389 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
391 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
392 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
394 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
397 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
400 assert(isInt<33>(Result) &&
"overflow check failed for relocation");
407 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
413 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
419 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
425 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
431 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
437 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
446 void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
455 DEBUG(
dbgs() <<
"resolveARMRelocation, LocalAddress: "
457 <<
" FinalAddress: " <<
format(
"%p", FinalAddress) <<
" Value: "
458 <<
format(
"%x", Value) <<
" Type: " <<
format(
"%x", Type)
459 <<
" Addend: " <<
format(
"%x", Addend) <<
"\n");
465 case ELF::R_ARM_NONE:
468 case ELF::R_ARM_PREL31:
471 ((Value - FinalAddress) & ~0x80000000);
473 case ELF::R_ARM_TARGET1:
474 case ELF::R_ARM_ABS32:
479 case ELF::R_ARM_MOVW_ABS_NC:
480 case ELF::R_ARM_MOVT_ABS:
481 if (Type == ELF::R_ARM_MOVW_ABS_NC)
482 Value = Value & 0xFFFF;
483 else if (Type == ELF::R_ARM_MOVT_ABS)
484 Value = (Value >> 16) & 0xFFFF;
487 (((Value >> 12) & 0xF) << 16);
490 case ELF::R_ARM_PC24:
491 case ELF::R_ARM_CALL:
492 case ELF::R_ARM_JUMP24:
493 int32_t RelValue =
static_cast<int32_t
>(Value - FinalAddress - 8);
494 RelValue = (RelValue & 0x03FFFFFC) >> 2;
502 void RuntimeDyldELF::setMipsABI(
const ObjectFile &Obj) {
519 ObjSectionToIDMap &LocalSections,
531 for (
auto &Section: Obj.
sections()) {
533 if (
auto EC = Section.
getName(SectionName))
536 if (SectionName ==
".got"
537 || SectionName ==
".toc"
538 || SectionName ==
".tocbss"
539 || SectionName ==
".plt") {
540 if (
auto SectionIDOrErr =
544 return SectionIDOrErr.takeError();
559 ObjSectionToIDMap &LocalSections,
570 if (
auto EC = RelSecI->getName(RelSectionName))
573 if (RelSectionName !=
".opd")
577 e = si->relocation_end();
581 uint64_t TypeFunc = i->getType();
582 if (TypeFunc != ELF::R_PPC64_ADDR64) {
587 uint64_t TargetSymbolOffset = i->getOffset();
590 if (
auto AddendOrErr = i->getAddend())
591 Addend = *AddendOrErr;
600 uint64_t TypeTOC = i->getType();
601 if (TypeTOC != ELF::R_PPC64_TOC)
607 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
611 if (
auto TSIOrErr = TargetSymbol->
getSection())
614 return TSIOrErr.takeError();
617 bool IsCode = TSI->isText();
622 return SectionIDOrErr.takeError();
635 static inline uint16_t
applyPPClo(uint64_t value) {
return value & 0xffff; }
638 return (value >> 16) & 0xffff;
642 return ((value + 0x8000) >> 16) & 0xffff;
646 return (value >> 32) & 0xffff;
650 return ((value + 0x8000) >> 32) & 0xffff;
654 return (value >> 48) & 0xffff;
658 return ((value + 0x8000) >> 48) & 0xffff;
661 void RuntimeDyldELF::resolvePPC32Relocation(
const SectionEntry &Section,
662 uint64_t Offset, uint64_t Value,
669 case ELF::R_PPC_ADDR16_LO:
672 case ELF::R_PPC_ADDR16_HI:
675 case ELF::R_PPC_ADDR16_HA:
681 void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
682 uint64_t Offset, uint64_t Value,
689 case ELF::R_PPC64_ADDR16:
692 case ELF::R_PPC64_ADDR16_DS:
695 case ELF::R_PPC64_ADDR16_LO:
698 case ELF::R_PPC64_ADDR16_LO_DS:
701 case ELF::R_PPC64_ADDR16_HI:
704 case ELF::R_PPC64_ADDR16_HA:
707 case ELF::R_PPC64_ADDR16_HIGHER:
710 case ELF::R_PPC64_ADDR16_HIGHERA:
713 case ELF::R_PPC64_ADDR16_HIGHEST:
716 case ELF::R_PPC64_ADDR16_HIGHESTA:
719 case ELF::R_PPC64_ADDR14: {
720 assert(((Value + Addend) & 3) == 0);
722 uint8_t aalk = *(LocalAddress + 3);
723 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
725 case ELF::R_PPC64_REL16_LO: {
727 uint64_t Delta = Value - FinalAddress + Addend;
730 case ELF::R_PPC64_REL16_HI: {
732 uint64_t Delta = Value - FinalAddress + Addend;
735 case ELF::R_PPC64_REL16_HA: {
737 uint64_t Delta = Value - FinalAddress + Addend;
740 case ELF::R_PPC64_ADDR32: {
741 int64_t Result =
static_cast<int64_t
>(Value + Addend);
742 if (SignExtend64<32>(Result) != Result)
746 case ELF::R_PPC64_REL24: {
748 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
749 if (SignExtend64<26>(delta) != delta)
752 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
754 case ELF::R_PPC64_REL32: {
756 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
757 if (SignExtend64<32>(delta) != delta)
761 case ELF::R_PPC64_REL64: {
763 uint64_t Delta = Value - FinalAddress + Addend;
766 case ELF::R_PPC64_ADDR64:
772 void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
773 uint64_t Offset, uint64_t Value,
780 case ELF::R_390_PC16DBL:
781 case ELF::R_390_PLT16DBL: {
783 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
787 case ELF::R_390_PC32DBL:
788 case ELF::R_390_PLT32DBL: {
790 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
794 case ELF::R_390_PC16: {
796 assert(int16_t(Delta) == Delta &&
"R_390_PC16 overflow");
800 case ELF::R_390_PC32: {
802 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
806 case ELF::R_390_PC64: {
812 *LocalAddress = (uint8_t)(Value + Addend);
826 void RuntimeDyldELF::resolveBPFRelocation(
const SectionEntry &Section,
827 uint64_t Offset, uint64_t Value,
835 case ELF::R_BPF_NONE:
837 case ELF::R_BPF_64_64: {
843 case ELF::R_BPF_64_32: {
845 assert(Value <= UINT32_MAX);
881 void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
882 uint64_t Offset, uint64_t Value,
884 uint64_t SymOffset, SID SectionID) {
887 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
890 resolveX86Relocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
895 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
901 resolveARMRelocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
905 resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
909 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
912 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
916 resolveBPFRelocation(Section, Offset, Value, Type, Addend);
923 void *RuntimeDyldELF::computePlaceholderAddress(
unsigned SectionID, uint64_t Offset)
const {
927 void RuntimeDyldELF::processSimpleRelocation(
unsigned SectionID, uint64_t Offset,
unsigned RelType,
RelocationValueRef Value) {
936 bool IsLocal)
const {
938 case ELF::R_MICROMIPS_GOT16:
940 return ELF::R_MICROMIPS_LO16;
942 case ELF::R_MICROMIPS_HI16:
943 return ELF::R_MICROMIPS_LO16;
944 case ELF::R_MIPS_GOT16:
946 return ELF::R_MIPS_LO16;
948 case ELF::R_MIPS_HI16:
949 return ELF::R_MIPS_LO16;
950 case ELF::R_MIPS_PCHI16:
951 return ELF::R_MIPS_PCLO16;
955 return ELF::R_MIPS_NONE;
967 bool RuntimeDyldELF::resolveAArch64ShortBranch(
978 const auto &
SymInfo = Loc->second;
985 uint64_t Offset = RelI->getOffset();
986 uint64_t SourceAddress =
Sections[SectionID].getLoadAddressWithOffset(Offset);
991 if (!isInt<28>(Address + Value.
Addend - SourceAddress))
994 resolveRelocation(
Sections[SectionID], Offset, Address, RelI->getType(),
1000 void RuntimeDyldELF::resolveAArch64Branch(
unsigned SectionID,
1005 DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
1008 uint64_t Offset = RelI->getOffset();
1009 unsigned RelType = RelI->getType();
1011 StubMap::const_iterator i = Stubs.find(Value);
1012 if (i != Stubs.end()) {
1013 resolveRelocation(Section, Offset,
1016 DEBUG(
dbgs() <<
" Stub function found\n");
1017 }
else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1019 DEBUG(
dbgs() <<
" Create a new stub function\n");
1025 ELF::R_AARCH64_MOVW_UABS_G3, Value.
Addend);
1028 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.
Addend);
1031 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.
Addend);
1034 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.
Addend);
1047 resolveRelocation(Section, Offset,
1059 const auto &Obj = cast<ELFObjectFileBase>(O);
1060 uint64_t RelType = RelI->getType();
1062 int64_t Addend = AddendOrErr ? *AddendOrErr : 0;
1068 if (
auto TargetNameOrErr = Symbol->
getName())
1069 TargetName = *TargetNameOrErr;
1071 return TargetNameOrErr.takeError();
1073 DEBUG(
dbgs() <<
"\t\tRelType: " << RelType <<
" Addend: " << Addend
1074 <<
" TargetName: " << TargetName <<
"\n");
1084 if (!SymTypeOrErr) {
1091 SymType = *SymTypeOrErr;
1094 const auto &
SymInfo = gsi->second;
1100 case SymbolRef::ST_Debug: {
1105 if (!SectionOrErr) {
1115 DEBUG(
dbgs() <<
"\t\tThis is section symbol\n");
1116 bool isCode = si->isText();
1121 return SectionIDOrErr.takeError();
1125 case SymbolRef::ST_Data:
1126 case SymbolRef::ST_Function:
1127 case SymbolRef::ST_Unknown: {
1145 uint64_t Offset = RelI->getOffset();
1147 DEBUG(
dbgs() <<
"\t\tSectionID: " << SectionID <<
" Offset: " << Offset
1150 if (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26) {
1151 resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1152 }
else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1155 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1156 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1157 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1159 }
else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1160 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1161 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1162 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1164 processSimpleRelocation(SectionID, Offset, RelType, Value);
1167 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1168 RelType == ELF::R_ARM_JUMP24) {
1170 DEBUG(
dbgs() <<
"\t\tThis is an ARM branch relocation.\n");
1174 StubMap::const_iterator i = Stubs.find(Value);
1175 if (i != Stubs.end()) {
1180 DEBUG(
dbgs() <<
" Stub function found\n");
1183 DEBUG(
dbgs() <<
" Create a new stub function\n");
1188 ELF::R_ARM_ABS32, Value.
Addend);
1194 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1202 reinterpret_cast<uint32_t*
>(computePlaceholderAddress(SectionID, Offset));
1203 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1204 RelType == ELF::R_ARM_ABS32) {
1205 Value.
Addend += *Placeholder;
1206 }
else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1208 Value.
Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1210 processSimpleRelocation(SectionID, Offset, RelType, Value);
1213 uint8_t *Placeholder =
reinterpret_cast<uint8_t *
>(
1214 computePlaceholderAddress(SectionID, Offset));
1216 if (RelType == ELF::R_MIPS_26) {
1218 DEBUG(
dbgs() <<
"\t\tThis is a Mips branch relocation.");
1224 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1229 StubMap::const_iterator i = Stubs.find(Value);
1230 if (i != Stubs.end()) {
1233 DEBUG(
dbgs() <<
" Stub function found\n");
1236 DEBUG(
dbgs() <<
" Create a new stub function\n");
1239 unsigned AbiVariant;
1247 ELF::R_MIPS_HI16, Value.
Addend);
1250 ELF::R_MIPS_LO16, Value.
Addend);
1265 }
else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1266 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1268 PendingRelocs.push_back(std::make_pair(Value, RE));
1269 }
else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1270 int64_t Addend = Value.
Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1271 for (
auto I = PendingRelocs.begin();
I != PendingRelocs.end();) {
1274 if (MatchingValue == Value &&
1275 RelType == getMatchingLoRelocation(Reloc.
RelType) &&
1282 I = PendingRelocs.erase(
I);
1292 if (RelType == ELF::R_MIPS_32)
1294 else if (RelType == ELF::R_MIPS_PC16)
1295 Value.
Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1296 else if (RelType == ELF::R_MIPS_PC19_S2)
1297 Value.
Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1298 else if (RelType == ELF::R_MIPS_PC21_S2)
1299 Value.
Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1300 else if (RelType == ELF::R_MIPS_PC26_S2)
1301 Value.
Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1302 processSimpleRelocation(SectionID, Offset, RelType, Value);
1307 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1308 || r_type == ELF::R_MIPS_GOT_DISP) {
1310 if (i != GOTSymbolOffsets.
end())
1314 GOTSymbolOffsets[TargetName] = RE.
SymOffset;
1322 if (RelType == ELF::R_PPC64_REL24) {
1324 unsigned AbiVariant;
1333 bool RangeOverflow =
false;
1334 if (!Value.
SymbolName && SymType != SymbolRef::ST_Unknown) {
1335 if (AbiVariant != 2) {
1339 if (
auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1340 return std::move(Err);
1344 uint8_t SymOther =
Symbol->getOther();
1347 uint8_t *RelocTarget =
1349 int64_t delta =
static_cast<int64_t
>(Target - RelocTarget);
1351 if (SignExtend64<26>(delta) == delta) {
1353 addRelocationForSection(RE, Value.
SectionID);
1355 RangeOverflow =
true;
1358 if (Value.
SymbolName || SymType == SymbolRef::ST_Unknown ||
1362 StubMap::const_iterator i = Stubs.find(Value);
1363 if (i != Stubs.end()) {
1365 resolveRelocation(Section, Offset,
1366 reinterpret_cast<uint64_t>(
1369 DEBUG(
dbgs() <<
" Stub function found\n");
1372 DEBUG(
dbgs() <<
" Create a new stub function\n");
1374 uint8_t *StubTargetAddr = createStubFunction(
1378 ELF::R_PPC64_ADDR64, Value.
Addend);
1384 uint64_t StubRelocOffset = StubTargetAddr - Section.
getAddress();
1385 if (!IsTargetLittleEndian)
1386 StubRelocOffset += 2;
1389 ELF::R_PPC64_ADDR16_HIGHEST, Value.
Addend);
1391 ELF::R_PPC64_ADDR16_HIGHER, Value.
Addend);
1393 ELF::R_PPC64_ADDR16_HI, Value.
Addend);
1395 ELF::R_PPC64_ADDR16_LO, Value.
Addend);
1398 addRelocationForSymbol(REhst, Value.
SymbolName);
1399 addRelocationForSymbol(REhr, Value.
SymbolName);
1400 addRelocationForSymbol(REh, Value.
SymbolName);
1401 addRelocationForSymbol(REl, Value.
SymbolName);
1403 addRelocationForSection(REhst, Value.
SectionID);
1404 addRelocationForSection(REhr, Value.
SectionID);
1405 addRelocationForSection(REh, Value.
SectionID);
1406 addRelocationForSection(REl, Value.
SectionID);
1409 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1415 if (Value.
SymbolName || SymType == SymbolRef::ST_Unknown) {
1417 if (AbiVariant == 2)
1418 writeInt32BE(Target + 4, 0xE8410018);
1420 writeInt32BE(Target + 4, 0xE8410028);
1423 }
else if (RelType == ELF::R_PPC64_TOC16 ||
1424 RelType == ELF::R_PPC64_TOC16_DS ||
1425 RelType == ELF::R_PPC64_TOC16_LO ||
1426 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1427 RelType == ELF::R_PPC64_TOC16_HI ||
1428 RelType == ELF::R_PPC64_TOC16_HA) {
1440 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16;
break;
1441 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS;
break;
1442 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO;
break;
1443 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS;
break;
1444 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI;
break;
1445 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA;
break;
1450 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1451 return std::move(Err);
1455 resolveRelocation(Sections[SectionID], Offset, Value.
Addend, RelType, 0);
1461 if (RelType == ELF::R_PPC64_TOC) {
1462 RelType = ELF::R_PPC64_ADDR64;
1463 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1464 return std::move(Err);
1465 }
else if (TargetName ==
".TOC.") {
1466 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1467 return std::move(Err);
1474 addRelocationForSymbol(RE, Value.
SymbolName);
1476 addRelocationForSection(RE, Value.
SectionID);
1479 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1489 DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
1493 StubMap::const_iterator i = Stubs.find(Value);
1494 uintptr_t StubAddress;
1495 if (i != Stubs.end()) {
1497 DEBUG(
dbgs() <<
" Stub function found\n");
1500 DEBUG(
dbgs() <<
" Create a new stub function\n");
1502 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1503 uintptr_t StubAlignment = getStubAlignment();
1505 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1507 unsigned StubOffset = StubAddress - BaseAddress;
1509 Stubs[Value] = StubOffset;
1510 createStubFunction((uint8_t *)StubAddress);
1514 addRelocationForSymbol(RE, Value.
SymbolName);
1516 addRelocationForSection(RE, Value.
SectionID);
1520 if (RelType == ELF::R_390_GOTENT)
1521 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1524 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1526 if (RelType == ELF::R_X86_64_PLT32) {
1547 StubMap::const_iterator i = Stubs.find(Value);
1548 uintptr_t StubAddress;
1549 if (i != Stubs.end()) {
1550 StubAddress = uintptr_t(Section.
getAddress()) + i->second;
1551 DEBUG(
dbgs() <<
" Stub function found\n");
1554 DEBUG(
dbgs() <<
" Create a new stub function\n");
1556 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1557 uintptr_t StubAlignment = getStubAlignment();
1559 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1561 unsigned StubOffset = StubAddress - BaseAddress;
1562 Stubs[Value] = StubOffset;
1563 createStubFunction((uint8_t *)StubAddress);
1569 uint64_t GOTOffset = allocateGOTEntries(1);
1572 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1573 ELF::R_X86_64_PC32);
1576 addRelocationForSymbol(
1577 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1582 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1587 addRelocationForSection(RE, Value.
SectionID);
1589 }
else if (RelType == ELF::R_X86_64_GOTPCREL ||
1590 RelType == ELF::R_X86_64_GOTPCRELX ||
1591 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1592 uint64_t GOTOffset = allocateGOTEntries(1);
1593 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1594 ELF::R_X86_64_PC32);
1598 computeGOTOffsetRE(GOTOffset, Value.
Offset, ELF::R_X86_64_64);
1600 addRelocationForSymbol(RE, Value.
SymbolName);
1602 addRelocationForSection(RE, Value.
SectionID);
1603 }
else if (RelType == ELF::R_X86_64_PC32) {
1605 processSimpleRelocation(SectionID, Offset, RelType, Value);
1606 }
else if (RelType == ELF::R_X86_64_PC64) {
1608 processSimpleRelocation(SectionID, Offset, RelType, Value);
1610 processSimpleRelocation(SectionID, Offset, RelType, Value);
1616 processSimpleRelocation(SectionID, Offset, RelType, Value);
1621 size_t RuntimeDyldELF::getGOTEntrySize() {
1632 Result =
sizeof(uint64_t);
1646 Result =
sizeof(uint64_t);
1656 uint64_t RuntimeDyldELF::allocateGOTEntries(
unsigned no) {
1657 if (GOTSectionID == 0) {
1664 CurrentGOTIndex += no;
1669 unsigned GOTRelType) {
1670 auto E = GOTOffsetMap.insert({Value, 0});
1672 uint64_t GOTOffset = allocateGOTEntries(1);
1676 computeGOTOffsetRE(GOTOffset, Value.
Offset, GOTRelType);
1682 E.first->second = GOTOffset;
1685 return E.first->second;
1688 void RuntimeDyldELF::resolveGOTOffsetRelocation(
unsigned SectionID,
1697 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
1698 uint64_t SymbolOffset,
1706 if (!PendingRelocs.empty())
1707 return make_error<RuntimeDyldError>(
"Can't find matching LO16 reloc");
1710 if (GOTSectionID != 0) {
1714 GOTSectionID,
".got",
false);
1716 return make_error<RuntimeDyldError>(
"Unable to allocate memory for GOT!");
1726 memset(Addr, 0, TotalSize);
1732 if (SI->relocation_begin() != SI->relocation_end()) {
1734 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
1735 assert (i != SectionMap.end());
1739 GOTSymbolOffsets.
clear();
1744 ObjSectionToIDMap::iterator i, e;
1745 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1749 if (Name ==
".eh_frame") {
1750 UnregisteredEHFrameSections.
push_back(i->second);
1756 CurrentGOTIndex = 0;
1765 bool RuntimeDyldELF::relocationNeedsGot(
const RelocationRef &R)
const {
1768 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
1769 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
1772 return RelTy == ELF::R_X86_64_GOTPCREL ||
1773 RelTy == ELF::R_X86_64_GOTPCRELX ||
1774 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
1778 bool RuntimeDyldELF::relocationNeedsStub(
const RelocationRef &R)
const {
1787 case ELF::R_X86_64_GOTPCREL:
1788 case ELF::R_X86_64_GOTPCRELX:
1789 case ELF::R_X86_64_REX_GOTPCRELX:
1790 case ELF::R_X86_64_PC32:
1791 case ELF::R_X86_64_PC64:
1792 case ELF::R_X86_64_64:
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
void push_back(const T &Elt)
Represents either an error or a value T.
static void or32AArch64Imm(void *L, uint64_t Imm)
Expected< StringRef > getName() const
DataRefImpl getRawDataRefImpl() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void writeInt16BE(uint8_t *Addr, uint16_t Value)
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
RuntimeDyld::MemoryManager & MemMgr
size_t getGOTEntrySize() override
uint8_t * getAddress() const
iterator find(StringRef Key)
constexpr bool isInt< 8 >(int64_t x)
This class is the base class for all object file types.
RuntimeDyldCheckerImpl * Checker
static uint16_t applyPPChigher(uint64_t value)
void write32le(void *P, uint32_t V)
static uint16_t applyPPChighesta(uint64_t value)
Error takeError()
Take ownership of the stored error.
DataRefImpl getRawDataRefImpl() const
static uint16_t applyPPChighera(uint64_t value)
ELFType< support::big, false > ELF32BE
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
unsigned SectionID
SectionID - the section this relocation points to.
This is a value type class that represents a single relocation in the list of relocations in the obje...
StringRef getData() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
std::error_code getName(StringRef &Result) const
Expected< SymbolRef::Type > getType() const
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
static uint16_t applyPPChi(uint64_t value)
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
ELFType< support::little, true > ELF64LE
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Function Alias Analysis false
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
The instances of the Type class are immutable: once they are created, they are never changed...
void registerEHFrames() override
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static uint64_t getBits(uint64_t Val, int Start, int End)
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
ELFType< support::little, false > ELF32LE
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
static StringRef getArchTypePrefix(ArchType Kind)
getArchTypePrefix - Get the "prefix" canonical name for the Kind architecture.
~RuntimeDyldELF() override
static const unsigned End
static uint16_t applyPPClo(uint64_t value)
uintptr_t getStubOffset() const
virtual basic_symbol_iterator symbol_end() const =0
virtual std::error_code getPlatformFlags(unsigned &Result) const
Returns platform-specific object flags, if any.
void writeInt32BE(uint8_t *Addr, uint32_t Value)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
section_iterator_range sections() const
bool isCompatibleFile(const object::ObjectFile &Obj) const override
DenseMap< SID, SID > SectionToGOTMap
static void write(bool isBE, void *P, T V)
virtual uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool IsReadOnly)=0
Allocate a memory block of (at least) the given size suitable for data.
StringRef getFileName() const
static uint16_t applyPPChighest(uint64_t value)
static ErrorSuccess success()
Create a success value.
constexpr bool isInt< 32 >(int64_t x)
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
virtual section_iterator section_begin() const =0
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
ErrorOr< int64_t > getAddend() const
LLVM_NODISCARD bool isa(const Y &Val)
static void or32le(void *P, int32_t V)
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Target - Wrapper for Target specific information.
This is a value type class that represents a single symbol in the list of symbols in the object file...
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
llvm_unreachable("Unknown RegBank!")
uint64_t Offset
Offset - offset into the section.
virtual section_iterator section_end() const =0
std::map< SectionRef, unsigned > ObjSectionToIDMap
void writeInt64BE(uint8_t *Addr, uint64_t Value)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
uint32_t read32le(const void *P)
bool isLittleEndian() 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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
A raw_ostream that writes to an std::string.
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static void write32AArch64Addr(void *L, uint64_t Imm)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
void advanceStubOffset(unsigned StubSize)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size)=0
Register the EH frames with the runtime so that c++ exceptions work.
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
ELFType< support::big, true > ELF64BE
StringRef getName() const
static uint16_t applyPPCha(uint64_t value)
virtual StringRef getFileFormatName() const =0
typename ELFT::Shdr Elf_Shdr
This is a value type class that represents a single section in the list of sections in the object fil...