public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Jie Jiang" <jiangjie@nudt.edu.cn>
To: gcc-help@gcc.gnu.org
Subject: Why is the strong symbol in a shared library overridden by a weak symbol in a static library?
Date: Tue, 09 Apr 2013 14:32:00 -0000	[thread overview]
Message-ID: <718828c1.417e.13def330d34.Coremail.jiangjie@nudt.edu.cn> (raw)

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

             reply	other threads:[~2013-04-09 14:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-09 14:32 Jie Jiang [this message]
2013-04-10  4:04 ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=718828c1.417e.13def330d34.Coremail.jiangjie@nudt.edu.cn \
    --to=jiangjie@nudt.edu.cn \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).