Skip to content

Commit 6512e48

Browse files
authored
[LLD][ARM] Allow R_ARM_SBREL32 relocations in debug info (#116956)
The R_ARM_SBREL32 relocation is used in debug info for ARM RWPI (read-write position independent) code. Compiler-generated DWARF info will use an expression to add the relocated value to the actual value of the static base (held in r9) at run-time, so it should be relocated as if the static base is at address 0.
1 parent 55f5d68 commit 6512e48

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,
10941094
// R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC
10951095
// sections.
10961096
if (LLVM_LIKELY(expr == R_ABS) || expr == R_DTPREL || expr == R_GOTPLTREL ||
1097-
expr == R_RISCV_ADD) {
1097+
expr == R_RISCV_ADD || expr == R_ARM_SBREL) {
10981098
target.relocateNoSym(bufLoc, type,
10991099
SignExtend64<bits>(sym.getVA(ctx, addend)));
11001100
continue;

lld/test/ELF/arm-rwpi-debug-relocs.s

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// Test that R_ARM_SBREL32 relocations in debug info are relocated as if the
2+
/// static base register (r9) is zero. Real DWARF info will use an expression to
3+
/// add this to the real value of the static base at runtime.
4+
5+
// REQUIRES: arm
6+
// RUN: rm -rf %t && split-file %s %t && cd %t
7+
8+
// RUN: llvm-mc -filetype=obj -triple=armv7a asm.s -o obj.o
9+
// RUN: ld.lld -T lds.ld obj.o -o exe.elf 2>&1 | FileCheck %s --implicit-check-not=warning: --allow-empty
10+
// RUN: llvm-objdump -D exe.elf | FileCheck --check-prefix=DISASM %s
11+
12+
// DISASM-LABEL: <rw>:
13+
// DISASM-NEXT: 1000: 0000002a
14+
15+
// DISASM-LABEL: <rw2>:
16+
// DISASM-NEXT: 2000: 000004d2
17+
18+
// DISASM-LABEL: <.debug_something>:
19+
// DISASM-NEXT: 0: 00001000
20+
// DISASM-NEXT: ...
21+
// DISASM-NEXT: 104: 00002000
22+
23+
//--- lds.ld
24+
SECTIONS {
25+
data1 0x1000 : { *(data1) }
26+
data2 0x2000 : { *(data2) }
27+
}
28+
29+
//--- asm.s
30+
.text
31+
.type _start,%function
32+
.globl _start
33+
_start:
34+
bx lr
35+
.size _start, .-_start
36+
37+
.section data1, "aw", %progbits
38+
.type rw,%object
39+
.globl rw
40+
rw:
41+
.long 42
42+
.size rw, 4
43+
44+
.section data2, "aw", %progbits
45+
.type rw2,%object
46+
.globl rw2
47+
rw2:
48+
.long 1234
49+
.size rw2, 4
50+
51+
.section .debug_something, "", %progbits
52+
.long rw(sbrel)
53+
.space 0x100
54+
.long rw2(sbrel)

0 commit comments

Comments
 (0)