ld: load --as-needed shared libraries for -u symbols
Make linker load --as-needed shared libraries when symbols are explicitly requested with -u.
This patch modifies the linker to treat command-line specified symbols with -u as explicit needs for —as-needed shared libraries. Previously, such libraries might not have been loaded if no other symbols explicitly required them, potentially leading to unexpected behavior or incorrect test results. The changes ensure consistency with archive library behavior and fix issues in test cases related to common symbol size.
In Details
This change affects ld.bfd's handling of --as-needed shared libraries. When a symbol is listed with -u symbol_name, the linker now marks the shared library containing that symbol as needed, even if no other code explicitly references it. This is achieved by introducing ldlang_ref to update non_ir_ref_regular and related ELF hash table entries, ensuring that libraries are linked when their symbols are forced via -u.
- --as-needed
- A linker option that causes shared libraries to be linked in only if they are actually used by the program. If a shared library is not needed, it is not included in the output.
- -u symbol
- A linker option that forces the inclusion of
symbol, even if it is not referenced by any other object. This is useful for dynamically loaded modules that export symbols. - common symbol
- A symbol that has been declared but not yet defined. In C, this often corresponds to global variables without an initializer. The linker typically allocates space for common symbols.