Skip to content

Commit ee2d0fe

Browse files
committed
[lld][ARM] Don't emit veneers for wraparound branches.
If an instruction at the high end of the 32-bit address space branches to one at the low end, then the branch can be within range for a B or BL instruction, and doesn't need a veneer. `ARM::inBranchRange` was failing to detect this because it calculated the offset as an int64_t, so that the offset was a small value ± 2^32 instead of just the smalle value. Fixes #165211.
1 parent 554ea40 commit ee2d0fe

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

lld/ELF/Arch/ARM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
472472
// Bit 0 == 1 denotes Thumb state, it is not part of the range.
473473
dst &= ~0x1;
474474

475-
int64_t offset = dst - src;
475+
int64_t offset = llvm::SignExtend64<32>(dst - src);
476476
switch (type) {
477477
case R_ARM_PC24:
478478
case R_ARM_PLT32:
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// REQUIRES: arm
2+
// RUN: rm -rf %t && split-file %s %t && cd %t
3+
// RUN: llvm-mc -filetype=obj -triple=armv7-none-eabi code.s -o code.o
4+
// RUN: ld.lld -T unsigned1.ld code.o -o unsigned1.elf
5+
// RUN: llvm-objdump --triple=armv7 -d unsigned1.elf | FileCheck %s --check-prefix=UNSIGNED1
6+
// RUN: ld.lld -T unsigned2.ld code.o -o unsigned2.elf
7+
// RUN: llvm-objdump --triple=armv7 -d unsigned2.elf | FileCheck %s --check-prefix=UNSIGNED2
8+
// RUN: ld.lld -T signed1.ld code.o -o signed1.elf
9+
// RUN: llvm-objdump --triple=armv7 -d signed1.elf | FileCheck %s --check-prefix=SIGNED1
10+
// RUN: ld.lld -T signed2.ld code.o -o signed2.elf
11+
// RUN: llvm-objdump --triple=armv7 -d signed2.elf | FileCheck %s --check-prefix=SIGNED2
12+
13+
// The aim of this test is to ensure that a BL instruction near one end of the
14+
// address space can reach a function at the extreme other end, directly, using
15+
// a branch offset that makes the address wrap round. We check this at both the
16+
// unsigned wraparound point (one address near 0 and the other near 0xFFFFFFFF)
17+
// and the signed wraparound point (addresses either side of 0x80000000),
18+
// crossing the boundary in both directions. In all four cases we expect a
19+
// direct branch with no veneer.
20+
21+
// UNSIGNED1: Disassembly of section .text.lowaddr:
22+
// UNSIGNED1: 00010000 <func>:
23+
// UNSIGNED1: 10000: e12fff1e bx lr
24+
//
25+
// UNSIGNED1: Disassembly of section .text.highaddr:
26+
// UNSIGNED1: ffff0000 <_start>:
27+
// UNSIGNED1: ffff0000: eb007ffe bl 0x10000
28+
// UNSIGNED1: ffff0004: e12fff1e bx lr
29+
30+
// UNSIGNED2: Disassembly of section .text.lowaddr:
31+
// UNSIGNED2: 00010000 <_start>:
32+
// UNSIGNED2: 10000: ebff7ffe bl 0xffff0000
33+
// UNSIGNED2: 10004: e12fff1e bx lr
34+
//
35+
// UNSIGNED2: Disassembly of section .text.highaddr:
36+
// UNSIGNED2: ffff0000 <func>:
37+
// UNSIGNED2: ffff0000: e12fff1e bx lr
38+
39+
// SIGNED1: Disassembly of section .text.posaddr:
40+
// SIGNED1: 7fff0000 <_start>:
41+
// SIGNED1: 7fff0000: eb007ffe bl 0x80010000
42+
// SIGNED1: 7fff0004: e12fff1e bx lr
43+
//
44+
// SIGNED1: Disassembly of section .text.negaddr:
45+
// SIGNED1: 80010000 <func>:
46+
// SIGNED1: 80010000: e12fff1e bx lr
47+
48+
// SIGNED2: Disassembly of section .text.posaddr:
49+
// SIGNED2: 7fff0000 <func>:
50+
// SIGNED2: 7fff0000: e12fff1e bx lr
51+
//
52+
// SIGNED2: Disassembly of section .text.negaddr:
53+
// SIGNED2: 80010000 <_start>:
54+
// SIGNED2: 80010000: ebff7ffe bl 0x7fff0000
55+
// SIGNED2: 80010004: e12fff1e bx lr
56+
57+
//--- code.s
58+
59+
.section .text.callee, "ax", %progbits
60+
.global func
61+
.type func, %function
62+
func:
63+
bx lr
64+
65+
.section .text.caller, "ax", %progbits
66+
.global _start
67+
.type _start, %function
68+
_start:
69+
bl func
70+
bx lr
71+
72+
//--- unsigned1.ld
73+
74+
ENTRY(_start)
75+
PHDRS {
76+
lowaddr PT_LOAD FLAGS(0x1 | 0x4);
77+
highaddr PT_LOAD FLAGS(0x1 | 0x4);
78+
}
79+
SECTIONS {
80+
.text.lowaddr 0x00010000 : { *(.text.callee) } :lowaddr
81+
.text.highaddr 0xffff0000 : { *(.text.caller) } :highaddr
82+
}
83+
84+
//--- unsigned2.ld
85+
86+
ENTRY(_start)
87+
PHDRS {
88+
lowaddr PT_LOAD FLAGS(0x1 | 0x4);
89+
highaddr PT_LOAD FLAGS(0x1 | 0x4);
90+
}
91+
SECTIONS {
92+
.text.lowaddr 0x00010000 : { *(.text.caller) } :lowaddr
93+
.text.highaddr 0xffff0000 : { *(.text.callee) } :highaddr
94+
}
95+
96+
//--- signed1.ld
97+
98+
ENTRY(_start)
99+
PHDRS {
100+
posaddr PT_LOAD FLAGS(0x1 | 0x4);
101+
negaddr PT_LOAD FLAGS(0x1 | 0x4);
102+
}
103+
SECTIONS {
104+
.text.posaddr 0x7fff0000 : { *(.text.caller) } :posaddr
105+
.text.negaddr 0x80010000 : { *(.text.callee) } :negaddr
106+
}
107+
108+
//--- signed2.ld
109+
110+
ENTRY(_start)
111+
PHDRS {
112+
posaddr PT_LOAD FLAGS(0x1 | 0x4);
113+
negaddr PT_LOAD FLAGS(0x1 | 0x4);
114+
}
115+
SECTIONS {
116+
.text.posaddr 0x7fff0000 : { *(.text.callee) } :posaddr
117+
.text.negaddr 0x80010000 : { *(.text.caller) } :negaddr
118+
}

0 commit comments

Comments
 (0)