[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: static pie: confusion between _DYNAMIC, crt1.o, Scrt1.o



On Sat, 2022-10-22 at 16:06 +0200, Samuel Thibault wrote:
> Hello,
> 
> We're seeing a missing _DYNAMIC symbol here:
> 
> https://buildd.debian.org/status/fetch.php?pkg=gnupg2&arch=hurd-i386&ver=2.2.40-1&stamp=1666261690&raw=0
> 
> which is leaving me completely confused.
> 
> 
> gcc -I/usr/include  -I/usr/include  -Wall -Wno-format-zero-length -
> Wno-pointer-sign -Wpointer-arith -g -O2 -ffile-prefix-
> map=/<<PKGBUILDDIR>>=. -fstack-protector-strong -Wformat -
> Werror=format-security  -Wl,-z,relro -Wl,-z,now -pie -static -o gpgv
> gpgv.o build-packet.o compress.o  free-packet.o getkey.o keydb.o
> keyring.o seskey.o kbnode.o mainproc.o armor.o mdfilter.o textfilter.o
> progress.o misc.o rmd160.o openfile.o keyid.o parse-packet.o cpr.o
> plaintext.o sig-check.o keylist.o pkglue.o ecdh.o verify.o
> ../kbx/libkeybox.a ../common/libcommon.a ../regexp/libregexp.a
> ../common/libgpgrl.a -lz   -L/usr/lib/i386-gnu -lgcrypt -
> L/usr/lib/i386-gnu -lgpg-error
> [...]
> /usr/bin/ld: /usr/lib/i386-gnu/libcrt.a(dl-reloc-static-pie.o): in
> function `_dl_relocate_static_pie':
> (.text+0x31): undefined reference to `_DYNAMIC'
> 
> So this is static pie (-pie -static)

No it's not.  The GCC option for static PIE is -static-pie (no
whitespace between -static and -pie), not -pie -static.  See the
example:

$ cc hw.c -pie -static
$ file a.out 
a.out: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux),
statically linked, for GNU/Linux 5.19.0, with debug_info, not stripped
$ cc hw.c -static-pie 
$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux),
static-pie linked, for GNU/Linux 5.19.0, with debug_info, not stripped

> and I'm getting just the same
> issue with a mere int main(void) {} built with -static -pie.
> 
> 
> When debugging a bit on Debian Hurd:
> 
> $ gcc test.o -o test -pie -static -v
> [...]
>  /usr/lib/gcc/i686-gnu/12/collect2 -plugin /usr/lib/gcc/i686-
> gnu/12/liblto_plugin.so -plugin-opt=/usr/lib/gcc/i686-gnu/12/lto-
> wrapper -plugin-opt=-fresolution=/tmp/ccR0YAaP.res -plugin-opt=-pass-
> through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-
> through=-lc --build-id -m elf_i386 --hash-style=gnu --as-needed -
> static -o test /usr/lib/gcc/i686-gnu/12/../../../i386-gnu/Scrt1.o
> /usr/lib/gcc/i686-gnu/12/../../../i386-gnu/crti.o /usr/lib/gcc/i686-
> gnu/12/crtbeginT.o -L/usr/lib/gcc/i686-gnu/12 -L/usr/lib/gcc/i686-
> gnu/12/../../../i386-gnu -L/usr/lib/gcc/i686-gnu/12/../../.. -
> L/lib/i386-gnu -L/usr/lib/i386-gnu test.o --start-group -lgcc -lgcc_eh
> -lc --end-group /usr/lib/gcc/i686-gnu/12/crtend.o /usr/lib/gcc/i686-
> gnu/12/../../../i386-gnu/crtn.o
> 
> So this is using Scrt1.o

A static PIE should use rcrt1.o.  If you pass the correct -static-pie
option to GCC, GCC will use it.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


Reply to: