1 Overview

We know by learning STM32F1 that if you want to optimize your code, you must know the rom and ram usage of the project. Therefore, we need to use the .map file. By analyzing the .map file, you can easily view the occupation of the project rom and ram, including a single source file or even a rom for each function.

Q: Can that .map be divided into several parts?

A: By carefully reading the .map file, we can roughly divide it into five parts:

List the calling relationships of different functions

List redundant functions optimized by MDK

List local and global labels

List memory maps for image files

List the component size of the image file

2 the .map file analysis

2.1 Basic concept

Section: The code and data block that describe the image file.

RO: Abbreviation for Read-Only, including RO-data (read-only data) and RO-code (code).

RW: Abbreviation for Read-Write, mainly RW-data, Rw-data initializes the initial value by the program.

ZI: Abbreviation for Zero-initialized, mainly ZI-data, initialized to 0 by the programmer.

.text: synonymous with RO-code

.constdata: synonymous with RO-data

.bss: Synonymous with ZI-data, usually refers to the area where uninitialized global variables are stored.

.data: synonymous with RW-data

2.2 Case analysis

first part:

This part of the code reflects the calling relationship of each function. For example, the arrow indicates that the LED_Init function in the code segment of the led file is called in the main file code segment.

the second part:

There is a configuration option in MDK. If you don’t check it, you can see that the compiler optimizes 3 redundant functions, about 50 bytes. Then we open the options and compile them all to see the results.

At this point it is obvious that the compiler optimizes a lot of unused code, saving at least 3258 bytes of space. Therefore, I personally suggest that you choose the option when you create the project in the future.


the third part:

Q: What is a partial label? What is a global label?

A: Local tags are variables that are declared statically inside a function, and functions that are declared with static are basically local tags. Global tags are static variables and functions that are declared statically. Variables and C file functions are global tags. There is also a variable in the assembly file. If the scope is the local label of the file, the scope is the global label of the whole project.

In fact, I don’t think we need to distinguish between them. Anyway, these two labels are definitely not in the local area. I know how to find them in .map.

This i. prefix indicates a function in a file, such as the BusFault_Handler function in the stm32f10x_it file here; this SetSysClock should pay attention to why the function previously modified with the i. prefix appears again. ? It turns out that this function is a local tag function modified with static, using 8 bytes of RAM size, the type belongs to M3’s Thumb instruction code.

the fourth part:

There are several main points here. The first is the image entry address 0x8000131. Then you can know the start address and size of the load domain, and finally know the start address and size of the execution domain ROM and RAM.

It should also be noted that the initialized RW data must be copied from the ROM to the execution address in RAM and the ZI Section (variable area initialized to 0) must be created before the image is executed.

Q: Why do you want to do this? Who will complete this step?

A: Because our code is stored in Flash, a power-on code will not run in the ROM, we must first copy the code inside the ROM to RAM to run. This step should be implemented by the __main function.

n general, the code we compiled, Code, RO-data, and RW-data are all stored in FLASH, and then the RW data is copied to RAM through the startup file.

the fifth part:

The last part is relatively simple, nothing more than the code size of each image component in each file, we are more concerned about the size of the actual download to FLASH, such as the Total ROM Size above.