From: GNU Libc Maintainers Date: Tue, 4 Oct 2022 05:07:58 +0000 (+0100) Subject: git-static-pie X-Git-Tag: archive/raspbian/2.35-2+rpi1^2~74 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=2b76f3745d74ca26c35eb866da0d09fe8376e476;p=glibc.git git-static-pie commit af6b1cce9812273c7f597be6536d28eaec6fb89b Author: Samuel Thibault Date: Mon Aug 22 22:34:31 2022 +0200 hurd: Fix starting static binaries with stack protection enabled gcc introduces gs:0x14 accesses in most functions, so we need some tcbhead to be ready very early during initialization. This configures a static area which can be referenced by various protected functions, until proper TLS is set up. Gbp-Pq: Topic hurd-i386 Gbp-Pq: Name git-static-pie.diff --- diff --git a/sysdeps/mach/hurd/i386/init-first.c b/sysdeps/mach/hurd/i386/init-first.c index 534a796e0..09f52e6e3 100644 --- a/sysdeps/mach/hurd/i386/init-first.c +++ b/sysdeps/mach/hurd/i386/init-first.c @@ -42,6 +42,11 @@ extern int __libc_argc attribute_hidden; extern char **__libc_argv attribute_hidden; extern char **_dl_argv; +#ifndef SHARED +unsigned short __init1_desc; +static tcbhead_t __init1_tcbhead; +#endif + /* Things that want to be run before _hurd_init or much anything else. Importantly, these are called before anything tries to use malloc. */ DEFINE_HOOK (_hurd_preinit_hook, (void)); @@ -238,6 +243,13 @@ first_init (void) /* Initialize data structures so we can do RPCs. */ __mach_init (); +#ifndef SHARED + /* In the static case, we need to set up TLS early so that the stack + protection guard can be read at gs:0x14 by the gcc-generated snippets. */ + _hurd_tls_init(&__init1_tcbhead); + asm ("movw %%gs,%w0" : "=m" (__init1_desc)); +#endif + RUN_RELHOOK (_hurd_preinit_hook, ()); } @@ -280,6 +292,7 @@ strong_alias (posixland_init, __libc_init_first); This poorly-named function is called by static-start.S, which should not exist at all. */ void +inhibit_stack_protector _hurd_stack_setup (void) { intptr_t caller = (intptr_t) __builtin_return_address (0); diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h index 264ed9a9c..3817b7230 100644 --- a/sysdeps/mach/hurd/i386/tls.h +++ b/sysdeps/mach/hurd/i386/tls.h @@ -49,7 +49,6 @@ typedef struct mach_port_t reply_port; /* This thread's reply port. */ struct hurd_sigstate *_hurd_sigstate; } tcbhead_t; -#endif /* Return tcbhead_t from a TLS segment descriptor. */ # define HURD_DESC_TLS(desc) \ @@ -60,10 +59,18 @@ typedef struct }) /* Return 1 if TLS is not initialized yet. */ +#ifndef SHARED +extern unsigned short __init1_desc; +#define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds) || (gs) == __init1_desc) +#else +#define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds)) +#endif + #define __LIBC_NO_TLS() \ ({ unsigned short ds, gs; \ asm ("movw %%ds,%w0; movw %%gs,%w1" : "=q" (ds), "=q" (gs)); \ - __builtin_expect (ds == gs, 0); }) + __builtin_expect(__HURD_DESC_INITIAL(gs, ds), 0); }) +#endif /* The TCB can have any size and the memory following the address the thread pointer points to is unspecified. Allocate the TCB there. */