Contents|Index|Previous|Next

Assignment: Defining Symbols

You may create global symbols, and assign values (addresses) to global symbols, using any of the following example’s C assignment operators:

symbol= expression;
symbol&= expression;
symbol+= expression;
symbol-= expression;
symbol*= expression;
symbol/= expression;

Two things distinguish assignment from other operators in ld expressions.

Assignment may only be used at the root of an expression; ‘a=b+3;’ is allowed, but ‘a+b=3;’ is an error.

You must place a trailing semicolon (;) at the end of an assignment statement.

Assignment statements may appear:

as commands in their own right in an ld script; or

as independent statements within a SECTIONS command; or

as part of the contents of a section definition in a SECTIONS command.

The first two cases are equivalent in effect—both define a symbol with an absolute address. The last case defines a symbol whose address is relative to a particular section (see Specifying Output Sections).

When a linker expression is evaluated and assigned to a variable, it is given either an absolute or a relocatable type.

An absolute expression type is one in which the symbol contains the value that it will have in the output file; a relocatable expression type is one in which the value is expressed as a fixed offset from the base of a section.

The type of the expression is controlled by its position in the script file.

A symbol assigned within a section definition is created relative to the base of the section; a symbol assigned in any other place is created as an absolute symbol.

Since a symbol created within a section definition is relative to the base of the section, it will remain relocatable if relocatable output is requested.

A symbol may be created with an absolute value even when assigned to within a section definition by using the absolute assignment function ABSOLUTE.

For example, to create an absolute symbol whose address is the last byte of an output section named .data, use the following example’s input.

SECTIONS{ ...
.data :
{
*(.data)
_edata = ABSOLUTE(.) ;
}
... }

The linker tries to put off the evaluation of an assignment until all the terms in the source expression are known (see Evaluation). For instance, the sizes of sections cannot be known until after allocation, so assignments dependent upon these are not performed until after allocation. Some expressions, such as those depending upon the location counter dot,‘.’ must be evaluated during allocation. If the result of an expression is required, but the value is not available, then an error results. For example, a script like the following will cause the error message:
Non constant expression for initial address

SECTIONS { ...
text 9+this_isnt_constant :
{ ...
}
... }

In some cases, it is desirable for a linker script to define a symbol only if it is referenced, and only if it is not defined by any object included in the link. For example, traditional linkers defined the symbol ‘etext’. However, ANSI C requires that the user be able to use ‘etext’ as a function name without encountering an error. The PROVIDE keyword may be used to define a symbol, such as ‘etext’, only if it is referenced but not defined. The syntax is: PROVIDE(symbol=expression).