LLVM  16.0.0git
ELF_riscv.cpp
Go to the documentation of this file.
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 
14 #include "ELFLinkGraphBuilder.h"
15 #include "JITLinkGeneric.h"
17 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/ELF.h"
22 #include "llvm/Support/Endian.h"
23 
24 #define DEBUG_TYPE "jitlink"
25 using namespace llvm;
26 using namespace llvm::jitlink;
27 using namespace llvm::jitlink::riscv;
28 
29 namespace {
30 
31 class PerGraphGOTAndPLTStubsBuilder_ELF_riscv
33  PerGraphGOTAndPLTStubsBuilder_ELF_riscv> {
34 public:
35  static constexpr size_t StubEntrySize = 16;
36  static const uint8_t NullGOTEntryContent[8];
37  static const uint8_t RV64StubContent[StubEntrySize];
38  static const uint8_t RV32StubContent[StubEntrySize];
39 
41  PerGraphGOTAndPLTStubsBuilder_ELF_riscv>::PerGraphGOTAndPLTStubsBuilder;
42 
43  bool isRV64() const { return G.getPointerSize() == 8; }
44 
45  bool isGOTEdgeToFix(Edge &E) const { return E.getKind() == R_RISCV_GOT_HI20; }
46 
47  Symbol &createGOTEntry(Symbol &Target) {
48  Block &GOTBlock =
49  G.createContentBlock(getGOTSection(), getGOTEntryBlockContent(),
50  orc::ExecutorAddr(), G.getPointerSize(), 0);
51  GOTBlock.addEdge(isRV64() ? R_RISCV_64 : R_RISCV_32, 0, Target, 0);
52  return G.addAnonymousSymbol(GOTBlock, 0, G.getPointerSize(), false, false);
53  }
54 
55  Symbol &createPLTStub(Symbol &Target) {
56  Block &StubContentBlock = G.createContentBlock(
57  getStubsSection(), getStubBlockContent(), orc::ExecutorAddr(), 4, 0);
58  auto &GOTEntrySymbol = getGOTEntry(Target);
59  StubContentBlock.addEdge(R_RISCV_CALL, 0, GOTEntrySymbol, 0);
60  return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true,
61  false);
62  }
63 
64  void fixGOTEdge(Edge &E, Symbol &GOTEntry) {
65  // Replace the relocation pair (R_RISCV_GOT_HI20, R_RISCV_PCREL_LO12)
66  // with (R_RISCV_PCREL_HI20, R_RISCV_PCREL_LO12)
67  // Therefore, here just change the R_RISCV_GOT_HI20 to R_RISCV_PCREL_HI20
68  E.setKind(R_RISCV_PCREL_HI20);
69  E.setTarget(GOTEntry);
70  }
71 
72  void fixPLTEdge(Edge &E, Symbol &PLTStubs) {
73  assert(E.getKind() == R_RISCV_CALL_PLT && "Not a R_RISCV_CALL_PLT edge?");
74  E.setKind(R_RISCV_CALL);
75  E.setTarget(PLTStubs);
76  }
77 
78  bool isExternalBranchEdge(Edge &E) const {
79  return E.getKind() == R_RISCV_CALL_PLT;
80  }
81 
82 private:
83  Section &getGOTSection() const {
84  if (!GOTSection)
85  GOTSection = &G.createSection("$__GOT", orc::MemProt::Read);
86  return *GOTSection;
87  }
88 
89  Section &getStubsSection() const {
90  if (!StubsSection)
91  StubsSection =
92  &G.createSection("$__STUBS", orc::MemProt::Read | orc::MemProt::Exec);
93  return *StubsSection;
94  }
95 
96  ArrayRef<char> getGOTEntryBlockContent() {
97  return {reinterpret_cast<const char *>(NullGOTEntryContent),
98  G.getPointerSize()};
99  }
100 
101  ArrayRef<char> getStubBlockContent() {
102  auto StubContent = isRV64() ? RV64StubContent : RV32StubContent;
103  return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
104  }
105 
106  mutable Section *GOTSection = nullptr;
107  mutable Section *StubsSection = nullptr;
108 };
109 
110 const uint8_t PerGraphGOTAndPLTStubsBuilder_ELF_riscv::NullGOTEntryContent[8] =
111  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
112 
113 const uint8_t
114  PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV64StubContent[StubEntrySize] = {
115  0x17, 0x0e, 0x00, 0x00, // auipc t3, literal
116  0x03, 0x3e, 0x0e, 0x00, // ld t3, literal(t3)
117  0x67, 0x00, 0x0e, 0x00, // jr t3
118  0x13, 0x00, 0x00, 0x00}; // nop
119 
120 const uint8_t
121  PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV32StubContent[StubEntrySize] = {
122  0x17, 0x0e, 0x00, 0x00, // auipc t3, literal
123  0x03, 0x2e, 0x0e, 0x00, // lw t3, literal(t3)
124  0x67, 0x00, 0x0e, 0x00, // jr t3
125  0x13, 0x00, 0x00, 0x00}; // nop
126 } // namespace
127 namespace llvm {
128 namespace jitlink {
129 
131  using namespace riscv;
132  assert((E.getKind() == R_RISCV_PCREL_LO12_I ||
133  E.getKind() == R_RISCV_PCREL_LO12_S) &&
134  "Can only have high relocation for R_RISCV_PCREL_LO12_I or "
135  "R_RISCV_PCREL_LO12_S");
136 
137  const Symbol &Sym = E.getTarget();
138  const Block &B = Sym.getBlock();
139  orc::ExecutorAddrDiff Offset = Sym.getOffset();
140 
141  struct Comp {
142  bool operator()(const Edge &Lhs, orc::ExecutorAddrDiff Offset) {
143  return Lhs.getOffset() < Offset;
144  }
145  bool operator()(orc::ExecutorAddrDiff Offset, const Edge &Rhs) {
146  return Offset < Rhs.getOffset();
147  }
148  };
149 
150  auto Bound =
151  std::equal_range(B.edges().begin(), B.edges().end(), Offset, Comp{});
152 
153  for (auto It = Bound.first; It != Bound.second; ++It) {
154  if (It->getKind() == R_RISCV_PCREL_HI20)
155  return *It;
156  }
157 
158  return make_error<JITLinkError>(
159  "No HI20 PCREL relocation type be found for LO12 PCREL relocation type");
160 }
161 
162 static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) {
163  return (Num & (((1ULL << Size) - 1) << Low)) >> Low;
164 }
165 
166 static inline bool isAlignmentCorrect(uint64_t Value, int N) {
167  return (Value & (N - 1)) ? false : true;
168 }
169 
170 // Requires 0 < N <= 64.
171 static inline bool isInRangeForImm(int64_t Value, int N) {
172  return Value == llvm::SignExtend64(Value, N);
173 }
174 
175 class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
177 
178 public:
179  ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx,
180  std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig)
181  : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
182 
183 private:
184  Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
185  using namespace riscv;
186  using namespace llvm::support;
187 
188  char *BlockWorkingMem = B.getAlreadyMutableContent().data();
189  char *FixupPtr = BlockWorkingMem + E.getOffset();
190  orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
191  switch (E.getKind()) {
192  case R_RISCV_32: {
193  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
194  *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
195  break;
196  }
197  case R_RISCV_64: {
198  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
199  *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
200  break;
201  }
202  case R_RISCV_BRANCH: {
203  int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
204  if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 12)))
205  return makeTargetOutOfRangeError(G, B, E);
207  return makeAlignmentError(FixupAddress, Value, 2, E);
208  uint32_t Imm31_25 =
209  extractBits(Value, 5, 6) << 25 | extractBits(Value, 12, 1) << 31;
210  uint32_t Imm11_7 =
211  extractBits(Value, 1, 4) << 8 | extractBits(Value, 11, 1) << 7;
212  uint32_t RawInstr = *(little32_t *)FixupPtr;
213  *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
214  break;
215  }
216  case R_RISCV_JAL: {
217  int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
218  if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 20)))
219  return makeTargetOutOfRangeError(G, B, E);
221  return makeAlignmentError(FixupAddress, Value, 2, E);
222  uint32_t Imm20 = extractBits(Value, 20, 1) << 31;
223  uint32_t Imm10_1 = extractBits(Value, 1, 10) << 21;
224  uint32_t Imm11 = extractBits(Value, 11, 1) << 20;
225  uint32_t Imm19_12 = extractBits(Value, 12, 8) << 12;
226  uint32_t RawInstr = *(little32_t *)FixupPtr;
227  *(little32_t *)FixupPtr = RawInstr | Imm20 | Imm10_1 | Imm11 | Imm19_12;
228  break;
229  }
230  case R_RISCV_HI20: {
231  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
232  int64_t Hi = Value + 0x800;
233  if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
234  return makeTargetOutOfRangeError(G, B, E);
235  uint32_t RawInstr = *(little32_t *)FixupPtr;
236  *(little32_t *)FixupPtr =
237  (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
238  break;
239  }
240  case R_RISCV_LO12_I: {
241  // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
242  // with current relocation R_RISCV_LO12_I. So here may need a check.
243  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
244  int32_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_LO12_S: {
251  // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs
252  // with current relocation R_RISCV_LO12_S. So here may need a check.
253  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
254  int64_t Lo = Value & 0xFFF;
255  uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
256  uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
257  uint32_t RawInstr = *(little32_t *)FixupPtr;
258  *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
259  break;
260  }
261  case R_RISCV_CALL: {
262  int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
263  int64_t Hi = Value + 0x800;
264  if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
265  return makeTargetOutOfRangeError(G, B, E);
266  int32_t Lo = Value & 0xFFF;
267  uint32_t RawInstrAuipc = *(little32_t *)FixupPtr;
268  uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4);
269  *(little32_t *)FixupPtr =
270  RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000));
271  *(little32_t *)(FixupPtr + 4) =
272  RawInstrJalr | (static_cast<uint32_t>(Lo) << 20);
273  break;
274  }
275  case R_RISCV_PCREL_HI20: {
276  int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
277  int64_t Hi = Value + 0x800;
278  if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32)))
279  return makeTargetOutOfRangeError(G, B, E);
280  uint32_t RawInstr = *(little32_t *)FixupPtr;
281  *(little32_t *)FixupPtr =
282  (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000));
283  break;
284  }
285  case R_RISCV_PCREL_LO12_I: {
286  // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
287  // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a
288  // check.
289  auto RelHI20 = getRISCVPCRelHi20(E);
290  if (!RelHI20)
291  return RelHI20.takeError();
292  int64_t Value = RelHI20->getTarget().getAddress() +
293  RelHI20->getAddend() - E.getTarget().getAddress();
294  int64_t Lo = Value & 0xFFF;
295  uint32_t RawInstr = *(little32_t *)FixupPtr;
296  *(little32_t *)FixupPtr =
297  (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20);
298  break;
299  }
300  case R_RISCV_PCREL_LO12_S: {
301  // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and
302  // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a
303  // check.
304  auto RelHI20 = getRISCVPCRelHi20(E);
305  int64_t Value = RelHI20->getTarget().getAddress() +
306  RelHI20->getAddend() - E.getTarget().getAddress();
307  int64_t Lo = Value & 0xFFF;
308  uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25;
309  uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7;
310  uint32_t RawInstr = *(little32_t *)FixupPtr;
311 
312  *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7;
313  break;
314  }
315  case R_RISCV_ADD64: {
316  int64_t Value = (E.getTarget().getAddress() +
317  support::endian::read64le(reinterpret_cast<const void *>(
318  FixupAddress.getValue())) +
319  E.getAddend())
320  .getValue();
321  *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
322  break;
323  }
324  case R_RISCV_ADD32: {
325  int64_t Value = (E.getTarget().getAddress() +
326  support::endian::read32le(reinterpret_cast<const void *>(
327  FixupAddress.getValue())) +
328  E.getAddend())
329  .getValue();
330  *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
331  break;
332  }
333  case R_RISCV_ADD16: {
334  int64_t Value = (E.getTarget().getAddress() +
335  support::endian::read16le(reinterpret_cast<const void *>(
336  FixupAddress.getValue())) +
337  E.getAddend())
338  .getValue();
339  *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
340  break;
341  }
342  case R_RISCV_ADD8: {
343  int64_t Value =
344  (E.getTarget().getAddress() +
345  *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) +
346  E.getAddend())
347  .getValue();
348  *FixupPtr = static_cast<uint8_t>(Value);
349  break;
350  }
351  case R_RISCV_SUB64: {
352  int64_t Value = support::endian::read64le(reinterpret_cast<const void *>(
353  FixupAddress.getValue())) -
354  E.getTarget().getAddress().getValue() - E.getAddend();
355  *(little64_t *)FixupPtr = static_cast<uint64_t>(Value);
356  break;
357  }
358  case R_RISCV_SUB32: {
359  int64_t Value = support::endian::read32le(reinterpret_cast<const void *>(
360  FixupAddress.getValue())) -
361  E.getTarget().getAddress().getValue() - E.getAddend();
362  *(little32_t *)FixupPtr = static_cast<uint32_t>(Value);
363  break;
364  }
365  case R_RISCV_SUB16: {
366  int64_t Value = support::endian::read16le(reinterpret_cast<const void *>(
367  FixupAddress.getValue())) -
368  E.getTarget().getAddress().getValue() - E.getAddend();
369  *(little16_t *)FixupPtr = static_cast<uint32_t>(Value);
370  break;
371  }
372  case R_RISCV_SUB8: {
373  int64_t Value =
374  *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) -
375  E.getTarget().getAddress().getValue() - E.getAddend();
376  *FixupPtr = static_cast<uint8_t>(Value);
377  break;
378  }
379  case R_RISCV_SUB6: {
380  int64_t Value =
381  *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) & 0x3f;
382  Value -= E.getTarget().getAddress().getValue() - E.getAddend();
383  *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<uint8_t>(Value) & 0x3f);
384  break;
385  }
386  case R_RISCV_SET6: {
387  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
388  uint32_t RawData = *(little32_t *)FixupPtr;
389  int64_t Word6 = Value & 0x3f;
390  *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6;
391  break;
392  }
393  case R_RISCV_SET8: {
394  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
395  uint32_t RawData = *(little32_t *)FixupPtr;
396  int64_t Word8 = Value & 0xff;
397  *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8;
398  break;
399  }
400  case R_RISCV_SET16: {
401  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
402  uint32_t RawData = *(little32_t *)FixupPtr;
403  int64_t Word16 = Value & 0xffff;
404  *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16;
405  break;
406  }
407  case R_RISCV_SET32: {
408  int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
409  int64_t Word32 = Value & 0xffffffff;
410  *(little32_t *)FixupPtr = Word32;
411  break;
412  }
413  case R_RISCV_32_PCREL: {
414  int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
415  int64_t Word32 = Value & 0xffffffff;
416  *(little32_t *)FixupPtr = Word32;
417  break;
418  }
419  }
420  return Error::success();
421  }
422 };
423 
424 template <typename ELFT>
426 private:
428  getRelocationKind(const uint32_t Type) {
429  using namespace riscv;
430  switch (Type) {
431  case ELF::R_RISCV_32:
433  case ELF::R_RISCV_64:
435  case ELF::R_RISCV_BRANCH:
437  case ELF::R_RISCV_JAL:
439  case ELF::R_RISCV_HI20:
441  case ELF::R_RISCV_LO12_I:
443  case ELF::R_RISCV_LO12_S:
445  case ELF::R_RISCV_CALL:
457  case ELF::R_RISCV_ADD64:
459  case ELF::R_RISCV_ADD32:
461  case ELF::R_RISCV_ADD16:
463  case ELF::R_RISCV_ADD8:
465  case ELF::R_RISCV_SUB64:
467  case ELF::R_RISCV_SUB32:
469  case ELF::R_RISCV_SUB16:
471  case ELF::R_RISCV_SUB8:
473  case ELF::R_RISCV_SUB6:
475  case ELF::R_RISCV_SET6:
477  case ELF::R_RISCV_SET8:
479  case ELF::R_RISCV_SET16:
481  case ELF::R_RISCV_SET32:
485  }
486 
487  return make_error<JITLinkError>(
488  "Unsupported riscv relocation:" + formatv("{0:d}: ", Type) +
490  }
491 
492  Error addRelocations() override {
493  LLVM_DEBUG(dbgs() << "Processing relocations:\n");
494 
496  using Self = ELFLinkGraphBuilder_riscv<ELFT>;
497  for (const auto &RelSect : Base::Sections)
498  if (Error Err = Base::forEachRelaRelocation(RelSect, this,
499  &Self::addSingleRelocation))
500  return Err;
501 
502  return Error::success();
503  }
504 
505  Error addSingleRelocation(const typename ELFT::Rela &Rel,
506  const typename ELFT::Shdr &FixupSect,
507  Block &BlockToFix) {
509 
510  uint32_t Type = Rel.getType(false);
511  // We do not implement linker relaxation, except what is required for
512  // alignment (see below).
513  if (Type == llvm::ELF::R_RISCV_RELAX)
514  return Error::success();
515 
516  int64_t Addend = Rel.r_addend;
517  if (Type == llvm::ELF::R_RISCV_ALIGN) {
518  uint64_t Alignment = PowerOf2Ceil(Addend);
519  // FIXME: Implement support for ensuring alignment together with linker
520  // relaxation; 2 bytes are guaranteed by the length of compressed
521  // instructions, so this does not need any action from our side.
522  if (Alignment > 2)
523  return make_error<JITLinkError>(
524  formatv("Unsupported relocation R_RISCV_ALIGN with alignment {0} "
525  "larger than 2 (addend: {1})",
526  Alignment, Addend));
527  return Error::success();
528  }
529 
530  Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type);
531  if (!Kind)
532  return Kind.takeError();
533 
534  uint32_t SymbolIndex = Rel.getSymbol(false);
535  auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
536  if (!ObjSymbol)
537  return ObjSymbol.takeError();
538 
539  Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
540  if (!GraphSymbol)
541  return make_error<StringError>(
542  formatv("Could not find symbol at given index, did you add it to "
543  "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
544  SymbolIndex, (*ObjSymbol)->st_shndx,
545  Base::GraphSymbols.size()),
547 
548  auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
549  Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
550  Edge GE(*Kind, Offset, *GraphSymbol, Addend);
551  LLVM_DEBUG({
552  dbgs() << " ";
553  printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind));
554  dbgs() << "\n";
555  });
556 
557  BlockToFix.addEdge(std::move(GE));
558  return Error::success();
559  }
560 
561 public:
563  const object::ELFFile<ELFT> &Obj, const Triple T)
564  : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName,
565  riscv::getEdgeKindName) {}
566 };
567 
570  LLVM_DEBUG({
571  dbgs() << "Building jitlink graph for new input "
572  << ObjectBuffer.getBufferIdentifier() << "...\n";
573  });
574 
575  auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
576  if (!ELFObj)
577  return ELFObj.takeError();
578 
579  if ((*ELFObj)->getArch() == Triple::riscv64) {
580  auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
582  (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
583  (*ELFObj)->makeTriple())
584  .buildGraph();
585  } else {
586  assert((*ELFObj)->getArch() == Triple::riscv32 &&
587  "Invalid triple for RISCV ELF object file");
588  auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
590  (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
591  (*ELFObj)->makeTriple())
592  .buildGraph();
593  }
594 }
595 
596 void link_ELF_riscv(std::unique_ptr<LinkGraph> G,
597  std::unique_ptr<JITLinkContext> Ctx) {
598  PassConfiguration Config;
599  const Triple &TT = G->getTargetTriple();
600  if (Ctx->shouldAddDefaultTargetPasses(TT)) {
601  if (auto MarkLive = Ctx->getMarkLivePass(TT))
602  Config.PrePrunePasses.push_back(std::move(MarkLive));
603  else
604  Config.PrePrunePasses.push_back(markAllSymbolsLive);
605  Config.PostPrunePasses.push_back(
606  PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass);
607  }
608  if (auto Err = Ctx->modifyPassConfig(*G, Config))
609  return Ctx->notifyFailed(std::move(Err));
610 
612 }
613 
614 } // namespace jitlink
615 } // namespace llvm
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
llvm::Triple::riscv64
@ riscv64
Definition: Triple.h:76
llvm::Hexagon::Word16
@ Word16
Definition: HexagonFixupKinds.h:124
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::support::detail::packed_endian_specific_integral
Definition: Endian.h:206
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
llvm::support::endian::read16le
uint16_t read16le(const void *P)
Definition: Endian.h:380
llvm::MipsISD::Lo
@ Lo
Definition: MipsISelLowering.h:79
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::orc::ExecutorAddr::getValue
uint64_t getValue() const
Definition: ExecutorAddress.h:105
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::PowerOf2Ceil
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:630
llvm::Hexagon::Word32
@ Word32
Definition: HexagonFixupKinds.h:125
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::MipsISD::Hi
@ Hi
Definition: MipsISelLowering.h:75
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:251
ELF.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
false
Function Alias Analysis false
Definition: AliasAnalysis.cpp:772
llvm::orc::MemProt::Read
@ Read
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::ThreadPriority::Low
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
llvm::object::ObjectFile::createELFObjectFile
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Definition: ELFObjectFile.cpp:71
llvm::ELF::EM_RISCV
@ EM_RISCV
Definition: ELF.h:317
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::object::getELFRelocationTypeName
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
uint64_t
ELF.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1836
riscv.h
llvm::AArch64CC::GE
@ GE
Definition: AArch64BaseInfo.h:265
llvm::ArrayRef< char >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
uint32_t
ELF_riscv.h
llvm::SignExtend64
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:718
ELFObjectFile.h
PerGraphGOTAndPLTStubsBuilder.h
std
Definition: BitVector.h:851
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::Triple::riscv32
@ riscv32
Definition: Triple.h:75
llvm::support
Definition: Endian.h:25
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::support::endian::read64le
uint64_t read64le(const void *P)
Definition: Endian.h:382
llvm::support::endian::read32le
uint32_t read32le(const void *P)
Definition: Endian.h:381
N
#define N
llvm::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
ELFLinkGraphBuilder.h
llvm::orc::MemProt::Exec
@ Exec
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:210
Endian.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::object::ELFFile
Definition: ELF.h:95
JITLinkGeneric.h
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
Shdr
Elf_Shdr Shdr
Definition: ELFObjHandler.cpp:78
llvm::Hexagon::Word8
@ Word8
Definition: HexagonFixupKinds.h:123