
Memory Model
- Flash Memory :
- Read-Only
- Begins at address 0x08000000
- Size depends on specific STM32 microcontroller
- Program code is stored here.
- Contains a vector table at address 0x08000004This consists of pointers to the various exception handlers
- SRAM :
- Read and Write
- Begins at 0x20000000
- Size depends on specific STM32 microcontroller
- Variables and Stack are stored here
Linker Script
- Script given to the linker in order to specify the memory layout and to initialize the various memory sections used by the firmware.
- The linker script is responsible for making sure various portions of the firmware are in their proper place and also for associating meaningful labels with specific regions of memory used by the startup code.
The script has 4 features:
- Memory Layout : Available memory types
- Section Definitions : Placing specific parts at specific locations
- Options : Commands e.g. ENTRY POINT
- Symbols : Variables to inject into program at link time
Memory
- In order to allocate program memory, the linker needs to know the types of memory, their addresses and sizes.
- We use the MEMORY definition to provide this information.
MEMORY
{
name [(attr)] : ORIGIN = origin, LENGTH = len
…
}
E.g.
MEMORY
{
FLASH(rx) : ORIGIN = 0x08000000, LENGTH = 512K
SRAM(rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
Sections
- Code and data are organized into sections.
- Symbols of the same memory region are placed in the same section.
SECTIONS
{
…
}
E.g.
SECTIONS
{
.text :
{
*(.text) /* merge all .text sections of input files */
}> FLASH
}
Sections (cont.)
The 3 relevant sections:
- .text Placed in the FLASH
- .data Placed in the SRAM
- .bss Placed in the SRAM
Startup Code
Contains:
- Reset Handler
- This function copies the initial values for variables from the FLASH where the linker places them to the SRAM.
- It also zeros out the uninitialized data portion of the SRAM.
- Interrupt Vector Table
- This is an array that holds memory address of interrupt handler functions
- In order to allow application code to conveniently redefine the various interrupt handlers, every required vector is assigned an overrideable _weak alias to a default function which loops forever
VMA and LMA
-
LMA : Load Memory Address
The address at which a section is loaded
-
VMA : Virtual Memory Address
The address of a section during execution
-
The linker distinguishes between the VMA and LMA address
Getting familiar with the script (cont.)
- Every allocatable and loadable output section has two addresses: VMA and LMA