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.

Creating SSH Public and Private Key... x
Creating SSH Public and Private Key in Linux / Ubuntu

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development, Debugging and Performance Tools Development and Build Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web Design and Development Website Hosting Wordpress Yocto Embedded Linux