Linker
This component defines the memory layout and segment placement for the STM32 microcontroller firmware. It is a critical part of the BSP (Board Support Package) and is required by the linker to correctly organize code and data in the device's memory.
Purpose
The linker script provides:
- Explicit control over memory regions such as Flash, RAM, and CCMRAM
- Placement of special sections like the vector table,
.text,.data,.bss, and stack - Symbols that are referenced during startup (e.g.,
_sdata,_edata,_sidata) - Support for CMSIS-style initialization arrays (
.preinit_array,.init_array)
Key Features
- Defines
FLASH,RAM, and optionallyCCMRAMwithORIGINandLENGTH - Places the vector table at the beginning of
FLASH - Reserves memory for:
- Initialized data (
.data) - Uninitialized data (
.bss) - Heap and stack
- Initialized data (
- Optionally places critical data or stack in
CCMRAMfor faster access - Adds read-only permissions for
.init_array,.preinit_array, and.fini_arrayto prevent RWX warnings from the linker
Typical Layout
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
}
SECTIONS
{
.isr_vector :
{
KEEP(*(.isr_vector))
} > FLASH
.text :
{
*(.text*)
*(.rodata*)
} > FLASH
.data : AT (_etext)
{
_sdata = .;
*(.data*)
_edata = .;
} > RAM
.bss :
{
_sbss = .;
*(.bss*)
_ebss = .;
} > RAM
.ccm_data :
{
*(.ccmram*)
} > CCMRAM
.stack :
{
. = ALIGN(8);
_stack_top = .;
} > RAM
/* Initialization arrays */
.preinit_array (READONLY) :
{
KEEP(*(.preinit_array))
} > FLASH
.init_array (READONLY) :
{
KEEP(*(.init_array))
} > FLASH
}
Symbols Provided
The linker script defines symbols used in the startup and initialization process:
-
_sidata: Start address of the initial values for the.datasection, typically located in Flash -
_sdata: Start address of the.datasection in RAM -
_edata: End address of the.datasection in RAM -
_sbss: Start address of the.bsssection (uninitialized data) in RAM -
_ebss: End address of the.bsssection in RAM -
_ccmram_start,_ccmram_end: Bounds of the CCMRAM memory area (optional) -
_stack_top: Top of the stack, usually at the end of RAM
These symbols are used by the startup code to:
- Copy initialized data from Flash to RAM (
.datasection) - Zero-initialize the
.bsssection - Set the initial stack pointer
- Optionally relocate the vector table to RAM or CCMRAM if needed
Usage Notes
- The linker script must be kept in sync with the memory configuration defined in your project settings.
- Any custom sections added in your code (e.g.,
.ccmram,.bootloader,.shared) must be accounted for in the linker script. - Pay attention to alignment and section permissions to avoid hard faults or unexpected behavior.
Integration
The linker file script is generated from STM32G4xx_Linker.ld.in by CMake and depends on configured STM32 MCU.
Ensure your compiler and linker flags allow usage of these symbols in your C code, typically via:
extern uint32_t _sdata;
extern uint32_t _edata;
extern uint32_t _sidata;
extern uint32_t _sbss;
extern uint32_t _ebss;
extern uint32_t _ccmram_start;
extern uint32_t _ccmram_length;
extern uint32_t _ccmram_end;
extern uint32_t _ram_start;
extern uint32_t _ram_length;
extern uint32_t _ram_end;
🛠 CMake Integration
- Include
Nvic_Libin your CMake library. - Include
Nvic_Port.hin your project. - Link against the Nvic module implementation files.
- Configure the module as needed for your hardware.
License
This project is licensed under the Creative Commons Attribution–NonCommercial 4.0 International (CC BY-NC 4.0).
You are free to use, modify, and share this work for non-commercial purposes, provided appropriate credit is given.
See LICENSE.md for full terms or visit creativecommons.org/licenses/by-nc/4.0.
Authors
- Mr.Nobody — embedbits.com
Contributions are welcome! Please open a pull request.