Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 225 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Mapping Object Sections to Memory Regions

Learners will use the SECTIONS command to explicitly assign compiled code and data segments to the memory regions defined previously.

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 .text section 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 for FLASH."
  • The AT Keyword: For initialized data, we use AT > FLASH because the data needs to exist in non-volatile memory (FLASH) when the power is off, but it must be copied to its operational address in SRAM when 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.