HeLLoTIS
HeLLoTIS

Reputation: 93

Compare Signature of Wrapped Functions

gcc supports to wrap functions, so that the wrapper function is called instead of the real function. This replacement happens on linker level.

/* myfile.c */
int myF(int x);

int __wrap_myF(int x)
{
    return x + 1;
}

int main(int argc, char ** argv)
{
    return myF(5);
}

When I compile this with

gcc -Wl,--wrap=myF myfile.c -o myfile.exe

and execute the binary, I get the return value 5.

When I now change the signature of __wrap_myF(), then this is not recognized by the compiler/linker. I would expect an error or at least a warning, if I change the signature as following:

int myF(int x);

int __wrap_myF(int x, int y)  /* <-- added a second parameter */
{
    return x + 1;
}

int main(int argc, char ** argv)
{
    return myF(5);
}

Is there a way that gcc automatically throws an error, if the wrap functions signature is not equivalent to the wrapped one?

Is there some gcc extension, that allows to compare the signatures of two functions?

_Static_assert(__builtin_signature(myF) == __builtin_signature(__wrap_myF), "failure")

If there would be a C standard way I would appreciate this, but I strongly doubt this.

Thanks in advance.

Upvotes: 0

Views: 58

Answers (1)

KamilCuk
KamilCuk

Reputation: 141688

If there would be a C standard way

There is no such way.

Is there some gcc extension, that allows to compare the signatures of two functions?

_Static_assert(
   __builtin_types_compatible_p(
        __typeof__(&myF), __typeof__(&__wrap_myF)
   ), "failure");

Upvotes: 1

Related Questions