29 #define DEBUG_TYPE "dyld"
37 template <
class T>
static void write(
bool isBE,
void *
P, T V) {
38 isBE ? write<T, support::big>(
P, V) : write<T, support::little>(
P, V);
52 return (Val >> Start) &
Mask;
57 template <
class ELFT>
class DyldELFObject :
public ELFObjectFile<ELFT> {
73 static bool classof(
const Binary *v) {
90 this->isDyldELFObject =
true;
97 if (
auto E = Obj.takeError())
99 std::unique_ptr<DyldELFObject<ELFT>>
Ret(
100 new DyldELFObject<ELFT>(
std::move(*Obj)));
104 template <
class ELFT>
105 void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
109 const_cast<Elf_Shdr *
>(
reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
113 shdr->sh_addr =
static_cast<addr_type
>(
Addr);
116 template <
class ELFT>
117 void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
120 Elf_Sym *sym =
const_cast<Elf_Sym *
>(
125 sym->st_value =
static_cast<addr_type
>(
Addr);
128 class LoadedELFObjectInfo final
130 RuntimeDyld::LoadedObjectInfo> {
132 LoadedELFObjectInfo(
RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
136 getObjectForDebug(
const ObjectFile &Obj)
const override;
139 template <
typename ELFT>
142 const LoadedELFObjectInfo &L) {
147 DyldELFObject<ELFT>::create(Buffer);
151 std::unique_ptr<DyldELFObject<ELFT>> Obj =
std::move(*ObjOrErr);
155 for (
const auto &Sec : Obj->sections()) {
162 if (*NameOrErr !=
"") {
164 Elf_Shdr *shdr =
const_cast<Elf_Shdr *
>(
165 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
167 if (
uint64_t SecLoadAddr = L.getSectionLoadAddress(*
SI)) {
170 shdr->sh_addr =
static_cast<addr_type
>(SecLoadAddr);
180 createELFDebugObject(
const ObjectFile &Obj,
const LoadedELFObjectInfo &L) {
183 std::unique_ptr<MemoryBuffer> Buffer =
190 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
193 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
196 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
199 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
208 LoadedELFObjectInfo::getObjectForDebug(
const ObjectFile &Obj)
const {
209 return createELFDebugObject(Obj, *
this);
222 for (
int i = 0,
e = UnregisteredEHFrameSections.size();
i !=
e; ++
i) {
223 SID EHFrameSID = UnregisteredEHFrameSections[
i];
224 uint8_t *EHFrameAddr =
Sections[EHFrameSID].getAddress();
226 size_t EHFrameSize =
Sections[EHFrameSID].getSize();
229 UnregisteredEHFrameSections.
clear();
232 std::unique_ptr<RuntimeDyldELF>
247 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
250 return std::make_unique<LoadedELFObjectInfo>(*
this, *ObjSectionToIDOrErr);
259 void RuntimeDyldELF::resolveX86_64Relocation(
const SectionEntry &Section,
267 case ELF::R_X86_64_NONE:
269 case ELF::R_X86_64_8: {
272 uint8_t TruncatedAddr = (
Value & 0xFF);
273 *Section.getAddressWithOffset(Offset) = TruncatedAddr;
275 <<
format(
"%p\n", Section.getAddressWithOffset(Offset)));
278 case ELF::R_X86_64_16: {
285 <<
format(
"%p\n", Section.getAddressWithOffset(Offset)));
288 case ELF::R_X86_64_64: {
295 case ELF::R_X86_64_32:
296 case ELF::R_X86_64_32S: {
299 (
Type == ELF::R_X86_64_32S &&
300 ((int64_t)
Value <= INT32_MAX && (int64_t)
Value >= INT32_MIN)));
308 case ELF::R_X86_64_PC8: {
310 int64_t RealOffset =
Value + Addend - FinalAddress;
312 int8_t TruncOffset = (RealOffset & 0xFF);
316 case ELF::R_X86_64_PC32: {
318 int64_t RealOffset =
Value + Addend - FinalAddress;
320 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
325 case ELF::R_X86_64_PC64: {
327 int64_t RealOffset =
Value + Addend - FinalAddress;
331 <<
format(
"%p\n", FinalAddress));
334 case ELF::R_X86_64_GOTOFF64: {
337 for (
const auto &Section :
Sections) {
338 if (
Section.getName() ==
".got") {
339 GOTBase =
Section.getLoadAddressWithOffset(0);
343 assert(GOTBase != 0 &&
"missing GOT");
344 int64_t GOTOffset =
Value - GOTBase + Addend;
348 case ELF::R_X86_64_DTPMOD64: {
353 case ELF::R_X86_64_DTPOFF64:
354 case ELF::R_X86_64_TPOFF64: {
363 case ELF::R_X86_64_DTPOFF32:
364 case ELF::R_X86_64_TPOFF32: {
367 int64_t RealValue =
Value + Addend;
368 assert(RealValue >= INT32_MIN && RealValue <= INT32_MAX);
369 int32_t TruncValue = RealValue;
377 void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
381 case ELF::R_386_32: {
388 case ELF::R_386_PLT32:
389 case ELF::R_386_PC32: {
391 Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
405 void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
416 <<
" FinalAddress: 0x" <<
format(
"%llx", FinalAddress)
417 <<
" Value: 0x" <<
format(
"%llx",
Value) <<
" Type: 0x"
419 <<
format(
"%llx", Addend) <<
"\n");
425 case ELF::R_AARCH64_NONE:
427 case ELF::R_AARCH64_ABS16: {
429 assert(
static_cast<int64_t
>(Result) >= INT16_MIN && Result < UINT16_MAX);
430 write(isBE, TargetPtr,
static_cast<uint16_t>(Result & 0xffffU));
433 case ELF::R_AARCH64_ABS32: {
435 assert(
static_cast<int64_t
>(Result) >= INT32_MIN && Result < UINT32_MAX);
436 write(isBE, TargetPtr,
static_cast<uint32_t>(Result & 0xffffffffU));
439 case ELF::R_AARCH64_ABS64:
442 case ELF::R_AARCH64_PLT32: {
444 assert(
static_cast<int64_t
>(Result) >= INT32_MIN &&
445 static_cast<int64_t
>(Result) <= INT32_MAX);
449 case ELF::R_AARCH64_PREL16: {
451 assert(
static_cast<int64_t
>(Result) >= INT16_MIN &&
452 static_cast<int64_t
>(Result) <= UINT16_MAX);
453 write(isBE, TargetPtr,
static_cast<uint16_t>(Result & 0xffffU));
456 case ELF::R_AARCH64_PREL32: {
458 assert(
static_cast<int64_t
>(Result) >= INT32_MIN &&
459 static_cast<int64_t
>(Result) <= UINT32_MAX);
460 write(isBE, TargetPtr,
static_cast<uint32_t>(Result & 0xffffffffU));
463 case ELF::R_AARCH64_PREL64:
464 write(isBE, TargetPtr,
Value + Addend - FinalAddress);
466 case ELF::R_AARCH64_CONDBR19: {
469 assert(isInt<21>(BranchImm));
470 *TargetPtr &= 0xff00001fU;
472 or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
475 case ELF::R_AARCH64_TSTBR14: {
480 *TargetPtr &= 0xfff8001fU;
482 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) << 3);
485 case ELF::R_AARCH64_CALL26:
486 case ELF::R_AARCH64_JUMP26: {
492 assert(isInt<28>(BranchImm));
493 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
496 case ELF::R_AARCH64_MOVW_UABS_G3:
497 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF000000000000) >> 43);
499 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
500 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF00000000) >> 27);
502 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
503 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF0000) >> 11);
505 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
506 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF) << 5);
508 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
511 ((
Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
514 assert(isInt<33>(Result) &&
"overflow check failed for relocation");
521 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
527 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
533 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
539 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
545 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
551 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
557 case ELF::R_AARCH64_LD_PREL_LO19: {
562 assert(isInt<21>(Result));
564 *TargetPtr &= 0xff00001fU;
567 *TargetPtr |= ((
Result & 0xffc) << (5 - 2));
570 case ELF::R_AARCH64_ADR_PREL_LO21: {
575 assert(isInt<21>(Result));
577 *TargetPtr &= 0x9f00001fU;
580 *TargetPtr |= ((
Result & 0xffc) << (5 - 2));
581 *TargetPtr |= (
Result & 0x3) << 29;
587 void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
593 uint32_t FinalAddress =
Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
597 <<
Section.getAddressWithOffset(Offset)
598 <<
" FinalAddress: " <<
format(
"%p", FinalAddress)
601 <<
" Addend: " <<
format(
"%x", Addend) <<
"\n");
607 case ELF::R_ARM_NONE:
610 case ELF::R_ARM_PREL31:
613 ((
Value - FinalAddress) & ~0x80000000);
615 case ELF::R_ARM_TARGET1:
616 case ELF::R_ARM_ABS32:
621 case ELF::R_ARM_MOVW_ABS_NC:
622 case ELF::R_ARM_MOVT_ABS:
623 if (
Type == ELF::R_ARM_MOVW_ABS_NC)
625 else if (
Type == ELF::R_ARM_MOVT_ABS)
629 (((
Value >> 12) & 0xF) << 16);
632 case ELF::R_ARM_PC24:
633 case ELF::R_ARM_CALL:
634 case ELF::R_ARM_JUMP24:
635 int32_t RelValue =
static_cast<int32_t
>(
Value - FinalAddress - 8);
636 RelValue = (RelValue & 0x03FFFFFC) >> 2;
644 void RuntimeDyldELF::setMipsABI(
const ObjectFile &Obj) {
652 if (
auto *
E = dyn_cast<ELFObjectFileBase>(&Obj)) {
653 unsigned AbiVariant =
E->getPlatformFlags();
662 ObjSectionToIDMap &LocalSections,
674 for (
auto &Section : Obj.
sections()) {
684 if (
auto SectionIDOrErr =
688 return SectionIDOrErr.takeError();
703 ObjSectionToIDMap &LocalSections,
723 if (RelSectionName !=
".opd")
727 e = si->relocation_end();
732 if (TypeFunc != ELF::R_PPC64_ADDR64) {
737 uint64_t TargetSymbolOffset =
i->getOffset();
740 if (
auto AddendOrErr =
i->getAddend())
741 Addend = *AddendOrErr;
743 return AddendOrErr.takeError();
751 if (TypeTOC != ELF::R_PPC64_TOC)
757 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
761 if (
auto TSIOrErr = TargetSymbol->
getSection())
764 return TSIOrErr.takeError();
767 bool IsCode = TSI->isText();
772 return SectionIDOrErr.takeError();
788 return (value >> 16) & 0xffff;
792 return ((value + 0x8000) >> 16) & 0xffff;
796 return (value >> 32) & 0xffff;
800 return ((value + 0x8000) >> 32) & 0xffff;
804 return (value >> 48) & 0xffff;
808 return ((value + 0x8000) >> 48) & 0xffff;
811 void RuntimeDyldELF::resolvePPC32Relocation(
const SectionEntry &Section,
814 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
819 case ELF::R_PPC_ADDR16_LO:
822 case ELF::R_PPC_ADDR16_HI:
825 case ELF::R_PPC_ADDR16_HA:
831 void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
834 uint8_t *LocalAddress =
Section.getAddressWithOffset(Offset);
839 case ELF::R_PPC64_ADDR16:
842 case ELF::R_PPC64_ADDR16_DS:
845 case ELF::R_PPC64_ADDR16_LO:
848 case ELF::R_PPC64_ADDR16_LO_DS:
851 case ELF::R_PPC64_ADDR16_HI:
852 case ELF::R_PPC64_ADDR16_HIGH:
855 case ELF::R_PPC64_ADDR16_HA:
856 case ELF::R_PPC64_ADDR16_HIGHA:
859 case ELF::R_PPC64_ADDR16_HIGHER:
862 case ELF::R_PPC64_ADDR16_HIGHERA:
865 case ELF::R_PPC64_ADDR16_HIGHEST:
868 case ELF::R_PPC64_ADDR16_HIGHESTA:
871 case ELF::R_PPC64_ADDR14: {
874 uint8_t aalk = *(LocalAddress + 3);
877 case ELF::R_PPC64_REL16_LO: {
882 case ELF::R_PPC64_REL16_HI: {
887 case ELF::R_PPC64_REL16_HA: {
892 case ELF::R_PPC64_ADDR32: {
893 int64_t
Result =
static_cast<int64_t
>(
Value + Addend);
894 if (SignExtend64<32>(Result) !=
Result)
898 case ELF::R_PPC64_REL24: {
900 int64_t delta =
static_cast<int64_t
>(
Value - FinalAddress + Addend);
901 if (SignExtend64<26>(delta) != delta)
905 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
907 case ELF::R_PPC64_REL32: {
909 int64_t delta =
static_cast<int64_t
>(
Value - FinalAddress + Addend);
910 if (SignExtend64<32>(delta) != delta)
914 case ELF::R_PPC64_REL64: {
919 case ELF::R_PPC64_ADDR64:
925 void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
928 uint8_t *LocalAddress =
Section.getAddressWithOffset(Offset);
933 case ELF::R_390_PC16DBL:
934 case ELF::R_390_PLT16DBL: {
935 int64_t Delta = (
Value + Addend) -
Section.getLoadAddressWithOffset(Offset);
936 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
940 case ELF::R_390_PC32DBL:
941 case ELF::R_390_PLT32DBL: {
942 int64_t Delta = (
Value + Addend) -
Section.getLoadAddressWithOffset(Offset);
943 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
947 case ELF::R_390_PC16: {
948 int64_t Delta = (
Value + Addend) -
Section.getLoadAddressWithOffset(Offset);
949 assert(int16_t(Delta) == Delta &&
"R_390_PC16 overflow");
953 case ELF::R_390_PC32: {
954 int64_t Delta = (
Value + Addend) -
Section.getLoadAddressWithOffset(Offset);
955 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
959 case ELF::R_390_PC64: {
960 int64_t Delta = (
Value + Addend) -
Section.getLoadAddressWithOffset(Offset);
965 *LocalAddress = (uint8_t)(
Value + Addend);
979 void RuntimeDyldELF::resolveBPFRelocation(
const SectionEntry &Section,
988 case ELF::R_BPF_NONE:
989 case ELF::R_BPF_64_64:
990 case ELF::R_BPF_64_32:
991 case ELF::R_BPF_64_NODYLD32:
993 case ELF::R_BPF_64_ABS64: {
999 case ELF::R_BPF_64_ABS32: {
1037 void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
1040 uint64_t SymOffset, SID SectionID) {
1043 resolveX86_64Relocation(Section, Offset,
Value,
Type, Addend, SymOffset);
1051 resolveAArch64Relocation(Section, Offset,
Value,
Type, Addend);
1062 resolvePPC32Relocation(Section, Offset,
Value,
Type, Addend);
1066 resolvePPC64Relocation(Section, Offset,
Value,
Type, Addend);
1069 resolveSystemZRelocation(Section, Offset,
Value,
Type, Addend);
1073 resolveBPFRelocation(Section, Offset,
Value,
Type, Addend);
1080 void *RuntimeDyldELF::computePlaceholderAddress(
unsigned SectionID,
uint64_t Offset)
const {
1086 if (
Value.SymbolName)
1093 bool IsLocal)
const {
1095 case ELF::R_MICROMIPS_GOT16:
1097 return ELF::R_MICROMIPS_LO16;
1099 case ELF::R_MICROMIPS_HI16:
1100 return ELF::R_MICROMIPS_LO16;
1101 case ELF::R_MIPS_GOT16:
1103 return ELF::R_MIPS_LO16;
1105 case ELF::R_MIPS_HI16:
1106 return ELF::R_MIPS_LO16;
1107 case ELF::R_MIPS_PCHI16:
1108 return ELF::R_MIPS_PCLO16;
1112 return ELF::R_MIPS_NONE;
1124 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1128 if (
Value.SymbolName) {
1135 const auto &
SymInfo = Loc->second;
1143 uint64_t SourceAddress =
Sections[SectionID].getLoadAddressWithOffset(Offset);
1148 if (!isInt<28>(Address +
Value.Addend - SourceAddress))
1151 resolveRelocation(
Sections[SectionID], Offset, Address, RelI->getType(),
1157 void RuntimeDyldELF::resolveAArch64Branch(
unsigned SectionID,
1162 LLVM_DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
1166 unsigned RelType = RelI->getType();
1168 StubMap::const_iterator
i = Stubs.find(
Value);
1169 if (
i != Stubs.end()) {
1170 resolveRelocation(Section, Offset,
1174 }
else if (!resolveAArch64ShortBranch(SectionID, RelI,
Value)) {
1182 ELF::R_AARCH64_MOVW_UABS_G3,
Value.Addend);
1184 StubTargetAddr -
Section.getAddress() + 4,
1185 ELF::R_AARCH64_MOVW_UABS_G2_NC,
Value.Addend);
1187 StubTargetAddr -
Section.getAddress() + 8,
1188 ELF::R_AARCH64_MOVW_UABS_G1_NC,
Value.Addend);
1190 StubTargetAddr -
Section.getAddress() + 12,
1191 ELF::R_AARCH64_MOVW_UABS_G0_NC,
Value.Addend);
1193 if (
Value.SymbolName) {
1204 resolveRelocation(Section, Offset,
1208 Section.advanceStubOffset(getMaxStubSize());
1216 const auto &Obj = cast<ELFObjectFileBase>(
O);
1217 uint64_t RelType = RelI->getType();
1220 Addend = *AddendOrErr;
1228 if (
auto TargetNameOrErr =
Symbol->getName())
1229 TargetName = *TargetNameOrErr;
1231 return TargetNameOrErr.takeError();
1233 LLVM_DEBUG(
dbgs() <<
"\t\tRelType: " << RelType <<
" Addend: " << Addend
1234 <<
" TargetName: " << TargetName <<
"\n");
1244 if (!SymTypeOrErr) {
1250 SymType = *SymTypeOrErr;
1253 const auto &
SymInfo = gsi->second;
1259 case SymbolRef::ST_Debug: {
1263 auto SectionOrErr =
Symbol->getSection();
1264 if (!SectionOrErr) {
1274 bool isCode = si->isText();
1277 Value.SectionID = *SectionIDOrErr;
1279 return SectionIDOrErr.takeError();
1280 Value.Addend = Addend;
1283 case SymbolRef::ST_Data:
1284 case SymbolRef::ST_Function:
1285 case SymbolRef::ST_Unknown: {
1287 Value.Addend = Addend;
1293 if (!
Value.SymbolName)
1294 Value.SymbolName =
"";
1303 uint64_t Offset = RelI->getOffset();
1305 LLVM_DEBUG(
dbgs() <<
"\t\tSectionID: " << SectionID <<
" Offset: " << Offset
1308 if ((RelType == ELF::R_AARCH64_CALL26 ||
1309 RelType == ELF::R_AARCH64_JUMP26) &&
1311 resolveAArch64Branch(SectionID,
Value, RelI, Stubs);
1312 }
else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1315 uint64_t GOTOffset = findOrAllocGOTEntry(
Value, ELF::R_AARCH64_ABS64);
1316 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1317 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1319 }
else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1320 uint64_t GOTOffset = findOrAllocGOTEntry(
Value, ELF::R_AARCH64_ABS64);
1321 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1322 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1324 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1327 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1328 RelType == ELF::R_ARM_JUMP24) {
1334 StubMap::const_iterator
i = Stubs.find(
Value);
1335 if (
i != Stubs.end()) {
1338 reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
i->second)),
1344 Stubs[
Value] = Section.getStubOffset();
1346 Section.getAddressWithOffset(Section.getStubOffset()));
1348 ELF::R_ARM_ABS32,
Value.Addend);
1349 if (
Value.SymbolName)
1354 resolveRelocation(Section, Offset,
reinterpret_cast<uint64_t>(
1355 Section.getAddressWithOffset(
1356 Section.getStubOffset())),
1358 Section.advanceStubOffset(getMaxStubSize());
1362 reinterpret_cast<uint32_t*
>(computePlaceholderAddress(SectionID, Offset));
1363 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1364 RelType == ELF::R_ARM_ABS32) {
1365 Value.Addend += *Placeholder;
1366 }
else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1368 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1370 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1373 uint8_t *Placeholder =
reinterpret_cast<uint8_t *
>(
1374 computePlaceholderAddress(SectionID, Offset));
1376 if (RelType == ELF::R_MIPS_26) {
1384 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1386 Value.Addend += Addend;
1389 StubMap::const_iterator
i = Stubs.find(
Value);
1390 if (
i != Stubs.end()) {
1397 Stubs[
Value] = Section.getStubOffset();
1402 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1405 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1406 ELF::R_MIPS_HI16,
Value.Addend);
1408 StubTargetAddr - Section.getAddress() + 4,
1409 ELF::R_MIPS_LO16,
Value.Addend);
1411 if (
Value.SymbolName) {
1419 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1421 Section.advanceStubOffset(getMaxStubSize());
1423 }
else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1424 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1426 PendingRelocs.push_back(std::make_pair(
Value, RE));
1427 }
else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1428 int64_t Addend =
Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1429 for (
auto I = PendingRelocs.begin();
I != PendingRelocs.end();) {
1432 if (MatchingValue ==
Value &&
1433 RelType == getMatchingLoRelocation(Reloc.
RelType) &&
1436 if (
Value.SymbolName)
1440 I = PendingRelocs.erase(
I);
1445 if (
Value.SymbolName)
1450 if (RelType == ELF::R_MIPS_32)
1451 Value.Addend += Opcode;
1452 else if (RelType == ELF::R_MIPS_PC16)
1453 Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1454 else if (RelType == ELF::R_MIPS_PC19_S2)
1455 Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1456 else if (RelType == ELF::R_MIPS_PC21_S2)
1457 Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1458 else if (RelType == ELF::R_MIPS_PC26_S2)
1459 Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1460 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1465 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1466 || r_type == ELF::R_MIPS_GOT_DISP) {
1468 if (
i != GOTSymbolOffsets.
end())
1472 GOTSymbolOffsets[TargetName] = RE.
SymOffset;
1474 if (
Value.SymbolName)
1478 }
else if (RelType == ELF::R_MIPS_26) {
1484 StubMap::const_iterator
i = Stubs.find(
Value);
1485 if (
i != Stubs.end()) {
1492 Stubs[
Value] = Section.getStubOffset();
1497 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1501 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1502 ELF::R_MIPS_HI16,
Value.Addend);
1504 StubTargetAddr - Section.getAddress() + 4,
1505 ELF::R_MIPS_LO16,
Value.Addend);
1506 if (
Value.SymbolName) {
1517 StubTargetAddr - Section.getAddress(),
1518 ELF::R_MIPS_HIGHEST,
Value.Addend);
1520 StubTargetAddr - Section.getAddress() + 4,
1521 ELF::R_MIPS_HIGHER,
Value.Addend);
1523 StubTargetAddr - Section.getAddress() + 12,
1524 ELF::R_MIPS_HI16,
Value.Addend);
1526 StubTargetAddr - Section.getAddress() + 20,
1527 ELF::R_MIPS_LO16,
Value.Addend);
1528 if (
Value.SymbolName) {
1540 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1542 Section.advanceStubOffset(getMaxStubSize());
1545 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1549 if (RelType == ELF::R_PPC64_REL24) {
1558 uint8_t *
Target = Section.getAddressWithOffset(Offset);
1559 bool RangeOverflow =
false;
1560 bool IsExtern =
Value.SymbolName || SymType == SymbolRef::ST_Unknown;
1562 if (AbiVariant != 2) {
1566 if (
auto Err = findOPDEntrySection(Obj, ObjSectionToID,
Value))
1571 if (
Value.SectionID == SectionID){
1572 uint8_t SymOther =
Symbol->getOther();
1576 uint8_t *RelocTarget =
1578 int64_t delta =
static_cast<int64_t
>(
Target - RelocTarget);
1580 if (SignExtend64<26>(delta) != delta) {
1581 RangeOverflow =
true;
1582 }
else if ((AbiVariant != 2) ||
1583 (AbiVariant == 2 &&
Value.SectionID == SectionID)) {
1588 if (IsExtern || (AbiVariant == 2 &&
Value.SectionID != SectionID) ||
1592 StubMap::const_iterator
i = Stubs.find(
Value);
1593 if (
i != Stubs.end()) {
1595 resolveRelocation(Section, Offset,
1597 Section.getAddressWithOffset(
i->second)),
1603 Stubs[
Value] = Section.getStubOffset();
1605 Section.getAddressWithOffset(Section.getStubOffset()),
1608 ELF::R_PPC64_ADDR64,
Value.Addend);
1614 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
1616 StubRelocOffset += 2;
1619 ELF::R_PPC64_ADDR16_HIGHEST,
Value.Addend);
1621 ELF::R_PPC64_ADDR16_HIGHER,
Value.Addend);
1623 ELF::R_PPC64_ADDR16_HI,
Value.Addend);
1625 ELF::R_PPC64_ADDR16_LO,
Value.Addend);
1627 if (
Value.SymbolName) {
1639 resolveRelocation(Section, Offset,
reinterpret_cast<uint64_t>(
1640 Section.getAddressWithOffset(
1641 Section.getStubOffset())),
1643 Section.advanceStubOffset(getMaxStubSize());
1645 if (IsExtern || (AbiVariant == 2 &&
Value.SectionID != SectionID)) {
1647 if (AbiVariant == 2)
1653 }
else if (RelType == ELF::R_PPC64_TOC16 ||
1654 RelType == ELF::R_PPC64_TOC16_DS ||
1655 RelType == ELF::R_PPC64_TOC16_LO ||
1656 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1657 RelType == ELF::R_PPC64_TOC16_HI ||
1658 RelType == ELF::R_PPC64_TOC16_HA) {
1670 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16;
break;
1671 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS;
break;
1672 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO;
break;
1673 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS;
break;
1674 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI;
break;
1675 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA;
break;
1680 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1685 resolveRelocation(
Sections[SectionID], Offset,
Value.Addend, RelType, 0);
1691 if (RelType == ELF::R_PPC64_TOC) {
1692 RelType = ELF::R_PPC64_ADDR64;
1693 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID,
Value))
1695 }
else if (TargetName ==
".TOC.") {
1696 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID,
Value))
1698 Value.Addend += Addend;
1703 if (
Value.SymbolName)
1709 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1719 LLVM_DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
1723 StubMap::const_iterator
i = Stubs.find(
Value);
1724 uintptr_t StubAddress;
1725 if (
i != Stubs.end()) {
1726 StubAddress = uintptr_t(Section.getAddressWithOffset(
i->second));
1732 uintptr_t BaseAddress = uintptr_t(Section.getAddress());
1733 uintptr_t StubAlignment = getStubAlignment();
1735 (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
1737 unsigned StubOffset = StubAddress - BaseAddress;
1739 Stubs[
Value] = StubOffset;
1743 if (
Value.SymbolName)
1747 Section.advanceStubOffset(getMaxStubSize());
1750 if (RelType == ELF::R_390_GOTENT)
1751 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1754 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1756 if (RelType == ELF::R_X86_64_PLT32) {
1777 StubMap::const_iterator
i = Stubs.find(
Value);
1778 uintptr_t StubAddress;
1779 if (
i != Stubs.end()) {
1780 StubAddress = uintptr_t(Section->getAddress()) +
i->second;
1786 uintptr_t BaseAddress = uintptr_t(Section->getAddress());
1787 uintptr_t StubAlignment = getStubAlignment();
1789 (BaseAddress + Section->getStubOffset() + StubAlignment - 1) &
1791 unsigned StubOffset = StubAddress - BaseAddress;
1792 Stubs[
Value] = StubOffset;
1796 Section->advanceStubOffset(getMaxStubSize());
1799 uint64_t GOTOffset = allocateGOTEntries(1);
1805 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1806 ELF::R_X86_64_PC32);
1810 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1815 resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1819 computePlaceholderAddress(SectionID, Offset));
1820 processSimpleRelocation(SectionID, Offset, ELF::R_X86_64_PC32,
Value);
1822 }
else if (RelType == ELF::R_X86_64_GOTPCREL ||
1823 RelType == ELF::R_X86_64_GOTPCRELX ||
1824 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1825 uint64_t GOTOffset = allocateGOTEntries(1);
1826 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1827 ELF::R_X86_64_PC32);
1831 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_64);
1832 if (
Value.SymbolName)
1836 }
else if (RelType == ELF::R_X86_64_GOT64) {
1838 uint64_t GOTOffset = allocateGOTEntries(1);
1839 resolveRelocation(
Sections[SectionID], Offset, GOTOffset,
1840 ELF::R_X86_64_64, 0);
1844 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_64);
1845 if (
Value.SymbolName)
1849 }
else if (RelType == ELF::R_X86_64_GOTPC32) {
1853 (void)allocateGOTEntries(0);
1854 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC32);
1855 }
else if (RelType == ELF::R_X86_64_GOTPC64) {
1856 (void)allocateGOTEntries(0);
1857 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1858 }
else if (RelType == ELF::R_X86_64_GOTOFF64) {
1860 (void)allocateGOTEntries(0);
1861 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1862 }
else if (RelType == ELF::R_X86_64_PC32) {
1864 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1865 }
else if (RelType == ELF::R_X86_64_PC64) {
1867 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1868 }
else if (RelType == ELF::R_X86_64_GOTTPOFF) {
1869 processX86_64GOTTPOFFRelocation(SectionID, Offset,
Value, Addend);
1870 }
else if (RelType == ELF::R_X86_64_TLSGD ||
1871 RelType == ELF::R_X86_64_TLSLD) {
1874 auto &GetAddrRelocation = *RelI;
1875 processX86_64TLSRelocation(SectionID, Offset, RelType,
Value, Addend,
1878 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1884 processSimpleRelocation(SectionID, Offset, RelType,
Value);
1889 void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(
unsigned SectionID,
1900 struct CodeSequence {
1912 std::array<CodeSequence, 2> CodeSequences;
1916 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1917 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
1919 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00
1922 CodeSequences[0].ExpectedCodeSequence =
1924 CodeSequences[0].TLSSequenceOffset = 12;
1926 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1927 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
1928 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00
1931 CodeSequences[0].TpoffRelocationOffset = 12;
1936 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1937 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
1938 0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00
1940 CodeSequences[1].ExpectedCodeSequence =
1942 CodeSequences[1].TLSSequenceOffset = 3;
1944 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1945 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1946 0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
1949 CodeSequences[1].TpoffRelocationOffset = 10;
1954 for (
const auto &
C : CodeSequences) {
1955 assert(
C.ExpectedCodeSequence.size() ==
C.NewCodeSequence.size() &&
1956 "Old and new code sequences must have the same size");
1958 if (Offset <
C.TLSSequenceOffset ||
1959 (Offset -
C.TLSSequenceOffset +
C.NewCodeSequence.size()) >
1966 auto TLSSequenceStartOffset =
Offset -
C.TLSSequenceOffset;
1967 auto *TLSSequence =
Section.getAddressWithOffset(TLSSequenceStartOffset);
1969 C.ExpectedCodeSequence) {
1973 memcpy(TLSSequence,
C.NewCodeSequence.data(),
C.NewCodeSequence.size());
1980 TLSSequenceStartOffset +
C.TpoffRelocationOffset,
1981 ELF::R_X86_64_TPOFF32,
Value.Addend - Addend);
1983 if (
Value.SymbolName)
1996 uint64_t GOTOffset = allocateGOTEntries(1);
1997 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1998 ELF::R_X86_64_PC32);
2000 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_TPOFF64);
2001 if (
Value.SymbolName)
2008 void RuntimeDyldELF::processX86_64TLSRelocation(
2021 bool IsSmallCodeModel;
2023 bool IsGOTPCRel =
false;
2025 switch (GetAddrRelocation.
getType()) {
2026 case ELF::R_X86_64_GOTPCREL:
2027 case ELF::R_X86_64_REX_GOTPCRELX:
2028 case ELF::R_X86_64_GOTPCRELX:
2031 case ELF::R_X86_64_PLT32:
2032 IsSmallCodeModel =
true;
2034 case ELF::R_X86_64_PLTOFF64:
2035 IsSmallCodeModel =
false;
2039 "invalid TLS relocations for General/Local Dynamic TLS Model: "
2040 "expected PLT or GOT relocation for __tls_get_addr function");
2051 if (RelType == ELF::R_X86_64_TLSGD) {
2056 if (IsSmallCodeModel) {
2058 static const std::initializer_list<uint8_t> CodeSequence = {
2060 0x48, 0x8d, 0x3d, 0x00, 0x00,
2064 0xe8, 0x00, 0x00, 0x00, 0x00
2067 TLSSequenceOffset = 4;
2071 static const std::initializer_list<uint8_t> CodeSequence = {
2073 0x48, 0x8d, 0x3d, 0x00, 0x00,
2077 0xff, 0x15, 0x00, 0x00, 0x00,
2081 TLSSequenceOffset = 4;
2086 static const std::initializer_list<uint8_t> SmallSequence = {
2087 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2089 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00
2093 TpoffRelocOffset = 12;
2095 static const std::initializer_list<uint8_t> CodeSequence = {
2096 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
2098 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2104 TLSSequenceOffset = 3;
2107 static const std::initializer_list<uint8_t> LargeSequence = {
2108 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2110 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00,
2112 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00
2115 TpoffRelocOffset = 12;
2121 RelocationEntry RE(SectionID, Offset - TLSSequenceOffset + TpoffRelocOffset,
2122 ELF::R_X86_64_TPOFF32,
Value.Addend - Addend);
2123 if (
Value.SymbolName)
2127 }
else if (RelType == ELF::R_X86_64_TLSLD) {
2128 if (IsSmallCodeModel) {
2130 static const std::initializer_list<uint8_t> CodeSequence = {
2131 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00,
2132 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00
2135 TLSSequenceOffset = 3;
2138 static const std::initializer_list<uint8_t> SmallSequence = {
2140 0x64, 0x48, 0x8b, 0x04, 0x25,
2141 0x00, 0x00, 0x00, 0x00
2147 static const std::initializer_list<uint8_t> CodeSequence = {
2148 0x48, 0x8d, 0x3d, 0x00,
2150 0xff, 0x15, 0x00, 0x00,
2155 TLSSequenceOffset = 3;
2159 static const std::initializer_list<uint8_t> SmallSequence = {
2160 0x0f, 0x1f, 0x40, 0x00,
2161 0x64, 0x48, 0x8b, 0x04, 0x25,
2162 0x00, 0x00, 0x00, 0x00
2169 static const std::initializer_list<uint8_t> CodeSequence = {
2170 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
2172 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2178 TLSSequenceOffset = 3;
2181 static const std::initializer_list<uint8_t> LargeSequence = {
2183 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00,
2185 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00
2194 "Old and new code sequences must have the same size");
2197 if (Offset < TLSSequenceOffset ||
2198 (Offset - TLSSequenceOffset + NewCodeSequence.
size()) >
2203 auto *TLSSequence =
Section.getAddressWithOffset(Offset - TLSSequenceOffset);
2205 ExpectedCodeSequence) {
2207 "invalid TLS sequence for Global/Local Dynamic TLS Model");
2210 memcpy(TLSSequence, NewCodeSequence.
data(), NewCodeSequence.
size());
2248 uint64_t RuntimeDyldELF::allocateGOTEntries(
unsigned no) {
2249 if (GOTSectionID == 0) {
2256 CurrentGOTIndex += no;
2261 unsigned GOTRelType) {
2262 auto E = GOTOffsetMap.insert({
Value, 0});
2264 uint64_t GOTOffset = allocateGOTEntries(1);
2268 computeGOTOffsetRE(GOTOffset,
Value.Offset, GOTRelType);
2269 if (
Value.SymbolName)
2274 E.first->second = GOTOffset;
2277 return E.first->second;
2280 void RuntimeDyldELF::resolveGOTOffsetRelocation(
unsigned SectionID,
2298 if (!PendingRelocs.empty())
2299 return make_error<RuntimeDyldError>(
"Can't find matching LO16 reloc");
2302 if (GOTSectionID != 0) {
2306 GOTSectionID,
".got",
false);
2308 return make_error<RuntimeDyldError>(
"Unable to allocate memory for GOT!");
2315 memset(
Addr, 0, TotalSize);
2321 if (
SI->relocation_begin() !=
SI->relocation_end()) {
2324 return make_error<RuntimeDyldError>(
2328 ObjSectionToIDMap::iterator
i = SectionMap.find(*RelocatedSection);
2329 assert (
i != SectionMap.end());
2333 GOTSymbolOffsets.
clear();
2338 ObjSectionToIDMap::iterator
i,
e;
2339 for (
i = SectionMap.begin(),
e = SectionMap.end();
i !=
e; ++
i) {
2349 if (
Name ==
".eh_frame") {
2350 UnregisteredEHFrameSections.push_back(
i->second);
2356 CurrentGOTIndex = 0;
2365 bool RuntimeDyldELF::relocationNeedsGot(
const RelocationRef &R)
const {
2366 unsigned RelTy = R.getType();
2368 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
2369 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
2372 return RelTy == ELF::R_X86_64_GOTPCREL ||
2373 RelTy == ELF::R_X86_64_GOTPCRELX ||
2374 RelTy == ELF::R_X86_64_GOT64 ||
2375 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
2379 bool RuntimeDyldELF::relocationNeedsStub(
const RelocationRef &R)
const {
2383 switch (R.getType()) {
2388 case ELF::R_X86_64_GOTPCREL:
2389 case ELF::R_X86_64_GOTPCRELX:
2390 case ELF::R_X86_64_REX_GOTPCRELX:
2391 case ELF::R_X86_64_GOTPC64:
2392 case ELF::R_X86_64_GOT64:
2393 case ELF::R_X86_64_GOTOFF64:
2394 case ELF::R_X86_64_PC32:
2395 case ELF::R_X86_64_PC64:
2396 case ELF::R_X86_64_64: