Section 12.5

Linker script

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.

Three more elements complete the language.

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 256−64=192256 - 64 = 192 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 256+1024+4096+2048=7424256 + 1024 + 4096 + 2048 = 7424 bytes out of 8×1024=81928 \times 1024 = 8192, which leaves 8192−7424=7688192 - 7424 = 768 bytes. Flash use is 40000+256=4025640000 + 256 = 40256 bytes out of 192×1024=196608192 \times 1024 = 196608. 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 256+2048+4096+2048=8448256 + 2048 + 4096 + 2048 = 8448 bytes, which is 8448−8192=2568448 - 8192 = 256 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 3000−2048=9523000 - 2048 = 952 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:

  1. sections assigned to a region without explicit addresses were placed at increasing addresses inside that region;

  2. the sections assigned to each region fit within the LENGTH of that region;

  3. no two sections that occupy memory have overlapping address ranges, a check that the linker makes unless it is switched off;

  4. every ASSERT condition in the script was true.

The linker does not check:

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.

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].