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 13 #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h" 14 #include "ELFLinkGraphBuilder.h" 15 #include "JITLinkGeneric.h" 16 #include "PerGraphGOTAndPLTStubsBuilder.h" 17 #include "llvm/BinaryFormat/ELF.h" 18 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 19 #include "llvm/ExecutionEngine/JITLink/riscv.h" 20 #include "llvm/Object/ELF.h" 21 #include "llvm/Object/ELFObjectFile.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 32 : public PerGraphGOTAndPLTStubsBuilder< 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 40 using PerGraphGOTAndPLTStubsBuilder< 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", MemProt::Read); 86 return *GOTSection; 87 } 88 89 Section &getStubsSection() const { 90 if (!StubsSection) 91 StubsSection = 92 &G.createSection("$__STUBS", MemProt::Read | 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 130 static Expected<const Edge &> getRISCVPCRelHi20(const Edge &E) { 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)) - 1) << Low)) >> Low; 164 } 165 166 inline Error checkAlignment(llvm::orc::ExecutorAddr loc, uint64_t v, int n, 167 const Edge &E) { 168 if (v & (n - 1)) 169 return make_error<JITLinkError>("0x" + llvm::utohexstr(loc.getValue()) + 170 " improper alignment for relocation " + 171 formatv("{0:d}", E.getKind()) + ": 0x" + 172 llvm::utohexstr(v) + " is not aligned to " + 173 Twine(n) + " bytes"); 174 return Error::success(); 175 } 176 177 static inline bool isInRangeForImmS32(int64_t Value) { 178 return (Value >= std::numeric_limits<int32_t>::min() && 179 Value <= std::numeric_limits<int32_t>::max()); 180 } 181 182 class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> { 183 friend class JITLinker<ELFJITLinker_riscv>; 184 185 public: 186 ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx, 187 std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig) 188 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {} 189 190 private: 191 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { 192 using namespace riscv; 193 using namespace llvm::support; 194 195 char *BlockWorkingMem = B.getAlreadyMutableContent().data(); 196 char *FixupPtr = BlockWorkingMem + E.getOffset(); 197 orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset(); 198 switch (E.getKind()) { 199 case R_RISCV_32: { 200 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 201 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 202 break; 203 } 204 case R_RISCV_64: { 205 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 206 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 207 break; 208 } 209 case R_RISCV_BRANCH: { 210 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 211 Error AlignmentIssue = checkAlignment(FixupAddress, Value, 2, E); 212 if (AlignmentIssue) { 213 return AlignmentIssue; 214 } 215 int64_t Lo = Value & 0xFFF; 216 uint32_t Imm31_25 = extractBits(Lo, 5, 6) << 25 | extractBits(Lo, 12, 1) 217 << 31; 218 uint32_t Imm11_7 = extractBits(Lo, 1, 4) << 8 | extractBits(Lo, 11, 1) 219 << 7; 220 uint32_t RawInstr = *(little32_t *)FixupPtr; 221 *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7; 222 break; 223 } 224 case R_RISCV_HI20: { 225 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 226 int64_t Hi = Value + 0x800; 227 if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi))) 228 return makeTargetOutOfRangeError(G, B, E); 229 uint32_t RawInstr = *(little32_t *)FixupPtr; 230 *(little32_t *)FixupPtr = 231 (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 232 break; 233 } 234 case R_RISCV_LO12_I: { 235 // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs 236 // with current relocation R_RISCV_LO12_I. So here may need a check. 237 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 238 int32_t Lo = Value & 0xFFF; 239 uint32_t RawInstr = *(little32_t *)FixupPtr; 240 *(little32_t *)FixupPtr = 241 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 242 break; 243 } 244 case R_RISCV_CALL: { 245 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 246 int64_t Hi = Value + 0x800; 247 if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi))) 248 return makeTargetOutOfRangeError(G, B, E); 249 int32_t Lo = Value & 0xFFF; 250 uint32_t RawInstrAuipc = *(little32_t *)FixupPtr; 251 uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4); 252 *(little32_t *)FixupPtr = 253 RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 254 *(little32_t *)(FixupPtr + 4) = 255 RawInstrJalr | (static_cast<uint32_t>(Lo) << 20); 256 break; 257 } 258 case R_RISCV_PCREL_HI20: { 259 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 260 int64_t Hi = Value + 0x800; 261 if (LLVM_UNLIKELY(!isInRangeForImmS32(Hi))) 262 return makeTargetOutOfRangeError(G, B, E); 263 uint32_t RawInstr = *(little32_t *)FixupPtr; 264 *(little32_t *)FixupPtr = 265 (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 266 break; 267 } 268 case R_RISCV_PCREL_LO12_I: { 269 // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 270 // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a 271 // check. 272 auto RelHI20 = getRISCVPCRelHi20(E); 273 if (!RelHI20) 274 return RelHI20.takeError(); 275 int64_t Value = RelHI20->getTarget().getAddress() + 276 RelHI20->getAddend() - E.getTarget().getAddress(); 277 int64_t Lo = Value & 0xFFF; 278 uint32_t RawInstr = *(little32_t *)FixupPtr; 279 *(little32_t *)FixupPtr = 280 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 281 break; 282 } 283 case R_RISCV_PCREL_LO12_S: { 284 // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 285 // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a 286 // check. 287 auto RelHI20 = getRISCVPCRelHi20(E); 288 int64_t Value = RelHI20->getTarget().getAddress() + 289 RelHI20->getAddend() - E.getTarget().getAddress(); 290 int64_t Lo = Value & 0xFFF; 291 uint32_t Imm31_25 = extractBits(Lo, 5, 7) << 25; 292 uint32_t Imm11_7 = extractBits(Lo, 0, 5) << 7; 293 uint32_t RawInstr = *(little32_t *)FixupPtr; 294 295 *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7; 296 break; 297 } 298 case R_RISCV_ADD64: { 299 int64_t Value = (E.getTarget().getAddress() + 300 support::endian::read64le(reinterpret_cast<const void *>( 301 FixupAddress.getValue())) + 302 E.getAddend()) 303 .getValue(); 304 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 305 break; 306 } 307 case R_RISCV_ADD32: { 308 int64_t Value = (E.getTarget().getAddress() + 309 support::endian::read32le(reinterpret_cast<const void *>( 310 FixupAddress.getValue())) + 311 E.getAddend()) 312 .getValue(); 313 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 314 break; 315 } 316 case R_RISCV_ADD16: { 317 int64_t Value = (E.getTarget().getAddress() + 318 support::endian::read16le(reinterpret_cast<const void *>( 319 FixupAddress.getValue())) + 320 E.getAddend()) 321 .getValue(); 322 *(little16_t *)FixupPtr = static_cast<uint32_t>(Value); 323 break; 324 } 325 case R_RISCV_ADD8: { 326 int64_t Value = 327 (E.getTarget().getAddress() + 328 *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) + 329 E.getAddend()) 330 .getValue(); 331 *FixupPtr = static_cast<uint8_t>(Value); 332 break; 333 } 334 case R_RISCV_SUB64: { 335 int64_t Value = support::endian::read64le(reinterpret_cast<const void *>( 336 FixupAddress.getValue())) - 337 E.getTarget().getAddress().getValue() - E.getAddend(); 338 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 339 break; 340 } 341 case R_RISCV_SUB32: { 342 int64_t Value = support::endian::read32le(reinterpret_cast<const void *>( 343 FixupAddress.getValue())) - 344 E.getTarget().getAddress().getValue() - E.getAddend(); 345 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 346 break; 347 } 348 case R_RISCV_SUB16: { 349 int64_t Value = support::endian::read16le(reinterpret_cast<const void *>( 350 FixupAddress.getValue())) - 351 E.getTarget().getAddress().getValue() - E.getAddend(); 352 *(little16_t *)FixupPtr = static_cast<uint32_t>(Value); 353 break; 354 } 355 case R_RISCV_SUB8: { 356 int64_t Value = 357 *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) - 358 E.getTarget().getAddress().getValue() - E.getAddend(); 359 *FixupPtr = static_cast<uint8_t>(Value); 360 break; 361 } 362 case R_RISCV_SET6: { 363 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 364 uint32_t RawData = *(little32_t *)FixupPtr; 365 int64_t Word6 = Value & 0x3f; 366 *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6; 367 break; 368 } 369 case R_RISCV_SET8: { 370 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 371 uint32_t RawData = *(little32_t *)FixupPtr; 372 int64_t Word8 = Value & 0xff; 373 *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8; 374 break; 375 } 376 case R_RISCV_SET16: { 377 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 378 uint32_t RawData = *(little32_t *)FixupPtr; 379 int64_t Word16 = Value & 0xffff; 380 *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16; 381 break; 382 } 383 case R_RISCV_SET32: { 384 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 385 int64_t Word32 = Value & 0xffffffff; 386 *(little32_t *)FixupPtr = Word32; 387 break; 388 } 389 case R_RISCV_32_PCREL: { 390 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 391 int64_t Word32 = Value & 0xffffffff; 392 *(little32_t *)FixupPtr = Word32; 393 break; 394 } 395 } 396 return Error::success(); 397 } 398 }; 399 400 template <typename ELFT> 401 class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> { 402 private: 403 static Expected<riscv::EdgeKind_riscv> 404 getRelocationKind(const uint32_t Type) { 405 using namespace riscv; 406 switch (Type) { 407 case ELF::R_RISCV_32: 408 return EdgeKind_riscv::R_RISCV_32; 409 case ELF::R_RISCV_64: 410 return EdgeKind_riscv::R_RISCV_64; 411 case ELF::R_RISCV_BRANCH: 412 return EdgeKind_riscv::R_RISCV_BRANCH; 413 case ELF::R_RISCV_HI20: 414 return EdgeKind_riscv::R_RISCV_HI20; 415 case ELF::R_RISCV_LO12_I: 416 return EdgeKind_riscv::R_RISCV_LO12_I; 417 case ELF::R_RISCV_CALL: 418 return EdgeKind_riscv::R_RISCV_CALL; 419 case ELF::R_RISCV_PCREL_HI20: 420 return EdgeKind_riscv::R_RISCV_PCREL_HI20; 421 case ELF::R_RISCV_PCREL_LO12_I: 422 return EdgeKind_riscv::R_RISCV_PCREL_LO12_I; 423 case ELF::R_RISCV_PCREL_LO12_S: 424 return EdgeKind_riscv::R_RISCV_PCREL_LO12_S; 425 case ELF::R_RISCV_GOT_HI20: 426 return EdgeKind_riscv::R_RISCV_GOT_HI20; 427 case ELF::R_RISCV_CALL_PLT: 428 return EdgeKind_riscv::R_RISCV_CALL_PLT; 429 case ELF::R_RISCV_ADD64: 430 return EdgeKind_riscv::R_RISCV_ADD64; 431 case ELF::R_RISCV_ADD32: 432 return EdgeKind_riscv::R_RISCV_ADD32; 433 case ELF::R_RISCV_ADD16: 434 return EdgeKind_riscv::R_RISCV_ADD16; 435 case ELF::R_RISCV_ADD8: 436 return EdgeKind_riscv::R_RISCV_ADD8; 437 case ELF::R_RISCV_SUB64: 438 return EdgeKind_riscv::R_RISCV_SUB64; 439 case ELF::R_RISCV_SUB32: 440 return EdgeKind_riscv::R_RISCV_SUB32; 441 case ELF::R_RISCV_SUB16: 442 return EdgeKind_riscv::R_RISCV_SUB16; 443 case ELF::R_RISCV_SUB8: 444 return EdgeKind_riscv::R_RISCV_SUB8; 445 case ELF::R_RISCV_SET6: 446 return EdgeKind_riscv::R_RISCV_SET6; 447 case ELF::R_RISCV_SET8: 448 return EdgeKind_riscv::R_RISCV_SET8; 449 case ELF::R_RISCV_SET16: 450 return EdgeKind_riscv::R_RISCV_SET16; 451 case ELF::R_RISCV_SET32: 452 return EdgeKind_riscv::R_RISCV_SET32; 453 case ELF::R_RISCV_32_PCREL: 454 return EdgeKind_riscv::R_RISCV_32_PCREL; 455 } 456 457 return make_error<JITLinkError>("Unsupported riscv relocation:" + 458 formatv("{0:d}", Type)); 459 } 460 461 Error addRelocations() override { 462 LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 463 464 using Base = ELFLinkGraphBuilder<ELFT>; 465 using Self = ELFLinkGraphBuilder_riscv<ELFT>; 466 for (const auto &RelSect : Base::Sections) 467 if (Error Err = Base::forEachRelocation(RelSect, this, 468 &Self::addSingleRelocation)) 469 return Err; 470 471 return Error::success(); 472 } 473 474 Error addSingleRelocation(const typename ELFT::Rela &Rel, 475 const typename ELFT::Shdr &FixupSect, 476 Block &BlockToFix) { 477 using Base = ELFLinkGraphBuilder<ELFT>; 478 479 uint32_t SymbolIndex = Rel.getSymbol(false); 480 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec); 481 if (!ObjSymbol) 482 return ObjSymbol.takeError(); 483 484 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex); 485 if (!GraphSymbol) 486 return make_error<StringError>( 487 formatv("Could not find symbol at given index, did you add it to " 488 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}", 489 SymbolIndex, (*ObjSymbol)->st_shndx, 490 Base::GraphSymbols.size()), 491 inconvertibleErrorCode()); 492 493 uint32_t Type = Rel.getType(false); 494 Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type); 495 if (!Kind) 496 return Kind.takeError(); 497 498 int64_t Addend = Rel.r_addend; 499 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset; 500 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress(); 501 Edge GE(*Kind, Offset, *GraphSymbol, Addend); 502 LLVM_DEBUG({ 503 dbgs() << " "; 504 printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind)); 505 dbgs() << "\n"; 506 }); 507 508 BlockToFix.addEdge(std::move(GE)); 509 return Error::success(); 510 } 511 512 public: 513 ELFLinkGraphBuilder_riscv(StringRef FileName, 514 const object::ELFFile<ELFT> &Obj, const Triple T) 515 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName, 516 riscv::getEdgeKindName) {} 517 }; 518 519 Expected<std::unique_ptr<LinkGraph>> 520 createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) { 521 LLVM_DEBUG({ 522 dbgs() << "Building jitlink graph for new input " 523 << ObjectBuffer.getBufferIdentifier() << "...\n"; 524 }); 525 526 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer); 527 if (!ELFObj) 528 return ELFObj.takeError(); 529 530 if ((*ELFObj)->getArch() == Triple::riscv64) { 531 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj); 532 return ELFLinkGraphBuilder_riscv<object::ELF64LE>( 533 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 534 (*ELFObj)->makeTriple()) 535 .buildGraph(); 536 } else { 537 assert((*ELFObj)->getArch() == Triple::riscv32 && 538 "Invalid triple for RISCV ELF object file"); 539 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj); 540 return ELFLinkGraphBuilder_riscv<object::ELF32LE>( 541 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 542 (*ELFObj)->makeTriple()) 543 .buildGraph(); 544 } 545 } 546 547 void link_ELF_riscv(std::unique_ptr<LinkGraph> G, 548 std::unique_ptr<JITLinkContext> Ctx) { 549 PassConfiguration Config; 550 const Triple &TT = G->getTargetTriple(); 551 if (Ctx->shouldAddDefaultTargetPasses(TT)) { 552 if (auto MarkLive = Ctx->getMarkLivePass(TT)) 553 Config.PrePrunePasses.push_back(std::move(MarkLive)); 554 else 555 Config.PrePrunePasses.push_back(markAllSymbolsLive); 556 Config.PostPrunePasses.push_back( 557 PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass); 558 } 559 if (auto Err = Ctx->modifyPassConfig(*G, Config)) 560 return Ctx->notifyFailed(std::move(Err)); 561 562 ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config)); 563 } 564 565 } // namespace jitlink 566 } // namespace llvm 567