LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-11-2007, 04:09 AM   #1
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Rep: Reputation: 15
Changing a binaries linked libraries


Hi everyone,

I have a binary file that I compiled on a centos 4.5 distro. I wish to copy this file to another machine (which is a streipped down embedded distro) and run it there, however it obviously has a number of linked libraries which aren't in the same location on that machine or don't exist at all.

Basically, if I run ldd on the library (on the machine it was compiled on) this is the output:

Code:
ldd ddccontrol
       libddccontrol.so.0 => /usr/local/lib/libddccontrol.so.0
(0x005f3000)
       libxml2.so.2 => /usr/lib/libxml2.so.2 (0x02ba5000)
       libz.so.1 => /usr/lib/libz.so.1 (0x00d0c000)
       libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00dd9000)
       libm.so.6 => /lib/tls/libm.so.6 (0x00bf0000)
       libc.so.6 => /lib/tls/libc.so.6 (0x00ac4000)
       /lib/ld-linux.so.2 (0x00aab000)
Now some of those libraries/paths dont exist on the other machine, so I'm looking for a way to just package the binary with the libs, like:

myapp/binary
myapp/lib/libxml2.so.2 etc.

I have tried opening the binary file in a hex editor to change the path but I end up with a seg fault so I assume I'm doing it wrong. Can anyone give me any pointers on how to do this better, perhaps a specific tool i could use or maybe even recommend a hex editor.

Any help is appreciated,

Thanks.
 
Old 09-12-2007, 06:02 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
There are two separate issues here: the first issue is that on the target system, some libraries exist, but may be at a different location. This is ok (as long as the location is in the system library path). This is because the full path to a library is not specified to the binary (under normal circumstances: see rpath later on). The output of ldd shows the library soname specified, followed by an ASCII arrow (“=>”), followed by the location from which the library would be fetched were the executable executed. I’ll illustrate this functionality with an example (NOTE: please don’t use LD_LIBRARY_PATH as a permanent solution. I’m just using it for demonstration):
Code:
$ mkdir -p /tmp/thisisatest
$ cp -L /usr/local/lib/libddccontrol.so.0 /tmp/thisisatest
$ export LD_LIBRARY_PATH="/tmp/thisisatest"
$ ldd ddccontrol
       libddccontrol.so.0 => /tmp/thisisatest/libddccontrol.so.0 (0x005f3000)
       libxml2.so.2 => /usr/lib/libxml2.so.2 (0x02ba5000)
       libz.so.1 => /usr/lib/libz.so.1 (0x00d0c000)
       libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00dd9000)
       libm.so.6 => /lib/tls/libm.so.6 (0x00bf0000)
       libc.so.6 => /lib/tls/libc.so.6 (0x00ac4000)
$ rm -rf /tmp/thisisatest
$ unset LD_LIBRARY_PATH
$ ldd ddccontrol
       libddccontrol.so.0 => /usr/local/lib/libddccontrol.so.0 (0x005f3000)
       libxml2.so.2 => /usr/lib/libxml2.so.2 (0x02ba5000)
       libz.so.1 => /usr/lib/libz.so.1 (0x00d0c000)
       libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00dd9000)
       libm.so.6 => /lib/tls/libm.so.6 (0x00bf0000)
       libc.so.6 => /lib/tls/libc.so.6 (0x00ac4000)
       /lib/ld-linux.so.2 (0x00aab000)
Notice how the first line of output changes upon changing the system’s library lookup path. Ostensibly, this should happen with some of the libraries on the target machine by default (e.g., libc.so.6, libm.so.6, libpthread.so.0, and/or libz.so.1).

The next part of your question regards libraries that don’t exist on the target system (at all). There are many ways to deal with this (among which one of the most discouraged is fiddling with LD_LIBRARY_PATH). If you are compiling the sources by hand, the easiest solution is to link the affected libraries statically (there is little reason to use dynamic libs for only one binary on an small system). Another solution at compile time (assuming you want to use and bundle the shared libraries) is to use runtime path specification. This is either directly or indirectly through ld (directly with “-rpath /path/to/libs” or indirectly through gcc with “-Wl,-rpath,/path/to/libs”). A third method exists if you are unable to recompile. You can use an ELF file modification program to rewrite the runtime path specification. PatchELF is one such program. The final resort (which is often discouraged as it may exhibit unwanted behavior) is to run the resulting binary (on the host system) with the LD_LIBRARY_PATH environment variable set to accommodate the bundled libraries).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pre-compiled libraries and binaries. pappy_mcfae Slackware 8 03-27-2007 02:07 AM
Dynamically linked libraries.. vishalbutte Programming 6 04-17-2006 08:54 PM
How to see which shared libraries are linked to a program zsjoska Linux - Newbie 2 11-19-2004 01:45 PM
Linux Dynamicly Linked Libraries sikil_nuru Programming 3 03-16-2004 07:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration