Embedded Linux Tricks

How to run an executable compiled with a libc version different than the default one

The first option is to run the executable with the following wrapper:

#!/bin/bash

exec $PREFIX/lib/ld-linux.so.3 --library-path $PREFIX/lib/:$PREFIX/usr/lib/ $BINARY "$@"

Bash

where $PREFIX is the directory where the non-standard libc is and $BINARY is the binary that needs to be executed. This approach doesn’t maintain the argv[0] for the program being executed (so it can be a problem for programs that use it to decide what to do such as git).

The second option is to use the patchelf tool to set a different interpreter (ld-linux.so) and RPATH ( $PREFIX/lib/:$PREFIX/usr/lib/ ) in the binary.

Make sure you don’t patch `ld.so`!

The script below takes some debian binary packages from ./debs and automatically patches them:

#!/bin/bash
 
PREFIX=/opt/mylibc
 
mkdir root
 
for file in debs/*
do
echo "Uncompressing $file"
ar x $file
( cd root && tar -zxf ../data.tar.gz)
done
 
for file in $(find root -type f -perm +111)
do

#don't patch the loader
if [[ "$(basename $file)" == "root/lib/ld-2.11.3.so" ]] ; then
continue
fi
 
#don't try to patch non ELF executables
if [[ "$(file $file | grep ELF )" == "" ]]
then
continue
fi
 
echo "Patching $file"
patchelf --set-interpreter $PREFIX/lib/ld-linux.so.3 $file
patchelf --set-rpath $PREFIX/lib/:$PREFIX/usr/lib $file
 
done

Bash