| 1 | /* Definitions used by AArch64 indirect function resolvers. |
| 2 | Copyright (C) 2019-2026 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _SYS_IFUNC_H |
| 20 | #define _SYS_IFUNC_H |
| 21 | |
| 22 | #include <sys/cdefs.h> |
| 23 | |
| 24 | /* A second argument is passed to the ifunc resolver. */ |
| 25 | #define _IFUNC_ARG_HWCAP	(1ULL << 62) |
| 26 | |
| 27 | /* Maximum number of HWCAP elements that are currently supported. */ |
| 28 | #define _IFUNC_HWCAP_MAX	4 |
| 29 | |
| 30 | /* The prototype of a GNU indirect function resolver on AArch64 is |
| 31 | |
| 32 | ElfW(Addr) ifunc_resolver (uint64_t, const uint64_t *); |
| 33 | |
| 34 | The following prototype is also compatible: |
| 35 | |
| 36 | ElfW(Addr) ifunc_resolver (uint64_t, const __ifunc_arg_t *); |
| 37 | |
| 38 | The first argument might have the _IFUNC_ARG_HWCAP bit set and |
| 39 | the remaining bits should match the AT_HWCAP settings. |
| 40 | |
| 41 | If the _IFUNC_ARG_HWCAP bit is set in the first argument, then |
| 42 | the second argument is passed to the resolver function. In |
| 43 | this case, the second argument is a const pointer to a buffer |
| 44 | that allows to access all available HWCAP elements. |
| 45 | |
| 46 | This buffer has its size in bytes at offset 0. The HWCAP elements |
| 47 | are available at offsets 8, 16, 24, 32... respectively for AT_HWCAP, |
| 48 | AT_HWCAP2, AT_HWCAP3, AT_HWCAP4... (these offsets are multiples of |
| 49 | sizeof (unsigned long)). |
| 50 | |
| 51 | Indirect function resolvers must check availability of HWCAP |
| 52 | elements at runtime before accessing them using the size of the |
| 53 | buffer. */ |
| 54 | |
| 55 | struct __ifunc_arg_t |
| 56 | { |
| 57 | unsigned long _size; /* Size of the struct, so it can grow. */ |
| 58 | unsigned long _hwcap; |
| 59 | unsigned long _hwcap2; /* End of 1st published struct. */ |
| 60 | unsigned long _hwcap3; |
| 61 | unsigned long _hwcap4; /* End of 2nd published struct. */ |
| 62 | }; |
| 63 | |
| 64 | typedef struct __ifunc_arg_t __ifunc_arg_t; |
| 65 | |
| 66 | /* Constants for IDs of HWCAP elements to be used with the |
| 67 | __ifunc_hwcap function below. */ |
| 68 | enum |
| 69 | { |
| 70 | _IFUNC_ARG_AT_HWCAP = 1, |
| 71 | _IFUNC_ARG_AT_HWCAP2 = 2, |
| 72 | _IFUNC_ARG_AT_HWCAP3 = 3, |
| 73 | _IFUNC_ARG_AT_HWCAP4 = 4, |
| 74 | }; |
| 75 | |
| 76 | /* A helper function to obtain HWCAP element by its ID from the |
| 77 | parameters ARG0 and ARG1 passed to the ifunc resolver. Note that |
| 78 | ID 1 corresponds to AT_HWCAP, ID 2 corresponds to AT_HWCAP2, etc. |
| 79 | If there is no element available for the requested ID then 0 is |
| 80 | returned. If ID doesn't much any supported AT_HWCAP{,2,...} value, |
| 81 | then 0 is also returned. */ |
| 82 | static __inline unsigned long __attribute__ ((unused, always_inline)) |
| 83 | __ifunc_hwcap (unsigned long __id, |
| 84 | 	 unsigned long __arg0, const unsigned long *__arg1) |
| 85 | { |
| 86 | if (__glibc_likely (__arg0 & _IFUNC_ARG_HWCAP)) |
| 87 | { |
| 88 | const unsigned long size = __arg1[0]; |
| 89 | const unsigned long offset = __id * sizeof (unsigned long); |
| 90 | return offset < size && __id > 0 ? __arg1[__id] : 0; |
| 91 | } |
| 92 | return __id == 1 ? __arg0 : 0; |
| 93 | } |
| 94 | |
| 95 | #endif |