The Linker Script: Sections on the Memory Map

Start-up code copies .data "from its load address to its run address". Who decided those two addresses? A file of twenty lines that you write once for each chip.

You Will Be Able To

  • Read a linker script as two lists: the memories of the chip, and the sections with their places.
  • Work out the run address, the load address and the linker symbols of a small program.
  • Compare execute in place with copy to RAM by the RAM each needs and the words each copies.
Lesson
S6.17
Time
26 min
Runs on
Your browser
Check
5 questions

A product has run for a year. Then one buffer grows from 64 bytes to 496. The build prints no error, and the unit tests pass. In the field the device restarts now and then, always deep inside a nested call.

Before You Start

You need Start-Up Code: .data, .bss, the load address, the run address and the stack top. You need The Memory Map: a region has a first address and a size.

The linker and its symbols come from Assembler, Linker and Loader.

Five questions this page answers
Question
1What are the two parts of a linker script?
2How does the linker work out the address of each section?
3Which section has a load address that differs from its run address, and why?
4Where do the addresses in start-up code come from?
5What does it cost to copy the code to RAM, and what does it buy?

The Idea: Tell the Linker About the Chip

The linker knows the sizes of the sections. It does not know your chip. A text file tells it, and that file is called the linker script. The script has two lists.

The first list names the memories of the chip: where each begins and how long it is. It is the memory map again, cut down to the regions that hold the program.

The second list names the sections in order, and says in which memory each one runs. The linker walks down it with a location counter for each memory: the next free address. It gives a section that address, then adds the size of the section.

For .data the script names two memories. It runs in RAM and is stored in ROM. So the linker moves both counters, and the section gets a run address and a load address.

Last, the script gives names to the addresses that start-up code needs. Those names are linker symbols. The start-up code of the last lesson used numbers such as 0xD0. In a real project it uses the symbols, and the linker fills in the numbers.

New Words

New words on this page
WordMeaning
.textThe section that holds the instructions.
.rodataThe section of constants: data that is read and never written.
Location counterThe next free address of a memory while the linker places sections.
Linker symbolA name that the script gives to an address, such as the end of .data.
Execute in placeThe code runs from the memory it is stored in. XIP for short.
Copy to RAMThe code is copied to RAM at start, and runs there.

One Picture: Sections and Memories

Four Sections Drop into Two MemoriesStep through the picture. The linker script says which memory each section runs in.
  • Processor
  • Peripheral
  • Memory
Four Sections Drop Into Two MemoriesStep through the picture. The linker script says which memory each section runs in. component .text the instructions component .rodata constants that neverchange component .data variables with a firstvalue component .bss variables that startat 0 component ROM keeps its content;cannot be written component RAM can be written; holdsnothing at power-on runs hereruns herestored hereruns hereruns here
  1. component .textthe instructions
    • runs here → ROM
  2. component .rodataconstants that never change
    • runs here → ROM
  3. component ROMkeeps its content; cannot be written
  4. component .datavariables with a first value
    • stored here → ROM
    • runs here → RAM
  5. component RAMcan be written; holds nothing at power-on
  6. component .bssvariables that start at 0
    • runs here → RAM
  1. Code and constants

    Nothing writes them, and they must be there at power-on. They are stored in ROM and used in ROM.

  2. .data needs both

    It must be there at power-on, so it is stored in ROM. It must be writable, so it runs in RAM. One section, two addresses.

  3. .bss needs only RAM

    It has no first values to keep. The script only reserves its room.

  4. One counter for each memory

    Within a memory the sections follow each other. The linker keeps the next free address of each memory, and moves it on by the size of each section.

Read the diagram as text
  • .text: the instructions
  • .rodata: constants that never change
  • .data: variables with a first value
  • .bss: variables that start at 0
  • ROM: keeps its content; cannot be written
  • RAM: can be written; holds nothing at power-on
  • .text → ROM: runs here
  • .rodata → ROM: runs here
  • .data → ROM: stored here
  • .data → RAM: runs here
  • .bss → RAM: runs here

The layout that the script on this page describes. The stack is not a section: it takes the top of RAM.

Here is such a file for the layout of the picture, in the syntax of a widely used free linker.

