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

Resolving Linker Memory Overlap Errors

Learners will diagnose and fix memory layout conflicts by analyzing linker map files and adjusting section size constraints.

In this lesson, you will learn how to identify and resolve linker memory overlap errors by analyzing linker map files and adjusting your linker script memory definitions.

The Problem: When Two Worlds Collide

In embedded systems, the linker script acts as the blueprint for your application’s memory layout. It defines distinct regions (using the MEMORY command) and assigns code or data sections to them. A memory overlap error occurs when your code size exceeds the physical bounds of a region, or when two sections are mapped to overlapping memory addresses.

When the linker reports an error like region 'FLASH' overflowed by 128 bytes or section .text overlaps section .data, it is telling you that the data you've written cannot fit into the physical constraints of the chip.

Diagnosis via the Map File

To resolve this, you must look at the Linker Map File (often generated with the -Wl,-Map=output.map flag). This file provides a bird's-eye view of where every symbol and section is placed. Search the map file for the memory regions and look for the "End Address" of one section compared to the "Start Address" of the next.

The Fix

Once you identify the conflict, you have two primary ways to resolve it:

  1. Adjusting Constraints: If your code is valid but simply too large, you may need to shrink your stack/heap definitions or move specific data sections to a different memory region (like moving constant tables from FLASH to SRAM if your hardware allows).
  2. Modifying the Linker Script: You might find that a section was accidentally placed in the wrong address range. Check your SECTIONS command in the linker script:
MEMORY
{
    FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
    RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 20K
}

SECTIONS
{
    .text : { *(.text) } > FLASH    /* If this exceeds 128K, you get an overlap/overflow */
    .data : { *(.data) } > RAM
}

If you see an error, check if your .text or .data sections are competing for the same address space. You may need to resize the LENGTH of a region or reassign sections to ensure they exist in unique, non-overlapping blocks of address space.

Key Takeaway: Memory overlap errors are solved by cross-referencing your linker map file with your linker script to ensure every section has a unique, non-overflowing home within the defined memory regions.