Skip to content

Commit 1981b1b

Browse files
[ELF] Demote symbols in /DISCARD/ discarded sections to Undefined (#69295)
When an input section is matched by /DISCARD/ in a linker script, GNU ld reports errors for relocations referencing symbols defined in the section: `.aaa' referenced in section `.bbb' of a.o: defined in discarded section `.aaa' of a.o Implement the error by demoting eligible symbols to `Undefined` and changing STB_WEAK to STB_GLOBAL. As a side benefit, in relocatable links, relocations referencing symbols defined relative to /DISCARD/ discarded sections no longer set symbol/type to zeros. It's arguable whether a weak reference to a discarded symbol should lead to errors. GNU ld reports an error and our demoting approach reports an error as well. Close #58891 Co-authored-by: Bevin Hansson <[email protected]>
1 parent ef0e0ad commit 1981b1b

File tree

8 files changed

+103
-23
lines changed

8 files changed

+103
-23
lines changed

lld/ELF/LinkerScript.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ void LinkerScript::processSectionCommands() {
613613
discard(*s);
614614
discardSynthetic(*osec);
615615
osec->commands.clear();
616+
seenDiscard = true;
616617
return false;
617618
}
618619

lld/ELF/LinkerScript.h

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class LinkerScript final {
356356

357357
bool hasSectionsCommand = false;
358358
bool seenDataAlign = false;
359+
bool seenDiscard = false;
359360
bool seenRelroEnd = false;
360361
bool errorOnMissingSection = false;
361362
std::string backwardDotErr;

lld/ELF/MapFile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static void writeCref(raw_fd_ostream &os) {
229229
if (isa<SharedSymbol>(sym))
230230
map[sym].insert(file);
231231
if (auto *d = dyn_cast<Defined>(sym))
232-
if (!d->isLocal() && (!d->section || d->section->isLive()))
232+
if (!d->isLocal())
233233
map[d].insert(file);
234234
}
235235
}

lld/ELF/Relocations.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
507507
template <class ELFT>
508508
static std::string maybeReportDiscarded(Undefined &sym) {
509509
auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
510-
if (!file || !sym.discardedSecIdx ||
511-
file->getSections()[sym.discardedSecIdx] != &InputSection::discarded)
510+
if (!file || !sym.discardedSecIdx)
512511
return "";
513512
ArrayRef<typename ELFT::Shdr> objSections =
514513
file->template getELFShdrs<ELFT>();
@@ -1575,7 +1574,8 @@ template <class ELFT> void elf::scanRelocations() {
15751574
scanner.template scanSection<ELFT>(*sec);
15761575
if (part.armExidx && part.armExidx->isLive())
15771576
for (InputSection *sec : part.armExidx->exidxSections)
1578-
scanner.template scanSection<ELFT>(*sec);
1577+
if (sec->isLive())
1578+
scanner.template scanSection<ELFT>(*sec);
15791579
}
15801580
});
15811581
}

lld/ELF/Symbols.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,13 @@ void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
316316
if (!config->warnSymbolOrdering)
317317
return;
318318

