ifunc attribute in C++
asomers@gmail.com
asomers@gmail.com
Fri Jan 30 15:27:00 GMT 2015
On Fri, Jan 30, 2015 at 8:15 AM, Alexander Monakov <amonakov@ispras.ru> wrote:
>
>
> On Fri, 30 Jan 2015, asomers@gmail.com wrote:
>
>> Does the ifunc attribute work in C++, and if so can somebody please
>> show me a working example? I found several examples that work in C,
>> but none in C++. For example, the below file works fine in C:
>
> Yes, it works in C++; the issue you're hitting is that due to C++ name
> mangling, the assembler name of the resolver function (foo_ifunc) is changed,
> and no longer matches what you wrote in the attribute. Perhaps the easiest
> fix for that is to put the resolver inside an 'extern "C" { ... }' block.
>
> HTH.
> Alexander
Thanks, Alexander. I thought I had already tried that, but I guess I
put the wrong function(s) in the extern block. For the record, here
is the program that works:
#include <stdio.h>
typedef int foo_t(int);
int foo (int) __attribute__ ((ifunc ("foo_ifunc")));
static int global = 1;
static int
f1 (int x)
{
return x;
}
static int
f2 (int x)
{
return x + 1;
}
extern "C" {
foo_t *
foo_ifunc (void)
{
return global == 1 ? f1 : f2;
}
}
int
main (int argc, char** argv __attribute__((unused)))
{
printf ("%d\n", foo(argc));
return (0);
}
-Alan
More information about the Gcc-help
mailing list