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.
| Question | |
|---|---|
| 1 | What are the two parts of a linker script? |
| 2 | How does the linker work out the address of each section? |
| 3 | Which section has a load address that differs from its run address, and why? |
| 4 | Where do the addresses in start-up code come from? |
| 5 | What 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
| Word | Meaning |
|---|---|
| .text | The section that holds the instructions. |
| .rodata | The section of constants: data that is read and never written. |
| Location counter | The next free address of a memory while the linker places sections. |
| Linker symbol | A name that the script gives to an address, such as the end of .data. |
| Execute in place | The code runs from the memory it is stored in. XIP for short. |
| Copy to RAM | The code is copied to RAM at start, and runs there. |
One Picture: Sections and Memories
- Processor
- Peripheral
- Memory
- Code and constants
Nothing writes them, and they must be there at power-on. They are stored in ROM and used in ROM.
- .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.
- .bss needs only RAM
It has no first values to keep. The script only reserves its room.
- 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.
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);
}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
| Region | Base | Last Address | Size | Bus | Kind | Description |
|---|---|---|---|---|---|---|
| Boot ROM | 0x0000_0000 | 0x0000_0fff | 4 KiB | AHB | rom | Reset vector and boot code. |
| Unmapped | 0x0000_1000 | 0x01ff_ffff | 32764 KiB | — | hole | Reads and writes receive the default error response. |
| ap_clint | 0x0200_0000 | 0x0200_ffff | 64 KiB | AHB | device | Core-local timer and software interrupts. |
| Unmapped | 0x0201_0000 | 0x0bff_ffff | 163776 KiB | — | hole | Reads and writes receive the default error response. |
| ap_plic | 0x0c00_0000 | 0x0c3f_ffff | 4 MiB | AHB | device | Platform interrupt controller, 16 sources. |
| Unmapped | 0x0c40_0000 | 0x0fff_ffff | 60 MiB | — | hole | Reads and writes receive the default error response. |
| UART0 | 0x1000_0000 | 0x1000_0fff | 4 KiB | APB | device | Serial console. |
| GPIO | 0x1000_1000 | 0x1000_1fff | 4 KiB | APB | device | 16 general-purpose pins. |
| Timer and PWM | 0x1000_2000 | 0x1000_2fff | 4 KiB | APB | device | Timer and pulse-width modulation. |
| Watchdog | 0x1000_3000 | 0x1000_3fff | 4 KiB | APB | device | Watchdog reset control. |
| ap_sysctl | 0x1000_4000 | 0x1000_4fff | 4 KiB | APB | device | Chip ID, reset cause, clock control, pin mux and straps. |
| SPI | 0x1000_5000 | 0x1000_5fff | 4 KiB | APB | device | SPI controller. |
| DMA | 0x1000_6000 | 0x1000_6fff | 4 KiB | APB | device | DMA control registers; transfers use its AHB manager port. |
| Unmapped | 0x1000_7000 | 0x1000_7fff | 4 KiB | — | hole | Reads and writes receive the default error response. |
| Reserved APB expansion | 0x1000_8000 | 0x1000_ffff | 32 KiB | APB | reserved | Reserved for v2 peripherals; accesses return an error. |
| Unmapped | 0x1001_0000 | 0x7fff_ffff | 1834944 KiB | — | hole | Reads and writes receive the default error response. |
| SRAM | 0x8000_0000 | 0x8000_ffff | 64 KiB | AHB | ram | Program and data memory. |
| Reserved SRAM expansion | 0x8001_0000 | 0x8001_ffff | 64 KiB | AHB | reserved | Reserved for v2 SRAM growth; accesses return an error. |
| Unmapped | 0x8002_0000 | 0xffff_ffff | 2097024 KiB | — | hole | Reads 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.
| Section | Size | Run address | Load address | ROM counter after | RAM counter after |
|---|---|---|---|---|---|
| .text | 0x100 | 0x0000_0000 | the same | 0x100 | 0x8000_0000 |
| .rodata | 0x20 | 0x0000_0100 | the same | 0x120 | 0x8000_0000 |
| .data | 0x10 | 0x8000_0000 | 0x0000_0120 | 0x130 | 0x8000_0010 |
| .bss | 0x20 | 0x8000_0010 | none | 0x130 | 0x8000_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
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.
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.
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.
Rule 4: A section that is written must run in RAM.
So .data has two addresses, and .bss has one.
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.
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.
.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 wordsRule 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.
.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 | Copy to RAM | |
|---|---|---|
| .text runs at | 00000000, in ROM | 80000000, in RAM |
| RAM taken by sections | 88 bytes | 568 bytes |
| Words copied at start | 6 | 126 |
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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The chip is described in a script | The chip is built into the linker | One linker serves every chip. Each chip needs a script that agrees with its memory map. |
| Symbols carry the addresses to start-up code | Numbers typed into start-up code | The addresses follow every change of size. A wrong symbol name fails at link time, which is early. |
| Execute in place | Copy to RAM | Little 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:
RAM: 88 bytes of sections, 512 bytes of stack, 600 of 1024 used
free between the sections and the stack: 424 bytesThen the buffer grows to 496 bytes.
RAM: 520 bytes of sections, 512 bytes of stack, 1032 of 1024 used
the stack and the sections overlap by 8 bytesThe 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 section looks ahead. The lab places four sections in two memories. The script of a product is several pages long.
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.
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.
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.
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.
.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 wordsCheck Yourself
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.