Skip to content

Commit eb0f1dc

Browse files
authored
[LLD][ELF] Include offset when adding Thunk symbols (#144995)
Include the offset of a thunk in the ThunkSection when adding symbols. At Thunk creation time the offset is set to 0 as we don't know where in the ThunkSection the Thunk will end up. The symbol values are updated by the setOffset() call in assignOffsets(). When we transform a thunk from a short to a long, we sometimes add a mapping symbol. At this point the offset of the thunk is non zero and we need to account for that when defining the symbol, as the setOffset() call subtracts the offset before adding the new one back in. To test; added a second thunk that is converted to a long thunk to aarch64-thunk-bit-multipass. This second thunk is given a non zero offset from the start of the Thunk Section so we can observe the mapping symbol being put in the wrong place without accounting for the offset. fixes: #142326
1 parent b85387d commit eb0f1dc

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lld/ELF/Thunks.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
601601

602602
Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
603603
InputSectionBase &section) {
604-
Defined *d = addSyntheticLocal(ctx, name, type, value, /*size=*/0, section);
604+
Defined *d =
605+
addSyntheticLocal(ctx, name, type, value + offset, /*size=*/0, section);
605606
syms.push_back(d);
606607
return d;
607608
}

lld/test/ELF/aarch64-thunk-bti-multipass.s

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
_start:
3030
/// Call that requires a thunk.
3131
bl fn1
32+
bl fn2
3233
/// padding so that the thunk for fn1 is placed after this section is
3334
/// sufficiently close to the target to be within short range, but only
3435
/// just so that a small displacement will mean a long thunk is needed.
@@ -39,13 +40,20 @@ _start:
3940

4041
// CHECK-LABEL: <_start>:
4142
// CHECK-NEXT: 10001000: bl 0x10002008 <__AArch64AbsLongThunk_fn1>
43+
// CHECK-NEXT: bl 0x10002018 <__AArch64AbsLongThunk_fn2>
4244

4345
// CHECK-LABEL: <__AArch64AbsLongThunk_fn1>:
4446
// CHECK-NEXT: 10002008: ldr x16, 0x10002010 <__AArch64AbsLongThunk_fn1+0x8>
4547
// CHECK-NEXT: br x16
4648
// CHECK-NEXT: 00 30 00 18 .word 0x18003000
4749
// CHECK-NEXT: 00 00 00 00 .word 0x00000000
4850

51+
// CHECK-LABEL: <__AArch64AbsLongThunk_fn2>:
52+
// CHECK-NEXT: 10002018: ldr x16, 0x10002020 <__AArch64AbsLongThunk_fn2+0x8>
53+
// CHECK-NEXT: br x16
54+
// CHECK-NEXT: 04 40 00 18 .word 0x18004004
55+
// CHECK-NEXT: 00 00 00 00 .word 0x00000000
56+
4957
.section .text.1, "ax", %progbits
5058
.balign 0x1000
5159
.global farcall
@@ -75,6 +83,12 @@ farcall:
7583
fn1:
7684
ret
7785

86+
.section .text.3, "ax", %progbits
87+
.global fn2
88+
.type fn2, %function
89+
fn2:
90+
ret
91+
7892
.section .text.far, "ax", %progbits
7993
.type far, %function
8094
.global far
@@ -88,6 +102,12 @@ far:
88102
// CHECK-LABEL: <fn1>:
89103
// CHECK-NEXT: 18004000: ret
90104

105+
// CHECK-LABEL: <__AArch64BTIThunk_fn2>:
106+
// CHECK-NEXT: 18004004: bti c
107+
108+
// CHECK-LABEL: <fn2>:
109+
// CHECK-NEXT: 18004008: ret
110+
91111
// CHECK-LABEL: <__AArch64BTIThunk_far>:
92112
// CHECK-NEXT: 30000000: bti c
93113

@@ -104,6 +124,6 @@ SECTIONS {
104124
.rodata 0x10000000 : { *(.note.gnu.property) } :low
105125
.text_low : { *(.text.0) } :low
106126
.text 0x18001000 : { *(.text.1) } :mid
107-
.text_aligned : { *(.text.2) } :mid
127+
.text_aligned : { *(.text.2) *(.text.3) } :mid
108128
.text_high 0x30000000 : { *(.text.far) } :high
109129
}

0 commit comments

Comments
 (0)