This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Why is the strong symbol in a shared library overridden by a weak symbol in a static library?
- From: Ian Lance Taylor <iant at google dot com>
- To: Jie Jiang <jiangjie at nudt dot edu dot cn>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Tue, 9 Apr 2013 21:04:28 -0700
- Subject: Re: Why is the strong symbol in a shared library overridden by a weak symbol in a static library?
- References: <718828c1 dot 417e dot 13def330d34 dot Coremail dot jiangjie at nudt dot edu dot cn>
On Tue, Apr 9, 2013 at 7:28 AM, Jie Jiang <jiangjie@nudt.edu.cn> wrote:
>
> Recently I got a weird question about strong symbol and weak symbol with gcc/linux on X86_64.
This is really not a GCC question.
> Theoretically, in case of the presence of both a strong symbol and a weak symbol (with the same symbol name, of course), the strong symbol will be chosen by the linker. But in my test, if the strong symbol (foo) calls a function (bar) defined in a static library, which has a weak definition of symbol 'foo', the final symbol appears in the executable will be the weak symbol. However, if the static library is changed to a shared library (from libweak.a to libweak.so), the final 'foo' symbol in the executable will be strong, instead of weak symbol.
Your example shows a situation in which a program defines a weak
symbol foo and is linked against a shared library that defines a
strong symbol foo. You are asking why the dynamic linker does not
override the weak symbol foo in the main program with the strong
symbol foo defined in the shared library. It's a reasonable question,
but the glibc dynamic linker does not work that way. The glibc
dynamic linker stops searching once it finds a weak definition. This
is for efficiency.
That said, try defining the environment symbol LD_DYNAMIC_WEAK before
you run the program.
Ian