Go to the first, previous, next, last section, table of contents.


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

filename
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 which 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, separate the section names with whitespace.
* (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 ld 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:
SECTIONS {
  .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: ld 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 wildcard pattern. The linker handles wildcards 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 wildcard, the wildcard 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 wildcard characters will match a `/' character.

Wildcards only match files which are explicitly specified on the command line. The linker does not search directories to expand wildcards. However, if you specify a simple file name--a name with no wildcard 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:

SECTIONS { 
  .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.

SECTIONS {
  outputa 0x10000 :
    {
    all.o
    foo.o (.input1)
    }
  outputb :
    {
    foo.o (.input2)
    foo1.o (.input1)
    }
  outputc :
    {
    *(.input1)
    *(.input2)
    }
}

This example shows how wildcard 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.

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


Go to the first, previous, next, last section, table of contents.