[PATCH] Various TLS-related fixes

Christian Seiler <[email protected]> Sun, 7 Feb 2016 01:39:37 +0100
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Hi,

While trying to build dietlibc using qemu-user binfmt chroots on
various architectures, I've stumbled upon a couple of issues:

1. Newer versions of dietlibc now define errno as __thread if
   WANT_TLS is defined (which is semantically correct), but that
   means that initialization code for TLS has to be present for
   all architectures if WANT_TLS is defined - otherwise dietlibc.a
   will be built, but running programs linked against dietlibc.a
   will fail as soon as they try to access e.g. errno. (Even if
   they are single threaded!)

   Patch to add the proper TLS initialization code for ppc32,
   mips and mipsel:

   https://gist.githubusercontent.com/chris-se/178b71c67a110f7c2dae/raw/a01d3fe63362fb25bc95082d1747e6fecddfe76d/tls_fixes.patch

   (Doesn't mean threads will actually work there, on e.g.
   powerpc threads still don't work at all, but at least programs
   using errno don't crash now if WANT_TLS is set - which they
   didn't use to up until relatively recently, when
   __errno_location was always a weak symbol.)

2. Defining WANT_SSP but not WANT_TLS doesn't work right now,
   because libpthread won't build (true, libpthread isn't that
   useful w/o WANT_TLS, but the Makefile builds it regardless),
   because of a missing include and a syntax error.

   Patch that fixes this:

   https://gist.githubusercontent.com/chris-se/7fca4c24d2609e650b58/raw/f63a8c4f650ec787f9687f5612793df9357fe9db/notls_but_ssp_compiler_errors.patch

3. On amd64 __testandset (used e.g. by pthread_once) is completely
   broken (there is a linker warning that it's not tested ;-)),
   because it does

     xchg %rax,(%rdi)

   so it will do a full 64bit swap. Unfortunately, %rdi points to
   a 32 bit integer on the stack, and if that is the first
   variable of the function (e.g. in libpthread/test-basic.c's
   0 function), then the least significant 32bits of the stack
   base pointer the function stored away will be overwritten with
   lots of zeros, causing the program to crash.

   Patch can be found (now removes the linker warning, because
   it's now tested):

   https://gist.githubusercontent.com/chris-se/f45b1fadd36c73dbea9c/raw/d36ec1b58908bc6bcbfd7c3c346afb7cd778486b/fix_testandset_amd64.patch

   (Note that __testandset is only used by libpthread and only
   with assumed 32 bit int as pointer parameter, so this is
   correct.)

There are some other threading issues that I'm still looking into,
but these fixes are now well-tested.

Christian