LLVM 17.0.0git
ELF_x86_64.cpp
Go to the documentation of this file.
1//===---- ELF_x86_64.cpp -JIT linker implementation for ELF/x86-64 ----===//
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/x86-64 jit-link implementation.
10//
11//===----------------------------------------------------------------------===//
12
19#include "llvm/Support/Endian.h"
20
22#include "EHFrameSupportImpl.h"
23#include "ELFLinkGraphBuilder.h"
24#include "JITLinkGeneric.h"
25
26#define DEBUG_TYPE "jitlink"
27
28using namespace llvm;
29using namespace llvm::jitlink;
30
31namespace {
32
33constexpr StringRef ELFGOTSymbolName = "_GLOBAL_OFFSET_TABLE_";
34constexpr StringRef ELFTLSInfoSectionName = "$__TLSINFO";
35
36class TLSInfoTableManager_ELF_x86_64
37 : public TableManager<TLSInfoTableManager_ELF_x86_64> {
38public:
39 static const uint8_t TLSInfoEntryContent[16];
40
41 static StringRef getSectionName() { return ELFTLSInfoSectionName; }
42
43 bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
46 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
47 << formatv("{0:x}", B->getFixupAddress(E)) << " ("
48 << formatv("{0:x}", B->getAddress()) << " + "
49 << formatv("{0:x}", E.getOffset()) << ")\n";
50 });
51 E.setKind(x86_64::Delta32);
52 E.setTarget(getEntryForTarget(G, E.getTarget()));
53 return true;
54 }
55 return false;
56 }
57
58 Symbol &createEntry(LinkGraph &G, Symbol &Target) {
59 // the TLS Info entry's key value will be written by the fixTLVSectionByName
60 // pass, so create mutable content.
61 auto &TLSInfoEntry = G.createMutableContentBlock(
62 getTLSInfoSection(G), G.allocateContent(getTLSInfoEntryContent()),
63 orc::ExecutorAddr(), 8, 0);
64 TLSInfoEntry.addEdge(x86_64::Pointer64, 8, Target, 0);
65 return G.addAnonymousSymbol(TLSInfoEntry, 0, 16, false, false);
66 }
67
68private:
69 Section &getTLSInfoSection(LinkGraph &G) {
70 if (!TLSInfoTable)
71 TLSInfoTable =
72 &G.createSection(ELFTLSInfoSectionName, orc::MemProt::Read);
73 return *TLSInfoTable;
74 }
75
76 ArrayRef<char> getTLSInfoEntryContent() const {
77 return {reinterpret_cast<const char *>(TLSInfoEntryContent),
78 sizeof(TLSInfoEntryContent)};
79 }
80
81 Section *TLSInfoTable = nullptr;
82};
83
84const uint8_t TLSInfoTableManager_ELF_x86_64::TLSInfoEntryContent[16] = {
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*pthread key */
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*data address*/
87};
88
89Error buildTables_ELF_x86_64(LinkGraph &G) {
90 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
91
94 TLSInfoTableManager_ELF_x86_64 TLSInfo;
95 visitExistingEdges(G, GOT, PLT, TLSInfo);
96 return Error::success();
97}
98} // namespace
99
100namespace llvm {
101namespace jitlink {
102
103// This should become a template as the ELFFile is so a lot of this could become
104// generic
105class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
106private:
107 using ELFT = object::ELF64LE;
108
109 enum ELFX86RelocationKind : Edge::Kind {
110 Branch32 = Edge::FirstRelocation,
111 Pointer32,
112 Pointer32Signed,
113 Pointer64,
114 PCRel32,
115 PCRel32GOTLoad,
116 PCRel32GOTLoadRelaxable,
117 PCRel32REXGOTLoadRelaxable,
118 PCRel32TLV,
119 PCRel64GOT,
120 GOTOFF64,
121 GOT64,
122 Delta64,
123 };
124
125 static Expected<ELFX86RelocationKind> getRelocationKind(const uint32_t Type) {
126 switch (Type) {
127 case ELF::R_X86_64_32:
128 return ELFX86RelocationKind::Pointer32;
129 case ELF::R_X86_64_32S:
130 return ELFX86RelocationKind::Pointer32Signed;
131 case ELF::R_X86_64_PC32:
132 return ELFX86RelocationKind::PCRel32;
133 case ELF::R_X86_64_PC64:
134 case ELF::R_X86_64_GOTPC64:
135 return ELFX86RelocationKind::Delta64;
136 case ELF::R_X86_64_64:
137 return ELFX86RelocationKind::Pointer64;
138 case ELF::R_X86_64_GOTPCREL:
139 return ELFX86RelocationKind::PCRel32GOTLoad;
140 case ELF::R_X86_64_GOTPCRELX:
141 return ELFX86RelocationKind::PCRel32GOTLoadRelaxable;
142 case ELF::R_X86_64_REX_GOTPCRELX:
143 return ELFX86RelocationKind::PCRel32REXGOTLoadRelaxable;
144 case ELF::R_X86_64_GOTPCREL64:
145 return ELFX86RelocationKind::PCRel64GOT;
146 case ELF::R_X86_64_GOT64:
147 return ELFX86RelocationKind::GOT64;
148 case ELF::R_X86_64_GOTOFF64:
149 return ELFX86RelocationKind::GOTOFF64;
150 case ELF::R_X86_64_PLT32:
151 return ELFX86RelocationKind::Branch32;
152 case ELF::R_X86_64_TLSGD:
153 return ELFX86RelocationKind::PCRel32TLV;
154 }
155 return make_error<JITLinkError>(
156 "Unsupported x86-64 relocation type " + formatv("{0:d}: ", Type) +
158 }
159
160 Error addRelocations() override {
161 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
162
164 using Self = ELFLinkGraphBuilder_x86_64;
165 for (const auto &RelSect : Base::Sections) {
166 // Validate the section to read relocation entries from.
167 if (RelSect.sh_type == ELF::SHT_REL)
168 return make_error<StringError>(
169 "No SHT_REL in valid x64 ELF object files",
171
172 if (Error Err = Base::forEachRelaRelocation(RelSect, this,
173 &Self::addSingleRelocation))
174 return Err;
175 }
176
177 return Error::success();
178 }
179
180 Error addSingleRelocation(const typename ELFT::Rela &Rel,
181 const typename ELFT::Shdr &FixupSection,
182 Block &BlockToFix) {
184
185 uint32_t SymbolIndex = Rel.getSymbol(false);
186 auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
187 if (!ObjSymbol)
188 return ObjSymbol.takeError();
189
190 Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
191 if (!GraphSymbol)
192 return make_error<StringError>(
193 formatv("Could not find symbol at given index, did you add it to "
194 "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
195 SymbolIndex, (*ObjSymbol)->st_shndx,
196 Base::GraphSymbols.size()),
198
199 // Validate the relocation kind.
200 auto ELFRelocKind = getRelocationKind(Rel.getType(false));
201 if (!ELFRelocKind)
202 return ELFRelocKind.takeError();
203
204 int64_t Addend = Rel.r_addend;
206 switch (*ELFRelocKind) {
207 case PCRel32:
208 Kind = x86_64::Delta32;
209 break;
210 case Delta64:
211 Kind = x86_64::Delta64;
212 break;
213 case Pointer32:
214 Kind = x86_64::Pointer32;
215 break;
216 case Pointer32Signed:
218 break;
219 case Pointer64:
220 Kind = x86_64::Pointer64;
221 break;
222 case PCRel32GOTLoad: {
224 break;
225 }
226 case PCRel32REXGOTLoadRelaxable: {
228 Addend = 0;
229 break;
230 }
231 case PCRel32TLV: {
233 break;
234 }
235 case PCRel32GOTLoadRelaxable: {
237 Addend = 0;
238 break;
239 }
240 case PCRel64GOT: {
242 break;
243 }
244 case GOT64: {
246 break;
247 }
248 case GOTOFF64: {
250 break;
251 }
252 case Branch32: {
254 // BranchPCRel32 implicitly handles the '-4' PC adjustment, so we have to
255 // adjust the addend by '+4' to compensate.
256 Addend += 4;
257 break;
258 }
259 }
260
261 auto FixupAddress = orc::ExecutorAddr(FixupSection.sh_addr) + Rel.r_offset;
262 Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
263 Edge GE(Kind, Offset, *GraphSymbol, Addend);
264 LLVM_DEBUG({
265 dbgs() << " ";
266 printEdge(dbgs(), BlockToFix, GE, x86_64::getEdgeKindName(Kind));
267 dbgs() << "\n";
268 });
269
270 BlockToFix.addEdge(std::move(GE));
271 return Error::success();
272 }
273
274public:
277 : ELFLinkGraphBuilder(Obj, Triple("x86_64-unknown-linux"), FileName,
278 x86_64::getEdgeKindName) {}
279};
280
281class ELFJITLinker_x86_64 : public JITLinker<ELFJITLinker_x86_64> {
282 friend class JITLinker<ELFJITLinker_x86_64>;
283
284public:
285 ELFJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
286 std::unique_ptr<LinkGraph> G,
287 PassConfiguration PassConfig)
288 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {
290 [this](LinkGraph &G) { return getOrCreateGOTSymbol(G); });
291 }
292
293private:
294 Symbol *GOTSymbol = nullptr;
295
296 Error getOrCreateGOTSymbol(LinkGraph &G) {
297 auto DefineExternalGOTSymbolIfPresent =
299 [&](LinkGraph &LG, Symbol &Sym) -> SectionRangeSymbolDesc {
300 if (Sym.getName() == ELFGOTSymbolName)
301 if (auto *GOTSection = G.findSectionByName(
303 GOTSymbol = &Sym;
304 return {*GOTSection, true};
305 }
306 return {};
307 });
308
309 // Try to attach _GLOBAL_OFFSET_TABLE_ to the GOT if it's defined as an
310 // external.
311 if (auto Err = DefineExternalGOTSymbolIfPresent(G))
312 return Err;
313
314 // If we succeeded then we're done.
315 if (GOTSymbol)
316 return Error::success();
317
318 // Otherwise look for a GOT section: If it already has a start symbol we'll
319 // record it, otherwise we'll create our own.
320 // If there's a GOT section but we didn't find an external GOT symbol...
321 if (auto *GOTSection =
322 G.findSectionByName(x86_64::GOTTableManager::getSectionName())) {
323
324 // Check for an existing defined symbol.
325 for (auto *Sym : GOTSection->symbols())
326 if (Sym->getName() == ELFGOTSymbolName) {
327 GOTSymbol = Sym;
328 return Error::success();
329 }
330
331 // If there's no defined symbol then create one.
332 SectionRange SR(*GOTSection);
333 if (SR.empty())
334 GOTSymbol =
335 &G.addAbsoluteSymbol(ELFGOTSymbolName, orc::ExecutorAddr(), 0,
337 else
338 GOTSymbol =
339 &G.addDefinedSymbol(*SR.getFirstBlock(), 0, ELFGOTSymbolName, 0,
340 Linkage::Strong, Scope::Local, false, true);
341 }
342
343 return Error::success();
344 }
345
346 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
347 return x86_64::applyFixup(G, B, E, GOTSymbol);
348 }
349};
350
353 LLVM_DEBUG({
354 dbgs() << "Building jitlink graph for new input "
355 << ObjectBuffer.getBufferIdentifier() << "...\n";
356 });
357
358 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
359 if (!ELFObj)
360 return ELFObj.takeError();
361
362 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
363 return ELFLinkGraphBuilder_x86_64((*ELFObj)->getFileName(),
364 ELFObjFile.getELFFile())
365 .buildGraph();
366}
367
370 constexpr StringRef StartSymbolPrefix = "__start";
371 constexpr StringRef EndSymbolPrefix = "__end";
372
373 auto SymName = Sym.getName();
374 if (SymName.startswith(StartSymbolPrefix)) {
375 if (auto *Sec =
376 G.findSectionByName(SymName.drop_front(StartSymbolPrefix.size())))
377 return {*Sec, true};
378 } else if (SymName.startswith(EndSymbolPrefix)) {
379 if (auto *Sec =
380 G.findSectionByName(SymName.drop_front(EndSymbolPrefix.size())))
381 return {*Sec, false};
382 }
383 return {};
384}
385
386void link_ELF_x86_64(std::unique_ptr<LinkGraph> G,
387 std::unique_ptr<JITLinkContext> Ctx) {
388 PassConfiguration Config;
389
390 if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
391
392 Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame"));
393 Config.PrePrunePasses.push_back(EHFrameEdgeFixer(
396 Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame"));
397
398 // Construct a JITLinker and run the link function.
399 // Add a mark-live pass.
400 if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
401 Config.PrePrunePasses.push_back(std::move(MarkLive));
402 else
403 Config.PrePrunePasses.push_back(markAllSymbolsLive);
404
405 // Add an in-place GOT/Stubs/TLSInfoEntry build pass.
406 Config.PostPrunePasses.push_back(buildTables_ELF_x86_64);
407
408 // Resolve any external section start / end symbols.
409 Config.PostAllocationPasses.push_back(
412
413 // Add GOT/Stubs optimizer pass.
415 }
416
417 if (auto Err = Ctx->modifyPassConfig(*G, Config))
418 return Ctx->notifyFailed(std::move(Err));
419
420 ELFJITLinker_x86_64::link(std::move(Ctx), std::move(G), std::move(Config));
421}
422} // end namespace jitlink
423} // end 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
#define G(x, y, z)
Definition: MD5.cpp:56
if(VerifyEach)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
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
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
Target - Wrapper for Target specific information.
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
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Represents an address in the executor process.
@ EM_X86_64
Definition: ELF.h:178
@ SHT_REL
Definition: ELF.h:1003
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
ELFType< support::little, true > ELF64LE
Definition: ELFTypes.h:97
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
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