behavior of weak definitions in archive libraries

David Mosberger davidm@napali.hpl.hp.com
Tue Nov 11 22:08:00 GMT 2003


We're seeing a problem which appears to come from an unexpected (to
me) behavior of the linker: it appears that if a program has a weak
reference to a symbol in an archive object and there is no other
reason for the archive object to be linked in, then the weak reference
will end up being resolved to NULL.

For example:

$ cat weak.c
int weakref = 13;
$ gcc -c weak.c
$ ar rv libweak.a weak.o
r - weak.o
$ cat t.c
#include <stdio.h>

extern int weakref __attribute__((weak));

int main (int argc, char **argv) {
  printf ("weakref=%p\n", &weakref);
  return 0;
}
$ gcc t.c libweak.a
$ ./a.out
weakref=(nil)

Is this behavior intentional?  I'm seeing it both on ia64 and x86
(linux).

	--david



More information about the Binutils mailing list