Why is the strong symbol in a shared library overridden by a weak symbol in a static library?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux