LLVM 17.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_THM_CALL:
45 case ELF::R_ARM_THM_JUMP24:
47 case ELF::R_ARM_THM_MOVW_ABS_NC:
49 case ELF::R_ARM_THM_MOVT_ABS:
51 }
52
53 return make_error<JITLinkError>(
54 "Unsupported aarch32 relocation " + formatv("{0:d}: ", ELFType) +
56}
57
58/// Translate from JITLink-internal edge kind back to ELF relocation type.
60 switch (static_cast<aarch32::EdgeKind_aarch32>(Kind)) {
62 return ELF::R_ARM_REL32;
64 return ELF::R_ARM_ABS32;
66 return ELF::R_ARM_CALL;
68 return ELF::R_ARM_THM_CALL;
70 return ELF::R_ARM_THM_JUMP24;
72 return ELF::R_ARM_THM_MOVW_ABS_NC;
74 return ELF::R_ARM_THM_MOVT_ABS;
75 }
76
77 return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",
78 Kind));
79}
80
81/// Get a human-readable name for the given ELF AArch32 edge kind.
83 // No ELF-specific edge kinds yet
85}
86
87class ELFJITLinker_aarch32 : public JITLinker<ELFJITLinker_aarch32> {
88 friend class JITLinker<ELFJITLinker_aarch32>;
89
90public:
91 ELFJITLinker_aarch32(std::unique_ptr<JITLinkContext> Ctx,
92 std::unique_ptr<LinkGraph> G, PassConfiguration PassCfg,
93 aarch32::ArmConfig ArmCfg)
94 : JITLinker(std::move(Ctx), std::move(G), std::move(PassCfg)),
95 ArmCfg(std::move(ArmCfg)) {}
96
97private:
98 aarch32::ArmConfig ArmCfg;
99
100 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
101 return aarch32::applyFixup(G, B, E, ArmCfg);
102 }
103};
104
105template <support::endianness DataEndianness>
107 : public ELFLinkGraphBuilder<ELFType<DataEndianness, false>> {
108private:
111
112 bool excludeSection(const typename ELFT::Shdr &Sect) const override {
113 // TODO: An .ARM.exidx (Exception Index table) entry is 8-bytes in size and
114 // consists of 2 words. It might be sufficient to process only relocations
115 // in the the second word (offset 4). Please find more details in: Exception
116 // Handling ABI for the ArmĀ® Architecture -> Index table entries
117 if (Sect.sh_type == ELF::SHT_ARM_EXIDX)
118 return true;
119 return false;
120 }
121
122 Error addRelocations() override {
123 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
125 for (const auto &RelSect : Base::Sections) {
126 if (Error Err = Base::forEachRelRelocation(RelSect, this,
127 &Self::addSingleRelRelocation))
128 return Err;
129 }
130 return Error::success();
131 }
132
133 Error addSingleRelRelocation(const typename ELFT::Rel &Rel,
134 const typename ELFT::Shdr &FixupSect,
135 Block &BlockToFix) {
136 uint32_t SymbolIndex = Rel.getSymbol(false);
137 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
138 if (!ObjSymbol)
139 return ObjSymbol.takeError();
140
141 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
142 if (!GraphSymbol)
143 return make_error<StringError>(
144 formatv("Could not find symbol at given index, did you add it to "
145 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
146 SymbolIndex, (*ObjSymbol)->st_shndx,
147 Base::GraphSymbols.size()),
149
150 uint32_t Type = Rel.getType(false);
152 if (!Kind)
153 return Kind.takeError();
154
155 auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
156 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
157 Edge E(*Kind, Offset, *GraphSymbol, 0);
158
159 Expected<int64_t> Addend =
160 aarch32::readAddend(*Base::G, BlockToFix, E, ArmCfg);
161 if (!Addend)
162 return Addend.takeError();
163
164 E.setAddend(*Addend);
165 LLVM_DEBUG({
166 dbgs() << " ";
167 printEdge(dbgs(), BlockToFix, E, getELFAArch32EdgeKindName(*Kind));
168 dbgs() << "\n";
169 });
170
171 BlockToFix.addEdge(std::move(E));
172 return Error::success();
173 }
174
175 aarch32::ArmConfig ArmCfg;
176
177protected:
178 TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override {
179 if (Sym.getValue() & 0x01)
181 return TargetFlagsType{};
182 }
183
185 TargetFlagsType Flags) override {
187 static constexpr uint64_t ThumbBit = 0x01;
188 return Sym.getValue() & ~ThumbBit;
189 }
190
191public:
195 aarch32::ArmConfig ArmCfg)
196 : ELFLinkGraphBuilder<ELFT>(Obj, std::move(TT), std::move(Features),
197 FileName, getELFAArch32EdgeKindName),
198 ArmCfg(std::move(ArmCfg)) {}
199};
200
201template <aarch32::StubsFlavor Flavor>
203 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
204
206 visitExistingEdges(G, PLT);
207 return Error::success();
208}
209
212 LLVM_DEBUG({
213 dbgs() << "Building jitlink graph for new input "
214 << ObjectBuffer.getBufferIdentifier() << "...\n";
215 });
216
217 auto ELFObj = ObjectFile::createELFObjectFile(ObjectBuffer);
218 if (!ELFObj)
219 return ELFObj.takeError();
220
221 auto Features = (*ELFObj)->getFeatures();
222 if (!Features)
223 return Features.takeError();
224
225 // Find out what exact AArch32 instruction set and features we target.
226 auto TT = (*ELFObj)->makeTriple();
227 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
228 if (AK == ARM::ArchKind::INVALID)
229 return make_error<JITLinkError>(
230 "Failed to build ELF link graph: Invalid ARM ArchKind");
231
232 // Resolve our internal configuration for the target. If at some point the
233 // CPUArch alone becomes too unprecise, we can find more details in the
234 // Tag_CPU_arch_profile.
235 aarch32::ArmConfig ArmCfg;
236 using namespace ARMBuildAttrs;
237 auto Arch = static_cast<CPUArch>(ARM::getArchAttr(AK));
238 switch (Arch) {
239 case v7:
240 case v8_A:
241 ArmCfg = aarch32::getArmConfigForCPUArch(Arch);
243 "Provide a config for each supported CPU");
244 break;
245 default:
246 return make_error<JITLinkError>(
247 "Failed to build ELF link graph: Unsupported CPU arch " +
249 }
250
251 // Populate the link-graph.
252 switch (TT.getArch()) {
253 case Triple::arm:
254 case Triple::thumb: {
255 auto &ELFFile = cast<ELFObjectFile<ELF32LE>>(**ELFObj).getELFFile();
257 (*ELFObj)->getFileName(), ELFFile, TT, Features->getFeatures(),
258 ArmCfg)
259 .buildGraph();
260 }
261 case Triple::armeb:
262 case Triple::thumbeb: {
263 auto &ELFFile = cast<ELFObjectFile<ELF32BE>>(**ELFObj).getELFFile();
265 (*ELFObj)->getFileName(), ELFFile, TT, Features->getFeatures(),
266 ArmCfg)
267 .buildGraph();
268 }
269 default:
270 return make_error<JITLinkError>(
271 "Failed to build ELF/aarch32 link graph: Invalid target triple " +
272 TT.getTriple());
273 }
274}
275
276void link_ELF_aarch32(std::unique_ptr<LinkGraph> G,
277 std::unique_ptr<JITLinkContext> Ctx) {
278 const Triple &TT = G->getTargetTriple();
279
280 using namespace ARMBuildAttrs;
281 ARM::ArchKind AK = ARM::parseArch(TT.getArchName());
282 auto CPU = static_cast<CPUArch>(ARM::getArchAttr(AK));
284
285 PassConfiguration PassCfg;
286 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
287 // Add a mark-live pass.
288 if (auto MarkLive = Ctx->getMarkLivePass(TT))
289 PassCfg.PrePrunePasses.push_back(std::move(MarkLive));
290 else
291 PassCfg.PrePrunePasses.push_back(markAllSymbolsLive);
292
293 switch (ArmCfg.Stubs) {
294 case aarch32::Thumbv7:
295 PassCfg.PostPrunePasses.push_back(
296 buildTables_ELF_aarch32<aarch32::Thumbv7>);
297 break;
299 llvm_unreachable("Check before building graph");
300 }
301 }
302
303 if (auto Err = Ctx->modifyPassConfig(*G, PassCfg))
304 return Ctx->notifyFailed(std::move(Err));
305
306 ELFJITLinker_aarch32::link(std::move(Ctx), std::move(G), std::move(PassCfg),
307 std::move(ArmCfg));
308}
309
310} // namespace jitlink
311} // 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:463
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
@ Flags
Definition: TextStubV5.cpp:93
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:1051
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
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:79
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:1946
Definition: BitVector.h:858