Bug 15096 - DT_RPATH != DT_RUNPATH
Summary: DT_RPATH != DT_RUNPATH
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-04 18:09 UTC by H.J. Lu
Modified: 2013-02-05 22:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2013-02-04 18:09:53 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 runpath]$ cat foo.c
#include <dlfcn.h>

int
main ()
{
  void* handle = dlopen("libso1.so", RTLD_LAZY);
  typedef int (*hello_t)();
  hello_t hello1 = (hello_t)dlsym(handle, "hello1");
  hello1();
  return 0;
}
[hjl@gnu-6 runpath]$ cat so1.c
extern void hello2 (void);

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

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

Dynamic section at offset 0x8b0 contains 27 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libso1.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [.]
 0x000000000000000c (INIT)               0x400558
 0x000000000000000d (FINI)               0x400794
 0x0000000000000019 (INIT_ARRAY)         0x600898
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x6008a0
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400260
 0x0000000000000005 (STRTAB)             0x4003d0
 0x0000000000000006 (SYMTAB)             0x400298
 0x000000000000000a (STRSZ)              202 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x600ab8
 0x0000000000000002 (PLTRELSZ)           72 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x400510
 0x0000000000000007 (RELA)               0x4004f8
 0x0000000000000008 (RELASZ)             24 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x4004b8
 0x000000006fffffff (VERNEEDNUM)         2
 0x000000006ffffff0 (VERSYM)             0x40049a
 0x0000000000000000 (NULL)               0x0
./foo1
hello
readelf -d foo2

Dynamic section at offset 0x8b0 contains 27 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libso1.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000001d (RUNPATH)            Library runpath: [.]
 0x000000000000000c (INIT)               0x400558
 0x000000000000000d (FINI)               0x400794
 0x0000000000000019 (INIT_ARRAY)         0x600898
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x6008a0
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400260
 0x0000000000000005 (STRTAB)             0x4003d0
 0x0000000000000006 (SYMTAB)             0x400298
 0x000000000000000a (STRSZ)              202 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x600ab8
 0x0000000000000002 (PLTRELSZ)           72 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x400510
 0x0000000000000007 (RELA)               0x4004f8
 0x0000000000000008 (RELASZ)             24 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x4004b8
 0x000000006fffffff (VERNEEDNUM)         2
 0x000000006ffffff0 (VERSYM)             0x40049a
 0x0000000000000000 (NULL)               0x0
./foo2
./foo2: error while loading shared libraries: libso2.so: cannot open shared object file: No such file or directory
make: *** [all] Error 127
[hjl@gnu-6 runpath]$
Comment 1 H.J. Lu 2013-02-04 18:59:25 UTC
We should set link_info.new_dtags to TRUE by default only if
command_line.rpath is NULL.
Comment 2 H.J. Lu 2013-02-04 20:57:18 UTC
Another problem is link_info.new_dtags is set to TRUE for all
enabled emulations, instead of just target emulations.
Comment 3 Sourceware Commits 2013-02-05 01:36:08 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2013-02-05 01:36:05

Modified files:
	ld             : ChangeLog 
	ld/emultempl   : alphaelf.em cr16elf.em crxelf.em elf32.em 
	                 hppaelf.em ia64elf.em mipself.em 
	ld/testsuite   : ChangeLog 
Added files:
	ld/testsuite/ld-elf: new-dtags-1.d new-dtags-2.d new-dtags-3.d 
	                     new-dtags-4.d new-dtags-5.d new-dtags-6.d 
	                     new-dtags-7.d new-dtags-8.d 

Log message:
	Don't enable new dtags by default with -rpath
	
	ld/
	
	2013-02-04  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR ld/15096
	* emultempl/alphaelf.em (alpha_after_parse): Call
	gld${EMULATION_NAME}_after_parse instead of after_parse_default.
	* emultempl/cr16elf.em (cr16elf_after_parse): Likewise.
	* emultempl/crxelf.em (crxelf_after_parse): Likewise.
	* emultempl/hppaelf.em (hppaelf_after_parse): Likewise.
	* emultempl/mipself.em (mips_after_parse): Likewise.
	
	* emultempl/ia64elf.em (gld${EMULATION_NAME}_after_parse): Renamed
	to ...
	(gld_${EMULATION_NAME}_after_parse): This.  Call
	gld${EMULATION_NAME}_after_parse instead of after_parse_default.
	(LDEMUL_AFTER_PARSE): Set to gld_${EMULATION_NAME}_after_parse.
	
	* emultempl/elf32.em (new_dtags_set): New variable.
	(gld${EMULATION_NAME}_before_parse): Don't set link_info.new_dtags
	here.
	(gld${EMULATION_NAME}_after_parse): New function.
	(ld_${EMULATION_NAME}_emulation): Replace after_parse_default'
	with gld${EMULATION_NAME}_after_parse.
	(gld${EMULATION_NAME}_handle_option): Set new_dtags_set to TRUE
	when setting link_info.new_dtags.
	
	ld/testsuite/
	
	2013-02-04  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR ld/15096
	* ld-elf/new-dtags-1.d: New test.
	* ld-elf/new-dtags-2.d: Likewise.
	* ld-elf/new-dtags-3.d: Likewise.
	* ld-elf/new-dtags-4.d: Likewise.
	* ld-elf/new-dtags-5.d: Likewise.
	* ld-elf/new-dtags-6.d: Likewise.
	* ld-elf/new-dtags-7.d: Likewise.
	* ld-elf/new-dtags-8.d: Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2547&r2=1.2548
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/alphaelf.em.diff?cvsroot=src&r1=1.17&r2=1.18
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/cr16elf.em.diff?cvsroot=src&r1=1.8&r2=1.9
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/crxelf.em.diff?cvsroot=src&r1=1.8&r2=1.9
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/elf32.em.diff?cvsroot=src&r1=1.237&r2=1.238
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/hppaelf.em.diff?cvsroot=src&r1=1.59&r2=1.60
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/ia64elf.em.diff?cvsroot=src&r1=1.10&r2=1.11
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/mipself.em.diff?cvsroot=src&r1=1.14&r2=1.15
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1674&r2=1.1675
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-1.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-2.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-3.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-4.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-5.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-6.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-7.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/new-dtags-8.d.diff?cvsroot=src&r1=NONE&r2=1.1
Comment 4 H.J. Lu 2013-02-05 01:45:25 UTC
Fixed.