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 23 #define DEBUG_TYPE "jitlink" 24 using namespace llvm; 25 using namespace llvm::jitlink; 26 using namespace llvm::jitlink::riscv; 27 28 namespace { 29 30 class PerGraphGOTAndPLTStubsBuilder_ELF_riscv 31 : public PerGraphGOTAndPLTStubsBuilder< 32 PerGraphGOTAndPLTStubsBuilder_ELF_riscv> { 33 public: 34 static constexpr size_t StubEntrySize = 16; 35 static const uint8_t NullGOTEntryContent[8]; 36 static const uint8_t RV64StubContent[StubEntrySize]; 37 static const uint8_t RV32StubContent[StubEntrySize]; 38 39 using PerGraphGOTAndPLTStubsBuilder< 40 PerGraphGOTAndPLTStubsBuilder_ELF_riscv>::PerGraphGOTAndPLTStubsBuilder; 41 42 bool isRV64() const { return G.getPointerSize() == 8; } 43 44 bool isGOTEdgeToFix(Edge &E) const { return E.getKind() == R_RISCV_GOT_HI20; } 45 46 Symbol &createGOTEntry(Symbol &Target) { 47 Block &GOTBlock = G.createContentBlock( 48 getGOTSection(), getGOTEntryBlockContent(), 0, G.getPointerSize(), 0); 49 GOTBlock.addEdge(isRV64() ? R_RISCV_64 : R_RISCV_32, 0, Target, 0); 50 return G.addAnonymousSymbol(GOTBlock, 0, G.getPointerSize(), false, false); 51 } 52 53 Symbol &createPLTStub(Symbol &Target) { 54 Block &StubContentBlock = 55 G.createContentBlock(getStubsSection(), getStubBlockContent(), 0, 4, 0); 56 auto &GOTEntrySymbol = getGOTEntry(Target); 57 StubContentBlock.addEdge(R_RISCV_CALL, 0, GOTEntrySymbol, 0); 58 return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, 59 false); 60 } 61 62 void fixGOTEdge(Edge &E, Symbol &GOTEntry) { 63 // Replace the relocation pair (R_RISCV_GOT_HI20, R_RISCV_PCREL_LO12) 64 // with (R_RISCV_PCREL_HI20, R_RISCV_PCREL_LO12) 65 // Therefore, here just change the R_RISCV_GOT_HI20 to R_RISCV_PCREL_HI20 66 E.setKind(R_RISCV_PCREL_HI20); 67 E.setTarget(GOTEntry); 68 } 69 70 void fixPLTEdge(Edge &E, Symbol &PLTStubs) { 71 assert(E.getKind() == R_RISCV_CALL_PLT && "Not a R_RISCV_CALL_PLT edge?"); 72 E.setKind(R_RISCV_CALL); 73 E.setTarget(PLTStubs); 74 } 75 76 bool isExternalBranchEdge(Edge &E) const { 77 return E.getKind() == R_RISCV_CALL_PLT; 78 } 79 80 private: 81 Section &getGOTSection() const { 82 if (!GOTSection) 83 GOTSection = &G.createSection("$__GOT", sys::Memory::MF_READ); 84 return *GOTSection; 85 } 86 87 Section &getStubsSection() const { 88 if (!StubsSection) { 89 auto StubsProt = static_cast<sys::Memory::ProtectionFlags>( 90 sys::Memory::MF_READ | sys::Memory::MF_EXEC); 91 StubsSection = &G.createSection("$__STUBS", StubsProt); 92 } 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 JITTargetAddress Offset = Sym.getOffset(); 140 141 struct Comp { 142 bool operator()(const Edge &Lhs, JITTargetAddress Offset) { 143 return Lhs.getOffset() < Offset; 144 } 145 bool operator()(JITTargetAddress 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(uint64_t Num, unsigned High, unsigned Low) { 163 return (Num & ((1ULL << (High + 1)) - 1)) >> Low; 164 } 165 166 class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> { 167 friend class JITLinker<ELFJITLinker_riscv>; 168 169 public: 170 ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx, 171 std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig) 172 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {} 173 174 private: 175 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { 176 using namespace riscv; 177 using namespace llvm::support; 178 179 char *BlockWorkingMem = B.getAlreadyMutableContent().data(); 180 char *FixupPtr = BlockWorkingMem + E.getOffset(); 181 JITTargetAddress FixupAddress = B.getAddress() + E.getOffset(); 182 switch (E.getKind()) { 183 case R_RISCV_32: { 184 int64_t Value = E.getTarget().getAddress() + E.getAddend(); 185 *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 186 break; 187 } 188 case R_RISCV_64: { 189 int64_t Value = E.getTarget().getAddress() + E.getAddend(); 190 *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 191 break; 192 } 193 case R_RISCV_HI20: { 194 int64_t Value = E.getTarget().getAddress() + E.getAddend(); 195 int32_t Hi = (Value + 0x800) & 0xFFFFF000; 196 uint32_t RawInstr = *(little32_t *)FixupPtr; 197 *(little32_t *)FixupPtr = (RawInstr & 0xFFF) | static_cast<uint32_t>(Hi); 198 break; 199 } 200 case R_RISCV_LO12_I: { 201 int64_t Value = E.getTarget().getAddress() + E.getAddend(); 202 int32_t Lo = Value & 0xFFF; 203 uint32_t RawInstr = *(little32_t *)FixupPtr; 204 *(little32_t *)FixupPtr = 205 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 206 break; 207 } 208 case R_RISCV_CALL: { 209 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 210 int32_t Hi = (Value + 0x800) & 0xFFFFF000; 211 int32_t Lo = Value & 0xFFF; 212 uint32_t RawInstrAuipc = *(little32_t *)FixupPtr; 213 uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4); 214 *(little32_t *)FixupPtr = RawInstrAuipc | static_cast<uint32_t>(Hi); 215 *(little32_t *)(FixupPtr + 4) = 216 RawInstrJalr | (static_cast<uint32_t>(Lo) << 20); 217 break; 218 } 219 case R_RISCV_PCREL_HI20: { 220 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 221 int32_t Hi = (Value + 0x800) & 0xFFFFF000; 222 uint32_t RawInstr = *(little32_t *)FixupPtr; 223 *(little32_t *)FixupPtr = (RawInstr & 0xFFF) | static_cast<uint32_t>(Hi); 224 break; 225 } 226 case R_RISCV_PCREL_LO12_I: { 227 auto RelHI20 = getRISCVPCRelHi20(E); 228 if (!RelHI20) 229 return RelHI20.takeError(); 230 int64_t Value = RelHI20->getTarget().getAddress() + 231 RelHI20->getAddend() - E.getTarget().getAddress(); 232 int64_t Lo = Value & 0xFFF; 233 uint32_t RawInstr = *(little32_t *)FixupPtr; 234 *(little32_t *)FixupPtr = 235 (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 236 break; 237 } 238 case R_RISCV_PCREL_LO12_S: { 239 auto RelHI20 = getRISCVPCRelHi20(E); 240 int64_t Value = RelHI20->getTarget().getAddress() + 241 RelHI20->getAddend() - E.getTarget().getAddress(); 242 int64_t Lo = Value & 0xFFF; 243 uint32_t Imm31_25 = extractBits(Lo, 11, 5) << 25; 244 uint32_t Imm11_7 = extractBits(Lo, 4, 0) << 7; 245 uint32_t RawInstr = *(little32_t *)FixupPtr; 246 247 *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7; 248 break; 249 } 250 } 251 return Error::success(); 252 } 253 }; 254 255 template <typename ELFT> 256 class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> { 257 private: 258 static Expected<riscv::EdgeKind_riscv> 259 getRelocationKind(const uint32_t Type) { 260 using namespace riscv; 261 switch (Type) { 262 case ELF::R_RISCV_32: 263 return EdgeKind_riscv::R_RISCV_32; 264 case ELF::R_RISCV_64: 265 return EdgeKind_riscv::R_RISCV_64; 266 case ELF::R_RISCV_HI20: 267 return EdgeKind_riscv::R_RISCV_HI20; 268 case ELF::R_RISCV_LO12_I: 269 return EdgeKind_riscv::R_RISCV_LO12_I; 270 case ELF::R_RISCV_CALL: 271 return EdgeKind_riscv::R_RISCV_CALL; 272 case ELF::R_RISCV_PCREL_HI20: 273 return EdgeKind_riscv::R_RISCV_PCREL_HI20; 274 case ELF::R_RISCV_PCREL_LO12_I: 275 return EdgeKind_riscv::R_RISCV_PCREL_LO12_I; 276 case ELF::R_RISCV_PCREL_LO12_S: 277 return EdgeKind_riscv::R_RISCV_PCREL_LO12_S; 278 case ELF::R_RISCV_GOT_HI20: 279 return EdgeKind_riscv::R_RISCV_GOT_HI20; 280 case ELF::R_RISCV_CALL_PLT: 281 return EdgeKind_riscv::R_RISCV_CALL_PLT; 282 } 283 284 return make_error<JITLinkError>("Unsupported riscv relocation:" + 285 formatv("{0:d}", Type)); 286 } 287 288 Error addRelocations() override { 289 LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 290 291 using Base = ELFLinkGraphBuilder<ELFT>; 292 using Self = ELFLinkGraphBuilder_riscv<ELFT>; 293 for (const auto &RelSect : Base::Sections) 294 if (Error Err = Base::forEachRelocation(RelSect, this, 295 &Self::addSingleRelocation)) 296 return Err; 297 298 return Error::success(); 299 } 300 301 Error addSingleRelocation(const typename ELFT::Rela &Rel, 302 const typename ELFT::Shdr &FixupSect, 303 Section &GraphSection) { 304 using Base = ELFLinkGraphBuilder<ELFT>; 305 306 uint32_t SymbolIndex = Rel.getSymbol(false); 307 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec); 308 if (!ObjSymbol) 309 return ObjSymbol.takeError(); 310 311 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex); 312 if (!GraphSymbol) 313 return make_error<StringError>( 314 formatv("Could not find symbol at given index, did you add it to " 315 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}", 316 SymbolIndex, (*ObjSymbol)->st_shndx, 317 Base::GraphSymbols.size()), 318 inconvertibleErrorCode()); 319 320 uint32_t Type = Rel.getType(false); 321 Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type); 322 if (!Kind) 323 return Kind.takeError(); 324 325 int64_t Addend = Rel.r_addend; 326 Block *BlockToFix = *(GraphSection.blocks().begin()); 327 JITTargetAddress FixupAddress = FixupSect.sh_addr + Rel.r_offset; 328 Edge::OffsetT Offset = FixupAddress - BlockToFix->getAddress(); 329 Edge GE(*Kind, Offset, *GraphSymbol, Addend); 330 LLVM_DEBUG({ 331 dbgs() << " "; 332 printEdge(dbgs(), *BlockToFix, GE, riscv::getEdgeKindName(*Kind)); 333 dbgs() << "\n"; 334 }); 335 336 BlockToFix->addEdge(std::move(GE)); 337 return Error::success(); 338 } 339 340 public: 341 ELFLinkGraphBuilder_riscv(StringRef FileName, 342 const object::ELFFile<ELFT> &Obj, const Triple T) 343 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(T), FileName, 344 riscv::getEdgeKindName) {} 345 }; 346 347 Expected<std::unique_ptr<LinkGraph>> 348 createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) { 349 LLVM_DEBUG({ 350 dbgs() << "Building jitlink graph for new input " 351 << ObjectBuffer.getBufferIdentifier() << "...\n"; 352 }); 353 354 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer); 355 if (!ELFObj) 356 return ELFObj.takeError(); 357 358 if ((*ELFObj)->getArch() == Triple::riscv64) { 359 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj); 360 return ELFLinkGraphBuilder_riscv<object::ELF64LE>( 361 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 362 (*ELFObj)->makeTriple()) 363 .buildGraph(); 364 } else { 365 assert((*ELFObj)->getArch() == Triple::riscv32 && 366 "Invalid triple for RISCV ELF object file"); 367 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj); 368 return ELFLinkGraphBuilder_riscv<object::ELF32LE>( 369 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 370 (*ELFObj)->makeTriple()) 371 .buildGraph(); 372 } 373 } 374 375 void link_ELF_riscv(std::unique_ptr<LinkGraph> G, 376 std::unique_ptr<JITLinkContext> Ctx) { 377 PassConfiguration Config; 378 const Triple &TT = G->getTargetTriple(); 379 if (Ctx->shouldAddDefaultTargetPasses(TT)) { 380 if (auto MarkLive = Ctx->getMarkLivePass(TT)) 381 Config.PrePrunePasses.push_back(std::move(MarkLive)); 382 else 383 Config.PrePrunePasses.push_back(markAllSymbolsLive); 384 Config.PostPrunePasses.push_back( 385 PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass); 386 } 387 if (auto Err = Ctx->modifyPassConfig(*G, Config)) 388 return Ctx->notifyFailed(std::move(Err)); 389 390 ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config)); 391 } 392 393 } // namespace jitlink 394 } // namespace llvm 395