How to prohibit shared library binding in absolute path?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to prohibit shared library binding in absolute path?

Pan ruochen
Hi All,

I link an application against some shared libraries in absolute-path format.
The linker writes the absolute path into the output application.

$gcc -fpic -shared -fvisibility=hidden -O2 -o libs1.so s1.c
$gcc -fpic -shared -fvisibility=hidden -O2 -o libs2.so s2.c
$gcc -O2 -o foo -Wl,-rpath=. main.c /home/admin/x1/libs1.so libs2.so
$readelf -a foo | grep Shared
 0x00000001 (NEEDED)                     Shared library:
[/home/admin/x1/libs1.so]
 0x00000001 (NEEDED)                     Shared library: [libs2.so]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

The shared libraries couldn't be moved to other directories since `foo' inform
the dynamic linker that libs1.so must be inside the directory /home/admin/x1.
This will cause problems for cross building.

I expect to get while linking agaist /home/admin/x1/libs1.so:
$readelf -a foo | grep Shared
 0x00000001 (NEEDED)                     Shared library: [libs1.so]
 0x00000001 (NEEDED)                     Shared library: [libs2.so]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

How to do?

PRC
Mar 21,2011
Reply | Threaded
Open this post in threaded view
|

Re: How to prohibit shared library binding in absolute path?

Ralf Wildenhues
Hello Pan,

* Pan ruochen wrote on Mon, Mar 21, 2011 at 07:28:47AM CET:
> I link an application against some shared libraries in absolute-path format.
> The linker writes the absolute path into the output application.
>
> $gcc -fpic -shared -fvisibility=hidden -O2 -o libs1.so s1.c
> $gcc -fpic -shared -fvisibility=hidden -O2 -o libs2.so s2.c
> $gcc -O2 -o foo -Wl,-rpath=. main.c /home/admin/x1/libs1.so libs2.so

To avoid that, give the libraries a soname to get that recorded in
DT_NEEDED of consumers, rather than the name of the library used for
linking: -Wl,-soname,libs1.so

Cheers,
Ralf