Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Lets say I have a /bin/cat executable that uses the following shared libraries:

linux-vdso.so.1
libc.so.6
/lib64/ld-linux-x86-64.so.2

Is it possible to somehow "join" that stuff together (executable archive or something like that)?

I don't have the code of the program I want to do that to, so I can't just compile it with -static flag.

share|improve this question

migrated from serverfault.com Aug 28 '12 at 1:52

This question came from our site for professional system and network administrators.

3 Answers 3

Depends on what you mean by "join". The best way is to recompile the original source as a static binary.

However, you can include all of the libraries in a subdirectory that is controlled by the application (/usr/share/myapp for instance) and then put that directory in the library search order. One way to do this would be (in Linux) to use the LD_LIBRARY_PATH:

LD_LIBRARY_PATH=/usr/share/myapp/lib:/usr/local/lib:/usr/lib/i686-linux-gnu:/lib/i686-linux-gnu

In Linux, you can modify the /etc/ld.so.conf file to do the same thing - and Ubuntu adds the *.conf files in the /etc/ld.so.conf.d directory also.

share|improve this answer
    
+1. For clarification, I believe you'd have to recompile the original binary as a static binary. –  Kyle Smith Feb 21 '12 at 17:15
    
Nope. Just set up the library search path as desired and be done with it. –  Mei Feb 21 '12 at 18:05

Converting bunch of shared objects and a shared-lib ELF executable to one statically linked blob is not straight-forward.

The executable archive you mentioned might be possible. Assuming that by 'executable archive' you meant a file that when run un-zips itself and runs a program without dependencies on other files on the system.

This can be done quite simply using shell script and bunch of utilities found in most linux distros (However it adds dependencies on shell and the utilties). Making an ELF 'executable archive' might be much more involved although the principles are the same. If you expect your version of the program to be executed frequently, you should modify the script to 'install' it on to the users system as indicated by David and janneb.

An example using shell script based executable archive

Collect the files you need for the execution, an example using cat program would have the following:

fachas_cat_files/cat
fachas_cat_files/lib/libc.so.6

Base64 encode the tgz file and make it cat.b64, can be done like this after setting up your fachas_cat_files directory.

tar -cz fachas_cat_files | base64 > cat.b64

This makes a representation of the tar file with printable characters so it can be included in the script.

Note down the md5sums of the files you are packaging, which you use to verify whether the untarred files are your own or not, in the shell script.

find fachas_cat_files/ -type f -exec md5sum {} \; > cat.md5

Make a shell script like the following and name it your new "archive" program.

#!/bin/bash

TEMP_DIR=/tmp

# Check Md5sum
md5sum -c --quiet >/dev/null 2>&1 <<EOF 
--- Paste contents of cat.md5 here. ---
EOF

# Untar from base64 encoded tarball.
test $? -eq 0 || base64 -d <<EOF | tar -xz -C ${TEMP_DIR}
--- Paste contents of cat.b64 here. ---
EOF

# Execute the binary.
LD_LIBRARY_PATH=${TEMP_DIR}/fachas_cat_files/lib/ ${TEMP_DIR}/fachas_cat_files/cat $*

# Optionally remove the temporary files, if you do, the whole md5sum set of steps is 
# not necessary.
# rm -fr ${TEMP_DIR}/fachas_cat_files
share|improve this answer

No, it's not possible. What you might do is to create an archive (.tar.gz or whatever) containing

  • The original executable renamed, say bin/cat.real
  • A private directory, say lib/fachas-cat containing copies of all the libraries bin/cat.real needs
  • A shellscript bin/cat which sets LD_LIBRARY_PATH to $SOME_INSTALL_PREFIX/lib/fachas-cat/ and runs bin/cat.real
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.