Skip to content

Commit 47eb3f1

Browse files
committed
[ELF] Ensure output section is not discarded in addStartEndSymbols()
Fixes https://bugs.llvm.org/show_bug.cgi?id=52534. Differential Revision: https://reviews.llvm.org/D114179
1 parent 812e64e commit 47eb3f1

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ void LinkerScript::assignOffsets(OutputSection *sec) {
10341034
}
10351035
}
10361036

1037-
static bool isDiscardable(OutputSection &sec) {
1037+
static bool isDiscardable(const OutputSection &sec) {
10381038
if (sec.name == "/DISCARD/")
10391039
return true;
10401040

@@ -1063,6 +1063,11 @@ static bool isDiscardable(OutputSection &sec) {
10631063
return true;
10641064
}
10651065

1066+
bool LinkerScript::isDiscarded(const OutputSection *sec) const {
1067+
return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
1068+
isDiscardable(*sec);
1069+
}
1070+
10661071
static void maybePropagatePhdrs(OutputSection &sec,
10671072
std::vector<StringRef> &phdrs) {
10681073
if (sec.phdrs.empty()) {

lld/ELF/LinkerScript.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class LinkerScript final {
318318
void processSymbolAssignments();
319319
void declareSymbols();
320320

321+
bool isDiscarded(const OutputSection *sec) const;
322+
321323
// Used to handle INSERT AFTER statements.
322324
void processInsertCommands();
323325

lld/ELF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
22762276
Default = Out::elfHeader;
22772277

22782278
auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2279-
if (os) {
2279+
if (os && !script->isDiscarded(os)) {
22802280
addOptionalRegular(start, os, 0);
22812281
addOptionalRegular(end, os, -1);
22822282
} else {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# REQUIRES: x86
2+
# RUN: split-file %s %t
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/t.s -o %t.o
4+
5+
## PR52534: https://bugs.llvm.org/show_bug.cgi?id=52534
6+
## Check case where .preinit_array is discarded.
7+
## Link should succeed without causing an out of range relocation error.
8+
# RUN: ld.lld -T %t/discarded.script %t.o -o %t1 --image-base=0x80000000
9+
# RUN: llvm-readelf -s %t1 | FileCheck --check-prefixes=CHECK,DISCARDED %s
10+
11+
## Check case where .preinit_array is emitted but empty.
12+
# RUN: ld.lld -T %t/empty.script %t.o -o %t2
13+
# RUN: llvm-readelf -s %t2 | FileCheck --check-prefixes=CHECK,EMPTY %s
14+
15+
# CHECK: [[#%x,ADDR:]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_start
16+
# CHECK-NEXT: [[#ADDR]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_end
17+
18+
# DISCARDED-NEXT: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] _start
19+
20+
# EMPTY-NOT: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] _start
21+
# EMPTY: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] ADDR
22+
23+
#--- t.s
24+
.global _start
25+
_start:
26+
movq __preinit_array_start@GOTPCREL(%rip),%rax
27+
movq __preinit_array_end@GOTPCREL(%rip),%rax
28+
29+
#--- discarded.script
30+
SECTIONS {
31+
.text : { *(.text); }
32+
.preinit_array : { *(.preinit_array); }
33+
}
34+
35+
#--- empty.script
36+
SECTIONS {
37+
.text : { *(.text); }
38+
.preinit_array : { ADDR = .; *(.preinit_array); }
39+
}

0 commit comments

Comments
 (0)