home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / ld / ld.info-2 (.txt) < prev    next >
GNU Info File  |  1996-09-28  |  48KB  |  968 lines

  1. This is Info file ld.info, produced by Makeinfo-1.55 from the input
  2. file ./ld.texinfo.
  3. START-INFO-DIR-ENTRY
  4. * Ld: (ld).                       The GNU linker.
  5. END-INFO-DIR-ENTRY
  6.    This file documents the GNU linker LD.
  7.    Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation,
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided also
  13. that the entire resulting derived work is distributed under the terms
  14. of a permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions.
  18. File: ld.info,  Node: Arithmetic Functions,  Next: Semicolons,  Prev: Assignment,  Up: Expressions
  19. Arithmetic Functions
  20. --------------------
  21.    The command language includes a number of built-in functions for use
  22. in link script expressions.
  23. `ABSOLUTE(EXP)'
  24.      Return the absolute (non-relocatable, as opposed to non-negative)
  25.      value of the expression EXP.  Primarily useful to assign an
  26.      absolute value to a symbol within a section definition, where
  27.      symbol values are normally section-relative.
  28. `ADDR(SECTION)'
  29.      Return the absolute address of the named SECTION.  Your script must
  30.      previously have defined the location of that section. In the
  31.      following example, `symbol_1' and `symbol_2' are assigned identical
  32.      values:
  33.           SECTIONS{ ...
  34.             .output1 :
  35.               {
  36.               start_of_output_1 = ABSOLUTE(.);
  37.               ...
  38.               }
  39.             .output :
  40.               {
  41.               symbol_1 = ADDR(.output1);
  42.               symbol_2 = start_of_output_1;
  43.               }
  44.           ... }
  45. `ALIGN(EXP)'
  46.      Return the result of the current location counter (`.') aligned to
  47.      the next EXP boundary.  EXP must be an expression whose value is a
  48.      power of two.  This is equivalent to
  49.           (. + EXP - 1) & ~(EXP - 1)
  50.      `ALIGN' doesn't change the value of the location counter--it just
  51.      does arithmetic on it.  As an example, to align the output `.data'
  52.      section to the next `0x2000' byte boundary after the preceding
  53.      section and to set a variable within the section to the next
  54.      `0x8000' boundary after the input sections:
  55.           SECTIONS{ ...
  56.             .data ALIGN(0x2000): {
  57.               *(.data)
  58.               variable = ALIGN(0x8000);
  59.             }
  60.           ... }
  61.      The first use of `ALIGN' in this example specifies the location of
  62.      a section because it is used as the optional START attribute of a
  63.      section definition (*note Section Options::.).  The second use
  64.      simply defines the value of a variable.
  65.      The built-in `NEXT' is closely related to `ALIGN'.
  66. `DEFINED(SYMBOL)'
  67.      Return 1 if SYMBOL is in the linker global symbol table and is
  68.      defined, otherwise return 0.  You can use this function to provide
  69.      default values for symbols.  For example, the following
  70.      command-file fragment shows how to set a global symbol `begin' to
  71.      the first location in the `.text' section--but if a symbol called
  72.      `begin' already existed, its value is preserved:
  73.           SECTIONS{ ...
  74.             .text : {
  75.               begin = DEFINED(begin) ? begin : . ;
  76.               ...
  77.             }
  78.           ... }
  79. `NEXT(EXP)'
  80.      Return the next unallocated address that is a multiple of EXP.
  81.      This function is closely related to `ALIGN(EXP)'; unless you use
  82.      the `MEMORY' command to define discontinuous memory for the output
  83.      file, the two functions are equivalent.
  84. `SIZEOF(SECTION)'
  85.      Return the size in bytes of the named SECTION, if that section has
  86.      been allocated.  In the following example, `symbol_1' and
  87.      `symbol_2' are assigned identical values:
  88.           SECTIONS{ ...
  89.             .output {
  90.               .start = . ;
  91.               ...
  92.               .end = . ;
  93.               }
  94.             symbol_1 = .end - .start ;
  95.             symbol_2 = SIZEOF(.output);
  96.           ... }
  97. `SIZEOF_HEADERS'
  98. `sizeof_headers'
  99.      Return the size in bytes of the output file's headers.  You can
  100.      use this number as the start address of the first section, if you
  101.      choose, to facilitate paging.
  102. File: ld.info,  Node: Semicolons,  Prev: Arithmetic Functions,  Up: Expressions
  103. Semicolons
  104. ----------
  105.    Semicolons (";") are required in the following places.  In all other
  106. places they can appear for aesthetic reasons but are otherwise ignored.
  107. `Assignment'
  108.      Semicolons must appear at the end of assignment expressions.
  109.      *Note Assignment::
  110. `PHDRS'
  111.      Semicolons must appear at the end of a `PHDRS' statement.  *Note
  112.      PHDRS::
  113. File: ld.info,  Node: MEMORY,  Next: SECTIONS,  Prev: Expressions,  Up: Commands
  114. Memory Layout
  115. =============
  116.    The linker's default configuration permits allocation of all
  117. available memory.  You can override this configuration by using the
  118. `MEMORY' command.  The `MEMORY' command describes the location and size
  119. of blocks of memory in the target.  By using it carefully, you can
  120. describe which memory regions may be used by the linker, and which
  121. memory regions it must avoid.  The linker does not shuffle sections to
  122. fit into the available regions, but does move the requested sections
  123. into the correct regions and issue errors when the regions become too
  124. full.
  125.    A command file may contain at most one use of the `MEMORY' command;
  126. however, you can define as many blocks of memory within it as you wish.
  127. The syntax is:
  128.      MEMORY
  129.        {
  130.          NAME (ATTR) : ORIGIN = ORIGIN, LENGTH = LEN
  131.          ...
  132.        }
  133. `NAME'
  134.      is a name used internally by the linker to refer to the region. Any
  135.      symbol name may be used.  The region names are stored in a separate
  136.      name space, and will not conflict with symbols, file names or
  137.      section names.  Use distinct names to specify multiple regions.
  138. `(ATTR)'
  139.      is an optional list of attributes, permitted for compatibility
  140.      with the AT&T linker but not used by `ld' beyond checking that the
  141.      attribute list is valid.  Valid attribute lists must be made up of
  142.      the characters "`LIRWX'".  If you omit the attribute list, you may
  143.      omit the parentheses around it as well.
  144. `ORIGIN'
  145.      is the start address of the region in physical memory.  It is an
  146.      expression that must evaluate to a constant before memory
  147.      allocation is performed. The keyword `ORIGIN' may be abbreviated
  148.      to `org' or `o' (but not, for example, `ORG').
  149. `LEN'
  150.      is the size in bytes of the region (an expression).  The keyword
  151.      `LENGTH' may be abbreviated to `len' or `l'.
  152.    For example, to specify that memory has two regions available for
  153. allocation--one starting at 0 for 256 kilobytes, and the other starting
  154. at `0x40000000' for four megabytes:
  155.      MEMORY
  156.        {
  157.        rom : ORIGIN = 0, LENGTH = 256K
  158.        ram : org = 0x40000000, l = 4M
  159.        }
  160.    Once you have defined a region of memory named MEM, you can direct
  161. specific output sections there by using a command ending in `>MEM'
  162. within the `SECTIONS' command (*note Section Options::.).  If the
  163. combined output sections directed to a region are too big for the
  164. region, the linker will issue an error message.
  165. File: ld.info,  Node: SECTIONS,  Next: PHDRS,  Prev: MEMORY,  Up: Commands
  166. Specifying Output Sections
  167. ==========================
  168.    The `SECTIONS' command controls exactly where input sections are
  169. placed into output sections, their order in the output file, and to
  170. which output sections they are allocated.
  171.    You may use at most one `SECTIONS' command in a script file, but you
  172. can have as many statements within it as you wish.  Statements within
  173. the `SECTIONS' command can do one of three things:
  174.    * define the entry point;
  175.    * assign a value to a symbol;
  176.    * describe the placement of a named output section, and which input
  177.      sections go into it.
  178.    You can also use the first two operations--defining the entry point
  179. and defining symbols--outside the `SECTIONS' command: *note Entry
  180. Point::., and *Note Assignment::.  They are permitted here as well for
  181. your convenience in reading the script, so that symbols and the entry
  182. point can be defined at meaningful points in your output-file layout.
  183.    If you do not use a `SECTIONS' command, the linker places each input
  184. section into an identically named output section in the order that the
  185. sections are first encountered in the input files.  If all input
  186. sections are present in the first file, for example, the order of
  187. sections in the output file will match the order in the first input
  188. file.
  189. * Menu:
  190. * Section Definition::          Section Definitions
  191. * Section Placement::           Section Placement
  192. * Section Data Expressions::    Section Data Expressions
  193. * Section Options::             Optional Section Attributes
  194. File: ld.info,  Node: Section Definition,  Next: Section Placement,  Up: SECTIONS
  195. Section Definitions
  196. -------------------
  197.    The most frequently used statement in the `SECTIONS' command is the
  198. "section definition", which specifies the properties of an output
  199. section: its location, alignment, contents, fill pattern, and target
  200. memory region.  Most of these specifications are optional; the simplest
  201. form of a section definition is
  202.      SECTIONS { ...
  203.        SECNAME : {
  204.          CONTENTS
  205.        }
  206.      ... }
  207. SECNAME is the name of the output section, and CONTENTS a specification
  208. of what goes there--for example, a list of input files or sections of
  209. input files (*note Section Placement::.).  As you might assume, the
  210. whitespace shown is optional.  You do need the colon `:' and the braces
  211. `{}', however.
  212.    SECNAME must meet the constraints of your output format.  In formats
  213. which only support a limited number of sections, such as `a.out', the
  214. name must be one of the names supported by the format (`a.out', for
  215. example, allows only `.text', `.data' or `.bss'). If the output format
  216. supports any number of sections, but with numbers and not names (as is
  217. the case for Oasys), the name should be supplied as a quoted numeric
  218. string.  A section name may consist of any sequence of characters, but
  219. any name which does not conform to the standard `ld' symbol name syntax
  220. must be quoted.  *Note Symbol Names: Symbols.
  221.    The special SECNAME `/DISCARD/' may be used to discard input
  222. sections.  Any sections which are assigned to an output section named
  223. `/DISCARD/' are not included in the final link output.
  224.    The linker will not create output sections which do not have any
  225. contents.  This is for convenience when referring to input sections that
  226. may or may not exist.  For example,
  227.      .foo { *(.foo) }
  228.    will only create a `.foo' section in the output file if there is a
  229. `.foo' section in at least one input file.
  230. File: ld.info,  Node: Section Placement,  Next: Section Data Expressions,  Prev: Section Definition,  Up: SECTIONS
  231. Section Placement
  232. -----------------
  233.    In a section definition, you can specify the contents of an output
  234. section by listing particular input files, by listing particular
  235. input-file sections, or by a combination of the two.  You can also place
  236. arbitrary data in the section, and define symbols relative to the
  237. beginning of the section.
  238.    The CONTENTS of a section definition may include any of the
  239. following kinds of statement.  You can include as many of these as you
  240. like in a single section definition, separated from one another by
  241. whitespace.
  242. `FILENAME'
  243.      You may simply name a particular input file to be placed in the
  244.      current output section; *all* sections from that file are placed
  245.      in the current section definition.  If the file name has already
  246.      been mentioned in another section definition, with an explicit
  247.      section name list, then only those sections which have not yet
  248.      been allocated are used.
  249.      To specify a list of particular files by name:
  250.           .data : { afile.o bfile.o cfile.o }
  251.      The example also illustrates that multiple statements can be
  252.      included in the contents of a section definition, since each file
  253.      name is a separate statement.
  254. `FILENAME( SECTION )'
  255. `FILENAME( SECTION , SECTION, ... )'
  256. `FILENAME( SECTION SECTION ... )'
  257.      You can name one or more sections from your input files, for
  258.      insertion in the current output section.  If you wish to specify a
  259.      list of input-file sections inside the parentheses, you may
  260.      separate the section names by either commas or whitespace.
  261. `* (SECTION)'
  262. `* (SECTION, SECTION, ...)'
  263. `* (SECTION SECTION ...)'
  264.      Instead of explicitly naming particular input files in a link
  265.      control script, you can refer to *all* files from the `ld' command
  266.      line: use `*' instead of a particular file name before the
  267.      parenthesized input-file section list.
  268.      If you have already explicitly included some files by name, `*'
  269.      refers to all *remaining* files--those whose places in the output
  270.      file have not yet been defined.
  271.      For example, to copy sections `1' through `4' from an Oasys file
  272.      into the `.text' section of an `a.out' file, and sections `13' and
  273.      `14' into the `.data' section:
  274.           SECTIONS {
  275.             .text :{
  276.               *("1" "2" "3" "4")
  277.             }
  278.           
  279.             .data :{
  280.               *("13" "14")
  281.             }
  282.           }
  283.      `[ SECTION ... ]' used to be accepted as an alternate way to
  284.      specify named sections from all unallocated input files.  Because
  285.      some operating systems (VMS) allow brackets in file names, that
  286.      notation is no longer supported.
  287. `FILENAME`( COMMON )''
  288. `*( COMMON )'
  289.      Specify where in your output file to place uninitialized data with
  290.      this notation.  `*(COMMON)' by itself refers to all uninitialized
  291.      data from all input files (so far as it is not yet allocated);
  292.      FILENAME`(COMMON)' refers to uninitialized data from a particular
  293.      file.  Both are special cases of the general mechanisms for
  294.      specifying where to place input-file sections: `ld' permits you to
  295.      refer to uninitialized data as if it were in an input-file section
  296.      named `COMMON', regardless of the input file's format.
  297.    For example, the following command script arranges the output file
  298. into three consecutive sections, named `.text', `.data', and `.bss',
  299. taking the input for each from the correspondingly named sections of
  300. all the input files:
  301.      SECTIONS {
  302.        .text : { *(.text) }
  303.        .data : { *(.data) }
  304.        .bss :  { *(.bss)  *(COMMON) }
  305.      }
  306.    The following example reads all of the sections from file `all.o'
  307. and places them at the start of output section `outputa' which starts
  308. at location `0x10000'. All of section `.input1' from file `foo.o'
  309. follows immediately, in the same output section.  All of section
  310. `.input2' from `foo.o' goes into output section `outputb', followed by
  311. section `.input1' from `foo1.o'.  All of the remaining `.input1' and
  312. `.input2' sections from any files are written to output section
  313. `outputc'.
  314.      SECTIONS {
  315.        outputa 0x10000 :
  316.          {
  317.          all.o
  318.          foo.o (.input1)
  319.          }
  320.        outputb :
  321.          {
  322.          foo.o (.input2)
  323.          foo1.o (.input1)
  324.          }
  325.        outputc :
  326.          {
  327.          *(.input1)
  328.          *(.input2)
  329.          }
  330.      }
  331. File: ld.info,  Node: Section Data Expressions,  Next: Section Options,  Prev: Section Placement,  Up: SECTIONS
  332. Section Data Expressions
  333. ------------------------
  334.    The foregoing statements arrange, in your output file, data
  335. originating from your input files.  You can also place data directly in
  336. an output section from the link command script.  Most of these
  337. additional statements involve expressions (*note Expressions::.).
  338. Although these statements are shown separately here for ease of
  339. presentation, no such segregation is needed within a section definition
  340. in the `SECTIONS' command; you can intermix them freely with any of the
  341. statements we've just described.
  342. `CREATE_OBJECT_SYMBOLS'
  343.      Create a symbol for each input file in the current section, set to
  344.      the address of the first byte of data written from that input
  345.      file.  For instance, with `a.out' files it is conventional to have
  346.      a symbol for each input file.  You can accomplish this by defining
  347.      the output `.text' section as follows:
  348.           SECTIONS {
  349.             .text 0x2020 :
  350.                {
  351.               CREATE_OBJECT_SYMBOLS
  352.               *(.text)
  353.               _etext = ALIGN(0x2000);
  354.               }
  355.             ...
  356.           }
  357.      If `sample.ld' is a file containing this script, and `a.o', `b.o',
  358.      `c.o', and `d.o' are four input files with contents like the
  359.      following--
  360.           /* a.c */
  361.           
  362.           afunction() { }
  363.           int adata=1;
  364.           int abss;
  365.      `ld -M -T sample.ld a.o b.o c.o d.o' would create a map like this,
  366.      containing symbols matching the object file names:
  367.           00000000 A __DYNAMIC
  368.           00004020 B _abss
  369.           00004000 D _adata
  370.           00002020 T _afunction
  371.           00004024 B _bbss
  372.           00004008 D _bdata
  373.           00002038 T _bfunction
  374.           00004028 B _cbss
  375.           00004010 D _cdata
  376.           00002050 T _cfunction
  377.           0000402c B _dbss
  378.           00004018 D _ddata
  379.           00002068 T _dfunction
  380.           00004020 D _edata
  381.           00004030 B _end
  382.           00004000 T _etext
  383.           00002020 t a.o
  384.           00002038 t b.o
  385.           00002050 t c.o
  386.           00002068 t d.o
  387. `SYMBOL = EXPRESSION ;'
  388. `SYMBOL F= EXPRESSION ;'
  389.      SYMBOL is any symbol name (*note Symbols::.).  "F=" refers to any
  390.      of the operators `&= += -= *= /=' which combine arithmetic and
  391.      assignment.
  392.      When you assign a value to a symbol within a particular section
  393.      definition, the value is relative to the beginning of the section
  394.      (*note Assignment::.).  If you write
  395.           SECTIONS {
  396.             abs = 14 ;
  397.             ...
  398.             .data : { ... rel = 14 ; ... }
  399.             abs2 = 14 + ADDR(.data);
  400.             ...
  401.           }
  402.      `abs' and `rel' do not have the same value; `rel' has the same
  403.      value as `abs2'.
  404. `BYTE(EXPRESSION)'
  405. `SHORT(EXPRESSION)'
  406. `LONG(EXPRESSION)'
  407. `QUAD(EXPRESSION)'
  408.      By including one of these four statements in a section definition,
  409.      you can explicitly place one, two, four, or eight bytes
  410.      (respectively) at the current address of that section.  `QUAD' is
  411.      only supported when using a 64 bit host or target.
  412.      Multiple-byte quantities are represented in whatever byte order is
  413.      appropriate for the output file format (*note BFD::.).
  414. `FILL(EXPRESSION)'
  415.      Specify the "fill pattern" for the current section.  Any otherwise
  416.      unspecified regions of memory within the section (for example,
  417.      regions you skip over by assigning a new value to the location
  418.      counter `.') are filled with the two least significant bytes from
  419.      the EXPRESSION argument.  A `FILL' statement covers memory
  420.      locations *after* the point it occurs in the section definition; by
  421.      including more than one `FILL' statement, you can have different
  422.      fill patterns in different parts of an output section.
  423. File: ld.info,  Node: Section Options,  Prev: Section Data Expressions,  Up: SECTIONS
  424. Optional Section Attributes
  425. ---------------------------
  426.    Here is the full syntax of a section definition, including all the
  427. optional portions:
  428.      SECTIONS {
  429.      ...
  430.      SECNAME START BLOCK(ALIGN) (NOLOAD) : AT ( LDADR )
  431.        { CONTENTS } >REGION :PHDR =FILL
  432.      ...
  433.      }
  434.    SECNAME and CONTENTS are required.  *Note Section Definition::, and
  435. *Note Section Placement::, for details on CONTENTS.  The remaining
  436. elements--START, `BLOCK(ALIGN)', `(NOLOAD)', `AT ( LDADR )', `>REGION',
  437. `:PHDR', and `=FILL'--are all optional.
  438. `START'
  439.      You can force the output section to be loaded at a specified
  440.      address by specifying START immediately following the section name.
  441.      sTART can be represented as any expression. The following example
  442.      generates section OUTPUT at location `0x40000000':
  443.           SECTIONS {
  444.             ...
  445.             output 0x40000000: {
  446.               ...
  447.               }
  448.             ...
  449.           }
  450. `BLOCK(ALIGN)'
  451.      You can include `BLOCK()' specification to advance the location
  452.      counter `.' prior to the beginning of the section, so that the
  453.      section will begin at the specified alignment.  ALIGN is an
  454.      expression.
  455. `(NOLOAD)'
  456.      Use `(NOLOAD)' to prevent a section from being loaded into memory
  457.      each time it is accessed.  For example, in the script sample
  458.      below, the `ROM' segment is addressed at memory location `0' and
  459.      does not need to be loaded into each object file:
  460.           SECTIONS {
  461.             ROM  0  (NOLOAD)  : { ... }
  462.             ...
  463.           }
  464. `AT ( LDADR )'
  465.      The expression LDADR that follows the `AT' keyword specifies the
  466.      load address of the section.  The default (if you do not use the
  467.      `AT' keyword) is to make the load address the same as the
  468.      relocation address.  This feature is designed to make it easy to
  469.      build a ROM image.  For example, this `SECTIONS' definition
  470.      creates two output sections: one called `.text', which starts at
  471.      `0x1000', and one called `.mdata', which is loaded at the end of
  472.      the `.text' section even though its relocation address is
  473.      `0x2000'.  The symbol `_data' is defined with the value `0x2000':
  474.           SECTIONS
  475.             {
  476.             .text 0x1000 : { *(.text) _etext = . ; }
  477.             .mdata 0x2000 :
  478.               AT ( ADDR(.text) + SIZEOF ( .text ) )
  479.               { _data = . ; *(.data); _edata = . ;  }
  480.             .bss 0x3000 :
  481.               { _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;}
  482.           }
  483.      The run-time initialization code (for C programs, usually `crt0')
  484.      for use with a ROM generated this way has to include something like
  485.      the following, to copy the initialized data from the ROM image to
  486.      its runtime address:
  487.           char *src = _etext;
  488.           char *dst = _data;
  489.           
  490.           /* ROM has data at end of text; copy it. */
  491.           while (dst < _edata) {
  492.             *dst++ = *src++;
  493.           }
  494.           
  495.           /* Zero bss */
  496.           for (dst = _bstart; dst< _bend; dst++)
  497.             *dst = 0;
  498. `>REGION'
  499.      Assign this section to a previously defined region of memory.
  500.      *Note MEMORY::.
  501. `:PHDR'
  502.      Assign this section to a segment described by a program header.
  503.      *Note PHDRS::.  If a section is assigned to one or more segments,
  504.      then all subsequent allocated sections will be assigned to those
  505.      segments as well, unless they use an explicitly `:PHDR' modifier.
  506.      To prevent a section from being assigned to a segment when it would
  507.      normally default to one, use `:NONE'.
  508. `=FILL'
  509.      Including `=FILL' in a section definition specifies the initial
  510.      fill value for that section.  You may use any expression to
  511.      specify FILL.  Any unallocated holes in the current output section
  512.      when written to the output file will be filled with the two least
  513.      significant bytes of the value, repeated as necessary.  You can
  514.      also change the fill value with a `FILL' statement in the CONTENTS
  515.      of a section definition.
  516. File: ld.info,  Node: PHDRS,  Next: Entry Point,  Prev: SECTIONS,  Up: Commands
  517. ELF Program Headers
  518. ===================
  519.    The ELF object file format uses "program headers", which are read by
  520. the system loader and describe how the program should be loaded into
  521. memory.  These program headers must be set correctly in order to run the
  522. program on a native ELF system.  The linker will create reasonable
  523. program headers by default.  However, in some cases, it is desirable to
  524. specify the program headers more precisely; the `PHDRS' command may be
  525. used for this purpose.  When the `PHDRS' command is used, the linker
  526. will not generate any program headers itself.
  527.    The `PHDRS' command is only meaningful when generating an ELF output
  528. file.  It is ignored in other cases.  This manual does not describe the
  529. details of how the system loader interprets program headers; for more
  530. information, see the ELF ABI.  The program headers of an ELF file may
  531. be displayed using the `-p' option of the `objdump' command.
  532.    This is the syntax of the `PHDRS' command.  The words `PHDRS',
  533. `FILEHDR', `AT', and `FLAGS' are keywords.
  534.      PHDRS
  535.      {
  536.        NAME TYPE [ FILEHDR ] [ PHDRS ] [ AT ( ADDRESS ) ]
  537.              [ FLAGS ( FLAGS ) ] ;
  538.      }
  539.    The NAME is used only for reference in the `SECTIONS' command of the
  540. linker script.  It does not get put into the output file.
  541.    Certain program header types describe segments of memory which are
  542. loaded from the file by the system loader.  In the linker script, the
  543. contents of these segments are specified by directing allocated output
  544. sections to be placed in the segment.  To do this, the command
  545. describing the output section in the `SECTIONS' command should use
  546. `:NAME', where NAME is the name of the program header as it appears in
  547. the `PHDRS' command.  *Note Section Options::.
  548.    It is normal for certain sections to appear in more than one segment.
  549. This merely implies that one segment of memory contains another.  This
  550. is specified by repeating `:NAME', using it once for each program
  551. header in which the section is to appear.
  552.    If a section is placed in one or more segments using `:NAME', then
  553. all subsequent allocated sections which do not specify `:NAME' are
  554. placed in the same segments.  This is for convenience, since generally
  555. a whole set of contiguous sections will be placed in a single segment.
  556. To prevent a section from being assigned to a segment when it would
  557. normally default to one, use `:NONE'.
  558.    The `FILEHDR' and `PHDRS' keywords which may appear after the
  559. program header type also indicate contents of the segment of memory.
  560. The `FILEHDR' keyword means that the segment should include the ELF
  561. file header.  The `PHDRS' keyword means that the segment should include
  562. the ELF program headers themselves.
  563.    The TYPE may be one of the following.  The numbers indicate the
  564. value of the keyword.
  565. `PT_NULL' (0)
  566.      Indicates an unused program header.
  567. `PT_LOAD' (1)
  568.      Indicates that this program header describes a segment to be
  569.      loaded from the file.
  570. `PT_DYNAMIC' (2)
  571.      Indicates a segment where dynamic linking information can be found.
  572. `PT_INTERP' (3)
  573.      Indicates a segment where the name of the program interpreter may
  574.      be found.
  575. `PT_NOTE' (4)
  576.      Indicates a segment holding note information.
  577. `PT_SHLIB' (5)
  578.      A reserved program header type, defined but not specified by the
  579.      ELF ABI.
  580. `PT_PHDR' (6)
  581.      Indicates a segment where the program headers may be found.
  582. EXPRESSION
  583.      An expression giving the numeric type of the program header.  This
  584.      may be used for types not defined above.
  585.    It is possible to specify that a segment should be loaded at a
  586. particular address in memory.  This is done using an `AT' expression.
  587. This is identical to the `AT' command used in the `SECTIONS' command
  588. (*note Section Options::.).  Using the `AT' command for a program
  589. header overrides any information in the `SECTIONS' command.
  590.    Normally the segment flags are set based on the sections.  The
  591. `FLAGS' keyword may be used to explicitly specify the segment flags.
  592. The value of FLAGS must be an integer.  It is used to set the `p_flags'
  593. field of the program header.
  594.    Here is an example of the use of `PHDRS'.  This shows a typical set
  595. of program headers used on a native ELF system.
  596.      PHDRS
  597.      {
  598.        headers PT_PHDR PHDRS ;
  599.        interp PT_INTERP ;
  600.        text PT_LOAD FILEHDR PHDRS ;
  601.        data PT_LOAD ;
  602.        dynamic PT_DYNAMIC ;
  603.      }
  604.      
  605.      SECTIONS
  606.      {
  607.        . = SIZEOF_HEADERS;
  608.        .interp : { *(.interp) } :text :interp
  609.        .text : { *(.text) } :text
  610.        .rodata : { *(.rodata) } /* defaults to :text */
  611.        ...
  612.        . = . + 0x1000; /* move to a new page in memory */
  613.        .data : { *(.data) } :data
  614.        .dynamic : { *(.dynamic) } :data :dynamic
  615.        ...
  616.      }
  617. File: ld.info,  Node: Entry Point,  Next: Option Commands,  Prev: PHDRS,  Up: Commands
  618. The Entry Point
  619. ===============
  620.    The linker command language includes a command specifically for
  621. defining the first executable instruction in an output file (its "entry
  622. point").  Its argument is a symbol name:
  623.      ENTRY(SYMBOL)
  624.    Like symbol assignments, the `ENTRY' command may be placed either as
  625. an independent command in the command file, or among the section
  626. definitions within the `SECTIONS' command--whatever makes the most
  627. sense for your layout.
  628.    `ENTRY' is only one of several ways of choosing the entry point.
  629. You may indicate it in any of the following ways (shown in descending
  630. order of priority: methods higher in the list override methods lower
  631. down).
  632.    * the `-e' ENTRY command-line option;
  633.    * the `ENTRY(SYMBOL)' command in a linker control script;
  634.    * the value of the symbol `start', if present;
  635.    * the address of the first byte of the `.text' section, if present;
  636.    * The address `0'.
  637.    For example, you can use these rules to generate an entry point with
  638. an assignment statement: if no symbol `start' is defined within your
  639. input files, you can simply define it, assigning it an appropriate
  640. value--
  641.      start = 0x2020;
  642. The example shows an absolute address, but you can use any expression.
  643. For example, if your input object files use some other symbol-name
  644. convention for the entry point, you can just assign the value of
  645. whatever symbol contains the start address to `start':
  646.      start = other_symbol ;
  647. File: ld.info,  Node: Option Commands,  Prev: Entry Point,  Up: Commands
  648. Option Commands
  649. ===============
  650.    The command language includes a number of other commands that you can
  651. use for specialized purposes.  They are similar in purpose to
  652. command-line options.
  653. `CONSTRUCTORS'
  654.      When linking using the `a.out' object file format, the linker uses
  655.      an unusual set construct to support C++ global constructors and
  656.      destructors.  When linking object file formats which do not support
  657.      arbitrary sections, such as `ECOFF' and `XCOFF', the linker will
  658.      automatically recognize C++ global constructors and destructors by
  659.      name.  For these object file formats, the `CONSTRUCTORS' command
  660.      tells the linker where this information should be placed.  The
  661.      `CONSTRUCTORS' command is ignored for other object file formats.
  662.      The symbol `__CTOR_LIST__' marks the start of the global
  663.      constructors, and the symbol `__DTOR_LIST' marks the end.  The
  664.      first word in the list is the number of entries, followed by the
  665.      address of each constructor or destructor, followed by a zero
  666.      word.  The compiler must arrange to actually run the code.  For
  667.      these object file formats GNU C++ calls constructors from a
  668.      subroutine `__main'; a call to `__main' is automatically inserted
  669.      into the startup code for `main'.  GNU C++ runs destructors either
  670.      by using `atexit', or directly from the function `exit'.
  671.      For object file formats such as `COFF' or `ELF' which support
  672.      multiple sections, GNU C++ will normally arrange to put the
  673.      addresses of global constructors and destructors into the `.ctors'
  674.      and `.dtors' sections.  Placing the following sequence into your
  675.      linker script will build the sort of table which the GNU C++
  676.      runtime code expects to see.
  677.                 __CTOR_LIST__ = .;
  678.                 LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
  679.                 *(.ctors)
  680.                 LONG(0)
  681.                 __CTOR_END__ = .;
  682.                 __DTOR_LIST__ = .;
  683.                 LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
  684.                 *(.dtors)
  685.                 LONG(0)
  686.                 __DTOR_END__ = .;
  687.      Normally the compiler and linker will handle these issues
  688.      automatically, and you will not need to concern yourself with
  689.      them.  However, you may need to consider this if you are using C++
  690.      and writing your own linker scripts.
  691. `FLOAT'
  692. `NOFLOAT'
  693.      These keywords were used in some older linkers to request a
  694.      particular math subroutine library.  `ld' doesn't use the
  695.      keywords, assuming instead that any necessary subroutines are in
  696.      libraries specified using the general mechanisms for linking to
  697.      archives; but to permit the use of scripts that were written for
  698.      the older linkers, the keywords `FLOAT' and `NOFLOAT' are accepted
  699.      and ignored.
  700. `FORCE_COMMON_ALLOCATION'
  701.      This command has the same effect as the `-d' command-line option:
  702.      to make `ld' assign space to common symbols even if a relocatable
  703.      output file is specified (`-r').
  704. `INPUT ( FILE, FILE, ... )'
  705. `INPUT ( FILE FILE ... )'
  706.      Use this command to include binary input files in the link, without
  707.      including them in a particular section definition.  Specify the
  708.      full name for each FILE, including `.a' if required.
  709.      `ld' searches for each FILE through the archive-library search
  710.      path, just as for files you specify on the command line.  See the
  711.      description of `-L' in *Note Command Line Options: Options.
  712.      If you use `-lFILE', `ld' will transform the name to `libFILE.a'
  713.      as with the command line argument `-l'.
  714. `GROUP ( FILE, FILE, ... )'
  715. `GROUP ( FILE FILE ... )'
  716.      This command is like `INPUT', except that the named files should
  717.      all be archives, and they are searched repeatedly until no new
  718.      undefined references are created.  See the description of `-(' in
  719.      *Note Command Line Options: Options.
  720. `OUTPUT ( FILENAME )'
  721.      Use this command to name the link output file FILENAME.  The
  722.      effect of `OUTPUT(FILENAME)' is identical to the effect of
  723.      `-o FILENAME', which overrides it.  You can use this command to
  724.      supply a default output-file name other than `a.out'.
  725. `OUTPUT_ARCH ( BFDNAME )'
  726.      Specify a particular output machine architecture, with one of the
  727.      names used by the BFD back-end routines (*note BFD::.).  This
  728.      command is often unnecessary; the architecture is most often set
  729.      implicitly by either the system BFD configuration or as a side
  730.      effect of the `OUTPUT_FORMAT' command.
  731. `OUTPUT_FORMAT ( BFDNAME )'
  732.      When `ld' is configured to support multiple object code formats,
  733.      you can use this command to specify a particular output format.
  734.      bFDNAME is one of the names used by the BFD back-end routines
  735.      (*note BFD::.).  The effect is identical to the effect of the
  736.      `-oformat' command-line option.  This selection affects only the
  737.      output file; the related command `TARGET' affects primarily input
  738.      files.
  739. `SEARCH_DIR ( PATH )'
  740.      Add PATH to the list of paths where `ld' looks for archive
  741.      libraries.  `SEARCH_DIR(PATH)' has the same effect as `-LPATH' on
  742.      the command line.
  743. `STARTUP ( FILENAME )'
  744.      Ensure that FILENAME is the first input file used in the link
  745.      process.
  746. `TARGET ( FORMAT )'
  747.      When `ld' is configured to support multiple object code formats,
  748.      you can use this command to change the input-file object code
  749.      format (like the command-line option `-b' or its synonym
  750.      `-format').  The argument FORMAT is one of the strings used by BFD
  751.      to name binary formats.  If `TARGET' is specified but
  752.      `OUTPUT_FORMAT' is not, the last `TARGET' argument is also used as
  753.      the default format for the `ld' output file.  *Note BFD::.
  754.      If you don't use the `TARGET' command, `ld' uses the value of the
  755.      environment variable `GNUTARGET', if available, to select the
  756.      output file format.  If that variable is also absent, `ld' uses
  757.      the default format configured for your machine in the BFD
  758.      libraries.
  759. File: ld.info,  Node: Machine Dependent,  Next: BFD,  Prev: Commands,  Up: Top
  760. Machine Dependent Features
  761. **************************
  762.    `ld' has additional features on some platforms; the following
  763. sections describe them.  Machines where `ld' has no additional
  764. functionality are not listed.
  765. * Menu:
  766. * H8/300::                      `ld' and the H8/300
  767. * i960::                        `ld' and the Intel 960 family
  768. File: ld.info,  Node: H8/300,  Next: i960,  Up: Machine Dependent
  769. `ld' and the H8/300
  770. ===================
  771.    For the H8/300, `ld' can perform these global optimizations when you
  772. specify the `-relax' command-line option.
  773. *relaxing address modes*
  774.      `ld' finds all `jsr' and `jmp' instructions whose targets are
  775.      within eight bits, and turns them into eight-bit program-counter
  776.      relative `bsr' and `bra' instructions, respectively.
  777. *synthesizing instructions*
  778.      `ld' finds all `mov.b' instructions which use the sixteen-bit
  779.      absolute address form, but refer to the top page of memory, and
  780.      changes them to use the eight-bit address form.  (That is: the
  781.      linker turns `mov.b `@'AA:16' into `mov.b `@'AA:8' whenever the
  782.      address AA is in the top page of memory).
  783. File: ld.info,  Node: i960,  Prev: H8/300,  Up: Machine Dependent
  784. `ld' and the Intel 960 family
  785. =============================
  786.    You can use the `-AARCHITECTURE' command line option to specify one
  787. of the two-letter names identifying members of the 960 family; the
  788. option specifies the desired output target, and warns of any
  789. incompatible instructions in the input files.  It also modifies the
  790. linker's search strategy for archive libraries, to support the use of
  791. libraries specific to each particular architecture, by including in the
  792. search loop names suffixed with the string identifying the architecture.
  793.    For example, if your `ld' command line included `-ACA' as well as
  794. `-ltry', the linker would look (in its built-in search paths, and in
  795. any paths you specify with `-L') for a library with the names
  796.      try
  797.      libtry.a
  798.      tryca
  799.      libtryca.a
  800. The first two possibilities would be considered in any event; the last
  801. two are due to the use of `-ACA'.
  802.    You can meaningfully use `-A' more than once on a command line, since
  803. the 960 architecture family allows combination of target architectures;
  804. each use will add another pair of name variants to search for when `-l'
  805. specifies a library.
  806.    `ld' supports the `-relax' option for the i960 family.  If you
  807. specify `-relax', `ld' finds all `balx' and `calx' instructions whose
  808. targets are within 24 bits, and turns them into 24-bit program-counter
  809. relative `bal' and `cal' instructions, respectively.  `ld' also turns
  810. `cal' instructions into `bal' instructions when it determines that the
  811. target subroutine is a leaf routine (that is, the target subroutine does
  812. not itself call any subroutines).
  813. File: ld.info,  Node: BFD,  Next: MRI,  Prev: Machine Dependent,  Up: Top
  814.    The linker accesses object and archive files using the BFD libraries.
  815. These libraries allow the linker to use the same routines to operate on
  816. object files whatever the object file format.  A different object file
  817. format can be supported simply by creating a new BFD back end and adding
  818. it to the library.  To conserve runtime memory, however, the linker and
  819. associated tools are usually configured to support only a subset of the
  820. object file formats available.  You can use `objdump -i' (*note
  821. objdump: (binutils.info)objdump.) to list all the formats available for
  822. your configuration.
  823.    As with most implementations, BFD is a compromise between several
  824. conflicting requirements. The major factor influencing BFD design was
  825. efficiency: any time used converting between formats is time which
  826. would not have been spent had BFD not been involved. This is partly
  827. offset by abstraction payback; since BFD simplifies applications and
  828. back ends, more time and care may be spent optimizing algorithms for a
  829. greater speed.
  830.    One minor artifact of the BFD solution which you should bear in mind
  831. is the potential for information loss.  There are two places where
  832. useful information can be lost using the BFD mechanism: during
  833. conversion and during output. *Note BFD information loss::.
  834. * Menu:
  835. * BFD outline::                 How it works: an outline of BFD
  836. File: ld.info,  Node: BFD outline,  Up: BFD
  837. How it works: an outline of BFD
  838. ===============================
  839.    When an object file is opened, BFD subroutines automatically
  840. determine the format of the input object file.  They then build a
  841. descriptor in memory with pointers to routines that will be used to
  842. access elements of the object file's data structures.
  843.    As different information from the the object files is required, BFD
  844. reads from different sections of the file and processes them.  For
  845. example, a very common operation for the linker is processing symbol
  846. tables.  Each BFD back end provides a routine for converting between
  847. the object file's representation of symbols and an internal canonical
  848. format. When the linker asks for the symbol table of an object file, it
  849. calls through a memory pointer to the routine from the relevant BFD
  850. back end which reads and converts the table into a canonical form.  The
  851. linker then operates upon the canonical form. When the link is finished
  852. and the linker writes the output file's symbol table, another BFD back
  853. end routine is called to take the newly created symbol table and
  854. convert it into the chosen output format.
  855. * Menu:
  856. * BFD information loss::    Information Loss
  857. * Canonical format::        The BFD    canonical object-file format
  858. File: ld.info,  Node: BFD information loss,  Next: Canonical format,  Up: BFD outline
  859. Information Loss
  860. ----------------
  861.    *Information can be lost during output.* The output formats
  862. supported by BFD do not provide identical facilities, and information
  863. which can be described in one form has nowhere to go in another format.
  864. One example of this is alignment information in `b.out'. There is
  865. nowhere in an `a.out' format file to store alignment information on the
  866. contained data, so when a file is linked from `b.out' and an `a.out'
  867. image is produced, alignment information will not propagate to the
  868. output file. (The linker will still use the alignment information
  869. internally, so the link is performed correctly).
  870.    Another example is COFF section names. COFF files may contain an
  871. unlimited number of sections, each one with a textual section name. If
  872. the target of the link is a format which does not have many sections
  873. (e.g., `a.out') or has sections without names (e.g., the Oasys format),
  874. the link cannot be done simply. You can circumvent this problem by
  875. describing the desired input-to-output section mapping with the linker
  876. command language.
  877.    *Information can be lost during canonicalization.* The BFD internal
  878. canonical form of the external formats is not exhaustive; there are
  879. structures in input formats for which there is no direct representation
  880. internally.  This means that the BFD back ends cannot maintain all
  881. possible data richness through the transformation between external to
  882. internal and back to external formats.
  883.    This limitation is only a problem when an application reads one
  884. format and writes another.  Each BFD back end is responsible for
  885. maintaining as much data as possible, and the internal BFD canonical
  886. form has structures which are opaque to the BFD core, and exported only
  887. to the back ends. When a file is read in one format, the canonical form
  888. is generated for BFD and the application. At the same time, the back
  889. end saves away any information which may otherwise be lost. If the data
  890. is then written back in the same format, the back end routine will be
  891. able to use the canonical form provided by the BFD core as well as the
  892. information it prepared earlier.  Since there is a great deal of
  893. commonality between back ends, there is no information lost when
  894. linking or copying big endian COFF to little endian COFF, or `a.out' to
  895. `b.out'.  When a mixture of formats is linked, the information is only
  896. lost from the files whose format differs from the destination.
  897. File: ld.info,  Node: Canonical format,  Prev: BFD information loss,  Up: BFD outline
  898. The BFD canonical object-file format
  899. ------------------------------------
  900.    The greatest potential for loss of information occurs when there is
  901. the least overlap between the information provided by the source
  902. format, that stored by the canonical format, and that needed by the
  903. destination format. A brief description of the canonical form may help
  904. you understand which kinds of data you can count on preserving across
  905. conversions.
  906. *files*
  907.      Information stored on a per-file basis includes target machine
  908.      architecture, particular implementation format type, a demand
  909.      pageable bit, and a write protected bit.  Information like Unix
  910.      magic numbers is not stored here--only the magic numbers' meaning,
  911.      so a `ZMAGIC' file would have both the demand pageable bit and the
  912.      write protected text bit set.  The byte order of the target is
  913.      stored on a per-file basis, so that big- and little-endian object
  914.      files may be used with one another.
  915. *sections*
  916.      Each section in the input file contains the name of the section,
  917.      the section's original address in the object file, size and
  918.      alignment information, various flags, and pointers into other BFD
  919.      data structures.
  920. *symbols*
  921.      Each symbol contains a pointer to the information for the object
  922.      file which originally defined it, its name, its value, and various
  923.      flag bits.  When a BFD back end reads in a symbol table, it
  924.      relocates all symbols to make them relative to the base of the
  925.      section where they were defined.  Doing this ensures that each
  926.      symbol points to its containing section.  Each symbol also has a
  927.      varying amount of hidden private data for the BFD back end.  Since
  928.      the symbol points to the original file, the private data format
  929.      for that symbol is accessible.  `ld' can operate on a collection
  930.      of symbols of wildly different formats without problems.
  931.      Normal global and simple local symbols are maintained on output,
  932.      so an output file (no matter its format) will retain symbols
  933.      pointing to functions and to global, static, and common variables.
  934.      Some symbol information is not worth retaining; in `a.out', type
  935.      information is stored in the symbol table as long symbol names.
  936.      This information would be useless to most COFF debuggers; the
  937.      linker has command line switches to allow users to throw it away.
  938.      There is one word of type information within the symbol, so if the
  939.      format supports symbol type information within symbols (for
  940.      example, COFF, IEEE, Oasys) and the type is simple enough to fit
  941.      within one word (nearly everything but aggregates), the
  942.      information will be preserved.
  943. *relocation level*
  944.      Each canonical BFD relocation record contains a pointer to the
  945.      symbol to relocate to, the offset of the data to relocate, the
  946.      section the data is in, and a pointer to a relocation type
  947.      descriptor. Relocation is performed by passing messages through
  948.      the relocation type descriptor and the symbol pointer. Therefore,
  949.      relocations can be performed on output data using a relocation
  950.      method that is only available in one of the input formats. For
  951.      instance, Oasys provides a byte relocation format.  A relocation
  952.      record requesting this relocation type would point indirectly to a
  953.      routine to perform this, so the relocation may be performed on a
  954.      byte being written to a 68k COFF file, even though 68k COFF has no
  955.      such relocation type.
  956. *line numbers*
  957.      Object formats can contain, for debugging purposes, some form of
  958.      mapping between symbols, source line numbers, and addresses in the
  959.      output file.  These addresses have to be relocated along with the
  960.      symbol information.  Each symbol with an associated list of line
  961.      number records points to the first record of the list.  The head
  962.      of a line number list consists of a pointer to the symbol, which
  963.      allows finding out the address of the function whose line number
  964.      is being described. The rest of the list is made up of pairs:
  965.      offsets into the section and line numbers. Any format which can
  966.      simply derive this information can pass it successfully between
  967.      formats (COFF, IEEE and Oasys).
  968.