Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* linker.c -- BFD linker routines
  2.    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  3.    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
  4.    Free Software Foundation, Inc.
  5.    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
  6.  
  7.    This file is part of BFD, the Binary File Descriptor library.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 3 of the License, or
  12.    (at your option) any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  22.    MA 02110-1301, USA.  */
  23.  
  24. #include "sysdep.h"
  25. #include "bfd.h"
  26. #include "libbfd.h"
  27. #include "bfdlink.h"
  28. #include "genlink.h"
  29.  
  30. /*
  31. SECTION
  32.         Linker Functions
  33.  
  34. @cindex Linker
  35.         The linker uses three special entry points in the BFD target
  36.         vector.  It is not necessary to write special routines for
  37.         these entry points when creating a new BFD back end, since
  38.         generic versions are provided.  However, writing them can
  39.         speed up linking and make it use significantly less runtime
  40.         memory.
  41.  
  42.         The first routine creates a hash table used by the other
  43.         routines.  The second routine adds the symbols from an object
  44.         file to the hash table.  The third routine takes all the
  45.         object files and links them together to create the output
  46.         file.  These routines are designed so that the linker proper
  47.         does not need to know anything about the symbols in the object
  48.         files that it is linking.  The linker merely arranges the
  49.         sections as directed by the linker script and lets BFD handle
  50.         the details of symbols and relocs.
  51.  
  52.         The second routine and third routines are passed a pointer to
  53.         a <<struct bfd_link_info>> structure (defined in
  54.         <<bfdlink.h>>) which holds information relevant to the link,
  55.         including the linker hash table (which was created by the
  56.         first routine) and a set of callback functions to the linker
  57.         proper.
  58.  
  59.         The generic linker routines are in <<linker.c>>, and use the
  60.         header file <<genlink.h>>.  As of this writing, the only back
  61.         ends which have implemented versions of these routines are
  62.         a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
  63.         routines are used as examples throughout this section.
  64.  
  65. @menu
  66. @* Creating a Linker Hash Table::
  67. @* Adding Symbols to the Hash Table::
  68. @* Performing the Final Link::
  69. @end menu
  70.  
  71. INODE
  72. Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
  73. SUBSECTION
  74.         Creating a linker hash table
  75.  
  76. @cindex _bfd_link_hash_table_create in target vector
  77. @cindex target vector (_bfd_link_hash_table_create)
  78.         The linker routines must create a hash table, which must be
  79.         derived from <<struct bfd_link_hash_table>> described in
  80.         <<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
  81.         create a derived hash table.  This entry point is called using
  82.         the target vector of the linker output file.
  83.  
  84.         The <<_bfd_link_hash_table_create>> entry point must allocate
  85.         and initialize an instance of the desired hash table.  If the
  86.         back end does not require any additional information to be
  87.         stored with the entries in the hash table, the entry point may
  88.         simply create a <<struct bfd_link_hash_table>>.  Most likely,
  89.         however, some additional information will be needed.
  90.  
  91.         For example, with each entry in the hash table the a.out
  92.         linker keeps the index the symbol has in the final output file
  93.         (this index number is used so that when doing a relocatable
  94.         link the symbol index used in the output file can be quickly
  95.         filled in when copying over a reloc).  The a.out linker code
  96.         defines the required structures and functions for a hash table
  97.         derived from <<struct bfd_link_hash_table>>.  The a.out linker
  98.         hash table is created by the function
  99.         <<NAME(aout,link_hash_table_create)>>; it simply allocates
  100.         space for the hash table, initializes it, and returns a
  101.         pointer to it.
  102.  
  103.         When writing the linker routines for a new back end, you will
  104.         generally not know exactly which fields will be required until
  105.         you have finished.  You should simply create a new hash table
  106.         which defines no additional fields, and then simply add fields
  107.         as they become necessary.
  108.  
  109. INODE
  110. Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
  111. SUBSECTION
  112.         Adding symbols to the hash table
  113.  
  114. @cindex _bfd_link_add_symbols in target vector
  115. @cindex target vector (_bfd_link_add_symbols)
  116.         The linker proper will call the <<_bfd_link_add_symbols>>
  117.         entry point for each object file or archive which is to be
  118.         linked (typically these are the files named on the command
  119.         line, but some may also come from the linker script).  The
  120.         entry point is responsible for examining the file.  For an
  121.         object file, BFD must add any relevant symbol information to
  122.         the hash table.  For an archive, BFD must determine which
  123.         elements of the archive should be used and adding them to the
  124.         link.
  125.  
  126.         The a.out version of this entry point is
  127.         <<NAME(aout,link_add_symbols)>>.
  128.  
  129. @menu
  130. @* Differing file formats::
  131. @* Adding symbols from an object file::
  132. @* Adding symbols from an archive::
  133. @end menu
  134.  
  135. INODE
  136. Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
  137. SUBSUBSECTION
  138.         Differing file formats
  139.  
  140.         Normally all the files involved in a link will be of the same
  141.         format, but it is also possible to link together different
  142.         format object files, and the back end must support that.  The
  143.         <<_bfd_link_add_symbols>> entry point is called via the target
  144.         vector of the file to be added.  This has an important
  145.         consequence: the function may not assume that the hash table
  146.         is the type created by the corresponding
  147.         <<_bfd_link_hash_table_create>> vector.  All the
  148.         <<_bfd_link_add_symbols>> function can assume about the hash
  149.         table is that it is derived from <<struct
  150.         bfd_link_hash_table>>.
  151.  
  152.         Sometimes the <<_bfd_link_add_symbols>> function must store
  153.         some information in the hash table entry to be used by the
  154.         <<_bfd_final_link>> function.  In such a case the output bfd
  155.         xvec must be checked to make sure that the hash table was
  156.         created by an object file of the same format.
  157.  
  158.         The <<_bfd_final_link>> routine must be prepared to handle a
  159.         hash entry without any extra information added by the
  160.         <<_bfd_link_add_symbols>> function.  A hash entry without
  161.         extra information will also occur when the linker script
  162.         directs the linker to create a symbol.  Note that, regardless
  163.         of how a hash table entry is added, all the fields will be
  164.         initialized to some sort of null value by the hash table entry
  165.         initialization function.
  166.  
  167.         See <<ecoff_link_add_externals>> for an example of how to
  168.         check the output bfd before saving information (in this
  169.         case, the ECOFF external symbol debugging information) in a
  170.         hash table entry.
  171.  
  172. INODE
  173. Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
  174. SUBSUBSECTION
  175.         Adding symbols from an object file
  176.  
  177.         When the <<_bfd_link_add_symbols>> routine is passed an object
  178.         file, it must add all externally visible symbols in that
  179.         object file to the hash table.  The actual work of adding the
  180.         symbol to the hash table is normally handled by the function
  181.         <<_bfd_generic_link_add_one_symbol>>.  The
  182.         <<_bfd_link_add_symbols>> routine is responsible for reading
  183.         all the symbols from the object file and passing the correct
  184.         information to <<_bfd_generic_link_add_one_symbol>>.
  185.  
  186.         The <<_bfd_link_add_symbols>> routine should not use
  187.         <<bfd_canonicalize_symtab>> to read the symbols.  The point of
  188.         providing this routine is to avoid the overhead of converting
  189.         the symbols into generic <<asymbol>> structures.
  190.  
  191. @findex _bfd_generic_link_add_one_symbol
  192.         <<_bfd_generic_link_add_one_symbol>> handles the details of
  193.         combining common symbols, warning about multiple definitions,
  194.         and so forth.  It takes arguments which describe the symbol to
  195.         add, notably symbol flags, a section, and an offset.  The
  196.         symbol flags include such things as <<BSF_WEAK>> or
  197.         <<BSF_INDIRECT>>.  The section is a section in the object
  198.         file, or something like <<bfd_und_section_ptr>> for an undefined
  199.         symbol or <<bfd_com_section_ptr>> for a common symbol.
  200.  
  201.         If the <<_bfd_final_link>> routine is also going to need to
  202.         read the symbol information, the <<_bfd_link_add_symbols>>
  203.         routine should save it somewhere attached to the object file
  204.         BFD.  However, the information should only be saved if the
  205.         <<keep_memory>> field of the <<info>> argument is TRUE, so
  206.         that the <<-no-keep-memory>> linker switch is effective.
  207.  
  208.         The a.out function which adds symbols from an object file is
  209.         <<aout_link_add_object_symbols>>, and most of the interesting
  210.         work is in <<aout_link_add_symbols>>.  The latter saves
  211.         pointers to the hash tables entries created by
  212.         <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
  213.         so that the <<_bfd_final_link>> routine does not have to call
  214.         the hash table lookup routine to locate the entry.
  215.  
  216. INODE
  217. Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
  218. SUBSUBSECTION
  219.         Adding symbols from an archive
  220.  
  221.         When the <<_bfd_link_add_symbols>> routine is passed an
  222.         archive, it must look through the symbols defined by the
  223.         archive and decide which elements of the archive should be
  224.         included in the link.  For each such element it must call the
  225.         <<add_archive_element>> linker callback, and it must add the
  226.         symbols from the object file to the linker hash table.  (The
  227.         callback may in fact indicate that a replacement BFD should be
  228.         used, in which case the symbols from that BFD should be added
  229.         to the linker hash table instead.)
  230.  
  231. @findex _bfd_generic_link_add_archive_symbols
  232.         In most cases the work of looking through the symbols in the
  233.         archive should be done by the
  234.         <<_bfd_generic_link_add_archive_symbols>> function.  This
  235.         function builds a hash table from the archive symbol table and
  236.         looks through the list of undefined symbols to see which
  237.         elements should be included.
  238.         <<_bfd_generic_link_add_archive_symbols>> is passed a function
  239.         to call to make the final decision about adding an archive
  240.         element to the link and to do the actual work of adding the
  241.         symbols to the linker hash table.
  242.  
  243.         The function passed to
  244.         <<_bfd_generic_link_add_archive_symbols>> must read the
  245.         symbols of the archive element and decide whether the archive
  246.         element should be included in the link.  If the element is to
  247.         be included, the <<add_archive_element>> linker callback
  248.         routine must be called with the element as an argument, and
  249.         the element's symbols must be added to the linker hash table
  250.         just as though the element had itself been passed to the
  251.         <<_bfd_link_add_symbols>> function.  The <<add_archive_element>>
  252.         callback has the option to indicate that it would like to
  253.         replace the element archive with a substitute BFD, in which
  254.         case it is the symbols of that substitute BFD that must be
  255.         added to the linker hash table instead.
  256.  
  257.         When the a.out <<_bfd_link_add_symbols>> function receives an
  258.         archive, it calls <<_bfd_generic_link_add_archive_symbols>>
  259.         passing <<aout_link_check_archive_element>> as the function
  260.         argument. <<aout_link_check_archive_element>> calls
  261.         <<aout_link_check_ar_symbols>>.  If the latter decides to add
  262.         the element (an element is only added if it provides a real,
  263.         non-common, definition for a previously undefined or common
  264.         symbol) it calls the <<add_archive_element>> callback and then
  265.         <<aout_link_check_archive_element>> calls
  266.         <<aout_link_add_symbols>> to actually add the symbols to the
  267.         linker hash table - possibly those of a substitute BFD, if the
  268.         <<add_archive_element>> callback avails itself of that option.
  269.  
  270.         The ECOFF back end is unusual in that it does not normally
  271.         call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
  272.         archives already contain a hash table of symbols.  The ECOFF
  273.         back end searches the archive itself to avoid the overhead of
  274.         creating a new hash table.
  275.  
  276. INODE
  277. Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
  278. SUBSECTION
  279.         Performing the final link
  280.  
  281. @cindex _bfd_link_final_link in target vector
  282. @cindex target vector (_bfd_final_link)
  283.         When all the input files have been processed, the linker calls
  284.         the <<_bfd_final_link>> entry point of the output BFD.  This
  285.         routine is responsible for producing the final output file,
  286.         which has several aspects.  It must relocate the contents of
  287.         the input sections and copy the data into the output sections.
  288.         It must build an output symbol table including any local
  289.         symbols from the input files and the global symbols from the
  290.         hash table.  When producing relocatable output, it must
  291.         modify the input relocs and write them into the output file.
  292.         There may also be object format dependent work to be done.
  293.  
  294.         The linker will also call the <<write_object_contents>> entry
  295.         point when the BFD is closed.  The two entry points must work
  296.         together in order to produce the correct output file.
  297.  
  298.         The details of how this works are inevitably dependent upon
  299.         the specific object file format.  The a.out
  300.         <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
  301.  
  302. @menu
  303. @* Information provided by the linker::
  304. @* Relocating the section contents::
  305. @* Writing the symbol table::
  306. @end menu
  307.  
  308. INODE
  309. Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
  310. SUBSUBSECTION
  311.         Information provided by the linker
  312.  
  313.         Before the linker calls the <<_bfd_final_link>> entry point,
  314.         it sets up some data structures for the function to use.
  315.  
  316.         The <<input_bfds>> field of the <<bfd_link_info>> structure
  317.         will point to a list of all the input files included in the
  318.         link.  These files are linked through the <<link_next>> field
  319.         of the <<bfd>> structure.
  320.  
  321.         Each section in the output file will have a list of
  322.         <<link_order>> structures attached to the <<map_head.link_order>>
  323.         field (the <<link_order>> structure is defined in
  324.         <<bfdlink.h>>).  These structures describe how to create the
  325.         contents of the output section in terms of the contents of
  326.         various input sections, fill constants, and, eventually, other
  327.         types of information.  They also describe relocs that must be
  328.         created by the BFD backend, but do not correspond to any input
  329.         file; this is used to support -Ur, which builds constructors
  330.         while generating a relocatable object file.
  331.  
  332. INODE
  333. Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
  334. SUBSUBSECTION
  335.         Relocating the section contents
  336.  
  337.         The <<_bfd_final_link>> function should look through the
  338.         <<link_order>> structures attached to each section of the
  339.         output file.  Each <<link_order>> structure should either be
  340.         handled specially, or it should be passed to the function
  341.         <<_bfd_default_link_order>> which will do the right thing
  342.         (<<_bfd_default_link_order>> is defined in <<linker.c>>).
  343.  
  344.         For efficiency, a <<link_order>> of type
  345.         <<bfd_indirect_link_order>> whose associated section belongs
  346.         to a BFD of the same format as the output BFD must be handled
  347.         specially.  This type of <<link_order>> describes part of an
  348.         output section in terms of a section belonging to one of the
  349.         input files.  The <<_bfd_final_link>> function should read the
  350.         contents of the section and any associated relocs, apply the
  351.         relocs to the section contents, and write out the modified
  352.         section contents.  If performing a relocatable link, the
  353.         relocs themselves must also be modified and written out.
  354.  
  355. @findex _bfd_relocate_contents
  356. @findex _bfd_final_link_relocate
  357.         The functions <<_bfd_relocate_contents>> and
  358.         <<_bfd_final_link_relocate>> provide some general support for
  359.         performing the actual relocations, notably overflow checking.
  360.         Their arguments include information about the symbol the
  361.         relocation is against and a <<reloc_howto_type>> argument
  362.         which describes the relocation to perform.  These functions
  363.         are defined in <<reloc.c>>.
  364.  
  365.         The a.out function which handles reading, relocating, and
  366.         writing section contents is <<aout_link_input_section>>.  The
  367.         actual relocation is done in <<aout_link_input_section_std>>
  368.         and <<aout_link_input_section_ext>>.
  369.  
  370. INODE
  371. Writing the symbol table, , Relocating the section contents, Performing the Final Link
  372. SUBSUBSECTION
  373.         Writing the symbol table
  374.  
  375.         The <<_bfd_final_link>> function must gather all the symbols
  376.         in the input files and write them out.  It must also write out
  377.         all the symbols in the global hash table.  This must be
  378.         controlled by the <<strip>> and <<discard>> fields of the
  379.         <<bfd_link_info>> structure.
  380.  
  381.         The local symbols of the input files will not have been
  382.         entered into the linker hash table.  The <<_bfd_final_link>>
  383.         routine must consider each input file and include the symbols
  384.         in the output file.  It may be convenient to do this when
  385.         looking through the <<link_order>> structures, or it may be
  386.         done by stepping through the <<input_bfds>> list.
  387.  
  388.         The <<_bfd_final_link>> routine must also traverse the global
  389.         hash table to gather all the externally visible symbols.  It
  390.         is possible that most of the externally visible symbols may be
  391.         written out when considering the symbols of each input file,
  392.         but it is still necessary to traverse the hash table since the
  393.         linker script may have defined some symbols that are not in
  394.         any of the input files.
  395.  
  396.         The <<strip>> field of the <<bfd_link_info>> structure
  397.         controls which symbols are written out.  The possible values
  398.         are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
  399.         then the <<keep_hash>> field of the <<bfd_link_info>>
  400.         structure is a hash table of symbols to keep; each symbol
  401.         should be looked up in this hash table, and only symbols which
  402.         are present should be included in the output file.
  403.  
  404.         If the <<strip>> field of the <<bfd_link_info>> structure
  405.         permits local symbols to be written out, the <<discard>> field
  406.         is used to further controls which local symbols are included
  407.         in the output file.  If the value is <<discard_l>>, then all
  408.         local symbols which begin with a certain prefix are discarded;
  409.         this is controlled by the <<bfd_is_local_label_name>> entry point.
  410.  
  411.         The a.out backend handles symbols by calling
  412.         <<aout_link_write_symbols>> on each input BFD and then
  413.         traversing the global hash table with the function
  414.         <<aout_link_write_other_symbol>>.  It builds a string table
  415.         while writing out the symbols, which is written to the output
  416.         file at the end of <<NAME(aout,final_link)>>.
  417. */
  418.  
  419. static bfd_boolean generic_link_add_object_symbols
  420.   (bfd *, struct bfd_link_info *, bfd_boolean collect);
  421. static bfd_boolean generic_link_add_symbols
  422.   (bfd *, struct bfd_link_info *, bfd_boolean);
  423. static bfd_boolean generic_link_check_archive_element_no_collect
  424.   (bfd *, struct bfd_link_info *, bfd_boolean *);
  425. static bfd_boolean generic_link_check_archive_element_collect
  426.   (bfd *, struct bfd_link_info *, bfd_boolean *);
  427. static bfd_boolean generic_link_check_archive_element
  428.   (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
  429. static bfd_boolean generic_link_add_symbol_list
  430.   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
  431.    bfd_boolean);
  432. static bfd_boolean generic_add_output_symbol
  433.   (bfd *, size_t *psymalloc, asymbol *);
  434. static bfd_boolean default_data_link_order
  435.   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
  436. static bfd_boolean default_indirect_link_order
  437.   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
  438.    bfd_boolean);
  439.  
  440. /* The link hash table structure is defined in bfdlink.h.  It provides
  441.    a base hash table which the backend specific hash tables are built
  442.    upon.  */
  443.  
  444. /* Routine to create an entry in the link hash table.  */
  445.  
  446. struct bfd_hash_entry *
  447. _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
  448.                         struct bfd_hash_table *table,
  449.                         const char *string)
  450. {
  451.   /* Allocate the structure if it has not already been allocated by a
  452.      subclass.  */
  453.   if (entry == NULL)
  454.     {
  455.       entry = (struct bfd_hash_entry *)
  456.           bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
  457.       if (entry == NULL)
  458.         return entry;
  459.     }
  460.  
  461.   /* Call the allocation method of the superclass.  */
  462.   entry = bfd_hash_newfunc (entry, table, string);
  463.   if (entry)
  464.     {
  465.       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
  466.  
  467.       /* Initialize the local fields.  */
  468.       memset ((char *) &h->root + sizeof (h->root), 0,
  469.               sizeof (*h) - sizeof (h->root));
  470.     }
  471.  
  472.   return entry;
  473. }
  474.  
  475. /* Initialize a link hash table.  The BFD argument is the one
  476.    responsible for creating this table.  */
  477.  
  478. bfd_boolean
  479. _bfd_link_hash_table_init
  480.   (struct bfd_link_hash_table *table,
  481.    bfd *abfd ATTRIBUTE_UNUSED,
  482.    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  483.                                       struct bfd_hash_table *,
  484.                                       const char *),
  485.    unsigned int entsize)
  486. {
  487.   table->undefs = NULL;
  488.   table->undefs_tail = NULL;
  489.   table->type = bfd_link_generic_hash_table;
  490.  
  491.   return bfd_hash_table_init (&table->table, newfunc, entsize);
  492. }
  493.  
  494. /* Look up a symbol in a link hash table.  If follow is TRUE, we
  495.    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
  496.    the real symbol.  */
  497.  
  498. struct bfd_link_hash_entry *
  499. bfd_link_hash_lookup (struct bfd_link_hash_table *table,
  500.                       const char *string,
  501.                       bfd_boolean create,
  502.                       bfd_boolean copy,
  503.                       bfd_boolean follow)
  504. {
  505.   struct bfd_link_hash_entry *ret;
  506.  
  507.   ret = ((struct bfd_link_hash_entry *)
  508.          bfd_hash_lookup (&table->table, string, create, copy));
  509.  
  510.   if (follow && ret != NULL)
  511.     {
  512.       while (ret->type == bfd_link_hash_indirect
  513.              || ret->type == bfd_link_hash_warning)
  514.         ret = ret->u.i.link;
  515.     }
  516.  
  517.   return ret;
  518. }
  519.  
  520. /* Look up a symbol in the main linker hash table if the symbol might
  521.    be wrapped.  This should only be used for references to an
  522.    undefined symbol, not for definitions of a symbol.  */
  523.  
  524. struct bfd_link_hash_entry *
  525. bfd_wrapped_link_hash_lookup (bfd *abfd,
  526.                               struct bfd_link_info *info,
  527.                               const char *string,
  528.                               bfd_boolean create,
  529.                               bfd_boolean copy,
  530.                               bfd_boolean follow)
  531. {
  532.   bfd_size_type amt;
  533.  
  534.   if (info->wrap_hash != NULL)
  535.     {
  536.       const char *l;
  537.       char prefix = '\0';
  538.  
  539.       l = string;
  540.       if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
  541.         {
  542.           prefix = *l;
  543.           ++l;
  544.         }
  545.  
  546. #undef WRAP
  547. #define WRAP "__wrap_"
  548.  
  549.       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
  550.         {
  551.           char *n;
  552.           struct bfd_link_hash_entry *h;
  553.  
  554.           /* This symbol is being wrapped.  We want to replace all
  555.              references to SYM with references to __wrap_SYM.  */
  556.  
  557.           amt = strlen (l) + sizeof WRAP + 1;
  558.           n = (char *) bfd_malloc (amt);
  559.           if (n == NULL)
  560.             return NULL;
  561.  
  562.           n[0] = prefix;
  563.           n[1] = '\0';
  564.           strcat (n, WRAP);
  565.           strcat (n, l);
  566.           h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
  567.           free (n);
  568.           return h;
  569.         }
  570.  
  571. #undef WRAP
  572.  
  573. #undef  REAL
  574. #define REAL "__real_"
  575.  
  576.       if (*l == '_'
  577.           && CONST_STRNEQ (l, REAL)
  578.           && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
  579.                               FALSE, FALSE) != NULL)
  580.         {
  581.           char *n;
  582.           struct bfd_link_hash_entry *h;
  583.  
  584.           /* This is a reference to __real_SYM, where SYM is being
  585.              wrapped.  We want to replace all references to __real_SYM
  586.              with references to SYM.  */
  587.  
  588.           amt = strlen (l + sizeof REAL - 1) + 2;
  589.           n = (char *) bfd_malloc (amt);
  590.           if (n == NULL)
  591.             return NULL;
  592.  
  593.           n[0] = prefix;
  594.           n[1] = '\0';
  595.           strcat (n, l + sizeof REAL - 1);
  596.           h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
  597.           free (n);
  598.           return h;
  599.         }
  600.  
  601. #undef REAL
  602.     }
  603.  
  604.   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
  605. }
  606.  
  607. /* Traverse a generic link hash table.  Differs from bfd_hash_traverse
  608.    in the treatment of warning symbols.  When warning symbols are
  609.    created they replace the real symbol, so you don't get to see the
  610.    real symbol in a bfd_hash_travere.  This traversal calls func with
  611.    the real symbol.  */
  612.  
  613. void
  614. bfd_link_hash_traverse
  615.   (struct bfd_link_hash_table *htab,
  616.    bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
  617.    void *info)
  618. {
  619.   unsigned int i;
  620.  
  621.   htab->table.frozen = 1;
  622.   for (i = 0; i < htab->table.size; i++)
  623.     {
  624.       struct bfd_link_hash_entry *p;
  625.  
  626.       p = (struct bfd_link_hash_entry *) htab->table.table[i];
  627.       for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
  628.         if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
  629.           goto out;
  630.     }
  631.  out:
  632.   htab->table.frozen = 0;
  633. }
  634.  
  635. /* Add a symbol to the linker hash table undefs list.  */
  636.  
  637. void
  638. bfd_link_add_undef (struct bfd_link_hash_table *table,
  639.                     struct bfd_link_hash_entry *h)
  640. {
  641.   BFD_ASSERT (h->u.undef.next == NULL);
  642.   if (table->undefs_tail != NULL)
  643.     table->undefs_tail->u.undef.next = h;
  644.   if (table->undefs == NULL)
  645.     table->undefs = h;
  646.   table->undefs_tail = h;
  647. }
  648.  
  649. /* The undefs list was designed so that in normal use we don't need to
  650.    remove entries.  However, if symbols on the list are changed from
  651.    bfd_link_hash_undefined to either bfd_link_hash_undefweak or
  652.    bfd_link_hash_new for some reason, then they must be removed from the
  653.    list.  Failure to do so might result in the linker attempting to add
  654.    the symbol to the list again at a later stage.  */
  655.  
  656. void
  657. bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
  658. {
  659.   struct bfd_link_hash_entry **pun;
  660.  
  661.   pun = &table->undefs;
  662.   while (*pun != NULL)
  663.     {
  664.       struct bfd_link_hash_entry *h = *pun;
  665.  
  666.       if (h->type == bfd_link_hash_new
  667.           || h->type == bfd_link_hash_undefweak)
  668.         {
  669.           *pun = h->u.undef.next;
  670.           h->u.undef.next = NULL;
  671.           if (h == table->undefs_tail)
  672.             {
  673.               if (pun == &table->undefs)
  674.                 table->undefs_tail = NULL;
  675.               else
  676.                 /* pun points at an u.undef.next field.  Go back to
  677.                    the start of the link_hash_entry.  */
  678.                 table->undefs_tail = (struct bfd_link_hash_entry *)
  679.                   ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
  680.               break;
  681.             }
  682.         }
  683.       else
  684.         pun = &h->u.undef.next;
  685.     }
  686. }
  687. /* Routine to create an entry in a generic link hash table.  */
  688.  
  689. struct bfd_hash_entry *
  690. _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
  691.                                 struct bfd_hash_table *table,
  692.                                 const char *string)
  693. {
  694.   /* Allocate the structure if it has not already been allocated by a
  695.      subclass.  */
  696.   if (entry == NULL)
  697.     {
  698.       entry = (struct bfd_hash_entry *)
  699.         bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
  700.       if (entry == NULL)
  701.         return entry;
  702.     }
  703.  
  704.   /* Call the allocation method of the superclass.  */
  705.   entry = _bfd_link_hash_newfunc (entry, table, string);
  706.   if (entry)
  707.     {
  708.       struct generic_link_hash_entry *ret;
  709.  
  710.       /* Set local fields.  */
  711.       ret = (struct generic_link_hash_entry *) entry;
  712.       ret->written = FALSE;
  713.       ret->sym = NULL;
  714.     }
  715.  
  716.   return entry;
  717. }
  718.  
  719. /* Create a generic link hash table.  */
  720.  
  721. struct bfd_link_hash_table *
  722. _bfd_generic_link_hash_table_create (bfd *abfd)
  723. {
  724.   struct generic_link_hash_table *ret;
  725.   bfd_size_type amt = sizeof (struct generic_link_hash_table);
  726.  
  727.   ret = (struct generic_link_hash_table *) bfd_malloc (amt);
  728.   if (ret == NULL)
  729.     return NULL;
  730.   if (! _bfd_link_hash_table_init (&ret->root, abfd,
  731.                                    _bfd_generic_link_hash_newfunc,
  732.                                    sizeof (struct generic_link_hash_entry)))
  733.     {
  734.       free (ret);
  735.       return NULL;
  736.     }
  737.   return &ret->root;
  738. }
  739.  
  740. void
  741. _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
  742. {
  743.   struct generic_link_hash_table *ret
  744.     = (struct generic_link_hash_table *) hash;
  745.  
  746.   bfd_hash_table_free (&ret->root.table);
  747.   free (ret);
  748. }
  749.  
  750. /* Grab the symbols for an object file when doing a generic link.  We
  751.    store the symbols in the outsymbols field.  We need to keep them
  752.    around for the entire link to ensure that we only read them once.
  753.    If we read them multiple times, we might wind up with relocs and
  754.    the hash table pointing to different instances of the symbol
  755.    structure.  */
  756.  
  757. bfd_boolean
  758. bfd_generic_link_read_symbols (bfd *abfd)
  759. {
  760.   if (bfd_get_outsymbols (abfd) == NULL)
  761.     {
  762.       long symsize;
  763.       long symcount;
  764.  
  765.       symsize = bfd_get_symtab_upper_bound (abfd);
  766.       if (symsize < 0)
  767.         return FALSE;
  768.       bfd_get_outsymbols (abfd) = (struct bfd_symbol **) bfd_alloc (abfd,
  769.                                                                     symsize);
  770.       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
  771.         return FALSE;
  772.       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
  773.       if (symcount < 0)
  774.         return FALSE;
  775.       bfd_get_symcount (abfd) = symcount;
  776.     }
  777.  
  778.   return TRUE;
  779. }
  780. /* Generic function to add symbols to from an object file to the
  781.    global hash table.  This version does not automatically collect
  782.    constructors by name.  */
  783.  
  784. bfd_boolean
  785. _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  786. {
  787.   return generic_link_add_symbols (abfd, info, FALSE);
  788. }
  789.  
  790. /* Generic function to add symbols from an object file to the global
  791.    hash table.  This version automatically collects constructors by
  792.    name, as the collect2 program does.  It should be used for any
  793.    target which does not provide some other mechanism for setting up
  794.    constructors and destructors; these are approximately those targets
  795.    for which gcc uses collect2 and do not support stabs.  */
  796.  
  797. bfd_boolean
  798. _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
  799. {
  800.   return generic_link_add_symbols (abfd, info, TRUE);
  801. }
  802.  
  803. /* Indicate that we are only retrieving symbol values from this
  804.    section.  We want the symbols to act as though the values in the
  805.    file are absolute.  */
  806.  
  807. void
  808. _bfd_generic_link_just_syms (asection *sec,
  809.                              struct bfd_link_info *info ATTRIBUTE_UNUSED)
  810. {
  811.   sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
  812.   sec->output_section = bfd_abs_section_ptr;
  813.   sec->output_offset = sec->vma;
  814. }
  815.  
  816. /* Copy the type of a symbol assiciated with a linker hast table entry.
  817.    Override this so that symbols created in linker scripts get their
  818.    type from the RHS of the assignment.
  819.    The default implementation does nothing.  */
  820. void
  821. _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
  822.     struct bfd_link_hash_entry * hdest ATTRIBUTE_UNUSED,
  823.     struct bfd_link_hash_entry * hsrc ATTRIBUTE_UNUSED)
  824. {
  825. }
  826.  
  827. /* Add symbols from an object file to the global hash table.  */
  828.  
  829. static bfd_boolean
  830. generic_link_add_symbols (bfd *abfd,
  831.                           struct bfd_link_info *info,
  832.                           bfd_boolean collect)
  833. {
  834.   bfd_boolean ret;
  835.  
  836.   switch (bfd_get_format (abfd))
  837.     {
  838.     case bfd_object:
  839.       ret = generic_link_add_object_symbols (abfd, info, collect);
  840.       break;
  841.     case bfd_archive:
  842.       ret = (_bfd_generic_link_add_archive_symbols
  843.              (abfd, info,
  844.               (collect
  845.                ? generic_link_check_archive_element_collect
  846.                : generic_link_check_archive_element_no_collect)));
  847.       break;
  848.     default:
  849.       bfd_set_error (bfd_error_wrong_format);
  850.       ret = FALSE;
  851.     }
  852.  
  853.   return ret;
  854. }
  855.  
  856. /* Add symbols from an object file to the global hash table.  */
  857.  
  858. static bfd_boolean
  859. generic_link_add_object_symbols (bfd *abfd,
  860.                                  struct bfd_link_info *info,
  861.                                  bfd_boolean collect)
  862. {
  863.   bfd_size_type symcount;
  864.   struct bfd_symbol **outsyms;
  865.  
  866.   if (!bfd_generic_link_read_symbols (abfd))
  867.     return FALSE;
  868.   symcount = _bfd_generic_link_get_symcount (abfd);
  869.   outsyms = _bfd_generic_link_get_symbols (abfd);
  870.   return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
  871. }
  872. /* We build a hash table of all symbols defined in an archive.  */
  873.  
  874. /* An archive symbol may be defined by multiple archive elements.
  875.    This linked list is used to hold the elements.  */
  876.  
  877. struct archive_list
  878. {
  879.   struct archive_list *next;
  880.   unsigned int indx;
  881. };
  882.  
  883. /* An entry in an archive hash table.  */
  884.  
  885. struct archive_hash_entry
  886. {
  887.   struct bfd_hash_entry root;
  888.   /* Where the symbol is defined.  */
  889.   struct archive_list *defs;
  890. };
  891.  
  892. /* An archive hash table itself.  */
  893.  
  894. struct archive_hash_table
  895. {
  896.   struct bfd_hash_table table;
  897. };
  898.  
  899. /* Create a new entry for an archive hash table.  */
  900.  
  901. static struct bfd_hash_entry *
  902. archive_hash_newfunc (struct bfd_hash_entry *entry,
  903.                       struct bfd_hash_table *table,
  904.                       const char *string)
  905. {
  906.   struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
  907.  
  908.   /* Allocate the structure if it has not already been allocated by a
  909.      subclass.  */
  910.   if (ret == NULL)
  911.     ret = (struct archive_hash_entry *)
  912.         bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
  913.   if (ret == NULL)
  914.     return NULL;
  915.  
  916.   /* Call the allocation method of the superclass.  */
  917.   ret = ((struct archive_hash_entry *)
  918.          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  919.  
  920.   if (ret)
  921.     {
  922.       /* Initialize the local fields.  */
  923.       ret->defs = NULL;
  924.     }
  925.  
  926.   return &ret->root;
  927. }
  928.  
  929. /* Initialize an archive hash table.  */
  930.  
  931. static bfd_boolean
  932. archive_hash_table_init
  933.   (struct archive_hash_table *table,
  934.    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  935.                                       struct bfd_hash_table *,
  936.                                       const char *),
  937.    unsigned int entsize)
  938. {
  939.   return bfd_hash_table_init (&table->table, newfunc, entsize);
  940. }
  941.  
  942. /* Look up an entry in an archive hash table.  */
  943.  
  944. #define archive_hash_lookup(t, string, create, copy) \
  945.   ((struct archive_hash_entry *) \
  946.    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
  947.  
  948. /* Allocate space in an archive hash table.  */
  949.  
  950. #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
  951.  
  952. /* Free an archive hash table.  */
  953.  
  954. #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
  955.  
  956. /* Generic function to add symbols from an archive file to the global
  957.    hash file.  This function presumes that the archive symbol table
  958.    has already been read in (this is normally done by the
  959.    bfd_check_format entry point).  It looks through the undefined and
  960.    common symbols and searches the archive symbol table for them.  If
  961.    it finds an entry, it includes the associated object file in the
  962.    link.
  963.  
  964.    The old linker looked through the archive symbol table for
  965.    undefined symbols.  We do it the other way around, looking through
  966.    undefined symbols for symbols defined in the archive.  The
  967.    advantage of the newer scheme is that we only have to look through
  968.    the list of undefined symbols once, whereas the old method had to
  969.    re-search the symbol table each time a new object file was added.
  970.  
  971.    The CHECKFN argument is used to see if an object file should be
  972.    included.  CHECKFN should set *PNEEDED to TRUE if the object file
  973.    should be included, and must also call the bfd_link_info
  974.    add_archive_element callback function and handle adding the symbols
  975.    to the global hash table.  CHECKFN must notice if the callback
  976.    indicates a substitute BFD, and arrange to add those symbols instead
  977.    if it does so.  CHECKFN should only return FALSE if some sort of
  978.    error occurs.
  979.  
  980.    For some formats, such as a.out, it is possible to look through an
  981.    object file but not actually include it in the link.  The
  982.    archive_pass field in a BFD is used to avoid checking the symbols
  983.    of an object files too many times.  When an object is included in
  984.    the link, archive_pass is set to -1.  If an object is scanned but
  985.    not included, archive_pass is set to the pass number.  The pass
  986.    number is incremented each time a new object file is included.  The
  987.    pass number is used because when a new object file is included it
  988.    may create new undefined symbols which cause a previously examined
  989.    object file to be included.  */
  990.  
  991. bfd_boolean
  992. _bfd_generic_link_add_archive_symbols
  993.   (bfd *abfd,
  994.    struct bfd_link_info *info,
  995.    bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
  996. {
  997.   carsym *arsyms;
  998.   carsym *arsym_end;
  999.   register carsym *arsym;
  1000.   int pass;
  1001.   struct archive_hash_table arsym_hash;
  1002.   unsigned int indx;
  1003.   struct bfd_link_hash_entry **pundef;
  1004.  
  1005.   if (! bfd_has_map (abfd))
  1006.     {
  1007.       /* An empty archive is a special case.  */
  1008.       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  1009.         return TRUE;
  1010.       bfd_set_error (bfd_error_no_armap);
  1011.       return FALSE;
  1012.     }
  1013.  
  1014.   arsyms = bfd_ardata (abfd)->symdefs;
  1015.   arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
  1016.  
  1017.   /* In order to quickly determine whether an symbol is defined in
  1018.      this archive, we build a hash table of the symbols.  */
  1019.   if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
  1020.                                  sizeof (struct archive_hash_entry)))
  1021.     return FALSE;
  1022.   for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
  1023.     {
  1024.       struct archive_hash_entry *arh;
  1025.       struct archive_list *l, **pp;
  1026.  
  1027.       arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
  1028.       if (arh == NULL)
  1029.         goto error_return;
  1030.       l = ((struct archive_list *)
  1031.            archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
  1032.       if (l == NULL)
  1033.         goto error_return;
  1034.       l->indx = indx;
  1035.       for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
  1036.         ;
  1037.       *pp = l;
  1038.       l->next = NULL;
  1039.     }
  1040.  
  1041.   /* The archive_pass field in the archive itself is used to
  1042.      initialize PASS, sine we may search the same archive multiple
  1043.      times.  */
  1044.   pass = abfd->archive_pass + 1;
  1045.  
  1046.   /* New undefined symbols are added to the end of the list, so we
  1047.      only need to look through it once.  */
  1048.   pundef = &info->hash->undefs;
  1049.   while (*pundef != NULL)
  1050.     {
  1051.       struct bfd_link_hash_entry *h;
  1052.       struct archive_hash_entry *arh;
  1053.       struct archive_list *l;
  1054.  
  1055.       h = *pundef;
  1056.  
  1057.       /* When a symbol is defined, it is not necessarily removed from
  1058.          the list.  */
  1059.       if (h->type != bfd_link_hash_undefined
  1060.           && h->type != bfd_link_hash_common)
  1061.         {
  1062.           /* Remove this entry from the list, for general cleanliness
  1063.              and because we are going to look through the list again
  1064.              if we search any more libraries.  We can't remove the
  1065.              entry if it is the tail, because that would lose any
  1066.              entries we add to the list later on (it would also cause
  1067.              us to lose track of whether the symbol has been
  1068.              referenced).  */
  1069.           if (*pundef != info->hash->undefs_tail)
  1070.             *pundef = (*pundef)->u.undef.next;
  1071.           else
  1072.             pundef = &(*pundef)->u.undef.next;
  1073.           continue;
  1074.         }
  1075.  
  1076.       /* Look for this symbol in the archive symbol map.  */
  1077.       arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
  1078.       if (arh == NULL)
  1079.         {
  1080.           /* If we haven't found the exact symbol we're looking for,
  1081.              let's look for its import thunk */
  1082.           if (info->pei386_auto_import)
  1083.             {
  1084.               bfd_size_type amt = strlen (h->root.string) + 10;
  1085.               char *buf = (char *) bfd_malloc (amt);
  1086.               if (buf == NULL)
  1087.                 return FALSE;
  1088.  
  1089.               sprintf (buf, "__imp_%s", h->root.string);
  1090.               arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
  1091.               free(buf);
  1092.             }
  1093.           if (arh == NULL)
  1094.             {
  1095.               pundef = &(*pundef)->u.undef.next;
  1096.               continue;
  1097.             }
  1098.         }
  1099.       /* Look at all the objects which define this symbol.  */
  1100.       for (l = arh->defs; l != NULL; l = l->next)
  1101.         {
  1102.           bfd *element;
  1103.           bfd_boolean needed;
  1104.  
  1105.           /* If the symbol has gotten defined along the way, quit.  */
  1106.           if (h->type != bfd_link_hash_undefined
  1107.               && h->type != bfd_link_hash_common)
  1108.             break;
  1109.  
  1110.           element = bfd_get_elt_at_index (abfd, l->indx);
  1111.           if (element == NULL)
  1112.             goto error_return;
  1113.  
  1114.           /* If we've already included this element, or if we've
  1115.              already checked it on this pass, continue.  */
  1116.           if (element->archive_pass == -1
  1117.               || element->archive_pass == pass)
  1118.             continue;
  1119.  
  1120.           /* If we can't figure this element out, just ignore it.  */
  1121.           if (! bfd_check_format (element, bfd_object))
  1122.             {
  1123.               element->archive_pass = -1;
  1124.               continue;
  1125.             }
  1126.  
  1127.           /* CHECKFN will see if this element should be included, and
  1128.              go ahead and include it if appropriate.  */
  1129.           if (! (*checkfn) (element, info, &needed))
  1130.             goto error_return;
  1131.  
  1132.           if (! needed)
  1133.             element->archive_pass = pass;
  1134.           else
  1135.             {
  1136.               element->archive_pass = -1;
  1137.  
  1138.               /* Increment the pass count to show that we may need to
  1139.                  recheck object files which were already checked.  */
  1140.               ++pass;
  1141.             }
  1142.         }
  1143.  
  1144.       pundef = &(*pundef)->u.undef.next;
  1145.     }
  1146.  
  1147.   archive_hash_table_free (&arsym_hash);
  1148.  
  1149.   /* Save PASS in case we are called again.  */
  1150.   abfd->archive_pass = pass;
  1151.  
  1152.   return TRUE;
  1153.  
  1154.  error_return:
  1155.   archive_hash_table_free (&arsym_hash);
  1156.   return FALSE;
  1157. }
  1158. /* See if we should include an archive element.  This version is used
  1159.    when we do not want to automatically collect constructors based on
  1160.    the symbol name, presumably because we have some other mechanism
  1161.    for finding them.  */
  1162.  
  1163. static bfd_boolean
  1164. generic_link_check_archive_element_no_collect (
  1165.                                                bfd *abfd,
  1166.                                                struct bfd_link_info *info,
  1167.                                                bfd_boolean *pneeded)
  1168. {
  1169.   return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
  1170. }
  1171.  
  1172. /* See if we should include an archive element.  This version is used
  1173.    when we want to automatically collect constructors based on the
  1174.    symbol name, as collect2 does.  */
  1175.  
  1176. static bfd_boolean
  1177. generic_link_check_archive_element_collect (bfd *abfd,
  1178.                                             struct bfd_link_info *info,
  1179.                                             bfd_boolean *pneeded)
  1180. {
  1181.   return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
  1182. }
  1183.  
  1184. /* See if we should include an archive element.  Optionally collect
  1185.    constructors.  */
  1186.  
  1187. static bfd_boolean
  1188. generic_link_check_archive_element (bfd *abfd,
  1189.                                     struct bfd_link_info *info,
  1190.                                     bfd_boolean *pneeded,
  1191.                                     bfd_boolean collect)
  1192. {
  1193.   asymbol **pp, **ppend;
  1194.  
  1195.   *pneeded = FALSE;
  1196.  
  1197.   if (!bfd_generic_link_read_symbols (abfd))
  1198.     return FALSE;
  1199.  
  1200.   pp = _bfd_generic_link_get_symbols (abfd);
  1201.   ppend = pp + _bfd_generic_link_get_symcount (abfd);
  1202.   for (; pp < ppend; pp++)
  1203.     {
  1204.       asymbol *p;
  1205.       struct bfd_link_hash_entry *h;
  1206.  
  1207.       p = *pp;
  1208.  
  1209.       /* We are only interested in globally visible symbols.  */
  1210.       if (! bfd_is_com_section (p->section)
  1211.           && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
  1212.         continue;
  1213.  
  1214.       /* We are only interested if we know something about this
  1215.          symbol, and it is undefined or common.  An undefined weak
  1216.          symbol (type bfd_link_hash_undefweak) is not considered to be
  1217.          a reference when pulling files out of an archive.  See the
  1218.          SVR4 ABI, p. 4-27.  */
  1219.       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
  1220.                                 FALSE, TRUE);
  1221.       if (h == NULL
  1222.           || (h->type != bfd_link_hash_undefined
  1223.               && h->type != bfd_link_hash_common))
  1224.         continue;
  1225.  
  1226.       /* P is a symbol we are looking for.  */
  1227.  
  1228.       if (! bfd_is_com_section (p->section))
  1229.         {
  1230.           bfd_size_type symcount;
  1231.           asymbol **symbols;
  1232.           bfd *oldbfd = abfd;
  1233.  
  1234.           /* This object file defines this symbol, so pull it in.  */
  1235.           if (!(*info->callbacks
  1236.                 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
  1237.                                         &abfd))
  1238.             return FALSE;
  1239.           /* Potentially, the add_archive_element hook may have set a
  1240.              substitute BFD for us.  */
  1241.           if (abfd != oldbfd
  1242.               && !bfd_generic_link_read_symbols (abfd))
  1243.             return FALSE;
  1244.           symcount = _bfd_generic_link_get_symcount (abfd);
  1245.           symbols = _bfd_generic_link_get_symbols (abfd);
  1246.           if (! generic_link_add_symbol_list (abfd, info, symcount,
  1247.                                               symbols, collect))
  1248.             return FALSE;
  1249.           *pneeded = TRUE;
  1250.           return TRUE;
  1251.         }
  1252.  
  1253.       /* P is a common symbol.  */
  1254.  
  1255.       if (h->type == bfd_link_hash_undefined)
  1256.         {
  1257.           bfd *symbfd;
  1258.           bfd_vma size;
  1259.           unsigned int power;
  1260.  
  1261.           symbfd = h->u.undef.abfd;
  1262.           if (symbfd == NULL)
  1263.             {
  1264.               /* This symbol was created as undefined from outside
  1265.                  BFD.  We assume that we should link in the object
  1266.                  file.  This is for the -u option in the linker.  */
  1267.               if (!(*info->callbacks
  1268.                     ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
  1269.                                             &abfd))
  1270.                 return FALSE;
  1271.               /* Potentially, the add_archive_element hook may have set a
  1272.                  substitute BFD for us.  But no symbols are going to get
  1273.                  registered by anything we're returning to from here.  */
  1274.               *pneeded = TRUE;
  1275.               return TRUE;
  1276.             }
  1277.  
  1278.           /* Turn the symbol into a common symbol but do not link in
  1279.              the object file.  This is how a.out works.  Object
  1280.              formats that require different semantics must implement
  1281.              this function differently.  This symbol is already on the
  1282.              undefs list.  We add the section to a common section
  1283.              attached to symbfd to ensure that it is in a BFD which
  1284.              will be linked in.  */
  1285.           h->type = bfd_link_hash_common;
  1286.           h->u.c.p = (struct bfd_link_hash_common_entry *)
  1287.             bfd_hash_allocate (&info->hash->table,
  1288.                                sizeof (struct bfd_link_hash_common_entry));
  1289.           if (h->u.c.p == NULL)
  1290.             return FALSE;
  1291.  
  1292.           size = bfd_asymbol_value (p);
  1293.           h->u.c.size = size;
  1294.  
  1295.           power = bfd_log2 (size);
  1296.           if (power > 4)
  1297.             power = 4;
  1298.           h->u.c.p->alignment_power = power;
  1299.  
  1300.           if (p->section == bfd_com_section_ptr)
  1301.             h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
  1302.           else
  1303.             h->u.c.p->section = bfd_make_section_old_way (symbfd,
  1304.                                                           p->section->name);
  1305.           h->u.c.p->section->flags |= SEC_ALLOC;
  1306.         }
  1307.       else
  1308.         {
  1309.           /* Adjust the size of the common symbol if necessary.  This
  1310.              is how a.out works.  Object formats that require
  1311.              different semantics must implement this function
  1312.              differently.  */
  1313.           if (bfd_asymbol_value (p) > h->u.c.size)
  1314.             h->u.c.size = bfd_asymbol_value (p);
  1315.         }
  1316.     }
  1317.  
  1318.   /* This archive element is not needed.  */
  1319.   return TRUE;
  1320. }
  1321.  
  1322. /* Add the symbols from an object file to the global hash table.  ABFD
  1323.    is the object file.  INFO is the linker information.  SYMBOL_COUNT
  1324.    is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
  1325.    is TRUE if constructors should be automatically collected by name
  1326.    as is done by collect2.  */
  1327.  
  1328. static bfd_boolean
  1329. generic_link_add_symbol_list (bfd *abfd,
  1330.                               struct bfd_link_info *info,
  1331.                               bfd_size_type symbol_count,
  1332.                               asymbol **symbols,
  1333.                               bfd_boolean collect)
  1334. {
  1335.   asymbol **pp, **ppend;
  1336.  
  1337.   pp = symbols;
  1338.   ppend = symbols + symbol_count;
  1339.   for (; pp < ppend; pp++)
  1340.     {
  1341.       asymbol *p;
  1342.  
  1343.       p = *pp;
  1344.  
  1345.       if ((p->flags & (BSF_INDIRECT
  1346.                        | BSF_WARNING
  1347.                        | BSF_GLOBAL
  1348.                        | BSF_CONSTRUCTOR
  1349.                        | BSF_WEAK)) != 0
  1350.           || bfd_is_und_section (bfd_get_section (p))
  1351.           || bfd_is_com_section (bfd_get_section (p))
  1352.           || bfd_is_ind_section (bfd_get_section (p)))
  1353.         {
  1354.           const char *name;
  1355.           const char *string;
  1356.           struct generic_link_hash_entry *h;
  1357.           struct bfd_link_hash_entry *bh;
  1358.  
  1359.           string = name = bfd_asymbol_name (p);
  1360.           if (((p->flags & BSF_INDIRECT) != 0
  1361.                || bfd_is_ind_section (p->section))
  1362.               && pp + 1 < ppend)
  1363.             {
  1364.               pp++;
  1365.               string = bfd_asymbol_name (*pp);
  1366.             }
  1367.           else if ((p->flags & BSF_WARNING) != 0
  1368.                    && pp + 1 < ppend)
  1369.             {
  1370.               /* The name of P is actually the warning string, and the
  1371.                  next symbol is the one to warn about.  */
  1372.               pp++;
  1373.               name = bfd_asymbol_name (*pp);
  1374.             }
  1375.  
  1376.           bh = NULL;
  1377.           if (! (_bfd_generic_link_add_one_symbol
  1378.                  (info, abfd, name, p->flags, bfd_get_section (p),
  1379.                   p->value, string, FALSE, collect, &bh)))
  1380.             return FALSE;
  1381.           h = (struct generic_link_hash_entry *) bh;
  1382.  
  1383.           /* If this is a constructor symbol, and the linker didn't do
  1384.              anything with it, then we want to just pass the symbol
  1385.              through to the output file.  This will happen when
  1386.              linking with -r.  */
  1387.           if ((p->flags & BSF_CONSTRUCTOR) != 0
  1388.               && (h == NULL || h->root.type == bfd_link_hash_new))
  1389.             {
  1390.               p->udata.p = NULL;
  1391.               continue;
  1392.             }
  1393.  
  1394.           /* Save the BFD symbol so that we don't lose any backend
  1395.              specific information that may be attached to it.  We only
  1396.              want this one if it gives more information than the
  1397.              existing one; we don't want to replace a defined symbol
  1398.              with an undefined one.  This routine may be called with a
  1399.              hash table other than the generic hash table, so we only
  1400.              do this if we are certain that the hash table is a
  1401.              generic one.  */
  1402.           if (info->output_bfd->xvec == abfd->xvec)
  1403.             {
  1404.               if (h->sym == NULL
  1405.                   || (! bfd_is_und_section (bfd_get_section (p))
  1406.                       && (! bfd_is_com_section (bfd_get_section (p))
  1407.                           || bfd_is_und_section (bfd_get_section (h->sym)))))
  1408.                 {
  1409.                   h->sym = p;
  1410.                   /* BSF_OLD_COMMON is a hack to support COFF reloc
  1411.                      reading, and it should go away when the COFF
  1412.                      linker is switched to the new version.  */
  1413.                   if (bfd_is_com_section (bfd_get_section (p)))
  1414.                     p->flags |= BSF_OLD_COMMON;
  1415.                 }
  1416.             }
  1417.  
  1418.           /* Store a back pointer from the symbol to the hash
  1419.              table entry for the benefit of relaxation code until
  1420.              it gets rewritten to not use asymbol structures.
  1421.              Setting this is also used to check whether these
  1422.              symbols were set up by the generic linker.  */
  1423.           p->udata.p = h;
  1424.         }
  1425.     }
  1426.  
  1427.   return TRUE;
  1428. }
  1429. /* We use a state table to deal with adding symbols from an object
  1430.    file.  The first index into the state table describes the symbol
  1431.    from the object file.  The second index into the state table is the
  1432.    type of the symbol in the hash table.  */
  1433.  
  1434. /* The symbol from the object file is turned into one of these row
  1435.    values.  */
  1436.  
  1437. enum link_row
  1438. {
  1439.   UNDEF_ROW,            /* Undefined.  */
  1440.   UNDEFW_ROW,           /* Weak undefined.  */
  1441.   DEF_ROW,              /* Defined.  */
  1442.   DEFW_ROW,             /* Weak defined.  */
  1443.   COMMON_ROW,           /* Common.  */
  1444.   INDR_ROW,             /* Indirect.  */
  1445.   WARN_ROW,             /* Warning.  */
  1446.   SET_ROW               /* Member of set.  */
  1447. };
  1448.  
  1449. /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
  1450. #undef FAIL
  1451.  
  1452. /* The actions to take in the state table.  */
  1453.  
  1454. enum link_action
  1455. {
  1456.   FAIL,         /* Abort.  */
  1457.   UND,          /* Mark symbol undefined.  */
  1458.   WEAK,         /* Mark symbol weak undefined.  */
  1459.   DEF,          /* Mark symbol defined.  */
  1460.   DEFW,         /* Mark symbol weak defined.  */
  1461.   COM,          /* Mark symbol common.  */
  1462.   REF,          /* Mark defined symbol referenced.  */
  1463.   CREF,         /* Possibly warn about common reference to defined symbol.  */
  1464.   CDEF,         /* Define existing common symbol.  */
  1465.   NOACT,        /* No action.  */
  1466.   BIG,          /* Mark symbol common using largest size.  */
  1467.   MDEF,         /* Multiple definition error.  */
  1468.   MIND,         /* Multiple indirect symbols.  */
  1469.   IND,          /* Make indirect symbol.  */
  1470.   CIND,         /* Make indirect symbol from existing common symbol.  */
  1471.   SET,          /* Add value to set.  */
  1472.   MWARN,        /* Make warning symbol.  */
  1473.   WARN,         /* Issue warning.  */
  1474.   CWARN,        /* Warn if referenced, else MWARN.  */
  1475.   CYCLE,        /* Repeat with symbol pointed to.  */
  1476.   REFC,         /* Mark indirect symbol referenced and then CYCLE.  */
  1477.   WARNC         /* Issue warning and then CYCLE.  */
  1478. };
  1479.  
  1480. /* The state table itself.  The first index is a link_row and the
  1481.    second index is a bfd_link_hash_type.  */
  1482.  
  1483. static const enum link_action link_action[8][8] =
  1484. {
  1485.   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
  1486.   /* UNDEF_ROW  */  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
  1487.   /* UNDEFW_ROW */  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
  1488.   /* DEF_ROW    */  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
  1489.   /* DEFW_ROW   */  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
  1490.   /* COMMON_ROW */  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
  1491.   /* INDR_ROW   */  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
  1492.   /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, NOACT },
  1493.   /* SET_ROW    */  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
  1494. };
  1495.  
  1496. /* Most of the entries in the LINK_ACTION table are straightforward,
  1497.    but a few are somewhat subtle.
  1498.  
  1499.    A reference to an indirect symbol (UNDEF_ROW/indr or
  1500.    UNDEFW_ROW/indr) is counted as a reference both to the indirect
  1501.    symbol and to the symbol the indirect symbol points to.
  1502.  
  1503.    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
  1504.    causes the warning to be issued.
  1505.  
  1506.    A common definition of an indirect symbol (COMMON_ROW/indr) is
  1507.    treated as a multiple definition error.  Likewise for an indirect
  1508.    definition of a common symbol (INDR_ROW/com).
  1509.  
  1510.    An indirect definition of a warning (INDR_ROW/warn) does not cause
  1511.    the warning to be issued.
  1512.  
  1513.    If a warning is created for an indirect symbol (WARN_ROW/indr) no
  1514.    warning is created for the symbol the indirect symbol points to.
  1515.  
  1516.    Adding an entry to a set does not count as a reference to a set,
  1517.    and no warning is issued (SET_ROW/warn).  */
  1518.  
  1519. /* Return the BFD in which a hash entry has been defined, if known.  */
  1520.  
  1521. static bfd *
  1522. hash_entry_bfd (struct bfd_link_hash_entry *h)
  1523. {
  1524.   while (h->type == bfd_link_hash_warning)
  1525.     h = h->u.i.link;
  1526.   switch (h->type)
  1527.     {
  1528.     default:
  1529.       return NULL;
  1530.     case bfd_link_hash_undefined:
  1531.     case bfd_link_hash_undefweak:
  1532.       return h->u.undef.abfd;
  1533.     case bfd_link_hash_defined:
  1534.     case bfd_link_hash_defweak:
  1535.       return h->u.def.section->owner;
  1536.     case bfd_link_hash_common:
  1537.       return h->u.c.p->section->owner;
  1538.     }
  1539.   /*NOTREACHED*/
  1540. }
  1541.  
  1542. /* Add a symbol to the global hash table.
  1543.    ABFD is the BFD the symbol comes from.
  1544.    NAME is the name of the symbol.
  1545.    FLAGS is the BSF_* bits associated with the symbol.
  1546.    SECTION is the section in which the symbol is defined; this may be
  1547.      bfd_und_section_ptr or bfd_com_section_ptr.
  1548.    VALUE is the value of the symbol, relative to the section.
  1549.    STRING is used for either an indirect symbol, in which case it is
  1550.      the name of the symbol to indirect to, or a warning symbol, in
  1551.      which case it is the warning string.
  1552.    COPY is TRUE if NAME or STRING must be copied into locally
  1553.      allocated memory if they need to be saved.
  1554.    COLLECT is TRUE if we should automatically collect gcc constructor
  1555.      or destructor names as collect2 does.
  1556.    HASHP, if not NULL, is a place to store the created hash table
  1557.      entry; if *HASHP is not NULL, the caller has already looked up
  1558.      the hash table entry, and stored it in *HASHP.  */
  1559.  
  1560. bfd_boolean
  1561. _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
  1562.                                   bfd *abfd,
  1563.                                   const char *name,
  1564.                                   flagword flags,
  1565.                                   asection *section,
  1566.                                   bfd_vma value,
  1567.                                   const char *string,
  1568.                                   bfd_boolean copy,
  1569.                                   bfd_boolean collect,
  1570.                                   struct bfd_link_hash_entry **hashp)
  1571. {
  1572.   enum link_row row;
  1573.   struct bfd_link_hash_entry *h;
  1574.   bfd_boolean cycle;
  1575.  
  1576.   BFD_ASSERT (section != NULL);
  1577.  
  1578.   if (bfd_is_ind_section (section)
  1579.       || (flags & BSF_INDIRECT) != 0)
  1580.     row = INDR_ROW;
  1581.   else if ((flags & BSF_WARNING) != 0)
  1582.     row = WARN_ROW;
  1583.   else if ((flags & BSF_CONSTRUCTOR) != 0)
  1584.     row = SET_ROW;
  1585.   else if (bfd_is_und_section (section))
  1586.     {
  1587.       if ((flags & BSF_WEAK) != 0)
  1588.         row = UNDEFW_ROW;
  1589.       else
  1590.         row = UNDEF_ROW;
  1591.     }
  1592.   else if ((flags & BSF_WEAK) != 0)
  1593.     row = DEFW_ROW;
  1594.   else if (bfd_is_com_section (section))
  1595.     row = COMMON_ROW;
  1596.   else
  1597.     row = DEF_ROW;
  1598.  
  1599.   if (hashp != NULL && *hashp != NULL)
  1600.     h = *hashp;
  1601.   else
  1602.     {
  1603.       if (row == UNDEF_ROW || row == UNDEFW_ROW)
  1604.         h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
  1605.       else
  1606.         h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
  1607.       if (h == NULL)
  1608.         {
  1609.           if (hashp != NULL)
  1610.             *hashp = NULL;
  1611.           return FALSE;
  1612.         }
  1613.     }
  1614.  
  1615.   if (info->notice_all
  1616.       || (info->notice_hash != NULL
  1617.           && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
  1618.     {
  1619.       if (! (*info->callbacks->notice) (info, h,
  1620.                                         abfd, section, value, flags, string))
  1621.         return FALSE;
  1622.     }
  1623.  
  1624.   if (hashp != NULL)
  1625.     *hashp = h;
  1626.  
  1627.   do
  1628.     {
  1629.       enum link_action action;
  1630.  
  1631.       cycle = FALSE;
  1632.       action = link_action[(int) row][(int) h->type];
  1633.       switch (action)
  1634.         {
  1635.         case FAIL:
  1636.           abort ();
  1637.  
  1638.         case NOACT:
  1639.           /* Do nothing.  */
  1640.           break;
  1641.  
  1642.         case UND:
  1643.           /* Make a new undefined symbol.  */
  1644.           h->type = bfd_link_hash_undefined;
  1645.           h->u.undef.abfd = abfd;
  1646.           bfd_link_add_undef (info->hash, h);
  1647.           break;
  1648.  
  1649.         case WEAK:
  1650.           /* Make a new weak undefined symbol.  */
  1651.           h->type = bfd_link_hash_undefweak;
  1652.           h->u.undef.abfd = abfd;
  1653.           break;
  1654.  
  1655.         case CDEF:
  1656.           /* We have found a definition for a symbol which was
  1657.              previously common.  */
  1658.           BFD_ASSERT (h->type == bfd_link_hash_common);
  1659.           if (! ((*info->callbacks->multiple_common)
  1660.                  (info, h, abfd, bfd_link_hash_defined, 0)))
  1661.             return FALSE;
  1662.           /* Fall through.  */
  1663.         case DEF:
  1664.         case DEFW:
  1665.           {
  1666.             enum bfd_link_hash_type oldtype;
  1667.  
  1668.             /* Define a symbol.  */
  1669.             oldtype = h->type;
  1670.             if (action == DEFW)
  1671.               h->type = bfd_link_hash_defweak;
  1672.             else
  1673.               h->type = bfd_link_hash_defined;
  1674.             h->u.def.section = section;
  1675.             h->u.def.value = value;
  1676.  
  1677.             /* If we have been asked to, we act like collect2 and
  1678.                identify all functions that might be global
  1679.                constructors and destructors and pass them up in a
  1680.                callback.  We only do this for certain object file
  1681.                types, since many object file types can handle this
  1682.                automatically.  */
  1683.             if (collect && name[0] == '_')
  1684.               {
  1685.                 const char *s;
  1686.  
  1687.                 /* A constructor or destructor name starts like this:
  1688.                    _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
  1689.                    the second are the same character (we accept any
  1690.                    character there, in case a new object file format
  1691.                    comes along with even worse naming restrictions).  */
  1692.  
  1693. #define CONS_PREFIX "GLOBAL_"
  1694. #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
  1695.  
  1696.                 s = name + 1;
  1697.                 while (*s == '_')
  1698.                   ++s;
  1699.                 if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
  1700.                   {
  1701.                     char c;
  1702.  
  1703.                     c = s[CONS_PREFIX_LEN + 1];
  1704.                     if ((c == 'I' || c == 'D')
  1705.                         && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
  1706.                       {
  1707.                         /* If this is a definition of a symbol which
  1708.                            was previously weakly defined, we are in
  1709.                            trouble.  We have already added a
  1710.                            constructor entry for the weak defined
  1711.                            symbol, and now we are trying to add one
  1712.                            for the new symbol.  Fortunately, this case
  1713.                            should never arise in practice.  */
  1714.                         if (oldtype == bfd_link_hash_defweak)
  1715.                           abort ();
  1716.  
  1717.                         if (! ((*info->callbacks->constructor)
  1718.                                (info, c == 'I',
  1719.                                 h->root.string, abfd, section, value)))
  1720.                           return FALSE;
  1721.                       }
  1722.                   }
  1723.               }
  1724.           }
  1725.  
  1726.           break;
  1727.  
  1728.         case COM:
  1729.           /* We have found a common definition for a symbol.  */
  1730.           if (h->type == bfd_link_hash_new)
  1731.             bfd_link_add_undef (info->hash, h);
  1732.           h->type = bfd_link_hash_common;
  1733.           h->u.c.p = (struct bfd_link_hash_common_entry *)
  1734.             bfd_hash_allocate (&info->hash->table,
  1735.                                sizeof (struct bfd_link_hash_common_entry));
  1736.           if (h->u.c.p == NULL)
  1737.             return FALSE;
  1738.  
  1739.           h->u.c.size = value;
  1740.  
  1741.           /* Select a default alignment based on the size.  This may
  1742.              be overridden by the caller.  */
  1743.           {
  1744.             unsigned int power;
  1745.  
  1746.             power = bfd_log2 (value);
  1747.             if (power > 4)
  1748.               power = 4;
  1749.             h->u.c.p->alignment_power = power;
  1750.           }
  1751.  
  1752.           /* The section of a common symbol is only used if the common
  1753.              symbol is actually allocated.  It basically provides a
  1754.              hook for the linker script to decide which output section
  1755.              the common symbols should be put in.  In most cases, the
  1756.              section of a common symbol will be bfd_com_section_ptr,
  1757.              the code here will choose a common symbol section named
  1758.              "COMMON", and the linker script will contain *(COMMON) in
  1759.              the appropriate place.  A few targets use separate common
  1760.              sections for small symbols, and they require special
  1761.              handling.  */
  1762.           if (section == bfd_com_section_ptr)
  1763.             {
  1764.               h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
  1765.               h->u.c.p->section->flags |= SEC_ALLOC;
  1766.             }
  1767.           else if (section->owner != abfd)
  1768.             {
  1769.               h->u.c.p->section = bfd_make_section_old_way (abfd,
  1770.                                                             section->name);
  1771.               h->u.c.p->section->flags |= SEC_ALLOC;
  1772.             }
  1773.           else
  1774.             h->u.c.p->section = section;
  1775.           break;
  1776.  
  1777.         case REF:
  1778.           /* A reference to a defined symbol.  */
  1779.           if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
  1780.             h->u.undef.next = h;
  1781.           break;
  1782.  
  1783.         case BIG:
  1784.           /* We have found a common definition for a symbol which
  1785.              already had a common definition.  Use the maximum of the
  1786.              two sizes, and use the section required by the larger symbol.  */
  1787.           BFD_ASSERT (h->type == bfd_link_hash_common);
  1788.           if (! ((*info->callbacks->multiple_common)
  1789.                  (info, h, abfd, bfd_link_hash_common, value)))
  1790.             return FALSE;
  1791.           if (value > h->u.c.size)
  1792.             {
  1793.               unsigned int power;
  1794.  
  1795.               h->u.c.size = value;
  1796.  
  1797.               /* Select a default alignment based on the size.  This may
  1798.                  be overridden by the caller.  */
  1799.               power = bfd_log2 (value);
  1800.               if (power > 4)
  1801.                 power = 4;
  1802.               h->u.c.p->alignment_power = power;
  1803.  
  1804.               /* Some systems have special treatment for small commons,
  1805.                  hence we want to select the section used by the larger
  1806.                  symbol.  This makes sure the symbol does not go in a
  1807.                  small common section if it is now too large.  */
  1808.               if (section == bfd_com_section_ptr)
  1809.                 {
  1810.                   h->u.c.p->section
  1811.                     = bfd_make_section_old_way (abfd, "COMMON");
  1812.                   h->u.c.p->section->flags |= SEC_ALLOC;
  1813.                 }
  1814.               else if (section->owner != abfd)
  1815.                 {
  1816.                   h->u.c.p->section
  1817.                     = bfd_make_section_old_way (abfd, section->name);
  1818.                   h->u.c.p->section->flags |= SEC_ALLOC;
  1819.                 }
  1820.               else
  1821.                 h->u.c.p->section = section;
  1822.             }
  1823.           break;
  1824.  
  1825.         case CREF:
  1826.           /* We have found a common definition for a symbol which
  1827.              was already defined.  */
  1828.           if (! ((*info->callbacks->multiple_common)
  1829.                  (info, h, abfd, bfd_link_hash_common, value)))
  1830.             return FALSE;
  1831.           break;
  1832.  
  1833.         case MIND:
  1834.           /* Multiple indirect symbols.  This is OK if they both point
  1835.              to the same symbol.  */
  1836.           if (strcmp (h->u.i.link->root.string, string) == 0)
  1837.             break;
  1838.           /* Fall through.  */
  1839.         case MDEF:
  1840.           /* Handle a multiple definition.  */
  1841.           if (! ((*info->callbacks->multiple_definition)
  1842.                  (info, h, abfd, section, value)))
  1843.             return FALSE;
  1844.           break;
  1845.  
  1846.         case CIND:
  1847.           /* Create an indirect symbol from an existing common symbol.  */
  1848.           BFD_ASSERT (h->type == bfd_link_hash_common);
  1849.           if (! ((*info->callbacks->multiple_common)
  1850.                  (info, h, abfd, bfd_link_hash_indirect, 0)))
  1851.             return FALSE;
  1852.           /* Fall through.  */
  1853.         case IND:
  1854.           /* Create an indirect symbol.  */
  1855.           {
  1856.             struct bfd_link_hash_entry *inh;
  1857.  
  1858.             /* STRING is the name of the symbol we want to indirect
  1859.                to.  */
  1860.             inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
  1861.                                                 copy, FALSE);
  1862.             if (inh == NULL)
  1863.               return FALSE;
  1864.             if (inh->type == bfd_link_hash_indirect
  1865.                 && inh->u.i.link == h)
  1866.               {
  1867.                 (*_bfd_error_handler)
  1868.                   (_("%B: indirect symbol `%s' to `%s' is a loop"),
  1869.                    abfd, name, string);
  1870.                 bfd_set_error (bfd_error_invalid_operation);
  1871.                 return FALSE;
  1872.               }
  1873.             if (inh->type == bfd_link_hash_new)
  1874.               {
  1875.                 inh->type = bfd_link_hash_undefined;
  1876.                 inh->u.undef.abfd = abfd;
  1877.                 bfd_link_add_undef (info->hash, inh);
  1878.               }
  1879.  
  1880.             /* If the indirect symbol has been referenced, we need to
  1881.                push the reference down to the symbol we are
  1882.                referencing.  */
  1883.             if (h->type != bfd_link_hash_new)
  1884.               {
  1885.                 row = UNDEF_ROW;
  1886.                 cycle = TRUE;
  1887.               }
  1888.  
  1889.             h->type = bfd_link_hash_indirect;
  1890.             h->u.i.link = inh;
  1891.           }
  1892.           break;
  1893.  
  1894.         case SET:
  1895.           /* Add an entry to a set.  */
  1896.           if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
  1897.                                                 abfd, section, value))
  1898.             return FALSE;
  1899.           break;
  1900.  
  1901.         case WARNC:
  1902.           /* Issue a warning and cycle.  */
  1903.           if (h->u.i.warning != NULL)
  1904.             {
  1905.               if (! (*info->callbacks->warning) (info, h->u.i.warning,
  1906.                                                  h->root.string, abfd,
  1907.                                                  NULL, 0))
  1908.                 return FALSE;
  1909.               /* Only issue a warning once.  */
  1910.               h->u.i.warning = NULL;
  1911.             }
  1912.           /* Fall through.  */
  1913.         case CYCLE:
  1914.           /* Try again with the referenced symbol.  */
  1915.           h = h->u.i.link;
  1916.           cycle = TRUE;
  1917.           break;
  1918.  
  1919.         case REFC:
  1920.           /* A reference to an indirect symbol.  */
  1921.           if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
  1922.             h->u.undef.next = h;
  1923.           h = h->u.i.link;
  1924.           cycle = TRUE;
  1925.           break;
  1926.  
  1927.         case WARN:
  1928.           /* Issue a warning.  */
  1929.           if (! (*info->callbacks->warning) (info, string, h->root.string,
  1930.                                              hash_entry_bfd (h), NULL, 0))
  1931.             return FALSE;
  1932.           break;
  1933.  
  1934.         case CWARN:
  1935.           /* Warn if this symbol has been referenced already,
  1936.              otherwise add a warning.  A symbol has been referenced if
  1937.              the u.undef.next field is not NULL, or it is the tail of the
  1938.              undefined symbol list.  The REF case above helps to
  1939.              ensure this.  */
  1940.           if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
  1941.             {
  1942.               if (! (*info->callbacks->warning) (info, string, h->root.string,
  1943.                                                  hash_entry_bfd (h), NULL, 0))
  1944.                 return FALSE;
  1945.               break;
  1946.             }
  1947.           /* Fall through.  */
  1948.         case MWARN:
  1949.           /* Make a warning symbol.  */
  1950.           {
  1951.             struct bfd_link_hash_entry *sub;
  1952.  
  1953.             /* STRING is the warning to give.  */
  1954.             sub = ((struct bfd_link_hash_entry *)
  1955.                    ((*info->hash->table.newfunc)
  1956.                     (NULL, &info->hash->table, h->root.string)));
  1957.             if (sub == NULL)
  1958.               return FALSE;
  1959.             *sub = *h;
  1960.             sub->type = bfd_link_hash_warning;
  1961.             sub->u.i.link = h;
  1962.             if (! copy)
  1963.               sub->u.i.warning = string;
  1964.             else
  1965.               {
  1966.                 char *w;
  1967.                 size_t len = strlen (string) + 1;
  1968.  
  1969.                 w = (char *) bfd_hash_allocate (&info->hash->table, len);
  1970.                 if (w == NULL)
  1971.                   return FALSE;
  1972.                 memcpy (w, string, len);
  1973.                 sub->u.i.warning = w;
  1974.               }
  1975.  
  1976.             bfd_hash_replace (&info->hash->table,
  1977.                               (struct bfd_hash_entry *) h,
  1978.                               (struct bfd_hash_entry *) sub);
  1979.             if (hashp != NULL)
  1980.               *hashp = sub;
  1981.           }
  1982.           break;
  1983.         }
  1984.     }
  1985.   while (cycle);
  1986.  
  1987.   return TRUE;
  1988. }
  1989. /* Generic final link routine.  */
  1990.  
  1991. bfd_boolean
  1992. _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
  1993. {
  1994.   bfd *sub;
  1995.   asection *o;
  1996.   struct bfd_link_order *p;
  1997.   size_t outsymalloc;
  1998.   struct generic_write_global_symbol_info wginfo;
  1999.  
  2000.   bfd_get_outsymbols (abfd) = NULL;
  2001.   bfd_get_symcount (abfd) = 0;
  2002.   outsymalloc = 0;
  2003.  
  2004.   /* Mark all sections which will be included in the output file.  */
  2005.   for (o = abfd->sections; o != NULL; o = o->next)
  2006.     for (p = o->map_head.link_order; p != NULL; p = p->next)
  2007.       if (p->type == bfd_indirect_link_order)
  2008.         p->u.indirect.section->linker_mark = TRUE;
  2009.  
  2010.   /* Build the output symbol table.  */
  2011.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  2012.     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
  2013.       return FALSE;
  2014.  
  2015.   /* Accumulate the global symbols.  */
  2016.   wginfo.info = info;
  2017.   wginfo.output_bfd = abfd;
  2018.   wginfo.psymalloc = &outsymalloc;
  2019.   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
  2020.                                    _bfd_generic_link_write_global_symbol,
  2021.                                    &wginfo);
  2022.  
  2023.   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
  2024.      shouldn't really need one, since we have SYMCOUNT, but some old
  2025.      code still expects one.  */
  2026.   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
  2027.     return FALSE;
  2028.  
  2029.   if (info->relocatable)
  2030.     {
  2031.       /* Allocate space for the output relocs for each section.  */
  2032.       for (o = abfd->sections; o != NULL; o = o->next)
  2033.         {
  2034.           o->reloc_count = 0;
  2035.           for (p = o->map_head.link_order; p != NULL; p = p->next)
  2036.             {
  2037.               if (p->type == bfd_section_reloc_link_order
  2038.                   || p->type == bfd_symbol_reloc_link_order)
  2039.                 ++o->reloc_count;
  2040.               else if (p->type == bfd_indirect_link_order)
  2041.                 {
  2042.                   asection *input_section;
  2043.                   bfd *input_bfd;
  2044.                   long relsize;
  2045.                   arelent **relocs;
  2046.                   asymbol **symbols;
  2047.                   long reloc_count;
  2048.  
  2049.                   input_section = p->u.indirect.section;
  2050.                   input_bfd = input_section->owner;
  2051.                   relsize = bfd_get_reloc_upper_bound (input_bfd,
  2052.                                                        input_section);
  2053.                   if (relsize < 0)
  2054.                     return FALSE;
  2055.                   relocs = (arelent **) bfd_malloc (relsize);
  2056.                   if (!relocs && relsize != 0)
  2057.                     return FALSE;
  2058.                   symbols = _bfd_generic_link_get_symbols (input_bfd);
  2059.                   reloc_count = bfd_canonicalize_reloc (input_bfd,
  2060.                                                         input_section,
  2061.                                                         relocs,
  2062.                                                         symbols);
  2063.                   free (relocs);
  2064.                   if (reloc_count < 0)
  2065.                     return FALSE;
  2066.                   BFD_ASSERT ((unsigned long) reloc_count
  2067.                               == input_section->reloc_count);
  2068.                   o->reloc_count += reloc_count;
  2069.                 }
  2070.             }
  2071.           if (o->reloc_count > 0)
  2072.             {
  2073.               bfd_size_type amt;
  2074.  
  2075.               amt = o->reloc_count;
  2076.               amt *= sizeof (arelent *);
  2077.               o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
  2078.               if (!o->orelocation)
  2079.                 return FALSE;
  2080.               o->flags |= SEC_RELOC;
  2081.               /* Reset the count so that it can be used as an index
  2082.                  when putting in the output relocs.  */
  2083.               o->reloc_count = 0;
  2084.             }
  2085.         }
  2086.     }
  2087.  
  2088.   /* Handle all the link order information for the sections.  */
  2089.   for (o = abfd->sections; o != NULL; o = o->next)
  2090.     {
  2091.       for (p = o->map_head.link_order; p != NULL; p = p->next)
  2092.         {
  2093.           switch (p->type)
  2094.             {
  2095.             case bfd_section_reloc_link_order:
  2096.             case bfd_symbol_reloc_link_order:
  2097.               if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
  2098.                 return FALSE;
  2099.               break;
  2100.             case bfd_indirect_link_order:
  2101.               if (! default_indirect_link_order (abfd, info, o, p, TRUE))
  2102.                 return FALSE;
  2103.               break;
  2104.             default:
  2105.               if (! _bfd_default_link_order (abfd, info, o, p))
  2106.                 return FALSE;
  2107.               break;
  2108.             }
  2109.         }
  2110.     }
  2111.  
  2112.   return TRUE;
  2113. }
  2114.  
  2115. /* Add an output symbol to the output BFD.  */
  2116.  
  2117. static bfd_boolean
  2118. generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
  2119. {
  2120.   if (bfd_get_symcount (output_bfd) >= *psymalloc)
  2121.     {
  2122.       asymbol **newsyms;
  2123.       bfd_size_type amt;
  2124.  
  2125.       if (*psymalloc == 0)
  2126.         *psymalloc = 124;
  2127.       else
  2128.         *psymalloc *= 2;
  2129.       amt = *psymalloc;
  2130.       amt *= sizeof (asymbol *);
  2131.       newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
  2132.       if (newsyms == NULL)
  2133.         return FALSE;
  2134.       bfd_get_outsymbols (output_bfd) = newsyms;
  2135.     }
  2136.  
  2137.   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
  2138.   if (sym != NULL)
  2139.     ++ bfd_get_symcount (output_bfd);
  2140.  
  2141.   return TRUE;
  2142. }
  2143.  
  2144. /* Handle the symbols for an input BFD.  */
  2145.  
  2146. bfd_boolean
  2147. _bfd_generic_link_output_symbols (bfd *output_bfd,
  2148.                                   bfd *input_bfd,
  2149.                                   struct bfd_link_info *info,
  2150.                                   size_t *psymalloc)
  2151. {
  2152.   asymbol **sym_ptr;
  2153.   asymbol **sym_end;
  2154.  
  2155.   if (!bfd_generic_link_read_symbols (input_bfd))
  2156.     return FALSE;
  2157.  
  2158.   /* Create a filename symbol if we are supposed to.  */
  2159.   if (info->create_object_symbols_section != NULL)
  2160.     {
  2161.       asection *sec;
  2162.  
  2163.       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
  2164.         {
  2165.           if (sec->output_section == info->create_object_symbols_section)
  2166.             {
  2167.               asymbol *newsym;
  2168.  
  2169.               newsym = bfd_make_empty_symbol (input_bfd);
  2170.               if (!newsym)
  2171.                 return FALSE;
  2172.               newsym->name = input_bfd->filename;
  2173.               newsym->value = 0;
  2174.               newsym->flags = BSF_LOCAL | BSF_FILE;
  2175.               newsym->section = sec;
  2176.  
  2177.               if (! generic_add_output_symbol (output_bfd, psymalloc,
  2178.                                                newsym))
  2179.                 return FALSE;
  2180.  
  2181.               break;
  2182.             }
  2183.         }
  2184.     }
  2185.  
  2186.   /* Adjust the values of the globally visible symbols, and write out
  2187.      local symbols.  */
  2188.   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
  2189.   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
  2190.   for (; sym_ptr < sym_end; sym_ptr++)
  2191.     {
  2192.       asymbol *sym;
  2193.       struct generic_link_hash_entry *h;
  2194.       bfd_boolean output;
  2195.  
  2196.       h = NULL;
  2197.       sym = *sym_ptr;
  2198.       if ((sym->flags & (BSF_INDIRECT
  2199.                          | BSF_WARNING
  2200.                          | BSF_GLOBAL
  2201.                          | BSF_CONSTRUCTOR
  2202.                          | BSF_WEAK)) != 0
  2203.           || bfd_is_und_section (bfd_get_section (sym))
  2204.           || bfd_is_com_section (bfd_get_section (sym))
  2205.           || bfd_is_ind_section (bfd_get_section (sym)))
  2206.         {
  2207.           if (sym->udata.p != NULL)
  2208.             h = (struct generic_link_hash_entry *) sym->udata.p;
  2209.           else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
  2210.             {
  2211.               /* This case normally means that the main linker code
  2212.                  deliberately ignored this constructor symbol.  We
  2213.                  should just pass it through.  This will screw up if
  2214.                  the constructor symbol is from a different,
  2215.                  non-generic, object file format, but the case will
  2216.                  only arise when linking with -r, which will probably
  2217.                  fail anyhow, since there will be no way to represent
  2218.                  the relocs in the output format being used.  */
  2219.               h = NULL;
  2220.             }
  2221.           else if (bfd_is_und_section (bfd_get_section (sym)))
  2222.             h = ((struct generic_link_hash_entry *)
  2223.                  bfd_wrapped_link_hash_lookup (output_bfd, info,
  2224.                                                bfd_asymbol_name (sym),
  2225.                                                FALSE, FALSE, TRUE));
  2226.           else
  2227.             h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
  2228.                                                bfd_asymbol_name (sym),
  2229.                                                FALSE, FALSE, TRUE);
  2230.  
  2231.           if (h != NULL)
  2232.             {
  2233.               /* Force all references to this symbol to point to
  2234.                  the same area in memory.  It is possible that
  2235.                  this routine will be called with a hash table
  2236.                  other than a generic hash table, so we double
  2237.                  check that.  */
  2238.               if (info->output_bfd->xvec == input_bfd->xvec)
  2239.                 {
  2240.                   if (h->sym != NULL)
  2241.                     *sym_ptr = sym = h->sym;
  2242.                 }
  2243.  
  2244.               switch (h->root.type)
  2245.                 {
  2246.                 default:
  2247.                 case bfd_link_hash_new:
  2248.                   abort ();
  2249.                 case bfd_link_hash_undefined:
  2250.                   break;
  2251.                 case bfd_link_hash_undefweak:
  2252.                   sym->flags |= BSF_WEAK;
  2253.                   break;
  2254.                 case bfd_link_hash_indirect:
  2255.                   h = (struct generic_link_hash_entry *) h->root.u.i.link;
  2256.                   /* fall through */
  2257.                 case bfd_link_hash_defined:
  2258.                   sym->flags |= BSF_GLOBAL;
  2259.                   sym->flags &=~ BSF_CONSTRUCTOR;
  2260.                   sym->value = h->root.u.def.value;
  2261.                   sym->section = h->root.u.def.section;
  2262.                   break;
  2263.                 case bfd_link_hash_defweak:
  2264.                   sym->flags |= BSF_WEAK;
  2265.                   sym->flags &=~ BSF_CONSTRUCTOR;
  2266.                   sym->value = h->root.u.def.value;
  2267.                   sym->section = h->root.u.def.section;
  2268.                   break;
  2269.                 case bfd_link_hash_common:
  2270.                   sym->value = h->root.u.c.size;
  2271.                   sym->flags |= BSF_GLOBAL;
  2272.                   if (! bfd_is_com_section (sym->section))
  2273.                     {
  2274.                       BFD_ASSERT (bfd_is_und_section (sym->section));
  2275.                       sym->section = bfd_com_section_ptr;
  2276.                     }
  2277.                   /* We do not set the section of the symbol to
  2278.                      h->root.u.c.p->section.  That value was saved so
  2279.                      that we would know where to allocate the symbol
  2280.                      if it was defined.  In this case the type is
  2281.                      still bfd_link_hash_common, so we did not define
  2282.                      it, so we do not want to use that section.  */
  2283.                   break;
  2284.                 }
  2285.             }
  2286.         }
  2287.  
  2288.       /* This switch is straight from the old code in
  2289.          write_file_locals in ldsym.c.  */
  2290.       if (info->strip == strip_all
  2291.           || (info->strip == strip_some
  2292.               && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
  2293.                                   FALSE, FALSE) == NULL))
  2294.         output = FALSE;
  2295.       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
  2296.         {
  2297.           /* If this symbol is marked as occurring now, rather
  2298.              than at the end, output it now.  This is used for
  2299.              COFF C_EXT FCN symbols.  FIXME: There must be a
  2300.              better way.  */
  2301.           if (bfd_asymbol_bfd (sym) == input_bfd
  2302.               && (sym->flags & BSF_NOT_AT_END) != 0)
  2303.             output = TRUE;
  2304.           else
  2305.             output = FALSE;
  2306.         }
  2307.       else if (bfd_is_ind_section (sym->section))
  2308.         output = FALSE;
  2309.       else if ((sym->flags & BSF_DEBUGGING) != 0)
  2310.         {
  2311.           if (info->strip == strip_none)
  2312.             output = TRUE;
  2313.           else
  2314.             output = FALSE;
  2315.         }
  2316.       else if (bfd_is_und_section (sym->section)
  2317.                || bfd_is_com_section (sym->section))
  2318.         output = FALSE;
  2319.       else if ((sym->flags & BSF_LOCAL) != 0)
  2320.         {
  2321.           if ((sym->flags & BSF_WARNING) != 0)
  2322.             output = FALSE;
  2323.           else
  2324.             {
  2325.               switch (info->discard)
  2326.                 {
  2327.                 default:
  2328.                 case discard_all:
  2329.                   output = FALSE;
  2330.                   break;
  2331.                 case discard_sec_merge:
  2332.                   output = TRUE;
  2333.                   if (info->relocatable
  2334.                       || ! (sym->section->flags & SEC_MERGE))
  2335.                     break;
  2336.                   /* FALLTHROUGH */
  2337.                 case discard_l:
  2338.                   if (bfd_is_local_label (input_bfd, sym))
  2339.                     output = FALSE;
  2340.                   else
  2341.                     output = TRUE;
  2342.                   break;
  2343.                 case discard_none:
  2344.                   output = TRUE;
  2345.                   break;
  2346.                 }
  2347.             }
  2348.         }
  2349.       else if ((sym->flags & BSF_CONSTRUCTOR))
  2350.         {
  2351.           if (info->strip != strip_all)
  2352.             output = TRUE;
  2353.           else
  2354.             output = FALSE;
  2355.         }
  2356.       else if (sym->flags == 0
  2357.                && (sym->section->owner->flags & BFD_PLUGIN) != 0)
  2358.         /* LTO doesn't set symbol information.  We get here with the
  2359.            generic linker for a symbol that was "common" but no longer
  2360.            needs to be global.  */
  2361.         output = FALSE;
  2362.       else
  2363.         abort ();
  2364.  
  2365.       /* If this symbol is in a section which is not being included
  2366.          in the output file, then we don't want to output the
  2367.          symbol.  */
  2368.       if (!bfd_is_abs_section (sym->section)
  2369.           && bfd_section_removed_from_list (output_bfd,
  2370.                                             sym->section->output_section))
  2371.         output = FALSE;
  2372.  
  2373.       if (output)
  2374.         {
  2375.           if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
  2376.             return FALSE;
  2377.           if (h != NULL)
  2378.             h->written = TRUE;
  2379.         }
  2380.     }
  2381.  
  2382.   return TRUE;
  2383. }
  2384.  
  2385. /* Set the section and value of a generic BFD symbol based on a linker
  2386.    hash table entry.  */
  2387.  
  2388. static void
  2389. set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
  2390. {
  2391.   switch (h->type)
  2392.     {
  2393.     default:
  2394.       abort ();
  2395.       break;
  2396.     case bfd_link_hash_new:
  2397.       /* This can happen when a constructor symbol is seen but we are
  2398.          not building constructors.  */
  2399.       if (sym->section != NULL)
  2400.         {
  2401.           BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
  2402.         }
  2403.       else
  2404.         {
  2405.           sym->flags |= BSF_CONSTRUCTOR;
  2406.           sym->section = bfd_abs_section_ptr;
  2407.           sym->value = 0;
  2408.         }
  2409.       break;
  2410.     case bfd_link_hash_undefined:
  2411.       sym->section = bfd_und_section_ptr;
  2412.       sym->value = 0;
  2413.       break;
  2414.     case bfd_link_hash_undefweak:
  2415.       sym->section = bfd_und_section_ptr;
  2416.       sym->value = 0;
  2417.       sym->flags |= BSF_WEAK;
  2418.       break;
  2419.     case bfd_link_hash_defined:
  2420.       sym->section = h->u.def.section;
  2421.       sym->value = h->u.def.value;
  2422.       break;
  2423.     case bfd_link_hash_defweak:
  2424.       sym->flags |= BSF_WEAK;
  2425.       sym->section = h->u.def.section;
  2426.       sym->value = h->u.def.value;
  2427.       break;
  2428.     case bfd_link_hash_common:
  2429.       sym->value = h->u.c.size;
  2430.       if (sym->section == NULL)
  2431.         sym->section = bfd_com_section_ptr;
  2432.       else if (! bfd_is_com_section (sym->section))
  2433.         {
  2434.           BFD_ASSERT (bfd_is_und_section (sym->section));
  2435.           sym->section = bfd_com_section_ptr;
  2436.         }
  2437.       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
  2438.       break;
  2439.     case bfd_link_hash_indirect:
  2440.     case bfd_link_hash_warning:
  2441.       /* FIXME: What should we do here?  */
  2442.       break;
  2443.     }
  2444. }
  2445.  
  2446. /* Write out a global symbol, if it hasn't already been written out.
  2447.    This is called for each symbol in the hash table.  */
  2448.  
  2449. bfd_boolean
  2450. _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
  2451.                                        void *data)
  2452. {
  2453.   struct generic_write_global_symbol_info *wginfo =
  2454.       (struct generic_write_global_symbol_info *) data;
  2455.   asymbol *sym;
  2456.  
  2457.   if (h->written)
  2458.     return TRUE;
  2459.  
  2460.   h->written = TRUE;
  2461.  
  2462.   if (wginfo->info->strip == strip_all
  2463.       || (wginfo->info->strip == strip_some
  2464.           && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
  2465.                               FALSE, FALSE) == NULL))
  2466.     return TRUE;
  2467.  
  2468.   if (h->sym != NULL)
  2469.     sym = h->sym;
  2470.   else
  2471.     {
  2472.       sym = bfd_make_empty_symbol (wginfo->output_bfd);
  2473.       if (!sym)
  2474.         return FALSE;
  2475.       sym->name = h->root.root.string;
  2476.       sym->flags = 0;
  2477.     }
  2478.  
  2479.   set_symbol_from_hash (sym, &h->root);
  2480.  
  2481.   sym->flags |= BSF_GLOBAL;
  2482.  
  2483.   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
  2484.                                    sym))
  2485.     {
  2486.       /* FIXME: No way to return failure.  */
  2487.       abort ();
  2488.     }
  2489.  
  2490.   return TRUE;
  2491. }
  2492.  
  2493. /* Create a relocation.  */
  2494.  
  2495. bfd_boolean
  2496. _bfd_generic_reloc_link_order (bfd *abfd,
  2497.                                struct bfd_link_info *info,
  2498.                                asection *sec,
  2499.                                struct bfd_link_order *link_order)
  2500. {
  2501.   arelent *r;
  2502.  
  2503.   if (! info->relocatable)
  2504.     abort ();
  2505.   if (sec->orelocation == NULL)
  2506.     abort ();
  2507.  
  2508.   r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
  2509.   if (r == NULL)
  2510.     return FALSE;
  2511.  
  2512.   r->address = link_order->offset;
  2513.   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
  2514.   if (r->howto == 0)
  2515.     {
  2516.       bfd_set_error (bfd_error_bad_value);
  2517.       return FALSE;
  2518.     }
  2519.  
  2520.   /* Get the symbol to use for the relocation.  */
  2521.   if (link_order->type == bfd_section_reloc_link_order)
  2522.     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
  2523.   else
  2524.     {
  2525.       struct generic_link_hash_entry *h;
  2526.  
  2527.       h = ((struct generic_link_hash_entry *)
  2528.            bfd_wrapped_link_hash_lookup (abfd, info,
  2529.                                          link_order->u.reloc.p->u.name,
  2530.                                          FALSE, FALSE, TRUE));
  2531.       if (h == NULL
  2532.           || ! h->written)
  2533.         {
  2534.           if (! ((*info->callbacks->unattached_reloc)
  2535.                  (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
  2536.             return FALSE;
  2537.           bfd_set_error (bfd_error_bad_value);
  2538.           return FALSE;
  2539.         }
  2540.       r->sym_ptr_ptr = &h->sym;
  2541.     }
  2542.  
  2543.   /* If this is an inplace reloc, write the addend to the object file.
  2544.      Otherwise, store it in the reloc addend.  */
  2545.   if (! r->howto->partial_inplace)
  2546.     r->addend = link_order->u.reloc.p->addend;
  2547.   else
  2548.     {
  2549.       bfd_size_type size;
  2550.       bfd_reloc_status_type rstat;
  2551.       bfd_byte *buf;
  2552.       bfd_boolean ok;
  2553.       file_ptr loc;
  2554.  
  2555.       size = bfd_get_reloc_size (r->howto);
  2556.       buf = (bfd_byte *) bfd_zmalloc (size);
  2557.       if (buf == NULL)
  2558.         return FALSE;
  2559.       rstat = _bfd_relocate_contents (r->howto, abfd,
  2560.                                       (bfd_vma) link_order->u.reloc.p->addend,
  2561.                                       buf);
  2562.       switch (rstat)
  2563.         {
  2564.         case bfd_reloc_ok:
  2565.           break;
  2566.         default:
  2567.         case bfd_reloc_outofrange:
  2568.           abort ();
  2569.         case bfd_reloc_overflow:
  2570.           if (! ((*info->callbacks->reloc_overflow)
  2571.                  (info, NULL,
  2572.                   (link_order->type == bfd_section_reloc_link_order
  2573.                    ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
  2574.                    : link_order->u.reloc.p->u.name),
  2575.                   r->howto->name, link_order->u.reloc.p->addend,
  2576.                   NULL, NULL, 0)))
  2577.             {
  2578.               free (buf);
  2579.               return FALSE;
  2580.             }
  2581.           break;
  2582.         }
  2583.       loc = link_order->offset * bfd_octets_per_byte (abfd);
  2584.       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
  2585.       free (buf);
  2586.       if (! ok)
  2587.         return FALSE;
  2588.  
  2589.       r->addend = 0;
  2590.     }
  2591.  
  2592.   sec->orelocation[sec->reloc_count] = r;
  2593.   ++sec->reloc_count;
  2594.  
  2595.   return TRUE;
  2596. }
  2597. /* Allocate a new link_order for a section.  */
  2598.  
  2599. struct bfd_link_order *
  2600. bfd_new_link_order (bfd *abfd, asection *section)
  2601. {
  2602.   bfd_size_type amt = sizeof (struct bfd_link_order);
  2603.   struct bfd_link_order *new_lo;
  2604.  
  2605.   new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
  2606.   if (!new_lo)
  2607.     return NULL;
  2608.  
  2609.   new_lo->type = bfd_undefined_link_order;
  2610.  
  2611.   if (section->map_tail.link_order != NULL)
  2612.     section->map_tail.link_order->next = new_lo;
  2613.   else
  2614.     section->map_head.link_order = new_lo;
  2615.   section->map_tail.link_order = new_lo;
  2616.  
  2617.   return new_lo;
  2618. }
  2619.  
  2620. /* Default link order processing routine.  Note that we can not handle
  2621.    the reloc_link_order types here, since they depend upon the details
  2622.    of how the particular backends generates relocs.  */
  2623.  
  2624. bfd_boolean
  2625. _bfd_default_link_order (bfd *abfd,
  2626.                          struct bfd_link_info *info,
  2627.                          asection *sec,
  2628.                          struct bfd_link_order *link_order)
  2629. {
  2630.   switch (link_order->type)
  2631.     {
  2632.     case bfd_undefined_link_order:
  2633.     case bfd_section_reloc_link_order:
  2634.     case bfd_symbol_reloc_link_order:
  2635.     default:
  2636.       abort ();
  2637.     case bfd_indirect_link_order:
  2638.       return default_indirect_link_order (abfd, info, sec, link_order,
  2639.                                           FALSE);
  2640.     case bfd_data_link_order:
  2641.       return default_data_link_order (abfd, info, sec, link_order);
  2642.     }
  2643. }
  2644.  
  2645. /* Default routine to handle a bfd_data_link_order.  */
  2646.  
  2647. static bfd_boolean
  2648. default_data_link_order (bfd *abfd,
  2649.                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2650.                          asection *sec,
  2651.                          struct bfd_link_order *link_order)
  2652. {
  2653.   bfd_size_type size;
  2654.   size_t fill_size;
  2655.   bfd_byte *fill;
  2656.   file_ptr loc;
  2657.   bfd_boolean result;
  2658.  
  2659.   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
  2660.  
  2661.   size = link_order->size;
  2662.   if (size == 0)
  2663.     return TRUE;
  2664.  
  2665.   fill = link_order->u.data.contents;
  2666.   fill_size = link_order->u.data.size;
  2667.   if (fill_size == 0)
  2668.     {
  2669.       fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
  2670.                                     (sec->flags & SEC_CODE) != 0);
  2671.       if (fill == NULL)
  2672.         return FALSE;
  2673.     }
  2674.   else if (fill_size < size)
  2675.     {
  2676.       bfd_byte *p;
  2677.       fill = (bfd_byte *) bfd_malloc (size);
  2678.       if (fill == NULL)
  2679.         return FALSE;
  2680.       p = fill;
  2681.       if (fill_size == 1)
  2682.         memset (p, (int) link_order->u.data.contents[0], (size_t) size);
  2683.       else
  2684.         {
  2685.           do
  2686.             {
  2687.               memcpy (p, link_order->u.data.contents, fill_size);
  2688.               p += fill_size;
  2689.               size -= fill_size;
  2690.             }
  2691.           while (size >= fill_size);
  2692.           if (size != 0)
  2693.             memcpy (p, link_order->u.data.contents, (size_t) size);
  2694.           size = link_order->size;
  2695.         }
  2696.     }
  2697.  
  2698.   loc = link_order->offset * bfd_octets_per_byte (abfd);
  2699.   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
  2700.  
  2701.   if (fill != link_order->u.data.contents)
  2702.     free (fill);
  2703.   return result;
  2704. }
  2705.  
  2706. /* Default routine to handle a bfd_indirect_link_order.  */
  2707.  
  2708. static bfd_boolean
  2709. default_indirect_link_order (bfd *output_bfd,
  2710.                              struct bfd_link_info *info,
  2711.                              asection *output_section,
  2712.                              struct bfd_link_order *link_order,
  2713.                              bfd_boolean generic_linker)
  2714. {
  2715.   asection *input_section;
  2716.   bfd *input_bfd;
  2717.   bfd_byte *contents = NULL;
  2718.   bfd_byte *new_contents;
  2719.   bfd_size_type sec_size;
  2720.   file_ptr loc;
  2721.  
  2722.   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
  2723.  
  2724.   input_section = link_order->u.indirect.section;
  2725.   input_bfd = input_section->owner;
  2726.   if (input_section->size == 0)
  2727.     return TRUE;
  2728.  
  2729.   BFD_ASSERT (input_section->output_section == output_section);
  2730.   BFD_ASSERT (input_section->output_offset == link_order->offset);
  2731.   BFD_ASSERT (input_section->size == link_order->size);
  2732.  
  2733.   if (info->relocatable
  2734.       && input_section->reloc_count > 0
  2735.       && output_section->orelocation == NULL)
  2736.     {
  2737.       /* Space has not been allocated for the output relocations.
  2738.          This can happen when we are called by a specific backend
  2739.          because somebody is attempting to link together different
  2740.          types of object files.  Handling this case correctly is
  2741.          difficult, and sometimes impossible.  */
  2742.       (*_bfd_error_handler)
  2743.         (_("Attempt to do relocatable link with %s input and %s output"),
  2744.          bfd_get_target (input_bfd), bfd_get_target (output_bfd));
  2745.       bfd_set_error (bfd_error_wrong_format);
  2746.       return FALSE;
  2747.     }
  2748.  
  2749.   if (! generic_linker)
  2750.     {
  2751.       asymbol **sympp;
  2752.       asymbol **symppend;
  2753.  
  2754.       /* Get the canonical symbols.  The generic linker will always
  2755.          have retrieved them by this point, but we are being called by
  2756.          a specific linker, presumably because we are linking
  2757.          different types of object files together.  */
  2758.       if (!bfd_generic_link_read_symbols (input_bfd))
  2759.         return FALSE;
  2760.  
  2761.       /* Since we have been called by a specific linker, rather than
  2762.          the generic linker, the values of the symbols will not be
  2763.          right.  They will be the values as seen in the input file,
  2764.          not the values of the final link.  We need to fix them up
  2765.          before we can relocate the section.  */
  2766.       sympp = _bfd_generic_link_get_symbols (input_bfd);
  2767.       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
  2768.       for (; sympp < symppend; sympp++)
  2769.         {
  2770.           asymbol *sym;
  2771.           struct bfd_link_hash_entry *h;
  2772.  
  2773.           sym = *sympp;
  2774.  
  2775.           if ((sym->flags & (BSF_INDIRECT
  2776.                              | BSF_WARNING
  2777.                              | BSF_GLOBAL
  2778.                              | BSF_CONSTRUCTOR
  2779.                              | BSF_WEAK)) != 0
  2780.               || bfd_is_und_section (bfd_get_section (sym))
  2781.               || bfd_is_com_section (bfd_get_section (sym))
  2782.               || bfd_is_ind_section (bfd_get_section (sym)))
  2783.             {
  2784.               /* sym->udata may have been set by
  2785.                  generic_link_add_symbol_list.  */
  2786.               if (sym->udata.p != NULL)
  2787.                 h = (struct bfd_link_hash_entry *) sym->udata.p;
  2788.               else if (bfd_is_und_section (bfd_get_section (sym)))
  2789.                 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
  2790.                                                   bfd_asymbol_name (sym),
  2791.                                                   FALSE, FALSE, TRUE);
  2792.               else
  2793.                 h = bfd_link_hash_lookup (info->hash,
  2794.                                           bfd_asymbol_name (sym),
  2795.                                           FALSE, FALSE, TRUE);
  2796.               if (h != NULL)
  2797.                 set_symbol_from_hash (sym, h);
  2798.             }
  2799.         }
  2800.     }
  2801.  
  2802.   if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
  2803.       && input_section->size != 0)
  2804.     {
  2805.       /* Group section contents are set by bfd_elf_set_group_contents.  */
  2806.       if (!output_bfd->output_has_begun)
  2807.         {
  2808.           /* FIXME: This hack ensures bfd_elf_set_group_contents is called.  */
  2809.           if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
  2810.             goto error_return;
  2811.         }
  2812.       new_contents = output_section->contents;
  2813.       BFD_ASSERT (new_contents != NULL);
  2814.       BFD_ASSERT (input_section->output_offset == 0);
  2815.     }
  2816.   else
  2817.     {
  2818.       /* Get and relocate the section contents.  */
  2819.       sec_size = (input_section->rawsize > input_section->size
  2820.                   ? input_section->rawsize
  2821.                   : input_section->size);
  2822.       contents = (bfd_byte *) bfd_malloc (sec_size);
  2823.       if (contents == NULL && sec_size != 0)
  2824.         goto error_return;
  2825.       new_contents = (bfd_get_relocated_section_contents
  2826.                       (output_bfd, info, link_order, contents,
  2827.                        info->relocatable,
  2828.                        _bfd_generic_link_get_symbols (input_bfd)));
  2829.       if (!new_contents)
  2830.         goto error_return;
  2831.     }
  2832.  
  2833.   /* Output the section contents.  */
  2834.   loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
  2835.   if (! bfd_set_section_contents (output_bfd, output_section,
  2836.                                   new_contents, loc, input_section->size))
  2837.     goto error_return;
  2838.  
  2839.   if (contents != NULL)
  2840.     free (contents);
  2841.   return TRUE;
  2842.  
  2843.  error_return:
  2844.   if (contents != NULL)
  2845.     free (contents);
  2846.   return FALSE;
  2847. }
  2848.  
  2849. /* A little routine to count the number of relocs in a link_order
  2850.    list.  */
  2851.  
  2852. unsigned int
  2853. _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
  2854. {
  2855.   register unsigned int c;
  2856.   register struct bfd_link_order *l;
  2857.  
  2858.   c = 0;
  2859.   for (l = link_order; l != NULL; l = l->next)
  2860.     {
  2861.       if (l->type == bfd_section_reloc_link_order
  2862.           || l->type == bfd_symbol_reloc_link_order)
  2863.         ++c;
  2864.     }
  2865.  
  2866.   return c;
  2867. }
  2868.  
  2869. /*
  2870. FUNCTION
  2871.         bfd_link_split_section
  2872.  
  2873. SYNOPSIS
  2874.         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
  2875.  
  2876. DESCRIPTION
  2877.         Return nonzero if @var{sec} should be split during a
  2878.         reloceatable or final link.
  2879.  
  2880. .#define bfd_link_split_section(abfd, sec) \
  2881. .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
  2882. .
  2883.  
  2884. */
  2885.  
  2886. bfd_boolean
  2887. _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
  2888.                                  asection *sec ATTRIBUTE_UNUSED)
  2889. {
  2890.   return FALSE;
  2891. }
  2892.  
  2893. /*
  2894. FUNCTION
  2895.         bfd_section_already_linked
  2896.  
  2897. SYNOPSIS
  2898.         bfd_boolean bfd_section_already_linked (bfd *abfd,
  2899.                                                 asection *sec,
  2900.                                                 struct bfd_link_info *info);
  2901.  
  2902. DESCRIPTION
  2903.         Check if @var{data} has been already linked during a reloceatable
  2904.         or final link.  Return TRUE if it has.
  2905.  
  2906. .#define bfd_section_already_linked(abfd, sec, info) \
  2907. .       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
  2908. .
  2909.  
  2910. */
  2911.  
  2912. /* Sections marked with the SEC_LINK_ONCE flag should only be linked
  2913.    once into the output.  This routine checks each section, and
  2914.    arrange to discard it if a section of the same name has already
  2915.    been linked.  This code assumes that all relevant sections have the
  2916.    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
  2917.    section name.  bfd_section_already_linked is called via
  2918.    bfd_map_over_sections.  */
  2919.  
  2920. /* The hash table.  */
  2921.  
  2922. static struct bfd_hash_table _bfd_section_already_linked_table;
  2923.  
  2924. /* Support routines for the hash table used by section_already_linked,
  2925.    initialize the table, traverse, lookup, fill in an entry and remove
  2926.    the table.  */
  2927.  
  2928. void
  2929. bfd_section_already_linked_table_traverse
  2930.   (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
  2931.                         void *), void *info)
  2932. {
  2933.   bfd_hash_traverse (&_bfd_section_already_linked_table,
  2934.                      (bfd_boolean (*) (struct bfd_hash_entry *,
  2935.                                        void *)) func,
  2936.                      info);
  2937. }
  2938.  
  2939. struct bfd_section_already_linked_hash_entry *
  2940. bfd_section_already_linked_table_lookup (const char *name)
  2941. {
  2942.   return ((struct bfd_section_already_linked_hash_entry *)
  2943.           bfd_hash_lookup (&_bfd_section_already_linked_table, name,
  2944.                            TRUE, FALSE));
  2945. }
  2946.  
  2947. bfd_boolean
  2948. bfd_section_already_linked_table_insert
  2949.   (struct bfd_section_already_linked_hash_entry *already_linked_list,
  2950.    asection *sec)
  2951. {
  2952.   struct bfd_section_already_linked *l;
  2953.  
  2954.   /* Allocate the memory from the same obstack as the hash table is
  2955.      kept in.  */
  2956.   l = (struct bfd_section_already_linked *)
  2957.       bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
  2958.   if (l == NULL)
  2959.     return FALSE;
  2960.   l->sec = sec;
  2961.   l->next = already_linked_list->entry;
  2962.   already_linked_list->entry = l;
  2963.   return TRUE;
  2964. }
  2965.  
  2966. static struct bfd_hash_entry *
  2967. already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
  2968.                         struct bfd_hash_table *table,
  2969.                         const char *string ATTRIBUTE_UNUSED)
  2970. {
  2971.   struct bfd_section_already_linked_hash_entry *ret =
  2972.     (struct bfd_section_already_linked_hash_entry *)
  2973.       bfd_hash_allocate (table, sizeof *ret);
  2974.  
  2975.   if (ret == NULL)
  2976.     return NULL;
  2977.  
  2978.   ret->entry = NULL;
  2979.  
  2980.   return &ret->root;
  2981. }
  2982.  
  2983. bfd_boolean
  2984. bfd_section_already_linked_table_init (void)
  2985. {
  2986.   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
  2987.                                 already_linked_newfunc,
  2988.                                 sizeof (struct bfd_section_already_linked_hash_entry),
  2989.                                 42);
  2990. }
  2991.  
  2992. void
  2993. bfd_section_already_linked_table_free (void)
  2994. {
  2995.   bfd_hash_table_free (&_bfd_section_already_linked_table);
  2996. }
  2997.  
  2998. /* Report warnings as appropriate for duplicate section SEC.
  2999.    Return FALSE if we decide to keep SEC after all.  */
  3000.  
  3001. bfd_boolean
  3002. _bfd_handle_already_linked (asection *sec,
  3003.                             struct bfd_section_already_linked *l,
  3004.                             struct bfd_link_info *info)
  3005. {
  3006.   switch (sec->flags & SEC_LINK_DUPLICATES)
  3007.     {
  3008.     default:
  3009.       abort ();
  3010.  
  3011.     case SEC_LINK_DUPLICATES_DISCARD:
  3012.       /* If we found an LTO IR match for this comdat group on
  3013.          the first pass, replace it with the LTO output on the
  3014.          second pass.  We can't simply choose real object
  3015.          files over IR because the first pass may contain a
  3016.          mix of LTO and normal objects and we must keep the
  3017.          first match, be it IR or real.  */
  3018.       if (info->loading_lto_outputs
  3019.           && (l->sec->owner->flags & BFD_PLUGIN) != 0)
  3020.         {
  3021.           l->sec = sec;
  3022.           return FALSE;
  3023.         }
  3024.       break;
  3025.  
  3026.     case SEC_LINK_DUPLICATES_ONE_ONLY:
  3027.       info->callbacks->einfo
  3028.         (_("%B: ignoring duplicate section `%A'\n"),
  3029.          sec->owner, sec);
  3030.       break;
  3031.  
  3032.     case SEC_LINK_DUPLICATES_SAME_SIZE:
  3033.       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
  3034.         ;
  3035.       else if (sec->size != l->sec->size)
  3036.         info->callbacks->einfo
  3037.           (_("%B: duplicate section `%A' has different size\n"),
  3038.            sec->owner, sec);
  3039.       break;
  3040.  
  3041.     case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  3042.       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
  3043.         ;
  3044.       else if (sec->size != l->sec->size)
  3045.         info->callbacks->einfo
  3046.           (_("%B: duplicate section `%A' has different size\n"),
  3047.            sec->owner, sec);
  3048.       else if (sec->size != 0)
  3049.         {
  3050.           bfd_byte *sec_contents, *l_sec_contents = NULL;
  3051.  
  3052.           if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
  3053.             info->callbacks->einfo
  3054.               (_("%B: could not read contents of section `%A'\n"),
  3055.                sec->owner, sec);
  3056.           else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
  3057.                                                 &l_sec_contents))
  3058.             info->callbacks->einfo
  3059.               (_("%B: could not read contents of section `%A'\n"),
  3060.                l->sec->owner, l->sec);
  3061.           else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
  3062.             info->callbacks->einfo
  3063.               (_("%B: duplicate section `%A' has different contents\n"),
  3064.                sec->owner, sec);
  3065.  
  3066.           if (sec_contents)
  3067.             free (sec_contents);
  3068.           if (l_sec_contents)
  3069.             free (l_sec_contents);
  3070.         }
  3071.       break;
  3072.     }
  3073.  
  3074.   /* Set the output_section field so that lang_add_section
  3075.      does not create a lang_input_section structure for this
  3076.      section.  Since there might be a symbol in the section
  3077.      being discarded, we must retain a pointer to the section
  3078.      which we are really going to use.  */
  3079.   sec->output_section = bfd_abs_section_ptr;
  3080.   sec->kept_section = l->sec;
  3081.   return TRUE;
  3082. }
  3083.  
  3084. /* This is used on non-ELF inputs.  */
  3085.  
  3086. bfd_boolean
  3087. _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
  3088.                                      asection *sec,
  3089.                                      struct bfd_link_info *info)
  3090. {
  3091.   const char *name;
  3092.   struct bfd_section_already_linked *l;
  3093.   struct bfd_section_already_linked_hash_entry *already_linked_list;
  3094.  
  3095.   if ((sec->flags & SEC_LINK_ONCE) == 0)
  3096.     return FALSE;
  3097.  
  3098.   /* The generic linker doesn't handle section groups.  */
  3099.   if ((sec->flags & SEC_GROUP) != 0)
  3100.     return FALSE;
  3101.  
  3102.   /* FIXME: When doing a relocatable link, we may have trouble
  3103.      copying relocations in other sections that refer to local symbols
  3104.      in the section being discarded.  Those relocations will have to
  3105.      be converted somehow; as of this writing I'm not sure that any of
  3106.      the backends handle that correctly.
  3107.  
  3108.      It is tempting to instead not discard link once sections when
  3109.      doing a relocatable link (technically, they should be discarded
  3110.      whenever we are building constructors).  However, that fails,
  3111.      because the linker winds up combining all the link once sections
  3112.      into a single large link once section, which defeats the purpose
  3113.      of having link once sections in the first place.  */
  3114.  
  3115.   name = bfd_get_section_name (abfd, sec);
  3116.  
  3117.   already_linked_list = bfd_section_already_linked_table_lookup (name);
  3118.  
  3119.   l = already_linked_list->entry;
  3120.   if (l != NULL)
  3121.     {
  3122.       /* The section has already been linked.  See if we should
  3123.          issue a warning.  */
  3124.       return _bfd_handle_already_linked (sec, l, info);
  3125.     }
  3126.  
  3127.   /* This is the first section with this name.  Record it.  */
  3128.   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  3129.     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  3130.   return FALSE;
  3131. }
  3132.  
  3133. /* Choose a neighbouring section to S in OBFD that will be output, or
  3134.    the absolute section if ADDR is out of bounds of the neighbours.  */
  3135.  
  3136. asection *
  3137. _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
  3138. {
  3139.   asection *next, *prev, *best;
  3140.  
  3141.   /* Find preceding kept section.  */
  3142.   for (prev = s->prev; prev != NULL; prev = prev->prev)
  3143.     if ((prev->flags & SEC_EXCLUDE) == 0
  3144.         && !bfd_section_removed_from_list (obfd, prev))
  3145.       break;
  3146.  
  3147.   /* Find following kept section.  Start at prev->next because
  3148.      other sections may have been added after S was removed.  */
  3149.   if (s->prev != NULL)
  3150.     next = s->prev->next;
  3151.   else
  3152.     next = s->owner->sections;
  3153.   for (; next != NULL; next = next->next)
  3154.     if ((next->flags & SEC_EXCLUDE) == 0
  3155.         && !bfd_section_removed_from_list (obfd, next))
  3156.       break;
  3157.  
  3158.   /* Choose better of two sections, based on flags.  The idea
  3159.      is to choose a section that will be in the same segment
  3160.      as S would have been if it was kept.  */
  3161.   best = next;
  3162.   if (prev == NULL)
  3163.     {
  3164.       if (next == NULL)
  3165.         best = bfd_abs_section_ptr;
  3166.     }
  3167.   else if (next == NULL)
  3168.     best = prev;
  3169.   else if (((prev->flags ^ next->flags)
  3170.             & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
  3171.     {
  3172.       if (((next->flags ^ s->flags)
  3173.            & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
  3174.           /* We prefer to choose a loaded section.  Section S
  3175.              doesn't have SEC_LOAD set (it being excluded, that
  3176.              part of the flag processing didn't happen) so we
  3177.              can't compare that flag to those of NEXT and PREV.  */
  3178.           || ((prev->flags & SEC_LOAD) != 0
  3179.               && (next->flags & SEC_LOAD) == 0))
  3180.         best = prev;
  3181.     }
  3182.   else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
  3183.     {
  3184.       if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
  3185.         best = prev;
  3186.     }
  3187.   else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
  3188.     {
  3189.       if (((next->flags ^ s->flags) & SEC_CODE) != 0)
  3190.         best = prev;
  3191.     }
  3192.   else
  3193.     {
  3194.       /* Flags we care about are the same.  Prefer the following
  3195.          section if that will result in a positive valued sym.  */
  3196.       if (addr < next->vma)
  3197.         best = prev;
  3198.     }
  3199.  
  3200.   return best;
  3201. }
  3202.  
  3203. /* Convert symbols in excluded output sections to use a kept section.  */
  3204.  
  3205. static bfd_boolean
  3206. fix_syms (struct bfd_link_hash_entry *h, void *data)
  3207. {
  3208.   bfd *obfd = (bfd *) data;
  3209.  
  3210.   if (h->type == bfd_link_hash_defined
  3211.       || h->type == bfd_link_hash_defweak)
  3212.     {
  3213.       asection *s = h->u.def.section;
  3214.       if (s != NULL
  3215.           && s->output_section != NULL
  3216.           && (s->output_section->flags & SEC_EXCLUDE) != 0
  3217.           && bfd_section_removed_from_list (obfd, s->output_section))
  3218.         {
  3219.           asection *op;
  3220.  
  3221.           h->u.def.value += s->output_offset + s->output_section->vma;
  3222.           op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
  3223.           h->u.def.value -= op->vma;
  3224.           h->u.def.section = op;
  3225.         }
  3226.     }
  3227.  
  3228.   return TRUE;
  3229. }
  3230.  
  3231. void
  3232. _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
  3233. {
  3234.   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
  3235. }
  3236.  
  3237. /*
  3238. FUNCTION
  3239.         bfd_generic_define_common_symbol
  3240.  
  3241. SYNOPSIS
  3242.         bfd_boolean bfd_generic_define_common_symbol
  3243.           (bfd *output_bfd, struct bfd_link_info *info,
  3244.            struct bfd_link_hash_entry *h);
  3245.  
  3246. DESCRIPTION
  3247.         Convert common symbol @var{h} into a defined symbol.
  3248.         Return TRUE on success and FALSE on failure.
  3249.  
  3250. .#define bfd_define_common_symbol(output_bfd, info, h) \
  3251. .       BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
  3252. .
  3253. */
  3254.  
  3255. bfd_boolean
  3256. bfd_generic_define_common_symbol (bfd *output_bfd,
  3257.                                   struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3258.                                   struct bfd_link_hash_entry *h)
  3259. {
  3260.   unsigned int power_of_two;
  3261.   bfd_vma alignment, size;
  3262.   asection *section;
  3263.  
  3264.   BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
  3265.  
  3266.   size = h->u.c.size;
  3267.   power_of_two = h->u.c.p->alignment_power;
  3268.   section = h->u.c.p->section;
  3269.  
  3270.   /* Increase the size of the section to align the common symbol.
  3271.      The alignment must be a power of two.  */
  3272.   alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
  3273.   BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
  3274.   section->size += alignment - 1;
  3275.   section->size &= -alignment;
  3276.  
  3277.   /* Adjust the section's overall alignment if necessary.  */
  3278.   if (power_of_two > section->alignment_power)
  3279.     section->alignment_power = power_of_two;
  3280.  
  3281.   /* Change the symbol from common to defined.  */
  3282.   h->type = bfd_link_hash_defined;
  3283.   h->u.def.section = section;
  3284.   h->u.def.value = section->size;
  3285.  
  3286.   /* Increase the size of the section.  */
  3287.   section->size += size;
  3288.  
  3289.   /* Make sure the section is allocated in memory, and make sure that
  3290.      it is no longer a common section.  */
  3291.   section->flags |= SEC_ALLOC;
  3292.   section->flags &= ~SEC_IS_COMMON;
  3293.   return TRUE;
  3294. }
  3295.  
  3296. /*
  3297. FUNCTION
  3298.         bfd_find_version_for_sym
  3299.  
  3300. SYNOPSIS
  3301.         struct bfd_elf_version_tree * bfd_find_version_for_sym
  3302.           (struct bfd_elf_version_tree *verdefs,
  3303.            const char *sym_name, bfd_boolean *hide);
  3304.  
  3305. DESCRIPTION
  3306.         Search an elf version script tree for symbol versioning
  3307.         info and export / don't-export status for a given symbol.
  3308.         Return non-NULL on success and NULL on failure; also sets
  3309.         the output @samp{hide} boolean parameter.
  3310.  
  3311. */
  3312.  
  3313. struct bfd_elf_version_tree *
  3314. bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
  3315.                           const char *sym_name,
  3316.                           bfd_boolean *hide)
  3317. {
  3318.   struct bfd_elf_version_tree *t;
  3319.   struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
  3320.   struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
  3321.  
  3322.   local_ver = NULL;
  3323.   global_ver = NULL;
  3324.   star_local_ver = NULL;
  3325.   star_global_ver = NULL;
  3326.   exist_ver = NULL;
  3327.   for (t = verdefs; t != NULL; t = t->next)
  3328.     {
  3329.       if (t->globals.list != NULL)
  3330.         {
  3331.           struct bfd_elf_version_expr *d = NULL;
  3332.  
  3333.           while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
  3334.             {
  3335.               if (d->literal || strcmp (d->pattern, "*") != 0)
  3336.                 global_ver = t;
  3337.               else
  3338.                 star_global_ver = t;
  3339.               if (d->symver)
  3340.                 exist_ver = t;
  3341.               d->script = 1;
  3342.               /* If the match is a wildcard pattern, keep looking for
  3343.                  a more explicit, perhaps even local, match.  */
  3344.               if (d->literal)
  3345.                 break;
  3346.             }
  3347.  
  3348.           if (d != NULL)
  3349.             break;
  3350.         }
  3351.  
  3352.       if (t->locals.list != NULL)
  3353.         {
  3354.           struct bfd_elf_version_expr *d = NULL;
  3355.  
  3356.           while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
  3357.             {
  3358.               if (d->literal || strcmp (d->pattern, "*") != 0)
  3359.                 local_ver = t;
  3360.               else
  3361.                 star_local_ver = t;
  3362.               /* If the match is a wildcard pattern, keep looking for
  3363.                  a more explicit, perhaps even global, match.  */
  3364.               if (d->literal)
  3365.                 {
  3366.                   /* An exact match overrides a global wildcard.  */
  3367.                   global_ver = NULL;
  3368.                   star_global_ver = NULL;
  3369.                   break;
  3370.                 }
  3371.             }
  3372.  
  3373.           if (d != NULL)
  3374.             break;
  3375.         }
  3376.     }
  3377.  
  3378.   if (global_ver == NULL && local_ver == NULL)
  3379.     global_ver = star_global_ver;
  3380.  
  3381.   if (global_ver != NULL)
  3382.     {
  3383.       /* If we already have a versioned symbol that matches the
  3384.          node for this symbol, then we don't want to create a
  3385.          duplicate from the unversioned symbol.  Instead hide the
  3386.          unversioned symbol.  */
  3387.       *hide = exist_ver == global_ver;
  3388.       return global_ver;
  3389.     }
  3390.  
  3391.   if (local_ver == NULL)
  3392.     local_ver = star_local_ver;
  3393.  
  3394.   if (local_ver != NULL)
  3395.     {
  3396.       *hide = TRUE;
  3397.       return local_ver;
  3398.     }
  3399.  
  3400.   return NULL;
  3401. }
  3402.  
  3403. /*
  3404. FUNCTION
  3405.         bfd_hide_sym_by_version
  3406.  
  3407. SYNOPSIS
  3408.         bfd_boolean bfd_hide_sym_by_version
  3409.           (struct bfd_elf_version_tree *verdefs, const char *sym_name);
  3410.  
  3411. DESCRIPTION
  3412.         Search an elf version script tree for symbol versioning
  3413.         info for a given symbol.  Return TRUE if the symbol is hidden.
  3414.  
  3415. */
  3416.  
  3417. bfd_boolean
  3418. bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
  3419.                          const char *sym_name)
  3420. {
  3421.   bfd_boolean hidden = FALSE;
  3422.   bfd_find_version_for_sym (verdefs, sym_name, &hidden);
  3423.   return hidden;
  3424. }
  3425.