Re: rpath question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/23/2015 03:17 PM, Aaron Cody wrote:
let's say I have an executable myprog that depends on a shared library foo.so
foo.so in turn depends on bar.so
now I set rpath on myprog to be /tmp, rpath on foo.so to be /tmp/foo
and rpath on bar.so to be /tmp/bar

Q: when I run foo, what will the resulting rpath be?

/tmp
/tmp:/tmp/foo
/tmp:/tmp/foo:/tmp/bar
some other value?

Rpath resolution is done by the dynamic linker, ld.so, not by
the compiler, so this isn't the right forum for the question.

That said, the answer depends on whether or not a.out is linked
with libbar. If it is, ld.so will look for bar.so in /tmp first
(and fail to load the program if the library isn't there).
Otherwise it will search for it in /tmp/foo.

The general algorithm used by ld.so is specified by ELF so
the ELF standard is the reference to check (see the section
titled Shared Object Dependencies). To definitively answer
the question you should verify it by an experiment. An easy
way to see the order in which ld.so considers the DT_RPATH
settings in the program and the DSOs it depends on is by
setting the glibc LD_DEBUG environment variable to the value
libs when running the program.

For example like so:

$ cat a.c && mkdir -p foo bar foobar && gcc -DFOO -Wall -fpic -shared -o foo/libfoo.so a.c && gcc -DBAR -Lfoo -Wall -Wl,-rpath,foobar -fpic -lfoo -shared -o bar/libbar.so a.c && gcc -DFOOBAR -Wall -fpic -shared -o foobar/libfoo.so a.c && gcc -Wall -Wl,-rpath,bar a.c -Lbar -lbar -ldl && LD_DEBUG=libs ./a.out
extern int puts (const char*);
extern int foo (void);
extern int bar (void);
#if FOO
int foo (void) { puts (__func__); return 0; }
#elif BAR
int bar (void) {
    puts (__func__);
    return foo ();
}
#elif FOOBAR
int foo (void) { puts ("the other foo"); return 1; }
#else
int main (void) {
    puts (__func__);
    return bar ();
}
#endif





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux