문제

Can someone help me understanding the gcc name mangling conventions?

Consider the following test code

#include <stdio.h>

const int x = 42;
int y = 42;

int main( int argc, const char* argv[] )
{
        return 0;
}

When running nm I get the following (surprising?) result:

0000000000000000 T main
0000000000000000 D y
0000000000000000 r _ZL1x

This shows that the compiler only mangles global variables placed in the read only section. I would expect the compiler either to mangle ALL or NO global variables

Is this intended behaviour? For me it looks inconsistent.

도움이 되었습니까?

해결책

Mangling is mostly used to distinguish linker symbols that would otherwise reasonably clash.

Since x is implicitly static, multiple translation units can legally have different variables all called x, so the symbol is mangled to avoid collisions.

Since y is not static, there can be only one global variable called y in the program, so there's no need to avoid collisions (they should either be flagged as ODR violations or de-duplicated by the linker).

The other use is for functions, to distinguish overloads with the same name but different argument lists. That clearly doesn't apply here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top