Definition
A linker script is a text file that tells a linker §12.4, the tool that joins the pieces of machine code that a compiler produces into one program, where in memory to place each part. The script names the machine’s memory regions with their start addresses and sizes. It then says which sections, the blocks of code or data in those pieces, go into which region and in what order. It can also name addresses, such as the first address after the program’s data, for program code to use.
The idea
A desktop operating system decides where to place a program in memory when the program starts. A microcontroller §6.1, a small computer on one chip, usually has no such operating system, and its memory is fixed in the hardware. Flash memory §6.6, which keeps its content without power, sits at one range of addresses, and RAM at another. Code belongs at flash addresses and variables at RAM addresses. The stack (the memory that function calls use for local data) and the heap (the memory handed out on request while the program runs) each need a block of RAM of their own, and part of the flash may be reserved for stored data.
The linker script is where this map is written down. The picture is a floor plan: the regions are the rooms, and the sections are the furniture. The linker refuses a plan in which the furniture does not fit. The picture also shows the limit: a plan cannot stop anyone from breaking through a wall later. While the program runs, a stack can grow beyond the block that the script gave it, and the script is no longer involved.
How it works
The description follows the script language of the GNU linker ld. GNU, a name whose letters stand for GNU’s Not Unix, is the free-software project that publishes it. A script has two main commands.
MEMORYdeclares the regions. Each region has a name, a start address (ORIGIN) and a size in bytes (LENGTH). In sizes, the suffixKmultiplies by 1024.SECTIONSdescribes the output. Each entry names an output section (a section of the finished program), lists the input sections (sections of the compiled files) collected into it, and ends with>and the name of the region that receives it. The pattern*(.text*)means: from every input file, every section whose name starts with.text.
Three more elements complete the language.
The location counter, written as a single dot. It holds the address where the next item will be placed. The linker advances it by the size of everything it places, so consecutive items in a region follow one another without overlap. An assignment such as
. += 4Kmoves it forward and so reserves an empty block.Two addresses for one section. The manual of
ldcalls them the VMA (virtual memory address), the address that the section has while the program runs, and the LMA (load memory address), the address where its bytes are stored. They differ for variables with initial values on a microcontroller: the values are stored in flash memory, and startup code copies them into RAM before the functionmainruns. The script writes this as> RAM AT> FLASH.Symbols. An assignment such as
_stack_top = .;gives the current address a name. Program code can refer to that name, for example to tell the processor where the stack starts.
The command ASSERT(condition, message) adds a check written by the author of the script: if the condition is false, the link stops and prints the message.
Worked example
An Arm microcontroller has 256 KiB of flash memory starting at address 0x00000000 and 8 KiB of RAM starting at 0x20000000. The prefix 0x marks a number written in hexadecimal, base 16. The last 64 KiB of the flash are set aside for a storage partition, a fixed range reserved for saved data, so the program may use only the first KiB. The script is:
MEMORY
{
FLASH : ORIGIN = 0x00000000, LENGTH = 192K
RAM : ORIGIN = 0x20000000, LENGTH = 8K
}
SECTIONS
{
.text : { *(.text*) *(.rodata*) } > FLASH
.data : { *(.data*) } > RAM AT> FLASH
.bss : { *(.bss*) } > RAM
.heap : { _heap_start = .; . += 4K; _heap_end = .; } > RAM
.stack : { _stack_limit = .; . += 2K; _stack_top = .; } > RAM
}
By convention .text holds code, .rodata constants, .data variables with initial values, and .bss variables that start as zero. The compiled files contain 40,000 bytes of code and constants, 256 bytes of .data and 1,024 bytes of .bss. The linker places them in order:
| section | bytes | first address | address after the end |
|---|---|---|---|
.text |
40,000 | 0x00000000 |
0x00009C40 |
.data, stored copy |
256 | 0x00009C40 |
0x00009D40 |
.data, in use |
256 | 0x20000000 |
0x20000100 |
.bss |
1,024 | 0x20000100 |
0x20000500 |
.heap |
4,096 | 0x20000500 |
0x20001500 |
.stack |
2,048 | 0x20001500 |
0x20001D00 |
The addresses are hexadecimal: 40,000 is 0x9C40, 256 is 0x100, 1,024 is 0x400, 4,096 is 0x1000 and 2,048 is 0x800.
RAM use is bytes out of , which leaves bytes. Flash use is bytes out of . Every block starts where the previous one ends, so nothing overlaps. The program also stays clear of the storage partition, which begins at address 196,608, written 0x00030000.
Now let the .bss data grow to 2,048 bytes. RAM use becomes bytes, which is bytes too many. The linker stops with the message region `RAM' overflowed by 256 bytes and produces no program.
The script does not catch the following case. In the first layout, the program calls a function that has a local array §14.5 of 3,000 bytes. On Arm processors the stack grows toward lower addresses, and in this program it starts at _stack_top. It passes _stack_limit after 2,048 bytes and extends at least bytes into the heap block, so writing to the array can overwrite heap data. The link reported nothing, because the linker never sees how deep the stack goes.
Stated precisely
When a link with ld succeeds, the following holds:
sections assigned to a region without explicit addresses were placed at increasing addresses inside that region;
the sections assigned to each region fit within the
LENGTHof that region;no two sections that occupy memory have overlapping address ranges, a check that the linker makes unless it is switched off;
every
ASSERTcondition in the script was true.
The linker does not check:
whether
ORIGINandLENGTHmatch the real chip;whether two declared regions overlap each other. Version 2.42 accepts overlapping regions without a message.
The conditions for run time are separate. For the stack: the deepest stack use on any path through the program, including the handling of interrupts §6.11 (hardware events that pause the running code and use stack memory themselves), is no larger than the reserved block. For the heap: the code that hands out heap memory refuses any request that would pass _heap_end. Both are properties of the running program, not of the script.
Limits and common mistakes
A linker script reserves memory. It does not guard memory. A stack overflow §4.10, in which the stack outgrows its block, is prevented or detected only by means outside the script. A memory protection unit §6.8 is hardware that can make a range of addresses inaccessible, so that an access to the range is refused and reported as an error, called a fault. A known pattern placed at the stack limit can be checked from time to time, which detects an overflow after it has happened. A proven upper bound on the stack depth shows in advance that no overflow can occur.
Wrong numbers give a wrong layout without any error. The boundary of a partition is often written twice: in the script, and in the settings of the software that uses the partition, such as a bootloader §6.30 (the small program that runs first and starts the main program) or a storage driver. If the script says LENGTH = 256K although the last 64 KiB belong to the partition, the link succeeds, and a program that is large enough is placed over the partition. Nothing keeps the two numbers equal unless both come from one definition or an ASSERT compares them.
Questions answered
What prevents code, stack, heap, or persistent partitions from overlapping?
Three different things prevent three different kinds of overlap: the linker for the pieces that it places when the program is built, agreement between the linker script and the storage software for persistent partitions, and run-time checks or a proven bound for the growth of the stack and the heap. The linker script, the text file that tells the linker where in memory to place each piece of a program, provides only the first.
Code, data and the blocks reserved for stack and heap, when the program is built: the linker. It places the sections (the blocks of code and data) of each memory region one after another, so they cannot overlap. It stops with an error when a region is too small or when two sections would share addresses.
Persistent partitions, meaning flash areas that keep stored data: agreement between the script and the software that uses the partition. The script keeps the program out of a partition only by ending its flash region where the partition begins. The linker does not know that the partition exists, so the addresses that the storage software uses must match the script. Only a shared definition or an explicit check during the build ensures that.
Growth of the stack and the heap while the program runs: nothing in the script. The stack is the memory that function calls use for local data, and the heap is the memory handed out on request. The script only reserves one block for each. Keeping the stack inside its block needs a guard that acts at run time, such as a memory protection unit (hardware that blocks access to chosen addresses) or a checked pattern at the stack limit, or a proven upper bound on the stack depth. The heap stays inside its block only if the code that hands out heap memory checks every request against the end of the block.
A successful link therefore shows that the planned layout has no overlaps. It does not show that the running program keeps to the plan.
Related entries: ELF (§12.6), Overflow-safe range check (§4.17), Flash memory (§6.6), Stack overflow (§4.10), Linker (§12.4), Memory protection unit (MPU) (§6.8), Static, stack, and heap storage (§4.11). Further reading: [82, 109, 174].