Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top