[PATCH v5 1/1] malloc: aarch64: Add ifuncs for malloc functions

DJ Delorie dj@redhat.com
Wed Jun 10 01:01:47 GMT 2026


LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>

Yury Khrustalev <yury.khrustalev@arm.com> writes:
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 6ce76cb4df..9a2e796f0e 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -499,6 +499,7 @@ libc_hidden_proto (__libc_free)
>    set to zero.
>  */
>  void*  __libc_calloc(size_t, size_t);
> +libc_hidden_proto (__libc_calloc)

Ok.

> @@ -551,6 +552,7 @@ libc_hidden_proto (__libc_memalign)
>    size of the system. If the pagesize is unknown, 4096 is used.
>  */
>  void*  __libc_valloc(size_t);
> +libc_hidden_proto (__libc_valloc)

Ok.

> @@ -585,6 +587,7 @@ struct mallinfo __libc_mallinfo(void);
>    round up n to nearest pagesize.
>   */
>  void*  __libc_pvalloc(size_t);
> +libc_hidden_proto (__libc_pvalloc)

Ok.

> @@ -628,6 +631,7 @@ int      __malloc_trim(size_t);
>  
>  */
>  size_t   __malloc_usable_size(void*);
> +libc_hidden_proto (__malloc_usable_size)

Ok.

> @@ -657,6 +661,7 @@ void     __malloc_stats(void);
>    POSIX wrapper like memalign(), checking for validity of size.
>  */
>  int      __posix_memalign(void **, size_t, size_t);
> +libc_hidden_proto (__posix_memalign)

Ok.

> @@ -3315,8 +3320,11 @@ libc_hidden_def (__libc_memalign)
>  
>  /* For ISO C17.  */
>  void *
> -weak_function
> -aligned_alloc (size_t alignment, size_t bytes)
> +__aligned_alloc (size_t alignment, size_t bytes);
> +libc_hidden_proto (__aligned_alloc)
> +
> +void *
> +__aligned_alloc (size_t alignment, size_t bytes)

Ok, matched below.

> @@ -3328,11 +3336,15 @@ aligned_alloc (size_t alignment, size_t bytes)
>  
>    return _mid_memalign (alignment, bytes);
>  }
> +libc_hidden_def (__aligned_alloc)

Ok.

> -weak_function
> -free_sized (void *ptr, __attribute_maybe_unused__ size_t size)
> +__free_sized (void *ptr, __attribute_maybe_unused__ size_t size);
> +libc_hidden_proto (__free_sized)
> +
> +void
> +__free_sized (void *ptr, __attribute_maybe_unused__ size_t size)
>  {
>  }
> +libc_hidden_def (__free_sized)

Ok.

> -weak_function
> -free_aligned_sized (void *ptr, __attribute_maybe_unused__ size_t alignment,
> +__free_aligned_sized (void *ptr, __attribute_maybe_unused__ size_t alignment,
> +                    __attribute_maybe_unused__ size_t size);
> +libc_hidden_proto (__free_aligned_sized)
> +
> +void
> +__free_aligned_sized (void *ptr, __attribute_maybe_unused__ size_t alignment,
>                      __attribute_maybe_unused__ size_t size)
>  {
>  }
> +libc_hidden_def (__free_aligned_sized)

Ok.

> @@ -3403,6 +3421,7 @@ __libc_valloc (size_t bytes)
>  {
>    return _mid_memalign (GLRO (dl_pagesize), bytes);
>  }
> +libc_hidden_def (__libc_valloc)

Ok.

> @@ -3420,6 +3439,7 @@ __libc_pvalloc (size_t bytes)
>  
>    return _mid_memalign (pagesize, rounded_bytes & -pagesize);
>  }
> +libc_hidden_def (__libc_pvalloc)

Ok.

> @@ -3541,6 +3561,7 @@ __libc_calloc (size_t n, size_t elem_size)
>  #endif
>    return __libc_calloc2 (bytes);
>  }
> +libc_hidden_def (__libc_calloc)
>  #endif /* IS_IN (libc) */

Ok.

> @@ -4537,6 +4558,7 @@ __malloc_usable_size (void *m)
>      return 0;
>    return musable (m);
>  }
> +libc_hidden_def (__malloc_usable_size)
>  #endif /* IS_IN (libc) */

Ok.

