why C++ cannot alias an inline function, C can ?
Jason Vas Dias
jason.vas.dias@gmail.com
Sun Apr 1 12:33:00 GMT 2018
On 01/04/2018, Max Filippov <jcmvbkbc@gmail.com> wrote:
> On Sun, Apr 1, 2018 at 4:34 AM, Jason Vas Dias <jason.vas.dias@gmail.com>
> wrote:
>> In C++ however, it does not compile :
>>
>> $ g++ -g -std=gnu++11 -x c++ -c t.c
>> t.c:5:8: error: 'void bar()' aliased to undefined symbol 'foo'
>> void bar(void);
>> ^
>>
>> Even though, if I compile the object with the declaration
>> of bar() commented out , I can see C++ is not mangling
>> the name "foo" :
>
> gcc manual says the follwing about the alias attribute:
> "In C++, the mangled name for the target must be used."
>
> so with the following modification your example compiles:
>
> static inline __attribute__((always_inline,alias("_ZL3foov")))
> void bar(void);
>
> --
> Thanks.
> -- Max
>
Aha! But how to determine the mangled name beforehand ?
Even if I compile the object without the alias, then inspect
the object with objdump, there is no mangled symbol
_ZL3foov defined in the object file .
So I have to form the mangled name string beforehand,
even for symbols for which there is no linkage ?
ie. there should never be any symbol table entry for 'foo' -
only in the debuginfo there will be information about it .
So I must run some name mangler / generator as a
pre-processing step to get the correct mangled name
string ?
Like, if I compile just :
static inline __attribute__((always_inline))
void foo(void) {}
void f(void)
{ foo();
}
Then both
$ nm -a t.o | grep ZL3foov
and
$ objdump -t t.o | grep ZL3foov
and
$ objdump -g t.o | grep ZL3foov
produce no output. So how does one determine that mangled name
a-priori ? There seems to be no way of getting cpp to do it, anyway ,
which is what it would have to do to be an acceptable solution -
there must be a string literal constant argument to '__attribute__(alias()))'.
I think some kind of new pragma is needed , to allow one to say
something like:
#define _S(_X) #X
#define QUOTED_STRING(_X) _S(_X)
static inline __attribute__((always_inline, alias(
QUOTED_STRING(
#pragma symbol_name(foo)
// insert mangled symbol name here
)
)));
I'm going to investigate patching GCC to support
something like that.
I think it is unacceptable to be forced to used
mangled symbol names for objects which are
not symbols (pure inline functions) .
Thanks & Regards,
Jason
))
void bar(void) ;
More information about the Gcc
mailing list