33.4. Specifying Output Sections

The SECTIONS command controls exactly where input sections are placed into output sections, their order in the output file, and to which output sections they are allocated.

You may use at most one SECTIONS command in a script file, but you can have as many statements within it as you wish. Statements within the SECTIONS command can do one of three things:

You can also use the first two operations — defining the entry point and defining symbols — outside the SECTIONS command: see Section 33.5 and Section 33.2.6. They are permitted here as well for your convenience in reading the script, so that symbols and the entry point can be defined at meaningful points in your output-file layout.

If you do not use a SECTIONS command, the linker places each input section into an identically named output section in the order that the sections are first encountered in the input files. If all input sections are present in the first file, for example, the order of sections in the output file will match the order in the first input file.

33.4.1. Section Definitions

The most frequently used statement in the SECTIONS command is the section definition, which specifies the properties of an output section: its location, alignment, contents, fill pattern, and target memory region. Most of these specifications are optional; the simplest form of a section definition is

  secname : {
... }

secname is the name of the output section, and contents a specification of what goes there — for example, a list of input files or sections of input files (see Section 33.4.2.). As you might assume, the white-space shown is optional. You do need the colon “:” and the braces “{}”, however.

secname must meet the constraints of your output format. In formats that only support a limited number of sections, such as a.out, the name must be one of the names supported by the format (a.out, for example, allows only .text, .data or .bss). If the output format supports any number of sections, but with numbers and not names (as is the case for Oasys), the name should be supplied as a quoted numeric string. A section name may consist of any sequence of characters, but any name that does not conform to the standard linker symbol name syntax must be quoted. See Section 27.3, Symbols.

The special secname/DISCARD/” may be used to discard input sections. Any sections that are assigned to an output section named “/DISCARD/” are not included in the final link output.

The linker will not create output sections that do not have any contents. This is for convenience when referring to input sections that may or may not exist. For example,

.foo { *(.foo) }

will only create a “.foo” section in the output file if there is a “.foo” section in at least one input file.

33.4.2. Section Placement

In a section definition, you can specify the contents of an output section by listing particular input files, by listing particular input-file sections, or by a combination of the two. You can also place arbitrary data in the section, and define symbols relative to the beginning of the section.

The contents of a section definition may include any of the following kinds of statement. You can include as many of these as you like in a single section definition, separated from one another by white-space.


You may simply name a particular input file to be placed in the current output section; all sections from that file are placed in the current section definition. If the file name has already been mentioned in another section definition, with an explicit section name list, then only those sections that have not yet been allocated are used.

To specify a list of particular files by name:

.data : { afile.o bfile.o cfile.o }

The example also illustrates that multiple statements can be included in the contents of a section definition, since each file name is a separate statement.

filename( section ), filename( section , section, ... ), filename( section section ... )

You can name one or more sections from your input files, for insertion in the current output section. If you wish to specify a list of input-file sections inside the parentheses, you may separate the section names by either commas or white-space.

* (section), * (section, section, ...), * (section section ...)

Instead of explicitly naming particular input files in a link control script, you can refer to all files from the linker command line: use “*” instead of a particular file name before the parenthesized input-file section list.

If you have already explicitly included some files by name, “*” refers to all remaining files — those whose places in the output file have not yet been defined.

