Complete Guide to
Linker Scripts in
Embedded Systems
A linker script is a crucial
component in embedded system
programming that controls how
the linker arranges code and data
in memory. In embedded systems,
where memory is limited and
hardware-specific, understanding
linker scripts is essential to ensure
proper code execution.
Linker Scripts in Embedded Systems
01. What is a Linker Script?
A linker script is a configuration file (usually with a .ld extension)
used by the GNU Linker (ld) to define:
The memory layout of a microcontroller or processor.
The placement of different sections of a program (code,
initialized data, uninitialized data, stack, and heap).
How and where different program segments should be stored
in memory.
It provides fine-grained control over program memory, making it
essential in bare-metal programming and embedded systems.
Linker Scripts in Embedded Systems
02. Why is a Linker Script Used in Embedded
Systems?
Unlike desktop applications that run in an operating system with
virtual memory, embedded systems have:
1. Limited Memory: Programs must fit into small FLASH and
RAM memory regions.
2. No Dynamic Memory Management: Memory allocation must
be carefully planned at compile-time.
3. Specific Memory Mapping: Certain memory locations are
reserved for peripherals, bootloaders, interrupt vectors, etc.
4. Bootloader and Firmware Updates: A linker script ensures
correct placement of bootloaders, application code, and update
mechanisms.
5. Efficient Use of Memory: Ensures code, data, and stack are
properly aligned to avoid fragmentation.
Linker Scripts in Embedded Systems
03. How a Linker Script Works
The linker script controls how the
compiler output (.o files) is combined into
an executable.
Compilation Process
1. Preprocessing & Compilation (gcc -c)
a. Converts C files into object files
(.o).
2. Assembly (as)
a. Converts assembly files into
machine code.
3. Linking (ld -T script.ld)
a. Uses the linker script to arrange
sections and generate a final
executable (.elf or .bin).
4. Flashing
a. The final binary is loaded onto the
microcontroller using tools like
OpenOCD or ST-Link.
Linker Scripts in Embedded Systems
04. Basic Structure of a Linker Script
A linker script consists of three main parts:
1. MEMORY
a. Defines memory regions (FLASH, RAM, etc.).
2. ENTRY()
a. Defines the entry point of execution.
3. SECTIONS
a. Specifies where different sections (.text, .data, .bss) go.
05. Example Linker Script (stm32.ld)
Linker Scripts in Embedded Systems
Linker Scripts in Embedded Systems
06. Breakdown of Sections
MEMORY
Defines FLASH (code storage) and RAM (runtime storage).
ORIGIN: Start address.
LENGTH: Size of the memory.
ENTRY(Reset_Handler)
Defines the entry function, usually Reset_Handler, which
initializes the system.
SECTIONS
.text: Contains program instructions and is placed in FLASH.
.data: Contains initialized variables, copied from FLASH to
RAM at startup.
.bss: Contains uninitialized variables, initialized to zero at
runtime.
.stack: Reserves memory for the stack.
Linker Scripts in Embedded Systems
07. Common Symbols in Linker Scripts
These symbols can be used in C startup files to manage memory
layout:
Example usage in C:
Linker Scripts in Embedded Systems
08. Debugging Linker Script Issues
09. Conclusion
A linker script is essential for defining memory layout in
embedded systems.
It maps program sections (.text, .data, .bss) to FLASH and
RAM.
It controls execution startup and ensures efficient memory
usage.
Debugging linker issues helps in optimizing embedded
applications.
Was it Like
helpful? Comment
follow for more!
Share
Chetan Shidling
@chetan_shidling_ Save
www.cselectricalandelectronics.com