Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- The GNU linker. - <b>arithmetic functions</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Arithmetic Functions
--------------------

   The command language includes a number of built-in functions for use
in link script expressions.
`ABSOLUTE(EXP)'
     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.

`ADDR(SECTION)'
     Return the absolute address of the named SECTION.  Your script must
     previously have defined the location of that section. In the
     following example, `symbol_1' and `symbol_2' are assigned identical
     values:
          SECTIONS{ ...
            .output1 :
              {
              start_of_output_1 = ABSOLUTE(.);
              ...
              }
            .output :
              {
              symbol_1 = ADDR(.output1);
              symbol_2 = start_of_output_1;
              }
          ... }

`ALIGN(EXP)'
     Return the result of the current location counter (`.') aligned to
     the next EXP boundary.  EXP must be an expression whose value is a
     power of two.  This is equivalent to
          (. + EXP - 1) & ~(EXP - 1)

     `ALIGN' doesn't change the value of the location counter--it just
     does arithmetic on it.  As an example, to align the output `.data'
     section to the next `0x2000' byte boundary after the preceding
     section and to set a variable within the section to the next
     `0x8000' boundary after the input sections:
          SECTIONS{ ...
            .data ALIGN(0x2000): {
              *(.data)
              variable = ALIGN(0x8000);
            }
          ... }

     The first use of `ALIGN' in this example specifies the location of
     a section because it is used as the optional START attribute of a
     section definition (Section Options:.).  The second use   
     simply defines the value of a variable.

     The built-in `NEXT' is closely related to `ALIGN'.

`DEFINED(SYMBOL)'
     Return 1 if SYMBOL is in the linker global symbol table and is
     defined, otherwise return 0.  You can use this function to provide
     default values for symbols.  For example, the following
     command-file fragment shows how to set a global symbol `begin' to
     the first location in the `.text' section--but if a symbol called
     `begin' already existed, its value is preserved:

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

`NEXT(EXP)'
     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.

`SIZEOF(SECTION)'
     Return the size in bytes of the named SECTION, if that section has
     been allocated.  In the following example, `symbol_1' and
     `symbol_2' are assigned identical values:
          SECTIONS{ ...
            .output {
              .start = . ;
              ...
              .end = . ;
              }
            symbol_1 = .end - .start ;
            symbol_2 = SIZEOF(.output);
          ... }

`SIZEOF_HEADERS'
`sizeof_headers'
     Return the size in bytes of the output file's headers.  You can
     use this number as the start address of the first section, if you
     choose, to facilitate paging.


See Also: Section Options

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson