Skip to content

Commit 34b14cc

Browse files
authored
[lld][ELF] Suppress --orphan-handling=error/warn without SECTIONS (#93630)
Without a linker script, `--orphan-handling=error` or `=warn` reports all input sections, including even well-known sections like `.text`, `.bss`, `.dynamic`, or `.symtab`. However, in this case, no sections should be considered orphans because they all are placed with the same default rules. This patch suppresses errors/warnings for placing orphan sections if no linker script with the `SECTIONS` command is provided. The proposed behavior matches GNU gold. GNU ld in the same scenario only reports sections that are not in its default linker script, thus, it avoids complaining about `.text` and similar.
1 parent 058d429 commit 34b14cc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,8 @@ void LinkerScript::addOrphanSections() {
936936

937937
void LinkerScript::diagnoseOrphanHandling() const {
938938
llvm::TimeTraceScope timeScope("Diagnose orphan sections");
939-
if (config->orphanHandling == OrphanHandlingPolicy::Place)
939+
if (config->orphanHandling == OrphanHandlingPolicy::Place ||
940+
!hasSectionsCommand)
940941
return;
941942
for (const InputSectionBase *sec : orphanSections) {
942943
// .relro_padding is inserted before DATA_SEGMENT_RELRO_END, if present,

lld/test/ELF/linkerscript/orphan-report.s

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
# RUN: ld.lld -shared --orphan-handling=place -o %t.out --script %t.script \
1212
# RUN: %t.o 2>&1 -verbose -error-limit=0 | FileCheck %s --check-prefix=DEFAULT
1313

14+
## Check --orphan-handling=error or =warn do not report errors if no linker
15+
## script is used.
16+
# RUN: ld.lld -shared -orphan-handling=error -o /dev/null %t.o 2>&1 | count 0
17+
# RUN: ld.lld -shared -orphan-handling=warn -o /dev/null %t.o 2>&1 | count 0
18+
# RUN: ld.lld -r -orphan-handling=error -o /dev/null %t.o 2>&1 | count 0
19+
# RUN: ld.lld -r -orphan-handling=warn -o /dev/null %t.o 2>&1 | count 0
20+
1421
## Check --orphan-handling=error reports errors about orphans.
1522
# RUN: not ld.lld --orphan-handling=error -o /dev/null -T %t.script \
1623
# RUN: %t.o 2>&1 | FileCheck %s --check-prefixes=COMMON,SYMTAB

0 commit comments

Comments
 (0)