Home » Development and Build » Compilation » How to create Linker .map file in Linux ?

How to create Linker .map file in Linux ?

Understanding map files created by some of the Embedded compilers is required for the Engineers who are working on firmware development or trying to optimise your code to fit into limited amount of RAM.

Here, with GCC we will demonstrate how to create a file file during the compilation to understand the memory map of the executable in more details.

Lets write a simple program with few variables,

#include <stdio.h>

int global_initialised_var = 10;
int global_uninitialised_var;

int main(int argc, char**argv) {
        int local_initialised_var = 10;
        int local_uninitialised_var;

        return 0;
}

Now, when we generate the executable for this program, we can use the simple gcc command as,

$ gcc -o helloworld helloworld.c

But above command, will only generate the “helloworld” executable, and we need map file for this executable, so we need to use below command,

$ gcc -o helloworld helloworld.c  -Xlinker -Map=helloworld.map

with this, we can get the map file,

$ file helloworld.map 
helloworld.map: assembler source, ASCII text

Now, if we open this, we can see the details of the variables and its sections assigned in the executable.

As, we can see above the “global initialised variable” is in “.data” section and “global uninitialised variable” is in “.bss” section.

Leave a Comment