Back to index

cell-binutils  2.17cvs20070401
linker.c
Go to the documentation of this file.
00001 /* linker.c -- BFD linker routines
00002    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
00003    2003, 2004, 2005, 2006 Free Software Foundation, Inc.
00004    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "libbfd.h"
00025 #include "bfdlink.h"
00026 #include "genlink.h"
00027 
00028 /*
00029 SECTION
00030        Linker Functions
00031 
00032 @cindex Linker
00033        The linker uses three special entry points in the BFD target
00034        vector.  It is not necessary to write special routines for
00035        these entry points when creating a new BFD back end, since
00036        generic versions are provided.  However, writing them can
00037        speed up linking and make it use significantly less runtime
00038        memory.
00039 
00040        The first routine creates a hash table used by the other
00041        routines.  The second routine adds the symbols from an object
00042        file to the hash table.  The third routine takes all the
00043        object files and links them together to create the output
00044        file.  These routines are designed so that the linker proper
00045        does not need to know anything about the symbols in the object
00046        files that it is linking.  The linker merely arranges the
00047        sections as directed by the linker script and lets BFD handle
00048        the details of symbols and relocs.
00049 
00050        The second routine and third routines are passed a pointer to
00051        a <<struct bfd_link_info>> structure (defined in
00052        <<bfdlink.h>>) which holds information relevant to the link,
00053        including the linker hash table (which was created by the
00054        first routine) and a set of callback functions to the linker
00055        proper.
00056 
00057        The generic linker routines are in <<linker.c>>, and use the
00058        header file <<genlink.h>>.  As of this writing, the only back
00059        ends which have implemented versions of these routines are
00060        a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
00061        routines are used as examples throughout this section.
00062 
00063 @menu
00064 @* Creating a Linker Hash Table::
00065 @* Adding Symbols to the Hash Table::
00066 @* Performing the Final Link::
00067 @end menu
00068 
00069 INODE
00070 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
00071 SUBSECTION
00072        Creating a linker hash table
00073 
00074 @cindex _bfd_link_hash_table_create in target vector
00075 @cindex target vector (_bfd_link_hash_table_create)
00076        The linker routines must create a hash table, which must be
00077        derived from <<struct bfd_link_hash_table>> described in
00078        <<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
00079        create a derived hash table.  This entry point is called using
00080        the target vector of the linker output file.
00081 
00082        The <<_bfd_link_hash_table_create>> entry point must allocate
00083        and initialize an instance of the desired hash table.  If the
00084        back end does not require any additional information to be
00085        stored with the entries in the hash table, the entry point may
00086        simply create a <<struct bfd_link_hash_table>>.  Most likely,
00087        however, some additional information will be needed.
00088 
00089        For example, with each entry in the hash table the a.out
00090        linker keeps the index the symbol has in the final output file
00091        (this index number is used so that when doing a relocatable
00092        link the symbol index used in the output file can be quickly
00093        filled in when copying over a reloc).  The a.out linker code
00094        defines the required structures and functions for a hash table
00095        derived from <<struct bfd_link_hash_table>>.  The a.out linker
00096        hash table is created by the function
00097        <<NAME(aout,link_hash_table_create)>>; it simply allocates
00098        space for the hash table, initializes it, and returns a
00099        pointer to it.
00100 
00101        When writing the linker routines for a new back end, you will
00102        generally not know exactly which fields will be required until
00103        you have finished.  You should simply create a new hash table
00104        which defines no additional fields, and then simply add fields
00105        as they become necessary.
00106 
00107 INODE
00108 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
00109 SUBSECTION
00110        Adding symbols to the hash table
00111 
00112 @cindex _bfd_link_add_symbols in target vector
00113 @cindex target vector (_bfd_link_add_symbols)
00114        The linker proper will call the <<_bfd_link_add_symbols>>
00115        entry point for each object file or archive which is to be
00116        linked (typically these are the files named on the command
00117        line, but some may also come from the linker script).  The
00118        entry point is responsible for examining the file.  For an
00119        object file, BFD must add any relevant symbol information to
00120        the hash table.  For an archive, BFD must determine which
00121        elements of the archive should be used and adding them to the
00122        link.
00123 
00124        The a.out version of this entry point is
00125        <<NAME(aout,link_add_symbols)>>.
00126 
00127 @menu
00128 @* Differing file formats::
00129 @* Adding symbols from an object file::
00130 @* Adding symbols from an archive::
00131 @end menu
00132 
00133 INODE
00134 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
00135 SUBSUBSECTION
00136        Differing file formats
00137 
00138        Normally all the files involved in a link will be of the same
00139        format, but it is also possible to link together different
00140        format object files, and the back end must support that.  The
00141        <<_bfd_link_add_symbols>> entry point is called via the target
00142        vector of the file to be added.  This has an important
00143        consequence: the function may not assume that the hash table
00144        is the type created by the corresponding
00145        <<_bfd_link_hash_table_create>> vector.  All the
00146        <<_bfd_link_add_symbols>> function can assume about the hash
00147        table is that it is derived from <<struct
00148        bfd_link_hash_table>>.
00149 
00150        Sometimes the <<_bfd_link_add_symbols>> function must store
00151        some information in the hash table entry to be used by the
00152        <<_bfd_final_link>> function.  In such a case the <<creator>>
00153        field of the hash table must be checked to make sure that the
00154        hash table was created by an object file of the same format.
00155 
00156        The <<_bfd_final_link>> routine must be prepared to handle a
00157        hash entry without any extra information added by the
00158        <<_bfd_link_add_symbols>> function.  A hash entry without
00159        extra information will also occur when the linker script
00160        directs the linker to create a symbol.  Note that, regardless
00161        of how a hash table entry is added, all the fields will be
00162        initialized to some sort of null value by the hash table entry
00163        initialization function.
00164 
00165        See <<ecoff_link_add_externals>> for an example of how to
00166        check the <<creator>> field before saving information (in this
00167        case, the ECOFF external symbol debugging information) in a
00168        hash table entry.
00169 
00170 INODE
00171 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
00172 SUBSUBSECTION
00173        Adding symbols from an object file
00174 
00175        When the <<_bfd_link_add_symbols>> routine is passed an object
00176        file, it must add all externally visible symbols in that
00177        object file to the hash table.  The actual work of adding the
00178        symbol to the hash table is normally handled by the function
00179        <<_bfd_generic_link_add_one_symbol>>.  The
00180        <<_bfd_link_add_symbols>> routine is responsible for reading
00181        all the symbols from the object file and passing the correct
00182        information to <<_bfd_generic_link_add_one_symbol>>.
00183 
00184        The <<_bfd_link_add_symbols>> routine should not use
00185        <<bfd_canonicalize_symtab>> to read the symbols.  The point of
00186        providing this routine is to avoid the overhead of converting
00187        the symbols into generic <<asymbol>> structures.
00188 
00189 @findex _bfd_generic_link_add_one_symbol
00190        <<_bfd_generic_link_add_one_symbol>> handles the details of
00191        combining common symbols, warning about multiple definitions,
00192        and so forth.  It takes arguments which describe the symbol to
00193        add, notably symbol flags, a section, and an offset.  The
00194        symbol flags include such things as <<BSF_WEAK>> or
00195        <<BSF_INDIRECT>>.  The section is a section in the object
00196        file, or something like <<bfd_und_section_ptr>> for an undefined
00197        symbol or <<bfd_com_section_ptr>> for a common symbol.
00198 
00199        If the <<_bfd_final_link>> routine is also going to need to
00200        read the symbol information, the <<_bfd_link_add_symbols>>
00201        routine should save it somewhere attached to the object file
00202        BFD.  However, the information should only be saved if the
00203        <<keep_memory>> field of the <<info>> argument is TRUE, so
00204        that the <<-no-keep-memory>> linker switch is effective.
00205 
00206        The a.out function which adds symbols from an object file is
00207        <<aout_link_add_object_symbols>>, and most of the interesting
00208        work is in <<aout_link_add_symbols>>.  The latter saves
00209        pointers to the hash tables entries created by
00210        <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
00211        so that the <<_bfd_final_link>> routine does not have to call
00212        the hash table lookup routine to locate the entry.
00213 
00214 INODE
00215 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
00216 SUBSUBSECTION
00217        Adding symbols from an archive
00218 
00219        When the <<_bfd_link_add_symbols>> routine is passed an
00220        archive, it must look through the symbols defined by the
00221        archive and decide which elements of the archive should be
00222        included in the link.  For each such element it must call the
00223        <<add_archive_element>> linker callback, and it must add the
00224        symbols from the object file to the linker hash table.
00225 
00226 @findex _bfd_generic_link_add_archive_symbols
00227        In most cases the work of looking through the symbols in the
00228        archive should be done by the
00229        <<_bfd_generic_link_add_archive_symbols>> function.  This
00230        function builds a hash table from the archive symbol table and
00231        looks through the list of undefined symbols to see which
00232        elements should be included.
00233        <<_bfd_generic_link_add_archive_symbols>> is passed a function
00234        to call to make the final decision about adding an archive
00235        element to the link and to do the actual work of adding the
00236        symbols to the linker hash table.
00237 
00238        The function passed to
00239        <<_bfd_generic_link_add_archive_symbols>> must read the
00240        symbols of the archive element and decide whether the archive
00241        element should be included in the link.  If the element is to
00242        be included, the <<add_archive_element>> linker callback
00243        routine must be called with the element as an argument, and
00244        the elements symbols must be added to the linker hash table
00245        just as though the element had itself been passed to the
00246        <<_bfd_link_add_symbols>> function.
00247 
00248        When the a.out <<_bfd_link_add_symbols>> function receives an
00249        archive, it calls <<_bfd_generic_link_add_archive_symbols>>
00250        passing <<aout_link_check_archive_element>> as the function
00251        argument. <<aout_link_check_archive_element>> calls
00252        <<aout_link_check_ar_symbols>>.  If the latter decides to add
00253        the element (an element is only added if it provides a real,
00254        non-common, definition for a previously undefined or common
00255        symbol) it calls the <<add_archive_element>> callback and then
00256        <<aout_link_check_archive_element>> calls
00257        <<aout_link_add_symbols>> to actually add the symbols to the
00258        linker hash table.
00259 
00260        The ECOFF back end is unusual in that it does not normally
00261        call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
00262        archives already contain a hash table of symbols.  The ECOFF
00263        back end searches the archive itself to avoid the overhead of
00264        creating a new hash table.
00265 
00266 INODE
00267 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
00268 SUBSECTION
00269        Performing the final link
00270 
00271 @cindex _bfd_link_final_link in target vector
00272 @cindex target vector (_bfd_final_link)
00273        When all the input files have been processed, the linker calls
00274        the <<_bfd_final_link>> entry point of the output BFD.  This
00275        routine is responsible for producing the final output file,
00276        which has several aspects.  It must relocate the contents of
00277        the input sections and copy the data into the output sections.
00278        It must build an output symbol table including any local
00279        symbols from the input files and the global symbols from the
00280        hash table.  When producing relocatable output, it must
00281        modify the input relocs and write them into the output file.
00282        There may also be object format dependent work to be done.
00283 
00284        The linker will also call the <<write_object_contents>> entry
00285        point when the BFD is closed.  The two entry points must work
00286        together in order to produce the correct output file.
00287 
00288        The details of how this works are inevitably dependent upon
00289        the specific object file format.  The a.out
00290        <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
00291 
00292 @menu
00293 @* Information provided by the linker::
00294 @* Relocating the section contents::
00295 @* Writing the symbol table::
00296 @end menu
00297 
00298 INODE
00299 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
00300 SUBSUBSECTION
00301        Information provided by the linker
00302 
00303        Before the linker calls the <<_bfd_final_link>> entry point,
00304        it sets up some data structures for the function to use.
00305 
00306        The <<input_bfds>> field of the <<bfd_link_info>> structure
00307        will point to a list of all the input files included in the
00308        link.  These files are linked through the <<link_next>> field
00309        of the <<bfd>> structure.
00310 
00311        Each section in the output file will have a list of
00312        <<link_order>> structures attached to the <<map_head.link_order>>
00313        field (the <<link_order>> structure is defined in
00314        <<bfdlink.h>>).  These structures describe how to create the
00315        contents of the output section in terms of the contents of
00316        various input sections, fill constants, and, eventually, other
00317        types of information.  They also describe relocs that must be
00318        created by the BFD backend, but do not correspond to any input
00319        file; this is used to support -Ur, which builds constructors
00320        while generating a relocatable object file.
00321 
00322 INODE
00323 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
00324 SUBSUBSECTION
00325        Relocating the section contents
00326 
00327        The <<_bfd_final_link>> function should look through the
00328        <<link_order>> structures attached to each section of the
00329        output file.  Each <<link_order>> structure should either be
00330        handled specially, or it should be passed to the function
00331        <<_bfd_default_link_order>> which will do the right thing
00332        (<<_bfd_default_link_order>> is defined in <<linker.c>>).
00333 
00334        For efficiency, a <<link_order>> of type
00335        <<bfd_indirect_link_order>> whose associated section belongs
00336        to a BFD of the same format as the output BFD must be handled
00337        specially.  This type of <<link_order>> describes part of an
00338        output section in terms of a section belonging to one of the
00339        input files.  The <<_bfd_final_link>> function should read the
00340        contents of the section and any associated relocs, apply the
00341        relocs to the section contents, and write out the modified
00342        section contents.  If performing a relocatable link, the
00343        relocs themselves must also be modified and written out.
00344 
00345 @findex _bfd_relocate_contents
00346 @findex _bfd_final_link_relocate
00347        The functions <<_bfd_relocate_contents>> and
00348        <<_bfd_final_link_relocate>> provide some general support for
00349        performing the actual relocations, notably overflow checking.
00350        Their arguments include information about the symbol the
00351        relocation is against and a <<reloc_howto_type>> argument
00352        which describes the relocation to perform.  These functions
00353        are defined in <<reloc.c>>.
00354 
00355        The a.out function which handles reading, relocating, and
00356        writing section contents is <<aout_link_input_section>>.  The
00357        actual relocation is done in <<aout_link_input_section_std>>
00358        and <<aout_link_input_section_ext>>.
00359 
00360 INODE
00361 Writing the symbol table, , Relocating the section contents, Performing the Final Link
00362 SUBSUBSECTION
00363        Writing the symbol table
00364 
00365        The <<_bfd_final_link>> function must gather all the symbols
00366        in the input files and write them out.  It must also write out
00367        all the symbols in the global hash table.  This must be
00368        controlled by the <<strip>> and <<discard>> fields of the
00369        <<bfd_link_info>> structure.
00370 
00371        The local symbols of the input files will not have been
00372        entered into the linker hash table.  The <<_bfd_final_link>>
00373        routine must consider each input file and include the symbols
00374        in the output file.  It may be convenient to do this when
00375        looking through the <<link_order>> structures, or it may be
00376        done by stepping through the <<input_bfds>> list.
00377 
00378        The <<_bfd_final_link>> routine must also traverse the global
00379        hash table to gather all the externally visible symbols.  It
00380        is possible that most of the externally visible symbols may be
00381        written out when considering the symbols of each input file,
00382        but it is still necessary to traverse the hash table since the
00383        linker script may have defined some symbols that are not in
00384        any of the input files.
00385 
00386        The <<strip>> field of the <<bfd_link_info>> structure
00387        controls which symbols are written out.  The possible values
00388        are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
00389        then the <<keep_hash>> field of the <<bfd_link_info>>
00390        structure is a hash table of symbols to keep; each symbol
00391        should be looked up in this hash table, and only symbols which
00392        are present should be included in the output file.
00393 
00394        If the <<strip>> field of the <<bfd_link_info>> structure
00395        permits local symbols to be written out, the <<discard>> field
00396        is used to further controls which local symbols are included
00397        in the output file.  If the value is <<discard_l>>, then all
00398        local symbols which begin with a certain prefix are discarded;
00399        this is controlled by the <<bfd_is_local_label_name>> entry point.
00400 
00401        The a.out backend handles symbols by calling
00402        <<aout_link_write_symbols>> on each input BFD and then
00403        traversing the global hash table with the function
00404        <<aout_link_write_other_symbol>>.  It builds a string table
00405        while writing out the symbols, which is written to the output
00406        file at the end of <<NAME(aout,final_link)>>.
00407 */
00408 
00409 static bfd_boolean generic_link_add_object_symbols
00410   (bfd *, struct bfd_link_info *, bfd_boolean collect);
00411 static bfd_boolean generic_link_add_symbols
00412   (bfd *, struct bfd_link_info *, bfd_boolean);
00413 static bfd_boolean generic_link_check_archive_element_no_collect
00414   (bfd *, struct bfd_link_info *, bfd_boolean *);
00415 static bfd_boolean generic_link_check_archive_element_collect
00416   (bfd *, struct bfd_link_info *, bfd_boolean *);
00417 static bfd_boolean generic_link_check_archive_element
00418   (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
00419 static bfd_boolean generic_link_add_symbol_list
00420   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
00421    bfd_boolean);
00422 static bfd_boolean generic_add_output_symbol
00423   (bfd *, size_t *psymalloc, asymbol *);
00424 static bfd_boolean default_data_link_order
00425   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
00426 static bfd_boolean default_indirect_link_order
00427   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
00428    bfd_boolean);
00429 
00430 /* The link hash table structure is defined in bfdlink.h.  It provides
00431    a base hash table which the backend specific hash tables are built
00432    upon.  */
00433 
00434 /* Routine to create an entry in the link hash table.  */
00435 
00436 struct bfd_hash_entry *
00437 _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
00438                      struct bfd_hash_table *table,
00439                      const char *string)
00440 {
00441   /* Allocate the structure if it has not already been allocated by a
00442      subclass.  */
00443   if (entry == NULL)
00444     {
00445       entry = bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
00446       if (entry == NULL)
00447        return entry;
00448     }
00449 
00450   /* Call the allocation method of the superclass.  */
00451   entry = bfd_hash_newfunc (entry, table, string);
00452   if (entry)
00453     {
00454       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
00455 
00456       /* Initialize the local fields.  */
00457       h->type = bfd_link_hash_new;
00458       memset (&h->u.undef.next, 0,
00459              (sizeof (struct bfd_link_hash_entry)
00460               - offsetof (struct bfd_link_hash_entry, u.undef.next)));
00461     }
00462 
00463   return entry;
00464 }
00465 
00466 /* Initialize a link hash table.  The BFD argument is the one
00467    responsible for creating this table.  */
00468 
00469 bfd_boolean
00470 _bfd_link_hash_table_init
00471   (struct bfd_link_hash_table *table,
00472    bfd *abfd,
00473    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
00474                                   struct bfd_hash_table *,
00475                                   const char *),
00476    unsigned int entsize)
00477 {
00478   table->creator = abfd->xvec;
00479   table->undefs = NULL;
00480   table->undefs_tail = NULL;
00481   table->type = bfd_link_generic_hash_table;
00482 
00483   return bfd_hash_table_init (&table->table, newfunc, entsize);
00484 }
00485 
00486 /* Look up a symbol in a link hash table.  If follow is TRUE, we
00487    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
00488    the real symbol.  */
00489 
00490 struct bfd_link_hash_entry *
00491 bfd_link_hash_lookup (struct bfd_link_hash_table *table,
00492                     const char *string,
00493                     bfd_boolean create,
00494                     bfd_boolean copy,
00495                     bfd_boolean follow)
00496 {
00497   struct bfd_link_hash_entry *ret;
00498 
00499   ret = ((struct bfd_link_hash_entry *)
00500         bfd_hash_lookup (&table->table, string, create, copy));
00501 
00502   if (follow && ret != NULL)
00503     {
00504       while (ret->type == bfd_link_hash_indirect
00505             || ret->type == bfd_link_hash_warning)
00506        ret = ret->u.i.link;
00507     }
00508 
00509   return ret;
00510 }
00511 
00512 /* Look up a symbol in the main linker hash table if the symbol might
00513    be wrapped.  This should only be used for references to an
00514    undefined symbol, not for definitions of a symbol.  */
00515 
00516 struct bfd_link_hash_entry *
00517 bfd_wrapped_link_hash_lookup (bfd *abfd,
00518                            struct bfd_link_info *info,
00519                            const char *string,
00520                            bfd_boolean create,
00521                            bfd_boolean copy,
00522                            bfd_boolean follow)
00523 {
00524   bfd_size_type amt;
00525 
00526   if (info->wrap_hash != NULL)
00527     {
00528       const char *l;
00529       char prefix = '\0';
00530 
00531       l = string;
00532       if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
00533        {
00534          prefix = *l;
00535          ++l;
00536        }
00537 
00538 #undef WRAP
00539 #define WRAP "__wrap_"
00540 
00541       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
00542        {
00543          char *n;
00544          struct bfd_link_hash_entry *h;
00545 
00546          /* This symbol is being wrapped.  We want to replace all
00547              references to SYM with references to __wrap_SYM.  */
00548 
00549          amt = strlen (l) + sizeof WRAP + 1;
00550          n = bfd_malloc (amt);
00551          if (n == NULL)
00552            return NULL;
00553 
00554          n[0] = prefix;
00555          n[1] = '\0';
00556          strcat (n, WRAP);
00557          strcat (n, l);
00558          h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
00559          free (n);
00560          return h;
00561        }
00562 
00563 #undef WRAP
00564 
00565 #undef  REAL
00566 #define REAL "__real_"
00567 
00568       if (*l == '_'
00569          && CONST_STRNEQ (l, REAL)
00570          && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
00571                            FALSE, FALSE) != NULL)
00572        {
00573          char *n;
00574          struct bfd_link_hash_entry *h;
00575 
00576          /* This is a reference to __real_SYM, where SYM is being
00577              wrapped.  We want to replace all references to __real_SYM
00578              with references to SYM.  */
00579 
00580          amt = strlen (l + sizeof REAL - 1) + 2;
00581          n = bfd_malloc (amt);
00582          if (n == NULL)
00583            return NULL;
00584 
00585          n[0] = prefix;
00586          n[1] = '\0';
00587          strcat (n, l + sizeof REAL - 1);
00588          h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
00589          free (n);
00590          return h;
00591        }
00592 
00593 #undef REAL
00594     }
00595 
00596   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
00597 }
00598 
00599 /* Traverse a generic link hash table.  The only reason this is not a
00600    macro is to do better type checking.  This code presumes that an
00601    argument passed as a struct bfd_hash_entry * may be caught as a
00602    struct bfd_link_hash_entry * with no explicit cast required on the
00603    call.  */
00604 
00605 void
00606 bfd_link_hash_traverse
00607   (struct bfd_link_hash_table *table,
00608    bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
00609    void *info)
00610 {
00611   bfd_hash_traverse (&table->table,
00612                    (bfd_boolean (*) (struct bfd_hash_entry *, void *)) func,
00613                    info);
00614 }
00615 
00616 /* Add a symbol to the linker hash table undefs list.  */
00617 
00618 void
00619 bfd_link_add_undef (struct bfd_link_hash_table *table,
00620                   struct bfd_link_hash_entry *h)
00621 {
00622   BFD_ASSERT (h->u.undef.next == NULL);
00623   if (table->undefs_tail != NULL)
00624     table->undefs_tail->u.undef.next = h;
00625   if (table->undefs == NULL)
00626     table->undefs = h;
00627   table->undefs_tail = h;
00628 }
00629 
00630 /* The undefs list was designed so that in normal use we don't need to
00631    remove entries.  However, if symbols on the list are changed from
00632    bfd_link_hash_undefined to either bfd_link_hash_undefweak or
00633    bfd_link_hash_new for some reason, then they must be removed from the
00634    list.  Failure to do so might result in the linker attempting to add
00635    the symbol to the list again at a later stage.  */
00636 
00637 void
00638 bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
00639 {
00640   struct bfd_link_hash_entry **pun;
00641 
00642   pun = &table->undefs;
00643   while (*pun != NULL)
00644     {
00645       struct bfd_link_hash_entry *h = *pun;
00646 
00647       if (h->type == bfd_link_hash_new
00648          || h->type == bfd_link_hash_undefweak)
00649        {
00650          *pun = h->u.undef.next;
00651          h->u.undef.next = NULL;
00652          if (h == table->undefs_tail)
00653            {
00654              if (pun == &table->undefs)
00655               table->undefs_tail = NULL;
00656              else
00657               /* pun points at an u.undef.next field.  Go back to
00658                  the start of the link_hash_entry.  */
00659               table->undefs_tail = (struct bfd_link_hash_entry *)
00660                 ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
00661              break;
00662            }
00663        }
00664       else
00665        pun = &h->u.undef.next;
00666     }
00667 }
00668 
00669 /* Routine to create an entry in a generic link hash table.  */
00670 
00671 struct bfd_hash_entry *
00672 _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
00673                             struct bfd_hash_table *table,
00674                             const char *string)
00675 {
00676   /* Allocate the structure if it has not already been allocated by a
00677      subclass.  */
00678   if (entry == NULL)
00679     {
00680       entry =
00681        bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
00682       if (entry == NULL)
00683        return entry;
00684     }
00685 
00686   /* Call the allocation method of the superclass.  */
00687   entry = _bfd_link_hash_newfunc (entry, table, string);
00688   if (entry)
00689     {
00690       struct generic_link_hash_entry *ret;
00691 
00692       /* Set local fields.  */
00693       ret = (struct generic_link_hash_entry *) entry;
00694       ret->written = FALSE;
00695       ret->sym = NULL;
00696     }
00697 
00698   return entry;
00699 }
00700 
00701 /* Create a generic link hash table.  */
00702 
00703 struct bfd_link_hash_table *
00704 _bfd_generic_link_hash_table_create (bfd *abfd)
00705 {
00706   struct generic_link_hash_table *ret;
00707   bfd_size_type amt = sizeof (struct generic_link_hash_table);
00708 
00709   ret = bfd_malloc (amt);
00710   if (ret == NULL)
00711     return NULL;
00712   if (! _bfd_link_hash_table_init (&ret->root, abfd,
00713                                _bfd_generic_link_hash_newfunc,
00714                                sizeof (struct generic_link_hash_entry)))
00715     {
00716       free (ret);
00717       return NULL;
00718     }
00719   return &ret->root;
00720 }
00721 
00722 void
00723 _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
00724 {
00725   struct generic_link_hash_table *ret
00726     = (struct generic_link_hash_table *) hash;
00727 
00728   bfd_hash_table_free (&ret->root.table);
00729   free (ret);
00730 }
00731 
00732 /* Grab the symbols for an object file when doing a generic link.  We
00733    store the symbols in the outsymbols field.  We need to keep them
00734    around for the entire link to ensure that we only read them once.
00735    If we read them multiple times, we might wind up with relocs and
00736    the hash table pointing to different instances of the symbol
00737    structure.  */
00738 
00739 static bfd_boolean
00740 generic_link_read_symbols (bfd *abfd)
00741 {
00742   if (bfd_get_outsymbols (abfd) == NULL)
00743     {
00744       long symsize;
00745       long symcount;
00746 
00747       symsize = bfd_get_symtab_upper_bound (abfd);
00748       if (symsize < 0)
00749        return FALSE;
00750       bfd_get_outsymbols (abfd) = bfd_alloc (abfd, symsize);
00751       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
00752        return FALSE;
00753       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
00754       if (symcount < 0)
00755        return FALSE;
00756       bfd_get_symcount (abfd) = symcount;
00757     }
00758 
00759   return TRUE;
00760 }
00761 
00762 /* Generic function to add symbols to from an object file to the
00763    global hash table.  This version does not automatically collect
00764    constructors by name.  */
00765 
00766 bfd_boolean
00767 _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
00768 {
00769   return generic_link_add_symbols (abfd, info, FALSE);
00770 }
00771 
00772 /* Generic function to add symbols from an object file to the global
00773    hash table.  This version automatically collects constructors by
00774    name, as the collect2 program does.  It should be used for any
00775    target which does not provide some other mechanism for setting up
00776    constructors and destructors; these are approximately those targets
00777    for which gcc uses collect2 and do not support stabs.  */
00778 
00779 bfd_boolean
00780 _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
00781 {
00782   return generic_link_add_symbols (abfd, info, TRUE);
00783 }
00784 
00785 /* Indicate that we are only retrieving symbol values from this
00786    section.  We want the symbols to act as though the values in the
00787    file are absolute.  */
00788 
00789 void
00790 _bfd_generic_link_just_syms (asection *sec,
00791                           struct bfd_link_info *info ATTRIBUTE_UNUSED)
00792 {
00793   sec->output_section = bfd_abs_section_ptr;
00794   sec->output_offset = sec->vma;
00795 }
00796 
00797 /* Add symbols from an object file to the global hash table.  */
00798 
00799 static bfd_boolean
00800 generic_link_add_symbols (bfd *abfd,
00801                        struct bfd_link_info *info,
00802                        bfd_boolean collect)
00803 {
00804   bfd_boolean ret;
00805 
00806   switch (bfd_get_format (abfd))
00807     {
00808     case bfd_object:
00809       ret = generic_link_add_object_symbols (abfd, info, collect);
00810       break;
00811     case bfd_archive:
00812       ret = (_bfd_generic_link_add_archive_symbols
00813             (abfd, info,
00814              (collect
00815               ? generic_link_check_archive_element_collect
00816               : generic_link_check_archive_element_no_collect)));
00817       break;
00818     default:
00819       bfd_set_error (bfd_error_wrong_format);
00820       ret = FALSE;
00821     }
00822 
00823   return ret;
00824 }
00825 
00826 /* Add symbols from an object file to the global hash table.  */
00827 
00828 static bfd_boolean
00829 generic_link_add_object_symbols (bfd *abfd,
00830                              struct bfd_link_info *info,
00831                              bfd_boolean collect)
00832 {
00833   bfd_size_type symcount;
00834   struct bfd_symbol **outsyms;
00835 
00836   if (! generic_link_read_symbols (abfd))
00837     return FALSE;
00838   symcount = _bfd_generic_link_get_symcount (abfd);
00839   outsyms = _bfd_generic_link_get_symbols (abfd);
00840   return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
00841 }
00842 
00843 /* We build a hash table of all symbols defined in an archive.  */
00844 
00845 /* An archive symbol may be defined by multiple archive elements.
00846    This linked list is used to hold the elements.  */
00847 
00848 struct archive_list
00849 {
00850   struct archive_list *next;
00851   unsigned int indx;
00852 };
00853 
00854 /* An entry in an archive hash table.  */
00855 
00856 struct archive_hash_entry
00857 {
00858   struct bfd_hash_entry root;
00859   /* Where the symbol is defined.  */
00860   struct archive_list *defs;
00861 };
00862 
00863 /* An archive hash table itself.  */
00864 
00865 struct archive_hash_table
00866 {
00867   struct bfd_hash_table table;
00868 };
00869 
00870 /* Create a new entry for an archive hash table.  */
00871 
00872 static struct bfd_hash_entry *
00873 archive_hash_newfunc (struct bfd_hash_entry *entry,
00874                     struct bfd_hash_table *table,
00875                     const char *string)
00876 {
00877   struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
00878 
00879   /* Allocate the structure if it has not already been allocated by a
00880      subclass.  */
00881   if (ret == NULL)
00882     ret = bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
00883   if (ret == NULL)
00884     return NULL;
00885 
00886   /* Call the allocation method of the superclass.  */
00887   ret = ((struct archive_hash_entry *)
00888         bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
00889 
00890   if (ret)
00891     {
00892       /* Initialize the local fields.  */
00893       ret->defs = NULL;
00894     }
00895 
00896   return &ret->root;
00897 }
00898 
00899 /* Initialize an archive hash table.  */
00900 
00901 static bfd_boolean
00902 archive_hash_table_init
00903   (struct archive_hash_table *table,
00904    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
00905                                   struct bfd_hash_table *,
00906                                   const char *),
00907    unsigned int entsize)
00908 {
00909   return bfd_hash_table_init (&table->table, newfunc, entsize);
00910 }
00911 
00912 /* Look up an entry in an archive hash table.  */
00913 
00914 #define archive_hash_lookup(t, string, create, copy) \
00915   ((struct archive_hash_entry *) \
00916    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
00917 
00918 /* Allocate space in an archive hash table.  */
00919 
00920 #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
00921 
00922 /* Free an archive hash table.  */
00923 
00924 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
00925 
00926 /* Generic function to add symbols from an archive file to the global
00927    hash file.  This function presumes that the archive symbol table
00928    has already been read in (this is normally done by the
00929    bfd_check_format entry point).  It looks through the undefined and
00930    common symbols and searches the archive symbol table for them.  If
00931    it finds an entry, it includes the associated object file in the
00932    link.
00933 
00934    The old linker looked through the archive symbol table for
00935    undefined symbols.  We do it the other way around, looking through
00936    undefined symbols for symbols defined in the archive.  The
00937    advantage of the newer scheme is that we only have to look through
00938    the list of undefined symbols once, whereas the old method had to
00939    re-search the symbol table each time a new object file was added.
00940 
00941    The CHECKFN argument is used to see if an object file should be
00942    included.  CHECKFN should set *PNEEDED to TRUE if the object file
00943    should be included, and must also call the bfd_link_info
00944    add_archive_element callback function and handle adding the symbols
00945    to the global hash table.  CHECKFN should only return FALSE if some
00946    sort of error occurs.
00947 
00948    For some formats, such as a.out, it is possible to look through an
00949    object file but not actually include it in the link.  The
00950    archive_pass field in a BFD is used to avoid checking the symbols
00951    of an object files too many times.  When an object is included in
00952    the link, archive_pass is set to -1.  If an object is scanned but
00953    not included, archive_pass is set to the pass number.  The pass
00954    number is incremented each time a new object file is included.  The
00955    pass number is used because when a new object file is included it
00956    may create new undefined symbols which cause a previously examined
00957    object file to be included.  */
00958 
00959 bfd_boolean
00960 _bfd_generic_link_add_archive_symbols
00961   (bfd *abfd,
00962    struct bfd_link_info *info,
00963    bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
00964 {
00965   carsym *arsyms;
00966   carsym *arsym_end;
00967   register carsym *arsym;
00968   int pass;
00969   struct archive_hash_table arsym_hash;
00970   unsigned int indx;
00971   struct bfd_link_hash_entry **pundef;
00972 
00973   if (! bfd_has_map (abfd))
00974     {
00975       /* An empty archive is a special case.  */
00976       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
00977        return TRUE;
00978       bfd_set_error (bfd_error_no_armap);
00979       return FALSE;
00980     }
00981 
00982   arsyms = bfd_ardata (abfd)->symdefs;
00983   arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
00984 
00985   /* In order to quickly determine whether an symbol is defined in
00986      this archive, we build a hash table of the symbols.  */
00987   if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
00988                              sizeof (struct archive_hash_entry)))
00989     return FALSE;
00990   for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
00991     {
00992       struct archive_hash_entry *arh;
00993       struct archive_list *l, **pp;
00994 
00995       arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
00996       if (arh == NULL)
00997        goto error_return;
00998       l = ((struct archive_list *)
00999           archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
01000       if (l == NULL)
01001        goto error_return;
01002       l->indx = indx;
01003       for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
01004        ;
01005       *pp = l;
01006       l->next = NULL;
01007     }
01008 
01009   /* The archive_pass field in the archive itself is used to
01010      initialize PASS, sine we may search the same archive multiple
01011      times.  */
01012   pass = abfd->archive_pass + 1;
01013 
01014   /* New undefined symbols are added to the end of the list, so we
01015      only need to look through it once.  */
01016   pundef = &info->hash->undefs;
01017   while (*pundef != NULL)
01018     {
01019       struct bfd_link_hash_entry *h;
01020       struct archive_hash_entry *arh;
01021       struct archive_list *l;
01022 
01023       h = *pundef;
01024 
01025       /* When a symbol is defined, it is not necessarily removed from
01026         the list.  */
01027       if (h->type != bfd_link_hash_undefined
01028          && h->type != bfd_link_hash_common)
01029        {
01030          /* Remove this entry from the list, for general cleanliness
01031             and because we are going to look through the list again
01032             if we search any more libraries.  We can't remove the
01033             entry if it is the tail, because that would lose any
01034             entries we add to the list later on (it would also cause
01035             us to lose track of whether the symbol has been
01036             referenced).  */
01037          if (*pundef != info->hash->undefs_tail)
01038            *pundef = (*pundef)->u.undef.next;
01039          else
01040            pundef = &(*pundef)->u.undef.next;
01041          continue;
01042        }
01043 
01044       /* Look for this symbol in the archive symbol map.  */
01045       arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
01046       if (arh == NULL)
01047        {
01048          /* If we haven't found the exact symbol we're looking for,
01049             let's look for its import thunk */
01050          if (info->pei386_auto_import)
01051            {
01052              bfd_size_type amt = strlen (h->root.string) + 10;
01053              char *buf = bfd_malloc (amt);
01054              if (buf == NULL)
01055               return FALSE;
01056 
01057              sprintf (buf, "__imp_%s", h->root.string);
01058              arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
01059              free(buf);
01060            }
01061          if (arh == NULL)
01062            {
01063              pundef = &(*pundef)->u.undef.next;
01064              continue;
01065            }
01066        }
01067       /* Look at all the objects which define this symbol.  */
01068       for (l = arh->defs; l != NULL; l = l->next)
01069        {
01070          bfd *element;
01071          bfd_boolean needed;
01072 
01073          /* If the symbol has gotten defined along the way, quit.  */
01074          if (h->type != bfd_link_hash_undefined
01075              && h->type != bfd_link_hash_common)
01076            break;
01077 
01078          element = bfd_get_elt_at_index (abfd, l->indx);
01079          if (element == NULL)
01080            goto error_return;
01081 
01082          /* If we've already included this element, or if we've
01083             already checked it on this pass, continue.  */
01084          if (element->archive_pass == -1
01085              || element->archive_pass == pass)
01086            continue;
01087 
01088          /* If we can't figure this element out, just ignore it.  */
01089          if (! bfd_check_format (element, bfd_object))
01090            {
01091              element->archive_pass = -1;
01092              continue;
01093            }
01094 
01095          /* CHECKFN will see if this element should be included, and
01096             go ahead and include it if appropriate.  */
01097          if (! (*checkfn) (element, info, &needed))
01098            goto error_return;
01099 
01100          if (! needed)
01101            element->archive_pass = pass;
01102          else
01103            {
01104              element->archive_pass = -1;
01105 
01106              /* Increment the pass count to show that we may need to
01107                recheck object files which were already checked.  */
01108              ++pass;
01109            }
01110        }
01111 
01112       pundef = &(*pundef)->u.undef.next;
01113     }
01114 
01115   archive_hash_table_free (&arsym_hash);
01116 
01117   /* Save PASS in case we are called again.  */
01118   abfd->archive_pass = pass;
01119 
01120   return TRUE;
01121 
01122  error_return:
01123   archive_hash_table_free (&arsym_hash);
01124   return FALSE;
01125 }
01126 
01127 /* See if we should include an archive element.  This version is used
01128    when we do not want to automatically collect constructors based on
01129    the symbol name, presumably because we have some other mechanism
01130    for finding them.  */
01131 
01132 static bfd_boolean
01133 generic_link_check_archive_element_no_collect (
01134                                           bfd *abfd,
01135                                           struct bfd_link_info *info,
01136                                           bfd_boolean *pneeded)
01137 {
01138   return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
01139 }
01140 
01141 /* See if we should include an archive element.  This version is used
01142    when we want to automatically collect constructors based on the
01143    symbol name, as collect2 does.  */
01144 
01145 static bfd_boolean
01146 generic_link_check_archive_element_collect (bfd *abfd,
01147                                        struct bfd_link_info *info,
01148                                        bfd_boolean *pneeded)
01149 {
01150   return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
01151 }
01152 
01153 /* See if we should include an archive element.  Optionally collect
01154    constructors.  */
01155 
01156 static bfd_boolean
01157 generic_link_check_archive_element (bfd *abfd,
01158                                 struct bfd_link_info *info,
01159                                 bfd_boolean *pneeded,
01160                                 bfd_boolean collect)
01161 {
01162   asymbol **pp, **ppend;
01163 
01164   *pneeded = FALSE;
01165 
01166   if (! generic_link_read_symbols (abfd))
01167     return FALSE;
01168 
01169   pp = _bfd_generic_link_get_symbols (abfd);
01170   ppend = pp + _bfd_generic_link_get_symcount (abfd);
01171   for (; pp < ppend; pp++)
01172     {
01173       asymbol *p;
01174       struct bfd_link_hash_entry *h;
01175 
01176       p = *pp;
01177 
01178       /* We are only interested in globally visible symbols.  */
01179       if (! bfd_is_com_section (p->section)
01180          && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
01181        continue;
01182 
01183       /* We are only interested if we know something about this
01184         symbol, and it is undefined or common.  An undefined weak
01185         symbol (type bfd_link_hash_undefweak) is not considered to be
01186         a reference when pulling files out of an archive.  See the
01187         SVR4 ABI, p. 4-27.  */
01188       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
01189                             FALSE, TRUE);
01190       if (h == NULL
01191          || (h->type != bfd_link_hash_undefined
01192              && h->type != bfd_link_hash_common))
01193        continue;
01194 
01195       /* P is a symbol we are looking for.  */
01196 
01197       if (! bfd_is_com_section (p->section))
01198        {
01199          bfd_size_type symcount;
01200          asymbol **symbols;
01201 
01202          /* This object file defines this symbol, so pull it in.  */
01203          if (! (*info->callbacks->add_archive_element) (info, abfd,
01204                                                   bfd_asymbol_name (p)))
01205            return FALSE;
01206          symcount = _bfd_generic_link_get_symcount (abfd);
01207          symbols = _bfd_generic_link_get_symbols (abfd);
01208          if (! generic_link_add_symbol_list (abfd, info, symcount,
01209                                          symbols, collect))
01210            return FALSE;
01211          *pneeded = TRUE;
01212          return TRUE;
01213        }
01214 
01215       /* P is a common symbol.  */
01216 
01217       if (h->type == bfd_link_hash_undefined)
01218        {
01219          bfd *symbfd;
01220          bfd_vma size;
01221          unsigned int power;
01222 
01223          symbfd = h->u.undef.abfd;
01224          if (symbfd == NULL)
01225            {
01226              /* This symbol was created as undefined from outside
01227                BFD.  We assume that we should link in the object
01228                file.  This is for the -u option in the linker.  */
01229              if (! (*info->callbacks->add_archive_element)
01230                 (info, abfd, bfd_asymbol_name (p)))
01231               return FALSE;
01232              *pneeded = TRUE;
01233              return TRUE;
01234            }
01235 
01236          /* Turn the symbol into a common symbol but do not link in
01237             the object file.  This is how a.out works.  Object
01238             formats that require different semantics must implement
01239             this function differently.  This symbol is already on the
01240             undefs list.  We add the section to a common section
01241             attached to symbfd to ensure that it is in a BFD which
01242             will be linked in.  */
01243          h->type = bfd_link_hash_common;
01244          h->u.c.p =
01245            bfd_hash_allocate (&info->hash->table,
01246                             sizeof (struct bfd_link_hash_common_entry));
01247          if (h->u.c.p == NULL)
01248            return FALSE;
01249 
01250          size = bfd_asymbol_value (p);
01251          h->u.c.size = size;
01252 
01253          power = bfd_log2 (size);
01254          if (power > 4)
01255            power = 4;
01256          h->u.c.p->alignment_power = power;
01257 
01258          if (p->section == bfd_com_section_ptr)
01259            h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
01260          else
01261            h->u.c.p->section = bfd_make_section_old_way (symbfd,
01262                                                    p->section->name);
01263          h->u.c.p->section->flags = SEC_ALLOC;
01264        }
01265       else
01266        {
01267          /* Adjust the size of the common symbol if necessary.  This
01268             is how a.out works.  Object formats that require
01269             different semantics must implement this function
01270             differently.  */
01271          if (bfd_asymbol_value (p) > h->u.c.size)
01272            h->u.c.size = bfd_asymbol_value (p);
01273        }
01274     }
01275 
01276   /* This archive element is not needed.  */
01277   return TRUE;
01278 }
01279 
01280 /* Add the symbols from an object file to the global hash table.  ABFD
01281    is the object file.  INFO is the linker information.  SYMBOL_COUNT
01282    is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
01283    is TRUE if constructors should be automatically collected by name
01284    as is done by collect2.  */
01285 
01286 static bfd_boolean
01287 generic_link_add_symbol_list (bfd *abfd,
01288                            struct bfd_link_info *info,
01289                            bfd_size_type symbol_count,
01290                            asymbol **symbols,
01291                            bfd_boolean collect)
01292 {
01293   asymbol **pp, **ppend;
01294 
01295   pp = symbols;
01296   ppend = symbols + symbol_count;
01297   for (; pp < ppend; pp++)
01298     {
01299       asymbol *p;
01300 
01301       p = *pp;
01302 
01303       if ((p->flags & (BSF_INDIRECT
01304                      | BSF_WARNING
01305                      | BSF_GLOBAL
01306                      | BSF_CONSTRUCTOR
01307                      | BSF_WEAK)) != 0
01308          || bfd_is_und_section (bfd_get_section (p))
01309          || bfd_is_com_section (bfd_get_section (p))
01310          || bfd_is_ind_section (bfd_get_section (p)))
01311        {
01312          const char *name;
01313          const char *string;
01314          struct generic_link_hash_entry *h;
01315          struct bfd_link_hash_entry *bh;
01316 
01317          name = bfd_asymbol_name (p);
01318          if (((p->flags & BSF_INDIRECT) != 0
01319               || bfd_is_ind_section (p->section))
01320              && pp + 1 < ppend)
01321            {
01322              pp++;
01323              string = bfd_asymbol_name (*pp);
01324            }
01325          else if ((p->flags & BSF_WARNING) != 0
01326                  && pp + 1 < ppend)
01327            {
01328              /* The name of P is actually the warning string, and the
01329                next symbol is the one to warn about.  */
01330              string = name;
01331              pp++;
01332              name = bfd_asymbol_name (*pp);
01333            }
01334          else
01335            string = NULL;
01336 
01337          bh = NULL;
01338          if (! (_bfd_generic_link_add_one_symbol
01339                (info, abfd, name, p->flags, bfd_get_section (p),
01340                 p->value, string, FALSE, collect, &bh)))
01341            return FALSE;
01342          h = (struct generic_link_hash_entry *) bh;
01343 
01344          /* If this is a constructor symbol, and the linker didn't do
01345              anything with it, then we want to just pass the symbol
01346              through to the output file.  This will happen when
01347              linking with -r.  */
01348          if ((p->flags & BSF_CONSTRUCTOR) != 0
01349              && (h == NULL || h->root.type == bfd_link_hash_new))
01350            {
01351              p->udata.p = NULL;
01352              continue;
01353            }
01354 
01355          /* Save the BFD symbol so that we don't lose any backend
01356             specific information that may be attached to it.  We only
01357             want this one if it gives more information than the
01358             existing one; we don't want to replace a defined symbol
01359             with an undefined one.  This routine may be called with a
01360             hash table other than the generic hash table, so we only
01361             do this if we are certain that the hash table is a
01362             generic one.  */
01363          if (info->hash->creator == abfd->xvec)
01364            {
01365              if (h->sym == NULL
01366                 || (! bfd_is_und_section (bfd_get_section (p))
01367                     && (! bfd_is_com_section (bfd_get_section (p))
01368                        || bfd_is_und_section (bfd_get_section (h->sym)))))
01369               {
01370                 h->sym = p;
01371                 /* BSF_OLD_COMMON is a hack to support COFF reloc
01372                    reading, and it should go away when the COFF
01373                    linker is switched to the new version.  */
01374                 if (bfd_is_com_section (bfd_get_section (p)))
01375                   p->flags |= BSF_OLD_COMMON;
01376               }
01377            }
01378 
01379          /* Store a back pointer from the symbol to the hash
01380             table entry for the benefit of relaxation code until
01381             it gets rewritten to not use asymbol structures.
01382             Setting this is also used to check whether these
01383             symbols were set up by the generic linker.  */
01384          p->udata.p = h;
01385        }
01386     }
01387 
01388   return TRUE;
01389 }
01390 
01391 /* We use a state table to deal with adding symbols from an object
01392    file.  The first index into the state table describes the symbol
01393    from the object file.  The second index into the state table is the
01394    type of the symbol in the hash table.  */
01395 
01396 /* The symbol from the object file is turned into one of these row
01397    values.  */
01398 
01399 enum link_row
01400 {
01401   UNDEF_ROW,         /* Undefined.  */
01402   UNDEFW_ROW,        /* Weak undefined.  */
01403   DEF_ROW,           /* Defined.  */
01404   DEFW_ROW,          /* Weak defined.  */
01405   COMMON_ROW,        /* Common.  */
01406   INDR_ROW,          /* Indirect.  */
01407   WARN_ROW,          /* Warning.  */
01408   SET_ROW            /* Member of set.  */
01409 };
01410 
01411 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
01412 #undef FAIL
01413 
01414 /* The actions to take in the state table.  */
01415 
01416 enum link_action
01417 {
01418   FAIL,              /* Abort.  */
01419   UND,        /* Mark symbol undefined.  */
01420   WEAK,              /* Mark symbol weak undefined.  */
01421   DEF,        /* Mark symbol defined.  */
01422   DEFW,              /* Mark symbol weak defined.  */
01423   COM,        /* Mark symbol common.  */
01424   REF,        /* Mark defined symbol referenced.  */
01425   CREF,              /* Possibly warn about common reference to defined symbol.  */
01426   CDEF,              /* Define existing common symbol.  */
01427   NOACT,      /* No action.  */
01428   BIG,        /* Mark symbol common using largest size.  */
01429   MDEF,              /* Multiple definition error.  */
01430   MIND,              /* Multiple indirect symbols.  */
01431   IND,        /* Make indirect symbol.  */
01432   CIND,              /* Make indirect symbol from existing common symbol.  */
01433   SET,        /* Add value to set.  */
01434   MWARN,      /* Make warning symbol.  */
01435   WARN,              /* Issue warning.  */
01436   CWARN,      /* Warn if referenced, else MWARN.  */
01437   CYCLE,      /* Repeat with symbol pointed to.  */
01438   REFC,              /* Mark indirect symbol referenced and then CYCLE.  */
01439   WARNC              /* Issue warning and then CYCLE.  */
01440 };
01441 
01442 /* The state table itself.  The first index is a link_row and the
01443    second index is a bfd_link_hash_type.  */
01444 
01445 static const enum link_action link_action[8][8] =
01446 {
01447   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
01448   /* UNDEF_ROW       */  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
01449   /* UNDEFW_ROW      */  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
01450   /* DEF_ROW  */  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
01451   /* DEFW_ROW        */  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
01452   /* COMMON_ROW      */  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
01453   /* INDR_ROW */  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
01454   /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, NOACT },
01455   /* SET_ROW  */  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
01456 };
01457 
01458 /* Most of the entries in the LINK_ACTION table are straightforward,
01459    but a few are somewhat subtle.
01460 
01461    A reference to an indirect symbol (UNDEF_ROW/indr or
01462    UNDEFW_ROW/indr) is counted as a reference both to the indirect
01463    symbol and to the symbol the indirect symbol points to.
01464 
01465    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
01466    causes the warning to be issued.
01467 
01468    A common definition of an indirect symbol (COMMON_ROW/indr) is
01469    treated as a multiple definition error.  Likewise for an indirect
01470    definition of a common symbol (INDR_ROW/com).
01471 
01472    An indirect definition of a warning (INDR_ROW/warn) does not cause
01473    the warning to be issued.
01474 
01475    If a warning is created for an indirect symbol (WARN_ROW/indr) no
01476    warning is created for the symbol the indirect symbol points to.
01477 
01478    Adding an entry to a set does not count as a reference to a set,
01479    and no warning is issued (SET_ROW/warn).  */
01480 
01481 /* Return the BFD in which a hash entry has been defined, if known.  */
01482 
01483 static bfd *
01484 hash_entry_bfd (struct bfd_link_hash_entry *h)
01485 {
01486   while (h->type == bfd_link_hash_warning)
01487     h = h->u.i.link;
01488   switch (h->type)
01489     {
01490     default:
01491       return NULL;
01492     case bfd_link_hash_undefined:
01493     case bfd_link_hash_undefweak:
01494       return h->u.undef.abfd;
01495     case bfd_link_hash_defined:
01496     case bfd_link_hash_defweak:
01497       return h->u.def.section->owner;
01498     case bfd_link_hash_common:
01499       return h->u.c.p->section->owner;
01500     }
01501   /*NOTREACHED*/
01502 }
01503 
01504 /* Add a symbol to the global hash table.
01505    ABFD is the BFD the symbol comes from.
01506    NAME is the name of the symbol.
01507    FLAGS is the BSF_* bits associated with the symbol.
01508    SECTION is the section in which the symbol is defined; this may be
01509      bfd_und_section_ptr or bfd_com_section_ptr.
01510    VALUE is the value of the symbol, relative to the section.
01511    STRING is used for either an indirect symbol, in which case it is
01512      the name of the symbol to indirect to, or a warning symbol, in
01513      which case it is the warning string.
01514    COPY is TRUE if NAME or STRING must be copied into locally
01515      allocated memory if they need to be saved.
01516    COLLECT is TRUE if we should automatically collect gcc constructor
01517      or destructor names as collect2 does.
01518    HASHP, if not NULL, is a place to store the created hash table
01519      entry; if *HASHP is not NULL, the caller has already looked up
01520      the hash table entry, and stored it in *HASHP.  */
01521 
01522 bfd_boolean
01523 _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
01524                               bfd *abfd,
01525                               const char *name,
01526                               flagword flags,
01527                               asection *section,
01528                               bfd_vma value,
01529                               const char *string,
01530                               bfd_boolean copy,
01531                               bfd_boolean collect,
01532                               struct bfd_link_hash_entry **hashp)
01533 {
01534   enum link_row row;
01535   struct bfd_link_hash_entry *h;
01536   bfd_boolean cycle;
01537 
01538   if (bfd_is_ind_section (section)
01539       || (flags & BSF_INDIRECT) != 0)
01540     row = INDR_ROW;
01541   else if ((flags & BSF_WARNING) != 0)
01542     row = WARN_ROW;
01543   else if ((flags & BSF_CONSTRUCTOR) != 0)
01544     row = SET_ROW;
01545   else if (bfd_is_und_section (section))
01546     {
01547       if ((flags & BSF_WEAK) != 0)
01548        row = UNDEFW_ROW;
01549       else
01550        row = UNDEF_ROW;
01551     }
01552   else if ((flags & BSF_WEAK) != 0)
01553     row = DEFW_ROW;
01554   else if (bfd_is_com_section (section))
01555     row = COMMON_ROW;
01556   else
01557     row = DEF_ROW;
01558 
01559   if (hashp != NULL && *hashp != NULL)
01560     h = *hashp;
01561   else
01562     {
01563       if (row == UNDEF_ROW || row == UNDEFW_ROW)
01564        h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
01565       else
01566        h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
01567       if (h == NULL)
01568        {
01569          if (hashp != NULL)
01570            *hashp = NULL;
01571          return FALSE;
01572        }
01573     }
01574 
01575   if (info->notice_all
01576       || (info->notice_hash != NULL
01577          && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
01578     {
01579       if (! (*info->callbacks->notice) (info, h->root.string, abfd, section,
01580                                    value))
01581        return FALSE;
01582     }
01583 
01584   if (hashp != NULL)
01585     *hashp = h;
01586 
01587   do
01588     {
01589       enum link_action action;
01590 
01591       cycle = FALSE;
01592       action = link_action[(int) row][(int) h->type];
01593       switch (action)
01594        {
01595        case FAIL:
01596          abort ();
01597 
01598        case NOACT:
01599          /* Do nothing.  */
01600          break;
01601 
01602        case UND:
01603          /* Make a new undefined symbol.  */
01604          h->type = bfd_link_hash_undefined;
01605          h->u.undef.abfd = abfd;
01606          bfd_link_add_undef (info->hash, h);
01607          break;
01608 
01609        case WEAK:
01610          /* Make a new weak undefined symbol.  */
01611          h->type = bfd_link_hash_undefweak;
01612          h->u.undef.abfd = abfd;
01613          h->u.undef.weak = abfd;
01614          break;
01615 
01616        case CDEF:
01617          /* We have found a definition for a symbol which was
01618             previously common.  */
01619          BFD_ASSERT (h->type == bfd_link_hash_common);
01620          if (! ((*info->callbacks->multiple_common)
01621                (info, h->root.string,
01622                 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
01623                 abfd, bfd_link_hash_defined, 0)))
01624            return FALSE;
01625          /* Fall through.  */
01626        case DEF:
01627        case DEFW:
01628          {
01629            enum bfd_link_hash_type oldtype;
01630 
01631            /* Define a symbol.  */
01632            oldtype = h->type;
01633            if (action == DEFW)
01634              h->type = bfd_link_hash_defweak;
01635            else
01636              h->type = bfd_link_hash_defined;
01637            h->u.def.section = section;
01638            h->u.def.value = value;
01639 
01640            /* If we have been asked to, we act like collect2 and
01641               identify all functions that might be global
01642               constructors and destructors and pass them up in a
01643               callback.  We only do this for certain object file
01644               types, since many object file types can handle this
01645               automatically.  */
01646            if (collect && name[0] == '_')
01647              {
01648               const char *s;
01649 
01650               /* A constructor or destructor name starts like this:
01651                  _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
01652                  the second are the same character (we accept any
01653                  character there, in case a new object file format
01654                  comes along with even worse naming restrictions).  */
01655 
01656 #define CONS_PREFIX "GLOBAL_"
01657 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
01658 
01659               s = name + 1;
01660               while (*s == '_')
01661                 ++s;
01662               if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
01663                 {
01664                   char c;
01665 
01666                   c = s[CONS_PREFIX_LEN + 1];
01667                   if ((c == 'I' || c == 'D')
01668                      && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
01669                     {
01670                      /* If this is a definition of a symbol which
01671                            was previously weakly defined, we are in
01672                            trouble.  We have already added a
01673                            constructor entry for the weak defined
01674                            symbol, and now we are trying to add one
01675                            for the new symbol.  Fortunately, this case
01676                            should never arise in practice.  */
01677                      if (oldtype == bfd_link_hash_defweak)
01678                        abort ();
01679 
01680                      if (! ((*info->callbacks->constructor)
01681                             (info, c == 'I',
01682                             h->root.string, abfd, section, value)))
01683                        return FALSE;
01684                     }
01685                 }
01686              }
01687          }
01688 
01689          break;
01690 
01691        case COM:
01692          /* We have found a common definition for a symbol.  */
01693          if (h->type == bfd_link_hash_new)
01694            bfd_link_add_undef (info->hash, h);
01695          h->type = bfd_link_hash_common;
01696          h->u.c.p =
01697            bfd_hash_allocate (&info->hash->table,
01698                             sizeof (struct bfd_link_hash_common_entry));
01699          if (h->u.c.p == NULL)
01700            return FALSE;
01701 
01702          h->u.c.size = value;
01703 
01704          /* Select a default alignment based on the size.  This may
01705              be overridden by the caller.  */
01706          {
01707            unsigned int power;
01708 
01709            power = bfd_log2 (value);
01710            if (power > 4)
01711              power = 4;
01712            h->u.c.p->alignment_power = power;
01713          }
01714 
01715          /* The section of a common symbol is only used if the common
01716              symbol is actually allocated.  It basically provides a
01717              hook for the linker script to decide which output section
01718              the common symbols should be put in.  In most cases, the
01719              section of a common symbol will be bfd_com_section_ptr,
01720              the code here will choose a common symbol section named
01721              "COMMON", and the linker script will contain *(COMMON) in
01722              the appropriate place.  A few targets use separate common
01723              sections for small symbols, and they require special
01724              handling.  */
01725          if (section == bfd_com_section_ptr)
01726            {
01727              h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
01728              h->u.c.p->section->flags = SEC_ALLOC;
01729            }
01730          else if (section->owner != abfd)
01731            {
01732              h->u.c.p->section = bfd_make_section_old_way (abfd,
01733                                                      section->name);
01734              h->u.c.p->section->flags = SEC_ALLOC;
01735            }
01736          else
01737            h->u.c.p->section = section;
01738          break;
01739 
01740        case REF:
01741          /* A reference to a defined symbol.  */
01742          if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
01743            h->u.undef.next = h;
01744          break;
01745 
01746        case BIG:
01747          /* We have found a common definition for a symbol which
01748             already had a common definition.  Use the maximum of the
01749             two sizes, and use the section required by the larger symbol.  */
01750          BFD_ASSERT (h->type == bfd_link_hash_common);
01751          if (! ((*info->callbacks->multiple_common)
01752                (info, h->root.string,
01753                 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
01754                 abfd, bfd_link_hash_common, value)))
01755            return FALSE;
01756          if (value > h->u.c.size)
01757            {
01758              unsigned int power;
01759 
01760              h->u.c.size = value;
01761 
01762              /* Select a default alignment based on the size.  This may
01763                be overridden by the caller.  */
01764              power = bfd_log2 (value);
01765              if (power > 4)
01766               power = 4;
01767              h->u.c.p->alignment_power = power;
01768 
01769              /* Some systems have special treatment for small commons,
01770                hence we want to select the section used by the larger
01771                symbol.  This makes sure the symbol does not go in a
01772                small common section if it is now too large.  */
01773              if (section == bfd_com_section_ptr)
01774               {
01775                 h->u.c.p->section
01776                   = bfd_make_section_old_way (abfd, "COMMON");
01777                 h->u.c.p->section->flags = SEC_ALLOC;
01778               }
01779              else if (section->owner != abfd)
01780               {
01781                 h->u.c.p->section
01782                   = bfd_make_section_old_way (abfd, section->name);
01783                 h->u.c.p->section->flags = SEC_ALLOC;
01784               }
01785              else
01786               h->u.c.p->section = section;
01787            }
01788          break;
01789 
01790        case CREF:
01791          {
01792            bfd *obfd;
01793 
01794            /* We have found a common definition for a symbol which
01795               was already defined.  FIXME: It would nice if we could
01796               report the BFD which defined an indirect symbol, but we
01797               don't have anywhere to store the information.  */
01798            if (h->type == bfd_link_hash_defined
01799               || h->type == bfd_link_hash_defweak)
01800              obfd = h->u.def.section->owner;
01801            else
01802              obfd = NULL;
01803            if (! ((*info->callbacks->multiple_common)
01804                  (info, h->root.string, obfd, h->type, 0,
01805                   abfd, bfd_link_hash_common, value)))
01806              return FALSE;
01807          }
01808          break;
01809 
01810        case MIND:
01811          /* Multiple indirect symbols.  This is OK if they both point
01812             to the same symbol.  */
01813          if (strcmp (h->u.i.link->root.string, string) == 0)
01814            break;
01815          /* Fall through.  */
01816        case MDEF:
01817          /* Handle a multiple definition.  */
01818          if (!info->allow_multiple_definition)
01819            {
01820              asection *msec = NULL;
01821              bfd_vma mval = 0;
01822 
01823              switch (h->type)
01824               {
01825               case bfd_link_hash_defined:
01826                 msec = h->u.def.section;
01827                 mval = h->u.def.value;
01828                 break;
01829                case bfd_link_hash_indirect:
01830                 msec = bfd_ind_section_ptr;
01831                 mval = 0;
01832                 break;
01833               default:
01834                 abort ();
01835               }
01836 
01837              /* Ignore a redefinition of an absolute symbol to the
01838                same value; it's harmless.  */
01839              if (h->type == bfd_link_hash_defined
01840                 && bfd_is_abs_section (msec)
01841                 && bfd_is_abs_section (section)
01842                 && value == mval)
01843               break;
01844 
01845              if (! ((*info->callbacks->multiple_definition)
01846                    (info, h->root.string, msec->owner, msec, mval,
01847                     abfd, section, value)))
01848               return FALSE;
01849            }
01850          break;
01851 
01852        case CIND:
01853          /* Create an indirect symbol from an existing common symbol.  */
01854          BFD_ASSERT (h->type == bfd_link_hash_common);
01855          if (! ((*info->callbacks->multiple_common)
01856                (info, h->root.string,
01857                 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
01858                 abfd, bfd_link_hash_indirect, 0)))
01859            return FALSE;
01860          /* Fall through.  */
01861        case IND:
01862          /* Create an indirect symbol.  */
01863          {
01864            struct bfd_link_hash_entry *inh;
01865 
01866            /* STRING is the name of the symbol we want to indirect
01867               to.  */
01868            inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
01869                                           copy, FALSE);
01870            if (inh == NULL)
01871              return FALSE;
01872            if (inh->type == bfd_link_hash_indirect
01873               && inh->u.i.link == h)
01874              {
01875               (*_bfd_error_handler)
01876                 (_("%B: indirect symbol `%s' to `%s' is a loop"),
01877                  abfd, name, string);
01878               bfd_set_error (bfd_error_invalid_operation);
01879               return FALSE;
01880              }
01881            if (inh->type == bfd_link_hash_new)
01882              {
01883               inh->type = bfd_link_hash_undefined;
01884               inh->u.undef.abfd = abfd;
01885               bfd_link_add_undef (info->hash, inh);
01886              }
01887 
01888            /* If the indirect symbol has been referenced, we need to
01889               push the reference down to the symbol we are
01890               referencing.  */
01891            if (h->type != bfd_link_hash_new)
01892              {
01893               row = UNDEF_ROW;
01894               cycle = TRUE;
01895              }
01896 
01897            h->type = bfd_link_hash_indirect;
01898            h->u.i.link = inh;
01899          }
01900          break;
01901 
01902        case SET:
01903          /* Add an entry to a set.  */
01904          if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
01905                                           abfd, section, value))
01906            return FALSE;
01907          break;
01908 
01909        case WARNC:
01910          /* Issue a warning and cycle.  */
01911          if (h->u.i.warning != NULL)
01912            {
01913              if (! (*info->callbacks->warning) (info, h->u.i.warning,
01914                                            h->root.string, abfd,
01915                                            NULL, 0))
01916               return FALSE;
01917              /* Only issue a warning once.  */
01918              h->u.i.warning = NULL;
01919            }
01920          /* Fall through.  */
01921        case CYCLE:
01922          /* Try again with the referenced symbol.  */
01923          h = h->u.i.link;
01924          cycle = TRUE;
01925          break;
01926 
01927        case REFC:
01928          /* A reference to an indirect symbol.  */
01929          if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
01930            h->u.undef.next = h;
01931          h = h->u.i.link;
01932          cycle = TRUE;
01933          break;
01934 
01935        case WARN:
01936          /* Issue a warning.  */
01937          if (! (*info->callbacks->warning) (info, string, h->root.string,
01938                                         hash_entry_bfd (h), NULL, 0))
01939            return FALSE;
01940          break;
01941 
01942        case CWARN:
01943          /* Warn if this symbol has been referenced already,
01944             otherwise add a warning.  A symbol has been referenced if
01945             the u.undef.next field is not NULL, or it is the tail of the
01946             undefined symbol list.  The REF case above helps to
01947             ensure this.  */
01948          if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
01949            {
01950              if (! (*info->callbacks->warning) (info, string, h->root.string,
01951                                            hash_entry_bfd (h), NULL, 0))
01952               return FALSE;
01953              break;
01954            }
01955          /* Fall through.  */
01956        case MWARN:
01957          /* Make a warning symbol.  */
01958          {
01959            struct bfd_link_hash_entry *sub;
01960 
01961            /* STRING is the warning to give.  */
01962            sub = ((struct bfd_link_hash_entry *)
01963                  ((*info->hash->table.newfunc)
01964                   (NULL, &info->hash->table, h->root.string)));
01965            if (sub == NULL)
01966              return FALSE;
01967            *sub = *h;
01968            sub->type = bfd_link_hash_warning;
01969            sub->u.i.link = h;
01970            if (! copy)
01971              sub->u.i.warning = string;
01972            else
01973              {
01974               char *w;
01975               size_t len = strlen (string) + 1;
01976 
01977               w = bfd_hash_allocate (&info->hash->table, len);
01978               if (w == NULL)
01979                 return FALSE;
01980               memcpy (w, string, len);
01981               sub->u.i.warning = w;
01982              }
01983 
01984            bfd_hash_replace (&info->hash->table,
01985                            (struct bfd_hash_entry *) h,
01986                            (struct bfd_hash_entry *) sub);
01987            if (hashp != NULL)
01988              *hashp = sub;
01989          }
01990          break;
01991        }
01992     }
01993   while (cycle);
01994 
01995   return TRUE;
01996 }
01997 
01998 /* Generic final link routine.  */
01999 
02000 bfd_boolean
02001 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
02002 {
02003   bfd *sub;
02004   asection *o;
02005   struct bfd_link_order *p;
02006   size_t outsymalloc;
02007   struct generic_write_global_symbol_info wginfo;
02008 
02009   bfd_get_outsymbols (abfd) = NULL;
02010   bfd_get_symcount (abfd) = 0;
02011   outsymalloc = 0;
02012 
02013   /* Mark all sections which will be included in the output file.  */
02014   for (o = abfd->sections; o != NULL; o = o->next)
02015     for (p = o->map_head.link_order; p != NULL; p = p->next)
02016       if (p->type == bfd_indirect_link_order)
02017        p->u.indirect.section->linker_mark = TRUE;
02018 
02019   /* Build the output symbol table.  */
02020   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
02021     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
02022       return FALSE;
02023 
02024   /* Accumulate the global symbols.  */
02025   wginfo.info = info;
02026   wginfo.output_bfd = abfd;
02027   wginfo.psymalloc = &outsymalloc;
02028   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
02029                                _bfd_generic_link_write_global_symbol,
02030                                &wginfo);
02031 
02032   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
02033      shouldn't really need one, since we have SYMCOUNT, but some old
02034      code still expects one.  */
02035   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
02036     return FALSE;
02037 
02038   if (info->relocatable)
02039     {
02040       /* Allocate space for the output relocs for each section.  */
02041       for (o = abfd->sections; o != NULL; o = o->next)
02042        {
02043          o->reloc_count = 0;
02044          for (p = o->map_head.link_order; p != NULL; p = p->next)
02045            {
02046              if (p->type == bfd_section_reloc_link_order
02047                 || p->type == bfd_symbol_reloc_link_order)
02048               ++o->reloc_count;
02049              else if (p->type == bfd_indirect_link_order)
02050               {
02051                 asection *input_section;
02052                 bfd *input_bfd;
02053                 long relsize;
02054                 arelent **relocs;
02055                 asymbol **symbols;
02056                 long reloc_count;
02057 
02058                 input_section = p->u.indirect.section;
02059                 input_bfd = input_section->owner;
02060                 relsize = bfd_get_reloc_upper_bound (input_bfd,
02061                                                  input_section);
02062                 if (relsize < 0)
02063                   return FALSE;
02064                 relocs = bfd_malloc (relsize);
02065                 if (!relocs && relsize != 0)
02066                   return FALSE;
02067                 symbols = _bfd_generic_link_get_symbols (input_bfd);
02068                 reloc_count = bfd_canonicalize_reloc (input_bfd,
02069                                                  input_section,
02070                                                  relocs,
02071                                                  symbols);
02072                 free (relocs);
02073                 if (reloc_count < 0)
02074                   return FALSE;
02075                 BFD_ASSERT ((unsigned long) reloc_count
02076                            == input_section->reloc_count);
02077                 o->reloc_count += reloc_count;
02078               }
02079            }
02080          if (o->reloc_count > 0)
02081            {
02082              bfd_size_type amt;
02083 
02084              amt = o->reloc_count;
02085              amt *= sizeof (arelent *);
02086              o->orelocation = bfd_alloc (abfd, amt);
02087              if (!o->orelocation)
02088               return FALSE;
02089              o->flags |= SEC_RELOC;
02090              /* Reset the count so that it can be used as an index
02091                when putting in the output relocs.  */
02092              o->reloc_count = 0;
02093            }
02094        }
02095     }
02096 
02097   /* Handle all the link order information for the sections.  */
02098   for (o = abfd->sections; o != NULL; o = o->next)
02099     {
02100       for (p = o->map_head.link_order; p != NULL; p = p->next)
02101        {
02102          switch (p->type)
02103            {
02104            case bfd_section_reloc_link_order:
02105            case bfd_symbol_reloc_link_order:
02106              if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
02107               return FALSE;
02108              break;
02109            case bfd_indirect_link_order:
02110              if (! default_indirect_link_order (abfd, info, o, p, TRUE))
02111               return FALSE;
02112              break;
02113            default:
02114              if (! _bfd_default_link_order (abfd, info, o, p))
02115               return FALSE;
02116              break;
02117            }
02118        }
02119     }
02120 
02121   return TRUE;
02122 }
02123 
02124 /* Add an output symbol to the output BFD.  */
02125 
02126 static bfd_boolean
02127 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
02128 {
02129   if (bfd_get_symcount (output_bfd) >= *psymalloc)
02130     {
02131       asymbol **newsyms;
02132       bfd_size_type amt;
02133 
02134       if (*psymalloc == 0)
02135        *psymalloc = 124;
02136       else
02137        *psymalloc *= 2;
02138       amt = *psymalloc;
02139       amt *= sizeof (asymbol *);
02140       newsyms = bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
02141       if (newsyms == NULL)
02142        return FALSE;
02143       bfd_get_outsymbols (output_bfd) = newsyms;
02144     }
02145 
02146   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
02147   if (sym != NULL)
02148     ++ bfd_get_symcount (output_bfd);
02149 
02150   return TRUE;
02151 }
02152 
02153 /* Handle the symbols for an input BFD.  */
02154 
02155 bfd_boolean
02156 _bfd_generic_link_output_symbols (bfd *output_bfd,
02157                               bfd *input_bfd,
02158                               struct bfd_link_info *info,
02159                               size_t *psymalloc)
02160 {
02161   asymbol **sym_ptr;
02162   asymbol **sym_end;
02163 
02164   if (! generic_link_read_symbols (input_bfd))
02165     return FALSE;
02166 
02167   /* Create a filename symbol if we are supposed to.  */
02168   if (info->create_object_symbols_section != NULL)
02169     {
02170       asection *sec;
02171 
02172       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
02173        {
02174          if (sec->output_section == info->create_object_symbols_section)
02175            {
02176              asymbol *newsym;
02177 
02178              newsym = bfd_make_empty_symbol (input_bfd);
02179              if (!newsym)
02180               return FALSE;
02181              newsym->name = input_bfd->filename;
02182              newsym->value = 0;
02183              newsym->flags = BSF_LOCAL | BSF_FILE;
02184              newsym->section = sec;
02185 
02186              if (! generic_add_output_symbol (output_bfd, psymalloc,
02187                                           newsym))
02188               return FALSE;
02189 
02190              break;
02191            }
02192        }
02193     }
02194 
02195   /* Adjust the values of the globally visible symbols, and write out
02196      local symbols.  */
02197   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
02198   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
02199   for (; sym_ptr < sym_end; sym_ptr++)
02200     {
02201       asymbol *sym;
02202       struct generic_link_hash_entry *h;
02203       bfd_boolean output;
02204 
02205       h = NULL;
02206       sym = *sym_ptr;
02207       if ((sym->flags & (BSF_INDIRECT
02208                       | BSF_WARNING
02209                       | BSF_GLOBAL
02210                       | BSF_CONSTRUCTOR
02211                       | BSF_WEAK)) != 0
02212          || bfd_is_und_section (bfd_get_section (sym))
02213          || bfd_is_com_section (bfd_get_section (sym))
02214          || bfd_is_ind_section (bfd_get_section (sym)))
02215        {
02216          if (sym->udata.p != NULL)
02217            h = sym->udata.p;
02218          else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
02219            {
02220              /* This case normally means that the main linker code
02221                  deliberately ignored this constructor symbol.  We
02222                  should just pass it through.  This will screw up if
02223                  the constructor symbol is from a different,
02224                  non-generic, object file format, but the case will
02225                  only arise when linking with -r, which will probably
02226                  fail anyhow, since there will be no way to represent
02227                  the relocs in the output format being used.  */
02228              h = NULL;
02229            }
02230          else if (bfd_is_und_section (bfd_get_section (sym)))
02231            h = ((struct generic_link_hash_entry *)
02232                bfd_wrapped_link_hash_lookup (output_bfd, info,
02233                                           bfd_asymbol_name (sym),
02234                                           FALSE, FALSE, TRUE));
02235          else
02236            h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
02237                                           bfd_asymbol_name (sym),
02238                                           FALSE, FALSE, TRUE);
02239 
02240          if (h != NULL)
02241            {
02242              /* Force all references to this symbol to point to
02243                the same area in memory.  It is possible that
02244                this routine will be called with a hash table
02245                other than a generic hash table, so we double
02246                check that.  */
02247              if (info->hash->creator == input_bfd->xvec)
02248               {
02249                 if (h->sym != NULL)
02250                   *sym_ptr = sym = h->sym;
02251               }
02252 
02253              switch (h->root.type)
02254               {
02255               default:
02256               case bfd_link_hash_new:
02257                 abort ();
02258               case bfd_link_hash_undefined:
02259                 break;
02260               case bfd_link_hash_undefweak:
02261                 sym->flags |= BSF_WEAK;
02262                 break;
02263               case bfd_link_hash_indirect:
02264                 h = (struct generic_link_hash_entry *) h->root.u.i.link;
02265                 /* fall through */
02266               case bfd_link_hash_defined:
02267                 sym->flags |= BSF_GLOBAL;
02268                 sym->flags &=~ BSF_CONSTRUCTOR;
02269                 sym->value = h->root.u.def.value;
02270                 sym->section = h->root.u.def.section;
02271                 break;
02272               case bfd_link_hash_defweak:
02273                 sym->flags |= BSF_WEAK;
02274                 sym->flags &=~ BSF_CONSTRUCTOR;
02275                 sym->value = h->root.u.def.value;
02276                 sym->section = h->root.u.def.section;
02277                 break;
02278               case bfd_link_hash_common:
02279                 sym->value = h->root.u.c.size;
02280                 sym->flags |= BSF_GLOBAL;
02281                 if (! bfd_is_com_section (sym->section))
02282                   {
02283                     BFD_ASSERT (bfd_is_und_section (sym->section));
02284                     sym->section = bfd_com_section_ptr;
02285                   }
02286                 /* We do not set the section of the symbol to
02287                    h->root.u.c.p->section.  That value was saved so
02288                    that we would know where to allocate the symbol
02289                    if it was defined.  In this case the type is
02290                    still bfd_link_hash_common, so we did not define
02291                    it, so we do not want to use that section.  */
02292                 break;
02293               }
02294            }
02295        }
02296 
02297       /* This switch is straight from the old code in
02298         write_file_locals in ldsym.c.  */
02299       if (info->strip == strip_all
02300          || (info->strip == strip_some
02301              && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
02302                               FALSE, FALSE) == NULL))
02303        output = FALSE;
02304       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
02305        {
02306          /* If this symbol is marked as occurring now, rather
02307             than at the end, output it now.  This is used for
02308             COFF C_EXT FCN symbols.  FIXME: There must be a
02309             better way.  */
02310          if (bfd_asymbol_bfd (sym) == input_bfd
02311              && (sym->flags & BSF_NOT_AT_END) != 0)
02312            output = TRUE;
02313          else
02314            output = FALSE;
02315        }
02316       else if (bfd_is_ind_section (sym->section))
02317        output = FALSE;
02318       else if ((sym->flags & BSF_DEBUGGING) != 0)
02319        {
02320          if (info->strip == strip_none)
02321            output = TRUE;
02322          else
02323            output = FALSE;
02324        }
02325       else if (bfd_is_und_section (sym->section)
02326               || bfd_is_com_section (sym->section))
02327        output = FALSE;
02328       else if ((sym->flags & BSF_LOCAL) != 0)
02329        {
02330          if ((sym->flags & BSF_WARNING) != 0)
02331            output = FALSE;
02332          else
02333            {
02334              switch (info->discard)
02335               {
02336               default:
02337               case discard_all:
02338                 output = FALSE;
02339                 break;
02340               case discard_sec_merge:
02341                 output = TRUE;
02342                 if (info->relocatable
02343                     || ! (sym->section->flags & SEC_MERGE))
02344                   break;
02345                 /* FALLTHROUGH */
02346               case discard_l:
02347                 if (bfd_is_local_label (input_bfd, sym))
02348                   output = FALSE;
02349                 else
02350                   output = TRUE;
02351                 break;
02352               case discard_none:
02353                 output = TRUE;
02354                 break;
02355               }
02356            }
02357        }
02358       else if ((sym->flags & BSF_CONSTRUCTOR))
02359        {
02360          if (info->strip != strip_all)
02361            output = TRUE;
02362          else
02363            output = FALSE;
02364        }
02365       else
02366        abort ();
02367 
02368       /* If this symbol is in a section which is not being included
02369         in the output file, then we don't want to output the
02370         symbol.  */
02371       if (!bfd_is_abs_section (sym->section)
02372          && bfd_section_removed_from_list (output_bfd,
02373                                        sym->section->output_section))
02374        output = FALSE;
02375 
02376       if (output)
02377        {
02378          if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
02379            return FALSE;
02380          if (h != NULL)
02381            h->written = TRUE;
02382        }
02383     }
02384 
02385   return TRUE;
02386 }
02387 
02388 /* Set the section and value of a generic BFD symbol based on a linker
02389    hash table entry.  */
02390 
02391 static void
02392 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
02393 {
02394   switch (h->type)
02395     {
02396     default:
02397       abort ();
02398       break;
02399     case bfd_link_hash_new:
02400       /* This can happen when a constructor symbol is seen but we are
02401          not building constructors.  */
02402       if (sym->section != NULL)
02403        {
02404          BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
02405        }
02406       else
02407        {
02408          sym->flags |= BSF_CONSTRUCTOR;
02409          sym->section = bfd_abs_section_ptr;
02410          sym->value = 0;
02411        }
02412       break;
02413     case bfd_link_hash_undefined:
02414       sym->section = bfd_und_section_ptr;
02415       sym->value = 0;
02416       break;
02417     case bfd_link_hash_undefweak:
02418       sym->section = bfd_und_section_ptr;
02419       sym->value = 0;
02420       sym->flags |= BSF_WEAK;
02421       break;
02422     case bfd_link_hash_defined:
02423       sym->section = h->u.def.section;
02424       sym->value = h->u.def.value;
02425       break;
02426     case bfd_link_hash_defweak:
02427       sym->flags |= BSF_WEAK;
02428       sym->section = h->u.def.section;
02429       sym->value = h->u.def.value;
02430       break;
02431     case bfd_link_hash_common:
02432       sym->value = h->u.c.size;
02433       if (sym->section == NULL)
02434        sym->section = bfd_com_section_ptr;
02435       else if (! bfd_is_com_section (sym->section))
02436        {
02437          BFD_ASSERT (bfd_is_und_section (sym->section));
02438          sym->section = bfd_com_section_ptr;
02439        }
02440       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
02441       break;
02442     case bfd_link_hash_indirect:
02443     case bfd_link_hash_warning:
02444       /* FIXME: What should we do here?  */
02445       break;
02446     }
02447 }
02448 
02449 /* Write out a global symbol, if it hasn't already been written out.
02450    This is called for each symbol in the hash table.  */
02451 
02452 bfd_boolean
02453 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
02454                                    void *data)
02455 {
02456   struct generic_write_global_symbol_info *wginfo = data;
02457   asymbol *sym;
02458 
02459   if (h->root.type == bfd_link_hash_warning)
02460     h = (struct generic_link_hash_entry *) h->root.u.i.link;
02461 
02462   if (h->written)
02463     return TRUE;
02464 
02465   h->written = TRUE;
02466 
02467   if (wginfo->info->strip == strip_all
02468       || (wginfo->info->strip == strip_some
02469          && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
02470                            FALSE, FALSE) == NULL))
02471     return TRUE;
02472 
02473   if (h->sym != NULL)
02474     sym = h->sym;
02475   else
02476     {
02477       sym = bfd_make_empty_symbol (wginfo->output_bfd);
02478       if (!sym)
02479        return FALSE;
02480       sym->name = h->root.root.string;
02481       sym->flags = 0;
02482     }
02483 
02484   set_symbol_from_hash (sym, &h->root);
02485 
02486   sym->flags |= BSF_GLOBAL;
02487 
02488   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
02489                                sym))
02490     {
02491       /* FIXME: No way to return failure.  */
02492       abort ();
02493     }
02494 
02495   return TRUE;
02496 }
02497 
02498 /* Create a relocation.  */
02499 
02500 bfd_boolean
02501 _bfd_generic_reloc_link_order (bfd *abfd,
02502                             struct bfd_link_info *info,
02503                             asection *sec,
02504                             struct bfd_link_order *link_order)
02505 {
02506   arelent *r;
02507 
02508   if (! info->relocatable)
02509     abort ();
02510   if (sec->orelocation == NULL)
02511     abort ();
02512 
02513   r = bfd_alloc (abfd, sizeof (arelent));
02514   if (r == NULL)
02515     return FALSE;
02516 
02517   r->address = link_order->offset;
02518   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
02519   if (r->howto == 0)
02520     {
02521       bfd_set_error (bfd_error_bad_value);
02522       return FALSE;
02523     }
02524 
02525   /* Get the symbol to use for the relocation.  */
02526   if (link_order->type == bfd_section_reloc_link_order)
02527     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
02528   else
02529     {
02530       struct generic_link_hash_entry *h;
02531 
02532       h = ((struct generic_link_hash_entry *)
02533           bfd_wrapped_link_hash_lookup (abfd, info,
02534                                     link_order->u.reloc.p->u.name,
02535                                     FALSE, FALSE, TRUE));
02536       if (h == NULL
02537          || ! h->written)
02538        {
02539          if (! ((*info->callbacks->unattached_reloc)
02540                (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
02541            return FALSE;
02542          bfd_set_error (bfd_error_bad_value);
02543          return FALSE;
02544        }
02545       r->sym_ptr_ptr = &h->sym;
02546     }
02547 
02548   /* If this is an inplace reloc, write the addend to the object file.
02549      Otherwise, store it in the reloc addend.  */
02550   if (! r->howto->partial_inplace)
02551     r->addend = link_order->u.reloc.p->addend;
02552   else
02553     {
02554       bfd_size_type size;
02555       bfd_reloc_status_type rstat;
02556       bfd_byte *buf;
02557       bfd_boolean ok;
02558       file_ptr loc;
02559 
02560       size = bfd_get_reloc_size (r->howto);
02561       buf = bfd_zmalloc (size);
02562       if (buf == NULL)
02563        return FALSE;
02564       rstat = _bfd_relocate_contents (r->howto, abfd,
02565                                   (bfd_vma) link_order->u.reloc.p->addend,
02566                                   buf);
02567       switch (rstat)
02568        {
02569        case bfd_reloc_ok:
02570          break;
02571        default:
02572        case bfd_reloc_outofrange:
02573          abort ();
02574        case bfd_reloc_overflow:
02575          if (! ((*info->callbacks->reloc_overflow)
02576                (info, NULL,
02577                 (link_order->type == bfd_section_reloc_link_order
02578                  ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
02579                  : link_order->u.reloc.p->u.name),
02580                 r->howto->name, link_order->u.reloc.p->addend,
02581                 NULL, NULL, 0)))
02582            {
02583              free (buf);
02584              return FALSE;
02585            }
02586          break;
02587        }
02588       loc = link_order->offset * bfd_octets_per_byte (abfd);
02589       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
02590       free (buf);
02591       if (! ok)
02592        return FALSE;
02593 
02594       r->addend = 0;
02595     }
02596 
02597   sec->orelocation[sec->reloc_count] = r;
02598   ++sec->reloc_count;
02599 
02600   return TRUE;
02601 }
02602 
02603 /* Allocate a new link_order for a section.  */
02604 
02605 struct bfd_link_order *
02606 bfd_new_link_order (bfd *abfd, asection *section)
02607 {
02608   bfd_size_type amt = sizeof (struct bfd_link_order);
02609   struct bfd_link_order *new;
02610 
02611   new = bfd_zalloc (abfd, amt);
02612   if (!new)
02613     return NULL;
02614 
02615   new->type = bfd_undefined_link_order;
02616 
02617   if (section->map_tail.link_order != NULL)
02618     section->map_tail.link_order->next = new;
02619   else
02620     section->map_head.link_order = new;
02621   section->map_tail.link_order = new;
02622 
02623   return new;
02624 }
02625 
02626 /* Default link order processing routine.  Note that we can not handle
02627    the reloc_link_order types here, since they depend upon the details
02628    of how the particular backends generates relocs.  */
02629 
02630 bfd_boolean
02631 _bfd_default_link_order (bfd *abfd,
02632                       struct bfd_link_info *info,
02633                       asection *sec,
02634                       struct bfd_link_order *link_order)
02635 {
02636   switch (link_order->type)
02637     {
02638     case bfd_undefined_link_order:
02639     case bfd_section_reloc_link_order:
02640     case bfd_symbol_reloc_link_order:
02641     default:
02642       abort ();
02643     case bfd_indirect_link_order:
02644       return default_indirect_link_order (abfd, info, sec, link_order,
02645                                      FALSE);
02646     case bfd_data_link_order:
02647       return default_data_link_order (abfd, info, sec, link_order);
02648     }
02649 }
02650 
02651 /* Default routine to handle a bfd_data_link_order.  */
02652 
02653 static bfd_boolean
02654 default_data_link_order (bfd *abfd,
02655                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
02656                       asection *sec,
02657                       struct bfd_link_order *link_order)
02658 {
02659   bfd_size_type size;
02660   size_t fill_size;
02661   bfd_byte *fill;
02662   file_ptr loc;
02663   bfd_boolean result;
02664 
02665   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
02666 
02667   size = link_order->size;
02668   if (size == 0)
02669     return TRUE;
02670 
02671   fill = link_order->u.data.contents;
02672   fill_size = link_order->u.data.size;
02673   if (fill_size != 0 && fill_size < size)
02674     {
02675       bfd_byte *p;
02676       fill = bfd_malloc (size);
02677       if (fill == NULL)
02678        return FALSE;
02679       p = fill;
02680       if (fill_size == 1)
02681        memset (p, (int) link_order->u.data.contents[0], (size_t) size);
02682       else
02683        {
02684          do
02685            {
02686              memcpy (p, link_order->u.data.contents, fill_size);
02687              p += fill_size;
02688              size -= fill_size;
02689            }
02690          while (size >= fill_size);
02691          if (size != 0)
02692            memcpy (p, link_order->u.data.contents, (size_t) size);
02693          size = link_order->size;
02694        }
02695     }
02696 
02697   loc = link_order->offset * bfd_octets_per_byte (abfd);
02698   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
02699 
02700   if (fill != link_order->u.data.contents)
02701     free (fill);
02702   return result;
02703 }
02704 
02705 /* Default routine to handle a bfd_indirect_link_order.  */
02706 
02707 static bfd_boolean
02708 default_indirect_link_order (bfd *output_bfd,
02709                           struct bfd_link_info *info,
02710                           asection *output_section,
02711                           struct bfd_link_order *link_order,
02712                           bfd_boolean generic_linker)
02713 {
02714   asection *input_section;
02715   bfd *input_bfd;
02716   bfd_byte *contents = NULL;
02717   bfd_byte *new_contents;
02718   bfd_size_type sec_size;
02719   file_ptr loc;
02720 
02721   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
02722 
02723   input_section = link_order->u.indirect.section;
02724   input_bfd = input_section->owner;
02725   if (input_section->size == 0)
02726     return TRUE;
02727 
02728   BFD_ASSERT (input_section->output_section == output_section);
02729   BFD_ASSERT (input_section->output_offset == link_order->offset);
02730   BFD_ASSERT (input_section->size == link_order->size);
02731 
02732   if (info->relocatable
02733       && input_section->reloc_count > 0
02734       && output_section->orelocation == NULL)
02735     {
02736       /* Space has not been allocated for the output relocations.
02737         This can happen when we are called by a specific backend
02738         because somebody is attempting to link together different
02739         types of object files.  Handling this case correctly is
02740         difficult, and sometimes impossible.  */
02741       (*_bfd_error_handler)
02742        (_("Attempt to do relocatable link with %s input and %s output"),
02743         bfd_get_target (input_bfd), bfd_get_target (output_bfd));
02744       bfd_set_error (bfd_error_wrong_format);
02745       return FALSE;
02746     }
02747 
02748   if (! generic_linker)
02749     {
02750       asymbol **sympp;
02751       asymbol **symppend;
02752 
02753       /* Get the canonical symbols.  The generic linker will always
02754         have retrieved them by this point, but we are being called by
02755         a specific linker, presumably because we are linking
02756         different types of object files together.  */
02757       if (! generic_link_read_symbols (input_bfd))
02758        return FALSE;
02759 
02760       /* Since we have been called by a specific linker, rather than
02761         the generic linker, the values of the symbols will not be
02762         right.  They will be the values as seen in the input file,
02763         not the values of the final link.  We need to fix them up
02764         before we can relocate the section.  */
02765       sympp = _bfd_generic_link_get_symbols (input_bfd);
02766       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
02767       for (; sympp < symppend; sympp++)
02768        {
02769          asymbol *sym;
02770          struct bfd_link_hash_entry *h;
02771 
02772          sym = *sympp;
02773 
02774          if ((sym->flags & (BSF_INDIRECT
02775                           | BSF_WARNING
02776                           | BSF_GLOBAL
02777                           | BSF_CONSTRUCTOR
02778                           | BSF_WEAK)) != 0
02779              || bfd_is_und_section (bfd_get_section (sym))
02780              || bfd_is_com_section (bfd_get_section (sym))
02781              || bfd_is_ind_section (bfd_get_section (sym)))
02782            {
02783              /* sym->udata may have been set by
02784                generic_link_add_symbol_list.  */
02785              if (sym->udata.p != NULL)
02786               h = sym->udata.p;
02787              else if (bfd_is_und_section (bfd_get_section (sym)))
02788               h = bfd_wrapped_link_hash_lookup (output_bfd, info,
02789                                             bfd_asymbol_name (sym),
02790                                             FALSE, FALSE, TRUE);
02791              else
02792               h = bfd_link_hash_lookup (info->hash,
02793                                      bfd_asymbol_name (sym),
02794                                      FALSE, FALSE, TRUE);
02795              if (h != NULL)
02796               set_symbol_from_hash (sym, h);
02797            }
02798        }
02799     }
02800 
02801   /* Get and relocate the section contents.  */
02802   sec_size = (input_section->rawsize > input_section->size
02803              ? input_section->rawsize
02804              : input_section->size);
02805   contents = bfd_malloc (sec_size);
02806   if (contents == NULL && sec_size != 0)
02807     goto error_return;
02808   new_contents = (bfd_get_relocated_section_contents
02809                 (output_bfd, info, link_order, contents, info->relocatable,
02810                  _bfd_generic_link_get_symbols (input_bfd)));
02811   if (!new_contents)
02812     goto error_return;
02813 
02814   /* Output the section contents.  */
02815   loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
02816   if (! bfd_set_section_contents (output_bfd, output_section,
02817                               new_contents, loc, input_section->size))
02818     goto error_return;
02819 
02820   if (contents != NULL)
02821     free (contents);
02822   return TRUE;
02823 
02824  error_return:
02825   if (contents != NULL)
02826     free (contents);
02827   return FALSE;
02828 }
02829 
02830 /* A little routine to count the number of relocs in a link_order
02831    list.  */
02832 
02833 unsigned int
02834 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
02835 {
02836   register unsigned int c;
02837   register struct bfd_link_order *l;
02838 
02839   c = 0;
02840   for (l = link_order; l != NULL; l = l->next)
02841     {
02842       if (l->type == bfd_section_reloc_link_order
02843          || l->type == bfd_symbol_reloc_link_order)
02844        ++c;
02845     }
02846 
02847   return c;
02848 }
02849 
02850 /*
02851 FUNCTION
02852        bfd_link_split_section
02853 
02854 SYNOPSIS
02855         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
02856 
02857 DESCRIPTION
02858        Return nonzero if @var{sec} should be split during a
02859        reloceatable or final link.
02860 
02861 .#define bfd_link_split_section(abfd, sec) \
02862 .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
02863 .
02864 
02865 */
02866 
02867 bfd_boolean
02868 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
02869                              asection *sec ATTRIBUTE_UNUSED)
02870 {
02871   return FALSE;
02872 }
02873 
02874 /*
02875 FUNCTION
02876        bfd_section_already_linked
02877 
02878 SYNOPSIS
02879         void bfd_section_already_linked (bfd *abfd, asection *sec,
02880                                     struct bfd_link_info *info);
02881 
02882 DESCRIPTION
02883        Check if @var{sec} has been already linked during a reloceatable
02884        or final link.
02885 
02886 .#define bfd_section_already_linked(abfd, sec, info) \
02887 .       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
02888 .
02889 
02890 */
02891 
02892 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
02893    once into the output.  This routine checks each section, and
02894    arrange to discard it if a section of the same name has already
02895    been linked.  This code assumes that all relevant sections have the 
02896    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
02897    section name.  bfd_section_already_linked is called via
02898    bfd_map_over_sections.  */
02899 
02900 /* The hash table.  */
02901 
02902 static struct bfd_hash_table _bfd_section_already_linked_table;
02903 
02904 /* Support routines for the hash table used by section_already_linked,
02905    initialize the table, traverse, lookup, fill in an entry and remove
02906    the table.  */
02907 
02908 void
02909 bfd_section_already_linked_table_traverse
02910   (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
02911                      void *), void *info)
02912 {
02913   bfd_hash_traverse (&_bfd_section_already_linked_table,
02914                    (bfd_boolean (*) (struct bfd_hash_entry *,
02915                                    void *)) func,
02916                    info);
02917 }
02918 
02919 struct bfd_section_already_linked_hash_entry *
02920 bfd_section_already_linked_table_lookup (const char *name)
02921 {
02922   return ((struct bfd_section_already_linked_hash_entry *)
02923          bfd_hash_lookup (&_bfd_section_already_linked_table, name,
02924                         TRUE, FALSE));
02925 }
02926 
02927 void
02928 bfd_section_already_linked_table_insert
02929   (struct bfd_section_already_linked_hash_entry *already_linked_list,
02930    asection *sec)
02931 {
02932   struct bfd_section_already_linked *l;
02933 
02934   /* Allocate the memory from the same obstack as the hash table is
02935      kept in.  */
02936   l = bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
02937   l->sec = sec;
02938   l->next = already_linked_list->entry;
02939   already_linked_list->entry = l;
02940 }
02941 
02942 static struct bfd_hash_entry *
02943 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
02944                      struct bfd_hash_table *table,
02945                      const char *string ATTRIBUTE_UNUSED)
02946 {
02947   struct bfd_section_already_linked_hash_entry *ret =
02948     bfd_hash_allocate (table, sizeof *ret);
02949 
02950   ret->entry = NULL;
02951 
02952   return &ret->root;
02953 }
02954 
02955 bfd_boolean
02956 bfd_section_already_linked_table_init (void)
02957 {
02958   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
02959                             already_linked_newfunc,
02960                             sizeof (struct bfd_section_already_linked_hash_entry),
02961                             42);
02962 }
02963 
02964 void
02965 bfd_section_already_linked_table_free (void)
02966 {
02967   bfd_hash_table_free (&_bfd_section_already_linked_table);
02968 }
02969 
02970 /* This is used on non-ELF inputs.  */
02971 
02972 void
02973 _bfd_generic_section_already_linked (bfd *abfd, asection *sec,
02974                                  struct bfd_link_info *info ATTRIBUTE_UNUSED)
02975 {
02976   flagword flags;
02977   const char *name;
02978   struct bfd_section_already_linked *l;
02979   struct bfd_section_already_linked_hash_entry *already_linked_list;
02980 
02981   flags = sec->flags;
02982   if ((flags & SEC_LINK_ONCE) == 0)
02983     return;
02984 
02985   /* FIXME: When doing a relocatable link, we may have trouble
02986      copying relocations in other sections that refer to local symbols
02987      in the section being discarded.  Those relocations will have to
02988      be converted somehow; as of this writing I'm not sure that any of
02989      the backends handle that correctly.
02990 
02991      It is tempting to instead not discard link once sections when
02992      doing a relocatable link (technically, they should be discarded
02993      whenever we are building constructors).  However, that fails,
02994      because the linker winds up combining all the link once sections
02995      into a single large link once section, which defeats the purpose
02996      of having link once sections in the first place.  */
02997 
02998   name = bfd_get_section_name (abfd, sec);
02999 
03000   already_linked_list = bfd_section_already_linked_table_lookup (name);
03001 
03002   for (l = already_linked_list->entry; l != NULL; l = l->next)
03003     {
03004       bfd_boolean skip = FALSE;
03005       struct coff_comdat_info *s_comdat
03006        = bfd_coff_get_comdat_section (abfd, sec);
03007       struct coff_comdat_info *l_comdat
03008        = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
03009 
03010       /* We may have 3 different sections on the list: group section,
03011         comdat section and linkonce section. SEC may be a linkonce or
03012         comdat section. We always ignore group section. For non-COFF
03013         inputs, we also ignore comdat section.
03014 
03015         FIXME: Is that safe to match a linkonce section with a comdat
03016         section for COFF inputs?  */
03017       if ((l->sec->flags & SEC_GROUP) != 0)
03018        skip = TRUE;
03019       else if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
03020        {
03021          if (s_comdat != NULL
03022              && l_comdat != NULL
03023              && strcmp (s_comdat->name, l_comdat->name) != 0)
03024            skip = TRUE;
03025        }
03026       else if (l_comdat != NULL)
03027        skip = TRUE;
03028 
03029       if (!skip)
03030        {
03031          /* The section has already been linked.  See if we should
03032              issue a warning.  */
03033          switch (flags & SEC_LINK_DUPLICATES)
03034            {
03035            default:
03036              abort ();
03037 
03038            case SEC_LINK_DUPLICATES_DISCARD:
03039              break;
03040 
03041            case SEC_LINK_DUPLICATES_ONE_ONLY:
03042              (*_bfd_error_handler)
03043               (_("%B: warning: ignoring duplicate section `%A'\n"),
03044                abfd, sec);
03045              break;
03046 
03047            case SEC_LINK_DUPLICATES_SAME_CONTENTS:
03048              /* FIXME: We should really dig out the contents of both
03049                  sections and memcmp them.  The COFF/PE spec says that
03050                  the Microsoft linker does not implement this
03051                  correctly, so I'm not going to bother doing it
03052                  either.  */
03053              /* Fall through.  */
03054            case SEC_LINK_DUPLICATES_SAME_SIZE:
03055              if (sec->size != l->sec->size)
03056               (*_bfd_error_handler)
03057                 (_("%B: warning: duplicate section `%A' has different size\n"),
03058                  abfd, sec);
03059              break;
03060            }
03061 
03062          /* Set the output_section field so that lang_add_section
03063             does not create a lang_input_section structure for this
03064             section.  Since there might be a symbol in the section
03065             being discarded, we must retain a pointer to the section
03066             which we are really going to use.  */
03067          sec->output_section = bfd_abs_section_ptr;
03068          sec->kept_section = l->sec;
03069 
03070          return;
03071        }
03072     }
03073 
03074   /* This is the first section with this name.  Record it.  */
03075   bfd_section_already_linked_table_insert (already_linked_list, sec);
03076 }
03077 
03078 /* Convert symbols in excluded output sections to use a kept section.  */
03079 
03080 static bfd_boolean
03081 fix_syms (struct bfd_link_hash_entry *h, void *data)
03082 {
03083   bfd *obfd = (bfd *) data;
03084 
03085   if (h->type == bfd_link_hash_warning)
03086     h = h->u.i.link;
03087 
03088   if (h->type == bfd_link_hash_defined
03089       || h->type == bfd_link_hash_defweak)
03090     {
03091       asection *s = h->u.def.section;
03092       if (s != NULL
03093          && s->output_section != NULL
03094          && (s->output_section->flags & SEC_EXCLUDE) != 0
03095          && bfd_section_removed_from_list (obfd, s->output_section))
03096        {
03097          asection *op, *op1;
03098 
03099          h->u.def.value += s->output_offset + s->output_section->vma;
03100 
03101          /* Find preceding kept section.  */
03102          for (op1 = s->output_section->prev; op1 != NULL; op1 = op1->prev)
03103            if ((op1->flags & SEC_EXCLUDE) == 0
03104               && !bfd_section_removed_from_list (obfd, op1))
03105              break;
03106 
03107          /* Find following kept section.  Start at prev->next because
03108             other sections may have been added after S was removed.  */
03109          if (s->output_section->prev != NULL)
03110            op = s->output_section->prev->next;
03111          else
03112            op = s->output_section->owner->sections;
03113          for (; op != NULL; op = op->next)
03114            if ((op->flags & SEC_EXCLUDE) == 0
03115               && !bfd_section_removed_from_list (obfd, op))
03116              break;
03117 
03118          /* Choose better of two sections, based on flags.  The idea
03119             is to choose a section that will be in the same segment
03120             as S would have been if it was kept.  */
03121          if (op1 == NULL)
03122            {
03123              if (op == NULL)
03124               op = bfd_abs_section_ptr;
03125            }
03126          else if (op == NULL)
03127            op = op1;
03128          else if (((op1->flags ^ op->flags)
03129                   & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0)
03130            {
03131              if (((op->flags ^ s->flags)
03132                  & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0)
03133               op = op1;
03134            }
03135          else if (((op1->flags ^ op->flags) & SEC_READONLY) != 0)
03136            {
03137              if (((op->flags ^ s->flags) & SEC_READONLY) != 0)
03138               op = op1;
03139            }
03140          else if (((op1->flags ^ op->flags) & SEC_CODE) != 0)
03141            {
03142              if (((op->flags ^ s->flags) & SEC_CODE) != 0)
03143               op = op1;
03144            }
03145          else
03146            {
03147              /* Flags we care about are the same.  Prefer the following
03148                section if that will result in a positive valued sym.  */
03149              if (h->u.def.value < op->vma)
03150               op = op1;
03151            }
03152 
03153          h->u.def.value -= op->vma;
03154          h->u.def.section = op;
03155        }
03156     }
03157 
03158   return TRUE;
03159 }
03160 
03161 void
03162 _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
03163 {
03164   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
03165 }