aarch64: add __ifunc_hwcap function to be used in ifunc resolvers

Add a new helper function __ifunc_hwcap() as 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 returned when the requested element is not available
or does not correspond to a valid AT_HWCAP{,2,...} value.

Also add relevant tests.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
This commit is contained in:
Yury Khrustalev 2025-04-24 16:58:46 +01:00
parent ea14d04e9a
commit fcd6a8b5c5
6 changed files with 224 additions and 1 deletions

View File

@ -41,7 +41,12 @@ 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 \
tst-ifunc-arg-4 \
#tests-internal
tests += tst-vpcs
modules-names += tst-vpcs-mod

View File

@ -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)
@ -61,4 +63,33 @@ struct __ifunc_arg_t
typedef struct __ifunc_arg_t __ifunc_arg_t;
/* Constants for IDs of HWCAP elements to be used with the
__ifunc_hwcap function below. */
enum
{
_IFUNC_ARG_AT_HWCAP = 1,
_IFUNC_ARG_AT_HWCAP2 = 2,
_IFUNC_ARG_AT_HWCAP3 = 3,
_IFUNC_ARG_AT_HWCAP4 = 4,
};
/* 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, 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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 (_IFUNC_ARG_AT_HWCAP, _IFUNC_ARG_HWCAP, arg), p1); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, _IFUNC_ARG_HWCAP, arg), p2); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP3, _IFUNC_ARG_HWCAP, arg), p3); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP4, _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 (_IFUNC_ARG_AT_HWCAP, p1, arg), p1); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, p1, arg), 0); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP3, p1, arg), 0); \
TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP4, 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, /* Placeholder for size */
_IFUNC_ARG_AT_HWCAP, /* AT_HWCAP */
_IFUNC_ARG_AT_HWCAP2, /* AT_HWCAP2 */
_IFUNC_ARG_AT_HWCAP3, /* AT_HWCAP3 */
_IFUNC_ARG_AT_HWCAP4, /* 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>

View File

@ -0,0 +1,67 @@
/* Test for ifunc resolver that uses __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 <stdio.h>
#include <stdint.h>
#include <sys/auxv.h>
#include <sys/ifunc.h>
#include <support/check.h>
static int
one (void)
{
return 1;
}
static int
two (void)
{
return 2;
}
/* Resolver function. */
static void *
resolver (uint64_t arg0, const uint64_t arg1[])
{
uint64_t hwcap2 = __ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, arg0, arg1);
if (hwcap2 & HWCAP2_POE)
return (void *)one;
else
return (void *)two;
}
/* An extern visible ifunc symbol. */
int fun (void) __attribute__((ifunc ("resolver")));
static int
do_test (void)
{
if (getauxval (AT_HWCAP2) & HWCAP2_POE)
{
printf ("using 1st implementation\n");
TEST_VERIFY (fun () == 1);
}
else
{
printf ("using 2nd implementation\n");
TEST_VERIFY (fun () == 2);
}
return 0;
}
#include <support/test-driver.c>