Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instruct the compiler to generate an alias for a virtual function?

See this question for the background.

Basically, I have the following definition of a class

class  MyClass {
    virtual int foo4(double, int);
};

Is there a way to instruct the compiler to generate two symbols that would resolve to foo4? That is, I want that if an executable asks for the dynamic linker to resolve _ZN7MyClass4foo4Edi (symbol for MyClass::foo4(double, int)) and some other symbol (let's say _ZN7MyClass9reserved1Ev, a symbol for MyClass::reserved1()), the dynamic linker would resolve both to &MyClass::foo4(double, int). I`m using fairly modern GCC on Linux.

like image 430
p12 Avatar asked Oct 09 '22 15:10

p12


1 Answers

In gcc, use the "alias" attribute.

int reserved1() __attribute__((alias ("_ZN7MyClass4foo4Edi")));

... but I believe this will only work in the same object file as (a) definition of the symbol, so I'm not sure it will suit your uses cases: see here. Specifically, it will only be an alias for one version of the virtual call, and won't be inherited by subclasses; additionally, you cannot use it to alias a weak symbol.

like image 117
Borealid Avatar answered Oct 13 '22 10:10

Borealid