Files
glibc/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h
T
Adhemerval Zanella 99e9c5d1ca Fix assert during static startup (BZ 33326)
The BZ#33326 testcase triggers an assertion during process startup,
which results in a segmentation fault instead of an error message
and process termination with a SIGABRT.  The assert issues
__libc_message_impl, which in turn might call string functions
depending on the ABI (strchrnul, strlen, memcpy/mempcpy), system
calls (writev and mmap), and finally the abort call.

The dl-symbol-redir-ifunc.h is also expanded to cover strchrnul on
x86_64, s390, powerpc64 (both endianness) and loongarch, mempcpy on
powerpc64be, and memcpy on aarch64.  On s390 the redirection is only
issued if the ifunc variant is built, since strchrnul-c.c only renames
the C implementation to STRCHRNUL_DEFAULT when HAVE_STRCHRNUL_IFUNC is
set.

The buffer that backs up the assert message is now allocated through
_dl_mmap, which issues the syscall directly instead of calling __mmap
(setting errno on failure requires the thread pointer).

The abort call now issues __raise_direct instead of raise (the Hurd
port aliases __raise_direct to raise).

On i386, syscalls should not use the vDSO during program startup because
the thread pointer is not yet initialized.  This requires __raise_direct,
_dl_writev, and _dl_mmap to be built with I386_USE_SYSENTER set to 0.

Creating a test case is challenging. For static-pie, the assert is only
called for ill-formed ELF files on elf_get_dynamic_info and by some targets
on ELF_DYNAMIC_RELOCATE (although not all targets use assert in their
dl-machine.h).  Some targets also issue __libc_fatal on ARCH_SETUP_IREL,
but also only for ill-formatted ELF files.

The test employs a different strategy and overrides the __tunables_init
symbol, which is invoked immediately before self-relocation and TLS setup.
The test is built with -Wl,-z,muldefs to avoid linker issues.

I checked on aarch64, x86_64, i686, s390x (qemu), sparc (qemu),
mips64el (qemu), armhf, riscv, and powerpc.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
2026-08-31 15:43:44 -03:00

89 lines
2.9 KiB
C

/* Symbol rediretion for loader/static initialization code.
Copyright (C) 2022-2026 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifndef _DL_IFUNC_GENERIC_H
#define _DL_IFUNC_GENERIC_H
#ifndef SHARED
#include <isa-level.h>
#if MINIMUM_X86_ISA_LEVEL >= 4
# define HAVE_MEMSET_IFUNC_GENERIC "__memset_evex_unaligned"
#elif MINIMUM_X86_ISA_LEVEL == 3
# define HAVE_MEMSET_IFUNC_GENERIC "__memset_avx2_unaligned"
#else
# define HAVE_MEMSET_IFUNC_GENERIC "__memset_sse2_unaligned"
#endif
asm ("memset = " HAVE_MEMSET_IFUNC_GENERIC);
#if MINIMUM_X86_ISA_LEVEL >= 4
# define HAVE_MEMCMP_IFUNC_GENERIC "__memcmp_evex_movbe"
#elif MINIMUM_X86_ISA_LEVEL == 3
# define HAVE_MEMCMP_IFUNC_GENERIC "__memcmp_avx2_movbe"
#else
# define HAVE_MEMCMP_IFUNC_GENERIC "__memcmp_sse2"
#endif
asm ("memcmp = " HAVE_MEMCMP_IFUNC_GENERIC);
#if MINIMUM_X86_ISA_LEVEL >= 4
# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_evex"
#elif MINIMUM_X86_ISA_LEVEL == 3
# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_avx2"
#else
# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_sse2"
#endif
asm ("strlen = " HAVE_STRLEN_IFUNC_GENERIC);
#if MINIMUM_X86_ISA_LEVEL >= 4
# define HAVE_MEMCPY_IFUNC_GENERIC "__memcpy_evex_unaligned"
# define HAVE_MEMMOVE_IFUNC_GENERIC "__memmove_evex_unaligned"
# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_evex_unaligned"
#elif MINIMUM_X86_ISA_LEVEL == 3
# define HAVE_MEMCPY_IFUNC_GENERIC "__memcpy_avx_unaligned"
# define HAVE_MEMMOVE_IFUNC_GENERIC "__memmove_avx_unaligned"
# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_avx_unaligned"
#else
# define HAVE_MEMCPY_IFUNC_GENERIC "__memcpy_sse2_unaligned"
# define HAVE_MEMMOVE_IFUNC_GENERIC "__memmove_sse2_unaligned"
# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_sse2_unaligned"
#endif
asm ("memcpy = " HAVE_MEMCPY_IFUNC_GENERIC);
asm ("memmove = " HAVE_MEMMOVE_IFUNC_GENERIC);
asm ("mempcpy = " HAVE_MEMPCPY_IFUNC_GENERIC);
asm ("__mempcpy = " HAVE_MEMPCPY_IFUNC_GENERIC);
#if MINIMUM_X86_ISA_LEVEL >= 4
# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_evex"
#elif MINIMUM_X86_ISA_LEVEL == 3
# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_avx2"
#else
# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_sse2"
#endif
asm ("__strchrnul = " HAVE_STRCHRNUL_IFUNC_GENERIC);
#endif /* SHARED */
#endif