On Tue, Apr 9, 2013 at 7:28 AM, Jie Jiang <jiangjie@xxxxxxxxxxx> 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