In this lesson, you will learn how to use the SECTIONS command in a linker script to explicitly direct your compiled code and data into the physical memory regions we defined previously.
The Role of the Linker Script
In our last lesson, we defined our hardware layout using the MEMORY command, creating distinct blocks like FLASH for read-only instructions and SRAM for read-write variables. However, the compiler doesn't automatically know which parts of your code belong in which region. The linker script acts as the "map" that connects the dots between your object files and your physical hardware.
Using the SECTIONS Command
The SECTIONS command tells the linker how to group input sections (like .text for code or .data for initialized variables) into output sections and assign them to a specific memory region.
Consider this example where we place our application code into FLASH and our variables into SRAM:
SECTIONS
{
.text :
{
*(.text) /* Place all compiled code here */
} > FLASH /* Direct this entire section to FLASH */
.data :
{
*(.data) /* Place all initialized variables here */
} > SRAM AT > FLASH /* Lives in SRAM, but stored in FLASH */
}
Understanding the Mapping
- The Input
*(.text): The wildcard*tells the linker to gather the.textsection from every object file provided. - The Output
> FLASH: This tells the linker: "Once you have gathered all the code, ensure the resulting binary block is placed starting at the origin address defined forFLASH." - The
ATKeyword: For initialized data, we useAT > FLASHbecause the data needs to exist in non-volatile memory (FLASH) when the power is off, but it must be copied to its operational address inSRAMwhen the processor boots.
By explicitly mapping these sections, you gain total control over where your software physically resides, allowing you to optimize for features like tight-coupled memory or specialized cache behavior.
Key Takeaway: The SECTIONS command acts as the bridge that maps your logical object segments to the specific physical addresses defined in your MEMORY regions.