LLVM 20.0.0git
ELF_loongarch.cpp
Go to the documentation of this file.
1//===--- ELF_loongarch.cpp - JIT linker implementation for ELF/loongarch --===//
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/loongarch jit-link implementation.
10//
11//===----------------------------------------------------------------------===//
12
18#include "llvm/Object/ELF.h"
20
21#include "EHFrameSupportImpl.h"
22#include "ELFLinkGraphBuilder.h"
23#include "JITLinkGeneric.h"
24
25#define DEBUG_TYPE "jitlink"
26
27using namespace llvm;
28using namespace llvm::jitlink;
29using namespace llvm::jitlink::loongarch;
30
31namespace {
32
33class ELFJITLinker_loongarch : public JITLinker<ELFJITLinker_loongarch> {
34 friend class JITLinker<ELFJITLinker_loongarch>;
35
36public:
37 ELFJITLinker_loongarch(std::unique_ptr<JITLinkContext> Ctx,
38 std::unique_ptr<LinkGraph> G,
39 PassConfiguration PassConfig)
40 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
41
42private:
43 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
44 return loongarch::applyFixup(G, B, E);
45 }
46};
47
48template <typename ELFT>
49class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
50private:
52 getRelocationKind(const uint32_t Type) {
53 using namespace loongarch;
54 switch (Type) {
55 case ELF::R_LARCH_64:
56 return Pointer64;
57 case ELF::R_LARCH_32:
58 return Pointer32;
59 case ELF::R_LARCH_32_PCREL:
60 return Delta32;
61 case ELF::R_LARCH_B26:
62 return Branch26PCRel;
63 case ELF::R_LARCH_PCALA_HI20:
64 return Page20;
65 case ELF::R_LARCH_PCALA_LO12:
66 return PageOffset12;
67 case ELF::R_LARCH_GOT_PC_HI20:
69 case ELF::R_LARCH_GOT_PC_LO12:
71 case ELF::R_LARCH_CALL36:
72 return Call36PCRel;
73 }
74
75 return make_error<JITLinkError>(
76 "Unsupported loongarch relocation:" + formatv("{0:d}: ", Type) +
78 }
79
80 Error addRelocations() override {
81 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
82
84 using Self = ELFLinkGraphBuilder_loongarch<ELFT>;
85 for (const auto &RelSect : Base::Sections)
86 if (Error Err = Base::forEachRelaRelocation(RelSect, this,
87 &Self::addSingleRelocation))
88 return Err;
89
90 return Error::success();
91 }
92
93 Error addSingleRelocation(const typename ELFT::Rela &Rel,
94 const typename ELFT::Shdr &FixupSect,
95 Block &BlockToFix) {
97
98 uint32_t SymbolIndex = Rel.getSymbol(false);
99 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
100 if (!ObjSymbol)
101 return ObjSymbol.takeError();
102
103 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
104 if (!GraphSymbol)
105 return make_error<StringError>(
106 formatv("Could not find symbol at given index, did you add it to "
107 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
108 SymbolIndex, (*ObjSymbol)->st_shndx,
109 Base::GraphSymbols.size()),
111
112 uint32_t Type = Rel.getType(false);
113 Expected<loongarch::EdgeKind_loongarch> Kind = getRelocationKind(Type);
114 if (!Kind)
115 return Kind.takeError();
116
117 int64_t Addend = Rel.r_addend;
118 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
119 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
120 Edge GE(*Kind, Offset, *GraphSymbol, Addend);
121 LLVM_DEBUG({
122 dbgs() << " ";
123 printEdge(dbgs(), BlockToFix, GE, loongarch::getEdgeKindName(*Kind));
124 dbgs() << "\n";
125 });
126
127 BlockToFix.addEdge(std::move(GE));
128
129 return Error::success();
130 }
131
132public:
133 ELFLinkGraphBuilder_loongarch(StringRef FileName,
134 const object::ELFFile<ELFT> &Obj,
135 std::shared_ptr<orc::SymbolStringPool> SSP,
136 Triple TT, SubtargetFeatures Features)
137 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(SSP), std::move(TT),
138 std::move(Features), FileName,
140};
141
142Error buildTables_ELF_loongarch(LinkGraph &G) {
143 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
144
145 GOTTableManager GOT;
146 PLTTableManager PLT(GOT);
147 visitExistingEdges(G, GOT, PLT);
148 return Error::success();
149}
150
151} // namespace
152
153namespace llvm {
154namespace jitlink {
155
157 MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
158 LLVM_DEBUG({
159 dbgs() << "Building jitlink graph for new input "
160 << ObjectBuffer.getBufferIdentifier() << "...\n";
161 });
162
163 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
164 if (!ELFObj)
165 return ELFObj.takeError();
166
167 auto Features = (*ELFObj)->getFeatures();
168 if (!Features)
169 return Features.takeError();
170
171 if ((*ELFObj)->getArch() == Triple::loongarch64) {
172 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
173 return ELFLinkGraphBuilder_loongarch<object::ELF64LE>(
174 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(),
175 std::move(SSP), (*ELFObj)->makeTriple(), std::move(*Features))
176 .buildGraph();
177 }
178
179 assert((*ELFObj)->getArch() == Triple::loongarch32 &&
180 "Invalid triple for LoongArch ELF object file");
181 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF32LE>>(**ELFObj);
182 return ELFLinkGraphBuilder_loongarch<object::ELF32LE>(
183 (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), std::move(SSP),
184 (*ELFObj)->makeTriple(), std::move(*Features))
185 .buildGraph();
186}
187
188void link_ELF_loongarch(std::unique_ptr<LinkGraph> G,
189 std::unique_ptr<JITLinkContext> Ctx) {
191 const Triple &TT = G->getTargetTriple();
192 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
193 // Add eh-frame passes.
194 Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame"));
195 Config.PrePrunePasses.push_back(
196 EHFrameEdgeFixer(".eh_frame", G->getPointerSize(), Pointer32, Pointer64,
198 Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame"));
199
200 // Add a mark-live pass.
201 if (auto MarkLive = Ctx->getMarkLivePass(TT))
202 Config.PrePrunePasses.push_back(std::move(MarkLive));
203 else
204 Config.PrePrunePasses.push_back(markAllSymbolsLive);
205
206 // Add an in-place GOT/PLTStubs build pass.
207 Config.PostPrunePasses.push_back(buildTables_ELF_loongarch);
208 }
209
210 if (auto Err = Ctx->modifyPassConfig(*G, Config))
211 return Ctx->notifyFailed(std::move(Err));
212
213 ELFJITLinker_loongarch::link(std::move(Ctx), std::move(G), std::move(Config));
214}
215
216} // namespace jitlink
217} // namespace llvm
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DEBUG(...)
Definition: Debug.h:106
RelaxConfig Config
Definition: ELF_riscv.cpp:506
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ loongarch32
Definition: Triple.h:61
@ loongarch64
Definition: Triple.h:62
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Represents an address in the executor process.
@ EM_LOONGARCH
Definition: ELF.h:323
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163