Previous: , Up: Expressions   [Contents][Index]

3.10.9 Builtin Functions

The linker script language includes a number of builtin functions for use in linker script expressions.


Return the absolute (non-relocatable, as opposed to non-negative) value of the expression exp. Primarily useful to assign an absolute value to a symbol within a section definition, where symbol values are normally section relative. See Expression Section.


Return the address (VMA) of the named section. Your script must previously have defined the location of that section. In the following example, start_of_output_1, symbol_1 and symbol_2 are assigned equivalent values, except that symbol_1 will be relative to the .output1 section while the other two will be absolute:

  .output1 :
    start_of_output_1 = ABSOLUTE(.);
  .output :
    symbol_1 = ADDR(.output1);
    symbol_2 = start_of_output_1;

Return the location counter (.) or arbitrary expression aligned to the next align boundary. The single operand ALIGN doesnt change the value of the location counterit just does arithmetic on it. The two operand ALIGN allows an arbitrary expression to be aligned upwards (ALIGN(align) is equivalent to ALIGN(ABSOLUTE(.), align)).

Here is an example which aligns the output .data section to the next 0x2000 byte boundary after the preceding section and sets a variable within the section to the next 0x8000 boundary after the input sections:

  .data ALIGN(0x2000): {
    variable = ALIGN(0x8000);

The first use of ALIGN in this example specifies the location of a section because it is used as the optional address attribute of a section definition (see Output Section Address). The second use of ALIGN is used to defines the value of a symbol.

The builtin function NEXT is closely related to ALIGN.


Return the alignment in bytes of the named section, if that section has been allocated. If the section has not been allocated when this is evaluated, the linker will report an error. In the following example, the alignment of the .output section is stored as the first value in that section.

  .output {
    LONG (ALIGNOF (.output))

This is a synonym for ALIGN, for compatibility with older linker scripts. It is most often seen when setting the address of an output section.

DATA_SEGMENT_ALIGN(maxpagesize, commonpagesize)

This is equivalent to either

(ALIGN(maxpagesize) + (. & (maxpagesize - 1)))


 + ((. + commonpagesize - 1) & (maxpagesize - commonpagesize)))

depending on whether the latter uses fewer commonpagesize sized pages for the data segment (area between the result of this expression and DATA_SEGMENT_END) than the former or not. If the latter form is used, it means commonpagesize bytes of runtime memory will be saved at the expense of up to commonpagesize wasted bytes in the on-disk file.

This expression can only be used directly in SECTIONS commands, not in any output section descriptions and only once in the linker script. commonpagesize should be less or equal to maxpagesize and should be the system page size the object wants to be optimized for while still running on system page sizes up to maxpagesize. Note however that -z relro protection will not be effective if the system page size is larger than commonpagesize.


  . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);

This defines the end of data segment for DATA_SEGMENT_ALIGN evaluation purposes.


This defines the end of the PT_GNU_RELRO segment when -z relro option is used. When -z relro option is not present, DATA_SEGMENT_RELRO_END does nothing, otherwise DATA_SEGMENT_ALIGN is padded so that exp + offset is aligned to the commonpagesize argument given to DATA_SEGMENT_ALIGN. If present in the linker script, it must be placed between DATA_SEGMENT_ALIGN and DATA_SEGMENT_END. Evaluates to the second argument plus any padding needed at the end of the PT_GNU_RELRO segment due to section alignment.


Return 1 if symbol is in the linker global symbol table and is defined before the statement using DEFINED in the script, otherwise return 0. You can use this function to provide default values for symbols. For example, the following script fragment shows how to set a global symbol begin to the first location in the .text sectionbut if a symbol called begin already existed, its value is preserved:

  .text : {
    begin = DEFINED(begin) ? begin : . ;

Return the length of the memory region named memory.


Return the absolute LMA of the named section. (see Output Section LMA).


Return the binary logarithm of exp rounded towards infinity. LOG2CEIL(0) returns 0.

MAX(exp1, exp2)

Returns the maximum of exp1 and exp2.

MIN(exp1, exp2)

Returns the minimum of exp1 and exp2.


Return the next unallocated address that is a multiple of exp. This function is closely related to ALIGN(exp); unless you use the MEMORY command to define discontinuous memory for the output file, the two functions are equivalent.


Return the origin of the memory region named memory.

SEGMENT_START(segment, default)

Return the base address of the named segment. If an explicit value has already been given for this segment (with a command-line -T option) then that value will be returned otherwise the value will be default. At present, the -T command-line option can only be used to set the base address for the text, data, and bss sections, but you can use SEGMENT_START with any segment name.


Return the size in bytes of the named section, if that section has been allocated. If the section has not been allocated when this is evaluated, the linker will report an error. In the following example, symbol_1 and symbol_2 are assigned identical values:

  .output {
    .start = . ;
    .end = . ;
  symbol_1 = .end - .start ;
  symbol_2 = SIZEOF(.output);

Return the size in bytes of the output files headers. This is information which appears at the start of the output file. You can use this number when setting the start address of the first section, if you choose, to facilitate paging.

When producing an ELF output file, if the linker script uses the SIZEOF_HEADERS builtin function, the linker must compute the number of program headers before it has determined all the section addresses and sizes. If the linker later discovers that it needs additional program headers, it will report an error not enough room for program headers. To avoid this error, you must avoid using the SIZEOF_HEADERS function, or you must rework your linker script to avoid forcing the linker to use additional program headers, or you must define the program headers yourself using the PHDRS command (see PHDRS).

Previous: , Up: Expressions   [Contents][Index]