For example, to copy sections 1 through 4 from an Oasys file into the .text section of an a.out file, and sections 13 and 14 into the .data section:

  .text :{
    *("1" "2" "3" "4")

  .data :{
    *("13" "14")

[ section ... ]” used to be accepted as an alternate way to specify named sections from all unallocated input files. Because some operating systems (VMS) allow brackets in file names, that notation is no longer supported.

filename( COMMON ), *( COMMON )

Specify where in your output file to place uninitialized data with this notation. *(COMMON) by itself refers to all uninitialized data from all input files (so far as it is not yet allocated); filename(COMMON) refers to uninitialized data from a particular file. Both are special cases of the general mechanisms for specifying where to place input-file sections: the linker permits you to refer to uninitialized data as if it were in an input-file section named COMMON, regardless of the input file's format.

In any place where you may use a specific file or section name, you may also use a wild-card pattern. The linker handles wild-cards much as the UNIX shell does. A “*” character matches any number of characters. A “?” character matches any single character. The sequence “[chars]” will match a single instance of any of the chars; the “-” character may be used to specify a range of characters, as in “[a-z]” to match any lower case letter. A “\” character may be used to quote the following character.

When a file name is matched with a wild-card, the wild-card characters will not match a “/” character (used to separate directory names on UNIX). A pattern consisting of a single “*” character is an exception; it will always match any file name. In a section name, the wild-card characters will match a “/” character.

Wild-cards only match files that are explicitly specified on the command line. The linker does not search directories to expand wild-cards. However, if you specify a simple file name — a name with no wild-card characters — in a linker script, and the file name is not also specified on the command line, the linker will attempt to open the file as though it appeared on the command line.

In the following example, the command script arranges the output file into three consecutive sections, named .text, .data, and .bss, taking the input for each from the correspondingly named sections of all the input files:

  .text : { *(.text) }
  .data : { *(.data) }
  .bss :  { *(.bss)  *(COMMON) }

The following example reads all of the sections from file all.o and places them at the start of output section outputa, which starts at location 0x10000. All of section .input1 from file foo.o follows immediately, in the same output section. All of section .input2 from foo.o goes into output section outputb, followed by section .input1 from foo1.o. All of the remaining .input1 and .input2 sections from any files are written to output section outputc.

  outputa 0x10000 :
    foo.o (.input1)
  outputb :
    foo.o (.input2)
    foo1.o (.input1)
  outputc :

This example shows how wild-card patterns might be used to partition files. All .text sections are placed in .text, and all .bss sections are placed in .bss. For all files beginning with an upper case character, the .data section is placed into .DATA; for all other files, the .data section is placed into .data.

  .text : { *(.text) }
  .DATA : { [A-Z]*(.data) }
  .data : { *(.data) }
  .bss : { *(.bss) }

33.4.3. Section Data Expressions

The foregoing statements arrange, in your output file, data originating from your input files. You can also place data directly in an output section from the link command script. Most of these additional statements involve expressions (see Expressions, Section 33.2.). Although these statements are shown separately here for ease of presentation, no such segregation is needed within a section definition in the SECTIONS command; you can intermix them freely with any of the statements we've just described.


Create a symbol for each input file in the current section, set to the address of the first byte of data written from that input file. For instance, with a.out files it is conventional to have a symbol for each input file. You can accomplish this by defining the output .text section as follows:

  .text 0x2020 :
    _etext = ALIGN(0x2000);

If sample.ld is a file containing this script, and a.o, b.o, c.o, and d.o are four input files with contents like the following--

/* a.c */

afunction() { }
int adata=1;
int abss;

ld -M -T sample.ld a.o b.o c.o d.o” would create a map like this, containing symbols matching the object file names:

00000000 A __DYNAMIC
00004020 B _abss
00004000 D _adata
00002020 T _afunction
00004024 B _bbss
00004008 D _bdata
00002038 T _bfunction
00004028 B _cbss
00004010 D _cdata
00002050 T _cfunction
0000402c B _dbss
00004018 D _ddata
00002068 T _dfunction
00004020 D _edata
00004030 B _end
00004000 T _etext
00002020 t a.o
00002038 t b.o
00002050 t c.o
00002068 t d.o
symbol = expression ;, symbol f= expression ;

symbol is any symbol name (see Section 33.2.2.). "f=" refers to any of the operators &= += -= *= /= which combine arithmetic and assignment.

When you assign a value to a symbol within a particular section definition, the value is relative to the beginning of the section (see Section 33.2.6.). If you write

  abs = 14 ;
  .data : { ... rel = 14 ; ... }
  abs2 = 14 + ADDR(.data);

abs and rel do not have the same value; rel has the same value as abs2.

BYTE(expression), SHORT(expression), LONG(expression), QUAD(expression)

By including one of these four statements in a section definition, you can explicitly place one, two, four, or eight bytes (respectively) at the current address of that section. QUAD is only supported when using a 64-bit host or target.

Multiple-byte quantities are represented in whatever byte order is appropriate for the output file format (see Appendix A.).


Specify the “fill pattern” for the current section. Any otherwise unspecified regions of memory within the section (for example, regions you skip over by assigning a new value to the location counter “.”) are filled with the two least significant bytes from the expression argument. A FILL statement covers memory locations after the point it occurs in the section definition; by including more than one FILL statement, you can have different fill patterns in different parts of an output section.

33.4.4. Optional Section Attributes

Here is the full syntax of a section definition, including all the optional portions:

secname start BLOCK(align) (NOLOAD) : AT ( ldadr )
  { contents } >region :phdr =fill

secname and contents are required. See Section 33.4.1, and Section 33.4.2, for details on contents. The remaining elements — start, BLOCK(align), (NOLOAD), AT ( ldadr ), >region, :phdr, and =fill — are all optional.


You can force the output section to be loaded at a specified address by specifying start immediately following the section name. start can be represented as any expression. The following example generates section output at location 0x40000000:

  output 0x40000000: {

You can include BLOCK() specification to advance the location counter . prior to the beginning of the section, so that the section will begin at the specified alignment. align is an expression.


Use “(NOLOAD)” to prevent a section from being loaded into memory each time it is accessed. For example, in the script sample below, the ROM segment is addressed at memory location “0” and does not need to be loaded into each object file:

  ROM  0  (NOLOAD)  : { ... }
AT ( ldadr )

The expression ldadr that follows the AT keyword specifies the load address of the section. The default (if you do not use the AT keyword) is to make the load address the same as the relocation address. This feature is designed to make it easy to build a ROM image. For example, this SECTIONS definition creates two output sections: one called “.text”, which starts at 0x1000, and one called “.mdata”, which is loaded at the end of the “.text” section even though its relocation address is 0x2000. The symbol _data is defined with the value 0x2000:

  .text 0x1000 : { *(.text) _etext = . ; }
  .mdata 0x2000 :
    AT ( ADDR(.text) + SIZEOF ( .text ) )
    { _data = . ; *(.data); _edata = . ;  }
  .bss 0x3000 :
    { _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;}

The run-time initialization code (for C programs, usually crt0) for use with a ROM generated this way has to include something like the following, to copy the initialized data from the ROM image to its runtime address:

char *src = _etext;
char *dst = _data;

/* ROM has data at end of text; copy it. */
while (dst < _edata) {
  *dst++ = *src++;

/* Zero bss */
for (dst = _bstart; dst< _bend; dst++)
  *dst = 0;

Assign this section to a previously defined region of memory. See Section 33.3.


Including =fill in a section definition specifies the initial fill value for that section. You may use any expression to specify fill. Any unallocated holes in the current output section when written to the output file will be filled with the two least significant bytes of the value, repeated as necessary. You can also change the fill value with a FILL statement in the contents of a section definition.

33.4.5. Overlays

The OVERLAY command provides an easy way to describe sections that are to be loaded as part of a single memory image but are to be run at the same memory address. At run time, some sort of overlay manager will copy the overlaid sections in and out of the runtime memory address as required, perhaps by simply manipulating addressing bits. This approach can be useful, for example, when a certain region of memory is faster than another.

The OVERLAY command is used within a SECTIONS command. It appears as follows:

  OVERLAY start : [ NOCROSSREFS ] AT ( ldaddr )
     secname1 { contents } :phdr =fill
     secname2 { contents } :phdr =fill
   } >region :phdr =fill

Everything is optional except OVERLAY (a keyword), and each section must have a name (secname1 and secname2 above). The section definitions within the OVERLAY construct are identical to those within the general SECTIONS construct (see Section 33.4.), except that no addresses and no memory regions may be defined for sections within an OVERLAY.

The sections are all defined with the same starting address. The load addresses of the sections are arranged such that they are consecutive in memory starting at the load address used for the OVERLAY as a whole (as with normal section definitions, the load address is optional, and defaults to the start address; the start address is also optional, and defaults to .).

If the NOCROSSREFS keyword is used, and there any references among the sections, the linker will report an error. Since the sections all run at the same address, it normally does not make sense for one section to refer directly to another. See Section 33.7.

For each section within the OVERLAY, the linker automatically defines two symbols. The symbol __load_start_secname is defined as the starting load address of the section. The symbol __load_stop_secname is defined as the final load address of the section. Any characters within secname that are not legal within C identifiers are removed. C (or assembler) code may use these symbols to move the overlaid sections around as necessary.

At the end of the overlay, the value of . is set to the start address of the overlay plus the size of the largest section.

Here is an example. Remember that this would appear inside a SECTIONS construct.

  OVERLAY 0x1000 : AT (0x4000)
     .text0 { o1/*.o(.text) }
     .text1 { o2/*.o(.text) }

This will define both .text0 and .text1 to start at address 0x1000. .text0 will be loaded at address 0x4000, and .text1 will be loaded immediately after .text0. The following symbols will be defined: __load_start_text0, __load_stop_text0, __load_start_text1, __load_stop_text1.

C code to copy overlay .text1 into the overlay area might look like the following.

  extern char __load_start_text1, __load_stop_text1;
  memcpy ((char *) 0x1000, &__load_start_text1,
          &__load_stop_text1 - &__load_start_text1);

Note that the OVERLAY command is just syntactic sugar, since everything it does can be done using the more basic commands. The above example could have been written identically as follows.

  .text0 0x1000 : AT (0x4000) { o1/*.o(.text) }
  __load_start_text0 = LOADADDR (.text0);
  __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
  .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) { o2/*.o(.text) }
  __load_start_text1 = LOADADDR (.text1);
  __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
  . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));