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"
15 #include "MachOLinkGraphBuilder.h"
16 #include "PerGraphGOTAndPLTStubsBuilder.h"
18 #define DEBUG_TYPE "jitlink"
21 using namespace llvm::jitlink
;
22 using namespace llvm::jitlink::MachO_arm64_Edges
;
26 class MachOLinkGraphBuilder_arm64
: public MachOLinkGraphBuilder
{
28 MachOLinkGraphBuilder_arm64(const object::MachOObjectFile
&Obj
)
29 : MachOLinkGraphBuilder(Obj
, Triple("arm64-apple-darwin"),
30 getMachOARM64RelocationKindName
),
31 NumSymbols(Obj
.getSymtabLoadCommand().nsyms
) {}
34 static Expected
<MachOARM64RelocationKind
>
35 getRelocationKind(const MachO::relocation_info
&RI
) {
37 case MachO::ARM64_RELOC_UNSIGNED
:
40 return RI
.r_extern
? Pointer64
: Pointer64Anon
;
41 else if (RI
.r_length
== 2)
45 case MachO::ARM64_RELOC_SUBTRACTOR
:
46 // SUBTRACTOR must be non-pc-rel, extern, with length 2 or 3.
47 // Initially represent SUBTRACTOR relocations with 'Delta<W>'.
48 // They may be turned into NegDelta<W> by parsePairRelocation.
49 if (!RI
.r_pcrel
&& RI
.r_extern
) {
52 else if (RI
.r_length
== 3)
56 case MachO::ARM64_RELOC_BRANCH26
:
57 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
60 case MachO::ARM64_RELOC_PAGE21
:
61 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
64 case MachO::ARM64_RELOC_PAGEOFF12
:
65 if (!RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
68 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21
:
69 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
72 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
73 if (!RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
74 return GOTPageOffset12
;
76 case MachO::ARM64_RELOC_POINTER_TO_GOT
:
77 if (RI
.r_pcrel
&& RI
.r_extern
&& RI
.r_length
== 2)
80 case MachO::ARM64_RELOC_ADDEND
:
81 if (!RI
.r_pcrel
&& !RI
.r_extern
&& RI
.r_length
== 2)
86 return make_error
<JITLinkError
>(
87 "Unsupported arm64 relocation: address=" +
88 formatv("{0:x8}", RI
.r_address
) +
89 ", symbolnum=" + formatv("{0:x6}", RI
.r_symbolnum
) +
90 ", kind=" + formatv("{0:x1}", RI
.r_type
) +
91 ", pc_rel=" + (RI
.r_pcrel
? "true" : "false") +
92 ", extern=" + (RI
.r_extern
? "true" : "false") +
93 ", length=" + formatv("{0:d}", RI
.r_length
));
97 std::tuple
<MachOARM64RelocationKind
, Symbol
*, uint64_t>;
99 // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success,
100 // returns the edge kind and addend to be used.
101 Expected
<PairRelocInfo
>
102 parsePairRelocation(Block
&BlockToFix
, Edge::Kind SubtractorKind
,
103 const MachO::relocation_info
&SubRI
,
104 JITTargetAddress FixupAddress
, const char *FixupContent
,
105 object::relocation_iterator
&UnsignedRelItr
,
106 object::relocation_iterator
&RelEnd
) {
107 using namespace support
;
109 assert(((SubtractorKind
== Delta32
&& SubRI
.r_length
== 2) ||
110 (SubtractorKind
== Delta64
&& SubRI
.r_length
== 3)) &&
111 "Subtractor kind should match length");
112 assert(SubRI
.r_extern
&& "SUBTRACTOR reloc symbol should be extern");
113 assert(!SubRI
.r_pcrel
&& "SUBTRACTOR reloc should not be PCRel");
115 if (UnsignedRelItr
== RelEnd
)
116 return make_error
<JITLinkError
>("arm64 SUBTRACTOR without paired "
117 "UNSIGNED relocation");
119 auto UnsignedRI
= getRelocationInfo(UnsignedRelItr
);
121 if (SubRI
.r_address
!= UnsignedRI
.r_address
)
122 return make_error
<JITLinkError
>("arm64 SUBTRACTOR and paired UNSIGNED "
123 "point to different addresses");
125 if (SubRI
.r_length
!= UnsignedRI
.r_length
)
126 return make_error
<JITLinkError
>("length of arm64 SUBTRACTOR and paired "
127 "UNSIGNED reloc must match");
130 if (auto FromSymbolOrErr
= findSymbolByIndex(SubRI
.r_symbolnum
))
131 FromSymbol
= FromSymbolOrErr
->GraphSymbol
;
133 return FromSymbolOrErr
.takeError();
135 // Read the current fixup value.
136 uint64_t FixupValue
= 0;
137 if (SubRI
.r_length
== 3)
138 FixupValue
= *(const little64_t
*)FixupContent
;
140 FixupValue
= *(const little32_t
*)FixupContent
;
142 // Find 'ToSymbol' using symbol number or address, depending on whether the
143 // paired UNSIGNED relocation is extern.
144 Symbol
*ToSymbol
= nullptr;
145 if (UnsignedRI
.r_extern
) {
146 // Find target symbol by symbol index.
147 if (auto ToSymbolOrErr
= findSymbolByIndex(UnsignedRI
.r_symbolnum
))
148 ToSymbol
= ToSymbolOrErr
->GraphSymbol
;
150 return ToSymbolOrErr
.takeError();
152 auto ToSymbolSec
= findSectionByIndex(UnsignedRI
.r_symbolnum
- 1);
154 return ToSymbolSec
.takeError();
155 ToSymbol
= getSymbolByAddress(ToSymbolSec
->Address
);
156 assert(ToSymbol
&& "No symbol for section");
157 FixupValue
-= ToSymbol
->getAddress();
160 MachOARM64RelocationKind DeltaKind
;
161 Symbol
*TargetSymbol
;
163 if (&BlockToFix
== &FromSymbol
->getAddressable()) {
164 TargetSymbol
= ToSymbol
;
165 DeltaKind
= (SubRI
.r_length
== 3) ? Delta64
: Delta32
;
166 Addend
= FixupValue
+ (FixupAddress
- FromSymbol
->getAddress());
167 // FIXME: handle extern 'from'.
168 } else if (&BlockToFix
== &ToSymbol
->getAddressable()) {
169 TargetSymbol
= &*FromSymbol
;
170 DeltaKind
= (SubRI
.r_length
== 3) ? NegDelta64
: NegDelta32
;
171 Addend
= FixupValue
- (FixupAddress
- ToSymbol
->getAddress());
173 // BlockToFix was neither FromSymbol nor ToSymbol.
174 return make_error
<JITLinkError
>("SUBTRACTOR relocation must fix up "
175 "either 'A' or 'B' (or a symbol in one "
176 "of their alt-entry groups)");
179 return PairRelocInfo(DeltaKind
, TargetSymbol
, Addend
);
182 Error
addRelocations() override
{
183 using namespace support
;
184 auto &Obj
= getObject();
186 LLVM_DEBUG(dbgs() << "Processing relocations:\n");
188 for (auto &S
: Obj
.sections()) {
190 JITTargetAddress SectionAddress
= S
.getAddress();
192 // Skip relocations virtual sections.
194 if (S
.relocation_begin() != S
.relocation_end())
195 return make_error
<JITLinkError
>("Virtual section contains "
200 // Skip relocations for debug symbols.
203 getSectionByIndex(Obj
.getSectionIndex(S
.getRawDataRefImpl()));
204 if (!NSec
.GraphSection
) {
206 dbgs() << " Skipping relocations for MachO section "
207 << NSec
.SegName
<< "/" << NSec
.SectName
208 << " which has no associated graph section\n";
214 for (auto RelItr
= S
.relocation_begin(), RelEnd
= S
.relocation_end();
215 RelItr
!= RelEnd
; ++RelItr
) {
217 MachO::relocation_info RI
= getRelocationInfo(RelItr
);
219 // Sanity check the relocation kind.
220 auto Kind
= getRelocationKind(RI
);
222 return Kind
.takeError();
224 // Find the address of the value to fix up.
225 JITTargetAddress FixupAddress
= SectionAddress
+ (uint32_t)RI
.r_address
;
229 getSectionByIndex(Obj
.getSectionIndex(S
.getRawDataRefImpl()));
230 dbgs() << " " << NSec
.SectName
<< " + "
231 << formatv("{0:x8}", RI
.r_address
) << ":\n";
234 // Find the block that the fixup points to.
235 Block
*BlockToFix
= nullptr;
237 auto SymbolToFixOrErr
= findSymbolByAddress(FixupAddress
);
238 if (!SymbolToFixOrErr
)
239 return SymbolToFixOrErr
.takeError();
240 BlockToFix
= &SymbolToFixOrErr
->getBlock();
243 if (FixupAddress
+ static_cast<JITTargetAddress
>(1ULL << RI
.r_length
) >
244 BlockToFix
->getAddress() + BlockToFix
->getContent().size())
245 return make_error
<JITLinkError
>(
246 "Relocation content extends past end of fixup block");
248 // Get a pointer to the fixup content.
249 const char *FixupContent
= BlockToFix
->getContent().data() +
250 (FixupAddress
- BlockToFix
->getAddress());
252 // The target symbol and addend will be populated by the switch below.
253 Symbol
*TargetSymbol
= nullptr;
256 if (*Kind
== PairedAddend
) {
257 // If this is an Addend relocation then process it and move to the
260 Addend
= SignExtend64(RI
.r_symbolnum
, 24);
262 if (RelItr
== RelEnd
)
263 return make_error
<JITLinkError
>("Unpaired Addend reloc at " +
264 formatv("{0:x16}", FixupAddress
));
266 RI
= getRelocationInfo(RelItr
);
268 Kind
= getRelocationKind(RI
);
270 return Kind
.takeError();
272 if (*Kind
!= Branch26
&& *Kind
!= Page21
&& *Kind
!= PageOffset12
)
273 return make_error
<JITLinkError
>(
274 "Invalid relocation pair: Addend + " +
275 StringRef(getMachOARM64RelocationKindName(*Kind
)));
278 dbgs() << " Addend: value = " << formatv("{0:x6}", Addend
)
279 << ", pair is " << getMachOARM64RelocationKindName(*Kind
)
283 // Find the address of the value to fix up.
284 JITTargetAddress PairedFixupAddress
=
285 SectionAddress
+ (uint32_t)RI
.r_address
;
286 if (PairedFixupAddress
!= FixupAddress
)
287 return make_error
<JITLinkError
>("Paired relocation points at "
293 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
294 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
296 return TargetSymbolOrErr
.takeError();
297 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
298 if ((Instr
& 0x7fffffff) != 0x14000000)
299 return make_error
<JITLinkError
>("BRANCH26 target is not a B or BL "
300 "instruction with a zero addend");
304 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
305 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
307 return TargetSymbolOrErr
.takeError();
308 Addend
= *(const ulittle32_t
*)FixupContent
;
311 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
312 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
314 return TargetSymbolOrErr
.takeError();
315 Addend
= *(const ulittle64_t
*)FixupContent
;
317 case Pointer64Anon
: {
318 JITTargetAddress TargetAddress
= *(const ulittle64_t
*)FixupContent
;
319 if (auto TargetSymbolOrErr
= findSymbolByAddress(TargetAddress
))
320 TargetSymbol
= &*TargetSymbolOrErr
;
322 return TargetSymbolOrErr
.takeError();
323 Addend
= TargetAddress
- TargetSymbol
->getAddress();
328 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
329 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
331 return TargetSymbolOrErr
.takeError();
332 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
333 if ((Instr
& 0xffffffe0) != 0x90000000)
334 return make_error
<JITLinkError
>("PAGE21/GOTPAGE21 target is not an "
335 "ADRP instruction with a zero "
340 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
341 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
343 return TargetSymbolOrErr
.takeError();
344 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
345 uint32_t EncodedAddend
= (Instr
& 0x003FFC00) >> 10;
346 if (EncodedAddend
!= 0)
347 return make_error
<JITLinkError
>("GOTPAGEOFF12 target has non-zero "
351 case GOTPageOffset12
: {
352 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
353 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
355 return TargetSymbolOrErr
.takeError();
356 uint32_t Instr
= *(const ulittle32_t
*)FixupContent
;
357 if ((Instr
& 0xfffffc00) != 0xf9400000)
358 return make_error
<JITLinkError
>("GOTPAGEOFF12 target is not an LDR "
359 "immediate instruction with a zero "
364 if (auto TargetSymbolOrErr
= findSymbolByIndex(RI
.r_symbolnum
))
365 TargetSymbol
= TargetSymbolOrErr
->GraphSymbol
;
367 return TargetSymbolOrErr
.takeError();
371 // We use Delta32/Delta64 to represent SUBTRACTOR relocations.
372 // parsePairRelocation handles the paired reloc, and returns the
373 // edge kind to be used (either Delta32/Delta64, or
374 // NegDelta32/NegDelta64, depending on the direction of the
375 // subtraction) along with the addend.
377 parsePairRelocation(*BlockToFix
, *Kind
, RI
, FixupAddress
,
378 FixupContent
, ++RelItr
, RelEnd
);
380 return PairInfo
.takeError();
381 std::tie(*Kind
, TargetSymbol
, Addend
) = *PairInfo
;
382 assert(TargetSymbol
&& "No target symbol from parsePairRelocation?");
386 llvm_unreachable("Special relocation kind should not appear in "
392 Edge
GE(*Kind
, FixupAddress
- BlockToFix
->getAddress(), *TargetSymbol
,
394 printEdge(dbgs(), *BlockToFix
, GE
,
395 getMachOARM64RelocationKindName(*Kind
));
398 BlockToFix
->addEdge(*Kind
, FixupAddress
- BlockToFix
->getAddress(),
399 *TargetSymbol
, Addend
);
402 return Error::success();
405 unsigned NumSymbols
= 0;
408 class PerGraphGOTAndPLTStubsBuilder_MachO_arm64
409 : public PerGraphGOTAndPLTStubsBuilder
<
410 PerGraphGOTAndPLTStubsBuilder_MachO_arm64
> {
412 using PerGraphGOTAndPLTStubsBuilder
<
413 PerGraphGOTAndPLTStubsBuilder_MachO_arm64
>::PerGraphGOTAndPLTStubsBuilder
;
415 bool isGOTEdgeToFix(Edge
&E
) const {
416 return E
.getKind() == GOTPage21
|| E
.getKind() == GOTPageOffset12
||
417 E
.getKind() == PointerToGOT
;
420 Symbol
&createGOTEntry(Symbol
&Target
) {
421 auto &GOTEntryBlock
= G
.createContentBlock(
422 getGOTSection(), getGOTEntryBlockContent(), 0, 8, 0);
423 GOTEntryBlock
.addEdge(Pointer64
, 0, Target
, 0);
424 return G
.addAnonymousSymbol(GOTEntryBlock
, 0, 8, false, false);
427 void fixGOTEdge(Edge
&E
, Symbol
&GOTEntry
) {
428 if (E
.getKind() == GOTPage21
|| E
.getKind() == GOTPageOffset12
) {
429 // Update the target, but leave the edge addend as-is.
430 E
.setTarget(GOTEntry
);
431 } else if (E
.getKind() == PointerToGOT
) {
432 E
.setTarget(GOTEntry
);
435 llvm_unreachable("Not a GOT edge?");
438 bool isExternalBranchEdge(Edge
&E
) {
439 return E
.getKind() == Branch26
&& !E
.getTarget().isDefined();
442 Symbol
&createPLTStub(Symbol
&Target
) {
443 auto &StubContentBlock
=
444 G
.createContentBlock(getStubsSection(), getStubBlockContent(), 0, 1, 0);
445 // Re-use GOT entries for stub targets.
446 auto &GOTEntrySymbol
= getGOTEntry(Target
);
447 StubContentBlock
.addEdge(LDRLiteral19
, 0, GOTEntrySymbol
, 0);
448 return G
.addAnonymousSymbol(StubContentBlock
, 0, 8, true, false);
451 void fixPLTEdge(Edge
&E
, Symbol
&Stub
) {
452 assert(E
.getKind() == Branch26
&& "Not a Branch32 edge?");
453 assert(E
.getAddend() == 0 && "Branch32 edge has non-zero addend?");
458 Section
&getGOTSection() {
460 GOTSection
= &G
.createSection("$__GOT", sys::Memory::MF_READ
);
464 Section
&getStubsSection() {
466 auto StubsProt
= static_cast<sys::Memory::ProtectionFlags
>(
467 sys::Memory::MF_READ
| sys::Memory::MF_EXEC
);
468 StubsSection
= &G
.createSection("$__STUBS", StubsProt
);
470 return *StubsSection
;
473 ArrayRef
<char> getGOTEntryBlockContent() {
474 return {reinterpret_cast<const char *>(NullGOTEntryContent
),
475 sizeof(NullGOTEntryContent
)};
478 ArrayRef
<char> getStubBlockContent() {
479 return {reinterpret_cast<const char *>(StubContent
), sizeof(StubContent
)};
482 static const uint8_t NullGOTEntryContent
[8];
483 static const uint8_t StubContent
[8];
484 Section
*GOTSection
= nullptr;
485 Section
*StubsSection
= nullptr;
489 PerGraphGOTAndPLTStubsBuilder_MachO_arm64::NullGOTEntryContent
[8] = {
490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
491 const uint8_t PerGraphGOTAndPLTStubsBuilder_MachO_arm64::StubContent
[8] = {
492 0x10, 0x00, 0x00, 0x58, // LDR x16, <literal>
493 0x00, 0x02, 0x1f, 0xd6 // BR x16
501 class MachOJITLinker_arm64
: public JITLinker
<MachOJITLinker_arm64
> {
502 friend class JITLinker
<MachOJITLinker_arm64
>;
505 MachOJITLinker_arm64(std::unique_ptr
<JITLinkContext
> Ctx
,
506 std::unique_ptr
<LinkGraph
> G
,
507 PassConfiguration PassConfig
)
508 : JITLinker(std::move(Ctx
), std::move(G
), std::move(PassConfig
)) {}
512 static unsigned getPageOffset12Shift(uint32_t Instr
) {
513 constexpr uint32_t LoadStoreImm12Mask
= 0x3b000000;
514 constexpr uint32_t Vec128Mask
= 0x04800000;
516 if ((Instr
& LoadStoreImm12Mask
) == 0x39000000) {
517 uint32_t ImplicitShift
= Instr
>> 30;
518 if (ImplicitShift
== 0)
519 if ((Instr
& Vec128Mask
) == Vec128Mask
)
522 return ImplicitShift
;
528 Error
applyFixup(LinkGraph
&G
, Block
&B
, const Edge
&E
) const {
529 using namespace support
;
531 char *BlockWorkingMem
= B
.getAlreadyMutableContent().data();
532 char *FixupPtr
= BlockWorkingMem
+ E
.getOffset();
533 JITTargetAddress FixupAddress
= B
.getAddress() + E
.getOffset();
535 switch (E
.getKind()) {
537 assert((FixupAddress
& 0x3) == 0 && "Branch-inst is not 32-bit aligned");
539 int64_t Value
= E
.getTarget().getAddress() - FixupAddress
+ E
.getAddend();
541 if (static_cast<uint64_t>(Value
) & 0x3)
542 return make_error
<JITLinkError
>("Branch26 target is not 32-bit "
545 if (Value
< -(1 << 27) || Value
> ((1 << 27) - 1))
546 return makeTargetOutOfRangeError(G
, B
, E
);
548 uint32_t RawInstr
= *(little32_t
*)FixupPtr
;
549 assert((RawInstr
& 0x7fffffff) == 0x14000000 &&
550 "RawInstr isn't a B or BR immediate instruction");
551 uint32_t Imm
= (static_cast<uint32_t>(Value
) & ((1 << 28) - 1)) >> 2;
552 uint32_t FixedInstr
= RawInstr
| Imm
;
553 *(little32_t
*)FixupPtr
= FixedInstr
;
557 uint64_t Value
= E
.getTarget().getAddress() + E
.getAddend();
558 if (Value
> std::numeric_limits
<uint32_t>::max())
559 return makeTargetOutOfRangeError(G
, B
, E
);
560 *(ulittle32_t
*)FixupPtr
= Value
;
564 case Pointer64Anon
: {
565 uint64_t Value
= E
.getTarget().getAddress() + E
.getAddend();
566 *(ulittle64_t
*)FixupPtr
= Value
;
571 assert((E
.getKind() != GOTPage21
|| E
.getAddend() == 0) &&
572 "GOTPAGE21 with non-zero addend");
573 uint64_t TargetPage
=
574 (E
.getTarget().getAddress() + E
.getAddend()) &
575 ~static_cast<uint64_t>(4096 - 1);
576 uint64_t PCPage
= FixupAddress
& ~static_cast<uint64_t>(4096 - 1);
578 int64_t PageDelta
= TargetPage
- PCPage
;
579 if (PageDelta
< -(1 << 30) || PageDelta
> ((1 << 30) - 1))
580 return makeTargetOutOfRangeError(G
, B
, E
);
582 uint32_t RawInstr
= *(ulittle32_t
*)FixupPtr
;
583 assert((RawInstr
& 0xffffffe0) == 0x90000000 &&
584 "RawInstr isn't an ADRP instruction");
585 uint32_t ImmLo
= (static_cast<uint64_t>(PageDelta
) >> 12) & 0x3;
586 uint32_t ImmHi
= (static_cast<uint64_t>(PageDelta
) >> 14) & 0x7ffff;
587 uint32_t FixedInstr
= RawInstr
| (ImmLo
<< 29) | (ImmHi
<< 5);
588 *(ulittle32_t
*)FixupPtr
= FixedInstr
;
592 uint64_t TargetOffset
=
593 (E
.getTarget().getAddress() + E
.getAddend()) & 0xfff;
595 uint32_t RawInstr
= *(ulittle32_t
*)FixupPtr
;
596 unsigned ImmShift
= getPageOffset12Shift(RawInstr
);
598 if (TargetOffset
& ((1 << ImmShift
) - 1))
599 return make_error
<JITLinkError
>("PAGEOFF12 target is not aligned");
601 uint32_t EncodedImm
= (TargetOffset
>> ImmShift
) << 10;
602 uint32_t FixedInstr
= RawInstr
| EncodedImm
;
603 *(ulittle32_t
*)FixupPtr
= FixedInstr
;
606 case GOTPageOffset12
: {
607 assert(E
.getAddend() == 0 && "GOTPAGEOF12 with non-zero addend");
609 uint32_t RawInstr
= *(ulittle32_t
*)FixupPtr
;
610 assert((RawInstr
& 0xfffffc00) == 0xf9400000 &&
611 "RawInstr isn't a 64-bit LDR immediate");
613 uint32_t TargetOffset
= E
.getTarget().getAddress() & 0xfff;
614 assert((TargetOffset
& 0x7) == 0 && "GOT entry is not 8-byte aligned");
615 uint32_t EncodedImm
= (TargetOffset
>> 3) << 10;
616 uint32_t FixedInstr
= RawInstr
| EncodedImm
;
617 *(ulittle32_t
*)FixupPtr
= FixedInstr
;
621 assert((FixupAddress
& 0x3) == 0 && "LDR is not 32-bit aligned");
622 assert(E
.getAddend() == 0 && "LDRLiteral19 with non-zero addend");
623 uint32_t RawInstr
= *(ulittle32_t
*)FixupPtr
;
624 assert(RawInstr
== 0x58000010 && "RawInstr isn't a 64-bit LDR literal");
625 int64_t Delta
= E
.getTarget().getAddress() - FixupAddress
;
627 return make_error
<JITLinkError
>("LDR literal target is not 32-bit "
629 if (Delta
< -(1 << 20) || Delta
> ((1 << 20) - 1))
630 return makeTargetOutOfRangeError(G
, B
, E
);
632 uint32_t EncodedImm
= (static_cast<uint32_t>(Delta
) >> 2) << 5;
633 uint32_t FixedInstr
= RawInstr
| EncodedImm
;
634 *(ulittle32_t
*)FixupPtr
= FixedInstr
;
642 if (E
.getKind() == Delta32
|| E
.getKind() == Delta64
)
643 Value
= E
.getTarget().getAddress() - FixupAddress
+ E
.getAddend();
645 Value
= FixupAddress
- E
.getTarget().getAddress() + E
.getAddend();
647 if (E
.getKind() == Delta32
|| E
.getKind() == NegDelta32
) {
648 if (Value
< std::numeric_limits
<int32_t>::min() ||
649 Value
> std::numeric_limits
<int32_t>::max())
650 return makeTargetOutOfRangeError(G
, B
, E
);
651 *(little32_t
*)FixupPtr
= Value
;
653 *(little64_t
*)FixupPtr
= Value
;
657 llvm_unreachable("Unrecognized edge kind");
660 return Error::success();
663 uint64_t NullValue
= 0;
666 Expected
<std::unique_ptr
<LinkGraph
>>
667 createLinkGraphFromMachOObject_arm64(MemoryBufferRef ObjectBuffer
) {
668 auto MachOObj
= object::ObjectFile::createMachOObjectFile(ObjectBuffer
);
670 return MachOObj
.takeError();
671 return MachOLinkGraphBuilder_arm64(**MachOObj
).buildGraph();
674 void link_MachO_arm64(std::unique_ptr
<LinkGraph
> G
,
675 std::unique_ptr
<JITLinkContext
> Ctx
) {
677 PassConfiguration Config
;
679 if (Ctx
->shouldAddDefaultTargetPasses(G
->getTargetTriple())) {
680 // Add a mark-live pass.
681 if (auto MarkLive
= Ctx
->getMarkLivePass(G
->getTargetTriple()))
682 Config
.PrePrunePasses
.push_back(std::move(MarkLive
));
684 Config
.PrePrunePasses
.push_back(markAllSymbolsLive
);
686 // Add an in-place GOT/Stubs pass.
687 Config
.PostPrunePasses
.push_back(
688 PerGraphGOTAndPLTStubsBuilder_MachO_arm64::asPass
);
691 if (auto Err
= Ctx
->modifyPassConfig(*G
, Config
))
692 return Ctx
->notifyFailed(std::move(Err
));
694 // Construct a JITLinker and run the link function.
695 MachOJITLinker_arm64::link(std::move(Ctx
), std::move(G
), std::move(Config
));
698 const char *getMachOARM64RelocationKindName(Edge::Kind R
) {
705 return "Pointer64Anon";
709 return "PageOffset12";
712 case GOTPageOffset12
:
713 return "GOTPageOffset12";
715 return "PointerToGOT";
717 return "PairedAddend";
719 return "LDRLiteral19";
729 return getGenericEdgeKindName(static_cast<Edge::Kind
>(R
));
733 } // end namespace jitlink
734 } // end namespace llvm