The man for gold states:
-L DIR, --library-path DIR
Add directory to search path
--rpath-link DIR
Add DIR to link time shared library search path
The man for bfd ld makes it sort of sound like -rpath-link is used for recursively included sos.
ld.lld doesn't even list it as an argument.
Could somebody clarify this situation for me?
Here is a demo, for GNU
ld, of the difference between-Land-rpath-link- and for good measure, the difference between-rpath-linkand-rpath.foo.c
bar.c
foobar.c
main.c
Make two shared libraries,
libfoo.soandlibbar.so:Make a third shared library,
libfoobar.sothat depends on the first two;Oops. The linker doesn't know where to look to resolve
-lfooor-lbar.The
-Loption fixes that.The
-Ldiroption tells the linker thatdiris one of the directories to search for libraries that resolve the-lnameoptions it is given. It searches the-Ldirectories first, in their commandline order; then it searches its configured default directories, in their configured order.Now make a program that depends on
libfoobar.so:Oops again. The linker detects the dynamic dependencies requested by
libfoobar.sobut can't satisfy them. Let's resist its advice -try using -rpath or -rpath-link- for a bit and see what we can do with-Land-l:So far so good. But:
at runtime, the loader can't find
libfoobar.so.What about the linker's advice then? With
-rpath-link, we can do:and that linkage also succeeds. (
$(pwd)means "Print Working Directory" and just "copies" the current path.)The
-rpath-link=diroption tells the linker that when it encounters an input file that requests dynamic dependencies - likelibfoobar.so- it should search directorydirto resolve them. So we don't need to specify those dependencies with-lfoo -lbarand don't even need to know what they are. What they are is information already written in the dynamic section oflibfoobar.so:-We just need to know a directory where they can be found, whatever they are.
But does that give us a runnable
prog?No. Same as story as before. That's because
-rpath-link=dirgives the linker the information that the loader would need to resolve some of the dynamic dependencies ofprogat runtime - assuming it remained true at runtime - but it doesn't write that information into the dynamic section ofprog. It just lets the linkage succeed, without our needing to spell out all the recursive dynamic dependencies of the linkage with-loptions.At runtime,
libfoo.so,libbar.so- and indeedlibfoobar.so- might well not be where they are now -$(pwd)- but the loader might be able to locate them by other means: through theldconfigcache or a setting of theLD_LIBRARY_PATHenvironment variable, e.g:rpath=dirprovides the linker with the same information asrpath-link=dirand instructs the linker to bake that information into the dynamic section of the output file. Let's try that:All good. Because now,
progcontains the information that$(pwd)is a runtime search path for shared libraries that it depends on, as we can see:That search path will be tried after the directories listed in
LD_LIBRARY_PATH, if any are set, and before the system defaults - theldconfig-ed directories, plus/liband/usr/lib.