xref: /freebsd-src/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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"
22*04eeddc0SDimitry 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) {
48*04eeddc0SDimitry Andric     Block &GOTBlock =
49*04eeddc0SDimitry Andric         G.createContentBlock(getGOTSection(), getGOTEntryBlockContent(),
50*04eeddc0SDimitry 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) {
56*04eeddc0SDimitry Andric     Block &StubContentBlock = G.createContentBlock(
57*04eeddc0SDimitry 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)
85349cc55cSDimitry Andric       GOTSection = &G.createSection("$__GOT", MemProt::Read);
86349cc55cSDimitry Andric     return *GOTSection;
87349cc55cSDimitry Andric   }
88349cc55cSDimitry Andric 
89349cc55cSDimitry Andric   Section &getStubsSection() const {
90349cc55cSDimitry Andric     if (!StubsSection)
91349cc55cSDimitry Andric       StubsSection =
92349cc55cSDimitry Andric           &G.createSection("$__STUBS", MemProt::Read | 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();
139*04eeddc0SDimitry Andric   orc::ExecutorAddrDiff Offset = Sym.getOffset();
140fe6060f1SDimitry Andric 
141fe6060f1SDimitry Andric   struct Comp {
142*04eeddc0SDimitry Andric     bool operator()(const Edge &Lhs, orc::ExecutorAddrDiff Offset) {
143fe6060f1SDimitry Andric       return Lhs.getOffset() < Offset;
144fe6060f1SDimitry Andric     }
145*04eeddc0SDimitry 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 
162*04eeddc0SDimitry Andric static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) {
163*04eeddc0SDimitry Andric   return (Num & (((1ULL << (Size + 1)) - 1) << Low)) >> Low;
164*04eeddc0SDimitry Andric }
165*04eeddc0SDimitry Andric 
166*04eeddc0SDimitry Andric inline Error checkAlignment(llvm::orc::ExecutorAddr loc, uint64_t v, int n,
167*04eeddc0SDimitry Andric                             const Edge &E) {
168*04eeddc0SDimitry Andric   if (v & (n - 1))
169*04eeddc0SDimitry Andric     return make_error<JITLinkError>("0x" + llvm::utohexstr(loc.getValue()) +
170*04eeddc0SDimitry Andric                                     " improper alignment for relocation " +
171*04eeddc0SDimitry Andric                                     formatv("{0:d}", E.getKind()) + ": 0x" +
172*04eeddc0SDimitry Andric                                     llvm::utohexstr(v) + " is not aligned to " +
173*04eeddc0SDimitry Andric                                     Twine(n) + " bytes");
174*04eeddc0SDimitry Andric   return Error::success();
175*04eeddc0SDimitry Andric }
176*04eeddc0SDimitry Andric 
177*04eeddc0SDimitry Andric static inline bool isInRangeForImmS32(int64_t Value) {
178*04eeddc0SDimitry Andric   return (Value >= std::numeric_limits<int32_t>::min() &&
179*04eeddc0SDimitry Andric           Value <= std::numeric_limits<int32_t>::max());
180fe6060f1SDimitry Andric }
181fe6060f1SDimitry Andric 
182fe6060f1SDimitry Andric class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
183fe6060f1SDimitry Andric   friend class JITLinker<ELFJITLinker_riscv>;
184fe6060f1SDimitry Andric 
185fe6060f1SDimitry Andric public:
186fe6060f1SDimitry Andric   ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx,
187fe6060f1SDimitry Andric                      std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
188fe6060f1SDimitry Andric       : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric private:
191fe6060f1SDimitry Andric   Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
192fe6060f1SDimitry Andric     using namespace riscv;
193fe6060f1SDimitry Andric     using namespace llvm::support;
194fe6060f1SDimitry Andric 
195fe6060f1SDimitry Andric     char *BlockWorkingMem = B.getAlreadyMutableContent().data();
196fe6060f1SDimitry Andric     char *FixupPtr = BlockWorkingMem + E.getOffset();
197*04eeddc0SDimitry Andric     orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
198fe6060f1SDimitry Andric     switch (E.getKind()) {
199349cc55cSDimitry Andric     case R_RISCV_32: {
200*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
201349cc55cSDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
202349cc55cSDimitry Andric       break;
203349cc55cSDimitry Andric     }
204349cc55cSDimitry Andric     case R_RISCV_64: {
205*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
206349cc55cSDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
207349cc55cSDimitry Andric       break;
208349cc55cSDimitry Andric     }
209*04eeddc0SDimitry Andric     case R_RISCV_BRANCH: {
210*04eeddc0SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
211*04eeddc0SDimitry Andric       Error AlignmentIssue = checkAlignment(FixupAddress, Value, 2, E);
212*04eeddc0SDimitry Andric       if (AlignmentIssue) {
213*04eeddc0SDimitry Andric         return AlignmentIssue;
214*04eeddc0SDimitry Andric       }
215*04eeddc0SDimitry Andric       int64_t Lo = Value & 0xFFF;
216*04eeddc0SDimitry Andric       uint32_t Imm31_25 = extractBits(Lo, 5, 6) << 25 | extractBits(Lo, 12, 1)
217*04eeddc0SDimitry Andric                                                             << 31;
218*04eeddc0SDimitry Andric       uint32_t Imm11_7 = extractBits(Lo, 1, 4) << 8 | extractBits(Lo, 11, 1)
219*04eeddc0SDimitry Andric                                                           << 7;
220fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
221*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
222*04eeddc0SDimitry Andric       break;
223*04eeddc0SDimitry Andric     }
224*04eeddc0SDimitry Andric     case R_RISCV_HI20: {
225*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
226*04eeddc0SDimitry Andric       int64_t Hi = Value + 0x800;
227*04eeddc0SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
228*04eeddc0SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
229*04eeddc0SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
230*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
231*04eeddc0SDimitry Andric           (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
232fe6060f1SDimitry Andric       break;
233fe6060f1SDimitry Andric     }
234fe6060f1SDimitry Andric     case R_RISCV_LO12_I: {
235*04eeddc0SDimitry Andric       // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
236*04eeddc0SDimitry Andric       // with current relocation R_RISCV_LO12_I. So here may need a check.
237*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
238fe6060f1SDimitry Andric       int32_t Lo = Value & 0xFFF;
239fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
240fe6060f1SDimitry Andric       *(little32_t *)FixupPtr =
241fe6060f1SDimitry Andric           (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
242fe6060f1SDimitry Andric       break;
243fe6060f1SDimitry Andric     }
244fe6060f1SDimitry Andric     case R_RISCV_CALL: {
245fe6060f1SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
246*04eeddc0SDimitry Andric       int64_t Hi = Value + 0x800;
247*04eeddc0SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
248*04eeddc0SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
249fe6060f1SDimitry Andric       int32_t Lo = Value & 0xFFF;
250fe6060f1SDimitry Andric       uint32_t RawInstrAuipc = *(little32_t *)FixupPtr;
251fe6060f1SDimitry Andric       uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4);
252*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
253*04eeddc0SDimitry Andric           RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000));
254fe6060f1SDimitry Andric       *(little32_t *)(FixupPtr + 4) =
255fe6060f1SDimitry Andric           RawInstrJalr | (static_cast<uint32_t>(Lo) << 20);
256fe6060f1SDimitry Andric       break;
257fe6060f1SDimitry Andric     }
258fe6060f1SDimitry Andric     case R_RISCV_PCREL_HI20: {
259fe6060f1SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
260*04eeddc0SDimitry Andric       int64_t Hi = Value + 0x800;
261*04eeddc0SDimitry Andric       if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi)))
262*04eeddc0SDimitry Andric         return makeTargetOutOfRangeError(G, B, E);
263fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
264*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr =
265*04eeddc0SDimitry Andric           (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
266fe6060f1SDimitry Andric       break;
267fe6060f1SDimitry Andric     }
268fe6060f1SDimitry Andric     case R_RISCV_PCREL_LO12_I: {
269*04eeddc0SDimitry Andric       // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
270*04eeddc0SDimitry Andric       // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a
271*04eeddc0SDimitry Andric       // check.
272fe6060f1SDimitry Andric       auto RelHI20 = getRISCVPCRelHi20(E);
273fe6060f1SDimitry Andric       if (!RelHI20)
274fe6060f1SDimitry Andric         return RelHI20.takeError();
275fe6060f1SDimitry Andric       int64_t Value = RelHI20->getTarget().getAddress() +
276fe6060f1SDimitry Andric                       RelHI20->getAddend() - E.getTarget().getAddress();
277fe6060f1SDimitry Andric       int64_t Lo = Value & 0xFFF;
278fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
279fe6060f1SDimitry Andric       *(little32_t *)FixupPtr =
280fe6060f1SDimitry Andric           (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
281fe6060f1SDimitry Andric       break;
282fe6060f1SDimitry Andric     }
283fe6060f1SDimitry Andric     case R_RISCV_PCREL_LO12_S: {
284*04eeddc0SDimitry Andric       // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
285*04eeddc0SDimitry Andric       // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a
286*04eeddc0SDimitry Andric       // check.
287fe6060f1SDimitry Andric       auto RelHI20 = getRISCVPCRelHi20(E);
288fe6060f1SDimitry Andric       int64_t Value = RelHI20->getTarget().getAddress() +
289fe6060f1SDimitry Andric                       RelHI20->getAddend() - E.getTarget().getAddress();
290fe6060f1SDimitry Andric       int64_t Lo = Value & 0xFFF;
291*04eeddc0SDimitry Andric       uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
292*04eeddc0SDimitry Andric       uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
293fe6060f1SDimitry Andric       uint32_t RawInstr = *(little32_t *)FixupPtr;
294fe6060f1SDimitry Andric 
295fe6060f1SDimitry Andric       *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
296fe6060f1SDimitry Andric       break;
297fe6060f1SDimitry Andric     }
298*04eeddc0SDimitry Andric     case R_RISCV_ADD64: {
299*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
300*04eeddc0SDimitry Andric                        support::endian::read64le(reinterpret_cast<const void *>(
301*04eeddc0SDimitry Andric                            FixupAddress.getValue())) +
302*04eeddc0SDimitry Andric                        E.getAddend())
303*04eeddc0SDimitry Andric                           .getValue();
304*04eeddc0SDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
305*04eeddc0SDimitry Andric       break;
306*04eeddc0SDimitry Andric     }
307*04eeddc0SDimitry Andric     case R_RISCV_ADD32: {
308*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
309*04eeddc0SDimitry Andric                        support::endian::read32le(reinterpret_cast<const void *>(
310*04eeddc0SDimitry Andric                            FixupAddress.getValue())) +
311*04eeddc0SDimitry Andric                        E.getAddend())
312*04eeddc0SDimitry Andric                           .getValue();
313*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
314*04eeddc0SDimitry Andric       break;
315*04eeddc0SDimitry Andric     }
316*04eeddc0SDimitry Andric     case R_RISCV_ADD16: {
317*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() +
318*04eeddc0SDimitry Andric                        support::endian::read16le(reinterpret_cast<const void *>(
319*04eeddc0SDimitry Andric                            FixupAddress.getValue())) +
320*04eeddc0SDimitry Andric                        E.getAddend())
321*04eeddc0SDimitry Andric                           .getValue();
322*04eeddc0SDimitry Andric       *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
323*04eeddc0SDimitry Andric       break;
324*04eeddc0SDimitry Andric     }
325*04eeddc0SDimitry Andric     case R_RISCV_ADD8: {
326*04eeddc0SDimitry Andric       int64_t Value =
327*04eeddc0SDimitry Andric           (E.getTarget().getAddress() +
328*04eeddc0SDimitry Andric            *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) +
329*04eeddc0SDimitry Andric            E.getAddend())
330*04eeddc0SDimitry Andric               .getValue();
331*04eeddc0SDimitry Andric       *FixupPtr = static_cast<uint8_t>(Value);
332*04eeddc0SDimitry Andric       break;
333*04eeddc0SDimitry Andric     }
334*04eeddc0SDimitry Andric     case R_RISCV_SUB64: {
335*04eeddc0SDimitry Andric       int64_t Value = support::endian::read64le(reinterpret_cast<const void *>(
336*04eeddc0SDimitry Andric                           FixupAddress.getValue())) -
337*04eeddc0SDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
338*04eeddc0SDimitry Andric       *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
339*04eeddc0SDimitry Andric       break;
340*04eeddc0SDimitry Andric     }
341*04eeddc0SDimitry Andric     case R_RISCV_SUB32: {
342*04eeddc0SDimitry Andric       int64_t Value = support::endian::read32le(reinterpret_cast<const void *>(
343*04eeddc0SDimitry Andric                           FixupAddress.getValue())) -
344*04eeddc0SDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
345*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
346*04eeddc0SDimitry Andric       break;
347*04eeddc0SDimitry Andric     }
348*04eeddc0SDimitry Andric     case R_RISCV_SUB16: {
349*04eeddc0SDimitry Andric       int64_t Value = support::endian::read16le(reinterpret_cast<const void *>(
350*04eeddc0SDimitry Andric                           FixupAddress.getValue())) -
351*04eeddc0SDimitry Andric                       E.getTarget().getAddress().getValue() - E.getAddend();
352*04eeddc0SDimitry Andric       *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
353*04eeddc0SDimitry Andric       break;
354*04eeddc0SDimitry Andric     }
355*04eeddc0SDimitry Andric     case R_RISCV_SUB8: {
356*04eeddc0SDimitry Andric       int64_t Value =
357*04eeddc0SDimitry Andric           *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) -
358*04eeddc0SDimitry Andric           E.getTarget().getAddress().getValue() - E.getAddend();
359*04eeddc0SDimitry Andric       *FixupPtr = static_cast<uint8_t>(Value);
360*04eeddc0SDimitry Andric       break;
361*04eeddc0SDimitry Andric     }
362*04eeddc0SDimitry Andric     case R_RISCV_SET6: {
363*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
364*04eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
365*04eeddc0SDimitry Andric       int64_t Word6 = Value & 0x3f;
366*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6;
367*04eeddc0SDimitry Andric       break;
368*04eeddc0SDimitry Andric     }
369*04eeddc0SDimitry Andric     case R_RISCV_SET8: {
370*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
371*04eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
372*04eeddc0SDimitry Andric       int64_t Word8 = Value & 0xff;
373*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8;
374*04eeddc0SDimitry Andric       break;
375*04eeddc0SDimitry Andric     }
376*04eeddc0SDimitry Andric     case R_RISCV_SET16: {
377*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
378*04eeddc0SDimitry Andric       uint32_t RawData = *(little32_t *)FixupPtr;
379*04eeddc0SDimitry Andric       int64_t Word16 = Value & 0xffff;
380*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16;
381*04eeddc0SDimitry Andric       break;
382*04eeddc0SDimitry Andric     }
383*04eeddc0SDimitry Andric     case R_RISCV_SET32: {
384*04eeddc0SDimitry Andric       int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
385*04eeddc0SDimitry Andric       int64_t Word32 = Value & 0xffffffff;
386*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = Word32;
387*04eeddc0SDimitry Andric       break;
388*04eeddc0SDimitry Andric     }
389*04eeddc0SDimitry Andric     case R_RISCV_32_PCREL: {
390*04eeddc0SDimitry Andric       int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
391*04eeddc0SDimitry Andric       int64_t Word32 = Value & 0xffffffff;
392*04eeddc0SDimitry Andric       *(little32_t *)FixupPtr = Word32;
393*04eeddc0SDimitry Andric       break;
394*04eeddc0SDimitry Andric     }
395fe6060f1SDimitry Andric     }
396fe6060f1SDimitry Andric     return Error::success();
397fe6060f1SDimitry Andric   }
398fe6060f1SDimitry Andric };
399fe6060f1SDimitry Andric 
400fe6060f1SDimitry Andric template <typename ELFT>
401fe6060f1SDimitry Andric class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
402fe6060f1SDimitry Andric private:
403fe6060f1SDimitry Andric   static Expected<riscv::EdgeKind_riscv>
404fe6060f1SDimitry Andric   getRelocationKind(const uint32_t Type) {
405fe6060f1SDimitry Andric     using namespace riscv;
406fe6060f1SDimitry Andric     switch (Type) {
407fe6060f1SDimitry Andric     case ELF::R_RISCV_32:
408fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_32;
409fe6060f1SDimitry Andric     case ELF::R_RISCV_64:
410fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_64;
411*04eeddc0SDimitry Andric     case ELF::R_RISCV_BRANCH:
412*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_BRANCH;
413fe6060f1SDimitry Andric     case ELF::R_RISCV_HI20:
414fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_HI20;
415fe6060f1SDimitry Andric     case ELF::R_RISCV_LO12_I:
416fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_LO12_I;
417fe6060f1SDimitry Andric     case ELF::R_RISCV_CALL:
418fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_CALL;
419fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_HI20:
420fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_HI20;
421fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_LO12_I:
422fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_LO12_I;
423fe6060f1SDimitry Andric     case ELF::R_RISCV_PCREL_LO12_S:
424fe6060f1SDimitry Andric       return EdgeKind_riscv::R_RISCV_PCREL_LO12_S;
425349cc55cSDimitry Andric     case ELF::R_RISCV_GOT_HI20:
426349cc55cSDimitry Andric       return EdgeKind_riscv::R_RISCV_GOT_HI20;
427349cc55cSDimitry Andric     case ELF::R_RISCV_CALL_PLT:
428349cc55cSDimitry Andric       return EdgeKind_riscv::R_RISCV_CALL_PLT;
429*04eeddc0SDimitry Andric     case ELF::R_RISCV_ADD64:
430*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD64;
431*04eeddc0SDimitry Andric     case ELF::R_RISCV_ADD32:
432*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD32;
433*04eeddc0SDimitry Andric     case ELF::R_RISCV_ADD16:
434*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD16;
435*04eeddc0SDimitry Andric     case ELF::R_RISCV_ADD8:
436*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_ADD8;
437*04eeddc0SDimitry Andric     case ELF::R_RISCV_SUB64:
438*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB64;
439*04eeddc0SDimitry Andric     case ELF::R_RISCV_SUB32:
440*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB32;
441*04eeddc0SDimitry Andric     case ELF::R_RISCV_SUB16:
442*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB16;
443*04eeddc0SDimitry Andric     case ELF::R_RISCV_SUB8:
444*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SUB8;
445*04eeddc0SDimitry Andric     case ELF::R_RISCV_SET6:
446*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET6;
447*04eeddc0SDimitry Andric     case ELF::R_RISCV_SET8:
448*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET8;
449*04eeddc0SDimitry Andric     case ELF::R_RISCV_SET16:
450*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET16;
451*04eeddc0SDimitry Andric     case ELF::R_RISCV_SET32:
452*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_SET32;
453*04eeddc0SDimitry Andric     case ELF::R_RISCV_32_PCREL:
454*04eeddc0SDimitry Andric       return EdgeKind_riscv::R_RISCV_32_PCREL;
455fe6060f1SDimitry Andric     }
456fe6060f1SDimitry Andric 
457fe6060f1SDimitry Andric     return make_error<JITLinkError>("Unsupported riscv relocation:" +
458fe6060f1SDimitry Andric                                     formatv("{0:d}", Type));
459fe6060f1SDimitry Andric   }
460fe6060f1SDimitry Andric 
461fe6060f1SDimitry Andric   Error addRelocations() override {
462349cc55cSDimitry Andric     LLVM_DEBUG(dbgs() << "Processing relocations:\n");
463349cc55cSDimitry Andric 
464fe6060f1SDimitry Andric     using Base = ELFLinkGraphBuilder<ELFT>;
465349cc55cSDimitry Andric     using Self = ELFLinkGraphBuilder_riscv<ELFT>;
466349cc55cSDimitry Andric     for (const auto &RelSect : Base::Sections)
467349cc55cSDimitry Andric       if (Error Err = Base::forEachRelocation(RelSect, this,
468349cc55cSDimitry Andric                                               &Self::addSingleRelocation))
469349cc55cSDimitry Andric         return Err;
470fe6060f1SDimitry Andric 
471349cc55cSDimitry Andric     return Error::success();
472fe6060f1SDimitry Andric   }
473fe6060f1SDimitry Andric 
474349cc55cSDimitry Andric   Error addSingleRelocation(const typename ELFT::Rela &Rel,
475349cc55cSDimitry Andric                             const typename ELFT::Shdr &FixupSect,
476*04eeddc0SDimitry Andric                             Block &BlockToFix) {
477349cc55cSDimitry Andric     using Base = ELFLinkGraphBuilder<ELFT>;
478349cc55cSDimitry Andric 
479349cc55cSDimitry Andric     uint32_t SymbolIndex = Rel.getSymbol(false);
480349cc55cSDimitry Andric     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
481349cc55cSDimitry Andric     if (!ObjSymbol)
482349cc55cSDimitry Andric       return ObjSymbol.takeError();
483349cc55cSDimitry Andric 
484349cc55cSDimitry Andric     Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
485349cc55cSDimitry Andric     if (!GraphSymbol)
486349cc55cSDimitry Andric       return make_error<StringError>(
487349cc55cSDimitry Andric           formatv("Could not find symbol at given index, did you add it to "
488349cc55cSDimitry Andric                   "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
489349cc55cSDimitry Andric                   SymbolIndex, (*ObjSymbol)->st_shndx,
490349cc55cSDimitry Andric                   Base::GraphSymbols.size()),
491349cc55cSDimitry Andric           inconvertibleErrorCode());
492349cc55cSDimitry Andric 
493349cc55cSDimitry Andric     uint32_t Type = Rel.getType(false);
494349cc55cSDimitry Andric     Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type);
495fe6060f1SDimitry Andric     if (!Kind)
496fe6060f1SDimitry Andric       return Kind.takeError();
497fe6060f1SDimitry Andric 
498349cc55cSDimitry Andric     int64_t Addend = Rel.r_addend;
499*04eeddc0SDimitry Andric     auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
500*04eeddc0SDimitry Andric     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
501349cc55cSDimitry Andric     Edge GE(*Kind, Offset, *GraphSymbol, Addend);
502349cc55cSDimitry Andric     LLVM_DEBUG({
503349cc55cSDimitry Andric       dbgs() << "    ";
504*04eeddc0SDimitry Andric       printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind));
505349cc55cSDimitry Andric       dbgs() << "\n";
506349cc55cSDimitry Andric     });
507349cc55cSDimitry Andric 
508*04eeddc0SDimitry Andric     BlockToFix.addEdge(std::move(GE));
509fe6060f1SDimitry Andric     return Error::success();
510fe6060f1SDimitry Andric   }
511fe6060f1SDimitry Andric 
512fe6060f1SDimitry Andric public:
513fe6060f1SDimitry Andric   ELFLinkGraphBuilder_riscv(StringRef FileName,
514fe6060f1SDimitry Andric                             const object::ELFFile<ELFT> &Obj, const Triple T)
515fe6060f1SDimitry Andric       : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
516fe6060f1SDimitry Andric                                   riscv::getEdgeKindName) {}
517fe6060f1SDimitry Andric };
518fe6060f1SDimitry Andric 
519fe6060f1SDimitry Andric Expected<std::unique_ptr<LinkGraph>>
520fe6060f1SDimitry Andric createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) {
521fe6060f1SDimitry Andric   LLVM_DEBUG({
522fe6060f1SDimitry Andric     dbgs() << "Building jitlink graph for new input "
523fe6060f1SDimitry Andric            << ObjectBuffer.getBufferIdentifier() << "...\n";
524fe6060f1SDimitry Andric   });
525fe6060f1SDimitry Andric 
526fe6060f1SDimitry Andric   auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
527fe6060f1SDimitry Andric   if (!ELFObj)
528fe6060f1SDimitry Andric     return ELFObj.takeError();
529fe6060f1SDimitry Andric 
530fe6060f1SDimitry Andric   if ((*ELFObj)->getArch() == Triple::riscv64) {
531fe6060f1SDimitry Andric     auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
532fe6060f1SDimitry Andric     return ELFLinkGraphBuilder_riscv<object::ELF64LE>(
533fe6060f1SDimitry Andric                (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
534fe6060f1SDimitry Andric                (*ELFObj)->makeTriple())
535fe6060f1SDimitry Andric         .buildGraph();
536fe6060f1SDimitry Andric   } else {
537fe6060f1SDimitry Andric     assert((*ELFObj)->getArch() == Triple::riscv32 &&
538fe6060f1SDimitry Andric            "Invalid triple for RISCV ELF object file");
539fe6060f1SDimitry Andric     auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
540fe6060f1SDimitry Andric     return ELFLinkGraphBuilder_riscv<object::ELF32LE>(
541fe6060f1SDimitry Andric                (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
542fe6060f1SDimitry Andric                (*ELFObj)->makeTriple())
543fe6060f1SDimitry Andric         .buildGraph();
544fe6060f1SDimitry Andric   }
545fe6060f1SDimitry Andric }
546fe6060f1SDimitry Andric 
547fe6060f1SDimitry Andric void link_ELF_riscv(std::unique_ptr<LinkGraph> G,
548fe6060f1SDimitry Andric                     std::unique_ptr<JITLinkContext> Ctx) {
549fe6060f1SDimitry Andric   PassConfiguration Config;
550fe6060f1SDimitry Andric   const Triple &TT = G->getTargetTriple();
551fe6060f1SDimitry Andric   if (Ctx->shouldAddDefaultTargetPasses(TT)) {
552fe6060f1SDimitry Andric     if (auto MarkLive = Ctx->getMarkLivePass(TT))
553fe6060f1SDimitry Andric       Config.PrePrunePasses.push_back(std::move(MarkLive));
554fe6060f1SDimitry Andric     else
555fe6060f1SDimitry Andric       Config.PrePrunePasses.push_back(markAllSymbolsLive);
556349cc55cSDimitry Andric     Config.PostPrunePasses.push_back(
557349cc55cSDimitry Andric         PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass);
558fe6060f1SDimitry Andric   }
559fe6060f1SDimitry Andric   if (auto Err = Ctx->modifyPassConfig(*G, Config))
560fe6060f1SDimitry Andric     return Ctx->notifyFailed(std::move(Err));
561fe6060f1SDimitry Andric 
562fe6060f1SDimitry Andric   ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config));
563fe6060f1SDimitry Andric }
564fe6060f1SDimitry Andric 
565fe6060f1SDimitry Andric } // namespace jitlink
566fe6060f1SDimitry Andric } // namespace llvm
567