MEMORY

The MEMORY directive allows you to describe the location and size of memory segment blocks in the target. This directive specifies the linker the memory areas to avoid, and the memory areas into which it links the code and date.

The linker command file may only contain one MEMORY directive. However, within the confines of the MEMORY directive, you may define as many memory segments as you wish.

Prototype
MEMORY { memory_spec }

The memory_spec is:

segmentName (accessFlags) : ORIGIN = address, LENGTH = length, [COMPRESS] [> fileName]

segmentName can include alphanumeric characters and underscore '_' characters.

accessFlags are passed into the output ELF file (Phdr.p_flags). The accessFlags can be:

ORIGIN address is one of the following:

Table 1. Origin Address
A memory address Specify a hex address, such as 0x8000.
An AFTER command Use the AFTER(name [,name]) command to tell the linker to place the memory segment after the specified segment. In the example below, overlay1 and overlay2 are placed after the code segment. When multiple memory segments are specified as parameters for AFTER, the highest memory address is used.
Example
memory{


code     (RWX) : ORIGIN = 0x8000, LENGTH = 0


overlay1 (RWX) : ORIGIN = AFTER(code), LENGTH = 0


overlay2 (RWX) : ORIGIN = AFTER(code), LENGTH = 0


data     (RW) : ORIGIN = 0x1000, LENGTH = 0


}

ORIGIN is the assigned address.

LENGTH is one of the following:

Table 2. Length
A value greater than zero If you try to put more code and data into a memory segment than your specified length allows, the linker stops with an error.
Autolength by specifying zero When the length is 0, the linker lets you put as much code and data into a memory segment as you want.
Note: There is no overflow checking with autolength. The linker can produce an unexpected result if you use the autolength feature without leaving enough free memory space to contain the memory segment. For this reason, when you use autolength, use the AFTER keyword to specify origin addresses.

> fileName is an option to write the segment to a binary file on disk instead of an ELF program header. The binary file is put in the same folder as the ELF output file. This option has two variants:

Table 3. Option Choices
>fileName Writes the segment to a new file.
>>fileName Appends the segment to an existing file.