A linker script, written for this page. A dot is the location counter. "> RAM AT > ROM" means: runs in RAM, stored in ROM. This course has no linker, so the file is shown and not run.
MEMORY
{
  ROM (rx)  : ORIGIN = 0x00000000, LENGTH = 4K
  RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 64K
}
SECTIONS
{
  .text   : { *(.text*) }   > ROM
  .rodata : { *(.rodata*) } > ROM
  .data   : { _data_start = .;  *(.data*)  _data_end = .; } > RAM AT > ROM
  _data_load = LOADADDR(.data);
  .bss    : { _bss_start = .;  *(.bss*)  _bss_end = .; } > RAM
  _stack_top = ORIGIN(RAM) + LENGTH(RAM);
}
Where the Address Goes in ap_soc v1.0
Where the Address Goes in ap_soc v1.0Addresses increase upward. Region heights use a compressed scale. In ap_soc, reads and writes to reserved or unmapped addresses return ERROR. AHB IDLE and BUSY receive zero-wait OKAY (IHI 0033 issue C §§3.2, 4.2.1). Selected. 0x8000_0000 = SRAM + 0x0 Exact ranges follow in the table. ROM RAM Device Reservedunmapped active transfer: ERROREnd 0xffff_ffffReserved SRAM expansion, reserved, AHB, 0x8001_0000, 64 KiB Reserved SRAMexpansion 64 KiB 0x8001_0000 SRAM, ram, AHB, 0x8000_0000, 64 KiB SRAM 64 KiB 0x8000_0000 0x8000_0000 = SRAM + 0x0 Reserved APB expansion, reserved, APB, 0x1000_8000, 32 KiB Reserved APBexpansion 32 KiB 0x1000_8000 DMA, device, APB, 0x1000_6000, 4 KiB DMA 4 KiB 0x1000_6000 SPI, device, APB, 0x1000_5000, 4 KiB SPI 4 KiB 0x1000_5000 ap_sysctl, device, APB, 0x1000_4000, 4 KiB ap_sysctl 4 KiB 0x1000_4000 Watchdog, device, APB, 0x1000_3000, 4 KiB Watchdog 4 KiB 0x1000_3000 Timer and PWM, device, APB, 0x1000_2000, 4 KiB Timer and PWM 4 KiB 0x1000_2000 GPIO, device, APB, 0x1000_1000, 4 KiB GPIO 4 KiB 0x1000_1000 UART0, device, APB, 0x1000_0000, 4 KiB UART0 4 KiB 0x1000_0000 ap_plic, device, AHB, 0x0c00_0000, 4 MiB ap_plic 4 MiB 0x0c00_0000 ap_clint, device, AHB, 0x0200_0000, 64 KiB ap_clint 64 KiB 0x0200_0000 Boot ROM, rom, AHB, 0x0000_0000, 4 KiB Boot ROM 4 KiB 0x0000_0000 AHB APB bridge window 0x1000_0000 · 64 KiB AHB

Addresses increase upward. Region heights use a compressed scale. In ap_soc, reads and writes to reserved or unmapped addresses return ERROR. AHB IDLE and BUSY receive zero-wait OKAY (IHI 0033 issue C §§3.2, 4.2.1).

Exact Address Ranges and Descriptions
Where the Address Goes in ap_soc v1.0 · inclusive end addresses
RegionBaseLast AddressSizeBusKindDescription
Boot ROM 0x0000_00000x0000_0fff4 KiB AHBromReset vector and boot code.
Unmapped 0x0000_10000x01ff_ffff32764 KiB —holeReads and writes receive the default error response.
ap_clint 0x0200_00000x0200_ffff64 KiB AHBdeviceCore-local timer and software interrupts.
Unmapped 0x0201_00000x0bff_ffff163776 KiB —holeReads and writes receive the default error response.
ap_plic 0x0c00_00000x0c3f_ffff4 MiB AHBdevicePlatform interrupt controller, 16 sources.
Unmapped 0x0c40_00000x0fff_ffff60 MiB —holeReads and writes receive the default error response.
UART0 0x1000_00000x1000_0fff4 KiB APBdeviceSerial console.
GPIO 0x1000_10000x1000_1fff4 KiB APBdevice16 general-purpose pins.
Timer and PWM 0x1000_20000x1000_2fff4 KiB APBdeviceTimer and pulse-width modulation.
Watchdog 0x1000_30000x1000_3fff4 KiB APBdeviceWatchdog reset control.
ap_sysctl 0x1000_40000x1000_4fff4 KiB APBdeviceChip ID, reset cause, clock control, pin mux and straps.
SPI 0x1000_50000x1000_5fff4 KiB APBdeviceSPI controller.
DMA 0x1000_60000x1000_6fff4 KiB APBdeviceDMA control registers; transfers use its AHB manager port.
Unmapped 0x1000_70000x1000_7fff4 KiB —holeReads and writes receive the default error response.
Reserved APB expansion 0x1000_80000x1000_ffff32 KiB APBreservedReserved for v2 peripherals; accesses return an error.
Unmapped 0x1001_00000x7fff_ffff1834944 KiB —holeReads and writes receive the default error response.
SRAM 0x8000_00000x8000_ffff64 KiB AHBramProgram and data memory.
Reserved SRAM expansion 0x8001_00000x8001_ffff64 KiB AHBreservedReserved for v2 SRAM growth; accesses return an error.
Unmapped 0x8002_00000xffff_ffff2097024 KiB —holeReads and writes receive the default error response.