319-
// If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
320-
// is emitted. It makes sense to not warn on undefined symbols.
319+
// If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning is
320+
// emitted. It makes sense to not warn on undefined symbols (excluding those
321+
// demoted by demoteSymbols).
321322
//
322323
// Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
323324
// but we don't have to be compatible here.
324-
if (sym->isUndefined() &&
325+
if (sym->isUndefined() && !cast<Undefined>(sym)->discardedSecIdx &&
325326
config->unresolvedSymbols == UnresolvedPolicy::Ignore)
326327
return;
327328

@@ -330,9 +331,12 @@ void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
330331

331332
auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
332333

333-
if (sym->isUndefined())
334-
report(": unable to order undefined symbol: ");
335-
else if (sym->isShared())
334+
if (sym->isUndefined()) {
335+
if (cast<Undefined>(sym)->discardedSecIdx)
336+
report(": unable to order discarded symbol: ");
337+
else
338+
report(": unable to order undefined symbol: ");
339+
} else if (sym->isShared())
336340
report(": unable to order shared symbol: ");
337341
else if (d && !d->section)
338342
report(": unable to order absolute symbol: ");

lld/ELF/Writer.cpp

+45-6
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,59 @@ void elf::addReservedSymbols() {
251251
ElfSym::edata2 = add("_edata", -1);
252252
}
253253

254+
static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
255+
if (map.empty())
256+
for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))
257+
map.try_emplace(sec, i);
258+
// Change WEAK to GLOBAL so that if a scanned relocation references sym,
259+
// maybeReportUndefined will report an error.
260+
uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
261+
Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
262+
/*discardedSecIdx=*/map.lookup(sym.section))
263+
.overwrite(sym);
264+
}
265+
254266
// If all references to a DSO happen to be weak, the DSO is not added to
255267
// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
256268
// dangling references to an unneeded DSO. Use a weak binding to avoid
257269
// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
270+
//
271+
// In addition, demote symbols defined in discarded sections, so that
272+
// references to /DISCARD/ discarded symbols will lead to errors.
258273
static void demoteSymbolsAndComputeIsPreemptible() {
259274
llvm::TimeTraceScope timeScope("Demote symbols");
275+
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
260276
for (Symbol *sym : symtab.getSymbols()) {
261-
auto *s = dyn_cast<SharedSymbol>(sym);
262-
if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
263-
uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
264-
Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
265-
.overwrite(*sym);
266-
sym->versionId = VER_NDX_GLOBAL;
277+
if (auto *d = dyn_cast<Defined>(sym)) {
278+
if (d->section && !d->section->isLive())
279+
demoteDefined(*d, sectionIndexMap[d->file]);
280+
} else {
281+
auto *s = dyn_cast<SharedSymbol>(sym);
282+
if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
283+
uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
284+
Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
285+
.overwrite(*sym);
286+
sym->versionId = VER_NDX_GLOBAL;
287+
}
267288
}
268289

269290
if (config->hasDynSymTab)
270291
sym->isPreemptible = computeIsPreemptible(*sym);
271292
}
272293
}
273294

295+
static void demoteLocalSymbolsInDiscardedSections() {
296+
llvm::TimeTraceScope timeScope("Demote local symbols");
297+
parallelForEach(ctx.objectFiles, [&](ELFFileBase *file) {
298+
DenseMap<SectionBase *, size_t> sectionIndexMap;
299+
for (Symbol *sym : file->getLocalSymbols()) {
300+
Defined *d = dyn_cast<Defined>(sym);
301+
if (d && d->section && !d->section->isLive())
302+
demoteDefined(*d, sectionIndexMap);
303+
}
304+
});
305+
}
306+
274307
// Fully static executables don't support MTE globals at this point in time, as
275308
// we currently rely on:
276309
// - A dynamic loader to process relocations, and
@@ -1958,6 +1991,12 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
19581991
}
19591992

19601993
demoteSymbolsAndComputeIsPreemptible();
1994+
// Also demote local symbols defined relative to discarded input sections so
1995+
// that relocations referencing them will lead to errors. To avoid unneeded
1996+
// work, we only do this when /DISCARD/ is seen, but this demotation also
1997+
// applies to --gc-sections discarded sections.
1998+
if (script->seenDiscard)
1999+
demoteLocalSymbolsInDiscardedSections();
19612000

19622001
// Change values of linker-script-defined symbols from placeholders (assigned
19632002
// by declareSymbols) to actual definitions.

lld/test/ELF/gc-sections-tls.s

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
# ERR: error: {{.*}}.o has an STT_TLS symbol but doesn't have an SHF_TLS section
99

10+
## TODO As a corner case, when /DISCARD/ is present, demoteLocalSymbolsInDiscardedSections
11+
## demotes tls and the error is not triggered.
12+
# RUN: echo 'SECTIONS { /DISCARD/ : {} }' > %t.lds
13+
# RUN: ld.lld %t.o --gc-sections -T %t.lds -o /dev/null
14+
1015
## If we happen to have a PT_TLS, we will resolve the relocation to
1116
## an arbitrary value (current implementation uses a negative value).
1217
# RUN: echo '.section .tbss,"awT"; .globl root; root: .long 0' | \
@@ -17,6 +22,9 @@
1722
# CHECK: Hex dump of section '.noalloc':
1823
# CHECK-NEXT: 0x00000000 {{[0-9a-f]+}} ffffffff
1924

25+
.globl _start
26+
_start:
27+
2028
.section .tbss,"awT",@nobits
2129
tls:
2230
.long 0

lld/test/ELF/linkerscript/discard-section.s

