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"
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.
35 switch (ELFType) {
36 case ELF::R_ARM_ABS32:
38 case ELF::R_ARM_REL32:
40 case ELF::R_ARM_CALL:
41 return aarch32::Arm_Call;
42 case ELF::R_ARM_JUMP24:
44 case ELF::R_ARM_MOVW_ABS_NC:
46 case ELF::R_ARM_MOVT_ABS:
48 case ELF::R_ARM_THM_CALL:
50 case ELF::R_ARM_THM_JUMP24:
52 case ELF::R_ARM_THM_MOVW_ABS_NC:
54 case ELF::R_ARM_THM_MOVT_ABS:
56 case ELF::R_ARM_THM_MOVW_PREL_NC:
58 case ELF::R_ARM_THM_MOVT_PREL:
60 }
61
62 return make_error<JITLinkError>(
63 "Unsupported aarch32 relocation " + formatv("{0:d}: ", ELFType) +
65}
66
67/// Translate from JITLink-internal edge kind back to ELF relocation type.
69 switch (static_cast<aarch32::EdgeKind_aarch32>(Kind)) {
71 return ELF::R_ARM_REL32;
73 return ELF::R_ARM_ABS32;
75 return ELF::R_ARM_CALL;
77 return ELF::R_ARM_JUMP24;
79 return ELF::R_ARM_MOVW_ABS_NC;
81 return ELF::R_ARM_MOVT_ABS;
83 return ELF::R_ARM_THM_CALL;
85 return ELF::R_ARM_THM_JUMP24;
87 return ELF::R_ARM_THM_MOVW_ABS_NC;
89 return ELF::R_ARM_THM_MOVT_ABS;
91 return ELF::R_ARM_THM_MOVW_PREL_NC;
93 return ELF::R_ARM_THM_MOVT_PREL;
94 }
95
96 return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",
97 Kind));
98}
99
100/// Get a human-readable name for the given ELF AArch32 edge kind.
102 // No ELF-specific edge kinds yet
103 return aarch32::getEdgeKindName(R);
104}
105
106class ELFJITLinker_aarch32 : public JITLinker<ELFJITLinker_aarch32> {
107 friend class JITLinker<ELFJITLinker_aarch32>;
108
109public:
110 ELFJITLinker_aarch32(std::unique_ptr<JITLinkContext> Ctx,
111 std::unique_ptr<LinkGraph> G, PassConfiguration PassCfg,
112 aarch32::ArmConfig ArmCfg)
113 : JITLinker(std::move(Ctx), std::move(G), std::move(PassCfg)),
114 ArmCfg(std::move(ArmCfg)) {}
115
116private:
117 aarch32::ArmConfig ArmCfg;
118
119 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
120 return aarch32::applyFixup(G, B, E, ArmCfg);
121 }
122};
123
124template <llvm::endianness DataEndianness>
126 : public ELFLinkGraphBuilder<ELFType<DataEndianness, false>> {
127private:
130
131 bool excludeSection(const typename ELFT::Shdr &Sect) const override {
132 // TODO: An .ARM.exidx (Exception Index table) entry is 8-bytes in size and
133 // consists of 2 words. It might be sufficient to process only relocations
134 // in the the second word (offset 4). Please find more details in: Exception
135 // Handling ABI for the ArmĀ® Architecture -> Index table entries
136 if (Sect.sh_type == ELF::SHT_ARM_EXIDX)
137 return true;
138 return false;
139 }
140
141 Error addRelocations() override {
142 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
144 for (const auto &RelSect : Base::Sections) {
145 if (Error Err = Base::forEachRelRelocation(RelSect, this,
146 &Self::addSingleRelRelocation))
147 return Err;
148 }
149 return Error::success();
150 }
151
152 Error addSingleRelRelocation(const typename ELFT::Rel &Rel,
153 const typename ELFT::Shdr &FixupSect,
154 Block &BlockToFix) {
155 uint32_t SymbolIndex = Rel.getSymbol(false);
156 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
157 if (!ObjSymbol)
158 return ObjSymbol.takeError();
159
160 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
161 if (!GraphSymbol)
162 return make_error<StringError>(
163 formatv("Could not find symbol at given index, did you add it to "
164 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
165 SymbolIndex, (*ObjSymbol)->st_shndx,
166 Base::GraphSymbols.size()),
168
169 uint32_t Type = Rel.getType(false);
171 if (!Kind)
172 return Kind.takeError();
173
174 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
175 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
176
177 Expected<int64_t> Addend =
178 aarch32::readAddend(*Base::G, BlockToFix, Offset, *Kind, ArmCfg);
179 if (!Addend)
180 return Addend.takeError();
181
182 Edge E(*Kind, Offset, *GraphSymbol, *Addend);
183 LLVM_DEBUG({
184 dbgs() << " ";
185 printEdge(dbgs(), BlockToFix, E, getELFAArch32EdgeKindName(*Kind));
186 dbgs() << "\n";
187 });
188
189 BlockToFix.addEdge(std::move(E));
190 return Error::success();
191 }
192
193 aarch32::ArmConfig ArmCfg;
194
195protected:
196 TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override {
197 if (Sym.getValue() & 0x01)
199 return TargetFlagsType{};
200 }
201
203 TargetFlagsType Flags) override {
204 assert((makeTargetFlags(Sym) & Flags) == Flags);
205 static constexpr uint64_t ThumbBit = 0x01;
206 return Sym.getValue() & ~ThumbBit;
207 }
208
209public:
212 SubtargetFeatures Features,
213 aarch32::ArmConfig ArmCfg)
214 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features),
215 FileName, getELFAArch32EdgeKindName),
216 ArmCfg(std::move(ArmCfg)) {}
217};
218
219template <aarch32::StubsFlavor Flavor>
221 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
222
224 visitExistingEdges(G, PLT);
225 return Error::success();
226}
227
230 LLVM_DEBUG({
231 dbgs() << "Building jitlink graph for new input "
232 << ObjectBuffer.getBufferIdentifier() << "...\n";
233 });
234
235 auto ELFObj = ObjectFile::createELFObjectFile(ObjectBuffer);
236 if (!ELFObj)
237 return ELFObj.takeError();
238
239 auto Features = (*ELFObj)->getFeatures();
240 if (!Features)
241 return Features.takeError();
242
243 // Find out what exact AArch32 instruction set and features we target.
244 auto TT = (*ELFObj)->makeTriple();
245 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
246 if (AK == ARM::ArchKind::INVALID)
247 return make_error<JITLinkError>(
248 "Failed to build ELF link graph: Invalid ARM ArchKind");
249
250 // Resolve our internal configuration for the target. If at some point the
251 // CPUArch alone becomes too unprecise, we can find more details in the
252 // Tag_CPU_arch_profile.
253 aarch32::ArmConfig ArmCfg;
254 using namespace ARMBuildAttrs;
255 auto Arch = static_cast<CPUArch>(ARM::getArchAttr(AK));
256 switch (Arch) {
257 case v7:
258 case v8_A:
259 ArmCfg = aarch32::getArmConfigForCPUArch(Arch);
261 "Provide a config for each supported CPU");
262 break;
263 default:
264 return make_error<JITLinkError>(
265 "Failed to build ELF link graph: Unsupported CPU arch " +
267 }
268
269 // Populate the link-graph.
270 switch (TT.getArch()) {
271 case Triple::arm:
272 case Triple::thumb: {
273 auto &ELFFile = cast<ELFObjectFile<ELF32LE>>(**ELFObj).getELFFile();
275 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features),
276 ArmCfg)
277 .buildGraph();
278 }
279 case Triple::armeb:
280 case Triple::thumbeb: {
281 auto &ELFFile = cast<ELFObjectFile<ELF32BE>>(**ELFObj).getELFFile();
283 (*ELFObj)->getFileName(), ELFFile, TT, std::move(*Features),
284 ArmCfg)
285 .buildGraph();
286 }
287 default:
288 return make_error<JITLinkError>(
289 "Failed to build ELF/aarch32 link graph: Invalid target triple " +
290 TT.getTriple());
291 }
292}
293
294void link_ELF_aarch32(std::unique_ptr<LinkGraph> G,
295 std::unique_ptr<JITLinkContext> Ctx) {
296 const Triple &TT = G->getTargetTriple();
297
298 using namespace ARMBuildAttrs;
299 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
300 auto CPU = static_cast<CPUArch>(ARM::getArchAttr(AK));
302
303 PassConfiguration PassCfg;
304 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
305 // Add a mark-live pass.
306 if (auto MarkLive = Ctx->getMarkLivePass(TT))
307 PassCfg.PrePrunePasses.push_back(std::move(MarkLive));
308 else
309 PassCfg.PrePrunePasses.push_back(markAllSymbolsLive);
310
311 switch (ArmCfg.Stubs) {
312 case aarch32::Thumbv7:
313 PassCfg.PostPrunePasses.push_back(
314 buildTables_ELF_aarch32<aarch32::Thumbv7>);
315 break;
317 llvm_unreachable("Check before building graph");
318 }
319 }
320
321 if (auto Err = Ctx->modifyPassConfig(*G, PassCfg))
322 return Ctx->notifyFailed(std::move(Err));
323
324 ELFJITLinker_aarch32::link(std::move(Ctx), std::move(G), std::move(PassCfg),
325 std::move(ArmCfg));
326}
327
328} // namespace jitlink
329} // 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:477
#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:703
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:1056
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:456
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:1853
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858