[devel] Q: --no-add-needed

Kirill A. Shutemov kirill на shutemov.name
Пт Мар 19 08:40:51 UTC 2010


2010/3/19 Max Ivanov <ivanov.maxim на gmail.com>:
>> There is potentially important change in gcc-4.4.3-5 from Fedora. IIUC, they
>> added --no-add-needed (AKA --no-copy-dt-needed-entries) to default
>> linker flags.
>
> What is expected consequences of this change?

For example, we have two libraries liba.so and libb.so. liba.so
provides symbol 'a':

$ cat liba.c
int a()
{
        return 1;
}
$ gcc -shared -fpic liba.c -o liba.so

libb.so provides symbol 'b' and links with liba.so

$ cat libb.c
int a(void);

int b()
{
        return a() + 1;
}
$ gcc -shared -fpic libb.c -o libb.so -L. -la
$ LD_LIBRARY_PATH=. ldd libb.so
        linux-gate.so.1 =>  (0xb77fc000)
        liba.so => ./liba.so (0xb77f5000)
        libc.so.6 => /lib/libc.so.6 (0xb768c000)
        /lib/ld-linux.so.2 (0xb77fd000)

Also, we have a program 'test' with want to use both symbols 'a' and
'b'. With --add-needed (by default, currently) you only need to link
it with libb.so to use both symbols 'a' and 'b', since libb linked
with liba.

$ cat test.c
#include <stdio.h>

int a(void);
int b(void);

int main()
{
        printf("a: %d, b: %d\n", a(), b());
        return 0;
}
$ LD_LIBRARY_PATH=. gcc test.c -o test -Wl,--add-needed -L. -lb
$ LD_LIBRARY_PATH=. ./test
a: 1, b: 2

With --no-add-needed you have to link with liba.so as well to use symbol 'a'

$ LD_LIBRARY_PATH=. gcc test.c -o test -Wl,--no-add-needed -L. -lb
/usr/bin/ld: /tmp/.private/kas/cc05FEHn.o: undefined reference to symbol 'a'
/usr/bin/ld: note: 'a' is defined in DSO ./liba.so so try adding it to
the linker command line
./liba.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
$ LD_LIBRARY_PATH=. gcc test.c -o test -Wl,--no-add-needed -L. -lb -la
$ LD_LIBRARY_PATH=. ./test
a: 1, b: 2


Подробная информация о списке рассылки Devel