+34-7
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,55 @@
44
# RUN: rm -rf %t && split-file %s %t && cd %t
55
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
66
# RUN: llvm-mc -filetype=obj -triple=x86_64 b.s -o b.o
7-
# RUN: ld.lld -T a.lds a.o b.o -z undefs -o /dev/null 2>&1 | count 0
8-
# RUN: ld.lld -T a.lds a.o b.o -o /dev/null 2>&1 | count 0
9-
# RUN: ld.lld -r -T a.lds a.o b.o -o a.ro 2>&1 | count 0
7+
# RUN: not ld.lld --threads=1 -T a.lds a.o b.o -z undefs -o /dev/null 2>&1 | FileCheck %s --check-prefix=LOCAL --implicit-check-not=error:
8+
# RUN: not ld.lld --threads=1 -T a.lds a.o b.o -o /dev/null 2>&1 | FileCheck %s --check-prefixes=LOCAL,NONLOCAL --implicit-check-not=error:
9+
# RUN: ld.lld -r -T a.lds a.o b.o -o a.ro 2>&1 | FileCheck %s --check-prefix=WARNING --implicit-check-not=warning:
1010
# RUN: llvm-readelf -r -s a.ro | FileCheck %s --check-prefix=RELOC
1111

12+
# LOCAL: error: relocation refers to a discarded section: .aaa
13+
# LOCAL-NEXT: >>> defined in a.o
14+
# LOCAL-NEXT: >>> referenced by a.o:(.bbb+0x0)
15+
16+
# NONLOCAL: error: relocation refers to a symbol in a discarded section: global
17+
# NONLOCAL-NEXT: >>> defined in a.o
18+
# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x0)
19+
20+
# NONLOCAL: error: relocation refers to a symbol in a discarded section: weak
21+
# NONLOCAL-NEXT: >>> defined in a.o
22+
# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x8)
23+
24+
# NONLOCAL: error: relocation refers to a symbol in a discarded section: weakref1
25+
# NONLOCAL-NEXT: >>> defined in a.o
26+
# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x10)
27+
28+
# NONLOCAL: error: relocation refers to a symbol in a discarded section: weakref2
29+
# NONLOCAL-NEXT: >>> defined in a.o
30+
# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x18)
31+
32+
# WARNING: warning: relocation refers to a discarded section: .aaa
33+
# WARNING-NEXT: >>> referenced by a.o:(.rela.bbb+0x0)
34+
1235
# RELOC: Relocation section '.rela.bbb' at offset {{.*}} contains 1 entries:
1336
# RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
1437
# RELOC-NEXT: 0000000000000000 0000000000000000 R_X86_64_NONE 0
1538
# RELOC-EMPTY:
1639
# RELOC-NEXT: Relocation section '.rela.data' at offset {{.*}} contains 4 entries:
1740
# RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
18-
# RELOC-NEXT: 0000000000000000 0000000000000001 R_X86_64_64 0
19-
# RELOC-NEXT: 0000000000000008 0000000000000001 R_X86_64_64 0
20-
# RELOC-NEXT: 0000000000000010 0000000000000001 R_X86_64_64 0
21-
# RELOC-NEXT: 0000000000000018 0000000000000001 R_X86_64_64 0
41+
# RELOC-NEXT: 0000000000000000 0000000500000001 R_X86_64_64 0000000000000000 global + 0
42+
# RELOC-NEXT: 0000000000000008 0000000700000001 R_X86_64_64 0000000000000000 weak + 0
43+
# RELOC-NEXT: 0000000000000010 0000000600000001 R_X86_64_64 0000000000000000 weakref1 + 0
44+
# RELOC-NEXT: 0000000000000018 0000000800000001 R_X86_64_64 0000000000000000 weakref2 + 0
2245

2346
# RELOC: Num: Value Size Type Bind Vis Ndx Name
2447
# RELOC-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
2548
# RELOC-NEXT: 1: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text
2649
# RELOC-NEXT: 2: 0000000000000000 0 SECTION LOCAL DEFAULT 2 .bbb
2750
# RELOC-NEXT: 3: 0000000000000000 0 SECTION LOCAL DEFAULT 4 .data
2851
# RELOC-NEXT: 4: 0000000000000000 0 NOTYPE GLOBAL DEFAULT 1 _start
52+
# RELOC-NEXT: 5: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND global
53+
# RELOC-NEXT: 6: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weakref1
54+
# RELOC-NEXT: 7: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weak
55+
# RELOC-NEXT: 8: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND weakref2
2956
# RELOC-EMPTY:
3057

3158
#--- a.s

0 commit comments

Comments
 (0)