29 #define DEBUG_TYPE "dyld" 34 or32le(L, (Imm & 0xFFF) << 10);
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);
43 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
44 uint64_t
Mask = (0x3 << 29) | (0x1FFFFC << 3);
50 static uint64_t
getBits(uint64_t Val,
int Start,
int End) {
51 uint64_t
Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
52 return (Val >> Start) &
Mask;
57 template <
class ELFT>
class DyldELFObject :
public ELFObjectFile<ELFT> {
67 typedef typename ELFT::uint addr_type;
75 void updateSectionAddress(
const SectionRef &Sec, uint64_t Addr);
77 void updateSymbolAddress(
const SymbolRef &SymRef, uint64_t Addr);
80 static bool classof(
const Binary *v) {
97 this->isDyldELFObject =
true;
100 template <
class ELFT>
104 if (
auto E = Obj.takeError())
106 std::unique_ptr<DyldELFObject<ELFT>>
Ret(
107 new DyldELFObject<ELFT>(std::move(*Obj)));
108 return std::move(Ret);
111 template <
class ELFT>
112 void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
116 const_cast<Elf_Shdr *
>(
reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
120 shdr->sh_addr =
static_cast<addr_type
>(Addr);
123 template <
class ELFT>
124 void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
127 Elf_Sym *sym =
const_cast<Elf_Sym *
>(
132 sym->st_value =
static_cast<addr_type
>(Addr);
135 class LoadedELFObjectInfo final
137 RuntimeDyld::LoadedObjectInfo> {
139 LoadedELFObjectInfo(
RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
143 getObjectForDebug(
const ObjectFile &Obj)
const override;
146 template <
typename ELFT>
149 const LoadedELFObjectInfo &L) {
150 typedef typename ELFT::Shdr Elf_Shdr;
151 typedef typename ELFT::uint addr_type;
154 DyldELFObject<ELFT>::create(Buffer);
158 std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
162 for (
const auto &Sec : Obj->sections()) {
169 if (*NameOrErr !=
"") {
171 Elf_Shdr *shdr =
const_cast<Elf_Shdr *
>(
172 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
174 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*
SI)) {
177 shdr->sh_addr =
static_cast<addr_type
>(SecLoadAddr);
183 return std::move(Obj);
187 createELFDebugObject(
const ObjectFile &Obj,
const LoadedELFObjectInfo &L) {
190 std::unique_ptr<MemoryBuffer> Buffer =
197 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
200 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
203 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
206 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
215 LoadedELFObjectInfo::getObjectForDebug(
const ObjectFile &Obj)
const {
216 return createELFDebugObject(Obj, *
this);
225 :
RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
229 for (
int i = 0,
e = UnregisteredEHFrameSections.
size(); i !=
e; ++i) {
230 SID EHFrameSID = UnregisteredEHFrameSections[i];
231 uint8_t *EHFrameAddr =
Sections[EHFrameSID].getAddress();
232 uint64_t EHFrameLoadAddr =
Sections[EHFrameSID].getLoadAddress();
233 size_t EHFrameSize =
Sections[EHFrameSID].getSize();
236 UnregisteredEHFrameSections.
clear();
239 std::unique_ptr<RuntimeDyldELF>
254 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
257 return std::make_unique<LoadedELFObjectInfo>(*
this, *ObjSectionToIDOrErr);
269 uint64_t SymOffset) {
274 case ELF::R_X86_64_NONE:
276 case ELF::R_X86_64_64: {
283 case ELF::R_X86_64_32:
284 case ELF::R_X86_64_32S: {
286 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
287 (Type == ELF::R_X86_64_32S &&
288 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
289 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
296 case ELF::R_X86_64_PC8: {
298 int64_t RealOffset = Value + Addend - FinalAddress;
300 int8_t TruncOffset = (RealOffset & 0xFF);
304 case ELF::R_X86_64_PC32: {
306 int64_t RealOffset = Value + Addend - FinalAddress;
308 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
313 case ELF::R_X86_64_PC64: {
315 int64_t RealOffset = Value + Addend - FinalAddress;
319 <<
format(
"%p\n", FinalAddress));
322 case ELF::R_X86_64_GOTOFF64: {
324 uint64_t GOTBase = 0;
325 for (
const auto &Section :
Sections) {
326 if (Section.
getName() ==
".got") {
331 assert(GOTBase != 0 &&
"missing GOT");
332 int64_t GOTOffset = Value - GOTBase + Addend;
339 void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
343 case ELF::R_386_32: {
350 case ELF::R_386_PLT32:
351 case ELF::R_386_PC32: {
354 uint32_t RealOffset = Value + Addend - FinalAddress;
367 void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
368 uint64_t Offset, uint64_t Value,
378 <<
" FinalAddress: 0x" <<
format(
"%llx", FinalAddress)
379 <<
" Value: 0x" <<
format(
"%llx", Value) <<
" Type: 0x" 380 <<
format(
"%x", Type) <<
" Addend: 0x" 381 <<
format(
"%llx", Addend) <<
"\n");
387 case ELF::R_AARCH64_ABS16: {
388 uint64_t Result = Value + Addend;
389 assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
390 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
393 case ELF::R_AARCH64_ABS32: {
394 uint64_t Result = Value + Addend;
395 assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
396 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
399 case ELF::R_AARCH64_ABS64:
400 write(isBE, TargetPtr, Value + Addend);
402 case ELF::R_AARCH64_PREL32: {
403 uint64_t Result = Value + Addend - FinalAddress;
404 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
405 static_cast<int64_t>(Result) <= UINT32_MAX);
406 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
409 case ELF::R_AARCH64_PREL64:
410 write(isBE, TargetPtr, Value + Addend - FinalAddress);
412 case ELF::R_AARCH64_CALL26:
413 case ELF::R_AARCH64_JUMP26: {
416 uint64_t BranchImm = Value + Addend - FinalAddress;
419 assert(isInt<28>(BranchImm));
420 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
423 case ELF::R_AARCH64_MOVW_UABS_G3:
424 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
426 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
427 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
429 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
430 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
432 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
433 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
435 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
438 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
441 assert(isInt<33>(Result) &&
"overflow check failed for relocation");
448 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
454 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
460 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
466 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
472 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
478 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
487 void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
498 <<
" FinalAddress: " <<
format(
"%p", FinalAddress)
499 <<
" Value: " <<
format(
"%x", Value)
500 <<
" Type: " <<
format(
"%x", Type)
501 <<
" Addend: " <<
format(
"%x", Addend) <<
"\n");
507 case ELF::R_ARM_NONE:
510 case ELF::R_ARM_PREL31:
513 ((Value - FinalAddress) & ~0x80000000);
515 case ELF::R_ARM_TARGET1:
516 case ELF::R_ARM_ABS32:
521 case ELF::R_ARM_MOVW_ABS_NC:
522 case ELF::R_ARM_MOVT_ABS:
523 if (Type == ELF::R_ARM_MOVW_ABS_NC)
524 Value = Value & 0xFFFF;
525 else if (Type == ELF::R_ARM_MOVT_ABS)
526 Value = (Value >> 16) & 0xFFFF;
529 (((Value >> 12) & 0xF) << 16);
532 case ELF::R_ARM_PC24:
533 case ELF::R_ARM_CALL:
534 case ELF::R_ARM_JUMP24:
535 int32_t RelValue =
static_cast<int32_t
>(Value - FinalAddress - 8);
536 RelValue = (RelValue & 0x03FFFFFC) >> 2;
544 void RuntimeDyldELF::setMipsABI(
const ObjectFile &Obj) {
552 if (
auto *
E = dyn_cast<ELFObjectFileBase>(&Obj)) {
553 unsigned AbiVariant =
E->getPlatformFlags();
574 for (
auto &Section : Obj.
sections()) {
580 if (SectionName ==
".got" 581 || SectionName ==
".toc" 582 || SectionName ==
".tocbss" 583 || SectionName ==
".plt") {
584 if (
auto SectionIDOrErr =
588 return SectionIDOrErr.takeError();
623 if (RelSectionName !=
".opd")
627 e = si->relocation_end();
631 uint64_t TypeFunc = i->getType();
632 if (TypeFunc != ELF::R_PPC64_ADDR64) {
637 uint64_t TargetSymbolOffset = i->getOffset();
640 if (
auto AddendOrErr = i->getAddend())
641 Addend = *AddendOrErr;
643 return AddendOrErr.takeError();
650 uint64_t TypeTOC = i->
getType();
651 if (TypeTOC != ELF::R_PPC64_TOC)
657 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
661 if (
auto TSIOrErr = TargetSymbol->
getSection())
664 return TSIOrErr.takeError();
667 bool IsCode = TSI->isText();
672 return SectionIDOrErr.takeError();
688 return (value >> 16) & 0xffff;
692 return ((value + 0x8000) >> 16) & 0xffff;
696 return (value >> 32) & 0xffff;
700 return ((value + 0x8000) >> 32) & 0xffff;
704 return (value >> 48) & 0xffff;
708 return ((value + 0x8000) >> 48) & 0xffff;
711 void RuntimeDyldELF::resolvePPC32Relocation(
const SectionEntry &Section,
712 uint64_t Offset, uint64_t Value,
719 case ELF::R_PPC_ADDR16_LO:
722 case ELF::R_PPC_ADDR16_HI:
725 case ELF::R_PPC_ADDR16_HA:
731 void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
732 uint64_t Offset, uint64_t Value,
739 case ELF::R_PPC64_ADDR16:
742 case ELF::R_PPC64_ADDR16_DS:
745 case ELF::R_PPC64_ADDR16_LO:
748 case ELF::R_PPC64_ADDR16_LO_DS:
751 case ELF::R_PPC64_ADDR16_HI:
752 case ELF::R_PPC64_ADDR16_HIGH:
755 case ELF::R_PPC64_ADDR16_HA:
756 case ELF::R_PPC64_ADDR16_HIGHA:
759 case ELF::R_PPC64_ADDR16_HIGHER:
762 case ELF::R_PPC64_ADDR16_HIGHERA:
765 case ELF::R_PPC64_ADDR16_HIGHEST:
768 case ELF::R_PPC64_ADDR16_HIGHESTA:
771 case ELF::R_PPC64_ADDR14: {
772 assert(((Value + Addend) & 3) == 0);
774 uint8_t aalk = *(LocalAddress + 3);
775 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
777 case ELF::R_PPC64_REL16_LO: {
779 uint64_t Delta = Value - FinalAddress + Addend;
782 case ELF::R_PPC64_REL16_HI: {
784 uint64_t Delta = Value - FinalAddress + Addend;
787 case ELF::R_PPC64_REL16_HA: {
789 uint64_t Delta = Value - FinalAddress + Addend;
792 case ELF::R_PPC64_ADDR32: {
793 int64_t Result =
static_cast<int64_t
>(Value + Addend);
794 if (SignExtend64<32>(Result) != Result)
798 case ELF::R_PPC64_REL24: {
800 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
801 if (SignExtend64<26>(delta) != delta)
805 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
807 case ELF::R_PPC64_REL32: {
809 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
810 if (SignExtend64<32>(delta) != delta)
814 case ELF::R_PPC64_REL64: {
816 uint64_t Delta = Value - FinalAddress + Addend;
819 case ELF::R_PPC64_ADDR64:
825 void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
826 uint64_t Offset, uint64_t Value,
833 case ELF::R_390_PC16DBL:
834 case ELF::R_390_PLT16DBL: {
836 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
840 case ELF::R_390_PC32DBL:
841 case ELF::R_390_PLT32DBL: {
843 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
847 case ELF::R_390_PC16: {
849 assert(int16_t(Delta) == Delta &&
"R_390_PC16 overflow");
853 case ELF::R_390_PC32: {
855 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
859 case ELF::R_390_PC64: {
865 *LocalAddress = (uint8_t)(Value + Addend);
879 void RuntimeDyldELF::resolveBPFRelocation(
const SectionEntry &Section,
880 uint64_t Offset, uint64_t Value,
888 case ELF::R_BPF_NONE:
890 case ELF::R_BPF_64_64: {
896 case ELF::R_BPF_64_32: {
898 assert(Value <= UINT32_MAX);
934 void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
935 uint64_t Offset, uint64_t Value,
937 uint64_t SymOffset,
SID SectionID) {
940 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
943 resolveX86Relocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
948 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
954 resolveARMRelocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
958 resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
962 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
965 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
969 resolveBPFRelocation(Section, Offset, Value, Type, Addend);
976 void *RuntimeDyldELF::computePlaceholderAddress(
unsigned SectionID, uint64_t Offset)
const {
980 void RuntimeDyldELF::processSimpleRelocation(
unsigned SectionID, uint64_t Offset,
unsigned RelType,
RelocationValueRef Value) {
989 bool IsLocal)
const {
991 case ELF::R_MICROMIPS_GOT16:
993 return ELF::R_MICROMIPS_LO16;
995 case ELF::R_MICROMIPS_HI16:
996 return ELF::R_MICROMIPS_LO16;
997 case ELF::R_MIPS_GOT16:
999 return ELF::R_MIPS_LO16;
1001 case ELF::R_MIPS_HI16:
1002 return ELF::R_MIPS_LO16;
1003 case ELF::R_MIPS_PCHI16:
1004 return ELF::R_MIPS_PCLO16;
1008 return ELF::R_MIPS_NONE;
1020 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1031 const auto &
SymInfo = Loc->second;
1038 uint64_t Offset = RelI->getOffset();
1039 uint64_t SourceAddress =
Sections[SectionID].getLoadAddressWithOffset(Offset);
1044 if (!isInt<28>(Address + Value.
Addend - SourceAddress))
1047 resolveRelocation(
Sections[SectionID], Offset, Address, RelI->getType(),
1053 void RuntimeDyldELF::resolveAArch64Branch(
unsigned SectionID,
1058 LLVM_DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
1061 uint64_t Offset = RelI->getOffset();
1062 unsigned RelType = RelI->getType();
1064 StubMap::const_iterator i = Stubs.find(Value);
1065 if (i != Stubs.end()) {
1066 resolveRelocation(Section, Offset,
1070 }
else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1078 ELF::R_AARCH64_MOVW_UABS_G3, Value.
Addend);
1081 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.
Addend);
1084 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.
Addend);
1087 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.
Addend);
1100 resolveRelocation(Section, Offset,
1112 const auto &Obj = cast<ELFObjectFileBase>(
O);
1113 uint64_t RelType = RelI->getType();
1116 Addend = *AddendOrErr;
1124 if (
auto TargetNameOrErr = Symbol->getName())
1125 TargetName = *TargetNameOrErr;
1127 return TargetNameOrErr.takeError();
1129 LLVM_DEBUG(
dbgs() <<
"\t\tRelType: " << RelType <<
" Addend: " << Addend
1130 <<
" TargetName: " << TargetName <<
"\n");
1140 if (!SymTypeOrErr) {
1147 SymType = *SymTypeOrErr;
1150 const auto &
SymInfo = gsi->second;
1156 case SymbolRef::ST_Debug: {
1160 auto SectionOrErr = Symbol->getSection();
1161 if (!SectionOrErr) {
1172 bool isCode = si->isText();
1177 return SectionIDOrErr.takeError();
1181 case SymbolRef::ST_Data:
1182 case SymbolRef::ST_Function:
1183 case SymbolRef::ST_Unknown: {
1201 uint64_t Offset = RelI->getOffset();
1203 LLVM_DEBUG(
dbgs() <<
"\t\tSectionID: " << SectionID <<
" Offset: " << Offset
1206 if (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26) {
1207 resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1208 }
else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1211 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1212 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1213 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1215 }
else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1216 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1217 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1218 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1220 processSimpleRelocation(SectionID, Offset, RelType, Value);
1223 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1224 RelType == ELF::R_ARM_JUMP24) {
1230 StubMap::const_iterator i = Stubs.find(Value);
1231 if (i != Stubs.end()) {
1244 ELF::R_ARM_ABS32, Value.
Addend);
1250 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1258 reinterpret_cast<uint32_t*
>(computePlaceholderAddress(SectionID, Offset));
1259 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1260 RelType == ELF::R_ARM_ABS32) {
1261 Value.
Addend += *Placeholder;
1262 }
else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1264 Value.
Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1266 processSimpleRelocation(SectionID, Offset, RelType, Value);
1269 uint8_t *Placeholder =
reinterpret_cast<uint8_t *
>(
1270 computePlaceholderAddress(SectionID, Offset));
1272 if (RelType == ELF::R_MIPS_26) {
1280 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1285 StubMap::const_iterator i = Stubs.find(Value);
1286 if (i != Stubs.end()) {
1302 ELF::R_MIPS_HI16, Value.
Addend);
1305 ELF::R_MIPS_LO16, Value.
Addend);
1319 }
else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1320 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1322 PendingRelocs.push_back(std::make_pair(Value, RE));
1323 }
else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1324 int64_t Addend = Value.
Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1325 for (
auto I = PendingRelocs.begin();
I != PendingRelocs.end();) {
1328 if (MatchingValue == Value &&
1329 RelType == getMatchingLoRelocation(Reloc.
RelType) &&
1336 I = PendingRelocs.erase(
I);
1346 if (RelType == ELF::R_MIPS_32)
1348 else if (RelType == ELF::R_MIPS_PC16)
1349 Value.
Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1350 else if (RelType == ELF::R_MIPS_PC19_S2)
1351 Value.
Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1352 else if (RelType == ELF::R_MIPS_PC21_S2)
1353 Value.
Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1354 else if (RelType == ELF::R_MIPS_PC26_S2)
1355 Value.
Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1356 processSimpleRelocation(SectionID, Offset, RelType, Value);
1361 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1362 || r_type == ELF::R_MIPS_GOT_DISP) {
1364 if (i != GOTSymbolOffsets.
end())
1368 GOTSymbolOffsets[TargetName] = RE.
SymOffset;
1374 }
else if (RelType == ELF::R_MIPS_26) {
1380 StubMap::const_iterator i = Stubs.find(Value);
1381 if (i != Stubs.end()) {
1398 ELF::R_MIPS_HI16, Value.
Addend);
1401 ELF::R_MIPS_LO16, Value.
Addend);
1414 ELF::R_MIPS_HIGHEST, Value.
Addend);
1417 ELF::R_MIPS_HIGHER, Value.
Addend);
1420 ELF::R_MIPS_HI16, Value.
Addend);
1423 ELF::R_MIPS_LO16, Value.
Addend);
1441 processSimpleRelocation(SectionID, Offset, RelType, Value);
1445 if (RelType == ELF::R_PPC64_REL24) {
1455 bool RangeOverflow =
false;
1456 bool IsExtern = Value.
SymbolName || SymType == SymbolRef::ST_Unknown;
1458 if (AbiVariant != 2) {
1462 if (
auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1463 return std::move(Err);
1468 uint8_t SymOther =
Symbol->getOther();
1472 uint8_t *RelocTarget =
1474 int64_t delta =
static_cast<int64_t
>(Target - RelocTarget);
1476 if (SignExtend64<26>(delta) != delta) {
1477 RangeOverflow =
true;
1478 }
else if ((AbiVariant != 2) ||
1479 (AbiVariant == 2 && Value.
SectionID == SectionID)) {
1484 if (IsExtern || (AbiVariant == 2 && Value.
SectionID != SectionID) ||
1488 StubMap::const_iterator i = Stubs.find(Value);
1489 if (i != Stubs.end()) {
1491 resolveRelocation(Section, Offset,
1492 reinterpret_cast<uint64_t>(
1504 ELF::R_PPC64_ADDR64, Value.
Addend);
1510 uint64_t StubRelocOffset = StubTargetAddr - Section.
getAddress();
1512 StubRelocOffset += 2;
1515 ELF::R_PPC64_ADDR16_HIGHEST, Value.
Addend);
1517 ELF::R_PPC64_ADDR16_HIGHER, Value.
Addend);
1519 ELF::R_PPC64_ADDR16_HI, Value.
Addend);
1521 ELF::R_PPC64_ADDR16_LO, Value.
Addend);
1535 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1541 if (IsExtern || (AbiVariant == 2 && Value.
SectionID != SectionID)) {
1543 if (AbiVariant == 2)
1549 }
else if (RelType == ELF::R_PPC64_TOC16 ||
1550 RelType == ELF::R_PPC64_TOC16_DS ||
1551 RelType == ELF::R_PPC64_TOC16_LO ||
1552 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1553 RelType == ELF::R_PPC64_TOC16_HI ||
1554 RelType == ELF::R_PPC64_TOC16_HA) {
1566 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16;
break;
1567 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS;
break;
1568 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO;
break;
1569 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS;
break;
1570 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI;
break;
1571 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA;
break;
1576 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1577 return std::move(Err);
1581 resolveRelocation(
Sections[SectionID], Offset, Value.
Addend, RelType, 0);
1587 if (RelType == ELF::R_PPC64_TOC) {
1588 RelType = ELF::R_PPC64_ADDR64;
1589 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1590 return std::move(Err);
1591 }
else if (TargetName ==
".TOC.") {
1592 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1593 return std::move(Err);
1605 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1615 LLVM_DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
1619 StubMap::const_iterator i = Stubs.find(Value);
1620 uintptr_t StubAddress;
1621 if (i != Stubs.end()) {
1628 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1629 uintptr_t StubAlignment = getStubAlignment();
1631 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1633 unsigned StubOffset = StubAddress - BaseAddress;
1635 Stubs[
Value] = StubOffset;
1646 if (RelType == ELF::R_390_GOTENT)
1647 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1650 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1652 if (RelType == ELF::R_X86_64_PLT32) {
1673 StubMap::const_iterator i = Stubs.find(Value);
1674 uintptr_t StubAddress;
1675 if (i != Stubs.end()) {
1676 StubAddress = uintptr_t(Section.
getAddress()) + i->second;
1682 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1683 uintptr_t StubAlignment = getStubAlignment();
1685 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1687 unsigned StubOffset = StubAddress - BaseAddress;
1688 Stubs[
Value] = StubOffset;
1695 uint64_t GOTOffset = allocateGOTEntries(1);
1698 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1699 ELF::R_X86_64_PC32);
1703 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1708 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1715 }
else if (RelType == ELF::R_X86_64_GOTPCREL ||
1716 RelType == ELF::R_X86_64_GOTPCRELX ||
1717 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1718 uint64_t GOTOffset = allocateGOTEntries(1);
1719 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1720 ELF::R_X86_64_PC32);
1724 computeGOTOffsetRE(GOTOffset, Value.
Offset, ELF::R_X86_64_64);
1729 }
else if (RelType == ELF::R_X86_64_GOT64) {
1731 uint64_t GOTOffset = allocateGOTEntries(1);
1732 resolveRelocation(
Sections[SectionID], Offset, GOTOffset,
1733 ELF::R_X86_64_64, 0);
1737 computeGOTOffsetRE(GOTOffset, Value.
Offset, ELF::R_X86_64_64);
1742 }
else if (RelType == ELF::R_X86_64_GOTPC64) {
1746 (void)allocateGOTEntries(0);
1747 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1748 }
else if (RelType == ELF::R_X86_64_GOTOFF64) {
1750 (void)allocateGOTEntries(0);
1751 processSimpleRelocation(SectionID, Offset, RelType, Value);
1752 }
else if (RelType == ELF::R_X86_64_PC32) {
1754 processSimpleRelocation(SectionID, Offset, RelType, Value);
1755 }
else if (RelType == ELF::R_X86_64_PC64) {
1757 processSimpleRelocation(SectionID, Offset, RelType, Value);
1759 processSimpleRelocation(SectionID, Offset, RelType, Value);
1765 processSimpleRelocation(SectionID, Offset, RelType, Value);
1781 Result =
sizeof(uint64_t);
1795 Result =
sizeof(uint64_t);
1805 uint64_t RuntimeDyldELF::allocateGOTEntries(
unsigned no) {
1806 if (GOTSectionID == 0) {
1813 CurrentGOTIndex += no;
1818 unsigned GOTRelType) {
1819 auto E = GOTOffsetMap.insert({
Value, 0});
1821 uint64_t GOTOffset = allocateGOTEntries(1);
1825 computeGOTOffsetRE(GOTOffset, Value.
Offset, GOTRelType);
1831 E.first->second = GOTOffset;
1834 return E.first->second;
1837 void RuntimeDyldELF::resolveGOTOffsetRelocation(
unsigned SectionID,
1846 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
1847 uint64_t SymbolOffset,
1855 if (!PendingRelocs.empty())
1856 return make_error<RuntimeDyldError>(
"Can't find matching LO16 reloc");
1859 if (GOTSectionID != 0) {
1863 GOTSectionID,
".got",
false);
1865 return make_error<RuntimeDyldError>(
"Unable to allocate memory for GOT!");
1872 memset(Addr, 0, TotalSize);
1878 if (
SI->relocation_begin() !=
SI->relocation_end()) {
1881 return make_error<RuntimeDyldError>(
1885 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
1886 assert (i != SectionMap.end());
1890 GOTSymbolOffsets.
clear();
1895 ObjSectionToIDMap::iterator i,
e;
1896 for (i = SectionMap.begin(), e = SectionMap.end(); i !=
e; ++i) {
1906 if (Name ==
".eh_frame") {
1907 UnregisteredEHFrameSections.
push_back(i->second);
1913 CurrentGOTIndex = 0;
1922 bool RuntimeDyldELF::relocationNeedsGot(
const RelocationRef &R)
const {
1925 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
1926 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
1929 return RelTy == ELF::R_X86_64_GOTPCREL ||
1930 RelTy == ELF::R_X86_64_GOTPCRELX ||
1931 RelTy == ELF::R_X86_64_GOT64 ||
1932 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
1936 bool RuntimeDyldELF::relocationNeedsStub(
const RelocationRef &R)
const {
1945 case ELF::R_X86_64_GOTPCREL:
1946 case ELF::R_X86_64_GOTPCRELX:
1947 case ELF::R_X86_64_REX_GOTPCRELX:
1948 case ELF::R_X86_64_GOTPC64:
1949 case ELF::R_X86_64_GOT64:
1950 case ELF::R_X86_64_GOTOFF64:
1951 case ELF::R_X86_64_PC32:
1952 case ELF::R_X86_64_PC64:
1953 case ELF::R_X86_64_64:
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
static void or32AArch64Imm(void *L, uint64_t Imm)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
void writeInt16BE(uint8_t *Addr, uint16_t Value)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringRef getFileName() const
void push_back(const T &Elt)
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
RuntimeDyld::MemoryManager & MemMgr
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
size_t getGOTEntrySize() override
iterator find(StringRef Key)
constexpr bool isInt< 8 >(int64_t x)
This class is the base class for all object file types.
static uint16_t applyPPChigher(uint64_t value)
void write32le(void *P, uint32_t V)
static uint16_t applyPPChighesta(uint64_t value)
uint8_t * getAddress() const
Error takeError()
Take ownership of the stored error.
static uint16_t applyPPChighera(uint64_t value)
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
unsigned SectionID
SectionID - the section this relocation points to.
bool IsTargetLittleEndian
std::string toString(Error E)
Write all error messages (if any) in E to a string.
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
DataRefImpl getRawDataRefImpl() const
This is a value type class that represents a single relocation in the list of relocations in the obje...
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
section_iterator_range sections() const
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
static uint16_t applyPPChi(uint64_t value)
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
static void or32le(void *P, int32_t V)
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
const Elf_Sym * getSymbol(DataRefImpl Sym) const
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
The instances of the Type class are immutable: once they are created, they are never changed...
void registerEHFrames() override
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static uint64_t getBits(uint64_t Val, int Start, int End)
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
static StringRef getArchTypePrefix(ArchType Kind)
getArchTypePrefix - Get the "prefix" canonical name for the Kind architecture.
Expected< int64_t > getAddend() const
~RuntimeDyldELF() override
Symbol resolution interface.
static uint16_t applyPPClo(uint64_t value)
virtual basic_symbol_iterator symbol_end() const =0
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object)
void writeInt32BE(uint8_t *Addr, uint32_t Value)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
bool isCompatibleFile(const object::ObjectFile &Obj) const override
DenseMap< SID, SID > SectionToGOTMap
static void write(bool isBE, void *P, T V)
virtual uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool IsReadOnly)=0
Allocate a memory block of (at least) the given size suitable for data.
DataRefImpl getRawDataRefImpl() const
void consumeError(Error Err)
Consume a Error without doing anything.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isLittleEndian() const
static uint16_t applyPPChighest(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 ErrorSuccess success()
Create a success value.
constexpr bool isInt< 32 >(int64_t x)
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
virtual section_iterator section_begin() const =0
Expected< SymbolRef::Type > getType() const
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
JITSymbolResolver & Resolver
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
LLVM_NODISCARD bool isa(const Y &Val)
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
Expected< StringRef > getName() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Target - Wrapper for Target specific information.
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...
uintptr_t getStubOffset() const
This is a value type class that represents a single symbol in the list of symbols in the object file...
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
uint64_t Offset
Offset - offset into the section.
virtual section_iterator section_end() const =0
std::map< SectionRef, unsigned > ObjSectionToIDMap
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift)
void writeInt64BE(uint8_t *Addr, uint64_t Value)
uint32_t read32le(const void *P)
StringRef getName() const
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
A raw_ostream that writes to an std::string.
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
void advanceStubOffset(unsigned StubSize)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size)=0
Register the EH frames with the runtime so that c++ exceptions work.
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
static uint16_t applyPPCha(uint64_t value)
virtual StringRef getFileFormatName() const =0
StringRef getData() const
This is a value type class that represents a single section in the list of sections in the object fil...