Skip to content

Commit 6503a68

Browse files
committed
[lld/mac] Don't assert when ICFing arm64 code
WordLiteralSection dedupes literals by content. WordLiteralInputSection::getOffset() used to read a literal at the passed-in offset and look up this value in the deduping map to find the offset of the deduped value. But it's possible that (e.g.) a 16-byte literal's value is accessed 4 bytes in. To get the offset at that address, we have to get the deduped value at offset 0 and then apply the offset 4 to the result. (See also WordLiteralSection::finalizeContents() which fills in those maps.) Only a problem on arm64 because in x86_64 the offset is part of the instruction instead of a separate ARM64_RELOC_ADDEND relocation. (See bug for more details.) Fixes PR51999. Differential Revision: https://reviews.llvm.org/D112584
1 parent 0a06068 commit 6503a68

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

lld/MachO/InputSection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
227227

228228
uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
229229
auto *osec = cast<WordLiteralSection>(parent);
230-
const uint8_t *buf = data.data();
230+
const uintptr_t buf = reinterpret_cast<uintptr_t>(data.data());
231231
switch (sectionType(getFlags())) {
232232
case S_4BYTE_LITERALS:
233-
return osec->getLiteral4Offset(buf + off);
233+
return osec->getLiteral4Offset(buf + (off & ~3LLU)) | (off & 3);
234234
case S_8BYTE_LITERALS:
235-
return osec->getLiteral8Offset(buf + off);
235+
return osec->getLiteral8Offset(buf + (off & ~7LLU)) | (off & 7);
236236
case S_16BYTE_LITERALS:
237-
return osec->getLiteral16Offset(buf + off);
237+
return osec->getLiteral16Offset(buf + (off & ~15LLU)) | (off & 15);
238238
default:
239239
llvm_unreachable("invalid literal section type");
240240
}

lld/MachO/SyntheticSections.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,16 @@ class WordLiteralSection final : public SyntheticSection {
562562
!literal8Map.empty();
563563
}
564564

565-
uint64_t getLiteral16Offset(const uint8_t *buf) const {
565+
uint64_t getLiteral16Offset(uintptr_t buf) const {
566566
return literal16Map.at(*reinterpret_cast<const UInt128 *>(buf)) * 16;
567567
}
568568

569-
uint64_t getLiteral8Offset(const uint8_t *buf) const {
569+
uint64_t getLiteral8Offset(uintptr_t buf) const {
570570
return literal16Map.size() * 16 +
571571
literal8Map.at(*reinterpret_cast<const uint64_t *>(buf)) * 8;
572572
}
573573

574-
uint64_t getLiteral4Offset(const uint8_t *buf) const {
574+
uint64_t getLiteral4Offset(uintptr_t buf) const {
575575
return literal16Map.size() * 16 + literal8Map.size() * 8 +
576576
literal4Map.at(*reinterpret_cast<const uint32_t *>(buf)) * 4;
577577
}

lld/test/MachO/icf-arm64.s

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# REQUIRES: aarch64
2+
# RUN: rm -rf %t; split-file %s %t
3+
4+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin19.0.0 %t/main.s -o %t/main.o
5+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin19.0.0 %t/f2.s -o %t/f2.o
6+
# RUN: %lld -arch arm64 -lSystem --icf=all -o %t/main %t/main.o %t/f2.o
7+
# RUN: llvm-objdump -d --syms --print-imm-hex %t/main | FileCheck %s
8+
9+
# CHECK-LABEL: SYMBOL TABLE:
10+
# CHECK: [[#%x,F1_REF:]] g F __TEXT,__text _f1
11+
# CHECK: [[#%x,F1_REF:]] g F __TEXT,__text _f2
12+
13+
# CHECK-LABEL: Disassembly of section __TEXT,__text:
14+
# CHECK: <_main>:
15+
# CHECK: bl 0x[[#%x,F1_REF:]]
16+
# CHECK: bl 0x[[#%x,F1_REF:]]
17+
18+
#--- main.s
19+
20+
.subsections_via_symbols
21+
22+
.literal16
23+
.p2align 3
24+
L_align16:
25+
.quad 0xffffffffffffffff
26+
.short 0xaaaa
27+
.short 0xaaaa
28+
.space 4, 0xaa
29+
30+
.literal8
31+
.p2align 3
32+
L_align8:
33+
.quad 0xeeeeeeeeeeeeeeee
34+
35+
.literal4
36+
.p2align 2
37+
L_align4:
38+
.short 0xbbbb
39+
.short 0xbbbb
40+
41+
42+
.text
43+
.p2align 2
44+
45+
.globl _main, _f1, _f2
46+
47+
## Test that loading from __literalN sections at non-literal boundaries
48+
## doesn't confuse ICF. This function should be folded with the identical
49+
## _f2 in f2 (which uses literals of the same value in a different isec).
50+
_f1:
51+
adrp x9, L_align16@PAGE + 4
52+
add x9, x9, L_align16@PAGEOFF + 4
53+
ldr x10, [x9]
54+
55+
adrp x9, L_align8@PAGE + 4
56+
add x9, x9, L_align8@PAGEOFF + 4
57+
ldr w11, [x9]
58+
59+
adrp x9, L_align4@PAGE + 2
60+
add x9, x9, L_align4@PAGEOFF + 2
61+
ldrh w12, [x9]
62+
63+
ret
64+
65+
_main:
66+
bl _f1
67+
bl _f2
68+
69+
#--- f2.s
70+
71+
.subsections_via_symbols
72+
73+
.literal16
74+
.p2align 3
75+
L_align16:
76+
.quad 0xffffffffffffffff
77+
.short 0xaaaa
78+
.short 0xaaaa
79+
.space 4, 170
80+
81+
.literal8
82+
.p2align 3
83+
L_align8:
84+
.quad 0xeeeeeeeeeeeeeeee
85+
86+
.literal4
87+
.p2align 2
88+
L_align4:
89+
.short 0xbbbb
90+
.short 0xbbbb
91+
92+
.text
93+
.p2align 2
94+
95+
.globl _f2
96+
_f2:
97+
adrp x9, L_align16@PAGE + 4
98+
add x9, x9, L_align16@PAGEOFF + 4
99+
ldr x10, [x9]
100+
101+
adrp x9, L_align8@PAGE + 4
102+
add x9, x9, L_align8@PAGEOFF + 4
103+
ldr w11, [x9]
104+
105+
adrp x9, L_align4@PAGE + 2
106+
add x9, x9, L_align4@PAGEOFF + 2
107+
ldrh w12, [x9]
108+
109+
ret

0 commit comments

Comments
 (0)