15796c8dcSSimon Schubert /* linker.c -- BFD linker routines
25796c8dcSSimon Schubert Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3*ef5ccd6cSJohn Marino 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
45796c8dcSSimon Schubert Free Software Foundation, Inc.
55796c8dcSSimon Schubert Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This file is part of BFD, the Binary File Descriptor library.
85796c8dcSSimon Schubert
95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert (at your option) any later version.
135796c8dcSSimon Schubert
145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175796c8dcSSimon Schubert GNU General Public License for more details.
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert along with this program; if not, write to the Free Software
215796c8dcSSimon Schubert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
225796c8dcSSimon Schubert MA 02110-1301, USA. */
235796c8dcSSimon Schubert
245796c8dcSSimon Schubert #include "sysdep.h"
255796c8dcSSimon Schubert #include "bfd.h"
265796c8dcSSimon Schubert #include "libbfd.h"
275796c8dcSSimon Schubert #include "bfdlink.h"
285796c8dcSSimon Schubert #include "genlink.h"
295796c8dcSSimon Schubert
305796c8dcSSimon Schubert /*
315796c8dcSSimon Schubert SECTION
325796c8dcSSimon Schubert Linker Functions
335796c8dcSSimon Schubert
345796c8dcSSimon Schubert @cindex Linker
355796c8dcSSimon Schubert The linker uses three special entry points in the BFD target
365796c8dcSSimon Schubert vector. It is not necessary to write special routines for
375796c8dcSSimon Schubert these entry points when creating a new BFD back end, since
385796c8dcSSimon Schubert generic versions are provided. However, writing them can
395796c8dcSSimon Schubert speed up linking and make it use significantly less runtime
405796c8dcSSimon Schubert memory.
415796c8dcSSimon Schubert
425796c8dcSSimon Schubert The first routine creates a hash table used by the other
435796c8dcSSimon Schubert routines. The second routine adds the symbols from an object
445796c8dcSSimon Schubert file to the hash table. The third routine takes all the
455796c8dcSSimon Schubert object files and links them together to create the output
465796c8dcSSimon Schubert file. These routines are designed so that the linker proper
475796c8dcSSimon Schubert does not need to know anything about the symbols in the object
485796c8dcSSimon Schubert files that it is linking. The linker merely arranges the
495796c8dcSSimon Schubert sections as directed by the linker script and lets BFD handle
505796c8dcSSimon Schubert the details of symbols and relocs.
515796c8dcSSimon Schubert
525796c8dcSSimon Schubert The second routine and third routines are passed a pointer to
535796c8dcSSimon Schubert a <<struct bfd_link_info>> structure (defined in
545796c8dcSSimon Schubert <<bfdlink.h>>) which holds information relevant to the link,
555796c8dcSSimon Schubert including the linker hash table (which was created by the
565796c8dcSSimon Schubert first routine) and a set of callback functions to the linker
575796c8dcSSimon Schubert proper.
585796c8dcSSimon Schubert
595796c8dcSSimon Schubert The generic linker routines are in <<linker.c>>, and use the
605796c8dcSSimon Schubert header file <<genlink.h>>. As of this writing, the only back
615796c8dcSSimon Schubert ends which have implemented versions of these routines are
625796c8dcSSimon Schubert a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
635796c8dcSSimon Schubert routines are used as examples throughout this section.
645796c8dcSSimon Schubert
655796c8dcSSimon Schubert @menu
665796c8dcSSimon Schubert @* Creating a Linker Hash Table::
675796c8dcSSimon Schubert @* Adding Symbols to the Hash Table::
685796c8dcSSimon Schubert @* Performing the Final Link::
695796c8dcSSimon Schubert @end menu
705796c8dcSSimon Schubert
715796c8dcSSimon Schubert INODE
725796c8dcSSimon Schubert Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
735796c8dcSSimon Schubert SUBSECTION
745796c8dcSSimon Schubert Creating a linker hash table
755796c8dcSSimon Schubert
765796c8dcSSimon Schubert @cindex _bfd_link_hash_table_create in target vector
775796c8dcSSimon Schubert @cindex target vector (_bfd_link_hash_table_create)
785796c8dcSSimon Schubert The linker routines must create a hash table, which must be
795796c8dcSSimon Schubert derived from <<struct bfd_link_hash_table>> described in
805796c8dcSSimon Schubert <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
815796c8dcSSimon Schubert create a derived hash table. This entry point is called using
825796c8dcSSimon Schubert the target vector of the linker output file.
835796c8dcSSimon Schubert
845796c8dcSSimon Schubert The <<_bfd_link_hash_table_create>> entry point must allocate
855796c8dcSSimon Schubert and initialize an instance of the desired hash table. If the
865796c8dcSSimon Schubert back end does not require any additional information to be
875796c8dcSSimon Schubert stored with the entries in the hash table, the entry point may
885796c8dcSSimon Schubert simply create a <<struct bfd_link_hash_table>>. Most likely,
895796c8dcSSimon Schubert however, some additional information will be needed.
905796c8dcSSimon Schubert
915796c8dcSSimon Schubert For example, with each entry in the hash table the a.out
925796c8dcSSimon Schubert linker keeps the index the symbol has in the final output file
935796c8dcSSimon Schubert (this index number is used so that when doing a relocatable
945796c8dcSSimon Schubert link the symbol index used in the output file can be quickly
955796c8dcSSimon Schubert filled in when copying over a reloc). The a.out linker code
965796c8dcSSimon Schubert defines the required structures and functions for a hash table
975796c8dcSSimon Schubert derived from <<struct bfd_link_hash_table>>. The a.out linker
985796c8dcSSimon Schubert hash table is created by the function
995796c8dcSSimon Schubert <<NAME(aout,link_hash_table_create)>>; it simply allocates
1005796c8dcSSimon Schubert space for the hash table, initializes it, and returns a
1015796c8dcSSimon Schubert pointer to it.
1025796c8dcSSimon Schubert
1035796c8dcSSimon Schubert When writing the linker routines for a new back end, you will
1045796c8dcSSimon Schubert generally not know exactly which fields will be required until
1055796c8dcSSimon Schubert you have finished. You should simply create a new hash table
1065796c8dcSSimon Schubert which defines no additional fields, and then simply add fields
1075796c8dcSSimon Schubert as they become necessary.
1085796c8dcSSimon Schubert
1095796c8dcSSimon Schubert INODE
1105796c8dcSSimon Schubert Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
1115796c8dcSSimon Schubert SUBSECTION
1125796c8dcSSimon Schubert Adding symbols to the hash table
1135796c8dcSSimon Schubert
1145796c8dcSSimon Schubert @cindex _bfd_link_add_symbols in target vector
1155796c8dcSSimon Schubert @cindex target vector (_bfd_link_add_symbols)
1165796c8dcSSimon Schubert The linker proper will call the <<_bfd_link_add_symbols>>
1175796c8dcSSimon Schubert entry point for each object file or archive which is to be
1185796c8dcSSimon Schubert linked (typically these are the files named on the command
1195796c8dcSSimon Schubert line, but some may also come from the linker script). The
1205796c8dcSSimon Schubert entry point is responsible for examining the file. For an
1215796c8dcSSimon Schubert object file, BFD must add any relevant symbol information to
1225796c8dcSSimon Schubert the hash table. For an archive, BFD must determine which
1235796c8dcSSimon Schubert elements of the archive should be used and adding them to the
1245796c8dcSSimon Schubert link.
1255796c8dcSSimon Schubert
1265796c8dcSSimon Schubert The a.out version of this entry point is
1275796c8dcSSimon Schubert <<NAME(aout,link_add_symbols)>>.
1285796c8dcSSimon Schubert
1295796c8dcSSimon Schubert @menu
1305796c8dcSSimon Schubert @* Differing file formats::
1315796c8dcSSimon Schubert @* Adding symbols from an object file::
1325796c8dcSSimon Schubert @* Adding symbols from an archive::
1335796c8dcSSimon Schubert @end menu
1345796c8dcSSimon Schubert
1355796c8dcSSimon Schubert INODE
1365796c8dcSSimon Schubert Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
1375796c8dcSSimon Schubert SUBSUBSECTION
1385796c8dcSSimon Schubert Differing file formats
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert Normally all the files involved in a link will be of the same
1415796c8dcSSimon Schubert format, but it is also possible to link together different
1425796c8dcSSimon Schubert format object files, and the back end must support that. The
1435796c8dcSSimon Schubert <<_bfd_link_add_symbols>> entry point is called via the target
1445796c8dcSSimon Schubert vector of the file to be added. This has an important
1455796c8dcSSimon Schubert consequence: the function may not assume that the hash table
1465796c8dcSSimon Schubert is the type created by the corresponding
1475796c8dcSSimon Schubert <<_bfd_link_hash_table_create>> vector. All the
1485796c8dcSSimon Schubert <<_bfd_link_add_symbols>> function can assume about the hash
1495796c8dcSSimon Schubert table is that it is derived from <<struct
1505796c8dcSSimon Schubert bfd_link_hash_table>>.
1515796c8dcSSimon Schubert
1525796c8dcSSimon Schubert Sometimes the <<_bfd_link_add_symbols>> function must store
1535796c8dcSSimon Schubert some information in the hash table entry to be used by the
1545796c8dcSSimon Schubert <<_bfd_final_link>> function. In such a case the output bfd
1555796c8dcSSimon Schubert xvec must be checked to make sure that the hash table was
1565796c8dcSSimon Schubert created by an object file of the same format.
1575796c8dcSSimon Schubert
1585796c8dcSSimon Schubert The <<_bfd_final_link>> routine must be prepared to handle a
1595796c8dcSSimon Schubert hash entry without any extra information added by the
1605796c8dcSSimon Schubert <<_bfd_link_add_symbols>> function. A hash entry without
1615796c8dcSSimon Schubert extra information will also occur when the linker script
1625796c8dcSSimon Schubert directs the linker to create a symbol. Note that, regardless
1635796c8dcSSimon Schubert of how a hash table entry is added, all the fields will be
1645796c8dcSSimon Schubert initialized to some sort of null value by the hash table entry
1655796c8dcSSimon Schubert initialization function.
1665796c8dcSSimon Schubert
1675796c8dcSSimon Schubert See <<ecoff_link_add_externals>> for an example of how to
1685796c8dcSSimon Schubert check the output bfd before saving information (in this
1695796c8dcSSimon Schubert case, the ECOFF external symbol debugging information) in a
1705796c8dcSSimon Schubert hash table entry.
1715796c8dcSSimon Schubert
1725796c8dcSSimon Schubert INODE
1735796c8dcSSimon Schubert Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
1745796c8dcSSimon Schubert SUBSUBSECTION
1755796c8dcSSimon Schubert Adding symbols from an object file
1765796c8dcSSimon Schubert
1775796c8dcSSimon Schubert When the <<_bfd_link_add_symbols>> routine is passed an object
1785796c8dcSSimon Schubert file, it must add all externally visible symbols in that
1795796c8dcSSimon Schubert object file to the hash table. The actual work of adding the
1805796c8dcSSimon Schubert symbol to the hash table is normally handled by the function
1815796c8dcSSimon Schubert <<_bfd_generic_link_add_one_symbol>>. The
1825796c8dcSSimon Schubert <<_bfd_link_add_symbols>> routine is responsible for reading
1835796c8dcSSimon Schubert all the symbols from the object file and passing the correct
1845796c8dcSSimon Schubert information to <<_bfd_generic_link_add_one_symbol>>.
1855796c8dcSSimon Schubert
1865796c8dcSSimon Schubert The <<_bfd_link_add_symbols>> routine should not use
1875796c8dcSSimon Schubert <<bfd_canonicalize_symtab>> to read the symbols. The point of
1885796c8dcSSimon Schubert providing this routine is to avoid the overhead of converting
1895796c8dcSSimon Schubert the symbols into generic <<asymbol>> structures.
1905796c8dcSSimon Schubert
1915796c8dcSSimon Schubert @findex _bfd_generic_link_add_one_symbol
1925796c8dcSSimon Schubert <<_bfd_generic_link_add_one_symbol>> handles the details of
1935796c8dcSSimon Schubert combining common symbols, warning about multiple definitions,
1945796c8dcSSimon Schubert and so forth. It takes arguments which describe the symbol to
1955796c8dcSSimon Schubert add, notably symbol flags, a section, and an offset. The
1965796c8dcSSimon Schubert symbol flags include such things as <<BSF_WEAK>> or
1975796c8dcSSimon Schubert <<BSF_INDIRECT>>. The section is a section in the object
1985796c8dcSSimon Schubert file, or something like <<bfd_und_section_ptr>> for an undefined
1995796c8dcSSimon Schubert symbol or <<bfd_com_section_ptr>> for a common symbol.
2005796c8dcSSimon Schubert
2015796c8dcSSimon Schubert If the <<_bfd_final_link>> routine is also going to need to
2025796c8dcSSimon Schubert read the symbol information, the <<_bfd_link_add_symbols>>
2035796c8dcSSimon Schubert routine should save it somewhere attached to the object file
2045796c8dcSSimon Schubert BFD. However, the information should only be saved if the
2055796c8dcSSimon Schubert <<keep_memory>> field of the <<info>> argument is TRUE, so
2065796c8dcSSimon Schubert that the <<-no-keep-memory>> linker switch is effective.
2075796c8dcSSimon Schubert
2085796c8dcSSimon Schubert The a.out function which adds symbols from an object file is
2095796c8dcSSimon Schubert <<aout_link_add_object_symbols>>, and most of the interesting
2105796c8dcSSimon Schubert work is in <<aout_link_add_symbols>>. The latter saves
2115796c8dcSSimon Schubert pointers to the hash tables entries created by
2125796c8dcSSimon Schubert <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
2135796c8dcSSimon Schubert so that the <<_bfd_final_link>> routine does not have to call
2145796c8dcSSimon Schubert the hash table lookup routine to locate the entry.
2155796c8dcSSimon Schubert
2165796c8dcSSimon Schubert INODE
2175796c8dcSSimon Schubert Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
2185796c8dcSSimon Schubert SUBSUBSECTION
2195796c8dcSSimon Schubert Adding symbols from an archive
2205796c8dcSSimon Schubert
2215796c8dcSSimon Schubert When the <<_bfd_link_add_symbols>> routine is passed an
2225796c8dcSSimon Schubert archive, it must look through the symbols defined by the
2235796c8dcSSimon Schubert archive and decide which elements of the archive should be
2245796c8dcSSimon Schubert included in the link. For each such element it must call the
2255796c8dcSSimon Schubert <<add_archive_element>> linker callback, and it must add the
226c50c785cSJohn Marino symbols from the object file to the linker hash table. (The
227c50c785cSJohn Marino callback may in fact indicate that a replacement BFD should be
228c50c785cSJohn Marino used, in which case the symbols from that BFD should be added
229c50c785cSJohn Marino to the linker hash table instead.)
2305796c8dcSSimon Schubert
2315796c8dcSSimon Schubert @findex _bfd_generic_link_add_archive_symbols
2325796c8dcSSimon Schubert In most cases the work of looking through the symbols in the
2335796c8dcSSimon Schubert archive should be done by the
2345796c8dcSSimon Schubert <<_bfd_generic_link_add_archive_symbols>> function. This
2355796c8dcSSimon Schubert function builds a hash table from the archive symbol table and
2365796c8dcSSimon Schubert looks through the list of undefined symbols to see which
2375796c8dcSSimon Schubert elements should be included.
2385796c8dcSSimon Schubert <<_bfd_generic_link_add_archive_symbols>> is passed a function
2395796c8dcSSimon Schubert to call to make the final decision about adding an archive
2405796c8dcSSimon Schubert element to the link and to do the actual work of adding the
2415796c8dcSSimon Schubert symbols to the linker hash table.
2425796c8dcSSimon Schubert
2435796c8dcSSimon Schubert The function passed to
2445796c8dcSSimon Schubert <<_bfd_generic_link_add_archive_symbols>> must read the
2455796c8dcSSimon Schubert symbols of the archive element and decide whether the archive
2465796c8dcSSimon Schubert element should be included in the link. If the element is to
2475796c8dcSSimon Schubert be included, the <<add_archive_element>> linker callback
2485796c8dcSSimon Schubert routine must be called with the element as an argument, and
249c50c785cSJohn Marino the element's symbols must be added to the linker hash table
2505796c8dcSSimon Schubert just as though the element had itself been passed to the
251c50c785cSJohn Marino <<_bfd_link_add_symbols>> function. The <<add_archive_element>>
252c50c785cSJohn Marino callback has the option to indicate that it would like to
253c50c785cSJohn Marino replace the element archive with a substitute BFD, in which
254c50c785cSJohn Marino case it is the symbols of that substitute BFD that must be
255c50c785cSJohn Marino added to the linker hash table instead.
2565796c8dcSSimon Schubert
2575796c8dcSSimon Schubert When the a.out <<_bfd_link_add_symbols>> function receives an
2585796c8dcSSimon Schubert archive, it calls <<_bfd_generic_link_add_archive_symbols>>
2595796c8dcSSimon Schubert passing <<aout_link_check_archive_element>> as the function
2605796c8dcSSimon Schubert argument. <<aout_link_check_archive_element>> calls
2615796c8dcSSimon Schubert <<aout_link_check_ar_symbols>>. If the latter decides to add
2625796c8dcSSimon Schubert the element (an element is only added if it provides a real,
2635796c8dcSSimon Schubert non-common, definition for a previously undefined or common
2645796c8dcSSimon Schubert symbol) it calls the <<add_archive_element>> callback and then
2655796c8dcSSimon Schubert <<aout_link_check_archive_element>> calls
2665796c8dcSSimon Schubert <<aout_link_add_symbols>> to actually add the symbols to the
267c50c785cSJohn Marino linker hash table - possibly those of a substitute BFD, if the
268c50c785cSJohn Marino <<add_archive_element>> callback avails itself of that option.
2695796c8dcSSimon Schubert
2705796c8dcSSimon Schubert The ECOFF back end is unusual in that it does not normally
2715796c8dcSSimon Schubert call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
2725796c8dcSSimon Schubert archives already contain a hash table of symbols. The ECOFF
2735796c8dcSSimon Schubert back end searches the archive itself to avoid the overhead of
2745796c8dcSSimon Schubert creating a new hash table.
2755796c8dcSSimon Schubert
2765796c8dcSSimon Schubert INODE
2775796c8dcSSimon Schubert Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
2785796c8dcSSimon Schubert SUBSECTION
2795796c8dcSSimon Schubert Performing the final link
2805796c8dcSSimon Schubert
2815796c8dcSSimon Schubert @cindex _bfd_link_final_link in target vector
2825796c8dcSSimon Schubert @cindex target vector (_bfd_final_link)
2835796c8dcSSimon Schubert When all the input files have been processed, the linker calls
2845796c8dcSSimon Schubert the <<_bfd_final_link>> entry point of the output BFD. This
2855796c8dcSSimon Schubert routine is responsible for producing the final output file,
2865796c8dcSSimon Schubert which has several aspects. It must relocate the contents of
2875796c8dcSSimon Schubert the input sections and copy the data into the output sections.
2885796c8dcSSimon Schubert It must build an output symbol table including any local
2895796c8dcSSimon Schubert symbols from the input files and the global symbols from the
2905796c8dcSSimon Schubert hash table. When producing relocatable output, it must
2915796c8dcSSimon Schubert modify the input relocs and write them into the output file.
2925796c8dcSSimon Schubert There may also be object format dependent work to be done.
2935796c8dcSSimon Schubert
2945796c8dcSSimon Schubert The linker will also call the <<write_object_contents>> entry
2955796c8dcSSimon Schubert point when the BFD is closed. The two entry points must work
2965796c8dcSSimon Schubert together in order to produce the correct output file.
2975796c8dcSSimon Schubert
2985796c8dcSSimon Schubert The details of how this works are inevitably dependent upon
2995796c8dcSSimon Schubert the specific object file format. The a.out
3005796c8dcSSimon Schubert <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
3015796c8dcSSimon Schubert
3025796c8dcSSimon Schubert @menu
3035796c8dcSSimon Schubert @* Information provided by the linker::
3045796c8dcSSimon Schubert @* Relocating the section contents::
3055796c8dcSSimon Schubert @* Writing the symbol table::
3065796c8dcSSimon Schubert @end menu
3075796c8dcSSimon Schubert
3085796c8dcSSimon Schubert INODE
3095796c8dcSSimon Schubert Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
3105796c8dcSSimon Schubert SUBSUBSECTION
3115796c8dcSSimon Schubert Information provided by the linker
3125796c8dcSSimon Schubert
3135796c8dcSSimon Schubert Before the linker calls the <<_bfd_final_link>> entry point,
3145796c8dcSSimon Schubert it sets up some data structures for the function to use.
3155796c8dcSSimon Schubert
3165796c8dcSSimon Schubert The <<input_bfds>> field of the <<bfd_link_info>> structure
3175796c8dcSSimon Schubert will point to a list of all the input files included in the
3185796c8dcSSimon Schubert link. These files are linked through the <<link_next>> field
3195796c8dcSSimon Schubert of the <<bfd>> structure.
3205796c8dcSSimon Schubert
3215796c8dcSSimon Schubert Each section in the output file will have a list of
3225796c8dcSSimon Schubert <<link_order>> structures attached to the <<map_head.link_order>>
3235796c8dcSSimon Schubert field (the <<link_order>> structure is defined in
3245796c8dcSSimon Schubert <<bfdlink.h>>). These structures describe how to create the
3255796c8dcSSimon Schubert contents of the output section in terms of the contents of
3265796c8dcSSimon Schubert various input sections, fill constants, and, eventually, other
3275796c8dcSSimon Schubert types of information. They also describe relocs that must be
3285796c8dcSSimon Schubert created by the BFD backend, but do not correspond to any input
3295796c8dcSSimon Schubert file; this is used to support -Ur, which builds constructors
3305796c8dcSSimon Schubert while generating a relocatable object file.
3315796c8dcSSimon Schubert
3325796c8dcSSimon Schubert INODE
3335796c8dcSSimon Schubert Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
3345796c8dcSSimon Schubert SUBSUBSECTION
3355796c8dcSSimon Schubert Relocating the section contents
3365796c8dcSSimon Schubert
3375796c8dcSSimon Schubert The <<_bfd_final_link>> function should look through the
3385796c8dcSSimon Schubert <<link_order>> structures attached to each section of the
3395796c8dcSSimon Schubert output file. Each <<link_order>> structure should either be
3405796c8dcSSimon Schubert handled specially, or it should be passed to the function
3415796c8dcSSimon Schubert <<_bfd_default_link_order>> which will do the right thing
3425796c8dcSSimon Schubert (<<_bfd_default_link_order>> is defined in <<linker.c>>).
3435796c8dcSSimon Schubert
3445796c8dcSSimon Schubert For efficiency, a <<link_order>> of type
3455796c8dcSSimon Schubert <<bfd_indirect_link_order>> whose associated section belongs
3465796c8dcSSimon Schubert to a BFD of the same format as the output BFD must be handled
3475796c8dcSSimon Schubert specially. This type of <<link_order>> describes part of an
3485796c8dcSSimon Schubert output section in terms of a section belonging to one of the
3495796c8dcSSimon Schubert input files. The <<_bfd_final_link>> function should read the
3505796c8dcSSimon Schubert contents of the section and any associated relocs, apply the
3515796c8dcSSimon Schubert relocs to the section contents, and write out the modified
3525796c8dcSSimon Schubert section contents. If performing a relocatable link, the
3535796c8dcSSimon Schubert relocs themselves must also be modified and written out.
3545796c8dcSSimon Schubert
3555796c8dcSSimon Schubert @findex _bfd_relocate_contents
3565796c8dcSSimon Schubert @findex _bfd_final_link_relocate
3575796c8dcSSimon Schubert The functions <<_bfd_relocate_contents>> and
3585796c8dcSSimon Schubert <<_bfd_final_link_relocate>> provide some general support for
3595796c8dcSSimon Schubert performing the actual relocations, notably overflow checking.
3605796c8dcSSimon Schubert Their arguments include information about the symbol the
3615796c8dcSSimon Schubert relocation is against and a <<reloc_howto_type>> argument
3625796c8dcSSimon Schubert which describes the relocation to perform. These functions
3635796c8dcSSimon Schubert are defined in <<reloc.c>>.
3645796c8dcSSimon Schubert
3655796c8dcSSimon Schubert The a.out function which handles reading, relocating, and
3665796c8dcSSimon Schubert writing section contents is <<aout_link_input_section>>. The
3675796c8dcSSimon Schubert actual relocation is done in <<aout_link_input_section_std>>
3685796c8dcSSimon Schubert and <<aout_link_input_section_ext>>.
3695796c8dcSSimon Schubert
3705796c8dcSSimon Schubert INODE
3715796c8dcSSimon Schubert Writing the symbol table, , Relocating the section contents, Performing the Final Link
3725796c8dcSSimon Schubert SUBSUBSECTION
3735796c8dcSSimon Schubert Writing the symbol table
3745796c8dcSSimon Schubert
3755796c8dcSSimon Schubert The <<_bfd_final_link>> function must gather all the symbols
3765796c8dcSSimon Schubert in the input files and write them out. It must also write out
3775796c8dcSSimon Schubert all the symbols in the global hash table. This must be
3785796c8dcSSimon Schubert controlled by the <<strip>> and <<discard>> fields of the
3795796c8dcSSimon Schubert <<bfd_link_info>> structure.
3805796c8dcSSimon Schubert
3815796c8dcSSimon Schubert The local symbols of the input files will not have been
3825796c8dcSSimon Schubert entered into the linker hash table. The <<_bfd_final_link>>
3835796c8dcSSimon Schubert routine must consider each input file and include the symbols
3845796c8dcSSimon Schubert in the output file. It may be convenient to do this when
3855796c8dcSSimon Schubert looking through the <<link_order>> structures, or it may be
3865796c8dcSSimon Schubert done by stepping through the <<input_bfds>> list.
3875796c8dcSSimon Schubert
3885796c8dcSSimon Schubert The <<_bfd_final_link>> routine must also traverse the global
3895796c8dcSSimon Schubert hash table to gather all the externally visible symbols. It
3905796c8dcSSimon Schubert is possible that most of the externally visible symbols may be
3915796c8dcSSimon Schubert written out when considering the symbols of each input file,
3925796c8dcSSimon Schubert but it is still necessary to traverse the hash table since the
3935796c8dcSSimon Schubert linker script may have defined some symbols that are not in
3945796c8dcSSimon Schubert any of the input files.
3955796c8dcSSimon Schubert
3965796c8dcSSimon Schubert The <<strip>> field of the <<bfd_link_info>> structure
3975796c8dcSSimon Schubert controls which symbols are written out. The possible values
3985796c8dcSSimon Schubert are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
3995796c8dcSSimon Schubert then the <<keep_hash>> field of the <<bfd_link_info>>
4005796c8dcSSimon Schubert structure is a hash table of symbols to keep; each symbol
4015796c8dcSSimon Schubert should be looked up in this hash table, and only symbols which
4025796c8dcSSimon Schubert are present should be included in the output file.
4035796c8dcSSimon Schubert
4045796c8dcSSimon Schubert If the <<strip>> field of the <<bfd_link_info>> structure
4055796c8dcSSimon Schubert permits local symbols to be written out, the <<discard>> field
4065796c8dcSSimon Schubert is used to further controls which local symbols are included
4075796c8dcSSimon Schubert in the output file. If the value is <<discard_l>>, then all
4085796c8dcSSimon Schubert local symbols which begin with a certain prefix are discarded;
4095796c8dcSSimon Schubert this is controlled by the <<bfd_is_local_label_name>> entry point.
4105796c8dcSSimon Schubert
4115796c8dcSSimon Schubert The a.out backend handles symbols by calling
4125796c8dcSSimon Schubert <<aout_link_write_symbols>> on each input BFD and then
4135796c8dcSSimon Schubert traversing the global hash table with the function
4145796c8dcSSimon Schubert <<aout_link_write_other_symbol>>. It builds a string table
4155796c8dcSSimon Schubert while writing out the symbols, which is written to the output
4165796c8dcSSimon Schubert file at the end of <<NAME(aout,final_link)>>.
4175796c8dcSSimon Schubert */
4185796c8dcSSimon Schubert
4195796c8dcSSimon Schubert static bfd_boolean generic_link_add_object_symbols
4205796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_boolean collect);
4215796c8dcSSimon Schubert static bfd_boolean generic_link_add_symbols
4225796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_boolean);
4235796c8dcSSimon Schubert static bfd_boolean generic_link_check_archive_element_no_collect
4245796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_boolean *);
4255796c8dcSSimon Schubert static bfd_boolean generic_link_check_archive_element_collect
4265796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_boolean *);
4275796c8dcSSimon Schubert static bfd_boolean generic_link_check_archive_element
4285796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
4295796c8dcSSimon Schubert static bfd_boolean generic_link_add_symbol_list
4305796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
4315796c8dcSSimon Schubert bfd_boolean);
4325796c8dcSSimon Schubert static bfd_boolean generic_add_output_symbol
4335796c8dcSSimon Schubert (bfd *, size_t *psymalloc, asymbol *);
4345796c8dcSSimon Schubert static bfd_boolean default_data_link_order
4355796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
4365796c8dcSSimon Schubert static bfd_boolean default_indirect_link_order
4375796c8dcSSimon Schubert (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
4385796c8dcSSimon Schubert bfd_boolean);
4395796c8dcSSimon Schubert
4405796c8dcSSimon Schubert /* The link hash table structure is defined in bfdlink.h. It provides
4415796c8dcSSimon Schubert a base hash table which the backend specific hash tables are built
4425796c8dcSSimon Schubert upon. */
4435796c8dcSSimon Schubert
4445796c8dcSSimon Schubert /* Routine to create an entry in the link hash table. */
4455796c8dcSSimon Schubert
4465796c8dcSSimon Schubert struct bfd_hash_entry *
_bfd_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)4475796c8dcSSimon Schubert _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
4485796c8dcSSimon Schubert struct bfd_hash_table *table,
4495796c8dcSSimon Schubert const char *string)
4505796c8dcSSimon Schubert {
4515796c8dcSSimon Schubert /* Allocate the structure if it has not already been allocated by a
4525796c8dcSSimon Schubert subclass. */
4535796c8dcSSimon Schubert if (entry == NULL)
4545796c8dcSSimon Schubert {
4555796c8dcSSimon Schubert entry = (struct bfd_hash_entry *)
4565796c8dcSSimon Schubert bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
4575796c8dcSSimon Schubert if (entry == NULL)
4585796c8dcSSimon Schubert return entry;
4595796c8dcSSimon Schubert }
4605796c8dcSSimon Schubert
4615796c8dcSSimon Schubert /* Call the allocation method of the superclass. */
4625796c8dcSSimon Schubert entry = bfd_hash_newfunc (entry, table, string);
4635796c8dcSSimon Schubert if (entry)
4645796c8dcSSimon Schubert {
4655796c8dcSSimon Schubert struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
4665796c8dcSSimon Schubert
4675796c8dcSSimon Schubert /* Initialize the local fields. */
468a45ae5f8SJohn Marino memset ((char *) &h->root + sizeof (h->root), 0,
469a45ae5f8SJohn Marino sizeof (*h) - sizeof (h->root));
4705796c8dcSSimon Schubert }
4715796c8dcSSimon Schubert
4725796c8dcSSimon Schubert return entry;
4735796c8dcSSimon Schubert }
4745796c8dcSSimon Schubert
4755796c8dcSSimon Schubert /* Initialize a link hash table. The BFD argument is the one
4765796c8dcSSimon Schubert responsible for creating this table. */
4775796c8dcSSimon Schubert
4785796c8dcSSimon Schubert bfd_boolean
_bfd_link_hash_table_init(struct bfd_link_hash_table * table,bfd * abfd ATTRIBUTE_UNUSED,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize)4795796c8dcSSimon Schubert _bfd_link_hash_table_init
4805796c8dcSSimon Schubert (struct bfd_link_hash_table *table,
4815796c8dcSSimon Schubert bfd *abfd ATTRIBUTE_UNUSED,
4825796c8dcSSimon Schubert struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
4835796c8dcSSimon Schubert struct bfd_hash_table *,
4845796c8dcSSimon Schubert const char *),
4855796c8dcSSimon Schubert unsigned int entsize)
4865796c8dcSSimon Schubert {
4875796c8dcSSimon Schubert table->undefs = NULL;
4885796c8dcSSimon Schubert table->undefs_tail = NULL;
4895796c8dcSSimon Schubert table->type = bfd_link_generic_hash_table;
4905796c8dcSSimon Schubert
4915796c8dcSSimon Schubert return bfd_hash_table_init (&table->table, newfunc, entsize);
4925796c8dcSSimon Schubert }
4935796c8dcSSimon Schubert
4945796c8dcSSimon Schubert /* Look up a symbol in a link hash table. If follow is TRUE, we
4955796c8dcSSimon Schubert follow bfd_link_hash_indirect and bfd_link_hash_warning links to
4965796c8dcSSimon Schubert the real symbol. */
4975796c8dcSSimon Schubert
4985796c8dcSSimon Schubert struct bfd_link_hash_entry *
bfd_link_hash_lookup(struct bfd_link_hash_table * table,const char * string,bfd_boolean create,bfd_boolean copy,bfd_boolean follow)4995796c8dcSSimon Schubert bfd_link_hash_lookup (struct bfd_link_hash_table *table,
5005796c8dcSSimon Schubert const char *string,
5015796c8dcSSimon Schubert bfd_boolean create,
5025796c8dcSSimon Schubert bfd_boolean copy,
5035796c8dcSSimon Schubert bfd_boolean follow)
5045796c8dcSSimon Schubert {
5055796c8dcSSimon Schubert struct bfd_link_hash_entry *ret;
5065796c8dcSSimon Schubert
5075796c8dcSSimon Schubert ret = ((struct bfd_link_hash_entry *)
5085796c8dcSSimon Schubert bfd_hash_lookup (&table->table, string, create, copy));
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert if (follow && ret != NULL)
5115796c8dcSSimon Schubert {
5125796c8dcSSimon Schubert while (ret->type == bfd_link_hash_indirect
5135796c8dcSSimon Schubert || ret->type == bfd_link_hash_warning)
5145796c8dcSSimon Schubert ret = ret->u.i.link;
5155796c8dcSSimon Schubert }
5165796c8dcSSimon Schubert
5175796c8dcSSimon Schubert return ret;
5185796c8dcSSimon Schubert }
5195796c8dcSSimon Schubert
5205796c8dcSSimon Schubert /* Look up a symbol in the main linker hash table if the symbol might
5215796c8dcSSimon Schubert be wrapped. This should only be used for references to an
5225796c8dcSSimon Schubert undefined symbol, not for definitions of a symbol. */
5235796c8dcSSimon Schubert
5245796c8dcSSimon Schubert struct bfd_link_hash_entry *
bfd_wrapped_link_hash_lookup(bfd * abfd,struct bfd_link_info * info,const char * string,bfd_boolean create,bfd_boolean copy,bfd_boolean follow)5255796c8dcSSimon Schubert bfd_wrapped_link_hash_lookup (bfd *abfd,
5265796c8dcSSimon Schubert struct bfd_link_info *info,
5275796c8dcSSimon Schubert const char *string,
5285796c8dcSSimon Schubert bfd_boolean create,
5295796c8dcSSimon Schubert bfd_boolean copy,
5305796c8dcSSimon Schubert bfd_boolean follow)
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert bfd_size_type amt;
5335796c8dcSSimon Schubert
5345796c8dcSSimon Schubert if (info->wrap_hash != NULL)
5355796c8dcSSimon Schubert {
5365796c8dcSSimon Schubert const char *l;
5375796c8dcSSimon Schubert char prefix = '\0';
5385796c8dcSSimon Schubert
5395796c8dcSSimon Schubert l = string;
5405796c8dcSSimon Schubert if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
5415796c8dcSSimon Schubert {
5425796c8dcSSimon Schubert prefix = *l;
5435796c8dcSSimon Schubert ++l;
5445796c8dcSSimon Schubert }
5455796c8dcSSimon Schubert
5465796c8dcSSimon Schubert #undef WRAP
5475796c8dcSSimon Schubert #define WRAP "__wrap_"
5485796c8dcSSimon Schubert
5495796c8dcSSimon Schubert if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
5505796c8dcSSimon Schubert {
5515796c8dcSSimon Schubert char *n;
5525796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
5535796c8dcSSimon Schubert
5545796c8dcSSimon Schubert /* This symbol is being wrapped. We want to replace all
5555796c8dcSSimon Schubert references to SYM with references to __wrap_SYM. */
5565796c8dcSSimon Schubert
5575796c8dcSSimon Schubert amt = strlen (l) + sizeof WRAP + 1;
5585796c8dcSSimon Schubert n = (char *) bfd_malloc (amt);
5595796c8dcSSimon Schubert if (n == NULL)
5605796c8dcSSimon Schubert return NULL;
5615796c8dcSSimon Schubert
5625796c8dcSSimon Schubert n[0] = prefix;
5635796c8dcSSimon Schubert n[1] = '\0';
5645796c8dcSSimon Schubert strcat (n, WRAP);
5655796c8dcSSimon Schubert strcat (n, l);
5665796c8dcSSimon Schubert h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
5675796c8dcSSimon Schubert free (n);
5685796c8dcSSimon Schubert return h;
5695796c8dcSSimon Schubert }
5705796c8dcSSimon Schubert
5715796c8dcSSimon Schubert #undef WRAP
5725796c8dcSSimon Schubert
5735796c8dcSSimon Schubert #undef REAL
5745796c8dcSSimon Schubert #define REAL "__real_"
5755796c8dcSSimon Schubert
5765796c8dcSSimon Schubert if (*l == '_'
5775796c8dcSSimon Schubert && CONST_STRNEQ (l, REAL)
5785796c8dcSSimon Schubert && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
5795796c8dcSSimon Schubert FALSE, FALSE) != NULL)
5805796c8dcSSimon Schubert {
5815796c8dcSSimon Schubert char *n;
5825796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
5835796c8dcSSimon Schubert
5845796c8dcSSimon Schubert /* This is a reference to __real_SYM, where SYM is being
5855796c8dcSSimon Schubert wrapped. We want to replace all references to __real_SYM
5865796c8dcSSimon Schubert with references to SYM. */
5875796c8dcSSimon Schubert
5885796c8dcSSimon Schubert amt = strlen (l + sizeof REAL - 1) + 2;
5895796c8dcSSimon Schubert n = (char *) bfd_malloc (amt);
5905796c8dcSSimon Schubert if (n == NULL)
5915796c8dcSSimon Schubert return NULL;
5925796c8dcSSimon Schubert
5935796c8dcSSimon Schubert n[0] = prefix;
5945796c8dcSSimon Schubert n[1] = '\0';
5955796c8dcSSimon Schubert strcat (n, l + sizeof REAL - 1);
5965796c8dcSSimon Schubert h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
5975796c8dcSSimon Schubert free (n);
5985796c8dcSSimon Schubert return h;
5995796c8dcSSimon Schubert }
6005796c8dcSSimon Schubert
6015796c8dcSSimon Schubert #undef REAL
6025796c8dcSSimon Schubert }
6035796c8dcSSimon Schubert
6045796c8dcSSimon Schubert return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
6055796c8dcSSimon Schubert }
6065796c8dcSSimon Schubert
607a45ae5f8SJohn Marino /* Traverse a generic link hash table. Differs from bfd_hash_traverse
608a45ae5f8SJohn Marino in the treatment of warning symbols. When warning symbols are
609a45ae5f8SJohn Marino created they replace the real symbol, so you don't get to see the
610a45ae5f8SJohn Marino real symbol in a bfd_hash_travere. This traversal calls func with
611a45ae5f8SJohn Marino the real symbol. */
6125796c8dcSSimon Schubert
6135796c8dcSSimon Schubert void
bfd_link_hash_traverse(struct bfd_link_hash_table * htab,bfd_boolean (* func)(struct bfd_link_hash_entry *,void *),void * info)6145796c8dcSSimon Schubert bfd_link_hash_traverse
615a45ae5f8SJohn Marino (struct bfd_link_hash_table *htab,
6165796c8dcSSimon Schubert bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
6175796c8dcSSimon Schubert void *info)
6185796c8dcSSimon Schubert {
619a45ae5f8SJohn Marino unsigned int i;
620a45ae5f8SJohn Marino
621a45ae5f8SJohn Marino htab->table.frozen = 1;
622a45ae5f8SJohn Marino for (i = 0; i < htab->table.size; i++)
623a45ae5f8SJohn Marino {
624a45ae5f8SJohn Marino struct bfd_link_hash_entry *p;
625a45ae5f8SJohn Marino
626a45ae5f8SJohn Marino p = (struct bfd_link_hash_entry *) htab->table.table[i];
627a45ae5f8SJohn Marino for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
628a45ae5f8SJohn Marino if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
629a45ae5f8SJohn Marino goto out;
630a45ae5f8SJohn Marino }
631a45ae5f8SJohn Marino out:
632a45ae5f8SJohn Marino htab->table.frozen = 0;
6335796c8dcSSimon Schubert }
6345796c8dcSSimon Schubert
6355796c8dcSSimon Schubert /* Add a symbol to the linker hash table undefs list. */
6365796c8dcSSimon Schubert
6375796c8dcSSimon Schubert void
bfd_link_add_undef(struct bfd_link_hash_table * table,struct bfd_link_hash_entry * h)6385796c8dcSSimon Schubert bfd_link_add_undef (struct bfd_link_hash_table *table,
6395796c8dcSSimon Schubert struct bfd_link_hash_entry *h)
6405796c8dcSSimon Schubert {
6415796c8dcSSimon Schubert BFD_ASSERT (h->u.undef.next == NULL);
6425796c8dcSSimon Schubert if (table->undefs_tail != NULL)
6435796c8dcSSimon Schubert table->undefs_tail->u.undef.next = h;
6445796c8dcSSimon Schubert if (table->undefs == NULL)
6455796c8dcSSimon Schubert table->undefs = h;
6465796c8dcSSimon Schubert table->undefs_tail = h;
6475796c8dcSSimon Schubert }
6485796c8dcSSimon Schubert
6495796c8dcSSimon Schubert /* The undefs list was designed so that in normal use we don't need to
6505796c8dcSSimon Schubert remove entries. However, if symbols on the list are changed from
6515796c8dcSSimon Schubert bfd_link_hash_undefined to either bfd_link_hash_undefweak or
6525796c8dcSSimon Schubert bfd_link_hash_new for some reason, then they must be removed from the
6535796c8dcSSimon Schubert list. Failure to do so might result in the linker attempting to add
6545796c8dcSSimon Schubert the symbol to the list again at a later stage. */
6555796c8dcSSimon Schubert
6565796c8dcSSimon Schubert void
bfd_link_repair_undef_list(struct bfd_link_hash_table * table)6575796c8dcSSimon Schubert bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
6585796c8dcSSimon Schubert {
6595796c8dcSSimon Schubert struct bfd_link_hash_entry **pun;
6605796c8dcSSimon Schubert
6615796c8dcSSimon Schubert pun = &table->undefs;
6625796c8dcSSimon Schubert while (*pun != NULL)
6635796c8dcSSimon Schubert {
6645796c8dcSSimon Schubert struct bfd_link_hash_entry *h = *pun;
6655796c8dcSSimon Schubert
6665796c8dcSSimon Schubert if (h->type == bfd_link_hash_new
6675796c8dcSSimon Schubert || h->type == bfd_link_hash_undefweak)
6685796c8dcSSimon Schubert {
6695796c8dcSSimon Schubert *pun = h->u.undef.next;
6705796c8dcSSimon Schubert h->u.undef.next = NULL;
6715796c8dcSSimon Schubert if (h == table->undefs_tail)
6725796c8dcSSimon Schubert {
6735796c8dcSSimon Schubert if (pun == &table->undefs)
6745796c8dcSSimon Schubert table->undefs_tail = NULL;
6755796c8dcSSimon Schubert else
6765796c8dcSSimon Schubert /* pun points at an u.undef.next field. Go back to
6775796c8dcSSimon Schubert the start of the link_hash_entry. */
6785796c8dcSSimon Schubert table->undefs_tail = (struct bfd_link_hash_entry *)
6795796c8dcSSimon Schubert ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
6805796c8dcSSimon Schubert break;
6815796c8dcSSimon Schubert }
6825796c8dcSSimon Schubert }
6835796c8dcSSimon Schubert else
6845796c8dcSSimon Schubert pun = &h->u.undef.next;
6855796c8dcSSimon Schubert }
6865796c8dcSSimon Schubert }
6875796c8dcSSimon Schubert
6885796c8dcSSimon Schubert /* Routine to create an entry in a generic link hash table. */
6895796c8dcSSimon Schubert
6905796c8dcSSimon Schubert struct bfd_hash_entry *
_bfd_generic_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)6915796c8dcSSimon Schubert _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
6925796c8dcSSimon Schubert struct bfd_hash_table *table,
6935796c8dcSSimon Schubert const char *string)
6945796c8dcSSimon Schubert {
6955796c8dcSSimon Schubert /* Allocate the structure if it has not already been allocated by a
6965796c8dcSSimon Schubert subclass. */
6975796c8dcSSimon Schubert if (entry == NULL)
6985796c8dcSSimon Schubert {
6995796c8dcSSimon Schubert entry = (struct bfd_hash_entry *)
7005796c8dcSSimon Schubert bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
7015796c8dcSSimon Schubert if (entry == NULL)
7025796c8dcSSimon Schubert return entry;
7035796c8dcSSimon Schubert }
7045796c8dcSSimon Schubert
7055796c8dcSSimon Schubert /* Call the allocation method of the superclass. */
7065796c8dcSSimon Schubert entry = _bfd_link_hash_newfunc (entry, table, string);
7075796c8dcSSimon Schubert if (entry)
7085796c8dcSSimon Schubert {
7095796c8dcSSimon Schubert struct generic_link_hash_entry *ret;
7105796c8dcSSimon Schubert
7115796c8dcSSimon Schubert /* Set local fields. */
7125796c8dcSSimon Schubert ret = (struct generic_link_hash_entry *) entry;
7135796c8dcSSimon Schubert ret->written = FALSE;
7145796c8dcSSimon Schubert ret->sym = NULL;
7155796c8dcSSimon Schubert }
7165796c8dcSSimon Schubert
7175796c8dcSSimon Schubert return entry;
7185796c8dcSSimon Schubert }
7195796c8dcSSimon Schubert
7205796c8dcSSimon Schubert /* Create a generic link hash table. */
7215796c8dcSSimon Schubert
7225796c8dcSSimon Schubert struct bfd_link_hash_table *
_bfd_generic_link_hash_table_create(bfd * abfd)7235796c8dcSSimon Schubert _bfd_generic_link_hash_table_create (bfd *abfd)
7245796c8dcSSimon Schubert {
7255796c8dcSSimon Schubert struct generic_link_hash_table *ret;
7265796c8dcSSimon Schubert bfd_size_type amt = sizeof (struct generic_link_hash_table);
7275796c8dcSSimon Schubert
7285796c8dcSSimon Schubert ret = (struct generic_link_hash_table *) bfd_malloc (amt);
7295796c8dcSSimon Schubert if (ret == NULL)
7305796c8dcSSimon Schubert return NULL;
7315796c8dcSSimon Schubert if (! _bfd_link_hash_table_init (&ret->root, abfd,
7325796c8dcSSimon Schubert _bfd_generic_link_hash_newfunc,
7335796c8dcSSimon Schubert sizeof (struct generic_link_hash_entry)))
7345796c8dcSSimon Schubert {
7355796c8dcSSimon Schubert free (ret);
7365796c8dcSSimon Schubert return NULL;
7375796c8dcSSimon Schubert }
7385796c8dcSSimon Schubert return &ret->root;
7395796c8dcSSimon Schubert }
7405796c8dcSSimon Schubert
7415796c8dcSSimon Schubert void
_bfd_generic_link_hash_table_free(struct bfd_link_hash_table * hash)7425796c8dcSSimon Schubert _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
7435796c8dcSSimon Schubert {
7445796c8dcSSimon Schubert struct generic_link_hash_table *ret
7455796c8dcSSimon Schubert = (struct generic_link_hash_table *) hash;
7465796c8dcSSimon Schubert
7475796c8dcSSimon Schubert bfd_hash_table_free (&ret->root.table);
7485796c8dcSSimon Schubert free (ret);
7495796c8dcSSimon Schubert }
7505796c8dcSSimon Schubert
7515796c8dcSSimon Schubert /* Grab the symbols for an object file when doing a generic link. We
7525796c8dcSSimon Schubert store the symbols in the outsymbols field. We need to keep them
7535796c8dcSSimon Schubert around for the entire link to ensure that we only read them once.
7545796c8dcSSimon Schubert If we read them multiple times, we might wind up with relocs and
7555796c8dcSSimon Schubert the hash table pointing to different instances of the symbol
7565796c8dcSSimon Schubert structure. */
7575796c8dcSSimon Schubert
7585796c8dcSSimon Schubert bfd_boolean
bfd_generic_link_read_symbols(bfd * abfd)7595796c8dcSSimon Schubert bfd_generic_link_read_symbols (bfd *abfd)
7605796c8dcSSimon Schubert {
7615796c8dcSSimon Schubert if (bfd_get_outsymbols (abfd) == NULL)
7625796c8dcSSimon Schubert {
7635796c8dcSSimon Schubert long symsize;
7645796c8dcSSimon Schubert long symcount;
7655796c8dcSSimon Schubert
7665796c8dcSSimon Schubert symsize = bfd_get_symtab_upper_bound (abfd);
7675796c8dcSSimon Schubert if (symsize < 0)
7685796c8dcSSimon Schubert return FALSE;
7695796c8dcSSimon Schubert bfd_get_outsymbols (abfd) = (struct bfd_symbol **) bfd_alloc (abfd,
7705796c8dcSSimon Schubert symsize);
7715796c8dcSSimon Schubert if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
7725796c8dcSSimon Schubert return FALSE;
7735796c8dcSSimon Schubert symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
7745796c8dcSSimon Schubert if (symcount < 0)
7755796c8dcSSimon Schubert return FALSE;
7765796c8dcSSimon Schubert bfd_get_symcount (abfd) = symcount;
7775796c8dcSSimon Schubert }
7785796c8dcSSimon Schubert
7795796c8dcSSimon Schubert return TRUE;
7805796c8dcSSimon Schubert }
7815796c8dcSSimon Schubert
7825796c8dcSSimon Schubert /* Generic function to add symbols to from an object file to the
7835796c8dcSSimon Schubert global hash table. This version does not automatically collect
7845796c8dcSSimon Schubert constructors by name. */
7855796c8dcSSimon Schubert
7865796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info)7875796c8dcSSimon Schubert _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
7885796c8dcSSimon Schubert {
7895796c8dcSSimon Schubert return generic_link_add_symbols (abfd, info, FALSE);
7905796c8dcSSimon Schubert }
7915796c8dcSSimon Schubert
7925796c8dcSSimon Schubert /* Generic function to add symbols from an object file to the global
7935796c8dcSSimon Schubert hash table. This version automatically collects constructors by
7945796c8dcSSimon Schubert name, as the collect2 program does. It should be used for any
7955796c8dcSSimon Schubert target which does not provide some other mechanism for setting up
7965796c8dcSSimon Schubert constructors and destructors; these are approximately those targets
7975796c8dcSSimon Schubert for which gcc uses collect2 and do not support stabs. */
7985796c8dcSSimon Schubert
7995796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_add_symbols_collect(bfd * abfd,struct bfd_link_info * info)8005796c8dcSSimon Schubert _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert return generic_link_add_symbols (abfd, info, TRUE);
8035796c8dcSSimon Schubert }
8045796c8dcSSimon Schubert
8055796c8dcSSimon Schubert /* Indicate that we are only retrieving symbol values from this
8065796c8dcSSimon Schubert section. We want the symbols to act as though the values in the
8075796c8dcSSimon Schubert file are absolute. */
8085796c8dcSSimon Schubert
8095796c8dcSSimon Schubert void
_bfd_generic_link_just_syms(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED)8105796c8dcSSimon Schubert _bfd_generic_link_just_syms (asection *sec,
8115796c8dcSSimon Schubert struct bfd_link_info *info ATTRIBUTE_UNUSED)
8125796c8dcSSimon Schubert {
813*ef5ccd6cSJohn Marino sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
8145796c8dcSSimon Schubert sec->output_section = bfd_abs_section_ptr;
8155796c8dcSSimon Schubert sec->output_offset = sec->vma;
8165796c8dcSSimon Schubert }
8175796c8dcSSimon Schubert
818cf7f2e2dSJohn Marino /* Copy the type of a symbol assiciated with a linker hast table entry.
819cf7f2e2dSJohn Marino Override this so that symbols created in linker scripts get their
820cf7f2e2dSJohn Marino type from the RHS of the assignment.
821cf7f2e2dSJohn Marino The default implementation does nothing. */
822cf7f2e2dSJohn Marino void
_bfd_generic_copy_link_hash_symbol_type(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * hdest ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * hsrc ATTRIBUTE_UNUSED)823cf7f2e2dSJohn Marino _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
824cf7f2e2dSJohn Marino struct bfd_link_hash_entry * hdest ATTRIBUTE_UNUSED,
825cf7f2e2dSJohn Marino struct bfd_link_hash_entry * hsrc ATTRIBUTE_UNUSED)
826cf7f2e2dSJohn Marino {
827cf7f2e2dSJohn Marino }
828cf7f2e2dSJohn Marino
8295796c8dcSSimon Schubert /* Add symbols from an object file to the global hash table. */
8305796c8dcSSimon Schubert
8315796c8dcSSimon Schubert static bfd_boolean
generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)8325796c8dcSSimon Schubert generic_link_add_symbols (bfd *abfd,
8335796c8dcSSimon Schubert struct bfd_link_info *info,
8345796c8dcSSimon Schubert bfd_boolean collect)
8355796c8dcSSimon Schubert {
8365796c8dcSSimon Schubert bfd_boolean ret;
8375796c8dcSSimon Schubert
8385796c8dcSSimon Schubert switch (bfd_get_format (abfd))
8395796c8dcSSimon Schubert {
8405796c8dcSSimon Schubert case bfd_object:
8415796c8dcSSimon Schubert ret = generic_link_add_object_symbols (abfd, info, collect);
8425796c8dcSSimon Schubert break;
8435796c8dcSSimon Schubert case bfd_archive:
8445796c8dcSSimon Schubert ret = (_bfd_generic_link_add_archive_symbols
8455796c8dcSSimon Schubert (abfd, info,
8465796c8dcSSimon Schubert (collect
8475796c8dcSSimon Schubert ? generic_link_check_archive_element_collect
8485796c8dcSSimon Schubert : generic_link_check_archive_element_no_collect)));
8495796c8dcSSimon Schubert break;
8505796c8dcSSimon Schubert default:
8515796c8dcSSimon Schubert bfd_set_error (bfd_error_wrong_format);
8525796c8dcSSimon Schubert ret = FALSE;
8535796c8dcSSimon Schubert }
8545796c8dcSSimon Schubert
8555796c8dcSSimon Schubert return ret;
8565796c8dcSSimon Schubert }
8575796c8dcSSimon Schubert
8585796c8dcSSimon Schubert /* Add symbols from an object file to the global hash table. */
8595796c8dcSSimon Schubert
8605796c8dcSSimon Schubert static bfd_boolean
generic_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)8615796c8dcSSimon Schubert generic_link_add_object_symbols (bfd *abfd,
8625796c8dcSSimon Schubert struct bfd_link_info *info,
8635796c8dcSSimon Schubert bfd_boolean collect)
8645796c8dcSSimon Schubert {
8655796c8dcSSimon Schubert bfd_size_type symcount;
8665796c8dcSSimon Schubert struct bfd_symbol **outsyms;
8675796c8dcSSimon Schubert
8685796c8dcSSimon Schubert if (!bfd_generic_link_read_symbols (abfd))
8695796c8dcSSimon Schubert return FALSE;
8705796c8dcSSimon Schubert symcount = _bfd_generic_link_get_symcount (abfd);
8715796c8dcSSimon Schubert outsyms = _bfd_generic_link_get_symbols (abfd);
8725796c8dcSSimon Schubert return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert
8755796c8dcSSimon Schubert /* We build a hash table of all symbols defined in an archive. */
8765796c8dcSSimon Schubert
8775796c8dcSSimon Schubert /* An archive symbol may be defined by multiple archive elements.
8785796c8dcSSimon Schubert This linked list is used to hold the elements. */
8795796c8dcSSimon Schubert
8805796c8dcSSimon Schubert struct archive_list
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert struct archive_list *next;
8835796c8dcSSimon Schubert unsigned int indx;
8845796c8dcSSimon Schubert };
8855796c8dcSSimon Schubert
8865796c8dcSSimon Schubert /* An entry in an archive hash table. */
8875796c8dcSSimon Schubert
8885796c8dcSSimon Schubert struct archive_hash_entry
8895796c8dcSSimon Schubert {
8905796c8dcSSimon Schubert struct bfd_hash_entry root;
8915796c8dcSSimon Schubert /* Where the symbol is defined. */
8925796c8dcSSimon Schubert struct archive_list *defs;
8935796c8dcSSimon Schubert };
8945796c8dcSSimon Schubert
8955796c8dcSSimon Schubert /* An archive hash table itself. */
8965796c8dcSSimon Schubert
8975796c8dcSSimon Schubert struct archive_hash_table
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert struct bfd_hash_table table;
9005796c8dcSSimon Schubert };
9015796c8dcSSimon Schubert
9025796c8dcSSimon Schubert /* Create a new entry for an archive hash table. */
9035796c8dcSSimon Schubert
9045796c8dcSSimon Schubert static struct bfd_hash_entry *
archive_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)9055796c8dcSSimon Schubert archive_hash_newfunc (struct bfd_hash_entry *entry,
9065796c8dcSSimon Schubert struct bfd_hash_table *table,
9075796c8dcSSimon Schubert const char *string)
9085796c8dcSSimon Schubert {
9095796c8dcSSimon Schubert struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
9105796c8dcSSimon Schubert
9115796c8dcSSimon Schubert /* Allocate the structure if it has not already been allocated by a
9125796c8dcSSimon Schubert subclass. */
9135796c8dcSSimon Schubert if (ret == NULL)
9145796c8dcSSimon Schubert ret = (struct archive_hash_entry *)
9155796c8dcSSimon Schubert bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
9165796c8dcSSimon Schubert if (ret == NULL)
9175796c8dcSSimon Schubert return NULL;
9185796c8dcSSimon Schubert
9195796c8dcSSimon Schubert /* Call the allocation method of the superclass. */
9205796c8dcSSimon Schubert ret = ((struct archive_hash_entry *)
9215796c8dcSSimon Schubert bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
9225796c8dcSSimon Schubert
9235796c8dcSSimon Schubert if (ret)
9245796c8dcSSimon Schubert {
9255796c8dcSSimon Schubert /* Initialize the local fields. */
9265796c8dcSSimon Schubert ret->defs = NULL;
9275796c8dcSSimon Schubert }
9285796c8dcSSimon Schubert
9295796c8dcSSimon Schubert return &ret->root;
9305796c8dcSSimon Schubert }
9315796c8dcSSimon Schubert
9325796c8dcSSimon Schubert /* Initialize an archive hash table. */
9335796c8dcSSimon Schubert
9345796c8dcSSimon Schubert static bfd_boolean
archive_hash_table_init(struct archive_hash_table * table,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize)9355796c8dcSSimon Schubert archive_hash_table_init
9365796c8dcSSimon Schubert (struct archive_hash_table *table,
9375796c8dcSSimon Schubert struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
9385796c8dcSSimon Schubert struct bfd_hash_table *,
9395796c8dcSSimon Schubert const char *),
9405796c8dcSSimon Schubert unsigned int entsize)
9415796c8dcSSimon Schubert {
9425796c8dcSSimon Schubert return bfd_hash_table_init (&table->table, newfunc, entsize);
9435796c8dcSSimon Schubert }
9445796c8dcSSimon Schubert
9455796c8dcSSimon Schubert /* Look up an entry in an archive hash table. */
9465796c8dcSSimon Schubert
9475796c8dcSSimon Schubert #define archive_hash_lookup(t, string, create, copy) \
9485796c8dcSSimon Schubert ((struct archive_hash_entry *) \
9495796c8dcSSimon Schubert bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
9505796c8dcSSimon Schubert
9515796c8dcSSimon Schubert /* Allocate space in an archive hash table. */
9525796c8dcSSimon Schubert
9535796c8dcSSimon Schubert #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
9545796c8dcSSimon Schubert
9555796c8dcSSimon Schubert /* Free an archive hash table. */
9565796c8dcSSimon Schubert
9575796c8dcSSimon Schubert #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
9585796c8dcSSimon Schubert
9595796c8dcSSimon Schubert /* Generic function to add symbols from an archive file to the global
9605796c8dcSSimon Schubert hash file. This function presumes that the archive symbol table
9615796c8dcSSimon Schubert has already been read in (this is normally done by the
9625796c8dcSSimon Schubert bfd_check_format entry point). It looks through the undefined and
9635796c8dcSSimon Schubert common symbols and searches the archive symbol table for them. If
9645796c8dcSSimon Schubert it finds an entry, it includes the associated object file in the
9655796c8dcSSimon Schubert link.
9665796c8dcSSimon Schubert
9675796c8dcSSimon Schubert The old linker looked through the archive symbol table for
9685796c8dcSSimon Schubert undefined symbols. We do it the other way around, looking through
9695796c8dcSSimon Schubert undefined symbols for symbols defined in the archive. The
9705796c8dcSSimon Schubert advantage of the newer scheme is that we only have to look through
9715796c8dcSSimon Schubert the list of undefined symbols once, whereas the old method had to
9725796c8dcSSimon Schubert re-search the symbol table each time a new object file was added.
9735796c8dcSSimon Schubert
9745796c8dcSSimon Schubert The CHECKFN argument is used to see if an object file should be
9755796c8dcSSimon Schubert included. CHECKFN should set *PNEEDED to TRUE if the object file
9765796c8dcSSimon Schubert should be included, and must also call the bfd_link_info
9775796c8dcSSimon Schubert add_archive_element callback function and handle adding the symbols
978c50c785cSJohn Marino to the global hash table. CHECKFN must notice if the callback
979c50c785cSJohn Marino indicates a substitute BFD, and arrange to add those symbols instead
980c50c785cSJohn Marino if it does so. CHECKFN should only return FALSE if some sort of
981c50c785cSJohn Marino error occurs.
9825796c8dcSSimon Schubert
9835796c8dcSSimon Schubert For some formats, such as a.out, it is possible to look through an
9845796c8dcSSimon Schubert object file but not actually include it in the link. The
9855796c8dcSSimon Schubert archive_pass field in a BFD is used to avoid checking the symbols
9865796c8dcSSimon Schubert of an object files too many times. When an object is included in
9875796c8dcSSimon Schubert the link, archive_pass is set to -1. If an object is scanned but
9885796c8dcSSimon Schubert not included, archive_pass is set to the pass number. The pass
9895796c8dcSSimon Schubert number is incremented each time a new object file is included. The
9905796c8dcSSimon Schubert pass number is used because when a new object file is included it
9915796c8dcSSimon Schubert may create new undefined symbols which cause a previously examined
9925796c8dcSSimon Schubert object file to be included. */
9935796c8dcSSimon Schubert
9945796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean (* checkfn)(bfd *,struct bfd_link_info *,bfd_boolean *))9955796c8dcSSimon Schubert _bfd_generic_link_add_archive_symbols
9965796c8dcSSimon Schubert (bfd *abfd,
9975796c8dcSSimon Schubert struct bfd_link_info *info,
9985796c8dcSSimon Schubert bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
9995796c8dcSSimon Schubert {
10005796c8dcSSimon Schubert carsym *arsyms;
10015796c8dcSSimon Schubert carsym *arsym_end;
10025796c8dcSSimon Schubert register carsym *arsym;
10035796c8dcSSimon Schubert int pass;
10045796c8dcSSimon Schubert struct archive_hash_table arsym_hash;
10055796c8dcSSimon Schubert unsigned int indx;
10065796c8dcSSimon Schubert struct bfd_link_hash_entry **pundef;
10075796c8dcSSimon Schubert
10085796c8dcSSimon Schubert if (! bfd_has_map (abfd))
10095796c8dcSSimon Schubert {
10105796c8dcSSimon Schubert /* An empty archive is a special case. */
10115796c8dcSSimon Schubert if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
10125796c8dcSSimon Schubert return TRUE;
10135796c8dcSSimon Schubert bfd_set_error (bfd_error_no_armap);
10145796c8dcSSimon Schubert return FALSE;
10155796c8dcSSimon Schubert }
10165796c8dcSSimon Schubert
10175796c8dcSSimon Schubert arsyms = bfd_ardata (abfd)->symdefs;
10185796c8dcSSimon Schubert arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
10195796c8dcSSimon Schubert
10205796c8dcSSimon Schubert /* In order to quickly determine whether an symbol is defined in
10215796c8dcSSimon Schubert this archive, we build a hash table of the symbols. */
10225796c8dcSSimon Schubert if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
10235796c8dcSSimon Schubert sizeof (struct archive_hash_entry)))
10245796c8dcSSimon Schubert return FALSE;
10255796c8dcSSimon Schubert for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
10265796c8dcSSimon Schubert {
10275796c8dcSSimon Schubert struct archive_hash_entry *arh;
10285796c8dcSSimon Schubert struct archive_list *l, **pp;
10295796c8dcSSimon Schubert
10305796c8dcSSimon Schubert arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
10315796c8dcSSimon Schubert if (arh == NULL)
10325796c8dcSSimon Schubert goto error_return;
10335796c8dcSSimon Schubert l = ((struct archive_list *)
10345796c8dcSSimon Schubert archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
10355796c8dcSSimon Schubert if (l == NULL)
10365796c8dcSSimon Schubert goto error_return;
10375796c8dcSSimon Schubert l->indx = indx;
10385796c8dcSSimon Schubert for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
10395796c8dcSSimon Schubert ;
10405796c8dcSSimon Schubert *pp = l;
10415796c8dcSSimon Schubert l->next = NULL;
10425796c8dcSSimon Schubert }
10435796c8dcSSimon Schubert
10445796c8dcSSimon Schubert /* The archive_pass field in the archive itself is used to
10455796c8dcSSimon Schubert initialize PASS, sine we may search the same archive multiple
10465796c8dcSSimon Schubert times. */
10475796c8dcSSimon Schubert pass = abfd->archive_pass + 1;
10485796c8dcSSimon Schubert
10495796c8dcSSimon Schubert /* New undefined symbols are added to the end of the list, so we
10505796c8dcSSimon Schubert only need to look through it once. */
10515796c8dcSSimon Schubert pundef = &info->hash->undefs;
10525796c8dcSSimon Schubert while (*pundef != NULL)
10535796c8dcSSimon Schubert {
10545796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
10555796c8dcSSimon Schubert struct archive_hash_entry *arh;
10565796c8dcSSimon Schubert struct archive_list *l;
10575796c8dcSSimon Schubert
10585796c8dcSSimon Schubert h = *pundef;
10595796c8dcSSimon Schubert
10605796c8dcSSimon Schubert /* When a symbol is defined, it is not necessarily removed from
10615796c8dcSSimon Schubert the list. */
10625796c8dcSSimon Schubert if (h->type != bfd_link_hash_undefined
10635796c8dcSSimon Schubert && h->type != bfd_link_hash_common)
10645796c8dcSSimon Schubert {
10655796c8dcSSimon Schubert /* Remove this entry from the list, for general cleanliness
10665796c8dcSSimon Schubert and because we are going to look through the list again
10675796c8dcSSimon Schubert if we search any more libraries. We can't remove the
10685796c8dcSSimon Schubert entry if it is the tail, because that would lose any
10695796c8dcSSimon Schubert entries we add to the list later on (it would also cause
10705796c8dcSSimon Schubert us to lose track of whether the symbol has been
10715796c8dcSSimon Schubert referenced). */
10725796c8dcSSimon Schubert if (*pundef != info->hash->undefs_tail)
10735796c8dcSSimon Schubert *pundef = (*pundef)->u.undef.next;
10745796c8dcSSimon Schubert else
10755796c8dcSSimon Schubert pundef = &(*pundef)->u.undef.next;
10765796c8dcSSimon Schubert continue;
10775796c8dcSSimon Schubert }
10785796c8dcSSimon Schubert
10795796c8dcSSimon Schubert /* Look for this symbol in the archive symbol map. */
10805796c8dcSSimon Schubert arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
10815796c8dcSSimon Schubert if (arh == NULL)
10825796c8dcSSimon Schubert {
10835796c8dcSSimon Schubert /* If we haven't found the exact symbol we're looking for,
10845796c8dcSSimon Schubert let's look for its import thunk */
10855796c8dcSSimon Schubert if (info->pei386_auto_import)
10865796c8dcSSimon Schubert {
10875796c8dcSSimon Schubert bfd_size_type amt = strlen (h->root.string) + 10;
10885796c8dcSSimon Schubert char *buf = (char *) bfd_malloc (amt);
10895796c8dcSSimon Schubert if (buf == NULL)
10905796c8dcSSimon Schubert return FALSE;
10915796c8dcSSimon Schubert
10925796c8dcSSimon Schubert sprintf (buf, "__imp_%s", h->root.string);
10935796c8dcSSimon Schubert arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
10945796c8dcSSimon Schubert free(buf);
10955796c8dcSSimon Schubert }
10965796c8dcSSimon Schubert if (arh == NULL)
10975796c8dcSSimon Schubert {
10985796c8dcSSimon Schubert pundef = &(*pundef)->u.undef.next;
10995796c8dcSSimon Schubert continue;
11005796c8dcSSimon Schubert }
11015796c8dcSSimon Schubert }
11025796c8dcSSimon Schubert /* Look at all the objects which define this symbol. */
11035796c8dcSSimon Schubert for (l = arh->defs; l != NULL; l = l->next)
11045796c8dcSSimon Schubert {
11055796c8dcSSimon Schubert bfd *element;
11065796c8dcSSimon Schubert bfd_boolean needed;
11075796c8dcSSimon Schubert
11085796c8dcSSimon Schubert /* If the symbol has gotten defined along the way, quit. */
11095796c8dcSSimon Schubert if (h->type != bfd_link_hash_undefined
11105796c8dcSSimon Schubert && h->type != bfd_link_hash_common)
11115796c8dcSSimon Schubert break;
11125796c8dcSSimon Schubert
11135796c8dcSSimon Schubert element = bfd_get_elt_at_index (abfd, l->indx);
11145796c8dcSSimon Schubert if (element == NULL)
11155796c8dcSSimon Schubert goto error_return;
11165796c8dcSSimon Schubert
11175796c8dcSSimon Schubert /* If we've already included this element, or if we've
11185796c8dcSSimon Schubert already checked it on this pass, continue. */
11195796c8dcSSimon Schubert if (element->archive_pass == -1
11205796c8dcSSimon Schubert || element->archive_pass == pass)
11215796c8dcSSimon Schubert continue;
11225796c8dcSSimon Schubert
11235796c8dcSSimon Schubert /* If we can't figure this element out, just ignore it. */
11245796c8dcSSimon Schubert if (! bfd_check_format (element, bfd_object))
11255796c8dcSSimon Schubert {
11265796c8dcSSimon Schubert element->archive_pass = -1;
11275796c8dcSSimon Schubert continue;
11285796c8dcSSimon Schubert }
11295796c8dcSSimon Schubert
11305796c8dcSSimon Schubert /* CHECKFN will see if this element should be included, and
11315796c8dcSSimon Schubert go ahead and include it if appropriate. */
11325796c8dcSSimon Schubert if (! (*checkfn) (element, info, &needed))
11335796c8dcSSimon Schubert goto error_return;
11345796c8dcSSimon Schubert
11355796c8dcSSimon Schubert if (! needed)
11365796c8dcSSimon Schubert element->archive_pass = pass;
11375796c8dcSSimon Schubert else
11385796c8dcSSimon Schubert {
11395796c8dcSSimon Schubert element->archive_pass = -1;
11405796c8dcSSimon Schubert
11415796c8dcSSimon Schubert /* Increment the pass count to show that we may need to
11425796c8dcSSimon Schubert recheck object files which were already checked. */
11435796c8dcSSimon Schubert ++pass;
11445796c8dcSSimon Schubert }
11455796c8dcSSimon Schubert }
11465796c8dcSSimon Schubert
11475796c8dcSSimon Schubert pundef = &(*pundef)->u.undef.next;
11485796c8dcSSimon Schubert }
11495796c8dcSSimon Schubert
11505796c8dcSSimon Schubert archive_hash_table_free (&arsym_hash);
11515796c8dcSSimon Schubert
11525796c8dcSSimon Schubert /* Save PASS in case we are called again. */
11535796c8dcSSimon Schubert abfd->archive_pass = pass;
11545796c8dcSSimon Schubert
11555796c8dcSSimon Schubert return TRUE;
11565796c8dcSSimon Schubert
11575796c8dcSSimon Schubert error_return:
11585796c8dcSSimon Schubert archive_hash_table_free (&arsym_hash);
11595796c8dcSSimon Schubert return FALSE;
11605796c8dcSSimon Schubert }
11615796c8dcSSimon Schubert
11625796c8dcSSimon Schubert /* See if we should include an archive element. This version is used
11635796c8dcSSimon Schubert when we do not want to automatically collect constructors based on
11645796c8dcSSimon Schubert the symbol name, presumably because we have some other mechanism
11655796c8dcSSimon Schubert for finding them. */
11665796c8dcSSimon Schubert
11675796c8dcSSimon Schubert static bfd_boolean
generic_link_check_archive_element_no_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)11685796c8dcSSimon Schubert generic_link_check_archive_element_no_collect (
11695796c8dcSSimon Schubert bfd *abfd,
11705796c8dcSSimon Schubert struct bfd_link_info *info,
11715796c8dcSSimon Schubert bfd_boolean *pneeded)
11725796c8dcSSimon Schubert {
11735796c8dcSSimon Schubert return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
11745796c8dcSSimon Schubert }
11755796c8dcSSimon Schubert
11765796c8dcSSimon Schubert /* See if we should include an archive element. This version is used
11775796c8dcSSimon Schubert when we want to automatically collect constructors based on the
11785796c8dcSSimon Schubert symbol name, as collect2 does. */
11795796c8dcSSimon Schubert
11805796c8dcSSimon Schubert static bfd_boolean
generic_link_check_archive_element_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)11815796c8dcSSimon Schubert generic_link_check_archive_element_collect (bfd *abfd,
11825796c8dcSSimon Schubert struct bfd_link_info *info,
11835796c8dcSSimon Schubert bfd_boolean *pneeded)
11845796c8dcSSimon Schubert {
11855796c8dcSSimon Schubert return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
11865796c8dcSSimon Schubert }
11875796c8dcSSimon Schubert
11885796c8dcSSimon Schubert /* See if we should include an archive element. Optionally collect
11895796c8dcSSimon Schubert constructors. */
11905796c8dcSSimon Schubert
11915796c8dcSSimon Schubert static bfd_boolean
generic_link_check_archive_element(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded,bfd_boolean collect)11925796c8dcSSimon Schubert generic_link_check_archive_element (bfd *abfd,
11935796c8dcSSimon Schubert struct bfd_link_info *info,
11945796c8dcSSimon Schubert bfd_boolean *pneeded,
11955796c8dcSSimon Schubert bfd_boolean collect)
11965796c8dcSSimon Schubert {
11975796c8dcSSimon Schubert asymbol **pp, **ppend;
11985796c8dcSSimon Schubert
11995796c8dcSSimon Schubert *pneeded = FALSE;
12005796c8dcSSimon Schubert
12015796c8dcSSimon Schubert if (!bfd_generic_link_read_symbols (abfd))
12025796c8dcSSimon Schubert return FALSE;
12035796c8dcSSimon Schubert
12045796c8dcSSimon Schubert pp = _bfd_generic_link_get_symbols (abfd);
12055796c8dcSSimon Schubert ppend = pp + _bfd_generic_link_get_symcount (abfd);
12065796c8dcSSimon Schubert for (; pp < ppend; pp++)
12075796c8dcSSimon Schubert {
12085796c8dcSSimon Schubert asymbol *p;
12095796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
12105796c8dcSSimon Schubert
12115796c8dcSSimon Schubert p = *pp;
12125796c8dcSSimon Schubert
12135796c8dcSSimon Schubert /* We are only interested in globally visible symbols. */
12145796c8dcSSimon Schubert if (! bfd_is_com_section (p->section)
12155796c8dcSSimon Schubert && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
12165796c8dcSSimon Schubert continue;
12175796c8dcSSimon Schubert
12185796c8dcSSimon Schubert /* We are only interested if we know something about this
12195796c8dcSSimon Schubert symbol, and it is undefined or common. An undefined weak
12205796c8dcSSimon Schubert symbol (type bfd_link_hash_undefweak) is not considered to be
12215796c8dcSSimon Schubert a reference when pulling files out of an archive. See the
12225796c8dcSSimon Schubert SVR4 ABI, p. 4-27. */
12235796c8dcSSimon Schubert h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
12245796c8dcSSimon Schubert FALSE, TRUE);
12255796c8dcSSimon Schubert if (h == NULL
12265796c8dcSSimon Schubert || (h->type != bfd_link_hash_undefined
12275796c8dcSSimon Schubert && h->type != bfd_link_hash_common))
12285796c8dcSSimon Schubert continue;
12295796c8dcSSimon Schubert
12305796c8dcSSimon Schubert /* P is a symbol we are looking for. */
12315796c8dcSSimon Schubert
12325796c8dcSSimon Schubert if (! bfd_is_com_section (p->section))
12335796c8dcSSimon Schubert {
12345796c8dcSSimon Schubert bfd_size_type symcount;
12355796c8dcSSimon Schubert asymbol **symbols;
1236c50c785cSJohn Marino bfd *oldbfd = abfd;
12375796c8dcSSimon Schubert
12385796c8dcSSimon Schubert /* This object file defines this symbol, so pull it in. */
1239c50c785cSJohn Marino if (!(*info->callbacks
1240c50c785cSJohn Marino ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1241c50c785cSJohn Marino &abfd))
1242c50c785cSJohn Marino return FALSE;
1243c50c785cSJohn Marino /* Potentially, the add_archive_element hook may have set a
1244c50c785cSJohn Marino substitute BFD for us. */
1245c50c785cSJohn Marino if (abfd != oldbfd
1246c50c785cSJohn Marino && !bfd_generic_link_read_symbols (abfd))
12475796c8dcSSimon Schubert return FALSE;
12485796c8dcSSimon Schubert symcount = _bfd_generic_link_get_symcount (abfd);
12495796c8dcSSimon Schubert symbols = _bfd_generic_link_get_symbols (abfd);
12505796c8dcSSimon Schubert if (! generic_link_add_symbol_list (abfd, info, symcount,
12515796c8dcSSimon Schubert symbols, collect))
12525796c8dcSSimon Schubert return FALSE;
12535796c8dcSSimon Schubert *pneeded = TRUE;
12545796c8dcSSimon Schubert return TRUE;
12555796c8dcSSimon Schubert }
12565796c8dcSSimon Schubert
12575796c8dcSSimon Schubert /* P is a common symbol. */
12585796c8dcSSimon Schubert
12595796c8dcSSimon Schubert if (h->type == bfd_link_hash_undefined)
12605796c8dcSSimon Schubert {
12615796c8dcSSimon Schubert bfd *symbfd;
12625796c8dcSSimon Schubert bfd_vma size;
12635796c8dcSSimon Schubert unsigned int power;
12645796c8dcSSimon Schubert
12655796c8dcSSimon Schubert symbfd = h->u.undef.abfd;
12665796c8dcSSimon Schubert if (symbfd == NULL)
12675796c8dcSSimon Schubert {
12685796c8dcSSimon Schubert /* This symbol was created as undefined from outside
12695796c8dcSSimon Schubert BFD. We assume that we should link in the object
12705796c8dcSSimon Schubert file. This is for the -u option in the linker. */
1271c50c785cSJohn Marino if (!(*info->callbacks
1272c50c785cSJohn Marino ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1273c50c785cSJohn Marino &abfd))
12745796c8dcSSimon Schubert return FALSE;
1275c50c785cSJohn Marino /* Potentially, the add_archive_element hook may have set a
1276c50c785cSJohn Marino substitute BFD for us. But no symbols are going to get
1277c50c785cSJohn Marino registered by anything we're returning to from here. */
12785796c8dcSSimon Schubert *pneeded = TRUE;
12795796c8dcSSimon Schubert return TRUE;
12805796c8dcSSimon Schubert }
12815796c8dcSSimon Schubert
12825796c8dcSSimon Schubert /* Turn the symbol into a common symbol but do not link in
12835796c8dcSSimon Schubert the object file. This is how a.out works. Object
12845796c8dcSSimon Schubert formats that require different semantics must implement
12855796c8dcSSimon Schubert this function differently. This symbol is already on the
12865796c8dcSSimon Schubert undefs list. We add the section to a common section
12875796c8dcSSimon Schubert attached to symbfd to ensure that it is in a BFD which
12885796c8dcSSimon Schubert will be linked in. */
12895796c8dcSSimon Schubert h->type = bfd_link_hash_common;
12905796c8dcSSimon Schubert h->u.c.p = (struct bfd_link_hash_common_entry *)
12915796c8dcSSimon Schubert bfd_hash_allocate (&info->hash->table,
12925796c8dcSSimon Schubert sizeof (struct bfd_link_hash_common_entry));
12935796c8dcSSimon Schubert if (h->u.c.p == NULL)
12945796c8dcSSimon Schubert return FALSE;
12955796c8dcSSimon Schubert
12965796c8dcSSimon Schubert size = bfd_asymbol_value (p);
12975796c8dcSSimon Schubert h->u.c.size = size;
12985796c8dcSSimon Schubert
12995796c8dcSSimon Schubert power = bfd_log2 (size);
13005796c8dcSSimon Schubert if (power > 4)
13015796c8dcSSimon Schubert power = 4;
13025796c8dcSSimon Schubert h->u.c.p->alignment_power = power;
13035796c8dcSSimon Schubert
13045796c8dcSSimon Schubert if (p->section == bfd_com_section_ptr)
13055796c8dcSSimon Schubert h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
13065796c8dcSSimon Schubert else
13075796c8dcSSimon Schubert h->u.c.p->section = bfd_make_section_old_way (symbfd,
13085796c8dcSSimon Schubert p->section->name);
1309a45ae5f8SJohn Marino h->u.c.p->section->flags |= SEC_ALLOC;
13105796c8dcSSimon Schubert }
13115796c8dcSSimon Schubert else
13125796c8dcSSimon Schubert {
13135796c8dcSSimon Schubert /* Adjust the size of the common symbol if necessary. This
13145796c8dcSSimon Schubert is how a.out works. Object formats that require
13155796c8dcSSimon Schubert different semantics must implement this function
13165796c8dcSSimon Schubert differently. */
13175796c8dcSSimon Schubert if (bfd_asymbol_value (p) > h->u.c.size)
13185796c8dcSSimon Schubert h->u.c.size = bfd_asymbol_value (p);
13195796c8dcSSimon Schubert }
13205796c8dcSSimon Schubert }
13215796c8dcSSimon Schubert
13225796c8dcSSimon Schubert /* This archive element is not needed. */
13235796c8dcSSimon Schubert return TRUE;
13245796c8dcSSimon Schubert }
13255796c8dcSSimon Schubert
13265796c8dcSSimon Schubert /* Add the symbols from an object file to the global hash table. ABFD
13275796c8dcSSimon Schubert is the object file. INFO is the linker information. SYMBOL_COUNT
13285796c8dcSSimon Schubert is the number of symbols. SYMBOLS is the list of symbols. COLLECT
13295796c8dcSSimon Schubert is TRUE if constructors should be automatically collected by name
13305796c8dcSSimon Schubert as is done by collect2. */
13315796c8dcSSimon Schubert
13325796c8dcSSimon Schubert static bfd_boolean
generic_link_add_symbol_list(bfd * abfd,struct bfd_link_info * info,bfd_size_type symbol_count,asymbol ** symbols,bfd_boolean collect)13335796c8dcSSimon Schubert generic_link_add_symbol_list (bfd *abfd,
13345796c8dcSSimon Schubert struct bfd_link_info *info,
13355796c8dcSSimon Schubert bfd_size_type symbol_count,
13365796c8dcSSimon Schubert asymbol **symbols,
13375796c8dcSSimon Schubert bfd_boolean collect)
13385796c8dcSSimon Schubert {
13395796c8dcSSimon Schubert asymbol **pp, **ppend;
13405796c8dcSSimon Schubert
13415796c8dcSSimon Schubert pp = symbols;
13425796c8dcSSimon Schubert ppend = symbols + symbol_count;
13435796c8dcSSimon Schubert for (; pp < ppend; pp++)
13445796c8dcSSimon Schubert {
13455796c8dcSSimon Schubert asymbol *p;
13465796c8dcSSimon Schubert
13475796c8dcSSimon Schubert p = *pp;
13485796c8dcSSimon Schubert
13495796c8dcSSimon Schubert if ((p->flags & (BSF_INDIRECT
13505796c8dcSSimon Schubert | BSF_WARNING
13515796c8dcSSimon Schubert | BSF_GLOBAL
13525796c8dcSSimon Schubert | BSF_CONSTRUCTOR
13535796c8dcSSimon Schubert | BSF_WEAK)) != 0
13545796c8dcSSimon Schubert || bfd_is_und_section (bfd_get_section (p))
13555796c8dcSSimon Schubert || bfd_is_com_section (bfd_get_section (p))
13565796c8dcSSimon Schubert || bfd_is_ind_section (bfd_get_section (p)))
13575796c8dcSSimon Schubert {
13585796c8dcSSimon Schubert const char *name;
13595796c8dcSSimon Schubert const char *string;
13605796c8dcSSimon Schubert struct generic_link_hash_entry *h;
13615796c8dcSSimon Schubert struct bfd_link_hash_entry *bh;
13625796c8dcSSimon Schubert
13635796c8dcSSimon Schubert string = name = bfd_asymbol_name (p);
13645796c8dcSSimon Schubert if (((p->flags & BSF_INDIRECT) != 0
13655796c8dcSSimon Schubert || bfd_is_ind_section (p->section))
13665796c8dcSSimon Schubert && pp + 1 < ppend)
13675796c8dcSSimon Schubert {
13685796c8dcSSimon Schubert pp++;
13695796c8dcSSimon Schubert string = bfd_asymbol_name (*pp);
13705796c8dcSSimon Schubert }
13715796c8dcSSimon Schubert else if ((p->flags & BSF_WARNING) != 0
13725796c8dcSSimon Schubert && pp + 1 < ppend)
13735796c8dcSSimon Schubert {
13745796c8dcSSimon Schubert /* The name of P is actually the warning string, and the
13755796c8dcSSimon Schubert next symbol is the one to warn about. */
13765796c8dcSSimon Schubert pp++;
13775796c8dcSSimon Schubert name = bfd_asymbol_name (*pp);
13785796c8dcSSimon Schubert }
13795796c8dcSSimon Schubert
13805796c8dcSSimon Schubert bh = NULL;
13815796c8dcSSimon Schubert if (! (_bfd_generic_link_add_one_symbol
13825796c8dcSSimon Schubert (info, abfd, name, p->flags, bfd_get_section (p),
13835796c8dcSSimon Schubert p->value, string, FALSE, collect, &bh)))
13845796c8dcSSimon Schubert return FALSE;
13855796c8dcSSimon Schubert h = (struct generic_link_hash_entry *) bh;
13865796c8dcSSimon Schubert
13875796c8dcSSimon Schubert /* If this is a constructor symbol, and the linker didn't do
13885796c8dcSSimon Schubert anything with it, then we want to just pass the symbol
13895796c8dcSSimon Schubert through to the output file. This will happen when
13905796c8dcSSimon Schubert linking with -r. */
13915796c8dcSSimon Schubert if ((p->flags & BSF_CONSTRUCTOR) != 0
13925796c8dcSSimon Schubert && (h == NULL || h->root.type == bfd_link_hash_new))
13935796c8dcSSimon Schubert {
13945796c8dcSSimon Schubert p->udata.p = NULL;
13955796c8dcSSimon Schubert continue;
13965796c8dcSSimon Schubert }
13975796c8dcSSimon Schubert
13985796c8dcSSimon Schubert /* Save the BFD symbol so that we don't lose any backend
13995796c8dcSSimon Schubert specific information that may be attached to it. We only
14005796c8dcSSimon Schubert want this one if it gives more information than the
14015796c8dcSSimon Schubert existing one; we don't want to replace a defined symbol
14025796c8dcSSimon Schubert with an undefined one. This routine may be called with a
14035796c8dcSSimon Schubert hash table other than the generic hash table, so we only
14045796c8dcSSimon Schubert do this if we are certain that the hash table is a
14055796c8dcSSimon Schubert generic one. */
14065796c8dcSSimon Schubert if (info->output_bfd->xvec == abfd->xvec)
14075796c8dcSSimon Schubert {
14085796c8dcSSimon Schubert if (h->sym == NULL
14095796c8dcSSimon Schubert || (! bfd_is_und_section (bfd_get_section (p))
14105796c8dcSSimon Schubert && (! bfd_is_com_section (bfd_get_section (p))
14115796c8dcSSimon Schubert || bfd_is_und_section (bfd_get_section (h->sym)))))
14125796c8dcSSimon Schubert {
14135796c8dcSSimon Schubert h->sym = p;
14145796c8dcSSimon Schubert /* BSF_OLD_COMMON is a hack to support COFF reloc
14155796c8dcSSimon Schubert reading, and it should go away when the COFF
14165796c8dcSSimon Schubert linker is switched to the new version. */
14175796c8dcSSimon Schubert if (bfd_is_com_section (bfd_get_section (p)))
14185796c8dcSSimon Schubert p->flags |= BSF_OLD_COMMON;
14195796c8dcSSimon Schubert }
14205796c8dcSSimon Schubert }
14215796c8dcSSimon Schubert
14225796c8dcSSimon Schubert /* Store a back pointer from the symbol to the hash
14235796c8dcSSimon Schubert table entry for the benefit of relaxation code until
14245796c8dcSSimon Schubert it gets rewritten to not use asymbol structures.
14255796c8dcSSimon Schubert Setting this is also used to check whether these
14265796c8dcSSimon Schubert symbols were set up by the generic linker. */
14275796c8dcSSimon Schubert p->udata.p = h;
14285796c8dcSSimon Schubert }
14295796c8dcSSimon Schubert }
14305796c8dcSSimon Schubert
14315796c8dcSSimon Schubert return TRUE;
14325796c8dcSSimon Schubert }
14335796c8dcSSimon Schubert
14345796c8dcSSimon Schubert /* We use a state table to deal with adding symbols from an object
14355796c8dcSSimon Schubert file. The first index into the state table describes the symbol
14365796c8dcSSimon Schubert from the object file. The second index into the state table is the
14375796c8dcSSimon Schubert type of the symbol in the hash table. */
14385796c8dcSSimon Schubert
14395796c8dcSSimon Schubert /* The symbol from the object file is turned into one of these row
14405796c8dcSSimon Schubert values. */
14415796c8dcSSimon Schubert
14425796c8dcSSimon Schubert enum link_row
14435796c8dcSSimon Schubert {
14445796c8dcSSimon Schubert UNDEF_ROW, /* Undefined. */
14455796c8dcSSimon Schubert UNDEFW_ROW, /* Weak undefined. */
14465796c8dcSSimon Schubert DEF_ROW, /* Defined. */
14475796c8dcSSimon Schubert DEFW_ROW, /* Weak defined. */
14485796c8dcSSimon Schubert COMMON_ROW, /* Common. */
14495796c8dcSSimon Schubert INDR_ROW, /* Indirect. */
14505796c8dcSSimon Schubert WARN_ROW, /* Warning. */
14515796c8dcSSimon Schubert SET_ROW /* Member of set. */
14525796c8dcSSimon Schubert };
14535796c8dcSSimon Schubert
14545796c8dcSSimon Schubert /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
14555796c8dcSSimon Schubert #undef FAIL
14565796c8dcSSimon Schubert
14575796c8dcSSimon Schubert /* The actions to take in the state table. */
14585796c8dcSSimon Schubert
14595796c8dcSSimon Schubert enum link_action
14605796c8dcSSimon Schubert {
14615796c8dcSSimon Schubert FAIL, /* Abort. */
14625796c8dcSSimon Schubert UND, /* Mark symbol undefined. */
14635796c8dcSSimon Schubert WEAK, /* Mark symbol weak undefined. */
14645796c8dcSSimon Schubert DEF, /* Mark symbol defined. */
14655796c8dcSSimon Schubert DEFW, /* Mark symbol weak defined. */
14665796c8dcSSimon Schubert COM, /* Mark symbol common. */
14675796c8dcSSimon Schubert REF, /* Mark defined symbol referenced. */
14685796c8dcSSimon Schubert CREF, /* Possibly warn about common reference to defined symbol. */
14695796c8dcSSimon Schubert CDEF, /* Define existing common symbol. */
14705796c8dcSSimon Schubert NOACT, /* No action. */
14715796c8dcSSimon Schubert BIG, /* Mark symbol common using largest size. */
14725796c8dcSSimon Schubert MDEF, /* Multiple definition error. */
14735796c8dcSSimon Schubert MIND, /* Multiple indirect symbols. */
14745796c8dcSSimon Schubert IND, /* Make indirect symbol. */
14755796c8dcSSimon Schubert CIND, /* Make indirect symbol from existing common symbol. */
14765796c8dcSSimon Schubert SET, /* Add value to set. */
14775796c8dcSSimon Schubert MWARN, /* Make warning symbol. */
14785796c8dcSSimon Schubert WARN, /* Issue warning. */
14795796c8dcSSimon Schubert CWARN, /* Warn if referenced, else MWARN. */
14805796c8dcSSimon Schubert CYCLE, /* Repeat with symbol pointed to. */
14815796c8dcSSimon Schubert REFC, /* Mark indirect symbol referenced and then CYCLE. */
14825796c8dcSSimon Schubert WARNC /* Issue warning and then CYCLE. */
14835796c8dcSSimon Schubert };
14845796c8dcSSimon Schubert
14855796c8dcSSimon Schubert /* The state table itself. The first index is a link_row and the
14865796c8dcSSimon Schubert second index is a bfd_link_hash_type. */
14875796c8dcSSimon Schubert
14885796c8dcSSimon Schubert static const enum link_action link_action[8][8] =
14895796c8dcSSimon Schubert {
14905796c8dcSSimon Schubert /* current\prev new undef undefw def defw com indr warn */
14915796c8dcSSimon Schubert /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
14925796c8dcSSimon Schubert /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
14935796c8dcSSimon Schubert /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MDEF, CYCLE },
14945796c8dcSSimon Schubert /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
14955796c8dcSSimon Schubert /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
14965796c8dcSSimon Schubert /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
14975796c8dcSSimon Schubert /* WARN_ROW */ {MWARN, WARN, WARN, CWARN, CWARN, WARN, CWARN, NOACT },
14985796c8dcSSimon Schubert /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
14995796c8dcSSimon Schubert };
15005796c8dcSSimon Schubert
15015796c8dcSSimon Schubert /* Most of the entries in the LINK_ACTION table are straightforward,
15025796c8dcSSimon Schubert but a few are somewhat subtle.
15035796c8dcSSimon Schubert
15045796c8dcSSimon Schubert A reference to an indirect symbol (UNDEF_ROW/indr or
15055796c8dcSSimon Schubert UNDEFW_ROW/indr) is counted as a reference both to the indirect
15065796c8dcSSimon Schubert symbol and to the symbol the indirect symbol points to.
15075796c8dcSSimon Schubert
15085796c8dcSSimon Schubert A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
15095796c8dcSSimon Schubert causes the warning to be issued.
15105796c8dcSSimon Schubert
15115796c8dcSSimon Schubert A common definition of an indirect symbol (COMMON_ROW/indr) is
15125796c8dcSSimon Schubert treated as a multiple definition error. Likewise for an indirect
15135796c8dcSSimon Schubert definition of a common symbol (INDR_ROW/com).
15145796c8dcSSimon Schubert
15155796c8dcSSimon Schubert An indirect definition of a warning (INDR_ROW/warn) does not cause
15165796c8dcSSimon Schubert the warning to be issued.
15175796c8dcSSimon Schubert
15185796c8dcSSimon Schubert If a warning is created for an indirect symbol (WARN_ROW/indr) no
15195796c8dcSSimon Schubert warning is created for the symbol the indirect symbol points to.
15205796c8dcSSimon Schubert
15215796c8dcSSimon Schubert Adding an entry to a set does not count as a reference to a set,
15225796c8dcSSimon Schubert and no warning is issued (SET_ROW/warn). */
15235796c8dcSSimon Schubert
15245796c8dcSSimon Schubert /* Return the BFD in which a hash entry has been defined, if known. */
15255796c8dcSSimon Schubert
15265796c8dcSSimon Schubert static bfd *
hash_entry_bfd(struct bfd_link_hash_entry * h)15275796c8dcSSimon Schubert hash_entry_bfd (struct bfd_link_hash_entry *h)
15285796c8dcSSimon Schubert {
15295796c8dcSSimon Schubert while (h->type == bfd_link_hash_warning)
15305796c8dcSSimon Schubert h = h->u.i.link;
15315796c8dcSSimon Schubert switch (h->type)
15325796c8dcSSimon Schubert {
15335796c8dcSSimon Schubert default:
15345796c8dcSSimon Schubert return NULL;
15355796c8dcSSimon Schubert case bfd_link_hash_undefined:
15365796c8dcSSimon Schubert case bfd_link_hash_undefweak:
15375796c8dcSSimon Schubert return h->u.undef.abfd;
15385796c8dcSSimon Schubert case bfd_link_hash_defined:
15395796c8dcSSimon Schubert case bfd_link_hash_defweak:
15405796c8dcSSimon Schubert return h->u.def.section->owner;
15415796c8dcSSimon Schubert case bfd_link_hash_common:
15425796c8dcSSimon Schubert return h->u.c.p->section->owner;
15435796c8dcSSimon Schubert }
15445796c8dcSSimon Schubert /*NOTREACHED*/
15455796c8dcSSimon Schubert }
15465796c8dcSSimon Schubert
15475796c8dcSSimon Schubert /* Add a symbol to the global hash table.
15485796c8dcSSimon Schubert ABFD is the BFD the symbol comes from.
15495796c8dcSSimon Schubert NAME is the name of the symbol.
15505796c8dcSSimon Schubert FLAGS is the BSF_* bits associated with the symbol.
15515796c8dcSSimon Schubert SECTION is the section in which the symbol is defined; this may be
15525796c8dcSSimon Schubert bfd_und_section_ptr or bfd_com_section_ptr.
15535796c8dcSSimon Schubert VALUE is the value of the symbol, relative to the section.
15545796c8dcSSimon Schubert STRING is used for either an indirect symbol, in which case it is
15555796c8dcSSimon Schubert the name of the symbol to indirect to, or a warning symbol, in
15565796c8dcSSimon Schubert which case it is the warning string.
15575796c8dcSSimon Schubert COPY is TRUE if NAME or STRING must be copied into locally
15585796c8dcSSimon Schubert allocated memory if they need to be saved.
15595796c8dcSSimon Schubert COLLECT is TRUE if we should automatically collect gcc constructor
15605796c8dcSSimon Schubert or destructor names as collect2 does.
15615796c8dcSSimon Schubert HASHP, if not NULL, is a place to store the created hash table
15625796c8dcSSimon Schubert entry; if *HASHP is not NULL, the caller has already looked up
15635796c8dcSSimon Schubert the hash table entry, and stored it in *HASHP. */
15645796c8dcSSimon Schubert
15655796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_add_one_symbol(struct bfd_link_info * info,bfd * abfd,const char * name,flagword flags,asection * section,bfd_vma value,const char * string,bfd_boolean copy,bfd_boolean collect,struct bfd_link_hash_entry ** hashp)15665796c8dcSSimon Schubert _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
15675796c8dcSSimon Schubert bfd *abfd,
15685796c8dcSSimon Schubert const char *name,
15695796c8dcSSimon Schubert flagword flags,
15705796c8dcSSimon Schubert asection *section,
15715796c8dcSSimon Schubert bfd_vma value,
15725796c8dcSSimon Schubert const char *string,
15735796c8dcSSimon Schubert bfd_boolean copy,
15745796c8dcSSimon Schubert bfd_boolean collect,
15755796c8dcSSimon Schubert struct bfd_link_hash_entry **hashp)
15765796c8dcSSimon Schubert {
15775796c8dcSSimon Schubert enum link_row row;
15785796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
15795796c8dcSSimon Schubert bfd_boolean cycle;
15805796c8dcSSimon Schubert
1581a45ae5f8SJohn Marino BFD_ASSERT (section != NULL);
1582a45ae5f8SJohn Marino
15835796c8dcSSimon Schubert if (bfd_is_ind_section (section)
15845796c8dcSSimon Schubert || (flags & BSF_INDIRECT) != 0)
15855796c8dcSSimon Schubert row = INDR_ROW;
15865796c8dcSSimon Schubert else if ((flags & BSF_WARNING) != 0)
15875796c8dcSSimon Schubert row = WARN_ROW;
15885796c8dcSSimon Schubert else if ((flags & BSF_CONSTRUCTOR) != 0)
15895796c8dcSSimon Schubert row = SET_ROW;
15905796c8dcSSimon Schubert else if (bfd_is_und_section (section))
15915796c8dcSSimon Schubert {
15925796c8dcSSimon Schubert if ((flags & BSF_WEAK) != 0)
15935796c8dcSSimon Schubert row = UNDEFW_ROW;
15945796c8dcSSimon Schubert else
15955796c8dcSSimon Schubert row = UNDEF_ROW;
15965796c8dcSSimon Schubert }
15975796c8dcSSimon Schubert else if ((flags & BSF_WEAK) != 0)
15985796c8dcSSimon Schubert row = DEFW_ROW;
15995796c8dcSSimon Schubert else if (bfd_is_com_section (section))
16005796c8dcSSimon Schubert row = COMMON_ROW;
16015796c8dcSSimon Schubert else
16025796c8dcSSimon Schubert row = DEF_ROW;
16035796c8dcSSimon Schubert
16045796c8dcSSimon Schubert if (hashp != NULL && *hashp != NULL)
16055796c8dcSSimon Schubert h = *hashp;
16065796c8dcSSimon Schubert else
16075796c8dcSSimon Schubert {
16085796c8dcSSimon Schubert if (row == UNDEF_ROW || row == UNDEFW_ROW)
16095796c8dcSSimon Schubert h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
16105796c8dcSSimon Schubert else
16115796c8dcSSimon Schubert h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
16125796c8dcSSimon Schubert if (h == NULL)
16135796c8dcSSimon Schubert {
16145796c8dcSSimon Schubert if (hashp != NULL)
16155796c8dcSSimon Schubert *hashp = NULL;
16165796c8dcSSimon Schubert return FALSE;
16175796c8dcSSimon Schubert }
16185796c8dcSSimon Schubert }
16195796c8dcSSimon Schubert
16205796c8dcSSimon Schubert if (info->notice_all
16215796c8dcSSimon Schubert || (info->notice_hash != NULL
16225796c8dcSSimon Schubert && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
16235796c8dcSSimon Schubert {
1624a45ae5f8SJohn Marino if (! (*info->callbacks->notice) (info, h,
1625a45ae5f8SJohn Marino abfd, section, value, flags, string))
16265796c8dcSSimon Schubert return FALSE;
16275796c8dcSSimon Schubert }
16285796c8dcSSimon Schubert
16295796c8dcSSimon Schubert if (hashp != NULL)
16305796c8dcSSimon Schubert *hashp = h;
16315796c8dcSSimon Schubert
16325796c8dcSSimon Schubert do
16335796c8dcSSimon Schubert {
16345796c8dcSSimon Schubert enum link_action action;
16355796c8dcSSimon Schubert
16365796c8dcSSimon Schubert cycle = FALSE;
16375796c8dcSSimon Schubert action = link_action[(int) row][(int) h->type];
16385796c8dcSSimon Schubert switch (action)
16395796c8dcSSimon Schubert {
16405796c8dcSSimon Schubert case FAIL:
16415796c8dcSSimon Schubert abort ();
16425796c8dcSSimon Schubert
16435796c8dcSSimon Schubert case NOACT:
16445796c8dcSSimon Schubert /* Do nothing. */
16455796c8dcSSimon Schubert break;
16465796c8dcSSimon Schubert
16475796c8dcSSimon Schubert case UND:
16485796c8dcSSimon Schubert /* Make a new undefined symbol. */
16495796c8dcSSimon Schubert h->type = bfd_link_hash_undefined;
16505796c8dcSSimon Schubert h->u.undef.abfd = abfd;
16515796c8dcSSimon Schubert bfd_link_add_undef (info->hash, h);
16525796c8dcSSimon Schubert break;
16535796c8dcSSimon Schubert
16545796c8dcSSimon Schubert case WEAK:
16555796c8dcSSimon Schubert /* Make a new weak undefined symbol. */
16565796c8dcSSimon Schubert h->type = bfd_link_hash_undefweak;
16575796c8dcSSimon Schubert h->u.undef.abfd = abfd;
16585796c8dcSSimon Schubert break;
16595796c8dcSSimon Schubert
16605796c8dcSSimon Schubert case CDEF:
16615796c8dcSSimon Schubert /* We have found a definition for a symbol which was
16625796c8dcSSimon Schubert previously common. */
16635796c8dcSSimon Schubert BFD_ASSERT (h->type == bfd_link_hash_common);
16645796c8dcSSimon Schubert if (! ((*info->callbacks->multiple_common)
1665a45ae5f8SJohn Marino (info, h, abfd, bfd_link_hash_defined, 0)))
16665796c8dcSSimon Schubert return FALSE;
16675796c8dcSSimon Schubert /* Fall through. */
16685796c8dcSSimon Schubert case DEF:
16695796c8dcSSimon Schubert case DEFW:
16705796c8dcSSimon Schubert {
16715796c8dcSSimon Schubert enum bfd_link_hash_type oldtype;
16725796c8dcSSimon Schubert
16735796c8dcSSimon Schubert /* Define a symbol. */
16745796c8dcSSimon Schubert oldtype = h->type;
16755796c8dcSSimon Schubert if (action == DEFW)
16765796c8dcSSimon Schubert h->type = bfd_link_hash_defweak;
16775796c8dcSSimon Schubert else
16785796c8dcSSimon Schubert h->type = bfd_link_hash_defined;
16795796c8dcSSimon Schubert h->u.def.section = section;
16805796c8dcSSimon Schubert h->u.def.value = value;
16815796c8dcSSimon Schubert
16825796c8dcSSimon Schubert /* If we have been asked to, we act like collect2 and
16835796c8dcSSimon Schubert identify all functions that might be global
16845796c8dcSSimon Schubert constructors and destructors and pass them up in a
16855796c8dcSSimon Schubert callback. We only do this for certain object file
16865796c8dcSSimon Schubert types, since many object file types can handle this
16875796c8dcSSimon Schubert automatically. */
16885796c8dcSSimon Schubert if (collect && name[0] == '_')
16895796c8dcSSimon Schubert {
16905796c8dcSSimon Schubert const char *s;
16915796c8dcSSimon Schubert
16925796c8dcSSimon Schubert /* A constructor or destructor name starts like this:
16935796c8dcSSimon Schubert _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
16945796c8dcSSimon Schubert the second are the same character (we accept any
16955796c8dcSSimon Schubert character there, in case a new object file format
16965796c8dcSSimon Schubert comes along with even worse naming restrictions). */
16975796c8dcSSimon Schubert
16985796c8dcSSimon Schubert #define CONS_PREFIX "GLOBAL_"
16995796c8dcSSimon Schubert #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
17005796c8dcSSimon Schubert
17015796c8dcSSimon Schubert s = name + 1;
17025796c8dcSSimon Schubert while (*s == '_')
17035796c8dcSSimon Schubert ++s;
17045796c8dcSSimon Schubert if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
17055796c8dcSSimon Schubert {
17065796c8dcSSimon Schubert char c;
17075796c8dcSSimon Schubert
17085796c8dcSSimon Schubert c = s[CONS_PREFIX_LEN + 1];
17095796c8dcSSimon Schubert if ((c == 'I' || c == 'D')
17105796c8dcSSimon Schubert && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
17115796c8dcSSimon Schubert {
17125796c8dcSSimon Schubert /* If this is a definition of a symbol which
17135796c8dcSSimon Schubert was previously weakly defined, we are in
17145796c8dcSSimon Schubert trouble. We have already added a
17155796c8dcSSimon Schubert constructor entry for the weak defined
17165796c8dcSSimon Schubert symbol, and now we are trying to add one
17175796c8dcSSimon Schubert for the new symbol. Fortunately, this case
17185796c8dcSSimon Schubert should never arise in practice. */
17195796c8dcSSimon Schubert if (oldtype == bfd_link_hash_defweak)
17205796c8dcSSimon Schubert abort ();
17215796c8dcSSimon Schubert
17225796c8dcSSimon Schubert if (! ((*info->callbacks->constructor)
17235796c8dcSSimon Schubert (info, c == 'I',
17245796c8dcSSimon Schubert h->root.string, abfd, section, value)))
17255796c8dcSSimon Schubert return FALSE;
17265796c8dcSSimon Schubert }
17275796c8dcSSimon Schubert }
17285796c8dcSSimon Schubert }
17295796c8dcSSimon Schubert }
17305796c8dcSSimon Schubert
17315796c8dcSSimon Schubert break;
17325796c8dcSSimon Schubert
17335796c8dcSSimon Schubert case COM:
17345796c8dcSSimon Schubert /* We have found a common definition for a symbol. */
17355796c8dcSSimon Schubert if (h->type == bfd_link_hash_new)
17365796c8dcSSimon Schubert bfd_link_add_undef (info->hash, h);
17375796c8dcSSimon Schubert h->type = bfd_link_hash_common;
17385796c8dcSSimon Schubert h->u.c.p = (struct bfd_link_hash_common_entry *)
17395796c8dcSSimon Schubert bfd_hash_allocate (&info->hash->table,
17405796c8dcSSimon Schubert sizeof (struct bfd_link_hash_common_entry));
17415796c8dcSSimon Schubert if (h->u.c.p == NULL)
17425796c8dcSSimon Schubert return FALSE;
17435796c8dcSSimon Schubert
17445796c8dcSSimon Schubert h->u.c.size = value;
17455796c8dcSSimon Schubert
17465796c8dcSSimon Schubert /* Select a default alignment based on the size. This may
17475796c8dcSSimon Schubert be overridden by the caller. */
17485796c8dcSSimon Schubert {
17495796c8dcSSimon Schubert unsigned int power;
17505796c8dcSSimon Schubert
17515796c8dcSSimon Schubert power = bfd_log2 (value);
17525796c8dcSSimon Schubert if (power > 4)
17535796c8dcSSimon Schubert power = 4;
17545796c8dcSSimon Schubert h->u.c.p->alignment_power = power;
17555796c8dcSSimon Schubert }
17565796c8dcSSimon Schubert
17575796c8dcSSimon Schubert /* The section of a common symbol is only used if the common
17585796c8dcSSimon Schubert symbol is actually allocated. It basically provides a
17595796c8dcSSimon Schubert hook for the linker script to decide which output section
17605796c8dcSSimon Schubert the common symbols should be put in. In most cases, the
17615796c8dcSSimon Schubert section of a common symbol will be bfd_com_section_ptr,
17625796c8dcSSimon Schubert the code here will choose a common symbol section named
17635796c8dcSSimon Schubert "COMMON", and the linker script will contain *(COMMON) in
17645796c8dcSSimon Schubert the appropriate place. A few targets use separate common
17655796c8dcSSimon Schubert sections for small symbols, and they require special
17665796c8dcSSimon Schubert handling. */
17675796c8dcSSimon Schubert if (section == bfd_com_section_ptr)
17685796c8dcSSimon Schubert {
17695796c8dcSSimon Schubert h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1770a45ae5f8SJohn Marino h->u.c.p->section->flags |= SEC_ALLOC;
17715796c8dcSSimon Schubert }
17725796c8dcSSimon Schubert else if (section->owner != abfd)
17735796c8dcSSimon Schubert {
17745796c8dcSSimon Schubert h->u.c.p->section = bfd_make_section_old_way (abfd,
17755796c8dcSSimon Schubert section->name);
1776a45ae5f8SJohn Marino h->u.c.p->section->flags |= SEC_ALLOC;
17775796c8dcSSimon Schubert }
17785796c8dcSSimon Schubert else
17795796c8dcSSimon Schubert h->u.c.p->section = section;
17805796c8dcSSimon Schubert break;
17815796c8dcSSimon Schubert
17825796c8dcSSimon Schubert case REF:
17835796c8dcSSimon Schubert /* A reference to a defined symbol. */
17845796c8dcSSimon Schubert if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
17855796c8dcSSimon Schubert h->u.undef.next = h;
17865796c8dcSSimon Schubert break;
17875796c8dcSSimon Schubert
17885796c8dcSSimon Schubert case BIG:
17895796c8dcSSimon Schubert /* We have found a common definition for a symbol which
17905796c8dcSSimon Schubert already had a common definition. Use the maximum of the
17915796c8dcSSimon Schubert two sizes, and use the section required by the larger symbol. */
17925796c8dcSSimon Schubert BFD_ASSERT (h->type == bfd_link_hash_common);
17935796c8dcSSimon Schubert if (! ((*info->callbacks->multiple_common)
1794a45ae5f8SJohn Marino (info, h, abfd, bfd_link_hash_common, value)))
17955796c8dcSSimon Schubert return FALSE;
17965796c8dcSSimon Schubert if (value > h->u.c.size)
17975796c8dcSSimon Schubert {
17985796c8dcSSimon Schubert unsigned int power;
17995796c8dcSSimon Schubert
18005796c8dcSSimon Schubert h->u.c.size = value;
18015796c8dcSSimon Schubert
18025796c8dcSSimon Schubert /* Select a default alignment based on the size. This may
18035796c8dcSSimon Schubert be overridden by the caller. */
18045796c8dcSSimon Schubert power = bfd_log2 (value);
18055796c8dcSSimon Schubert if (power > 4)
18065796c8dcSSimon Schubert power = 4;
18075796c8dcSSimon Schubert h->u.c.p->alignment_power = power;
18085796c8dcSSimon Schubert
18095796c8dcSSimon Schubert /* Some systems have special treatment for small commons,
18105796c8dcSSimon Schubert hence we want to select the section used by the larger
18115796c8dcSSimon Schubert symbol. This makes sure the symbol does not go in a
18125796c8dcSSimon Schubert small common section if it is now too large. */
18135796c8dcSSimon Schubert if (section == bfd_com_section_ptr)
18145796c8dcSSimon Schubert {
18155796c8dcSSimon Schubert h->u.c.p->section
18165796c8dcSSimon Schubert = bfd_make_section_old_way (abfd, "COMMON");
1817a45ae5f8SJohn Marino h->u.c.p->section->flags |= SEC_ALLOC;
18185796c8dcSSimon Schubert }
18195796c8dcSSimon Schubert else if (section->owner != abfd)
18205796c8dcSSimon Schubert {
18215796c8dcSSimon Schubert h->u.c.p->section
18225796c8dcSSimon Schubert = bfd_make_section_old_way (abfd, section->name);
1823a45ae5f8SJohn Marino h->u.c.p->section->flags |= SEC_ALLOC;
18245796c8dcSSimon Schubert }
18255796c8dcSSimon Schubert else
18265796c8dcSSimon Schubert h->u.c.p->section = section;
18275796c8dcSSimon Schubert }
18285796c8dcSSimon Schubert break;
18295796c8dcSSimon Schubert
18305796c8dcSSimon Schubert case CREF:
18315796c8dcSSimon Schubert /* We have found a common definition for a symbol which
1832a45ae5f8SJohn Marino was already defined. */
18335796c8dcSSimon Schubert if (! ((*info->callbacks->multiple_common)
1834a45ae5f8SJohn Marino (info, h, abfd, bfd_link_hash_common, value)))
18355796c8dcSSimon Schubert return FALSE;
18365796c8dcSSimon Schubert break;
18375796c8dcSSimon Schubert
18385796c8dcSSimon Schubert case MIND:
18395796c8dcSSimon Schubert /* Multiple indirect symbols. This is OK if they both point
18405796c8dcSSimon Schubert to the same symbol. */
18415796c8dcSSimon Schubert if (strcmp (h->u.i.link->root.string, string) == 0)
18425796c8dcSSimon Schubert break;
18435796c8dcSSimon Schubert /* Fall through. */
18445796c8dcSSimon Schubert case MDEF:
18455796c8dcSSimon Schubert /* Handle a multiple definition. */
18465796c8dcSSimon Schubert if (! ((*info->callbacks->multiple_definition)
1847a45ae5f8SJohn Marino (info, h, abfd, section, value)))
18485796c8dcSSimon Schubert return FALSE;
18495796c8dcSSimon Schubert break;
18505796c8dcSSimon Schubert
18515796c8dcSSimon Schubert case CIND:
18525796c8dcSSimon Schubert /* Create an indirect symbol from an existing common symbol. */
18535796c8dcSSimon Schubert BFD_ASSERT (h->type == bfd_link_hash_common);
18545796c8dcSSimon Schubert if (! ((*info->callbacks->multiple_common)
1855a45ae5f8SJohn Marino (info, h, abfd, bfd_link_hash_indirect, 0)))
18565796c8dcSSimon Schubert return FALSE;
18575796c8dcSSimon Schubert /* Fall through. */
18585796c8dcSSimon Schubert case IND:
18595796c8dcSSimon Schubert /* Create an indirect symbol. */
18605796c8dcSSimon Schubert {
18615796c8dcSSimon Schubert struct bfd_link_hash_entry *inh;
18625796c8dcSSimon Schubert
18635796c8dcSSimon Schubert /* STRING is the name of the symbol we want to indirect
18645796c8dcSSimon Schubert to. */
18655796c8dcSSimon Schubert inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
18665796c8dcSSimon Schubert copy, FALSE);
18675796c8dcSSimon Schubert if (inh == NULL)
18685796c8dcSSimon Schubert return FALSE;
18695796c8dcSSimon Schubert if (inh->type == bfd_link_hash_indirect
18705796c8dcSSimon Schubert && inh->u.i.link == h)
18715796c8dcSSimon Schubert {
18725796c8dcSSimon Schubert (*_bfd_error_handler)
18735796c8dcSSimon Schubert (_("%B: indirect symbol `%s' to `%s' is a loop"),
18745796c8dcSSimon Schubert abfd, name, string);
18755796c8dcSSimon Schubert bfd_set_error (bfd_error_invalid_operation);
18765796c8dcSSimon Schubert return FALSE;
18775796c8dcSSimon Schubert }
18785796c8dcSSimon Schubert if (inh->type == bfd_link_hash_new)
18795796c8dcSSimon Schubert {
18805796c8dcSSimon Schubert inh->type = bfd_link_hash_undefined;
18815796c8dcSSimon Schubert inh->u.undef.abfd = abfd;
18825796c8dcSSimon Schubert bfd_link_add_undef (info->hash, inh);
18835796c8dcSSimon Schubert }
18845796c8dcSSimon Schubert
18855796c8dcSSimon Schubert /* If the indirect symbol has been referenced, we need to
18865796c8dcSSimon Schubert push the reference down to the symbol we are
18875796c8dcSSimon Schubert referencing. */
18885796c8dcSSimon Schubert if (h->type != bfd_link_hash_new)
18895796c8dcSSimon Schubert {
18905796c8dcSSimon Schubert row = UNDEF_ROW;
18915796c8dcSSimon Schubert cycle = TRUE;
18925796c8dcSSimon Schubert }
18935796c8dcSSimon Schubert
18945796c8dcSSimon Schubert h->type = bfd_link_hash_indirect;
18955796c8dcSSimon Schubert h->u.i.link = inh;
18965796c8dcSSimon Schubert }
18975796c8dcSSimon Schubert break;
18985796c8dcSSimon Schubert
18995796c8dcSSimon Schubert case SET:
19005796c8dcSSimon Schubert /* Add an entry to a set. */
19015796c8dcSSimon Schubert if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
19025796c8dcSSimon Schubert abfd, section, value))
19035796c8dcSSimon Schubert return FALSE;
19045796c8dcSSimon Schubert break;
19055796c8dcSSimon Schubert
19065796c8dcSSimon Schubert case WARNC:
19075796c8dcSSimon Schubert /* Issue a warning and cycle. */
19085796c8dcSSimon Schubert if (h->u.i.warning != NULL)
19095796c8dcSSimon Schubert {
19105796c8dcSSimon Schubert if (! (*info->callbacks->warning) (info, h->u.i.warning,
19115796c8dcSSimon Schubert h->root.string, abfd,
19125796c8dcSSimon Schubert NULL, 0))
19135796c8dcSSimon Schubert return FALSE;
19145796c8dcSSimon Schubert /* Only issue a warning once. */
19155796c8dcSSimon Schubert h->u.i.warning = NULL;
19165796c8dcSSimon Schubert }
19175796c8dcSSimon Schubert /* Fall through. */
19185796c8dcSSimon Schubert case CYCLE:
19195796c8dcSSimon Schubert /* Try again with the referenced symbol. */
19205796c8dcSSimon Schubert h = h->u.i.link;
19215796c8dcSSimon Schubert cycle = TRUE;
19225796c8dcSSimon Schubert break;
19235796c8dcSSimon Schubert
19245796c8dcSSimon Schubert case REFC:
19255796c8dcSSimon Schubert /* A reference to an indirect symbol. */
19265796c8dcSSimon Schubert if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
19275796c8dcSSimon Schubert h->u.undef.next = h;
19285796c8dcSSimon Schubert h = h->u.i.link;
19295796c8dcSSimon Schubert cycle = TRUE;
19305796c8dcSSimon Schubert break;
19315796c8dcSSimon Schubert
19325796c8dcSSimon Schubert case WARN:
19335796c8dcSSimon Schubert /* Issue a warning. */
19345796c8dcSSimon Schubert if (! (*info->callbacks->warning) (info, string, h->root.string,
19355796c8dcSSimon Schubert hash_entry_bfd (h), NULL, 0))
19365796c8dcSSimon Schubert return FALSE;
19375796c8dcSSimon Schubert break;
19385796c8dcSSimon Schubert
19395796c8dcSSimon Schubert case CWARN:
19405796c8dcSSimon Schubert /* Warn if this symbol has been referenced already,
19415796c8dcSSimon Schubert otherwise add a warning. A symbol has been referenced if
19425796c8dcSSimon Schubert the u.undef.next field is not NULL, or it is the tail of the
19435796c8dcSSimon Schubert undefined symbol list. The REF case above helps to
19445796c8dcSSimon Schubert ensure this. */
19455796c8dcSSimon Schubert if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
19465796c8dcSSimon Schubert {
19475796c8dcSSimon Schubert if (! (*info->callbacks->warning) (info, string, h->root.string,
19485796c8dcSSimon Schubert hash_entry_bfd (h), NULL, 0))
19495796c8dcSSimon Schubert return FALSE;
19505796c8dcSSimon Schubert break;
19515796c8dcSSimon Schubert }
19525796c8dcSSimon Schubert /* Fall through. */
19535796c8dcSSimon Schubert case MWARN:
19545796c8dcSSimon Schubert /* Make a warning symbol. */
19555796c8dcSSimon Schubert {
19565796c8dcSSimon Schubert struct bfd_link_hash_entry *sub;
19575796c8dcSSimon Schubert
19585796c8dcSSimon Schubert /* STRING is the warning to give. */
19595796c8dcSSimon Schubert sub = ((struct bfd_link_hash_entry *)
19605796c8dcSSimon Schubert ((*info->hash->table.newfunc)
19615796c8dcSSimon Schubert (NULL, &info->hash->table, h->root.string)));
19625796c8dcSSimon Schubert if (sub == NULL)
19635796c8dcSSimon Schubert return FALSE;
19645796c8dcSSimon Schubert *sub = *h;
19655796c8dcSSimon Schubert sub->type = bfd_link_hash_warning;
19665796c8dcSSimon Schubert sub->u.i.link = h;
19675796c8dcSSimon Schubert if (! copy)
19685796c8dcSSimon Schubert sub->u.i.warning = string;
19695796c8dcSSimon Schubert else
19705796c8dcSSimon Schubert {
19715796c8dcSSimon Schubert char *w;
19725796c8dcSSimon Schubert size_t len = strlen (string) + 1;
19735796c8dcSSimon Schubert
19745796c8dcSSimon Schubert w = (char *) bfd_hash_allocate (&info->hash->table, len);
19755796c8dcSSimon Schubert if (w == NULL)
19765796c8dcSSimon Schubert return FALSE;
19775796c8dcSSimon Schubert memcpy (w, string, len);
19785796c8dcSSimon Schubert sub->u.i.warning = w;
19795796c8dcSSimon Schubert }
19805796c8dcSSimon Schubert
19815796c8dcSSimon Schubert bfd_hash_replace (&info->hash->table,
19825796c8dcSSimon Schubert (struct bfd_hash_entry *) h,
19835796c8dcSSimon Schubert (struct bfd_hash_entry *) sub);
19845796c8dcSSimon Schubert if (hashp != NULL)
19855796c8dcSSimon Schubert *hashp = sub;
19865796c8dcSSimon Schubert }
19875796c8dcSSimon Schubert break;
19885796c8dcSSimon Schubert }
19895796c8dcSSimon Schubert }
19905796c8dcSSimon Schubert while (cycle);
19915796c8dcSSimon Schubert
19925796c8dcSSimon Schubert return TRUE;
19935796c8dcSSimon Schubert }
19945796c8dcSSimon Schubert
19955796c8dcSSimon Schubert /* Generic final link routine. */
19965796c8dcSSimon Schubert
19975796c8dcSSimon Schubert bfd_boolean
_bfd_generic_final_link(bfd * abfd,struct bfd_link_info * info)19985796c8dcSSimon Schubert _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
19995796c8dcSSimon Schubert {
20005796c8dcSSimon Schubert bfd *sub;
20015796c8dcSSimon Schubert asection *o;
20025796c8dcSSimon Schubert struct bfd_link_order *p;
20035796c8dcSSimon Schubert size_t outsymalloc;
20045796c8dcSSimon Schubert struct generic_write_global_symbol_info wginfo;
20055796c8dcSSimon Schubert
20065796c8dcSSimon Schubert bfd_get_outsymbols (abfd) = NULL;
20075796c8dcSSimon Schubert bfd_get_symcount (abfd) = 0;
20085796c8dcSSimon Schubert outsymalloc = 0;
20095796c8dcSSimon Schubert
20105796c8dcSSimon Schubert /* Mark all sections which will be included in the output file. */
20115796c8dcSSimon Schubert for (o = abfd->sections; o != NULL; o = o->next)
20125796c8dcSSimon Schubert for (p = o->map_head.link_order; p != NULL; p = p->next)
20135796c8dcSSimon Schubert if (p->type == bfd_indirect_link_order)
20145796c8dcSSimon Schubert p->u.indirect.section->linker_mark = TRUE;
20155796c8dcSSimon Schubert
20165796c8dcSSimon Schubert /* Build the output symbol table. */
20175796c8dcSSimon Schubert for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
20185796c8dcSSimon Schubert if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
20195796c8dcSSimon Schubert return FALSE;
20205796c8dcSSimon Schubert
20215796c8dcSSimon Schubert /* Accumulate the global symbols. */
20225796c8dcSSimon Schubert wginfo.info = info;
20235796c8dcSSimon Schubert wginfo.output_bfd = abfd;
20245796c8dcSSimon Schubert wginfo.psymalloc = &outsymalloc;
20255796c8dcSSimon Schubert _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
20265796c8dcSSimon Schubert _bfd_generic_link_write_global_symbol,
20275796c8dcSSimon Schubert &wginfo);
20285796c8dcSSimon Schubert
20295796c8dcSSimon Schubert /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
20305796c8dcSSimon Schubert shouldn't really need one, since we have SYMCOUNT, but some old
20315796c8dcSSimon Schubert code still expects one. */
20325796c8dcSSimon Schubert if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
20335796c8dcSSimon Schubert return FALSE;
20345796c8dcSSimon Schubert
20355796c8dcSSimon Schubert if (info->relocatable)
20365796c8dcSSimon Schubert {
20375796c8dcSSimon Schubert /* Allocate space for the output relocs for each section. */
20385796c8dcSSimon Schubert for (o = abfd->sections; o != NULL; o = o->next)
20395796c8dcSSimon Schubert {
20405796c8dcSSimon Schubert o->reloc_count = 0;
20415796c8dcSSimon Schubert for (p = o->map_head.link_order; p != NULL; p = p->next)
20425796c8dcSSimon Schubert {
20435796c8dcSSimon Schubert if (p->type == bfd_section_reloc_link_order
20445796c8dcSSimon Schubert || p->type == bfd_symbol_reloc_link_order)
20455796c8dcSSimon Schubert ++o->reloc_count;
20465796c8dcSSimon Schubert else if (p->type == bfd_indirect_link_order)
20475796c8dcSSimon Schubert {
20485796c8dcSSimon Schubert asection *input_section;
20495796c8dcSSimon Schubert bfd *input_bfd;
20505796c8dcSSimon Schubert long relsize;
20515796c8dcSSimon Schubert arelent **relocs;
20525796c8dcSSimon Schubert asymbol **symbols;
20535796c8dcSSimon Schubert long reloc_count;
20545796c8dcSSimon Schubert
20555796c8dcSSimon Schubert input_section = p->u.indirect.section;
20565796c8dcSSimon Schubert input_bfd = input_section->owner;
20575796c8dcSSimon Schubert relsize = bfd_get_reloc_upper_bound (input_bfd,
20585796c8dcSSimon Schubert input_section);
20595796c8dcSSimon Schubert if (relsize < 0)
20605796c8dcSSimon Schubert return FALSE;
20615796c8dcSSimon Schubert relocs = (arelent **) bfd_malloc (relsize);
20625796c8dcSSimon Schubert if (!relocs && relsize != 0)
20635796c8dcSSimon Schubert return FALSE;
20645796c8dcSSimon Schubert symbols = _bfd_generic_link_get_symbols (input_bfd);
20655796c8dcSSimon Schubert reloc_count = bfd_canonicalize_reloc (input_bfd,
20665796c8dcSSimon Schubert input_section,
20675796c8dcSSimon Schubert relocs,
20685796c8dcSSimon Schubert symbols);
20695796c8dcSSimon Schubert free (relocs);
20705796c8dcSSimon Schubert if (reloc_count < 0)
20715796c8dcSSimon Schubert return FALSE;
20725796c8dcSSimon Schubert BFD_ASSERT ((unsigned long) reloc_count
20735796c8dcSSimon Schubert == input_section->reloc_count);
20745796c8dcSSimon Schubert o->reloc_count += reloc_count;
20755796c8dcSSimon Schubert }
20765796c8dcSSimon Schubert }
20775796c8dcSSimon Schubert if (o->reloc_count > 0)
20785796c8dcSSimon Schubert {
20795796c8dcSSimon Schubert bfd_size_type amt;
20805796c8dcSSimon Schubert
20815796c8dcSSimon Schubert amt = o->reloc_count;
20825796c8dcSSimon Schubert amt *= sizeof (arelent *);
20835796c8dcSSimon Schubert o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
20845796c8dcSSimon Schubert if (!o->orelocation)
20855796c8dcSSimon Schubert return FALSE;
20865796c8dcSSimon Schubert o->flags |= SEC_RELOC;
20875796c8dcSSimon Schubert /* Reset the count so that it can be used as an index
20885796c8dcSSimon Schubert when putting in the output relocs. */
20895796c8dcSSimon Schubert o->reloc_count = 0;
20905796c8dcSSimon Schubert }
20915796c8dcSSimon Schubert }
20925796c8dcSSimon Schubert }
20935796c8dcSSimon Schubert
20945796c8dcSSimon Schubert /* Handle all the link order information for the sections. */
20955796c8dcSSimon Schubert for (o = abfd->sections; o != NULL; o = o->next)
20965796c8dcSSimon Schubert {
20975796c8dcSSimon Schubert for (p = o->map_head.link_order; p != NULL; p = p->next)
20985796c8dcSSimon Schubert {
20995796c8dcSSimon Schubert switch (p->type)
21005796c8dcSSimon Schubert {
21015796c8dcSSimon Schubert case bfd_section_reloc_link_order:
21025796c8dcSSimon Schubert case bfd_symbol_reloc_link_order:
21035796c8dcSSimon Schubert if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
21045796c8dcSSimon Schubert return FALSE;
21055796c8dcSSimon Schubert break;
21065796c8dcSSimon Schubert case bfd_indirect_link_order:
21075796c8dcSSimon Schubert if (! default_indirect_link_order (abfd, info, o, p, TRUE))
21085796c8dcSSimon Schubert return FALSE;
21095796c8dcSSimon Schubert break;
21105796c8dcSSimon Schubert default:
21115796c8dcSSimon Schubert if (! _bfd_default_link_order (abfd, info, o, p))
21125796c8dcSSimon Schubert return FALSE;
21135796c8dcSSimon Schubert break;
21145796c8dcSSimon Schubert }
21155796c8dcSSimon Schubert }
21165796c8dcSSimon Schubert }
21175796c8dcSSimon Schubert
21185796c8dcSSimon Schubert return TRUE;
21195796c8dcSSimon Schubert }
21205796c8dcSSimon Schubert
21215796c8dcSSimon Schubert /* Add an output symbol to the output BFD. */
21225796c8dcSSimon Schubert
21235796c8dcSSimon Schubert static bfd_boolean
generic_add_output_symbol(bfd * output_bfd,size_t * psymalloc,asymbol * sym)21245796c8dcSSimon Schubert generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
21255796c8dcSSimon Schubert {
21265796c8dcSSimon Schubert if (bfd_get_symcount (output_bfd) >= *psymalloc)
21275796c8dcSSimon Schubert {
21285796c8dcSSimon Schubert asymbol **newsyms;
21295796c8dcSSimon Schubert bfd_size_type amt;
21305796c8dcSSimon Schubert
21315796c8dcSSimon Schubert if (*psymalloc == 0)
21325796c8dcSSimon Schubert *psymalloc = 124;
21335796c8dcSSimon Schubert else
21345796c8dcSSimon Schubert *psymalloc *= 2;
21355796c8dcSSimon Schubert amt = *psymalloc;
21365796c8dcSSimon Schubert amt *= sizeof (asymbol *);
21375796c8dcSSimon Schubert newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
21385796c8dcSSimon Schubert if (newsyms == NULL)
21395796c8dcSSimon Schubert return FALSE;
21405796c8dcSSimon Schubert bfd_get_outsymbols (output_bfd) = newsyms;
21415796c8dcSSimon Schubert }
21425796c8dcSSimon Schubert
21435796c8dcSSimon Schubert bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
21445796c8dcSSimon Schubert if (sym != NULL)
21455796c8dcSSimon Schubert ++ bfd_get_symcount (output_bfd);
21465796c8dcSSimon Schubert
21475796c8dcSSimon Schubert return TRUE;
21485796c8dcSSimon Schubert }
21495796c8dcSSimon Schubert
21505796c8dcSSimon Schubert /* Handle the symbols for an input BFD. */
21515796c8dcSSimon Schubert
21525796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_output_symbols(bfd * output_bfd,bfd * input_bfd,struct bfd_link_info * info,size_t * psymalloc)21535796c8dcSSimon Schubert _bfd_generic_link_output_symbols (bfd *output_bfd,
21545796c8dcSSimon Schubert bfd *input_bfd,
21555796c8dcSSimon Schubert struct bfd_link_info *info,
21565796c8dcSSimon Schubert size_t *psymalloc)
21575796c8dcSSimon Schubert {
21585796c8dcSSimon Schubert asymbol **sym_ptr;
21595796c8dcSSimon Schubert asymbol **sym_end;
21605796c8dcSSimon Schubert
21615796c8dcSSimon Schubert if (!bfd_generic_link_read_symbols (input_bfd))
21625796c8dcSSimon Schubert return FALSE;
21635796c8dcSSimon Schubert
21645796c8dcSSimon Schubert /* Create a filename symbol if we are supposed to. */
21655796c8dcSSimon Schubert if (info->create_object_symbols_section != NULL)
21665796c8dcSSimon Schubert {
21675796c8dcSSimon Schubert asection *sec;
21685796c8dcSSimon Schubert
21695796c8dcSSimon Schubert for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
21705796c8dcSSimon Schubert {
21715796c8dcSSimon Schubert if (sec->output_section == info->create_object_symbols_section)
21725796c8dcSSimon Schubert {
21735796c8dcSSimon Schubert asymbol *newsym;
21745796c8dcSSimon Schubert
21755796c8dcSSimon Schubert newsym = bfd_make_empty_symbol (input_bfd);
21765796c8dcSSimon Schubert if (!newsym)
21775796c8dcSSimon Schubert return FALSE;
21785796c8dcSSimon Schubert newsym->name = input_bfd->filename;
21795796c8dcSSimon Schubert newsym->value = 0;
21805796c8dcSSimon Schubert newsym->flags = BSF_LOCAL | BSF_FILE;
21815796c8dcSSimon Schubert newsym->section = sec;
21825796c8dcSSimon Schubert
21835796c8dcSSimon Schubert if (! generic_add_output_symbol (output_bfd, psymalloc,
21845796c8dcSSimon Schubert newsym))
21855796c8dcSSimon Schubert return FALSE;
21865796c8dcSSimon Schubert
21875796c8dcSSimon Schubert break;
21885796c8dcSSimon Schubert }
21895796c8dcSSimon Schubert }
21905796c8dcSSimon Schubert }
21915796c8dcSSimon Schubert
21925796c8dcSSimon Schubert /* Adjust the values of the globally visible symbols, and write out
21935796c8dcSSimon Schubert local symbols. */
21945796c8dcSSimon Schubert sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
21955796c8dcSSimon Schubert sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
21965796c8dcSSimon Schubert for (; sym_ptr < sym_end; sym_ptr++)
21975796c8dcSSimon Schubert {
21985796c8dcSSimon Schubert asymbol *sym;
21995796c8dcSSimon Schubert struct generic_link_hash_entry *h;
22005796c8dcSSimon Schubert bfd_boolean output;
22015796c8dcSSimon Schubert
22025796c8dcSSimon Schubert h = NULL;
22035796c8dcSSimon Schubert sym = *sym_ptr;
22045796c8dcSSimon Schubert if ((sym->flags & (BSF_INDIRECT
22055796c8dcSSimon Schubert | BSF_WARNING
22065796c8dcSSimon Schubert | BSF_GLOBAL
22075796c8dcSSimon Schubert | BSF_CONSTRUCTOR
22085796c8dcSSimon Schubert | BSF_WEAK)) != 0
22095796c8dcSSimon Schubert || bfd_is_und_section (bfd_get_section (sym))
22105796c8dcSSimon Schubert || bfd_is_com_section (bfd_get_section (sym))
22115796c8dcSSimon Schubert || bfd_is_ind_section (bfd_get_section (sym)))
22125796c8dcSSimon Schubert {
22135796c8dcSSimon Schubert if (sym->udata.p != NULL)
22145796c8dcSSimon Schubert h = (struct generic_link_hash_entry *) sym->udata.p;
22155796c8dcSSimon Schubert else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
22165796c8dcSSimon Schubert {
22175796c8dcSSimon Schubert /* This case normally means that the main linker code
22185796c8dcSSimon Schubert deliberately ignored this constructor symbol. We
22195796c8dcSSimon Schubert should just pass it through. This will screw up if
22205796c8dcSSimon Schubert the constructor symbol is from a different,
22215796c8dcSSimon Schubert non-generic, object file format, but the case will
22225796c8dcSSimon Schubert only arise when linking with -r, which will probably
22235796c8dcSSimon Schubert fail anyhow, since there will be no way to represent
22245796c8dcSSimon Schubert the relocs in the output format being used. */
22255796c8dcSSimon Schubert h = NULL;
22265796c8dcSSimon Schubert }
22275796c8dcSSimon Schubert else if (bfd_is_und_section (bfd_get_section (sym)))
22285796c8dcSSimon Schubert h = ((struct generic_link_hash_entry *)
22295796c8dcSSimon Schubert bfd_wrapped_link_hash_lookup (output_bfd, info,
22305796c8dcSSimon Schubert bfd_asymbol_name (sym),
22315796c8dcSSimon Schubert FALSE, FALSE, TRUE));
22325796c8dcSSimon Schubert else
22335796c8dcSSimon Schubert h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
22345796c8dcSSimon Schubert bfd_asymbol_name (sym),
22355796c8dcSSimon Schubert FALSE, FALSE, TRUE);
22365796c8dcSSimon Schubert
22375796c8dcSSimon Schubert if (h != NULL)
22385796c8dcSSimon Schubert {
22395796c8dcSSimon Schubert /* Force all references to this symbol to point to
22405796c8dcSSimon Schubert the same area in memory. It is possible that
22415796c8dcSSimon Schubert this routine will be called with a hash table
22425796c8dcSSimon Schubert other than a generic hash table, so we double
22435796c8dcSSimon Schubert check that. */
22445796c8dcSSimon Schubert if (info->output_bfd->xvec == input_bfd->xvec)
22455796c8dcSSimon Schubert {
22465796c8dcSSimon Schubert if (h->sym != NULL)
22475796c8dcSSimon Schubert *sym_ptr = sym = h->sym;
22485796c8dcSSimon Schubert }
22495796c8dcSSimon Schubert
22505796c8dcSSimon Schubert switch (h->root.type)
22515796c8dcSSimon Schubert {
22525796c8dcSSimon Schubert default:
22535796c8dcSSimon Schubert case bfd_link_hash_new:
22545796c8dcSSimon Schubert abort ();
22555796c8dcSSimon Schubert case bfd_link_hash_undefined:
22565796c8dcSSimon Schubert break;
22575796c8dcSSimon Schubert case bfd_link_hash_undefweak:
22585796c8dcSSimon Schubert sym->flags |= BSF_WEAK;
22595796c8dcSSimon Schubert break;
22605796c8dcSSimon Schubert case bfd_link_hash_indirect:
22615796c8dcSSimon Schubert h = (struct generic_link_hash_entry *) h->root.u.i.link;
22625796c8dcSSimon Schubert /* fall through */
22635796c8dcSSimon Schubert case bfd_link_hash_defined:
22645796c8dcSSimon Schubert sym->flags |= BSF_GLOBAL;
22655796c8dcSSimon Schubert sym->flags &=~ BSF_CONSTRUCTOR;
22665796c8dcSSimon Schubert sym->value = h->root.u.def.value;
22675796c8dcSSimon Schubert sym->section = h->root.u.def.section;
22685796c8dcSSimon Schubert break;
22695796c8dcSSimon Schubert case bfd_link_hash_defweak:
22705796c8dcSSimon Schubert sym->flags |= BSF_WEAK;
22715796c8dcSSimon Schubert sym->flags &=~ BSF_CONSTRUCTOR;
22725796c8dcSSimon Schubert sym->value = h->root.u.def.value;
22735796c8dcSSimon Schubert sym->section = h->root.u.def.section;
22745796c8dcSSimon Schubert break;
22755796c8dcSSimon Schubert case bfd_link_hash_common:
22765796c8dcSSimon Schubert sym->value = h->root.u.c.size;
22775796c8dcSSimon Schubert sym->flags |= BSF_GLOBAL;
22785796c8dcSSimon Schubert if (! bfd_is_com_section (sym->section))
22795796c8dcSSimon Schubert {
22805796c8dcSSimon Schubert BFD_ASSERT (bfd_is_und_section (sym->section));
22815796c8dcSSimon Schubert sym->section = bfd_com_section_ptr;
22825796c8dcSSimon Schubert }
22835796c8dcSSimon Schubert /* We do not set the section of the symbol to
22845796c8dcSSimon Schubert h->root.u.c.p->section. That value was saved so
22855796c8dcSSimon Schubert that we would know where to allocate the symbol
22865796c8dcSSimon Schubert if it was defined. In this case the type is
22875796c8dcSSimon Schubert still bfd_link_hash_common, so we did not define
22885796c8dcSSimon Schubert it, so we do not want to use that section. */
22895796c8dcSSimon Schubert break;
22905796c8dcSSimon Schubert }
22915796c8dcSSimon Schubert }
22925796c8dcSSimon Schubert }
22935796c8dcSSimon Schubert
22945796c8dcSSimon Schubert /* This switch is straight from the old code in
22955796c8dcSSimon Schubert write_file_locals in ldsym.c. */
22965796c8dcSSimon Schubert if (info->strip == strip_all
22975796c8dcSSimon Schubert || (info->strip == strip_some
22985796c8dcSSimon Schubert && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
22995796c8dcSSimon Schubert FALSE, FALSE) == NULL))
23005796c8dcSSimon Schubert output = FALSE;
23015796c8dcSSimon Schubert else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
23025796c8dcSSimon Schubert {
23035796c8dcSSimon Schubert /* If this symbol is marked as occurring now, rather
23045796c8dcSSimon Schubert than at the end, output it now. This is used for
23055796c8dcSSimon Schubert COFF C_EXT FCN symbols. FIXME: There must be a
23065796c8dcSSimon Schubert better way. */
23075796c8dcSSimon Schubert if (bfd_asymbol_bfd (sym) == input_bfd
23085796c8dcSSimon Schubert && (sym->flags & BSF_NOT_AT_END) != 0)
23095796c8dcSSimon Schubert output = TRUE;
23105796c8dcSSimon Schubert else
23115796c8dcSSimon Schubert output = FALSE;
23125796c8dcSSimon Schubert }
23135796c8dcSSimon Schubert else if (bfd_is_ind_section (sym->section))
23145796c8dcSSimon Schubert output = FALSE;
23155796c8dcSSimon Schubert else if ((sym->flags & BSF_DEBUGGING) != 0)
23165796c8dcSSimon Schubert {
23175796c8dcSSimon Schubert if (info->strip == strip_none)
23185796c8dcSSimon Schubert output = TRUE;
23195796c8dcSSimon Schubert else
23205796c8dcSSimon Schubert output = FALSE;
23215796c8dcSSimon Schubert }
23225796c8dcSSimon Schubert else if (bfd_is_und_section (sym->section)
23235796c8dcSSimon Schubert || bfd_is_com_section (sym->section))
23245796c8dcSSimon Schubert output = FALSE;
23255796c8dcSSimon Schubert else if ((sym->flags & BSF_LOCAL) != 0)
23265796c8dcSSimon Schubert {
23275796c8dcSSimon Schubert if ((sym->flags & BSF_WARNING) != 0)
23285796c8dcSSimon Schubert output = FALSE;
23295796c8dcSSimon Schubert else
23305796c8dcSSimon Schubert {
23315796c8dcSSimon Schubert switch (info->discard)
23325796c8dcSSimon Schubert {
23335796c8dcSSimon Schubert default:
23345796c8dcSSimon Schubert case discard_all:
23355796c8dcSSimon Schubert output = FALSE;
23365796c8dcSSimon Schubert break;
23375796c8dcSSimon Schubert case discard_sec_merge:
23385796c8dcSSimon Schubert output = TRUE;
23395796c8dcSSimon Schubert if (info->relocatable
23405796c8dcSSimon Schubert || ! (sym->section->flags & SEC_MERGE))
23415796c8dcSSimon Schubert break;
23425796c8dcSSimon Schubert /* FALLTHROUGH */
23435796c8dcSSimon Schubert case discard_l:
23445796c8dcSSimon Schubert if (bfd_is_local_label (input_bfd, sym))
23455796c8dcSSimon Schubert output = FALSE;
23465796c8dcSSimon Schubert else
23475796c8dcSSimon Schubert output = TRUE;
23485796c8dcSSimon Schubert break;
23495796c8dcSSimon Schubert case discard_none:
23505796c8dcSSimon Schubert output = TRUE;
23515796c8dcSSimon Schubert break;
23525796c8dcSSimon Schubert }
23535796c8dcSSimon Schubert }
23545796c8dcSSimon Schubert }
23555796c8dcSSimon Schubert else if ((sym->flags & BSF_CONSTRUCTOR))
23565796c8dcSSimon Schubert {
23575796c8dcSSimon Schubert if (info->strip != strip_all)
23585796c8dcSSimon Schubert output = TRUE;
23595796c8dcSSimon Schubert else
23605796c8dcSSimon Schubert output = FALSE;
23615796c8dcSSimon Schubert }
2362*ef5ccd6cSJohn Marino else if (sym->flags == 0
2363*ef5ccd6cSJohn Marino && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2364*ef5ccd6cSJohn Marino /* LTO doesn't set symbol information. We get here with the
2365*ef5ccd6cSJohn Marino generic linker for a symbol that was "common" but no longer
2366*ef5ccd6cSJohn Marino needs to be global. */
2367*ef5ccd6cSJohn Marino output = FALSE;
23685796c8dcSSimon Schubert else
23695796c8dcSSimon Schubert abort ();
23705796c8dcSSimon Schubert
23715796c8dcSSimon Schubert /* If this symbol is in a section which is not being included
23725796c8dcSSimon Schubert in the output file, then we don't want to output the
23735796c8dcSSimon Schubert symbol. */
23745796c8dcSSimon Schubert if (!bfd_is_abs_section (sym->section)
23755796c8dcSSimon Schubert && bfd_section_removed_from_list (output_bfd,
23765796c8dcSSimon Schubert sym->section->output_section))
23775796c8dcSSimon Schubert output = FALSE;
23785796c8dcSSimon Schubert
23795796c8dcSSimon Schubert if (output)
23805796c8dcSSimon Schubert {
23815796c8dcSSimon Schubert if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
23825796c8dcSSimon Schubert return FALSE;
23835796c8dcSSimon Schubert if (h != NULL)
23845796c8dcSSimon Schubert h->written = TRUE;
23855796c8dcSSimon Schubert }
23865796c8dcSSimon Schubert }
23875796c8dcSSimon Schubert
23885796c8dcSSimon Schubert return TRUE;
23895796c8dcSSimon Schubert }
23905796c8dcSSimon Schubert
23915796c8dcSSimon Schubert /* Set the section and value of a generic BFD symbol based on a linker
23925796c8dcSSimon Schubert hash table entry. */
23935796c8dcSSimon Schubert
23945796c8dcSSimon Schubert static void
set_symbol_from_hash(asymbol * sym,struct bfd_link_hash_entry * h)23955796c8dcSSimon Schubert set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
23965796c8dcSSimon Schubert {
23975796c8dcSSimon Schubert switch (h->type)
23985796c8dcSSimon Schubert {
23995796c8dcSSimon Schubert default:
24005796c8dcSSimon Schubert abort ();
24015796c8dcSSimon Schubert break;
24025796c8dcSSimon Schubert case bfd_link_hash_new:
24035796c8dcSSimon Schubert /* This can happen when a constructor symbol is seen but we are
24045796c8dcSSimon Schubert not building constructors. */
24055796c8dcSSimon Schubert if (sym->section != NULL)
24065796c8dcSSimon Schubert {
24075796c8dcSSimon Schubert BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
24085796c8dcSSimon Schubert }
24095796c8dcSSimon Schubert else
24105796c8dcSSimon Schubert {
24115796c8dcSSimon Schubert sym->flags |= BSF_CONSTRUCTOR;
24125796c8dcSSimon Schubert sym->section = bfd_abs_section_ptr;
24135796c8dcSSimon Schubert sym->value = 0;
24145796c8dcSSimon Schubert }
24155796c8dcSSimon Schubert break;
24165796c8dcSSimon Schubert case bfd_link_hash_undefined:
24175796c8dcSSimon Schubert sym->section = bfd_und_section_ptr;
24185796c8dcSSimon Schubert sym->value = 0;
24195796c8dcSSimon Schubert break;
24205796c8dcSSimon Schubert case bfd_link_hash_undefweak:
24215796c8dcSSimon Schubert sym->section = bfd_und_section_ptr;
24225796c8dcSSimon Schubert sym->value = 0;
24235796c8dcSSimon Schubert sym->flags |= BSF_WEAK;
24245796c8dcSSimon Schubert break;
24255796c8dcSSimon Schubert case bfd_link_hash_defined:
24265796c8dcSSimon Schubert sym->section = h->u.def.section;
24275796c8dcSSimon Schubert sym->value = h->u.def.value;
24285796c8dcSSimon Schubert break;
24295796c8dcSSimon Schubert case bfd_link_hash_defweak:
24305796c8dcSSimon Schubert sym->flags |= BSF_WEAK;
24315796c8dcSSimon Schubert sym->section = h->u.def.section;
24325796c8dcSSimon Schubert sym->value = h->u.def.value;
24335796c8dcSSimon Schubert break;
24345796c8dcSSimon Schubert case bfd_link_hash_common:
24355796c8dcSSimon Schubert sym->value = h->u.c.size;
24365796c8dcSSimon Schubert if (sym->section == NULL)
24375796c8dcSSimon Schubert sym->section = bfd_com_section_ptr;
24385796c8dcSSimon Schubert else if (! bfd_is_com_section (sym->section))
24395796c8dcSSimon Schubert {
24405796c8dcSSimon Schubert BFD_ASSERT (bfd_is_und_section (sym->section));
24415796c8dcSSimon Schubert sym->section = bfd_com_section_ptr;
24425796c8dcSSimon Schubert }
24435796c8dcSSimon Schubert /* Do not set the section; see _bfd_generic_link_output_symbols. */
24445796c8dcSSimon Schubert break;
24455796c8dcSSimon Schubert case bfd_link_hash_indirect:
24465796c8dcSSimon Schubert case bfd_link_hash_warning:
24475796c8dcSSimon Schubert /* FIXME: What should we do here? */
24485796c8dcSSimon Schubert break;
24495796c8dcSSimon Schubert }
24505796c8dcSSimon Schubert }
24515796c8dcSSimon Schubert
24525796c8dcSSimon Schubert /* Write out a global symbol, if it hasn't already been written out.
24535796c8dcSSimon Schubert This is called for each symbol in the hash table. */
24545796c8dcSSimon Schubert
24555796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_write_global_symbol(struct generic_link_hash_entry * h,void * data)24565796c8dcSSimon Schubert _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
24575796c8dcSSimon Schubert void *data)
24585796c8dcSSimon Schubert {
24595796c8dcSSimon Schubert struct generic_write_global_symbol_info *wginfo =
24605796c8dcSSimon Schubert (struct generic_write_global_symbol_info *) data;
24615796c8dcSSimon Schubert asymbol *sym;
24625796c8dcSSimon Schubert
24635796c8dcSSimon Schubert if (h->written)
24645796c8dcSSimon Schubert return TRUE;
24655796c8dcSSimon Schubert
24665796c8dcSSimon Schubert h->written = TRUE;
24675796c8dcSSimon Schubert
24685796c8dcSSimon Schubert if (wginfo->info->strip == strip_all
24695796c8dcSSimon Schubert || (wginfo->info->strip == strip_some
24705796c8dcSSimon Schubert && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
24715796c8dcSSimon Schubert FALSE, FALSE) == NULL))
24725796c8dcSSimon Schubert return TRUE;
24735796c8dcSSimon Schubert
24745796c8dcSSimon Schubert if (h->sym != NULL)
24755796c8dcSSimon Schubert sym = h->sym;
24765796c8dcSSimon Schubert else
24775796c8dcSSimon Schubert {
24785796c8dcSSimon Schubert sym = bfd_make_empty_symbol (wginfo->output_bfd);
24795796c8dcSSimon Schubert if (!sym)
24805796c8dcSSimon Schubert return FALSE;
24815796c8dcSSimon Schubert sym->name = h->root.root.string;
24825796c8dcSSimon Schubert sym->flags = 0;
24835796c8dcSSimon Schubert }
24845796c8dcSSimon Schubert
24855796c8dcSSimon Schubert set_symbol_from_hash (sym, &h->root);
24865796c8dcSSimon Schubert
24875796c8dcSSimon Schubert sym->flags |= BSF_GLOBAL;
24885796c8dcSSimon Schubert
24895796c8dcSSimon Schubert if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
24905796c8dcSSimon Schubert sym))
24915796c8dcSSimon Schubert {
24925796c8dcSSimon Schubert /* FIXME: No way to return failure. */
24935796c8dcSSimon Schubert abort ();
24945796c8dcSSimon Schubert }
24955796c8dcSSimon Schubert
24965796c8dcSSimon Schubert return TRUE;
24975796c8dcSSimon Schubert }
24985796c8dcSSimon Schubert
24995796c8dcSSimon Schubert /* Create a relocation. */
25005796c8dcSSimon Schubert
25015796c8dcSSimon Schubert bfd_boolean
_bfd_generic_reloc_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)25025796c8dcSSimon Schubert _bfd_generic_reloc_link_order (bfd *abfd,
25035796c8dcSSimon Schubert struct bfd_link_info *info,
25045796c8dcSSimon Schubert asection *sec,
25055796c8dcSSimon Schubert struct bfd_link_order *link_order)
25065796c8dcSSimon Schubert {
25075796c8dcSSimon Schubert arelent *r;
25085796c8dcSSimon Schubert
25095796c8dcSSimon Schubert if (! info->relocatable)
25105796c8dcSSimon Schubert abort ();
25115796c8dcSSimon Schubert if (sec->orelocation == NULL)
25125796c8dcSSimon Schubert abort ();
25135796c8dcSSimon Schubert
25145796c8dcSSimon Schubert r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
25155796c8dcSSimon Schubert if (r == NULL)
25165796c8dcSSimon Schubert return FALSE;
25175796c8dcSSimon Schubert
25185796c8dcSSimon Schubert r->address = link_order->offset;
25195796c8dcSSimon Schubert r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
25205796c8dcSSimon Schubert if (r->howto == 0)
25215796c8dcSSimon Schubert {
25225796c8dcSSimon Schubert bfd_set_error (bfd_error_bad_value);
25235796c8dcSSimon Schubert return FALSE;
25245796c8dcSSimon Schubert }
25255796c8dcSSimon Schubert
25265796c8dcSSimon Schubert /* Get the symbol to use for the relocation. */
25275796c8dcSSimon Schubert if (link_order->type == bfd_section_reloc_link_order)
25285796c8dcSSimon Schubert r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
25295796c8dcSSimon Schubert else
25305796c8dcSSimon Schubert {
25315796c8dcSSimon Schubert struct generic_link_hash_entry *h;
25325796c8dcSSimon Schubert
25335796c8dcSSimon Schubert h = ((struct generic_link_hash_entry *)
25345796c8dcSSimon Schubert bfd_wrapped_link_hash_lookup (abfd, info,
25355796c8dcSSimon Schubert link_order->u.reloc.p->u.name,
25365796c8dcSSimon Schubert FALSE, FALSE, TRUE));
25375796c8dcSSimon Schubert if (h == NULL
25385796c8dcSSimon Schubert || ! h->written)
25395796c8dcSSimon Schubert {
25405796c8dcSSimon Schubert if (! ((*info->callbacks->unattached_reloc)
25415796c8dcSSimon Schubert (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
25425796c8dcSSimon Schubert return FALSE;
25435796c8dcSSimon Schubert bfd_set_error (bfd_error_bad_value);
25445796c8dcSSimon Schubert return FALSE;
25455796c8dcSSimon Schubert }
25465796c8dcSSimon Schubert r->sym_ptr_ptr = &h->sym;
25475796c8dcSSimon Schubert }
25485796c8dcSSimon Schubert
25495796c8dcSSimon Schubert /* If this is an inplace reloc, write the addend to the object file.
25505796c8dcSSimon Schubert Otherwise, store it in the reloc addend. */
25515796c8dcSSimon Schubert if (! r->howto->partial_inplace)
25525796c8dcSSimon Schubert r->addend = link_order->u.reloc.p->addend;
25535796c8dcSSimon Schubert else
25545796c8dcSSimon Schubert {
25555796c8dcSSimon Schubert bfd_size_type size;
25565796c8dcSSimon Schubert bfd_reloc_status_type rstat;
25575796c8dcSSimon Schubert bfd_byte *buf;
25585796c8dcSSimon Schubert bfd_boolean ok;
25595796c8dcSSimon Schubert file_ptr loc;
25605796c8dcSSimon Schubert
25615796c8dcSSimon Schubert size = bfd_get_reloc_size (r->howto);
25625796c8dcSSimon Schubert buf = (bfd_byte *) bfd_zmalloc (size);
25635796c8dcSSimon Schubert if (buf == NULL)
25645796c8dcSSimon Schubert return FALSE;
25655796c8dcSSimon Schubert rstat = _bfd_relocate_contents (r->howto, abfd,
25665796c8dcSSimon Schubert (bfd_vma) link_order->u.reloc.p->addend,
25675796c8dcSSimon Schubert buf);
25685796c8dcSSimon Schubert switch (rstat)
25695796c8dcSSimon Schubert {
25705796c8dcSSimon Schubert case bfd_reloc_ok:
25715796c8dcSSimon Schubert break;
25725796c8dcSSimon Schubert default:
25735796c8dcSSimon Schubert case bfd_reloc_outofrange:
25745796c8dcSSimon Schubert abort ();
25755796c8dcSSimon Schubert case bfd_reloc_overflow:
25765796c8dcSSimon Schubert if (! ((*info->callbacks->reloc_overflow)
25775796c8dcSSimon Schubert (info, NULL,
25785796c8dcSSimon Schubert (link_order->type == bfd_section_reloc_link_order
25795796c8dcSSimon Schubert ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
25805796c8dcSSimon Schubert : link_order->u.reloc.p->u.name),
25815796c8dcSSimon Schubert r->howto->name, link_order->u.reloc.p->addend,
25825796c8dcSSimon Schubert NULL, NULL, 0)))
25835796c8dcSSimon Schubert {
25845796c8dcSSimon Schubert free (buf);
25855796c8dcSSimon Schubert return FALSE;
25865796c8dcSSimon Schubert }
25875796c8dcSSimon Schubert break;
25885796c8dcSSimon Schubert }
25895796c8dcSSimon Schubert loc = link_order->offset * bfd_octets_per_byte (abfd);
25905796c8dcSSimon Schubert ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
25915796c8dcSSimon Schubert free (buf);
25925796c8dcSSimon Schubert if (! ok)
25935796c8dcSSimon Schubert return FALSE;
25945796c8dcSSimon Schubert
25955796c8dcSSimon Schubert r->addend = 0;
25965796c8dcSSimon Schubert }
25975796c8dcSSimon Schubert
25985796c8dcSSimon Schubert sec->orelocation[sec->reloc_count] = r;
25995796c8dcSSimon Schubert ++sec->reloc_count;
26005796c8dcSSimon Schubert
26015796c8dcSSimon Schubert return TRUE;
26025796c8dcSSimon Schubert }
26035796c8dcSSimon Schubert
26045796c8dcSSimon Schubert /* Allocate a new link_order for a section. */
26055796c8dcSSimon Schubert
26065796c8dcSSimon Schubert struct bfd_link_order *
bfd_new_link_order(bfd * abfd,asection * section)26075796c8dcSSimon Schubert bfd_new_link_order (bfd *abfd, asection *section)
26085796c8dcSSimon Schubert {
26095796c8dcSSimon Schubert bfd_size_type amt = sizeof (struct bfd_link_order);
26105796c8dcSSimon Schubert struct bfd_link_order *new_lo;
26115796c8dcSSimon Schubert
26125796c8dcSSimon Schubert new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
26135796c8dcSSimon Schubert if (!new_lo)
26145796c8dcSSimon Schubert return NULL;
26155796c8dcSSimon Schubert
26165796c8dcSSimon Schubert new_lo->type = bfd_undefined_link_order;
26175796c8dcSSimon Schubert
26185796c8dcSSimon Schubert if (section->map_tail.link_order != NULL)
26195796c8dcSSimon Schubert section->map_tail.link_order->next = new_lo;
26205796c8dcSSimon Schubert else
26215796c8dcSSimon Schubert section->map_head.link_order = new_lo;
26225796c8dcSSimon Schubert section->map_tail.link_order = new_lo;
26235796c8dcSSimon Schubert
26245796c8dcSSimon Schubert return new_lo;
26255796c8dcSSimon Schubert }
26265796c8dcSSimon Schubert
26275796c8dcSSimon Schubert /* Default link order processing routine. Note that we can not handle
26285796c8dcSSimon Schubert the reloc_link_order types here, since they depend upon the details
26295796c8dcSSimon Schubert of how the particular backends generates relocs. */
26305796c8dcSSimon Schubert
26315796c8dcSSimon Schubert bfd_boolean
_bfd_default_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)26325796c8dcSSimon Schubert _bfd_default_link_order (bfd *abfd,
26335796c8dcSSimon Schubert struct bfd_link_info *info,
26345796c8dcSSimon Schubert asection *sec,
26355796c8dcSSimon Schubert struct bfd_link_order *link_order)
26365796c8dcSSimon Schubert {
26375796c8dcSSimon Schubert switch (link_order->type)
26385796c8dcSSimon Schubert {
26395796c8dcSSimon Schubert case bfd_undefined_link_order:
26405796c8dcSSimon Schubert case bfd_section_reloc_link_order:
26415796c8dcSSimon Schubert case bfd_symbol_reloc_link_order:
26425796c8dcSSimon Schubert default:
26435796c8dcSSimon Schubert abort ();
26445796c8dcSSimon Schubert case bfd_indirect_link_order:
26455796c8dcSSimon Schubert return default_indirect_link_order (abfd, info, sec, link_order,
26465796c8dcSSimon Schubert FALSE);
26475796c8dcSSimon Schubert case bfd_data_link_order:
26485796c8dcSSimon Schubert return default_data_link_order (abfd, info, sec, link_order);
26495796c8dcSSimon Schubert }
26505796c8dcSSimon Schubert }
26515796c8dcSSimon Schubert
26525796c8dcSSimon Schubert /* Default routine to handle a bfd_data_link_order. */
26535796c8dcSSimon Schubert
26545796c8dcSSimon Schubert static bfd_boolean
default_data_link_order(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_order * link_order)26555796c8dcSSimon Schubert default_data_link_order (bfd *abfd,
26565796c8dcSSimon Schubert struct bfd_link_info *info ATTRIBUTE_UNUSED,
26575796c8dcSSimon Schubert asection *sec,
26585796c8dcSSimon Schubert struct bfd_link_order *link_order)
26595796c8dcSSimon Schubert {
26605796c8dcSSimon Schubert bfd_size_type size;
26615796c8dcSSimon Schubert size_t fill_size;
26625796c8dcSSimon Schubert bfd_byte *fill;
26635796c8dcSSimon Schubert file_ptr loc;
26645796c8dcSSimon Schubert bfd_boolean result;
26655796c8dcSSimon Schubert
26665796c8dcSSimon Schubert BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
26675796c8dcSSimon Schubert
26685796c8dcSSimon Schubert size = link_order->size;
26695796c8dcSSimon Schubert if (size == 0)
26705796c8dcSSimon Schubert return TRUE;
26715796c8dcSSimon Schubert
26725796c8dcSSimon Schubert fill = link_order->u.data.contents;
26735796c8dcSSimon Schubert fill_size = link_order->u.data.size;
2674*ef5ccd6cSJohn Marino if (fill_size == 0)
2675*ef5ccd6cSJohn Marino {
2676*ef5ccd6cSJohn Marino fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
2677*ef5ccd6cSJohn Marino (sec->flags & SEC_CODE) != 0);
2678*ef5ccd6cSJohn Marino if (fill == NULL)
2679*ef5ccd6cSJohn Marino return FALSE;
2680*ef5ccd6cSJohn Marino }
2681*ef5ccd6cSJohn Marino else if (fill_size < size)
26825796c8dcSSimon Schubert {
26835796c8dcSSimon Schubert bfd_byte *p;
26845796c8dcSSimon Schubert fill = (bfd_byte *) bfd_malloc (size);
26855796c8dcSSimon Schubert if (fill == NULL)
26865796c8dcSSimon Schubert return FALSE;
26875796c8dcSSimon Schubert p = fill;
26885796c8dcSSimon Schubert if (fill_size == 1)
26895796c8dcSSimon Schubert memset (p, (int) link_order->u.data.contents[0], (size_t) size);
26905796c8dcSSimon Schubert else
26915796c8dcSSimon Schubert {
26925796c8dcSSimon Schubert do
26935796c8dcSSimon Schubert {
26945796c8dcSSimon Schubert memcpy (p, link_order->u.data.contents, fill_size);
26955796c8dcSSimon Schubert p += fill_size;
26965796c8dcSSimon Schubert size -= fill_size;
26975796c8dcSSimon Schubert }
26985796c8dcSSimon Schubert while (size >= fill_size);
26995796c8dcSSimon Schubert if (size != 0)
27005796c8dcSSimon Schubert memcpy (p, link_order->u.data.contents, (size_t) size);
27015796c8dcSSimon Schubert size = link_order->size;
27025796c8dcSSimon Schubert }
27035796c8dcSSimon Schubert }
27045796c8dcSSimon Schubert
27055796c8dcSSimon Schubert loc = link_order->offset * bfd_octets_per_byte (abfd);
27065796c8dcSSimon Schubert result = bfd_set_section_contents (abfd, sec, fill, loc, size);
27075796c8dcSSimon Schubert
27085796c8dcSSimon Schubert if (fill != link_order->u.data.contents)
27095796c8dcSSimon Schubert free (fill);
27105796c8dcSSimon Schubert return result;
27115796c8dcSSimon Schubert }
27125796c8dcSSimon Schubert
27135796c8dcSSimon Schubert /* Default routine to handle a bfd_indirect_link_order. */
27145796c8dcSSimon Schubert
27155796c8dcSSimon Schubert static bfd_boolean
default_indirect_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order,bfd_boolean generic_linker)27165796c8dcSSimon Schubert default_indirect_link_order (bfd *output_bfd,
27175796c8dcSSimon Schubert struct bfd_link_info *info,
27185796c8dcSSimon Schubert asection *output_section,
27195796c8dcSSimon Schubert struct bfd_link_order *link_order,
27205796c8dcSSimon Schubert bfd_boolean generic_linker)
27215796c8dcSSimon Schubert {
27225796c8dcSSimon Schubert asection *input_section;
27235796c8dcSSimon Schubert bfd *input_bfd;
27245796c8dcSSimon Schubert bfd_byte *contents = NULL;
27255796c8dcSSimon Schubert bfd_byte *new_contents;
27265796c8dcSSimon Schubert bfd_size_type sec_size;
27275796c8dcSSimon Schubert file_ptr loc;
27285796c8dcSSimon Schubert
27295796c8dcSSimon Schubert BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
27305796c8dcSSimon Schubert
27315796c8dcSSimon Schubert input_section = link_order->u.indirect.section;
27325796c8dcSSimon Schubert input_bfd = input_section->owner;
27335796c8dcSSimon Schubert if (input_section->size == 0)
27345796c8dcSSimon Schubert return TRUE;
27355796c8dcSSimon Schubert
27365796c8dcSSimon Schubert BFD_ASSERT (input_section->output_section == output_section);
27375796c8dcSSimon Schubert BFD_ASSERT (input_section->output_offset == link_order->offset);
27385796c8dcSSimon Schubert BFD_ASSERT (input_section->size == link_order->size);
27395796c8dcSSimon Schubert
27405796c8dcSSimon Schubert if (info->relocatable
27415796c8dcSSimon Schubert && input_section->reloc_count > 0
27425796c8dcSSimon Schubert && output_section->orelocation == NULL)
27435796c8dcSSimon Schubert {
27445796c8dcSSimon Schubert /* Space has not been allocated for the output relocations.
27455796c8dcSSimon Schubert This can happen when we are called by a specific backend
27465796c8dcSSimon Schubert because somebody is attempting to link together different
27475796c8dcSSimon Schubert types of object files. Handling this case correctly is
27485796c8dcSSimon Schubert difficult, and sometimes impossible. */
27495796c8dcSSimon Schubert (*_bfd_error_handler)
27505796c8dcSSimon Schubert (_("Attempt to do relocatable link with %s input and %s output"),
27515796c8dcSSimon Schubert bfd_get_target (input_bfd), bfd_get_target (output_bfd));
27525796c8dcSSimon Schubert bfd_set_error (bfd_error_wrong_format);
27535796c8dcSSimon Schubert return FALSE;
27545796c8dcSSimon Schubert }
27555796c8dcSSimon Schubert
27565796c8dcSSimon Schubert if (! generic_linker)
27575796c8dcSSimon Schubert {
27585796c8dcSSimon Schubert asymbol **sympp;
27595796c8dcSSimon Schubert asymbol **symppend;
27605796c8dcSSimon Schubert
27615796c8dcSSimon Schubert /* Get the canonical symbols. The generic linker will always
27625796c8dcSSimon Schubert have retrieved them by this point, but we are being called by
27635796c8dcSSimon Schubert a specific linker, presumably because we are linking
27645796c8dcSSimon Schubert different types of object files together. */
27655796c8dcSSimon Schubert if (!bfd_generic_link_read_symbols (input_bfd))
27665796c8dcSSimon Schubert return FALSE;
27675796c8dcSSimon Schubert
27685796c8dcSSimon Schubert /* Since we have been called by a specific linker, rather than
27695796c8dcSSimon Schubert the generic linker, the values of the symbols will not be
27705796c8dcSSimon Schubert right. They will be the values as seen in the input file,
27715796c8dcSSimon Schubert not the values of the final link. We need to fix them up
27725796c8dcSSimon Schubert before we can relocate the section. */
27735796c8dcSSimon Schubert sympp = _bfd_generic_link_get_symbols (input_bfd);
27745796c8dcSSimon Schubert symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
27755796c8dcSSimon Schubert for (; sympp < symppend; sympp++)
27765796c8dcSSimon Schubert {
27775796c8dcSSimon Schubert asymbol *sym;
27785796c8dcSSimon Schubert struct bfd_link_hash_entry *h;
27795796c8dcSSimon Schubert
27805796c8dcSSimon Schubert sym = *sympp;
27815796c8dcSSimon Schubert
27825796c8dcSSimon Schubert if ((sym->flags & (BSF_INDIRECT
27835796c8dcSSimon Schubert | BSF_WARNING
27845796c8dcSSimon Schubert | BSF_GLOBAL
27855796c8dcSSimon Schubert | BSF_CONSTRUCTOR
27865796c8dcSSimon Schubert | BSF_WEAK)) != 0
27875796c8dcSSimon Schubert || bfd_is_und_section (bfd_get_section (sym))
27885796c8dcSSimon Schubert || bfd_is_com_section (bfd_get_section (sym))
27895796c8dcSSimon Schubert || bfd_is_ind_section (bfd_get_section (sym)))
27905796c8dcSSimon Schubert {
27915796c8dcSSimon Schubert /* sym->udata may have been set by
27925796c8dcSSimon Schubert generic_link_add_symbol_list. */
27935796c8dcSSimon Schubert if (sym->udata.p != NULL)
27945796c8dcSSimon Schubert h = (struct bfd_link_hash_entry *) sym->udata.p;
27955796c8dcSSimon Schubert else if (bfd_is_und_section (bfd_get_section (sym)))
27965796c8dcSSimon Schubert h = bfd_wrapped_link_hash_lookup (output_bfd, info,
27975796c8dcSSimon Schubert bfd_asymbol_name (sym),
27985796c8dcSSimon Schubert FALSE, FALSE, TRUE);
27995796c8dcSSimon Schubert else
28005796c8dcSSimon Schubert h = bfd_link_hash_lookup (info->hash,
28015796c8dcSSimon Schubert bfd_asymbol_name (sym),
28025796c8dcSSimon Schubert FALSE, FALSE, TRUE);
28035796c8dcSSimon Schubert if (h != NULL)
28045796c8dcSSimon Schubert set_symbol_from_hash (sym, h);
28055796c8dcSSimon Schubert }
28065796c8dcSSimon Schubert }
28075796c8dcSSimon Schubert }
28085796c8dcSSimon Schubert
28095796c8dcSSimon Schubert if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
28105796c8dcSSimon Schubert && input_section->size != 0)
28115796c8dcSSimon Schubert {
28125796c8dcSSimon Schubert /* Group section contents are set by bfd_elf_set_group_contents. */
28135796c8dcSSimon Schubert if (!output_bfd->output_has_begun)
28145796c8dcSSimon Schubert {
28155796c8dcSSimon Schubert /* FIXME: This hack ensures bfd_elf_set_group_contents is called. */
28165796c8dcSSimon Schubert if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
28175796c8dcSSimon Schubert goto error_return;
28185796c8dcSSimon Schubert }
28195796c8dcSSimon Schubert new_contents = output_section->contents;
28205796c8dcSSimon Schubert BFD_ASSERT (new_contents != NULL);
28215796c8dcSSimon Schubert BFD_ASSERT (input_section->output_offset == 0);
28225796c8dcSSimon Schubert }
28235796c8dcSSimon Schubert else
28245796c8dcSSimon Schubert {
28255796c8dcSSimon Schubert /* Get and relocate the section contents. */
28265796c8dcSSimon Schubert sec_size = (input_section->rawsize > input_section->size
28275796c8dcSSimon Schubert ? input_section->rawsize
28285796c8dcSSimon Schubert : input_section->size);
28295796c8dcSSimon Schubert contents = (bfd_byte *) bfd_malloc (sec_size);
28305796c8dcSSimon Schubert if (contents == NULL && sec_size != 0)
28315796c8dcSSimon Schubert goto error_return;
28325796c8dcSSimon Schubert new_contents = (bfd_get_relocated_section_contents
28335796c8dcSSimon Schubert (output_bfd, info, link_order, contents,
28345796c8dcSSimon Schubert info->relocatable,
28355796c8dcSSimon Schubert _bfd_generic_link_get_symbols (input_bfd)));
28365796c8dcSSimon Schubert if (!new_contents)
28375796c8dcSSimon Schubert goto error_return;
28385796c8dcSSimon Schubert }
28395796c8dcSSimon Schubert
28405796c8dcSSimon Schubert /* Output the section contents. */
28415796c8dcSSimon Schubert loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
28425796c8dcSSimon Schubert if (! bfd_set_section_contents (output_bfd, output_section,
28435796c8dcSSimon Schubert new_contents, loc, input_section->size))
28445796c8dcSSimon Schubert goto error_return;
28455796c8dcSSimon Schubert
28465796c8dcSSimon Schubert if (contents != NULL)
28475796c8dcSSimon Schubert free (contents);
28485796c8dcSSimon Schubert return TRUE;
28495796c8dcSSimon Schubert
28505796c8dcSSimon Schubert error_return:
28515796c8dcSSimon Schubert if (contents != NULL)
28525796c8dcSSimon Schubert free (contents);
28535796c8dcSSimon Schubert return FALSE;
28545796c8dcSSimon Schubert }
28555796c8dcSSimon Schubert
28565796c8dcSSimon Schubert /* A little routine to count the number of relocs in a link_order
28575796c8dcSSimon Schubert list. */
28585796c8dcSSimon Schubert
28595796c8dcSSimon Schubert unsigned int
_bfd_count_link_order_relocs(struct bfd_link_order * link_order)28605796c8dcSSimon Schubert _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
28615796c8dcSSimon Schubert {
28625796c8dcSSimon Schubert register unsigned int c;
28635796c8dcSSimon Schubert register struct bfd_link_order *l;
28645796c8dcSSimon Schubert
28655796c8dcSSimon Schubert c = 0;
28665796c8dcSSimon Schubert for (l = link_order; l != NULL; l = l->next)
28675796c8dcSSimon Schubert {
28685796c8dcSSimon Schubert if (l->type == bfd_section_reloc_link_order
28695796c8dcSSimon Schubert || l->type == bfd_symbol_reloc_link_order)
28705796c8dcSSimon Schubert ++c;
28715796c8dcSSimon Schubert }
28725796c8dcSSimon Schubert
28735796c8dcSSimon Schubert return c;
28745796c8dcSSimon Schubert }
28755796c8dcSSimon Schubert
28765796c8dcSSimon Schubert /*
28775796c8dcSSimon Schubert FUNCTION
28785796c8dcSSimon Schubert bfd_link_split_section
28795796c8dcSSimon Schubert
28805796c8dcSSimon Schubert SYNOPSIS
28815796c8dcSSimon Schubert bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
28825796c8dcSSimon Schubert
28835796c8dcSSimon Schubert DESCRIPTION
28845796c8dcSSimon Schubert Return nonzero if @var{sec} should be split during a
28855796c8dcSSimon Schubert reloceatable or final link.
28865796c8dcSSimon Schubert
28875796c8dcSSimon Schubert .#define bfd_link_split_section(abfd, sec) \
28885796c8dcSSimon Schubert . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
28895796c8dcSSimon Schubert .
28905796c8dcSSimon Schubert
28915796c8dcSSimon Schubert */
28925796c8dcSSimon Schubert
28935796c8dcSSimon Schubert bfd_boolean
_bfd_generic_link_split_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)28945796c8dcSSimon Schubert _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
28955796c8dcSSimon Schubert asection *sec ATTRIBUTE_UNUSED)
28965796c8dcSSimon Schubert {
28975796c8dcSSimon Schubert return FALSE;
28985796c8dcSSimon Schubert }
28995796c8dcSSimon Schubert
29005796c8dcSSimon Schubert /*
29015796c8dcSSimon Schubert FUNCTION
29025796c8dcSSimon Schubert bfd_section_already_linked
29035796c8dcSSimon Schubert
29045796c8dcSSimon Schubert SYNOPSIS
2905a45ae5f8SJohn Marino bfd_boolean bfd_section_already_linked (bfd *abfd,
2906a45ae5f8SJohn Marino asection *sec,
29075796c8dcSSimon Schubert struct bfd_link_info *info);
29085796c8dcSSimon Schubert
29095796c8dcSSimon Schubert DESCRIPTION
2910a45ae5f8SJohn Marino Check if @var{data} has been already linked during a reloceatable
2911a45ae5f8SJohn Marino or final link. Return TRUE if it has.
29125796c8dcSSimon Schubert
29135796c8dcSSimon Schubert .#define bfd_section_already_linked(abfd, sec, info) \
29145796c8dcSSimon Schubert . BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
29155796c8dcSSimon Schubert .
29165796c8dcSSimon Schubert
29175796c8dcSSimon Schubert */
29185796c8dcSSimon Schubert
29195796c8dcSSimon Schubert /* Sections marked with the SEC_LINK_ONCE flag should only be linked
29205796c8dcSSimon Schubert once into the output. This routine checks each section, and
29215796c8dcSSimon Schubert arrange to discard it if a section of the same name has already
29225796c8dcSSimon Schubert been linked. This code assumes that all relevant sections have the
29235796c8dcSSimon Schubert SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
29245796c8dcSSimon Schubert section name. bfd_section_already_linked is called via
29255796c8dcSSimon Schubert bfd_map_over_sections. */
29265796c8dcSSimon Schubert
29275796c8dcSSimon Schubert /* The hash table. */
29285796c8dcSSimon Schubert
29295796c8dcSSimon Schubert static struct bfd_hash_table _bfd_section_already_linked_table;
29305796c8dcSSimon Schubert
29315796c8dcSSimon Schubert /* Support routines for the hash table used by section_already_linked,
29325796c8dcSSimon Schubert initialize the table, traverse, lookup, fill in an entry and remove
29335796c8dcSSimon Schubert the table. */
29345796c8dcSSimon Schubert
29355796c8dcSSimon Schubert void
bfd_section_already_linked_table_traverse(bfd_boolean (* func)(struct bfd_section_already_linked_hash_entry *,void *),void * info)29365796c8dcSSimon Schubert bfd_section_already_linked_table_traverse
29375796c8dcSSimon Schubert (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
29385796c8dcSSimon Schubert void *), void *info)
29395796c8dcSSimon Schubert {
29405796c8dcSSimon Schubert bfd_hash_traverse (&_bfd_section_already_linked_table,
29415796c8dcSSimon Schubert (bfd_boolean (*) (struct bfd_hash_entry *,
29425796c8dcSSimon Schubert void *)) func,
29435796c8dcSSimon Schubert info);
29445796c8dcSSimon Schubert }
29455796c8dcSSimon Schubert
29465796c8dcSSimon Schubert struct bfd_section_already_linked_hash_entry *
bfd_section_already_linked_table_lookup(const char * name)29475796c8dcSSimon Schubert bfd_section_already_linked_table_lookup (const char *name)
29485796c8dcSSimon Schubert {
29495796c8dcSSimon Schubert return ((struct bfd_section_already_linked_hash_entry *)
29505796c8dcSSimon Schubert bfd_hash_lookup (&_bfd_section_already_linked_table, name,
29515796c8dcSSimon Schubert TRUE, FALSE));
29525796c8dcSSimon Schubert }
29535796c8dcSSimon Schubert
29545796c8dcSSimon Schubert bfd_boolean
bfd_section_already_linked_table_insert(struct bfd_section_already_linked_hash_entry * already_linked_list,asection * sec)29555796c8dcSSimon Schubert bfd_section_already_linked_table_insert
29565796c8dcSSimon Schubert (struct bfd_section_already_linked_hash_entry *already_linked_list,
29575796c8dcSSimon Schubert asection *sec)
29585796c8dcSSimon Schubert {
29595796c8dcSSimon Schubert struct bfd_section_already_linked *l;
29605796c8dcSSimon Schubert
29615796c8dcSSimon Schubert /* Allocate the memory from the same obstack as the hash table is
29625796c8dcSSimon Schubert kept in. */
29635796c8dcSSimon Schubert l = (struct bfd_section_already_linked *)
29645796c8dcSSimon Schubert bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
29655796c8dcSSimon Schubert if (l == NULL)
29665796c8dcSSimon Schubert return FALSE;
29675796c8dcSSimon Schubert l->sec = sec;
29685796c8dcSSimon Schubert l->next = already_linked_list->entry;
29695796c8dcSSimon Schubert already_linked_list->entry = l;
29705796c8dcSSimon Schubert return TRUE;
29715796c8dcSSimon Schubert }
29725796c8dcSSimon Schubert
29735796c8dcSSimon Schubert static struct bfd_hash_entry *
already_linked_newfunc(struct bfd_hash_entry * entry ATTRIBUTE_UNUSED,struct bfd_hash_table * table,const char * string ATTRIBUTE_UNUSED)29745796c8dcSSimon Schubert already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
29755796c8dcSSimon Schubert struct bfd_hash_table *table,
29765796c8dcSSimon Schubert const char *string ATTRIBUTE_UNUSED)
29775796c8dcSSimon Schubert {
29785796c8dcSSimon Schubert struct bfd_section_already_linked_hash_entry *ret =
29795796c8dcSSimon Schubert (struct bfd_section_already_linked_hash_entry *)
29805796c8dcSSimon Schubert bfd_hash_allocate (table, sizeof *ret);
29815796c8dcSSimon Schubert
29825796c8dcSSimon Schubert if (ret == NULL)
29835796c8dcSSimon Schubert return NULL;
29845796c8dcSSimon Schubert
29855796c8dcSSimon Schubert ret->entry = NULL;
29865796c8dcSSimon Schubert
29875796c8dcSSimon Schubert return &ret->root;
29885796c8dcSSimon Schubert }
29895796c8dcSSimon Schubert
29905796c8dcSSimon Schubert bfd_boolean
bfd_section_already_linked_table_init(void)29915796c8dcSSimon Schubert bfd_section_already_linked_table_init (void)
29925796c8dcSSimon Schubert {
29935796c8dcSSimon Schubert return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
29945796c8dcSSimon Schubert already_linked_newfunc,
29955796c8dcSSimon Schubert sizeof (struct bfd_section_already_linked_hash_entry),
29965796c8dcSSimon Schubert 42);
29975796c8dcSSimon Schubert }
29985796c8dcSSimon Schubert
29995796c8dcSSimon Schubert void
bfd_section_already_linked_table_free(void)30005796c8dcSSimon Schubert bfd_section_already_linked_table_free (void)
30015796c8dcSSimon Schubert {
30025796c8dcSSimon Schubert bfd_hash_table_free (&_bfd_section_already_linked_table);
30035796c8dcSSimon Schubert }
30045796c8dcSSimon Schubert
3005a45ae5f8SJohn Marino /* Report warnings as appropriate for duplicate section SEC.
3006a45ae5f8SJohn Marino Return FALSE if we decide to keep SEC after all. */
30075796c8dcSSimon Schubert
3008a45ae5f8SJohn Marino bfd_boolean
_bfd_handle_already_linked(asection * sec,struct bfd_section_already_linked * l,struct bfd_link_info * info)3009a45ae5f8SJohn Marino _bfd_handle_already_linked (asection *sec,
3010a45ae5f8SJohn Marino struct bfd_section_already_linked *l,
30115796c8dcSSimon Schubert struct bfd_link_info *info)
30125796c8dcSSimon Schubert {
3013a45ae5f8SJohn Marino switch (sec->flags & SEC_LINK_DUPLICATES)
3014a45ae5f8SJohn Marino {
3015a45ae5f8SJohn Marino default:
3016a45ae5f8SJohn Marino abort ();
3017a45ae5f8SJohn Marino
3018a45ae5f8SJohn Marino case SEC_LINK_DUPLICATES_DISCARD:
3019a45ae5f8SJohn Marino /* If we found an LTO IR match for this comdat group on
3020a45ae5f8SJohn Marino the first pass, replace it with the LTO output on the
3021a45ae5f8SJohn Marino second pass. We can't simply choose real object
3022a45ae5f8SJohn Marino files over IR because the first pass may contain a
3023a45ae5f8SJohn Marino mix of LTO and normal objects and we must keep the
3024a45ae5f8SJohn Marino first match, be it IR or real. */
3025a45ae5f8SJohn Marino if (info->loading_lto_outputs
3026a45ae5f8SJohn Marino && (l->sec->owner->flags & BFD_PLUGIN) != 0)
3027a45ae5f8SJohn Marino {
3028a45ae5f8SJohn Marino l->sec = sec;
3029a45ae5f8SJohn Marino return FALSE;
3030a45ae5f8SJohn Marino }
3031a45ae5f8SJohn Marino break;
3032a45ae5f8SJohn Marino
3033a45ae5f8SJohn Marino case SEC_LINK_DUPLICATES_ONE_ONLY:
3034a45ae5f8SJohn Marino info->callbacks->einfo
3035a45ae5f8SJohn Marino (_("%B: ignoring duplicate section `%A'\n"),
3036a45ae5f8SJohn Marino sec->owner, sec);
3037a45ae5f8SJohn Marino break;
3038a45ae5f8SJohn Marino
3039a45ae5f8SJohn Marino case SEC_LINK_DUPLICATES_SAME_SIZE:
3040a45ae5f8SJohn Marino if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3041a45ae5f8SJohn Marino ;
3042a45ae5f8SJohn Marino else if (sec->size != l->sec->size)
3043a45ae5f8SJohn Marino info->callbacks->einfo
3044a45ae5f8SJohn Marino (_("%B: duplicate section `%A' has different size\n"),
3045a45ae5f8SJohn Marino sec->owner, sec);
3046a45ae5f8SJohn Marino break;
3047a45ae5f8SJohn Marino
3048a45ae5f8SJohn Marino case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3049a45ae5f8SJohn Marino if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3050a45ae5f8SJohn Marino ;
3051a45ae5f8SJohn Marino else if (sec->size != l->sec->size)
3052a45ae5f8SJohn Marino info->callbacks->einfo
3053a45ae5f8SJohn Marino (_("%B: duplicate section `%A' has different size\n"),
3054a45ae5f8SJohn Marino sec->owner, sec);
3055a45ae5f8SJohn Marino else if (sec->size != 0)
3056a45ae5f8SJohn Marino {
3057a45ae5f8SJohn Marino bfd_byte *sec_contents, *l_sec_contents = NULL;
3058a45ae5f8SJohn Marino
3059a45ae5f8SJohn Marino if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
3060a45ae5f8SJohn Marino info->callbacks->einfo
3061a45ae5f8SJohn Marino (_("%B: could not read contents of section `%A'\n"),
3062a45ae5f8SJohn Marino sec->owner, sec);
3063a45ae5f8SJohn Marino else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
3064a45ae5f8SJohn Marino &l_sec_contents))
3065a45ae5f8SJohn Marino info->callbacks->einfo
3066a45ae5f8SJohn Marino (_("%B: could not read contents of section `%A'\n"),
3067a45ae5f8SJohn Marino l->sec->owner, l->sec);
3068a45ae5f8SJohn Marino else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
3069a45ae5f8SJohn Marino info->callbacks->einfo
3070a45ae5f8SJohn Marino (_("%B: duplicate section `%A' has different contents\n"),
3071a45ae5f8SJohn Marino sec->owner, sec);
3072a45ae5f8SJohn Marino
3073a45ae5f8SJohn Marino if (sec_contents)
3074a45ae5f8SJohn Marino free (sec_contents);
3075a45ae5f8SJohn Marino if (l_sec_contents)
3076a45ae5f8SJohn Marino free (l_sec_contents);
3077a45ae5f8SJohn Marino }
3078a45ae5f8SJohn Marino break;
3079a45ae5f8SJohn Marino }
3080a45ae5f8SJohn Marino
3081a45ae5f8SJohn Marino /* Set the output_section field so that lang_add_section
3082a45ae5f8SJohn Marino does not create a lang_input_section structure for this
3083a45ae5f8SJohn Marino section. Since there might be a symbol in the section
3084a45ae5f8SJohn Marino being discarded, we must retain a pointer to the section
3085a45ae5f8SJohn Marino which we are really going to use. */
3086a45ae5f8SJohn Marino sec->output_section = bfd_abs_section_ptr;
3087a45ae5f8SJohn Marino sec->kept_section = l->sec;
3088a45ae5f8SJohn Marino return TRUE;
3089a45ae5f8SJohn Marino }
3090a45ae5f8SJohn Marino
3091a45ae5f8SJohn Marino /* This is used on non-ELF inputs. */
3092a45ae5f8SJohn Marino
3093a45ae5f8SJohn Marino bfd_boolean
_bfd_generic_section_already_linked(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_info * info)3094a45ae5f8SJohn Marino _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
3095a45ae5f8SJohn Marino asection *sec,
3096a45ae5f8SJohn Marino struct bfd_link_info *info)
3097a45ae5f8SJohn Marino {
30985796c8dcSSimon Schubert const char *name;
30995796c8dcSSimon Schubert struct bfd_section_already_linked *l;
31005796c8dcSSimon Schubert struct bfd_section_already_linked_hash_entry *already_linked_list;
31015796c8dcSSimon Schubert
3102a45ae5f8SJohn Marino if ((sec->flags & SEC_LINK_ONCE) == 0)
3103a45ae5f8SJohn Marino return FALSE;
3104a45ae5f8SJohn Marino
3105a45ae5f8SJohn Marino /* The generic linker doesn't handle section groups. */
3106a45ae5f8SJohn Marino if ((sec->flags & SEC_GROUP) != 0)
3107a45ae5f8SJohn Marino return FALSE;
31085796c8dcSSimon Schubert
31095796c8dcSSimon Schubert /* FIXME: When doing a relocatable link, we may have trouble
31105796c8dcSSimon Schubert copying relocations in other sections that refer to local symbols
31115796c8dcSSimon Schubert in the section being discarded. Those relocations will have to
31125796c8dcSSimon Schubert be converted somehow; as of this writing I'm not sure that any of
31135796c8dcSSimon Schubert the backends handle that correctly.
31145796c8dcSSimon Schubert
31155796c8dcSSimon Schubert It is tempting to instead not discard link once sections when
31165796c8dcSSimon Schubert doing a relocatable link (technically, they should be discarded
31175796c8dcSSimon Schubert whenever we are building constructors). However, that fails,
31185796c8dcSSimon Schubert because the linker winds up combining all the link once sections
31195796c8dcSSimon Schubert into a single large link once section, which defeats the purpose
31205796c8dcSSimon Schubert of having link once sections in the first place. */
31215796c8dcSSimon Schubert
31225796c8dcSSimon Schubert name = bfd_get_section_name (abfd, sec);
31235796c8dcSSimon Schubert
31245796c8dcSSimon Schubert already_linked_list = bfd_section_already_linked_table_lookup (name);
31255796c8dcSSimon Schubert
3126a45ae5f8SJohn Marino l = already_linked_list->entry;
3127a45ae5f8SJohn Marino if (l != NULL)
31285796c8dcSSimon Schubert {
31295796c8dcSSimon Schubert /* The section has already been linked. See if we should
31305796c8dcSSimon Schubert issue a warning. */
3131a45ae5f8SJohn Marino return _bfd_handle_already_linked (sec, l, info);
31325796c8dcSSimon Schubert }
31335796c8dcSSimon Schubert
31345796c8dcSSimon Schubert /* This is the first section with this name. Record it. */
31355796c8dcSSimon Schubert if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
31365796c8dcSSimon Schubert info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
3137a45ae5f8SJohn Marino return FALSE;
31385796c8dcSSimon Schubert }
31395796c8dcSSimon Schubert
3140*ef5ccd6cSJohn Marino /* Choose a neighbouring section to S in OBFD that will be output, or
3141*ef5ccd6cSJohn Marino the absolute section if ADDR is out of bounds of the neighbours. */
3142*ef5ccd6cSJohn Marino
3143*ef5ccd6cSJohn Marino asection *
_bfd_nearby_section(bfd * obfd,asection * s,bfd_vma addr)3144*ef5ccd6cSJohn Marino _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3145*ef5ccd6cSJohn Marino {
3146*ef5ccd6cSJohn Marino asection *next, *prev, *best;
3147*ef5ccd6cSJohn Marino
3148*ef5ccd6cSJohn Marino /* Find preceding kept section. */
3149*ef5ccd6cSJohn Marino for (prev = s->prev; prev != NULL; prev = prev->prev)
3150*ef5ccd6cSJohn Marino if ((prev->flags & SEC_EXCLUDE) == 0
3151*ef5ccd6cSJohn Marino && !bfd_section_removed_from_list (obfd, prev))
3152*ef5ccd6cSJohn Marino break;
3153*ef5ccd6cSJohn Marino
3154*ef5ccd6cSJohn Marino /* Find following kept section. Start at prev->next because
3155*ef5ccd6cSJohn Marino other sections may have been added after S was removed. */
3156*ef5ccd6cSJohn Marino if (s->prev != NULL)
3157*ef5ccd6cSJohn Marino next = s->prev->next;
3158*ef5ccd6cSJohn Marino else
3159*ef5ccd6cSJohn Marino next = s->owner->sections;
3160*ef5ccd6cSJohn Marino for (; next != NULL; next = next->next)
3161*ef5ccd6cSJohn Marino if ((next->flags & SEC_EXCLUDE) == 0
3162*ef5ccd6cSJohn Marino && !bfd_section_removed_from_list (obfd, next))
3163*ef5ccd6cSJohn Marino break;
3164*ef5ccd6cSJohn Marino
3165*ef5ccd6cSJohn Marino /* Choose better of two sections, based on flags. The idea
3166*ef5ccd6cSJohn Marino is to choose a section that will be in the same segment
3167*ef5ccd6cSJohn Marino as S would have been if it was kept. */
3168*ef5ccd6cSJohn Marino best = next;
3169*ef5ccd6cSJohn Marino if (prev == NULL)
3170*ef5ccd6cSJohn Marino {
3171*ef5ccd6cSJohn Marino if (next == NULL)
3172*ef5ccd6cSJohn Marino best = bfd_abs_section_ptr;
3173*ef5ccd6cSJohn Marino }
3174*ef5ccd6cSJohn Marino else if (next == NULL)
3175*ef5ccd6cSJohn Marino best = prev;
3176*ef5ccd6cSJohn Marino else if (((prev->flags ^ next->flags)
3177*ef5ccd6cSJohn Marino & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3178*ef5ccd6cSJohn Marino {
3179*ef5ccd6cSJohn Marino if (((next->flags ^ s->flags)
3180*ef5ccd6cSJohn Marino & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3181*ef5ccd6cSJohn Marino /* We prefer to choose a loaded section. Section S
3182*ef5ccd6cSJohn Marino doesn't have SEC_LOAD set (it being excluded, that
3183*ef5ccd6cSJohn Marino part of the flag processing didn't happen) so we
3184*ef5ccd6cSJohn Marino can't compare that flag to those of NEXT and PREV. */
3185*ef5ccd6cSJohn Marino || ((prev->flags & SEC_LOAD) != 0
3186*ef5ccd6cSJohn Marino && (next->flags & SEC_LOAD) == 0))
3187*ef5ccd6cSJohn Marino best = prev;
3188*ef5ccd6cSJohn Marino }
3189*ef5ccd6cSJohn Marino else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3190*ef5ccd6cSJohn Marino {
3191*ef5ccd6cSJohn Marino if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3192*ef5ccd6cSJohn Marino best = prev;
3193*ef5ccd6cSJohn Marino }
3194*ef5ccd6cSJohn Marino else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3195*ef5ccd6cSJohn Marino {
3196*ef5ccd6cSJohn Marino if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3197*ef5ccd6cSJohn Marino best = prev;
3198*ef5ccd6cSJohn Marino }
3199*ef5ccd6cSJohn Marino else
3200*ef5ccd6cSJohn Marino {
3201*ef5ccd6cSJohn Marino /* Flags we care about are the same. Prefer the following
3202*ef5ccd6cSJohn Marino section if that will result in a positive valued sym. */
3203*ef5ccd6cSJohn Marino if (addr < next->vma)
3204*ef5ccd6cSJohn Marino best = prev;
3205*ef5ccd6cSJohn Marino }
3206*ef5ccd6cSJohn Marino
3207*ef5ccd6cSJohn Marino return best;
3208*ef5ccd6cSJohn Marino }
3209*ef5ccd6cSJohn Marino
32105796c8dcSSimon Schubert /* Convert symbols in excluded output sections to use a kept section. */
32115796c8dcSSimon Schubert
32125796c8dcSSimon Schubert static bfd_boolean
fix_syms(struct bfd_link_hash_entry * h,void * data)32135796c8dcSSimon Schubert fix_syms (struct bfd_link_hash_entry *h, void *data)
32145796c8dcSSimon Schubert {
32155796c8dcSSimon Schubert bfd *obfd = (bfd *) data;
32165796c8dcSSimon Schubert
32175796c8dcSSimon Schubert if (h->type == bfd_link_hash_defined
32185796c8dcSSimon Schubert || h->type == bfd_link_hash_defweak)
32195796c8dcSSimon Schubert {
32205796c8dcSSimon Schubert asection *s = h->u.def.section;
32215796c8dcSSimon Schubert if (s != NULL
32225796c8dcSSimon Schubert && s->output_section != NULL
32235796c8dcSSimon Schubert && (s->output_section->flags & SEC_EXCLUDE) != 0
32245796c8dcSSimon Schubert && bfd_section_removed_from_list (obfd, s->output_section))
32255796c8dcSSimon Schubert {
3226*ef5ccd6cSJohn Marino asection *op;
32275796c8dcSSimon Schubert
32285796c8dcSSimon Schubert h->u.def.value += s->output_offset + s->output_section->vma;
3229*ef5ccd6cSJohn Marino op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
32305796c8dcSSimon Schubert h->u.def.value -= op->vma;
32315796c8dcSSimon Schubert h->u.def.section = op;
32325796c8dcSSimon Schubert }
32335796c8dcSSimon Schubert }
32345796c8dcSSimon Schubert
32355796c8dcSSimon Schubert return TRUE;
32365796c8dcSSimon Schubert }
32375796c8dcSSimon Schubert
32385796c8dcSSimon Schubert void
_bfd_fix_excluded_sec_syms(bfd * obfd,struct bfd_link_info * info)32395796c8dcSSimon Schubert _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
32405796c8dcSSimon Schubert {
32415796c8dcSSimon Schubert bfd_link_hash_traverse (info->hash, fix_syms, obfd);
32425796c8dcSSimon Schubert }
32435796c8dcSSimon Schubert
32445796c8dcSSimon Schubert /*
32455796c8dcSSimon Schubert FUNCTION
32465796c8dcSSimon Schubert bfd_generic_define_common_symbol
32475796c8dcSSimon Schubert
32485796c8dcSSimon Schubert SYNOPSIS
32495796c8dcSSimon Schubert bfd_boolean bfd_generic_define_common_symbol
32505796c8dcSSimon Schubert (bfd *output_bfd, struct bfd_link_info *info,
32515796c8dcSSimon Schubert struct bfd_link_hash_entry *h);
32525796c8dcSSimon Schubert
32535796c8dcSSimon Schubert DESCRIPTION
32545796c8dcSSimon Schubert Convert common symbol @var{h} into a defined symbol.
32555796c8dcSSimon Schubert Return TRUE on success and FALSE on failure.
32565796c8dcSSimon Schubert
32575796c8dcSSimon Schubert .#define bfd_define_common_symbol(output_bfd, info, h) \
32585796c8dcSSimon Schubert . BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
32595796c8dcSSimon Schubert .
32605796c8dcSSimon Schubert */
32615796c8dcSSimon Schubert
32625796c8dcSSimon Schubert bfd_boolean
bfd_generic_define_common_symbol(bfd * output_bfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * h)32635796c8dcSSimon Schubert bfd_generic_define_common_symbol (bfd *output_bfd,
32645796c8dcSSimon Schubert struct bfd_link_info *info ATTRIBUTE_UNUSED,
32655796c8dcSSimon Schubert struct bfd_link_hash_entry *h)
32665796c8dcSSimon Schubert {
32675796c8dcSSimon Schubert unsigned int power_of_two;
32685796c8dcSSimon Schubert bfd_vma alignment, size;
32695796c8dcSSimon Schubert asection *section;
32705796c8dcSSimon Schubert
32715796c8dcSSimon Schubert BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
32725796c8dcSSimon Schubert
32735796c8dcSSimon Schubert size = h->u.c.size;
32745796c8dcSSimon Schubert power_of_two = h->u.c.p->alignment_power;
32755796c8dcSSimon Schubert section = h->u.c.p->section;
32765796c8dcSSimon Schubert
32775796c8dcSSimon Schubert /* Increase the size of the section to align the common symbol.
32785796c8dcSSimon Schubert The alignment must be a power of two. */
32795796c8dcSSimon Schubert alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
32805796c8dcSSimon Schubert BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
32815796c8dcSSimon Schubert section->size += alignment - 1;
32825796c8dcSSimon Schubert section->size &= -alignment;
32835796c8dcSSimon Schubert
32845796c8dcSSimon Schubert /* Adjust the section's overall alignment if necessary. */
32855796c8dcSSimon Schubert if (power_of_two > section->alignment_power)
32865796c8dcSSimon Schubert section->alignment_power = power_of_two;
32875796c8dcSSimon Schubert
32885796c8dcSSimon Schubert /* Change the symbol from common to defined. */
32895796c8dcSSimon Schubert h->type = bfd_link_hash_defined;
32905796c8dcSSimon Schubert h->u.def.section = section;
32915796c8dcSSimon Schubert h->u.def.value = section->size;
32925796c8dcSSimon Schubert
32935796c8dcSSimon Schubert /* Increase the size of the section. */
32945796c8dcSSimon Schubert section->size += size;
32955796c8dcSSimon Schubert
32965796c8dcSSimon Schubert /* Make sure the section is allocated in memory, and make sure that
32975796c8dcSSimon Schubert it is no longer a common section. */
32985796c8dcSSimon Schubert section->flags |= SEC_ALLOC;
32995796c8dcSSimon Schubert section->flags &= ~SEC_IS_COMMON;
33005796c8dcSSimon Schubert return TRUE;
33015796c8dcSSimon Schubert }
33025796c8dcSSimon Schubert
33035796c8dcSSimon Schubert /*
33045796c8dcSSimon Schubert FUNCTION
33055796c8dcSSimon Schubert bfd_find_version_for_sym
33065796c8dcSSimon Schubert
33075796c8dcSSimon Schubert SYNOPSIS
33085796c8dcSSimon Schubert struct bfd_elf_version_tree * bfd_find_version_for_sym
33095796c8dcSSimon Schubert (struct bfd_elf_version_tree *verdefs,
33105796c8dcSSimon Schubert const char *sym_name, bfd_boolean *hide);
33115796c8dcSSimon Schubert
33125796c8dcSSimon Schubert DESCRIPTION
33135796c8dcSSimon Schubert Search an elf version script tree for symbol versioning
33145796c8dcSSimon Schubert info and export / don't-export status for a given symbol.
33155796c8dcSSimon Schubert Return non-NULL on success and NULL on failure; also sets
33165796c8dcSSimon Schubert the output @samp{hide} boolean parameter.
33175796c8dcSSimon Schubert
33185796c8dcSSimon Schubert */
33195796c8dcSSimon Schubert
33205796c8dcSSimon Schubert struct bfd_elf_version_tree *
bfd_find_version_for_sym(struct bfd_elf_version_tree * verdefs,const char * sym_name,bfd_boolean * hide)33215796c8dcSSimon Schubert bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
33225796c8dcSSimon Schubert const char *sym_name,
33235796c8dcSSimon Schubert bfd_boolean *hide)
33245796c8dcSSimon Schubert {
33255796c8dcSSimon Schubert struct bfd_elf_version_tree *t;
33265796c8dcSSimon Schubert struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
33275796c8dcSSimon Schubert struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
33285796c8dcSSimon Schubert
33295796c8dcSSimon Schubert local_ver = NULL;
33305796c8dcSSimon Schubert global_ver = NULL;
33315796c8dcSSimon Schubert star_local_ver = NULL;
33325796c8dcSSimon Schubert star_global_ver = NULL;
33335796c8dcSSimon Schubert exist_ver = NULL;
33345796c8dcSSimon Schubert for (t = verdefs; t != NULL; t = t->next)
33355796c8dcSSimon Schubert {
33365796c8dcSSimon Schubert if (t->globals.list != NULL)
33375796c8dcSSimon Schubert {
33385796c8dcSSimon Schubert struct bfd_elf_version_expr *d = NULL;
33395796c8dcSSimon Schubert
33405796c8dcSSimon Schubert while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
33415796c8dcSSimon Schubert {
33425796c8dcSSimon Schubert if (d->literal || strcmp (d->pattern, "*") != 0)
33435796c8dcSSimon Schubert global_ver = t;
33445796c8dcSSimon Schubert else
33455796c8dcSSimon Schubert star_global_ver = t;
33465796c8dcSSimon Schubert if (d->symver)
33475796c8dcSSimon Schubert exist_ver = t;
33485796c8dcSSimon Schubert d->script = 1;
33495796c8dcSSimon Schubert /* If the match is a wildcard pattern, keep looking for
33505796c8dcSSimon Schubert a more explicit, perhaps even local, match. */
33515796c8dcSSimon Schubert if (d->literal)
33525796c8dcSSimon Schubert break;
33535796c8dcSSimon Schubert }
33545796c8dcSSimon Schubert
33555796c8dcSSimon Schubert if (d != NULL)
33565796c8dcSSimon Schubert break;
33575796c8dcSSimon Schubert }
33585796c8dcSSimon Schubert
33595796c8dcSSimon Schubert if (t->locals.list != NULL)
33605796c8dcSSimon Schubert {
33615796c8dcSSimon Schubert struct bfd_elf_version_expr *d = NULL;
33625796c8dcSSimon Schubert
33635796c8dcSSimon Schubert while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
33645796c8dcSSimon Schubert {
33655796c8dcSSimon Schubert if (d->literal || strcmp (d->pattern, "*") != 0)
33665796c8dcSSimon Schubert local_ver = t;
33675796c8dcSSimon Schubert else
33685796c8dcSSimon Schubert star_local_ver = t;
33695796c8dcSSimon Schubert /* If the match is a wildcard pattern, keep looking for
33705796c8dcSSimon Schubert a more explicit, perhaps even global, match. */
33715796c8dcSSimon Schubert if (d->literal)
33725796c8dcSSimon Schubert {
33735796c8dcSSimon Schubert /* An exact match overrides a global wildcard. */
33745796c8dcSSimon Schubert global_ver = NULL;
33755796c8dcSSimon Schubert star_global_ver = NULL;
33765796c8dcSSimon Schubert break;
33775796c8dcSSimon Schubert }
33785796c8dcSSimon Schubert }
33795796c8dcSSimon Schubert
33805796c8dcSSimon Schubert if (d != NULL)
33815796c8dcSSimon Schubert break;
33825796c8dcSSimon Schubert }
33835796c8dcSSimon Schubert }
33845796c8dcSSimon Schubert
33855796c8dcSSimon Schubert if (global_ver == NULL && local_ver == NULL)
33865796c8dcSSimon Schubert global_ver = star_global_ver;
33875796c8dcSSimon Schubert
33885796c8dcSSimon Schubert if (global_ver != NULL)
33895796c8dcSSimon Schubert {
33905796c8dcSSimon Schubert /* If we already have a versioned symbol that matches the
33915796c8dcSSimon Schubert node for this symbol, then we don't want to create a
33925796c8dcSSimon Schubert duplicate from the unversioned symbol. Instead hide the
33935796c8dcSSimon Schubert unversioned symbol. */
33945796c8dcSSimon Schubert *hide = exist_ver == global_ver;
33955796c8dcSSimon Schubert return global_ver;
33965796c8dcSSimon Schubert }
33975796c8dcSSimon Schubert
33985796c8dcSSimon Schubert if (local_ver == NULL)
33995796c8dcSSimon Schubert local_ver = star_local_ver;
34005796c8dcSSimon Schubert
34015796c8dcSSimon Schubert if (local_ver != NULL)
34025796c8dcSSimon Schubert {
34035796c8dcSSimon Schubert *hide = TRUE;
34045796c8dcSSimon Schubert return local_ver;
34055796c8dcSSimon Schubert }
34065796c8dcSSimon Schubert
34075796c8dcSSimon Schubert return NULL;
34085796c8dcSSimon Schubert }
3409a45ae5f8SJohn Marino
3410a45ae5f8SJohn Marino /*
3411a45ae5f8SJohn Marino FUNCTION
3412a45ae5f8SJohn Marino bfd_hide_sym_by_version
3413a45ae5f8SJohn Marino
3414a45ae5f8SJohn Marino SYNOPSIS
3415a45ae5f8SJohn Marino bfd_boolean bfd_hide_sym_by_version
3416a45ae5f8SJohn Marino (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3417a45ae5f8SJohn Marino
3418a45ae5f8SJohn Marino DESCRIPTION
3419a45ae5f8SJohn Marino Search an elf version script tree for symbol versioning
3420a45ae5f8SJohn Marino info for a given symbol. Return TRUE if the symbol is hidden.
3421a45ae5f8SJohn Marino
3422a45ae5f8SJohn Marino */
3423a45ae5f8SJohn Marino
3424a45ae5f8SJohn Marino bfd_boolean
bfd_hide_sym_by_version(struct bfd_elf_version_tree * verdefs,const char * sym_name)3425a45ae5f8SJohn Marino bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3426a45ae5f8SJohn Marino const char *sym_name)
3427a45ae5f8SJohn Marino {
3428a45ae5f8SJohn Marino bfd_boolean hidden = FALSE;
3429a45ae5f8SJohn Marino bfd_find_version_for_sym (verdefs, sym_name, &hidden);
3430a45ae5f8SJohn Marino return hidden;
3431a45ae5f8SJohn Marino }
3432