JonathonReinhart / staticx

Create static executable from dynamic executable
https://staticx.readthedocs.io/
Other
323 stars 34 forks source link

Build fails on centos:8 container of Aarch64 #219

Open Jongy opened 2 years ago

Jongy commented 2 years ago

I'm trying to use staticx on a centos:8 container. Running pip install staticx (downloads the latest - 0.13.6) tries to build staticx and fails with this (excerpt):

...
    running build_bootloader
    gcc -o scons_build/debug/libnssfix/libnssfix.so -Wl,--no-as-needed -nostdlib -Wl,--no-undefined -shared scons_build/debug/libnssfix/nssfix.os -Lscons_build/debug/lib scons_build/debug/libnssfix/libc.so.6 -lnss_dns -lnss_files
    /bin/ld: cannot find -lnss_dns
    /bin/ld: cannot find -lnss_files
    collect2: error: ld returned 1 exit status
...

For reference, I encountered this while trying to upgrade our build from centos:7 to centos:8. I ran the same steps for centos:7 and got this: WARNING: NSS not available; staticx will not include nssfix for GLIBC programs! so it's possible that on centos:7 this the build of nssfix has never worked either, it might be something relating to Aarch64...

I spent some time searching about nss_dns and nss_files but couldn't find the package that provides them. At some point I realized that the relevant symbols might be provided from libc.a itself, and decided to fool ld by running ar rcs /lib64/libnss_files.a; ar rcs /lib64/libnss_dns.a. The build then passed :shrug:

