LLVM 18.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"
20#include "llvm/Support/Endian.h"
23
24#include "ELFLinkGraphBuilder.h"
25#include "JITLinkGeneric.h"
26
27#define DEBUG_TYPE "jitlink"
28
29using namespace llvm::object;
30
31namespace llvm {
32namespace jitlink {
33
34/// Translate from ELF relocation type to JITLink-internal edge kind.
36 switch (ELFType) {
37 case ELF::R_ARM_ABS32:
39 case ELF::R_ARM_REL32:
41 case ELF::R_ARM_CALL:
42 return aarch32::Arm_Call;
43 case ELF::R_ARM_JUMP24:
45 case ELF::R_ARM_MOVW_ABS_NC:
47 case ELF::R_ARM_MOVT_ABS:
49 case ELF::R_ARM_THM_CALL:
51 case ELF::R_ARM_THM_JUMP24:
53 case ELF::R_ARM_THM_MOVW_ABS_NC:
55 case ELF::R_ARM_THM_MOVT_ABS:
57 }
58
59 return make_error<JITLinkError>(
60 "Unsupported aarch32 relocation " + formatv("{0:d}: ", ELFType) +
62}
63
64/// Translate from JITLink-internal edge kind back to ELF relocation type.
66 switch (static_cast<aarch32::EdgeKind_aarch32>(Kind)) {
68 return ELF::R_ARM_REL32;
70 return ELF::R_ARM_ABS32;
72 return ELF::R_ARM_CALL;
74 return ELF::R_ARM_JUMP24;
76 return ELF::R_ARM_MOVW_ABS_NC;
78 return ELF::R_ARM_MOVT_ABS;
80 return ELF::R_ARM_THM_CALL;
82 return ELF::R_ARM_THM_JUMP24;
84 return ELF::R_ARM_THM_MOVW_ABS_NC;
86 return ELF::R_ARM_THM_MOVT_ABS;
87 }
88
89 return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",
90 Kind));
91}
92
93/// Get a human-readable name for the given ELF AArch32 edge kind.
95 // No ELF-specific edge kinds yet
97}
98
99class ELFJITLinker_aarch32 : public JITLinker<ELFJITLinker_aarch32> {
100 friend class JITLinker<ELFJITLinker_aarch32>;
101
102public:
103 ELFJITLinker_aarch32(std::unique_ptr<JITLinkContext> Ctx,
104 std::unique_ptr<LinkGraph> G, PassConfiguration PassCfg,
105 aarch32::ArmConfig ArmCfg)
106 : JITLinker(std::move(Ctx), std::move(G), std::move(PassCfg)),
107 ArmCfg(std::move(ArmCfg)) {}
108
109private:
110 aarch32::ArmConfig ArmCfg;
111
112 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
113 return aarch32::applyFixup(G, B, E, ArmCfg);
114 }
115};
116
117template <support::endianness DataEndianness>
119 : public ELFLinkGraphBuilder<ELFType<DataEndianness, false>> {
120private:
123
124 bool excludeSection(const typename ELFT::Shdr &Sect) const override {
125 // TODO: An .ARM.exidx (Exception Index table) entry is 8-bytes in size and
126 // consists of 2 words. It might be sufficient to process only relocations
127 // in the the second word (offset 4). Please find more details in: Exception
128 // Handling ABI for the ArmĀ® Architecture -> Index table entries
129 if (Sect.sh_type == ELF::SHT_ARM_EXIDX)
130 return true;
131 return false;
132 }
133
134 Error addRelocations() override {
135 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
137 for (const auto &RelSect : Base::Sections) {
138 if (Error Err = Base::forEachRelRelocation(RelSect, this,
139 &Self::addSingleRelRelocation))
140 return Err;
141 }
142 return Error::success();
143 }
144
145 Error addSingleRelRelocation(const typename ELFT::Rel &Rel,
146 const typename ELFT::Shdr &FixupSect,
147 Block &BlockToFix) {
148 uint32_t SymbolIndex = Rel.getSymbol(false);
149 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
150 if (!ObjSymbol)
151 return ObjSymbol.takeError();
152
153 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
154 if (!GraphSymbol)
155 return make_error<StringError>(
156 formatv("Could not find symbol at given index, did you add it to "
157 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
158 SymbolIndex, (*ObjSymbol)->st_shndx,
159 Base::GraphSymbols.size()),
161
162 uint32_t Type = Rel.getType(false);
164 if (!Kind)
165 return Kind.takeError();
166
167 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
168 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
169 Edge E(*Kind, Offset, *GraphSymbol, 0);
170
171 Expected<int64_t> Addend =
172 aarch32::readAddend(*Base::G, BlockToFix, E, ArmCfg);
173 if (!Addend)
174 return Addend.takeError();
175
176 E.setAddend(*Addend);
177 LLVM_DEBUG({
178 dbgs() << " ";
179 printEdge(dbgs(), BlockToFix, E, getELFAArch32EdgeKindName(*Kind));
180 dbgs() << "\n";
181 });
182
183 BlockToFix.addEdge(std::move(E));
184 return Error::success();
185 }
186
187 aarch32::ArmConfig ArmCfg;
188
189protected:
190 TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override {
191 if (Sym.getValue() & 0x01)
193 return TargetFlagsType{};
194 }
195
197 TargetFlagsType Flags) override {
198 assert((makeTargetFlags(Sym) & Flags) == Flags);
199 static constexpr uint64_t ThumbBit = 0x01;
200 return Sym.getValue() & ~ThumbBit;
201 }
202
203public:
206 SubtargetFeatures Features,
207 aarch32::ArmConfig ArmCfg)
208 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features),
209 FileName, getELFAArch32EdgeKindName),
210 ArmCfg(std::move(ArmCfg)) {}
211};
212
213template <aarch32::StubsFlavor Flavor>
215 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
216
218 visitExistingEdges(G, PLT);
219 return Error::success();
220}
221
224 LLVM_DEBUG({
225 dbgs() << "Building jitlink graph for new input "
226 << ObjectBuffer.getBufferIdentifier() << "...\n";
227 });
228
229 auto ELFObj = ObjectFile::createELFObjectFile(ObjectBuffer);
230 if (!ELFObj)
231 return ELFObj.takeError();
232
233 auto Features = (*ELFObj)->getFeatures();
234 if (!Features)
235 return Features.takeError();
236
237 // Find out what exact AArch32 instruction set and features we target.
238 auto TT = (*ELFObj)->makeTriple();
239 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
240 if (AK == ARM::ArchKind::INVALID)
241 return make_error<JITLinkError>(
242 "Failed to build ELF link graph: Invalid ARM ArchKind");
243
244 // Resolve our internal configuration for the target. If at some point the
245 // CPUArch alone becomes too unprecise, we can find more details in the
246 // Tag_CPU_arch_profile.
247 aarch32::ArmConfig ArmCfg;
248 using namespace ARMBuildAttrs;
249 auto Arch = static_cast<CPUArch>(ARM::getArchAttr(AK));
250 switch (Arch) {
251 case v7:
252 case v8_A:
253 ArmCfg = aarch32::getArmConfigForCPUArch(Arch);
255 "Provide a config for each supported CPU");
256 break;
257 default:
258 return make_error<JITLinkError>(
259 "Failed to build ELF link graph: Unsupported CPU arch " +
261 }
262
263 // Populate the link-graph.
264 switch (TT.getArch()) {
265 case Triple::arm:
266 case Triple::thumb: {
267 auto &ELFFile = cast<ELFObjectFile<ELF32LE>>(**ELFObj).getELFFile();
269 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features),
270 ArmCfg)
271 .buildGraph();
272 }
273 case Triple::armeb:
274 case Triple::thumbeb: {
275 auto &ELFFile = cast<ELFObjectFile<ELF32BE>>(**ELFObj).getELFFile();
277 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features),
278 ArmCfg)
279 .buildGraph();
280 }
281 default:
282 return make_error<JITLinkError>(
283 "Failed to build ELF/aarch32 link graph: Invalid target triple " +
284 TT.getTriple());
285 }
286}
287
288void link_ELF_aarch32(std::unique_ptr<LinkGraph> G,
289 std::unique_ptr<JITLinkContext> Ctx) {
290 const Triple &TT = G->getTargetTriple();
291
292 using namespace ARMBuildAttrs;
293 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
294 auto CPU = static_cast<CPUArch>(ARM::getArchAttr(AK));
296
297 PassConfiguration PassCfg;
298 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
299 // Add a mark-live pass.
300 if (auto MarkLive = Ctx->getMarkLivePass(TT))
301 PassCfg.PrePrunePasses.push_back(std::move(MarkLive));
302 else
303 PassCfg.PrePrunePasses.push_back(markAllSymbolsLive);
304
305 switch (ArmCfg.Stubs) {
306 case aarch32::Thumbv7:
307 PassCfg.PostPrunePasses.push_back(
308 buildTables_ELF_aarch32<aarch32::Thumbv7>);
309 break;
311 llvm_unreachable("Check before building graph");
312 }
313 }
314
315 if (auto Err = Ctx->modifyPassConfig(*G, PassCfg))
316 return Ctx->notifyFailed(std::move(Err));
317
318 ELFJITLinker_aarch32::link(std::move(Ctx), std::move(G), std::move(PassCfg),
319 std::move(ArmCfg));
320}
321
322} // namespace jitlink
323} // 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(X)
Definition: Debug.h:101
Symbol * Sym
Definition: ELF_riscv.cpp:468
#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:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:660
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:156
@ SHT_ARM_EXIDX
Definition: ELF.h:1052
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:23
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:90
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:1854
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858