8000 feat: implement --orphan-handling by vishruth-thimmaiah · Pull Request #2566 · wild-linker/wild · GitHub
[go: up one dir, main page]

Skip to content

feat: implement --orphan-handling - #2566

Merged
vishruth-thimmaiah merged 14 commits into
wild-linker:mainfrom
vishruth-thimmaiah:orphan_handling
Sep 28, 2026
Merged

vishruth-thimmaiah merged 14 commits into
wild-linker:mainfrom
vishruth-thimmaiah:orphan_handling

Conversation

@vishruth-thimmaiah
Copy link
Copy Markdown
Member

--orphan-handling is used to handle sections not explicitly defined by linker scripts. It accepts four options,

place is the default, it inserts a section not defined by a linker script implicitly.
warn just emits a warning and continues.
error will emit an error and exit early.
discard will skip the section.

@davidlattimore davidlattimore left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here's a few tests that fail. If any are too hard to fix now and/or you don't think they're important, just say.

index 80c1cbb6b3..2bfe2199b8 100644
--- a/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c
+++ b/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c
@@ -1,5 +1,4 @@
-//#AbstractConfig:default
-//#LinkerScript:linker-script-orphan-handling.ld
+//#AbstractConfig:common
 //#Object:runtime.c
 // RISC-V: BFD complains about missing __global_pointer$ (defined in the default linker script)
 //#SkipArch:riscv64
@@ -8,6 +7,9 @@
 //#DiffIgnore:segment.LOAD.RW.alignment
 //#DiffIgnore:segment.LOAD.RWX.alignment
 
+//#AbstractConfig:default:common
+//#LinkerScript:linker-script-orphan-handling.ld
+
 //#Config:orphan-place:default
 //#LinkArgs:--orphan-handling=place
 //#Contains:.orphan_section
@@ -24,8 +26,55 @@
 //#LinkArgs:--orphan-handling=discard
 //#DoesNotContain:.orphan_section
 
+//#Config:orphan-debug-place:default
+//#CompArgs:-g
+//#LinkArgs:--orphan-handling=place
+//#ExpectSection:.debug_info
+
+//#Config:orphan-debug-discard:default
+//#CompArgs:-g
+//#LinkArgs:--orphan-handling=discard
+//#NoSection:.debug_info
+
+//#AbstractConfig:eh-frame:default
+//#CompArgs:-fasynchronous-unwind-tables -DNO_ORPHAN_VAR
+
+//#Config:orphan-eh-frame-place:eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=place
+//#ExpectSection:.eh_frame
+
+//#Config:orphan-eh-frame-warn:eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=warn
+//#ExpectWarning:orphan section .\.eh_frame'
+
+//#Config:orphan-eh-frame-error:eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=error
+//#ExpectError:unplaced orphan section .\.eh_frame'
+
+//#Config:orphan-eh-frame-discard:eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=discard
+//#NoSection:.eh_frame
+
+//#AbstractConfig:partial:common
+//#RunEnabled:false
+//#DiffEnabled:false
+
+//#Config:orphan-partial-place:partial
+//#LinkArgs:-r --orphan-handling=place
+//#ExpectSection:.orphan_section
+
+//#Config:orphan-partial-warn:partial
+//#LinkArgs:-r --orphan-handling=warn
+//#ExpectWarning:orphan section .\.orphan_section'
+
+//#Config:orphan-partial-error:partial
+//#LinkArgs:-r --orphan-handling=error
+//#ExpectError:unplaced orphan section .\.orphan_section'
+
 #include "../common/runtime.h"
 
+#ifndef NO_ORPHAN_VAR
 __attribute__((section(".orphan_section"), retain, used)) int orphan_var = 42;
+#endif
 
 void _start(void) { exit_syscall(42); }

Comment thread libwild/src/args/elf.rs Resolved
@vishruth-thimmaiah
Copy link
Copy Markdown
Member Author

@davidlattimore should we print multiple errors for each missing section when --orphan-handling=error is passed, similar to bfd/lld, or should we just print a single error message for the first missing section? The commit I've pushed adds support for printing multiple errors.

@davidlattimore davidlattimore left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reporting multiple errors is definitely something I think we want to address. It's possible that it might be better done in a separate PR either before or after this one, but I don't feel strongly, so if you want to keep it in this PR or partially handle it in this PR, that's also fine.

Here's a few new tests that fail. Possibly the no-script variants shouldn't be in a test with "linker-script" in its name, so if you keep these, then maybe the whole test should be renamed to just "orphan-handling".

diff --git a/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c b/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c
index f0009673d3..3cc7b464cd 100644
--- a/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c
+++ b/wild/tests/sources/elf/linker-script-orphan-handling/linker-script-orphan-handling.c
@@ -61,6 +61,26 @@
 //#LinkArgs:--no-gc-sections --orphan-handling=discard
 //#ExpectSection:.eh_frame
 
