Skip to main content

Linker

Supported STM32 families: F4 · G4 · H5 · H7 · L4 · U5

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 optionally CCMRAM with ORIGIN and LENGTH
  • Places the vector table at the beginning of FLASH
  • Reserves memory for:
    • Initialized data (.data)
    • Uninitialized data (.bss)
    • Heap and stack
  • Optionally places critical data or stack in CCMRAM for faster access
  • Adds read-only permissions for .init_array, .preinit_array, and .fini_array to 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 .data section, typically located in Flash

  • _sdata: Start address of the .data section in RAM

  • _edata: End address of the .data section in RAM

  • _sbss: Start address of the .bss section (uninitialized data) in RAM

  • _ebss: End address of the .bss section 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:

  1. Copy initialized data from Flash to RAM (.data section)
  2. Zero-initialize the .bss section
  3. Set the initial stack pointer
  4. 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​

  1. Include Nvic_Lib in your CMake library.
  2. Include Nvic_Port.h in your project.
  3. Link against the Nvic module implementation files.
  4. 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​

Contributions are welcome! Please open a pull request.