30 #define DEBUG_TYPE "dyld" 35 or32le(L, (Imm & 0xFFF) << 10);
38 template <
class T>
static void write(
bool isBE,
void *
P,
T V) {
39 isBE ? write<T, support::big>(
P, V) : write<T, support::little>(P, V);
44 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
45 uint64_t
Mask = (0x3 << 29) | (0x1FFFFC << 3);
51 static uint64_t
getBits(uint64_t Val,
int Start,
int End) {
52 uint64_t
Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
53 return (Val >> Start) &
Mask;
58 template <
class ELFT>
class DyldELFObject :
public ELFObjectFile<ELFT> {
68 typedef typename ELFT::uint addr_type;
76 void updateSectionAddress(
const SectionRef &Sec, uint64_t Addr);
78 void updateSymbolAddress(
const SymbolRef &SymRef, uint64_t Addr);
81 static bool classof(
const Binary *v) {
98 this->isDyldELFObject =
true;
101 template <
class ELFT>
105 if (
auto E = Obj.takeError())
107 std::unique_ptr<DyldELFObject<ELFT>>
Ret(
108 new DyldELFObject<ELFT>(std::move(*Obj)));
109 return std::move(Ret);
112 template <
class ELFT>
113 void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
117 const_cast<Elf_Shdr *
>(
reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
121 shdr->sh_addr =
static_cast<addr_type
>(Addr);
124 template <
class ELFT>
125 void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
128 Elf_Sym *sym =
const_cast<Elf_Sym *
>(
133 sym->st_value =
static_cast<addr_type
>(Addr);
136 class LoadedELFObjectInfo final
138 RuntimeDyld::LoadedObjectInfo> {
140 LoadedELFObjectInfo(
RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
144 getObjectForDebug(
const ObjectFile &Obj)
const override;
147 template <
typename ELFT>
150 const LoadedELFObjectInfo &L) {
151 typedef typename ELFT::Shdr Elf_Shdr;
152 typedef typename ELFT::uint addr_type;
155 DyldELFObject<ELFT>::create(Buffer);
159 std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
163 for (
const auto &Sec : Obj->sections()) {
166 if (SectionName !=
"") {
168 Elf_Shdr *shdr =
const_cast<Elf_Shdr *
>(
169 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
171 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*
SI)) {
174 shdr->sh_addr =
static_cast<addr_type
>(SecLoadAddr);
180 return std::move(Obj);
184 createELFDebugObject(
const ObjectFile &Obj,
const LoadedELFObjectInfo &L) {
187 std::unique_ptr<MemoryBuffer> Buffer =
194 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
197 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
200 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
203 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
212 LoadedELFObjectInfo::getObjectForDebug(
const ObjectFile &Obj)
const {
213 return createELFDebugObject(Obj, *
this);
222 :
RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
226 for (
int i = 0, e = UnregisteredEHFrameSections.
size(); i != e; ++i) {
227 SID EHFrameSID = UnregisteredEHFrameSections[i];
228 uint8_t *EHFrameAddr =
Sections[EHFrameSID].getAddress();
229 uint64_t EHFrameLoadAddr =
Sections[EHFrameSID].getLoadAddress();
230 size_t EHFrameSize =
Sections[EHFrameSID].getSize();
233 UnregisteredEHFrameSections.
clear();
236 std::unique_ptr<RuntimeDyldELF>
251 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
254 return llvm::make_unique<LoadedELFObjectInfo>(*
this, *ObjSectionToIDOrErr);
266 uint64_t SymOffset) {
271 case ELF::R_X86_64_NONE:
273 case ELF::R_X86_64_64: {
280 case ELF::R_X86_64_32:
281 case ELF::R_X86_64_32S: {
283 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
284 (Type == ELF::R_X86_64_32S &&
285 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
286 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
293 case ELF::R_X86_64_PC8: {
295 int64_t RealOffset = Value + Addend - FinalAddress;
297 int8_t TruncOffset = (RealOffset & 0xFF);
301 case ELF::R_X86_64_PC32: {
303 int64_t RealOffset = Value + Addend - FinalAddress;
305 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
310 case ELF::R_X86_64_PC64: {
312 int64_t RealOffset = Value + Addend - FinalAddress;
316 <<
format(
"%p\n", FinalAddress));
319 case ELF::R_X86_64_GOTOFF64: {
321 uint64_t GOTBase = 0;
322 for (
const auto &Section :
Sections) {
323 if (Section.
getName() ==
".got") {
328 assert(GOTBase != 0 &&
"missing GOT");
329 int64_t GOTOffset = Value - GOTBase + Addend;
336 void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
340 case ELF::R_386_32: {
347 case ELF::R_386_PLT32:
348 case ELF::R_386_PC32: {
351 uint32_t RealOffset = Value + Addend - FinalAddress;
364 void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
365 uint64_t Offset, uint64_t Value,
375 <<
" FinalAddress: 0x" <<
format(
"%llx", FinalAddress)
376 <<
" Value: 0x" <<
format(
"%llx", Value) <<
" Type: 0x" 377 <<
format(
"%x", Type) <<
" Addend: 0x" 378 <<
format(
"%llx", Addend) <<
"\n");
384 case ELF::R_AARCH64_ABS16: {
385 uint64_t Result = Value + Addend;
386 assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
387 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
390 case ELF::R_AARCH64_ABS32: {
391 uint64_t Result = Value + Addend;
392 assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
393 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
396 case ELF::R_AARCH64_ABS64:
397 write(isBE, TargetPtr, Value + Addend);
399 case ELF::R_AARCH64_PREL32: {
400 uint64_t Result = Value + Addend - FinalAddress;
401 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
402 static_cast<int64_t>(Result) <= UINT32_MAX);
403 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
406 case ELF::R_AARCH64_PREL64:
407 write(isBE, TargetPtr, Value + Addend - FinalAddress);
409 case ELF::R_AARCH64_CALL26:
410 case ELF::R_AARCH64_JUMP26: {
413 uint64_t BranchImm = Value + Addend - FinalAddress;
416 assert(isInt<28>(BranchImm));
417 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
420 case ELF::R_AARCH64_MOVW_UABS_G3:
421 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
423 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
424 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
426 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
427 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
429 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
430 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
432 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
435 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
438 assert(isInt<33>(Result) &&
"overflow check failed for relocation");
445 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
451 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
457 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
463 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
469 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
475 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
484 void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
495 <<
" FinalAddress: " <<
format(
"%p", FinalAddress)
496 <<
" Value: " <<
format(
"%x", Value)
497 <<
" Type: " <<
format(
"%x", Type)
498 <<
" Addend: " <<
format(
"%x", Addend) <<
"\n");
504 case ELF::R_ARM_NONE:
507 case ELF::R_ARM_PREL31:
510 ((Value - FinalAddress) & ~0x80000000);
512 case ELF::R_ARM_TARGET1:
513 case ELF::R_ARM_ABS32:
518 case ELF::R_ARM_MOVW_ABS_NC:
519 case ELF::R_ARM_MOVT_ABS:
520 if (Type == ELF::R_ARM_MOVW_ABS_NC)
521 Value = Value & 0xFFFF;
522 else if (Type == ELF::R_ARM_MOVT_ABS)
523 Value = (Value >> 16) & 0xFFFF;
526 (((Value >> 12) & 0xF) << 16);
529 case ELF::R_ARM_PC24:
530 case ELF::R_ARM_CALL:
531 case ELF::R_ARM_JUMP24:
532 int32_t RelValue =
static_cast<int32_t
>(Value - FinalAddress - 8);
533 RelValue = (RelValue & 0x03FFFFFC) >> 2;
541 void RuntimeDyldELF::setMipsABI(
const ObjectFile &Obj) {
549 if (
auto *
E = dyn_cast<ELFObjectFileBase>(&Obj)) {
550 unsigned AbiVariant =
E->getPlatformFlags();
571 for (
auto &Section: Obj.
sections()) {
573 if (
auto EC = Section.
getName(SectionName))
576 if (SectionName ==
".got" 577 || SectionName ==
".toc" 578 || SectionName ==
".tocbss" 579 || SectionName ==
".plt") {
580 if (
auto SectionIDOrErr =
584 return SectionIDOrErr.takeError();
610 if (
auto EC = RelSecI->getName(RelSectionName))
613 if (RelSectionName !=
".opd")
617 e = si->relocation_end();
621 uint64_t TypeFunc = i->getType();
622 if (TypeFunc != ELF::R_PPC64_ADDR64) {
627 uint64_t TargetSymbolOffset = i->getOffset();
630 if (
auto AddendOrErr = i->getAddend())
631 Addend = *AddendOrErr;
633 return AddendOrErr.takeError();
640 uint64_t TypeTOC = i->
getType();
641 if (TypeTOC != ELF::R_PPC64_TOC)
647 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
651 if (
auto TSIOrErr = TargetSymbol->
getSection())
654 return TSIOrErr.takeError();
657 bool IsCode = TSI->isText();
662 return SectionIDOrErr.takeError();
675 static inline uint16_t
applyPPClo(uint64_t value) {
return value & 0xffff; }
678 return (value >> 16) & 0xffff;
682 return ((value + 0x8000) >> 16) & 0xffff;
686 return (value >> 32) & 0xffff;
690 return ((value + 0x8000) >> 32) & 0xffff;
694 return (value >> 48) & 0xffff;
698 return ((value + 0x8000) >> 48) & 0xffff;
701 void RuntimeDyldELF::resolvePPC32Relocation(
const SectionEntry &Section,
702 uint64_t Offset, uint64_t Value,
709 case ELF::R_PPC_ADDR16_LO:
712 case ELF::R_PPC_ADDR16_HI:
715 case ELF::R_PPC_ADDR16_HA:
721 void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
722 uint64_t Offset, uint64_t Value,
729 case ELF::R_PPC64_ADDR16:
732 case ELF::R_PPC64_ADDR16_DS:
735 case ELF::R_PPC64_ADDR16_LO:
738 case ELF::R_PPC64_ADDR16_LO_DS:
741 case ELF::R_PPC64_ADDR16_HI:
742 case ELF::R_PPC64_ADDR16_HIGH:
745 case ELF::R_PPC64_ADDR16_HA:
746 case ELF::R_PPC64_ADDR16_HIGHA:
749 case ELF::R_PPC64_ADDR16_HIGHER:
752 case ELF::R_PPC64_ADDR16_HIGHERA:
755 case ELF::R_PPC64_ADDR16_HIGHEST:
758 case ELF::R_PPC64_ADDR16_HIGHESTA:
761 case ELF::R_PPC64_ADDR14: {
762 assert(((Value + Addend) & 3) == 0);
764 uint8_t aalk = *(LocalAddress + 3);
765 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
767 case ELF::R_PPC64_REL16_LO: {
769 uint64_t Delta = Value - FinalAddress + Addend;
772 case ELF::R_PPC64_REL16_HI: {
774 uint64_t Delta = Value - FinalAddress + Addend;
777 case ELF::R_PPC64_REL16_HA: {
779 uint64_t Delta = Value - FinalAddress + Addend;
782 case ELF::R_PPC64_ADDR32: {
783 int64_t Result =
static_cast<int64_t
>(Value + Addend);
784 if (SignExtend64<32>(Result) != Result)
788 case ELF::R_PPC64_REL24: {
790 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
791 if (SignExtend64<26>(delta) != delta)
795 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
797 case ELF::R_PPC64_REL32: {
799 int64_t delta =
static_cast<int64_t
>(Value - FinalAddress + Addend);
800 if (SignExtend64<32>(delta) != delta)
804 case ELF::R_PPC64_REL64: {
806 uint64_t Delta = Value - FinalAddress + Addend;
809 case ELF::R_PPC64_ADDR64:
815 void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
816 uint64_t Offset, uint64_t Value,
823 case ELF::R_390_PC16DBL:
824 case ELF::R_390_PLT16DBL: {
826 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
830 case ELF::R_390_PC32DBL:
831 case ELF::R_390_PLT32DBL: {
833 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
837 case ELF::R_390_PC16: {
839 assert(int16_t(Delta) == Delta &&
"R_390_PC16 overflow");
843 case ELF::R_390_PC32: {
845 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
849 case ELF::R_390_PC64: {
855 *LocalAddress = (uint8_t)(Value + Addend);
869 void RuntimeDyldELF::resolveBPFRelocation(
const SectionEntry &Section,
870 uint64_t Offset, uint64_t Value,
878 case ELF::R_BPF_NONE:
880 case ELF::R_BPF_64_64: {
886 case ELF::R_BPF_64_32: {
888 assert(Value <= UINT32_MAX);
924 void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
925 uint64_t Offset, uint64_t Value,
927 uint64_t SymOffset,
SID SectionID) {
930 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
933 resolveX86Relocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
938 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
944 resolveARMRelocation(Section, Offset, (
uint32_t)(Value & 0xffffffffL), Type,
948 resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
952 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
955 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
959 resolveBPFRelocation(Section, Offset, Value, Type, Addend);
966 void *RuntimeDyldELF::computePlaceholderAddress(
unsigned SectionID, uint64_t Offset)
const {
970 void RuntimeDyldELF::processSimpleRelocation(
unsigned SectionID, uint64_t Offset,
unsigned RelType,
RelocationValueRef Value) {
979 bool IsLocal)
const {
981 case ELF::R_MICROMIPS_GOT16:
983 return ELF::R_MICROMIPS_LO16;
985 case ELF::R_MICROMIPS_HI16:
986 return ELF::R_MICROMIPS_LO16;
987 case ELF::R_MIPS_GOT16:
989 return ELF::R_MIPS_LO16;
991 case ELF::R_MIPS_HI16:
992 return ELF::R_MIPS_LO16;
993 case ELF::R_MIPS_PCHI16:
994 return ELF::R_MIPS_PCLO16;
998 return ELF::R_MIPS_NONE;
1010 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1021 const auto &
SymInfo = Loc->second;
1028 uint64_t Offset = RelI->getOffset();
1029 uint64_t SourceAddress =
Sections[SectionID].getLoadAddressWithOffset(Offset);
1034 if (!isInt<28>(Address + Value.
Addend - SourceAddress))
1037 resolveRelocation(
Sections[SectionID], Offset, Address, RelI->getType(),
1043 void RuntimeDyldELF::resolveAArch64Branch(
unsigned SectionID,
1048 LLVM_DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
1051 uint64_t Offset = RelI->getOffset();
1052 unsigned RelType = RelI->getType();
1054 StubMap::const_iterator i = Stubs.find(Value);
1055 if (i != Stubs.end()) {
1056 resolveRelocation(Section, Offset,
1060 }
else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1068 ELF::R_AARCH64_MOVW_UABS_G3, Value.
Addend);
1071 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.
Addend);
1074 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.
Addend);
1077 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.
Addend);
1090 resolveRelocation(Section, Offset,
1102 const auto &Obj = cast<ELFObjectFileBase>(
O);
1103 uint64_t RelType = RelI->getType();
1106 Addend = *AddendOrErr;
1114 if (
auto TargetNameOrErr = Symbol->getName())
1115 TargetName = *TargetNameOrErr;
1117 return TargetNameOrErr.takeError();
1119 LLVM_DEBUG(
dbgs() <<
"\t\tRelType: " << RelType <<
" Addend: " << Addend
1120 <<
" TargetName: " << TargetName <<
"\n");
1130 if (!SymTypeOrErr) {
1137 SymType = *SymTypeOrErr;
1140 const auto &
SymInfo = gsi->second;
1146 case SymbolRef::ST_Debug: {
1150 auto SectionOrErr = Symbol->getSection();
1151 if (!SectionOrErr) {
1162 bool isCode = si->isText();
1167 return SectionIDOrErr.takeError();
1171 case SymbolRef::ST_Data:
1172 case SymbolRef::ST_Function:
1173 case SymbolRef::ST_Unknown: {
1191 uint64_t Offset = RelI->getOffset();
1193 LLVM_DEBUG(
dbgs() <<
"\t\tSectionID: " << SectionID <<
" Offset: " << Offset
1196 if (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26) {
1197 resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1198 }
else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1201 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1202 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1203 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1205 }
else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1206 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1207 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1208 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1210 processSimpleRelocation(SectionID, Offset, RelType, Value);
1213 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1214 RelType == ELF::R_ARM_JUMP24) {
1220 StubMap::const_iterator i = Stubs.find(Value);
1221 if (i != Stubs.end()) {
1234 ELF::R_ARM_ABS32, Value.
Addend);
1240 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1248 reinterpret_cast<uint32_t*
>(computePlaceholderAddress(SectionID, Offset));
1249 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1250 RelType == ELF::R_ARM_ABS32) {
1251 Value.
Addend += *Placeholder;
1252 }
else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1254 Value.
Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1256 processSimpleRelocation(SectionID, Offset, RelType, Value);
1259 uint8_t *Placeholder =
reinterpret_cast<uint8_t *
>(
1260 computePlaceholderAddress(SectionID, Offset));
1262 if (RelType == ELF::R_MIPS_26) {
1270 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1275 StubMap::const_iterator i = Stubs.find(Value);
1276 if (i != Stubs.end()) {
1292 ELF::R_MIPS_HI16, Value.
Addend);
1295 ELF::R_MIPS_LO16, Value.
Addend);
1309 }
else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1310 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1312 PendingRelocs.push_back(std::make_pair(Value, RE));
1313 }
else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1314 int64_t Addend = Value.
Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1315 for (
auto I = PendingRelocs.begin();
I != PendingRelocs.end();) {
1318 if (MatchingValue == Value &&
1319 RelType == getMatchingLoRelocation(Reloc.
RelType) &&
1326 I = PendingRelocs.erase(
I);
1336 if (RelType == ELF::R_MIPS_32)
1338 else if (RelType == ELF::R_MIPS_PC16)
1339 Value.
Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1340 else if (RelType == ELF::R_MIPS_PC19_S2)
1341 Value.
Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1342 else if (RelType == ELF::R_MIPS_PC21_S2)
1343 Value.
Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1344 else if (RelType == ELF::R_MIPS_PC26_S2)
1345 Value.
Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1346 processSimpleRelocation(SectionID, Offset, RelType, Value);
1351 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1352 || r_type == ELF::R_MIPS_GOT_DISP) {
1354 if (i != GOTSymbolOffsets.
end())
1358 GOTSymbolOffsets[TargetName] = RE.
SymOffset;
1364 }
else if (RelType == ELF::R_MIPS_26) {
1370 StubMap::const_iterator i = Stubs.find(Value);
1371 if (i != Stubs.end()) {
1388 ELF::R_MIPS_HI16, Value.
Addend);
1391 ELF::R_MIPS_LO16, Value.
Addend);
1404 ELF::R_MIPS_HIGHEST, Value.
Addend);
1407 ELF::R_MIPS_HIGHER, Value.
Addend);
1410 ELF::R_MIPS_HI16, Value.
Addend);
1413 ELF::R_MIPS_LO16, Value.
Addend);
1431 processSimpleRelocation(SectionID, Offset, RelType, Value);
1435 if (RelType == ELF::R_PPC64_REL24) {
1445 bool RangeOverflow =
false;
1446 bool IsExtern = Value.
SymbolName || SymType == SymbolRef::ST_Unknown;
1448 if (AbiVariant != 2) {
1452 if (
auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1453 return std::move(Err);
1458 uint8_t SymOther =
Symbol->getOther();
1462 uint8_t *RelocTarget =
1464 int64_t delta =
static_cast<int64_t
>(Target - RelocTarget);
1466 if (SignExtend64<26>(delta) != delta) {
1467 RangeOverflow =
true;
1468 }
else if ((AbiVariant != 2) ||
1469 (AbiVariant == 2 && Value.
SectionID == SectionID)) {
1474 if (IsExtern || (AbiVariant == 2 && Value.
SectionID != SectionID) ||
1478 StubMap::const_iterator i = Stubs.find(Value);
1479 if (i != Stubs.end()) {
1481 resolveRelocation(Section, Offset,
1482 reinterpret_cast<uint64_t>(
1494 ELF::R_PPC64_ADDR64, Value.
Addend);
1500 uint64_t StubRelocOffset = StubTargetAddr - Section.
getAddress();
1502 StubRelocOffset += 2;
1505 ELF::R_PPC64_ADDR16_HIGHEST, Value.
Addend);
1507 ELF::R_PPC64_ADDR16_HIGHER, Value.
Addend);
1509 ELF::R_PPC64_ADDR16_HI, Value.
Addend);
1511 ELF::R_PPC64_ADDR16_LO, Value.
Addend);
1525 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1531 if (IsExtern || (AbiVariant == 2 && Value.
SectionID != SectionID)) {
1533 if (AbiVariant == 2)
1539 }
else if (RelType == ELF::R_PPC64_TOC16 ||
1540 RelType == ELF::R_PPC64_TOC16_DS ||
1541 RelType == ELF::R_PPC64_TOC16_LO ||
1542 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1543 RelType == ELF::R_PPC64_TOC16_HI ||
1544 RelType == ELF::R_PPC64_TOC16_HA) {
1556 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16;
break;
1557 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS;
break;
1558 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO;
break;
1559 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS;
break;
1560 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI;
break;
1561 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA;
break;
1566 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1567 return std::move(Err);
1571 resolveRelocation(
Sections[SectionID], Offset, Value.
Addend, RelType, 0);
1577 if (RelType == ELF::R_PPC64_TOC) {
1578 RelType = ELF::R_PPC64_ADDR64;
1579 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1580 return std::move(Err);
1581 }
else if (TargetName ==
".TOC.") {
1582 if (
auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1583 return std::move(Err);
1595 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1605 LLVM_DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
1609 StubMap::const_iterator i = Stubs.find(Value);
1610 uintptr_t StubAddress;
1611 if (i != Stubs.end()) {
1618 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1619 uintptr_t StubAlignment = getStubAlignment();
1621 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1623 unsigned StubOffset = StubAddress - BaseAddress;
1625 Stubs[Value] = StubOffset;
1636 if (RelType == ELF::R_390_GOTENT)
1637 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1640 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1642 if (RelType == ELF::R_X86_64_PLT32) {
1663 StubMap::const_iterator i = Stubs.find(Value);
1664 uintptr_t StubAddress;
1665 if (i != Stubs.end()) {
1666 StubAddress = uintptr_t(Section.
getAddress()) + i->second;
1672 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
1673 uintptr_t StubAlignment = getStubAlignment();
1675 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
1677 unsigned StubOffset = StubAddress - BaseAddress;
1678 Stubs[Value] = StubOffset;
1685 uint64_t GOTOffset = allocateGOTEntries(1);
1688 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1689 ELF::R_X86_64_PC32);
1693 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1698 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1705 }
else if (RelType == ELF::R_X86_64_GOTPCREL ||
1706 RelType == ELF::R_X86_64_GOTPCRELX ||
1707 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1708 uint64_t GOTOffset = allocateGOTEntries(1);
1709 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1710 ELF::R_X86_64_PC32);
1714 computeGOTOffsetRE(GOTOffset, Value.
Offset, ELF::R_X86_64_64);
1719 }
else if (RelType == ELF::R_X86_64_GOT64) {
1721 uint64_t GOTOffset = allocateGOTEntries(1);
1722 resolveRelocation(
Sections[SectionID], Offset, GOTOffset,
1723 ELF::R_X86_64_64, 0);
1727 computeGOTOffsetRE(GOTOffset, Value.
Offset, ELF::R_X86_64_64);
1732 }
else if (RelType == ELF::R_X86_64_GOTPC64) {
1736 (void)allocateGOTEntries(0);
1737 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1738 }
else if (RelType == ELF::R_X86_64_GOTOFF64) {
1740 (void)allocateGOTEntries(0);
1741 processSimpleRelocation(SectionID, Offset, RelType, Value);
1742 }
else if (RelType == ELF::R_X86_64_PC32) {
1744 processSimpleRelocation(SectionID, Offset, RelType, Value);
1745 }
else if (RelType == ELF::R_X86_64_PC64) {
1747 processSimpleRelocation(SectionID, Offset, RelType, Value);
1749 processSimpleRelocation(SectionID, Offset, RelType, Value);
1755 processSimpleRelocation(SectionID, Offset, RelType, Value);
1771 Result =
sizeof(uint64_t);
1785 Result =
sizeof(uint64_t);
1795 uint64_t RuntimeDyldELF::allocateGOTEntries(
unsigned no) {
1796 if (GOTSectionID == 0) {
1803 CurrentGOTIndex += no;
1808 unsigned GOTRelType) {
1809 auto E = GOTOffsetMap.insert({Value, 0});
1811 uint64_t GOTOffset = allocateGOTEntries(1);
1815 computeGOTOffsetRE(GOTOffset, Value.
Offset, GOTRelType);
1821 E.first->second = GOTOffset;
1824 return E.first->second;
1827 void RuntimeDyldELF::resolveGOTOffsetRelocation(
unsigned SectionID,
1836 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
1837 uint64_t SymbolOffset,
1845 if (!PendingRelocs.empty())
1846 return make_error<RuntimeDyldError>(
"Can't find matching LO16 reloc");
1849 if (GOTSectionID != 0) {
1853 GOTSectionID,
".got",
false);
1855 return make_error<RuntimeDyldError>(
"Unable to allocate memory for GOT!");
1865 memset(Addr, 0, TotalSize);
1871 if (
SI->relocation_begin() !=
SI->relocation_end()) {
1873 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
1874 assert (i != SectionMap.end());
1878 GOTSymbolOffsets.
clear();
1883 ObjSectionToIDMap::iterator i, e;
1884 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1888 if (Name ==
".eh_frame") {
1889 UnregisteredEHFrameSections.
push_back(i->second);
1895 CurrentGOTIndex = 0;
1904 bool RuntimeDyldELF::relocationNeedsGot(
const RelocationRef &R)
const {
1907 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
1908 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
1911 return RelTy == ELF::R_X86_64_GOTPCREL ||
1912 RelTy == ELF::R_X86_64_GOTPCRELX ||
1913 RelTy == ELF::R_X86_64_GOT64 ||
1914 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
1918 bool RuntimeDyldELF::relocationNeedsStub(
const RelocationRef &R)
const {
1927 case ELF::R_X86_64_GOTPCREL:
1928 case ELF::R_X86_64_GOTPCRELX:
1929 case ELF::R_X86_64_REX_GOTPCRELX:
1930 case ELF::R_X86_64_GOTPC64:
1931 case ELF::R_X86_64_GOT64:
1932 case ELF::R_X86_64_GOTOFF64:
1933 case ELF::R_X86_64_PC32:
1934 case ELF::R_X86_64_PC64:
1935 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)
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.
RuntimeDyldCheckerImpl * Checker
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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)
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")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static uint64_t getBits(uint64_t Val, int Start, int End)
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
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.
std::error_code getName(StringRef &Result) const
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)
static void or32le(void *P, int32_t V)
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Target - Wrapper for Target specific information.
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
This is a value type class that represents a single symbol in the list of symbols in the object file...
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.
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
A raw_ostream that writes to an std::string.
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static void write32AArch64Addr(void *L, uint64_t Imm)
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...