10.5.4 MEMORY Command

The linker’s default configuration permits allocation of all available memory. This can be overridden by using the MEMORY command.

The MEMORY command describes the location and size of blocks of memory in the target. It can be used to describe which memory regions may be used by the linker and which memory regions it must avoid. Sections may then be assigned to particular memory regions. The linker will set section addresses based on the memory regions and will warn about regions that become too full. The linker will not shuffle sections around to fit into the available regions.

The syntax of the MEMORY command is:

MEMORY
{
  name [(attr)] : ORIGIN = origin, LENGTH = len
  ...
}

The name used in the linker script refers to the region. The region name has no meaning outside of the linker script. Region names are stored in a separate name space, and will not conflict with symbol names, file names or section names. Each memory region must have a distinct name.

The attr string is an optional list of attributes associated with the memory region. Historically, it was used to determine where unmapped sections should be located by the sequential memory allocator. This capability is no longer used because unmapped sections are now located by the best-fit allocator (for more information see Linker Allocation).

The origin is an expression for the start address of the memory region. The expression must evaluate to a constant before memory allocation is performed, which means that section relative symbols may not be used. The keyword ORIGIN may be abbreviated to org or o (but not, for example, ORG).

The len is an expression for the size in bytes of the memory region. As with the origin expression, the expression must evaluate to a constant before memory allocation is performed. The keyword LENGTH may be abbreviated to len or l.

Note: It is possible to use a preprocessor macro instead of a literal value for the origin and/or length of a memory region.

Once a memory region is defined, the linker can be directed to place specific output sections into that memory region by using the >region output section attribute. For example, to specify a memory region named mem, use >mem in the output section definition. If no address was specified for the output section, the linker will set the address to the next available address within the memory region. If the combined output sections directed to a memory region are too large for the region, the linker will issue an error message.