ELF_riscv.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //===------- ELF_riscv.cpp -JIT linker implementation for ELF/riscv -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // ELF/riscv jit-link implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h"
  13. #include "ELFLinkGraphBuilder.h"
  14. #include "JITLinkGeneric.h"
  15. #include "PerGraphGOTAndPLTStubsBuilder.h"
  16. #include "llvm/BinaryFormat/ELF.h"
  17. #include "llvm/ExecutionEngine/JITLink/JITLink.h"
  18. #include "llvm/ExecutionEngine/JITLink/riscv.h"
  19. #include "llvm/Object/ELF.h"
  20. #include "llvm/Object/ELFObjectFile.h"
  21. #include "llvm/Support/Endian.h"
  22. #define DEBUG_TYPE "jitlink"
  23. using namespace llvm;
  24. using namespace llvm::jitlink;
  25. using namespace llvm::jitlink::riscv;
  26. namespace {
  27. class PerGraphGOTAndPLTStubsBuilder_ELF_riscv
  28. : public PerGraphGOTAndPLTStubsBuilder<
  29. PerGraphGOTAndPLTStubsBuilder_ELF_riscv> {
  30. public:
  31. static constexpr size_t StubEntrySize = 16;
  32. static const uint8_t NullGOTEntryContent[8];
  33. static const uint8_t RV64StubContent[StubEntrySize];
  34. static const uint8_t RV32StubContent[StubEntrySize];
  35. using PerGraphGOTAndPLTStubsBuilder<
  36. PerGraphGOTAndPLTStubsBuilder_ELF_riscv>::PerGraphGOTAndPLTStubsBuilder;
  37. bool isRV64() const { return G.getPointerSize() == 8; }
  38. bool isGOTEdgeToFix(Edge &E) const { return E.getKind() == R_RISCV_GOT_HI20; }
  39. Symbol &createGOTEntry(Symbol &Target) {
  40. Block &GOTBlock =
  41. G.createContentBlock(getGOTSection(), getGOTEntryBlockContent(),
  42. orc::ExecutorAddr(), G.getPointerSize(), 0);
  43. GOTBlock.addEdge(isRV64() ? R_RISCV_64 : R_RISCV_32, 0, Target, 0);
  44. return G.addAnonymousSymbol(GOTBlock, 0, G.getPointerSize(), false, false);
  45. }
  46. Symbol &createPLTStub(Symbol &Target) {
  47. Block &StubContentBlock = G.createContentBlock(
  48. getStubsSection(), getStubBlockContent(), orc::ExecutorAddr(), 4, 0);
  49. auto &GOTEntrySymbol = getGOTEntry(Target);
  50. StubContentBlock.addEdge(R_RISCV_CALL, 0, GOTEntrySymbol, 0);
  51. return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true,
  52. false);
  53. }
  54. void fixGOTEdge(Edge &E, Symbol &GOTEntry) {
  55. // Replace the relocation pair (R_RISCV_GOT_HI20, R_RISCV_PCREL_LO12)
  56. // with (R_RISCV_PCREL_HI20, R_RISCV_PCREL_LO12)
  57. // Therefore, here just change the R_RISCV_GOT_HI20 to R_RISCV_PCREL_HI20
  58. E.setKind(R_RISCV_PCREL_HI20);
  59. E.setTarget(GOTEntry);
  60. }
  61. void fixPLTEdge(Edge &E, Symbol &PLTStubs) {
  62. assert(E.getKind() == R_RISCV_CALL_PLT && "Not a R_RISCV_CALL_PLT edge?");
  63. E.setKind(R_RISCV_CALL);
  64. E.setTarget(PLTStubs);
  65. }
  66. bool isExternalBranchEdge(Edge &E) const {
  67. return E.getKind() == R_RISCV_CALL_PLT;
  68. }
  69. private:
  70. Section &getGOTSection() const {
  71. if (!GOTSection)
  72. GOTSection = &G.createSection("$__GOT", MemProt::Read);
  73. return *GOTSection;
  74. }
  75. Section &getStubsSection() const {
  76. if (!StubsSection)
  77. StubsSection =
  78. &G.createSection("$__STUBS", MemProt::Read | MemProt::Exec);
  79. return *StubsSection;
  80. }
  81. ArrayRef<char> getGOTEntryBlockContent() {
  82. return {reinterpret_cast<const char *>(NullGOTEntryContent),
  83. G.getPointerSize()};
  84. }
  85. ArrayRef<char> getStubBlockContent() {
  86. auto StubContent = isRV64() ? RV64StubContent : RV32StubContent;
  87. return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
  88. }
  89. mutable Section *GOTSection = nullptr;
  90. mutable Section *StubsSection = nullptr;
  91. };
  92. const uint8_t PerGraphGOTAndPLTStubsBuilder_ELF_riscv::NullGOTEntryContent[8] =
  93. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  94. const uint8_t
  95. PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV64StubContent[StubEntrySize] = {
  96. 0x17, 0x0e, 0x00, 0x00, // auipc t3, literal
  97. 0x03, 0x3e, 0x0e, 0x00, // ld t3, literal(t3)
  98. 0x67, 0x00, 0x0e, 0x00, // jr t3
  99. 0x13, 0x00, 0x00, 0x00}; // nop
  100. const uint8_t
  101. PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV32StubContent[StubEntrySize] = {
  102. 0x17, 0x0e, 0x00, 0x00, // auipc t3, literal
  103. 0x03, 0x2e, 0x0e, 0x00, // lw t3, literal(t3)
  104. 0x67, 0x00, 0x0e, 0x00, // jr t3
  105. 0x13, 0x00, 0x00, 0x00}; // nop
  106. } // namespace
  107. namespace llvm {
  108. namespace jitlink {
  109. static Expected<const Edge &> getRISCVPCRelHi20(const Edge &E) {
  110. using namespace riscv;
  111. assert((E.getKind() == R_RISCV_PCREL_LO12_I ||
  112. E.getKind() == R_RISCV_PCREL_LO12_S) &&
  113. "Can only have high relocation for R_RISCV_PCREL_LO12_I or "
  114. "R_RISCV_PCREL_LO12_S");
  115. const Symbol &Sym = E.getTarget();
  116. const Block &B = Sym.getBlock();
  117. orc::ExecutorAddrDiff Offset = Sym.getOffset();
  118. struct Comp {
  119. bool operator()(const Edge &Lhs, orc::ExecutorAddrDiff Offset) {
  120. return Lhs.getOffset() < Offset;
  121. }
  122. bool operator()(orc::ExecutorAddrDiff Offset, const Edge &Rhs) {
  123. return Offset < Rhs.getOffset();
  124. }
  125. };
  126. auto Bound =
  127. std::equal_range(B.edges().begin(), B.edges().end(), Offset, Comp{});
  128. for (auto It = Bound.first; It != Bound.second; ++It) {
  129. if (It->getKind() == R_RISCV_PCREL_HI20)
  130. return *It;
  131. }
  132. return make_error<JITLinkError>(
  133. "No HI20 PCREL relocation type be found for LO12 PCREL relocation type");
  134. }
  135. static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) {
  136. return (Num & (((1ULL << (Size + 1)) - 1) << Low)) >> Low;
  137. }
  138. inline Error checkAlignment(llvm::orc::ExecutorAddr loc, uint64_t v, int n,
  139. const Edge &E) {
  140. if (v & (n - 1))
  141. return make_error<JITLinkError>("0x" + llvm::utohexstr(loc.getValue()) +
  142. " improper alignment for relocation " +
  143. formatv("{0:d}", E.getKind()) + ": 0x" +
  144. llvm::utohexstr(v) + " is not aligned to " +
  145. Twine(n) + " bytes");
  146. return Error::success();
  147. }
  148. static inline bool isInRangeForImmS32(int64_t Value) {
  149. return (Value >= std::numeric_limits<int32_t>::min() &&
  150. Value <= std::numeric_limits<int32_t>::max());
  151. }
  152. class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
  153. friend class JITLinker<ELFJITLinker_riscv>;
  154. public:
  155. ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx,
  156. std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
  157. : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
  158. private:
  159. Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
  160. using namespace riscv;
  161. using namespace llvm::support;
  162. char *BlockWorkingMem = B.getAlreadyMutableContent().data();
  163. char *FixupPtr = BlockWorkingMem + E.getOffset();
  164. orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
  165. switch (E.getKind()) {
  166. case R_RISCV_32: {
  167. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  168. *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
  169. break;
  170. }
  171. case R_RISCV_64: {
  172. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  173. *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
  174. break;
  175. }
  176. case R_RISCV_BRANCH: {
  177. int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
  178. Error AlignmentIssue = checkAlignment(FixupAddress, Value, 2, E);
  179. if (AlignmentIssue) {
  180. return AlignmentIssue;
  181. }
  182. int64_t Lo = Value & 0xFFF;
  183. uint32_t Imm31_25 = extractBits(Lo, 5, 6) << 25 | extractBits(Lo, 12, 1)
  184. << 31;
  185. uint32_t Imm11_7 = extractBits(Lo, 1, 4) << 8 | extractBits(Lo, 11, 1)
  186. << 7;
  187. uint32_t RawInstr = *(little32_t *)FixupPtr;
  188. *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
  189. break;
  190. }
  191. case R_RISCV_HI20: {
  192. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  193. int64_t Hi = Value + 0x800;
  194. if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
  195. return makeTargetOutOfRangeError(G, B, E);
  196. uint32_t RawInstr = *(little32_t *)FixupPtr;
  197. *(little32_t *)FixupPtr =
  198. (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
  199. break;
  200. }
  201. case R_RISCV_LO12_I: {
  202. // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
  203. // with current relocation R_RISCV_LO12_I. So here may need a check.
  204. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  205. int32_t Lo = Value & 0xFFF;
  206. uint32_t RawInstr = *(little32_t *)FixupPtr;
  207. *(little32_t *)FixupPtr =
  208. (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
  209. break;
  210. }
  211. case R_RISCV_CALL: {
  212. int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
  213. int64_t Hi = Value + 0x800;
  214. if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
  215. return makeTargetOutOfRangeError(G, B, E);
  216. int32_t Lo = Value & 0xFFF;
  217. uint32_t RawInstrAuipc = *(little32_t *)FixupPtr;
  218. uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4);
  219. *(little32_t *)FixupPtr =
  220. RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000));
  221. *(little32_t *)(FixupPtr + 4) =
  222. RawInstrJalr | (static_cast<uint32_t>(Lo) << 20);
  223. break;
  224. }
  225. case R_RISCV_PCREL_HI20: {
  226. int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
  227. int64_t Hi = Value + 0x800;
  228. if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
  229. return makeTargetOutOfRangeError(G, B, E);
  230. uint32_t RawInstr = *(little32_t *)FixupPtr;
  231. *(little32_t *)FixupPtr =
  232. (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
  233. break;
  234. }
  235. case R_RISCV_PCREL_LO12_I: {
  236. // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
  237. // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a
  238. // check.
  239. auto RelHI20 = getRISCVPCRelHi20(E);
  240. if (!RelHI20)
  241. return RelHI20.takeError();
  242. int64_t Value = RelHI20->getTarget().getAddress() +
  243. RelHI20->getAddend() - E.getTarget().getAddress();
  244. int64_t Lo = Value & 0xFFF;
  245. uint32_t RawInstr = *(little32_t *)FixupPtr;
  246. *(little32_t *)FixupPtr =
  247. (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
  248. break;
  249. }
  250. case R_RISCV_PCREL_LO12_S: {
  251. // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
  252. // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a
  253. // check.
  254. auto RelHI20 = getRISCVPCRelHi20(E);
  255. int64_t Value = RelHI20->getTarget().getAddress() +
  256. RelHI20->getAddend() - E.getTarget().getAddress();
  257. int64_t Lo = Value & 0xFFF;
  258. uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
  259. uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
  260. uint32_t RawInstr = *(little32_t *)FixupPtr;
  261. *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
  262. break;
  263. }
  264. case R_RISCV_ADD64: {
  265. int64_t Value = (E.getTarget().getAddress() +
  266. support::endian::read64le(reinterpret_cast<const void *>(
  267. FixupAddress.getValue())) +
  268. E.getAddend())
  269. .getValue();
  270. *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
  271. break;
  272. }
  273. case R_RISCV_ADD32: {
  274. int64_t Value = (E.getTarget().getAddress() +
  275. support::endian::read32le(reinterpret_cast<const void *>(
  276. FixupAddress.getValue())) +
  277. E.getAddend())
  278. .getValue();
  279. *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
  280. break;
  281. }
  282. case R_RISCV_ADD16: {
  283. int64_t Value = (E.getTarget().getAddress() +
  284. support::endian::read16le(reinterpret_cast<const void *>(
  285. FixupAddress.getValue())) +
  286. E.getAddend())
  287. .getValue();
  288. *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
  289. break;
  290. }
  291. case R_RISCV_ADD8: {
  292. int64_t Value =
  293. (E.getTarget().getAddress() +
  294. *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) +
  295. E.getAddend())
  296. .getValue();
  297. *FixupPtr = static_cast<uint8_t>(Value);
  298. break;
  299. }
  300. case R_RISCV_SUB64: {
  301. int64_t Value = support::endian::read64le(reinterpret_cast<const void *>(
  302. FixupAddress.getValue())) -
  303. E.getTarget().getAddress().getValue() - E.getAddend();
  304. *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
  305. break;
  306. }
  307. case R_RISCV_SUB32: {
  308. int64_t Value = support::endian::read32le(reinterpret_cast<const void *>(
  309. FixupAddress.getValue())) -
  310. E.getTarget().getAddress().getValue() - E.getAddend();
  311. *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
  312. break;
  313. }
  314. case R_RISCV_SUB16: {
  315. int64_t Value = support::endian::read16le(reinterpret_cast<const void *>(
  316. FixupAddress.getValue())) -
  317. E.getTarget().getAddress().getValue() - E.getAddend();
  318. *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
  319. break;
  320. }
  321. case R_RISCV_SUB8: {
  322. int64_t Value =
  323. *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) -
  324. E.getTarget().getAddress().getValue() - E.getAddend();
  325. *FixupPtr = static_cast<uint8_t>(Value);
  326. break;
  327. }
  328. case R_RISCV_SET6: {
  329. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  330. uint32_t RawData = *(little32_t *)FixupPtr;
  331. int64_t Word6 = Value & 0x3f;
  332. *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6;
  333. break;
  334. }
  335. case R_RISCV_SET8: {
  336. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  337. uint32_t RawData = *(little32_t *)FixupPtr;
  338. int64_t Word8 = Value & 0xff;
  339. *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8;
  340. break;
  341. }
  342. case R_RISCV_SET16: {
  343. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  344. uint32_t RawData = *(little32_t *)FixupPtr;
  345. int64_t Word16 = Value & 0xffff;
  346. *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16;
  347. break;
  348. }
  349. case R_RISCV_SET32: {
  350. int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
  351. int64_t Word32 = Value & 0xffffffff;
  352. *(little32_t *)FixupPtr = Word32;
  353. break;
  354. }
  355. case R_RISCV_32_PCREL: {
  356. int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
  357. int64_t Word32 = Value & 0xffffffff;
  358. *(little32_t *)FixupPtr = Word32;
  359. break;
  360. }
  361. }
  362. return Error::success();
  363. }
  364. };
  365. template <typename ELFT>
  366. class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
  367. private:
  368. static Expected<riscv::EdgeKind_riscv>
  369. getRelocationKind(const uint32_t Type) {
  370. using namespace riscv;
  371. switch (Type) {
  372. case ELF::R_RISCV_32:
  373. return EdgeKind_riscv::R_RISCV_32;
  374. case ELF::R_RISCV_64:
  375. return EdgeKind_riscv::R_RISCV_64;
  376. case ELF::R_RISCV_BRANCH:
  377. return EdgeKind_riscv::R_RISCV_BRANCH;
  378. case ELF::R_RISCV_HI20:
  379. return EdgeKind_riscv::R_RISCV_HI20;
  380. case ELF::R_RISCV_LO12_I:
  381. return EdgeKind_riscv::R_RISCV_LO12_I;
  382. case ELF::R_RISCV_CALL:
  383. return EdgeKind_riscv::R_RISCV_CALL;
  384. case ELF::R_RISCV_PCREL_HI20:
  385. return EdgeKind_riscv::R_RISCV_PCREL_HI20;
  386. case ELF::R_RISCV_PCREL_LO12_I:
  387. return EdgeKind_riscv::R_RISCV_PCREL_LO12_I;
  388. case ELF::R_RISCV_PCREL_LO12_S:
  389. return EdgeKind_riscv::R_RISCV_PCREL_LO12_S;
  390. case ELF::R_RISCV_GOT_HI20:
  391. return EdgeKind_riscv::R_RISCV_GOT_HI20;
  392. case ELF::R_RISCV_CALL_PLT:
  393. return EdgeKind_riscv::R_RISCV_CALL_PLT;
  394. case ELF::R_RISCV_ADD64:
  395. return EdgeKind_riscv::R_RISCV_ADD64;
  396. case ELF::R_RISCV_ADD32:
  397. return EdgeKind_riscv::R_RISCV_ADD32;
  398. case ELF::R_RISCV_ADD16:
  399. return EdgeKind_riscv::R_RISCV_ADD16;
  400. case ELF::R_RISCV_ADD8:
  401. return EdgeKind_riscv::R_RISCV_ADD8;
  402. case ELF::R_RISCV_SUB64:
  403. return EdgeKind_riscv::R_RISCV_SUB64;
  404. case ELF::R_RISCV_SUB32:
  405. return EdgeKind_riscv::R_RISCV_SUB32;
  406. case ELF::R_RISCV_SUB16:
  407. return EdgeKind_riscv::R_RISCV_SUB16;
  408. case ELF::R_RISCV_SUB8:
  409. return EdgeKind_riscv::R_RISCV_SUB8;
  410. case ELF::R_RISCV_SET6:
  411. return EdgeKind_riscv::R_RISCV_SET6;
  412. case ELF::R_RISCV_SET8:
  413. return EdgeKind_riscv::R_RISCV_SET8;
  414. case ELF::R_RISCV_SET16:
  415. return EdgeKind_riscv::R_RISCV_SET16;
  416. case ELF::R_RISCV_SET32:
  417. return EdgeKind_riscv::R_RISCV_SET32;
  418. case ELF::R_RISCV_32_PCREL:
  419. return EdgeKind_riscv::R_RISCV_32_PCREL;
  420. }
  421. return make_error<JITLinkError>("Unsupported riscv relocation:" +
  422. formatv("{0:d}", Type));
  423. }
  424. Error addRelocations() override {
  425. LLVM_DEBUG(dbgs() << "Processing relocations:\n");
  426. using Base = ELFLinkGraphBuilder<ELFT>;
  427. using Self = ELFLinkGraphBuilder_riscv<ELFT>;
  428. for (const auto &RelSect : Base::Sections)
  429. if (Error Err = Base::forEachRelocation(RelSect, this,
  430. &Self::addSingleRelocation))
  431. return Err;
  432. return Error::success();
  433. }
  434. Error addSingleRelocation(const typename ELFT::Rela &Rel,
  435. const typename ELFT::Shdr &FixupSect,
  436. Block &BlockToFix) {
  437. using Base = ELFLinkGraphBuilder<ELFT>;
  438. uint32_t SymbolIndex = Rel.getSymbol(false);
  439. auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
  440. if (!ObjSymbol)
  441. return ObjSymbol.takeError();
  442. Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
  443. if (!GraphSymbol)
  444. return make_error<StringError>(
  445. formatv("Could not find symbol at given index, did you add it to "
  446. "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
  447. SymbolIndex, (*ObjSymbol)->st_shndx,
  448. Base::GraphSymbols.size()),
  449. inconvertibleErrorCode());
  450. uint32_t Type = Rel.getType(false);
  451. Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type);
  452. if (!Kind)
  453. return Kind.takeError();
  454. int64_t Addend = Rel.r_addend;
  455. auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
  456. Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
  457. Edge GE(*Kind, Offset, *GraphSymbol, Addend);
  458. LLVM_DEBUG({
  459. dbgs() << " ";
  460. printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind));
  461. dbgs() << "\n";
  462. });
  463. BlockToFix.addEdge(std::move(GE));
  464. return Error::success();
  465. }
  466. public:
  467. ELFLinkGraphBuilder_riscv(StringRef FileName,
  468. const object::ELFFile<ELFT> &Obj, const Triple T)
  469. : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
  470. riscv::getEdgeKindName) {}
  471. };
  472. Expected<std::unique_ptr<LinkGraph>>
  473. createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) {
  474. LLVM_DEBUG({
  475. dbgs() << "Building jitlink graph for new input "
  476. << ObjectBuffer.getBufferIdentifier() << "...\n";
  477. });
  478. auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
  479. if (!ELFObj)
  480. return ELFObj.takeError();
  481. if ((*ELFObj)->getArch() == Triple::riscv64) {
  482. auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
  483. return ELFLinkGraphBuilder_riscv<object::ELF64LE>(
  484. (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
  485. (*ELFObj)->makeTriple())
  486. .buildGraph();
  487. } else {
  488. assert((*ELFObj)->getArch() == Triple::riscv32 &&
  489. "Invalid triple for RISCV ELF object file");
  490. auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
  491. return ELFLinkGraphBuilder_riscv<object::ELF32LE>(
  492. (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
  493. (*ELFObj)->makeTriple())
  494. .buildGraph();
  495. }
  496. }
  497. void link_ELF_riscv(std::unique_ptr<LinkGraph> G,
  498. std::unique_ptr<JITLinkContext> Ctx) {
  499. PassConfiguration Config;
  500. const Triple &TT = G->getTargetTriple();
  501. if (Ctx->shouldAddDefaultTargetPasses(TT)) {
  502. if (auto MarkLive = Ctx->getMarkLivePass(TT))
  503. Config.PrePrunePasses.push_back(std::move(MarkLive));
  504. else
  505. Config.PrePrunePasses.push_back(markAllSymbolsLive);
  506. Config.PostPrunePasses.push_back(
  507. PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass);
  508. }
  509. if (auto Err = Ctx->modifyPassConfig(*G, Config))
  510. return Ctx->notifyFailed(std::move(Err));
  511. ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config));
  512. }
  513. } // namespace jitlink
  514. } // namespace llvm