Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting SIGSEGV segmentation fault on staticx generated executable #232

Open
githubweather opened this issue Jan 10, 2023 · 6 comments
Open
Labels
awaiting feedback patchelf Issues related to patchelf

Comments

@githubweather
Copy link

(1) Generated a dynamically linked executable using pyinstaller on a Python script on a Linux Debian x86_64 machine
(2) This dynamically linked executable is running ok on the x86_64 machine
(3) But, want to generate a statically linked executable that I will be able to run on a mobile device
(4) Ran staticx (v0.13.8) on the dynamically linked executable. The resulting statically linked executable is generating the error seen in the title.
(5) Tried adding "-l lib..." in the staticx call, i.e., the libs I see when I do "ldd" on the dynamically linked executable.
(6) But still getting the seg fault.

@githubweather
Copy link
Author

githubweather commented Jan 11, 2023

For reference and the particulars, (maybe someone can see something wrong I'm doing) this is the pyinstaller call:

pyinstaller --clean -F --hidden-import=cftime --hidden-import=cftime._strptime --add-binary="/home/me/PYTHON3.11/lib/libpython3.11.so.1.0:." python_script

As mentioned above, the dynamically linked executable produced from this pyinstaller call runs fine on Debian Linux x86_64.

Then my staticx call is as follows:

staticx python_script -l /usr/lib/x86_64-linux-gnu/libnss_dns.so -l /usr/lib/x86_64-linux-gnu/libresolv.so -l /lib/x86_64-linux-gnu/libz.so.1 -l /lib/x86_64-linux-gnu/libpthread.so.0 -l  /lib/x86_64-linux-gnu/libc.so.6 -l /lib64/ld-linux-x86-64.so.2 python_script.exe --loglevel DEBUG

Then, when executing the resultant statically linked executable, python_script.exe on Debian Linux x86_64, I get the segmentation fault. When I run it with gdb, I see the segmentation fault is due to SIGSEGV.

@JonathonReinhart
Copy link
Owner

Please see https://staticx.readthedocs.io/en/latest/troubleshooting.html and include all of the referenced debug output.

@githubweather
Copy link
Author

githubweather commented Jan 12, 2023

Thanks for those pointers; as suggested I have pasted in the debug output. Please see below:

Below is the staticx --debug output::

INFO:root:Running StaticX version 0.13.8
INFO:root:Libraries:
INFO:root:  elftools: 0.29
DEBUG:root:External tools:
INFO:root:  ldd: /usr/bin/ldd: ldd (Debian GLIBC 2.28-10+deb10u2) 2.28
INFO:root:  objcopy: /usr/bin/objcopy: GNU objcopy (GNU Binutils for Debian) 2.31.1
INFO:root:  strip: /usr/bin/strip: GNU strip (GNU Binutils for Debian) 2.31.1
INFO:root:  patchelf: /home/me/PYTHON3.11/bin/patchelf: patchelf 0.17.0
DEBUG:root:Arguments:
DEBUG:root:  prog:      'dyn_link'
DEBUG:root:  output:    'dyn_link.sx'
DEBUG:root:  libs:      None
DEBUG:root:  strip:     False
DEBUG:root:  compress:  True
DEBUG:root:  debug:     True
INFO:root:Using XZ BCJ filter FILTER_X86
DEBUG:root:Bootloader: bootloader version 0.13.8 compiled Aug  7 2022 at 04:42:01 by musl-gcc version 10.2.1 20210110
INFO:root:Program interpreter: /lib64/ld-linux-x86-64.so.2
DEBUG:root:Running ['patchelf', '--remove-rpath', '/tmp/staticx-prog-tmuq3jki']
DEBUG:root:Running ['patchelf', '--set-interpreter', 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', '--set-rpath', 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr', '--force-rpath', '/tmp/staticx-prog-tmuq3jki']
DEBUG:root:Running ['patchelf', '--no-default-lib', '/tmp/staticx-prog-tmuq3jki']
DEBUG:root:Program linked with GLIBC: Found libc.so.6 GLIBC_2.7
DEBUG:root:Running ['patchelf', '--add-needed', 'libnssfix.so', '/tmp/staticx-prog-tmuq3jki']
INFO:root:Adding libnssfix.so
DEBUG:root:Running ['ldd', '/tmp/libnssfix-s6ye067s.so']
DEBUG:root:Ignoring synthetic library: linux-vdso.so.1
INFO:root:Processing library libc.so.6 (/lib/x86_64-linux-gnu/libc.so.6)
INFO:root:Adding Symlink libc.so.6 => libc-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libc-2.28.so as libc-2.28.so
INFO:root:Processing library libnss_dns.so.2 (/lib/x86_64-linux-gnu/libnss_dns.so.2)
INFO:root:Adding Symlink libnss_dns.so.2 => libnss_dns-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libnss_dns-2.28.so as libnss_dns-2.28.so
INFO:root:Processing library libnss_files.so.2 (/lib/x86_64-linux-gnu/libnss_files.so.2)
INFO:root:Adding Symlink libnss_files.so.2 => libnss_files-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libnss_files-2.28.so as libnss_files-2.28.so
INFO:root:Processing library ld-linux-x86-64.so.2 (/lib64/ld-linux-x86-64.so.2)
INFO:root:Adding Symlink ld-linux-x86-64.so.2 => ld-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/ld-2.28.so as ld-2.28.so
INFO:root:Processing library libresolv.so.2 (/lib/x86_64-linux-gnu/libresolv.so.2)
INFO:root:Adding Symlink libresolv.so.2 => libresolv-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libresolv-2.28.so as libresolv-2.28.so
INFO:root:Adding /tmp/staticx-prog-tmuq3jki as dyn_link
DEBUG:root:Running ['ldd', 'dyn_link']
DEBUG:root:Ignoring synthetic library: linux-vdso.so.1
INFO:root:Processing library libdl.so.2 (/lib/x86_64-linux-gnu/libdl.so.2)
INFO:root:Adding Symlink libdl.so.2 => libdl-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libdl-2.28.so as libdl-2.28.so
INFO:root:Processing library libz.so.1 (/lib/x86_64-linux-gnu/libz.so.1)
INFO:root:Adding Symlink libz.so.1 => libz.so.1.2.11
INFO:root:Adding /lib/x86_64-linux-gnu/libz.so.1.2.11 as libz.so.1.2.11
INFO:root:Processing library libpthread.so.0 (/lib/x86_64-linux-gnu/libpthread.so.0)
INFO:root:Adding Symlink libpthread.so.0 => libpthread-2.28.so
INFO:root:Adding /lib/x86_64-linux-gnu/libpthread-2.28.so as libpthread-2.28.so
DEBUG:root:Running ['objcopy', '--add-section', '.staticx.archive=/tmp/staticx-archive-nxbqoaps.tar', '/tmp/staticx-output-ayorg8cc']

Script done on 2023-01-12 13:33:31-07:00 [COMMAND_EXIT_CODE="0"]

Below is the gdb output::

Reading symbols from dyn_link.sx...done.
(gdb) set follow-fork-mode child
(gdb) r
Starting program: /home/me/TESTING/dist/dyn_link.sx 
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt -full
No stack.
(gdb) quit

Script done on 2023-01-12 13:36:28-07:00 [COMMAND_EXIT_CODE="0"]

@JonathonReinhart
Copy link
Owner

I haven't looked closely yet, but there are a few things that I don't understand:

  1. Why are you passing this option to pyinstaller?

    • --add-binary="/home/me/PYTHON3.11/lib/libpython3.11.so.1.0:."
  2. Why are you passing these options to staticx?

    • -l /usr/lib/x86_64-linux-gnu/libnss_dns.so
    • -l /usr/lib/x86_64-linux-gnu/libresolv.so
    • -l /lib/x86_64-linux-gnu/libz.so.1
    • -l /lib/x86_64-linux-gnu/libpthread.so.0
    • -l /lib/x86_64-linux-gnu/libc.so.6
    • -l /lib64/ld-linux-x86-64.so.2

AFAIK none of this should be necessary, and could potentially be harmful.

What version of Debian? What version of pyinstaller and how was it installed?

@githubweather
Copy link
Author

(1) I passed the --add-binary option to pyinstaller hoping it might help me get over the segmentation fault issue but it did not.
(2) I passed these -l options as these were the libs I saw when I did "ldd" on the dynamically linked executable. But I later learned, as you pointed out to another user, that this is not necessary.

Given this, I reran pyinstaller without the --add-binary option and then ran staticx without any of the -l. options. But am still getting the segmentation fault.

Debian version:
Linux debian 4.19.0-23-amd64 #1 SMP Debian 4.19.269-1 (2022-12-20) x86_64 GNU/Linux

Pyinstaller:
version - 5.3
I believe I used pip3 to install pyinstaller.

@JonathonReinhart
Copy link
Owner

I wonder if this is the same(ish) issue as #243.

I see you're using a fairly new version of patchelf: patchelf 0.17.0. I wonder if there is a bug that manifests as the assertion reported by patchelf 0.17.2.

Please try upgrading your version of patchelf to 0.17.2. Or downgrading it to something older, in the 0.16.x line. Or install your Linux distro's version.

@JonathonReinhart JonathonReinhart added awaiting feedback patchelf Issues related to patchelf labels Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback patchelf Issues related to patchelf
Projects
None yet
Development

No branches or pull requests

2 participants