In this lesson, you will learn how to logically segment your physical flash memory into distinct zones for bootloaders, applications, and non-volatile data using linker scripts.
Why Partition Flash?
In most embedded systems, the flash memory is a continuous address space, but your application requires distinct "neighborhoods." You must prevent your bootloader from being overwritten by an application update and ensure your configuration parameters (like calibration constants) don't get clobbered by your code. By defining these partitions in the linker script, you instruct the toolchain to treat these regions as separate memory banks with unique access constraints.
Defining Memory Regions
The linker script manages this through the MEMORY command. By specifying the ORIGIN and LENGTH for each partition, you create boundaries that the linker uses to check for overflow errors during the build process.
MEMORY
{
/* 16KB for the Bootloader */
BOOTLOADER (rx) : ORIGIN = 0x08000000, LENGTH = 0x4000
/* 240KB for the Application */
FLASH_APP (rx) : ORIGIN = 0x08004000, LENGTH = 0x3C000
/* 8KB for Non-Volatile Config Storage */
CONFIG (r) : ORIGIN = 0x08040000, LENGTH = 0x2000
}
Mapping Sections to Partitions
Once the regions are defined, you must map your code and data sections into them using the SECTIONS command. For example, to ensure your application code stays within the FLASH_APP region, you assign your .text section to that specific memory bank:
SECTIONS
{
.text : {
*(.isr_vector)
*(.text)
} > FLASH_APP
.config_data : {
KEEP(*(.config_section))
} > CONFIG
}
By placing the configuration data in its own CONFIG partition, you decouple it from the application logic. This allows you to update your application code independently without erasing or corrupting your persistent settings. If your code grows beyond the LENGTH specified, the linker will immediately flag a "region overflow" error, preventing you from shipping a broken binary.
Key Takeaway: Linker scripts use the MEMORY and SECTIONS commands to enforce hardware-level isolation, ensuring your bootloader, application, and configuration data occupy safe, non-overlapping physical addresses.