xref: /freebsd-src/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1fe6060f1SDimitry Andric //===------- ELF_riscv.cpp -JIT linker implementation for ELF/riscv -------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric //
9fe6060f1SDimitry Andric // ELF/riscv jit-link implementation.
10fe6060f1SDimitry Andric //
11fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h"
14349cc55cSDimitry Andric #include "ELFLinkGraphBuilder.h"
15349cc55cSDimitry Andric #include "JITLinkGeneric.h"
16349cc55cSDimitry Andric #include "PerGraphGOTAndPLTStubsBuilder.h"
17349cc55cSDimitry Andric #include "llvm/BinaryFormat/ELF.h"
18fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITLink/JITLink.h"
19fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITLink/riscv.h"
20fe6060f1SDimitry Andric #include "llvm/Object/ELF.h"
21fe6060f1SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
2204eeddc0SDimitry Andric #include "llvm/Support/Endian.h"
23fe6060f1SDimitry Andric 
24fe6060f1SDimitry Andric #define DEBUG_TYPE "jitlink"
25fe6060f1SDimitry Andric using namespace llvm;
26349cc55cSDimitry Andric using namespace llvm::jitlink;
27349cc55cSDimitry Andric using namespace llvm::jitlink::riscv;
28fe6060f1SDimitry Andric 
29349cc55cSDimitry Andric namespace {
30349cc55cSDimitry Andric 
31349cc55cSDimitry Andric class PerGraphGOTAndPLTStubsBuilder_ELF_riscv
32349cc55cSDimitry Andric     : public PerGraphGOTAndPLTStubsBuilder<
33349cc55cSDimitry Andric           PerGraphGOTAndPLTStubsBuilder_ELF_riscv> {
34349cc55cSDimitry Andric public:
35349cc55cSDimitry Andric   static constexpr size_t StubEntrySize = 16;
36349cc55cSDimitry Andric   static const uint8_t NullGOTEntryContent[8];
37349cc55cSDimitry Andric   static const uint8_t RV64StubContent[StubEntrySize];
38349cc55cSDimitry Andric   static const uint8_t RV32StubContent[StubEntrySize];
39349cc55cSDimitry Andric 
40349cc55cSDimitry Andric   using PerGraphGOTAndPLTStubsBuilder<
41349cc55cSDimitry Andric       PerGraphGOTAndPLTStubsBuilder_ELF_riscv>::PerGraphGOTAndPLTStubsBuilder;
42349cc55cSDimitry Andric 
43349cc55cSDimitry Andric   bool isRV64() const { return G.getPointerSize() == 8; }
44349cc55cSDimitry Andric 
45349cc55cSDimitry Andric   bool isGOTEdgeToFix(Edge &E) const { return E.getKind() == R_RISCV_GOT_HI20; }
46349cc55cSDimitry Andric 
47349cc55cSDimitry Andric   Symbol &createGOTEntry(Symbol &Target) {
4804eeddc0SDimitry Andric     Block &GOTBlock =
4904eeddc0SDimitry Andric         G.createContentBlock(getGOTSection(), getGOTEntryBlockContent(),
5004eeddc0SDimitry Andric                              orc::ExecutorAddr(), G.getPointerSize(), 0);
51349cc55cSDimitry Andric     GOTBlock.addEdge(isRV64() ? R_RISCV_64 : R_RISCV_32, 0, Target, 0);
52349cc55cSDimitry Andric     return G.addAnonymousSymbol(GOTBlock, 0, G.getPointerSize(), false, false);
53349cc55cSDimitry Andric   }
54349cc55cSDimitry Andric 
55349cc55cSDimitry Andric   Symbol &createPLTStub(Symbol &Target) {
5604eeddc0SDimitry Andric     Block &StubContentBlock = G.createContentBlock(
5704eeddc0SDimitry Andric         getStubsSection(), getStubBlockContent(), orc::ExecutorAddr(), 4, 0);
58349cc55cSDimitry Andric     auto &GOTEntrySymbol = getGOTEntry(Target);
59349cc55cSDimitry Andric     StubContentBlock.addEdge(R_RISCV_CALL, 0, GOTEntrySymbol, 0);
60349cc55cSDimitry Andric     return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true,
61349cc55cSDimitry Andric                                 false);
62349cc55cSDimitry Andric   }
63349cc55cSDimitry Andric 
64349cc55cSDimitry Andric   void fixGOTEdge(Edge &E, Symbol &GOTEntry) {
65349cc55cSDimitry Andric     // Replace the relocation pair (R_RISCV_GOT_HI20, R_RISCV_PCREL_LO12)
66349cc55cSDimitry Andric     // with (R_RISCV_PCREL_HI20, R_RISCV_PCREL_LO12)
67349cc55cSDimitry Andric     // Therefore, here just change the R_RISCV_GOT_HI20 to R_RISCV_PCREL_HI20
68349cc55cSDimitry Andric     E.setKind(R_RISCV_PCREL_HI20);
69349cc55cSDimitry Andric     E.setTarget(GOTEntry);
70349cc55cSDimitry Andric   }
71349cc55cSDimitry Andric 
72349cc55cSDimitry Andric   void fixPLTEdge(Edge &E, Symbol &PLTStubs) {
73349cc55cSDimitry Andric     assert(E.getKind() == R_RISCV_CALL_PLT && "Not a R_RISCV_CALL_PLT edge?");
74349cc55cSDimitry Andric     E.setKind(R_RISCV_CALL);
75349cc55cSDimitry Andric     E.setTarget(PLTStubs);
76349cc55cSDimitry Andric   }
77349cc55cSDimitry Andric 
78349cc55cSDimitry Andric   bool isExternalBranchEdge(Edge &E) const {
79349cc55cSDimitry Andric     return E.getKind() == R_RISCV_CALL_PLT;
80349cc55cSDimitry Andric   }
81349cc55cSDimitry Andric 
82349cc55cSDimitry Andric private:
83349cc55cSDimitry Andric   Section &getGOTSection() const {
84349cc55cSDimitry Andric     if (!GOTSection)
85*bdd1243dSDimitry Andric       GOTSection = &G.createSection("$__GOT", orc::MemProt::Read);
86349cc55cSDimitry Andric     return *GOTSection;
87349cc55cSDimitry Andric   }
88349cc55cSDimitry Andric 
89349cc55cSDimitry Andric   Section &getStubsSection() const {
90349cc55cSDimitry Andric     if (!StubsSection)
91349cc55cSDimitry Andric       StubsSection =
92*bdd1243dSDimitry Andric           &G.createSection("$__STUBS", orc::MemProt::Read | orc::MemProt::Exec);
93349cc55cSDimitry Andric     return *StubsSection;
94349cc55cSDimitry Andric   }
95349cc55cSDimitry Andric 
96349cc55cSDimitry Andric   ArrayRef<char> getGOTEntryBlockContent() {
97349cc55cSDimitry Andric     return {reinterpret_cast<const char *>(NullGOTEntryContent),
98349cc55cSDimitry Andric             G.getPointerSize()};
99349cc55cSDimitry Andric   }
100349cc55cSDimitry Andric 
101349cc55cSDimitry Andric   ArrayRef<char> getStubBlockContent() {
102349cc55cSDimitry Andric     auto StubContent = isRV64() ? RV64StubContent : RV32StubContent;
103349cc55cSDimitry Andric     return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
104349cc55cSDimitry Andric   }
105349cc55cSDimitry Andric 
106349cc55cSDimitry Andric   mutable Section *GOTSection = nullptr;
107349cc55cSDimitry Andric   mutable Section *StubsSection = nullptr;
108349cc55cSDimitry Andric };
109349cc55cSDimitry Andric 
110349cc55cSDimitry Andric const uint8_t PerGraphGOTAndPLTStubsBuilder_ELF_riscv::NullGOTEntryContent[8] =
111349cc55cSDimitry Andric     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
112349cc55cSDimitry Andric 
113349cc55cSDimitry Andric const uint8_t
114349cc55cSDimitry Andric     PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV64StubContent[StubEntrySize] = {
115349cc55cSDimitry Andric         0x17, 0x0e, 0x00, 0x00,  // auipc t3, literal
116349cc55cSDimitry Andric         0x03, 0x3e, 0x0e, 0x00,  // ld    t3, literal(t3)
117349cc55cSDimitry Andric         0x67, 0x00, 0x0e, 0x00,  // jr    t3
118349cc55cSDimitry Andric         0x13, 0x00, 0x00, 0x00}; // nop
119349cc55cSDimitry Andric 
120349cc55cSDimitry Andric const uint8_t
121349cc55cSDimitry Andric     PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV32StubContent[StubEntrySize] = {
122349cc55cSDimitry Andric         0x17, 0x0e, 0x00, 0x00,  // auipc t3, literal
123349cc55cSDimitry Andric         0x03, 0x2e, 0x0e, 0x00,  // lw    t3, literal(t3)
124349cc55cSDimitry Andric         0x67, 0x00, 0x0e, 0x00,  // jr    t3
125349cc55cSDimitry Andric         0x13, 0x00, 0x00, 0x00}; // nop
126349cc55cSDimitry Andric } // namespace
127fe6060f1SDimitry Andric namespace llvm {
128fe6060f1SDimitry Andric namespace jitlink {
129fe6060f1SDimitry Andric 
130fe6060f1SDimitry Andric static Expected<const Edge &> getRISCVPCRelHi20(const Edge &E) {
131fe6060f1SDimitry Andric   using namespace riscv;
132fe6060f1SDimitry Andric   assert((E.getKind() == R_RISCV_PCREL_LO12_I ||
133fe6060f1SDimitry Andric           E.getKind() == R_RISCV_PCREL_LO12_S) &&
134fe6060f1SDimitry Andric          "Can only have high relocation for R_RISCV_PCREL_LO12_I or "
135fe6060f1SDimitry Andric          "R_RISCV_PCREL_LO12_S");
136fe6060f1SDimitry Andric 
137fe6060f1SDimitry Andric   const Symbol &Sym = E.getTarget();
138fe6060f1SDimitry Andric   const Block &B = Sym.getBlock();
13904eeddc0SDimitry Andric   orc::ExecutorAddrDiff Offset = Sym.getOffset();
140fe6060f1SDimitry Andric 
141fe6060f1SDimitry Andric   struct Comp {
14204eeddc0SDimitry Andric     bool operator()(const Edge &Lhs, orc::ExecutorAddrDiff Offset) {
143fe6060f1SDimitry Andric       return Lhs.getOffset() < Offset;
144fe6060f1SDimitry Andric     }
14504eeddc0SDimitry Andric     bool operator()(orc::ExecutorAddrDiff Offset, const Edge &Rhs) {
146fe6060f1SDimitry Andric       return Offset < Rhs.getOffset();
147fe6060f1SDimitry Andric     }
148fe6060f1SDimitry Andric   };
149fe6060f1SDimitry Andric 
150fe6060f1SDimitry Andric   auto Bound =
151fe6060f1SDimitry Andric       std::equal_range(B.edges().begin(), B.edges().end(), Offset, Comp{});
152fe6060f1SDimitry Andric 
153fe6060f1SDimitry Andric   for (auto It = Bound.first; It != Bound.second; ++It) {
154fe6060f1SDimitry Andric     if (It->getKind() == R_RISCV_PCREL_HI20)
155fe6060f1SDimitry Andric       return *It;
156fe6060f1SDimitry Andric   }
157fe6060f1SDimitry Andric 
158fe6060f1SDimitry Andric   return make_error<JITLinkError>(
159fe6060f1SDimitry Andric       "No HI20 PCREL relocation type be found for LO12 PCREL relocation type");
160fe6060f1SDimitry Andric }
161fe6060f1SDimitry Andric 
16204eeddc0SDimitry Andric static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) {
16381ad6265SDimitry Andric   return (Num & (((1ULL << Size) - 1) << Low)) >> Low;
16404eeddc0SDimitry Andric }
16504eeddc0SDimitry Andric 
16681ad6265SDimitry Andric static inline bool isAlignmentCorrect(uint64_t Value, int N) {
16781ad6265SDimitry Andric   return (Value & (N - 1)) ? false : true;
16804eeddc0SDimitry Andric }
16904eeddc0SDimitry Andric 
17081ad6265SDimitry Andric // Requires 0 < N <= 64.
17181ad6265SDimitry Andric static inline bool isInRangeForImm(int64_t Value, int N) {
17281ad6265SDimitry Andric   return Value == llvm::SignExtend64(Value, N);
173fe6060f1SDimitry Andric }
174fe6060f1SDimitry Andric 
175fe6060f1SDimitry Andric class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
176fe6060f1SDimitry Andric   friend class JITLinker<ELFJITLinker_riscv>;
177fe6060f1SDimitry Andric 
178fe6060f1SDimitry Andric public:
179fe6060f1SDimitry Andric   ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx,
180fe6060f1SDimitry Andric                      std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
181fe6060f1SDimitry Andric       : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
182fe6060f1SDimitry Andric 
183fe6060f1SDimitry Andric private:
184fe6060f1SDimitry Andric   Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
185fe6060f1SDimitry Andric     using namespace riscv;
186fe6060f1SDimitry Andric     using namespace llvm::support;
187fe6060f1SDimitry Andric 
188fe6060f1SDimitry Andric     char *BlockWorkingMem = B.getAlreadyMutableContent().data();
189fe6060f1SDimitry Andric     char *FixupPtr = BlockWorkingMem + E.getOffset();
19004eeddc0SDimitry Andric     orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
191fe6060f1SDimitry Andric     switch (E.getKind()) {
192349cc55cSDimitry Andric     case R_RISCV_32: {
19304eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
194349cc55cSDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
195349cc55cSDimitry Andric       break;
196349cc55cSDimitry Andric     }
197349cc55cSDimitry Andric     case R_RISCV_64: {
19804eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
199349cc55cSDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
200349cc55cSDimitry Andric       break;
201349cc55cSDimitry Andric     }
20204eeddc0SDimitry Andric     case R_RISCV_BRANCH: {
20304eeddc0SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
20481ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 12)))
20581ad6265SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
20681ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
20781ad6265SDimitry Andric         return makeAlignmentError(FixupAddress, Value, 2, E);
208*bdd1243dSDimitry Andric       uint32_t Imm12 = extractBits(Value, 12, 1) << 31;
209*bdd1243dSDimitry Andric       uint32_t Imm10_5 = extractBits(Value, 5, 6) << 25;
210*bdd1243dSDimitry Andric       uint32_t Imm4_1 = extractBits(Value, 1, 4) << 8;
211*bdd1243dSDimitry Andric       uint32_t Imm11 = extractBits(Value, 11, 1) << 7;
212fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
213*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr =
214*bdd1243dSDimitry Andric           (RawInstr & 0x1FFF07F) | Imm12 | Imm10_5 | Imm4_1 | Imm11;
21504eeddc0SDimitry Andric       break;
21604eeddc0SDimitry Andric     }
21781ad6265SDimitry Andric     case R_RISCV_JAL: {
21881ad6265SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
21981ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 20)))
22081ad6265SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
22181ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
22281ad6265SDimitry Andric         return makeAlignmentError(FixupAddress, Value, 2, E);
22381ad6265SDimitry Andric       uint32_t Imm20 = extractBits(Value, 20, 1) << 31;
22481ad6265SDimitry Andric       uint32_t Imm10_1 = extractBits(Value, 1, 10) << 21;
22581ad6265SDimitry Andric       uint32_t Imm11 = extractBits(Value, 11, 1) << 20;
22681ad6265SDimitry Andric       uint32_t Imm19_12 = extractBits(Value, 12, 8) << 12;
22781ad6265SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
22804eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
229*bdd1243dSDimitry Andric           (RawInstr & 0xFFF) | Imm20 | Imm10_1 | Imm11 | Imm19_12;
230fe6060f1SDimitry Andric       break;
231fe6060f1SDimitry Andric     }
232fe6060f1SDimitry Andric     case R_RISCV_CALL: {
233fe6060f1SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
23404eeddc0SDimitry Andric       int64_t Hi = Value + 0x800;
23581ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
23604eeddc0SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
237fe6060f1SDimitry Andric       int32_t Lo = Value & 0xFFF;
238fe6060f1SDimitry Andric       uint32_t RawInstrAuipc = *(little32_t *)FixupPtr;
239fe6060f1SDimitry Andric       uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4);
24004eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
24104eeddc0SDimitry Andric           RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000));
242fe6060f1SDimitry Andric       *(little32_t *)(FixupPtr + 4) =
243fe6060f1SDimitry Andric           RawInstrJalr | (static_cast<uint32_t>(Lo) << 20);
244fe6060f1SDimitry Andric       break;
245fe6060f1SDimitry Andric     }
246*bdd1243dSDimitry Andric     // The relocations R_RISCV_CALL_PLT and R_RISCV_GOT_HI20 are handled by
247*bdd1243dSDimitry Andric     // PerGraphGOTAndPLTStubsBuilder_ELF_riscv and are transformed into
248*bdd1243dSDimitry Andric     // R_RISCV_CALL and R_RISCV_PCREL_HI20.
249fe6060f1SDimitry Andric     case R_RISCV_PCREL_HI20: {
250fe6060f1SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
25104eeddc0SDimitry Andric       int64_t Hi = Value + 0x800;
25281ad6265SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
25304eeddc0SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
254fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
25504eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
25604eeddc0SDimitry Andric           (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
257fe6060f1SDimitry Andric       break;
258fe6060f1SDimitry Andric     }
259fe6060f1SDimitry Andric     case R_RISCV_PCREL_LO12_I: {
26004eeddc0SDimitry Andric       // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
26104eeddc0SDimitry Andric       // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a
26204eeddc0SDimitry Andric       // check.
263fe6060f1SDimitry Andric       auto RelHI20 = getRISCVPCRelHi20(E);
264fe6060f1SDimitry Andric       if (!RelHI20)
265fe6060f1SDimitry Andric         return RelHI20.takeError();
266fe6060f1SDimitry Andric       int64_t Value = RelHI20->getTarget().getAddress() +
267fe6060f1SDimitry Andric                       RelHI20->getAddend() - E.getTarget().getAddress();
268fe6060f1SDimitry Andric       int64_t Lo = Value & 0xFFF;
269fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
270fe6060f1SDimitry Andric       *(little32_t *)FixupPtr =
271fe6060f1SDimitry Andric           (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
272fe6060f1SDimitry Andric       break;
273fe6060f1SDimitry Andric     }
274fe6060f1SDimitry Andric     case R_RISCV_PCREL_LO12_S: {
27504eeddc0SDimitry Andric       // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
27604eeddc0SDimitry Andric       // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a
27704eeddc0SDimitry Andric       // check.
278fe6060f1SDimitry Andric       auto RelHI20 = getRISCVPCRelHi20(E);
279*bdd1243dSDimitry Andric       if (!RelHI20)
280*bdd1243dSDimitry Andric         return RelHI20.takeError();
281fe6060f1SDimitry Andric       int64_t Value = RelHI20->getTarget().getAddress() +
282fe6060f1SDimitry Andric                       RelHI20->getAddend() - E.getTarget().getAddress();
283fe6060f1SDimitry Andric       int64_t Lo = Value & 0xFFF;
284*bdd1243dSDimitry Andric       uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
285*bdd1243dSDimitry Andric       uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
286fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
287fe6060f1SDimitry Andric 
288*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
289fe6060f1SDimitry Andric       break;
290fe6060f1SDimitry Andric     }
291*bdd1243dSDimitry Andric     case R_RISCV_HI20: {
292*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
293*bdd1243dSDimitry Andric       int64_t Hi = Value + 0x800;
294*bdd1243dSDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
295*bdd1243dSDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
296*bdd1243dSDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
297*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr =
298*bdd1243dSDimitry Andric           (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
29904eeddc0SDimitry Andric       break;
30004eeddc0SDimitry Andric     }
301*bdd1243dSDimitry Andric     case R_RISCV_LO12_I: {
302*bdd1243dSDimitry Andric       // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
303*bdd1243dSDimitry Andric       // with current relocation R_RISCV_LO12_I. So here may need a check.
304*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
305*bdd1243dSDimitry Andric       int32_t Lo = Value & 0xFFF;
306*bdd1243dSDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
307*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr =
308*bdd1243dSDimitry Andric           (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
30904eeddc0SDimitry Andric       break;
31004eeddc0SDimitry Andric     }
311*bdd1243dSDimitry Andric     case R_RISCV_LO12_S: {
312*bdd1243dSDimitry Andric       // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
313*bdd1243dSDimitry Andric       // with current relocation R_RISCV_LO12_S. So here may need a check.
314*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
315*bdd1243dSDimitry Andric       int64_t Lo = Value & 0xFFF;
316*bdd1243dSDimitry Andric       uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25;
317*bdd1243dSDimitry Andric       uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7;
318*bdd1243dSDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
319*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0;
32004eeddc0SDimitry Andric       break;
32104eeddc0SDimitry Andric     }
32204eeddc0SDimitry Andric     case R_RISCV_ADD8: {
32304eeddc0SDimitry Andric       int64_t Value =
32404eeddc0SDimitry Andric           (E.getTarget().getAddress() +
32504eeddc0SDimitry Andric            *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) +
32604eeddc0SDimitry Andric            E.getAddend())
32704eeddc0SDimitry Andric               .getValue();
32804eeddc0SDimitry Andric       *FixupPtr = static_cast<uint8_t>(Value);
32904eeddc0SDimitry Andric       break;
33004eeddc0SDimitry Andric     }
331*bdd1243dSDimitry Andric     case R_RISCV_ADD16: {
332*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
333*bdd1243dSDimitry Andric                        support::endian::read16le(reinterpret_cast<const void *>(
334*bdd1243dSDimitry Andric                            FixupAddress.getValue())) +
335*bdd1243dSDimitry Andric                        E.getAddend())
336*bdd1243dSDimitry Andric                           .getValue();
337*bdd1243dSDimitry Andric       *(little16_t *)FixupPtr = static_cast<uint16_t>(Value);
338*bdd1243dSDimitry Andric       break;
339*bdd1243dSDimitry Andric     }
340*bdd1243dSDimitry Andric     case R_RISCV_ADD32: {
341*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
342*bdd1243dSDimitry Andric                        support::endian::read32le(reinterpret_cast<const void *>(
343*bdd1243dSDimitry Andric                            FixupAddress.getValue())) +
344*bdd1243dSDimitry Andric                        E.getAddend())
345*bdd1243dSDimitry Andric                           .getValue();
346*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
347*bdd1243dSDimitry Andric       break;
348*bdd1243dSDimitry Andric     }
349*bdd1243dSDimitry Andric     case R_RISCV_ADD64: {
350*bdd1243dSDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
351*bdd1243dSDimitry Andric                        support::endian::read64le(reinterpret_cast<const void *>(
352*bdd1243dSDimitry Andric                            FixupAddress.getValue())) +
353*bdd1243dSDimitry Andric                        E.getAddend())
354*bdd1243dSDimitry Andric                           .getValue();
35504eeddc0SDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
35604eeddc0SDimitry Andric       break;
35704eeddc0SDimitry Andric     }
358*bdd1243dSDimitry Andric     case R_RISCV_SUB8: {
359*bdd1243dSDimitry Andric       int64_t Value =
360*bdd1243dSDimitry Andric           *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) -
36104eeddc0SDimitry Andric           E.getTarget().getAddress().getValue() - E.getAddend();
362*bdd1243dSDimitry Andric       *FixupPtr = static_cast<uint8_t>(Value);
36304eeddc0SDimitry Andric       break;
36404eeddc0SDimitry Andric     }
36504eeddc0SDimitry Andric     case R_RISCV_SUB16: {
36604eeddc0SDimitry Andric       int64_t Value = support::endian::read16le(reinterpret_cast<const void *>(
36704eeddc0SDimitry Andric                           FixupAddress.getValue())) -
36804eeddc0SDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
36904eeddc0SDimitry Andric       *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
37004eeddc0SDimitry Andric       break;
37104eeddc0SDimitry Andric     }
372*bdd1243dSDimitry Andric     case R_RISCV_SUB32: {
373*bdd1243dSDimitry Andric       int64_t Value = support::endian::read32le(reinterpret_cast<const void *>(
374*bdd1243dSDimitry Andric                           FixupAddress.getValue())) -
37504eeddc0SDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
376*bdd1243dSDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
377*bdd1243dSDimitry Andric       break;
378*bdd1243dSDimitry Andric     }
379*bdd1243dSDimitry Andric     case R_RISCV_SUB64: {
380*bdd1243dSDimitry Andric       int64_t Value = support::endian::read64le(reinterpret_cast<const void *>(
381*bdd1243dSDimitry Andric                           FixupAddress.getValue())) -
382*bdd1243dSDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
383*bdd1243dSDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
384*bdd1243dSDimitry Andric       break;
385*bdd1243dSDimitry Andric     }
386*bdd1243dSDimitry Andric     case R_RISCV_RVC_BRANCH: {
387*bdd1243dSDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
388*bdd1243dSDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 8)))
389*bdd1243dSDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
390*bdd1243dSDimitry Andric       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
391*bdd1243dSDimitry Andric         return makeAlignmentError(FixupAddress, Value, 2, E);
392*bdd1243dSDimitry Andric       uint16_t Imm8 = extractBits(Value, 8, 1) << 12;
393*bdd1243dSDimitry Andric       uint16_t Imm4_3 = extractBits(Value, 3, 2) << 10;
394*bdd1243dSDimitry Andric       uint16_t Imm7_6 = extractBits(Value, 6, 2) << 5;
395*bdd1243dSDimitry Andric       uint16_t Imm2_1 = extractBits(Value, 1, 2) << 3;
396*bdd1243dSDimitry Andric       uint16_t Imm5 = extractBits(Value, 5, 1) << 2;
397*bdd1243dSDimitry Andric       uint16_t RawInstr = *(little16_t *)FixupPtr;
398*bdd1243dSDimitry Andric       *(little16_t *)FixupPtr =
399*bdd1243dSDimitry Andric           (RawInstr & 0xE383) | Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5;
400*bdd1243dSDimitry Andric       break;
401*bdd1243dSDimitry Andric     }
402*bdd1243dSDimitry Andric     case R_RISCV_RVC_JUMP: {
403*bdd1243dSDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
404*bdd1243dSDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 11)))
405*bdd1243dSDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
406*bdd1243dSDimitry Andric       if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2)))
407*bdd1243dSDimitry Andric         return makeAlignmentError(FixupAddress, Value, 2, E);
408*bdd1243dSDimitry Andric       uint16_t Imm11 = extractBits(Value, 11, 1) << 12;
409*bdd1243dSDimitry Andric       uint16_t Imm4 = extractBits(Value, 4, 1) << 11;
410*bdd1243dSDimitry Andric       uint16_t Imm9_8 = extractBits(Value, 8, 2) << 9;
411*bdd1243dSDimitry Andric       uint16_t Imm10 = extractBits(Value, 10, 1) << 8;
412*bdd1243dSDimitry Andric       uint16_t Imm6 = extractBits(Value, 6, 1) << 7;
413*bdd1243dSDimitry Andric       uint16_t Imm7 = extractBits(Value, 7, 1) << 6;
414*bdd1243dSDimitry Andric       uint16_t Imm3_1 = extractBits(Value, 1, 3) << 3;
415*bdd1243dSDimitry Andric       uint16_t Imm5 = extractBits(Value, 5, 1) << 2;
416*bdd1243dSDimitry Andric       uint16_t RawInstr = *(little16_t *)FixupPtr;
417*bdd1243dSDimitry Andric       *(little16_t *)FixupPtr = (RawInstr & 0xE003) | Imm11 | Imm4 | Imm9_8 |
418*bdd1243dSDimitry Andric                                 Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5;
41904eeddc0SDimitry Andric       break;
42004eeddc0SDimitry Andric     }
42181ad6265SDimitry Andric     case R_RISCV_SUB6: {
42281ad6265SDimitry Andric       int64_t Value =
42381ad6265SDimitry Andric           *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) & 0x3f;
42481ad6265SDimitry Andric       Value -= E.getTarget().getAddress().getValue() - E.getAddend();
42581ad6265SDimitry Andric       *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<uint8_t>(Value) & 0x3f);
42681ad6265SDimitry Andric       break;
42781ad6265SDimitry Andric     }
42804eeddc0SDimitry Andric     case R_RISCV_SET6: {
42904eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
43004eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
43104eeddc0SDimitry Andric       int64_t Word6 = Value & 0x3f;
43204eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6;
43304eeddc0SDimitry Andric       break;
43404eeddc0SDimitry Andric     }
43504eeddc0SDimitry Andric     case R_RISCV_SET8: {
43604eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
43704eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
43804eeddc0SDimitry Andric       int64_t Word8 = Value & 0xff;
43904eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8;
44004eeddc0SDimitry Andric       break;
44104eeddc0SDimitry Andric     }
44204eeddc0SDimitry Andric     case R_RISCV_SET16: {
44304eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
44404eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
44504eeddc0SDimitry Andric       int64_t Word16 = Value & 0xffff;
44604eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16;
44704eeddc0SDimitry Andric       break;
44804eeddc0SDimitry Andric     }
44904eeddc0SDimitry Andric     case R_RISCV_SET32: {
45004eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
45104eeddc0SDimitry Andric       int64_t Word32 = Value & 0xffffffff;
45204eeddc0SDimitry Andric       *(little32_t *)FixupPtr = Word32;
45304eeddc0SDimitry Andric       break;
45404eeddc0SDimitry Andric     }
45504eeddc0SDimitry Andric     case R_RISCV_32_PCREL: {
45604eeddc0SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
45704eeddc0SDimitry Andric       int64_t Word32 = Value & 0xffffffff;
45804eeddc0SDimitry Andric       *(little32_t *)FixupPtr = Word32;
45904eeddc0SDimitry Andric       break;
46004eeddc0SDimitry Andric     }
461fe6060f1SDimitry Andric     }
462fe6060f1SDimitry Andric     return Error::success();
463fe6060f1SDimitry Andric   }
464fe6060f1SDimitry Andric };
465fe6060f1SDimitry Andric 
466fe6060f1SDimitry Andric template <typename ELFT>
467fe6060f1SDimitry Andric class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
468fe6060f1SDimitry Andric private:
469fe6060f1SDimitry Andric   static Expected<riscv::EdgeKind_riscv>
470fe6060f1SDimitry Andric   getRelocationKind(const uint32_t Type) {
471fe6060f1SDimitry Andric     using namespace riscv;
472fe6060f1SDimitry Andric     switch (Type) {
473fe6060f1SDimitry Andric     case ELF::R_RISCV_32:
474fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_32;
475fe6060f1SDimitry Andric     case ELF::R_RISCV_64:
476fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_64;
47704eeddc0SDimitry Andric     case ELF::R_RISCV_BRANCH:
47804eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_BRANCH;
47981ad6265SDimitry Andric     case ELF::R_RISCV_JAL:
48081ad6265SDimitry Andric       return EdgeKind_riscv::R_RISCV_JAL;
481fe6060f1SDimitry Andric     case ELF::R_RISCV_CALL:
482fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_CALL;
483*bdd1243dSDimitry Andric     case ELF::R_RISCV_CALL_PLT:
484*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_CALL_PLT;
485*bdd1243dSDimitry Andric     case ELF::R_RISCV_GOT_HI20:
486*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_GOT_HI20;
487fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_HI20:
488fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_HI20;
489fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_LO12_I:
490fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_LO12_I;
491fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_LO12_S:
492fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_LO12_S;
493*bdd1243dSDimitry Andric     case ELF::R_RISCV_HI20:
494*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_HI20;
495*bdd1243dSDimitry Andric     case ELF::R_RISCV_LO12_I:
496*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_LO12_I;
497*bdd1243dSDimitry Andric     case ELF::R_RISCV_LO12_S:
498*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_LO12_S;
49904eeddc0SDimitry Andric     case ELF::R_RISCV_ADD8:
50004eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD8;
501*bdd1243dSDimitry Andric     case ELF::R_RISCV_ADD16:
502*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD16;
503*bdd1243dSDimitry Andric     case ELF::R_RISCV_ADD32:
504*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD32;
505*bdd1243dSDimitry Andric     case ELF::R_RISCV_ADD64:
506*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD64;
50704eeddc0SDimitry Andric     case ELF::R_RISCV_SUB8:
50804eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB8;
509*bdd1243dSDimitry Andric     case ELF::R_RISCV_SUB16:
510*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB16;
511*bdd1243dSDimitry Andric     case ELF::R_RISCV_SUB32:
512*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB32;
513*bdd1243dSDimitry Andric     case ELF::R_RISCV_SUB64:
514*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB64;
515*bdd1243dSDimitry Andric     case ELF::R_RISCV_RVC_BRANCH:
516*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_RVC_BRANCH;
517*bdd1243dSDimitry Andric     case ELF::R_RISCV_RVC_JUMP:
518*bdd1243dSDimitry Andric       return EdgeKind_riscv::R_RISCV_RVC_JUMP;
51981ad6265SDimitry Andric     case ELF::R_RISCV_SUB6:
52081ad6265SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB6;
52104eeddc0SDimitry Andric     case ELF::R_RISCV_SET6:
52204eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET6;
52304eeddc0SDimitry Andric     case ELF::R_RISCV_SET8:
52404eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET8;
52504eeddc0SDimitry Andric     case ELF::R_RISCV_SET16:
52604eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET16;
52704eeddc0SDimitry Andric     case ELF::R_RISCV_SET32:
52804eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET32;
52904eeddc0SDimitry Andric     case ELF::R_RISCV_32_PCREL:
53004eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_32_PCREL;
531fe6060f1SDimitry Andric     }
532fe6060f1SDimitry Andric 
53381ad6265SDimitry Andric     return make_error<JITLinkError>(
53481ad6265SDimitry Andric         "Unsupported riscv relocation:" + formatv("{0:d}: ", Type) +
53581ad6265SDimitry Andric         object::getELFRelocationTypeName(ELF::EM_RISCV, Type));
536fe6060f1SDimitry Andric   }
537fe6060f1SDimitry Andric 
538fe6060f1SDimitry Andric   Error addRelocations() override {
539349cc55cSDimitry Andric     LLVM_DEBUG(dbgs() << "Processing relocations:\n");
540349cc55cSDimitry Andric 
541fe6060f1SDimitry Andric     using Base = ELFLinkGraphBuilder<ELFT>;
542349cc55cSDimitry Andric     using Self = ELFLinkGraphBuilder_riscv<ELFT>;
543349cc55cSDimitry Andric     for (const auto &RelSect : Base::Sections)
544*bdd1243dSDimitry Andric       if (Error Err = Base::forEachRelaRelocation(RelSect, this,
545349cc55cSDimitry Andric                                                   &Self::addSingleRelocation))
546349cc55cSDimitry Andric         return Err;
547fe6060f1SDimitry Andric 
548349cc55cSDimitry Andric     return Error::success();
549fe6060f1SDimitry Andric   }
550fe6060f1SDimitry Andric 
551349cc55cSDimitry Andric   Error addSingleRelocation(const typename ELFT::Rela &Rel,
552349cc55cSDimitry Andric                             const typename ELFT::Shdr &FixupSect,
55304eeddc0SDimitry Andric                             Block &BlockToFix) {
554349cc55cSDimitry Andric     using Base = ELFLinkGraphBuilder<ELFT>;
555349cc55cSDimitry Andric 
556753f127fSDimitry Andric     uint32_t Type = Rel.getType(false);
557753f127fSDimitry Andric     // We do not implement linker relaxation, except what is required for
558753f127fSDimitry Andric     // alignment (see below).
559753f127fSDimitry Andric     if (Type == llvm::ELF::R_RISCV_RELAX)
560753f127fSDimitry Andric       return Error::success();
561753f127fSDimitry Andric 
562753f127fSDimitry Andric     int64_t Addend = Rel.r_addend;
563753f127fSDimitry Andric     if (Type == llvm::ELF::R_RISCV_ALIGN) {
564753f127fSDimitry Andric       uint64_t Alignment = PowerOf2Ceil(Addend);
565753f127fSDimitry Andric       // FIXME: Implement support for ensuring alignment together with linker
566753f127fSDimitry Andric       // relaxation; 2 bytes are guaranteed by the length of compressed
567753f127fSDimitry Andric       // instructions, so this does not need any action from our side.
568753f127fSDimitry Andric       if (Alignment > 2)
569753f127fSDimitry Andric         return make_error<JITLinkError>(
570753f127fSDimitry Andric             formatv("Unsupported relocation R_RISCV_ALIGN with alignment {0} "
571753f127fSDimitry Andric                     "larger than 2 (addend: {1})",
572753f127fSDimitry Andric                     Alignment, Addend));
573753f127fSDimitry Andric       return Error::success();
574753f127fSDimitry Andric     }
575753f127fSDimitry Andric 
576753f127fSDimitry Andric     Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type);
577753f127fSDimitry Andric     if (!Kind)
578753f127fSDimitry Andric       return Kind.takeError();
579753f127fSDimitry Andric 
580349cc55cSDimitry Andric     uint32_t SymbolIndex = Rel.getSymbol(false);
581349cc55cSDimitry Andric     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
582349cc55cSDimitry Andric     if (!ObjSymbol)
583349cc55cSDimitry Andric       return ObjSymbol.takeError();
584349cc55cSDimitry Andric 
585349cc55cSDimitry Andric     Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
586349cc55cSDimitry Andric     if (!GraphSymbol)
587349cc55cSDimitry Andric       return make_error<StringError>(
588349cc55cSDimitry Andric           formatv("Could not find symbol at given index, did you add it to "
589349cc55cSDimitry Andric                   "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
590349cc55cSDimitry Andric                   SymbolIndex, (*ObjSymbol)->st_shndx,
591349cc55cSDimitry Andric                   Base::GraphSymbols.size()),
592349cc55cSDimitry Andric           inconvertibleErrorCode());
593349cc55cSDimitry Andric 
59404eeddc0SDimitry Andric     auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
59504eeddc0SDimitry Andric     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
596349cc55cSDimitry Andric     Edge GE(*Kind, Offset, *GraphSymbol, Addend);
597349cc55cSDimitry Andric     LLVM_DEBUG({
598349cc55cSDimitry Andric       dbgs() << "    ";
59904eeddc0SDimitry Andric       printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind));
600349cc55cSDimitry Andric       dbgs() << "\n";
601349cc55cSDimitry Andric     });
602349cc55cSDimitry Andric 
60304eeddc0SDimitry Andric     BlockToFix.addEdge(std::move(GE));
604fe6060f1SDimitry Andric     return Error::success();
605fe6060f1SDimitry Andric   }
606fe6060f1SDimitry Andric 
607fe6060f1SDimitry Andric public:
608fe6060f1SDimitry Andric   ELFLinkGraphBuilder_riscv(StringRef FileName,
609fe6060f1SDimitry Andric                             const object::ELFFile<ELFT> &Obj, const Triple T)
610fe6060f1SDimitry Andric       : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
611fe6060f1SDimitry Andric                                   riscv::getEdgeKindName) {}
612fe6060f1SDimitry Andric };
613fe6060f1SDimitry Andric 
614fe6060f1SDimitry Andric Expected<std::unique_ptr<LinkGraph>>
615fe6060f1SDimitry Andric createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) {
616fe6060f1SDimitry Andric   LLVM_DEBUG({
617fe6060f1SDimitry Andric     dbgs() << "Building jitlink graph for new input "
618fe6060f1SDimitry Andric            << ObjectBuffer.getBufferIdentifier() << "...\n";
619fe6060f1SDimitry Andric   });
620fe6060f1SDimitry Andric 
621fe6060f1SDimitry Andric   auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
622fe6060f1SDimitry Andric   if (!ELFObj)
623fe6060f1SDimitry Andric     return ELFObj.takeError();
624fe6060f1SDimitry Andric 
625fe6060f1SDimitry Andric   if ((*ELFObj)->getArch() == Triple::riscv64) {
626fe6060f1SDimitry Andric     auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
627fe6060f1SDimitry Andric     return ELFLinkGraphBuilder_riscv<object::ELF64LE>(
628fe6060f1SDimitry Andric                (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
629fe6060f1SDimitry Andric                (*ELFObj)->makeTriple())
630fe6060f1SDimitry Andric         .buildGraph();
631fe6060f1SDimitry Andric   } else {
632fe6060f1SDimitry Andric     assert((*ELFObj)->getArch() == Triple::riscv32 &&
633fe6060f1SDimitry Andric            "Invalid triple for RISCV ELF object file");
634fe6060f1SDimitry Andric     auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
635fe6060f1SDimitry Andric     return ELFLinkGraphBuilder_riscv<object::ELF32LE>(
636fe6060f1SDimitry Andric                (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
637fe6060f1SDimitry Andric                (*ELFObj)->makeTriple())
638fe6060f1SDimitry Andric         .buildGraph();
639fe6060f1SDimitry Andric   }
640fe6060f1SDimitry Andric }
641fe6060f1SDimitry Andric 
642fe6060f1SDimitry Andric void link_ELF_riscv(std::unique_ptr<LinkGraph> G,
643fe6060f1SDimitry Andric                     std::unique_ptr<JITLinkContext> Ctx) {
644fe6060f1SDimitry Andric   PassConfiguration Config;
645fe6060f1SDimitry Andric   const Triple &TT = G->getTargetTriple();
646fe6060f1SDimitry Andric   if (Ctx->shouldAddDefaultTargetPasses(TT)) {
647fe6060f1SDimitry Andric     if (auto MarkLive = Ctx->getMarkLivePass(TT))
648fe6060f1SDimitry Andric       Config.PrePrunePasses.push_back(std::move(MarkLive));
649fe6060f1SDimitry Andric     else
650fe6060f1SDimitry Andric       Config.PrePrunePasses.push_back(markAllSymbolsLive);
651349cc55cSDimitry Andric     Config.PostPrunePasses.push_back(
652349cc55cSDimitry Andric         PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass);
653fe6060f1SDimitry Andric   }
654fe6060f1SDimitry Andric   if (auto Err = Ctx->modifyPassConfig(*G, Config))
655fe6060f1SDimitry Andric     return Ctx->notifyFailed(std::move(Err));
656fe6060f1SDimitry Andric 
657fe6060f1SDimitry Andric   ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config));
658fe6060f1SDimitry Andric }
659fe6060f1SDimitry Andric 
660fe6060f1SDimitry Andric } // namespace jitlink
661fe6060f1SDimitry Andric } // namespace llvm
662