Skip to content

Commit 624ea34

Browse files
authored
[lld/MachO] Fix assert on unsorted data-in-code entries (#81758)
When the data-in-code entries are in separate sections, they are not guaranteed to be sorted. In particular, 68b1cc36f3df marked some libc++ string functions as noinline, which leads to global ctors involving strings now producing data-in-code sections in __TEXT,__StaticInit, which is why this now happens in practice. Since data-in-code entries are relatively rare and small, just sort them. No observed performance impact. See also crbug.com/41487860
1 parent c2fea4c commit 624ea34

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lld/MachO/SyntheticSections.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,10 +1050,13 @@ static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
10501050
if (entries.empty())
10511051
continue;
10521052

1053-
assert(is_sorted(entries, [](const data_in_code_entry &lhs,
1053+
std::vector<MachO::data_in_code_entry> sortedEntries;
1054+
sortedEntries.assign(entries.begin(), entries.end());
1055+
llvm::sort(sortedEntries, [](const data_in_code_entry &lhs,
10541056
const data_in_code_entry &rhs) {
10551057
return lhs.offset < rhs.offset;
1056-
}));
1058+
});
1059+
10571060
// For each code subsection find 'data in code' entries residing in it.
10581061
// Compute the new offset values as
10591062
// <offset within subsection> + <subsection address> - <__TEXT address>.
@@ -1066,12 +1069,12 @@ static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
10661069
continue;
10671070
const uint64_t beginAddr = section->addr + subsec.offset;
10681071
auto it = llvm::lower_bound(
1069-
entries, beginAddr,
1072+
sortedEntries, beginAddr,
10701073
[](const MachO::data_in_code_entry &entry, uint64_t addr) {
10711074
return entry.offset < addr;
10721075
});
10731076
const uint64_t endAddr = beginAddr + isec->getSize();
1074-
for (const auto end = entries.end();
1077+
for (const auto end = sortedEntries.end();
10751078
it != end && it->offset + it->length <= endAddr; ++it)
10761079
dataInCodeEntries.push_back(
10771080
{static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) -

lld/test/MachO/data-in-code.s

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/bar.s -o %t/bar.o
77
# RUN: %lld -lSystem %t/foo.o %t/bar.o -o %t/main.exe
88
# RUN: llvm-otool -l %t/main.exe > %t/objdump
9-
# RUN: llvm-objdump --macho --data-in-code %t/main.exe >> %t/objdump
9+
# RUN: llvm-otool -Gv %t/main.exe >> %t/objdump
1010
# RUN: FileCheck %s < %t/objdump
1111

1212
# CHECK-LABEL: sectname __text
@@ -18,12 +18,13 @@
1818
# CHECK-LABEL: cmd LC_DATA_IN_CODE
1919
# CHECK-NEXT: cmdsize 16
2020
# CHECK-NEXT: dataoff
21-
# CHECK-NEXT: datasize 16
21+
# CHECK-NEXT: datasize 24
2222

23-
# CHECK-LABEL: Data in code table (2 entries)
23+
# CHECK-LABEL: Data in code table (3 entries)
2424
# CHECK-NEXT: offset length kind
2525
# CHECK-NEXT: [[#%x,TEXT + 28]] 24 JUMP_TABLE32
26-
# CHECK-NEXT: [[#%x,TEXT + 68]] 12 JUMP_TABLE32
26+
# CHECK-NEXT: [[#%x,TEXT + 68]] 8 JUMP_TABLE32
27+
# CHECK-NEXT: [[#%x,TEXT + 84]] 12 JUMP_TABLE32
2728

2829
# RUN: %lld -lSystem %t/foo.o %t/bar.o -no_data_in_code_info -o %t/main.exe
2930
# RUN: llvm-otool -l %t/main.exe | FileCheck --check-prefix=OMIT %s
@@ -32,11 +33,22 @@
3233

3334
# RUN: %lld -lSystem %t/foo.o %t/bar.o -no_data_in_code_info -data_in_code_info -o %t/main.exe
3435
# RUN: llvm-otool -l %t/main.exe > %t/objdump
35-
# RUN: llvm-objdump --macho --data-in-code %t/main.exe >> %t/objdump
36+
# RUN: llvm-otool -Gv %t/main.exe >> %t/objdump
3637
# RUN: FileCheck %s < %t/objdump
3738

3839
#--- foo.s
3940
.text
41+
.section __TEXT,__StaticInit,regular,pure_instructions
42+
.p2align 4, 0x90
43+
_some_init_function:
44+
retq
45+
.p2align 2, 0x90
46+
.data_region jt32
47+
.long 0
48+
.long 0
49+
.end_data_region
50+
51+
.section __TEXT,__text,regular,pure_instructions
4052
.globl _main
4153
.p2align 4, 0x90
4254
_main:

0 commit comments

Comments
 (0)