Consulting Services
MicrosMicrocontroller

Getting Detailed Information of MCU Memory Usage from .map File

.map file, generated from GCC-toolchain, has almost all information needed to find memory usage in details. Symbols, including functions and variables, with memory address, size and memory section name that holds this symbol can be found there and much more.

We can find in .map file for example:

  •  memory configuration or the memory map
Memory Configuration

Name             Origin             Length             Attributes
FLASH            0x0001f000         0x00055ffc         xr
FIRTRUN          0x00074ffc         0x00000004         r
RAM              0x20002560         0x0000d7a0         xrw
NOINIT           0x2000fd00         0x00000300         xrw
*default*        0x00000000         0xffffffff
  • Used area of each memory section defined in linker script .ld. For example, the following is bss section definition in linker script. Linker script describes how memory and sections will be organized, and we could also define symbols to be used in the code like this one __bss_start__ = .;
    .bss :
    {
        . = ALIGN(4);
        __bss_start__ = .;
        *(.bss*)
        *(COMMON)
        . = ALIGN(4);
        __bss_end__ = .;
    } > RAM

The snippet above means that linker will assign current program counter value to __bss_start__  then fill the section as needed with variables and then supply the end address (PC) to __bss_end__  . We will find that in .map file, but instead of the dot we will find the real start and end address of bss section.

....
0x20002ca4 __bss_start__ = .
....
0x2000e7a8 __bss_end__ = .
....
  • Even if you didn’t ask linker to give you the PC value in the beginning of a section and end of it in a variable, you can calculate the section size, as .map file states the section that each symbol belongs to it beside the symbol size.

From the points above, I found .map file has all information needed to find memory usage statistics, so I made a draft for Python script to scan .map file for these useful information mentioned above. The idea started with me while I was debugging my code functions size using nm tool with .out file, but I felt that more information can be obtained from .map file.

This script searches in .map file for information related to memory usage. User has to change settings in the associated file “map_settings.txt” to set:

  • Directory to .map file
  • Sections defined in linker script that has unique start and end address keywords (.i.e __bss_start__)
  • Sections defined in linker script with no unique start and end address keywords. User must specify start keyword and next section keyword … look to the bellow:
....
*(.dtors)
*(.rodata*)

KEEP(*(.eh_frame*))
....

that was part from linker script. (.eh_frame) is the unique keyword that makes the map_file_analyzer.py knows where (.rodata) ends.

Script in Action

Download the script form Github repository. Note: More powerful .map file analyzing tools are there like MapViewer and AMAP.

 

Yahya Tawil

Embedded Hardware Engineer interested in open hardware and was born in the same year as Linux. Yahya is the editor-in-chief of Atadiat and believes in the importance of sharing free, practical, spam-free and high quality written content with others. His experience with Embedded Systems includes developing firmware with bare-metal C and Arduino, designing PCB&schematic and content creation.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button