From Fedora Project Wiki

(What do I do?)
Line 53: Line 53:
A call to gcc will succeed quietly:
A call to gcc will succeed quietly:
<code>gcc -o foo1 foo1.o -Wl,--rpath-link=.</code>
  gcc -o foo1 foo1.o -Wl,--rpath-link=.
=== Proposed ===
=== Proposed ===

Revision as of 19:25, 26 November 2009

Understanding the (Proposed) Change to DSO Linking


The default behaviour for ld is to not link objects that are listed as dependencies of another linked object. This is dangerous if the other object is ever changed to occlude the object on which your program depended, causing your program to break without any change to your code.

A concrete example: has:

 NEEDED            Shared library: []
 NEEDED            Shared library: []

Under the old system, a program that links with libxml2 and uses dlopen may not link with libdl, and a program that links with libxml2 and uses gzopen may not link with libz. While these programs will work, they are will break if libxml2 is ever changed to omit the dependency on libdl/libz.

What's the difference?

For example (courtesy Roland McGrath):

 ==> foo1.c <==
 #include <stdio.h>
 extern int foo ();
 main ()
   printf ("%d\n", foo ());
 ==> foo2.c <==
 extern int foo ();
 int bar () { return foo (); }
 ==> foo3.c <==
 int foo () { return 0; }

Prepare position-independent code: gcc -g -fPIC -c foo1.c foo2.c foo3.c

Generate gcc -shared -o foo3.o

Generate, linking gcc -shared -o foo2.o

The proposed change will affect the creation of foo1.


A call to gcc will succeed quietly:

 gcc -o foo1 foo1.o -Wl,--rpath-link=.


The call to gcc will fail, prompting the user to explicitly link the required shared object.

 gcc -o foo1 foo1.o -Wl,--rpath-link=.
/usr/bin/ld: foo1.o: undefined reference to symbol 'foo'
/usr/bin/ld: note: 'foo' is defined in DSO ./ so try adding it to the linker command line

So, the difference is whether you can refer to a symbol that's in a DSO that you didn't list explicitly in your link line, but that is a DT_NEEDED dependency of one of those (or recursively of those, I think).

The big difference is that with the proposed change in place, ld will no longer skip linking needed libraries by default. The current default behaviour will lead ld to skip linking with a library if it is listed as a needed by another library that the program uses. In abstract terms, if libA is needed by libB and your program requires both libA and libB, your program may only link to libB. Then if another version of libB comes out that does not list libA as a needed library, then a recompilation will mysteriously break.

What do I do?

If you encounter this error, the error message will prompt you to explicitly link to the DSO that you need. From the foo example, adding will get rid of the error:

gcc -o foo1 foo1.o -Wl,--rpath-link=.