LLVM 19.0.0git
MachO_x86_64.cpp
Go to the documentation of this file.
1//===---- MachO_x86_64.cpp -JIT linker implementation for MachO/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// MachO/x86-64 jit-link implementation.
10//
11//===----------------------------------------------------------------------===//
12
16
18
19#define DEBUG_TYPE "jitlink"
20
21using namespace llvm;
22using namespace llvm::jitlink;
23
24namespace {
25
26class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder {
27public:
28 MachOLinkGraphBuilder_x86_64(const object::MachOObjectFile &Obj,
29 SubtargetFeatures Features)
30 : MachOLinkGraphBuilder(Obj, Triple("x86_64-apple-darwin"),
31 std::move(Features), x86_64::getEdgeKindName) {}
32
33private:
34 enum MachONormalizedRelocationType : unsigned {
35 MachOBranch32,
36 MachOPointer32,
37 MachOPointer64,
38 MachOPointer64Anon,
39 MachOPCRel32,
40 MachOPCRel32Minus1,
41 MachOPCRel32Minus2,
42 MachOPCRel32Minus4,
43 MachOPCRel32Anon,
44 MachOPCRel32Minus1Anon,
45 MachOPCRel32Minus2Anon,
46 MachOPCRel32Minus4Anon,
47 MachOPCRel32GOTLoad,
48 MachOPCRel32GOT,
49 MachOPCRel32TLV,
50 MachOSubtractor32,
51 MachOSubtractor64,
52 };
53
55 getRelocKind(const MachO::relocation_info &RI) {
56 switch (RI.r_type) {
58 if (!RI.r_pcrel) {
59 if (RI.r_length == 3)
60 return RI.r_extern ? MachOPointer64 : MachOPointer64Anon;
61 else if (RI.r_extern && RI.r_length == 2)
62 return MachOPointer32;
63 }
64 break;
66 if (RI.r_pcrel && RI.r_length == 2)
67 return RI.r_extern ? MachOPCRel32 : MachOPCRel32Anon;
68 break;
70 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
71 return MachOBranch32;
72 break;
74 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
75 return MachOPCRel32GOTLoad;
76 break;
78 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
79 return MachOPCRel32GOT;
80 break;
82 if (!RI.r_pcrel && RI.r_extern) {
83 if (RI.r_length == 2)
84 return MachOSubtractor32;
85 else if (RI.r_length == 3)
86 return MachOSubtractor64;
87 }
88 break;
90 if (RI.r_pcrel && RI.r_length == 2)
91 return RI.r_extern ? MachOPCRel32Minus1 : MachOPCRel32Minus1Anon;
92 break;
94 if (RI.r_pcrel && RI.r_length == 2)
95 return RI.r_extern ? MachOPCRel32Minus2 : MachOPCRel32Minus2Anon;
96 break;
98 if (RI.r_pcrel && RI.r_length == 2)
99 return RI.r_extern ? MachOPCRel32Minus4 : MachOPCRel32Minus4Anon;
100 break;
102 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
103 return MachOPCRel32TLV;
104 break;
105 }
106
107 return make_error<JITLinkError>(
108 "Unsupported x86-64 relocation: address=" +
109 formatv("{0:x8}", RI.r_address) +
110 ", symbolnum=" + formatv("{0:x6}", RI.r_symbolnum) +
111 ", kind=" + formatv("{0:x1}", RI.r_type) +
112 ", pc_rel=" + (RI.r_pcrel ? "true" : "false") +
113 ", extern=" + (RI.r_extern ? "true" : "false") +
114 ", length=" + formatv("{0:d}", RI.r_length));
115 }
116
117 using PairRelocInfo = std::tuple<Edge::Kind, Symbol *, uint64_t>;
118
119 // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success,
120 // returns the edge kind and addend to be used.
121 Expected<PairRelocInfo> parsePairRelocation(
122 Block &BlockToFix, MachONormalizedRelocationType SubtractorKind,
123 const MachO::relocation_info &SubRI, orc::ExecutorAddr FixupAddress,
124 const char *FixupContent, object::relocation_iterator &UnsignedRelItr,
126 using namespace support;
127
128 assert(((SubtractorKind == MachOSubtractor32 && SubRI.r_length == 2) ||
129 (SubtractorKind == MachOSubtractor64 && SubRI.r_length == 3)) &&
130 "Subtractor kind should match length");
131 assert(SubRI.r_extern && "SUBTRACTOR reloc symbol should be extern");
132 assert(!SubRI.r_pcrel && "SUBTRACTOR reloc should not be PCRel");
133
134 if (UnsignedRelItr == RelEnd)
135 return make_error<JITLinkError>("x86_64 SUBTRACTOR without paired "
136 "UNSIGNED relocation");
137
138 auto UnsignedRI = getRelocationInfo(UnsignedRelItr);
139
140 if (SubRI.r_address != UnsignedRI.r_address)
141 return make_error<JITLinkError>("x86_64 SUBTRACTOR and paired UNSIGNED "
142 "point to different addresses");
143
144 if (SubRI.r_length != UnsignedRI.r_length)
145 return make_error<JITLinkError>("length of x86_64 SUBTRACTOR and paired "
146 "UNSIGNED reloc must match");
147
148 Symbol *FromSymbol;
149 if (auto FromSymbolOrErr = findSymbolByIndex(SubRI.r_symbolnum))
150 FromSymbol = FromSymbolOrErr->GraphSymbol;
151 else
152 return FromSymbolOrErr.takeError();
153
154 // Read the current fixup value.
155 uint64_t FixupValue = 0;
156 if (SubRI.r_length == 3)
157 FixupValue = *(const little64_t *)FixupContent;
158 else
159 FixupValue = *(const little32_t *)FixupContent;
160
161 // Find 'ToSymbol' using symbol number or address, depending on whether the
162 // paired UNSIGNED relocation is extern.
163 Symbol *ToSymbol = nullptr;
164 if (UnsignedRI.r_extern) {
165 // Find target symbol by symbol index.
166 if (auto ToSymbolOrErr = findSymbolByIndex(UnsignedRI.r_symbolnum))
167 ToSymbol = ToSymbolOrErr->GraphSymbol;
168 else
169 return ToSymbolOrErr.takeError();
170 } else {
171 auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
172 if (!ToSymbolSec)
173 return ToSymbolSec.takeError();
174 ToSymbol = getSymbolByAddress(*ToSymbolSec, ToSymbolSec->Address);
175 assert(ToSymbol && "No symbol for section");
176 FixupValue -= ToSymbol->getAddress().getValue();
177 }
178
179 Edge::Kind DeltaKind;
180 Symbol *TargetSymbol;
181 uint64_t Addend;
182
183 bool FixingFromSymbol = true;
184 if (&BlockToFix == &FromSymbol->getAddressable()) {
185 if (LLVM_UNLIKELY(&BlockToFix == &ToSymbol->getAddressable())) {
186 // From and To are symbols in the same block. Decide direction by offset
187 // instead.
188 if (ToSymbol->getAddress() > FixupAddress)
189 FixingFromSymbol = true;
190 else if (FromSymbol->getAddress() > FixupAddress)
191 FixingFromSymbol = false;
192 else
193 FixingFromSymbol = FromSymbol->getAddress() >= ToSymbol->getAddress();
194 } else
195 FixingFromSymbol = true;
196 } else {
197 if (&BlockToFix == &ToSymbol->getAddressable())
198 FixingFromSymbol = false;
199 else {
200 // BlockToFix was neither FromSymbol nor ToSymbol.
201 return make_error<JITLinkError>("SUBTRACTOR relocation must fix up "
202 "either 'A' or 'B' (or a symbol in one "
203 "of their alt-entry groups)");
204 }
205 }
206
207 if (FixingFromSymbol) {
208 TargetSymbol = ToSymbol;
209 DeltaKind = (SubRI.r_length == 3) ? x86_64::Delta64 : x86_64::Delta32;
210 Addend = FixupValue + (FixupAddress - FromSymbol->getAddress());
211 // FIXME: handle extern 'from'.
212 } else {
213 TargetSymbol = FromSymbol;
214 DeltaKind =
216 Addend = FixupValue - (FixupAddress - ToSymbol->getAddress());
217 }
218
219 return PairRelocInfo(DeltaKind, TargetSymbol, Addend);
220 }
221
222 Error addRelocations() override {
223 using namespace support;
224 auto &Obj = getObject();
225
226 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
227
228 for (const auto &S : Obj.sections()) {
229
230 orc::ExecutorAddr SectionAddress(S.getAddress());
231
232 // Skip relocations virtual sections.
233 if (S.isVirtual()) {
234 if (S.relocation_begin() != S.relocation_end())
235 return make_error<JITLinkError>("Virtual section contains "
236 "relocations");
237 continue;
238 }
239
240 auto NSec =
241 findSectionByIndex(Obj.getSectionIndex(S.getRawDataRefImpl()));
242 if (!NSec)
243 return NSec.takeError();
244
245 // Skip relocations for MachO sections without corresponding graph
246 // sections.
247 {
248 if (!NSec->GraphSection) {
249 LLVM_DEBUG({
250 dbgs() << " Skipping relocations for MachO section "
251 << NSec->SegName << "/" << NSec->SectName
252 << " which has no associated graph section\n";
253 });
254 continue;
255 }
256 }
257
258 // Add relocations for section.
259 for (auto RelItr = S.relocation_begin(), RelEnd = S.relocation_end();
260 RelItr != RelEnd; ++RelItr) {
261
263
264 // Find the address of the value to fix up.
265 auto FixupAddress = SectionAddress + (uint32_t)RI.r_address;
266
267 LLVM_DEBUG({
268 dbgs() << " " << NSec->SectName << " + "
269 << formatv("{0:x8}", RI.r_address) << ":\n";
270 });
271
272 // Find the block that the fixup points to.
273 Block *BlockToFix = nullptr;
274 {
275 auto SymbolToFixOrErr = findSymbolByAddress(*NSec, FixupAddress);
276 if (!SymbolToFixOrErr)
277 return SymbolToFixOrErr.takeError();
278 BlockToFix = &SymbolToFixOrErr->getBlock();
279 }
280
281 if (FixupAddress + orc::ExecutorAddrDiff(1ULL << RI.r_length) >
282 BlockToFix->getAddress() + BlockToFix->getContent().size())
283 return make_error<JITLinkError>(
284 "Relocation extends past end of fixup block");
285
286 // Get a pointer to the fixup content.
287 const char *FixupContent = BlockToFix->getContent().data() +
288 (FixupAddress - BlockToFix->getAddress());
289
290 size_t FixupOffset = FixupAddress - BlockToFix->getAddress();
291
292 // The target symbol and addend will be populated by the switch below.
293 Symbol *TargetSymbol = nullptr;
294 uint64_t Addend = 0;
295
296 // Validate the relocation kind.
297 auto MachORelocKind = getRelocKind(RI);
298 if (!MachORelocKind)
299 return MachORelocKind.takeError();
300
302
303 switch (*MachORelocKind) {
304 case MachOBranch32:
305 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
306 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
307 else
308 return TargetSymbolOrErr.takeError();
309 Addend = *(const little32_t *)FixupContent;
311 break;
312 case MachOPCRel32:
313 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
314 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
315 else
316 return TargetSymbolOrErr.takeError();
317 Addend = *(const little32_t *)FixupContent - 4;
318 Kind = x86_64::Delta32;
319 break;
320 case MachOPCRel32GOTLoad:
321 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
322 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
323 else
324 return TargetSymbolOrErr.takeError();
325 Addend = *(const little32_t *)FixupContent;
327 if (FixupOffset < 3)
328 return make_error<JITLinkError>("GOTLD at invalid offset " +
329 formatv("{0}", FixupOffset));
330 break;
331 case MachOPCRel32GOT:
332 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
333 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
334 else
335 return TargetSymbolOrErr.takeError();
336 Addend = *(const little32_t *)FixupContent - 4;
338 break;
339 case MachOPCRel32TLV:
340 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
341 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
342 else
343 return TargetSymbolOrErr.takeError();
344 Addend = *(const little32_t *)FixupContent;
346 if (FixupOffset < 3)
347 return make_error<JITLinkError>("TLV at invalid offset " +
348 formatv("{0}", FixupOffset));
349 break;
350 case MachOPointer32:
351 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
352 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
353 else
354 return TargetSymbolOrErr.takeError();
355 Addend = *(const ulittle32_t *)FixupContent;
356 Kind = x86_64::Pointer32;
357 break;
358 case MachOPointer64:
359 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
360 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
361 else
362 return TargetSymbolOrErr.takeError();
363 Addend = *(const ulittle64_t *)FixupContent;
364 Kind = x86_64::Pointer64;
365 break;
366 case MachOPointer64Anon: {
367 orc::ExecutorAddr TargetAddress(*(const ulittle64_t *)FixupContent);
368 auto TargetNSec = findSectionByIndex(RI.r_symbolnum - 1);
369 if (!TargetNSec)
370 return TargetNSec.takeError();
371 if (auto TargetSymbolOrErr =
372 findSymbolByAddress(*TargetNSec, TargetAddress))
373 TargetSymbol = &*TargetSymbolOrErr;
374 else
375 return TargetSymbolOrErr.takeError();
376 Addend = TargetAddress - TargetSymbol->getAddress();
377 Kind = x86_64::Pointer64;
378 break;
379 }
380 case MachOPCRel32Minus1:
381 case MachOPCRel32Minus2:
382 case MachOPCRel32Minus4:
383 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
384 TargetSymbol = TargetSymbolOrErr->GraphSymbol;
385 else
386 return TargetSymbolOrErr.takeError();
387 Addend = *(const little32_t *)FixupContent - 4;
388 Kind = x86_64::Delta32;
389 break;
390 case MachOPCRel32Anon: {
391 orc::ExecutorAddr TargetAddress(FixupAddress + 4 +
392 *(const little32_t *)FixupContent);
393 auto TargetNSec = findSectionByIndex(RI.r_symbolnum - 1);
394 if (!TargetNSec)
395 return TargetNSec.takeError();
396 if (auto TargetSymbolOrErr =
397 findSymbolByAddress(*TargetNSec, TargetAddress))
398 TargetSymbol = &*TargetSymbolOrErr;
399 else
400 return TargetSymbolOrErr.takeError();
401 Addend = TargetAddress - TargetSymbol->getAddress() - 4;
402 Kind = x86_64::Delta32;
403 break;
404 }
405 case MachOPCRel32Minus1Anon:
406 case MachOPCRel32Minus2Anon:
407 case MachOPCRel32Minus4Anon: {
410 1ULL << (*MachORelocKind - MachOPCRel32Minus1Anon));
411 orc::ExecutorAddr TargetAddress =
412 FixupAddress + Delta + *(const little32_t *)FixupContent;
413 auto TargetNSec = findSectionByIndex(RI.r_symbolnum - 1);
414 if (!TargetNSec)
415 return TargetNSec.takeError();
416 if (auto TargetSymbolOrErr =
417 findSymbolByAddress(*TargetNSec, TargetAddress))
418 TargetSymbol = &*TargetSymbolOrErr;
419 else
420 return TargetSymbolOrErr.takeError();
421 Addend = TargetAddress - TargetSymbol->getAddress() - Delta;
422 Kind = x86_64::Delta32;
423 break;
424 }
425 case MachOSubtractor32:
426 case MachOSubtractor64: {
427 // We use Delta32/Delta64 to represent SUBTRACTOR relocations.
428 // parsePairRelocation handles the paired reloc, and returns the
429 // edge kind to be used (either Delta32/Delta64, or
430 // NegDelta32/NegDelta64, depending on the direction of the
431 // subtraction) along with the addend.
432 auto PairInfo =
433 parsePairRelocation(*BlockToFix, *MachORelocKind, RI,
434 FixupAddress, FixupContent, ++RelItr, RelEnd);
435 if (!PairInfo)
436 return PairInfo.takeError();
437 std::tie(Kind, TargetSymbol, Addend) = *PairInfo;
438 assert(TargetSymbol && "No target symbol from parsePairRelocation?");
439 break;
440 }
441 }
442
443 LLVM_DEBUG({
444 dbgs() << " ";
445 Edge GE(Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol,
446 Addend);
447 printEdge(dbgs(), *BlockToFix, GE, x86_64::getEdgeKindName(Kind));
448 dbgs() << "\n";
449 });
450 BlockToFix->addEdge(Kind, FixupAddress - BlockToFix->getAddress(),
451 *TargetSymbol, Addend);
452 }
453 }
454 return Error::success();
455 }
456};
457
458Error buildGOTAndStubs_MachO_x86_64(LinkGraph &G) {
461 visitExistingEdges(G, GOT, PLT);
462 return Error::success();
463}
464
465} // namespace
466
467namespace llvm {
468namespace jitlink {
469
470class MachOJITLinker_x86_64 : public JITLinker<MachOJITLinker_x86_64> {
471 friend class JITLinker<MachOJITLinker_x86_64>;
472
473public:
474 MachOJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
475 std::unique_ptr<LinkGraph> G,
476 PassConfiguration PassConfig)
477 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
478
479private:
480 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
481 return x86_64::applyFixup(G, B, E, nullptr);
482 }
483};
484
487 auto MachOObj = object::ObjectFile::createMachOObjectFile(ObjectBuffer);
488 if (!MachOObj)
489 return MachOObj.takeError();
490
491 auto Features = (*MachOObj)->getFeatures();
492 if (!Features)
493 return Features.takeError();
494
495 return MachOLinkGraphBuilder_x86_64(**MachOObj, std::move(*Features))
496 .buildGraph();
497}
498
499void link_MachO_x86_64(std::unique_ptr<LinkGraph> G,
500 std::unique_ptr<JITLinkContext> Ctx) {
501
503
504 if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
505 // Add eh-frame passes.
506 Config.PrePrunePasses.push_back(createEHFrameSplitterPass_MachO_x86_64());
507 Config.PrePrunePasses.push_back(createEHFrameEdgeFixerPass_MachO_x86_64());
508
509 // Add compact unwind splitter pass.
510 Config.PrePrunePasses.push_back(
511 CompactUnwindSplitter("__LD,__compact_unwind"));
512
513 // Add a mark-live pass.
514 if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
515 Config.PrePrunePasses.push_back(std::move(MarkLive));
516 else
517 Config.PrePrunePasses.push_back(markAllSymbolsLive);
518
519 // Add an in-place GOT/Stubs pass.
520 Config.PostPrunePasses.push_back(buildGOTAndStubs_MachO_x86_64);
521
522 // Add GOT/Stubs optimizer pass.
523 Config.PreFixupPasses.push_back(x86_64::optimizeGOTAndStubAccesses);
524 }
525
526 if (auto Err = Ctx->modifyPassConfig(*G, Config))
527 return Ctx->notifyFailed(std::move(Err));
528
529 // Construct a JITLinker and run the link function.
530 MachOJITLinker_x86_64::link(std::move(Ctx), std::move(G), std::move(Config));
531}
532
534 return DWARFRecordSectionSplitter("__TEXT,__eh_frame");
535}
536
538 return EHFrameEdgeFixer("__TEXT,__eh_frame", x86_64::PointerSize,
541}
542
543} // end namespace jitlink
544} // 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_UNLIKELY(EXPR)
Definition: Compiler.h:241
#define LLVM_DEBUG(X)
Definition: Debug.h:101
RelaxConfig Config
Definition: ELF_riscv.cpp:506
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
const T * data() const
Definition: ArrayRef.h:162
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
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
uint64_t getSectionIndex(DataRefImpl Sec) const override
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Create a MachOObjectFile instance from a given buffer.
section_iterator_range sections() const
Definition: ObjectFile.h:328
Represents an address in the executor process.
uint64_t getValue() const
unique_function is a type-erasing functor similar to std::function.
@ X86_64_RELOC_UNSIGNED
Definition: MachO.h:483
@ X86_64_RELOC_SIGNED
Definition: MachO.h:484
@ X86_64_RELOC_GOT
Definition: MachO.h:487
@ X86_64_RELOC_GOT_LOAD
Definition: MachO.h:486
@ X86_64_RELOC_BRANCH
Definition: MachO.h:485
@ X86_64_RELOC_SIGNED_2
Definition: MachO.h:490
@ X86_64_RELOC_TLV
Definition: MachO.h:492
@ X86_64_RELOC_SIGNED_4
Definition: MachO.h:491
@ X86_64_RELOC_SUBTRACTOR
Definition: MachO.h:488
@ X86_64_RELOC_SIGNED_1
Definition: MachO.h:489
uint64_t ExecutorAddrDiff
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< 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:1858
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858