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" 145f757f3fSDimitry Andric #include "EHFrameSupportImpl.h" 15349cc55cSDimitry Andric #include "ELFLinkGraphBuilder.h" 16349cc55cSDimitry Andric #include "JITLinkGeneric.h" 17349cc55cSDimitry Andric #include "PerGraphGOTAndPLTStubsBuilder.h" 18349cc55cSDimitry Andric #include "llvm/BinaryFormat/ELF.h" 195f757f3fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/DWARFRecordSectionSplitter.h" 20fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITLink/JITLink.h" 21fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/JITLink/riscv.h" 22fe6060f1SDimitry Andric #include "llvm/Object/ELF.h" 23fe6060f1SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 2404eeddc0SDimitry Andric #include "llvm/Support/Endian.h" 25fe6060f1SDimitry Andric 26fe6060f1SDimitry Andric #define DEBUG_TYPE "jitlink" 27fe6060f1SDimitry Andric using namespace llvm; 28349cc55cSDimitry Andric using namespace llvm::jitlink; 29349cc55cSDimitry Andric using namespace llvm::jitlink::riscv; 30fe6060f1SDimitry Andric 31349cc55cSDimitry Andric namespace { 32349cc55cSDimitry Andric 33349cc55cSDimitry Andric class PerGraphGOTAndPLTStubsBuilder_ELF_riscv 34349cc55cSDimitry Andric : public PerGraphGOTAndPLTStubsBuilder< 35349cc55cSDimitry Andric PerGraphGOTAndPLTStubsBuilder_ELF_riscv> { 36349cc55cSDimitry Andric public: 37349cc55cSDimitry Andric static constexpr size_t StubEntrySize = 16; 38349cc55cSDimitry Andric static const uint8_t NullGOTEntryContent[8]; 39349cc55cSDimitry Andric static const uint8_t RV64StubContent[StubEntrySize]; 40349cc55cSDimitry Andric static const uint8_t RV32StubContent[StubEntrySize]; 41349cc55cSDimitry Andric 42349cc55cSDimitry Andric using PerGraphGOTAndPLTStubsBuilder< 43349cc55cSDimitry Andric PerGraphGOTAndPLTStubsBuilder_ELF_riscv>::PerGraphGOTAndPLTStubsBuilder; 44349cc55cSDimitry Andric 45349cc55cSDimitry Andric bool isRV64() const { return G.getPointerSize() == 8; } 46349cc55cSDimitry Andric 47349cc55cSDimitry Andric bool isGOTEdgeToFix(Edge &E) const { return E.getKind() == R_RISCV_GOT_HI20; } 48349cc55cSDimitry Andric 49349cc55cSDimitry Andric Symbol &createGOTEntry(Symbol &Target) { 5004eeddc0SDimitry Andric Block &GOTBlock = 5104eeddc0SDimitry Andric G.createContentBlock(getGOTSection(), getGOTEntryBlockContent(), 5204eeddc0SDimitry Andric orc::ExecutorAddr(), G.getPointerSize(), 0); 53349cc55cSDimitry Andric GOTBlock.addEdge(isRV64() ? R_RISCV_64 : R_RISCV_32, 0, Target, 0); 54349cc55cSDimitry Andric return G.addAnonymousSymbol(GOTBlock, 0, G.getPointerSize(), false, false); 55349cc55cSDimitry Andric } 56349cc55cSDimitry Andric 57349cc55cSDimitry Andric Symbol &createPLTStub(Symbol &Target) { 5804eeddc0SDimitry Andric Block &StubContentBlock = G.createContentBlock( 5904eeddc0SDimitry Andric getStubsSection(), getStubBlockContent(), orc::ExecutorAddr(), 4, 0); 60349cc55cSDimitry Andric auto &GOTEntrySymbol = getGOTEntry(Target); 61349cc55cSDimitry Andric StubContentBlock.addEdge(R_RISCV_CALL, 0, GOTEntrySymbol, 0); 62349cc55cSDimitry Andric return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, 63349cc55cSDimitry Andric false); 64349cc55cSDimitry Andric } 65349cc55cSDimitry Andric 66349cc55cSDimitry Andric void fixGOTEdge(Edge &E, Symbol &GOTEntry) { 67349cc55cSDimitry Andric // Replace the relocation pair (R_RISCV_GOT_HI20, R_RISCV_PCREL_LO12) 68349cc55cSDimitry Andric // with (R_RISCV_PCREL_HI20, R_RISCV_PCREL_LO12) 69349cc55cSDimitry Andric // Therefore, here just change the R_RISCV_GOT_HI20 to R_RISCV_PCREL_HI20 70349cc55cSDimitry Andric E.setKind(R_RISCV_PCREL_HI20); 71349cc55cSDimitry Andric E.setTarget(GOTEntry); 72349cc55cSDimitry Andric } 73349cc55cSDimitry Andric 74349cc55cSDimitry Andric void fixPLTEdge(Edge &E, Symbol &PLTStubs) { 7506c3fb27SDimitry Andric assert((E.getKind() == R_RISCV_CALL || E.getKind() == R_RISCV_CALL_PLT || 7606c3fb27SDimitry Andric E.getKind() == CallRelaxable) && 7706c3fb27SDimitry Andric "Not a PLT edge?"); 78349cc55cSDimitry Andric E.setKind(R_RISCV_CALL); 79349cc55cSDimitry Andric E.setTarget(PLTStubs); 80349cc55cSDimitry Andric } 81349cc55cSDimitry Andric 82349cc55cSDimitry Andric bool isExternalBranchEdge(Edge &E) const { 8306c3fb27SDimitry Andric return (E.getKind() == R_RISCV_CALL || E.getKind() == R_RISCV_CALL_PLT || 8406c3fb27SDimitry Andric E.getKind() == CallRelaxable) && 8506c3fb27SDimitry Andric !E.getTarget().isDefined(); 86349cc55cSDimitry Andric } 87349cc55cSDimitry Andric 88349cc55cSDimitry Andric private: 89349cc55cSDimitry Andric Section &getGOTSection() const { 90349cc55cSDimitry Andric if (!GOTSection) 91bdd1243dSDimitry Andric GOTSection = &G.createSection("$__GOT", orc::MemProt::Read); 92349cc55cSDimitry Andric return *GOTSection; 93349cc55cSDimitry Andric } 94349cc55cSDimitry Andric 95349cc55cSDimitry Andric Section &getStubsSection() const { 96349cc55cSDimitry Andric if (!StubsSection) 97349cc55cSDimitry Andric StubsSection = 98bdd1243dSDimitry Andric &G.createSection("$__STUBS", orc::MemProt::Read | orc::MemProt::Exec); 99349cc55cSDimitry Andric return *StubsSection; 100349cc55cSDimitry Andric } 101349cc55cSDimitry Andric 102349cc55cSDimitry Andric ArrayRef<char> getGOTEntryBlockContent() { 103349cc55cSDimitry Andric return {reinterpret_cast<const char *>(NullGOTEntryContent), 104349cc55cSDimitry Andric G.getPointerSize()}; 105349cc55cSDimitry Andric } 106349cc55cSDimitry Andric 107349cc55cSDimitry Andric ArrayRef<char> getStubBlockContent() { 108349cc55cSDimitry Andric auto StubContent = isRV64() ? RV64StubContent : RV32StubContent; 109349cc55cSDimitry Andric return {reinterpret_cast<const char *>(StubContent), StubEntrySize}; 110349cc55cSDimitry Andric } 111349cc55cSDimitry Andric 112349cc55cSDimitry Andric mutable Section *GOTSection = nullptr; 113349cc55cSDimitry Andric mutable Section *StubsSection = nullptr; 114349cc55cSDimitry Andric }; 115349cc55cSDimitry Andric 116349cc55cSDimitry Andric const uint8_t PerGraphGOTAndPLTStubsBuilder_ELF_riscv::NullGOTEntryContent[8] = 117349cc55cSDimitry Andric {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 118349cc55cSDimitry Andric 119349cc55cSDimitry Andric const uint8_t 120349cc55cSDimitry Andric PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV64StubContent[StubEntrySize] = { 121349cc55cSDimitry Andric 0x17, 0x0e, 0x00, 0x00, // auipc t3, literal 122349cc55cSDimitry Andric 0x03, 0x3e, 0x0e, 0x00, // ld t3, literal(t3) 123349cc55cSDimitry Andric 0x67, 0x00, 0x0e, 0x00, // jr t3 124349cc55cSDimitry Andric 0x13, 0x00, 0x00, 0x00}; // nop 125349cc55cSDimitry Andric 126349cc55cSDimitry Andric const uint8_t 127349cc55cSDimitry Andric PerGraphGOTAndPLTStubsBuilder_ELF_riscv::RV32StubContent[StubEntrySize] = { 128349cc55cSDimitry Andric 0x17, 0x0e, 0x00, 0x00, // auipc t3, literal 129349cc55cSDimitry Andric 0x03, 0x2e, 0x0e, 0x00, // lw t3, literal(t3) 130349cc55cSDimitry Andric 0x67, 0x00, 0x0e, 0x00, // jr t3 131349cc55cSDimitry Andric 0x13, 0x00, 0x00, 0x00}; // nop 132349cc55cSDimitry Andric } // namespace 133fe6060f1SDimitry Andric namespace llvm { 134fe6060f1SDimitry Andric namespace jitlink { 135fe6060f1SDimitry Andric 13604eeddc0SDimitry Andric static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) { 13781ad6265SDimitry Andric return (Num & (((1ULL << Size) - 1) << Low)) >> Low; 13804eeddc0SDimitry Andric } 13904eeddc0SDimitry Andric 14081ad6265SDimitry Andric static inline bool isAlignmentCorrect(uint64_t Value, int N) { 14181ad6265SDimitry Andric return (Value & (N - 1)) ? false : true; 14204eeddc0SDimitry Andric } 14304eeddc0SDimitry Andric 14481ad6265SDimitry Andric // Requires 0 < N <= 64. 14581ad6265SDimitry Andric static inline bool isInRangeForImm(int64_t Value, int N) { 14681ad6265SDimitry Andric return Value == llvm::SignExtend64(Value, N); 147fe6060f1SDimitry Andric } 148fe6060f1SDimitry Andric 149fe6060f1SDimitry Andric class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> { 150fe6060f1SDimitry Andric friend class JITLinker<ELFJITLinker_riscv>; 151fe6060f1SDimitry Andric 152fe6060f1SDimitry Andric public: 153fe6060f1SDimitry Andric ELFJITLinker_riscv(std::unique_ptr<JITLinkContext> Ctx, 154fe6060f1SDimitry Andric std::unique_ptr<LinkGraph> G, PassConfiguration PassConfig) 155*0fca6ea1SDimitry Andric : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) { 156*0fca6ea1SDimitry Andric JITLinkerBase::getPassConfig().PostAllocationPasses.push_back( 157*0fca6ea1SDimitry Andric [this](LinkGraph &G) { return gatherRISCVPCRelHi20(G); }); 158*0fca6ea1SDimitry Andric } 159fe6060f1SDimitry Andric 160fe6060f1SDimitry Andric private: 161*0fca6ea1SDimitry Andric DenseMap<std::pair<const Block *, orc::ExecutorAddrDiff>, const Edge *> 162*0fca6ea1SDimitry Andric RelHi20; 163*0fca6ea1SDimitry Andric 164*0fca6ea1SDimitry Andric Error gatherRISCVPCRelHi20(LinkGraph &G) { 165*0fca6ea1SDimitry Andric for (Block *B : G.blocks()) 166*0fca6ea1SDimitry Andric for (Edge &E : B->edges()) 167*0fca6ea1SDimitry Andric if (E.getKind() == R_RISCV_PCREL_HI20) 168*0fca6ea1SDimitry Andric RelHi20[{B, E.getOffset()}] = &E; 169*0fca6ea1SDimitry Andric 170*0fca6ea1SDimitry Andric return Error::success(); 171*0fca6ea1SDimitry Andric } 172*0fca6ea1SDimitry Andric 173*0fca6ea1SDimitry Andric Expected<const Edge &> getRISCVPCRelHi20(const Edge &E) const { 174*0fca6ea1SDimitry Andric using namespace riscv; 175*0fca6ea1SDimitry Andric assert((E.getKind() == R_RISCV_PCREL_LO12_I || 176*0fca6ea1SDimitry Andric E.getKind() == R_RISCV_PCREL_LO12_S) && 177*0fca6ea1SDimitry Andric "Can only have high relocation for R_RISCV_PCREL_LO12_I or " 178*0fca6ea1SDimitry Andric "R_RISCV_PCREL_LO12_S"); 179*0fca6ea1SDimitry Andric 180*0fca6ea1SDimitry Andric const Symbol &Sym = E.getTarget(); 181*0fca6ea1SDimitry Andric const Block &B = Sym.getBlock(); 182*0fca6ea1SDimitry Andric orc::ExecutorAddrDiff Offset = Sym.getOffset(); 183*0fca6ea1SDimitry Andric 184*0fca6ea1SDimitry Andric auto It = RelHi20.find({&B, Offset}); 185*0fca6ea1SDimitry Andric if (It != RelHi20.end()) 186*0fca6ea1SDimitry Andric return *It->second; 187*0fca6ea1SDimitry Andric 188*0fca6ea1SDimitry Andric return make_error<JITLinkError>("No HI20 PCREL relocation type be found " 189*0fca6ea1SDimitry Andric "for LO12 PCREL relocation type"); 190*0fca6ea1SDimitry Andric } 191*0fca6ea1SDimitry Andric 192fe6060f1SDimitry Andric Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { 193fe6060f1SDimitry Andric using namespace riscv; 194fe6060f1SDimitry Andric using namespace llvm::support; 195fe6060f1SDimitry Andric 196fe6060f1SDimitry Andric char *BlockWorkingMem = B.getAlreadyMutableContent().data(); 197fe6060f1SDimitry Andric char *FixupPtr = BlockWorkingMem + E.getOffset(); 19804eeddc0SDimitry Andric orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset(); 199fe6060f1SDimitry Andric switch (E.getKind()) { 200349cc55cSDimitry Andric case R_RISCV_32: { 20104eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 202349cc55cSDimitry Andric *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 203349cc55cSDimitry Andric break; 204349cc55cSDimitry Andric } 205349cc55cSDimitry Andric case R_RISCV_64: { 20604eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 207349cc55cSDimitry Andric *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 208349cc55cSDimitry Andric break; 209349cc55cSDimitry Andric } 21004eeddc0SDimitry Andric case R_RISCV_BRANCH: { 21104eeddc0SDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 21281ad6265SDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 12))) 21381ad6265SDimitry Andric return makeTargetOutOfRangeError(G, B, E); 21481ad6265SDimitry Andric if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 21581ad6265SDimitry Andric return makeAlignmentError(FixupAddress, Value, 2, E); 216bdd1243dSDimitry Andric uint32_t Imm12 = extractBits(Value, 12, 1) << 31; 217bdd1243dSDimitry Andric uint32_t Imm10_5 = extractBits(Value, 5, 6) << 25; 218bdd1243dSDimitry Andric uint32_t Imm4_1 = extractBits(Value, 1, 4) << 8; 219bdd1243dSDimitry Andric uint32_t Imm11 = extractBits(Value, 11, 1) << 7; 220fe6060f1SDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 221bdd1243dSDimitry Andric *(little32_t *)FixupPtr = 222bdd1243dSDimitry Andric (RawInstr & 0x1FFF07F) | Imm12 | Imm10_5 | Imm4_1 | Imm11; 22304eeddc0SDimitry Andric break; 22404eeddc0SDimitry Andric } 22581ad6265SDimitry Andric case R_RISCV_JAL: { 22681ad6265SDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 22781ad6265SDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 20))) 22881ad6265SDimitry Andric return makeTargetOutOfRangeError(G, B, E); 22981ad6265SDimitry Andric if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 23081ad6265SDimitry Andric return makeAlignmentError(FixupAddress, Value, 2, E); 23181ad6265SDimitry Andric uint32_t Imm20 = extractBits(Value, 20, 1) << 31; 23281ad6265SDimitry Andric uint32_t Imm10_1 = extractBits(Value, 1, 10) << 21; 23381ad6265SDimitry Andric uint32_t Imm11 = extractBits(Value, 11, 1) << 20; 23481ad6265SDimitry Andric uint32_t Imm19_12 = extractBits(Value, 12, 8) << 12; 23581ad6265SDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 23604eeddc0SDimitry Andric *(little32_t *)FixupPtr = 237bdd1243dSDimitry Andric (RawInstr & 0xFFF) | Imm20 | Imm10_1 | Imm11 | Imm19_12; 238fe6060f1SDimitry Andric break; 239fe6060f1SDimitry Andric } 24006c3fb27SDimitry Andric case CallRelaxable: 24106c3fb27SDimitry Andric // Treat as R_RISCV_CALL when the relaxation pass did not run 24206c3fb27SDimitry Andric case R_RISCV_CALL_PLT: 243fe6060f1SDimitry Andric case R_RISCV_CALL: { 244fe6060f1SDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 24504eeddc0SDimitry Andric int64_t Hi = Value + 0x800; 24681ad6265SDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 24704eeddc0SDimitry Andric return makeTargetOutOfRangeError(G, B, E); 248fe6060f1SDimitry Andric int32_t Lo = Value & 0xFFF; 249fe6060f1SDimitry Andric uint32_t RawInstrAuipc = *(little32_t *)FixupPtr; 250fe6060f1SDimitry Andric uint32_t RawInstrJalr = *(little32_t *)(FixupPtr + 4); 25104eeddc0SDimitry Andric *(little32_t *)FixupPtr = 25204eeddc0SDimitry Andric RawInstrAuipc | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 253fe6060f1SDimitry Andric *(little32_t *)(FixupPtr + 4) = 254fe6060f1SDimitry Andric RawInstrJalr | (static_cast<uint32_t>(Lo) << 20); 255fe6060f1SDimitry Andric break; 256fe6060f1SDimitry Andric } 257bdd1243dSDimitry Andric // The relocations R_RISCV_CALL_PLT and R_RISCV_GOT_HI20 are handled by 258bdd1243dSDimitry Andric // PerGraphGOTAndPLTStubsBuilder_ELF_riscv and are transformed into 259bdd1243dSDimitry Andric // R_RISCV_CALL and R_RISCV_PCREL_HI20. 260fe6060f1SDimitry Andric case R_RISCV_PCREL_HI20: { 261fe6060f1SDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 26204eeddc0SDimitry Andric int64_t Hi = Value + 0x800; 26381ad6265SDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 26404eeddc0SDimitry Andric return makeTargetOutOfRangeError(G, B, E); 265fe6060f1SDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 26604eeddc0SDimitry Andric *(little32_t *)FixupPtr = 26704eeddc0SDimitry Andric (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 268fe6060f1SDimitry Andric break; 269fe6060f1SDimitry Andric } 270fe6060f1SDimitry Andric case R_RISCV_PCREL_LO12_I: { 27104eeddc0SDimitry Andric // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 27204eeddc0SDimitry Andric // pairs with current relocation R_RISCV_PCREL_LO12_I. So here may need a 27304eeddc0SDimitry Andric // check. 274fe6060f1SDimitry Andric auto RelHI20 = getRISCVPCRelHi20(E); 275fe6060f1SDimitry Andric if (!RelHI20) 276fe6060f1SDimitry Andric return RelHI20.takeError(); 277fe6060f1SDimitry Andric int64_t Value = RelHI20->getTarget().getAddress() + 278fe6060f1SDimitry Andric RelHI20->getAddend() - E.getTarget().getAddress(); 279fe6060f1SDimitry Andric int64_t Lo = Value & 0xFFF; 280fe6060f1SDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 281fe6060f1SDimitry Andric *(little32_t *)FixupPtr = 282fe6060f1SDimitry Andric (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 283fe6060f1SDimitry Andric break; 284fe6060f1SDimitry Andric } 285fe6060f1SDimitry Andric case R_RISCV_PCREL_LO12_S: { 28604eeddc0SDimitry Andric // FIXME: We assume that R_RISCV_PCREL_HI20 is present in object code and 28704eeddc0SDimitry Andric // pairs with current relocation R_RISCV_PCREL_LO12_S. So here may need a 28804eeddc0SDimitry Andric // check. 289fe6060f1SDimitry Andric auto RelHI20 = getRISCVPCRelHi20(E); 290bdd1243dSDimitry Andric if (!RelHI20) 291bdd1243dSDimitry Andric return RelHI20.takeError(); 292fe6060f1SDimitry Andric int64_t Value = RelHI20->getTarget().getAddress() + 293fe6060f1SDimitry Andric RelHI20->getAddend() - E.getTarget().getAddress(); 294fe6060f1SDimitry Andric int64_t Lo = Value & 0xFFF; 295bdd1243dSDimitry Andric uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25; 296bdd1243dSDimitry Andric uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7; 297fe6060f1SDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 298fe6060f1SDimitry Andric 299bdd1243dSDimitry Andric *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0; 300fe6060f1SDimitry Andric break; 301fe6060f1SDimitry Andric } 302bdd1243dSDimitry Andric case R_RISCV_HI20: { 303bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 304bdd1243dSDimitry Andric int64_t Hi = Value + 0x800; 305bdd1243dSDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Hi, 32))) 306bdd1243dSDimitry Andric return makeTargetOutOfRangeError(G, B, E); 307bdd1243dSDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 308bdd1243dSDimitry Andric *(little32_t *)FixupPtr = 309bdd1243dSDimitry Andric (RawInstr & 0xFFF) | (static_cast<uint32_t>(Hi & 0xFFFFF000)); 31004eeddc0SDimitry Andric break; 31104eeddc0SDimitry Andric } 312bdd1243dSDimitry Andric case R_RISCV_LO12_I: { 313bdd1243dSDimitry Andric // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs 314bdd1243dSDimitry Andric // with current relocation R_RISCV_LO12_I. So here may need a check. 315bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 316bdd1243dSDimitry Andric int32_t Lo = Value & 0xFFF; 317bdd1243dSDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 318bdd1243dSDimitry Andric *(little32_t *)FixupPtr = 319bdd1243dSDimitry Andric (RawInstr & 0xFFFFF) | (static_cast<uint32_t>(Lo & 0xFFF) << 20); 32004eeddc0SDimitry Andric break; 32104eeddc0SDimitry Andric } 322bdd1243dSDimitry Andric case R_RISCV_LO12_S: { 323bdd1243dSDimitry Andric // FIXME: We assume that R_RISCV_HI20 is present in object code and pairs 324bdd1243dSDimitry Andric // with current relocation R_RISCV_LO12_S. So here may need a check. 325bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 326bdd1243dSDimitry Andric int64_t Lo = Value & 0xFFF; 327bdd1243dSDimitry Andric uint32_t Imm11_5 = extractBits(Lo, 5, 7) << 25; 328bdd1243dSDimitry Andric uint32_t Imm4_0 = extractBits(Lo, 0, 5) << 7; 329bdd1243dSDimitry Andric uint32_t RawInstr = *(little32_t *)FixupPtr; 330bdd1243dSDimitry Andric *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm11_5 | Imm4_0; 33104eeddc0SDimitry Andric break; 33204eeddc0SDimitry Andric } 33304eeddc0SDimitry Andric case R_RISCV_ADD8: { 33404eeddc0SDimitry Andric int64_t Value = 33504eeddc0SDimitry Andric (E.getTarget().getAddress() + 33606c3fb27SDimitry Andric *(reinterpret_cast<const uint8_t *>(FixupPtr)) + E.getAddend()) 33704eeddc0SDimitry Andric .getValue(); 33804eeddc0SDimitry Andric *FixupPtr = static_cast<uint8_t>(Value); 33904eeddc0SDimitry Andric break; 34004eeddc0SDimitry Andric } 341bdd1243dSDimitry Andric case R_RISCV_ADD16: { 342bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + 34306c3fb27SDimitry Andric support::endian::read16le(FixupPtr) + E.getAddend()) 344bdd1243dSDimitry Andric .getValue(); 345bdd1243dSDimitry Andric *(little16_t *)FixupPtr = static_cast<uint16_t>(Value); 346bdd1243dSDimitry Andric break; 347bdd1243dSDimitry Andric } 348bdd1243dSDimitry Andric case R_RISCV_ADD32: { 349bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + 35006c3fb27SDimitry Andric support::endian::read32le(FixupPtr) + E.getAddend()) 351bdd1243dSDimitry Andric .getValue(); 352bdd1243dSDimitry Andric *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 353bdd1243dSDimitry Andric break; 354bdd1243dSDimitry Andric } 355bdd1243dSDimitry Andric case R_RISCV_ADD64: { 356bdd1243dSDimitry Andric int64_t Value = (E.getTarget().getAddress() + 35706c3fb27SDimitry Andric support::endian::read64le(FixupPtr) + E.getAddend()) 358bdd1243dSDimitry Andric .getValue(); 35904eeddc0SDimitry Andric *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 36004eeddc0SDimitry Andric break; 36104eeddc0SDimitry Andric } 362bdd1243dSDimitry Andric case R_RISCV_SUB8: { 36306c3fb27SDimitry Andric int64_t Value = *(reinterpret_cast<const uint8_t *>(FixupPtr)) - 36404eeddc0SDimitry Andric E.getTarget().getAddress().getValue() - E.getAddend(); 365bdd1243dSDimitry Andric *FixupPtr = static_cast<uint8_t>(Value); 36604eeddc0SDimitry Andric break; 36704eeddc0SDimitry Andric } 36804eeddc0SDimitry Andric case R_RISCV_SUB16: { 36906c3fb27SDimitry Andric int64_t Value = support::endian::read16le(FixupPtr) - 37004eeddc0SDimitry Andric E.getTarget().getAddress().getValue() - E.getAddend(); 37104eeddc0SDimitry Andric *(little16_t *)FixupPtr = static_cast<uint32_t>(Value); 37204eeddc0SDimitry Andric break; 37304eeddc0SDimitry Andric } 374bdd1243dSDimitry Andric case R_RISCV_SUB32: { 37506c3fb27SDimitry Andric int64_t Value = support::endian::read32le(FixupPtr) - 37604eeddc0SDimitry Andric E.getTarget().getAddress().getValue() - E.getAddend(); 377bdd1243dSDimitry Andric *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 378bdd1243dSDimitry Andric break; 379bdd1243dSDimitry Andric } 380bdd1243dSDimitry Andric case R_RISCV_SUB64: { 38106c3fb27SDimitry Andric int64_t Value = support::endian::read64le(FixupPtr) - 382bdd1243dSDimitry Andric E.getTarget().getAddress().getValue() - E.getAddend(); 383bdd1243dSDimitry Andric *(little64_t *)FixupPtr = static_cast<uint64_t>(Value); 384bdd1243dSDimitry Andric break; 385bdd1243dSDimitry Andric } 386bdd1243dSDimitry Andric case R_RISCV_RVC_BRANCH: { 387bdd1243dSDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 388bdd1243dSDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 8))) 389bdd1243dSDimitry Andric return makeTargetOutOfRangeError(G, B, E); 390bdd1243dSDimitry Andric if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 391bdd1243dSDimitry Andric return makeAlignmentError(FixupAddress, Value, 2, E); 392bdd1243dSDimitry Andric uint16_t Imm8 = extractBits(Value, 8, 1) << 12; 393bdd1243dSDimitry Andric uint16_t Imm4_3 = extractBits(Value, 3, 2) << 10; 394bdd1243dSDimitry Andric uint16_t Imm7_6 = extractBits(Value, 6, 2) << 5; 395bdd1243dSDimitry Andric uint16_t Imm2_1 = extractBits(Value, 1, 2) << 3; 396bdd1243dSDimitry Andric uint16_t Imm5 = extractBits(Value, 5, 1) << 2; 397bdd1243dSDimitry Andric uint16_t RawInstr = *(little16_t *)FixupPtr; 398bdd1243dSDimitry Andric *(little16_t *)FixupPtr = 399bdd1243dSDimitry Andric (RawInstr & 0xE383) | Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5; 400bdd1243dSDimitry Andric break; 401bdd1243dSDimitry Andric } 402bdd1243dSDimitry Andric case R_RISCV_RVC_JUMP: { 403bdd1243dSDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 404bdd1243dSDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Value >> 1, 11))) 405bdd1243dSDimitry Andric return makeTargetOutOfRangeError(G, B, E); 406bdd1243dSDimitry Andric if (LLVM_UNLIKELY(!isAlignmentCorrect(Value, 2))) 407bdd1243dSDimitry Andric return makeAlignmentError(FixupAddress, Value, 2, E); 408bdd1243dSDimitry Andric uint16_t Imm11 = extractBits(Value, 11, 1) << 12; 409bdd1243dSDimitry Andric uint16_t Imm4 = extractBits(Value, 4, 1) << 11; 410bdd1243dSDimitry Andric uint16_t Imm9_8 = extractBits(Value, 8, 2) << 9; 411bdd1243dSDimitry Andric uint16_t Imm10 = extractBits(Value, 10, 1) << 8; 412bdd1243dSDimitry Andric uint16_t Imm6 = extractBits(Value, 6, 1) << 7; 413bdd1243dSDimitry Andric uint16_t Imm7 = extractBits(Value, 7, 1) << 6; 414bdd1243dSDimitry Andric uint16_t Imm3_1 = extractBits(Value, 1, 3) << 3; 415bdd1243dSDimitry Andric uint16_t Imm5 = extractBits(Value, 5, 1) << 2; 416bdd1243dSDimitry Andric uint16_t RawInstr = *(little16_t *)FixupPtr; 417bdd1243dSDimitry Andric *(little16_t *)FixupPtr = (RawInstr & 0xE003) | Imm11 | Imm4 | Imm9_8 | 418bdd1243dSDimitry Andric Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5; 41904eeddc0SDimitry Andric break; 42004eeddc0SDimitry Andric } 42181ad6265SDimitry Andric case R_RISCV_SUB6: { 42206c3fb27SDimitry Andric int64_t Value = *(reinterpret_cast<const uint8_t *>(FixupPtr)) & 0x3f; 42381ad6265SDimitry Andric Value -= E.getTarget().getAddress().getValue() - E.getAddend(); 42481ad6265SDimitry Andric *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<uint8_t>(Value) & 0x3f); 42581ad6265SDimitry Andric break; 42681ad6265SDimitry Andric } 42704eeddc0SDimitry Andric case R_RISCV_SET6: { 42804eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 42904eeddc0SDimitry Andric uint32_t RawData = *(little32_t *)FixupPtr; 43004eeddc0SDimitry Andric int64_t Word6 = Value & 0x3f; 43104eeddc0SDimitry Andric *(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6; 43204eeddc0SDimitry Andric break; 43304eeddc0SDimitry Andric } 43404eeddc0SDimitry Andric case R_RISCV_SET8: { 43504eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 43604eeddc0SDimitry Andric uint32_t RawData = *(little32_t *)FixupPtr; 43704eeddc0SDimitry Andric int64_t Word8 = Value & 0xff; 43804eeddc0SDimitry Andric *(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8; 43904eeddc0SDimitry Andric break; 44004eeddc0SDimitry Andric } 44104eeddc0SDimitry Andric case R_RISCV_SET16: { 44204eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 44304eeddc0SDimitry Andric uint32_t RawData = *(little32_t *)FixupPtr; 44404eeddc0SDimitry Andric int64_t Word16 = Value & 0xffff; 44504eeddc0SDimitry Andric *(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16; 44604eeddc0SDimitry Andric break; 44704eeddc0SDimitry Andric } 44804eeddc0SDimitry Andric case R_RISCV_SET32: { 44904eeddc0SDimitry Andric int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue(); 45004eeddc0SDimitry Andric int64_t Word32 = Value & 0xffffffff; 45104eeddc0SDimitry Andric *(little32_t *)FixupPtr = Word32; 45204eeddc0SDimitry Andric break; 45304eeddc0SDimitry Andric } 45404eeddc0SDimitry Andric case R_RISCV_32_PCREL: { 45504eeddc0SDimitry Andric int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 45604eeddc0SDimitry Andric int64_t Word32 = Value & 0xffffffff; 45704eeddc0SDimitry Andric *(little32_t *)FixupPtr = Word32; 45804eeddc0SDimitry Andric break; 45904eeddc0SDimitry Andric } 46006c3fb27SDimitry Andric case AlignRelaxable: 46106c3fb27SDimitry Andric // Ignore when the relaxation pass did not run 46206c3fb27SDimitry Andric break; 4635f757f3fSDimitry Andric case NegDelta32: { 4645f757f3fSDimitry Andric int64_t Value = FixupAddress - E.getTarget().getAddress() + E.getAddend(); 4655f757f3fSDimitry Andric if (LLVM_UNLIKELY(!isInRangeForImm(Value, 32))) 4665f757f3fSDimitry Andric return makeTargetOutOfRangeError(G, B, E); 4675f757f3fSDimitry Andric *(little32_t *)FixupPtr = static_cast<uint32_t>(Value); 4685f757f3fSDimitry Andric break; 4695f757f3fSDimitry Andric } 470fe6060f1SDimitry Andric } 471fe6060f1SDimitry Andric return Error::success(); 472fe6060f1SDimitry Andric } 473fe6060f1SDimitry Andric }; 474fe6060f1SDimitry Andric 47506c3fb27SDimitry Andric namespace { 47606c3fb27SDimitry Andric 47706c3fb27SDimitry Andric struct SymbolAnchor { 47806c3fb27SDimitry Andric uint64_t Offset; 47906c3fb27SDimitry Andric Symbol *Sym; 48006c3fb27SDimitry Andric bool End; // true for the anchor of getOffset() + getSize() 48106c3fb27SDimitry Andric }; 48206c3fb27SDimitry Andric 48306c3fb27SDimitry Andric struct BlockRelaxAux { 48406c3fb27SDimitry Andric // This records symbol start and end offsets which will be adjusted according 48506c3fb27SDimitry Andric // to the nearest RelocDeltas element. 48606c3fb27SDimitry Andric SmallVector<SymbolAnchor, 0> Anchors; 48706c3fb27SDimitry Andric // All edges that either 1) are R_RISCV_ALIGN or 2) have a R_RISCV_RELAX edge 48806c3fb27SDimitry Andric // at the same offset. 48906c3fb27SDimitry Andric SmallVector<Edge *, 0> RelaxEdges; 49006c3fb27SDimitry Andric // For RelaxEdges[I], the actual offset is RelaxEdges[I]->getOffset() - (I ? 49106c3fb27SDimitry Andric // RelocDeltas[I - 1] : 0). 49206c3fb27SDimitry Andric SmallVector<uint32_t, 0> RelocDeltas; 49306c3fb27SDimitry Andric // For RelaxEdges[I], the actual type is EdgeKinds[I]. 49406c3fb27SDimitry Andric SmallVector<Edge::Kind, 0> EdgeKinds; 49506c3fb27SDimitry Andric // List of rewritten instructions. Contains one raw encoded instruction per 49606c3fb27SDimitry Andric // element in EdgeKinds that isn't Invalid or R_RISCV_ALIGN. 49706c3fb27SDimitry Andric SmallVector<uint32_t, 0> Writes; 49806c3fb27SDimitry Andric }; 49906c3fb27SDimitry Andric 50006c3fb27SDimitry Andric struct RelaxConfig { 50106c3fb27SDimitry Andric bool IsRV32; 50206c3fb27SDimitry Andric bool HasRVC; 50306c3fb27SDimitry Andric }; 50406c3fb27SDimitry Andric 50506c3fb27SDimitry Andric struct RelaxAux { 50606c3fb27SDimitry Andric RelaxConfig Config; 50706c3fb27SDimitry Andric DenseMap<Block *, BlockRelaxAux> Blocks; 50806c3fb27SDimitry Andric }; 50906c3fb27SDimitry Andric 51006c3fb27SDimitry Andric } // namespace 51106c3fb27SDimitry Andric 51206c3fb27SDimitry Andric static bool shouldRelax(const Section &S) { 51306c3fb27SDimitry Andric return (S.getMemProt() & orc::MemProt::Exec) != orc::MemProt::None; 51406c3fb27SDimitry Andric } 51506c3fb27SDimitry Andric 51606c3fb27SDimitry Andric static bool isRelaxable(const Edge &E) { 51706c3fb27SDimitry Andric switch (E.getKind()) { 51806c3fb27SDimitry Andric default: 51906c3fb27SDimitry Andric return false; 52006c3fb27SDimitry Andric case CallRelaxable: 52106c3fb27SDimitry Andric case AlignRelaxable: 52206c3fb27SDimitry Andric return true; 52306c3fb27SDimitry Andric } 52406c3fb27SDimitry Andric } 52506c3fb27SDimitry Andric 52606c3fb27SDimitry Andric static RelaxAux initRelaxAux(LinkGraph &G) { 52706c3fb27SDimitry Andric RelaxAux Aux; 52806c3fb27SDimitry Andric Aux.Config.IsRV32 = G.getTargetTriple().isRISCV32(); 52906c3fb27SDimitry Andric const auto &Features = G.getFeatures().getFeatures(); 530*0fca6ea1SDimitry Andric Aux.Config.HasRVC = llvm::is_contained(Features, "+c") || 531*0fca6ea1SDimitry Andric llvm::is_contained(Features, "+zca"); 53206c3fb27SDimitry Andric 53306c3fb27SDimitry Andric for (auto &S : G.sections()) { 53406c3fb27SDimitry Andric if (!shouldRelax(S)) 53506c3fb27SDimitry Andric continue; 53606c3fb27SDimitry Andric for (auto *B : S.blocks()) { 53706c3fb27SDimitry Andric auto BlockEmplaceResult = Aux.Blocks.try_emplace(B); 53806c3fb27SDimitry Andric assert(BlockEmplaceResult.second && "Block encountered twice"); 53906c3fb27SDimitry Andric auto &BlockAux = BlockEmplaceResult.first->second; 54006c3fb27SDimitry Andric 54106c3fb27SDimitry Andric for (auto &E : B->edges()) 54206c3fb27SDimitry Andric if (isRelaxable(E)) 54306c3fb27SDimitry Andric BlockAux.RelaxEdges.push_back(&E); 54406c3fb27SDimitry Andric 54506c3fb27SDimitry Andric if (BlockAux.RelaxEdges.empty()) { 54606c3fb27SDimitry Andric Aux.Blocks.erase(BlockEmplaceResult.first); 54706c3fb27SDimitry Andric continue; 54806c3fb27SDimitry Andric } 54906c3fb27SDimitry Andric 55006c3fb27SDimitry Andric const auto NumEdges = BlockAux.RelaxEdges.size(); 55106c3fb27SDimitry Andric BlockAux.RelocDeltas.resize(NumEdges, 0); 55206c3fb27SDimitry Andric BlockAux.EdgeKinds.resize_for_overwrite(NumEdges); 55306c3fb27SDimitry Andric 55406c3fb27SDimitry Andric // Store anchors (offset and offset+size) for symbols. 55506c3fb27SDimitry Andric for (auto *Sym : S.symbols()) { 55606c3fb27SDimitry Andric if (!Sym->isDefined() || &Sym->getBlock() != B) 55706c3fb27SDimitry Andric continue; 55806c3fb27SDimitry Andric 55906c3fb27SDimitry Andric BlockAux.Anchors.push_back({Sym->getOffset(), Sym, false}); 56006c3fb27SDimitry Andric BlockAux.Anchors.push_back( 56106c3fb27SDimitry Andric {Sym->getOffset() + Sym->getSize(), Sym, true}); 56206c3fb27SDimitry Andric } 56306c3fb27SDimitry Andric } 56406c3fb27SDimitry Andric } 56506c3fb27SDimitry Andric 56606c3fb27SDimitry Andric // Sort anchors by offset so that we can find the closest relocation 56706c3fb27SDimitry Andric // efficiently. For a zero size symbol, ensure that its start anchor precedes 56806c3fb27SDimitry Andric // its end anchor. For two symbols with anchors at the same offset, their 56906c3fb27SDimitry Andric // order does not matter. 57006c3fb27SDimitry Andric for (auto &BlockAuxIter : Aux.Blocks) { 57106c3fb27SDimitry Andric llvm::sort(BlockAuxIter.second.Anchors, [](auto &A, auto &B) { 57206c3fb27SDimitry Andric return std::make_pair(A.Offset, A.End) < std::make_pair(B.Offset, B.End); 57306c3fb27SDimitry Andric }); 57406c3fb27SDimitry Andric } 57506c3fb27SDimitry Andric 57606c3fb27SDimitry Andric return Aux; 57706c3fb27SDimitry Andric } 57806c3fb27SDimitry Andric 57906c3fb27SDimitry Andric static void relaxAlign(orc::ExecutorAddr Loc, const Edge &E, uint32_t &Remove, 58006c3fb27SDimitry Andric Edge::Kind &NewEdgeKind) { 58106c3fb27SDimitry Andric // E points to the start of the padding bytes. 58206c3fb27SDimitry Andric // E + Addend points to the instruction to be aligned by removing padding. 58306c3fb27SDimitry Andric // Alignment is the smallest power of 2 strictly greater than Addend. 58406c3fb27SDimitry Andric const auto Align = NextPowerOf2(E.getAddend()); 58506c3fb27SDimitry Andric const auto DestLoc = alignTo(Loc.getValue(), Align); 58606c3fb27SDimitry Andric const auto SrcLoc = Loc.getValue() + E.getAddend(); 58706c3fb27SDimitry Andric Remove = SrcLoc - DestLoc; 58806c3fb27SDimitry Andric assert(static_cast<int32_t>(Remove) >= 0 && 58906c3fb27SDimitry Andric "R_RISCV_ALIGN needs expanding the content"); 59006c3fb27SDimitry Andric NewEdgeKind = AlignRelaxable; 59106c3fb27SDimitry Andric } 59206c3fb27SDimitry Andric 59306c3fb27SDimitry Andric static void relaxCall(const Block &B, BlockRelaxAux &Aux, 59406c3fb27SDimitry Andric const RelaxConfig &Config, orc::ExecutorAddr Loc, 59506c3fb27SDimitry Andric const Edge &E, uint32_t &Remove, 59606c3fb27SDimitry Andric Edge::Kind &NewEdgeKind) { 59706c3fb27SDimitry Andric const auto JALR = 59806c3fb27SDimitry Andric support::endian::read32le(B.getContent().data() + E.getOffset() + 4); 59906c3fb27SDimitry Andric const auto RD = extractBits(JALR, 7, 5); 60006c3fb27SDimitry Andric const auto Dest = E.getTarget().getAddress() + E.getAddend(); 60106c3fb27SDimitry Andric const auto Displace = Dest - Loc; 60206c3fb27SDimitry Andric 60306c3fb27SDimitry Andric if (Config.HasRVC && isInt<12>(Displace) && RD == 0) { 60406c3fb27SDimitry Andric NewEdgeKind = R_RISCV_RVC_JUMP; 60506c3fb27SDimitry Andric Aux.Writes.push_back(0xa001); // c.j 60606c3fb27SDimitry Andric Remove = 6; 60706c3fb27SDimitry Andric } else if (Config.HasRVC && Config.IsRV32 && isInt<12>(Displace) && RD == 1) { 60806c3fb27SDimitry Andric NewEdgeKind = R_RISCV_RVC_JUMP; 60906c3fb27SDimitry Andric Aux.Writes.push_back(0x2001); // c.jal 61006c3fb27SDimitry Andric Remove = 6; 61106c3fb27SDimitry Andric } else if (isInt<21>(Displace)) { 61206c3fb27SDimitry Andric NewEdgeKind = R_RISCV_JAL; 61306c3fb27SDimitry Andric Aux.Writes.push_back(0x6f | RD << 7); // jal 61406c3fb27SDimitry Andric Remove = 4; 61506c3fb27SDimitry Andric } else { 61606c3fb27SDimitry Andric // Not relaxable 61706c3fb27SDimitry Andric NewEdgeKind = R_RISCV_CALL_PLT; 61806c3fb27SDimitry Andric Remove = 0; 61906c3fb27SDimitry Andric } 62006c3fb27SDimitry Andric } 62106c3fb27SDimitry Andric 62206c3fb27SDimitry Andric static bool relaxBlock(LinkGraph &G, Block &Block, BlockRelaxAux &Aux, 62306c3fb27SDimitry Andric const RelaxConfig &Config) { 62406c3fb27SDimitry Andric const auto BlockAddr = Block.getAddress(); 62506c3fb27SDimitry Andric bool Changed = false; 62606c3fb27SDimitry Andric ArrayRef<SymbolAnchor> SA = ArrayRef(Aux.Anchors); 62706c3fb27SDimitry Andric uint32_t Delta = 0; 62806c3fb27SDimitry Andric 62906c3fb27SDimitry Andric Aux.EdgeKinds.assign(Aux.EdgeKinds.size(), Edge::Invalid); 63006c3fb27SDimitry Andric Aux.Writes.clear(); 63106c3fb27SDimitry Andric 63206c3fb27SDimitry Andric for (auto [I, E] : llvm::enumerate(Aux.RelaxEdges)) { 63306c3fb27SDimitry Andric const auto Loc = BlockAddr + E->getOffset() - Delta; 63406c3fb27SDimitry Andric auto &Cur = Aux.RelocDeltas[I]; 63506c3fb27SDimitry Andric uint32_t Remove = 0; 63606c3fb27SDimitry Andric switch (E->getKind()) { 63706c3fb27SDimitry Andric case AlignRelaxable: 63806c3fb27SDimitry Andric relaxAlign(Loc, *E, Remove, Aux.EdgeKinds[I]); 63906c3fb27SDimitry Andric break; 64006c3fb27SDimitry Andric case CallRelaxable: 64106c3fb27SDimitry Andric relaxCall(Block, Aux, Config, Loc, *E, Remove, Aux.EdgeKinds[I]); 64206c3fb27SDimitry Andric break; 64306c3fb27SDimitry Andric default: 64406c3fb27SDimitry Andric llvm_unreachable("Unexpected relaxable edge kind"); 64506c3fb27SDimitry Andric } 64606c3fb27SDimitry Andric 64706c3fb27SDimitry Andric // For all anchors whose offsets are <= E->getOffset(), they are preceded by 64806c3fb27SDimitry Andric // the previous relocation whose RelocDeltas value equals Delta. 64906c3fb27SDimitry Andric // Decrease their offset and update their size. 65006c3fb27SDimitry Andric for (; SA.size() && SA[0].Offset <= E->getOffset(); SA = SA.slice(1)) { 65106c3fb27SDimitry Andric if (SA[0].End) 65206c3fb27SDimitry Andric SA[0].Sym->setSize(SA[0].Offset - Delta - SA[0].Sym->getOffset()); 65306c3fb27SDimitry Andric else 65406c3fb27SDimitry Andric SA[0].Sym->setOffset(SA[0].Offset - Delta); 65506c3fb27SDimitry Andric } 65606c3fb27SDimitry Andric 65706c3fb27SDimitry Andric Delta += Remove; 65806c3fb27SDimitry Andric if (Delta != Cur) { 65906c3fb27SDimitry Andric Cur = Delta; 66006c3fb27SDimitry Andric Changed = true; 66106c3fb27SDimitry Andric } 66206c3fb27SDimitry Andric } 66306c3fb27SDimitry Andric 66406c3fb27SDimitry Andric for (const SymbolAnchor &A : SA) { 66506c3fb27SDimitry Andric if (A.End) 66606c3fb27SDimitry Andric A.Sym->setSize(A.Offset - Delta - A.Sym->getOffset()); 66706c3fb27SDimitry Andric else 66806c3fb27SDimitry Andric A.Sym->setOffset(A.Offset - Delta); 66906c3fb27SDimitry Andric } 67006c3fb27SDimitry Andric 67106c3fb27SDimitry Andric return Changed; 67206c3fb27SDimitry Andric } 67306c3fb27SDimitry Andric 67406c3fb27SDimitry Andric static bool relaxOnce(LinkGraph &G, RelaxAux &Aux) { 67506c3fb27SDimitry Andric bool Changed = false; 67606c3fb27SDimitry Andric 67706c3fb27SDimitry Andric for (auto &[B, BlockAux] : Aux.Blocks) 67806c3fb27SDimitry Andric Changed |= relaxBlock(G, *B, BlockAux, Aux.Config); 67906c3fb27SDimitry Andric 68006c3fb27SDimitry Andric return Changed; 68106c3fb27SDimitry Andric } 68206c3fb27SDimitry Andric 68306c3fb27SDimitry Andric static void finalizeBlockRelax(LinkGraph &G, Block &Block, BlockRelaxAux &Aux) { 68406c3fb27SDimitry Andric auto Contents = Block.getAlreadyMutableContent(); 68506c3fb27SDimitry Andric auto *Dest = Contents.data(); 68606c3fb27SDimitry Andric auto NextWrite = Aux.Writes.begin(); 68706c3fb27SDimitry Andric uint32_t Offset = 0; 68806c3fb27SDimitry Andric uint32_t Delta = 0; 68906c3fb27SDimitry Andric 69006c3fb27SDimitry Andric // Update section content: remove NOPs for R_RISCV_ALIGN and rewrite 69106c3fb27SDimitry Andric // instructions for relaxed relocations. 69206c3fb27SDimitry Andric for (auto [I, E] : llvm::enumerate(Aux.RelaxEdges)) { 69306c3fb27SDimitry Andric uint32_t Remove = Aux.RelocDeltas[I] - Delta; 69406c3fb27SDimitry Andric Delta = Aux.RelocDeltas[I]; 69506c3fb27SDimitry Andric if (Remove == 0 && Aux.EdgeKinds[I] == Edge::Invalid) 69606c3fb27SDimitry Andric continue; 69706c3fb27SDimitry Andric 69806c3fb27SDimitry Andric // Copy from last location to the current relocated location. 69906c3fb27SDimitry Andric const auto Size = E->getOffset() - Offset; 70006c3fb27SDimitry Andric std::memmove(Dest, Contents.data() + Offset, Size); 70106c3fb27SDimitry Andric Dest += Size; 70206c3fb27SDimitry Andric 70306c3fb27SDimitry Andric uint32_t Skip = 0; 70406c3fb27SDimitry Andric switch (Aux.EdgeKinds[I]) { 70506c3fb27SDimitry Andric case Edge::Invalid: 70606c3fb27SDimitry Andric break; 70706c3fb27SDimitry Andric case AlignRelaxable: 70806c3fb27SDimitry Andric // For R_RISCV_ALIGN, we will place Offset in a location (among NOPs) to 70906c3fb27SDimitry Andric // satisfy the alignment requirement. If both Remove and E->getAddend() 71006c3fb27SDimitry Andric // are multiples of 4, it is as if we have skipped some NOPs. Otherwise we 71106c3fb27SDimitry Andric // are in the middle of a 4-byte NOP, and we need to rewrite the NOP 71206c3fb27SDimitry Andric // sequence. 71306c3fb27SDimitry Andric if (Remove % 4 || E->getAddend() % 4) { 71406c3fb27SDimitry Andric Skip = E->getAddend() - Remove; 71506c3fb27SDimitry Andric uint32_t J = 0; 71606c3fb27SDimitry Andric for (; J + 4 <= Skip; J += 4) 71706c3fb27SDimitry Andric support::endian::write32le(Dest + J, 0x00000013); // nop 71806c3fb27SDimitry Andric if (J != Skip) { 71906c3fb27SDimitry Andric assert(J + 2 == Skip); 72006c3fb27SDimitry Andric support::endian::write16le(Dest + J, 0x0001); // c.nop 72106c3fb27SDimitry Andric } 72206c3fb27SDimitry Andric } 72306c3fb27SDimitry Andric break; 72406c3fb27SDimitry Andric case R_RISCV_RVC_JUMP: 72506c3fb27SDimitry Andric Skip = 2; 72606c3fb27SDimitry Andric support::endian::write16le(Dest, *NextWrite++); 72706c3fb27SDimitry Andric break; 72806c3fb27SDimitry Andric case R_RISCV_JAL: 72906c3fb27SDimitry Andric Skip = 4; 73006c3fb27SDimitry Andric support::endian::write32le(Dest, *NextWrite++); 73106c3fb27SDimitry Andric break; 73206c3fb27SDimitry Andric } 73306c3fb27SDimitry Andric 73406c3fb27SDimitry Andric Dest += Skip; 73506c3fb27SDimitry Andric Offset = E->getOffset() + Skip + Remove; 73606c3fb27SDimitry Andric } 73706c3fb27SDimitry Andric 73806c3fb27SDimitry Andric std::memmove(Dest, Contents.data() + Offset, Contents.size() - Offset); 73906c3fb27SDimitry Andric 74006c3fb27SDimitry Andric // Fixup edge offsets and kinds. 74106c3fb27SDimitry Andric Delta = 0; 74206c3fb27SDimitry Andric size_t I = 0; 74306c3fb27SDimitry Andric for (auto &E : Block.edges()) { 74406c3fb27SDimitry Andric E.setOffset(E.getOffset() - Delta); 74506c3fb27SDimitry Andric 74606c3fb27SDimitry Andric if (I < Aux.RelaxEdges.size() && Aux.RelaxEdges[I] == &E) { 74706c3fb27SDimitry Andric if (Aux.EdgeKinds[I] != Edge::Invalid) 74806c3fb27SDimitry Andric E.setKind(Aux.EdgeKinds[I]); 74906c3fb27SDimitry Andric 75006c3fb27SDimitry Andric Delta = Aux.RelocDeltas[I]; 75106c3fb27SDimitry Andric ++I; 75206c3fb27SDimitry Andric } 75306c3fb27SDimitry Andric } 75406c3fb27SDimitry Andric 75506c3fb27SDimitry Andric // Remove AlignRelaxable edges: all other relaxable edges got modified and 75606c3fb27SDimitry Andric // will be used later while linking. Alignment is entirely handled here so we 75706c3fb27SDimitry Andric // don't need these edges anymore. 75806c3fb27SDimitry Andric for (auto IE = Block.edges().begin(); IE != Block.edges().end();) { 75906c3fb27SDimitry Andric if (IE->getKind() == AlignRelaxable) 76006c3fb27SDimitry Andric IE = Block.removeEdge(IE); 76106c3fb27SDimitry Andric else 76206c3fb27SDimitry Andric ++IE; 76306c3fb27SDimitry Andric } 76406c3fb27SDimitry Andric } 76506c3fb27SDimitry Andric 76606c3fb27SDimitry Andric static void finalizeRelax(LinkGraph &G, RelaxAux &Aux) { 76706c3fb27SDimitry Andric for (auto &[B, BlockAux] : Aux.Blocks) 76806c3fb27SDimitry Andric finalizeBlockRelax(G, *B, BlockAux); 76906c3fb27SDimitry Andric } 77006c3fb27SDimitry Andric 77106c3fb27SDimitry Andric static Error relax(LinkGraph &G) { 77206c3fb27SDimitry Andric auto Aux = initRelaxAux(G); 77306c3fb27SDimitry Andric while (relaxOnce(G, Aux)) { 77406c3fb27SDimitry Andric } 77506c3fb27SDimitry Andric finalizeRelax(G, Aux); 77606c3fb27SDimitry Andric return Error::success(); 77706c3fb27SDimitry Andric } 77806c3fb27SDimitry Andric 779fe6060f1SDimitry Andric template <typename ELFT> 780fe6060f1SDimitry Andric class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> { 781fe6060f1SDimitry Andric private: 782fe6060f1SDimitry Andric static Expected<riscv::EdgeKind_riscv> 783fe6060f1SDimitry Andric getRelocationKind(const uint32_t Type) { 784fe6060f1SDimitry Andric using namespace riscv; 785fe6060f1SDimitry Andric switch (Type) { 786fe6060f1SDimitry Andric case ELF::R_RISCV_32: 787fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_32; 788fe6060f1SDimitry Andric case ELF::R_RISCV_64: 789fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_64; 79004eeddc0SDimitry Andric case ELF::R_RISCV_BRANCH: 79104eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_BRANCH; 79281ad6265SDimitry Andric case ELF::R_RISCV_JAL: 79381ad6265SDimitry Andric return EdgeKind_riscv::R_RISCV_JAL; 794fe6060f1SDimitry Andric case ELF::R_RISCV_CALL: 795fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_CALL; 796bdd1243dSDimitry Andric case ELF::R_RISCV_CALL_PLT: 797bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_CALL_PLT; 798bdd1243dSDimitry Andric case ELF::R_RISCV_GOT_HI20: 799bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_GOT_HI20; 800fe6060f1SDimitry Andric case ELF::R_RISCV_PCREL_HI20: 801fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_PCREL_HI20; 802fe6060f1SDimitry Andric case ELF::R_RISCV_PCREL_LO12_I: 803fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_PCREL_LO12_I; 804fe6060f1SDimitry Andric case ELF::R_RISCV_PCREL_LO12_S: 805fe6060f1SDimitry Andric return EdgeKind_riscv::R_RISCV_PCREL_LO12_S; 806bdd1243dSDimitry Andric case ELF::R_RISCV_HI20: 807bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_HI20; 808bdd1243dSDimitry Andric case ELF::R_RISCV_LO12_I: 809bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_LO12_I; 810bdd1243dSDimitry Andric case ELF::R_RISCV_LO12_S: 811bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_LO12_S; 81204eeddc0SDimitry Andric case ELF::R_RISCV_ADD8: 81304eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_ADD8; 814bdd1243dSDimitry Andric case ELF::R_RISCV_ADD16: 815bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_ADD16; 816bdd1243dSDimitry Andric case ELF::R_RISCV_ADD32: 817bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_ADD32; 818bdd1243dSDimitry Andric case ELF::R_RISCV_ADD64: 819bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_ADD64; 82004eeddc0SDimitry Andric case ELF::R_RISCV_SUB8: 82104eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_SUB8; 822bdd1243dSDimitry Andric case ELF::R_RISCV_SUB16: 823bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_SUB16; 824bdd1243dSDimitry Andric case ELF::R_RISCV_SUB32: 825bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_SUB32; 826bdd1243dSDimitry Andric case ELF::R_RISCV_SUB64: 827bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_SUB64; 828bdd1243dSDimitry Andric case ELF::R_RISCV_RVC_BRANCH: 829bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_RVC_BRANCH; 830bdd1243dSDimitry Andric case ELF::R_RISCV_RVC_JUMP: 831bdd1243dSDimitry Andric return EdgeKind_riscv::R_RISCV_RVC_JUMP; 83281ad6265SDimitry Andric case ELF::R_RISCV_SUB6: 83381ad6265SDimitry Andric return EdgeKind_riscv::R_RISCV_SUB6; 83404eeddc0SDimitry Andric case ELF::R_RISCV_SET6: 83504eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_SET6; 83604eeddc0SDimitry Andric case ELF::R_RISCV_SET8: 83704eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_SET8; 83804eeddc0SDimitry Andric case ELF::R_RISCV_SET16: 83904eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_SET16; 84004eeddc0SDimitry Andric case ELF::R_RISCV_SET32: 84104eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_SET32; 84204eeddc0SDimitry Andric case ELF::R_RISCV_32_PCREL: 84304eeddc0SDimitry Andric return EdgeKind_riscv::R_RISCV_32_PCREL; 84406c3fb27SDimitry Andric case ELF::R_RISCV_ALIGN: 84506c3fb27SDimitry Andric return EdgeKind_riscv::AlignRelaxable; 846fe6060f1SDimitry Andric } 847fe6060f1SDimitry Andric 84881ad6265SDimitry Andric return make_error<JITLinkError>( 84981ad6265SDimitry Andric "Unsupported riscv relocation:" + formatv("{0:d}: ", Type) + 85081ad6265SDimitry Andric object::getELFRelocationTypeName(ELF::EM_RISCV, Type)); 851fe6060f1SDimitry Andric } 852fe6060f1SDimitry Andric 85306c3fb27SDimitry Andric EdgeKind_riscv getRelaxableRelocationKind(EdgeKind_riscv Kind) { 85406c3fb27SDimitry Andric switch (Kind) { 85506c3fb27SDimitry Andric default: 85606c3fb27SDimitry Andric // Just ignore unsupported relaxations 85706c3fb27SDimitry Andric return Kind; 85806c3fb27SDimitry Andric case R_RISCV_CALL: 85906c3fb27SDimitry Andric case R_RISCV_CALL_PLT: 86006c3fb27SDimitry Andric return CallRelaxable; 86106c3fb27SDimitry Andric } 86206c3fb27SDimitry Andric } 86306c3fb27SDimitry Andric 864fe6060f1SDimitry Andric Error addRelocations() override { 865349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 866349cc55cSDimitry Andric 867fe6060f1SDimitry Andric using Base = ELFLinkGraphBuilder<ELFT>; 868349cc55cSDimitry Andric using Self = ELFLinkGraphBuilder_riscv<ELFT>; 869349cc55cSDimitry Andric for (const auto &RelSect : Base::Sections) 870bdd1243dSDimitry Andric if (Error Err = Base::forEachRelaRelocation(RelSect, this, 871349cc55cSDimitry Andric &Self::addSingleRelocation)) 872349cc55cSDimitry Andric return Err; 873fe6060f1SDimitry Andric 874349cc55cSDimitry Andric return Error::success(); 875fe6060f1SDimitry Andric } 876fe6060f1SDimitry Andric 877349cc55cSDimitry Andric Error addSingleRelocation(const typename ELFT::Rela &Rel, 878349cc55cSDimitry Andric const typename ELFT::Shdr &FixupSect, 87904eeddc0SDimitry Andric Block &BlockToFix) { 880349cc55cSDimitry Andric using Base = ELFLinkGraphBuilder<ELFT>; 881349cc55cSDimitry Andric 882753f127fSDimitry Andric uint32_t Type = Rel.getType(false); 883753f127fSDimitry Andric int64_t Addend = Rel.r_addend; 88406c3fb27SDimitry Andric 88506c3fb27SDimitry Andric if (Type == ELF::R_RISCV_RELAX) { 88606c3fb27SDimitry Andric if (BlockToFix.edges_empty()) 88706c3fb27SDimitry Andric return make_error<StringError>( 88806c3fb27SDimitry Andric "R_RISCV_RELAX without preceding relocation", 88906c3fb27SDimitry Andric inconvertibleErrorCode()); 89006c3fb27SDimitry Andric 89106c3fb27SDimitry Andric auto &PrevEdge = *std::prev(BlockToFix.edges().end()); 89206c3fb27SDimitry Andric auto Kind = static_cast<EdgeKind_riscv>(PrevEdge.getKind()); 89306c3fb27SDimitry Andric PrevEdge.setKind(getRelaxableRelocationKind(Kind)); 894753f127fSDimitry Andric return Error::success(); 895753f127fSDimitry Andric } 896753f127fSDimitry Andric 897753f127fSDimitry Andric Expected<riscv::EdgeKind_riscv> Kind = getRelocationKind(Type); 898753f127fSDimitry Andric if (!Kind) 899753f127fSDimitry Andric return Kind.takeError(); 900753f127fSDimitry Andric 901349cc55cSDimitry Andric uint32_t SymbolIndex = Rel.getSymbol(false); 902349cc55cSDimitry Andric auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec); 903349cc55cSDimitry Andric if (!ObjSymbol) 904349cc55cSDimitry Andric return ObjSymbol.takeError(); 905349cc55cSDimitry Andric 906349cc55cSDimitry Andric Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex); 907349cc55cSDimitry Andric if (!GraphSymbol) 908349cc55cSDimitry Andric return make_error<StringError>( 909349cc55cSDimitry Andric formatv("Could not find symbol at given index, did you add it to " 910349cc55cSDimitry Andric "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}", 911349cc55cSDimitry Andric SymbolIndex, (*ObjSymbol)->st_shndx, 912349cc55cSDimitry Andric Base::GraphSymbols.size()), 913349cc55cSDimitry Andric inconvertibleErrorCode()); 914349cc55cSDimitry Andric 91504eeddc0SDimitry Andric auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset; 91604eeddc0SDimitry Andric Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress(); 917349cc55cSDimitry Andric Edge GE(*Kind, Offset, *GraphSymbol, Addend); 918349cc55cSDimitry Andric LLVM_DEBUG({ 919349cc55cSDimitry Andric dbgs() << " "; 92004eeddc0SDimitry Andric printEdge(dbgs(), BlockToFix, GE, riscv::getEdgeKindName(*Kind)); 921349cc55cSDimitry Andric dbgs() << "\n"; 922349cc55cSDimitry Andric }); 923349cc55cSDimitry Andric 92404eeddc0SDimitry Andric BlockToFix.addEdge(std::move(GE)); 925fe6060f1SDimitry Andric return Error::success(); 926fe6060f1SDimitry Andric } 927fe6060f1SDimitry Andric 928fe6060f1SDimitry Andric public: 929fe6060f1SDimitry Andric ELFLinkGraphBuilder_riscv(StringRef FileName, 93006c3fb27SDimitry Andric const object::ELFFile<ELFT> &Obj, Triple TT, 93106c3fb27SDimitry Andric SubtargetFeatures Features) 93206c3fb27SDimitry Andric : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features), 93306c3fb27SDimitry Andric FileName, riscv::getEdgeKindName) {} 934fe6060f1SDimitry Andric }; 935fe6060f1SDimitry Andric 936fe6060f1SDimitry Andric Expected<std::unique_ptr<LinkGraph>> 937fe6060f1SDimitry Andric createLinkGraphFromELFObject_riscv(MemoryBufferRef ObjectBuffer) { 938fe6060f1SDimitry Andric LLVM_DEBUG({ 939fe6060f1SDimitry Andric dbgs() << "Building jitlink graph for new input " 940fe6060f1SDimitry Andric << ObjectBuffer.getBufferIdentifier() << "...\n"; 941fe6060f1SDimitry Andric }); 942fe6060f1SDimitry Andric 943fe6060f1SDimitry Andric auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer); 944fe6060f1SDimitry Andric if (!ELFObj) 945fe6060f1SDimitry Andric return ELFObj.takeError(); 946fe6060f1SDimitry Andric 94706c3fb27SDimitry Andric auto Features = (*ELFObj)->getFeatures(); 94806c3fb27SDimitry Andric if (!Features) 94906c3fb27SDimitry Andric return Features.takeError(); 95006c3fb27SDimitry Andric 951fe6060f1SDimitry Andric if ((*ELFObj)->getArch() == Triple::riscv64) { 952fe6060f1SDimitry Andric auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj); 953fe6060f1SDimitry Andric return ELFLinkGraphBuilder_riscv<object::ELF64LE>( 954fe6060f1SDimitry Andric (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 95506c3fb27SDimitry Andric (*ELFObj)->makeTriple(), std::move(*Features)) 956fe6060f1SDimitry Andric .buildGraph(); 957fe6060f1SDimitry Andric } else { 958fe6060f1SDimitry Andric assert((*ELFObj)->getArch() == Triple::riscv32 && 959fe6060f1SDimitry Andric "Invalid triple for RISCV ELF object file"); 960fe6060f1SDimitry Andric auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj); 961fe6060f1SDimitry Andric return ELFLinkGraphBuilder_riscv<object::ELF32LE>( 962fe6060f1SDimitry Andric (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), 96306c3fb27SDimitry Andric (*ELFObj)->makeTriple(), std::move(*Features)) 964fe6060f1SDimitry Andric .buildGraph(); 965fe6060f1SDimitry Andric } 966fe6060f1SDimitry Andric } 967fe6060f1SDimitry Andric 968fe6060f1SDimitry Andric void link_ELF_riscv(std::unique_ptr<LinkGraph> G, 969fe6060f1SDimitry Andric std::unique_ptr<JITLinkContext> Ctx) { 970fe6060f1SDimitry Andric PassConfiguration Config; 971fe6060f1SDimitry Andric const Triple &TT = G->getTargetTriple(); 972fe6060f1SDimitry Andric if (Ctx->shouldAddDefaultTargetPasses(TT)) { 9735f757f3fSDimitry Andric 9745f757f3fSDimitry Andric Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame")); 9755f757f3fSDimitry Andric Config.PrePrunePasses.push_back(EHFrameEdgeFixer( 9765f757f3fSDimitry Andric ".eh_frame", G->getPointerSize(), Edge::Invalid, Edge::Invalid, 9775f757f3fSDimitry Andric Edge::Invalid, Edge::Invalid, NegDelta32)); 9785f757f3fSDimitry Andric Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame")); 9795f757f3fSDimitry Andric 980fe6060f1SDimitry Andric if (auto MarkLive = Ctx->getMarkLivePass(TT)) 981fe6060f1SDimitry Andric Config.PrePrunePasses.push_back(std::move(MarkLive)); 982fe6060f1SDimitry Andric else 983fe6060f1SDimitry Andric Config.PrePrunePasses.push_back(markAllSymbolsLive); 984349cc55cSDimitry Andric Config.PostPrunePasses.push_back( 985349cc55cSDimitry Andric PerGraphGOTAndPLTStubsBuilder_ELF_riscv::asPass); 98606c3fb27SDimitry Andric Config.PostAllocationPasses.push_back(relax); 987fe6060f1SDimitry Andric } 988fe6060f1SDimitry Andric if (auto Err = Ctx->modifyPassConfig(*G, Config)) 989fe6060f1SDimitry Andric return Ctx->notifyFailed(std::move(Err)); 990fe6060f1SDimitry Andric 991fe6060f1SDimitry Andric ELFJITLinker_riscv::link(std::move(Ctx), std::move(G), std::move(Config)); 992fe6060f1SDimitry Andric } 993fe6060f1SDimitry Andric 99406c3fb27SDimitry Andric LinkGraphPassFunction createRelaxationPass_ELF_riscv() { return relax; } 99506c3fb27SDimitry Andric 996fe6060f1SDimitry Andric } // namespace jitlink 997fe6060f1SDimitry Andric } // namespace llvm 998