Skip to content

Commit 4fadf41

Browse files
committed
[ELF] Pass Ctx & to ARM/AArch64
1 parent 1f2c08b commit 4fadf41

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ uint64_t elf::getAArch64Page(uint64_t expr) {
3232
// Target Identification has been enabled. As linker generated branches are
3333
// via x16 the BTI landing pads are defined as: BTI C, BTI J, BTI JC, PACIASP,
3434
// PACIBSP.
35-
bool elf::isAArch64BTILandingPad(Symbol &s, int64_t a) {
35+
bool elf::isAArch64BTILandingPad(Ctx &ctx, Symbol &s, int64_t a) {
3636
// PLT entries accessed indirectly have a BTI c.
3737
if (s.isInPlt(ctx))
3838
return true;

lld/ELF/Target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void writePrefixedInst(Ctx &, uint8_t *loc, uint64_t insn);
239239
void addPPC64SaveRestore(Ctx &);
240240
uint64_t getPPC64TocBase(Ctx &ctx);
241241
uint64_t getAArch64Page(uint64_t expr);
242-
bool isAArch64BTILandingPad(Symbol &s, int64_t a);
242+
bool isAArch64BTILandingPad(Ctx &, Symbol &s, int64_t a);
243243
template <typename ELFT> void writeARMCmseImportLib(Ctx &);
244244
uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, RelType type);
245245
void riscvFinalizeRelax(int passes);

lld/ELF/Thunks.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,15 @@ void Thunk::setOffset(uint64_t newOffset) {
549549
}
550550

551551
// AArch64 Thunk base class.
552-
static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
552+
static uint64_t getAArch64ThunkDestVA(Ctx &ctx, const Symbol &s, int64_t a) {
553553
uint64_t v = s.isInPlt(ctx) ? s.getPltVA(ctx) : s.getVA(a);
554554
return v;
555555
}
556556

557557
bool AArch64Thunk::getMayUseShortThunk() {
558558
if (!mayUseShortThunk)
559559
return false;
560-
uint64_t s = getAArch64ThunkDestVA(destination, addend);
560+
uint64_t s = getAArch64ThunkDestVA(ctx, destination, addend);
561561
uint64_t p = getThunkTargetSym()->getVA();
562562
mayUseShortThunk = llvm::isInt<28>(s - p);
563563
return mayUseShortThunk;
@@ -568,7 +568,7 @@ void AArch64Thunk::writeTo(uint8_t *buf) {
568568
writeLong(buf);
569569
return;
570570
}
571-
uint64_t s = getAArch64ThunkDestVA(destination, addend);
571+
uint64_t s = getAArch64ThunkDestVA(ctx, destination, addend);
572572
uint64_t p = getThunkTargetSym()->getVA();
573573
write32(buf, 0x14000000); // b S
574574
ctx.target->relocateNoSym(buf, R_AARCH64_CALL26, s - p);
@@ -591,8 +591,9 @@ void AArch64ABSLongThunk::writeLong(uint8_t *buf) {
591591
// If mayNeedLandingPad is true then destination is an
592592
// AArch64BTILandingPadThunk that defines landingPad.
593593
assert(!mayNeedLandingPad || landingPad != nullptr);
594-
uint64_t s = mayNeedLandingPad ? landingPad->getVA(0)
595-
: getAArch64ThunkDestVA(destination, addend);
594+
uint64_t s = mayNeedLandingPad
595+
? landingPad->getVA(0)
596+
: getAArch64ThunkDestVA(ctx, destination, addend);
596597
memcpy(buf, data, sizeof(data));
597598
ctx.target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
598599
}
@@ -619,8 +620,9 @@ void AArch64ADRPThunk::writeLong(uint8_t *buf) {
619620
// if mayNeedLandingPad is true then destination is an
620621
// AArch64BTILandingPadThunk that defines landingPad.
621622
assert(!mayNeedLandingPad || landingPad != nullptr);
622-
uint64_t s = mayNeedLandingPad ? landingPad->getVA(0)
623-
: getAArch64ThunkDestVA(destination, addend);
623+
uint64_t s = mayNeedLandingPad
624+
? landingPad->getVA(0)
625+
: getAArch64ThunkDestVA(ctx, destination, addend);
624626
uint64_t p = getThunkTargetSym()->getVA();
625627
memcpy(buf, data, sizeof(data));
626628
ctx.target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
@@ -676,7 +678,7 @@ void AArch64BTILandingPadThunk::writeLong(uint8_t *buf) {
676678
}
677679

678680
// ARM Target Thunks
679-
static uint64_t getARMThunkDestVA(const Symbol &s) {
681+
static uint64_t getARMThunkDestVA(Ctx &ctx, const Symbol &s) {
680682
uint64_t v = s.isInPlt(ctx) ? s.getPltVA(ctx) : s.getVA();
681683
return SignExtend64<32>(v);
682684
}
@@ -686,7 +688,7 @@ static uint64_t getARMThunkDestVA(const Symbol &s) {
686688
bool ARMThunk::getMayUseShortThunk() {
687689
if (!mayUseShortThunk)
688690
return false;
689-
uint64_t s = getARMThunkDestVA(destination);
691+
uint64_t s = getARMThunkDestVA(ctx, destination);
690692
if (s & 1) {
691693
mayUseShortThunk = false;
692694
return false;
@@ -703,7 +705,7 @@ void ARMThunk::writeTo(uint8_t *buf) {
703705
return;
704706
}
705707

706-
uint64_t s = getARMThunkDestVA(destination);
708+
uint64_t s = getARMThunkDestVA(ctx, destination);
707709
uint64_t p = getThunkTargetSym()->getVA();
708710
int64_t offset = s - p - 8;
709711
write32(buf, 0xea000000); // b S
@@ -729,7 +731,7 @@ bool ARMThunk::isCompatibleWith(const InputSection &isec,
729731
bool ThumbThunk::getMayUseShortThunk() {
730732
if (!mayUseShortThunk || !ctx.arg.armJ1J2BranchEncoding)
731733
return false;
732-
uint64_t s = getARMThunkDestVA(destination);
734+
uint64_t s = getARMThunkDestVA(ctx, destination);
733735
if ((s & 1) == 0) {
734736
mayUseShortThunk = false;
735737
return false;
@@ -746,7 +748,7 @@ void ThumbThunk::writeTo(uint8_t *buf) {
746748
return;
747749
}
748750

749-
uint64_t s = getARMThunkDestVA(destination);
751+
uint64_t s = getARMThunkDestVA(ctx, destination);
750752
uint64_t p = getThunkTargetSym()->getVA();
751753
int64_t offset = s - p - 4;
752754
write16(buf + 0, 0xf000); // b.w S
@@ -768,7 +770,7 @@ void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
768770
write32(buf + 0, 0xe300c000); // movw ip,:lower16:S
769771
write32(buf + 4, 0xe340c000); // movt ip,:upper16:S
770772
write32(buf + 8, 0xe12fff1c); // bx ip
771-
uint64_t s = getARMThunkDestVA(destination);
773+
uint64_t s = getARMThunkDestVA(ctx, destination);
772774
ctx.target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
773775
ctx.target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
774776
}
@@ -785,7 +787,7 @@ void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
785787
write16(buf + 4, 0xf2c0); // movt ip, :upper16:S
786788
write16(buf + 6, 0x0c00);
787789
write16(buf + 8, 0x4760); // bx ip
788-
uint64_t s = getARMThunkDestVA(destination);
790+
uint64_t s = getARMThunkDestVA(ctx, destination);
789791
ctx.target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
790792
ctx.target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
791793
}
@@ -801,7 +803,7 @@ void ARMV7PILongThunk::writeLong(uint8_t *buf) {
801803
write32(buf + 4, 0xe340c000); // movt ip,:upper16:S - (P + (L1-P) + 8)
802804
write32(buf + 8, 0xe08cc00f); // L1: add ip, ip, pc
803805
write32(buf + 12, 0xe12fff1c); // bx ip
804-
uint64_t s = getARMThunkDestVA(destination);
806+
uint64_t s = getARMThunkDestVA(ctx, destination);
805807
uint64_t p = getThunkTargetSym()->getVA();
806808
int64_t offset = s - p - 16;
807809
ctx.target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
@@ -821,7 +823,7 @@ void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
821823
write16(buf + 6, 0x0c00);
822824
write16(buf + 8, 0x44fc); // L1: add ip, pc
823825
write16(buf + 10, 0x4760); // bx ip
824-
uint64_t s = getARMThunkDestVA(destination);
826+
uint64_t s = getARMThunkDestVA(ctx, destination);
825827
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
826828
int64_t offset = s - p - 12;
827829
ctx.target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
@@ -844,7 +846,7 @@ void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
844846
write16(buf + 4, 0x9001); // str r0, [sp, #4] ; SP + 4 = S
845847
write16(buf + 6, 0xbd01); // pop {r0, pc} ; restore r0 and branch to dest
846848
write32(buf + 8, 0x00000000); // L1: .word S
847-
uint64_t s = getARMThunkDestVA(destination);
849+
uint64_t s = getARMThunkDestVA(ctx, destination);
848850
ctx.target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
849851
}
850852

@@ -871,7 +873,7 @@ void ThumbV6MABSXOLongThunk::writeLong(uint8_t *buf) {
871873
write16(buf + 14, 0x3000); // adds r0, :lower0_7:S
872874
write16(buf + 16, 0x9001); // str r0, [sp, #4] ; SP + 4 = S
873875
write16(buf + 18, 0xbd01); // pop {r0, pc} ; restore r0 and branch to dest
874-
uint64_t s = getARMThunkDestVA(destination);
876+
uint64_t s = getARMThunkDestVA(ctx, destination);
875877
ctx.target->relocateNoSym(buf + 2, R_ARM_THM_ALU_ABS_G3, s);
876878
ctx.target->relocateNoSym(buf + 6, R_ARM_THM_ALU_ABS_G2_NC, s);
877879
ctx.target->relocateNoSym(buf + 10, R_ARM_THM_ALU_ABS_G1_NC, s);
@@ -895,7 +897,7 @@ void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
895897
write16(buf + 8, 0x44e7); // L1: add pc, ip ; transfer control
896898
write16(buf + 10, 0x46c0); // nop ; pad to 4-byte boundary
897899
write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 4)
898-
uint64_t s = getARMThunkDestVA(destination);
900+
uint64_t s = getARMThunkDestVA(ctx, destination);
899901
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
900902
ctx.target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
901903
}
@@ -912,7 +914,7 @@ void ARMV5LongLdrPcThunk::writeLong(uint8_t *buf) {
912914
write32(buf + 0, 0xe51ff004); // ldr pc, [pc,#-4] ; L1
913915
write32(buf + 4, 0x00000000); // L1: .word S
914916
ctx.target->relocateNoSym(buf + 4, R_ARM_ABS32,
915-
getARMThunkDestVA(destination));
917+
getARMThunkDestVA(ctx, destination));
916918
}
917919

918920
void ARMV5LongLdrPcThunk::addSymbols(ThunkSection &isec) {
@@ -928,7 +930,7 @@ void ARMV4ABSLongBXThunk::writeLong(uint8_t *buf) {
928930
write32(buf + 4, 0xe12fff1c); // bx r12
929931
write32(buf + 8, 0x00000000); // L1: .word S
930932
ctx.target->relocateNoSym(buf + 8, R_ARM_ABS32,
931-
getARMThunkDestVA(destination));
933+
getARMThunkDestVA(ctx, destination));
932934
}
933935

934936
void ARMV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {
@@ -945,7 +947,7 @@ void ThumbV4ABSLongBXThunk::writeLong(uint8_t *buf) {
945947
write32(buf + 4, 0xe51ff004); // ldr pc, [pc, #-4] ; L1
946948
write32(buf + 8, 0x00000000); // L1: .word S
947949
ctx.target->relocateNoSym(buf + 8, R_ARM_ABS32,
948-
getARMThunkDestVA(destination));
950+
getARMThunkDestVA(ctx, destination));
949951
}
950952

951953
void ThumbV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {
@@ -964,7 +966,7 @@ void ThumbV4ABSLongThunk::writeLong(uint8_t *buf) {
964966
write32(buf + 8, 0xe12fff1c); // bx r12
965967
write32(buf + 12, 0x00000000); // L1: .word S
966968
ctx.target->relocateNoSym(buf + 12, R_ARM_ABS32,
967-
getARMThunkDestVA(destination));
969+
getARMThunkDestVA(ctx, destination));
968970
}
969971

970972
void ThumbV4ABSLongThunk::addSymbols(ThunkSection &isec) {
@@ -981,7 +983,7 @@ void ARMV4PILongBXThunk::writeLong(uint8_t *buf) {
981983
write32(buf + 4, 0xe08fc00c); // L1: add ip, pc, ip
982984
write32(buf + 8, 0xe12fff1c); // bx ip
983985
write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)
984-
uint64_t s = getARMThunkDestVA(destination);
986+
uint64_t s = getARMThunkDestVA(ctx, destination);
985987
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
986988
ctx.target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
987989
}
@@ -998,7 +1000,7 @@ void ARMV4PILongThunk::writeLong(uint8_t *buf) {
9981000
write32(buf + 0, 0xe59fc000); // P: ldr ip, [pc] ; L2
9991001
write32(buf + 4, 0xe08ff00c); // L1: add pc, pc, r12
10001002
write32(buf + 8, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)
1001-
uint64_t s = getARMThunkDestVA(destination);
1003+
uint64_t s = getARMThunkDestVA(ctx, destination);
10021004
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
10031005
ctx.target->relocateNoSym(buf + 8, R_ARM_REL32, s - p - 12);
10041006
}
@@ -1017,7 +1019,7 @@ void ThumbV4PILongBXThunk::writeLong(uint8_t *buf) {
10171019
write32(buf + 4, 0xe59fc000); // ldr r12, [pc] ; L2
10181020
write32(buf + 8, 0xe08cf00f); // L1: add pc, r12, pc
10191021
write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)
1020-
uint64_t s = getARMThunkDestVA(destination);
1022+
uint64_t s = getARMThunkDestVA(ctx, destination);
10211023
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
10221024
ctx.target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 16);
10231025
}
@@ -1038,7 +1040,7 @@ void ThumbV4PILongThunk::writeLong(uint8_t *buf) {
10381040
write32(buf + 8, 0xe08fc00c); // L1: add ip, pc, ip
10391041
write32(buf + 12, 0xe12fff1c); // bx ip
10401042
write32(buf + 16, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)
1041-
uint64_t s = getARMThunkDestVA(destination);
1043+
uint64_t s = getARMThunkDestVA(ctx, destination);
10421044
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
10431045
ctx.target->relocateNoSym(buf + 16, R_ARM_REL32, s - p - 16);
10441046
}
@@ -1366,7 +1368,7 @@ static Thunk *addThunkAArch64(Ctx &ctx, RelType type, Symbol &s, int64_t a) {
13661368
fatal("unrecognized relocation type");
13671369
bool mayNeedLandingPad =
13681370
(ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) &&
1369-
!isAArch64BTILandingPad(s, a);
1371+
!isAArch64BTILandingPad(ctx, s, a);
13701372
if (ctx.arg.picThunk)
13711373
return make<AArch64ADRPThunk>(ctx, s, a, mayNeedLandingPad);
13721374
return make<AArch64ABSLongThunk>(ctx, s, a, mayNeedLandingPad);

0 commit comments

Comments
 (0)