This is the mail archive of the
binutils@sourceware.cygnus.com
mailing list for the binutils project.
GNU-ld behaviour does not match native linker behaviour
- To: binutils at sourceware dot cygnus dot com
- Subject: GNU-ld behaviour does not match native linker behaviour
- From: Nick Clifton <nickc at cygnus dot com>
- Date: Thu, 2 Dec 1999 12:08:50 GMT
Hi Guys,
It has been brought to my attention that GNU-ld does not handle
commons in libraries in the same way as SUN's or HP's linkers handle
them. Here is an example:
GNU-ld (eg Linux)
% cat main.c
int common_sym;
int main (void) { return printf ("%d\n\n", common_sym); }
% gcc main.c
% a.out
0
% cat define.c
int common_sym = 3;
int foo (void) { return 0; }
% gcc -c define.c
% ar r libtest.a define.o
% gcc main.c define.o
% a.out
3
% gcc main.c libtest.a
% a.out
0
% gcc -Wl,-ufoo main.c libtest.a
% a.out
3
Note how linking with a library which contains a defintion of the
contents of a common symbol has no effect unless there is some other
reason to link in the archive element which contains the common
defintion.
However on Solaris 7 things are slightly different:
% /opt/SUNWspro/bin/cc main.c
% a.out
0
% /opt/SUNWspro/bin/cc -c define.c
% /usr/ccs/bin/ar r libtest.a define.o
% /opt/SUNWspro/bin/cc main.c define.o
% a.out
3
% /opt/SUNWspro/bin/cc main.c libtest.a
% a.out
3
% /opt/SUNWspro/bin/cc -Wl,-ufoo main.c libtest.a
% a.out
3
So now the common symbol gets defined from the library even if there
is no other reason to link in that particular library element. The
same thing happens using HP-UX 11.0's native linker.
So - which linker is doing the right thing ?
I have a patch to change the behaviour of GNU-ld, although it is
rather cumbersome, but I do not know if I should even submit it, since
I am not at all sure that GNU-ld is doing the wrong thing.
Note - this handling of commons is particularly important when linking
together FORTRAN libraries, since the definition of the contents of a
common symbol can often be found in a seperate object file to the
defintion of the use of the common symbol.
Cheers
Nick