I know that the standard ld.bfd processes static libraries in the sequence specified, pulling in whichever objects from each library satisfy an unmet (at that point in time) dependency. Does gold follow the same process, and is this documented anywhere?
I'm seeing a case where gold is pulling in one more *.so file than ld.bfd (I'm using the --as-needed option), and am trying to figure out why and, in general, what other differences I should keep an eye out for.
The rules for selecting members of an archive library are pretty much the same between the two linkers, but have nothing to do with the rules for determining whether a shared library is "needed".
For archive libraries with a symbol index (i.e., ranlib
has been run), the linker considers each symbol in the index in turn. If the symbol satisfies an unresolved reference at that point in the link, the linker will include the archive member that defines the symbol. It makes repeated passes over the symbol index until no new archive members are added.
The rules get a bit complicated when it comes to weak symbols and common symbols, and gold and BFD ld differ slightly in their treatment of common symbols (at the moment -- see PR 23411).
Archive libraries without a symbol index aren't generally supported these days. It used to be that linkers would make a single pass through the library, adding an archive member if it satisfied an unresolved reference. This required libraries to be topologically sorted (and many Unix systems still have an "lorder" tool to do this).
For shared libraries under the effect of an --as-needed
option, we consider the library to be "needed" if it satisfies a non-weak reference from a regular object file. Gold marks a shared library "needed" even if the reference comes from an object that follows it in the link order; I think BFD ld marks it "needed" only if the reference precedes the shared library.
If you're trying to figure out exactly why the linker thinks a particular library is "needed" or not, you may find the linker's -y symbol
option useful.