From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13495 Path: news.gmane.org!.POSTED!not-for-mail From: argante Newsgroups: gmane.linux.lib.musl.general Subject: Re: static linking problem Date: Sun, 02 Dec 2018 17:20:15 +0000 Message-ID: References: <20181202001821.GV23599@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1543771119 27649 195.159.176.226 (2 Dec 2018 17:18:39 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Dec 2018 17:18:39 +0000 (UTC) To: "musl@lists.openwall.com" Original-X-From: musl-return-13511-gllmg-musl=m.gmane.org@lists.openwall.com Sun Dec 02 18:18:34 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1gTVO4-00072l-Hd for gllmg-musl@m.gmane.org; Sun, 02 Dec 2018 18:18:34 +0100 Original-Received: (qmail 19584 invoked by uid 550); 2 Dec 2018 17:20:39 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 19566 invoked from network); 2 Dec 2018 17:20:38 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=proton; t=1543771220; bh=hZFNXw2ZB7XHlmQoYmk7IKZGfXs3BfoZw/9lPtT5a3M=; h=Date:To:From:Reply-To:Subject:In-Reply-To:References:Feedback-ID: From; b=RJRbqKnl7MY5vk6ZAF2orqc5T/6m0CY79BXHp4K5bEsuTk2yUoaIzfawAWCVanQJu xvYRtZnmLArxbGL8j8St8tSgPqnd7psRHLnFOik45PMGS893xY/n9/qNyQHYZBO7DO gHx8af9J6pkaWT8kKUxsj3XSWxSEQQ1kSnnlX+F0W8rmkO4irWCDI2QcMKfan+9a5p lBJ957XnPoL6rPbenpLERj36MgXmf30PwsTk9MoTnAmo4zful4DvRBAoCrHqgJP67+ Ob+HDcHylAg3FykOEVd6iL1CRTWk5A5ct6HBVy0Pz6fgOKRYDxX7/s8ta0v/akUN0a cO4bSThol98WA== In-Reply-To: <20181202001821.GV23599@brightrain.aerifal.cx> Feedback-ID: sj1YN_iN560_iv4ir5EpCpCFj2RELVzNnBQpDIwMoUMNXowIBSidTdxSWFyLT3J_uOeJsE1taahISy929bGTAA==:Ext:ProtonMail Xref: news.gmane.org gmane.linux.lib.musl.general:13495 Archived-At: =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original Me= ssage =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 On Sunday, 2 December 2018 01:18, Rich Felker wrote: > On Sat, Dec 01, 2018 at 10:07:53PM +0000, argante wrote: > This does not indicate that the program is dynamic-linked, just that > it's PIE. Use readelf -l and look for INTERP. If it's present, the > program is dynamic linked. If it's absent, the program is static > linked. > > > ldd a.out > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > ldd (0x7fb6c936f000) > > Is this the musl ldd? I believe it will do this or similar, and that's > a known bug or at least limitation. It should report that the program > is not dynamic-linked. > > > /lib/ld-musl-x86_64.so.1 (0x7ff0dca25000) > > libc.so =3D> /lib/ld-musl-x86_64.so.1 (0x7ff0dca25000) > > This is dynamic-linked, yes. But there's nothing wrong with your > static-linked program. If you really don't want pie, use -no-pie or a > toolchain that wasn't built to produce pie by default (gcc's > --enable-default-pie option at configure time). > > Rich Rich, thank you for your help. I did everything step by step from scratch. Maybe it's a long mail, but sho= ws stages and results. I have a basic tool (make, cp, rm, sed etc.) and musl-cross toolchain in /c= ross dir (plus symlinks in cross/bin). # ls /cross/ bin include lib libexec share x86_64-linux-musl # # cp libc.so from /cross/x86_64-linux-musl/lib/ to /lib # ls -l /lib/ lrwxrwxrwx 1 root root 12 Dec 2 09:11 ld-musl-x86_64.so.1= -> /lib/libc.so -rwxr-xr-x 1 root root 740888 Dec 2 09:10 libc.so I also prepared simple scripts in /usr/ports: # ls /usr/ports/ binutils gcc gmp mpc mpfr musl MUSL ---- # cd /usr/ports/musl/ # ls build.sh musl-1.1.20.tar.gz # cat build.sh #!/bin/sh name=3D$(basename $(pwd)) version=3D"1.1.20" rm -rf $name-$version tar zxf $name-$version.tar.gz cd $name-$version ./configure \ =09--bindir/usr/bin \ =09--includedir=3D/usr/include \ =09--syslibdir=3D/lib \ =09--libdir=3D/lib \ =09--enable-gcc-wrapper \ =09--disable-debug \ =09--enable-optimize \ =09|| exit 1 make || exit 1 make install || exit 1 cd .. && rm -rf $name-$version # ./build.sh # ls -l /lib/ -rw-r--r-- 1 root root 1624 Dec 2 11:48 Scrt1.o -rw-r--r-- 1 root root 1576 Dec 2 11:48 crt1.o -rw-r--r-- 1 root root 1016 Dec 2 11:48 crti.o -rw-r--r-- 1 root root 960 Dec 2 11:48 crtn.o lrwxrwxrwx 1 root root 12 Dec 2 09:11 ld-musl-x86_64.so.1= -> /lib/libc.so -rw-r--r-- 1 root root 2573588 Dec 2 11:48 libc.a -rwxr-xr-x 1 root root 737368 Dec 2 11:48 libc.so -rw-r--r-- 1 root root 8 Dec 2 11:48 libcrypt.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libdl.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libm.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libpthread.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libresolv.a -rw-r--r-- 1 root root 8 Dec 2 11:48 librt.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libutil.a -rw-r--r-- 1 root root 8 Dec 2 11:48 libxnet.a -rw-r--r-- 1 root root 539 Dec 2 11:48 musl-gcc.specs -rw-r--r-- 1 root root 2192 Dec 2 11:48 rcrt1.o # /lib/ld-musl-x86_64.so.1 musl libc (x86_64) Version 1.1.20 Dynamic Program Loader Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname [args] GMP --- # ls build.sh gmp-6.1.2.tar.gz # cat build.sh #!/bin/sh name=3D$(basename $(pwd)) version=3D"6.1.2" rm -rf $name-$version tar zxfp $name-$version.tar.gz cd $name-$version || exit 1 ./configure \ =09--disable-shared || exit 1 make || exit 1 make install || exit 1 cd .. && rm -rf $name-$version MPFR ---- #!/bin/sh name=3D$(basename $(pwd)) version=3D"3.1.4" rm -rf $name-$version tar zxfp $name-$version.tar.gz cd $name-$version || exit 1 CFLAGS=3D"-I/usr/local/include" \ LDFLAGS=3D"-L/usr/local/lib" \ ./configure \ =09--disable-shared || exit 1 make || exit 1 make install || exit 1 cd .. && rm -rf $name-$version MPC --- #!/bin/sh name=3D$(basename $(pwd)) version=3D"1.0.3" rm -rf $name-$version tar zxfp $name-$version.tar.gz cd $name-$version || exit 1 CFLAGS=3D"-I/usr/local/include" \ LDFLAGS=3D"-L/usr/local/lib" \ ./configure \ =09--disable-shared || exit 1 make || exit 1 make install || exit 1 cd .. && rm -rf $name-$version BINUTILS -------- I suppose it could be a problem with ld. In this test I used patches from A= lpine: https://git.alpinelinux.org/cgit/aports/tree/main/binutils # ls binutils-2.31.1.tar.gz x86-Add-a-GNU_PROPERTY_X86_ISA_1_USED= -note-if-needed.patch binutils-ld-fix-static-linking.patch x86-Properly-add-X86_ISA_1_NEEDED-pro= perty.patch build.sh x86-Properly-merge-GNU_PROPERTY_X86_I= SA_1_USED.patch # cat build.sh #!/bin/sh name=3D$(basename $(pwd)) version=3D"2.31.1" rm -rf $name-$version tar zxfp $name-$version.tar.gz cd $name-$version || exit 1 patch -p1 -i ../binutils-ld-fix-static-linking.patch patch -p1 -i ../x86-Add-a-GNU_PROPERTY_X86_ISA_1_USED-note-if-needed.patch patch -p1 -i ../x86-Properly-add-X86_ISA_1_NEEDED-property.patch patch -p1 -i ../x86-Properly-merge-GNU_PROPERTY_X86_ISA_1_USED.patch ./configure \ =09--disable-shared \ =09--disable-static \ =09--disable-nls \ =09--disable-install-libiberty \ =09--enable-threads \ =09--disable-obsolete \ =09--enable-lto \ =09--enable-plugins \ =09--disable-multilib make || exit 1 make install || exit 1 cd .. && rm -rf $name-$version GCC --- # ls 0001-ssp_nonshared.diff 0010-static-pie-= support.diff 0002-posix_memalign.diff 0011-j2.diff 0003-cilkrts.diff 0012-s390x-musll= dso.diff 0004-libatomic-test-fix.diff 0013-microblaze-= pr65649.diff 0005-libgomp-test-fix.diff 0014-ldbl128-con= fig.diff 0006-libitm-test-fix.diff 0015-m68k.diff 0007-libvtv-test-fix.diff 0016-invalid_tls= _model.diff 0008-Revert-PR-driver-81523-Make-static-override-pie.diff build.sh 0009-Revert-RS6000-linux-startfile-endfile.diff gcc-7.3.0.tar.gz # cat build.sh #!/bin/sh version=3D"7.3.0" rm -rf gcc-$version tar zxfvp gcc-${version}.tar.gz || exit 1 cd gcc-${version} patch -p1 -i ../0001-ssp_nonshared.diff patch -p1 -i ../0002-posix_memalign.diff #patch -p1 -i ../0003-cilkrts.diff patch -p1 -i ../0004-libatomic-test-fix.diff patch -p1 -i ../0005-libgomp-test-fix.diff patch -p1 -i ../0006-libitm-test-fix.diff patch -p1 -i ../0007-libvtv-test-fix.diff patch -p1 -i ../0008-Revert-PR-driver-81523-Make-static-override-pie.diff patch -p1 -i ../0009-Revert-RS6000-linux-startfile-endfile.diff patch -p1 -i ../0010-static-pie-support.diff patch -p1 -i ../0011-j2.diff patch -p1 -i ../0012-s390x-muslldso.diff patch -p1 -i ../0013-microblaze-pr65649.diff patch -p1 -i ../0014-ldbl128-config.diff patch -p1 -i ../0015-m68k.diff patch -p1 -i ../0016-invalid_tls_model.diff ./configure \ =09CFLAGS=3D"-Os -g0" \ =09CXXFLAGS=3D"${CFLAGS}" \ =09--enable-languages=3Dc,c++ \ =09--disable-nls \ =09--disable-multilib \ =09--disable-werror \ =09--disable-libmudflap \ =09--disable-libsanitizer \ =09--disable-gnu-indirect-function \ =09--disable-libmpx \ =09--disable-libssp \ =09--disable-symver \ =09--disable-libgomp \ =09--disable-fixed-point \ =09--enable-tls \ =09--enable-deterministic-archives \ =09--enable-__cxa_atexit \ =09--enable-default-pie \ =09--build=3Dx86_64-linux-musl \ =09--with-mpc=3D/usr/local make -j4 || exit 1 make install || exit 1 cd .. && rm -rf gcc-$version # gcc -v Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/local/libexec/gcc/x86_64-linux-musl/7.3.0/lto-wr= apper Target: x86_64-linux-musl Configured with: ./configure CFLAGS=3D'-Os -g0' CXXFLAGS=3D --enable-langua= ges=3Dc,c++ --disable-nls --disable-multilib --disable-werror --disable-lib= mudflap --disable-libsanitizer --disable-gnu-indirect-function --disable-li= bmpx --disable-libssp --disable-symver --disable-libgomp --disable-fixed-po= int --enable-tls --enable-deterministic-archives --enable-__cxa_atexit --en= able-default-pie --build=3Dx86_64-linux-musl --with-mpc=3D/usr/local Thread model: posix gcc version 7.3.0 (GCC) tests: =3D=3D=3D=3D=3D=3D # cat test.c #include int main() { printf("hello..\n"); return 0; } 1. musl-cross gcc ----------------- # gcc -static test.c # ldd a.out ldd: a.out: Not a valid dynamic program # gcc -static -fpie test.c # ldd a.out ldd: a.out: Not a valid dynamic program # readelf -l a.out Elf file type is EXEC (Executable file) Entry point 0x400154 There are 4 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 0x0000000000000f0c 0x0000000000000f0c R E 200000 LOAD 0x0000000000000fe0 0x0000000000600fe0 0x0000000000600fe0 0x0000000000000128 0x0000000000000838 RW 200000 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 10 GNU_RELRO 0x0000000000000fe0 0x0000000000600fe0 0x0000000000600fe0 0x0000000000000020 0x0000000000000020 R 1 Section to Segment mapping: Segment Sections... 00 .init .text .fini .rodata .eh_frame 01 .ctors .dtors .data .bss 02 03 .ctors .dtors 2. New toolchain ---------------- # gcc test.c # ldd a.out =09/lib/ld-musl-x86_64.so.1 (0x7f342831a000) =09libc.so =3D> /lib/ld-musl-x86_64.so.1 (0x7f342831a000) # gcc -static test.c /usr/local/bin/ld: /lib/libc.a(__libc_start_main.o): relocation R_X86_64_32= against `.rodata.__init_libc.str1.1' can not be used when making a PIE obj= ect; recompile with -fPIC /usr/local/bin/ld: /lib/libc.a(exit.o): relocation R_X86_64_32 against unde= fined hidden symbol `__fini_array_end' can not be used when making a PIE ob= ject /usr/local/bin/ld: /lib/libc.a(puts.o): relocation R_X86_64_32 against hidd= en symbol `__stdout_FILE' can not be used when making a PIE object /usr/local/bin/ld: /lib/libc.a(__init_tls.o): relocation R_X86_64_32S again= st undefined hidden symbol `__libc' can not be used when making a PIE objec= t /usr/local/bin/ld: /lib/libc.a(__stdout_write.o): relocation R_X86_64_32S a= gainst hidden symbol `__stdio_write' can not be used when making a PIE obje= ct /usr/local/bin/ld: /lib/libc.a(ofl.o): relocation R_X86_64_32 against `.bss= .ofl_lock' can not be used when making a PIE object; recompile with -fPIC /usr/local/bin/ld: final link failed: nonrepresentable section on output collect2: error: ld returned 1 exit status I recompiled musl (with a new compiler), and now: # gcc -static -fno-pie test.c /usr/local/bin/ld: /tmp/ccJKiFnE.o: relocation R_X86_64_32 against `.rodata= ' can not be used when making a PIE object; recompile with -fPIC /usr/local/bin/ld: final link failed: nonrepresentable section on output collect2: error: ld returned 1 exit status # gcc -static test.c # ldd a.out =09ldd (0x7f76e24c3000) # ls -lh /usr/bin/ldd lrwxrwxrwx 1 root root 12 Dec 2 11:26 /usr/bin/ldd -> /li= b/libc.so # readelf -l a.out Elf file type is DYN (Shared object file) Entry point 0x1062 There are 7 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000308 0x0000000000000308 R 0x1000 LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 0x0000000000000f7f 0x0000000000000f7f R E 0x1000 LOAD 0x0000000000002000 0x0000000000002000 0x0000000000002000 0x0000000000000094 0x0000000000000094 R 0x1000 LOAD 0x0000000000002e58 0x0000000000003e58 0x0000000000003e58 0x00000000000002d8 0x00000000000009e0 RW 0x1000 DYNAMIC 0x0000000000002e80 0x0000000000003e80 0x0000000000003e80 0x0000000000000150 0x0000000000000150 RW 0x8 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 0x10 GNU_RELRO 0x0000000000002e58 0x0000000000003e58 0x0000000000003e58 0x00000000000001a8 0x00000000000001a8 R 0x1 Section to Segment mapping: Segment Sections... 00 .hash .gnu.hash .dynsym .dynstr .rela.dyn 01 .init .plt .plt.got .text .fini 02 .rodata .eh_frame 03 .ctors .dtors .data.rel.ro .dynamic .got .got.plt .data .bss 04 .dynamic 05 06 .ctors .dtors .data.rel.ro .dynamic .got I still can't understand why musl-cross ldd works correctly, and new toolch= ain shows such a strange result. Did I overlook something? I even tried -Wl= ,--no-dynamic-linker. Best regards