weak symbols vs archive libraries

Konstantin Belousov kostikbel at gmail.com
Wed Apr 18 10:50:03 UTC 2012


On Wed, Apr 18, 2012 at 01:36:26PM +0300, Andriy Gapon wrote:
> 
> I just would like to share something that I stumbled upon.
> Maybe this is something well known, then forgive me for the noise.
> 
> When ld combines multiple object files it overrides weak symbol definitions with
> a strong definition (if any).  There are many examples/demonstrations on the
> Internet on how this works, e.g.:
> http://winfred-lu.blogspot.com/2009/11/understand-weak-symbols-by-examples.html
> 
> But when the object files are spread across multiple archives, the there could
> be some surprises.
> My understanding is that there are two big rules that linker follows with
> respect to archive libraries:
> - linker extracts an object file from a library only if according to a symbol
> table of the library the object file contains some interesting symbols
> - if linker extracts an object file then it processes all symbols in it
> 
> And now the following observation: if linker has already seen a weak definition
> for a symbol, then it will not actively seek any other definitions for it.  But
> it will take into account the definitions that it stumbles upon.
> For example, if an object file in an archive library contains only (strong)
> definitions for some symbols that already have weak definitions, the linker will
> not extract that object file and will not look into it.  And thus the weak
> definition will not be overridden.  OTOH, if that object file contains a
> definition for at least one still undefined symbol, then the file will be
> interesting to linker, it will extract the file, process _all_ symbols in it and
> thus will override the weak definitions with the strong ones.
> 
> This is something that was unexpected to me.
> 
> Some references:
> http://webpages.charter.net/ppluzhnikov/linker.html
> http://glandium.org/blog/?p=2388

This is from the ELF standard version 1.2 PDF, page 1-5:

When the link editor searches archive libraries, it extracts archive
members that contain definitions of undefined global symbols. The
member's definition may be either a global or a weak symbol. The link
editor does not extract archive members to resolve undefined weak
symbols. Unresolved weak symbols have a zero value.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20120418/40adf1fb/attachment.pgp


More information about the freebsd-hackers mailing list