On the map of ap_soc, ROM is the region at 0 and RAM is the SRAM at 0x8000_0000. The script must agree with this map, region for region.

The Smallest Example, Worked by Hand

A small program has four sections. Sizes are in bytes, in hexadecimal. ROM begins at 0 and RAM at 0x8000_0000.

Placing four sections by hand
SectionSizeRun addressLoad addressROM counter afterRAM counter after
.text0x1000x0000_0000the same0x1000x8000_0000
.rodata0x200x0000_0100the same0x1200x8000_0000
.data0x100x8000_00000x0000_01200x1300x8000_0010
.bss0x200x8000_0010none0x1300x8000_0030

Each row takes the counters of the row above. The section .data moves both. The section .bss moves only the RAM counter, because nothing is stored for it.

The symbols fall out of the same table. The load address of .data is 0x120. It starts at 0x8000_0000 and ends at 0x8000_0010. So start-up code copies 0x10 bytes, which is four words. Then it clears 0x20 bytes, eight words.

The Rules

  1. Rule 1: The memories of the script are regions of the memory map.

    An address that the script invents is an address where nothing answers.

  2. Rule 2: Within a memory, sections follow each other in the order of the script.

    The location counter moves on by the size of each section.

  3. Rule 3: A section that must survive power-off is stored in memory that does not forget.

    That holds for code, for constants and for the first values of .data.

  4. Rule 4: A section that is written must run in RAM.

    So .data has two addresses, and .bss has one.

  5. Rule 5: Start-up code takes its addresses from linker symbols.

    A number typed by hand is wrong after the next change of a section size.

  6. Rule 6: The stack is not a section, and the linker does not watch it.

    Leave room between the end of the sections and the lowest address the stack may reach, and check it.

Sources for These Rules

Patterson and Hennessy, Computer Organization and Design, ARM edition, section 2.12: what a linker does with segments and addresses. Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 6.5.1 and 6.5.4: the memory map of a program, and linking.

Neither book shows a linker script. The script on this page, its keywords and rules 1 to 6 are common practice with the GNU linker. They were not checked against a library source.

The lab is our own small linker in test code. Its rule of rounding each section up to a multiple of 4 is a choice.

Now Run It

The lab at the end of this page is a very small linker, written as test code. It has the two memories of the script, the sizes of four sections, and the two location counters. It moves no code. It only works out addresses.

Recorded from the lab on Icarus. Sizes and addresses are in hexadecimal; the counts in the last four lines are decimal.
.text    run=00000000 load=00000000 size=000001a4
.rodata  run=000001a4 load=000001a4 size=0000003c
.data    run=80000000 load=000001e0 size=00000018
.bss     run=80000018 load=-------- size=00000040
_data_load=000001e0 _data_start=80000000 _data_end=80000018
_bss_start=80000018 _bss_end=80000058 _stack_top=80010000
ROM: 504 of 4096 bytes used
RAM: 88 bytes of sections, 1024 bytes of stack, 1112 of 65536 used
free between the sections and the stack: 64424 bytes
start-up copies 6 words and clears 16 words

Rule 2 at work: .rodata begins at 1a4, where .text ends. Rule 4 at work: .data is the only row with two different addresses. The row of .bss has no load address at all.

The two lines that begin with an underscore hold the symbols that start-up code needs. With them it copies 6 words and clears 16.

Now the other choice. Set XIP to 0, and the code runs from RAM.

