ld: Expression Section
1
1 3.10.8 The Section of an Expression
1 -----------------------------------
1
1 Addresses and symbols may be section relative, or absolute. A section
1 relative symbol is relocatable. If you request relocatable output using
1 the '-r' option, a further link operation may change the value of a
1 section relative symbol. On the other hand, an absolute symbol will
1 retain the same value throughout any further link operations.
1
1 Some terms in linker expressions are addresses. This is true of
1 section relative symbols and for builtin functions that return an
1 address, such as 'ADDR', 'LOADADDR', 'ORIGIN' and 'SEGMENT_START'.
1 Other terms are simply numbers, or are builtin functions that return a
1 non-address value, such as 'LENGTH'. One complication is that unless
1 you set 'LD_FEATURE ("SANE_EXPR")' (⇒Miscellaneous Commands),
1 numbers and absolute symbols are treated differently depending on their
1 location, for compatibility with older versions of 'ld'. Expressions
1 appearing outside an output section definition treat all numbers as
1 absolute addresses. Expressions appearing inside an output section
1 definition treat absolute symbols as numbers. If 'LD_FEATURE
1 ("SANE_EXPR")' is given, then absolute symbols and numbers are simply
1 treated as numbers everywhere.
1
1 In the following simple example,
1
1 SECTIONS
1 {
1 . = 0x100;
1 __executable_start = 0x100;
1 .data :
1 {
1 . = 0x10;
1 __data_start = 0x10;
1 *(.data)
1 }
1 ...
1 }
1
1 both '.' and '__executable_start' are set to the absolute address
1 0x100 in the first two assignments, then both '.' and '__data_start' are
1 set to 0x10 relative to the '.data' section in the second two
1 assignments.
1
1 For expressions involving numbers, relative addresses and absolute
1 addresses, ld follows these rules to evaluate terms:
1
1 * Unary operations on an absolute address or number, and binary
1 operations on two absolute addresses or two numbers, or between one
1 absolute address and a number, apply the operator to the value(s).
1 * Unary operations on a relative address, and binary operations on
1 two relative addresses in the same section or between one relative
1 address and a number, apply the operator to the offset part of the
1 address(es).
1 * Other binary operations, that is, between two relative addresses
1 not in the same section, or between a relative address and an
1 absolute address, first convert any non-absolute term to an
1 absolute address before applying the operator.
1
1 The result section of each sub-expression is as follows:
1
1 * An operation involving only numbers results in a number.
1 * The result of comparisons, '&&' and '||' is also a number.
1 * The result of other binary arithmetic and logical operations on two
1 relative addresses in the same section or two absolute addresses
1 (after above conversions) is also a number when 'LD_FEATURE
1 ("SANE_EXPR")' or inside an output section definition but an
1 absolute address otherwise.
1 * The result of other operations on relative addresses or one
1 relative address and a number, is a relative address in the same
1 section as the relative operand(s).
1 * The result of other operations on absolute addresses (after above
1 conversions) is an absolute address.
1
1 You can use the builtin function 'ABSOLUTE' to force an expression to
1 be absolute when it would otherwise be relative. For example, to create
1 an absolute symbol set to the address of the end of the output section
1 '.data':
1 SECTIONS
1 {
1 .data : { *(.data) _edata = ABSOLUTE(.); }
1 }
1 If 'ABSOLUTE' were not used, '_edata' would be relative to the '.data'
1 section.
1
1 Using 'LOADADDR' also forces an expression absolute, since this
1 particular builtin function returns an absolute address.
1