Bug 15098

Summary: DT_RPATH isn't set with -rpath on Linux
Product: binutils Reporter: H.J. Lu <hjl.tools>
Component: goldAssignee: Cary Coutant <ccoutant>
Status: NEW ---    
Severity: normal CC: ccoutant, cyfmxc
Priority: P2    
Version: 2.24   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description H.J. Lu 2013-02-05 01:42:36 UTC
According to gABI,

The set of directories specified by a given DT_RUNPATH entry is used to
find only the immediate dependencies of the executable or shared object
containing the DT_RUNPATH entry. That is, it is used only for those
dependencies contained in the DT_NEEDED entries of the dynamic structure
containing the DT_RUNPATH entry, itself. One object's DT_RUNPATH entry
does not affect the search for any other object's dependencies.

That means DT_RUNPATH on executable doesn't apply to shared objects
which executable depends on:

[hjl@gnu-6 pr15096a]$ cat foo.c
#include <stdio.h>
#include <dlfcn.h>

int
main ()
{
  typedef int (*hello_t)();
  hello_t hello1;
  void* handle = dlopen("libso1.so", RTLD_LAZY);
  
  if (!handle)
    {
      printf ("dlopen: %s\n", dlerror ());
      return 1;
    }

  hello1 = (hello_t)dlsym(handle, "hello1");
  hello1();
  return 0;
}
[hjl@gnu-6 pr15096a]$ cat so1.c
extern void hello2 (void);

void 
hello1 (void)
{
  hello2();
}
[hjl@gnu-6 pr15096a]$ cat so2.c
#include <stdio.h>

void
hello2 ()
{
  printf ("hello\n");
}
[hjl@gnu-6 pr15096a]$ make
gcc -fPIC -O2 -shared -o libso2.so so2.c
gcc -fPIC -O2 -shared -o libso1.so so1.c libso2.so
gcc -B./ -o foo2 foo.c -Wl,-rpath,. -ldl
readelf -d foo2

Dynamic section at offset 0x890 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000003 (PLTGOT)             0x401a88
 0x0000000000000002 (PLTRELSZ)           120 (bytes)
 0x0000000000000017 (JMPREL)             0x400488
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000007 (RELA)               0x400470
 0x0000000000000008 (RELASZ)             24 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000006 (SYMTAB)             0x400260
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000005 (STRTAB)             0x400350
 0x000000000000000a (STRSZ)              171 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400400
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x400500
 0x000000000000000d (FINI)               0x400784
 0x000000000000001a (FINI_ARRAY)         0x401ad8
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x0000000000000019 (INIT_ARRAY)         0x401ae0
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001d (RUNPATH)            Library runpath: [.]
 0x000000006ffffff0 (VERSYM)             0x40041c
 0x000000006ffffffe (VERNEED)            0x400430
 0x000000006fffffff (VERNEEDNUM)         2
 0x0000000000000000 (NULL)               0x0
./foo2
dlopen: libso2.so: cannot open shared object file: No such file or directory
make: *** [all] Error 1
[hjl@gnu-6 pr15096a]$ 

This is introduced by

http://sourceware.org/git/?p=binutils.git;a=commit;h=0e0646ad6f21c4d6230c31f452b73f133f4b5777