LLVM 20.0.0git
ELF_aarch32.cpp
Go to the documentation of this file.
1//===----- ELF_aarch32.cpp - JIT linker implementation for arm/thumb ------===//
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/aarch32 jit-link implementation.
10//
11//===----------------------------------------------------------------------===//
12
14
18#include "llvm/Object/ELF.h"
22
23#include "ELFLinkGraphBuilder.h"
24#include "JITLinkGeneric.h"
25
26#define DEBUG_TYPE "jitlink"
27
28using namespace llvm::object;
29
30namespace llvm {
31namespace jitlink {
32
33/// Translate from ELF relocation type to JITLink-internal edge kind.
36 switch (ELFType) {
37 case ELF::R_ARM_ABS32:
39 case ELF::R_ARM_GOT_PREL:
41 case ELF::R_ARM_REL32:
43 case ELF::R_ARM_CALL:
44 return aarch32::Arm_Call;
45 case ELF::R_ARM_JUMP24:
47 case ELF::R_ARM_MOVW_ABS_NC:
49 case ELF::R_ARM_MOVT_ABS:
51 case ELF::R_ARM_NONE:
52 return aarch32::None;
53 case ELF::R_ARM_PREL31:
55 case ELF::R_ARM_TARGET1:
56 return (ArmCfg.Target1Rel) ? aarch32::Data_Delta32
58 case ELF::R_ARM_THM_CALL:
60 case ELF::R_ARM_THM_JUMP24:
62 case ELF::R_ARM_THM_MOVW_ABS_NC:
64 case ELF::R_ARM_THM_MOVT_ABS:
66 case ELF::R_ARM_THM_MOVW_PREL_NC:
68 case ELF::R_ARM_THM_MOVT_PREL:
70 }
71
72 return make_error<JITLinkError>(
73 "Unsupported aarch32 relocation " + formatv("{0:d}: ", ELFType) +
75}
76
77/// Translate from JITLink-internal edge kind back to ELF relocation type.
79 switch (static_cast<aarch32::EdgeKind_aarch32>(Kind)) {
81 return ELF::R_ARM_REL32;
83 return ELF::R_ARM_ABS32;
85 return ELF::R_ARM_PREL31;
87 return ELF::R_ARM_GOT_PREL;
89 return ELF::R_ARM_CALL;
91 return ELF::R_ARM_JUMP24;
93 return ELF::R_ARM_MOVW_ABS_NC;
95 return ELF::R_ARM_MOVT_ABS;
97 return ELF::R_ARM_THM_CALL;
99 return ELF::R_ARM_THM_JUMP24;
101 return ELF::R_ARM_THM_MOVW_ABS_NC;
103 return ELF::R_ARM_THM_MOVT_ABS;
105 return ELF::R_ARM_THM_MOVW_PREL_NC;
107 return ELF::R_ARM_THM_MOVT_PREL;
108 case aarch32::None:
109 return ELF::R_ARM_NONE;
110 }
111
112 return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",
113 Kind));
114}
115
116/// Get a human-readable name for the given ELF AArch32 edge kind.
118 // No ELF-specific edge kinds yet
119 return aarch32::getEdgeKindName(R);
120}
121
122class ELFJITLinker_aarch32 : public JITLinker<ELFJITLinker_aarch32> {
123 friend class JITLinker<ELFJITLinker_aarch32>;
124
125public:
126 ELFJITLinker_aarch32(std::unique_ptr<JITLinkContext> Ctx,
127 std::unique_ptr<LinkGraph> G, PassConfiguration PassCfg,
128 aarch32::ArmConfig ArmCfg)
129 : JITLinker(std::move(Ctx), std::move(G), std::move(PassCfg)),
130 ArmCfg(std::move(ArmCfg)) {}
131
132private:
133 aarch32::ArmConfig ArmCfg;
134
135 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
136 return aarch32::applyFixup(G, B, E, ArmCfg);
137 }
138};
139
140template <llvm::endianness DataEndianness>
142 : public ELFLinkGraphBuilder<ELFType<DataEndianness, false>> {
143private:
146
147 Error addRelocations() override {
148 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
150 for (const auto &RelSect : Base::Sections) {
151 if (Error Err = Base::forEachRelRelocation(RelSect, this,
152 &Self::addSingleRelRelocation))
153 return Err;
154 }
155 return Error::success();
156 }
157
158 Error addSingleRelRelocation(const typename ELFT::Rel &Rel,
159 const typename ELFT::Shdr &FixupSect,
160 Block &BlockToFix) {
161 uint32_t SymbolIndex = Rel.getSymbol(false);
162 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
163 if (!ObjSymbol)
164 return ObjSymbol.takeError();
165
166 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
167 if (!GraphSymbol)
168 return make_error<StringError>(
169 formatv("Could not find symbol at given index, did you add it to "
170 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
171 SymbolIndex, (*ObjSymbol)->st_shndx,
172 Base::GraphSymbols.size()),
174
175 uint32_t Type = Rel.getType(false);
177 if (!Kind)
178 return Kind.takeError();
179
180 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
181 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
182
183 Expected<int64_t> Addend =
184 aarch32::readAddend(*Base::G, BlockToFix, Offset, *Kind, ArmCfg);
185 if (!Addend)
186 return Addend.takeError();
187
188 Edge E(*Kind, Offset, *GraphSymbol, *Addend);
189 LLVM_DEBUG({
190 dbgs() << " ";
191 printEdge(dbgs(), BlockToFix, E, getELFAArch32EdgeKindName(*Kind));
192 dbgs() << "\n";
193 });
194
195 BlockToFix.addEdge(std::move(E));
196 return Error::success();
197 }
198
199 aarch32::ArmConfig ArmCfg;
200
201protected:
202 TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override {
203 // Only emit target flag for callable symbols
204 if (Sym.getType() != ELF::STT_FUNC)
205 return TargetFlagsType{};
206 if (Sym.getValue() & 0x01)
208 return TargetFlagsType{};
209 }
210
212 TargetFlagsType Flags) override {
213 assert((makeTargetFlags(Sym) & Flags) == Flags);
214 static constexpr uint64_t ThumbBit = 0x01;
215 if (Sym.getType() == ELF::STT_FUNC)
216 return Sym.getValue() & ~ThumbBit;
217 return Sym.getValue();
218 }
219
220public:
223 std::shared_ptr<orc::SymbolStringPool> SSP,
224 Triple TT, SubtargetFeatures Features,
225 aarch32::ArmConfig ArmCfg)
226 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(SSP), std::move(TT),
227 std::move(Features), FileName,
229 ArmCfg(std::move(ArmCfg)) {}
230};
231
232template <typename StubsManagerType>
234 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
235
236 StubsManagerType StubsManager;
237 visitExistingEdges(G, StubsManager);
239 visitExistingEdges(G, GOT);
240
241 return Error::success();
242}
243
245 MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
246 LLVM_DEBUG({
247 dbgs() << "Building jitlink graph for new input "
248 << ObjectBuffer.getBufferIdentifier() << "...\n";
249 });
250
251 auto ELFObj = ObjectFile::createELFObjectFile(ObjectBuffer);
252 if (!ELFObj)
253 return ELFObj.takeError();
254
255 auto Features = (*ELFObj)->getFeatures();
256 if (!Features)
257 return Features.takeError();
258
259 // Find out what exact AArch32 instruction set and features we target.
260 auto TT = (*ELFObj)->makeTriple();
261 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
262 if (AK == ARM::ArchKind::INVALID)
263 return make_error<JITLinkError>(
264 "Failed to build ELF link graph: Invalid ARM ArchKind");
265
266 // Resolve our internal configuration for the target. If at some point the
267 // CPUArch alone becomes too unprecise, we can find more details in the
268 // Tag_CPU_arch_profile.
269 auto Arch = static_cast<ARMBuildAttrs::CPUArch>(ARM::getArchAttr(AK));
271
272 // Populate the link-graph.
273 switch (TT.getArch()) {
274 case Triple::arm:
275 case Triple::thumb: {
276 auto &ELFFile = cast<ELFObjectFile<ELF32LE>>(**ELFObj).getELFFile();
278 (*ELFObj)->getFileName(), ELFFile, std::move(SSP), TT,
279 std::move(*Features), ArmCfg)
280 .buildGraph();
281 }
282 case Triple::armeb:
283 case Triple::thumbeb: {
284 auto &ELFFile = cast<ELFObjectFile<ELF32BE>>(**ELFObj).getELFFile();
286 (*ELFObj)->getFileName(), ELFFile, std::move(SSP), TT,
287 std::move(*Features), ArmCfg)
288 .buildGraph();
289 }
290 default:
291 return make_error<JITLinkError>(
292 "Failed to build ELF/aarch32 link graph: Invalid target triple " +
293 TT.getTriple());
294 }
295}
296
297void link_ELF_aarch32(std::unique_ptr<LinkGraph> G,
298 std::unique_ptr<JITLinkContext> Ctx) {
299 const Triple &TT = G->getTargetTriple();
300
301 using namespace ARMBuildAttrs;
302 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
303 auto CPU = static_cast<CPUArch>(ARM::getArchAttr(AK));
305
306 PassConfiguration PassCfg;
307 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
308 // Add a mark-live pass.
309 if (auto MarkLive = Ctx->getMarkLivePass(TT))
310 PassCfg.PrePrunePasses.push_back(std::move(MarkLive));
311 else
312 PassCfg.PrePrunePasses.push_back(markAllSymbolsLive);
313
314 switch (ArmCfg.Stubs) {
316 PassCfg.PostPrunePasses.push_back(
317 buildTables_ELF_aarch32<aarch32::StubsManager_prev7>);
318 break;
320 PassCfg.PostPrunePasses.push_back(
321 buildTables_ELF_aarch32<aarch32::StubsManager_v7>);
322 break;
324 llvm_unreachable("Check before building graph");
325 }
326 }
327
328 if (auto Err = Ctx->modifyPassConfig(*G, PassCfg))
329 return Ctx->notifyFailed(std::move(Err));
330
331 ELFJITLinker_aarch32::link(std::move(Ctx), std::move(G), std::move(PassCfg),
332 std::move(ArmCfg));
333}
334
335} // namespace jitlink
336} // 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
Symbol * Sym
Definition: ELF_riscv.cpp:479
#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
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
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
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel &Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
Definition: ELF.h:756
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Represents an address in the executor process.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ArchKind parseArch(StringRef Arch)
unsigned getArchAttr(ArchKind AK)
@ EM_ARM
Definition: ELF.h:157
@ STT_FUNC
Definition: ELF.h:1355
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
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858