[PATCH v5 2/2] aarch64: helper function to access hwcap elements in ifunc resolver

enh enh@google.com
Tue May 6 14:29:43 GMT 2025


On Tue, May 6, 2025 at 10:27 AM enh <enh@google.com> wrote:
>
> On Mon, Apr 28, 2025 at 7:32 AM Yury Khrustalev <yury.khrustalev@arm.com> wrote:
> >
> > Add a portable way to access HWCAP elements via the parameter(s)
> > passed to an ifunc resolver checking the _IFUNC_ARG_HWCAP bit in
> > the first parameter and size of the buffer in the second parameter.
> >
> > Note that 0 is return when the requested element is not available
> > or does not correspond to a valid AT_HWCAP{,2,...} value.
> > ---
> >  sysdeps/aarch64/Makefile          |  2 +-
> >  sysdeps/aarch64/sys/ifunc.h       | 21 +++++++
> >  sysdeps/aarch64/tst-ifunc-arg-1.c | 12 ++++
> >  sysdeps/aarch64/tst-ifunc-arg-2.c | 11 ++++
> >  sysdeps/aarch64/tst-ifunc-arg-3.c | 97 +++++++++++++++++++++++++++++++
> >  5 files changed, 142 insertions(+), 1 deletion(-)
> >  create mode 100644 sysdeps/aarch64/tst-ifunc-arg-3.c
> >
> > diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile
> > index 4b7f8a5c07..c4d21336da 100644
> > --- a/sysdeps/aarch64/Makefile
> > +++ b/sysdeps/aarch64/Makefile
> > @@ -41,7 +41,7 @@ gen-as-const-headers += \
> >    dl-link.sym \
> >    rtld-global-offsets.sym
> >
> > -tests-internal += tst-ifunc-arg-1 tst-ifunc-arg-2
> > +tests-internal += tst-ifunc-arg-1 tst-ifunc-arg-2 tst-ifunc-arg-3
> >
> >  ifeq (yes,$(aarch64-variant-pcs))
> >  tests += tst-vpcs
> > diff --git a/sysdeps/aarch64/sys/ifunc.h b/sysdeps/aarch64/sys/ifunc.h
> > index fd4d49f00b..8d994f8418 100644
> > --- a/sysdeps/aarch64/sys/ifunc.h
> > +++ b/sysdeps/aarch64/sys/ifunc.h
> > @@ -19,6 +19,8 @@
> >  #ifndef _SYS_IFUNC_H
> >  #define _SYS_IFUNC_H
> >
> > +#include <sys/cdefs.h>
> > +
> >  /* A second argument is passed to the ifunc resolver.  */
> >  #define _IFUNC_ARG_HWCAP       (1ULL << 62)
> >
> > @@ -64,4 +66,23 @@ struct __ifunc_arg_t
> >
> >  typedef struct __ifunc_arg_t __ifunc_arg_t;
> >
> > +/* A helper function to obtain HWCAP element by its ID from the
> > +   parameters ARG0 and ARG1 passed to the ifunc resolver.  Note that
> > +   ID 1 corresponds to AT_HWCAP, ID 2 corresponds to AT_HWCAP2, etc.
> > +   If there is no element available for the requested ID then 0 is
> > +   returned.  If ID doesn't much any supported AT_HWCAP{,2,...} value,
> > +   then 0 is also returned.  */
> > +static __inline unsigned long __attribute__ ((unused, always_inline))
> > +__ifunc_hwcap (unsigned long __id,
> > +              unsigned long __arg0,
>
> is there any reason for this special case, since it's also available
> from the array? this just seems to make caller and callee slightly
> more complex for no significant advantage?

oh, it's to support the historical accident where glibc has versions
that _do_ pass arg0 but _don't_ pass arg1? (bionic went straight from
passing nothing to passing both arguments.)

