Skip to content

Commit 0930f62

Browse files
committed
[ELF] -r: fix crash when SHF_LINK_ORDER linked-to section has a larger index
Fixes: b8dface ThinLTO asan build may place `asan_globals` before the associated `.bss.xxx` section. `rel->getOutputSection()` is nullptr because `rel->parent` hasn't been set, leading to a crash. Simplify return `s->name` in this case.
1 parent b2b4ffb commit 0930f62

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
5757
if (auto *isec = dyn_cast<InputSection>(s)) {
5858
if (InputSectionBase *rel = isec->getRelocatedSection()) {
5959
OutputSection *out = rel->getOutputSection();
60+
if (!out) {
61+
assert(config->relocatable && (rel->flags & SHF_LINK_ORDER));
62+
return s->name;
63+
}
6064
if (s->type == SHT_RELA)
6165
return saver().save(".rela" + out->name);
6266
return saver().save(".rel" + out->name);

lld/test/ELF/linkorder-group.test

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# REQUIRES: x86
2+
## Test SHF_LINK_ORDER when the linked-to section has a larger index.
3+
# RUN: yaml2obj %s -o %t.o
4+
# RUN: ld.lld -r %t.o -o %t.ro
5+
# RUN: llvm-readelf -x asan_globals %t.ro | FileCheck %s
6+
7+
# CHECK: Hex dump of section 'asan_globals':
8+
# CHECK-NEXT: 0x00000000 00 .
9+
10+
--- !ELF
11+
FileHeader:
12+
Class: ELFCLASS64
13+
Data: ELFDATA2LSB
14+
Type: ET_REL
15+
Machine: EM_X86_64
16+
SectionHeaderStringTable: .strtab
17+
Sections:
18+
- Name: .text
19+
Type: SHT_PROGBITS
20+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
21+
- Name: .bss
22+
Type: SHT_NOBITS
23+
Flags: [ SHF_WRITE, SHF_ALLOC, SHF_GROUP ]
24+
Size: 0x1
25+
- Name: asan_globals
26+
Type: SHT_PROGBITS
27+
Flags: [ SHF_ALLOC, SHF_LINK_ORDER, SHF_GROUP ]
28+
Link: .bss
29+
Content: '00'
30+
- Name: .group
31+
Type: SHT_GROUP
32+
Link: .symtab
33+
Info: foo
34+
Members:
35+
- SectionOrType: GRP_COMDAT
36+
- SectionOrType: .bss
37+
- SectionOrType: asan_globals
38+
- SectionOrType: .relaasan_globals
39+
- Name: .relaasan_globals
40+
Type: SHT_RELA
41+
Flags: [ SHF_GROUP ]
42+
Link: .symtab
43+
Info: asan_globals
44+
Relocations:
45+
- Type: R_X86_64_NONE
46+
- Type: SectionHeaderTable
47+
Sections:
48+
- Name: .strtab
49+
- Name: .text
50+
- Name: .group
51+
- Name: asan_globals
52+
- Name: .relaasan_globals
53+
- Name: .bss
54+
- Name: .symtab
55+
Symbols:
56+
- Name: foo
57+
Section: .bss
58+
Binding: STB_WEAK

0 commit comments

Comments
 (0)