Make -u symbols load --as-needed shared libraries
The linker now correctly treats command-line -u symbols as references to load --as-needed shared libraries.
This change enhances the linker’s handling of the --as-needed option for shared libraries. Previously, an explicit symbol reference (-u symbol) on the command line would not cause an --as-needed shared library to be loaded if it wasn’t otherwise required. This commit modifies the linker to treat such symbols as a need, ensuring that --as-needed libraries are loaded when explicitly referenced, similar to how archives behave.
In Details
This commit modifies the linker's behavior to ensure that explicitly requested symbols via -u symbol on the command line cause --as-needed shared libraries to be loaded. Previously, ldlang_ref was not called for such symbols, meaning they were not considered references that would trigger loading of lazily-linked libraries. The change introduces ldlang_ref calls in insert_undefined and exp_fold_tree_1 to correctly mark these symbols as needed, aligning the behavior with archives and resolving issues in PR 32816.
- --as-needed
- A linker option that instructs the linker to only include a shared library in the link if it provides symbols that are actually needed by the program being linked. Libraries not needed are omitted.
- -u symbol
- A linker option that forces the inclusion of a symbol, even if it is not referenced by any object file. This is typically used to ensure a symbol from a shared library is resolved.
- archive
- A collection of object files bundled into a single file. When linked, the linker extracts only the necessary object files from the archive.
- shared library
- A library of code that can be linked into multiple programs dynamically at runtime, rather than being statically linked into each program.
- PR 32816
- A bug report filed against the GNU linker (ld), identified by number 32816, likely related to symbol visibility and dynamic linking behavior.