Reproducing Dockerfile (excerpts from https://github.com/Granulate/gprofiler/pull/314/files#diff-80f450bf3cbbeb863190c6fca06ff64231e4f27b64e118a4bb724e62e0eee9f4 which uses 0.12.1):

FROM centos:8
RUN if grep -q "CentOS Linux 8" /etc/os-release ; then \
    sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*; \
    sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*; \
    yum install -y dnf-plugins-core; \
    dnf config-manager --set-enabled powertools; \
    fi
RUN yum install -y epel-release
RUN yum install -y gcc python3 curl python3-pip patchelf python3-devel upx
RUN yum install -y glibc-static zlib-devel.aarch64
RUN python3 -m pip install 'wheel==0.37.0' 'scons==4.2.0'
RUN python3 -m pip install 'staticx==0.12.1'

Full error log:

Step 7/7 : RUN python3 -m pip install 'staticx==0.12.1'
 ---> Running in 187637213de0
WARNING: Running pip install with root privileges is generally not a good idea. Try `__main__.py install --user` instead.
Collecting staticx==0.12.1
  Downloading https://files.pythonhosted.org/packages/fd/87/29919b86b8180e0c7a64b912935630a5fcbe248ed244a1319bc1cfb0a040/staticx-0.12.1.tar.gz (62kB)
Collecting pyelftools (from staticx==0.12.1)
  Downloading https://files.pythonhosted.org/packages/40/22/6706cc385a099bc5bcb0ea179c2c11863b62c79d37bd47fe464c16f9798d/pyelftools-0.28-py2.py3-none-any.whl (155kB)
Building wheels for collected packages: staticx
  Running setup.py bdist_wheel for staticx: started
  Running setup.py bdist_wheel for staticx: finished with status 'error'
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-lwxl4nl1/staticx/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmponwnj4iopip-wheel- --python-tag cp36:
  running bdist_wheel
  running build
  running build_bootloader
  gcc -o scons_build/debug/bootloader/error.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/error.c
  gcc -o scons_build/debug/bootloader/elfutil.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/elfutil.c
  gcc -o scons_build/debug/bootloader/extract.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/extract.c
  gcc -o scons_build/debug/bootloader/main.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/main.c
  gcc -o scons_build/debug/bootloader/mmap.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/mmap.c
  gcc -o scons_build/debug/bootloader/util.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -static -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Ilibtar -Ilibxz bootloader/util.c
  gcc -o scons_build/debug/libtar/block.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/block.c
  gcc -o scons_build/debug/libtar/decode.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/decode.c
  gcc -o scons_build/debug/libtar/extract.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/extract.c
  gcc -o scons_build/debug/libtar/handle.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/handle.c
  gcc -o scons_build/debug/libtar/output.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/output.c
  gcc -o scons_build/debug/libtar/util.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/util.c
  gcc -o scons_build/debug/libtar/compat/strlcpy.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/compat/strlcpy.c
  gcc -o scons_build/debug/libtar/compat/strmode.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libtar/compat -Ilibtar/compat libtar/compat/strmode.c
  ar rc scons_build/debug/libtar/libtar.a scons_build/debug/libtar/block.o scons_build/debug/libtar/decode.o scons_build/debug/libtar/extract.o scons_build/debug/libtar/handle.o scons_build/debug/libtar/output.o scons_build/debug/libtar/util.o scons_build/debug/libtar/compat/strlcpy.o scons_build/debug/libtar/compat/strmode.o
  ranlib scons_build/debug/libtar/libtar.a
  Install file: "scons_build/debug/libtar/libtar.a" as "scons_build/debug/lib/libtar.a"
  gcc -o scons_build/debug/libxz/xz_crc32.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' libxz/xz_crc32.c
  gcc -o scons_build/debug/libxz/xz_dec_lzma2.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' libxz/xz_dec_lzma2.c
  gcc -o scons_build/debug/libxz/xz_dec_stream.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' libxz/xz_dec_stream.c
  gcc -o scons_build/debug/libxz/xz_dec_bcj.o -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' libxz/xz_dec_bcj.c
  ar rc scons_build/debug/libxz/libxz.a scons_build/debug/libxz/xz_crc32.o scons_build/debug/libxz/xz_dec_lzma2.o scons_build/debug/libxz/xz_dec_stream.o scons_build/debug/libxz/xz_dec_bcj.o
  ranlib scons_build/debug/libxz/libxz.a
  Install file: "scons_build/debug/libxz/libxz.a" as "scons_build/debug/lib/libxz.a"
  gcc -o scons_build/debug/bootloader/bootloader -static scons_build/debug/bootloader/error.o scons_build/debug/bootloader/elfutil.o scons_build/debug/bootloader/extract.o scons_build/debug/bootloader/main.o scons_build/debug/bootloader/mmap.o scons_build/debug/bootloader/util.o -Lscons_build/debug/lib -ltar -lxz
  scons_build/debug/lib/libtar.a(output.o): In function `th_print_long_ls':
  /tmp/pip-build-lwxl4nl1/staticx/libtar/output.c:59: warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  scons_build/debug/lib/libtar.a(decode.o): In function `th_get_gid':
  /tmp/pip-build-lwxl4nl1/staticx/libtar/decode.c:65: warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  scons_build/debug/lib/libtar.a(decode.o): In function `th_get_uid':
  /tmp/pip-build-lwxl4nl1/staticx/libtar/decode.c:49: warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  scons_build/debug/lib/libtar.a(output.o): In function `th_print_long_ls':
  /tmp/pip-build-lwxl4nl1/staticx/libtar/output.c:52: warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  Creating 'scons_build/debug/libnssfix/incgen/nsswitch_conf.h'
  Creating 'scons_build/debug/libnssfix/libc.so.6_stub.c'
  gcc -o scons_build/debug/libnssfix/libc.so.6_stub.os -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -fno-builtin -fPIC -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' scons_build/debug/libnssfix/libc.so.6_stub.c
  gcc -o scons_build/debug/libnssfix/libc.so.6 -Wl,-soname=libc.so.6 -nostdlib -shared scons_build/debug/libnssfix/libc.so.6_stub.os -Lscons_build/debug/lib
  gcc -o scons_build/debug/libnssfix/nssfix.os -c -std=gnu99 -Wall -Werror -Wmissing-prototypes -Wstrict-prototypes -g -fPIC -U_FORTIFY_SOURCE -DDEBUG=1 -DSTATICX_VERSION='"0.12.1"' -Iscons_build/debug/libnssfix/incgen libnssfix/nssfix.c
  gcc -o scons_build/debug/libnssfix/libnssfix.so -Wl,--no-as-needed -nostdlib -Wl,--no-undefined -shared scons_build/debug/libnssfix/nssfix.os -Lscons_build/debug/lib scons_build/debug/libnssfix/libc.so.6 -lnss_dns -lnss_files
  /bin/ld: cannot find -lnss_dns
  /bin/ld: cannot find -lnss_files
  collect2: error: ld returned 1 exit status
  scons: *** [scons_build/debug/libnssfix/libnssfix.so] Error 1
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 177, in <module>
      'bdist_wheel':      bdist_wheel_hook,
    File "/usr/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
      return distutils.core.setup(**attrs)
    File "/usr/lib64/python3.6/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/usr/lib64/python3.6/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/usr/local/lib/python3.6/site-packages/wheel/bdist_wheel.py", line 299, in run
      self.run_command('build')
    File "/usr/lib64/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 50, in run
      self.run_command('build_bootloader')
    File "/usr/lib64/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 45, in run
      check_call(args)
    File "/usr/lib64/python3.6/subprocess.py", line 311, in check_call
      raise CalledProcessError(retcode, cmd)
  subprocess.CalledProcessError: Command '['scons', '-Q', 'STATICX_VERSION=0.12.1']' returned non-zero exit status 2.

  ----------------------------------------
  Failed building wheel for staticx
  Running setup.py clean for staticx
Failed to build staticx
Installing collected packages: pyelftools, staticx
  Running setup.py install for staticx: started
    Running setup.py install for staticx: finished with status 'error'
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-lwxl4nl1/staticx/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-zmxx8lku-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_bootloader
    gcc -o scons_build/debug/libnssfix/libnssfix.so -Wl,--no-as-needed -nostdlib -Wl,--no-undefined -shared scons_build/debug/libnssfix/nssfix.os -Lscons_build/debug/lib scons_build/debug/libnssfix/libc.so.6 -lnss_dns -lnss_files
    /bin/ld: cannot find -lnss_dns
    /bin/ld: cannot find -lnss_files
    collect2: error: ld returned 1 exit status
    scons: *** [scons_build/debug/libnssfix/libnssfix.so] Error 1
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 177, in <module>
        'bdist_wheel':      bdist_wheel_hook,
      File "/usr/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
        return distutils.core.setup(**attrs)
      File "/usr/lib64/python3.6/distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/usr/lib64/python3.6/distutils/dist.py", line 955, in run_commands
        self.run_command(cmd)
      File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/usr/lib/python3.6/site-packages/setuptools/command/install.py", line 61, in run
        return orig.install.run(self)
      File "/usr/lib64/python3.6/distutils/command/install.py", line 556, in run
        self.run_command('build')
      File "/usr/lib64/python3.6/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 50, in run
        self.run_command('build_bootloader')
      File "/usr/lib64/python3.6/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/tmp/pip-build-lwxl4nl1/staticx/setup.py", line 45, in run
        check_call(args)
      File "/usr/lib64/python3.6/subprocess.py", line 311, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['scons', '-Q', 'STATICX_VERSION=0.12.1']' returned non-zero exit status 2.

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-lwxl4nl1/staticx/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-zmxx8lku-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-lwxl4nl1/staticx/
liujin1993 commented 2 years ago

I reslove it in ubuntu:22.04 through:

cd /lib/aarch64-linux-gnu/
ln -s libnss_dns.so.2 libnss_dns.so
ln -s libnss_files.so.2 libnss_files.so
Jongy commented 1 year ago

For the record, in https://github.com/Granulate/gprofiler/pull/314 I ended up resolving it by creating empty .a files: ar rcs /lib64/libnss_files.a && ar rcs /lib64/libnss_dns.a because the symbols seem to arrive from a different library anyway.