Skip to content

[lld][ELF] Suppress --orphan-handling=error/warn without SECTIONS #93630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2024

Conversation

igorkudrin
Copy link
Collaborator

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.

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.
@igorkudrin igorkudrin requested a review from MaskRay May 29, 2024 01:44
@igorkudrin igorkudrin self-assigned this May 29, 2024
@llvmbot llvmbot added the lld label May 29, 2024
@llvmbot
Copy link
Member

llvmbot commented May 29, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Igor Kudrin (igorkudrin)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/93630.diff

2 Files Affected:

  • (modified) lld/ELF/LinkerScript.cpp (+2-1)
  • (modified) lld/test/ELF/linkerscript/orphan-report.s (+11)
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 3ba59c112b8a8..94a39034d629f 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -936,7 +936,8 @@ void LinkerScript::addOrphanSections() {
 
 void LinkerScript::diagnoseOrphanHandling() const {
   llvm::TimeTraceScope timeScope("Diagnose orphan sections");
-  if (config->orphanHandling == OrphanHandlingPolicy::Place)
+  if (config->orphanHandling == OrphanHandlingPolicy::Place ||
+      !hasSectionsCommand)
     return;
   for (const InputSectionBase *sec : orphanSections) {
     // .relro_padding is inserted before DATA_SEGMENT_RELRO_END, if present,
diff --git a/lld/test/ELF/linkerscript/orphan-report.s b/lld/test/ELF/linkerscript/orphan-report.s
index 3dca23267ec64..cc7d8286b6373 100644
--- a/lld/test/ELF/linkerscript/orphan-report.s
+++ b/lld/test/ELF/linkerscript/orphan-report.s
@@ -11,6 +11,17 @@
 # RUN: ld.lld -shared --orphan-handling=place -o %t.out --script %t.script \
 # RUN:   %t.o 2>&1 -verbose  -error-limit=0 | FileCheck %s --check-prefix=DEFAULT
 
+## Check --orphan-handling=error or =warn do not report errors if no linker
+## script is used.
+# RUN: ld.lld -shared -orphan-handling=error -o /dev/null %t.o 2>&1 | \
+# RUN:   FileCheck %s --allow-empty --check-prefix=DEFAULT
+# RUN: ld.lld -shared -orphan-handling=warn -o /dev/null %t.o 2>&1 | \
+# RUN:   FileCheck %s --allow-empty --check-prefix=DEFAULT
+# RUN: ld.lld -r -orphan-handling=error -o /dev/null %t.o 2>&1 | \
+# RUN:   FileCheck %s --allow-empty --check-prefix=DEFAULT
+# RUN: ld.lld -r -orphan-handling=warn -o /dev/null %t.o 2>&1 | \
+# RUN:   FileCheck %s --allow-empty --check-prefix=DEFAULT
+
 ## Check --orphan-handling=error reports errors about orphans.
 # RUN: not ld.lld --orphan-handling=error -o /dev/null -T %t.script \
 # RUN:   %t.o 2>&1 | FileCheck %s --check-prefixes=COMMON,SYMTAB

@@ -11,6 +11,17 @@
# RUN: ld.lld -shared --orphan-handling=place -o %t.out --script %t.script \
# RUN: %t.o 2>&1 -verbose -error-limit=0 | FileCheck %s --check-prefix=DEFAULT

## Check --orphan-handling=error or =warn do not report errors if no linker
## script is used.
# RUN: ld.lld -shared -orphan-handling=error -o /dev/null %t.o 2>&1 | \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace FileCheck with | count 0

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

@igorkudrin igorkudrin merged commit 34b14cc into llvm:main May 29, 2024
7 checks passed
@igorkudrin igorkudrin deleted the orphan-handling-no-script-no-error branch May 29, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants