Skip to content
Development
Skill

/linker-scripts

Linker script skill for embedded bare-metal targets. Use when writing or modifying GNU ld linker scripts, placing code and data in specific flash or RAM regions, understanding VMA vs LMA, configuring startup .bss/.data initialization, using MEMORY and SECTIONS commands, or

From plugin
low-level-dev-skills
300150 skills

Claude Code

$ npx -y skills add mohitmishra786/low-level-dev-skills --skill linker-scripts --agent claude-code

SKILL.md

linker-scripts.SKILL.md
name: linker-scripts
description: Linker script skill for embedded bare-metal targets. Use when writing or modifying GNU ld linker scripts, placing code and data in specific flash or RAM regions, understanding VMA vs LMA, configuring startup .bss/.data initialization, using MEMORY and SECTIONS commands, or debugging linker errors about regions. Activates on queries about linker scripts, MEMORY command, SECTIONS command, .bss init, VMA vs LMA, weak symbols, or placing functions in specific memory regions.

Linker Scripts

Purpose

Guide agents through writing and modifying GNU ld linker scripts for embedded targets: MEMORY and SECTIONS commands, VMA vs LMA for code relocation, startup `.bss`/`.data` initialization, placing sections in specific regions, and using PROVIDE/KEEP/ALIGN directives.

Triggers

  • "How do I write a linker script for my MCU?"
  • "How do I place a function in a specific flash/RAM region?"
  • "What's the difference between VMA and LMA in a linker script?"
  • "How does .bss and .data initialization work at startup?"
  • "Linker error: region 'FLASH' overflowed"
  • "How do I use weak symbols in a linker script?"

Workflow

1. Linker script anatomy

/* Minimal Cortex-M linker script (works with newlib and C++) */

ENTRY(Reset_Handler)            /* entry point symbol */

MEMORY
{
    FLASH (rx)  : ORIGIN = 0x08000000, LENGTH = 512K
    RAM   (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}

SECTIONS
{
    .text :                     /* code section */
    {
        KEEP(*(.isr_vector))    /* interrupt vector must be first */
        *(.text)
        *(.text.*)
        *(.rodata)
        *(.rodata.*)
        KEEP(*(.init))          /* newlib/crt0 prologue/epilogue */
        KEEP(*(.fini))
        *(.ARM.extab* .gnu.linkonce.armextab.*)  /* unwind data */
        . = ALIGN(4);
    } > FLASH

    .ARM.exidx :                /* unwind tables (C++ exceptions, backtraces) */
    {
        __exidx_start = .;
        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
        __exidx_end = .;
    } > FLASH

    .init_array :               /* static constructors, run by __libc_init_array() */
    {
        PROVIDE_HIDDEN(__preinit_array_start = .);
        KEEP(*(.preinit_array*))
        PROVIDE_HIDDEN(__preinit_array_end = .);
        PROVIDE_HIDDEN(__init_array_start = .);
        KEEP(*(SORT(.init_array.*)))
        KEEP(*(.init_array*))
        PROVIDE_HIDDEN(__init_array_end = .);
        PROVIDE_HIDDEN(__fini_array_start = .);
        KEEP(*(SORT(.fini_array.*)))
        KEEP(*(.fini_array*))
        PROVIDE_HIDDEN(__fini_array_end = .);
        . = ALIGN(4);
        _etext = .;             /* end of flash code/constants */
    } > FLASH

    .data :                     /* VMA = RAM, LMA = FLASH */
    {
        _sdata = .;
        *(.data)
        *(.data.*)
        . = ALIGN(4);
        _edata = .;
    } > RAM AT > FLASH
    _sidata = LOADADDR(.data);  /* LMA of .data, used by startup copy loop */

    .bss (NOLOAD) :
    {
        _sbss = .;
        __bss_start__ = _sbss;  /* name newlib crt0 expects */
        *(.bss)
        *(.bss.*)
        *(COMMON)
        . = ALIGN(4);
        _ebss = .;
        __bss_end__ = _ebss;
    } > RAM

    PROVIDE(end = _ebss);       /* heap start for newlib _sbrk */

    /* Stack at top of RAM */
    _estack = ORIGIN(RAM) + LENGTH(RAM);
}

The `.init`/`.fini`, `.ARM.exidx`/`.ARM.extab` and `.init_array` rules are needed as soon as newlib, libstdc++ or C++ exceptions are linked in. Without them those sections become orphans after `.text` and overlap the load address of `.data`. `__bss_start__`, `__bss_end__` and `end` are the names newlib's `crt0` and `_sbrk` expect.

2. VMA vs LMA

  • **VMA** (Virtual Memory Address): where the section runs at runtime
  • **LMA** (Load Memory Address): where the section is stored in the image (flash)

For `.data`: stored in flash (LMA), copied to RAM at startup (VMA).

/* VMA in RAM, LMA in the next free spot of FLASH */
.data :
{
    _sdata = .;
    *(.data)
    _edata = .;
} > RAM AT > FLASH
_sidata = LOADADDR(.data);       /* where startup code copies from */

Prefer `> RAM AT > FLASH` over `AT(_etext)` or `AT(ADDR(.text) + SIZEOF(.text))`. The region form puts the LMA after every section already placed in FLASH (`.ARM.exidx`, `.init_array`, ...). A fixed address ignores them, and they end up overlapping `.data`.

3. Startup .bss / .data initialization

The C runtime must copy `.data` from flash to RAM and zero `.bss` before `main()`:

// startup.c or startup.s equivalent in C
extern uint32_t _sdata, _edata, _sidata; // _sidata = LMA of .data
extern uint32_t _sbss, _ebss;

void Reset_Handler(void) {
    // Copy .data from flash to RAM
    uint32_t *src = &_sidata;
    uint32_t *dst = &_sdata;
    while (dst < &_edata) *dst++ = *src++;

    // Zero-initialize .bss
    dst = &_sbss;
    while (dst < &_ebss) *dst++ = 0;

    // Call C++ constructors
    // (call __libc_init_array() if using newlib)

    main();
    for (;;);  // should never return
}

The linker script provides `_sidata` with `_sidata = LOADADDR(.data);`, as in section 1.

4. Placing code in specific regions

/* Place time-critical code in RAM for faster execution */
MEMORY
{
    FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
    RAM   (rwx): ORIGIN = 0x20000000, LENGTH = 128K
    CCM   (rw) : ORIGIN = 0x10000000, LENGTH = 64K   /* STM32F4 CCM: data only */
}

.fast_code :
{
    . = ALIGN(4);
    _sfast = .;
    *(.fast_code)   /* sections marked __attribute__((section(".fast_code"))) */
    . = ALIGN(4);
    _efast = .;
} > RAM AT > FLASH   /* runs from SRAM, stored in flash */
_sifast = LOADADDR(.fast_code);
// Mark a function to go in fast_code section
__attribute__((section(".fast_code"), noinline))  /* noinline: keep it out of flash callers */
void critical_isr_handler(void) {
    // runs from SRAM; startup must copy _sifast -> [_sfast, _efast) like .data
}

On STM32F4

Read more
@mohitmishra786
Ships withlow-level-dev-skills

A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.

Stars on GitHub

Momentum

stars in total
300
Star history of mohitmishra786/low-level-dev-skills on GitHub, a chart by star-history.com

Other skills on low-level-dev-skills.

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills3w309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc4w293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react2w252k
@facebook@facebookInstall Skill

Questions about linker-scripts.

Which coding agents can use linker-scripts?
linker-scripts installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.