gcc link file for memory map, what syntax is that?

Johan picture Johan · May 22, 2011 · Viewed 8.9k times · Source

When using gcc to cross-compile for an MCU you provide a linker script file to the linker so it knows how to create the final object file.

I would like to learn more about this type of file, but can't find a nice tutorial on how these files work, what kind of syntax they use, what are best practices, and what to avoid.

Here's an example of a stripped-down link file that would be provided to the linker with the "-Tlinkfile.ld" option:

MEMORY
{
    ram    (rwx) : ORIGIN = 0x20000000, LENGTH = 20k
    rom    (rx)  : ORIGIN = 0x00000000, LENGTH = 128K
}
SECTIONS
{
    .  = 0x0;           /* From 0x00000000 */
    .text : 
    {
        *(.nvic_vector)  /* Vector table */
        *(.text.*)      /* Program code */
        *(.text)        /* Program code */
        *(.rodata)      /* Read only data */
    } >rom

    .  = 0x20000000;    /* From 0x20000000 */      
    .data : 
    {
        *(.data)        /* Data memory */
    } >ram AT > rom

    .bss : 
    {
        *(.bss)         /* Zero-filled run time allocate data memory */
    } >ram AT > rom
}  

/Thanks

Answer

Matthew Slattery picture Matthew Slattery · May 22, 2011

The syntax is documented in the GNU binutils ld documentation - that's more of a reference than a tutorial, but there are various examples scattered through it.