xref: /openbsd-src/gnu/usr.bin/binutils/bfd/linker.c (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* linker.c -- BFD linker routines
2*007c2a45Smiod    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3*007c2a45Smiod    2003, 2004 Free Software Foundation, Inc.
42159047fSniklas    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
52159047fSniklas 
62159047fSniklas    This file is part of BFD, the Binary File Descriptor library.
72159047fSniklas 
82159047fSniklas    This program is free software; you can redistribute it and/or modify
92159047fSniklas    it under the terms of the GNU General Public License as published by
102159047fSniklas    the Free Software Foundation; either version 2 of the License, or
112159047fSniklas    (at your option) any later version.
122159047fSniklas 
132159047fSniklas    This program is distributed in the hope that it will be useful,
142159047fSniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162159047fSniklas    GNU General Public License for more details.
172159047fSniklas 
182159047fSniklas    You should have received a copy of the GNU General Public License
192159047fSniklas    along with this program; if not, write to the Free Software
202159047fSniklas    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
212159047fSniklas 
222159047fSniklas #include "bfd.h"
232159047fSniklas #include "sysdep.h"
242159047fSniklas #include "libbfd.h"
252159047fSniklas #include "bfdlink.h"
262159047fSniklas #include "genlink.h"
272159047fSniklas 
282159047fSniklas /*
292159047fSniklas SECTION
302159047fSniklas 	Linker Functions
312159047fSniklas 
322159047fSniklas @cindex Linker
332159047fSniklas 	The linker uses three special entry points in the BFD target
342159047fSniklas 	vector.  It is not necessary to write special routines for
352159047fSniklas 	these entry points when creating a new BFD back end, since
362159047fSniklas 	generic versions are provided.  However, writing them can
372159047fSniklas 	speed up linking and make it use significantly less runtime
382159047fSniklas 	memory.
392159047fSniklas 
402159047fSniklas 	The first routine creates a hash table used by the other
412159047fSniklas 	routines.  The second routine adds the symbols from an object
422159047fSniklas 	file to the hash table.  The third routine takes all the
432159047fSniklas 	object files and links them together to create the output
442159047fSniklas 	file.  These routines are designed so that the linker proper
452159047fSniklas 	does not need to know anything about the symbols in the object
462159047fSniklas 	files that it is linking.  The linker merely arranges the
472159047fSniklas 	sections as directed by the linker script and lets BFD handle
482159047fSniklas 	the details of symbols and relocs.
492159047fSniklas 
502159047fSniklas 	The second routine and third routines are passed a pointer to
512159047fSniklas 	a <<struct bfd_link_info>> structure (defined in
522159047fSniklas 	<<bfdlink.h>>) which holds information relevant to the link,
532159047fSniklas 	including the linker hash table (which was created by the
542159047fSniklas 	first routine) and a set of callback functions to the linker
552159047fSniklas 	proper.
562159047fSniklas 
572159047fSniklas 	The generic linker routines are in <<linker.c>>, and use the
582159047fSniklas 	header file <<genlink.h>>.  As of this writing, the only back
592159047fSniklas 	ends which have implemented versions of these routines are
602159047fSniklas 	a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
612159047fSniklas 	routines are used as examples throughout this section.
622159047fSniklas 
632159047fSniklas @menu
642159047fSniklas @* Creating a Linker Hash Table::
652159047fSniklas @* Adding Symbols to the Hash Table::
662159047fSniklas @* Performing the Final Link::
672159047fSniklas @end menu
682159047fSniklas 
692159047fSniklas INODE
702159047fSniklas Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
712159047fSniklas SUBSECTION
722159047fSniklas 	Creating a linker hash table
732159047fSniklas 
742159047fSniklas @cindex _bfd_link_hash_table_create in target vector
752159047fSniklas @cindex target vector (_bfd_link_hash_table_create)
762159047fSniklas 	The linker routines must create a hash table, which must be
772159047fSniklas 	derived from <<struct bfd_link_hash_table>> described in
78b305b0f1Sespie 	<<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
792159047fSniklas 	create a derived hash table.  This entry point is called using
802159047fSniklas 	the target vector of the linker output file.
812159047fSniklas 
822159047fSniklas 	The <<_bfd_link_hash_table_create>> entry point must allocate
832159047fSniklas 	and initialize an instance of the desired hash table.  If the
842159047fSniklas 	back end does not require any additional information to be
852159047fSniklas 	stored with the entries in the hash table, the entry point may
862159047fSniklas 	simply create a <<struct bfd_link_hash_table>>.  Most likely,
872159047fSniklas 	however, some additional information will be needed.
882159047fSniklas 
892159047fSniklas 	For example, with each entry in the hash table the a.out
902159047fSniklas 	linker keeps the index the symbol has in the final output file
91*007c2a45Smiod 	(this index number is used so that when doing a relocatable
922159047fSniklas 	link the symbol index used in the output file can be quickly
932159047fSniklas 	filled in when copying over a reloc).  The a.out linker code
942159047fSniklas 	defines the required structures and functions for a hash table
952159047fSniklas 	derived from <<struct bfd_link_hash_table>>.  The a.out linker
962159047fSniklas 	hash table is created by the function
972159047fSniklas 	<<NAME(aout,link_hash_table_create)>>; it simply allocates
982159047fSniklas 	space for the hash table, initializes it, and returns a
992159047fSniklas 	pointer to it.
1002159047fSniklas 
1012159047fSniklas 	When writing the linker routines for a new back end, you will
1022159047fSniklas 	generally not know exactly which fields will be required until
1032159047fSniklas 	you have finished.  You should simply create a new hash table
1042159047fSniklas 	which defines no additional fields, and then simply add fields
1052159047fSniklas 	as they become necessary.
1062159047fSniklas 
1072159047fSniklas INODE
1082159047fSniklas Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
1092159047fSniklas SUBSECTION
1102159047fSniklas 	Adding symbols to the hash table
1112159047fSniklas 
1122159047fSniklas @cindex _bfd_link_add_symbols in target vector
1132159047fSniklas @cindex target vector (_bfd_link_add_symbols)
1142159047fSniklas 	The linker proper will call the <<_bfd_link_add_symbols>>
1152159047fSniklas 	entry point for each object file or archive which is to be
1162159047fSniklas 	linked (typically these are the files named on the command
1172159047fSniklas 	line, but some may also come from the linker script).  The
1182159047fSniklas 	entry point is responsible for examining the file.  For an
1192159047fSniklas 	object file, BFD must add any relevant symbol information to
1202159047fSniklas 	the hash table.  For an archive, BFD must determine which
1212159047fSniklas 	elements of the archive should be used and adding them to the
1222159047fSniklas 	link.
1232159047fSniklas 
1242159047fSniklas 	The a.out version of this entry point is
1252159047fSniklas 	<<NAME(aout,link_add_symbols)>>.
1262159047fSniklas 
1272159047fSniklas @menu
1282159047fSniklas @* Differing file formats::
1292159047fSniklas @* Adding symbols from an object file::
1302159047fSniklas @* Adding symbols from an archive::
1312159047fSniklas @end menu
1322159047fSniklas 
1332159047fSniklas INODE
1342159047fSniklas Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
1352159047fSniklas SUBSUBSECTION
1362159047fSniklas 	Differing file formats
1372159047fSniklas 
1382159047fSniklas 	Normally all the files involved in a link will be of the same
1392159047fSniklas 	format, but it is also possible to link together different
1402159047fSniklas 	format object files, and the back end must support that.  The
1412159047fSniklas 	<<_bfd_link_add_symbols>> entry point is called via the target
1422159047fSniklas 	vector of the file to be added.  This has an important
1432159047fSniklas 	consequence: the function may not assume that the hash table
1442159047fSniklas 	is the type created by the corresponding
1452159047fSniklas 	<<_bfd_link_hash_table_create>> vector.  All the
1462159047fSniklas 	<<_bfd_link_add_symbols>> function can assume about the hash
1472159047fSniklas 	table is that it is derived from <<struct
1482159047fSniklas 	bfd_link_hash_table>>.
1492159047fSniklas 
1502159047fSniklas 	Sometimes the <<_bfd_link_add_symbols>> function must store
1512159047fSniklas 	some information in the hash table entry to be used by the
1522159047fSniklas 	<<_bfd_final_link>> function.  In such a case the <<creator>>
1532159047fSniklas 	field of the hash table must be checked to make sure that the
1542159047fSniklas 	hash table was created by an object file of the same format.
1552159047fSniklas 
1562159047fSniklas 	The <<_bfd_final_link>> routine must be prepared to handle a
1572159047fSniklas 	hash entry without any extra information added by the
1582159047fSniklas 	<<_bfd_link_add_symbols>> function.  A hash entry without
1592159047fSniklas 	extra information will also occur when the linker script
1602159047fSniklas 	directs the linker to create a symbol.  Note that, regardless
1612159047fSniklas 	of how a hash table entry is added, all the fields will be
1622159047fSniklas 	initialized to some sort of null value by the hash table entry
1632159047fSniklas 	initialization function.
1642159047fSniklas 
1652159047fSniklas 	See <<ecoff_link_add_externals>> for an example of how to
1662159047fSniklas 	check the <<creator>> field before saving information (in this
1672159047fSniklas 	case, the ECOFF external symbol debugging information) in a
1682159047fSniklas 	hash table entry.
1692159047fSniklas 
1702159047fSniklas INODE
1712159047fSniklas Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
1722159047fSniklas SUBSUBSECTION
1732159047fSniklas 	Adding symbols from an object file
1742159047fSniklas 
1752159047fSniklas 	When the <<_bfd_link_add_symbols>> routine is passed an object
1762159047fSniklas 	file, it must add all externally visible symbols in that
1772159047fSniklas 	object file to the hash table.  The actual work of adding the
1782159047fSniklas 	symbol to the hash table is normally handled by the function
1792159047fSniklas 	<<_bfd_generic_link_add_one_symbol>>.  The
1802159047fSniklas 	<<_bfd_link_add_symbols>> routine is responsible for reading
1812159047fSniklas 	all the symbols from the object file and passing the correct
1822159047fSniklas 	information to <<_bfd_generic_link_add_one_symbol>>.
1832159047fSniklas 
1842159047fSniklas 	The <<_bfd_link_add_symbols>> routine should not use
1852159047fSniklas 	<<bfd_canonicalize_symtab>> to read the symbols.  The point of
1862159047fSniklas 	providing this routine is to avoid the overhead of converting
1872159047fSniklas 	the symbols into generic <<asymbol>> structures.
1882159047fSniklas 
1892159047fSniklas @findex _bfd_generic_link_add_one_symbol
1902159047fSniklas 	<<_bfd_generic_link_add_one_symbol>> handles the details of
1912159047fSniklas 	combining common symbols, warning about multiple definitions,
1922159047fSniklas 	and so forth.  It takes arguments which describe the symbol to
1932159047fSniklas 	add, notably symbol flags, a section, and an offset.  The
1942159047fSniklas 	symbol flags include such things as <<BSF_WEAK>> or
1952159047fSniklas 	<<BSF_INDIRECT>>.  The section is a section in the object
1962159047fSniklas 	file, or something like <<bfd_und_section_ptr>> for an undefined
1972159047fSniklas 	symbol or <<bfd_com_section_ptr>> for a common symbol.
1982159047fSniklas 
1992159047fSniklas 	If the <<_bfd_final_link>> routine is also going to need to
2002159047fSniklas 	read the symbol information, the <<_bfd_link_add_symbols>>
2012159047fSniklas 	routine should save it somewhere attached to the object file
2022159047fSniklas 	BFD.  However, the information should only be saved if the
203c074d1c9Sdrahn 	<<keep_memory>> field of the <<info>> argument is TRUE, so
2042159047fSniklas 	that the <<-no-keep-memory>> linker switch is effective.
2052159047fSniklas 
2062159047fSniklas 	The a.out function which adds symbols from an object file is
2072159047fSniklas 	<<aout_link_add_object_symbols>>, and most of the interesting
2082159047fSniklas 	work is in <<aout_link_add_symbols>>.  The latter saves
2092159047fSniklas 	pointers to the hash tables entries created by
2102159047fSniklas 	<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
2112159047fSniklas 	so that the <<_bfd_final_link>> routine does not have to call
2122159047fSniklas 	the hash table lookup routine to locate the entry.
2132159047fSniklas 
2142159047fSniklas INODE
2152159047fSniklas Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
2162159047fSniklas SUBSUBSECTION
2172159047fSniklas 	Adding symbols from an archive
2182159047fSniklas 
2192159047fSniklas 	When the <<_bfd_link_add_symbols>> routine is passed an
2202159047fSniklas 	archive, it must look through the symbols defined by the
2212159047fSniklas 	archive and decide which elements of the archive should be
2222159047fSniklas 	included in the link.  For each such element it must call the
2232159047fSniklas 	<<add_archive_element>> linker callback, and it must add the
2242159047fSniklas 	symbols from the object file to the linker hash table.
2252159047fSniklas 
2262159047fSniklas @findex _bfd_generic_link_add_archive_symbols
2272159047fSniklas 	In most cases the work of looking through the symbols in the
2282159047fSniklas 	archive should be done by the
2292159047fSniklas 	<<_bfd_generic_link_add_archive_symbols>> function.  This
2302159047fSniklas 	function builds a hash table from the archive symbol table and
2312159047fSniklas 	looks through the list of undefined symbols to see which
2322159047fSniklas 	elements should be included.
2332159047fSniklas 	<<_bfd_generic_link_add_archive_symbols>> is passed a function
2342159047fSniklas 	to call to make the final decision about adding an archive
2352159047fSniklas 	element to the link and to do the actual work of adding the
2362159047fSniklas 	symbols to the linker hash table.
2372159047fSniklas 
2382159047fSniklas 	The function passed to
2392159047fSniklas 	<<_bfd_generic_link_add_archive_symbols>> must read the
2402159047fSniklas 	symbols of the archive element and decide whether the archive
2412159047fSniklas 	element should be included in the link.  If the element is to
2422159047fSniklas 	be included, the <<add_archive_element>> linker callback
2432159047fSniklas 	routine must be called with the element as an argument, and
2442159047fSniklas 	the elements symbols must be added to the linker hash table
2452159047fSniklas 	just as though the element had itself been passed to the
2462159047fSniklas 	<<_bfd_link_add_symbols>> function.
2472159047fSniklas 
2482159047fSniklas 	When the a.out <<_bfd_link_add_symbols>> function receives an
2492159047fSniklas 	archive, it calls <<_bfd_generic_link_add_archive_symbols>>
2502159047fSniklas 	passing <<aout_link_check_archive_element>> as the function
2512159047fSniklas 	argument. <<aout_link_check_archive_element>> calls
2522159047fSniklas 	<<aout_link_check_ar_symbols>>.  If the latter decides to add
2532159047fSniklas 	the element (an element is only added if it provides a real,
2542159047fSniklas 	non-common, definition for a previously undefined or common
2552159047fSniklas 	symbol) it calls the <<add_archive_element>> callback and then
2562159047fSniklas 	<<aout_link_check_archive_element>> calls
2572159047fSniklas 	<<aout_link_add_symbols>> to actually add the symbols to the
2582159047fSniklas 	linker hash table.
2592159047fSniklas 
2602159047fSniklas 	The ECOFF back end is unusual in that it does not normally
2612159047fSniklas 	call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
2622159047fSniklas 	archives already contain a hash table of symbols.  The ECOFF
2632159047fSniklas 	back end searches the archive itself to avoid the overhead of
2642159047fSniklas 	creating a new hash table.
2652159047fSniklas 
2662159047fSniklas INODE
2672159047fSniklas Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
2682159047fSniklas SUBSECTION
2692159047fSniklas 	Performing the final link
2702159047fSniklas 
2712159047fSniklas @cindex _bfd_link_final_link in target vector
2722159047fSniklas @cindex target vector (_bfd_final_link)
2732159047fSniklas 	When all the input files have been processed, the linker calls
2742159047fSniklas 	the <<_bfd_final_link>> entry point of the output BFD.  This
2752159047fSniklas 	routine is responsible for producing the final output file,
2762159047fSniklas 	which has several aspects.  It must relocate the contents of
2772159047fSniklas 	the input sections and copy the data into the output sections.
2782159047fSniklas 	It must build an output symbol table including any local
2792159047fSniklas 	symbols from the input files and the global symbols from the
280*007c2a45Smiod 	hash table.  When producing relocatable output, it must
2812159047fSniklas 	modify the input relocs and write them into the output file.
2822159047fSniklas 	There may also be object format dependent work to be done.
2832159047fSniklas 
2842159047fSniklas 	The linker will also call the <<write_object_contents>> entry
2852159047fSniklas 	point when the BFD is closed.  The two entry points must work
2862159047fSniklas 	together in order to produce the correct output file.
2872159047fSniklas 
2882159047fSniklas 	The details of how this works are inevitably dependent upon
2892159047fSniklas 	the specific object file format.  The a.out
2902159047fSniklas 	<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
2912159047fSniklas 
2922159047fSniklas @menu
2932159047fSniklas @* Information provided by the linker::
2942159047fSniklas @* Relocating the section contents::
2952159047fSniklas @* Writing the symbol table::
2962159047fSniklas @end menu
2972159047fSniklas 
2982159047fSniklas INODE
2992159047fSniklas Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
3002159047fSniklas SUBSUBSECTION
3012159047fSniklas 	Information provided by the linker
3022159047fSniklas 
3032159047fSniklas 	Before the linker calls the <<_bfd_final_link>> entry point,
3042159047fSniklas 	it sets up some data structures for the function to use.
3052159047fSniklas 
3062159047fSniklas 	The <<input_bfds>> field of the <<bfd_link_info>> structure
3072159047fSniklas 	will point to a list of all the input files included in the
3082159047fSniklas 	link.  These files are linked through the <<link_next>> field
3092159047fSniklas 	of the <<bfd>> structure.
3102159047fSniklas 
3112159047fSniklas 	Each section in the output file will have a list of
3122159047fSniklas 	<<link_order>> structures attached to the <<link_order_head>>
3132159047fSniklas 	field (the <<link_order>> structure is defined in
3142159047fSniklas 	<<bfdlink.h>>).  These structures describe how to create the
3152159047fSniklas 	contents of the output section in terms of the contents of
3162159047fSniklas 	various input sections, fill constants, and, eventually, other
3172159047fSniklas 	types of information.  They also describe relocs that must be
3182159047fSniklas 	created by the BFD backend, but do not correspond to any input
3192159047fSniklas 	file; this is used to support -Ur, which builds constructors
320*007c2a45Smiod 	while generating a relocatable object file.
3212159047fSniklas 
3222159047fSniklas INODE
3232159047fSniklas Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
3242159047fSniklas SUBSUBSECTION
3252159047fSniklas 	Relocating the section contents
3262159047fSniklas 
3272159047fSniklas 	The <<_bfd_final_link>> function should look through the
3282159047fSniklas 	<<link_order>> structures attached to each section of the
3292159047fSniklas 	output file.  Each <<link_order>> structure should either be
3302159047fSniklas 	handled specially, or it should be passed to the function
3312159047fSniklas 	<<_bfd_default_link_order>> which will do the right thing
3322159047fSniklas 	(<<_bfd_default_link_order>> is defined in <<linker.c>>).
3332159047fSniklas 
3342159047fSniklas 	For efficiency, a <<link_order>> of type
3352159047fSniklas 	<<bfd_indirect_link_order>> whose associated section belongs
3362159047fSniklas 	to a BFD of the same format as the output BFD must be handled
3372159047fSniklas 	specially.  This type of <<link_order>> describes part of an
3382159047fSniklas 	output section in terms of a section belonging to one of the
3392159047fSniklas 	input files.  The <<_bfd_final_link>> function should read the
3402159047fSniklas 	contents of the section and any associated relocs, apply the
3412159047fSniklas 	relocs to the section contents, and write out the modified
342*007c2a45Smiod 	section contents.  If performing a relocatable link, the
3432159047fSniklas 	relocs themselves must also be modified and written out.
3442159047fSniklas 
3452159047fSniklas @findex _bfd_relocate_contents
3462159047fSniklas @findex _bfd_final_link_relocate
3472159047fSniklas 	The functions <<_bfd_relocate_contents>> and
3482159047fSniklas 	<<_bfd_final_link_relocate>> provide some general support for
3492159047fSniklas 	performing the actual relocations, notably overflow checking.
3502159047fSniklas 	Their arguments include information about the symbol the
3512159047fSniklas 	relocation is against and a <<reloc_howto_type>> argument
3522159047fSniklas 	which describes the relocation to perform.  These functions
3532159047fSniklas 	are defined in <<reloc.c>>.
3542159047fSniklas 
3552159047fSniklas 	The a.out function which handles reading, relocating, and
3562159047fSniklas 	writing section contents is <<aout_link_input_section>>.  The
3572159047fSniklas 	actual relocation is done in <<aout_link_input_section_std>>
3582159047fSniklas 	and <<aout_link_input_section_ext>>.
3592159047fSniklas 
3602159047fSniklas INODE
3612159047fSniklas Writing the symbol table, , Relocating the section contents, Performing the Final Link
3622159047fSniklas SUBSUBSECTION
3632159047fSniklas 	Writing the symbol table
3642159047fSniklas 
3652159047fSniklas 	The <<_bfd_final_link>> function must gather all the symbols
3662159047fSniklas 	in the input files and write them out.  It must also write out
3672159047fSniklas 	all the symbols in the global hash table.  This must be
3682159047fSniklas 	controlled by the <<strip>> and <<discard>> fields of the
3692159047fSniklas 	<<bfd_link_info>> structure.
3702159047fSniklas 
3712159047fSniklas 	The local symbols of the input files will not have been
3722159047fSniklas 	entered into the linker hash table.  The <<_bfd_final_link>>
3732159047fSniklas 	routine must consider each input file and include the symbols
3742159047fSniklas 	in the output file.  It may be convenient to do this when
3752159047fSniklas 	looking through the <<link_order>> structures, or it may be
3762159047fSniklas 	done by stepping through the <<input_bfds>> list.
3772159047fSniklas 
3782159047fSniklas 	The <<_bfd_final_link>> routine must also traverse the global
3792159047fSniklas 	hash table to gather all the externally visible symbols.  It
3802159047fSniklas 	is possible that most of the externally visible symbols may be
3812159047fSniklas 	written out when considering the symbols of each input file,
3822159047fSniklas 	but it is still necessary to traverse the hash table since the
3832159047fSniklas 	linker script may have defined some symbols that are not in
3842159047fSniklas 	any of the input files.
3852159047fSniklas 
3862159047fSniklas 	The <<strip>> field of the <<bfd_link_info>> structure
3872159047fSniklas 	controls which symbols are written out.  The possible values
3882159047fSniklas 	are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
3892159047fSniklas 	then the <<keep_hash>> field of the <<bfd_link_info>>
3902159047fSniklas 	structure is a hash table of symbols to keep; each symbol
3912159047fSniklas 	should be looked up in this hash table, and only symbols which
3922159047fSniklas 	are present should be included in the output file.
3932159047fSniklas 
3942159047fSniklas 	If the <<strip>> field of the <<bfd_link_info>> structure
3952159047fSniklas 	permits local symbols to be written out, the <<discard>> field
3962159047fSniklas 	is used to further controls which local symbols are included
3972159047fSniklas 	in the output file.  If the value is <<discard_l>>, then all
3982159047fSniklas 	local symbols which begin with a certain prefix are discarded;
399b305b0f1Sespie 	this is controlled by the <<bfd_is_local_label_name>> entry point.
4002159047fSniklas 
4012159047fSniklas 	The a.out backend handles symbols by calling
4022159047fSniklas 	<<aout_link_write_symbols>> on each input BFD and then
4032159047fSniklas 	traversing the global hash table with the function
4042159047fSniklas 	<<aout_link_write_other_symbol>>.  It builds a string table
4052159047fSniklas 	while writing out the symbols, which is written to the output
4062159047fSniklas 	file at the end of <<NAME(aout,final_link)>>.
4072159047fSniklas */
4082159047fSniklas 
409c074d1c9Sdrahn static bfd_boolean generic_link_add_object_symbols
410*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_boolean collect);
411*007c2a45Smiod static bfd_boolean generic_link_add_symbols
412*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_boolean);
413c074d1c9Sdrahn static bfd_boolean generic_link_check_archive_element_no_collect
414*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *);
415c074d1c9Sdrahn static bfd_boolean generic_link_check_archive_element_collect
416*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *);
417c074d1c9Sdrahn static bfd_boolean generic_link_check_archive_element
418*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
419c074d1c9Sdrahn static bfd_boolean generic_link_add_symbol_list
420*007c2a45Smiod   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
421*007c2a45Smiod    bfd_boolean);
422c074d1c9Sdrahn static bfd_boolean generic_add_output_symbol
423*007c2a45Smiod   (bfd *, size_t *psymalloc, asymbol *);
424c074d1c9Sdrahn static bfd_boolean default_data_link_order
425*007c2a45Smiod   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
426c074d1c9Sdrahn static bfd_boolean default_indirect_link_order
427*007c2a45Smiod   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
428*007c2a45Smiod    bfd_boolean);
4292159047fSniklas 
4302159047fSniklas /* The link hash table structure is defined in bfdlink.h.  It provides
4312159047fSniklas    a base hash table which the backend specific hash tables are built
4322159047fSniklas    upon.  */
4332159047fSniklas 
4342159047fSniklas /* Routine to create an entry in the link hash table.  */
4352159047fSniklas 
4362159047fSniklas struct bfd_hash_entry *
_bfd_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)437*007c2a45Smiod _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
438*007c2a45Smiod 			struct bfd_hash_table *table,
439*007c2a45Smiod 			const char *string)
4402159047fSniklas {
4412159047fSniklas   /* Allocate the structure if it has not already been allocated by a
4422159047fSniklas      subclass.  */
443c074d1c9Sdrahn   if (entry == NULL)
4442159047fSniklas     {
445*007c2a45Smiod       entry = bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
446c074d1c9Sdrahn       if (entry == NULL)
447c074d1c9Sdrahn 	return entry;
4482159047fSniklas     }
4492159047fSniklas 
450c074d1c9Sdrahn   /* Call the allocation method of the superclass.  */
451c074d1c9Sdrahn   entry = bfd_hash_newfunc (entry, table, string);
452c074d1c9Sdrahn   if (entry)
453c074d1c9Sdrahn     {
454c074d1c9Sdrahn       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
455c074d1c9Sdrahn 
456c074d1c9Sdrahn       /* Initialize the local fields.  */
457c074d1c9Sdrahn       h->type = bfd_link_hash_new;
458*007c2a45Smiod       h->und_next = NULL;
459c074d1c9Sdrahn     }
460c074d1c9Sdrahn 
461c074d1c9Sdrahn   return entry;
4622159047fSniklas }
4632159047fSniklas 
4642159047fSniklas /* Initialize a link hash table.  The BFD argument is the one
4652159047fSniklas    responsible for creating this table.  */
4662159047fSniklas 
467c074d1c9Sdrahn bfd_boolean
_bfd_link_hash_table_init(struct bfd_link_hash_table * table,bfd * abfd,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *))468*007c2a45Smiod _bfd_link_hash_table_init
469*007c2a45Smiod   (struct bfd_link_hash_table *table,
470*007c2a45Smiod    bfd *abfd,
471*007c2a45Smiod    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
4722159047fSniklas 				      struct bfd_hash_table *,
473*007c2a45Smiod 				      const char *))
4742159047fSniklas {
4752159047fSniklas   table->creator = abfd->xvec;
4762159047fSniklas   table->undefs = NULL;
4772159047fSniklas   table->undefs_tail = NULL;
478c074d1c9Sdrahn   table->type = bfd_link_generic_hash_table;
479c074d1c9Sdrahn 
4802159047fSniklas   return bfd_hash_table_init (&table->table, newfunc);
4812159047fSniklas }
4822159047fSniklas 
483c074d1c9Sdrahn /* Look up a symbol in a link hash table.  If follow is TRUE, we
4842159047fSniklas    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
4852159047fSniklas    the real symbol.  */
4862159047fSniklas 
4872159047fSniklas 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)488*007c2a45Smiod bfd_link_hash_lookup (struct bfd_link_hash_table *table,
489*007c2a45Smiod 		      const char *string,
490*007c2a45Smiod 		      bfd_boolean create,
491*007c2a45Smiod 		      bfd_boolean copy,
492*007c2a45Smiod 		      bfd_boolean follow)
4932159047fSniklas {
4942159047fSniklas   struct bfd_link_hash_entry *ret;
4952159047fSniklas 
4962159047fSniklas   ret = ((struct bfd_link_hash_entry *)
4972159047fSniklas 	 bfd_hash_lookup (&table->table, string, create, copy));
4982159047fSniklas 
499*007c2a45Smiod   if (follow && ret != NULL)
5002159047fSniklas     {
5012159047fSniklas       while (ret->type == bfd_link_hash_indirect
5022159047fSniklas 	     || ret->type == bfd_link_hash_warning)
5032159047fSniklas 	ret = ret->u.i.link;
5042159047fSniklas     }
5052159047fSniklas 
5062159047fSniklas   return ret;
5072159047fSniklas }
5082159047fSniklas 
509c88b1d6cSniklas /* Look up a symbol in the main linker hash table if the symbol might
510c88b1d6cSniklas    be wrapped.  This should only be used for references to an
511c88b1d6cSniklas    undefined symbol, not for definitions of a symbol.  */
512c88b1d6cSniklas 
513c88b1d6cSniklas 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)514*007c2a45Smiod bfd_wrapped_link_hash_lookup (bfd *abfd,
515*007c2a45Smiod 			      struct bfd_link_info *info,
516*007c2a45Smiod 			      const char *string,
517*007c2a45Smiod 			      bfd_boolean create,
518*007c2a45Smiod 			      bfd_boolean copy,
519*007c2a45Smiod 			      bfd_boolean follow)
520c88b1d6cSniklas {
521c074d1c9Sdrahn   bfd_size_type amt;
522c074d1c9Sdrahn 
523c88b1d6cSniklas   if (info->wrap_hash != NULL)
524c88b1d6cSniklas     {
525c88b1d6cSniklas       const char *l;
526*007c2a45Smiod       char prefix = '\0';
527c88b1d6cSniklas 
528c88b1d6cSniklas       l = string;
529*007c2a45Smiod       if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
530*007c2a45Smiod 	{
531*007c2a45Smiod 	  prefix = *l;
532c88b1d6cSniklas 	  ++l;
533*007c2a45Smiod 	}
534c88b1d6cSniklas 
535c88b1d6cSniklas #undef WRAP
536c88b1d6cSniklas #define WRAP "__wrap_"
537c88b1d6cSniklas 
538c074d1c9Sdrahn       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
539c88b1d6cSniklas 	{
540c88b1d6cSniklas 	  char *n;
541c88b1d6cSniklas 	  struct bfd_link_hash_entry *h;
542c88b1d6cSniklas 
543c88b1d6cSniklas 	  /* This symbol is being wrapped.  We want to replace all
544c88b1d6cSniklas              references to SYM with references to __wrap_SYM.  */
545c88b1d6cSniklas 
546c074d1c9Sdrahn 	  amt = strlen (l) + sizeof WRAP + 1;
547*007c2a45Smiod 	  n = bfd_malloc (amt);
548c88b1d6cSniklas 	  if (n == NULL)
549c88b1d6cSniklas 	    return NULL;
550c88b1d6cSniklas 
551*007c2a45Smiod 	  n[0] = prefix;
552c88b1d6cSniklas 	  n[1] = '\0';
553c88b1d6cSniklas 	  strcat (n, WRAP);
554c88b1d6cSniklas 	  strcat (n, l);
555c074d1c9Sdrahn 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
556c88b1d6cSniklas 	  free (n);
557c88b1d6cSniklas 	  return h;
558c88b1d6cSniklas 	}
559c88b1d6cSniklas 
560c88b1d6cSniklas #undef WRAP
561c88b1d6cSniklas 
562c88b1d6cSniklas #undef REAL
563c88b1d6cSniklas #define REAL "__real_"
564c88b1d6cSniklas 
565c88b1d6cSniklas       if (*l == '_'
566c88b1d6cSniklas 	  && strncmp (l, REAL, sizeof REAL - 1) == 0
567c88b1d6cSniklas 	  && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
568c074d1c9Sdrahn 			      FALSE, FALSE) != NULL)
569c88b1d6cSniklas 	{
570c88b1d6cSniklas 	  char *n;
571c88b1d6cSniklas 	  struct bfd_link_hash_entry *h;
572c88b1d6cSniklas 
573c88b1d6cSniklas 	  /* This is a reference to __real_SYM, where SYM is being
574c88b1d6cSniklas              wrapped.  We want to replace all references to __real_SYM
575c88b1d6cSniklas              with references to SYM.  */
576c88b1d6cSniklas 
577c074d1c9Sdrahn 	  amt = strlen (l + sizeof REAL - 1) + 2;
578*007c2a45Smiod 	  n = bfd_malloc (amt);
579c88b1d6cSniklas 	  if (n == NULL)
580c88b1d6cSniklas 	    return NULL;
581c88b1d6cSniklas 
582*007c2a45Smiod 	  n[0] = prefix;
583c88b1d6cSniklas 	  n[1] = '\0';
584c88b1d6cSniklas 	  strcat (n, l + sizeof REAL - 1);
585c074d1c9Sdrahn 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
586c88b1d6cSniklas 	  free (n);
587c88b1d6cSniklas 	  return h;
588c88b1d6cSniklas 	}
589c88b1d6cSniklas 
590c88b1d6cSniklas #undef REAL
591c88b1d6cSniklas     }
592c88b1d6cSniklas 
593c88b1d6cSniklas   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
594c88b1d6cSniklas }
595c88b1d6cSniklas 
5962159047fSniklas /* Traverse a generic link hash table.  The only reason this is not a
5972159047fSniklas    macro is to do better type checking.  This code presumes that an
5982159047fSniklas    argument passed as a struct bfd_hash_entry * may be caught as a
5992159047fSniklas    struct bfd_link_hash_entry * with no explicit cast required on the
6002159047fSniklas    call.  */
6012159047fSniklas 
6022159047fSniklas void
bfd_link_hash_traverse(struct bfd_link_hash_table * table,bfd_boolean (* func)(struct bfd_link_hash_entry *,void *),void * info)603*007c2a45Smiod bfd_link_hash_traverse
604*007c2a45Smiod   (struct bfd_link_hash_table *table,
605*007c2a45Smiod    bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
606*007c2a45Smiod    void *info)
6072159047fSniklas {
6082159047fSniklas   bfd_hash_traverse (&table->table,
609*007c2a45Smiod 		     (bfd_boolean (*) (struct bfd_hash_entry *, void *)) func,
6102159047fSniklas 		     info);
6112159047fSniklas }
6122159047fSniklas 
6132159047fSniklas /* Add a symbol to the linker hash table undefs list.  */
6142159047fSniklas 
615*007c2a45Smiod void
bfd_link_add_undef(struct bfd_link_hash_table * table,struct bfd_link_hash_entry * h)616*007c2a45Smiod bfd_link_add_undef (struct bfd_link_hash_table *table,
617*007c2a45Smiod 		    struct bfd_link_hash_entry *h)
6182159047fSniklas {
619*007c2a45Smiod   BFD_ASSERT (h->und_next == NULL);
620*007c2a45Smiod   if (table->undefs_tail != NULL)
621*007c2a45Smiod     table->undefs_tail->und_next = h;
622*007c2a45Smiod   if (table->undefs == NULL)
6232159047fSniklas     table->undefs = h;
6242159047fSniklas   table->undefs_tail = h;
6252159047fSniklas }
6262159047fSniklas 
627c074d1c9Sdrahn /* Routine to create an entry in a generic link hash table.  */
6282159047fSniklas 
629c88b1d6cSniklas struct bfd_hash_entry *
_bfd_generic_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)630*007c2a45Smiod _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
631*007c2a45Smiod 				struct bfd_hash_table *table,
632*007c2a45Smiod 				const char *string)
6332159047fSniklas {
6342159047fSniklas   /* Allocate the structure if it has not already been allocated by a
6352159047fSniklas      subclass.  */
636c074d1c9Sdrahn   if (entry == NULL)
637c074d1c9Sdrahn     {
638*007c2a45Smiod       entry =
639c074d1c9Sdrahn 	bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
640c074d1c9Sdrahn       if (entry == NULL)
641c074d1c9Sdrahn 	return entry;
642c074d1c9Sdrahn     }
6432159047fSniklas 
6442159047fSniklas   /* Call the allocation method of the superclass.  */
645c074d1c9Sdrahn   entry = _bfd_link_hash_newfunc (entry, table, string);
646c074d1c9Sdrahn   if (entry)
6472159047fSniklas     {
648c074d1c9Sdrahn       struct generic_link_hash_entry *ret;
649c074d1c9Sdrahn 
6502159047fSniklas       /* Set local fields.  */
651c074d1c9Sdrahn       ret = (struct generic_link_hash_entry *) entry;
652c074d1c9Sdrahn       ret->written = FALSE;
6532159047fSniklas       ret->sym = NULL;
6542159047fSniklas     }
6552159047fSniklas 
656c074d1c9Sdrahn   return entry;
6572159047fSniklas }
6582159047fSniklas 
659c074d1c9Sdrahn /* Create a generic link hash table.  */
6602159047fSniklas 
6612159047fSniklas struct bfd_link_hash_table *
_bfd_generic_link_hash_table_create(bfd * abfd)662*007c2a45Smiod _bfd_generic_link_hash_table_create (bfd *abfd)
6632159047fSniklas {
6642159047fSniklas   struct generic_link_hash_table *ret;
665c074d1c9Sdrahn   bfd_size_type amt = sizeof (struct generic_link_hash_table);
6662159047fSniklas 
667*007c2a45Smiod   ret = bfd_malloc (amt);
6682159047fSniklas   if (ret == NULL)
669*007c2a45Smiod     return NULL;
6702159047fSniklas   if (! _bfd_link_hash_table_init (&ret->root, abfd,
671c88b1d6cSniklas 				   _bfd_generic_link_hash_newfunc))
6722159047fSniklas     {
6732159047fSniklas       free (ret);
674*007c2a45Smiod       return NULL;
6752159047fSniklas     }
6762159047fSniklas   return &ret->root;
6772159047fSniklas }
6782159047fSniklas 
679c074d1c9Sdrahn void
_bfd_generic_link_hash_table_free(struct bfd_link_hash_table * hash)680*007c2a45Smiod _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
681c074d1c9Sdrahn {
682c074d1c9Sdrahn   struct generic_link_hash_table *ret
683c074d1c9Sdrahn     = (struct generic_link_hash_table *) hash;
684c074d1c9Sdrahn 
685c074d1c9Sdrahn   bfd_hash_table_free (&ret->root.table);
686c074d1c9Sdrahn   free (ret);
687c074d1c9Sdrahn }
688c074d1c9Sdrahn 
6892159047fSniklas /* Grab the symbols for an object file when doing a generic link.  We
6902159047fSniklas    store the symbols in the outsymbols field.  We need to keep them
6912159047fSniklas    around for the entire link to ensure that we only read them once.
6922159047fSniklas    If we read them multiple times, we might wind up with relocs and
6932159047fSniklas    the hash table pointing to different instances of the symbol
6942159047fSniklas    structure.  */
6952159047fSniklas 
696c074d1c9Sdrahn static bfd_boolean
generic_link_read_symbols(bfd * abfd)697*007c2a45Smiod generic_link_read_symbols (bfd *abfd)
6982159047fSniklas {
699*007c2a45Smiod   if (bfd_get_outsymbols (abfd) == NULL)
7002159047fSniklas     {
7012159047fSniklas       long symsize;
7022159047fSniklas       long symcount;
7032159047fSniklas 
7042159047fSniklas       symsize = bfd_get_symtab_upper_bound (abfd);
7052159047fSniklas       if (symsize < 0)
706c074d1c9Sdrahn 	return FALSE;
707*007c2a45Smiod       bfd_get_outsymbols (abfd) = bfd_alloc (abfd, symsize);
708b305b0f1Sespie       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
709c074d1c9Sdrahn 	return FALSE;
710b305b0f1Sespie       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
7112159047fSniklas       if (symcount < 0)
712c074d1c9Sdrahn 	return FALSE;
713b305b0f1Sespie       bfd_get_symcount (abfd) = symcount;
7142159047fSniklas     }
7152159047fSniklas 
716c074d1c9Sdrahn   return TRUE;
7172159047fSniklas }
7182159047fSniklas 
7192159047fSniklas /* Generic function to add symbols to from an object file to the
7202159047fSniklas    global hash table.  This version does not automatically collect
7212159047fSniklas    constructors by name.  */
7222159047fSniklas 
723c074d1c9Sdrahn bfd_boolean
_bfd_generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info)724*007c2a45Smiod _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
7252159047fSniklas {
726c074d1c9Sdrahn   return generic_link_add_symbols (abfd, info, FALSE);
7272159047fSniklas }
7282159047fSniklas 
7292159047fSniklas /* Generic function to add symbols from an object file to the global
7302159047fSniklas    hash table.  This version automatically collects constructors by
7312159047fSniklas    name, as the collect2 program does.  It should be used for any
7322159047fSniklas    target which does not provide some other mechanism for setting up
7332159047fSniklas    constructors and destructors; these are approximately those targets
7342159047fSniklas    for which gcc uses collect2 and do not support stabs.  */
7352159047fSniklas 
736c074d1c9Sdrahn bfd_boolean
_bfd_generic_link_add_symbols_collect(bfd * abfd,struct bfd_link_info * info)737*007c2a45Smiod _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
7382159047fSniklas {
739c074d1c9Sdrahn   return generic_link_add_symbols (abfd, info, TRUE);
740c074d1c9Sdrahn }
741c074d1c9Sdrahn 
742c074d1c9Sdrahn /* Indicate that we are only retrieving symbol values from this
743c074d1c9Sdrahn    section.  We want the symbols to act as though the values in the
744c074d1c9Sdrahn    file are absolute.  */
745c074d1c9Sdrahn 
746c074d1c9Sdrahn void
_bfd_generic_link_just_syms(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED)747*007c2a45Smiod _bfd_generic_link_just_syms (asection *sec,
748*007c2a45Smiod 			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
749c074d1c9Sdrahn {
750c074d1c9Sdrahn   sec->output_section = bfd_abs_section_ptr;
751c074d1c9Sdrahn   sec->output_offset = sec->vma;
7522159047fSniklas }
7532159047fSniklas 
7542159047fSniklas /* Add symbols from an object file to the global hash table.  */
7552159047fSniklas 
756c074d1c9Sdrahn static bfd_boolean
generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)757*007c2a45Smiod generic_link_add_symbols (bfd *abfd,
758*007c2a45Smiod 			  struct bfd_link_info *info,
759*007c2a45Smiod 			  bfd_boolean collect)
7602159047fSniklas {
761c074d1c9Sdrahn   bfd_boolean ret;
7622159047fSniklas 
7632159047fSniklas   switch (bfd_get_format (abfd))
7642159047fSniklas     {
7652159047fSniklas     case bfd_object:
7662159047fSniklas       ret = generic_link_add_object_symbols (abfd, info, collect);
7672159047fSniklas       break;
7682159047fSniklas     case bfd_archive:
7692159047fSniklas       ret = (_bfd_generic_link_add_archive_symbols
7702159047fSniklas 	     (abfd, info,
7712159047fSniklas 	      (collect
7722159047fSniklas 	       ? generic_link_check_archive_element_collect
7732159047fSniklas 	       : generic_link_check_archive_element_no_collect)));
7742159047fSniklas       break;
7752159047fSniklas     default:
7762159047fSniklas       bfd_set_error (bfd_error_wrong_format);
777c074d1c9Sdrahn       ret = FALSE;
7782159047fSniklas     }
7792159047fSniklas 
7802159047fSniklas   return ret;
7812159047fSniklas }
7822159047fSniklas 
7832159047fSniklas /* Add symbols from an object file to the global hash table.  */
7842159047fSniklas 
785c074d1c9Sdrahn static bfd_boolean
generic_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)786*007c2a45Smiod generic_link_add_object_symbols (bfd *abfd,
787*007c2a45Smiod 				 struct bfd_link_info *info,
788*007c2a45Smiod 				 bfd_boolean collect)
7892159047fSniklas {
790c074d1c9Sdrahn   bfd_size_type symcount;
791*007c2a45Smiod   struct bfd_symbol **outsyms;
792c074d1c9Sdrahn 
7932159047fSniklas   if (! generic_link_read_symbols (abfd))
794c074d1c9Sdrahn     return FALSE;
795c074d1c9Sdrahn   symcount = _bfd_generic_link_get_symcount (abfd);
796c074d1c9Sdrahn   outsyms = _bfd_generic_link_get_symbols (abfd);
797c074d1c9Sdrahn   return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
7982159047fSniklas }
7992159047fSniklas 
8002159047fSniklas /* We build a hash table of all symbols defined in an archive.  */
8012159047fSniklas 
8022159047fSniklas /* An archive symbol may be defined by multiple archive elements.
8032159047fSniklas    This linked list is used to hold the elements.  */
8042159047fSniklas 
8052159047fSniklas struct archive_list
8062159047fSniklas {
8072159047fSniklas   struct archive_list *next;
808c074d1c9Sdrahn   unsigned int indx;
8092159047fSniklas };
8102159047fSniklas 
8112159047fSniklas /* An entry in an archive hash table.  */
8122159047fSniklas 
8132159047fSniklas struct archive_hash_entry
8142159047fSniklas {
8152159047fSniklas   struct bfd_hash_entry root;
8162159047fSniklas   /* Where the symbol is defined.  */
8172159047fSniklas   struct archive_list *defs;
8182159047fSniklas };
8192159047fSniklas 
8202159047fSniklas /* An archive hash table itself.  */
8212159047fSniklas 
8222159047fSniklas struct archive_hash_table
8232159047fSniklas {
8242159047fSniklas   struct bfd_hash_table table;
8252159047fSniklas };
8262159047fSniklas 
8272159047fSniklas /* Create a new entry for an archive hash table.  */
8282159047fSniklas 
8292159047fSniklas static struct bfd_hash_entry *
archive_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)830*007c2a45Smiod archive_hash_newfunc (struct bfd_hash_entry *entry,
831*007c2a45Smiod 		      struct bfd_hash_table *table,
832*007c2a45Smiod 		      const char *string)
8332159047fSniklas {
8342159047fSniklas   struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
8352159047fSniklas 
8362159047fSniklas   /* Allocate the structure if it has not already been allocated by a
8372159047fSniklas      subclass.  */
838*007c2a45Smiod   if (ret == NULL)
839*007c2a45Smiod     ret = bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
840*007c2a45Smiod   if (ret == NULL)
8412159047fSniklas     return NULL;
8422159047fSniklas 
8432159047fSniklas   /* Call the allocation method of the superclass.  */
8442159047fSniklas   ret = ((struct archive_hash_entry *)
8452159047fSniklas 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
8462159047fSniklas 
8472159047fSniklas   if (ret)
8482159047fSniklas     {
8492159047fSniklas       /* Initialize the local fields.  */
850*007c2a45Smiod       ret->defs = NULL;
8512159047fSniklas     }
8522159047fSniklas 
853*007c2a45Smiod   return &ret->root;
8542159047fSniklas }
8552159047fSniklas 
8562159047fSniklas /* Initialize an archive hash table.  */
8572159047fSniklas 
858c074d1c9Sdrahn 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 *))859*007c2a45Smiod archive_hash_table_init
860*007c2a45Smiod   (struct archive_hash_table *table,
861*007c2a45Smiod    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8622159047fSniklas 				      struct bfd_hash_table *,
863*007c2a45Smiod 				      const char *))
8642159047fSniklas {
8652159047fSniklas   return bfd_hash_table_init (&table->table, newfunc);
8662159047fSniklas }
8672159047fSniklas 
8682159047fSniklas /* Look up an entry in an archive hash table.  */
8692159047fSniklas 
8702159047fSniklas #define archive_hash_lookup(t, string, create, copy) \
8712159047fSniklas   ((struct archive_hash_entry *) \
8722159047fSniklas    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
8732159047fSniklas 
8742159047fSniklas /* Allocate space in an archive hash table.  */
8752159047fSniklas 
8762159047fSniklas #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
8772159047fSniklas 
8782159047fSniklas /* Free an archive hash table.  */
8792159047fSniklas 
8802159047fSniklas #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
8812159047fSniklas 
8822159047fSniklas /* Generic function to add symbols from an archive file to the global
8832159047fSniklas    hash file.  This function presumes that the archive symbol table
8842159047fSniklas    has already been read in (this is normally done by the
8852159047fSniklas    bfd_check_format entry point).  It looks through the undefined and
8862159047fSniklas    common symbols and searches the archive symbol table for them.  If
8872159047fSniklas    it finds an entry, it includes the associated object file in the
8882159047fSniklas    link.
8892159047fSniklas 
8902159047fSniklas    The old linker looked through the archive symbol table for
8912159047fSniklas    undefined symbols.  We do it the other way around, looking through
8922159047fSniklas    undefined symbols for symbols defined in the archive.  The
8932159047fSniklas    advantage of the newer scheme is that we only have to look through
8942159047fSniklas    the list of undefined symbols once, whereas the old method had to
8952159047fSniklas    re-search the symbol table each time a new object file was added.
8962159047fSniklas 
8972159047fSniklas    The CHECKFN argument is used to see if an object file should be
898c074d1c9Sdrahn    included.  CHECKFN should set *PNEEDED to TRUE if the object file
8992159047fSniklas    should be included, and must also call the bfd_link_info
9002159047fSniklas    add_archive_element callback function and handle adding the symbols
901c074d1c9Sdrahn    to the global hash table.  CHECKFN should only return FALSE if some
9022159047fSniklas    sort of error occurs.
9032159047fSniklas 
9042159047fSniklas    For some formats, such as a.out, it is possible to look through an
9052159047fSniklas    object file but not actually include it in the link.  The
9062159047fSniklas    archive_pass field in a BFD is used to avoid checking the symbols
9072159047fSniklas    of an object files too many times.  When an object is included in
9082159047fSniklas    the link, archive_pass is set to -1.  If an object is scanned but
9092159047fSniklas    not included, archive_pass is set to the pass number.  The pass
9102159047fSniklas    number is incremented each time a new object file is included.  The
9112159047fSniklas    pass number is used because when a new object file is included it
9122159047fSniklas    may create new undefined symbols which cause a previously examined
9132159047fSniklas    object file to be included.  */
9142159047fSniklas 
915c074d1c9Sdrahn 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 *))916*007c2a45Smiod _bfd_generic_link_add_archive_symbols
917*007c2a45Smiod   (bfd *abfd,
918*007c2a45Smiod    struct bfd_link_info *info,
919*007c2a45Smiod    bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
9202159047fSniklas {
9212159047fSniklas   carsym *arsyms;
9222159047fSniklas   carsym *arsym_end;
9232159047fSniklas   register carsym *arsym;
9242159047fSniklas   int pass;
9252159047fSniklas   struct archive_hash_table arsym_hash;
926c074d1c9Sdrahn   unsigned int indx;
9272159047fSniklas   struct bfd_link_hash_entry **pundef;
9282159047fSniklas 
9292159047fSniklas   if (! bfd_has_map (abfd))
9302159047fSniklas     {
9312159047fSniklas       /* An empty archive is a special case.  */
932*007c2a45Smiod       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
933c074d1c9Sdrahn 	return TRUE;
9342159047fSniklas       bfd_set_error (bfd_error_no_armap);
935c074d1c9Sdrahn       return FALSE;
9362159047fSniklas     }
9372159047fSniklas 
9382159047fSniklas   arsyms = bfd_ardata (abfd)->symdefs;
9392159047fSniklas   arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
9402159047fSniklas 
9412159047fSniklas   /* In order to quickly determine whether an symbol is defined in
9422159047fSniklas      this archive, we build a hash table of the symbols.  */
9432159047fSniklas   if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc))
944c074d1c9Sdrahn     return FALSE;
9452159047fSniklas   for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
9462159047fSniklas     {
9472159047fSniklas       struct archive_hash_entry *arh;
9482159047fSniklas       struct archive_list *l, **pp;
9492159047fSniklas 
950c074d1c9Sdrahn       arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
951*007c2a45Smiod       if (arh == NULL)
9522159047fSniklas 	goto error_return;
9532159047fSniklas       l = ((struct archive_list *)
9542159047fSniklas 	   archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
9552159047fSniklas       if (l == NULL)
9562159047fSniklas 	goto error_return;
9572159047fSniklas       l->indx = indx;
958*007c2a45Smiod       for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
9592159047fSniklas 	;
9602159047fSniklas       *pp = l;
9612159047fSniklas       l->next = NULL;
9622159047fSniklas     }
9632159047fSniklas 
9642159047fSniklas   /* The archive_pass field in the archive itself is used to
9652159047fSniklas      initialize PASS, sine we may search the same archive multiple
9662159047fSniklas      times.  */
9672159047fSniklas   pass = abfd->archive_pass + 1;
9682159047fSniklas 
9692159047fSniklas   /* New undefined symbols are added to the end of the list, so we
9702159047fSniklas      only need to look through it once.  */
9712159047fSniklas   pundef = &info->hash->undefs;
972*007c2a45Smiod   while (*pundef != NULL)
9732159047fSniklas     {
9742159047fSniklas       struct bfd_link_hash_entry *h;
9752159047fSniklas       struct archive_hash_entry *arh;
9762159047fSniklas       struct archive_list *l;
9772159047fSniklas 
9782159047fSniklas       h = *pundef;
9792159047fSniklas 
9802159047fSniklas       /* When a symbol is defined, it is not necessarily removed from
9812159047fSniklas 	 the list.  */
9822159047fSniklas       if (h->type != bfd_link_hash_undefined
9832159047fSniklas 	  && h->type != bfd_link_hash_common)
9842159047fSniklas 	{
9852159047fSniklas 	  /* Remove this entry from the list, for general cleanliness
9862159047fSniklas 	     and because we are going to look through the list again
9872159047fSniklas 	     if we search any more libraries.  We can't remove the
9882159047fSniklas 	     entry if it is the tail, because that would lose any
9892159047fSniklas 	     entries we add to the list later on (it would also cause
9902159047fSniklas 	     us to lose track of whether the symbol has been
9912159047fSniklas 	     referenced).  */
9922159047fSniklas 	  if (*pundef != info->hash->undefs_tail)
993*007c2a45Smiod 	    *pundef = (*pundef)->und_next;
9942159047fSniklas 	  else
995*007c2a45Smiod 	    pundef = &(*pundef)->und_next;
9962159047fSniklas 	  continue;
9972159047fSniklas 	}
9982159047fSniklas 
9992159047fSniklas       /* Look for this symbol in the archive symbol map.  */
1000c074d1c9Sdrahn       arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
1001*007c2a45Smiod       if (arh == NULL)
1002c074d1c9Sdrahn 	{
1003c074d1c9Sdrahn 	  /* If we haven't found the exact symbol we're looking for,
1004c074d1c9Sdrahn 	     let's look for its import thunk */
1005c074d1c9Sdrahn 	  if (info->pei386_auto_import)
1006c074d1c9Sdrahn 	    {
1007c074d1c9Sdrahn 	      bfd_size_type amt = strlen (h->root.string) + 10;
1008*007c2a45Smiod 	      char *buf = bfd_malloc (amt);
1009c074d1c9Sdrahn 	      if (buf == NULL)
1010c074d1c9Sdrahn 		return FALSE;
1011c074d1c9Sdrahn 
1012c074d1c9Sdrahn 	      sprintf (buf, "__imp_%s", h->root.string);
1013c074d1c9Sdrahn 	      arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
1014c074d1c9Sdrahn 	      free(buf);
1015c074d1c9Sdrahn 	    }
1016*007c2a45Smiod 	  if (arh == NULL)
10172159047fSniklas 	    {
1018*007c2a45Smiod 	      pundef = &(*pundef)->und_next;
10192159047fSniklas 	      continue;
10202159047fSniklas 	    }
1021c074d1c9Sdrahn 	}
10222159047fSniklas       /* Look at all the objects which define this symbol.  */
1023*007c2a45Smiod       for (l = arh->defs; l != NULL; l = l->next)
10242159047fSniklas 	{
10252159047fSniklas 	  bfd *element;
1026c074d1c9Sdrahn 	  bfd_boolean needed;
10272159047fSniklas 
10282159047fSniklas 	  /* If the symbol has gotten defined along the way, quit.  */
10292159047fSniklas 	  if (h->type != bfd_link_hash_undefined
10302159047fSniklas 	      && h->type != bfd_link_hash_common)
10312159047fSniklas 	    break;
10322159047fSniklas 
10332159047fSniklas 	  element = bfd_get_elt_at_index (abfd, l->indx);
1034*007c2a45Smiod 	  if (element == NULL)
10352159047fSniklas 	    goto error_return;
10362159047fSniklas 
10372159047fSniklas 	  /* If we've already included this element, or if we've
10382159047fSniklas 	     already checked it on this pass, continue.  */
10392159047fSniklas 	  if (element->archive_pass == -1
10402159047fSniklas 	      || element->archive_pass == pass)
10412159047fSniklas 	    continue;
10422159047fSniklas 
10432159047fSniklas 	  /* If we can't figure this element out, just ignore it.  */
10442159047fSniklas 	  if (! bfd_check_format (element, bfd_object))
10452159047fSniklas 	    {
10462159047fSniklas 	      element->archive_pass = -1;
10472159047fSniklas 	      continue;
10482159047fSniklas 	    }
10492159047fSniklas 
10502159047fSniklas 	  /* CHECKFN will see if this element should be included, and
10512159047fSniklas 	     go ahead and include it if appropriate.  */
10522159047fSniklas 	  if (! (*checkfn) (element, info, &needed))
10532159047fSniklas 	    goto error_return;
10542159047fSniklas 
10552159047fSniklas 	  if (! needed)
10562159047fSniklas 	    element->archive_pass = pass;
10572159047fSniklas 	  else
10582159047fSniklas 	    {
10592159047fSniklas 	      element->archive_pass = -1;
10602159047fSniklas 
10612159047fSniklas 	      /* Increment the pass count to show that we may need to
10622159047fSniklas 		 recheck object files which were already checked.  */
10632159047fSniklas 	      ++pass;
10642159047fSniklas 	    }
10652159047fSniklas 	}
10662159047fSniklas 
1067*007c2a45Smiod       pundef = &(*pundef)->und_next;
10682159047fSniklas     }
10692159047fSniklas 
10702159047fSniklas   archive_hash_table_free (&arsym_hash);
10712159047fSniklas 
10722159047fSniklas   /* Save PASS in case we are called again.  */
10732159047fSniklas   abfd->archive_pass = pass;
10742159047fSniklas 
1075c074d1c9Sdrahn   return TRUE;
10762159047fSniklas 
10772159047fSniklas  error_return:
10782159047fSniklas   archive_hash_table_free (&arsym_hash);
1079c074d1c9Sdrahn   return FALSE;
10802159047fSniklas }
10812159047fSniklas 
10822159047fSniklas /* See if we should include an archive element.  This version is used
10832159047fSniklas    when we do not want to automatically collect constructors based on
10842159047fSniklas    the symbol name, presumably because we have some other mechanism
10852159047fSniklas    for finding them.  */
10862159047fSniklas 
1087c074d1c9Sdrahn static bfd_boolean
generic_link_check_archive_element_no_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)1088*007c2a45Smiod generic_link_check_archive_element_no_collect (
1089*007c2a45Smiod 					       bfd *abfd,
1090*007c2a45Smiod 					       struct bfd_link_info *info,
1091*007c2a45Smiod 					       bfd_boolean *pneeded)
10922159047fSniklas {
1093c074d1c9Sdrahn   return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
10942159047fSniklas }
10952159047fSniklas 
10962159047fSniklas /* See if we should include an archive element.  This version is used
10972159047fSniklas    when we want to automatically collect constructors based on the
10982159047fSniklas    symbol name, as collect2 does.  */
10992159047fSniklas 
1100c074d1c9Sdrahn static bfd_boolean
generic_link_check_archive_element_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)1101*007c2a45Smiod generic_link_check_archive_element_collect (bfd *abfd,
1102*007c2a45Smiod 					    struct bfd_link_info *info,
1103*007c2a45Smiod 					    bfd_boolean *pneeded)
11042159047fSniklas {
1105c074d1c9Sdrahn   return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
11062159047fSniklas }
11072159047fSniklas 
11082159047fSniklas /* See if we should include an archive element.  Optionally collect
11092159047fSniklas    constructors.  */
11102159047fSniklas 
1111c074d1c9Sdrahn static bfd_boolean
generic_link_check_archive_element(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded,bfd_boolean collect)1112*007c2a45Smiod generic_link_check_archive_element (bfd *abfd,
1113*007c2a45Smiod 				    struct bfd_link_info *info,
1114*007c2a45Smiod 				    bfd_boolean *pneeded,
1115*007c2a45Smiod 				    bfd_boolean collect)
11162159047fSniklas {
11172159047fSniklas   asymbol **pp, **ppend;
11182159047fSniklas 
1119c074d1c9Sdrahn   *pneeded = FALSE;
11202159047fSniklas 
11212159047fSniklas   if (! generic_link_read_symbols (abfd))
1122c074d1c9Sdrahn     return FALSE;
11232159047fSniklas 
11242159047fSniklas   pp = _bfd_generic_link_get_symbols (abfd);
11252159047fSniklas   ppend = pp + _bfd_generic_link_get_symcount (abfd);
11262159047fSniklas   for (; pp < ppend; pp++)
11272159047fSniklas     {
11282159047fSniklas       asymbol *p;
11292159047fSniklas       struct bfd_link_hash_entry *h;
11302159047fSniklas 
11312159047fSniklas       p = *pp;
11322159047fSniklas 
11332159047fSniklas       /* We are only interested in globally visible symbols.  */
11342159047fSniklas       if (! bfd_is_com_section (p->section)
11352159047fSniklas 	  && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
11362159047fSniklas 	continue;
11372159047fSniklas 
11382159047fSniklas       /* We are only interested if we know something about this
11392159047fSniklas 	 symbol, and it is undefined or common.  An undefined weak
11402159047fSniklas 	 symbol (type bfd_link_hash_undefweak) is not considered to be
11412159047fSniklas 	 a reference when pulling files out of an archive.  See the
11422159047fSniklas 	 SVR4 ABI, p. 4-27.  */
1143c074d1c9Sdrahn       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1144c074d1c9Sdrahn 				FALSE, TRUE);
1145*007c2a45Smiod       if (h == NULL
11462159047fSniklas 	  || (h->type != bfd_link_hash_undefined
11472159047fSniklas 	      && h->type != bfd_link_hash_common))
11482159047fSniklas 	continue;
11492159047fSniklas 
11502159047fSniklas       /* P is a symbol we are looking for.  */
11512159047fSniklas 
11522159047fSniklas       if (! bfd_is_com_section (p->section))
11532159047fSniklas 	{
11542159047fSniklas 	  bfd_size_type symcount;
11552159047fSniklas 	  asymbol **symbols;
11562159047fSniklas 
11572159047fSniklas 	  /* This object file defines this symbol, so pull it in.  */
11582159047fSniklas 	  if (! (*info->callbacks->add_archive_element) (info, abfd,
11592159047fSniklas 							 bfd_asymbol_name (p)))
1160c074d1c9Sdrahn 	    return FALSE;
11612159047fSniklas 	  symcount = _bfd_generic_link_get_symcount (abfd);
11622159047fSniklas 	  symbols = _bfd_generic_link_get_symbols (abfd);
11632159047fSniklas 	  if (! generic_link_add_symbol_list (abfd, info, symcount,
11642159047fSniklas 					      symbols, collect))
1165c074d1c9Sdrahn 	    return FALSE;
1166c074d1c9Sdrahn 	  *pneeded = TRUE;
1167c074d1c9Sdrahn 	  return TRUE;
11682159047fSniklas 	}
11692159047fSniklas 
11702159047fSniklas       /* P is a common symbol.  */
11712159047fSniklas 
11722159047fSniklas       if (h->type == bfd_link_hash_undefined)
11732159047fSniklas 	{
11742159047fSniklas 	  bfd *symbfd;
11752159047fSniklas 	  bfd_vma size;
11762159047fSniklas 	  unsigned int power;
11772159047fSniklas 
11782159047fSniklas 	  symbfd = h->u.undef.abfd;
1179*007c2a45Smiod 	  if (symbfd == NULL)
11802159047fSniklas 	    {
11812159047fSniklas 	      /* This symbol was created as undefined from outside
11822159047fSniklas 		 BFD.  We assume that we should link in the object
11832159047fSniklas 		 file.  This is for the -u option in the linker.  */
11842159047fSniklas 	      if (! (*info->callbacks->add_archive_element)
11852159047fSniklas 		  (info, abfd, bfd_asymbol_name (p)))
1186c074d1c9Sdrahn 		return FALSE;
1187c074d1c9Sdrahn 	      *pneeded = TRUE;
1188c074d1c9Sdrahn 	      return TRUE;
11892159047fSniklas 	    }
11902159047fSniklas 
11912159047fSniklas 	  /* Turn the symbol into a common symbol but do not link in
11922159047fSniklas 	     the object file.  This is how a.out works.  Object
11932159047fSniklas 	     formats that require different semantics must implement
11942159047fSniklas 	     this function differently.  This symbol is already on the
11952159047fSniklas 	     undefs list.  We add the section to a common section
11962159047fSniklas 	     attached to symbfd to ensure that it is in a BFD which
11972159047fSniklas 	     will be linked in.  */
11982159047fSniklas 	  h->type = bfd_link_hash_common;
11992159047fSniklas 	  h->u.c.p =
12002159047fSniklas 	    bfd_hash_allocate (&info->hash->table,
1201*007c2a45Smiod 			       sizeof (struct bfd_link_hash_common_entry));
12022159047fSniklas 	  if (h->u.c.p == NULL)
1203c074d1c9Sdrahn 	    return FALSE;
12042159047fSniklas 
12052159047fSniklas 	  size = bfd_asymbol_value (p);
12062159047fSniklas 	  h->u.c.size = size;
12072159047fSniklas 
12082159047fSniklas 	  power = bfd_log2 (size);
12092159047fSniklas 	  if (power > 4)
12102159047fSniklas 	    power = 4;
12112159047fSniklas 	  h->u.c.p->alignment_power = power;
12122159047fSniklas 
12132159047fSniklas 	  if (p->section == bfd_com_section_ptr)
12142159047fSniklas 	    h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
12152159047fSniklas 	  else
12162159047fSniklas 	    h->u.c.p->section = bfd_make_section_old_way (symbfd,
12172159047fSniklas 							  p->section->name);
12182159047fSniklas 	  h->u.c.p->section->flags = SEC_ALLOC;
12192159047fSniklas 	}
12202159047fSniklas       else
12212159047fSniklas 	{
12222159047fSniklas 	  /* Adjust the size of the common symbol if necessary.  This
12232159047fSniklas 	     is how a.out works.  Object formats that require
12242159047fSniklas 	     different semantics must implement this function
12252159047fSniklas 	     differently.  */
12262159047fSniklas 	  if (bfd_asymbol_value (p) > h->u.c.size)
12272159047fSniklas 	    h->u.c.size = bfd_asymbol_value (p);
12282159047fSniklas 	}
12292159047fSniklas     }
12302159047fSniklas 
12312159047fSniklas   /* This archive element is not needed.  */
1232c074d1c9Sdrahn   return TRUE;
12332159047fSniklas }
12342159047fSniklas 
12352159047fSniklas /* Add the symbols from an object file to the global hash table.  ABFD
12362159047fSniklas    is the object file.  INFO is the linker information.  SYMBOL_COUNT
12372159047fSniklas    is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
1238c074d1c9Sdrahn    is TRUE if constructors should be automatically collected by name
12392159047fSniklas    as is done by collect2.  */
12402159047fSniklas 
1241c074d1c9Sdrahn 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)1242*007c2a45Smiod generic_link_add_symbol_list (bfd *abfd,
1243*007c2a45Smiod 			      struct bfd_link_info *info,
1244*007c2a45Smiod 			      bfd_size_type symbol_count,
1245*007c2a45Smiod 			      asymbol **symbols,
1246*007c2a45Smiod 			      bfd_boolean collect)
12472159047fSniklas {
12482159047fSniklas   asymbol **pp, **ppend;
12492159047fSniklas 
12502159047fSniklas   pp = symbols;
12512159047fSniklas   ppend = symbols + symbol_count;
12522159047fSniklas   for (; pp < ppend; pp++)
12532159047fSniklas     {
12542159047fSniklas       asymbol *p;
12552159047fSniklas 
12562159047fSniklas       p = *pp;
12572159047fSniklas 
12582159047fSniklas       if ((p->flags & (BSF_INDIRECT
12592159047fSniklas 		       | BSF_WARNING
12602159047fSniklas 		       | BSF_GLOBAL
12612159047fSniklas 		       | BSF_CONSTRUCTOR
12622159047fSniklas 		       | BSF_WEAK)) != 0
12632159047fSniklas 	  || bfd_is_und_section (bfd_get_section (p))
12642159047fSniklas 	  || bfd_is_com_section (bfd_get_section (p))
12652159047fSniklas 	  || bfd_is_ind_section (bfd_get_section (p)))
12662159047fSniklas 	{
12672159047fSniklas 	  const char *name;
12682159047fSniklas 	  const char *string;
12692159047fSniklas 	  struct generic_link_hash_entry *h;
1270c074d1c9Sdrahn 	  struct bfd_link_hash_entry *bh;
12712159047fSniklas 
12722159047fSniklas 	  name = bfd_asymbol_name (p);
12732159047fSniklas 	  if (((p->flags & BSF_INDIRECT) != 0
12742159047fSniklas 	       || bfd_is_ind_section (p->section))
12752159047fSniklas 	      && pp + 1 < ppend)
12762159047fSniklas 	    {
12772159047fSniklas 	      pp++;
12782159047fSniklas 	      string = bfd_asymbol_name (*pp);
12792159047fSniklas 	    }
12802159047fSniklas 	  else if ((p->flags & BSF_WARNING) != 0
12812159047fSniklas 		   && pp + 1 < ppend)
12822159047fSniklas 	    {
12832159047fSniklas 	      /* The name of P is actually the warning string, and the
12842159047fSniklas 		 next symbol is the one to warn about.  */
12852159047fSniklas 	      string = name;
12862159047fSniklas 	      pp++;
12872159047fSniklas 	      name = bfd_asymbol_name (*pp);
12882159047fSniklas 	    }
12892159047fSniklas 	  else
12902159047fSniklas 	    string = NULL;
12912159047fSniklas 
1292c074d1c9Sdrahn 	  bh = NULL;
12932159047fSniklas 	  if (! (_bfd_generic_link_add_one_symbol
12942159047fSniklas 		 (info, abfd, name, p->flags, bfd_get_section (p),
1295c074d1c9Sdrahn 		  p->value, string, FALSE, collect, &bh)))
1296c074d1c9Sdrahn 	    return FALSE;
1297c074d1c9Sdrahn 	  h = (struct generic_link_hash_entry *) bh;
12982159047fSniklas 
12992159047fSniklas 	  /* If this is a constructor symbol, and the linker didn't do
13002159047fSniklas              anything with it, then we want to just pass the symbol
13012159047fSniklas              through to the output file.  This will happen when
13022159047fSniklas              linking with -r.  */
13032159047fSniklas 	  if ((p->flags & BSF_CONSTRUCTOR) != 0
13042159047fSniklas 	      && (h == NULL || h->root.type == bfd_link_hash_new))
13052159047fSniklas 	    {
13062159047fSniklas 	      p->udata.p = NULL;
13072159047fSniklas 	      continue;
13082159047fSniklas 	    }
13092159047fSniklas 
13102159047fSniklas 	  /* Save the BFD symbol so that we don't lose any backend
13112159047fSniklas 	     specific information that may be attached to it.  We only
13122159047fSniklas 	     want this one if it gives more information than the
13132159047fSniklas 	     existing one; we don't want to replace a defined symbol
13142159047fSniklas 	     with an undefined one.  This routine may be called with a
13152159047fSniklas 	     hash table other than the generic hash table, so we only
13162159047fSniklas 	     do this if we are certain that the hash table is a
13172159047fSniklas 	     generic one.  */
13182159047fSniklas 	  if (info->hash->creator == abfd->xvec)
13192159047fSniklas 	    {
1320*007c2a45Smiod 	      if (h->sym == NULL
13212159047fSniklas 		  || (! bfd_is_und_section (bfd_get_section (p))
13222159047fSniklas 		      && (! bfd_is_com_section (bfd_get_section (p))
13232159047fSniklas 			  || bfd_is_und_section (bfd_get_section (h->sym)))))
13242159047fSniklas 		{
13252159047fSniklas 		  h->sym = p;
13262159047fSniklas 		  /* BSF_OLD_COMMON is a hack to support COFF reloc
13272159047fSniklas 		     reading, and it should go away when the COFF
13282159047fSniklas 		     linker is switched to the new version.  */
13292159047fSniklas 		  if (bfd_is_com_section (bfd_get_section (p)))
13302159047fSniklas 		    p->flags |= BSF_OLD_COMMON;
13312159047fSniklas 		}
1332b305b0f1Sespie 	    }
13332159047fSniklas 
13342159047fSniklas 	  /* Store a back pointer from the symbol to the hash
13352159047fSniklas 	     table entry for the benefit of relaxation code until
13362159047fSniklas 	     it gets rewritten to not use asymbol structures.
13372159047fSniklas 	     Setting this is also used to check whether these
13382159047fSniklas 	     symbols were set up by the generic linker.  */
1339*007c2a45Smiod 	  p->udata.p = h;
13402159047fSniklas 	}
13412159047fSniklas     }
13422159047fSniklas 
1343c074d1c9Sdrahn   return TRUE;
13442159047fSniklas }
13452159047fSniklas 
13462159047fSniklas /* We use a state table to deal with adding symbols from an object
13472159047fSniklas    file.  The first index into the state table describes the symbol
13482159047fSniklas    from the object file.  The second index into the state table is the
13492159047fSniklas    type of the symbol in the hash table.  */
13502159047fSniklas 
13512159047fSniklas /* The symbol from the object file is turned into one of these row
13522159047fSniklas    values.  */
13532159047fSniklas 
13542159047fSniklas enum link_row
13552159047fSniklas {
13562159047fSniklas   UNDEF_ROW,		/* Undefined.  */
13572159047fSniklas   UNDEFW_ROW,		/* Weak undefined.  */
13582159047fSniklas   DEF_ROW,		/* Defined.  */
13592159047fSniklas   DEFW_ROW,		/* Weak defined.  */
13602159047fSniklas   COMMON_ROW,		/* Common.  */
13612159047fSniklas   INDR_ROW,		/* Indirect.  */
13622159047fSniklas   WARN_ROW,		/* Warning.  */
13632159047fSniklas   SET_ROW		/* Member of set.  */
13642159047fSniklas };
13652159047fSniklas 
13662159047fSniklas /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
13672159047fSniklas #undef FAIL
13682159047fSniklas 
13692159047fSniklas /* The actions to take in the state table.  */
13702159047fSniklas 
13712159047fSniklas enum link_action
13722159047fSniklas {
13732159047fSniklas   FAIL,		/* Abort.  */
13742159047fSniklas   UND,		/* Mark symbol undefined.  */
13752159047fSniklas   WEAK,		/* Mark symbol weak undefined.  */
13762159047fSniklas   DEF,		/* Mark symbol defined.  */
13772159047fSniklas   DEFW,		/* Mark symbol weak defined.  */
13782159047fSniklas   COM,		/* Mark symbol common.  */
13792159047fSniklas   REF,		/* Mark defined symbol referenced.  */
13802159047fSniklas   CREF,		/* Possibly warn about common reference to defined symbol.  */
13812159047fSniklas   CDEF,		/* Define existing common symbol.  */
13822159047fSniklas   NOACT,	/* No action.  */
13832159047fSniklas   BIG,		/* Mark symbol common using largest size.  */
13842159047fSniklas   MDEF,		/* Multiple definition error.  */
13852159047fSniklas   MIND,		/* Multiple indirect symbols.  */
13862159047fSniklas   IND,		/* Make indirect symbol.  */
13872159047fSniklas   CIND,		/* Make indirect symbol from existing common symbol.  */
13882159047fSniklas   SET,		/* Add value to set.  */
13892159047fSniklas   MWARN,	/* Make warning symbol.  */
13902159047fSniklas   WARN,		/* Issue warning.  */
13912159047fSniklas   CWARN,	/* Warn if referenced, else MWARN.  */
13922159047fSniklas   CYCLE,	/* Repeat with symbol pointed to.  */
13932159047fSniklas   REFC,		/* Mark indirect symbol referenced and then CYCLE.  */
13942159047fSniklas   WARNC		/* Issue warning and then CYCLE.  */
13952159047fSniklas };
13962159047fSniklas 
13972159047fSniklas /* The state table itself.  The first index is a link_row and the
13982159047fSniklas    second index is a bfd_link_hash_type.  */
13992159047fSniklas 
14002159047fSniklas static const enum link_action link_action[8][8] =
14012159047fSniklas {
14022159047fSniklas   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
14032159047fSniklas   /* UNDEF_ROW 	*/  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
14042159047fSniklas   /* UNDEFW_ROW	*/  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
14052159047fSniklas   /* DEF_ROW 	*/  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
14062159047fSniklas   /* DEFW_ROW 	*/  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
1407c074d1c9Sdrahn   /* COMMON_ROW	*/  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
14082159047fSniklas   /* INDR_ROW	*/  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
1409c074d1c9Sdrahn   /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, NOACT },
14102159047fSniklas   /* SET_ROW	*/  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
14112159047fSniklas };
14122159047fSniklas 
14132159047fSniklas /* Most of the entries in the LINK_ACTION table are straightforward,
14142159047fSniklas    but a few are somewhat subtle.
14152159047fSniklas 
14162159047fSniklas    A reference to an indirect symbol (UNDEF_ROW/indr or
14172159047fSniklas    UNDEFW_ROW/indr) is counted as a reference both to the indirect
14182159047fSniklas    symbol and to the symbol the indirect symbol points to.
14192159047fSniklas 
14202159047fSniklas    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
14212159047fSniklas    causes the warning to be issued.
14222159047fSniklas 
14232159047fSniklas    A common definition of an indirect symbol (COMMON_ROW/indr) is
14242159047fSniklas    treated as a multiple definition error.  Likewise for an indirect
14252159047fSniklas    definition of a common symbol (INDR_ROW/com).
14262159047fSniklas 
14272159047fSniklas    An indirect definition of a warning (INDR_ROW/warn) does not cause
14282159047fSniklas    the warning to be issued.
14292159047fSniklas 
14302159047fSniklas    If a warning is created for an indirect symbol (WARN_ROW/indr) no
14312159047fSniklas    warning is created for the symbol the indirect symbol points to.
14322159047fSniklas 
14332159047fSniklas    Adding an entry to a set does not count as a reference to a set,
14342159047fSniklas    and no warning is issued (SET_ROW/warn).  */
14352159047fSniklas 
14362159047fSniklas /* Return the BFD in which a hash entry has been defined, if known.  */
14372159047fSniklas 
14382159047fSniklas static bfd *
hash_entry_bfd(struct bfd_link_hash_entry * h)1439*007c2a45Smiod hash_entry_bfd (struct bfd_link_hash_entry *h)
14402159047fSniklas {
14412159047fSniklas   while (h->type == bfd_link_hash_warning)
14422159047fSniklas     h = h->u.i.link;
14432159047fSniklas   switch (h->type)
14442159047fSniklas     {
14452159047fSniklas     default:
14462159047fSniklas       return NULL;
14472159047fSniklas     case bfd_link_hash_undefined:
14482159047fSniklas     case bfd_link_hash_undefweak:
14492159047fSniklas       return h->u.undef.abfd;
14502159047fSniklas     case bfd_link_hash_defined:
14512159047fSniklas     case bfd_link_hash_defweak:
14522159047fSniklas       return h->u.def.section->owner;
14532159047fSniklas     case bfd_link_hash_common:
14542159047fSniklas       return h->u.c.p->section->owner;
14552159047fSniklas     }
14562159047fSniklas   /*NOTREACHED*/
14572159047fSniklas }
14582159047fSniklas 
14592159047fSniklas /* Add a symbol to the global hash table.
14602159047fSniklas    ABFD is the BFD the symbol comes from.
14612159047fSniklas    NAME is the name of the symbol.
14622159047fSniklas    FLAGS is the BSF_* bits associated with the symbol.
14632159047fSniklas    SECTION is the section in which the symbol is defined; this may be
14642159047fSniklas      bfd_und_section_ptr or bfd_com_section_ptr.
14652159047fSniklas    VALUE is the value of the symbol, relative to the section.
14662159047fSniklas    STRING is used for either an indirect symbol, in which case it is
14672159047fSniklas      the name of the symbol to indirect to, or a warning symbol, in
14682159047fSniklas      which case it is the warning string.
1469c074d1c9Sdrahn    COPY is TRUE if NAME or STRING must be copied into locally
14702159047fSniklas      allocated memory if they need to be saved.
1471c074d1c9Sdrahn    COLLECT is TRUE if we should automatically collect gcc constructor
14722159047fSniklas      or destructor names as collect2 does.
14732159047fSniklas    HASHP, if not NULL, is a place to store the created hash table
14742159047fSniklas      entry; if *HASHP is not NULL, the caller has already looked up
14752159047fSniklas      the hash table entry, and stored it in *HASHP.  */
14762159047fSniklas 
1477c074d1c9Sdrahn 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)1478*007c2a45Smiod _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1479*007c2a45Smiod 				  bfd *abfd,
1480*007c2a45Smiod 				  const char *name,
1481*007c2a45Smiod 				  flagword flags,
1482*007c2a45Smiod 				  asection *section,
1483*007c2a45Smiod 				  bfd_vma value,
1484*007c2a45Smiod 				  const char *string,
1485*007c2a45Smiod 				  bfd_boolean copy,
1486*007c2a45Smiod 				  bfd_boolean collect,
1487*007c2a45Smiod 				  struct bfd_link_hash_entry **hashp)
14882159047fSniklas {
14892159047fSniklas   enum link_row row;
14902159047fSniklas   struct bfd_link_hash_entry *h;
1491c074d1c9Sdrahn   bfd_boolean cycle;
14922159047fSniklas 
14932159047fSniklas   if (bfd_is_ind_section (section)
14942159047fSniklas       || (flags & BSF_INDIRECT) != 0)
14952159047fSniklas     row = INDR_ROW;
14962159047fSniklas   else if ((flags & BSF_WARNING) != 0)
14972159047fSniklas     row = WARN_ROW;
14982159047fSniklas   else if ((flags & BSF_CONSTRUCTOR) != 0)
14992159047fSniklas     row = SET_ROW;
15002159047fSniklas   else if (bfd_is_und_section (section))
15012159047fSniklas     {
15022159047fSniklas       if ((flags & BSF_WEAK) != 0)
15032159047fSniklas 	row = UNDEFW_ROW;
15042159047fSniklas       else
15052159047fSniklas 	row = UNDEF_ROW;
15062159047fSniklas     }
15072159047fSniklas   else if ((flags & BSF_WEAK) != 0)
15082159047fSniklas     row = DEFW_ROW;
15092159047fSniklas   else if (bfd_is_com_section (section))
15102159047fSniklas     row = COMMON_ROW;
15112159047fSniklas   else
15122159047fSniklas     row = DEF_ROW;
15132159047fSniklas 
15142159047fSniklas   if (hashp != NULL && *hashp != NULL)
15152159047fSniklas     h = *hashp;
15162159047fSniklas   else
15172159047fSniklas     {
1518c88b1d6cSniklas       if (row == UNDEF_ROW || row == UNDEFW_ROW)
1519c074d1c9Sdrahn 	h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1520c88b1d6cSniklas       else
1521c074d1c9Sdrahn 	h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
15222159047fSniklas       if (h == NULL)
15232159047fSniklas 	{
15242159047fSniklas 	  if (hashp != NULL)
15252159047fSniklas 	    *hashp = NULL;
1526c074d1c9Sdrahn 	  return FALSE;
15272159047fSniklas 	}
15282159047fSniklas     }
15292159047fSniklas 
1530c88b1d6cSniklas   if (info->notice_all
1531*007c2a45Smiod       || (info->notice_hash != NULL
1532*007c2a45Smiod 	  && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
15332159047fSniklas     {
1534c88b1d6cSniklas       if (! (*info->callbacks->notice) (info, h->root.string, abfd, section,
1535c88b1d6cSniklas 					value))
1536c074d1c9Sdrahn 	return FALSE;
15372159047fSniklas     }
15382159047fSniklas 
1539*007c2a45Smiod   if (hashp != NULL)
15402159047fSniklas     *hashp = h;
15412159047fSniklas 
15422159047fSniklas   do
15432159047fSniklas     {
15442159047fSniklas       enum link_action action;
15452159047fSniklas 
1546c074d1c9Sdrahn       cycle = FALSE;
15472159047fSniklas       action = link_action[(int) row][(int) h->type];
15482159047fSniklas       switch (action)
15492159047fSniklas 	{
15502159047fSniklas 	case FAIL:
15512159047fSniklas 	  abort ();
15522159047fSniklas 
15532159047fSniklas 	case NOACT:
15542159047fSniklas 	  /* Do nothing.  */
15552159047fSniklas 	  break;
15562159047fSniklas 
15572159047fSniklas 	case UND:
15582159047fSniklas 	  /* Make a new undefined symbol.  */
15592159047fSniklas 	  h->type = bfd_link_hash_undefined;
15602159047fSniklas 	  h->u.undef.abfd = abfd;
15612159047fSniklas 	  bfd_link_add_undef (info->hash, h);
15622159047fSniklas 	  break;
15632159047fSniklas 
15642159047fSniklas 	case WEAK:
15652159047fSniklas 	  /* Make a new weak undefined symbol.  */
15662159047fSniklas 	  h->type = bfd_link_hash_undefweak;
15672159047fSniklas 	  h->u.undef.abfd = abfd;
15682159047fSniklas 	  break;
15692159047fSniklas 
15702159047fSniklas 	case CDEF:
15712159047fSniklas 	  /* We have found a definition for a symbol which was
15722159047fSniklas 	     previously common.  */
15732159047fSniklas 	  BFD_ASSERT (h->type == bfd_link_hash_common);
15742159047fSniklas 	  if (! ((*info->callbacks->multiple_common)
1575c88b1d6cSniklas 		 (info, h->root.string,
15762159047fSniklas 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1577*007c2a45Smiod 		  abfd, bfd_link_hash_defined, 0)))
1578c074d1c9Sdrahn 	    return FALSE;
15792159047fSniklas 	  /* Fall through.  */
15802159047fSniklas 	case DEF:
15812159047fSniklas 	case DEFW:
15822159047fSniklas 	  {
15832159047fSniklas 	    enum bfd_link_hash_type oldtype;
15842159047fSniklas 
15852159047fSniklas 	    /* Define a symbol.  */
15862159047fSniklas 	    oldtype = h->type;
15872159047fSniklas 	    if (action == DEFW)
15882159047fSniklas 	      h->type = bfd_link_hash_defweak;
15892159047fSniklas 	    else
15902159047fSniklas 	      h->type = bfd_link_hash_defined;
15912159047fSniklas 	    h->u.def.section = section;
15922159047fSniklas 	    h->u.def.value = value;
15932159047fSniklas 
15942159047fSniklas 	    /* If we have been asked to, we act like collect2 and
15952159047fSniklas 	       identify all functions that might be global
15962159047fSniklas 	       constructors and destructors and pass them up in a
15972159047fSniklas 	       callback.  We only do this for certain object file
15982159047fSniklas 	       types, since many object file types can handle this
15992159047fSniklas 	       automatically.  */
16002159047fSniklas 	    if (collect && name[0] == '_')
16012159047fSniklas 	      {
16022159047fSniklas 		const char *s;
16032159047fSniklas 
16042159047fSniklas 		/* A constructor or destructor name starts like this:
16052159047fSniklas 		   _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
16062159047fSniklas 		   the second are the same character (we accept any
16072159047fSniklas 		   character there, in case a new object file format
16082159047fSniklas 		   comes along with even worse naming restrictions).  */
16092159047fSniklas 
16102159047fSniklas #define CONS_PREFIX "GLOBAL_"
16112159047fSniklas #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
16122159047fSniklas 
16132159047fSniklas 		s = name + 1;
16142159047fSniklas 		while (*s == '_')
16152159047fSniklas 		  ++s;
16162159047fSniklas 		if (s[0] == 'G'
16172159047fSniklas 		    && strncmp (s, CONS_PREFIX, CONS_PREFIX_LEN - 1) == 0)
16182159047fSniklas 		  {
16192159047fSniklas 		    char c;
16202159047fSniklas 
16212159047fSniklas 		    c = s[CONS_PREFIX_LEN + 1];
16222159047fSniklas 		    if ((c == 'I' || c == 'D')
16232159047fSniklas 			&& s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
16242159047fSniklas 		      {
16252159047fSniklas 			/* If this is a definition of a symbol which
16262159047fSniklas                            was previously weakly defined, we are in
16272159047fSniklas                            trouble.  We have already added a
16282159047fSniklas                            constructor entry for the weak defined
16292159047fSniklas                            symbol, and now we are trying to add one
16302159047fSniklas                            for the new symbol.  Fortunately, this case
16312159047fSniklas                            should never arise in practice.  */
16322159047fSniklas 			if (oldtype == bfd_link_hash_defweak)
16332159047fSniklas 			  abort ();
16342159047fSniklas 
16352159047fSniklas 			if (! ((*info->callbacks->constructor)
1636c074d1c9Sdrahn 			       (info, c == 'I',
1637c88b1d6cSniklas 				h->root.string, abfd, section, value)))
1638c074d1c9Sdrahn 			  return FALSE;
16392159047fSniklas 		      }
16402159047fSniklas 		  }
16412159047fSniklas 	      }
16422159047fSniklas 	  }
16432159047fSniklas 
16442159047fSniklas 	  break;
16452159047fSniklas 
16462159047fSniklas 	case COM:
16472159047fSniklas 	  /* We have found a common definition for a symbol.  */
16482159047fSniklas 	  if (h->type == bfd_link_hash_new)
16492159047fSniklas 	    bfd_link_add_undef (info->hash, h);
16502159047fSniklas 	  h->type = bfd_link_hash_common;
16512159047fSniklas 	  h->u.c.p =
16522159047fSniklas 	    bfd_hash_allocate (&info->hash->table,
1653*007c2a45Smiod 			       sizeof (struct bfd_link_hash_common_entry));
16542159047fSniklas 	  if (h->u.c.p == NULL)
1655c074d1c9Sdrahn 	    return FALSE;
16562159047fSniklas 
16572159047fSniklas 	  h->u.c.size = value;
16582159047fSniklas 
16592159047fSniklas 	  /* Select a default alignment based on the size.  This may
16602159047fSniklas              be overridden by the caller.  */
16612159047fSniklas 	  {
16622159047fSniklas 	    unsigned int power;
16632159047fSniklas 
16642159047fSniklas 	    power = bfd_log2 (value);
16652159047fSniklas 	    if (power > 4)
16662159047fSniklas 	      power = 4;
16672159047fSniklas 	    h->u.c.p->alignment_power = power;
16682159047fSniklas 	  }
16692159047fSniklas 
16702159047fSniklas 	  /* The section of a common symbol is only used if the common
16712159047fSniklas              symbol is actually allocated.  It basically provides a
16722159047fSniklas              hook for the linker script to decide which output section
16732159047fSniklas              the common symbols should be put in.  In most cases, the
16742159047fSniklas              section of a common symbol will be bfd_com_section_ptr,
16752159047fSniklas              the code here will choose a common symbol section named
16762159047fSniklas              "COMMON", and the linker script will contain *(COMMON) in
16772159047fSniklas              the appropriate place.  A few targets use separate common
16782159047fSniklas              sections for small symbols, and they require special
16792159047fSniklas              handling.  */
16802159047fSniklas 	  if (section == bfd_com_section_ptr)
16812159047fSniklas 	    {
16822159047fSniklas 	      h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
16832159047fSniklas 	      h->u.c.p->section->flags = SEC_ALLOC;
16842159047fSniklas 	    }
16852159047fSniklas 	  else if (section->owner != abfd)
16862159047fSniklas 	    {
16872159047fSniklas 	      h->u.c.p->section = bfd_make_section_old_way (abfd,
16882159047fSniklas 							    section->name);
16892159047fSniklas 	      h->u.c.p->section->flags = SEC_ALLOC;
16902159047fSniklas 	    }
16912159047fSniklas 	  else
16922159047fSniklas 	    h->u.c.p->section = section;
16932159047fSniklas 	  break;
16942159047fSniklas 
16952159047fSniklas 	case REF:
16962159047fSniklas 	  /* A reference to a defined symbol.  */
1697*007c2a45Smiod 	  if (h->und_next == NULL && info->hash->undefs_tail != h)
1698*007c2a45Smiod 	    h->und_next = h;
16992159047fSniklas 	  break;
17002159047fSniklas 
17012159047fSniklas 	case BIG:
17022159047fSniklas 	  /* We have found a common definition for a symbol which
17032159047fSniklas 	     already had a common definition.  Use the maximum of the
1704c074d1c9Sdrahn 	     two sizes, and use the section required by the larger symbol.  */
17052159047fSniklas 	  BFD_ASSERT (h->type == bfd_link_hash_common);
17062159047fSniklas 	  if (! ((*info->callbacks->multiple_common)
1707c88b1d6cSniklas 		 (info, h->root.string,
17082159047fSniklas 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
17092159047fSniklas 		  abfd, bfd_link_hash_common, value)))
1710c074d1c9Sdrahn 	    return FALSE;
17112159047fSniklas 	  if (value > h->u.c.size)
17122159047fSniklas 	    {
17132159047fSniklas 	      unsigned int power;
17142159047fSniklas 
17152159047fSniklas 	      h->u.c.size = value;
17162159047fSniklas 
17172159047fSniklas 	      /* Select a default alignment based on the size.  This may
17182159047fSniklas 		 be overridden by the caller.  */
17192159047fSniklas 	      power = bfd_log2 (value);
17202159047fSniklas 	      if (power > 4)
17212159047fSniklas 		power = 4;
17222159047fSniklas 	      h->u.c.p->alignment_power = power;
1723c074d1c9Sdrahn 
1724c074d1c9Sdrahn 	      /* Some systems have special treatment for small commons,
1725c074d1c9Sdrahn 		 hence we want to select the section used by the larger
1726c074d1c9Sdrahn 		 symbol.  This makes sure the symbol does not go in a
1727c074d1c9Sdrahn 		 small common section if it is now too large.  */
1728c074d1c9Sdrahn 	      if (section == bfd_com_section_ptr)
1729c074d1c9Sdrahn 		{
1730c074d1c9Sdrahn 		  h->u.c.p->section
1731c074d1c9Sdrahn 		    = bfd_make_section_old_way (abfd, "COMMON");
1732c074d1c9Sdrahn 		  h->u.c.p->section->flags = SEC_ALLOC;
1733c074d1c9Sdrahn 		}
1734c074d1c9Sdrahn 	      else if (section->owner != abfd)
1735c074d1c9Sdrahn 		{
1736c074d1c9Sdrahn 		  h->u.c.p->section
1737c074d1c9Sdrahn 		    = bfd_make_section_old_way (abfd, section->name);
1738c074d1c9Sdrahn 		  h->u.c.p->section->flags = SEC_ALLOC;
1739c074d1c9Sdrahn 		}
1740c074d1c9Sdrahn 	      else
1741c074d1c9Sdrahn 		h->u.c.p->section = section;
17422159047fSniklas 	    }
17432159047fSniklas 	  break;
17442159047fSniklas 
17452159047fSniklas 	case CREF:
17462159047fSniklas 	  {
17472159047fSniklas 	    bfd *obfd;
17482159047fSniklas 
17492159047fSniklas 	    /* We have found a common definition for a symbol which
17502159047fSniklas 	       was already defined.  FIXME: It would nice if we could
17512159047fSniklas 	       report the BFD which defined an indirect symbol, but we
17522159047fSniklas 	       don't have anywhere to store the information.  */
17532159047fSniklas 	    if (h->type == bfd_link_hash_defined
17542159047fSniklas 		|| h->type == bfd_link_hash_defweak)
17552159047fSniklas 	      obfd = h->u.def.section->owner;
17562159047fSniklas 	    else
17572159047fSniklas 	      obfd = NULL;
17582159047fSniklas 	    if (! ((*info->callbacks->multiple_common)
1759*007c2a45Smiod 		   (info, h->root.string, obfd, h->type, 0,
17602159047fSniklas 		    abfd, bfd_link_hash_common, value)))
1761c074d1c9Sdrahn 	      return FALSE;
17622159047fSniklas 	  }
17632159047fSniklas 	  break;
17642159047fSniklas 
17652159047fSniklas 	case MIND:
17662159047fSniklas 	  /* Multiple indirect symbols.  This is OK if they both point
17672159047fSniklas 	     to the same symbol.  */
17682159047fSniklas 	  if (strcmp (h->u.i.link->root.string, string) == 0)
17692159047fSniklas 	    break;
17702159047fSniklas 	  /* Fall through.  */
17712159047fSniklas 	case MDEF:
17722159047fSniklas 	  /* Handle a multiple definition.  */
1773c074d1c9Sdrahn 	  if (!info->allow_multiple_definition)
17742159047fSniklas 	    {
1775b305b0f1Sespie 	      asection *msec = NULL;
1776b305b0f1Sespie 	      bfd_vma mval = 0;
17772159047fSniklas 
17782159047fSniklas 	      switch (h->type)
17792159047fSniklas 		{
17802159047fSniklas 		case bfd_link_hash_defined:
17812159047fSniklas 		  msec = h->u.def.section;
17822159047fSniklas 		  mval = h->u.def.value;
17832159047fSniklas 		  break;
17842159047fSniklas 	        case bfd_link_hash_indirect:
17852159047fSniklas 		  msec = bfd_ind_section_ptr;
17862159047fSniklas 		  mval = 0;
17872159047fSniklas 		  break;
17882159047fSniklas 		default:
17892159047fSniklas 		  abort ();
17902159047fSniklas 		}
17912159047fSniklas 
1792c074d1c9Sdrahn 	      /* Ignore a redefinition of an absolute symbol to the
1793c074d1c9Sdrahn 		 same value; it's harmless.  */
17942159047fSniklas 	      if (h->type == bfd_link_hash_defined
17952159047fSniklas 		  && bfd_is_abs_section (msec)
17962159047fSniklas 		  && bfd_is_abs_section (section)
17972159047fSniklas 		  && value == mval)
17982159047fSniklas 		break;
17992159047fSniklas 
18002159047fSniklas 	      if (! ((*info->callbacks->multiple_definition)
1801c074d1c9Sdrahn 		     (info, h->root.string, msec->owner, msec, mval,
1802c074d1c9Sdrahn 		      abfd, section, value)))
1803c074d1c9Sdrahn 		return FALSE;
18042159047fSniklas 	    }
18052159047fSniklas 	  break;
18062159047fSniklas 
18072159047fSniklas 	case CIND:
18082159047fSniklas 	  /* Create an indirect symbol from an existing common symbol.  */
18092159047fSniklas 	  BFD_ASSERT (h->type == bfd_link_hash_common);
18102159047fSniklas 	  if (! ((*info->callbacks->multiple_common)
1811c88b1d6cSniklas 		 (info, h->root.string,
18122159047fSniklas 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1813*007c2a45Smiod 		  abfd, bfd_link_hash_indirect, 0)))
1814c074d1c9Sdrahn 	    return FALSE;
18152159047fSniklas 	  /* Fall through.  */
18162159047fSniklas 	case IND:
18172159047fSniklas 	  /* Create an indirect symbol.  */
18182159047fSniklas 	  {
18192159047fSniklas 	    struct bfd_link_hash_entry *inh;
18202159047fSniklas 
18212159047fSniklas 	    /* STRING is the name of the symbol we want to indirect
18222159047fSniklas 	       to.  */
1823c074d1c9Sdrahn 	    inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1824c074d1c9Sdrahn 						copy, FALSE);
1825*007c2a45Smiod 	    if (inh == NULL)
1826c074d1c9Sdrahn 	      return FALSE;
1827b55d4692Sfgsch 	    if (inh->type == bfd_link_hash_indirect
1828b55d4692Sfgsch 		&& inh->u.i.link == h)
1829b55d4692Sfgsch 	      {
1830b55d4692Sfgsch 		(*_bfd_error_handler)
1831b55d4692Sfgsch 		  (_("%s: indirect symbol `%s' to `%s' is a loop"),
1832c074d1c9Sdrahn 		   bfd_archive_filename (abfd), name, string);
1833b55d4692Sfgsch 		bfd_set_error (bfd_error_invalid_operation);
1834c074d1c9Sdrahn 		return FALSE;
1835b55d4692Sfgsch 	      }
18362159047fSniklas 	    if (inh->type == bfd_link_hash_new)
18372159047fSniklas 	      {
18382159047fSniklas 		inh->type = bfd_link_hash_undefined;
18392159047fSniklas 		inh->u.undef.abfd = abfd;
18402159047fSniklas 		bfd_link_add_undef (info->hash, inh);
18412159047fSniklas 	      }
18422159047fSniklas 
18432159047fSniklas 	    /* If the indirect symbol has been referenced, we need to
18442159047fSniklas 	       push the reference down to the symbol we are
18452159047fSniklas 	       referencing.  */
18462159047fSniklas 	    if (h->type != bfd_link_hash_new)
18472159047fSniklas 	      {
18482159047fSniklas 		row = UNDEF_ROW;
1849c074d1c9Sdrahn 		cycle = TRUE;
18502159047fSniklas 	      }
18512159047fSniklas 
18522159047fSniklas 	    h->type = bfd_link_hash_indirect;
18532159047fSniklas 	    h->u.i.link = inh;
18542159047fSniklas 	  }
18552159047fSniklas 	  break;
18562159047fSniklas 
18572159047fSniklas 	case SET:
18582159047fSniklas 	  /* Add an entry to a set.  */
18592159047fSniklas 	  if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
18602159047fSniklas 						abfd, section, value))
1861c074d1c9Sdrahn 	    return FALSE;
18622159047fSniklas 	  break;
18632159047fSniklas 
18642159047fSniklas 	case WARNC:
18652159047fSniklas 	  /* Issue a warning and cycle.  */
18662159047fSniklas 	  if (h->u.i.warning != NULL)
18672159047fSniklas 	    {
1868c88b1d6cSniklas 	      if (! (*info->callbacks->warning) (info, h->u.i.warning,
1869c88b1d6cSniklas 						 h->root.string, abfd,
1870*007c2a45Smiod 						 NULL, 0))
1871c074d1c9Sdrahn 		return FALSE;
18722159047fSniklas 	      /* Only issue a warning once.  */
18732159047fSniklas 	      h->u.i.warning = NULL;
18742159047fSniklas 	    }
18752159047fSniklas 	  /* Fall through.  */
18762159047fSniklas 	case CYCLE:
18772159047fSniklas 	  /* Try again with the referenced symbol.  */
18782159047fSniklas 	  h = h->u.i.link;
1879c074d1c9Sdrahn 	  cycle = TRUE;
18802159047fSniklas 	  break;
18812159047fSniklas 
18822159047fSniklas 	case REFC:
18832159047fSniklas 	  /* A reference to an indirect symbol.  */
1884*007c2a45Smiod 	  if (h->und_next == NULL && info->hash->undefs_tail != h)
1885*007c2a45Smiod 	    h->und_next = h;
18862159047fSniklas 	  h = h->u.i.link;
1887c074d1c9Sdrahn 	  cycle = TRUE;
18882159047fSniklas 	  break;
18892159047fSniklas 
18902159047fSniklas 	case WARN:
18912159047fSniklas 	  /* Issue a warning.  */
1892c88b1d6cSniklas 	  if (! (*info->callbacks->warning) (info, string, h->root.string,
1893*007c2a45Smiod 					     hash_entry_bfd (h), NULL, 0))
1894c074d1c9Sdrahn 	    return FALSE;
18952159047fSniklas 	  break;
18962159047fSniklas 
18972159047fSniklas 	case CWARN:
18982159047fSniklas 	  /* Warn if this symbol has been referenced already,
18992159047fSniklas 	     otherwise add a warning.  A symbol has been referenced if
1900*007c2a45Smiod 	     the und_next field is not NULL, or it is the tail of the
19012159047fSniklas 	     undefined symbol list.  The REF case above helps to
19022159047fSniklas 	     ensure this.  */
1903*007c2a45Smiod 	  if (h->und_next != NULL || info->hash->undefs_tail == h)
19042159047fSniklas 	    {
1905c88b1d6cSniklas 	      if (! (*info->callbacks->warning) (info, string, h->root.string,
1906*007c2a45Smiod 						 hash_entry_bfd (h), NULL, 0))
1907c074d1c9Sdrahn 		return FALSE;
19082159047fSniklas 	      break;
19092159047fSniklas 	    }
19102159047fSniklas 	  /* Fall through.  */
19112159047fSniklas 	case MWARN:
19122159047fSniklas 	  /* Make a warning symbol.  */
19132159047fSniklas 	  {
19142159047fSniklas 	    struct bfd_link_hash_entry *sub;
19152159047fSniklas 
19162159047fSniklas 	    /* STRING is the warning to give.  */
19172159047fSniklas 	    sub = ((struct bfd_link_hash_entry *)
19182159047fSniklas 		   ((*info->hash->table.newfunc)
1919*007c2a45Smiod 		    (NULL, &info->hash->table, h->root.string)));
19202159047fSniklas 	    if (sub == NULL)
1921c074d1c9Sdrahn 	      return FALSE;
19222159047fSniklas 	    *sub = *h;
19232159047fSniklas 	    sub->type = bfd_link_hash_warning;
19242159047fSniklas 	    sub->u.i.link = h;
19252159047fSniklas 	    if (! copy)
19262159047fSniklas 	      sub->u.i.warning = string;
19272159047fSniklas 	    else
19282159047fSniklas 	      {
19292159047fSniklas 		char *w;
1930c074d1c9Sdrahn 		size_t len = strlen (string) + 1;
19312159047fSniklas 
1932c074d1c9Sdrahn 		w = bfd_hash_allocate (&info->hash->table, len);
19332159047fSniklas 		if (w == NULL)
1934c074d1c9Sdrahn 		  return FALSE;
1935c074d1c9Sdrahn 		memcpy (w, string, len);
19362159047fSniklas 		sub->u.i.warning = w;
19372159047fSniklas 	      }
19382159047fSniklas 
19392159047fSniklas 	    bfd_hash_replace (&info->hash->table,
19402159047fSniklas 			      (struct bfd_hash_entry *) h,
19412159047fSniklas 			      (struct bfd_hash_entry *) sub);
19422159047fSniklas 	    if (hashp != NULL)
19432159047fSniklas 	      *hashp = sub;
19442159047fSniklas 	  }
19452159047fSniklas 	  break;
19462159047fSniklas 	}
19472159047fSniklas     }
19482159047fSniklas   while (cycle);
19492159047fSniklas 
1950c074d1c9Sdrahn   return TRUE;
19512159047fSniklas }
19522159047fSniklas 
19532159047fSniklas /* Generic final link routine.  */
19542159047fSniklas 
1955c074d1c9Sdrahn bfd_boolean
_bfd_generic_final_link(bfd * abfd,struct bfd_link_info * info)1956*007c2a45Smiod _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
19572159047fSniklas {
19582159047fSniklas   bfd *sub;
19592159047fSniklas   asection *o;
19602159047fSniklas   struct bfd_link_order *p;
19612159047fSniklas   size_t outsymalloc;
19622159047fSniklas   struct generic_write_global_symbol_info wginfo;
19632159047fSniklas 
1964*007c2a45Smiod   bfd_get_outsymbols (abfd) = NULL;
1965b305b0f1Sespie   bfd_get_symcount (abfd) = 0;
19662159047fSniklas   outsymalloc = 0;
19672159047fSniklas 
1968e93f7393Sniklas   /* Mark all sections which will be included in the output file.  */
1969e93f7393Sniklas   for (o = abfd->sections; o != NULL; o = o->next)
1970e93f7393Sniklas     for (p = o->link_order_head; p != NULL; p = p->next)
1971e93f7393Sniklas       if (p->type == bfd_indirect_link_order)
1972c074d1c9Sdrahn 	p->u.indirect.section->linker_mark = TRUE;
1973e93f7393Sniklas 
19742159047fSniklas   /* Build the output symbol table.  */
1975*007c2a45Smiod   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
19762159047fSniklas     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1977c074d1c9Sdrahn       return FALSE;
19782159047fSniklas 
19792159047fSniklas   /* Accumulate the global symbols.  */
19802159047fSniklas   wginfo.info = info;
19812159047fSniklas   wginfo.output_bfd = abfd;
19822159047fSniklas   wginfo.psymalloc = &outsymalloc;
19832159047fSniklas   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
19842159047fSniklas 				   _bfd_generic_link_write_global_symbol,
1985*007c2a45Smiod 				   &wginfo);
19862159047fSniklas 
1987b305b0f1Sespie   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
1988b305b0f1Sespie      shouldn't really need one, since we have SYMCOUNT, but some old
1989b305b0f1Sespie      code still expects one.  */
1990b305b0f1Sespie   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
1991c074d1c9Sdrahn     return FALSE;
1992b305b0f1Sespie 
1993*007c2a45Smiod   if (info->relocatable)
19942159047fSniklas     {
19952159047fSniklas       /* Allocate space for the output relocs for each section.  */
1996*007c2a45Smiod       for (o = abfd->sections; o != NULL; o = o->next)
19972159047fSniklas 	{
19982159047fSniklas 	  o->reloc_count = 0;
1999*007c2a45Smiod 	  for (p = o->link_order_head; p != NULL; p = p->next)
20002159047fSniklas 	    {
20012159047fSniklas 	      if (p->type == bfd_section_reloc_link_order
20022159047fSniklas 		  || p->type == bfd_symbol_reloc_link_order)
20032159047fSniklas 		++o->reloc_count;
20042159047fSniklas 	      else if (p->type == bfd_indirect_link_order)
20052159047fSniklas 		{
20062159047fSniklas 		  asection *input_section;
20072159047fSniklas 		  bfd *input_bfd;
20082159047fSniklas 		  long relsize;
20092159047fSniklas 		  arelent **relocs;
20102159047fSniklas 		  asymbol **symbols;
20112159047fSniklas 		  long reloc_count;
20122159047fSniklas 
20132159047fSniklas 		  input_section = p->u.indirect.section;
20142159047fSniklas 		  input_bfd = input_section->owner;
20152159047fSniklas 		  relsize = bfd_get_reloc_upper_bound (input_bfd,
20162159047fSniklas 						       input_section);
20172159047fSniklas 		  if (relsize < 0)
2018c074d1c9Sdrahn 		    return FALSE;
2019*007c2a45Smiod 		  relocs = bfd_malloc (relsize);
20202159047fSniklas 		  if (!relocs && relsize != 0)
2021c074d1c9Sdrahn 		    return FALSE;
20222159047fSniklas 		  symbols = _bfd_generic_link_get_symbols (input_bfd);
20232159047fSniklas 		  reloc_count = bfd_canonicalize_reloc (input_bfd,
20242159047fSniklas 							input_section,
20252159047fSniklas 							relocs,
20262159047fSniklas 							symbols);
2027c074d1c9Sdrahn 		  free (relocs);
20282159047fSniklas 		  if (reloc_count < 0)
2029c074d1c9Sdrahn 		    return FALSE;
20302159047fSniklas 		  BFD_ASSERT ((unsigned long) reloc_count
20312159047fSniklas 			      == input_section->reloc_count);
20322159047fSniklas 		  o->reloc_count += reloc_count;
20332159047fSniklas 		}
20342159047fSniklas 	    }
20352159047fSniklas 	  if (o->reloc_count > 0)
20362159047fSniklas 	    {
2037c074d1c9Sdrahn 	      bfd_size_type amt;
2038c074d1c9Sdrahn 
2039c074d1c9Sdrahn 	      amt = o->reloc_count;
2040c074d1c9Sdrahn 	      amt *= sizeof (arelent *);
2041*007c2a45Smiod 	      o->orelocation = bfd_alloc (abfd, amt);
20422159047fSniklas 	      if (!o->orelocation)
2043c074d1c9Sdrahn 		return FALSE;
20442159047fSniklas 	      o->flags |= SEC_RELOC;
20452159047fSniklas 	      /* Reset the count so that it can be used as an index
20462159047fSniklas 		 when putting in the output relocs.  */
20472159047fSniklas 	      o->reloc_count = 0;
20482159047fSniklas 	    }
20492159047fSniklas 	}
20502159047fSniklas     }
20512159047fSniklas 
20522159047fSniklas   /* Handle all the link order information for the sections.  */
2053*007c2a45Smiod   for (o = abfd->sections; o != NULL; o = o->next)
20542159047fSniklas     {
2055*007c2a45Smiod       for (p = o->link_order_head; p != NULL; p = p->next)
20562159047fSniklas 	{
20572159047fSniklas 	  switch (p->type)
20582159047fSniklas 	    {
20592159047fSniklas 	    case bfd_section_reloc_link_order:
20602159047fSniklas 	    case bfd_symbol_reloc_link_order:
20612159047fSniklas 	      if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2062c074d1c9Sdrahn 		return FALSE;
20632159047fSniklas 	      break;
20642159047fSniklas 	    case bfd_indirect_link_order:
2065c074d1c9Sdrahn 	      if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2066c074d1c9Sdrahn 		return FALSE;
20672159047fSniklas 	      break;
20682159047fSniklas 	    default:
20692159047fSniklas 	      if (! _bfd_default_link_order (abfd, info, o, p))
2070c074d1c9Sdrahn 		return FALSE;
20712159047fSniklas 	      break;
20722159047fSniklas 	    }
20732159047fSniklas 	}
20742159047fSniklas     }
20752159047fSniklas 
2076c074d1c9Sdrahn   return TRUE;
20772159047fSniklas }
20782159047fSniklas 
20792159047fSniklas /* Add an output symbol to the output BFD.  */
20802159047fSniklas 
2081c074d1c9Sdrahn static bfd_boolean
generic_add_output_symbol(bfd * output_bfd,size_t * psymalloc,asymbol * sym)2082*007c2a45Smiod generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
20832159047fSniklas {
2084b305b0f1Sespie   if (bfd_get_symcount (output_bfd) >= *psymalloc)
20852159047fSniklas     {
20862159047fSniklas       asymbol **newsyms;
2087c074d1c9Sdrahn       bfd_size_type amt;
20882159047fSniklas 
20892159047fSniklas       if (*psymalloc == 0)
20902159047fSniklas 	*psymalloc = 124;
20912159047fSniklas       else
20922159047fSniklas 	*psymalloc *= 2;
2093c074d1c9Sdrahn       amt = *psymalloc;
2094c074d1c9Sdrahn       amt *= sizeof (asymbol *);
2095*007c2a45Smiod       newsyms = bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2096*007c2a45Smiod       if (newsyms == NULL)
2097c074d1c9Sdrahn 	return FALSE;
2098b305b0f1Sespie       bfd_get_outsymbols (output_bfd) = newsyms;
20992159047fSniklas     }
21002159047fSniklas 
2101b305b0f1Sespie   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2102b305b0f1Sespie   if (sym != NULL)
2103b305b0f1Sespie     ++ bfd_get_symcount (output_bfd);
21042159047fSniklas 
2105c074d1c9Sdrahn   return TRUE;
21062159047fSniklas }
21072159047fSniklas 
21082159047fSniklas /* Handle the symbols for an input BFD.  */
21092159047fSniklas 
2110c074d1c9Sdrahn bfd_boolean
_bfd_generic_link_output_symbols(bfd * output_bfd,bfd * input_bfd,struct bfd_link_info * info,size_t * psymalloc)2111*007c2a45Smiod _bfd_generic_link_output_symbols (bfd *output_bfd,
2112*007c2a45Smiod 				  bfd *input_bfd,
2113*007c2a45Smiod 				  struct bfd_link_info *info,
2114*007c2a45Smiod 				  size_t *psymalloc)
21152159047fSniklas {
21162159047fSniklas   asymbol **sym_ptr;
21172159047fSniklas   asymbol **sym_end;
21182159047fSniklas 
21192159047fSniklas   if (! generic_link_read_symbols (input_bfd))
2120c074d1c9Sdrahn     return FALSE;
21212159047fSniklas 
21222159047fSniklas   /* Create a filename symbol if we are supposed to.  */
2123*007c2a45Smiod   if (info->create_object_symbols_section != NULL)
21242159047fSniklas     {
21252159047fSniklas       asection *sec;
21262159047fSniklas 
2127*007c2a45Smiod       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
21282159047fSniklas 	{
21292159047fSniklas 	  if (sec->output_section == info->create_object_symbols_section)
21302159047fSniklas 	    {
21312159047fSniklas 	      asymbol *newsym;
21322159047fSniklas 
21332159047fSniklas 	      newsym = bfd_make_empty_symbol (input_bfd);
21342159047fSniklas 	      if (!newsym)
2135c074d1c9Sdrahn 		return FALSE;
21362159047fSniklas 	      newsym->name = input_bfd->filename;
21372159047fSniklas 	      newsym->value = 0;
21382159047fSniklas 	      newsym->flags = BSF_LOCAL | BSF_FILE;
21392159047fSniklas 	      newsym->section = sec;
21402159047fSniklas 
21412159047fSniklas 	      if (! generic_add_output_symbol (output_bfd, psymalloc,
21422159047fSniklas 					       newsym))
2143c074d1c9Sdrahn 		return FALSE;
21442159047fSniklas 
21452159047fSniklas 	      break;
21462159047fSniklas 	    }
21472159047fSniklas 	}
21482159047fSniklas     }
21492159047fSniklas 
21502159047fSniklas   /* Adjust the values of the globally visible symbols, and write out
21512159047fSniklas      local symbols.  */
21522159047fSniklas   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
21532159047fSniklas   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
21542159047fSniklas   for (; sym_ptr < sym_end; sym_ptr++)
21552159047fSniklas     {
21562159047fSniklas       asymbol *sym;
21572159047fSniklas       struct generic_link_hash_entry *h;
2158c074d1c9Sdrahn       bfd_boolean output;
21592159047fSniklas 
2160*007c2a45Smiod       h = NULL;
21612159047fSniklas       sym = *sym_ptr;
21622159047fSniklas       if ((sym->flags & (BSF_INDIRECT
21632159047fSniklas 			 | BSF_WARNING
21642159047fSniklas 			 | BSF_GLOBAL
21652159047fSniklas 			 | BSF_CONSTRUCTOR
21662159047fSniklas 			 | BSF_WEAK)) != 0
21672159047fSniklas 	  || bfd_is_und_section (bfd_get_section (sym))
21682159047fSniklas 	  || bfd_is_com_section (bfd_get_section (sym))
21692159047fSniklas 	  || bfd_is_ind_section (bfd_get_section (sym)))
21702159047fSniklas 	{
21712159047fSniklas 	  if (sym->udata.p != NULL)
2172*007c2a45Smiod 	    h = sym->udata.p;
21732159047fSniklas 	  else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
21742159047fSniklas 	    {
21752159047fSniklas 	      /* This case normally means that the main linker code
21762159047fSniklas                  deliberately ignored this constructor symbol.  We
21772159047fSniklas                  should just pass it through.  This will screw up if
21782159047fSniklas                  the constructor symbol is from a different,
21792159047fSniklas                  non-generic, object file format, but the case will
21802159047fSniklas                  only arise when linking with -r, which will probably
21812159047fSniklas                  fail anyhow, since there will be no way to represent
21822159047fSniklas                  the relocs in the output format being used.  */
21832159047fSniklas 	      h = NULL;
21842159047fSniklas 	    }
2185c88b1d6cSniklas 	  else if (bfd_is_und_section (bfd_get_section (sym)))
2186c88b1d6cSniklas 	    h = ((struct generic_link_hash_entry *)
2187c88b1d6cSniklas 		 bfd_wrapped_link_hash_lookup (output_bfd, info,
2188c88b1d6cSniklas 					       bfd_asymbol_name (sym),
2189c074d1c9Sdrahn 					       FALSE, FALSE, TRUE));
21902159047fSniklas 	  else
21912159047fSniklas 	    h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
21922159047fSniklas 					       bfd_asymbol_name (sym),
2193c074d1c9Sdrahn 					       FALSE, FALSE, TRUE);
21942159047fSniklas 
2195*007c2a45Smiod 	  if (h != NULL)
21962159047fSniklas 	    {
21972159047fSniklas 	      /* Force all references to this symbol to point to
21982159047fSniklas 		 the same area in memory.  It is possible that
21992159047fSniklas 		 this routine will be called with a hash table
22002159047fSniklas 		 other than a generic hash table, so we double
22012159047fSniklas 		 check that.  */
22022159047fSniklas 	      if (info->hash->creator == input_bfd->xvec)
22032159047fSniklas 		{
2204*007c2a45Smiod 		  if (h->sym != NULL)
22052159047fSniklas 		    *sym_ptr = sym = h->sym;
22062159047fSniklas 		}
22072159047fSniklas 
22082159047fSniklas 	      switch (h->root.type)
22092159047fSniklas 		{
22102159047fSniklas 		default:
22112159047fSniklas 		case bfd_link_hash_new:
22122159047fSniklas 		  abort ();
22132159047fSniklas 		case bfd_link_hash_undefined:
22142159047fSniklas 		  break;
22152159047fSniklas 		case bfd_link_hash_undefweak:
22162159047fSniklas 		  sym->flags |= BSF_WEAK;
22172159047fSniklas 		  break;
22182159047fSniklas 		case bfd_link_hash_indirect:
22192159047fSniklas 		  h = (struct generic_link_hash_entry *) h->root.u.i.link;
22202159047fSniklas 		  /* fall through */
22212159047fSniklas 		case bfd_link_hash_defined:
22222159047fSniklas 		  sym->flags |= BSF_GLOBAL;
22232159047fSniklas 		  sym->flags &=~ BSF_CONSTRUCTOR;
22242159047fSniklas 		  sym->value = h->root.u.def.value;
22252159047fSniklas 		  sym->section = h->root.u.def.section;
22262159047fSniklas 		  break;
22272159047fSniklas 		case bfd_link_hash_defweak:
22282159047fSniklas 		  sym->flags |= BSF_WEAK;
22292159047fSniklas 		  sym->flags &=~ BSF_CONSTRUCTOR;
22302159047fSniklas 		  sym->value = h->root.u.def.value;
22312159047fSniklas 		  sym->section = h->root.u.def.section;
22322159047fSniklas 		  break;
22332159047fSniklas 		case bfd_link_hash_common:
22342159047fSniklas 		  sym->value = h->root.u.c.size;
22352159047fSniklas 		  sym->flags |= BSF_GLOBAL;
22362159047fSniklas 		  if (! bfd_is_com_section (sym->section))
22372159047fSniklas 		    {
22382159047fSniklas 		      BFD_ASSERT (bfd_is_und_section (sym->section));
22392159047fSniklas 		      sym->section = bfd_com_section_ptr;
22402159047fSniklas 		    }
22412159047fSniklas 		  /* We do not set the section of the symbol to
22422159047fSniklas 		     h->root.u.c.p->section.  That value was saved so
22432159047fSniklas 		     that we would know where to allocate the symbol
22442159047fSniklas 		     if it was defined.  In this case the type is
22452159047fSniklas 		     still bfd_link_hash_common, so we did not define
22462159047fSniklas 		     it, so we do not want to use that section.  */
22472159047fSniklas 		  break;
22482159047fSniklas 		}
22492159047fSniklas 	    }
22502159047fSniklas 	}
22512159047fSniklas 
22522159047fSniklas       /* This switch is straight from the old code in
22532159047fSniklas 	 write_file_locals in ldsym.c.  */
22540c6d0228Sniklas       if (info->strip == strip_all
22550c6d0228Sniklas 	  || (info->strip == strip_some
2256*007c2a45Smiod 	      && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2257*007c2a45Smiod 				  FALSE, FALSE) == NULL))
2258c074d1c9Sdrahn 	output = FALSE;
22592159047fSniklas       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
22602159047fSniklas 	{
22612159047fSniklas 	  /* If this symbol is marked as occurring now, rather
22622159047fSniklas 	     than at the end, output it now.  This is used for
22632159047fSniklas 	     COFF C_EXT FCN symbols.  FIXME: There must be a
22642159047fSniklas 	     better way.  */
22652159047fSniklas 	  if (bfd_asymbol_bfd (sym) == input_bfd
22662159047fSniklas 	      && (sym->flags & BSF_NOT_AT_END) != 0)
2267c074d1c9Sdrahn 	    output = TRUE;
22682159047fSniklas 	  else
2269c074d1c9Sdrahn 	    output = FALSE;
22702159047fSniklas 	}
22712159047fSniklas       else if (bfd_is_ind_section (sym->section))
2272c074d1c9Sdrahn 	output = FALSE;
22732159047fSniklas       else if ((sym->flags & BSF_DEBUGGING) != 0)
22742159047fSniklas 	{
22752159047fSniklas 	  if (info->strip == strip_none)
2276c074d1c9Sdrahn 	    output = TRUE;
22772159047fSniklas 	  else
2278c074d1c9Sdrahn 	    output = FALSE;
22792159047fSniklas 	}
22802159047fSniklas       else if (bfd_is_und_section (sym->section)
22812159047fSniklas 	       || bfd_is_com_section (sym->section))
2282c074d1c9Sdrahn 	output = FALSE;
22832159047fSniklas       else if ((sym->flags & BSF_LOCAL) != 0)
22842159047fSniklas 	{
22852159047fSniklas 	  if ((sym->flags & BSF_WARNING) != 0)
2286c074d1c9Sdrahn 	    output = FALSE;
22872159047fSniklas 	  else
22882159047fSniklas 	    {
22892159047fSniklas 	      switch (info->discard)
22902159047fSniklas 		{
22912159047fSniklas 		default:
22922159047fSniklas 		case discard_all:
2293c074d1c9Sdrahn 		  output = FALSE;
22942159047fSniklas 		  break;
2295c074d1c9Sdrahn 		case discard_sec_merge:
2296c074d1c9Sdrahn 		  output = TRUE;
2297*007c2a45Smiod 		  if (info->relocatable
2298c074d1c9Sdrahn 		      || ! (sym->section->flags & SEC_MERGE))
2299c074d1c9Sdrahn 		    break;
2300c074d1c9Sdrahn 		  /* FALLTHROUGH */
23012159047fSniklas 		case discard_l:
2302b305b0f1Sespie 		  if (bfd_is_local_label (input_bfd, sym))
2303c074d1c9Sdrahn 		    output = FALSE;
23042159047fSniklas 		  else
2305c074d1c9Sdrahn 		    output = TRUE;
23062159047fSniklas 		  break;
23072159047fSniklas 		case discard_none:
2308c074d1c9Sdrahn 		  output = TRUE;
23092159047fSniklas 		  break;
23102159047fSniklas 		}
23112159047fSniklas 	    }
23122159047fSniklas 	}
23132159047fSniklas       else if ((sym->flags & BSF_CONSTRUCTOR))
23142159047fSniklas 	{
23152159047fSniklas 	  if (info->strip != strip_all)
2316c074d1c9Sdrahn 	    output = TRUE;
23172159047fSniklas 	  else
2318c074d1c9Sdrahn 	    output = FALSE;
23192159047fSniklas 	}
23202159047fSniklas       else
23212159047fSniklas 	abort ();
23222159047fSniklas 
2323e93f7393Sniklas       /* If this symbol is in a section which is not being included
2324e93f7393Sniklas 	 in the output file, then we don't want to output the symbol.
2325e93f7393Sniklas 
2326e93f7393Sniklas 	 Gross.  .bss and similar sections won't have the linker_mark
2327e93f7393Sniklas 	 field set.  */
2328e93f7393Sniklas       if ((sym->section->flags & SEC_HAS_CONTENTS) != 0
2329c074d1c9Sdrahn 	  && ! sym->section->linker_mark)
2330c074d1c9Sdrahn 	output = FALSE;
2331e93f7393Sniklas 
23322159047fSniklas       if (output)
23332159047fSniklas 	{
23342159047fSniklas 	  if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2335c074d1c9Sdrahn 	    return FALSE;
2336*007c2a45Smiod 	  if (h != NULL)
2337c074d1c9Sdrahn 	    h->written = TRUE;
23382159047fSniklas 	}
23392159047fSniklas     }
23402159047fSniklas 
2341c074d1c9Sdrahn   return TRUE;
23422159047fSniklas }
23432159047fSniklas 
23442159047fSniklas /* Set the section and value of a generic BFD symbol based on a linker
23452159047fSniklas    hash table entry.  */
23462159047fSniklas 
23472159047fSniklas static void
set_symbol_from_hash(asymbol * sym,struct bfd_link_hash_entry * h)2348*007c2a45Smiod set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
23492159047fSniklas {
23502159047fSniklas   switch (h->type)
23512159047fSniklas     {
23522159047fSniklas     default:
23532159047fSniklas       abort ();
23542159047fSniklas       break;
23552159047fSniklas     case bfd_link_hash_new:
23562159047fSniklas       /* This can happen when a constructor symbol is seen but we are
23572159047fSniklas          not building constructors.  */
23582159047fSniklas       if (sym->section != NULL)
23592159047fSniklas 	{
23602159047fSniklas 	  BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
23612159047fSniklas 	}
23622159047fSniklas       else
23632159047fSniklas 	{
23642159047fSniklas 	  sym->flags |= BSF_CONSTRUCTOR;
23652159047fSniklas 	  sym->section = bfd_abs_section_ptr;
23662159047fSniklas 	  sym->value = 0;
23672159047fSniklas 	}
23682159047fSniklas       break;
23692159047fSniklas     case bfd_link_hash_undefined:
23702159047fSniklas       sym->section = bfd_und_section_ptr;
23712159047fSniklas       sym->value = 0;
23722159047fSniklas       break;
23732159047fSniklas     case bfd_link_hash_undefweak:
23742159047fSniklas       sym->section = bfd_und_section_ptr;
23752159047fSniklas       sym->value = 0;
23762159047fSniklas       sym->flags |= BSF_WEAK;
23772159047fSniklas       break;
23782159047fSniklas     case bfd_link_hash_defined:
23792159047fSniklas       sym->section = h->u.def.section;
23802159047fSniklas       sym->value = h->u.def.value;
23812159047fSniklas       break;
23822159047fSniklas     case bfd_link_hash_defweak:
23832159047fSniklas       sym->flags |= BSF_WEAK;
23842159047fSniklas       sym->section = h->u.def.section;
23852159047fSniklas       sym->value = h->u.def.value;
23862159047fSniklas       break;
23872159047fSniklas     case bfd_link_hash_common:
23882159047fSniklas       sym->value = h->u.c.size;
23892159047fSniklas       if (sym->section == NULL)
23902159047fSniklas 	sym->section = bfd_com_section_ptr;
23912159047fSniklas       else if (! bfd_is_com_section (sym->section))
23922159047fSniklas 	{
23932159047fSniklas 	  BFD_ASSERT (bfd_is_und_section (sym->section));
23942159047fSniklas 	  sym->section = bfd_com_section_ptr;
23952159047fSniklas 	}
23962159047fSniklas       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
23972159047fSniklas       break;
23982159047fSniklas     case bfd_link_hash_indirect:
23992159047fSniklas     case bfd_link_hash_warning:
24002159047fSniklas       /* FIXME: What should we do here?  */
24012159047fSniklas       break;
24022159047fSniklas     }
24032159047fSniklas }
24042159047fSniklas 
24052159047fSniklas /* Write out a global symbol, if it hasn't already been written out.
24062159047fSniklas    This is called for each symbol in the hash table.  */
24072159047fSniklas 
2408c074d1c9Sdrahn bfd_boolean
_bfd_generic_link_write_global_symbol(struct generic_link_hash_entry * h,void * data)2409*007c2a45Smiod _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2410*007c2a45Smiod 				       void *data)
24112159047fSniklas {
2412*007c2a45Smiod   struct generic_write_global_symbol_info *wginfo = data;
24132159047fSniklas   asymbol *sym;
24142159047fSniklas 
2415c074d1c9Sdrahn   if (h->root.type == bfd_link_hash_warning)
2416c074d1c9Sdrahn     h = (struct generic_link_hash_entry *) h->root.u.i.link;
24172159047fSniklas 
2418c074d1c9Sdrahn   if (h->written)
2419c074d1c9Sdrahn     return TRUE;
2420c074d1c9Sdrahn 
2421c074d1c9Sdrahn   h->written = TRUE;
24222159047fSniklas 
24232159047fSniklas   if (wginfo->info->strip == strip_all
24242159047fSniklas       || (wginfo->info->strip == strip_some
24252159047fSniklas 	  && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2426c074d1c9Sdrahn 			      FALSE, FALSE) == NULL))
2427c074d1c9Sdrahn     return TRUE;
24282159047fSniklas 
2429*007c2a45Smiod   if (h->sym != NULL)
24302159047fSniklas     sym = h->sym;
24312159047fSniklas   else
24322159047fSniklas     {
24332159047fSniklas       sym = bfd_make_empty_symbol (wginfo->output_bfd);
24342159047fSniklas       if (!sym)
2435c074d1c9Sdrahn 	return FALSE;
24362159047fSniklas       sym->name = h->root.root.string;
24372159047fSniklas       sym->flags = 0;
24382159047fSniklas     }
24392159047fSniklas 
24402159047fSniklas   set_symbol_from_hash (sym, &h->root);
24412159047fSniklas 
24422159047fSniklas   sym->flags |= BSF_GLOBAL;
24432159047fSniklas 
24442159047fSniklas   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
24452159047fSniklas 				   sym))
24462159047fSniklas     {
24472159047fSniklas       /* FIXME: No way to return failure.  */
24482159047fSniklas       abort ();
24492159047fSniklas     }
24502159047fSniklas 
2451c074d1c9Sdrahn   return TRUE;
24522159047fSniklas }
24532159047fSniklas 
24542159047fSniklas /* Create a relocation.  */
24552159047fSniklas 
2456c074d1c9Sdrahn bfd_boolean
_bfd_generic_reloc_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)2457*007c2a45Smiod _bfd_generic_reloc_link_order (bfd *abfd,
2458*007c2a45Smiod 			       struct bfd_link_info *info,
2459*007c2a45Smiod 			       asection *sec,
2460*007c2a45Smiod 			       struct bfd_link_order *link_order)
24612159047fSniklas {
24622159047fSniklas   arelent *r;
24632159047fSniklas 
2464*007c2a45Smiod   if (! info->relocatable)
24652159047fSniklas     abort ();
2466*007c2a45Smiod   if (sec->orelocation == NULL)
24672159047fSniklas     abort ();
24682159047fSniklas 
2469*007c2a45Smiod   r = bfd_alloc (abfd, sizeof (arelent));
2470*007c2a45Smiod   if (r == NULL)
2471c074d1c9Sdrahn     return FALSE;
24722159047fSniklas 
24732159047fSniklas   r->address = link_order->offset;
24742159047fSniklas   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
24752159047fSniklas   if (r->howto == 0)
24762159047fSniklas     {
24772159047fSniklas       bfd_set_error (bfd_error_bad_value);
2478c074d1c9Sdrahn       return FALSE;
24792159047fSniklas     }
24802159047fSniklas 
24812159047fSniklas   /* Get the symbol to use for the relocation.  */
24822159047fSniklas   if (link_order->type == bfd_section_reloc_link_order)
24832159047fSniklas     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
24842159047fSniklas   else
24852159047fSniklas     {
24862159047fSniklas       struct generic_link_hash_entry *h;
24872159047fSniklas 
2488c88b1d6cSniklas       h = ((struct generic_link_hash_entry *)
2489c88b1d6cSniklas 	   bfd_wrapped_link_hash_lookup (abfd, info,
24902159047fSniklas 					 link_order->u.reloc.p->u.name,
2491c074d1c9Sdrahn 					 FALSE, FALSE, TRUE));
2492*007c2a45Smiod       if (h == NULL
24932159047fSniklas 	  || ! h->written)
24942159047fSniklas 	{
24952159047fSniklas 	  if (! ((*info->callbacks->unattached_reloc)
2496*007c2a45Smiod 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2497c074d1c9Sdrahn 	    return FALSE;
24982159047fSniklas 	  bfd_set_error (bfd_error_bad_value);
2499c074d1c9Sdrahn 	  return FALSE;
25002159047fSniklas 	}
25012159047fSniklas       r->sym_ptr_ptr = &h->sym;
25022159047fSniklas     }
25032159047fSniklas 
25042159047fSniklas   /* If this is an inplace reloc, write the addend to the object file.
25052159047fSniklas      Otherwise, store it in the reloc addend.  */
25062159047fSniklas   if (! r->howto->partial_inplace)
25072159047fSniklas     r->addend = link_order->u.reloc.p->addend;
25082159047fSniklas   else
25092159047fSniklas     {
25102159047fSniklas       bfd_size_type size;
25112159047fSniklas       bfd_reloc_status_type rstat;
25122159047fSniklas       bfd_byte *buf;
2513c074d1c9Sdrahn       bfd_boolean ok;
2514c074d1c9Sdrahn       file_ptr loc;
25152159047fSniklas 
25162159047fSniklas       size = bfd_get_reloc_size (r->howto);
2517*007c2a45Smiod       buf = bfd_zmalloc (size);
2518*007c2a45Smiod       if (buf == NULL)
2519c074d1c9Sdrahn 	return FALSE;
25202159047fSniklas       rstat = _bfd_relocate_contents (r->howto, abfd,
2521c074d1c9Sdrahn 				      (bfd_vma) link_order->u.reloc.p->addend,
2522c074d1c9Sdrahn 				      buf);
25232159047fSniklas       switch (rstat)
25242159047fSniklas 	{
25252159047fSniklas 	case bfd_reloc_ok:
25262159047fSniklas 	  break;
25272159047fSniklas 	default:
25282159047fSniklas 	case bfd_reloc_outofrange:
25292159047fSniklas 	  abort ();
25302159047fSniklas 	case bfd_reloc_overflow:
25312159047fSniklas 	  if (! ((*info->callbacks->reloc_overflow)
25322159047fSniklas 		 (info,
25332159047fSniklas 		  (link_order->type == bfd_section_reloc_link_order
25342159047fSniklas 		   ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
25352159047fSniklas 		   : link_order->u.reloc.p->u.name),
25362159047fSniklas 		  r->howto->name, link_order->u.reloc.p->addend,
2537*007c2a45Smiod 		  NULL, NULL, 0)))
25382159047fSniklas 	    {
25392159047fSniklas 	      free (buf);
2540c074d1c9Sdrahn 	      return FALSE;
25412159047fSniklas 	    }
25422159047fSniklas 	  break;
25432159047fSniklas 	}
2544c074d1c9Sdrahn       loc = link_order->offset * bfd_octets_per_byte (abfd);
2545*007c2a45Smiod       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
25462159047fSniklas       free (buf);
25472159047fSniklas       if (! ok)
2548c074d1c9Sdrahn 	return FALSE;
25492159047fSniklas 
25502159047fSniklas       r->addend = 0;
25512159047fSniklas     }
25522159047fSniklas 
25532159047fSniklas   sec->orelocation[sec->reloc_count] = r;
25542159047fSniklas   ++sec->reloc_count;
25552159047fSniklas 
2556c074d1c9Sdrahn   return TRUE;
25572159047fSniklas }
25582159047fSniklas 
25592159047fSniklas /* Allocate a new link_order for a section.  */
25602159047fSniklas 
25612159047fSniklas struct bfd_link_order *
bfd_new_link_order(bfd * abfd,asection * section)2562*007c2a45Smiod bfd_new_link_order (bfd *abfd, asection *section)
25632159047fSniklas {
2564c074d1c9Sdrahn   bfd_size_type amt = sizeof (struct bfd_link_order);
25652159047fSniklas   struct bfd_link_order *new;
25662159047fSniklas 
2567*007c2a45Smiod   new = bfd_zalloc (abfd, amt);
25682159047fSniklas   if (!new)
25692159047fSniklas     return NULL;
25702159047fSniklas 
25712159047fSniklas   new->type = bfd_undefined_link_order;
25722159047fSniklas 
2573*007c2a45Smiod   if (section->link_order_tail != NULL)
25742159047fSniklas     section->link_order_tail->next = new;
25752159047fSniklas   else
25762159047fSniklas     section->link_order_head = new;
25772159047fSniklas   section->link_order_tail = new;
25782159047fSniklas 
25792159047fSniklas   return new;
25802159047fSniklas }
25812159047fSniklas 
25822159047fSniklas /* Default link order processing routine.  Note that we can not handle
25832159047fSniklas    the reloc_link_order types here, since they depend upon the details
25842159047fSniklas    of how the particular backends generates relocs.  */
25852159047fSniklas 
2586c074d1c9Sdrahn bfd_boolean
_bfd_default_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)2587*007c2a45Smiod _bfd_default_link_order (bfd *abfd,
2588*007c2a45Smiod 			 struct bfd_link_info *info,
2589*007c2a45Smiod 			 asection *sec,
2590*007c2a45Smiod 			 struct bfd_link_order *link_order)
25912159047fSniklas {
25922159047fSniklas   switch (link_order->type)
25932159047fSniklas     {
25942159047fSniklas     case bfd_undefined_link_order:
25952159047fSniklas     case bfd_section_reloc_link_order:
25962159047fSniklas     case bfd_symbol_reloc_link_order:
25972159047fSniklas     default:
25982159047fSniklas       abort ();
25992159047fSniklas     case bfd_indirect_link_order:
26002159047fSniklas       return default_indirect_link_order (abfd, info, sec, link_order,
2601c074d1c9Sdrahn 					  FALSE);
26022159047fSniklas     case bfd_data_link_order:
2603c074d1c9Sdrahn       return default_data_link_order (abfd, info, sec, link_order);
26042159047fSniklas     }
26052159047fSniklas }
26062159047fSniklas 
2607c074d1c9Sdrahn /* Default routine to handle a bfd_data_link_order.  */
26082159047fSniklas 
2609c074d1c9Sdrahn static bfd_boolean
default_data_link_order(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_order * link_order)2610*007c2a45Smiod default_data_link_order (bfd *abfd,
2611*007c2a45Smiod 			 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2612*007c2a45Smiod 			 asection *sec,
2613*007c2a45Smiod 			 struct bfd_link_order *link_order)
26142159047fSniklas {
2615c074d1c9Sdrahn   bfd_size_type size;
2616c074d1c9Sdrahn   size_t fill_size;
2617c074d1c9Sdrahn   bfd_byte *fill;
2618c074d1c9Sdrahn   file_ptr loc;
2619c074d1c9Sdrahn   bfd_boolean result;
26202159047fSniklas 
26212159047fSniklas   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
26222159047fSniklas 
2623c074d1c9Sdrahn   size = link_order->size;
2624c074d1c9Sdrahn   if (size == 0)
2625c074d1c9Sdrahn     return TRUE;
26262159047fSniklas 
2627c074d1c9Sdrahn   fill = link_order->u.data.contents;
2628c074d1c9Sdrahn   fill_size = link_order->u.data.size;
2629c074d1c9Sdrahn   if (fill_size != 0 && fill_size < size)
2630c074d1c9Sdrahn     {
2631c074d1c9Sdrahn       bfd_byte *p;
2632*007c2a45Smiod       fill = bfd_malloc (size);
2633c074d1c9Sdrahn       if (fill == NULL)
2634c074d1c9Sdrahn 	return FALSE;
2635c074d1c9Sdrahn       p = fill;
2636c074d1c9Sdrahn       if (fill_size == 1)
2637c074d1c9Sdrahn 	memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2638c074d1c9Sdrahn       else
2639c074d1c9Sdrahn 	{
2640c074d1c9Sdrahn 	  do
2641c074d1c9Sdrahn 	    {
2642c074d1c9Sdrahn 	      memcpy (p, link_order->u.data.contents, fill_size);
2643c074d1c9Sdrahn 	      p += fill_size;
2644c074d1c9Sdrahn 	      size -= fill_size;
2645c074d1c9Sdrahn 	    }
2646c074d1c9Sdrahn 	  while (size >= fill_size);
2647c074d1c9Sdrahn 	  if (size != 0)
2648c074d1c9Sdrahn 	    memcpy (p, link_order->u.data.contents, (size_t) size);
2649c074d1c9Sdrahn 	  size = link_order->size;
2650c074d1c9Sdrahn 	}
2651c074d1c9Sdrahn     }
2652c074d1c9Sdrahn 
2653c074d1c9Sdrahn   loc = link_order->offset * bfd_octets_per_byte (abfd);
2654c074d1c9Sdrahn   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2655c074d1c9Sdrahn 
2656c074d1c9Sdrahn   if (fill != link_order->u.data.contents)
2657c074d1c9Sdrahn     free (fill);
26582159047fSniklas   return result;
26592159047fSniklas }
26602159047fSniklas 
26612159047fSniklas /* Default routine to handle a bfd_indirect_link_order.  */
26622159047fSniklas 
2663c074d1c9Sdrahn 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)2664*007c2a45Smiod default_indirect_link_order (bfd *output_bfd,
2665*007c2a45Smiod 			     struct bfd_link_info *info,
2666*007c2a45Smiod 			     asection *output_section,
2667*007c2a45Smiod 			     struct bfd_link_order *link_order,
2668*007c2a45Smiod 			     bfd_boolean generic_linker)
26692159047fSniklas {
26702159047fSniklas   asection *input_section;
26712159047fSniklas   bfd *input_bfd;
26722159047fSniklas   bfd_byte *contents = NULL;
26732159047fSniklas   bfd_byte *new_contents;
2674c074d1c9Sdrahn   bfd_size_type sec_size;
2675c074d1c9Sdrahn   file_ptr loc;
26762159047fSniklas 
26772159047fSniklas   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
26782159047fSniklas 
26792159047fSniklas   if (link_order->size == 0)
2680c074d1c9Sdrahn     return TRUE;
26812159047fSniklas 
26822159047fSniklas   input_section = link_order->u.indirect.section;
26832159047fSniklas   input_bfd = input_section->owner;
26842159047fSniklas 
26852159047fSniklas   BFD_ASSERT (input_section->output_section == output_section);
26862159047fSniklas   BFD_ASSERT (input_section->output_offset == link_order->offset);
26872159047fSniklas   BFD_ASSERT (input_section->_cooked_size == link_order->size);
26882159047fSniklas 
2689*007c2a45Smiod   if (info->relocatable
26902159047fSniklas       && input_section->reloc_count > 0
2691*007c2a45Smiod       && output_section->orelocation == NULL)
26922159047fSniklas     {
26932159047fSniklas       /* Space has not been allocated for the output relocations.
26942159047fSniklas 	 This can happen when we are called by a specific backend
26952159047fSniklas 	 because somebody is attempting to link together different
26962159047fSniklas 	 types of object files.  Handling this case correctly is
26972159047fSniklas 	 difficult, and sometimes impossible.  */
2698e93f7393Sniklas       (*_bfd_error_handler)
2699*007c2a45Smiod 	(_("Attempt to do relocatable link with %s input and %s output"),
2700e93f7393Sniklas 	 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2701e93f7393Sniklas       bfd_set_error (bfd_error_wrong_format);
2702c074d1c9Sdrahn       return FALSE;
27032159047fSniklas     }
27042159047fSniklas 
27052159047fSniklas   if (! generic_linker)
27062159047fSniklas     {
27072159047fSniklas       asymbol **sympp;
27082159047fSniklas       asymbol **symppend;
27092159047fSniklas 
27102159047fSniklas       /* Get the canonical symbols.  The generic linker will always
27112159047fSniklas 	 have retrieved them by this point, but we are being called by
27122159047fSniklas 	 a specific linker, presumably because we are linking
27132159047fSniklas 	 different types of object files together.  */
27142159047fSniklas       if (! generic_link_read_symbols (input_bfd))
2715c074d1c9Sdrahn 	return FALSE;
27162159047fSniklas 
27172159047fSniklas       /* Since we have been called by a specific linker, rather than
27182159047fSniklas 	 the generic linker, the values of the symbols will not be
27192159047fSniklas 	 right.  They will be the values as seen in the input file,
27202159047fSniklas 	 not the values of the final link.  We need to fix them up
27212159047fSniklas 	 before we can relocate the section.  */
27222159047fSniklas       sympp = _bfd_generic_link_get_symbols (input_bfd);
27232159047fSniklas       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
27242159047fSniklas       for (; sympp < symppend; sympp++)
27252159047fSniklas 	{
27262159047fSniklas 	  asymbol *sym;
27272159047fSniklas 	  struct bfd_link_hash_entry *h;
27282159047fSniklas 
27292159047fSniklas 	  sym = *sympp;
27302159047fSniklas 
27312159047fSniklas 	  if ((sym->flags & (BSF_INDIRECT
27322159047fSniklas 			     | BSF_WARNING
27332159047fSniklas 			     | BSF_GLOBAL
27342159047fSniklas 			     | BSF_CONSTRUCTOR
27352159047fSniklas 			     | BSF_WEAK)) != 0
27362159047fSniklas 	      || bfd_is_und_section (bfd_get_section (sym))
27372159047fSniklas 	      || bfd_is_com_section (bfd_get_section (sym))
27382159047fSniklas 	      || bfd_is_ind_section (bfd_get_section (sym)))
27392159047fSniklas 	    {
27402159047fSniklas 	      /* sym->udata may have been set by
27412159047fSniklas 		 generic_link_add_symbol_list.  */
27422159047fSniklas 	      if (sym->udata.p != NULL)
2743*007c2a45Smiod 		h = sym->udata.p;
2744c88b1d6cSniklas 	      else if (bfd_is_und_section (bfd_get_section (sym)))
2745c88b1d6cSniklas 		h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2746c88b1d6cSniklas 						  bfd_asymbol_name (sym),
2747c074d1c9Sdrahn 						  FALSE, FALSE, TRUE);
27482159047fSniklas 	      else
27492159047fSniklas 		h = bfd_link_hash_lookup (info->hash,
27502159047fSniklas 					  bfd_asymbol_name (sym),
2751c074d1c9Sdrahn 					  FALSE, FALSE, TRUE);
27522159047fSniklas 	      if (h != NULL)
27532159047fSniklas 		set_symbol_from_hash (sym, h);
27542159047fSniklas 	    }
27552159047fSniklas 	}
27562159047fSniklas     }
27572159047fSniklas 
27582159047fSniklas   /* Get and relocate the section contents.  */
2759c074d1c9Sdrahn   sec_size = bfd_section_size (input_bfd, input_section);
2760*007c2a45Smiod   contents = bfd_malloc (sec_size);
2761c074d1c9Sdrahn   if (contents == NULL && sec_size != 0)
27622159047fSniklas     goto error_return;
27632159047fSniklas   new_contents = (bfd_get_relocated_section_contents
2764*007c2a45Smiod 		  (output_bfd, info, link_order, contents, info->relocatable,
27652159047fSniklas 		   _bfd_generic_link_get_symbols (input_bfd)));
27662159047fSniklas   if (!new_contents)
27672159047fSniklas     goto error_return;
27682159047fSniklas 
27692159047fSniklas   /* Output the section contents.  */
2770c074d1c9Sdrahn   loc = link_order->offset * bfd_octets_per_byte (output_bfd);
27712159047fSniklas   if (! bfd_set_section_contents (output_bfd, output_section,
2772*007c2a45Smiod 				  new_contents, loc, link_order->size))
27732159047fSniklas     goto error_return;
27742159047fSniklas 
27752159047fSniklas   if (contents != NULL)
27762159047fSniklas     free (contents);
2777c074d1c9Sdrahn   return TRUE;
27782159047fSniklas 
27792159047fSniklas  error_return:
27802159047fSniklas   if (contents != NULL)
27812159047fSniklas     free (contents);
2782c074d1c9Sdrahn   return FALSE;
27832159047fSniklas }
27842159047fSniklas 
27852159047fSniklas /* A little routine to count the number of relocs in a link_order
27862159047fSniklas    list.  */
27872159047fSniklas 
27882159047fSniklas unsigned int
_bfd_count_link_order_relocs(struct bfd_link_order * link_order)2789*007c2a45Smiod _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
27902159047fSniklas {
27912159047fSniklas   register unsigned int c;
27922159047fSniklas   register struct bfd_link_order *l;
27932159047fSniklas 
27942159047fSniklas   c = 0;
2795*007c2a45Smiod   for (l = link_order; l != NULL; l = l->next)
27962159047fSniklas     {
27972159047fSniklas       if (l->type == bfd_section_reloc_link_order
27982159047fSniklas 	  || l->type == bfd_symbol_reloc_link_order)
27992159047fSniklas 	++c;
28002159047fSniklas     }
28012159047fSniklas 
28022159047fSniklas   return c;
28032159047fSniklas }
28042159047fSniklas 
28052159047fSniklas /*
28062159047fSniklas FUNCTION
28072159047fSniklas 	bfd_link_split_section
28082159047fSniklas 
28092159047fSniklas SYNOPSIS
2810c074d1c9Sdrahn         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
28112159047fSniklas 
28122159047fSniklas DESCRIPTION
28132159047fSniklas 	Return nonzero if @var{sec} should be split during a
28142159047fSniklas 	reloceatable or final link.
28152159047fSniklas 
28162159047fSniklas .#define bfd_link_split_section(abfd, sec) \
28172159047fSniklas .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
28182159047fSniklas .
28192159047fSniklas 
28202159047fSniklas */
28212159047fSniklas 
2822c074d1c9Sdrahn bfd_boolean
_bfd_generic_link_split_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)2823*007c2a45Smiod _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2824*007c2a45Smiod 				 asection *sec ATTRIBUTE_UNUSED)
28252159047fSniklas {
2826c074d1c9Sdrahn   return FALSE;
28272159047fSniklas }
2828