14 #define DEBUG_TYPE "dyld"
29 using namespace llvm::object;
56 ELFT>::value_type addr_type;
61 void updateSectionAddress(
const SectionRef &Sec, uint64_t Addr);
62 void updateSymbolAddress(
const SymbolRef &Sym, uint64_t Addr);
65 static inline bool classof(
const Binary *v) {
70 static inline bool classof(
79 DyldELFObject<ELFT> *DyldObj;
84 DyldELFObject<ELFT> *Obj)
89 virtual ~ELFObjectImage() {
91 deregisterWithDebugger();
96 virtual void updateSectionAddress(
const SectionRef &Sec, uint64_t Addr)
98 DyldObj->updateSectionAddress(Sec, Addr);
101 virtual void updateSymbolAddress(
const SymbolRef &Sym, uint64_t Addr)
103 DyldObj->updateSymbolAddress(Sym, Addr);
106 virtual void registerWithDebugger()
111 virtual void deregisterWithDebugger()
123 this->isDyldELFObject =
true;
127 void DyldELFObject<ELFT>::updateSectionAddress(
const SectionRef &Sec,
130 Elf_Shdr *shdr =
const_cast<Elf_Shdr*
>(
131 reinterpret_cast<const Elf_Shdr *
>(ShdrRef.
p));
135 shdr->sh_addr =
static_cast<addr_type
>(Addr);
139 void DyldELFObject<ELFT>::updateSymbolAddress(
const SymbolRef &SymRef,
142 Elf_Sym *sym =
const_cast<Elf_Sym*
>(
147 sym->st_value =
static_cast<addr_type
>(Addr);
157 for (
int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
158 SID EHFrameSID = UnregisteredEHFrameSections[i];
159 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
160 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
161 size_t EHFrameSize = Sections[EHFrameSID].Size;
162 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
163 RegisteredEHFrameSections.push_back(EHFrameSID);
165 UnregisteredEHFrameSections.clear();
171 for (
int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
172 SID EHFrameSID = RegisteredEHFrameSections[i];
173 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
174 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
175 size_t EHFrameSize = Sections[EHFrameSID].Size;
176 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
178 RegisteredEHFrameSections.clear();
184 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
190 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
191 new DyldELFObject<ELFType<support::little, 4, false> >(
193 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
196 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
197 new DyldELFObject<ELFType<support::big, 4, false> >(
199 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
202 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
203 new DyldELFObject<ELFType<support::big, 8, true> >(
205 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
208 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
209 new DyldELFObject<ELFType<support::little, 8, true> >(
211 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
225 uint64_t SymOffset) {
231 uint64_t *
Target =
reinterpret_cast<uint64_t*
>(Section.
Address + Offset);
232 *Target = Value + Addend;
234 <<
" at " <<
format(
"%p\n",Target));
242 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
243 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
244 uint32_t *Target =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
245 *Target = TruncatedAddr;
247 <<
" at " <<
format(
"%p\n",Target));
253 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
254 uint32_t *Target =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
255 uint64_t FinalAddress = Section.
LoadAddress + Offset;
259 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
260 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
261 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
262 *Target = TruncOffset;
268 uint32_t *Placeholder =
reinterpret_cast<uint32_t*
>(Section.
ObjAddress
270 uint32_t *Target =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
271 uint64_t FinalAddress = Section.
LoadAddress + Offset;
272 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
273 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
274 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
275 *Target = TruncOffset;
281 uint64_t *Placeholder =
reinterpret_cast<uint64_t*
>(Section.
ObjAddress
283 uint64_t *Target =
reinterpret_cast<uint64_t*
>(Section.
Address + Offset);
284 uint64_t FinalAddress = Section.
LoadAddress + Offset;
285 *Target = *Placeholder + Value + Addend - FinalAddress;
291 void RuntimeDyldELF::resolveX86Relocation(
const SectionEntry &Section,
300 uint32_t *Placeholder =
reinterpret_cast<uint32_t*
>(Section.
ObjAddress
302 uint32_t *Target =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
303 *Target = *Placeholder + Value + Addend;
309 uint32_t *Placeholder =
reinterpret_cast<uint32_t*
>(Section.
ObjAddress
311 uint32_t *Target =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
312 uint32_t FinalAddress = ((Section.
LoadAddress + Offset) & 0xFFFFFFFF);
313 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
314 *Target = RealOffset;
325 void RuntimeDyldELF::resolveAArch64Relocation(
const SectionEntry &Section,
330 uint32_t *TargetPtr =
reinterpret_cast<uint32_t*
>(Section.
Address + Offset);
331 uint64_t FinalAddress = Section.
LoadAddress + Offset;
333 DEBUG(
dbgs() <<
"resolveAArch64Relocation, LocalAddress: 0x"
335 <<
" FinalAddress: 0x" <<
format(
"%llx",FinalAddress)
336 <<
" Value: 0x" <<
format(
"%llx",Value)
337 <<
" Type: 0x" <<
format(
"%x",Type)
338 <<
" Addend: 0x" <<
format(
"%llx",Addend)
346 uint64_t *TargetPtr =
reinterpret_cast<uint64_t*
>(Section.
Address + Offset);
347 *TargetPtr = Value + Addend;
351 uint64_t Result = Value + Addend - FinalAddress;
352 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
353 static_cast<int64_t>(Result) <= UINT32_MAX);
354 *TargetPtr =
static_cast<uint32_t
>(Result & 0xffffffffU);
361 uint64_t BranchImm = Value + Addend - FinalAddress;
364 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
365 static_cast<int64_t>(BranchImm) < (1LL << 27));
369 *TargetPtr &= 0xfc000000U;
371 *TargetPtr |=
static_cast<uint32_t
>(BranchImm & 0xffffffcU) >> 2;
375 uint64_t Result = Value + Addend;
379 *TargetPtr &= 0xffe0001fU;
381 *TargetPtr |= Result >> (48 - 5);
383 assert((*TargetPtr >> 21 & 0x3) == 3 &&
"invalid shift for relocation");
387 uint64_t Result = Value + Addend;
391 *TargetPtr &= 0xffe0001fU;
393 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
395 assert((*TargetPtr >> 21 & 0x3) == 2 &&
"invalid shift for relocation");
399 uint64_t Result = Value + Addend;
403 *TargetPtr &= 0xffe0001fU;
405 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
407 assert((*TargetPtr >> 21 & 0x3) == 1 &&
"invalid shift for relocation");
411 uint64_t Result = Value + Addend;
415 *TargetPtr &= 0xffe0001fU;
417 *TargetPtr |= ((Result & 0xffffU) << 5);
419 assert((*TargetPtr >> 21 & 0x3) == 0 &&
"invalid shift for relocation");
425 void RuntimeDyldELF::resolveARMRelocation(
const SectionEntry &Section,
431 uint32_t *Placeholder =
reinterpret_cast<uint32_t*
>(Section.
ObjAddress +
433 uint32_t* TargetPtr = (uint32_t*)(Section.
Address + Offset);
434 uint32_t FinalAddress = ((Section.
LoadAddress + Offset) & 0xFFFFFFFF);
437 DEBUG(
dbgs() <<
"resolveARMRelocation, LocalAddress: "
439 <<
" FinalAddress: " <<
format(
"%p",FinalAddress)
440 <<
" Value: " <<
format(
"%x",Value)
441 <<
" Type: " <<
format(
"%x",Type)
442 <<
" Addend: " <<
format(
"%x",Addend)
453 *TargetPtr = *Placeholder + Value;
461 assert((*Placeholder & 0x000F0FFF) == 0);
462 Value = Value & 0xFFFF;
463 *TargetPtr = *Placeholder | (Value & 0xFFF);
464 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
471 assert((*Placeholder & 0x000F0FFF) == 0);
473 Value = (Value >> 16) & 0xFFFF;
474 *TargetPtr = *Placeholder | (Value & 0xFFF);
475 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
481 int32_t RelValue =
static_cast<int32_t
>(Value - FinalAddress - 8);
482 RelValue = (RelValue & 0x03FFFFFC) >> 2;
483 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
484 *TargetPtr &= 0xFF000000;
485 *TargetPtr |= RelValue;
498 void RuntimeDyldELF::resolveMIPSRelocation(
const SectionEntry &Section,
503 uint32_t *Placeholder =
reinterpret_cast<uint32_t*
>(Section.
ObjAddress +
505 uint32_t* TargetPtr = (uint32_t*)(Section.
Address + Offset);
508 DEBUG(
dbgs() <<
"resolveMipselocation, LocalAddress: "
512 <<
" Value: " <<
format(
"%x",Value)
513 <<
" Type: " <<
format(
"%x",Type)
514 <<
" Addend: " <<
format(
"%x",Addend)
522 *TargetPtr = Value + (*Placeholder);
525 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
529 Value += ((*Placeholder) & 0x0000ffff) << 16;
530 *TargetPtr = ((*Placeholder) & 0xffff0000) |
531 (((Value + 0x8000) >> 16) & 0xffff);
534 Value += ((*Placeholder) & 0x0000ffff);
535 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
542 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
543 (((Value + 0x8000) >> 16) & 0xffff);
546 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
552 uint64_t RuntimeDyldELF::findPPC64TOC()
const {
555 SectionList::const_iterator it = Sections.begin();
556 SectionList::const_iterator ite = Sections.end();
557 for (; it != ite; ++it) {
558 if (it->Name ==
".got" ||
559 it->Name ==
".toc" ||
560 it->Name ==
".tocbss" ||
571 it = Sections.begin();
576 return it->LoadAddress + 0x8000;
581 void RuntimeDyldELF::findOPDEntrySection(
ObjectImage &Obj,
582 ObjSectionToIDMap &LocalSections,
595 check(RelSecI->getName(RelSectionName));
596 if (RelSectionName !=
".opd")
600 e = si->end_relocations(); i != e;) {
606 check(i->getType(TypeFunc));
612 uint64_t TargetSymbolOffset;
614 check(i->getOffset(TargetSymbolOffset));
618 i = i.increment(err);
625 check(i->getType(TypeTOC));
632 if (Rel.
Addend != (int64_t)TargetSymbolOffset)
636 check(TargetSymbol->getSection(tsi));
637 Rel.
SectionID = findOrEmitSection(Obj, (*tsi),
true, LocalSections);
652 return value & 0xffff;
658 return (value >> 16) & 0xffff;
664 return (value >> 32) & 0xffff;
670 return (value >> 48) & 0xffff;
673 void RuntimeDyldELF::resolvePPC64Relocation(
const SectionEntry &Section,
678 uint8_t* LocalAddress = Section.
Address + Offset;
684 writeInt16BE(LocalAddress,
applyPPClo (Value + Addend));
687 writeInt16BE(LocalAddress,
applyPPChi (Value + Addend));
696 assert(((Value + Addend) & 3) == 0);
698 uint8_t aalk = *(LocalAddress+3);
699 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
702 int32_t Result =
static_cast<int32_t
>(Value + Addend);
703 if (SignExtend32<32>(Result) != Result)
705 writeInt32BE(LocalAddress, Result);
708 uint64_t FinalAddress = (Section.
LoadAddress + Offset);
709 int32_t delta =
static_cast<int32_t
>(Value - FinalAddress + Addend);
710 if (SignExtend32<24>(delta) != delta)
713 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
716 uint64_t FinalAddress = (Section.
LoadAddress + Offset);
717 int32_t delta =
static_cast<int32_t
>(Value - FinalAddress + Addend);
718 if (SignExtend32<32>(delta) != delta)
720 writeInt32BE(LocalAddress, delta);
723 uint64_t FinalAddress = (Section.
LoadAddress + Offset);
724 uint64_t Delta = Value - FinalAddress + Addend;
725 writeInt64BE(LocalAddress, Delta);
728 writeInt64BE(LocalAddress, Value + Addend);
731 writeInt64BE(LocalAddress, findPPC64TOC());
734 uint64_t TOCStart = findPPC64TOC();
735 Value =
applyPPClo((Value + Addend) - TOCStart);
736 writeInt16BE(LocalAddress,
applyPPClo(Value));
739 uint64_t TOCStart = findPPC64TOC();
740 Value = ((Value + Addend) - TOCStart);
741 writeInt16BE(LocalAddress,
applyPPClo(Value));
746 void RuntimeDyldELF::resolveSystemZRelocation(
const SectionEntry &Section,
751 uint8_t *LocalAddress = Section.
Address + Offset;
758 int64_t Delta = (Value + Addend) - (Section.
LoadAddress + Offset);
759 assert(int16_t(Delta / 2) * 2 == Delta &&
"R_390_PC16DBL overflow");
760 writeInt16BE(LocalAddress, Delta / 2);
765 int64_t Delta = (Value + Addend) - (Section.
LoadAddress + Offset);
766 assert(int32_t(Delta / 2) * 2 == Delta &&
"R_390_PC32DBL overflow");
767 writeInt32BE(LocalAddress, Delta / 2);
771 int64_t Delta = (Value + Addend) - (Section.
LoadAddress + Offset);
772 assert(int32_t(Delta) == Delta &&
"R_390_PC32 overflow");
773 writeInt32BE(LocalAddress, Delta);
777 writeInt64BE(LocalAddress, Value + Addend);
809 void RuntimeDyldELF::resolveRelocation(
const SectionEntry &Section,
814 uint64_t SymOffset) {
817 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
820 resolveX86Relocation(Section, Offset,
821 (uint32_t)(Value & 0xffffffffL), Type,
822 (uint32_t)(Addend & 0xffffffffL));
825 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
829 resolveARMRelocation(Section, Offset,
830 (uint32_t)(Value & 0xffffffffL), Type,
831 (uint32_t)(Addend & 0xffffffffL));
835 resolveMIPSRelocation(Section, Offset,
836 (uint32_t)(Value & 0xffffffffL), Type,
837 (uint32_t)(Addend & 0xffffffffL));
841 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
844 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
865 Symbol->getName(TargetName);
866 DEBUG(
dbgs() <<
"\t\tRelType: " << RelType
867 <<
" Addend: " << Addend
868 <<
" TargetName: " << TargetName
875 lsi = Symbols.
find(TargetName.
data());
876 Symbol->getType(SymType);
878 if (lsi != Symbols.
end()) {
886 gsi = GlobalSymbolTable.find(TargetName.
data());
887 if (gsi != GlobalSymbolTable.end()) {
898 Symbol->getSection(si);
901 DEBUG(
dbgs() <<
"\t\tThis is section symbol\n");
934 DEBUG(
dbgs() <<
"\t\tSectionID: " << SectionID
935 <<
" Offset: " << Offset
941 DEBUG(
dbgs() <<
"\t\tThis is an AArch64 branch relocation.");
945 StubMap::const_iterator i = Stubs.find(Value);
946 if (i != Stubs.end()) {
947 resolveRelocation(Section, Offset,
948 (uint64_t)Section.
Address + i->second, RelType, 0);
949 DEBUG(
dbgs() <<
" Stub function found\n");
952 DEBUG(
dbgs() <<
" Create a new stub function\n");
954 uint8_t *StubTargetAddr = createStubFunction(Section.
Address +
958 StubTargetAddr - Section.
Address,
961 StubTargetAddr - Section.
Address + 4,
964 StubTargetAddr - Section.
Address + 8,
967 StubTargetAddr - Section.
Address + 12,
971 addRelocationForSymbol(REmovz_g3, Value.
SymbolName);
972 addRelocationForSymbol(REmovk_g2, Value.
SymbolName);
973 addRelocationForSymbol(REmovk_g1, Value.
SymbolName);
974 addRelocationForSymbol(REmovk_g0, Value.
SymbolName);
976 addRelocationForSection(REmovz_g3, Value.
SectionID);
977 addRelocationForSection(REmovk_g2, Value.
SectionID);
978 addRelocationForSection(REmovk_g1, Value.
SectionID);
979 addRelocationForSection(REmovk_g0, Value.
SectionID);
981 resolveRelocation(Section, Offset,
991 DEBUG(
dbgs() <<
"\t\tThis is an ARM branch relocation.");
995 StubMap::const_iterator i = Stubs.find(Value);
996 if (i != Stubs.end()) {
997 resolveRelocation(Section, Offset,
998 (uint64_t)Section.
Address + i->second, RelType, 0);
999 DEBUG(
dbgs() <<
" Stub function found\n");
1002 DEBUG(
dbgs() <<
" Create a new stub function\n");
1004 uint8_t *StubTargetAddr = createStubFunction(Section.
Address +
1009 addRelocationForSymbol(RE, Value.
SymbolName);
1011 addRelocationForSection(RE, Value.
SectionID);
1013 resolveRelocation(Section, Offset,
1021 DEBUG(
dbgs() <<
"\t\tThis is a Mips branch relocation.");
1023 uint8_t *Target = Section.
Address + Offset;
1024 uint32_t *TargetAddress = (uint32_t *)Target;
1027 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1032 StubMap::const_iterator i = Stubs.find(Value);
1033 if (i != Stubs.end()) {
1035 addRelocationForSection(RE, SectionID);
1036 DEBUG(
dbgs() <<
" Stub function found\n");
1039 DEBUG(
dbgs() <<
" Create a new stub function\n");
1041 uint8_t *StubTargetAddr = createStubFunction(Section.
Address +
1046 StubTargetAddr - Section.
Address,
1049 StubTargetAddr - Section.
Address + 4,
1053 addRelocationForSymbol(REHi, Value.
SymbolName);
1054 addRelocationForSymbol(RELo, Value.
SymbolName);
1056 addRelocationForSection(REHi, Value.
SectionID);
1057 addRelocationForSection(RELo, Value.
SectionID);
1061 addRelocationForSection(RE, SectionID);
1070 uint8_t *Target = Section.
Address + Offset;
1071 bool RangeOverflow =
false;
1075 findOPDEntrySection(Obj, ObjSectionToID, Value);
1076 uint8_t *RelocTarget = Sections[Value.
SectionID].Address + Value.
Addend;
1077 int32_t delta =
static_cast<int32_t
>(Target - RelocTarget);
1079 if (SignExtend32<24>(delta) == delta) {
1082 addRelocationForSymbol(RE, Value.
SymbolName);
1084 addRelocationForSection(RE, Value.
SectionID);
1086 RangeOverflow =
true;
1092 StubMap::const_iterator i = Stubs.find(Value);
1093 if (i != Stubs.end()) {
1095 resolveRelocation(Section, Offset,
1096 (uint64_t)Section.
Address + i->second, RelType, 0);
1097 DEBUG(
dbgs() <<
" Stub function found\n");
1100 DEBUG(
dbgs() <<
" Create a new stub function\n");
1102 uint8_t *StubTargetAddr = createStubFunction(Section.
Address +
1110 StubTargetAddr - Section.
Address + 2,
1113 StubTargetAddr - Section.
Address + 6,
1116 StubTargetAddr - Section.
Address + 14,
1119 StubTargetAddr - Section.
Address + 18,
1123 addRelocationForSymbol(REhst, Value.
SymbolName);
1124 addRelocationForSymbol(REhr, Value.
SymbolName);
1125 addRelocationForSymbol(REh, Value.
SymbolName);
1126 addRelocationForSymbol(REl, Value.
SymbolName);
1128 addRelocationForSection(REhst, Value.
SectionID);
1129 addRelocationForSection(REhr, Value.
SectionID);
1130 addRelocationForSection(REh, Value.
SectionID);
1131 addRelocationForSection(REl, Value.
SectionID);
1134 resolveRelocation(Section, Offset,
1139 writeInt32BE(Target+4, 0xE8410028);
1151 addRelocationForSymbol(RE, Value.
SymbolName);
1153 addRelocationForSection(RE, Value.
SectionID);
1167 DEBUG(
dbgs() <<
"\t\tThis is a SystemZ indirect relocation.");
1171 StubMap::const_iterator i = Stubs.find(Value);
1172 uintptr_t StubAddress;
1173 if (i != Stubs.end()) {
1174 StubAddress = uintptr_t(Section.
Address) + i->second;
1175 DEBUG(
dbgs() <<
" Stub function found\n");
1178 DEBUG(
dbgs() <<
" Create a new stub function\n");
1180 uintptr_t BaseAddress = uintptr_t(Section.
Address);
1181 uintptr_t StubAlignment = getStubAlignment();
1182 StubAddress = (BaseAddress + Section.
StubOffset +
1183 StubAlignment - 1) & -StubAlignment;
1184 unsigned StubOffset = StubAddress - BaseAddress;
1186 Stubs[Value] = StubOffset;
1187 createStubFunction((uint8_t *)StubAddress);
1191 addRelocationForSymbol(RE, Value.
SymbolName);
1193 addRelocationForSection(RE, Value.
SectionID);
1194 Section.
StubOffset = StubOffset + getMaxStubSize();
1198 resolveRelocation(Section, Offset, StubAddress + 8,
1201 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1221 StubMap::const_iterator i = Stubs.find(Value);
1222 uintptr_t StubAddress;
1223 if (i != Stubs.end()) {
1224 StubAddress = uintptr_t(Section.
Address) + i->second;
1225 DEBUG(
dbgs() <<
" Stub function found\n");
1228 DEBUG(
dbgs() <<
" Create a new stub function\n");
1230 uintptr_t BaseAddress = uintptr_t(Section.
Address);
1231 uintptr_t StubAlignment = getStubAlignment();
1232 StubAddress = (BaseAddress + Section.
StubOffset +
1233 StubAlignment - 1) & -StubAlignment;
1234 unsigned StubOffset = StubAddress - BaseAddress;
1235 Stubs[Value] = StubOffset;
1236 createStubFunction((uint8_t *)StubAddress);
1239 GOTEntries.push_back(Value);
1244 addRelocationForSymbol(RE, Value.
SymbolName);
1247 Section.
StubOffset = StubOffset + getMaxStubSize();
1251 resolveRelocation(Section, Offset, StubAddress,
1256 addRelocationForSection(RE, Value.
SectionID);
1260 GOTEntries.push_back(Value);
1264 addRelocationForSymbol(RE, Value.
SymbolName);
1266 addRelocationForSection(RE, Value.
SectionID);
1270 void RuntimeDyldELF::updateGOTEntries(
StringRef Name, uint64_t Addr) {
1275 for (it = GOTs.
begin(); it !=
end; ++it) {
1276 GOTRelocations &GOTEntries = it->second;
1277 for (
int i = 0, e = GOTEntries.size(); i != e; ++i) {
1278 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1279 GOTEntries[i].Offset = Addr;
1285 size_t RuntimeDyldELF::getGOTEntrySize() {
1295 Result =
sizeof(uint64_t);
1302 Result =
sizeof(uint32_t);
1309 uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress,
1312 const size_t GOTEntrySize = getGOTEntrySize();
1318 for (it = GOTs.
begin(); it !=
end; ++it) {
1319 SID GOTSectionID = it->first;
1320 const GOTRelocations &GOTEntries = it->second;
1323 uint64_t SymbolOffset = 0;
1324 for (
int i = 0, e = GOTEntries.size(); i != e; ++i) {
1325 if (GOTEntries[i].SymbolName == 0) {
1326 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1327 GOTEntries[i].Offset == Offset) {
1329 SymbolOffset = GOTEntries[i].Offset;
1335 if (GOTEntries[i].Offset == LoadAddress) {
1343 if (GOTIndex != -1) {
1344 if (GOTEntrySize ==
sizeof(uint64_t)) {
1345 uint64_t *LocalGOTAddr = (uint64_t*)getSectionAddress(GOTSectionID);
1347 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1349 uint32_t *LocalGOTAddr = (uint32_t*)getSectionAddress(GOTSectionID);
1351 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1355 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1359 assert(GOTIndex != -1 &&
"Unable to find requested GOT entry.");
1367 size_t numGOTEntries = GOTEntries.size();
1368 if (numGOTEntries != 0) {
1370 unsigned SectionID = Sections.size();
1371 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1372 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1373 SectionID,
".got",
false);
1377 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1378 Sections.push_back(
SectionEntry(
".got", Addr, TotalSize, 0));
1381 memset(Addr, 0, TotalSize);
1389 ObjSectionToIDMap::iterator i, e;
1390 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1394 if (Name ==
".eh_frame") {
1395 UnregisteredEHFrameSections.push_back(i->second);
static bool Check(DecodeStatus &Out, DecodeStatus In)
const_iterator end(StringRef path)
Get end iterator over path.
DataRefImpl getRawDataRefImpl() const
virtual void finalizeLoad(ObjSectionToIDMap &SectionMap)
size_t getBufferSize() const
virtual bool deregisterObject(const ObjectBuffer &Object)=0
iterator find(StringRef Key)
static uint16_t applyPPChigher(uint64_t value)
uint8_t * Address
Address - address in the linker's memory where the section resides.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
DataRefImpl getRawDataRefImpl() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
unsigned SectionID
SectionID - the section this relocation points to.
virtual ~RuntimeDyldELF()
const char * getBufferStart() const
#define llvm_unreachable(msg)
std::map< RelocationValueRef, uintptr_t > StubMap
format_object1< T > format(const char *Fmt, const T &Val)
virtual void deregisterEHFrames()
const char * data() const
virtual object::symbol_iterator end_symbols() const =0
virtual void registerObject(const ObjectBuffer &Object)=0
error_code getName(StringRef &Result) const
MemoryBuffer * getMemBuffer() const
static uint16_t applyPPChi(uint64_t value)
int memcmp(const void *s1, const void *s2, size_t n);
friend const_iterator end(StringRef path)
Get end iterator over path.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
static error_code getELFRelocationAddend(const RelocationRef R, int64_t &Addend)
static JITRegistrar & getGDBRegistrar()
Returns a reference to a GDB JIT registrar singleton.
symbol_iterator getSymbol() const
static uint16_t applyPPClo(uint64_t value)
virtual void processRelocationRef(unsigned SectionID, RelocationRef RelI, ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, StubMap &Stubs)
Parses the object file relocation and stores it to Relocations or SymbolRelocations (this depends on ...
static uint16_t applyPPChighest(uint64_t value)
content_iterator & increment(error_code &err)
static const char ElfMagic[]
uint32_t RelType
RelType - relocation type.
virtual ObjectImage * createObjectImage(ObjectBuffer *InputBuffer)
const Elf_Sym * getSymbol(DataRefImpl Symb) const
size_t strlen(const char *s);
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
std::string message() const
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
virtual void registerEHFrames()
LLVM Value Representation.
error_code getType(uint64_t &Result) const
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
error_code getOffset(uint64_t &Result) const
virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const
virtual object::section_iterator end_sections() const =0
bool empty() const
empty - Check if the string is empty.
virtual object::section_iterator begin_sections() const =0