Next: , Previous: Evaluation, Up: Expressions


3.10.8 The Section of an Expression

Addresses and symbols may be section relative, or absolute. A section relative symbol is relocatable. If you request relocatable output using the `-r' option, a further link operation may change the value of a section relative symbol. On the other hand, an absolute symbol will retain the same value throughout any further link operations.

Some terms in linker expressions are addresses. This is true of section relative symbols and for builtin functions that return an address, such as ADDR, LOADADDR, ORIGIN and SEGMENT_START. Other terms are simply numbers, or are builtin functions that return a non-address value, such as LENGTH.

When the linker evaluates an expression, the result depends on where the expression is located in a linker script. Expressions appearing outside an output section definitions are evaluated with all terms first being converted to absolute addresses before applying operators, and evaluate to an absolute address result. Expressions appearing inside an output section definition are evaluated with more complex rules, but the aim is to treat terms as relative addresses and produce a relative address result. In particular, an assignment of a number to a symbol results in a symbol relative to the output section with an offset given by the number. So, in the following simple example,

     SECTIONS
       {
         . = 0x100;
         __executable_start = 0x100;
         .data :
         {
           . = 0x10;
           __data_start = 0x10;
           *(.data)
         }
         ...
       }

both . and __executable_start are set to the absolute address 0x100 in the first two assignments, then both . and __data_start are set to 0x10 relative to the .data section in the second two assignments.

For expressions appearing inside an output section definition involving numbers, relative addresses and absolute addresses, ld follows these rules to evaluate terms:

The result section of each sub-expression is as follows:

You can use the builtin function ABSOLUTE to force an expression to be absolute when it would otherwise be relative. For example, to create an absolute symbol set to the address of the end of the output section `.data':

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

If `ABSOLUTE' were not used, `_edata' would be relative to the `.data' section.

Using LOADADDR also forces an expression absolute, since this particular builtin function returns an absolute address.