GCC/Binutils: Add support for IFUNC
Opened this issue · 0 comments
For upcoming support for a linux distro, we need to implement in binutils support for IFUNC.
Here it is what Nick says about IFUNC:
-
GCC now accepts a new function attribute called "ifunc". This marks a function as an "indirect" function which allows the resolution of its value to be determined dynamically at load time. (So that for example a version of the function optimized for the current architecture cab selected).
To use this attribute it is first necessary to define all of different implementations of the function that will be available. Then you must define a resolver function which will return a pointer to the desired implementation based upon some criteria.
The implementation functions' declarations must all have the same API and the resolver must return a pointer to void function returning void. Here is an example:
void *slow_memcpy (void *dst, const void *src, size_t len)
{
char *d = dst; char *s = src;while (len--) *d++ = *s++; return dst;
}
void *fast_memcpy (void *dst, const void *src, size_t len)
{
__asm("foo %0 %1 %2" : "=m" (dst) : "m" (src), "r" (len) : memory);
return dst;
}static void (* resolve_memcpy (void)) (void)
{
return __cpu_has_foo () ? fast_memcpy : slow_memcpy;
}void *memcpy (void *, const void *, size_t) attribute ((ifunc ("resolve_memcpy")));
The exported header file declaring the function the user calls would just contain:
extern void *memcpy (void *, const void *, size_t);
allowing the user to call this as a regular function, unaware of how it is actually implemented.
Indirect functions cannot be weak, and require a recent binutils (at least version 2.20.1), and GNU C library (at least version 2.11.1).
REF:
http://nickclifton.livejournal.com/6612.html
http://www.gnu.org/software/hurd/open_issues/ifunc.html