What is Linker Script for ARM - Explained Simply
A linker script for ARM is a plain text file that tells the linker how to arrange program sections in memory by defining memory regions and placing code and data using commands like
MEMORY and SECTIONS.Examples
InputSimple linker script with one memory region
OutputDefines a single RAM region and places all code and data starting at address 0x20000000.
InputLinker script with FLASH and RAM regions
OutputSeparates code into FLASH memory starting at 0x08000000 and data into RAM at 0x20000000.
InputLinker script with custom stack size
OutputReserves a specific memory area for the stack and places it at the top of RAM.
Code
linker_script
/* Simple ARM linker script example */ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 40K } SECTIONS { .text : { *(.text*) } > FLASH .data : { *(.data*) } > RAM .bss : { *(.bss*) } > RAM }
Output
Defines FLASH and RAM memory regions and places .text section in FLASH and .data and .bss sections in RAM.
Why This Works
Step 1: Define Memory Regions
The MEMORY command specifies physical memory areas like FLASH and RAM with start addresses and sizes.
Step 2: Assign Sections to Memory
The SECTIONS command maps program sections such as .text (code) and .data (initialized data) to these memory regions.
Step 3: Control Program Layout
This script controls where code and data reside in the ARM device's memory, which is critical for correct execution.
Alternative Approaches
Using default linker script
linker_script
No custom linker script; rely on compiler defaults.
Simpler but less control; may not fit specific memory layouts.
Using linker script generator tools
linker_script
Use tools like STM32CubeMX to auto-generate linker scripts.
Faster setup but less learning about memory layout details.
Complexity: O(1) time, O(1) space
Time Complexity
Linker scripts are static configuration files, so they do not affect runtime complexity.
Space Complexity
They define memory layout but do not consume extra runtime memory themselves.
Which Approach is Fastest?
Using a custom linker script or default script has no impact on execution speed; it only affects memory placement.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Custom Linker Script | O(1) | O(1) | Precise memory control |
| Default Linker Script | O(1) | O(1) | Quick setup, less control |
| Generated Linker Script | O(1) | O(1) | Ease of use with hardware tools |
Always tailor your linker script to match your ARM device's actual memory map for reliable program execution.
Forgetting to update memory addresses in the linker script to match the target ARM hardware causes runtime errors.