Downloaded binary to patchelf

Hi everyone,

I want to install GrapheneOS on my phone, and thus need to use the latest SDK Platform-Tools from Google. However, I have troubles using the downloaded libraries, and instead of switching to another Linux distro, I thought it was time to understand a little bit more what’s happening under NixOS’ hood. I’ve read this previous thread, this blog post and the official NixOS PatchELF documentation, but as a new user, I’m still struggling with this.

So, what I did is unzip the downoaded latest platform-tools, and of course executing binaries as it is doesn’t work:

[tcip@nixos:~/Téléchargements/platform-tools]$ ./fastboot --version 
bash: ./fastboot: No such file or directory
[tcip@nixos:~/Téléchargements/platform-tools]$ ./adb --version 
bash: ./fastboot: No such file or directory

Than, I tried to get the needed shared libraries:

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --print-needed ./fastboot     
libdl.so.2
libpthread.so.0
libm.so.6
librt.so.1
libgcc_s.so.1
libc.so.6
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --print-needed ./adb 
libdl.so.2
libpthread.so.0
libm.so.6
librt.so.1
libgcc_s.so.1
libc.so.6
ld-linux-x86-64.so.2

and patchelf the binaries:

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libdl.so.2 ./fastboot 
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libpthread.so.0 ./fastboot           
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libm.so.6 ./fastboot             
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/librt.so.1 ./fastboot            
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libgcc_s.so.1 ./fastboot        
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libc.so.6 ./fastboot
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-rpath "$(patchelf --print-rpath ./fastboot):$(dirname $(readlink -e /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/))" ./fastboot
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libdl.so.2 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libpthread.so.0 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libm.so.6 ./adb      
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/librt.so.1 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libgcc_s.so.1 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libc.so.6 ./adb    
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/ld-linux-x86-64.so.2 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-rpath "$(patchelf --print-rpath ./fastboot):$(dirname $(readlink -e /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/))" ./fastboot

However, that worked for adb but not fastboot:

[tcip@nixos:~/Téléchargements/platform-tools]$ ./fastboot --version
GNU C Library (GNU libc) stable release version 2.27.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 5.3.0.
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

[tcip@nixos:~/Téléchargements/platform-tools]$ ./adb --version
Android Debug Bridge version 1.0.41
Version 29.0.5-5949299
Installed as /home/tcip/Téléchargements/platform-tools/adb

Any idea where I got wrong?

Thanks for the help, and all the best :slight_smile:

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libdl.so.2 ./fastboot 
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libpthread.so.0 ./fastboot           
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libm.so.6 ./fastboot             
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/librt.so.1 ./fastboot            
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libgcc_s.so.1 ./fastboot        

So you change the interpreter multiple times (this shouldn’t do anything useful)

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libc.so.6 ./fastboot

And end up with libc (instead of ld-linux) as interpreter (that doesn’t really work)

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-rpath "$(patchelf --print-rpath ./fastboot):$(dirname $(readlink -e /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/))" ./fastboot
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libdl.so.2 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libpthread.so.0 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libm.so.6 ./adb      
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/librt.so.1 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libgcc_s.so.1 ./adb
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/libc.so.6 ./adb    
[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-interpreter /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/ld-linux-x86-64.so.2 ./adb

And for fastboot you do the right thing in the end

[tcip@nixos:~/Téléchargements/platform-tools]$ patchelf --set-rpath "$(patchelf --print-rpath ./fastboot):$(dirname $(readlink -e /nix/store/pnd2kl27sag76h23wa5kl95a76n3k9i3-glibc-2.27/lib/))" ./fastboot

In general, you should set the interpreter to something sensible (ld-linux), and add the directories with libraries to rpath (like you do) without trying to do something with each library

hey @7c6f434c,

Thanks a lot for your answer – that’s a lot clearer to me, and everything is working now! NixOS is amazing, and so does its community :wink:.

All the best,