> @@ -5064,6 +5086,7 @@ __posix_memalign (void **memptr, size_t alignment, size_t size)
>    *memptr = mem;
>    return 0;
>  }
> +libc_hidden_def (__posix_memalign)
>  #endif /* IS_IN (libc) */

Ok.

> @@ -5226,6 +5249,9 @@ __malloc_info (int options, FILE *fp)
>  #if IS_IN (libc)
> +
> +/* See sysdeps/generic/malloc-ifuncs.h for details.  */
> +# if !defined (USE_MULTIARCH_MALLOC)

Ok.

> +weak_alias (__aligned_alloc, aligned_alloc)
> +weak_alias (__free_sized, free_sized)
> +weak_alias (__free_aligned_sized, free_aligned_sized)
> +#endif /* !USE_MULTIARCH_MALLOC */

Ok.

> @@ -5244,9 +5274,11 @@ weak_alias (__malloc_stats, malloc_stats)
>  weak_alias (__malloc_trim, malloc_trim)
>  #endif /* IS_IN (libc) */
>  
> -#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_26)
> +#if !defined (USE_MULTIARCH_MALLOC)
> +# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_26)
>  compat_symbol (libc, __libc_free, cfree, GLIBC_2_0);
> -#endif
> +# endif
> +#endif /* !USE_MULTIARCH_MALLOC */

Ok.

> diff --git a/sysdeps/aarch64/multiarch/Makefile b/sysdeps/aarch64/multiarch/Makefile
> index 38952655b1..a8a03a0491 100644
> --- a/sysdeps/aarch64/multiarch/Makefile
> +++ b/sysdeps/aarch64/multiarch/Makefile
> @@ -18,5 +18,11 @@ sysdep_routines += \
>    memset_zva64 \
>    strlen_asimd \
>    strlen_generic \
> -# sysdep_routines
> -endif
> +  # sysdep_routines
> +endif # ifeq ($(subdir),string)

Ok but unrelated.

> +ifeq ($(subdir),malloc)
> +sysdep_routines += \
> +  malloc-ifuncs \
> +  # sysdep_routines
> +endif # ifeq ($(subdir),malloc)

Ok.

> diff --git a/sysdeps/aarch64/multiarch/malloc-ifuncs.c b/sysdeps/aarch64/multiarch/malloc-ifuncs.c
> new file mode 100644
> index 0000000000..e475f43a31
> --- /dev/null
> +++ b/sysdeps/aarch64/multiarch/malloc-ifuncs.c
> @@ -0,0 +1,80 @@
> +/* Code for ifunc resolvers for malloc: aarch64 version.
> +   Copyright (C) 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/>.  */
> +
> +#if IS_IN (libc)
> +
> +#include <stdint.h>
> +#include <init-arch.h>
> +#include <shlib-compat.h>
> +#include <malloc-ifuncs.h>
> +
> +libc_ifunc_hidden (__libc_malloc, __libc_malloc_redirect,
> +		   __libc_malloc)
> +strong_alias (__libc_malloc_redirect, malloc)
> +
> +libc_ifunc_hidden (__libc_calloc, __libc_calloc_redirect,
> +		   __libc_calloc)
> +weak_alias (__libc_calloc_redirect, calloc)
> +
> +libc_ifunc_hidden (__libc_memalign, __libc_memalign_redirect,
> +		   __libc_memalign)
> +weak_alias (__libc_memalign_redirect, memalign)
> +
> +libc_ifunc_hidden (__libc_valloc, __libc_valloc_redirect,
> +		   __libc_valloc)
> +weak_alias (__libc_valloc_redirect, valloc)
> +
> +libc_ifunc_hidden (__libc_pvalloc, __libc_pvalloc_redirect,
> +		   __libc_pvalloc)
> +weak_alias (__libc_pvalloc_redirect, pvalloc)
> +
> +libc_ifunc_hidden (__libc_realloc, __libc_realloc_redirect,
> +		   __libc_realloc)
> +strong_alias (__libc_realloc_redirect, realloc)
> +
> +libc_ifunc_hidden (__libc_free, __libc_free_redirect,
> +		   __libc_free)
> +strong_alias (__libc_free_redirect, free)
> +
> +libc_ifunc_hidden (__malloc_usable_size, __malloc_usable_size_redirect,
> +		   __malloc_usable_size)
> +weak_alias (__malloc_usable_size_redirect, malloc_usable_size)
> +
> +libc_ifunc_hidden (__posix_memalign, __posix_memalign_redirect,
> +		   __posix_memalign)
> +weak_alias (__posix_memalign_redirect, posix_memalign)
> +
> +libc_ifunc_hidden (__aligned_alloc, __aligned_alloc_redirect,
> +		   __aligned_alloc)
> +weak_alias (__aligned_alloc_redirect, aligned_alloc)
> +
> +libc_ifunc_hidden (__free_sized, __free_sized_redirect,
> +		   __free_sized)
> +weak_alias (__free_sized_redirect, free_sized)
> +
> +libc_ifunc_hidden (__free_aligned_sized, __free_aligned_sized_redirect,
> +		   __free_aligned_sized)
> +weak_alias (__free_aligned_sized_redirect, free_aligned_sized)
> +
> +#endif /* IS_IN (libc) */

