Why does .map file have _variable names and not just variable names?
Started by 4 years ago●2 replies●latest reply 4 years ago●202 viewsIt is up to the compiler to name your variables any way it likes. As long as it is doing it in a consistent manner, the same compiler generated name will refer to the same linker object, so there's no problem.
The compiler often renames objects. Consider the following 3 declarations:
int x;
static int x;
foo() { static int x; }
All three declare an integer named 'x' which permanently exists in the BSS segment. However, the first declaration is a global variable, which should be visible from a different source file. The second is global within a file but should not be visible from other files. The third one should only be visible from the foo() function.
Therefore, the compiler must rename your variables, because it knows from a C level analysis which one you refer to, but the linker does not. When you link all the object files together, there can be only one object called 'x', even though you might have used that identifier many times throughout your code as a name of *different* objects.
Furthermore, C does not allow you to call a variable by a name that is also a C keyword. But the compiler generates assembly source and it also needs to avoid clashes with assembler keywords, instruction mnemonics and so on and so forth.
Some compilers emit global names as they are, without the underscore. Others put an underscore in front of them. Variables declared static are completely renamed, usually using a symbol that is not valid in C names, so they cannot possibly clash with a global.
So it's nothing to do with 'embedded software', it's just a result of the C scoping rules and the particular choice the writers of your particular compiler made.
The .map file is a human readable file.
The compiler makes human readable files, for er.... humans!
The _ prefix to variable names helps you to identify variables.
You could for example 'grep' all lines with '_' or even sort the file line-by-line alphabetically.
At least that's my 2p worth.