The same lab with XIP at 0: copy to RAM.
.text    run=80000000 load=00000000 size=000001a4
.rodata  run=800001a4 load=000001a4 size=0000003c
.data    run=800001e0 load=000001e0 size=00000018
.bss     run=800001f8 load=-------- size=00000040
_data_load=000001e0 _data_start=800001e0 _data_end=800001f8
_bss_start=800001f8 _bss_end=80000238 _stack_top=80010000
ROM: 504 of 4096 bytes used
RAM: 568 bytes of sections, 1024 bytes of stack, 1592 of 65536 used
free between the sections and the stack: 63944 bytes
start-up copies 126 words and clears 16 words
Execute in place against copy to RAM, for this program
Execute in placeCopy to RAM
.text runs at00000000, in ROM80000000, in RAM
RAM taken by sections88 bytes568 bytes
Words copied at start6126

From the two recorded runs.

Copy to RAM costs RAM, and it costs time at every start. What it buys does not show in this lab: speed. Program memory outside the chip is often slow to read, and RAM is fast. The boot loader of The Boot Flow made this choice for ap_soc.

The lab leaves things out. A real linker also joins files, resolves symbols and fills addresses into the code. Real scripts have more sections than four.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
The chip is described in a scriptThe chip is built into the linkerOne linker serves every chip. Each chip needs a script that agrees with its memory map.
Symbols carry the addresses to start-up codeNumbers typed into start-up codeThe addresses follow every change of size. A wrong symbol name fails at link time, which is early.
Execute in placeCopy to RAMLittle RAM and a fast start. Every fetch has the speed of the program memory.

The Trap: The Stack Is Nobody's Section

Back to the product from the top of the page. It is a small chip with 1024 bytes of RAM. The program asks for 512 bytes of stack. Before the change, the lab reports:

The small chip with a buffer of 64 bytes in .bss.
RAM: 88 bytes of sections, 512 bytes of stack, 600 of 1024 used
free between the sections and the stack: 424 bytes

Then the buffer grows to 496 bytes.

The same chip with the larger buffer.
RAM: 520 bytes of sections, 512 bytes of stack, 1032 of 1024 used
the stack and the sections overlap by 8 bytes

The sections still fit in RAM by themselves: 520 of 1024 bytes. A linker that is not asked about the stack reports nothing. But the deepest calls now reach 8 bytes into the buffer.

Most of the time the stack is shallow, and nothing happens. A deep call writes a return address over the end of the buffer, or the buffer is filled over a return address. The fault appears far from its cause.

Rule 6 is the cure. Give the stack a size in the script. Then let the script compare the end of the sections with the bottom of the stack. Our lab does that in its last check.

At Scale

This page4 sections, 2 memories
A productdozens of sections, many memories

This section looks ahead. The lab places four sections in two memories. The script of a product is several pages long.

  1. More memories

    A chip may have fast RAM beside the processor, slow RAM further away, and memory that keeps its content in sleep. Each is a line in the first list. Code or data is steered to it by name.

  2. Tables for start-up code

    With several regions to copy and to clear, the script builds a table of them. Start-up code walks the table.

  3. A script from the map

    Teams keep the memory map as data and build the script from it. The map, the address decoder and the script then cannot disagree.

  4. Two programs on one chip

    A boot loader and an application each have a script. The two scripts must share the memories without overlap.

Try It

Run the lab. Check each address of the output against the sizes at the top of the file.

Set XIP to 0. Find the three numbers of the comparison table in the output.

Set RAM_SIZE to 400 in hexadecimal and STACK to 200 in hexadecimal. Then raise BSS until the last check reports an overlap.

Give .rodata a size that is not a multiple of 4. Say what align4 does to the load address of .data.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: .text run=00000000 load=00000000 size=000001a4 .rodata run=000001a4 load=000001a4 size=0000003c .data run=80000000 load=000001e0 size=00000018 .bss run=80000018 load=-------- size=00000040 _data_load=000001e0 _data_start=80000000 _data_end=80000018 _bss_start=80000018 _bss_end=80000058 _stack_top=80010000 ROM: 504 of 4096 bytes used RAM: 88 bytes of sections, 1024 bytes of stack, 1112 of 65536 used free between the sections and the stack: 64424 bytes start-up copies 6 words and clears 16 words

Check Yourself

