Home > Blockchain >  patchelf set interpreter to a path relative to executable
patchelf set interpreter to a path relative to executable

Time:05-25

I have tried to do this:

patchelf --set-interpreter ../lib/ld-linux-x86-64.so.2 "${APPDIR}/usr/bin/myapp"

so I have this:

readelf -l AppDir/usr/bin/myapp
...
[Requesting program interpreter: ../lib/ld-linux-x86-64.so.2]

But it looks like it does not like the relative path as I get the error:

$  AppDir/usr/bin/myapp 
bash: AppDir/usr/bin/myapp: No such file or directory

I have found this question, as I tried to solve this by using $ORIGIN: Using $ORIGIN to specify the interpreter in ELF binaries isn't working, but it does not answer my question.

Is there a simple way to patch an executable with your certain linker, if you could not know beforehand its absolute path on a machine (e.g. if it's an appimage case)? The only workaround I know is to use path/to/my/ld.so --library-path <path/to/my/libs> path/to/my/exe. But I am just curious, if there is a way to actually patch an executable, so that I could launch it directly?

CodePudding user response:

You need to specify the path relative to the current working directory (the one you use to execute the program) and not relative to the directory that contains the binary file.

echo 'int main() { return 0; }' > main.c
gcc -Wl,--dynamic-linker=./lib/ld-linux-x86-64.so.2 -o main main.c

mkdir bin
mkdir lib
cp /lib64/ld-linux-x86-64.so.2 lib/
cp main bin/

bin/main          # this should run without problems
cd bin; ./main    # this throws a "No such file or directory" error

  • Related