public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Why is the strong symbol in a shared library overridden by a weak symbol in a static library?
@ 2013-04-09 14:32 Jie Jiang
  2013-04-10  4:04 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Jie Jiang @ 2013-04-09 14:32 UTC (permalink / raw)
  To: gcc-help

Hi all,

Recently I got a weird question about strong symbol and weak symbol with gcc/linux on X86_64.
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.

How to explain it?

Following please find the source codes, Makefile and outputs of my test case.

% cat strong1.c

#include <stdio.h>

void foo ()
{
        printf("%s:%s\n", __FILE__, __func__);
}

% gcc -fPIC -shared -o libstrong1.so strong1.c
% nm libstrong1.so | grep foo
0000000000000560 T foo


% cat strong2.c

#include <stdio.h>

extern void bar();

void foo ()
{
        bar();
        printf("%s:%s\n", __FILE__, __func__);
}

% gcc -fPIC -shared -o libstrong2.so strong2.c
% nm libstrong2.so | grep foo
00000000000005b0 T foo
% nm libstrong2.so | grep bar
                 U bar


% cat weak.c

#include <stdio.h>

extern void foo() __attribute__((weak));
extern void bar();

void foo ()
{
        printf("%s:%s\n", __FILE__, __func__);
}


void bar()
{
        printf("%s:%s\n", __FILE__, __func__);
}

% gcc -fPIC -c weak.c -o weak.o
% ar cr libweak.a weak.o
% nm libweak.a | grep foo
0000000000000000 W foo
% nm libweak.a | grep bar
0000000000000020 T bar

% gcc -fPIC -shared -o libweak.so weak.c
% nm libweak.so | grep foo
0000000000000580 W foo
% nm libweak.so | grep bar
00000000000005a0 T bar



% cat test.c

extern void foo();

int main (int argc, char ** argv)
{
        foo();

	return 0;
}

% cat Makefile
CC = gcc 
all:
        $(CC) -fPIC -shared -o libstrong1.so strong1.c
        $(CC) -fPIC -shared -o libstrong2.so strong2.c
        $(CC) -fPIC -shared -o libweak.so weak.c
        $(CC) -o test1 test.c -L. -lstrong1 -lweak
        $(CC) -o test2 test.c -L. -lstrong2 -lweak
        LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) ./test1
        LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) ./test2
        rm libweak.so
        $(CC)   -c weak.c -fPIC
        ar cr libweak.a weak.o
        $(CC) -o test3 test.c -L. -lstrong1 -lweak
        $(CC) -o test4 test.c -L. -lstrong2 -lweak
        LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) ./test3
        LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) ./test4
clean:
        rm *.a *.so *.o

% make
gcc -fPIC -shared -o libstrong1.so strong1.c
gcc -fPIC -shared -o libstrong2.so strong2.c
gcc -fPIC -shared -o libweak.so weak.c
gcc -o test1 test.c -L. -lstrong1 -lweak
gcc -o test2 test.c -L. -lstrong2 -lweak
LD_LIBRARY_PATH=.:. ./test1
strong1.c:foo    
LD_LIBRARY_PATH=.:. ./test2
weak.c:bar
strong2.c:foo
rm libweak.so
gcc	-c weak.c -fPIC
ar cr libweak.a weak.o
gcc -o test3 test.c -L. -lstrong1 -lweak
gcc -o test4 test.c -L. -lstrong2 -lweak
LD_LIBRARY_PATH=.:. ./test3
strong1.c:foo
LD_LIBRARY_PATH=.:. ./test4
weak.c:foo   <----- Here Weak symbol in weak.c, instead of strong symbol in strong2.c!

% gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Regards,
Jie

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Why is the strong symbol in a shared library overridden by a weak symbol in a static library?
  2013-04-09 14:32 Why is the strong symbol in a shared library overridden by a weak symbol in a static library? Jie Jiang
@ 2013-04-10  4:04 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2013-04-10  4:04 UTC (permalink / raw)
  To: Jie Jiang; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-04-10  4:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-09 14:32 Why is the strong symbol in a shared library overridden by a weak symbol in a static library? Jie Jiang
2013-04-10  4:04 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).