+//#AbstractConfig:filtered-eh-frame:common
+//#LinkerScript:orphan-handling-filtered-ehframe.ld
+//#CompArgs:-fasynchronous-unwind-tables -DNO_ORPHAN_VAR
+
+//#Config:filtered-eh-frame-place:filtered-eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=place
+//#ExpectSection:.eh_frame
+
+//#Config:filtered-eh-frame-warn:filtered-eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=warn
+//#ExpectWarning:orphan section .\.eh_frame'
+
+//#Config:filtered-eh-frame-error:filtered-eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=error
+//#ExpectError:unplaced orphan section .\.eh_frame'
+
+//#Config:filtered-eh-frame-discard:filtered-eh-frame
+//#LinkArgs:--no-gc-sections --orphan-handling=discard
+//#NoSection:.eh_frame
+
 //#AbstractConfig:partial:common
 //#RunEnabled:false
 //#DiffEnabled:false
@@ -77,6 +97,26 @@
 //#LinkArgs:-r --orphan-handling=error
 //#ExpectError:unplaced orphan section .\.orphan_section'
 
+//#Config:orphan-partial-discard:partial
+//#LinkArgs:-r --orphan-handling=discard
+//#NoSection:.orphan_section
+
+//#Config:no-script-place:common
+//#LinkArgs:--orphan-handling=place
+//#ExpectSection:.orphan_section
+
+//#Config:no-script-warn:common
+//#LinkArgs:--orphan-handling=warn
+//#ExpectWarning:orphan section .\.orphan_section'
+
+//#Config:no-script-error:common
+//#LinkArgs:--orphan-handling=error
+//#ExpectError:unplaced orphan section .\.orphan_section'
+
+//#Config:no-script-discard:common
+//#LinkArgs:--orphan-handling=discard
+//#NoSection:.orphan_section
+
 #include "../common/runtime.h"
 
 #ifndef NO_ORPHAN_VAR
diff --git a/wild/tests/sources/elf/linker-script-orphan-handling/orphan-handling-filtered-ehframe.ld b/wild/tests/sources/elf/linker-script-orphan-handling/orphan-handling-filtered-ehframe.ld
new file mode 100644
index 0000000000..3d83f2b950
--- /dev/null
+++ b/wild/tests/sources/elf/linker-script-orphan-handling/orphan-handling-filtered-ehframe.ld
@@ -0,0 +1,18 @@
+ENTRY(_start)
+
+SECTIONS {
+    . = 0x600000;
+    .eh_frame : {
+        *not-an-input*.o(.eh_frame)
+    }
+    .text : {
+        *(.text*)
+    }
+    . = 0x800000;
+    .data : {
+        *(.data*)
+    }
+    .bss : {
+        *(.bss*)
+    }
+}

Comment thread libwild/src/error.rs Resolved
@davidlattimore davidlattimore left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like there's no test for --orphan-handling=error when all sections are mapped. That case fails because of the null section when it shouldn't. Here's a test that shows it:

diff --git a/wild/tests/sources/elf/orphan-handling/orphan-handling.c b/wild/tests/sources/elf/orphan-handling/orphan-handling.c
index bc2974ef1e..22ee04eaf4 100644
--- a/wild/tests/sources/elf/orphan-handling/orphan-handling.c
+++ b/wild/tests/sources/elf/orphan-handling/orphan-handling.c
@@ -113,6 +113,10 @@
 //#LinkArgs:--orphan-handling=error
 //#ExpectError:unplaced orphan section .\.orphan_section'
 
+//#Config:no-orphans-error:common
+//#CompArgs:-DNO_ORPHAN_VAR
+//#LinkArgs:--orphan-handling=error
+
 //#Config:no-script-discard:common
 //#LinkArgs:--orphan-handling=discard
 //#NoSection:.orphan_section

Comment thread libwild/src/resolution.rs Outdated
&& check_orphan_placement::<P>(
args,
&obj.common.input,
&String::from_utf8_lossy(section_name),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm seeing a 20% increase in wall time for the "Resolve sections" phase. I suspect it's because we're doing UTF-8 validation for all sections.

Comment thread libwild/src/resolution.rs Outdated
.lookup::<P>(section_name, file_name, input_section)
};

if layout_rules.is_orphan::<P>(section_name, file_name, input_section)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like the slowdown in section resolution is still happening. I guess it wasn't utf-8 validation, but rather this call to is_orphan. If I add args.orphan_handling() != OrphanHandling::Place && to the start of this if-statement, the section resolution slowdown seems to go away.

Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
@vishruth-thimmaiah
vishruth-thimmaiah merged commit 322922e into wild-linker:main Sep 28, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0