RuntimeDyldELF.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Implementation of ELF support for the MC-JIT runtime dynamic linker.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "RuntimeDyldELF.h"
  14. #include "RuntimeDyldCheckerImpl.h"
  15. #include "llvm/ADT/IntervalMap.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/MC/MCStreamer.h"
  20. #include "llvm/Object/ELFObjectFile.h"
  21. #include "llvm/Object/ObjectFile.h"
  22. #include "llvm/Support/ELF.h"
  23. #include "llvm/Support/Endian.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/TargetRegistry.h"
  26. using namespace llvm;
  27. using namespace llvm::object;
  28. #define DEBUG_TYPE "dyld"
  29. static inline std::error_code check(std::error_code Err) {
  30. if (Err) {
  31. report_fatal_error(Err.message());
  32. }
  33. return Err;
  34. }
  35. namespace {
  36. template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
  37. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  38. typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
  39. typedef Elf_Sym_Impl<ELFT> Elf_Sym;
  40. typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
  41. typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
  42. typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
  43. typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
  44. public:
  45. DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec);
  46. void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
  47. void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
  48. // Methods for type inquiry through isa, cast and dyn_cast
  49. static inline bool classof(const Binary *v) {
  50. return (isa<ELFObjectFile<ELFT>>(v) &&
  51. classof(cast<ELFObjectFile<ELFT>>(v)));
  52. }
  53. static inline bool classof(const ELFObjectFile<ELFT> *v) {
  54. return v->isDyldType();
  55. }
  56. };
  57. // The MemoryBuffer passed into this constructor is just a wrapper around the
  58. // actual memory. Ultimately, the Binary parent class will take ownership of
  59. // this MemoryBuffer object but not the underlying memory.
  60. template <class ELFT>
  61. DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
  62. : ELFObjectFile<ELFT>(Wrapper, EC) {
  63. this->isDyldELFObject = true;
  64. }
  65. template <class ELFT>
  66. void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
  67. uint64_t Addr) {
  68. DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
  69. Elf_Shdr *shdr =
  70. const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
  71. // This assumes the address passed in matches the target address bitness
  72. // The template-based type cast handles everything else.
  73. shdr->sh_addr = static_cast<addr_type>(Addr);
  74. }
  75. template <class ELFT>
  76. void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
  77. uint64_t Addr) {
  78. Elf_Sym *sym = const_cast<Elf_Sym *>(
  79. ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
  80. // This assumes the address passed in matches the target address bitness
  81. // The template-based type cast handles everything else.
  82. sym->st_value = static_cast<addr_type>(Addr);
  83. }
  84. class LoadedELFObjectInfo
  85. : public RuntimeDyld::LoadedObjectInfoHelper<LoadedELFObjectInfo> {
  86. public:
  87. LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
  88. unsigned EndIdx)
  89. : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {}
  90. OwningBinary<ObjectFile>
  91. getObjectForDebug(const ObjectFile &Obj) const override;
  92. };
  93. template <typename ELFT>
  94. std::unique_ptr<DyldELFObject<ELFT>>
  95. createRTDyldELFObject(MemoryBufferRef Buffer,
  96. const LoadedELFObjectInfo &L,
  97. std::error_code &ec) {
  98. typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
  99. typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
  100. std::unique_ptr<DyldELFObject<ELFT>> Obj =
  101. llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec);
  102. // Iterate over all sections in the object.
  103. for (const auto &Sec : Obj->sections()) {
  104. StringRef SectionName;
  105. Sec.getName(SectionName);
  106. if (SectionName != "") {
  107. DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
  108. Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
  109. reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
  110. if (uint64_t SecLoadAddr = L.getSectionLoadAddress(SectionName)) {
  111. // This assumes that the address passed in matches the target address
  112. // bitness. The template-based type cast handles everything else.
  113. shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
  114. }
  115. }
  116. }
  117. return Obj;
  118. }
  119. OwningBinary<ObjectFile> createELFDebugObject(const ObjectFile &Obj,
  120. const LoadedELFObjectInfo &L) {
  121. assert(Obj.isELF() && "Not an ELF object file.");
  122. std::unique_ptr<MemoryBuffer> Buffer =
  123. MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
  124. std::error_code ec;
  125. std::unique_ptr<ObjectFile> DebugObj;
  126. if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) {
  127. typedef ELFType<support::little, false> ELF32LE;
  128. DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), L, ec);
  129. } else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) {
  130. typedef ELFType<support::big, false> ELF32BE;
  131. DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), L, ec);
  132. } else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) {
  133. typedef ELFType<support::big, true> ELF64BE;
  134. DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), L, ec);
  135. } else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) {
  136. typedef ELFType<support::little, true> ELF64LE;
  137. DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), L, ec);
  138. } else
  139. llvm_unreachable("Unexpected ELF format");
  140. assert(!ec && "Could not construct copy ELF object file");
  141. return OwningBinary<ObjectFile>(std::move(DebugObj), std::move(Buffer));
  142. }
  143. OwningBinary<ObjectFile>
  144. LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
  145. return createELFDebugObject(Obj, *this);
  146. }
  147. } // namespace
  148. namespace llvm {
  149. RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
  150. RuntimeDyld::SymbolResolver &Resolver)
  151. : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
  152. RuntimeDyldELF::~RuntimeDyldELF() {}
  153. void RuntimeDyldELF::registerEHFrames() {
  154. for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
  155. SID EHFrameSID = UnregisteredEHFrameSections[i];
  156. uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
  157. uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
  158. size_t EHFrameSize = Sections[EHFrameSID].Size;
  159. MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
  160. RegisteredEHFrameSections.push_back(EHFrameSID);
  161. }
  162. UnregisteredEHFrameSections.clear();
  163. }
  164. void RuntimeDyldELF::deregisterEHFrames() {
  165. for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
  166. SID EHFrameSID = RegisteredEHFrameSections[i];
  167. uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
  168. uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
  169. size_t EHFrameSize = Sections[EHFrameSID].Size;
  170. MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
  171. }
  172. RegisteredEHFrameSections.clear();
  173. }
  174. std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
  175. RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
  176. unsigned SectionStartIdx, SectionEndIdx;
  177. std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
  178. return llvm::make_unique<LoadedELFObjectInfo>(*this, SectionStartIdx,
  179. SectionEndIdx);
  180. }
  181. void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
  182. uint64_t Offset, uint64_t Value,
  183. uint32_t Type, int64_t Addend,
  184. uint64_t SymOffset) {
  185. switch (Type) {
  186. default:
  187. llvm_unreachable("Relocation type not implemented yet!");
  188. break;
  189. case ELF::R_X86_64_64: {
  190. support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
  191. DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
  192. << format("%p\n", Section.Address + Offset));
  193. break;
  194. }
  195. case ELF::R_X86_64_32:
  196. case ELF::R_X86_64_32S: {
  197. Value += Addend;
  198. assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
  199. (Type == ELF::R_X86_64_32S &&
  200. ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
  201. uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
  202. support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
  203. DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
  204. << format("%p\n", Section.Address + Offset));
  205. break;
  206. }
  207. case ELF::R_X86_64_PC32: {
  208. uint64_t FinalAddress = Section.LoadAddress + Offset;
  209. int64_t RealOffset = Value + Addend - FinalAddress;
  210. assert(isInt<32>(RealOffset));
  211. int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
  212. support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
  213. break;
  214. }
  215. case ELF::R_X86_64_PC64: {
  216. uint64_t FinalAddress = Section.LoadAddress + Offset;
  217. int64_t RealOffset = Value + Addend - FinalAddress;
  218. support::ulittle64_t::ref(Section.Address + Offset) = RealOffset;
  219. break;
  220. }
  221. }
  222. }
  223. void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
  224. uint64_t Offset, uint32_t Value,
  225. uint32_t Type, int32_t Addend) {
  226. switch (Type) {
  227. case ELF::R_386_32: {
  228. support::ulittle32_t::ref(Section.Address + Offset) = Value + Addend;
  229. break;
  230. }
  231. case ELF::R_386_PC32: {
  232. uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
  233. uint32_t RealOffset = Value + Addend - FinalAddress;
  234. support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
  235. break;
  236. }
  237. default:
  238. // There are other relocation types, but it appears these are the
  239. // only ones currently used by the LLVM ELF object writer
  240. llvm_unreachable("Relocation type not implemented yet!");
  241. break;
  242. }
  243. }
  244. void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
  245. uint64_t Offset, uint64_t Value,
  246. uint32_t Type, int64_t Addend) {
  247. uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
  248. uint64_t FinalAddress = Section.LoadAddress + Offset;
  249. DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
  250. << format("%llx", Section.Address + Offset)
  251. << " FinalAddress: 0x" << format("%llx", FinalAddress)
  252. << " Value: 0x" << format("%llx", Value) << " Type: 0x"
  253. << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
  254. << "\n");
  255. switch (Type) {
  256. default:
  257. llvm_unreachable("Relocation type not implemented yet!");
  258. break;
  259. case ELF::R_AARCH64_ABS64: {
  260. uint64_t *TargetPtr =
  261. reinterpret_cast<uint64_t *>(Section.Address + Offset);
  262. *TargetPtr = Value + Addend;
  263. break;
  264. }
  265. case ELF::R_AARCH64_PREL32: {
  266. uint64_t Result = Value + Addend - FinalAddress;
  267. assert(static_cast<int64_t>(Result) >= INT32_MIN &&
  268. static_cast<int64_t>(Result) <= UINT32_MAX);
  269. *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
  270. break;
  271. }
  272. case ELF::R_AARCH64_CALL26: // fallthrough
  273. case ELF::R_AARCH64_JUMP26: {
  274. // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
  275. // calculation.
  276. uint64_t BranchImm = Value + Addend - FinalAddress;
  277. // "Check that -2^27 <= result < 2^27".
  278. assert(isInt<28>(BranchImm));
  279. // AArch64 code is emitted with .rela relocations. The data already in any
  280. // bits affected by the relocation on entry is garbage.
  281. *TargetPtr &= 0xfc000000U;
  282. // Immediate goes in bits 25:0 of B and BL.
  283. *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
  284. break;
  285. }
  286. case ELF::R_AARCH64_MOVW_UABS_G3: {
  287. uint64_t Result = Value + Addend;
  288. // AArch64 code is emitted with .rela relocations. The data already in any
  289. // bits affected by the relocation on entry is garbage.
  290. *TargetPtr &= 0xffe0001fU;
  291. // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
  292. *TargetPtr |= Result >> (48 - 5);
  293. // Shift must be "lsl #48", in bits 22:21
  294. assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
  295. break;
  296. }
  297. case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
  298. uint64_t Result = Value + Addend;
  299. // AArch64 code is emitted with .rela relocations. The data already in any
  300. // bits affected by the relocation on entry is garbage.
  301. *TargetPtr &= 0xffe0001fU;
  302. // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
  303. *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
  304. // Shift must be "lsl #32", in bits 22:21
  305. assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
  306. break;
  307. }
  308. case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
  309. uint64_t Result = Value + Addend;
  310. // AArch64 code is emitted with .rela relocations. The data already in any
  311. // bits affected by the relocation on entry is garbage.
  312. *TargetPtr &= 0xffe0001fU;
  313. // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
  314. *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
  315. // Shift must be "lsl #16", in bits 22:2
  316. assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
  317. break;
  318. }
  319. case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
  320. uint64_t Result = Value + Addend;
  321. // AArch64 code is emitted with .rela relocations. The data already in any
  322. // bits affected by the relocation on entry is garbage.
  323. *TargetPtr &= 0xffe0001fU;
  324. // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
  325. *TargetPtr |= ((Result & 0xffffU) << 5);
  326. // Shift must be "lsl #0", in bits 22:21.
  327. assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
  328. break;
  329. }
  330. case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
  331. // Operation: Page(S+A) - Page(P)
  332. uint64_t Result =
  333. ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
  334. // Check that -2^32 <= X < 2^32
  335. assert(isInt<33>(Result) && "overflow check failed for relocation");
  336. // AArch64 code is emitted with .rela relocations. The data already in any
  337. // bits affected by the relocation on entry is garbage.
  338. *TargetPtr &= 0x9f00001fU;
  339. // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
  340. // from bits 32:12 of X.
  341. *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
  342. *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
  343. break;
  344. }
  345. case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
  346. // Operation: S + A
  347. uint64_t Result = Value + Addend;
  348. // AArch64 code is emitted with .rela relocations. The data already in any
  349. // bits affected by the relocation on entry is garbage.
  350. *TargetPtr &= 0xffc003ffU;
  351. // Immediate goes in bits 21:10 of LD/ST instruction, taken
  352. // from bits 11:2 of X
  353. *TargetPtr |= ((Result & 0xffc) << (10 - 2));
  354. break;
  355. }
  356. case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
  357. // Operation: S + A
  358. uint64_t Result = Value + Addend;
  359. // AArch64 code is emitted with .rela relocations. The data already in any
  360. // bits affected by the relocation on entry is garbage.
  361. *TargetPtr &= 0xffc003ffU;
  362. // Immediate goes in bits 21:10 of LD/ST instruction, taken
  363. // from bits 11:3 of X
  364. *TargetPtr |= ((Result & 0xff8) << (10 - 3));
  365. break;
  366. }
  367. }
  368. }
  369. void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
  370. uint64_t Offset, uint32_t Value,
  371. uint32_t Type, int32_t Addend) {
  372. // TODO: Add Thumb relocations.
  373. uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
  374. uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
  375. Value += Addend;
  376. DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
  377. << Section.Address + Offset
  378. << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
  379. << format("%x", Value) << " Type: " << format("%x", Type)
  380. << " Addend: " << format("%x", Addend) << "\n");
  381. switch (Type) {
  382. default:
  383. llvm_unreachable("Not implemented relocation type!");
  384. case ELF::R_ARM_NONE:
  385. break;
  386. case ELF::R_ARM_PREL31:
  387. case ELF::R_ARM_TARGET1:
  388. case ELF::R_ARM_ABS32:
  389. *TargetPtr = Value;
  390. break;
  391. // Write first 16 bit of 32 bit value to the mov instruction.
  392. // Last 4 bit should be shifted.
  393. case ELF::R_ARM_MOVW_ABS_NC:
  394. case ELF::R_ARM_MOVT_ABS:
  395. if (Type == ELF::R_ARM_MOVW_ABS_NC)
  396. Value = Value & 0xFFFF;
  397. else if (Type == ELF::R_ARM_MOVT_ABS)
  398. Value = (Value >> 16) & 0xFFFF;
  399. *TargetPtr &= ~0x000F0FFF;
  400. *TargetPtr |= Value & 0xFFF;
  401. *TargetPtr |= ((Value >> 12) & 0xF) << 16;
  402. break;
  403. // Write 24 bit relative value to the branch instruction.
  404. case ELF::R_ARM_PC24: // Fall through.
  405. case ELF::R_ARM_CALL: // Fall through.
  406. case ELF::R_ARM_JUMP24:
  407. int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
  408. RelValue = (RelValue & 0x03FFFFFC) >> 2;
  409. assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
  410. *TargetPtr &= 0xFF000000;
  411. *TargetPtr |= RelValue;
  412. break;
  413. }
  414. }
  415. void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
  416. uint64_t Offset, uint32_t Value,
  417. uint32_t Type, int32_t Addend) {
  418. uint8_t *TargetPtr = Section.Address + Offset;
  419. Value += Addend;
  420. DEBUG(dbgs() << "resolveMIPSRelocation, LocalAddress: "
  421. << Section.Address + Offset << " FinalAddress: "
  422. << format("%p", Section.LoadAddress + Offset) << " Value: "
  423. << format("%x", Value) << " Type: " << format("%x", Type)
  424. << " Addend: " << format("%x", Addend) << "\n");
  425. uint32_t Insn = readBytesUnaligned(TargetPtr, 4);
  426. switch (Type) {
  427. default:
  428. llvm_unreachable("Not implemented relocation type!");
  429. break;
  430. case ELF::R_MIPS_32:
  431. writeBytesUnaligned(Value, TargetPtr, 4);
  432. break;
  433. case ELF::R_MIPS_26:
  434. Insn &= 0xfc000000;
  435. Insn |= (Value & 0x0fffffff) >> 2;
  436. writeBytesUnaligned(Insn, TargetPtr, 4);
  437. break;
  438. case ELF::R_MIPS_HI16:
  439. // Get the higher 16-bits. Also add 1 if bit 15 is 1.
  440. Insn &= 0xffff0000;
  441. Insn |= ((Value + 0x8000) >> 16) & 0xffff;
  442. writeBytesUnaligned(Insn, TargetPtr, 4);
  443. break;
  444. case ELF::R_MIPS_LO16:
  445. Insn &= 0xffff0000;
  446. Insn |= Value & 0xffff;
  447. writeBytesUnaligned(Insn, TargetPtr, 4);
  448. break;
  449. case ELF::R_MIPS_PC32: {
  450. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  451. writeBytesUnaligned(Value - FinalAddress, (uint8_t *)TargetPtr, 4);
  452. break;
  453. }
  454. case ELF::R_MIPS_PC16: {
  455. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  456. Insn &= 0xffff0000;
  457. Insn |= ((Value - FinalAddress) >> 2) & 0xffff;
  458. writeBytesUnaligned(Insn, TargetPtr, 4);
  459. break;
  460. }
  461. case ELF::R_MIPS_PC19_S2: {
  462. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  463. Insn &= 0xfff80000;
  464. Insn |= ((Value - (FinalAddress & ~0x3)) >> 2) & 0x7ffff;
  465. writeBytesUnaligned(Insn, TargetPtr, 4);
  466. break;
  467. }
  468. case ELF::R_MIPS_PC21_S2: {
  469. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  470. Insn &= 0xffe00000;
  471. Insn |= ((Value - FinalAddress) >> 2) & 0x1fffff;
  472. writeBytesUnaligned(Insn, TargetPtr, 4);
  473. break;
  474. }
  475. case ELF::R_MIPS_PC26_S2: {
  476. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  477. Insn &= 0xfc000000;
  478. Insn |= ((Value - FinalAddress) >> 2) & 0x3ffffff;
  479. writeBytesUnaligned(Insn, TargetPtr, 4);
  480. break;
  481. }
  482. case ELF::R_MIPS_PCHI16: {
  483. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  484. Insn &= 0xffff0000;
  485. Insn |= ((Value - FinalAddress + 0x8000) >> 16) & 0xffff;
  486. writeBytesUnaligned(Insn, TargetPtr, 4);
  487. break;
  488. }
  489. case ELF::R_MIPS_PCLO16: {
  490. uint32_t FinalAddress = (Section.LoadAddress + Offset);
  491. Insn &= 0xffff0000;
  492. Insn |= (Value - FinalAddress) & 0xffff;
  493. writeBytesUnaligned(Insn, TargetPtr, 4);
  494. break;
  495. }
  496. }
  497. }
  498. void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
  499. if (Arch == Triple::UnknownArch ||
  500. !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
  501. IsMipsO32ABI = false;
  502. IsMipsN64ABI = false;
  503. return;
  504. }
  505. unsigned AbiVariant;
  506. Obj.getPlatformFlags(AbiVariant);
  507. IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
  508. IsMipsN64ABI = Obj.getFileFormatName().equals("ELF64-mips");
  509. if (AbiVariant & ELF::EF_MIPS_ABI2)
  510. llvm_unreachable("Mips N32 ABI is not supported yet");
  511. }
  512. void RuntimeDyldELF::resolveMIPS64Relocation(const SectionEntry &Section,
  513. uint64_t Offset, uint64_t Value,
  514. uint32_t Type, int64_t Addend,
  515. uint64_t SymOffset,
  516. SID SectionID) {
  517. uint32_t r_type = Type & 0xff;
  518. uint32_t r_type2 = (Type >> 8) & 0xff;
  519. uint32_t r_type3 = (Type >> 16) & 0xff;
  520. // RelType is used to keep information for which relocation type we are
  521. // applying relocation.
  522. uint32_t RelType = r_type;
  523. int64_t CalculatedValue = evaluateMIPS64Relocation(Section, Offset, Value,
  524. RelType, Addend,
  525. SymOffset, SectionID);
  526. if (r_type2 != ELF::R_MIPS_NONE) {
  527. RelType = r_type2;
  528. CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType,
  529. CalculatedValue, SymOffset,
  530. SectionID);
  531. }
  532. if (r_type3 != ELF::R_MIPS_NONE) {
  533. RelType = r_type3;
  534. CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType,
  535. CalculatedValue, SymOffset,
  536. SectionID);
  537. }
  538. applyMIPS64Relocation(Section.Address + Offset, CalculatedValue, RelType);
  539. }
  540. int64_t
  541. RuntimeDyldELF::evaluateMIPS64Relocation(const SectionEntry &Section,
  542. uint64_t Offset, uint64_t Value,
  543. uint32_t Type, int64_t Addend,
  544. uint64_t SymOffset, SID SectionID) {
  545. DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x"
  546. << format("%llx", Section.Address + Offset)
  547. << " FinalAddress: 0x"
  548. << format("%llx", Section.LoadAddress + Offset)
  549. << " Value: 0x" << format("%llx", Value) << " Type: 0x"
  550. << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
  551. << " SymOffset: " << format("%x", SymOffset)
  552. << "\n");
  553. switch (Type) {
  554. default:
  555. llvm_unreachable("Not implemented relocation type!");
  556. break;
  557. case ELF::R_MIPS_JALR:
  558. case ELF::R_MIPS_NONE:
  559. break;
  560. case ELF::R_MIPS_32:
  561. case ELF::R_MIPS_64:
  562. return Value + Addend;
  563. case ELF::R_MIPS_26:
  564. return ((Value + Addend) >> 2) & 0x3ffffff;
  565. case ELF::R_MIPS_GPREL16: {
  566. uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]);
  567. return Value + Addend - (GOTAddr + 0x7ff0);
  568. }
  569. case ELF::R_MIPS_SUB:
  570. return Value - Addend;
  571. case ELF::R_MIPS_HI16:
  572. // Get the higher 16-bits. Also add 1 if bit 15 is 1.
  573. return ((Value + Addend + 0x8000) >> 16) & 0xffff;
  574. case ELF::R_MIPS_LO16:
  575. return (Value + Addend) & 0xffff;
  576. case ELF::R_MIPS_CALL16:
  577. case ELF::R_MIPS_GOT_DISP:
  578. case ELF::R_MIPS_GOT_PAGE: {
  579. uint8_t *LocalGOTAddr =
  580. getSectionAddress(SectionToGOTMap[SectionID]) + SymOffset;
  581. uint64_t GOTEntry = readBytesUnaligned(LocalGOTAddr, 8);
  582. Value += Addend;
  583. if (Type == ELF::R_MIPS_GOT_PAGE)
  584. Value = (Value + 0x8000) & ~0xffff;
  585. if (GOTEntry)
  586. assert(GOTEntry == Value &&
  587. "GOT entry has two different addresses.");
  588. else
  589. writeBytesUnaligned(Value, LocalGOTAddr, 8);
  590. return (SymOffset - 0x7ff0) & 0xffff;
  591. }
  592. case ELF::R_MIPS_GOT_OFST: {
  593. int64_t page = (Value + Addend + 0x8000) & ~0xffff;
  594. return (Value + Addend - page) & 0xffff;
  595. }
  596. case ELF::R_MIPS_GPREL32: {
  597. uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]);
  598. return Value + Addend - (GOTAddr + 0x7ff0);
  599. }
  600. case ELF::R_MIPS_PC16: {
  601. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  602. return ((Value + Addend - FinalAddress) >> 2) & 0xffff;
  603. }
  604. case ELF::R_MIPS_PC32: {
  605. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  606. return Value + Addend - FinalAddress;
  607. }
  608. case ELF::R_MIPS_PC18_S3: {
  609. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  610. return ((Value + Addend - ((FinalAddress | 7) ^ 7)) >> 3) & 0x3ffff;
  611. }
  612. case ELF::R_MIPS_PC19_S2: {
  613. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  614. return ((Value + Addend - FinalAddress) >> 2) & 0x7ffff;
  615. }
  616. case ELF::R_MIPS_PC21_S2: {
  617. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  618. return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff;
  619. }
  620. case ELF::R_MIPS_PC26_S2: {
  621. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  622. return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff;
  623. }
  624. case ELF::R_MIPS_PCHI16: {
  625. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  626. return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff;
  627. }
  628. case ELF::R_MIPS_PCLO16: {
  629. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  630. return (Value + Addend - FinalAddress) & 0xffff;
  631. }
  632. }
  633. return 0;
  634. }
  635. void RuntimeDyldELF::applyMIPS64Relocation(uint8_t *TargetPtr,
  636. int64_t CalculatedValue,
  637. uint32_t Type) {
  638. uint32_t Insn = readBytesUnaligned(TargetPtr, 4);
  639. switch (Type) {
  640. default:
  641. break;
  642. case ELF::R_MIPS_32:
  643. case ELF::R_MIPS_GPREL32:
  644. case ELF::R_MIPS_PC32:
  645. writeBytesUnaligned(CalculatedValue & 0xffffffff, TargetPtr, 4);
  646. break;
  647. case ELF::R_MIPS_64:
  648. case ELF::R_MIPS_SUB:
  649. writeBytesUnaligned(CalculatedValue, TargetPtr, 8);
  650. break;
  651. case ELF::R_MIPS_26:
  652. case ELF::R_MIPS_PC26_S2:
  653. Insn = (Insn & 0xfc000000) | CalculatedValue;
  654. writeBytesUnaligned(Insn, TargetPtr, 4);
  655. break;
  656. case ELF::R_MIPS_GPREL16:
  657. Insn = (Insn & 0xffff0000) | (CalculatedValue & 0xffff);
  658. writeBytesUnaligned(Insn, TargetPtr, 4);
  659. break;
  660. case ELF::R_MIPS_HI16:
  661. case ELF::R_MIPS_LO16:
  662. case ELF::R_MIPS_PCHI16:
  663. case ELF::R_MIPS_PCLO16:
  664. case ELF::R_MIPS_PC16:
  665. case ELF::R_MIPS_CALL16:
  666. case ELF::R_MIPS_GOT_DISP:
  667. case ELF::R_MIPS_GOT_PAGE:
  668. case ELF::R_MIPS_GOT_OFST:
  669. Insn = (Insn & 0xffff0000) | CalculatedValue;
  670. writeBytesUnaligned(Insn, TargetPtr, 4);
  671. break;
  672. case ELF::R_MIPS_PC18_S3:
  673. Insn = (Insn & 0xfffc0000) | CalculatedValue;
  674. writeBytesUnaligned(Insn, TargetPtr, 4);
  675. break;
  676. case ELF::R_MIPS_PC19_S2:
  677. Insn = (Insn & 0xfff80000) | CalculatedValue;
  678. writeBytesUnaligned(Insn, TargetPtr, 4);
  679. break;
  680. case ELF::R_MIPS_PC21_S2:
  681. Insn = (Insn & 0xffe00000) | CalculatedValue;
  682. writeBytesUnaligned(Insn, TargetPtr, 4);
  683. break;
  684. }
  685. }
  686. // Return the .TOC. section and offset.
  687. void RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
  688. ObjSectionToIDMap &LocalSections,
  689. RelocationValueRef &Rel) {
  690. // Set a default SectionID in case we do not find a TOC section below.
  691. // This may happen for references to TOC base base (sym@toc, .odp
  692. // relocation) without a .toc directive. In this case just use the
  693. // first section (which is usually the .odp) since the code won't
  694. // reference the .toc base directly.
  695. Rel.SymbolName = NULL;
  696. Rel.SectionID = 0;
  697. // The TOC consists of sections .got, .toc, .tocbss, .plt in that
  698. // order. The TOC starts where the first of these sections starts.
  699. for (auto &Section: Obj.sections()) {
  700. StringRef SectionName;
  701. check(Section.getName(SectionName));
  702. if (SectionName == ".got"
  703. || SectionName == ".toc"
  704. || SectionName == ".tocbss"
  705. || SectionName == ".plt") {
  706. Rel.SectionID = findOrEmitSection(Obj, Section, false, LocalSections);
  707. break;
  708. }
  709. }
  710. // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
  711. // thus permitting a full 64 Kbytes segment.
  712. Rel.Addend = 0x8000;
  713. }
  714. // Returns the sections and offset associated with the ODP entry referenced
  715. // by Symbol.
  716. void RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
  717. ObjSectionToIDMap &LocalSections,
  718. RelocationValueRef &Rel) {
  719. // Get the ELF symbol value (st_value) to compare with Relocation offset in
  720. // .opd entries
  721. for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
  722. si != se; ++si) {
  723. section_iterator RelSecI = si->getRelocatedSection();
  724. if (RelSecI == Obj.section_end())
  725. continue;
  726. StringRef RelSectionName;
  727. check(RelSecI->getName(RelSectionName));
  728. if (RelSectionName != ".opd")
  729. continue;
  730. for (elf_relocation_iterator i = si->relocation_begin(),
  731. e = si->relocation_end();
  732. i != e;) {
  733. // The R_PPC64_ADDR64 relocation indicates the first field
  734. // of a .opd entry
  735. uint64_t TypeFunc = i->getType();
  736. if (TypeFunc != ELF::R_PPC64_ADDR64) {
  737. ++i;
  738. continue;
  739. }
  740. uint64_t TargetSymbolOffset = i->getOffset();
  741. symbol_iterator TargetSymbol = i->getSymbol();
  742. ErrorOr<int64_t> AddendOrErr = i->getAddend();
  743. Check(AddendOrErr.getError());
  744. int64_t Addend = *AddendOrErr;
  745. ++i;
  746. if (i == e)
  747. break;
  748. // Just check if following relocation is a R_PPC64_TOC
  749. uint64_t TypeTOC = i->getType();
  750. if (TypeTOC != ELF::R_PPC64_TOC)
  751. continue;
  752. // Finally compares the Symbol value and the target symbol offset
  753. // to check if this .opd entry refers to the symbol the relocation
  754. // points to.
  755. if (Rel.Addend != (int64_t)TargetSymbolOffset)
  756. continue;
  757. section_iterator tsi(Obj.section_end());
  758. check(TargetSymbol->getSection(tsi));
  759. bool IsCode = tsi->isText();
  760. Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
  761. Rel.Addend = (intptr_t)Addend;
  762. return;
  763. }
  764. }
  765. llvm_unreachable("Attempting to get address of ODP entry!");
  766. }
  767. // Relocation masks following the #lo(value), #hi(value), #ha(value),
  768. // #higher(value), #highera(value), #highest(value), and #highesta(value)
  769. // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
  770. // document.
  771. static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
  772. static inline uint16_t applyPPChi(uint64_t value) {
  773. return (value >> 16) & 0xffff;
  774. }
  775. static inline uint16_t applyPPCha (uint64_t value) {
  776. return ((value + 0x8000) >> 16) & 0xffff;
  777. }
  778. static inline uint16_t applyPPChigher(uint64_t value) {
  779. return (value >> 32) & 0xffff;
  780. }
  781. static inline uint16_t applyPPChighera (uint64_t value) {
  782. return ((value + 0x8000) >> 32) & 0xffff;
  783. }
  784. static inline uint16_t applyPPChighest(uint64_t value) {
  785. return (value >> 48) & 0xffff;
  786. }
  787. static inline uint16_t applyPPChighesta (uint64_t value) {
  788. return ((value + 0x8000) >> 48) & 0xffff;
  789. }
  790. void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
  791. uint64_t Offset, uint64_t Value,
  792. uint32_t Type, int64_t Addend) {
  793. uint8_t *LocalAddress = Section.Address + Offset;
  794. switch (Type) {
  795. default:
  796. llvm_unreachable("Relocation type not implemented yet!");
  797. break;
  798. case ELF::R_PPC64_ADDR16:
  799. writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
  800. break;
  801. case ELF::R_PPC64_ADDR16_DS:
  802. writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
  803. break;
  804. case ELF::R_PPC64_ADDR16_LO:
  805. writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
  806. break;
  807. case ELF::R_PPC64_ADDR16_LO_DS:
  808. writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
  809. break;
  810. case ELF::R_PPC64_ADDR16_HI:
  811. writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
  812. break;
  813. case ELF::R_PPC64_ADDR16_HA:
  814. writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
  815. break;
  816. case ELF::R_PPC64_ADDR16_HIGHER:
  817. writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
  818. break;
  819. case ELF::R_PPC64_ADDR16_HIGHERA:
  820. writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
  821. break;
  822. case ELF::R_PPC64_ADDR16_HIGHEST:
  823. writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
  824. break;
  825. case ELF::R_PPC64_ADDR16_HIGHESTA:
  826. writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
  827. break;
  828. case ELF::R_PPC64_ADDR14: {
  829. assert(((Value + Addend) & 3) == 0);
  830. // Preserve the AA/LK bits in the branch instruction
  831. uint8_t aalk = *(LocalAddress + 3);
  832. writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
  833. } break;
  834. case ELF::R_PPC64_REL16_LO: {
  835. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  836. uint64_t Delta = Value - FinalAddress + Addend;
  837. writeInt16BE(LocalAddress, applyPPClo(Delta));
  838. } break;
  839. case ELF::R_PPC64_REL16_HI: {
  840. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  841. uint64_t Delta = Value - FinalAddress + Addend;
  842. writeInt16BE(LocalAddress, applyPPChi(Delta));
  843. } break;
  844. case ELF::R_PPC64_REL16_HA: {
  845. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  846. uint64_t Delta = Value - FinalAddress + Addend;
  847. writeInt16BE(LocalAddress, applyPPCha(Delta));
  848. } break;
  849. case ELF::R_PPC64_ADDR32: {
  850. int32_t Result = static_cast<int32_t>(Value + Addend);
  851. if (SignExtend32<32>(Result) != Result)
  852. llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
  853. writeInt32BE(LocalAddress, Result);
  854. } break;
  855. case ELF::R_PPC64_REL24: {
  856. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  857. int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
  858. if (SignExtend32<24>(delta) != delta)
  859. llvm_unreachable("Relocation R_PPC64_REL24 overflow");
  860. // Generates a 'bl <address>' instruction
  861. writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
  862. } break;
  863. case ELF::R_PPC64_REL32: {
  864. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  865. int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
  866. if (SignExtend32<32>(delta) != delta)
  867. llvm_unreachable("Relocation R_PPC64_REL32 overflow");
  868. writeInt32BE(LocalAddress, delta);
  869. } break;
  870. case ELF::R_PPC64_REL64: {
  871. uint64_t FinalAddress = (Section.LoadAddress + Offset);
  872. uint64_t Delta = Value - FinalAddress + Addend;
  873. writeInt64BE(LocalAddress, Delta);
  874. } break;
  875. case ELF::R_PPC64_ADDR64:
  876. writeInt64BE(LocalAddress, Value + Addend);
  877. break;
  878. }
  879. }
  880. void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
  881. uint64_t Offset, uint64_t Value,
  882. uint32_t Type, int64_t Addend) {
  883. uint8_t *LocalAddress = Section.Address + Offset;
  884. switch (Type) {
  885. default:
  886. llvm_unreachable("Relocation type not implemented yet!");
  887. break;
  888. case ELF::R_390_PC16DBL:
  889. case ELF::R_390_PLT16DBL: {
  890. int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
  891. assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
  892. writeInt16BE(LocalAddress, Delta / 2);
  893. break;
  894. }
  895. case ELF::R_390_PC32DBL:
  896. case ELF::R_390_PLT32DBL: {
  897. int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
  898. assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
  899. writeInt32BE(LocalAddress, Delta / 2);
  900. break;
  901. }
  902. case ELF::R_390_PC32: {
  903. int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
  904. assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
  905. writeInt32BE(LocalAddress, Delta);
  906. break;
  907. }
  908. case ELF::R_390_64:
  909. writeInt64BE(LocalAddress, Value + Addend);
  910. break;
  911. }
  912. }
  913. // The target location for the relocation is described by RE.SectionID and
  914. // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
  915. // SectionEntry has three members describing its location.
  916. // SectionEntry::Address is the address at which the section has been loaded
  917. // into memory in the current (host) process. SectionEntry::LoadAddress is the
  918. // address that the section will have in the target process.
  919. // SectionEntry::ObjAddress is the address of the bits for this section in the
  920. // original emitted object image (also in the current address space).
  921. //
  922. // Relocations will be applied as if the section were loaded at
  923. // SectionEntry::LoadAddress, but they will be applied at an address based
  924. // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
  925. // Target memory contents if they are required for value calculations.
  926. //
  927. // The Value parameter here is the load address of the symbol for the
  928. // relocation to be applied. For relocations which refer to symbols in the
  929. // current object Value will be the LoadAddress of the section in which
  930. // the symbol resides (RE.Addend provides additional information about the
  931. // symbol location). For external symbols, Value will be the address of the
  932. // symbol in the target address space.
  933. void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
  934. uint64_t Value) {
  935. const SectionEntry &Section = Sections[RE.SectionID];
  936. return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
  937. RE.SymOffset, RE.SectionID);
  938. }
  939. void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
  940. uint64_t Offset, uint64_t Value,
  941. uint32_t Type, int64_t Addend,
  942. uint64_t SymOffset, SID SectionID) {
  943. switch (Arch) {
  944. case Triple::x86_64:
  945. resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
  946. break;
  947. case Triple::x86:
  948. resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
  949. (uint32_t)(Addend & 0xffffffffL));
  950. break;
  951. case Triple::aarch64:
  952. case Triple::aarch64_be:
  953. resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
  954. break;
  955. case Triple::arm: // Fall through.
  956. case Triple::armeb:
  957. case Triple::thumb:
  958. case Triple::thumbeb:
  959. resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
  960. (uint32_t)(Addend & 0xffffffffL));
  961. break;
  962. case Triple::mips: // Fall through.
  963. case Triple::mipsel:
  964. case Triple::mips64:
  965. case Triple::mips64el:
  966. if (IsMipsO32ABI)
  967. resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
  968. Type, (uint32_t)(Addend & 0xffffffffL));
  969. else if (IsMipsN64ABI)
  970. resolveMIPS64Relocation(Section, Offset, Value, Type, Addend, SymOffset,
  971. SectionID);
  972. else
  973. llvm_unreachable("Mips ABI not handled");
  974. break;
  975. case Triple::ppc64: // Fall through.
  976. case Triple::ppc64le:
  977. resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
  978. break;
  979. case Triple::systemz:
  980. resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
  981. break;
  982. default:
  983. llvm_unreachable("Unsupported CPU type!");
  984. }
  985. }
  986. void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
  987. return (void*)(Sections[SectionID].ObjAddress + Offset);
  988. }
  989. void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
  990. RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
  991. if (Value.SymbolName)
  992. addRelocationForSymbol(RE, Value.SymbolName);
  993. else
  994. addRelocationForSection(RE, Value.SectionID);
  995. }
  996. relocation_iterator RuntimeDyldELF::processRelocationRef(
  997. unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
  998. ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
  999. const auto &Obj = cast<ELFObjectFileBase>(O);
  1000. uint64_t RelType = RelI->getType();
  1001. ErrorOr<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend();
  1002. int64_t Addend = AddendOrErr ? *AddendOrErr : 0;
  1003. elf_symbol_iterator Symbol = RelI->getSymbol();
  1004. // Obtain the symbol name which is referenced in the relocation
  1005. StringRef TargetName;
  1006. if (Symbol != Obj.symbol_end()) {
  1007. ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
  1008. if (std::error_code EC = TargetNameOrErr.getError())
  1009. report_fatal_error(EC.message());
  1010. TargetName = *TargetNameOrErr;
  1011. }
  1012. DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
  1013. << " TargetName: " << TargetName << "\n");
  1014. RelocationValueRef Value;
  1015. // First search for the symbol in the local symbol table
  1016. SymbolRef::Type SymType = SymbolRef::ST_Unknown;
  1017. // Search for the symbol in the global symbol table
  1018. RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
  1019. if (Symbol != Obj.symbol_end()) {
  1020. gsi = GlobalSymbolTable.find(TargetName.data());
  1021. SymType = Symbol->getType();
  1022. }
  1023. if (gsi != GlobalSymbolTable.end()) {
  1024. const auto &SymInfo = gsi->second;
  1025. Value.SectionID = SymInfo.getSectionID();
  1026. Value.Offset = SymInfo.getOffset();
  1027. Value.Addend = SymInfo.getOffset() + Addend;
  1028. } else {
  1029. switch (SymType) {
  1030. case SymbolRef::ST_Debug: {
  1031. // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
  1032. // and can be changed by another developers. Maybe best way is add
  1033. // a new symbol type ST_Section to SymbolRef and use it.
  1034. section_iterator si(Obj.section_end());
  1035. Symbol->getSection(si);
  1036. if (si == Obj.section_end())
  1037. llvm_unreachable("Symbol section not found, bad object file format!");
  1038. DEBUG(dbgs() << "\t\tThis is section symbol\n");
  1039. bool isCode = si->isText();
  1040. Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
  1041. Value.Addend = Addend;
  1042. break;
  1043. }
  1044. case SymbolRef::ST_Data:
  1045. case SymbolRef::ST_Unknown: {
  1046. Value.SymbolName = TargetName.data();
  1047. Value.Addend = Addend;
  1048. // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
  1049. // will manifest here as a NULL symbol name.
  1050. // We can set this as a valid (but empty) symbol name, and rely
  1051. // on addRelocationForSymbol to handle this.
  1052. if (!Value.SymbolName)
  1053. Value.SymbolName = "";
  1054. break;
  1055. }
  1056. default:
  1057. llvm_unreachable("Unresolved symbol type!");
  1058. break;
  1059. }
  1060. }
  1061. uint64_t Offset = RelI->getOffset();
  1062. DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
  1063. << "\n");
  1064. if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
  1065. (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
  1066. // This is an AArch64 branch relocation, need to use a stub function.
  1067. DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
  1068. SectionEntry &Section = Sections[SectionID];
  1069. // Look for an existing stub.
  1070. StubMap::const_iterator i = Stubs.find(Value);
  1071. if (i != Stubs.end()) {
  1072. resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
  1073. RelType, 0);
  1074. DEBUG(dbgs() << " Stub function found\n");
  1075. } else {
  1076. // Create a new stub function.
  1077. DEBUG(dbgs() << " Create a new stub function\n");
  1078. Stubs[Value] = Section.StubOffset;
  1079. uint8_t *StubTargetAddr =
  1080. createStubFunction(Section.Address + Section.StubOffset);
  1081. RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
  1082. ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
  1083. RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
  1084. ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
  1085. RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
  1086. ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
  1087. RelocationEntry REmovk_g0(SectionID,
  1088. StubTargetAddr - Section.Address + 12,
  1089. ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
  1090. if (Value.SymbolName) {
  1091. addRelocationForSymbol(REmovz_g3, Value.SymbolName);
  1092. addRelocationForSymbol(REmovk_g2, Value.SymbolName);
  1093. addRelocationForSymbol(REmovk_g1, Value.SymbolName);
  1094. addRelocationForSymbol(REmovk_g0, Value.SymbolName);
  1095. } else {
  1096. addRelocationForSection(REmovz_g3, Value.SectionID);
  1097. addRelocationForSection(REmovk_g2, Value.SectionID);
  1098. addRelocationForSection(REmovk_g1, Value.SectionID);
  1099. addRelocationForSection(REmovk_g0, Value.SectionID);
  1100. }
  1101. resolveRelocation(Section, Offset,
  1102. (uint64_t)Section.Address + Section.StubOffset, RelType,
  1103. 0);
  1104. Section.StubOffset += getMaxStubSize();
  1105. }
  1106. } else if (Arch == Triple::arm) {
  1107. if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
  1108. RelType == ELF::R_ARM_JUMP24) {
  1109. // This is an ARM branch relocation, need to use a stub function.
  1110. DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
  1111. SectionEntry &Section = Sections[SectionID];
  1112. // Look for an existing stub.
  1113. StubMap::const_iterator i = Stubs.find(Value);
  1114. if (i != Stubs.end()) {
  1115. resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
  1116. RelType, 0);
  1117. DEBUG(dbgs() << " Stub function found\n");
  1118. } else {
  1119. // Create a new stub function.
  1120. DEBUG(dbgs() << " Create a new stub function\n");
  1121. Stubs[Value] = Section.StubOffset;
  1122. uint8_t *StubTargetAddr =
  1123. createStubFunction(Section.Address + Section.StubOffset);
  1124. RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
  1125. ELF::R_ARM_ABS32, Value.Addend);
  1126. if (Value.SymbolName)
  1127. addRelocationForSymbol(RE, Value.SymbolName);
  1128. else
  1129. addRelocationForSection(RE, Value.SectionID);
  1130. resolveRelocation(Section, Offset,
  1131. (uint64_t)Section.Address + Section.StubOffset, RelType,
  1132. 0);
  1133. Section.StubOffset += getMaxStubSize();
  1134. }
  1135. } else {
  1136. uint32_t *Placeholder =
  1137. reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
  1138. if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
  1139. RelType == ELF::R_ARM_ABS32) {
  1140. Value.Addend += *Placeholder;
  1141. } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
  1142. // See ELF for ARM documentation
  1143. Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
  1144. }
  1145. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1146. }
  1147. } else if (IsMipsO32ABI) {
  1148. uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
  1149. computePlaceholderAddress(SectionID, Offset));
  1150. uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
  1151. if (RelType == ELF::R_MIPS_26) {
  1152. // This is an Mips branch relocation, need to use a stub function.
  1153. DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
  1154. SectionEntry &Section = Sections[SectionID];
  1155. // Extract the addend from the instruction.
  1156. // We shift up by two since the Value will be down shifted again
  1157. // when applying the relocation.
  1158. uint32_t Addend = (Opcode & 0x03ffffff) << 2;
  1159. Value.Addend += Addend;
  1160. // Look up for existing stub.
  1161. StubMap::const_iterator i = Stubs.find(Value);
  1162. if (i != Stubs.end()) {
  1163. RelocationEntry RE(SectionID, Offset, RelType, i->second);
  1164. addRelocationForSection(RE, SectionID);
  1165. DEBUG(dbgs() << " Stub function found\n");
  1166. } else {
  1167. // Create a new stub function.
  1168. DEBUG(dbgs() << " Create a new stub function\n");
  1169. Stubs[Value] = Section.StubOffset;
  1170. uint8_t *StubTargetAddr =
  1171. createStubFunction(Section.Address + Section.StubOffset);
  1172. // Creating Hi and Lo relocations for the filled stub instructions.
  1173. RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
  1174. ELF::R_MIPS_HI16, Value.Addend);
  1175. RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
  1176. ELF::R_MIPS_LO16, Value.Addend);
  1177. if (Value.SymbolName) {
  1178. addRelocationForSymbol(REHi, Value.SymbolName);
  1179. addRelocationForSymbol(RELo, Value.SymbolName);
  1180. }
  1181. else {
  1182. addRelocationForSection(REHi, Value.SectionID);
  1183. addRelocationForSection(RELo, Value.SectionID);
  1184. }
  1185. RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
  1186. addRelocationForSection(RE, SectionID);
  1187. Section.StubOffset += getMaxStubSize();
  1188. }
  1189. } else {
  1190. // FIXME: Calculate correct addends for R_MIPS_HI16, R_MIPS_LO16,
  1191. // R_MIPS_PCHI16 and R_MIPS_PCLO16 relocations.
  1192. if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16)
  1193. Value.Addend += (Opcode & 0x0000ffff) << 16;
  1194. else if (RelType == ELF::R_MIPS_LO16)
  1195. Value.Addend += (Opcode & 0x0000ffff);
  1196. else if (RelType == ELF::R_MIPS_32)
  1197. Value.Addend += Opcode;
  1198. else if (RelType == ELF::R_MIPS_PCLO16)
  1199. Value.Addend += SignExtend32<16>((Opcode & 0x0000ffff));
  1200. else if (RelType == ELF::R_MIPS_PC16)
  1201. Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
  1202. else if (RelType == ELF::R_MIPS_PC19_S2)
  1203. Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
  1204. else if (RelType == ELF::R_MIPS_PC21_S2)
  1205. Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
  1206. else if (RelType == ELF::R_MIPS_PC26_S2)
  1207. Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
  1208. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1209. }
  1210. } else if (IsMipsN64ABI) {
  1211. uint32_t r_type = RelType & 0xff;
  1212. RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
  1213. if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
  1214. || r_type == ELF::R_MIPS_GOT_DISP) {
  1215. StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
  1216. if (i != GOTSymbolOffsets.end())
  1217. RE.SymOffset = i->second;
  1218. else {
  1219. RE.SymOffset = allocateGOTEntries(SectionID, 1);
  1220. GOTSymbolOffsets[TargetName] = RE.SymOffset;
  1221. }
  1222. }
  1223. if (Value.SymbolName)
  1224. addRelocationForSymbol(RE, Value.SymbolName);
  1225. else
  1226. addRelocationForSection(RE, Value.SectionID);
  1227. } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
  1228. if (RelType == ELF::R_PPC64_REL24) {
  1229. // Determine ABI variant in use for this object.
  1230. unsigned AbiVariant;
  1231. Obj.getPlatformFlags(AbiVariant);
  1232. AbiVariant &= ELF::EF_PPC64_ABI;
  1233. // A PPC branch relocation will need a stub function if the target is
  1234. // an external symbol (Symbol::ST_Unknown) or if the target address
  1235. // is not within the signed 24-bits branch address.
  1236. SectionEntry &Section = Sections[SectionID];
  1237. uint8_t *Target = Section.Address + Offset;
  1238. bool RangeOverflow = false;
  1239. if (SymType != SymbolRef::ST_Unknown) {
  1240. if (AbiVariant != 2) {
  1241. // In the ELFv1 ABI, a function call may point to the .opd entry,
  1242. // so the final symbol value is calculated based on the relocation
  1243. // values in the .opd section.
  1244. findOPDEntrySection(Obj, ObjSectionToID, Value);
  1245. } else {
  1246. // In the ELFv2 ABI, a function symbol may provide a local entry
  1247. // point, which must be used for direct calls.
  1248. uint8_t SymOther = Symbol->getOther();
  1249. Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
  1250. }
  1251. uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
  1252. int32_t delta = static_cast<int32_t>(Target - RelocTarget);
  1253. // If it is within 24-bits branch range, just set the branch target
  1254. if (SignExtend32<24>(delta) == delta) {
  1255. RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
  1256. if (Value.SymbolName)
  1257. addRelocationForSymbol(RE, Value.SymbolName);
  1258. else
  1259. addRelocationForSection(RE, Value.SectionID);
  1260. } else {
  1261. RangeOverflow = true;
  1262. }
  1263. }
  1264. if (SymType == SymbolRef::ST_Unknown || RangeOverflow) {
  1265. // It is an external symbol (SymbolRef::ST_Unknown) or within a range
  1266. // larger than 24-bits.
  1267. StubMap::const_iterator i = Stubs.find(Value);
  1268. if (i != Stubs.end()) {
  1269. // Symbol function stub already created, just relocate to it
  1270. resolveRelocation(Section, Offset,
  1271. (uint64_t)Section.Address + i->second, RelType, 0);
  1272. DEBUG(dbgs() << " Stub function found\n");
  1273. } else {
  1274. // Create a new stub function.
  1275. DEBUG(dbgs() << " Create a new stub function\n");
  1276. Stubs[Value] = Section.StubOffset;
  1277. uint8_t *StubTargetAddr =
  1278. createStubFunction(Section.Address + Section.StubOffset,
  1279. AbiVariant);
  1280. RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
  1281. ELF::R_PPC64_ADDR64, Value.Addend);
  1282. // Generates the 64-bits address loads as exemplified in section
  1283. // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
  1284. // apply to the low part of the instructions, so we have to update
  1285. // the offset according to the target endianness.
  1286. uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
  1287. if (!IsTargetLittleEndian)
  1288. StubRelocOffset += 2;
  1289. RelocationEntry REhst(SectionID, StubRelocOffset + 0,
  1290. ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
  1291. RelocationEntry REhr(SectionID, StubRelocOffset + 4,
  1292. ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
  1293. RelocationEntry REh(SectionID, StubRelocOffset + 12,
  1294. ELF::R_PPC64_ADDR16_HI, Value.Addend);
  1295. RelocationEntry REl(SectionID, StubRelocOffset + 16,
  1296. ELF::R_PPC64_ADDR16_LO, Value.Addend);
  1297. if (Value.SymbolName) {
  1298. addRelocationForSymbol(REhst, Value.SymbolName);
  1299. addRelocationForSymbol(REhr, Value.SymbolName);
  1300. addRelocationForSymbol(REh, Value.SymbolName);
  1301. addRelocationForSymbol(REl, Value.SymbolName);
  1302. } else {
  1303. addRelocationForSection(REhst, Value.SectionID);
  1304. addRelocationForSection(REhr, Value.SectionID);
  1305. addRelocationForSection(REh, Value.SectionID);
  1306. addRelocationForSection(REl, Value.SectionID);
  1307. }
  1308. resolveRelocation(Section, Offset,
  1309. (uint64_t)Section.Address + Section.StubOffset,
  1310. RelType, 0);
  1311. Section.StubOffset += getMaxStubSize();
  1312. }
  1313. if (SymType == SymbolRef::ST_Unknown) {
  1314. // Restore the TOC for external calls
  1315. if (AbiVariant == 2)
  1316. writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
  1317. else
  1318. writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
  1319. }
  1320. }
  1321. } else if (RelType == ELF::R_PPC64_TOC16 ||
  1322. RelType == ELF::R_PPC64_TOC16_DS ||
  1323. RelType == ELF::R_PPC64_TOC16_LO ||
  1324. RelType == ELF::R_PPC64_TOC16_LO_DS ||
  1325. RelType == ELF::R_PPC64_TOC16_HI ||
  1326. RelType == ELF::R_PPC64_TOC16_HA) {
  1327. // These relocations are supposed to subtract the TOC address from
  1328. // the final value. This does not fit cleanly into the RuntimeDyld
  1329. // scheme, since there may be *two* sections involved in determining
  1330. // the relocation value (the section of the symbol refered to by the
  1331. // relocation, and the TOC section associated with the current module).
  1332. //
  1333. // Fortunately, these relocations are currently only ever generated
  1334. // refering to symbols that themselves reside in the TOC, which means
  1335. // that the two sections are actually the same. Thus they cancel out
  1336. // and we can immediately resolve the relocation right now.
  1337. switch (RelType) {
  1338. case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
  1339. case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
  1340. case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
  1341. case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
  1342. case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
  1343. case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
  1344. default: llvm_unreachable("Wrong relocation type.");
  1345. }
  1346. RelocationValueRef TOCValue;
  1347. findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
  1348. if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
  1349. llvm_unreachable("Unsupported TOC relocation.");
  1350. Value.Addend -= TOCValue.Addend;
  1351. resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
  1352. } else {
  1353. // There are two ways to refer to the TOC address directly: either
  1354. // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
  1355. // ignored), or via any relocation that refers to the magic ".TOC."
  1356. // symbols (in which case the addend is respected).
  1357. if (RelType == ELF::R_PPC64_TOC) {
  1358. RelType = ELF::R_PPC64_ADDR64;
  1359. findPPC64TOCSection(Obj, ObjSectionToID, Value);
  1360. } else if (TargetName == ".TOC.") {
  1361. findPPC64TOCSection(Obj, ObjSectionToID, Value);
  1362. Value.Addend += Addend;
  1363. }
  1364. RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
  1365. if (Value.SymbolName)
  1366. addRelocationForSymbol(RE, Value.SymbolName);
  1367. else
  1368. addRelocationForSection(RE, Value.SectionID);
  1369. }
  1370. } else if (Arch == Triple::systemz &&
  1371. (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
  1372. // Create function stubs for both PLT and GOT references, regardless of
  1373. // whether the GOT reference is to data or code. The stub contains the
  1374. // full address of the symbol, as needed by GOT references, and the
  1375. // executable part only adds an overhead of 8 bytes.
  1376. //
  1377. // We could try to conserve space by allocating the code and data
  1378. // parts of the stub separately. However, as things stand, we allocate
  1379. // a stub for every relocation, so using a GOT in JIT code should be
  1380. // no less space efficient than using an explicit constant pool.
  1381. DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
  1382. SectionEntry &Section = Sections[SectionID];
  1383. // Look for an existing stub.
  1384. StubMap::const_iterator i = Stubs.find(Value);
  1385. uintptr_t StubAddress;
  1386. if (i != Stubs.end()) {
  1387. StubAddress = uintptr_t(Section.Address) + i->second;
  1388. DEBUG(dbgs() << " Stub function found\n");
  1389. } else {
  1390. // Create a new stub function.
  1391. DEBUG(dbgs() << " Create a new stub function\n");
  1392. uintptr_t BaseAddress = uintptr_t(Section.Address);
  1393. uintptr_t StubAlignment = getStubAlignment();
  1394. StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
  1395. -StubAlignment;
  1396. unsigned StubOffset = StubAddress - BaseAddress;
  1397. Stubs[Value] = StubOffset;
  1398. createStubFunction((uint8_t *)StubAddress);
  1399. RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
  1400. Value.Offset);
  1401. if (Value.SymbolName)
  1402. addRelocationForSymbol(RE, Value.SymbolName);
  1403. else
  1404. addRelocationForSection(RE, Value.SectionID);
  1405. Section.StubOffset = StubOffset + getMaxStubSize();
  1406. }
  1407. if (RelType == ELF::R_390_GOTENT)
  1408. resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
  1409. Addend);
  1410. else
  1411. resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
  1412. } else if (Arch == Triple::x86_64) {
  1413. if (RelType == ELF::R_X86_64_PLT32) {
  1414. // The way the PLT relocations normally work is that the linker allocates
  1415. // the
  1416. // PLT and this relocation makes a PC-relative call into the PLT. The PLT
  1417. // entry will then jump to an address provided by the GOT. On first call,
  1418. // the
  1419. // GOT address will point back into PLT code that resolves the symbol. After
  1420. // the first call, the GOT entry points to the actual function.
  1421. //
  1422. // For local functions we're ignoring all of that here and just replacing
  1423. // the PLT32 relocation type with PC32, which will translate the relocation
  1424. // into a PC-relative call directly to the function. For external symbols we
  1425. // can't be sure the function will be within 2^32 bytes of the call site, so
  1426. // we need to create a stub, which calls into the GOT. This case is
  1427. // equivalent to the usual PLT implementation except that we use the stub
  1428. // mechanism in RuntimeDyld (which puts stubs at the end of the section)
  1429. // rather than allocating a PLT section.
  1430. if (Value.SymbolName) {
  1431. // This is a call to an external function.
  1432. // Look for an existing stub.
  1433. SectionEntry &Section = Sections[SectionID];
  1434. StubMap::const_iterator i = Stubs.find(Value);
  1435. uintptr_t StubAddress;
  1436. if (i != Stubs.end()) {
  1437. StubAddress = uintptr_t(Section.Address) + i->second;
  1438. DEBUG(dbgs() << " Stub function found\n");
  1439. } else {
  1440. // Create a new stub function (equivalent to a PLT entry).
  1441. DEBUG(dbgs() << " Create a new stub function\n");
  1442. uintptr_t BaseAddress = uintptr_t(Section.Address);
  1443. uintptr_t StubAlignment = getStubAlignment();
  1444. StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
  1445. -StubAlignment;
  1446. unsigned StubOffset = StubAddress - BaseAddress;
  1447. Stubs[Value] = StubOffset;
  1448. createStubFunction((uint8_t *)StubAddress);
  1449. // Bump our stub offset counter
  1450. Section.StubOffset = StubOffset + getMaxStubSize();
  1451. // Allocate a GOT Entry
  1452. uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);
  1453. // The load of the GOT address has an addend of -4
  1454. resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4);
  1455. // Fill in the value of the symbol we're targeting into the GOT
  1456. addRelocationForSymbol(computeGOTOffsetRE(SectionID,GOTOffset,0,ELF::R_X86_64_64),
  1457. Value.SymbolName);
  1458. }
  1459. // Make the target call a call into the stub table.
  1460. resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
  1461. Addend);
  1462. } else {
  1463. RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
  1464. Value.Offset);
  1465. addRelocationForSection(RE, Value.SectionID);
  1466. }
  1467. } else if (RelType == ELF::R_X86_64_GOTPCREL) {
  1468. uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);
  1469. resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend);
  1470. // Fill in the value of the symbol we're targeting into the GOT
  1471. RelocationEntry RE = computeGOTOffsetRE(SectionID, GOTOffset, Value.Offset, ELF::R_X86_64_64);
  1472. if (Value.SymbolName)
  1473. addRelocationForSymbol(RE, Value.SymbolName);
  1474. else
  1475. addRelocationForSection(RE, Value.SectionID);
  1476. } else if (RelType == ELF::R_X86_64_PC32) {
  1477. Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
  1478. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1479. } else if (RelType == ELF::R_X86_64_PC64) {
  1480. Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
  1481. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1482. } else {
  1483. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1484. }
  1485. } else {
  1486. if (Arch == Triple::x86) {
  1487. Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
  1488. }
  1489. processSimpleRelocation(SectionID, Offset, RelType, Value);
  1490. }
  1491. return ++RelI;
  1492. }
  1493. size_t RuntimeDyldELF::getGOTEntrySize() {
  1494. // We don't use the GOT in all of these cases, but it's essentially free
  1495. // to put them all here.
  1496. size_t Result = 0;
  1497. switch (Arch) {
  1498. case Triple::x86_64:
  1499. case Triple::aarch64:
  1500. case Triple::aarch64_be:
  1501. case Triple::ppc64:
  1502. case Triple::ppc64le:
  1503. case Triple::systemz:
  1504. Result = sizeof(uint64_t);
  1505. break;
  1506. case Triple::x86:
  1507. case Triple::arm:
  1508. case Triple::thumb:
  1509. Result = sizeof(uint32_t);
  1510. break;
  1511. case Triple::mips:
  1512. case Triple::mipsel:
  1513. case Triple::mips64:
  1514. case Triple::mips64el:
  1515. if (IsMipsO32ABI)
  1516. Result = sizeof(uint32_t);
  1517. else if (IsMipsN64ABI)
  1518. Result = sizeof(uint64_t);
  1519. else
  1520. llvm_unreachable("Mips ABI not handled");
  1521. break;
  1522. default:
  1523. llvm_unreachable("Unsupported CPU type!");
  1524. }
  1525. return Result;
  1526. }
  1527. uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned SectionID, unsigned no)
  1528. {
  1529. (void)SectionID; // The GOT Section is the same for all section in the object file
  1530. if (GOTSectionID == 0) {
  1531. GOTSectionID = Sections.size();
  1532. // Reserve a section id. We'll allocate the section later
  1533. // once we know the total size
  1534. Sections.push_back(SectionEntry(".got", 0, 0, 0));
  1535. }
  1536. uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
  1537. CurrentGOTIndex += no;
  1538. return StartOffset;
  1539. }
  1540. void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, uint64_t GOTOffset)
  1541. {
  1542. // Fill in the relative address of the GOT Entry into the stub
  1543. RelocationEntry GOTRE(SectionID, Offset, ELF::R_X86_64_PC32, GOTOffset);
  1544. addRelocationForSection(GOTRE, GOTSectionID);
  1545. }
  1546. RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(unsigned SectionID, uint64_t GOTOffset, uint64_t SymbolOffset,
  1547. uint32_t Type)
  1548. {
  1549. (void)SectionID; // The GOT Section is the same for all section in the object file
  1550. return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
  1551. }
  1552. void RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
  1553. ObjSectionToIDMap &SectionMap) {
  1554. // If necessary, allocate the global offset table
  1555. if (GOTSectionID != 0) {
  1556. // Allocate memory for the section
  1557. size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
  1558. uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
  1559. GOTSectionID, ".got", false);
  1560. if (!Addr)
  1561. report_fatal_error("Unable to allocate memory for GOT!");
  1562. Sections[GOTSectionID] = SectionEntry(".got", Addr, TotalSize, 0);
  1563. if (Checker)
  1564. Checker->registerSection(Obj.getFileName(), GOTSectionID);
  1565. // For now, initialize all GOT entries to zero. We'll fill them in as
  1566. // needed when GOT-based relocations are applied.
  1567. memset(Addr, 0, TotalSize);
  1568. if (IsMipsN64ABI) {
  1569. // To correctly resolve Mips GOT relocations, we need a mapping from
  1570. // object's sections to GOTs.
  1571. for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
  1572. SI != SE; ++SI) {
  1573. if (SI->relocation_begin() != SI->relocation_end()) {
  1574. section_iterator RelocatedSection = SI->getRelocatedSection();
  1575. ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
  1576. assert (i != SectionMap.end());
  1577. SectionToGOTMap[i->second] = GOTSectionID;
  1578. }
  1579. }
  1580. GOTSymbolOffsets.clear();
  1581. }
  1582. }
  1583. // Look for and record the EH frame section.
  1584. ObjSectionToIDMap::iterator i, e;
  1585. for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
  1586. const SectionRef &Section = i->first;
  1587. StringRef Name;
  1588. Section.getName(Name);
  1589. if (Name == ".eh_frame") {
  1590. UnregisteredEHFrameSections.push_back(i->second);
  1591. break;
  1592. }
  1593. }
  1594. GOTSectionID = 0;
  1595. CurrentGOTIndex = 0;
  1596. }
  1597. bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
  1598. return Obj.isELF();
  1599. }
  1600. } // namespace llvm