[PATCH v5 4/5] Fix assert during static startup
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Mon Feb 2 17:03:21 GMT 2026
On 30/01/26 14:21, Florian Weimer wrote:
> * Adhemerval Zanella:
>
>> diff --git a/sysdeps/generic/dl-mmap.h b/sysdeps/generic/dl-mmap.h
>> new file mode 100644
>> index 0000000000..f786be0930
>> --- /dev/null
>> +++ b/sysdeps/generic/dl-mmap.h
>> @@ -0,0 +1,34 @@
>> +/* mmap wrapper for dynamic loader.
>
>> +#ifndef _DL_MMAP_H
>> +#define _DL_MMAP_H
>> +
>> +#include <sys/mman.h>
>> +
>> +/* This mmap call is used to allocate some memory to backup assert() messages
>> + before TLS setup is done (which setup the thread pointer used by some ABIs
>> + to issues syscalls). */
>> +
>> +static inline void *
>> +_dl_mmap (void *addr, size_t len, int prot, int flags)
>> +{
>> + return __mmap (addr, len, prot, flags, -1, 0);
>> +}
>
>> diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c
>> index 3f0e302b5e..1ff20d4feb 100644
>> --- a/sysdeps/posix/libc_fatal.c
>> +++ b/sysdeps/posix/libc_fatal.c
>> @@ -16,7 +16,12 @@
>> License along with the GNU C Library; if not, see
>> <https://www.gnu.org/licenses/>. */
>>
>> +/* Mark symbols hidden in static PIE for early self relocation to work. */
>> +#if BUILD_PIE_DEFAULT
>> +# pragma GCC visibility push(hidden)
>> +#endif
>> #include <dl-writev.h>
>> +#include <dl-mmap.h>
>> #include <assert.h>
>> #include <ldsodefs.h>
>> #include <setvmaname.h>
>> @@ -24,6 +29,7 @@
>> #include <stdio.h>
>> #include <sys/uio.h>
>> #include <unistd.h>
>> +#include <dl-symbol-redir-ifunc.h>
>>
>> #ifdef FATAL_PREPARE_INCLUDE
>> #include FATAL_PREPARE_INCLUDE
>
> The bug is about static startup. Why do we need to change the dynamic
> loader?
Because of the shared infra used for PIE as well afaik.
>
> There's already sysdeps/unix/sysv/linux/dl-early_allocate.c, which seems
> closely related?
The mmap here is used solely to create the extra abort message in a different
memory mapping to facilitate parsing a core file.
The _dl_early_allocate would work, although I am not sure if the underlying
semantic change would break anything (by potentially using sbrk instead of
mmap).
It would require to disable stack-protector on dl-early_allocate.c and
maybe on sbrk.c as well; so I am not sure if this would be an improvement.
>
>> diff --git a/sysdeps/unix/sysv/linux/i386/dl-mmap.h b/sysdeps/unix/sysv/linux/i386/dl-mmap.h
>> new file mode 100644
>> index 0000000000..944f7deb27
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/i386/dl-mmap.h
>> @@ -0,0 +1,38 @@
>> +/* mmap wrapper for dynamic loader.
>
>> +#include <sys/mman.h>
>> +#include <mmap_internal.h>
>> +
>> +/* This mmap call is used to allocate some memory to backup assert() messages
>> + before TLS setup is done, so it can not use "call *%gs:SYSINFO_OFFSET"
>> + during startup in static PIE. */
>> +#if BUILD_PIE_DEFAULT
>> +# define I386_USE_SYSENTER 0
>> +#endif
>> +
>> +static inline void *
>> +_dl_mmap (void *addr, size_t len, int prot, int flags)
>> +{
>> + return (void *) MMAP_CALL (mmap2, addr, len, prot, flags, -1, 0);
>> +}
>
> Isn't it too late to define the macro here? As far as I can see, it's
> used in #if conditionals <sysdep.h>. Maybe check that <sysdep.h> isn't
> included at this point, via #ifdef/#error/#endif?
I think it makes sense to check for ifdef in this case. I will change it.
More information about the Libc-alpha
mailing list