1 //===---- MachO_arm64.cpp - JIT linker implementation for MachO/arm64 -----===//
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
7 //===----------------------------------------------------------------------===//
9 // MachO/arm64 jit-link implementation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ExecutionEngine/JITLink/MachO_arm64.h"
14 #include "llvm/ExecutionEngine/JITLink/DWARFRecordSectionSplitter.h"
15 #include "llvm/ExecutionEngine/JITLink/aarch64.h"
17 #include "MachOLinkGraphBuilder.h"
19 #define DEBUG_TYPE "jitlink"
22 using namespace llvm::jitlink
;
26 class MachOLinkGraphBuilder_arm64
: public MachOLinkGraphBuilder
{
28 MachOLinkGraphBuilder_arm64(const object::MachOObjectFile
&Obj
,
29 SubtargetFeatures Features
)
30 : MachOLinkGraphBuilder(Obj
, Triple("arm64-apple-darwin"),
31 std::move(Features
), aarch64::getEdgeKindName
),
32 NumSymbols(Obj
.getSymtabLoadCommand().nsyms
) {}
35 enum MachOARM64RelocationKind
: Edge::Kind
{
36 MachOBranch26
= Edge::FirstRelocation
,
55 static Expected
<MachOARM64RelocationKind
>
56 getRelocationKind(const MachO::relocation_info
&RI
) {
58 case MachO::ARM64_RELOC_UNSIGNED
:
61 return RI
.r_extern
? MachOPointer64
: MachOPointer64Anon
;
62 else if (RI
.r_length
== 2)
63 return MachOPointer32
;
66 case MachO::ARM64_RELOC_SUBTRACTOR
:
67 // SUBTRACTOR must be non-pc-rel, extern, with length 2 or 3.
68 // Initially represent SUBTRACTOR relocations with 'Delta<W>'.
69 // They may be turned into NegDelta<W> by parsePairRelocation.
70 if (!RI
.r_pcrel
&& RI
.r_extern
) {
73 else if (RI
.r_length
== 3)
77 case MachO::ARM64_RELOC_BRANCH26
:
78 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
81 case MachO::ARM64_RELOC_PAGE21
:
82 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
85 case MachO::ARM64_RELOC_PAGEOFF12
:
86 if (!RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
87 return MachOPageOffset12
;
89 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21
:
90 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
91 return MachOGOTPage21
;
93 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
94 if (!RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
95 return MachOGOTPageOffset12
;
97 case MachO::ARM64_RELOC_POINTER_TO_GOT
:
98 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
99 return MachOPointerToGOT
;
101 case MachO::ARM64_RELOC_ADDEND
:
102 if (!RI
.r_pcrel
&& !RI
.r_extern
&& RI
.r_length
== 2)
103 return MachOPairedAddend
;
105 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21
:
106 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
107 return MachOTLVPage21
;
109 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
110 if (!RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
111 return MachOTLVPageOffset12
;
115 return make_error
<JITLinkError
>(
116 "Unsupported arm64 relocation: address=" +
117 formatv("{0:x8}", RI
.r_address
) +
118 ", symbolnum=" + formatv("{0:x6}", RI
.r_symbolnum
) +
119 ", kind=" + formatv("{0:x1}", RI
.r_type
) +
120 ", pc_rel=" + (RI
.r_pcrel
? "true" : "false") +
121 ", extern=" + (RI
.r_extern
? "true" : "false") +
122 ", length=" + formatv("{0:d}", RI
.r_length
));
125 using PairRelocInfo
= std::tuple
<Edge::Kind
, Symbol
*, uint64_t>;
127 // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success,
128 // returns the edge kind and addend to be used.
129 Expected
<PairRelocInfo
>
130 parsePairRelocation(Block
&BlockToFix
, Edge::Kind SubtractorKind
,
131 const MachO::relocation_info
&SubRI
,
132 orc::ExecutorAddr FixupAddress
, const char *FixupContent
,
133 object::relocation_iterator
&UnsignedRelItr
,
134 object::relocation_iterator
&RelEnd
) {
135 using namespace support
;
137 assert(((SubtractorKind
== MachODelta32
&& SubRI
.r_length
== 2) ||
138 (SubtractorKind
== MachODelta64
&& SubRI
.r_length
== 3)) &&
139 "Subtractor kind should match length");
140 assert(SubRI
.r_extern
&& "SUBTRACTOR reloc symbol should be extern");
141 assert(!SubRI
.r_pcrel
&& "SUBTRACTOR reloc should not be PCRel");
143 if (UnsignedRelItr
== RelEnd
)
144 return make_error
<JITLinkError
>("arm64 SUBTRACTOR without paired "
145 "UNSIGNED relocation");
147 auto UnsignedRI
= getRelocationInfo(UnsignedRelItr
);
149 if (SubRI
.r_address
!= UnsignedRI
.r_address
)
150 return make_error
<JITLinkError
>("arm64 SUBTRACTOR and paired UNSIGNED "
151 "point to different addresses");
153 if (SubRI
.r_length
!= UnsignedRI
.r_length
)
154 return make_error
<JITLinkError
>("length of arm64 SUBTRACTOR and paired "
155 "UNSIGNED reloc must match");
158 if (auto FromSymbolOrErr
= findSymbolByIndex(SubRI
.r_symbolnum
))
159 FromSymbol
= FromSymbolOrErr
->GraphSymbol
;
161 return FromSymbolOrErr
.takeError();
163 // Read the current fixup value.
164 uint64_t FixupValue
= 0;
165 if (SubRI
.r_length
== 3)
166 FixupValue
= *(const little64_t
*)FixupContent
;
168 FixupValue
= *(const little32_t
*)FixupContent
;
170 // Find 'ToSymbol' using symbol number or address, depending on whether the
171 // paired UNSIGNED relocation is extern.
172 Symbol
*ToSymbol
= nullptr;
173 if (UnsignedRI
.r_extern
) {
174 // Find target symbol by symbol index.
175 if (auto ToSymbolOrErr
= findSymbolByIndex(UnsignedRI
.r_symbolnum
))
176 ToSymbol
= ToSymbolOrErr
->GraphSymbol
;
178 return ToSymbolOrErr
.takeError();
180 auto ToSymbolSec
= findSectionByIndex(UnsignedRI
.r_symbolnum
- 1);
182 return ToSymbolSec
.takeError();
183 ToSymbol
= getSymbolByAddress(*ToSymbolSec
, ToSymbolSec
->Address
);
184 assert(ToSymbol
&& "No symbol for section");
185 FixupValue
-= ToSymbol
->getAddress().getValue();
188 Edge::Kind DeltaKind
;
189 Symbol
*TargetSymbol
;
192 bool FixingFromSymbol
= true;
193 if (&BlockToFix
== &FromSymbol
->getAddressable()) {
194 if (LLVM_UNLIKELY(&BlockToFix
== &ToSymbol
->getAddressable())) {
195 // From and To are symbols in the same block. Decide direction by offset
197 if (ToSymbol
->getAddress() > FixupAddress
)
198 FixingFromSymbol
= true;
199 else if (FromSymbol
->getAddress() > FixupAddress
)
200 FixingFromSymbol
= false;
202 FixingFromSymbol
= FromSymbol
->getAddress() >= ToSymbol
->getAddress();
204 FixingFromSymbol
= true;
206 if (&BlockToFix
== &ToSymbol
->getAddressable())
207 FixingFromSymbol
= false;
209 // BlockToFix was neither FromSymbol nor ToSymbol.
210 return make_error
<JITLinkError
>("SUBTRACTOR relocation must fix up "
211 "either 'A' or 'B' (or a symbol in one "
212 "of their alt-entry groups)");
216 if (FixingFromSymbol
) {
217 TargetSymbol
= ToSymbol
;
218 DeltaKind
= (SubRI
.r_length
== 3) ? aarch64::Delta64
: aarch64::Delta32
;
219 Addend
= FixupValue
+ (FixupAddress
- FromSymbol
->getAddress());
220 // FIXME: handle extern 'from'.
222 TargetSymbol
= &*FromSymbol
;
224 (SubRI
.r_length
== 3) ? aarch64::NegDelta64
: aarch64::NegDelta32
;
225 Addend
= FixupValue
- (FixupAddress
- ToSymbol
->getAddress());
228 return PairRelocInfo(DeltaKind
, TargetSymbol
, Addend
);
231 Error
addRelocations() override
{
232 using namespace support
;
233 auto &Obj
= getObject();
235 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
237 for (auto &S
: Obj
.sections()) {
239 orc::ExecutorAddr
SectionAddress(S
.getAddress());
241 // Skip relocations virtual sections.
243 if (S
.relocation_begin() != S
.relocation_end())
244 return make_error
<JITLinkError
>("Virtual section contains "
250 findSectionByIndex(Obj
.getSectionIndex(S
.getRawDataRefImpl()));
252 return NSec
.takeError();
254 // Skip relocations for MachO sections without corresponding graph
257 if (!NSec
->GraphSection
) {
259 dbgs() << " Skipping relocations for MachO section "
260 << NSec
->SegName
<< "/" << NSec
->SectName
261 << " which has no associated graph section\n";
267 for (auto RelItr
= S
.relocation_begin(), RelEnd
= S
.relocation_end();
268 RelItr
!= RelEnd
; ++RelItr
) {
270 MachO::relocation_info RI
= getRelocationInfo(RelItr
);
272 // Validate the relocation kind.
273 auto MachORelocKind
= getRelocationKind(RI
);
275 return MachORelocKind
.takeError();
277 // Find the address of the value to fix up.
278 orc::ExecutorAddr FixupAddress
=
279 SectionAddress
+ (uint32_t)RI
.r_address
;
281 dbgs() << " " << NSec
->SectName
<< " + "
282 << formatv("{0:x8}", RI
.r_address
) << ":\n";
285 // Find the block that the fixup points to.
286 Block
*BlockToFix
= nullptr;
288 auto SymbolToFixOrErr
= findSymbolByAddress(*NSec
, FixupAddress
);
289 if (!SymbolToFixOrErr
)
290 return SymbolToFixOrErr
.takeError();
291 BlockToFix
= &SymbolToFixOrErr
->getBlock();
294 if (FixupAddress
+ orc::ExecutorAddrDiff(1ULL << RI
.r_length
) >
295 BlockToFix
->getAddress() + BlockToFix
->getContent().size())
296 return make_error
<JITLinkError
>(
297 "Relocation content extends past end of fixup block");
299 Edge::Kind Kind
= Edge::Invalid
;
301 // Get a pointer to the fixup content.
302 const char *FixupContent
= BlockToFix
->getContent().data() +
303 (FixupAddress
- BlockToFix
->getAddress());
305 // The target symbol and addend will be populated by the switch below.
306 Symbol
*TargetSymbol
= nullptr;
309 if (*MachORelocKind
== MachOPairedAddend
) {
310 // If this is an Addend relocation then process it and move to the
313 Addend
= SignExtend64(RI
.r_symbolnum
, 24);
315 if (RelItr
== RelEnd
)
316 return make_error
<JITLinkError
>("Unpaired Addend reloc at " +
317 formatv("{0:x16}", FixupAddress
));
319 RI
= getRelocationInfo(RelItr
);
321 MachORelocKind
= getRelocationKind(RI
);
323 return MachORelocKind
.takeError();
325 if (*MachORelocKind
!= MachOBranch26
&&
326 *MachORelocKind
!= MachOPage21
&&
327 *MachORelocKind
!= MachOPageOffset12
)
328 return make_error
<JITLinkError
>(
329 "Invalid relocation pair: Addend + " +
330 StringRef(getMachOARM64RelocationKindName(*MachORelocKind
)));
333 dbgs() << " Addend: value = " << formatv("{0:x6}", Addend
)
335 << getMachOARM64RelocationKindName(*MachORelocKind
) << "\n";
338 // Find the address of the value to fix up.
339 orc::ExecutorAddr PairedFixupAddress
=
340 SectionAddress
+ (uint32_t)RI
.r_address
;
341 if (PairedFixupAddress
!= FixupAddress
)
342 return make_error
<JITLinkError
>("Paired relocation points at "
346 switch (*MachORelocKind
) {
347 case MachOBranch26
: {
348 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
349 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
351 return TargetSymbolOrErr
.takeError();
352 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
353 if ((Instr
& 0x7fffffff) != 0x14000000)
354 return make_error
<JITLinkError
>("BRANCH26 target is not a B or BL "
355 "instruction with a zero addend");
356 Kind
= aarch64::Branch26PCRel
;
360 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
361 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
363 return TargetSymbolOrErr
.takeError();
364 Addend
= *(const ulittle32_t
*)FixupContent
;
365 Kind
= aarch64::Pointer32
;
368 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
369 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
371 return TargetSymbolOrErr
.takeError();
372 Addend
= *(const ulittle64_t
*)FixupContent
;
373 Kind
= aarch64::Pointer64
;
375 case MachOPointer64Anon
: {
376 orc::ExecutorAddr
TargetAddress(*(const ulittle64_t
*)FixupContent
);
377 auto TargetNSec
= findSectionByIndex(RI
.r_symbolnum
- 1);
379 return TargetNSec
.takeError();
380 if (auto TargetSymbolOrErr
=
381 findSymbolByAddress(*TargetNSec
, TargetAddress
))
382 TargetSymbol
= &*TargetSymbolOrErr
;
384 return TargetSymbolOrErr
.takeError();
385 Addend
= TargetAddress
- TargetSymbol
->getAddress();
386 Kind
= aarch64::Pointer64
;
391 case MachOTLVPage21
: {
392 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
393 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
395 return TargetSymbolOrErr
.takeError();
396 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
397 if ((Instr
& 0xffffffe0) != 0x90000000)
398 return make_error
<JITLinkError
>("PAGE21/GOTPAGE21 target is not an "
399 "ADRP instruction with a zero "
402 if (*MachORelocKind
== MachOPage21
) {
403 Kind
= aarch64::Page21
;
404 } else if (*MachORelocKind
== MachOGOTPage21
) {
405 Kind
= aarch64::RequestGOTAndTransformToPage21
;
406 } else if (*MachORelocKind
== MachOTLVPage21
) {
407 Kind
= aarch64::RequestTLVPAndTransformToPage21
;
411 case MachOPageOffset12
: {
412 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
413 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
415 return TargetSymbolOrErr
.takeError();
416 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
417 uint32_t EncodedAddend
= (Instr
& 0x003FFC00) >> 10;
418 if (EncodedAddend
!= 0)
419 return make_error
<JITLinkError
>("GOTPAGEOFF12 target has non-zero "
421 Kind
= aarch64::PageOffset12
;
424 case MachOGOTPageOffset12
:
425 case MachOTLVPageOffset12
: {
426 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
427 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
429 return TargetSymbolOrErr
.takeError();
430 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
431 if ((Instr
& 0xfffffc00) != 0xf9400000)
432 return make_error
<JITLinkError
>("GOTPAGEOFF12 target is not an LDR "
433 "immediate instruction with a zero "
436 if (*MachORelocKind
== MachOGOTPageOffset12
) {
437 Kind
= aarch64::RequestGOTAndTransformToPageOffset12
;
438 } else if (*MachORelocKind
== MachOTLVPageOffset12
) {
439 Kind
= aarch64::RequestTLVPAndTransformToPageOffset12
;
443 case MachOPointerToGOT
:
444 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
445 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
447 return TargetSymbolOrErr
.takeError();
449 Kind
= aarch64::RequestGOTAndTransformToDelta32
;
453 // We use Delta32/Delta64 to represent SUBTRACTOR relocations.
454 // parsePairRelocation handles the paired reloc, and returns the
455 // edge kind to be used (either Delta32/Delta64, or
456 // NegDelta32/NegDelta64, depending on the direction of the
457 // subtraction) along with the addend.
459 parsePairRelocation(*BlockToFix
, *MachORelocKind
, RI
,
460 FixupAddress
, FixupContent
, ++RelItr
, RelEnd
);
462 return PairInfo
.takeError();
463 std::tie(Kind
, TargetSymbol
, Addend
) = *PairInfo
;
464 assert(TargetSymbol
&& "No target symbol from parsePairRelocation?");
468 llvm_unreachable("Special relocation kind should not appear in "
474 Edge
GE(Kind
, FixupAddress
- BlockToFix
->getAddress(), *TargetSymbol
,
476 printEdge(dbgs(), *BlockToFix
, GE
, aarch64::getEdgeKindName(Kind
));
479 BlockToFix
->addEdge(Kind
, FixupAddress
- BlockToFix
->getAddress(),
480 *TargetSymbol
, Addend
);
483 return Error::success();
486 /// Return the string name of the given MachO arm64 edge kind.
487 const char *getMachOARM64RelocationKindName(Edge::Kind R
) {
490 return "MachOBranch26";
492 return "MachOPointer64";
493 case MachOPointer64Anon
:
494 return "MachOPointer64Anon";
496 return "MachOPage21";
497 case MachOPageOffset12
:
498 return "MachOPageOffset12";
500 return "MachOGOTPage21";
501 case MachOGOTPageOffset12
:
502 return "MachOGOTPageOffset12";
504 return "MachOTLVPage21";
505 case MachOTLVPageOffset12
:
506 return "MachOTLVPageOffset12";
507 case MachOPointerToGOT
:
508 return "MachOPointerToGOT";
509 case MachOPairedAddend
:
510 return "MachOPairedAddend";
511 case MachOLDRLiteral19
:
512 return "MachOLDRLiteral19";
514 return "MachODelta32";
516 return "MachODelta64";
517 case MachONegDelta32
:
518 return "MachONegDelta32";
519 case MachONegDelta64
:
520 return "MachONegDelta64";
522 return getGenericEdgeKindName(static_cast<Edge::Kind
>(R
));
526 unsigned NumSymbols
= 0;
534 Error
buildTables_MachO_arm64(LinkGraph
&G
) {
535 LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
537 aarch64::GOTTableManager GOT
;
538 aarch64::PLTTableManager
PLT(GOT
);
539 visitExistingEdges(G
, GOT
, PLT
);
540 return Error::success();
543 class MachOJITLinker_arm64
: public JITLinker
<MachOJITLinker_arm64
> {
544 friend class JITLinker
<MachOJITLinker_arm64
>;
547 MachOJITLinker_arm64(std::unique_ptr
<JITLinkContext
> Ctx
,
548 std::unique_ptr
<LinkGraph
> G
,
549 PassConfiguration PassConfig
)
550 : JITLinker(std::move(Ctx
), std::move(G
), std::move(PassConfig
)) {}
553 Error
applyFixup(LinkGraph
&G
, Block
&B
, const Edge
&E
) const {
554 return aarch64::applyFixup(G
, B
, E
);
557 uint64_t NullValue
= 0;
560 Expected
<std::unique_ptr
<LinkGraph
>>
561 createLinkGraphFromMachOObject_arm64(MemoryBufferRef ObjectBuffer
) {
562 auto MachOObj
= object::ObjectFile::createMachOObjectFile(ObjectBuffer
);
564 return MachOObj
.takeError();
566 auto Features
= (*MachOObj
)->getFeatures();
568 return Features
.takeError();
570 return MachOLinkGraphBuilder_arm64(**MachOObj
, std::move(*Features
))
574 void link_MachO_arm64(std::unique_ptr
<LinkGraph
> G
,
575 std::unique_ptr
<JITLinkContext
> Ctx
) {
577 PassConfiguration Config
;
579 if (Ctx
->shouldAddDefaultTargetPasses(G
->getTargetTriple())) {
580 // Add a mark-live pass.
581 if (auto MarkLive
= Ctx
->getMarkLivePass(G
->getTargetTriple()))
582 Config
.PrePrunePasses
.push_back(std::move(MarkLive
));
584 Config
.PrePrunePasses
.push_back(markAllSymbolsLive
);
586 // Add compact unwind splitter pass.
587 Config
.PrePrunePasses
.push_back(
588 CompactUnwindSplitter("__LD,__compact_unwind"));
590 // Add eh-frame passes.
591 // FIXME: Prune eh-frames for which compact-unwind is available once
592 // we support compact-unwind registration with libunwind.
593 Config
.PrePrunePasses
.push_back(createEHFrameSplitterPass_MachO_arm64());
594 Config
.PrePrunePasses
.push_back(createEHFrameEdgeFixerPass_MachO_arm64());
596 // Add an in-place GOT/Stubs pass.
597 Config
.PostPrunePasses
.push_back(buildTables_MachO_arm64
);
600 if (auto Err
= Ctx
->modifyPassConfig(*G
, Config
))
601 return Ctx
->notifyFailed(std::move(Err
));
603 // Construct a JITLinker and run the link function.
604 MachOJITLinker_arm64::link(std::move(Ctx
), std::move(G
), std::move(Config
));
607 LinkGraphPassFunction
createEHFrameSplitterPass_MachO_arm64() {
608 return DWARFRecordSectionSplitter("__TEXT,__eh_frame");
611 LinkGraphPassFunction
createEHFrameEdgeFixerPass_MachO_arm64() {
612 return EHFrameEdgeFixer("__TEXT,__eh_frame", aarch64::PointerSize
,
613 aarch64::Pointer32
, aarch64::Pointer64
,
614 aarch64::Delta32
, aarch64::Delta64
,
615 aarch64::NegDelta32
);
618 } // end namespace jitlink
619 } // end namespace llvm