29#define DEBUG_TYPE "dyld"
37template <
class T>
static void write(
bool isBE,
void *
P,
T V) {
45 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
53 return (Val >> Start) & Mask;
58template <
class ELFT>
class DyldELFObject :
public ELFObjectFile<ELFT> {
61 typedef typename ELFT::uint addr_type;
63 DyldELFObject(ELFObjectFile<ELFT> &&Obj);
66 static Expected<std::unique_ptr<DyldELFObject>>
67 create(MemoryBufferRef
Wrapper);
69 void updateSectionAddress(
const SectionRef &Sec,
uint64_t Addr);
71 void updateSymbolAddress(
const SymbolRef &SymRef,
uint64_t Addr);
74 static bool classof(
const Binary *v) {
75 return (
isa<ELFObjectFile<ELFT>>(v) &&
76 classof(
cast<ELFObjectFile<ELFT>>(v)));
78 static bool classof(
const ELFObjectFile<ELFT> *v) {
79 return v->isDyldType();
91 this->isDyldELFObject =
true;
98 if (
auto E =
Obj.takeError())
100 std::unique_ptr<DyldELFObject<ELFT>> Ret(
101 new DyldELFObject<ELFT>(std::move(*Obj)));
102 return std::move(Ret);
106void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
110 const_cast<Elf_Shdr *
>(
reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
114 shdr->sh_addr =
static_cast<addr_type
>(Addr);
118void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
121 Elf_Sym *sym =
const_cast<Elf_Sym *
>(
126 sym->st_value =
static_cast<addr_type
>(Addr);
129class LoadedELFObjectInfo final
131 RuntimeDyld::LoadedObjectInfo> {
133 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
134 : LoadedObjectInfoHelper(RTDyld, std::
move(ObjSecToIDMap)) {}
136 OwningBinary<ObjectFile>
137 getObjectForDebug(
const ObjectFile &Obj)
const override;
140template <
typename ELFT>
143 const LoadedELFObjectInfo &L) {
144 typedef typename ELFT::Shdr Elf_Shdr;
145 typedef typename ELFT::uint addr_type;
148 DyldELFObject<ELFT>::create(Buffer);
152 std::unique_ptr<DyldELFObject<ELFT>>
Obj = std::move(*ObjOrErr);
156 for (
const auto &Sec :
Obj->sections()) {
163 if (*NameOrErr !=
"") {
165 Elf_Shdr *shdr =
const_cast<Elf_Shdr *
>(
166 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
168 if (
uint64_t SecLoadAddr =
L.getSectionLoadAddress(*
SI)) {
171 shdr->sh_addr =
static_cast<addr_type
>(SecLoadAddr);
177 return std::move(Obj);
181createELFDebugObject(
const ObjectFile &Obj,
const LoadedELFObjectInfo &L) {
182 assert(
Obj.isELF() &&
"Not an ELF object file.");
184 std::unique_ptr<MemoryBuffer> Buffer =
189 if (
Obj.getBytesInAddress() == 4 &&
Obj.isLittleEndian())
191 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
192 else if (
Obj.getBytesInAddress() == 4 && !
Obj.isLittleEndian())
194 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
195 else if (
Obj.getBytesInAddress() == 8 && !
Obj.isLittleEndian())
197 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
198 else if (
Obj.getBytesInAddress() == 8 &&
Obj.isLittleEndian())
200 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
209LoadedELFObjectInfo::getObjectForDebug(
const ObjectFile &Obj)
const {
210 return createELFDebugObject(Obj, *
this);
223 for (
SID EHFrameSID : UnregisteredEHFrameSections) {
225 uint64_t EHFrameLoadAddr =
Sections[EHFrameSID].getLoadAddress();
226 size_t EHFrameSize =
Sections[EHFrameSID].getSize();
227 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
229 UnregisteredEHFrameSections.clear();
232std::unique_ptr<RuntimeDyldELF>
247std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
250 return std::make_unique<LoadedELFObjectInfo>(*
this, *ObjSectionToIDOrErr);
259void RuntimeDyldELF::resolveX86_64Relocation(
const SectionEntry &Section,
262 uint64_t SymOffset) {
267 case ELF::R_X86_64_NONE:
269 case ELF::R_X86_64_8: {
273 *Section.getAddressWithOffset(
Offset) = TruncatedAddr;
275 <<
format(
"%p\n", Section.getAddressWithOffset(
Offset)));
278 case ELF::R_X86_64_16: {
282 support::ulittle16_t::ref(Section.getAddressWithOffset(
Offset)) =
285 <<
format(
"%p\n", Section.getAddressWithOffset(
Offset)));
288 case ELF::R_X86_64_64: {
289 support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset)) =
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)));
301 uint32_t TruncatedAddr = (
Value & 0xFFFFFFFF);
302 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
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);
321 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
325 case ELF::R_X86_64_PC64: {
327 int64_t RealOffset =
Value + Addend - FinalAddress;
328 support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset)) =
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;
345 support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset)) = GOTOffset;
348 case ELF::R_X86_64_DTPMOD64: {
350 support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset)) = 1;
353 case ELF::R_X86_64_DTPOFF64:
354 case ELF::R_X86_64_TPOFF64: {
359 support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset)) =
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;
370 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
377void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
379 uint32_t
Type, int32_t Addend) {
381 case ELF::R_386_32: {
382 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
388 case ELF::R_386_PLT32:
389 case ELF::R_386_PC32: {
390 uint32_t FinalAddress =
392 uint32_t RealOffset =
Value + Addend - FinalAddress;
393 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
405void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
407 uint32_t
Type, int64_t Addend) {
408 uint32_t *TargetPtr =
409 reinterpret_cast<uint32_t *
>(
Section.getAddressWithOffset(
Offset));
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: {
430 (Result >> 16) == 0);
431 write(isBE, TargetPtr,
static_cast<uint16_t
>(Result & 0xffffU));
434 case ELF::R_AARCH64_ABS32: {
437 (Result >> 32) == 0);
438 write(isBE, TargetPtr,
static_cast<uint32_t
>(Result & 0xffffffffU));
441 case ELF::R_AARCH64_ABS64:
444 case ELF::R_AARCH64_PLT32: {
446 assert(
static_cast<int64_t
>(Result) >= INT32_MIN &&
447 static_cast<int64_t
>(Result) <= INT32_MAX);
448 write(isBE, TargetPtr,
static_cast<uint32_t
>(Result));
451 case ELF::R_AARCH64_PREL16: {
453 assert(
static_cast<int64_t
>(Result) >= INT16_MIN &&
454 static_cast<int64_t
>(Result) <= UINT16_MAX);
455 write(isBE, TargetPtr,
static_cast<uint16_t
>(Result & 0xffffU));
458 case ELF::R_AARCH64_PREL32: {
460 assert(
static_cast<int64_t
>(Result) >= INT32_MIN &&
461 static_cast<int64_t
>(Result) <= UINT32_MAX);
462 write(isBE, TargetPtr,
static_cast<uint32_t
>(Result & 0xffffffffU));
465 case ELF::R_AARCH64_PREL64:
466 write(isBE, TargetPtr,
Value + Addend - FinalAddress);
468 case ELF::R_AARCH64_CONDBR19: {
472 *TargetPtr &= 0xff00001fU;
474 or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
477 case ELF::R_AARCH64_TSTBR14: {
486 or32le(TargetPtr, (BranchImm & 0x0000FFFC) << 3);
489 case ELF::R_AARCH64_CALL26:
490 case ELF::R_AARCH64_JUMP26: {
497 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
500 case ELF::R_AARCH64_MOVW_UABS_G3:
501 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF000000000000) >> 43);
503 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
504 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF00000000) >> 27);
506 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
507 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF0000) >> 11);
509 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
510 or32le(TargetPtr, ((
Value + Addend) & 0xFFFF) << 5);
512 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
515 ((
Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
525 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
531 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
537 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
543 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
549 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
555 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
561 case ELF::R_AARCH64_LD_PREL_LO19: {
568 *TargetPtr &= 0xff00001fU;
571 *TargetPtr |= ((
Result & 0xffc) << (5 - 2));
574 case ELF::R_AARCH64_ADR_PREL_LO21: {
581 *TargetPtr &= 0x9f00001fU;
584 *TargetPtr |= ((
Result & 0xffc) << (5 - 2));
585 *TargetPtr |= (
Result & 0x3) << 29;
591void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
593 uint32_t
Type, int32_t Addend) {
595 uint32_t *TargetPtr =
596 reinterpret_cast<uint32_t *
>(
Section.getAddressWithOffset(
Offset));
597 uint32_t FinalAddress =
Section.getLoadAddressWithOffset(
Offset) & 0xFFFFFFFF;
602 <<
" FinalAddress: " <<
format(
"%p", FinalAddress)
605 <<
" Addend: " <<
format(
"%x", Addend) <<
"\n");
611 case ELF::R_ARM_NONE:
614 case ELF::R_ARM_PREL31:
615 support::ulittle32_t::ref{TargetPtr} =
616 (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
617 ((
Value - FinalAddress) & ~0x80000000);
619 case ELF::R_ARM_TARGET1:
620 case ELF::R_ARM_ABS32:
621 support::ulittle32_t::ref{TargetPtr} =
Value;
625 case ELF::R_ARM_MOVW_ABS_NC:
626 case ELF::R_ARM_MOVT_ABS:
627 if (
Type == ELF::R_ARM_MOVW_ABS_NC)
629 else if (
Type == ELF::R_ARM_MOVT_ABS)
631 support::ulittle32_t::ref{TargetPtr} =
632 (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (
Value & 0xFFF) |
633 (((
Value >> 12) & 0xF) << 16);
636 case ELF::R_ARM_PC24:
637 case ELF::R_ARM_CALL:
638 case ELF::R_ARM_JUMP24:
639 int32_t RelValue =
static_cast<int32_t
>(
Value - FinalAddress - 8);
640 RelValue = (RelValue & 0x03FFFFFC) >> 2;
641 assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
642 support::ulittle32_t::ref{TargetPtr} =
643 (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
648bool RuntimeDyldELF::resolveLoongArch64ShortBranch(
652 if (
Value.SymbolName) {
657 const auto &SymInfo = Loc->second;
659 SymInfo.getOffset());
667 if (RelI->
getType() == ELF::R_LARCH_B26) {
683void RuntimeDyldELF::resolveLoongArch64Branch(
unsigned SectionID,
687 LLVM_DEBUG(
dbgs() <<
"\t\tThis is an LoongArch64 branch relocation.\n");
689 if (resolveLoongArch64ShortBranch(SectionID, RelI,
Value))
694 unsigned RelType = RelI->
getType();
698 resolveRelocation(Section,
Offset,
706 It->second =
Section.getStubOffset();
707 uint8_t *StubTargetAddr =
709 RelocationEntry LU12I_W(SectionID, StubTargetAddr -
Section.getAddress(),
710 ELF::R_LARCH_ABS_HI20,
Value.Addend);
711 RelocationEntry ORI(SectionID, StubTargetAddr -
Section.getAddress() + 4,
712 ELF::R_LARCH_ABS_LO12,
Value.Addend);
713 RelocationEntry LU32I_D(SectionID, StubTargetAddr -
Section.getAddress() + 8,
714 ELF::R_LARCH_ABS64_LO20,
Value.Addend);
715 RelocationEntry LU52I_D(SectionID, StubTargetAddr -
Section.getAddress() + 12,
716 ELF::R_LARCH_ABS64_HI12,
Value.Addend);
717 if (
Value.SymbolName) {
729 resolveRelocation(Section,
Offset,
738 return Hi == 63 ? Val >>
Lo : (Val & (((1ULL << (
Hi + 1)) - 1))) >>
Lo;
745 uint64_t pcalau12i_pc;
747 case ELF::R_LARCH_PCALA64_LO20:
748 case ELF::R_LARCH_GOT64_PC_LO20:
749 pcalau12i_pc = pc - 8;
751 case ELF::R_LARCH_PCALA64_HI12:
752 case ELF::R_LARCH_GOT64_PC_HI12:
753 pcalau12i_pc = pc - 12;
759 uint64_t result = (dest & ~0xfffULL) - (pcalau12i_pc & ~0xfffULL);
761 result += 0x1000 - 0x1'0000'0000;
762 if (result & 0x8000'0000)
763 result += 0x1'0000'0000;
767void RuntimeDyldELF::resolveLoongArch64Relocation(
const SectionEntry &Section,
771 auto *TargetPtr = Section.getAddressWithOffset(
Offset);
774 LLVM_DEBUG(
dbgs() <<
"resolveLoongArch64Relocation, LocalAddress: 0x"
776 <<
" FinalAddress: 0x" <<
format(
"%llx", FinalAddress)
777 <<
" Value: 0x" <<
format(
"%llx",
Value) <<
" Type: 0x"
779 <<
format(
"%llx", Addend) <<
"\n");
785 case ELF::R_LARCH_MARK_LA:
788 case ELF::R_LARCH_32:
789 support::ulittle32_t::ref{TargetPtr} =
790 static_cast<uint32_t
>(
Value + Addend);
792 case ELF::R_LARCH_64:
793 support::ulittle64_t::ref{TargetPtr} =
Value + Addend;
795 case ELF::R_LARCH_32_PCREL:
796 support::ulittle32_t::ref{TargetPtr} =
797 static_cast<uint32_t
>(
Value + Addend - FinalAddress);
799 case ELF::R_LARCH_B26: {
801 auto Instr = support::ulittle32_t::ref(TargetPtr);
804 Instr = (
Instr & 0xfc000000) | Imm15_0 | Imm25_16;
807 case ELF::R_LARCH_CALL36: {
809 auto Pcaddu18i = support::ulittle32_t::ref(TargetPtr);
812 Pcaddu18i = (Pcaddu18i & 0xfe00001f) | Imm35_16;
813 auto Jirl = support::ulittle32_t::ref(TargetPtr + 4);
814 uint32_t Imm15_0 =
extractBits(Call36, 15, 0) << 10;
815 Jirl = (Jirl & 0xfc0003ff) | Imm15_0;
818 case ELF::R_LARCH_GOT_PC_HI20:
819 case ELF::R_LARCH_PCALA_HI20: {
822 auto Instr = support::ulittle32_t::ref(TargetPtr);
823 uint32_t Imm31_12 =
extractBits(PageDelta, 31, 12) << 5;
827 case ELF::R_LARCH_GOT_PC_LO12:
828 case ELF::R_LARCH_PCALA_LO12: {
830 auto Instr = support::ulittle32_t::ref(TargetPtr);
831 uint32_t Imm11_0 = TargetOffset << 10;
835 case ELF::R_LARCH_GOT64_PC_LO20:
836 case ELF::R_LARCH_PCALA64_LO20: {
839 auto Instr = support::ulittle32_t::ref(TargetPtr);
840 uint32_t Imm51_32 =
extractBits(PageDelta, 51, 32) << 5;
844 case ELF::R_LARCH_GOT64_PC_HI12:
845 case ELF::R_LARCH_PCALA64_HI12: {
848 auto Instr = support::ulittle32_t::ref(TargetPtr);
849 uint32_t Imm63_52 =
extractBits(PageDelta, 63, 52) << 10;
853 case ELF::R_LARCH_ABS_HI20: {
855 auto Instr = support::ulittle32_t::ref(TargetPtr);
856 uint32_t Imm31_12 =
extractBits(Target, 31, 12) << 5;
860 case ELF::R_LARCH_ABS_LO12: {
862 auto Instr = support::ulittle32_t::ref(TargetPtr);
863 uint32_t Imm11_0 =
extractBits(Target, 11, 0) << 10;
867 case ELF::R_LARCH_ABS64_LO20: {
869 auto Instr = support::ulittle32_t::ref(TargetPtr);
870 uint32_t Imm51_32 =
extractBits(Target, 51, 32) << 5;
874 case ELF::R_LARCH_ABS64_HI12: {
876 auto Instr = support::ulittle32_t::ref(TargetPtr);
877 uint32_t Imm63_52 =
extractBits(Target, 63, 52) << 10;
881 case ELF::R_LARCH_ADD32:
882 support::ulittle32_t::ref{TargetPtr} =
883 (support::ulittle32_t::ref{TargetPtr} +
884 static_cast<uint32_t
>(
Value + Addend));
886 case ELF::R_LARCH_SUB32:
887 support::ulittle32_t::ref{TargetPtr} =
888 (support::ulittle32_t::ref{TargetPtr} -
889 static_cast<uint32_t
>(
Value + Addend));
891 case ELF::R_LARCH_ADD64:
892 support::ulittle64_t::ref{TargetPtr} =
893 (support::ulittle64_t::ref{TargetPtr} +
Value + Addend);
895 case ELF::R_LARCH_SUB64:
896 support::ulittle64_t::ref{TargetPtr} =
897 (support::ulittle64_t::ref{TargetPtr} -
Value - Addend);
902void RuntimeDyldELF::setMipsABI(
const ObjectFile &Obj) {
911 unsigned AbiVariant =
E->getPlatformFlags();
919Error RuntimeDyldELF::findPPC64TOCSection(
const ELFObjectFileBase &Obj,
920 ObjSectionToIDMap &LocalSections,
932 for (
auto &Section :
Obj.sections()) {
933 Expected<StringRef> NameOrErr =
Section.getName();
938 if (SectionName ==
".got"
939 || SectionName ==
".toc"
940 || SectionName ==
".tocbss"
941 || SectionName ==
".plt") {
942 if (
auto SectionIDOrErr =
946 return SectionIDOrErr.takeError();
960Error RuntimeDyldELF::findOPDEntrySection(
const ELFObjectFileBase &Obj,
961 ObjSectionToIDMap &LocalSections,
968 Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
973 if (RelSecI ==
Obj.section_end())
976 Expected<StringRef> NameOrErr = RelSecI->
getName();
979 StringRef RelSectionName = *NameOrErr;
981 if (RelSectionName !=
".opd")
984 for (elf_relocation_iterator i = si->relocation_begin(),
985 e = si->relocation_end();
990 if (TypeFunc != ELF::R_PPC64_ADDR64) {
995 uint64_t TargetSymbolOffset = i->getOffset();
996 symbol_iterator TargetSymbol = i->getSymbol();
998 if (
auto AddendOrErr = i->getAddend())
999 Addend = *AddendOrErr;
1001 return AddendOrErr.takeError();
1009 if (TypeTOC != ELF::R_PPC64_TOC)
1015 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
1019 if (
auto TSIOrErr = TargetSymbol->
getSection())
1022 return TSIOrErr.takeError();
1023 assert(TSI !=
Obj.section_end() &&
"TSI should refer to a valid section");
1025 bool IsCode = TSI->
isText();
1030 return SectionIDOrErr.takeError();
1031 Rel.
Addend = (intptr_t)Addend;
1043static inline uint16_t
applyPPClo(uint64_t value) {
return value & 0xffff; }
1046 return (value >> 16) & 0xffff;
1050 return ((value + 0x8000) >> 16) & 0xffff;
1054 return (value >> 32) & 0xffff;
1058 return ((value + 0x8000) >> 32) & 0xffff;
1062 return (value >> 48) & 0xffff;
1066 return ((value + 0x8000) >> 48) & 0xffff;
1069void RuntimeDyldELF::resolvePPC32Relocation(
const SectionEntry &Section,
1072 uint8_t *LocalAddress = Section.getAddressWithOffset(
Offset);
1073 uint32_t FinalAddress = Section.getLoadAddressWithOffset(
Offset);
1078 case ELF::R_PPC_ADDR16_LO:
1081 case ELF::R_PPC_ADDR16_HI:
1084 case ELF::R_PPC_ADDR16_HA:
1087 case ELF::R_PPC_ADDR32:
1090 case ELF::R_PPC_REL16_LO:
1093 case ELF::R_PPC_REL16_HA:
1096 case ELF::R_PPC_REL24: {
1097 int32_t Delta =
static_cast<int32_t
>(
Value - FinalAddress + Addend);
1102 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (Delta & 0x03FFFFFC));
1104 case ELF::R_PPC_REL32:
1110void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
1112 uint32_t
Type, int64_t Addend) {
1113 uint8_t *LocalAddress =
Section.getAddressWithOffset(
Offset);
1118 case ELF::R_PPC64_ADDR16:
1121 case ELF::R_PPC64_ADDR16_DS:
1124 case ELF::R_PPC64_ADDR16_LO:
1127 case ELF::R_PPC64_ADDR16_LO_DS:
1130 case ELF::R_PPC64_ADDR16_HI:
1131 case ELF::R_PPC64_ADDR16_HIGH:
1134 case ELF::R_PPC64_ADDR16_HA:
1135 case ELF::R_PPC64_ADDR16_HIGHA:
1138 case ELF::R_PPC64_ADDR16_HIGHER:
1141 case ELF::R_PPC64_ADDR16_HIGHERA:
1144 case ELF::R_PPC64_ADDR16_HIGHEST:
1147 case ELF::R_PPC64_ADDR16_HIGHESTA:
1150 case ELF::R_PPC64_ADDR14: {
1153 uint8_t aalk = *(LocalAddress + 3);
1156 case ELF::R_PPC64_REL16_LO: {
1161 case ELF::R_PPC64_REL16_HI: {
1166 case ELF::R_PPC64_REL16_HA: {
1171 case ELF::R_PPC64_ADDR32: {
1172 int64_t
Result =
static_cast<int64_t
>(
Value + Addend);
1177 case ELF::R_PPC64_REL24: {
1179 int64_t delta =
static_cast<int64_t
>(
Value - FinalAddress + Addend);
1184 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
1186 case ELF::R_PPC64_REL32: {
1188 int64_t delta =
static_cast<int64_t
>(
Value - FinalAddress + Addend);
1193 case ELF::R_PPC64_REL64: {
1198 case ELF::R_PPC64_ADDR64:
1204void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
1206 uint32_t
Type, int64_t Addend) {
1207 uint8_t *LocalAddress =
Section.getAddressWithOffset(
Offset);
1212 case ELF::R_390_PC16DBL:
1213 case ELF::R_390_PLT16DBL: {
1215 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
1219 case ELF::R_390_PC32DBL:
1220 case ELF::R_390_PLT32DBL: {
1222 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
1226 case ELF::R_390_PC16: {
1228 assert(int16_t(Delta) == Delta &&
"R_390_PC16 overflow");
1232 case ELF::R_390_PC32: {
1234 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
1238 case ELF::R_390_PC64: {
1244 *LocalAddress = (uint8_t)(
Value + Addend);
1258#ifdef HEXAGON_TARGET_ENABLED
1262static bool isUnsupportedHexagonRelocation(uint32_t
Type) {
1264 case ELF::R_HEX_GPREL16_0:
1265 case ELF::R_HEX_GPREL16_1:
1266 case ELF::R_HEX_GPREL16_2:
1267 case ELF::R_HEX_GPREL16_3:
1268 case ELF::R_HEX_COPY:
1269 case ELF::R_HEX_GLOB_DAT:
1270 case ELF::R_HEX_JMP_SLOT:
1271 case ELF::R_HEX_RELATIVE:
1272 case ELF::R_HEX_GOTREL_LO16:
1273 case ELF::R_HEX_GOTREL_HI16:
1274 case ELF::R_HEX_GOTREL_32:
1275 case ELF::R_HEX_GOT_LO16:
1276 case ELF::R_HEX_GOT_HI16:
1277 case ELF::R_HEX_GOT_32:
1278 case ELF::R_HEX_GOT_16:
1279 case ELF::R_HEX_DTPMOD_32:
1280 case ELF::R_HEX_DTPREL_LO16:
1281 case ELF::R_HEX_DTPREL_HI16:
1282 case ELF::R_HEX_DTPREL_32:
1283 case ELF::R_HEX_DTPREL_16:
1284 case ELF::R_HEX_GD_PLT_B22_PCREL:
1285 case ELF::R_HEX_GD_GOT_LO16:
1286 case ELF::R_HEX_GD_GOT_HI16:
1287 case ELF::R_HEX_GD_GOT_32:
1288 case ELF::R_HEX_GD_GOT_16:
1289 case ELF::R_HEX_IE_LO16:
1290 case ELF::R_HEX_IE_HI16:
1291 case ELF::R_HEX_IE_32:
1292 case ELF::R_HEX_IE_GOT_LO16:
1293 case ELF::R_HEX_IE_GOT_HI16:
1294 case ELF::R_HEX_IE_GOT_32:
1295 case ELF::R_HEX_IE_GOT_16:
1296 case ELF::R_HEX_TPREL_LO16:
1297 case ELF::R_HEX_TPREL_HI16:
1298 case ELF::R_HEX_TPREL_32:
1299 case ELF::R_HEX_TPREL_16:
1300 case ELF::R_HEX_GOTREL_32_6_X:
1301 case ELF::R_HEX_GOTREL_16_X:
1302 case ELF::R_HEX_GOTREL_11_X:
1303 case ELF::R_HEX_GOT_32_6_X:
1304 case ELF::R_HEX_GOT_16_X:
1305 case ELF::R_HEX_GOT_11_X:
1306 case ELF::R_HEX_DTPREL_32_6_X:
1307 case ELF::R_HEX_DTPREL_16_X:
1308 case ELF::R_HEX_DTPREL_11_X:
1309 case ELF::R_HEX_GD_GOT_32_6_X:
1310 case ELF::R_HEX_GD_GOT_16_X:
1311 case ELF::R_HEX_GD_GOT_11_X:
1312 case ELF::R_HEX_IE_32_6_X:
1313 case ELF::R_HEX_IE_16_X:
1314 case ELF::R_HEX_IE_GOT_32_6_X:
1315 case ELF::R_HEX_IE_GOT_16_X:
1316 case ELF::R_HEX_IE_GOT_11_X:
1317 case ELF::R_HEX_TPREL_32_6_X:
1318 case ELF::R_HEX_TPREL_16_X:
1319 case ELF::R_HEX_TPREL_11_X:
1320 case ELF::R_HEX_LD_PLT_B22_PCREL:
1321 case ELF::R_HEX_LD_GOT_LO16:
1322 case ELF::R_HEX_LD_GOT_HI16:
1323 case ELF::R_HEX_LD_GOT_32:
1324 case ELF::R_HEX_LD_GOT_16:
1325 case ELF::R_HEX_LD_GOT_32_6_X:
1326 case ELF::R_HEX_LD_GOT_16_X:
1327 case ELF::R_HEX_LD_GOT_11_X:
1328 case ELF::R_HEX_GD_PLT_B22_PCREL_X:
1329 case ELF::R_HEX_GD_PLT_B32_PCREL_X:
1330 case ELF::R_HEX_LD_PLT_B22_PCREL_X:
1331 case ELF::R_HEX_LD_PLT_B32_PCREL_X:
1342static bool isHexagonPCRelRelocation(uint32_t
Type) {
1344 case ELF::R_HEX_B22_PCREL:
1345 case ELF::R_HEX_B15_PCREL:
1346 case ELF::R_HEX_B7_PCREL:
1347 case ELF::R_HEX_B13_PCREL:
1348 case ELF::R_HEX_B9_PCREL:
1349 case ELF::R_HEX_B32_PCREL_X:
1350 case ELF::R_HEX_B22_PCREL_X:
1351 case ELF::R_HEX_B15_PCREL_X:
1352 case ELF::R_HEX_B13_PCREL_X:
1353 case ELF::R_HEX_B9_PCREL_X:
1354 case ELF::R_HEX_B7_PCREL_X:
1355 case ELF::R_HEX_32_PCREL:
1356 case ELF::R_HEX_PLT_B22_PCREL:
1357 case ELF::R_HEX_6_PCREL_X:
1367static bool isHexagonExtendedBranchLowRelocation(uint32_t
Type) {
1369 case ELF::R_HEX_B22_PCREL_X:
1370 case ELF::R_HEX_B15_PCREL_X:
1371 case ELF::R_HEX_B13_PCREL_X:
1372 case ELF::R_HEX_B9_PCREL_X:
1373 case ELF::R_HEX_B7_PCREL_X:
1383static unsigned getHexagonRelocationSize(uint32_t
Type) {
1396void RuntimeDyldELF::resolveHexagonRelocation(
const SectionEntry &Section,
1398 uint32_t
Type, int64_t Addend) {
1399#ifdef HEXAGON_TARGET_ENABLED
1400 if (
Type == ELF::R_HEX_NONE)
1405 auto Fail = [&](
const Twine &
Msg) {
1414 return Fail(
"Unknown Hexagon relocation type " + Twine(
Type));
1416 unsigned Size = getHexagonRelocationSize(
Type);
1418 return Fail(Twine(
Info.Name) +
" relocation at offset 0x" +
1423 int64_t Wide =
static_cast<int64_t
>(
Value + Addend);
1424 if (isHexagonPCRelRelocation(
Type))
1425 Wide -=
static_cast<int64_t
>(
Section.getLoadAddressWithOffset(
Offset));
1430 if (
Info.VerifyRange && !isHexagonExtendedBranchLowRelocation(
Type) &&
1432 return Fail(Twine(
Info.Name) +
" relocation out of range");
1434 uint32_t Reloc =
static_cast<uint32_t
>(Wide);
1437 " is not " + Twine(
Info.Alignment) +
"-byte aligned");
1440 if (
Info.VerifyRange && !isHexagonExtendedBranchLowRelocation(
Type)) {
1443 ?
static_cast<uint32_t
>(
static_cast<int32_t
>(Reloc) >>
Info.Shift)
1444 : Reloc >>
Info.Shift;
1446 return Fail(Twine(
Info.Name) +
" relocation out of range: 0x" +
1447 utohexstr(Reloc) +
" does not fit in " +
1448 Twine(
Info.EffectiveBits) +
" bits");
1453 <<
Section.getAddressWithOffset(
Offset) <<
" FinalAddress: "
1455 <<
" Value: " <<
format(
"%x", Reloc)
1457 <<
" Addend: " <<
format(
"%llx", Addend) <<
"\n");
1467void RuntimeDyldELF::resolveBPFRelocation(
const SectionEntry &Section,
1469 uint32_t
Type, int64_t Addend) {
1476 case ELF::R_BPF_NONE:
1477 case ELF::R_BPF_64_64:
1478 case ELF::R_BPF_64_32:
1479 case ELF::R_BPF_64_NODYLD32:
1481 case ELF::R_BPF_64_ABS64: {
1487 case ELF::R_BPF_64_ABS32: {
1500 auto Instr = support::ulittle32_t::ref(InstrAddr);
1501 Instr = (Instr & 0xfff) | UpperImm;
1506 auto Instr = support::ulittle32_t::ref(InstrAddr);
1507 Instr = (Instr & 0xfffff) | (LowerImm << 20);
1510void RuntimeDyldELF::resolveRISCVRelocation(
const SectionEntry &Section,
1516 std::string Err =
"Unimplemented reloc type: " + std::to_string(
Type);
1522 case ELF::R_RISCV_CALL:
1523 case ELF::R_RISCV_CALL_PLT: {
1531 case ELF::R_RISCV_HI20: {
1537 case ELF::R_RISCV_LO12_I: {
1543 case ELF::R_RISCV_GOT_HI20:
1544 case ELF::R_RISCV_PCREL_HI20: {
1558 case ELF::R_RISCV_PCREL_LO12_I: {
1559 for (
auto &&PendingReloc : PendingRelocs) {
1560 const RelocationValueRef &MatchingValue = PendingReloc.first;
1561 RelocationEntry &Reloc = PendingReloc.second;
1564 if (
Value + Addend == HIRelocPC) {
1567 auto PCOffset =
Symbol - HIRelocPC;
1574 "R_RISCV_PCREL_LO12_I without matching R_RISCV_PCREL_HI20");
1576 case ELF::R_RISCV_32_PCREL: {
1578 int64_t RealOffset =
Value + Addend - FinalAddress;
1579 int32_t TruncOffset =
Lo_32(RealOffset);
1580 support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset)) =
1584 case ELF::R_RISCV_32: {
1585 auto Ref = support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset));
1589 case ELF::R_RISCV_64: {
1590 auto Ref = support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset));
1594 case ELF::R_RISCV_ADD8: {
1599 case ELF::R_RISCV_ADD16: {
1600 auto Ref = support::ulittle16_t::ref(
Section.getAddressWithOffset(
Offset));
1604 case ELF::R_RISCV_ADD32: {
1605 auto Ref = support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset));
1609 case ELF::R_RISCV_ADD64: {
1610 auto Ref = support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset));
1614 case ELF::R_RISCV_SUB8: {
1619 case ELF::R_RISCV_SUB16: {
1620 auto Ref = support::ulittle16_t::ref(
Section.getAddressWithOffset(
Offset));
1624 case ELF::R_RISCV_SUB32: {
1625 auto Ref = support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset));
1629 case ELF::R_RISCV_SUB64: {
1630 auto Ref = support::ulittle64_t::ref(
Section.getAddressWithOffset(
Offset));
1634 case ELF::R_RISCV_SET8: {
1639 case ELF::R_RISCV_SET16: {
1640 auto Ref = support::ulittle16_t::ref(
Section.getAddressWithOffset(
Offset));
1644 case ELF::R_RISCV_SET32: {
1645 auto Ref = support::ulittle32_t::ref(
Section.getAddressWithOffset(
Offset));
1679void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
1682 uint64_t SymOffset,
SID SectionID) {
1685 resolveX86_64Relocation(Section,
Offset,
Value,
Type, Addend, SymOffset);
1725 resolveRISCVRelocation(Section,
Offset,
Value,
Type, Addend, SectionID);
1732void *RuntimeDyldELF::computePlaceholderAddress(
unsigned SectionID,
1738 RelocationEntry RE(SectionID,
Offset, RelType,
Value.Addend,
Value.Offset);
1739 if (
Value.SymbolName)
1745uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
1746 bool IsLocal)
const {
1748 case ELF::R_MICROMIPS_GOT16:
1750 return ELF::R_MICROMIPS_LO16;
1752 case ELF::R_MICROMIPS_HI16:
1753 return ELF::R_MICROMIPS_LO16;
1754 case ELF::R_MIPS_GOT16:
1756 return ELF::R_MIPS_LO16;
1758 case ELF::R_MIPS_HI16:
1759 return ELF::R_MIPS_LO16;
1760 case ELF::R_MIPS_PCHI16:
1761 return ELF::R_MIPS_PCLO16;
1765 return ELF::R_MIPS_NONE;
1777bool RuntimeDyldELF::resolveAArch64ShortBranch(
1781 unsigned TargetSectionID;
1782 if (
Value.SymbolName) {
1789 const auto &SymInfo = Loc->second;
1791 TargetSectionID = SymInfo.getSectionID();
1792 TargetOffset = SymInfo.getOffset();
1794 TargetSectionID =
Value.SectionID;
1800 if (TargetSectionID != SectionID)
1811 RelocationEntry RE(SectionID, SourceOffset, RelI->
getType(),
Value.Addend);
1812 if (
Value.SymbolName)
1820void RuntimeDyldELF::resolveAArch64Branch(
unsigned SectionID,
1825 LLVM_DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
1829 unsigned RelType = RelI->
getType();
1831 StubMap::const_iterator i = Stubs.find(
Value);
1832 if (i != Stubs.end()) {
1833 resolveRelocation(Section,
Offset,
1834 Section.getLoadAddressWithOffset(i->second), RelType, 0);
1836 }
else if (!resolveAArch64ShortBranch(SectionID, RelI,
Value)) {
1843 RelocationEntry REmovz_g3(SectionID, StubTargetAddr -
Section.getAddress(),
1844 ELF::R_AARCH64_MOVW_UABS_G3,
Value.Addend);
1845 RelocationEntry REmovk_g2(SectionID,
1846 StubTargetAddr -
Section.getAddress() + 4,
1847 ELF::R_AARCH64_MOVW_UABS_G2_NC,
Value.Addend);
1848 RelocationEntry REmovk_g1(SectionID,
1849 StubTargetAddr -
Section.getAddress() + 8,
1850 ELF::R_AARCH64_MOVW_UABS_G1_NC,
Value.Addend);
1851 RelocationEntry REmovk_g0(SectionID,
1852 StubTargetAddr -
Section.getAddress() + 12,
1853 ELF::R_AARCH64_MOVW_UABS_G0_NC,
Value.Addend);
1855 if (
Value.SymbolName) {
1866 resolveRelocation(Section,
Offset,
1878 uint64_t RelType = RelI->
getType();
1880#ifdef HEXAGON_TARGET_ENABLED
1882 if (isUnsupportedHexagonRelocation(RelType) ||
1887 TypeName +
" (" +
Twine(RelType) +
1893 "Hexagon relocations require an LLVM build with the Hexagon target");
1898 Addend = *AddendOrErr;
1905 if (Symbol != Obj.symbol_end()) {
1906 if (
auto TargetNameOrErr = Symbol->getName())
1907 TargetName = *TargetNameOrErr;
1909 return TargetNameOrErr.takeError();
1911 LLVM_DEBUG(
dbgs() <<
"\t\tRelType: " << RelType <<
" Addend: " << Addend
1912 <<
" TargetName: " << TargetName <<
"\n");
1919 if (Symbol != Obj.symbol_end()) {
1922 if (!SymTypeOrErr) {
1928 SymType = *SymTypeOrErr;
1931 const auto &
SymInfo = gsi->second;
1941 auto SectionOrErr = Symbol->getSection();
1942 if (!SectionOrErr) {
1949 if (si == Obj.section_end())
1952 bool isCode = si->
isText();
1955 Value.SectionID = *SectionIDOrErr;
1957 return SectionIDOrErr.takeError();
1958 Value.Addend = Addend;
1966 Value.Addend = Addend;
1972 if (!
Value.SymbolName)
1973 Value.SymbolName =
"";
1987 if ((RelType == ELF::R_AARCH64_CALL26 ||
1988 RelType == ELF::R_AARCH64_JUMP26) &&
1989 MemMgr.allowStubAllocation()) {
1990 resolveAArch64Branch(SectionID,
Value, RelI, Stubs);
1991 }
else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1994 uint64_t GOTOffset = findOrAllocGOTEntry(
Value, ELF::R_AARCH64_ABS64);
1995 resolveGOTOffsetRelocation(SectionID,
Offset, GOTOffset + Addend,
1996 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1998 }
else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1999 uint64_t GOTOffset = findOrAllocGOTEntry(
Value, ELF::R_AARCH64_ABS64);
2000 resolveGOTOffsetRelocation(SectionID,
Offset, GOTOffset + Addend,
2001 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
2003 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2006 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
2007 RelType == ELF::R_ARM_JUMP24) {
2013 auto [It, Inserted] = Stubs.try_emplace(
Value);
2015 resolveRelocation(Section,
Offset,
2016 Section.getLoadAddressWithOffset(It->second), RelType,
2022 It->second = Section.getStubOffset();
2024 Section.getAddressWithOffset(Section.getStubOffset()));
2026 ELF::R_ARM_ABS32,
Value.Addend);
2027 if (
Value.SymbolName)
2034 Section.getLoadAddressWithOffset(Section.getStubOffset()), RelType,
2040 reinterpret_cast<uint32_t*
>(computePlaceholderAddress(SectionID,
Offset));
2041 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
2042 RelType == ELF::R_ARM_ABS32) {
2043 Value.Addend += *Placeholder;
2044 }
else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
2046 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
2048 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2051 if ((RelType == ELF::R_LARCH_B26 || RelType == ELF::R_LARCH_CALL36) &&
2052 MemMgr.allowStubAllocation()) {
2053 resolveLoongArch64Branch(SectionID,
Value, RelI, Stubs);
2054 }
else if (RelType == ELF::R_LARCH_GOT_PC_HI20 ||
2055 RelType == ELF::R_LARCH_GOT_PC_LO12 ||
2056 RelType == ELF::R_LARCH_GOT64_PC_HI12 ||
2057 RelType == ELF::R_LARCH_GOT64_PC_LO20) {
2058 uint64_t GOTOffset = findOrAllocGOTEntry(
Value, ELF::R_LARCH_64);
2059 resolveGOTOffsetRelocation(SectionID,
Offset, GOTOffset + Addend,
2062 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2066 computePlaceholderAddress(SectionID,
Offset));
2068 if (RelType == ELF::R_MIPS_26) {
2076 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
2078 Value.Addend += Addend;
2081 auto [It, Inserted] = Stubs.try_emplace(
Value);
2089 It->second = Section.getStubOffset();
2091 unsigned AbiVariant = Obj.getPlatformFlags();
2094 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
2097 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
2098 ELF::R_MIPS_HI16,
Value.Addend);
2100 StubTargetAddr - Section.getAddress() + 4,
2101 ELF::R_MIPS_LO16,
Value.Addend);
2103 if (
Value.SymbolName) {
2115 }
else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
2116 int64_t Addend = (Opcode & 0x0000ffff) << 16;
2118 PendingRelocs.push_back(std::make_pair(
Value, RE));
2119 }
else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
2121 for (
auto I = PendingRelocs.begin();
I != PendingRelocs.end();) {
2124 if (MatchingValue ==
Value &&
2125 RelType == getMatchingLoRelocation(
Reloc.RelType) &&
2126 SectionID ==
Reloc.SectionID) {
2127 Reloc.Addend += Addend;
2128 if (
Value.SymbolName)
2132 I = PendingRelocs.erase(
I);
2137 if (
Value.SymbolName)
2142 if (RelType == ELF::R_MIPS_32)
2143 Value.Addend += Opcode;
2144 else if (RelType == ELF::R_MIPS_PC16)
2146 else if (RelType == ELF::R_MIPS_PC19_S2)
2148 else if (RelType == ELF::R_MIPS_PC21_S2)
2150 else if (RelType == ELF::R_MIPS_PC26_S2)
2152 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2157 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
2158 || r_type == ELF::R_MIPS_GOT_DISP) {
2159 auto [
I, Inserted] = GOTSymbolOffsets.try_emplace(TargetName);
2161 I->second = allocateGOTEntries(1);
2163 if (
Value.SymbolName)
2167 }
else if (RelType == ELF::R_MIPS_26) {
2173 StubMap::const_iterator i = Stubs.find(
Value);
2174 if (i != Stubs.end()) {
2181 Stubs[
Value] = Section.getStubOffset();
2183 unsigned AbiVariant = Obj.getPlatformFlags();
2186 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
2190 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
2191 ELF::R_MIPS_HI16,
Value.Addend);
2193 StubTargetAddr - Section.getAddress() + 4,
2194 ELF::R_MIPS_LO16,
Value.Addend);
2195 if (
Value.SymbolName) {
2206 StubTargetAddr - Section.getAddress(),
2207 ELF::R_MIPS_HIGHEST,
Value.Addend);
2209 StubTargetAddr - Section.getAddress() + 4,
2210 ELF::R_MIPS_HIGHER,
Value.Addend);
2212 StubTargetAddr - Section.getAddress() + 12,
2213 ELF::R_MIPS_HI16,
Value.Addend);
2215 StubTargetAddr - Section.getAddress() + 20,
2216 ELF::R_MIPS_LO16,
Value.Addend);
2217 if (
Value.SymbolName) {
2234 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2238 if (RelType == ELF::R_PPC_PLTREL24) {
2241 Value.Addend -= Addend;
2242 RelType = ELF::R_PPC_REL24;
2247 bool NeedsStub = RelType == ELF::R_PPC_REL24 &&
2248 (
Value.SymbolName ||
2251 Section.getAddressWithOffset(
Offset)));
2253 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2255 auto [It, Inserted] = Stubs.try_emplace(
Value, Section.getStubOffset());
2259 processSimpleRelocation(SectionID, It->second + 2, ELF::R_PPC_ADDR16_HI,
2261 processSimpleRelocation(SectionID, It->second + 6, ELF::R_PPC_ADDR16_LO,
2265 resolveRelocation(Section,
Offset,
2266 Section.getLoadAddressWithOffset(It->second), RelType,
2270 if (RelType == ELF::R_PPC64_REL24) {
2272 unsigned AbiVariant = Obj.getPlatformFlags();
2280 bool RangeOverflow =
false;
2283 if (AbiVariant != 2) {
2287 if (
auto Err = findOPDEntrySection(Obj, ObjSectionToID,
Value))
2288 return std::move(Err);
2292 if (
Value.SectionID == SectionID){
2293 uint8_t SymOther = Symbol->getOther();
2299 int64_t delta =
static_cast<int64_t
>(
Target - RelocTarget);
2302 RangeOverflow =
true;
2303 }
else if ((AbiVariant != 2) ||
2304 (AbiVariant == 2 &&
Value.SectionID == SectionID)) {
2309 if (IsExtern || (AbiVariant == 2 &&
Value.SectionID != SectionID) ||
2313 auto [It, Inserted] = Stubs.try_emplace(
Value);
2316 resolveRelocation(Section,
Offset,
2317 Section.getLoadAddressWithOffset(It->second),
2323 It->second = Section.getStubOffset();
2325 Section.getAddressWithOffset(Section.getStubOffset()),
2328 ELF::R_PPC64_ADDR64,
Value.Addend);
2334 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
2336 StubRelocOffset += 2;
2339 ELF::R_PPC64_ADDR16_HIGHEST,
Value.Addend);
2341 ELF::R_PPC64_ADDR16_HIGHER,
Value.Addend);
2343 ELF::R_PPC64_ADDR16_HI,
Value.Addend);
2345 ELF::R_PPC64_ADDR16_LO,
Value.Addend);
2347 if (
Value.SymbolName) {
2361 Section.getLoadAddressWithOffset(Section.getStubOffset()),
2365 if (IsExtern || (AbiVariant == 2 &&
Value.SectionID != SectionID)) {
2367 if (AbiVariant == 2)
2373 }
else if (RelType == ELF::R_PPC64_TOC16 ||
2374 RelType == ELF::R_PPC64_TOC16_DS ||
2375 RelType == ELF::R_PPC64_TOC16_LO ||
2376 RelType == ELF::R_PPC64_TOC16_LO_DS ||
2377 RelType == ELF::R_PPC64_TOC16_HI ||
2378 RelType == ELF::R_PPC64_TOC16_HA) {
2390 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16;
break;
2391 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS;
break;
2392 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO;
break;
2393 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS;
break;
2394 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI;
break;
2395 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA;
break;
2400 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
2401 return std::move(Err);
2411 if (RelType == ELF::R_PPC64_TOC) {
2412 RelType = ELF::R_PPC64_ADDR64;
2413 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID,
Value))
2414 return std::move(Err);
2415 }
else if (TargetName ==
".TOC.") {
2416 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID,
Value))
2417 return std::move(Err);
2418 Value.Addend += Addend;
2423 if (
Value.SymbolName)
2429 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
2439 LLVM_DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
2443 StubMap::const_iterator i = Stubs.find(
Value);
2445 if (i != Stubs.end()) {
2446 StubAddress =
uintptr_t(Section.getAddressWithOffset(i->second));
2454 alignTo(BaseAddress + Section.getStubOffset(), getStubAlignment());
2455 unsigned StubOffset = StubAddress - BaseAddress;
2457 Stubs[
Value] = StubOffset;
2461 if (
Value.SymbolName)
2468 if (RelType == ELF::R_390_GOTENT)
2469 resolveRelocation(Section,
Offset, StubAddress + 8, ELF::R_390_PC32DBL,
2472 resolveRelocation(Section,
Offset, StubAddress, RelType, Addend);
2474 if (RelType == ELF::R_X86_64_PLT32) {
2491 if (
Value.SymbolName &&
MemMgr.allowStubAllocation()) {
2495 auto [It, Inserted] = Stubs.try_emplace(
Value);
2498 StubAddress =
uintptr_t(Section->getAddress()) + It->second;
2505 StubAddress =
alignTo(BaseAddress + Section->getStubOffset(),
2506 getStubAlignment());
2507 unsigned StubOffset = StubAddress - BaseAddress;
2508 It->second = StubOffset;
2515 uint64_t GOTOffset = allocateGOTEntries(1);
2521 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
2522 ELF::R_X86_64_PC32);
2526 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
2531 resolveRelocation(*Section,
Offset, StubAddress, ELF::R_X86_64_PC32,
2534 Value.Addend += support::ulittle32_t::ref(
2535 computePlaceholderAddress(SectionID,
Offset));
2536 processSimpleRelocation(SectionID,
Offset, ELF::R_X86_64_PC32,
Value);
2538 }
else if (RelType == ELF::R_X86_64_GOTPCREL ||
2539 RelType == ELF::R_X86_64_GOTPCRELX ||
2540 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
2541 uint64_t GOTOffset = allocateGOTEntries(1);
2542 resolveGOTOffsetRelocation(SectionID,
Offset, GOTOffset + Addend,
2543 ELF::R_X86_64_PC32);
2547 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_64);
2548 if (
Value.SymbolName)
2552 }
else if (RelType == ELF::R_X86_64_GOT64) {
2554 uint64_t GOTOffset = allocateGOTEntries(1);
2556 ELF::R_X86_64_64, 0);
2560 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_64);
2561 if (
Value.SymbolName)
2565 }
else if (RelType == ELF::R_X86_64_GOTPC32) {
2569 (void)allocateGOTEntries(0);
2570 resolveGOTOffsetRelocation(SectionID,
Offset, Addend, ELF::R_X86_64_PC32);
2571 }
else if (RelType == ELF::R_X86_64_GOTPC64) {
2572 (void)allocateGOTEntries(0);
2573 resolveGOTOffsetRelocation(SectionID,
Offset, Addend, ELF::R_X86_64_PC64);
2574 }
else if (RelType == ELF::R_X86_64_GOTOFF64) {
2576 (void)allocateGOTEntries(0);
2577 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2578 }
else if (RelType == ELF::R_X86_64_PC32) {
2579 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID,
Offset));
2580 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2581 }
else if (RelType == ELF::R_X86_64_PC64) {
2582 Value.Addend += support::ulittle64_t::ref(
2583 computePlaceholderAddress(SectionID,
Offset));
2584 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2585 }
else if (RelType == ELF::R_X86_64_GOTTPOFF) {
2586 processX86_64GOTTPOFFRelocation(SectionID,
Offset,
Value, Addend);
2587 }
else if (RelType == ELF::R_X86_64_TLSGD ||
2588 RelType == ELF::R_X86_64_TLSLD) {
2591 auto &GetAddrRelocation = *RelI;
2592 processX86_64TLSRelocation(SectionID,
Offset, RelType,
Value, Addend,
2595 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2601 if (RelType == ELF::R_RISCV_GOT_HI20 ||
2602 RelType == ELF::R_RISCV_PCREL_HI20 ||
2603 RelType == ELF::R_RISCV_TPREL_HI20 ||
2604 RelType == ELF::R_RISCV_TLS_GD_HI20 ||
2605 RelType == ELF::R_RISCV_TLS_GOT_HI20) {
2607 PendingRelocs.push_back({
Value, RE});
2609 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2612 Value.Addend += support::ulittle32_t::ref(
2613 computePlaceholderAddress(SectionID,
Offset));
2615 processSimpleRelocation(SectionID,
Offset, RelType,
Value);
2620void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(
unsigned SectionID,
2631 struct CodeSequence {
2636 uint64_t TLSSequenceOffset;
2640 uint64_t TpoffRelocationOffset;
2643 std::array<CodeSequence, 2> CodeSequences;
2647 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
2648 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2650 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00
2653 CodeSequences[0].ExpectedCodeSequence =
2654 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
2655 CodeSequences[0].TLSSequenceOffset = 12;
2657 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
2658 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
2659 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00
2661 CodeSequences[0].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
2662 CodeSequences[0].TpoffRelocationOffset = 12;
2667 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
2668 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
2669 0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00
2671 CodeSequences[1].ExpectedCodeSequence =
2672 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
2673 CodeSequences[1].TLSSequenceOffset = 3;
2675 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
2676 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
2677 0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
2679 CodeSequences[1].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
2680 CodeSequences[1].TpoffRelocationOffset = 10;
2685 for (
const auto &
C : CodeSequences) {
2686 assert(
C.ExpectedCodeSequence.size() ==
C.NewCodeSequence.size() &&
2687 "Old and new code sequences must have the same size");
2689 if (
Offset <
C.TLSSequenceOffset ||
2690 (
Offset -
C.TLSSequenceOffset +
C.NewCodeSequence.size()) >
2697 auto TLSSequenceStartOffset =
Offset -
C.TLSSequenceOffset;
2698 auto *TLSSequence =
Section.getAddressWithOffset(TLSSequenceStartOffset);
2699 if (ArrayRef<uint8_t>(TLSSequence,
C.ExpectedCodeSequence.size()) !=
2700 C.ExpectedCodeSequence) {
2704 memcpy(TLSSequence,
C.NewCodeSequence.data(),
C.NewCodeSequence.size());
2710 RelocationEntry RE(SectionID,
2711 TLSSequenceStartOffset +
C.TpoffRelocationOffset,
2712 ELF::R_X86_64_TPOFF32,
Value.Addend - Addend);
2714 if (
Value.SymbolName)
2727 uint64_t GOTOffset = allocateGOTEntries(1);
2728 resolveGOTOffsetRelocation(SectionID,
Offset, GOTOffset + Addend,
2729 ELF::R_X86_64_PC32);
2730 RelocationEntry RE =
2731 computeGOTOffsetRE(GOTOffset,
Value.Offset, ELF::R_X86_64_TPOFF64);
2732 if (
Value.SymbolName)
2739void RuntimeDyldELF::processX86_64TLSRelocation(
2742 const RelocationRef &GetAddrRelocation) {
2752 bool IsSmallCodeModel;
2754 bool IsGOTPCRel =
false;
2756 switch (GetAddrRelocation.
getType()) {
2757 case ELF::R_X86_64_GOTPCREL:
2758 case ELF::R_X86_64_REX_GOTPCRELX:
2759 case ELF::R_X86_64_GOTPCRELX:
2762 case ELF::R_X86_64_PLT32:
2763 IsSmallCodeModel =
true;
2765 case ELF::R_X86_64_PLTOFF64:
2766 IsSmallCodeModel =
false;
2770 "invalid TLS relocations for General/Local Dynamic TLS Model: "
2771 "expected PLT or GOT relocation for __tls_get_addr function");
2778 ArrayRef<uint8_t> ExpectedCodeSequence;
2780 ArrayRef<uint8_t> NewCodeSequence;
2782 if (RelType == ELF::R_X86_64_TLSGD) {
2787 if (IsSmallCodeModel) {
2789 static const std::initializer_list<uint8_t> CodeSequence = {
2791 0x48, 0x8d, 0x3d, 0x00, 0x00,
2795 0xe8, 0x00, 0x00, 0x00, 0x00
2797 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2798 TLSSequenceOffset = 4;
2802 static const std::initializer_list<uint8_t> CodeSequence = {
2804 0x48, 0x8d, 0x3d, 0x00, 0x00,
2808 0xff, 0x15, 0x00, 0x00, 0x00,
2811 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2812 TLSSequenceOffset = 4;
2817 static const std::initializer_list<uint8_t> SmallSequence = {
2818 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2820 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00
2823 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2824 TpoffRelocOffset = 12;
2826 static const std::initializer_list<uint8_t> CodeSequence = {
2827 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
2829 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2834 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2835 TLSSequenceOffset = 3;
2838 static const std::initializer_list<uint8_t> LargeSequence = {
2839 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2841 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00,
2843 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00
2845 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2846 TpoffRelocOffset = 12;
2852 RelocationEntry RE(SectionID,
Offset - TLSSequenceOffset + TpoffRelocOffset,
2853 ELF::R_X86_64_TPOFF32,
Value.Addend - Addend);
2854 if (
Value.SymbolName)
2858 }
else if (RelType == ELF::R_X86_64_TLSLD) {
2859 if (IsSmallCodeModel) {
2861 static const std::initializer_list<uint8_t> CodeSequence = {
2862 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00,
2863 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00
2865 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2866 TLSSequenceOffset = 3;
2869 static const std::initializer_list<uint8_t> SmallSequence = {
2871 0x64, 0x48, 0x8b, 0x04, 0x25,
2872 0x00, 0x00, 0x00, 0x00
2874 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2878 static const std::initializer_list<uint8_t> CodeSequence = {
2879 0x48, 0x8d, 0x3d, 0x00,
2881 0xff, 0x15, 0x00, 0x00,
2885 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2886 TLSSequenceOffset = 3;
2890 static const std::initializer_list<uint8_t> SmallSequence = {
2891 0x0f, 0x1f, 0x40, 0x00,
2892 0x64, 0x48, 0x8b, 0x04, 0x25,
2893 0x00, 0x00, 0x00, 0x00
2895 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2900 static const std::initializer_list<uint8_t> CodeSequence = {
2901 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
2903 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2908 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2909 TLSSequenceOffset = 3;
2912 static const std::initializer_list<uint8_t> LargeSequence = {
2914 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00,
2916 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00
2918 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2925 "Old and new code sequences must have the same size");
2928 if (
Offset < TLSSequenceOffset ||
2929 (
Offset - TLSSequenceOffset + NewCodeSequence.
size()) >
2934 auto *TLSSequence =
Section.getAddressWithOffset(
Offset - TLSSequenceOffset);
2935 if (ArrayRef<uint8_t>(TLSSequence, ExpectedCodeSequence.
size()) !=
2936 ExpectedCodeSequence) {
2938 "invalid TLS sequence for Global/Local Dynamic TLS Model");
2941 memcpy(TLSSequence, NewCodeSequence.
data(), NewCodeSequence.
size());
2956 Result =
sizeof(uint64_t);
2974 Result =
sizeof(uint64_t);
2984uint64_t RuntimeDyldELF::allocateGOTEntries(
unsigned no) {
2985 if (GOTSectionID == 0) {
2992 CurrentGOTIndex += no;
2997 unsigned GOTRelType) {
2998 auto E = GOTOffsetMap.insert({
Value, 0});
3000 uint64_t GOTOffset = allocateGOTEntries(1);
3003 RelocationEntry RE =
3004 computeGOTOffsetRE(GOTOffset,
Value.Offset, GOTRelType);
3005 if (
Value.SymbolName)
3010 E.first->second = GOTOffset;
3013 return E.first->second;
3016void RuntimeDyldELF::resolveGOTOffsetRelocation(
unsigned SectionID,
3021 RelocationEntry GOTRE(SectionID,
Offset,
Type, GOTOffset);
3028 return RelocationEntry(GOTSectionID, GOTOffset,
Type, SymbolOffset);
3031void RuntimeDyldELF::processNewSymbol(
const SymbolRef &ObjSymbol,
SymbolTableEntry& Symbol) {
3036 if (ObjSymbolFlags & SymbolRef::SF_Indirect) {
3037 if (IFuncStubSectionID == 0) {
3040 IFuncStubSectionID =
Sections.size();
3042 SectionEntry(
".text.__llvm_IFuncStubs",
nullptr, 0, 0, 0));
3044 IFuncStubOffset = 64;
3047 IFuncStubs.push_back(IFuncStub{IFuncStubOffset,
Symbol});
3050 Symbol = SymbolTableEntry(IFuncStubSectionID, IFuncStubOffset,
3052 IFuncStubOffset += getMaxIFuncStubSize();
3059 if (!PendingRelocs.empty())
3064 if (IFuncStubSectionID != 0) {
3066 IFuncStubOffset, 1, IFuncStubSectionID,
".text.__llvm_IFuncStubs");
3067 if (!IFuncStubsAddr)
3069 "Unable to allocate memory for IFunc stubs!");
3071 SectionEntry(
".text.__llvm_IFuncStubs", IFuncStubsAddr, IFuncStubOffset,
3072 IFuncStubOffset, 0);
3074 createIFuncResolver(IFuncStubsAddr);
3077 << IFuncStubSectionID <<
" Addr: "
3078 <<
Sections[IFuncStubSectionID].getAddress() <<
'\n');
3079 for (
auto &IFuncStub : IFuncStubs) {
3080 auto &Symbol = IFuncStub.OriginalSymbol;
3082 <<
" Offset: " <<
format(
"%p", Symbol.getOffset())
3083 <<
" IFuncStubOffset: "
3084 <<
format(
"%p\n", IFuncStub.StubOffset));
3085 createIFuncStub(IFuncStubSectionID, 0, IFuncStub.StubOffset,
3086 Symbol.getSectionID(), Symbol.getOffset());
3089 IFuncStubSectionID = 0;
3090 IFuncStubOffset = 0;
3095 if (GOTSectionID != 0) {
3099 GOTSectionID,
".got",
false);
3108 memset(Addr, 0, TotalSize);
3114 if (!
SI->relocations().empty()) {
3121 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
3122 assert(i != SectionMap.end());
3126 GOTSymbolOffsets.clear();
3131 ObjSectionToIDMap::iterator i, e;
3132 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
3142 if (Name ==
".eh_frame") {
3143 UnregisteredEHFrameSections.push_back(i->second);
3148 GOTOffsetMap.clear();
3150 CurrentGOTIndex = 0;
3159void RuntimeDyldELF::createIFuncResolver(
uint8_t *Addr)
const {
3179 0x41, 0xff, 0x53, 0x08,
3191 static_assert(
sizeof(StubCode) <= 64,
3192 "maximum size of the IFunc resolver is 64B");
3193 memcpy(Addr, StubCode,
sizeof(StubCode));
3196 "IFunc resolver is not supported for target architecture");
3200void RuntimeDyldELF::createIFuncStub(
unsigned IFuncStubSectionID,
3203 unsigned IFuncSectionID,
3205 auto &IFuncStubSection =
Sections[IFuncStubSectionID];
3206 auto *Addr = IFuncStubSection.getAddressWithOffset(IFuncStubOffset);
3230 uint64_t GOT1 = allocateGOTEntries(2);
3233 RelocationEntry RE1(GOTSectionID, GOT1, ELF::R_X86_64_64,
3234 IFuncResolverOffset, {});
3236 RelocationEntry RE2(GOTSectionID, GOT2, ELF::R_X86_64_64, IFuncOffset, {});
3239 const uint8_t StubCode[] = {
3240 0x4c, 0x8d, 0x1d, 0x00, 0x00, 0x00, 0x00,
3243 assert(
sizeof(StubCode) <= getMaxIFuncStubSize() &&
3244 "IFunc stub size must not exceed getMaxIFuncStubSize()");
3245 memcpy(Addr, StubCode,
sizeof(StubCode));
3249 resolveGOTOffsetRelocation(IFuncStubSectionID, IFuncStubOffset + 3,
3250 GOT1 - 4, ELF::R_X86_64_PC32);
3256unsigned RuntimeDyldELF::getMaxIFuncStubSize()
const {
3263bool RuntimeDyldELF::relocationNeedsGot(
const RelocationRef &R)
const {
3264 unsigned RelTy =
R.getType();
3266 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
3267 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
3270 return RelTy == ELF::R_LARCH_GOT_PC_HI20 ||
3271 RelTy == ELF::R_LARCH_GOT_PC_LO12 ||
3272 RelTy == ELF::R_LARCH_GOT64_PC_HI12 ||
3273 RelTy == ELF::R_LARCH_GOT64_PC_LO20;
3276 return RelTy == ELF::R_X86_64_GOTPCREL ||
3277 RelTy == ELF::R_X86_64_GOTPCRELX ||
3278 RelTy == ELF::R_X86_64_GOT64 ||
3279 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
3283bool RuntimeDyldELF::relocationNeedsStub(
const RelocationRef &R)
const {
3287 switch (
R.getType()) {
3292 case ELF::R_X86_64_GOTPCREL:
3293 case ELF::R_X86_64_GOTPCRELX:
3294 case ELF::R_X86_64_REX_GOTPCRELX:
3295 case ELF::R_X86_64_GOTPC64:
3296 case ELF::R_X86_64_GOT64:
3297 case ELF::R_X86_64_GOTOFF64:
3298 case ELF::R_X86_64_PC32:
3299 case ELF::R_X86_64_PC64:
3300 case ELF::R_X86_64_64:
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
LLVM_ABI bool LLVMHexagonVerifyAlignment(uint32_t RelocType, uint32_t Value)
Return true if Value satisfies the alignment required by RelocType.
LLVM_ABI uint32_t LLVMHexagonApplyRelocation(uint32_t RelocType, uint32_t Instruction, uint32_t Value)
Apply the ELF relocation RelocType with value Value to Instruction.
struct LLVMHexagonRelocInfo LLVMHexagonRelocInfo
ABI properties of a Hexagon ELF relocation type, as listed in the Hexagon ABI document.
LLVM_ABI bool LLVMHexagonVerifyRange(uint32_t RelocType, uint32_t Value)
Return true if Value fits the range the ABI specifies for RelocType.
LLVM_ABI bool LLVMHexagonGetRelocationInfo(uint32_t RelocType, LLVMHexagonRelocInfo *Info)
Fill Info with the properties of RelocType.
static void or32le(void *P, int32_t V)
static void or32AArch64Imm(void *L, uint64_t Imm)
static uint64_t getBits(uint64_t Val, int Start, int End)
static void write32AArch64Addr(void *L, uint64_t Imm)
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
Symbol resolution interface.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
RelocationEntry - used to represent relocations internally in the dynamic linker.
uint32_t RelType
RelType - relocation type.
uint64_t Offset
Offset - offset into the section.
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
unsigned SectionID
SectionID - the section this relocation points to.
void registerEHFrames() override
size_t getGOTEntrySize() override
~RuntimeDyldELF() override
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
DenseMap< SID, SID > SectionToGOTMap
bool isCompatibleFile(const object::ObjectFile &Obj) const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
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 ...
std::map< SectionRef, unsigned > ObjSectionToIDMap
void writeInt32BE(uint8_t *Addr, uint32_t Value)
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
void writeInt64BE(uint8_t *Addr, uint64_t Value)
std::map< RelocationValueRef, uintptr_t > StubMap
void writeInt16BE(uint8_t *Addr, uint16_t Value)
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
bool IsTargetLittleEndian
JITSymbolResolver & Resolver
RuntimeDyld::MemoryManager & MemMgr
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint64_t getSectionLoadAddress(unsigned SectionID) const
virtual unsigned getMaxStubSize() const =0
RTDyldSymbolTable GlobalSymbolTable
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
SectionEntry - represents a section emitted into memory by the dynamic linker.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringMapIterBase< SymbolTableEntry, true > const_iterator
Represent a constant reference to a string, i.e.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Symbol info for RuntimeDyld.
Target - Wrapper for Target specific information.
static LLVM_ABI StringRef getArchTypePrefix(ArchType Kind)
Get the "prefix" canonical name for the Kind architecture.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
DataRefImpl getRawDataRefImpl() const
Expected< int64_t > getAddend() const
This class is the base class for all object file types.
virtual section_iterator section_begin() const =0
uint64_t getOffset() const
symbol_iterator getSymbol() const
void getTypeName(SmallVectorImpl< char > &Result) const
Get a string that represents the type of this relocation.
This is a value type class that represents a single section in the list of sections in the object fil...
DataRefImpl getRawDataRefImpl() const
bool isText() const
Whether this section contains instructions.
Expected< StringRef > getName() const
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.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
content_iterator< SectionRef > section_iterator
content_iterator< RelocationRef > relocation_iterator
@ Resolved
Queried, materialization begun.
NodeAddr< InstrNode * > Instr
void write32le(void *P, uint32_t V)
uint32_t read32le(const void *P)
detail::packed_endian_specific_integral< int32_t, llvm::endianness::little, unaligned > little32_t
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
static uint16_t applyPPChighera(uint64_t value)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
static uint16_t applyPPChi(uint64_t value)
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
static void applyITypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm)
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
static uint16_t applyPPChighesta(uint64_t value)
static uint16_t applyPPChighest(uint64_t value)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
static uint16_t applyPPCha(uint64_t value)
static void applyUTypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
static uint16_t applyPPClo(uint64_t value)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
@ Ref
The access may reference the value stored in memory.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static uint16_t applyPPChigher(uint64_t value)
static void or32le(void *P, int32_t V)
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
static uint32_t extractBits(uint64_t Val, uint32_t Hi, uint32_t Lo)
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
static uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, uint32_t type)
@ End
Run the machine verifier once, at the end of the pipeline.
void consumeError(Error Err)
Consume a Error without doing anything.
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift)
Implement std::hash so that hash_code can be used in STL containers.
ABI properties of a Hexagon ELF relocation type, as listed in the Hexagon ABI document.
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...