Skip to content

Commit 7c81649

Browse files
committed
[COFF] Align ARM64 range extension thunks at instruction boundary
RangeExtensionThunkARM64 is created for out-of-range branches on Windows ARM64 because branch instructions has limited bits to encode target address. Currently, RangeExtensionThunkARM64 is appended to its referencing COFF section from object file at link time without any alignment requirement, so if size of the preceding COFF section is not aligned to instruction boundary (4 bytes), RangeExtensionThunkARM64 will emit thunk instructions at unaligned address which is never a valid branch target on ARM64, and usually triggers invalid instruction exception when branching to it. This PR fixes it by requiring such thunks to align at 4 bytes. Differential revision: https://reviews.llvm.org/D72473
1 parent de0a224 commit 7c81649

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lld/COFF/Chunks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class RangeExtensionThunkARM : public NonSectionChunk {
510510

511511
class RangeExtensionThunkARM64 : public NonSectionChunk {
512512
public:
513-
explicit RangeExtensionThunkARM64(Defined *t) : target(t) {}
513+
explicit RangeExtensionThunkARM64(Defined *t) : target(t) { setAlignment(4); }
514514
size_t getSize() const override;
515515
void writeTo(uint8_t *buf) const override;
516516

lld/test/COFF/arm64-thunks.s

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
// RUN: lld-link -entry:main -subsystem:console %t.obj -out:%t.exe -verbose 2>&1 | FileCheck -check-prefix=VERBOSE %s
44
// RUN: llvm-objdump -d %t.exe | FileCheck -check-prefix=DISASM %s
55

6-
// VERBOSE: Added 1 thunks with margin {{.*}} in 1 passes
6+
// VERBOSE: Added 2 thunks with margin {{.*}} in 1 passes
77

88
.globl main
99
.globl func1
10+
.globl func2
1011
.text
1112
main:
1213
tbz w0, #0, func1
@@ -15,6 +16,14 @@ main:
1516
.space 0x8000
1617
.section .text$b, "xr"
1718
func1:
19+
tbz w0, #0, func2
20+
ret
21+
.space 1
22+
.section .text$c, "xr"
23+
.space 0x8000
24+
.section .text$d, "xr"
25+
.align 2
26+
func2:
1827
ret
1928

2029
// DISASM: 0000000140001000 .text:
@@ -24,4 +33,11 @@ func1:
2433
// DISASM: 14000100c: 10 52 00 91 add x16, x16, #20
2534
// DISASM: 140001010: 00 02 1f d6 br x16
2635

27-
// DISASM: 140009014: c0 03 5f d6 ret
36+
// DISASM: 140009014: 60 00 00 36 tbz w0, #0, #12 <.text+0x8020>
37+
// DISASM: 140009018: c0 03 5f d6 ret
38+
39+
// DISASM: 140009020: 50 00 00 90 adrp x16, #32768
40+
// DISASM: 140009024: 10 b2 00 91 add x16, x16, #44
41+
// DISASM: 140009028: 00 02 1f d6 br x16
42+
43+
// DISASM: 14001102c: c0 03 5f d6 ret

0 commit comments

Comments
 (0)