| Main index | Section 9 | Options |
#include <machine/ifunc.h>
Because DEFINE_IFUNC is a macro that defines a dynamically typed function, its usage looks somewhat unusual. The qual parameter is a list of zero or more C function qualifiers to be applied to the ifunc. This parameter is typically empty or the static qualifier. ret_type is the return type of the ifunc. name is the name of the ifunc. args is a parenthesized, comma-separated list of the parameter types of the function, as they would appear in a C function declaration.
The DEFINE_IFUNC usage must be followed by the resolver function body. The resolver must return a function with return type ret_type and parameter types args. The resolver function is defined with the ‘resolver’ gcc-style function attribute, causing the corresponding elf(5) function symbol to be of type STT_GNU_IFUNC instead of STT_FUNC. The kernel linker invokes the resolver to process relocations targeting ifunc calls and PLT entries referencing such symbols.
static size_t
fast_strlen(const char *s __unused)
{
size_t len;
/* Fast, but may not be correct in all cases. */
__asm("movq $42,%0 : "=r" (len));
return (len);
}
static size_t
slow_strlen(const char *s)
{
const char *t;
for (t = s; *t != '\0'; t++);
return (t - s);
}
DEFINE_IFUNC(, size_t, strlen, (const char *))
{
int enabled;
enabled = 1;
TUNABLE_INT_FETCH("debug.use_fast_strlen", &enabled);
if (enabled && (cpu_features & CPUID_FAST_STRLEN) != 0)
return (fast_strlen);
else
return (slow_strlen);
}
This defines a strlen() function with an optimized implementation for CPUs that advertise support.
| DEFINE_IFUNC (9) | May 18, 2019 |
| Main index | Section 9 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
