milahu / nixpkgs

Nix Packages collection
MIT License
1 stars 0 forks source link

gdb fails on wrapped executables: not in executable format: file format not recognized #48

Open milahu opened 7 months ago

milahu commented 7 months ago
$ gdb --args $(which wine)
"/nix/store/fa1fn9p561cy7nlzvdgmkgzpy20fxcm5-wine-9.0/bin/wine": not in executable format: file format not recognized

because wine is a wrapper for .wine

$ head -n1 $(which wine); tail -n1 $(which wine)
#! /nix/store/bdzvgpz8y5qd4iy4p59zl74l2qk5gcgy-bash-5.2-p21/bin/bash -e
exec "/nix/store/fa1fn9p561cy7nlzvdgmkgzpy20fxcm5-wine-9.0/bin/.wine"  "$@" 
$ file -b /nix/store/fa1fn9p561cy7nlzvdgmkgzpy20fxcm5-wine-9.0/bin/.wine
ELF 32-bit LSB pie executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /nix/store/qcssalnxr02k3fyr3n6lvrb4p842k8n2-glibc-2.38-27/lib/ld-linux.so.2, for GNU/Linux 3.10.0, not stripped

upstream issue: https://github.com/NixOS/nixpkgs/issues/95027 by @davidak

possible solutions

gdb bash

https://developers.redhat.com/articles/2022/12/27/debugging-binaries-invoked-scripts-gdb#debugging_programs_invoked_via_fork_and_exec_from_a_wrapper_script

gdb --args $(which bash) $(which wine)

ideally gdb should have an option to parse the shebang line of the wrapper script and automatically call the script interpreter

not all wrappers use exec so catch exec will not always work break main is more generic

modify the wrapper script

copy the wrapper script and replace exec with exec gdb --args

cd $(mktemp -d)
cp $(which wine) wine.sh
sed -i 's/exec /exec gdb --args /' wine.sh
./wine.sh asdf.exe

or use writable-nix-store to modify the wrapper script in /nix/store

example: chromium-wrapper.sh

gdb exec-wrapper

https://reverseengineering.stackexchange.com/questions/19497/gdb-dont-break-when-i-use-exec-wrapper-script-to-exec-my-target-binary

less useful

this requires the path of the wrapped binary