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", 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 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) << 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> { 176 friend class 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); 206 if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 207 return makeAlignmentError(FixupAddress, Value, 2, E); 208 uint32_t Imm12 = extractBits(Value, 12, 1) << 31; 209 uint32_t Imm10_5 = extractBits(Value, 5, 6) << 25; 210 uint32_t Imm4_1 = extractBits(Value, 1, 4) << 8; 211 uint32_t Imm11 = extractBits(Value, 11, 1) << 7; 212 uint32_t RawInstr = *(little32_t *)FixupPtr; 213 *(little32_t *)FixupPtr = 214 (RawInstr & 0x1FFF07F) | Imm12 | Imm10_5 | Imm4_1 | Imm11; 215 break; 216 } 217 case R_RISCV_JAL: { 218 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 219 if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 20))) 220 return makeTargetOutOfRangeError(G, B, E); 221 if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 222 return makeAlignmentError(FixupAddress, Value, 2, E); 223 uint32_t Imm20 = extractBits(Value, 20, 1) << 31; 224 uint32_t Imm10_1 = extractBits(Value, 1, 10) << 21; 225 uint32_t Imm11 = extractBits(Value, 11, 1) << 20; 226 uint32_t Imm19_12 = extractBits(Value, 12, 8) << 12; 227 uint32_t RawInstr = *(little32_t *)FixupPtr; 228 *(little32_t *)FixupPtr = 229 (RawInstr & 0xFFF) | Imm20 | Imm10_1 | Imm11 | Imm19_12; 230 break; 231 } 232 case R_RISCV_CALL: { 233 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 234 int64_t Hi = Value + 0x800; 235 if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 236 return makeTargetOutOfRangeError(G, B, E); 237 int32_t Lo = Value & 0xFFF; 238 uint32_t RawInstrAuipc = *(little32_t *)FixupPtr; 239 uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4); 240 *(little32_t *)FixupPtr = 241 RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 242 *(little32_t *)(FixupPtr + 4) = 243 RawInstrJalr | (static_cast<uint32_t>(Lo) << 20); 244 break; 245 } 246 // The relocations R_RISCV_CALL_PLT and R_RISCV_GOT_HI20 are handled by 247 // PerGraphGOTAndPLTStubsBuilder_ELF_riscv and are transformed into 248 // R_RISCV_CALL and R_RISCV_PCREL_HI20. 249 case R_RISCV_PCREL_HI20: { 250 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 251 int64_t Hi = Value + 0x800; 252 if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 253 return makeTargetOutOfRangeError(G, B, E); 254 uint32_t RawInstr = *(little32_t *)FixupPtr; 255 *(little32_t *)FixupPtr = 256 (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 257 break; 258 } 259 case R_RISCV_PCREL_LO12_I: { 260 // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 261 // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a 262 // check. 263 auto RelHI20 = getRISCVPCRelHi20(E); 264 if (!RelHI20) 265 return RelHI20.takeError(); 266 int64_t Value = RelHI20->getTarget().getAddress() + 267 RelHI20->getAddend() - E.getTarget().getAddress(); 268 int64_t Lo = Value & 0xFFF; 269 uint32_t RawInstr = *(little32_t *)FixupPtr; 270 *(little32_t *)FixupPtr = 271 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 272 break; 273 } 274 case R_RISCV_PCREL_LO12_S: { 275 // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 276 // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a 277 // check. 278 auto RelHI20 = getRISCVPCRelHi20(E); 279 if (!RelHI20) 280 return RelHI20.takeError(); 281 int64_t Value = RelHI20->getTarget().getAddress() + 282 RelHI20->getAddend() - E.getTarget().getAddress(); 283 int64_t Lo = Value & 0xFFF; 284 uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25; 285 uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7; 286 uint32_t RawInstr = *(little32_t *)FixupPtr; 287 288 *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0; 289 break; 290 } 291 case R_RISCV_HI20: { 292 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 293 int64_t Hi = Value + 0x800; 294 if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 295 return makeTargetOutOfRangeError(G, B, E); 296 uint32_t RawInstr = *(little32_t *)FixupPtr; 297 *(little32_t *)FixupPtr = 298 (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 299 break; 300 } 301 case R_RISCV_LO12_I: { 302 // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs 303 // with current relocation R_RISCV_LO12_I. So here may need a check. 304 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 305 int32_t Lo = Value & 0xFFF; 306 uint32_t RawInstr = *(little32_t *)FixupPtr; 307 *(little32_t *)FixupPtr = 308 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 309 break; 310 } 311 case R_RISCV_LO12_S: { 312 // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs 313 // with current relocation R_RISCV_LO12_S. So here may need a check. 314 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 315 int64_t Lo = Value & 0xFFF; 316 uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25; 317 uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7; 318 uint32_t RawInstr = *(little32_t *)FixupPtr; 319 *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0; 320 break; 321 } 322 case R_RISCV_ADD8: { 323 int64_t Value = 324 (E.getTarget().getAddress() + 325 *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) + 326 E.getAddend()) 327 .getValue(); 328 *FixupPtr = static_cast<uint8_t>(Value); 329 break; 330 } 331 case R_RISCV_ADD16: { 332 int64_t Value = (E.getTarget().getAddress() + 333 support::endian::read16le(reinterpret_cast<const void *>( 334 FixupAddress.getValue())) + 335 E.getAddend()) 336 .getValue(); 337 *(little16_t *)FixupPtr = static_cast<uint16_t>(Value); 338 break; 339 } 340 case R_RISCV_ADD32: { 341 int64_t Value = (E.getTarget().getAddress() + 342 support::endian::read32le(reinterpret_cast<const void *>( 343 FixupAddress.getValue())) + 344 E.getAddend()) 345 .getValue(); 346 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 347 break; 348 } 349 case R_RISCV_ADD64: { 350 int64_t Value = (E.getTarget().getAddress() + 351 support::endian::read64le(reinterpret_cast<const void *>( 352 FixupAddress.getValue())) + 353 E.getAddend()) 354 .getValue(); 355 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 356 break; 357 } 358 case R_RISCV_SUB8: { 359 int64_t Value = 360 *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) - 361 E.getTarget().getAddress().getValue() - E.getAddend(); 362 *FixupPtr = static_cast<uint8_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_SUB32: { 373 int64_t Value = support::endian::read32le(reinterpret_cast<const void *>( 374 FixupAddress.getValue())) - 375 E.getTarget().getAddress().getValue() - E.getAddend(); 376 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 377 break; 378 } 379 case R_RISCV_SUB64: { 380 int64_t Value = support::endian::read64le(reinterpret_cast<const void *>( 381 FixupAddress.getValue())) - 382 E.getTarget().getAddress().getValue() - E.getAddend(); 383 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 384 break; 385 } 386 case R_RISCV_RVC_BRANCH: { 387 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 388 if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 8))) 389 return makeTargetOutOfRangeError(G, B, E); 390 if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 391 return makeAlignmentError(FixupAddress, Value, 2, E); 392 uint16_t Imm8 = extractBits(Value, 8, 1) << 12; 393 uint16_t Imm4_3 = extractBits(Value, 3, 2) << 10; 394 uint16_t Imm7_6 = extractBits(Value, 6, 2) << 5; 395 uint16_t Imm2_1 = extractBits(Value, 1, 2) << 3; 396 uint16_t Imm5 = extractBits(Value, 5, 1) << 2; 397 uint16_t RawInstr = *(little16_t *)FixupPtr; 398 *(little16_t *)FixupPtr = 399 (RawInstr & 0xE383) | Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5; 400 break; 401 } 402 case R_RISCV_RVC_JUMP: { 403 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 404 if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 11))) 405 return makeTargetOutOfRangeError(G, B, E); 406 if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 407 return makeAlignmentError(FixupAddress, Value, 2, E); 408 uint16_t Imm11 = extractBits(Value, 11, 1) << 12; 409 uint16_t Imm4 = extractBits(Value, 4, 1) << 11; 410 uint16_t Imm9_8 = extractBits(Value, 8, 2) << 9; 411 uint16_t Imm10 = extractBits(Value, 10, 1) << 8; 412 uint16_t Imm6 = extractBits(Value, 6, 1) << 7; 413 uint16_t Imm7 = extractBits(Value, 7, 1) << 6; 414 uint16_t Imm3_1 = extractBits(Value, 1, 3) << 3; 415 uint16_t Imm5 = extractBits(Value, 5, 1) << 2; 416 uint16_t RawInstr = *(little16_t *)FixupPtr; 417 *(little16_t *)FixupPtr = (RawInstr & 0xE003) | Imm11 | Imm4 | Imm9_8 | 418 Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5; 419 break; 420 } 421 case R_RISCV_SUB6: { 422 int64_t Value = 423 *(reinterpret_cast<const uint8_t *>(FixupAddress.getValue())) & 0x3f; 424 Value -= E.getTarget().getAddress().getValue() - E.getAddend(); 425 *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<uint8_t>(Value) & 0x3f); 426 break; 427 } 428 case R_RISCV_SET6: { 429 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 430 uint32_t RawData = *(little32_t *)FixupPtr; 431 int64_t Word6 = Value & 0x3f; 432 *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6; 433 break; 434 } 435 case R_RISCV_SET8: { 436 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 437 uint32_t RawData = *(little32_t *)FixupPtr; 438 int64_t Word8 = Value & 0xff; 439 *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8; 440 break; 441 } 442 case R_RISCV_SET16: { 443 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 444 uint32_t RawData = *(little32_t *)FixupPtr; 445 int64_t Word16 = Value & 0xffff; 446 *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16; 447 break; 448 } 449 case R_RISCV_SET32: { 450 int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 451 int64_t Word32 = Value & 0xffffffff; 452 *(little32_t *)FixupPtr = Word32; 453 break; 454 } 455 case R_RISCV_32_PCREL: { 456 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 457 int64_t Word32 = Value & 0xffffffff; 458 *(little32_t *)FixupPtr = Word32; 459 break; 460 } 461 } 462 return Error::success(); 463 } 464 }; 465 466 template <typename ELFT> 467 class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> { 468 private: 469 static Expected<riscv::EdgeKind_riscv> 470 getRelocationKind(const uint32_t Type) { 471 using namespace riscv; 472 switch (Type) { 473 case ELF::R_RISCV_32: 474 return EdgeKind_riscv::R_RISCV_32; 475 case ELF::R_RISCV_64: 476 return EdgeKind_riscv::R_RISCV_64; 477 case ELF::R_RISCV_BRANCH: 478 return EdgeKind_riscv::R_RISCV_BRANCH; 479 case ELF::R_RISCV_JAL: 480 return EdgeKind_riscv::R_RISCV_JAL; 481 case ELF::R_RISCV_CALL: 482 return EdgeKind_riscv::R_RISCV_CALL; 483 case ELF::R_RISCV_CALL_PLT: 484 return EdgeKind_riscv::R_RISCV_CALL_PLT; 485 case ELF::R_RISCV_GOT_HI20: 486 return EdgeKind_riscv::R_RISCV_GOT_HI20; 487 case ELF::R_RISCV_PCREL_HI20: 488 return EdgeKind_riscv::R_RISCV_PCREL_HI20; 489 case ELF::R_RISCV_PCREL_LO12_I: 490 return EdgeKind_riscv::R_RISCV_PCREL_LO12_I; 491 case ELF::R_RISCV_PCREL_LO12_S: 492 return EdgeKind_riscv::R_RISCV_PCREL_LO12_S; 493 case ELF::R_RISCV_HI20: 494 return EdgeKind_riscv::R_RISCV_HI20; 495 case ELF::R_RISCV_LO12_I: 496 return EdgeKind_riscv::R_RISCV_LO12_I; 497 case ELF::R_RISCV_LO12_S: 498 return EdgeKind_riscv::R_RISCV_LO12_S; 499 case ELF::R_RISCV_ADD8: 500 return EdgeKind_riscv::R_RISCV_ADD8; 501 case ELF::R_RISCV_ADD16: 502 return EdgeKind_riscv::R_RISCV_ADD16; 503 case ELF::R_RISCV_ADD32: 504 return EdgeKind_riscv::R_RISCV_ADD32; 505 case ELF::R_RISCV_ADD64: 506 return EdgeKind_riscv::R_RISCV_ADD64; 507 case ELF::R_RISCV_SUB8: 508 return EdgeKind_riscv::R_RISCV_SUB8; 509 case ELF::R_RISCV_SUB16: 510 return EdgeKind_riscv::R_RISCV_SUB16; 511 case ELF::R_RISCV_SUB32: 512 return EdgeKind_riscv::R_RISCV_SUB32; 513 case ELF::R_RISCV_SUB64: 514 return EdgeKind_riscv::R_RISCV_SUB64; 515 case ELF::R_RISCV_RVC_BRANCH: 516 return EdgeKind_riscv::R_RISCV_RVC_BRANCH; 517 case ELF::R_RISCV_RVC_JUMP: 518 return EdgeKind_riscv::R_RISCV_RVC_JUMP; 519 case ELF::R_RISCV_SUB6: 520 return EdgeKind_riscv::R_RISCV_SUB6; 521 case ELF::R_RISCV_SET6: 522 return EdgeKind_riscv::R_RISCV_SET6; 523 case ELF::R_RISCV_SET8: 524 return EdgeKind_riscv::R_RISCV_SET8; 525 case ELF::R_RISCV_SET16: 526 return EdgeKind_riscv::R_RISCV_SET16; 527 case ELF::R_RISCV_SET32: 528 return EdgeKind_riscv::R_RISCV_SET32; 529 case ELF::R_RISCV_32_PCREL: 530 return EdgeKind_riscv::R_RISCV_32_PCREL; 531 } 532 533 return make_error<JITLinkError>( 534 "Unsupported riscv relocation:" + formatv("{0:d}: ", Type) + 535 object::getELFRelocationTypeName(ELF::EM_RISCV, Type)); 536 } 537 538 Error addRelocations() override { 539 LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 540 541 using Base = ELFLinkGraphBuilder<ELFT>; 542 using Self = ELFLinkGraphBuilder_riscv<ELFT>; 543 for (const auto &RelSect : Base::Sections) 544 if (Error Err = Base::forEachRelaRelocation(RelSect, this, 545 &Self::addSingleRelocation)) 546 return Err; 547 548 return Error::success(); 549 } 550 551 Error addSingleRelocation(const typename ELFT::Rela &Rel, 552 const typename ELFT::Shdr &FixupSect, 553 Block &BlockToFix) { 554 using Base = ELFLinkGraphBuilder<ELFT>; 555 556 uint32_t Type = Rel.getType(false); 557 // We do not implement linker relaxation, except what is required for 558 // alignment (see below). 559 if (Type == llvm::ELF::R_RISCV_RELAX) 560 return Error::success(); 561 562 int64_t Addend = Rel.r_addend; 563 if (Type == llvm::ELF::R_RISCV_ALIGN) { 564 uint64_t Alignment = PowerOf2Ceil(Addend); 565 // FIXME: Implement support for ensuring alignment together with linker 566 // relaxation; 2 bytes are guaranteed by the length of compressed 567 // instructions, so this does not need any action from our side. 568 if (Alignment > 2) 569 return make_error<JITLinkError>( 570 formatv("Unsupported relocation R_RISCV_ALIGN with alignment {0} " 571 "larger than 2 (addend: {1})", 572 Alignment, Addend)); 573 return Error::success(); 574 } 575 576 Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type); 577 if (!Kind) 578 return Kind.takeError(); 579 580 uint32_t SymbolIndex = Rel.getSymbol(false); 581 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec); 582 if (!ObjSymbol) 583 return ObjSymbol.takeError(); 584 585 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex); 586 if (!GraphSymbol) 587 return make_error<StringError>( 588 formatv("Could not find symbol at given index, did you add it to " 589 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}", 590 SymbolIndex, (*ObjSymbol)->st_shndx, 591 Base::GraphSymbols.size()), 592 inconvertibleErrorCode()); 593 594 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset; 595 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress(); 596 Edge GE(*Kind, Offset, *GraphSymbol, Addend); 597 LLVM_DEBUG({ 598 dbgs() << " "; 599 printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind)); 600 dbgs() << "\n"; 601 }); 602 603 BlockToFix.addEdge(std::move(GE)); 604 return Error::success(); 605 } 606 607 public: 608 ELFLinkGraphBuilder_riscv(StringRef FileName, 609 const object::ELFFile<ELFT> &Obj, const Triple T) 610 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName, 611 riscv::getEdgeKindName) {} 612 }; 613 614 Expected<std::unique_ptr<LinkGraph>> 615 createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) { 616 LLVM_DEBUG({ 617 dbgs() << "Building jitlink graph for new input " 618 << ObjectBuffer.getBufferIdentifier() << "...\n"; 619 }); 620 621 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer); 622 if (!ELFObj) 623 return ELFObj.takeError(); 624 625 if ((*ELFObj)->getArch() == Triple::riscv64) { 626 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj); 627 return ELFLinkGraphBuilder_riscv<object::ELF64LE>( 628 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 629 (*ELFObj)->makeTriple()) 630 .buildGraph(); 631 } else { 632 assert((*ELFObj)->getArch() == Triple::riscv32 && 633 "Invalid triple for RISCV ELF object file"); 634 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj); 635 return ELFLinkGraphBuilder_riscv<object::ELF32LE>( 636 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 637 (*ELFObj)->makeTriple()) 638 .buildGraph(); 639 } 640 } 641 642 void link_ELF_riscv(std::unique_ptr<LinkGraph> G, 643 std::unique_ptr<JITLinkContext> Ctx) { 644 PassConfiguration Config; 645 const Triple &TT = G->getTargetTriple(); 646 if (Ctx->shouldAddDefaultTargetPasses(TT)) { 647 if (auto MarkLive = Ctx->getMarkLivePass(TT)) 648 Config.PrePrunePasses.push_back(std::move(MarkLive)); 649 else 650 Config.PrePrunePasses.push_back(markAllSymbolsLive); 651 Config.PostPrunePasses.push_back( 652 PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass); 653 } 654 if (auto Err = Ctx->modifyPassConfig(*G, Config)) 655 return Ctx->notifyFailed(std::move(Err)); 656 657 ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config)); 658 } 659 660 } // namespace jitlink 661 } // namespace llvm 662