Split Flash Into Bootloader and Application With Two Linker Scripts
Write two GNU linker scripts that share one flash layout file, split flash between bootloader and application, and fail the build when either region overflows.

A bootloader and its application are two programs in one flash chip, built separately and linked for different addresses. The linker script is where you say so, and it is also the one place that can stop the two from silently overlapping. This post builds the pair for an STM32F407 with the GNU linker from binutils 2.43 (arm-none-eabi-gcc 14): a shared layout file included by both scripts, a bootloader script pinned to the first sectors, an application script that starts after a reserved image header, size guards that fail the build, and the commands that verify the result. The ideas apply to any Cortex-M part with memory-mapped flash.
Why two scripts and one source of truth
Each image needs its own linker script, because each has a different flash origin and length. The risk is that the two scripts disagree. If the bootloader script says it owns 32 KB and the application script starts at 0x08004000 because someone edited one file and not the other, the images overlap, and nothing complains until a device boots garbage. The standard defense is a single layout file that defines every boundary once, included by both scripts, plus a C header for the same numbers that a check keeps in step.
The F407's flash begins at 0x08000000, and its sectors are uneven: four of 16 KB, one of 64 KB and seven of 128 KB, so any region boundary must fall on a sector boundary or the update tooling will erase part of a neighbour. The layout used here is:
| Region | Start | Size | Sectors |
|---|---|---|---|
| Bootloader | 0x08000000 | 32 KB | 0 and 1 |
| Boot info | 0x08008000 | 16 KB | 2 |
| Spare | 0x0800C000 | 16 KB | 3 |
| Application header | 0x08010000 | 512 bytes | start of sector 4 |
| Application | 0x08010200 | 960 KB less 512 bytes | sectors 4 to 11 |
The shared layout file
GNU ld accepts symbol assignments before the MEMORY command and lets later expressions use them, as long as the values are absolute. Put the numbers in flash_layout.ld:
/* Single source of truth for the flash map. Included by both linker scripts. */
FLASH_BASE = 0x08000000;
FLASH_SIZE = 1024K;
BOOT_ORIGIN = 0x08000000;
BOOT_LENGTH = 32K;
BOOTINFO_ORIGIN = 0x08008000;
BOOTINFO_LENGTH = 16K;
APP_HDR_ORIGIN = 0x08010000;
APP_HDR_LENGTH = 0x200;
APP_ORIGIN = 0x08010200;
APP_LENGTH = (FLASH_BASE + FLASH_SIZE) - APP_ORIGIN;
RAM_ORIGIN = 0x20000000;
RAM_LENGTH = 128K;The application length is computed from the end of flash, so it adjusts if APP_ORIGIN moves. Keep the numbers in sector multiples. A comment at the top of the file noting which sectors each region covers is worth more than any tool.
The bootloader script
The bootloader owns BOOT_ORIGIN for BOOT_LENGTH bytes and nothing else:
INCLUDE flash_layout.ld
ENTRY(Reset_Handler)
MEMORY
{
BOOT (rx) : ORIGIN = BOOT_ORIGIN, LENGTH = BOOT_LENGTH
RAM (xrw) : ORIGIN = RAM_ORIGIN, LENGTH = RAM_LENGTH
}
_estack = ORIGIN(RAM) + LENGTH(RAM);
SECTIONS
{
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector))
. = ALIGN(4);
} > BOOT
.text :
{
. = ALIGN(4);
*(.text*)
*(.rodata*)
KEEP(*(.init))
KEEP(*(.fini))
. = ALIGN(4);
_etext = .;
} > BOOT
_sidata = LOADADDR(.data);
.data :
{
. = ALIGN(4);
_sdata = .;
*(.data*)
. = ALIGN(4);
_edata = .;
} > RAM AT> BOOT
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} > RAM
ASSERT(LOADADDR(.data) + SIZEOF(.data) <= BOOT_ORIGIN + BOOT_LENGTH,
"bootloader image exceeds its 32 KB region")
}The region overflow check is built in: if .text and the initial .data image do not fit in BOOT, the linker reports region 'BOOT' overflowed by N bytes and stops. The explicit ASSERT adds a message you chose, and also catches the less obvious case where only the load image of .data pushes past the end. Notice that the bootloader declares no APP region at all, so it cannot accidentally place anything there.
The AT> BOOT on .data is the standard idiom: the section runs at a RAM address but is stored in flash right after the code, and the start-up code copies it using _sidata, _sdata and _edata.
The application script
The application starts at APP_ORIGIN and leaves the header region unused:
INCLUDE flash_layout.ld
ENTRY(Reset_Handler)
MEMORY
{
APP_HDR (r) : ORIGIN = APP_HDR_ORIGIN, LENGTH = APP_HDR_LENGTH
APP (rx) : ORIGIN = APP_ORIGIN, LENGTH = APP_LENGTH
RAM (xrw): ORIGIN = RAM_ORIGIN, LENGTH = RAM_LENGTH
}
_estack = ORIGIN(RAM) + LENGTH(RAM);
SECTIONS
{
.isr_vector :
{
. = ALIGN(512);
KEEP(*(.isr_vector))
} > APP
.text :
{
. = ALIGN(4);
*(.text*)
*(.rodata*)
KEEP(*(.init))
KEEP(*(.fini))
. = ALIGN(4);
} > APP
_sidata = LOADADDR(.data);
.data :
{
. = ALIGN(4);
_sdata = .;
*(.data*)
. = ALIGN(4);
_edata = .;
} > RAM AT> APP
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} > RAM
__image_end = LOADADDR(.data) + SIZEOF(.data);
__image_size = __image_end - ORIGIN(APP);
}The APP_HDR region exists to reserve the 512 bytes. Nothing is linked into it. The post-build step prepends the header there when it builds the final image, as the bootloader guide in this cluster shows. Reserving the region in the script keeps the intent visible and lets a future change, such as linking a placeholder header struct into it, happen in one place.
The ALIGN(512) in front of .isr_vector is not needed here, since APP_ORIGIN is already at an offset of 0x200 and the sector base is 512-aligned, but it documents the requirement and makes the linker refuse a layout where it is violated by a change in origin. On the F407 the vector table is 98 entries, so VTOR needs alignment of 512 bytes.
C code that agrees with the scripts
The C side needs the same numbers, preferably from the linker rather than copied. The bootloader can read the application's origin from a symbol that the build supplies from the shared layout file via a small generated header, or define it once in a header that a check compares with the layout file. A simple and reliable pattern is to have the Makefile generate the header from the linker file so there is never a second copy:
awk '/^[A-Z_]+ *= *0x/ { gsub(/;/, ""); print "#define " $1 " " $3 "u" }' \
flash_layout.ld > build/flash_layout.hFor the entries above this writes lines such as #define APP_ORIGIN 0x08010200u. Symbols defined with units (32K) are skipped by the pattern, which is why the layout file gives the addresses in hexadecimal. In C, include build/flash_layout.h and use APP_ORIGIN for the jump target and the CRC range.
Building both
A Makefile fragment that links the two images with their scripts:
CFLAGS = -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os -g
LDFLAGS = -nostartfiles -Wl,--gc-sections -Wl,-Map=$(@:.elf=.map)
build/bootloader.elf: $(BOOT_OBJS) bootloader.ld flash_layout.ld
arm-none-eabi-gcc $(CFLAGS) $(LDFLAGS) -T bootloader.ld -o $@ $(BOOT_OBJS)
build/app.elf: $(APP_OBJS) app.ld flash_layout.ld
arm-none-eabi-gcc $(CFLAGS) $(LDFLAGS) -T app.ld -o $@ $(APP_OBJS)Both rules list flash_layout.ld as a dependency, so editing the layout rebuilds both images. The scripts are found with INCLUDE relative to the working directory, so run the build from the project root or pass -L with the directory containing flash_layout.ld.
Verify what the linker did
Do not trust a script you have not inspected. After building, list the sections:
arm-none-eabi-objdump -h build/bootloader.elf | grep -E "isr_vector|text|data"
arm-none-eabi-objdump -h build/app.elf | grep -E "isr_vector|text|data"
arm-none-eabi-size build/bootloader.elf build/app.elfFor small builds the output looks like this. The numbers depend on your code:
text data bss dec hex filename
6428 12 1056 7496 1d48 build/bootloader.elf
46232 320 9160 55712 d9a0 build/app.elfThen check the addresses with the section headers. The vector table of the bootloader must be at 0x08000000, and the application's at 0x08010200:
Idx Name Size VMA LMA File off Algn
0 .isr_vector 00000188 08010200 08010200 00010200 2**9The 2**9 alignment is the 512-byte requirement. A VMA that is not 0x08010200 means the origin did not take effect, and a .data entry whose LMA is in RAM means the AT> part is missing. Finally, test the guard: temporarily reduce BOOT_LENGTH to 4K and check that the bootloader link fails with a region 'BOOT' overflowed message. A guard you have never seen trigger is a guard you do not know works.
Common problems
**The application still links at
0x08000000.** It picked up a stock script from the toolchain template (-Tpointing at the wrong file), or the linker script command was placed in the wrong rule.**
INCLUDEcannot be found.** The linker searches the current directory and the-Lpaths. Add-L.or an explicit path.Unit mismatch.
32Kand0x8000are the same number, and mixing them is fine for the linker, but a text tool like theawkline above sees only hexadecimal values.The vector table is at the wrong alignment.
VTORignores low bits when the table is not aligned, and the first interrupt jumps to the wrong place.Overlap after a layout change. Add a build step that reads both ELF files and checks that the address ranges of their loadable segments do not intersect. A short Python script using
pyelftoolsdoes it in a dozen lines and runs in continuous integration.