Ok.

> +#ifdef SHARED
> +# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_26)
> +compat_symbol (libc, __libc_free_redirect, cfree, GLIBC_2_0);
> +# endif
> +#endif

Ok.

> diff --git a/sysdeps/aarch64/multiarch/malloc-ifuncs.h b/sysdeps/aarch64/multiarch/malloc-ifuncs.h
>
> @@ -0,0 +1,61 @@
> +/* Definitions for ifunc resolvers for malloc: aarch64 version.
> +   Copyright (C) 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 _AARCH64_MALLOC_IFUNCS_H
> +#define _AARCH64_MALLOC_IFUNCS_H
> +
> +#define USE_MULTIARCH_MALLOC 1
> +
> +#include <stddef.h>
> +#include <sys/cdefs.h>
> +
> +/* Core implementations of malloc functions.  An ifunc resolver must
> +   use these implementations as a fallback option.  Other implementations
> +   may internally call these core functions.  */
> +void *__libc_malloc (size_t);
> +libc_hidden_proto (__libc_malloc)
> +void *__libc_calloc (size_t, size_t);
> +libc_hidden_proto (__libc_calloc)
> +void *__libc_memalign (size_t, size_t);
> +libc_hidden_proto (__libc_memalign)
> +void *__libc_valloc (size_t);
> +libc_hidden_proto (__libc_valloc)
> +void *__libc_pvalloc (size_t);
> +libc_hidden_proto (__libc_pvalloc)
> +void *__libc_realloc (void *, size_t);
> +libc_hidden_proto (__libc_realloc)
> +void __libc_free (void *);
> +libc_hidden_proto (__libc_free)
> +size_t __malloc_usable_size (void *);
> +libc_hidden_proto (__malloc_usable_size)
> +
> +/* For additions of POSIX.  */
> +int __posix_memalign (void **, size_t, size_t);
> +libc_hidden_proto (__posix_memalign)
> +
> +/* For ISO C17.  */
> +void *__aligned_alloc (size_t, size_t);
> +libc_hidden_proto (__aligned_alloc)
> +
> +/* For ISO C23.  */
> +void __free_sized (void *, size_t);
> +libc_hidden_proto (__free_sized)
> +void __free_aligned_sized (void *, size_t, size_t);
> +libc_hidden_proto (__free_aligned_sized)
> +
> +#endif /* _AARCH64_MALLOC_IFUNCS_H */

Ok.

> diff --git a/sysdeps/generic/malloc-ifuncs.h b/sysdeps/generic/malloc-ifuncs.h
> new file mode 100644
> index 0000000000..3bb6e60ba1
> --- /dev/null
> +++ b/sysdeps/generic/malloc-ifuncs.h
> @@ -0,0 +1,35 @@
> +/* Definitions for ifunc resolvers for malloc: generic version.
> +   Copyright (C) 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 _GENERIC_MALLOC_IFUNCS_H
> +#define _GENERIC_MALLOC_IFUNCS_H
> +
> +/* Targets should define this macro if they provide ifuncs for
> +   malloc functions.  When USE_MULTIARCH_MALLOC is defined, the
> +   following functions should be implemented via ifuncs:
> +
> +     malloc, calloc, free, realloc
> +     memalign, valloc, pvalloc
> +     posix_memalign
> +     malloc_usable_size
> +     aligned_alloc, free_sized, free_aligned_sized
> +
> +*/
> +#undef USE_MULTIARCH_MALLOC
> +
> +#endif /* GENERIC_MALLOC_IFUNCS_H */

Ok.



More information about the Libc-alpha mailing list