Skip to content

Commit 2f98c5f

Browse files
committed
[BOLT] Update skipRelocation for aarch64
The ld might relax ADRP+ADD or ADRP+LDR sequences to the ADR+NOP, add the new case to the skipRelocation for aarch64. Vladislav Khmelevsky, Advanced Software Technology Lab, Huawei Differential Revision: https://reviews.llvm.org/D123334
1 parent b8bac95 commit 2f98c5f

File tree

4 files changed

+212
-12
lines changed

4 files changed

+212
-12
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,17 @@ bool skipRelocationProcessX86(uint64_t Type, uint64_t Contents) {
168168
bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
169169
auto IsMov = [](uint64_t Contents) -> bool {
170170
// The bits 28-23 are 0b100101
171-
if ((Contents & 0x1f800000) == 0x12800000)
172-
return true;
173-
return false;
171+
return (Contents & 0x1f800000) == 0x12800000;
174172
};
175173

176174
auto IsB = [](uint64_t Contents) -> bool {
177175
// The bits 31-26 are 0b000101
178-
if ((Contents & 0xfc000000) == 0x14000000)
179-
return true;
180-
return false;
176+
return (Contents & 0xfc000000) == 0x14000000;
177+
};
178+
179+
auto IsAdr = [](uint64_t Contents) -> bool {
180+
// The bits 31-24 are 0b0xx10000
181+
return (Contents & 0x9f000000) == 0x10000000;
181182
};
182183

183184
auto IsNop = [](uint64_t Contents) -> bool { return Contents == 0xd503201f; };
@@ -205,7 +206,7 @@ bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
205206
}
206207
}
207208

208-
// The ld might replace load/store instruction with jump and
209+
// The linker might replace load/store instruction with jump and
209210
// veneer due to errata 843419
210211
// https://documentation-service.arm.com/static/5fa29fddb209f547eebd361d
211212
// Thus load/store relocations for these instructions must be ignored
@@ -223,6 +224,18 @@ bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
223224
}
224225
}
225226

