Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCC/Binutils: Add support for IFUNC #578

Open
claziss opened this issue Sep 4, 2023 · 0 comments
Open

GCC/Binutils: Add support for IFUNC #578

claziss opened this issue Sep 4, 2023 · 0 comments

Comments

@claziss
Copy link
Contributor

claziss commented Sep 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant