Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 573/f20b8f1978d21aba97856c549acb0295 to your computer and use it in GitHub Desktop.
Save 573/f20b8f1978d21aba97856c549acb0295 to your computer and use it in GitHub Desktop.
Patching the Dynamic Linker/Loader of a Compiled Binary using patchelf #nix

Patching the Dynamic Linker/Loader of a Compiled Binary using patchelf

Given a foreign binary on a Nix system, you have to patch its dynamic linker/loader.

First you need to find out what linker/loader to use.

You can do this by going into your nix repl and loading your desired nixpkgs.

Then you run:

"${pkgs.glibc}/lib64/ld-linux-x86-64.so.2"

Which will return you a path that looks like this:

/nix/store/d54amiggq6bw23jw6mdsgamvs6v1g3bh-glibc-2.25-123/lib64/ld-linux-x86-64.so.2

You can then use:

patchelf --set-interpreter '/nix/store/d54amiggq6bw23jw6mdsgamvs6v1g3bh-glibc-2.25-123/lib64/ld-linux-x86-64.so.2' ./binarytobepatched

Or you can run it directly:

/nix/store/d54amiggq6bw23jw6mdsgamvs6v1g3bh-glibc-2.25-123/lib64/ld-linux-x86-64.so.2 ./binarytobepatched

Note that if you are doing this inside a Nix expression, you should be instead using:

$(cat $NIX_CC/nix-support/dynamic-linker)

Note that if you use ldd on NixOS on an unpatched binary it will probably show:

        /lib64/ld-linux-x86-64.so.2 => /nix/store/d54amiggq6bw23jw6mdsgamvs6v1g3bh-glibc-2.25-123/lib64/ld-linux-x86-64.so.2 (0x00007f04fcc29000)

That ld-linux-x86-64.so.2 path is derived from the Glibc that ldd itself was built with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment