#1069912 copy_exec doesn't detect libgcc dependency anymore when pthread is used

#1069912#5
Date:
2024-04-26 23:54:32 UTC
From:
To:
Hey.

I stumbled over some odd issue, originally described here:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1068849#84

In short:
I use the argon2 tool inside the initramfs, into which I copy it via
initramfs-tools’ copy_exec function (defined in
/usr/share/initramfs-tools/hook-functions).

Now that function uses ldd to find out whether a binary is linked
against libpthread and if so, also includes libgcc because of #950254
(in short: pthread seems to dlopen() libgcc).

Now argon2 somehow doesn't indicate it uses libpthread:
$ ldd /usr/bin/argon2
	linux-vdso.so.1 (0x00007ffedb996000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f90666bb000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f90668cb000)

which I don't quite understand why.
Actually, even when building it manually, an explicitly -lpthread it never
shows up there.

No idea why... perhaps some linker optimisation that goes wrong?
I tried with -Wl,--no-as-needed, but that doesn't help either.
I can only force it to link if using
   gcc … -Wl,--no-as-needed /usr/lib/x86_64-linux-gnu/libpthread.so.0
i.e. the real path of the .so.


So in my case, pthread_exit() fails eventually (in the initramfs), with
an error that libgcc cannot be found.


Any ideas what to do?


Thanks,
Chris.

#1069912#10
Date:
2024-04-27 00:26:01 UTC
From:
To:
Hey.


I think I found the root cause (thanks to Guilhem Moulin, who
pointed[0] me to it).

Apparently (which wasn't on my radar at all ^^), glibc since 2.34
dropped libpthread. So the whole check you do in hook-functions[2] will
probably never work anymore, which hit me with my argon2 (the tool not
the lib).

Now pthread apparently dlopen()s stuff from gcc, so in my case
pthread_exit fails.

Guilhem already remembered that you've been reluctant to
unconditionally include libgcc - which I can understand (I also want to
keep my initramfs as small as possible) - but this breaks stuff and
apart from ugly manual hacks I don't see an easy way around it.

Could you please either reconsider adding it unconditionally, or is
there some other way to find out whether a binary uses pthread stuff?

If not, would it be possible to provide at least some simpler to use
interface to copy_libgcc? Where e.g. I'd just say:
copy_libgcc /usr/bin/argon2 and it would automatically find the right
libdir for the right arch?

Thanks,
Chris.


[0] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1068849#89
[1] https://lists.gnu.org/archive/html/info-gnu/2021-08/msg00001.html
[2] https://salsa.debian.org/kernel-team/initramfs-tools/-/blob/cf964bfb4362019fd7fba1e839e403ff950dca8e/hook-functions#L248-L249