227+
// The linker might relax ADRP+ADD or ADRP+LDR sequences to the ADR+NOP
228+
switch (Type) {
229+
default:
230+
break;
231+
case ELF::R_AARCH64_ADR_PREL_PG_HI21:
232+
case ELF::R_AARCH64_ADD_ABS_LO12_NC:
233+
case ELF::R_AARCH64_ADR_GOT_PAGE:
234+
case ELF::R_AARCH64_LD64_GOT_LO12_NC:
235+
if (IsAdr(Contents))
236+
return true;
237+
}
238+
226239
return false;
227240
}
228241

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
--- !ELF
2+
FileHeader:
3+
Class: ELFCLASS64
4+
Data: ELFDATA2LSB
5+
Type: ET_DYN
6+
Machine: EM_AARCH64
7+
Entry: 0x10364
8+
ProgramHeaders:
9+
- Type: PT_PHDR
10+
Flags: [ PF_R ]
11+
VAddr: 0x40
12+
Align: 0x8
13+
- Type: PT_INTERP
14+
Flags: [ PF_R ]
15+
FirstSec: .interp
16+
LastSec: .interp
17+
VAddr: 0x238
18+
- Type: PT_LOAD
19+
Flags: [ PF_R ]
20+
FirstSec: .interp
21+
LastSec: .dynamic
22+
Align: 0x10000
23+
- Type: PT_LOAD
24+
Flags: [ PF_X, PF_R ]
25+
FirstSec: .text
26+
LastSec: .text
27+
VAddr: 0x10348
28+
Align: 0x10000
29+
- Type: PT_LOAD
30+
Flags: [ PF_W, PF_R ]
31+
FirstSec: .dynamic
32+
LastSec: .got
33+
VAddr: 0x20388
34+
Align: 0x10000
35+
- Type: PT_DYNAMIC
36+
Flags: [ PF_W, PF_R ]
37+
FirstSec: .dynamic
38+
LastSec: .dynamic
39+
VAddr: 0x20388
40+
Align: 0x8
41+
- Type: PT_GNU_RELRO
42+
Flags: [ PF_R ]
43+
FirstSec: .dynamic
44+
LastSec: .got
45+
VAddr: 0x20388
46+
- Type: PT_GNU_STACK
47+
Flags: [ PF_W, PF_R ]
48+
Align: 0x0
49+
Sections:
50+
- Name: .interp
51+
Type: SHT_PROGBITS
52+
Flags: [ SHF_ALLOC ]
53+
Address: 0x238
54+
AddressAlign: 0x1
55+
Content: 2F6C69622F6C642D6C696E75782D616172636836342E736F2E3100
56+
- Name: .dynsym
57+
Type: SHT_DYNSYM
58+
Flags: [ SHF_ALLOC ]
59+
Address: 0x258
60+
Link: .dynstr
61+
AddressAlign: 0x8
62+
- Name: .dynstr
63+
Type: SHT_STRTAB
64+
Flags: [ SHF_ALLOC ]
65+
Address: 0x28C
66+
AddressAlign: 0x1
67+
- Name: .rela.dyn
68+
Type: SHT_RELA
69+
Flags: [ SHF_ALLOC ]
70+
Address: 0x290
71+
Link: .dynsym
72+
AddressAlign: 0x8
73+
Relocations:
74+
- Offset: 0x20448
75+
Type: R_AARCH64_RELATIVE
76+
Addend: 66432
77+
- Name: .text
78+
Type: SHT_PROGBITS
79+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
80+
Address: 0x10348
81+
AddressAlign: 0x4
82+
Content: FF4300D1E00700F9E80740F908014092E003082AFF430091C0035FD6FD7BBFA9FD0300911F2003D580000010F5FFFF97FD7BC1A8C0035FD6C0035FD6
83+
- Name: .dynamic
84+
Type: SHT_DYNAMIC
85+
Flags: [ SHF_WRITE, SHF_ALLOC ]
86+
Address: 0x20388
87+
Link: .dynstr
88+
AddressAlign: 0x8
89+
Entries:
90+
- Tag: DT_FLAGS_1
91+
Value: 0x8000000
92+
- Tag: DT_RELA
93+
Value: 0x290
94+
- Tag: DT_RELASZ
95+
Value: 0x18
96+
- Tag: DT_RELAENT
97+
Value: 0x18
98+
- Tag: DT_RELACOUNT
99+
Value: 0x1
100+
- Tag: DT_SYMTAB
101+
Value: 0x258
102+
- Tag: DT_SYMENT
103+
Value: 0x18
104+
- Tag: DT_STRTAB
105+
Value: 0x28C
106+
- Tag: DT_STRSZ
107+
Value: 0x1
108+
- Tag: DT_GNU_HASH
109+
Value: 0x270
110+
- Tag: DT_NULL
111+
Value: 0x0
112+
- Name: .got
113+
Type: SHT_PROGBITS
114+
Flags: [ SHF_WRITE, SHF_ALLOC ]
115+
Address: 0x20448
116+
AddressAlign: 0x8
117+
Content: '0000000000000000'
118+
- Name: .rela.text
119+
Type: SHT_RELA
120+
Flags: [ SHF_INFO_LINK ]
121+
Link: .symtab
122+
AddressAlign: 0x8
123+
Info: .text
124+
Relocations:
125+
- Offset: 0x1036C
126+
Symbol: foo2
127+
Type: R_AARCH64_ADR_GOT_PAGE
128+
- Offset: 0x10370
129+
Symbol: foo2
130+
Type: R_AARCH64_LD64_GOT_LO12_NC
131+
- Offset: 0x10374
132+
Symbol: foo
133+
Type: R_AARCH64_CALL26
134+
Symbols:
135+
- Name: .text
136+
Type: STT_SECTION
137+
Section: .text
138+
Value: 0x10348
139+
- Name: ex2.c
140+
Type: STT_FILE
141+
Index: SHN_ABS
142+
- Name: '$x.0 (1)'
143+
Section: .text
144+
Value: 0x10380
145+
- Name: .interp
146+
Type: STT_SECTION
147+
Section: .interp
148+
Value: 0x238
149+
- Name: _DYNAMIC
150+
Section: .dynamic
151+
Value: 0x20388
152+
Other: [ STV_HIDDEN ]
153+
- Name: foo
154+
Type: STT_FUNC
155+
Section: .text
156+
Binding: STB_GLOBAL
157+
Value: 0x10348
158+
Size: 0x1C
159+
- Name: _start
160+
Type: STT_FUNC
161+
Section: .text
162+
Binding: STB_GLOBAL
163+
Value: 0x10364
164+
Size: 0x1C
165+
- Name: foo2
166+
Type: STT_FUNC
167+
Section: .text
168+
Binding: STB_GLOBAL
169+
Value: 0x10380
170+
Size: 0x4
171+
DynamicSymbols: []
172+
...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test checks that the binary with relaxed ADRP+LDR instructions is
2+
// processed normally with BOLT and the ADR instruction address is recognized
3+
// normally.
4+
5+
RUN: yaml2obj %p/Inputs/skip-got-rel.yaml &> %t.exe
6+
RUN: llvm-bolt %t.exe -o /dev/null -print-cfg -print-only=_start | FileCheck %s
7+
8+
CHECK: adr x0, foo2

bolt/test/runtime/plt-lld.test

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// This test checks that the pointers to PLT are properly updated.
22
// The test is using lld linker.
33

4-
// RUN: %clang %cflags -no-pie %p/../Inputs/plt.c -fuse-ld=lld \
5-
// RUN: -o %t.lld.exe -Wl,-q
6-
// RUN: llvm-bolt %t.lld.exe -o %t.lld.bolt.exe -use-old-text=0 -lite=0
7-
// RUN: %t.lld.bolt.exe | FileCheck %s
4+
// Non-PIE:
5+
RUN: %clang %cflags -no-pie %p/../Inputs/plt.c -fuse-ld=lld \
6+
RUN: -o %t.lld.exe -Wl,-q
7+
RUN: llvm-bolt %t.lld.exe -o %t.lld.bolt.exe -use-old-text=0 -lite=0
8+
RUN: %t.lld.bolt.exe | FileCheck %s
89

9-
// CHECK: Test completed
10+
// PIE:
11+
RUN: %clang %cflags -fPIC -pie %p/../Inputs/plt.c -fuse-ld=lld \
12+
RUN: -o %t.lld.pie.exe -Wl,-q
13+
RUN: llvm-bolt %t.lld.pie.exe -o %t.lld.bolt.pie.exe -use-old-text=0 -lite=0
14+
RUN: %t.lld.bolt.pie.exe | FileCheck %s
15+
16+
CHECK: Test completed

0 commit comments

Comments
 (0)