> > const unsigned long *__arg1)
> > +{
> > +  if (__glibc_likely (__arg0 & _IFUNC_ARG_HWCAP))
> > +    {
> > +      const unsigned long size = __arg1[0];
> > +      const unsigned long offset = __id * sizeof (unsigned long);
> > +      return offset < size && __id > 0 ? __arg1[__id] : 0;
> > +    }
> > +  return __id == 1 ? __arg0 : 0;
> > +}
> > +
> >  #endif
> > diff --git a/sysdeps/aarch64/tst-ifunc-arg-1.c b/sysdeps/aarch64/tst-ifunc-arg-1.c
> > index 9cc3d40c48..292c5ae9e3 100644
> > --- a/sysdeps/aarch64/tst-ifunc-arg-1.c
> > +++ b/sysdeps/aarch64/tst-ifunc-arg-1.c
> > @@ -60,6 +60,18 @@ do_test (void)
> >    TEST_COMPARE (saved_arg2._hwcap3, getauxval (AT_HWCAP3));
> >    TEST_COMPARE (saved_arg2._hwcap4, getauxval (AT_HWCAP4));
> >
> > +  const unsigned long *saved_arg2_ptr = (const unsigned long *)&saved_arg2;
> > +
> > +  TEST_COMPARE (__ifunc_hwcap (1, saved_arg1, saved_arg2_ptr),
> > +               getauxval (AT_HWCAP));
> > +  TEST_COMPARE (__ifunc_hwcap (2, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP2));
> > +  TEST_COMPARE (__ifunc_hwcap (3, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP3));
> > +  TEST_COMPARE (__ifunc_hwcap (4, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP4));
> > +
> > +
> >    return 0;
> >  }
> >
> > diff --git a/sysdeps/aarch64/tst-ifunc-arg-2.c b/sysdeps/aarch64/tst-ifunc-arg-2.c
> > index d9950f0e37..c05129a132 100644
> > --- a/sysdeps/aarch64/tst-ifunc-arg-2.c
> > +++ b/sysdeps/aarch64/tst-ifunc-arg-2.c
> > @@ -63,6 +63,17 @@ do_test (void)
> >    TEST_COMPARE (saved_arg2._hwcap3, getauxval (AT_HWCAP3));
> >    TEST_COMPARE (saved_arg2._hwcap4, getauxval (AT_HWCAP4));
> >
> > +  const unsigned long *saved_arg2_ptr = (const unsigned long *)&saved_arg2;
> > +
> > +  TEST_COMPARE (__ifunc_hwcap (1, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP));
> > +  TEST_COMPARE (__ifunc_hwcap (2, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP2));
> > +  TEST_COMPARE (__ifunc_hwcap (3, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP3));
> > +  TEST_COMPARE (__ifunc_hwcap (4, saved_arg1, saved_arg2_ptr),
> > +                getauxval (AT_HWCAP4));
> > +
> >    return 0;
> >  }
> >
> > diff --git a/sysdeps/aarch64/tst-ifunc-arg-3.c b/sysdeps/aarch64/tst-ifunc-arg-3.c
> > new file mode 100644
> > index 0000000000..0f47a0a3b1
> > --- /dev/null
> > +++ b/sysdeps/aarch64/tst-ifunc-arg-3.c
> > @@ -0,0 +1,97 @@
> > +/* Tests for __ifunc_hwcap helper function.
> > +   Copyright (C) 2025 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/>.  */
> > +
> > +#include <stdint.h>
> > +#include <sys/ifunc.h>
> > +#include <support/check.h>
> > +
> > +#define CHECK_VALUES_WITH_ARG(p1, p2, p3, p4) \
> > +  ({ \
> > +    TEST_COMPARE (__ifunc_hwcap (0, _IFUNC_ARG_HWCAP, arg), 0); \
> > +    TEST_COMPARE (__ifunc_hwcap (1, _IFUNC_ARG_HWCAP, arg), p1); \
> > +    TEST_COMPARE (__ifunc_hwcap (2, _IFUNC_ARG_HWCAP, arg), p2); \
> > +    TEST_COMPARE (__ifunc_hwcap (3, _IFUNC_ARG_HWCAP, arg), p3); \
> > +    TEST_COMPARE (__ifunc_hwcap (4, _IFUNC_ARG_HWCAP, arg), p4); \
> > +    TEST_COMPARE (__ifunc_hwcap (5, _IFUNC_ARG_HWCAP, arg), 0); \
> > +  })
> > +
> > +#define CHECK_VALUES_WITHOUT_ARG(p1) \
> > +  ({ \
> > +    TEST_COMPARE (__ifunc_hwcap (0, p1, arg), 0); \
> > +    TEST_COMPARE (__ifunc_hwcap (1, p1, arg), p1); \
> > +    TEST_COMPARE (__ifunc_hwcap (2, p1, arg), 0); \
> > +    TEST_COMPARE (__ifunc_hwcap (3, p1, arg), 0); \
> > +    TEST_COMPARE (__ifunc_hwcap (4, p1, arg), 0); \
> > +    TEST_COMPARE (__ifunc_hwcap (5, p1, arg), 0); \
> > +  })
> > +
> > +static void
> > +test_one (const unsigned long *arg)
> > +{
> > +  uint64_t size = arg[0] / sizeof (uint64_t);
> > +
> > +  switch (size)
> > +    {
> > +      case 1:
> > +       CHECK_VALUES_WITH_ARG (0, 0, 0, 0);
> > +       CHECK_VALUES_WITHOUT_ARG (0);
> > +       break;
> > +      case 2:
> > +       CHECK_VALUES_WITH_ARG (1, 0, 0, 0);
> > +       CHECK_VALUES_WITHOUT_ARG (1);
> > +       break;
> > +      case 3:
> > +       CHECK_VALUES_WITH_ARG (1, 2, 0, 0);
> > +       CHECK_VALUES_WITHOUT_ARG (1);
> > +       break;
> > +      case 4:
> > +       CHECK_VALUES_WITH_ARG (1, 2, 3, 0);
> > +       CHECK_VALUES_WITHOUT_ARG (1);
> > +       break;
> > +      case 5:
> > +       CHECK_VALUES_WITH_ARG (1, 2, 3, 4);
> > +       CHECK_VALUES_WITHOUT_ARG (1);
> > +       break;
> > +      default:
> > +       TEST_VERIFY (0); // unexpected size
> > +       break;
> > +    }
> > +}
> > +
> > +static int
> > +do_test (void)
> > +{
> > +  uint64_t arg[_IFUNC_HWCAP_MAX + 1] = {
> > +    0, /* Size */
> > +    1, /* AT_HWCAP */
> > +    2, /* AT_HWCAP2 */
> > +    3, /* AT_HWCAP3 */
> > +    4, /* AT_HWCAP4 */
> > +  };
> > +
> > +  for (int k = 0; k <= _IFUNC_HWCAP_MAX; k++)
> > +    {
> > +      /* Update size */
> > +      arg[0] = (k + 1) * sizeof (uint64_t);
> > +      test_one (arg);
> > +    }
> > +
> > +  return 0;
> > +}
> > +
> > +#include <support/test-driver.c>
> > --
> > 2.39.5
> >


More information about the Libc-alpha mailing list