Predict the outputThe sections are now 0x200, 0x20, 0x10 and 0x30 bytes, with execute in place. What is the last line the lab prints?
// A very small linker, as test code. It places the sections of a program in the memories of a chip,
// the way a linker script tells a real linker to. It moves no code: it only works out addresses.
module tb;
  // ---- The memories (the MEMORY part of a linker script) ----
  localparam logic [31:0] ROM_BASE = 32'h0000_0000, ROM_SIZE = 32'h1000;
  localparam logic [31:0] RAM_BASE = 32'h8000_0000, RAM_SIZE = 32'h10000;
  // ---- The sections, in bytes (what the compiler produced) ----
  localparam int TEXT = 'h200, RODATA = 'h20, DATA = 'h10, BSS = 'h30;
  localparam int STACK = 'h400;          // the room the program asks for its stack
  localparam bit XIP = 1;                // 1: execute in place, the code runs from ROM. 0: the code is copied to RAM

  logic [31:0] rom_at, ram_at;             // the two location counters: the next free address of each memory
  logic [31:0] run, load;
  int copy_bytes = 0;
  function automatic logic [31:0] align4(input logic [31:0] a);   // up to the next multiple of 4
    return (a + 3) & ~32'd3;
  endfunction
  // Place one section. in_ram: it runs in RAM. stored: its first values are kept in ROM.
  task automatic section(input string name, input int size, input bit in_ram, input bit stored);
    if (in_ram) begin ram_at = align4(ram_at);  run = ram_at;  ram_at += size; end
    if (stored) begin rom_at = align4(rom_at);  load = rom_at;  rom_at += size; end
    if (!in_ram) run = load;
    if (in_ram && stored) copy_bytes += size;
    if (stored) $display("%-8s run=%08h load=%08h size=%04h", name, run, load, size);
    else        $display("%-8s run=%08h load=-------- size=%04h", name, run, size);
  endtask

  logic [31:0] data_load, data_start, data_end, bss_start, bss_end, stack_top, stack_bottom;
  initial begin
    rom_at = ROM_BASE;  ram_at = RAM_BASE;
    // ---- The SECTIONS part of a linker script: one line for each section, in order ----
    section(".text",   TEXT,   !XIP, 1);
    section(".rodata", RODATA, !XIP, 1);
    section(".data",   DATA,   1,    1);   data_start = run;  data_load = load;  data_end = ram_at;
    section(".bss",    BSS,    1,    0);   bss_start = run;  bss_end = ram_at;
    stack_top = RAM_BASE + RAM_SIZE;       // the stack starts at the top of RAM and grows down
    stack_bottom = stack_top - STACK;
    // ---- The symbols that start-up code uses ----
    $display("_data_load=%08h _data_start=%08h _data_end=%08h", data_load, data_start, data_end);
    $display("_bss_start=%08h _bss_end=%08h _stack_top=%08h", bss_start, bss_end, stack_top);
    // ---- The checks ----
    $display("ROM: %0d of %0d bytes used%s", rom_at - ROM_BASE, ROM_SIZE, (rom_at - ROM_BASE > ROM_SIZE) ? "  <- DOES NOT FIT" : "");
    $display("RAM: %0d bytes of sections, %0d bytes of stack, %0d of %0d used", ram_at - RAM_BASE, STACK, ram_at - RAM_BASE + STACK, RAM_SIZE);
    if (align4(bss_end) > stack_bottom) $display("the stack and the sections overlap by %0d bytes", align4(bss_end) - stack_bottom);
    else $display("free between the sections and the stack: %0d bytes", stack_bottom - align4(bss_end));
    $display("start-up copies %0d words and clears %0d words", (copy_bytes + 3) / 4, (BSS + 3) / 4);
    $finish;                               // $finish ends the simulation
  end
endmodule
ConceptWhich section has a load address that differs from its run address, with execute in place?
ConceptROM begins at 0. The section .text has 0x80 bytes and .rodata has 0x10. Where is the load address of .data?
ConceptWhat does copy to RAM cost, compared with execute in place?
ConceptThe sections end at 0x8000_0208. The stack top is 0x8000_0400 and the stack may use 0x200 bytes. What is true?

Common Mistakes

Wrong Give .data only a run address.

Its first values are then stored nowhere that survives power-off.

Fix Store .data in ROM and run it in RAM.

Wrong Type section addresses into start-up code as numbers.

They move whenever a section changes size.

Fix Use the symbols that the linker script defines.

Wrong Trust "the sections fit in RAM" as proof that the stack fits.

The stack is not a section.

Fix Give the stack a size and check the gap below it.

Remember

  • A linker script lists the memories of the chip and the place of each section.
  • The linker keeps a location counter for each memory and moves it by the size of each section.
  • .data is stored in ROM and runs in RAM; .bss has room in RAM and no stored copy.
  • Start-up code gets its addresses from linker symbols. Execute in place saves RAM and start time; copy to RAM buys fetch speed.
  • The stack is not a section: give it a size and check the gap.

Practice

Report a problem with this page