xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/linker.c (revision b841744944f6c4f20c64e53feab02094e01cb9ec)
13d8817e4Smiod /* linker.c -- BFD linker routines
23d8817e4Smiod    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
33d8817e4Smiod    2003, 2004, 2005, 2006 Free Software Foundation, Inc.
43d8817e4Smiod    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
53d8817e4Smiod 
63d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
73d8817e4Smiod 
83d8817e4Smiod    This program is free software; you can redistribute it and/or modify
93d8817e4Smiod    it under the terms of the GNU General Public License as published by
103d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
113d8817e4Smiod    (at your option) any later version.
123d8817e4Smiod 
133d8817e4Smiod    This program is distributed in the hope that it will be useful,
143d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
153d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
163d8817e4Smiod    GNU General Public License for more details.
173d8817e4Smiod 
183d8817e4Smiod    You should have received a copy of the GNU General Public License
193d8817e4Smiod    along with this program; if not, write to the Free Software
203d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
213d8817e4Smiod 
223d8817e4Smiod #include "bfd.h"
233d8817e4Smiod #include "sysdep.h"
243d8817e4Smiod #include "libbfd.h"
253d8817e4Smiod #include "bfdlink.h"
263d8817e4Smiod #include "genlink.h"
273d8817e4Smiod 
283d8817e4Smiod /*
293d8817e4Smiod SECTION
303d8817e4Smiod 	Linker Functions
313d8817e4Smiod 
323d8817e4Smiod @cindex Linker
333d8817e4Smiod 	The linker uses three special entry points in the BFD target
343d8817e4Smiod 	vector.  It is not necessary to write special routines for
353d8817e4Smiod 	these entry points when creating a new BFD back end, since
363d8817e4Smiod 	generic versions are provided.  However, writing them can
373d8817e4Smiod 	speed up linking and make it use significantly less runtime
383d8817e4Smiod 	memory.
393d8817e4Smiod 
403d8817e4Smiod 	The first routine creates a hash table used by the other
413d8817e4Smiod 	routines.  The second routine adds the symbols from an object
423d8817e4Smiod 	file to the hash table.  The third routine takes all the
433d8817e4Smiod 	object files and links them together to create the output
443d8817e4Smiod 	file.  These routines are designed so that the linker proper
453d8817e4Smiod 	does not need to know anything about the symbols in the object
463d8817e4Smiod 	files that it is linking.  The linker merely arranges the
473d8817e4Smiod 	sections as directed by the linker script and lets BFD handle
483d8817e4Smiod 	the details of symbols and relocs.
493d8817e4Smiod 
503d8817e4Smiod 	The second routine and third routines are passed a pointer to
513d8817e4Smiod 	a <<struct bfd_link_info>> structure (defined in
523d8817e4Smiod 	<<bfdlink.h>>) which holds information relevant to the link,
533d8817e4Smiod 	including the linker hash table (which was created by the
543d8817e4Smiod 	first routine) and a set of callback functions to the linker
553d8817e4Smiod 	proper.
563d8817e4Smiod 
573d8817e4Smiod 	The generic linker routines are in <<linker.c>>, and use the
583d8817e4Smiod 	header file <<genlink.h>>.  As of this writing, the only back
593d8817e4Smiod 	ends which have implemented versions of these routines are
603d8817e4Smiod 	a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
613d8817e4Smiod 	routines are used as examples throughout this section.
623d8817e4Smiod 
633d8817e4Smiod @menu
643d8817e4Smiod @* Creating a Linker Hash Table::
653d8817e4Smiod @* Adding Symbols to the Hash Table::
663d8817e4Smiod @* Performing the Final Link::
673d8817e4Smiod @end menu
683d8817e4Smiod 
693d8817e4Smiod INODE
703d8817e4Smiod Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
713d8817e4Smiod SUBSECTION
723d8817e4Smiod 	Creating a linker hash table
733d8817e4Smiod 
743d8817e4Smiod @cindex _bfd_link_hash_table_create in target vector
753d8817e4Smiod @cindex target vector (_bfd_link_hash_table_create)
763d8817e4Smiod 	The linker routines must create a hash table, which must be
773d8817e4Smiod 	derived from <<struct bfd_link_hash_table>> described in
783d8817e4Smiod 	<<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
793d8817e4Smiod 	create a derived hash table.  This entry point is called using
803d8817e4Smiod 	the target vector of the linker output file.
813d8817e4Smiod 
823d8817e4Smiod 	The <<_bfd_link_hash_table_create>> entry point must allocate
833d8817e4Smiod 	and initialize an instance of the desired hash table.  If the
843d8817e4Smiod 	back end does not require any additional information to be
853d8817e4Smiod 	stored with the entries in the hash table, the entry point may
863d8817e4Smiod 	simply create a <<struct bfd_link_hash_table>>.  Most likely,
873d8817e4Smiod 	however, some additional information will be needed.
883d8817e4Smiod 
893d8817e4Smiod 	For example, with each entry in the hash table the a.out
903d8817e4Smiod 	linker keeps the index the symbol has in the final output file
913d8817e4Smiod 	(this index number is used so that when doing a relocatable
923d8817e4Smiod 	link the symbol index used in the output file can be quickly
933d8817e4Smiod 	filled in when copying over a reloc).  The a.out linker code
943d8817e4Smiod 	defines the required structures and functions for a hash table
953d8817e4Smiod 	derived from <<struct bfd_link_hash_table>>.  The a.out linker
963d8817e4Smiod 	hash table is created by the function
973d8817e4Smiod 	<<NAME(aout,link_hash_table_create)>>; it simply allocates
983d8817e4Smiod 	space for the hash table, initializes it, and returns a
993d8817e4Smiod 	pointer to it.
1003d8817e4Smiod 
1013d8817e4Smiod 	When writing the linker routines for a new back end, you will
1023d8817e4Smiod 	generally not know exactly which fields will be required until
1033d8817e4Smiod 	you have finished.  You should simply create a new hash table
1043d8817e4Smiod 	which defines no additional fields, and then simply add fields
1053d8817e4Smiod 	as they become necessary.
1063d8817e4Smiod 
1073d8817e4Smiod INODE
1083d8817e4Smiod Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
1093d8817e4Smiod SUBSECTION
1103d8817e4Smiod 	Adding symbols to the hash table
1113d8817e4Smiod 
1123d8817e4Smiod @cindex _bfd_link_add_symbols in target vector
1133d8817e4Smiod @cindex target vector (_bfd_link_add_symbols)
1143d8817e4Smiod 	The linker proper will call the <<_bfd_link_add_symbols>>
1153d8817e4Smiod 	entry point for each object file or archive which is to be
1163d8817e4Smiod 	linked (typically these are the files named on the command
1173d8817e4Smiod 	line, but some may also come from the linker script).  The
1183d8817e4Smiod 	entry point is responsible for examining the file.  For an
1193d8817e4Smiod 	object file, BFD must add any relevant symbol information to
1203d8817e4Smiod 	the hash table.  For an archive, BFD must determine which
1213d8817e4Smiod 	elements of the archive should be used and adding them to the
1223d8817e4Smiod 	link.
1233d8817e4Smiod 
1243d8817e4Smiod 	The a.out version of this entry point is
1253d8817e4Smiod 	<<NAME(aout,link_add_symbols)>>.
1263d8817e4Smiod 
1273d8817e4Smiod @menu
1283d8817e4Smiod @* Differing file formats::
1293d8817e4Smiod @* Adding symbols from an object file::
1303d8817e4Smiod @* Adding symbols from an archive::
1313d8817e4Smiod @end menu
1323d8817e4Smiod 
1333d8817e4Smiod INODE
1343d8817e4Smiod Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
1353d8817e4Smiod SUBSUBSECTION
1363d8817e4Smiod 	Differing file formats
1373d8817e4Smiod 
1383d8817e4Smiod 	Normally all the files involved in a link will be of the same
1393d8817e4Smiod 	format, but it is also possible to link together different
1403d8817e4Smiod 	format object files, and the back end must support that.  The
1413d8817e4Smiod 	<<_bfd_link_add_symbols>> entry point is called via the target
1423d8817e4Smiod 	vector of the file to be added.  This has an important
1433d8817e4Smiod 	consequence: the function may not assume that the hash table
1443d8817e4Smiod 	is the type created by the corresponding
1453d8817e4Smiod 	<<_bfd_link_hash_table_create>> vector.  All the
1463d8817e4Smiod 	<<_bfd_link_add_symbols>> function can assume about the hash
1473d8817e4Smiod 	table is that it is derived from <<struct
1483d8817e4Smiod 	bfd_link_hash_table>>.
1493d8817e4Smiod 
1503d8817e4Smiod 	Sometimes the <<_bfd_link_add_symbols>> function must store
1513d8817e4Smiod 	some information in the hash table entry to be used by the
1523d8817e4Smiod 	<<_bfd_final_link>> function.  In such a case the <<creator>>
1533d8817e4Smiod 	field of the hash table must be checked to make sure that the
1543d8817e4Smiod 	hash table was created by an object file of the same format.
1553d8817e4Smiod 
1563d8817e4Smiod 	The <<_bfd_final_link>> routine must be prepared to handle a
1573d8817e4Smiod 	hash entry without any extra information added by the
1583d8817e4Smiod 	<<_bfd_link_add_symbols>> function.  A hash entry without
1593d8817e4Smiod 	extra information will also occur when the linker script
1603d8817e4Smiod 	directs the linker to create a symbol.  Note that, regardless
1613d8817e4Smiod 	of how a hash table entry is added, all the fields will be
1623d8817e4Smiod 	initialized to some sort of null value by the hash table entry
1633d8817e4Smiod 	initialization function.
1643d8817e4Smiod 
1653d8817e4Smiod 	See <<ecoff_link_add_externals>> for an example of how to
1663d8817e4Smiod 	check the <<creator>> field before saving information (in this
1673d8817e4Smiod 	case, the ECOFF external symbol debugging information) in a
1683d8817e4Smiod 	hash table entry.
1693d8817e4Smiod 
1703d8817e4Smiod INODE
1713d8817e4Smiod Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
1723d8817e4Smiod SUBSUBSECTION
1733d8817e4Smiod 	Adding symbols from an object file
1743d8817e4Smiod 
1753d8817e4Smiod 	When the <<_bfd_link_add_symbols>> routine is passed an object
1763d8817e4Smiod 	file, it must add all externally visible symbols in that
1773d8817e4Smiod 	object file to the hash table.  The actual work of adding the
1783d8817e4Smiod 	symbol to the hash table is normally handled by the function
1793d8817e4Smiod 	<<_bfd_generic_link_add_one_symbol>>.  The
1803d8817e4Smiod 	<<_bfd_link_add_symbols>> routine is responsible for reading
1813d8817e4Smiod 	all the symbols from the object file and passing the correct
1823d8817e4Smiod 	information to <<_bfd_generic_link_add_one_symbol>>.
1833d8817e4Smiod 
1843d8817e4Smiod 	The <<_bfd_link_add_symbols>> routine should not use
1853d8817e4Smiod 	<<bfd_canonicalize_symtab>> to read the symbols.  The point of
1863d8817e4Smiod 	providing this routine is to avoid the overhead of converting
1873d8817e4Smiod 	the symbols into generic <<asymbol>> structures.
1883d8817e4Smiod 
1893d8817e4Smiod @findex _bfd_generic_link_add_one_symbol
1903d8817e4Smiod 	<<_bfd_generic_link_add_one_symbol>> handles the details of
1913d8817e4Smiod 	combining common symbols, warning about multiple definitions,
1923d8817e4Smiod 	and so forth.  It takes arguments which describe the symbol to
1933d8817e4Smiod 	add, notably symbol flags, a section, and an offset.  The
1943d8817e4Smiod 	symbol flags include such things as <<BSF_WEAK>> or
1953d8817e4Smiod 	<<BSF_INDIRECT>>.  The section is a section in the object
1963d8817e4Smiod 	file, or something like <<bfd_und_section_ptr>> for an undefined
1973d8817e4Smiod 	symbol or <<bfd_com_section_ptr>> for a common symbol.
1983d8817e4Smiod 
1993d8817e4Smiod 	If the <<_bfd_final_link>> routine is also going to need to
2003d8817e4Smiod 	read the symbol information, the <<_bfd_link_add_symbols>>
2013d8817e4Smiod 	routine should save it somewhere attached to the object file
2023d8817e4Smiod 	BFD.  However, the information should only be saved if the
2033d8817e4Smiod 	<<keep_memory>> field of the <<info>> argument is TRUE, so
2043d8817e4Smiod 	that the <<-no-keep-memory>> linker switch is effective.
2053d8817e4Smiod 
2063d8817e4Smiod 	The a.out function which adds symbols from an object file is
2073d8817e4Smiod 	<<aout_link_add_object_symbols>>, and most of the interesting
2083d8817e4Smiod 	work is in <<aout_link_add_symbols>>.  The latter saves
2093d8817e4Smiod 	pointers to the hash tables entries created by
2103d8817e4Smiod 	<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
2113d8817e4Smiod 	so that the <<_bfd_final_link>> routine does not have to call
2123d8817e4Smiod 	the hash table lookup routine to locate the entry.
2133d8817e4Smiod 
2143d8817e4Smiod INODE
2153d8817e4Smiod Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
2163d8817e4Smiod SUBSUBSECTION
2173d8817e4Smiod 	Adding symbols from an archive
2183d8817e4Smiod 
2193d8817e4Smiod 	When the <<_bfd_link_add_symbols>> routine is passed an
2203d8817e4Smiod 	archive, it must look through the symbols defined by the
2213d8817e4Smiod 	archive and decide which elements of the archive should be
2223d8817e4Smiod 	included in the link.  For each such element it must call the
2233d8817e4Smiod 	<<add_archive_element>> linker callback, and it must add the
2243d8817e4Smiod 	symbols from the object file to the linker hash table.
2253d8817e4Smiod 
2263d8817e4Smiod @findex _bfd_generic_link_add_archive_symbols
2273d8817e4Smiod 	In most cases the work of looking through the symbols in the
2283d8817e4Smiod 	archive should be done by the
2293d8817e4Smiod 	<<_bfd_generic_link_add_archive_symbols>> function.  This
2303d8817e4Smiod 	function builds a hash table from the archive symbol table and
2313d8817e4Smiod 	looks through the list of undefined symbols to see which
2323d8817e4Smiod 	elements should be included.
2333d8817e4Smiod 	<<_bfd_generic_link_add_archive_symbols>> is passed a function
2343d8817e4Smiod 	to call to make the final decision about adding an archive
2353d8817e4Smiod 	element to the link and to do the actual work of adding the
2363d8817e4Smiod 	symbols to the linker hash table.
2373d8817e4Smiod 
2383d8817e4Smiod 	The function passed to
2393d8817e4Smiod 	<<_bfd_generic_link_add_archive_symbols>> must read the
2403d8817e4Smiod 	symbols of the archive element and decide whether the archive
2413d8817e4Smiod 	element should be included in the link.  If the element is to
2423d8817e4Smiod 	be included, the <<add_archive_element>> linker callback
2433d8817e4Smiod 	routine must be called with the element as an argument, and
2443d8817e4Smiod 	the elements symbols must be added to the linker hash table
2453d8817e4Smiod 	just as though the element had itself been passed to the
2463d8817e4Smiod 	<<_bfd_link_add_symbols>> function.
2473d8817e4Smiod 
2483d8817e4Smiod 	When the a.out <<_bfd_link_add_symbols>> function receives an
2493d8817e4Smiod 	archive, it calls <<_bfd_generic_link_add_archive_symbols>>
2503d8817e4Smiod 	passing <<aout_link_check_archive_element>> as the function
2513d8817e4Smiod 	argument. <<aout_link_check_archive_element>> calls
2523d8817e4Smiod 	<<aout_link_check_ar_symbols>>.  If the latter decides to add
2533d8817e4Smiod 	the element (an element is only added if it provides a real,
2543d8817e4Smiod 	non-common, definition for a previously undefined or common
2553d8817e4Smiod 	symbol) it calls the <<add_archive_element>> callback and then
2563d8817e4Smiod 	<<aout_link_check_archive_element>> calls
2573d8817e4Smiod 	<<aout_link_add_symbols>> to actually add the symbols to the
2583d8817e4Smiod 	linker hash table.
2593d8817e4Smiod 
2603d8817e4Smiod 	The ECOFF back end is unusual in that it does not normally
2613d8817e4Smiod 	call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
2623d8817e4Smiod 	archives already contain a hash table of symbols.  The ECOFF
2633d8817e4Smiod 	back end searches the archive itself to avoid the overhead of
2643d8817e4Smiod 	creating a new hash table.
2653d8817e4Smiod 
2663d8817e4Smiod INODE
2673d8817e4Smiod Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
2683d8817e4Smiod SUBSECTION
2693d8817e4Smiod 	Performing the final link
2703d8817e4Smiod 
2713d8817e4Smiod @cindex _bfd_link_final_link in target vector
2723d8817e4Smiod @cindex target vector (_bfd_final_link)
2733d8817e4Smiod 	When all the input files have been processed, the linker calls
2743d8817e4Smiod 	the <<_bfd_final_link>> entry point of the output BFD.  This
2753d8817e4Smiod 	routine is responsible for producing the final output file,
2763d8817e4Smiod 	which has several aspects.  It must relocate the contents of
2773d8817e4Smiod 	the input sections and copy the data into the output sections.
2783d8817e4Smiod 	It must build an output symbol table including any local
2793d8817e4Smiod 	symbols from the input files and the global symbols from the
2803d8817e4Smiod 	hash table.  When producing relocatable output, it must
2813d8817e4Smiod 	modify the input relocs and write them into the output file.
2823d8817e4Smiod 	There may also be object format dependent work to be done.
2833d8817e4Smiod 
2843d8817e4Smiod 	The linker will also call the <<write_object_contents>> entry
2853d8817e4Smiod 	point when the BFD is closed.  The two entry points must work
2863d8817e4Smiod 	together in order to produce the correct output file.
2873d8817e4Smiod 
2883d8817e4Smiod 	The details of how this works are inevitably dependent upon
2893d8817e4Smiod 	the specific object file format.  The a.out
2903d8817e4Smiod 	<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
2913d8817e4Smiod 
2923d8817e4Smiod @menu
2933d8817e4Smiod @* Information provided by the linker::
2943d8817e4Smiod @* Relocating the section contents::
2953d8817e4Smiod @* Writing the symbol table::
2963d8817e4Smiod @end menu
2973d8817e4Smiod 
2983d8817e4Smiod INODE
2993d8817e4Smiod Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
3003d8817e4Smiod SUBSUBSECTION
3013d8817e4Smiod 	Information provided by the linker
3023d8817e4Smiod 
3033d8817e4Smiod 	Before the linker calls the <<_bfd_final_link>> entry point,
3043d8817e4Smiod 	it sets up some data structures for the function to use.
3053d8817e4Smiod 
3063d8817e4Smiod 	The <<input_bfds>> field of the <<bfd_link_info>> structure
3073d8817e4Smiod 	will point to a list of all the input files included in the
3083d8817e4Smiod 	link.  These files are linked through the <<link_next>> field
3093d8817e4Smiod 	of the <<bfd>> structure.
3103d8817e4Smiod 
3113d8817e4Smiod 	Each section in the output file will have a list of
3123d8817e4Smiod 	<<link_order>> structures attached to the <<map_head.link_order>>
3133d8817e4Smiod 	field (the <<link_order>> structure is defined in
3143d8817e4Smiod 	<<bfdlink.h>>).  These structures describe how to create the
3153d8817e4Smiod 	contents of the output section in terms of the contents of
3163d8817e4Smiod 	various input sections, fill constants, and, eventually, other
3173d8817e4Smiod 	types of information.  They also describe relocs that must be
3183d8817e4Smiod 	created by the BFD backend, but do not correspond to any input
3193d8817e4Smiod 	file; this is used to support -Ur, which builds constructors
3203d8817e4Smiod 	while generating a relocatable object file.
3213d8817e4Smiod 
3223d8817e4Smiod INODE
3233d8817e4Smiod Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
3243d8817e4Smiod SUBSUBSECTION
3253d8817e4Smiod 	Relocating the section contents
3263d8817e4Smiod 
3273d8817e4Smiod 	The <<_bfd_final_link>> function should look through the
3283d8817e4Smiod 	<<link_order>> structures attached to each section of the
3293d8817e4Smiod 	output file.  Each <<link_order>> structure should either be
3303d8817e4Smiod 	handled specially, or it should be passed to the function
3313d8817e4Smiod 	<<_bfd_default_link_order>> which will do the right thing
3323d8817e4Smiod 	(<<_bfd_default_link_order>> is defined in <<linker.c>>).
3333d8817e4Smiod 
3343d8817e4Smiod 	For efficiency, a <<link_order>> of type
3353d8817e4Smiod 	<<bfd_indirect_link_order>> whose associated section belongs
3363d8817e4Smiod 	to a BFD of the same format as the output BFD must be handled
3373d8817e4Smiod 	specially.  This type of <<link_order>> describes part of an
3383d8817e4Smiod 	output section in terms of a section belonging to one of the
3393d8817e4Smiod 	input files.  The <<_bfd_final_link>> function should read the
3403d8817e4Smiod 	contents of the section and any associated relocs, apply the
3413d8817e4Smiod 	relocs to the section contents, and write out the modified
3423d8817e4Smiod 	section contents.  If performing a relocatable link, the
3433d8817e4Smiod 	relocs themselves must also be modified and written out.
3443d8817e4Smiod 
3453d8817e4Smiod @findex _bfd_relocate_contents
3463d8817e4Smiod @findex _bfd_final_link_relocate
3473d8817e4Smiod 	The functions <<_bfd_relocate_contents>> and
3483d8817e4Smiod 	<<_bfd_final_link_relocate>> provide some general support for
3493d8817e4Smiod 	performing the actual relocations, notably overflow checking.
3503d8817e4Smiod 	Their arguments include information about the symbol the
3513d8817e4Smiod 	relocation is against and a <<reloc_howto_type>> argument
3523d8817e4Smiod 	which describes the relocation to perform.  These functions
3533d8817e4Smiod 	are defined in <<reloc.c>>.
3543d8817e4Smiod 
3553d8817e4Smiod 	The a.out function which handles reading, relocating, and
3563d8817e4Smiod 	writing section contents is <<aout_link_input_section>>.  The
3573d8817e4Smiod 	actual relocation is done in <<aout_link_input_section_std>>
3583d8817e4Smiod 	and <<aout_link_input_section_ext>>.
3593d8817e4Smiod 
3603d8817e4Smiod INODE
3613d8817e4Smiod Writing the symbol table, , Relocating the section contents, Performing the Final Link
3623d8817e4Smiod SUBSUBSECTION
3633d8817e4Smiod 	Writing the symbol table
3643d8817e4Smiod 
3653d8817e4Smiod 	The <<_bfd_final_link>> function must gather all the symbols
3663d8817e4Smiod 	in the input files and write them out.  It must also write out
3673d8817e4Smiod 	all the symbols in the global hash table.  This must be
3683d8817e4Smiod 	controlled by the <<strip>> and <<discard>> fields of the
3693d8817e4Smiod 	<<bfd_link_info>> structure.
3703d8817e4Smiod 
3713d8817e4Smiod 	The local symbols of the input files will not have been
3723d8817e4Smiod 	entered into the linker hash table.  The <<_bfd_final_link>>
3733d8817e4Smiod 	routine must consider each input file and include the symbols
3743d8817e4Smiod 	in the output file.  It may be convenient to do this when
3753d8817e4Smiod 	looking through the <<link_order>> structures, or it may be
3763d8817e4Smiod 	done by stepping through the <<input_bfds>> list.
3773d8817e4Smiod 
3783d8817e4Smiod 	The <<_bfd_final_link>> routine must also traverse the global
3793d8817e4Smiod 	hash table to gather all the externally visible symbols.  It
3803d8817e4Smiod 	is possible that most of the externally visible symbols may be
3813d8817e4Smiod 	written out when considering the symbols of each input file,
3823d8817e4Smiod 	but it is still necessary to traverse the hash table since the
3833d8817e4Smiod 	linker script may have defined some symbols that are not in
3843d8817e4Smiod 	any of the input files.
3853d8817e4Smiod 
3863d8817e4Smiod 	The <<strip>> field of the <<bfd_link_info>> structure
3873d8817e4Smiod 	controls which symbols are written out.  The possible values
3883d8817e4Smiod 	are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
3893d8817e4Smiod 	then the <<keep_hash>> field of the <<bfd_link_info>>
3903d8817e4Smiod 	structure is a hash table of symbols to keep; each symbol
3913d8817e4Smiod 	should be looked up in this hash table, and only symbols which
3923d8817e4Smiod 	are present should be included in the output file.
3933d8817e4Smiod 
3943d8817e4Smiod 	If the <<strip>> field of the <<bfd_link_info>> structure
3953d8817e4Smiod 	permits local symbols to be written out, the <<discard>> field
3963d8817e4Smiod 	is used to further controls which local symbols are included
3973d8817e4Smiod 	in the output file.  If the value is <<discard_l>>, then all
3983d8817e4Smiod 	local symbols which begin with a certain prefix are discarded;
3993d8817e4Smiod 	this is controlled by the <<bfd_is_local_label_name>> entry point.
4003d8817e4Smiod 
4013d8817e4Smiod 	The a.out backend handles symbols by calling
4023d8817e4Smiod 	<<aout_link_write_symbols>> on each input BFD and then
4033d8817e4Smiod 	traversing the global hash table with the function
4043d8817e4Smiod 	<<aout_link_write_other_symbol>>.  It builds a string table
4053d8817e4Smiod 	while writing out the symbols, which is written to the output
4063d8817e4Smiod 	file at the end of <<NAME(aout,final_link)>>.
4073d8817e4Smiod */
4083d8817e4Smiod 
4093d8817e4Smiod static bfd_boolean generic_link_add_object_symbols
4103d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_boolean collect);
4113d8817e4Smiod static bfd_boolean generic_link_add_symbols
4123d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_boolean);
4133d8817e4Smiod static bfd_boolean generic_link_check_archive_element_no_collect
4143d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *);
4153d8817e4Smiod static bfd_boolean generic_link_check_archive_element_collect
4163d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *);
4173d8817e4Smiod static bfd_boolean generic_link_check_archive_element
4183d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
4193d8817e4Smiod static bfd_boolean generic_link_add_symbol_list
4203d8817e4Smiod   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
4213d8817e4Smiod    bfd_boolean);
4223d8817e4Smiod static bfd_boolean generic_add_output_symbol
4233d8817e4Smiod   (bfd *, size_t *psymalloc, asymbol *);
4243d8817e4Smiod static bfd_boolean default_data_link_order
4253d8817e4Smiod   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
4263d8817e4Smiod static bfd_boolean default_indirect_link_order
4273d8817e4Smiod   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
4283d8817e4Smiod    bfd_boolean);
4293d8817e4Smiod 
4303d8817e4Smiod /* The link hash table structure is defined in bfdlink.h.  It provides
4313d8817e4Smiod    a base hash table which the backend specific hash tables are built
4323d8817e4Smiod    upon.  */
4333d8817e4Smiod 
4343d8817e4Smiod /* Routine to create an entry in the link hash table.  */
4353d8817e4Smiod 
4363d8817e4Smiod struct bfd_hash_entry *
_bfd_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)4373d8817e4Smiod _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
4383d8817e4Smiod 			struct bfd_hash_table *table,
4393d8817e4Smiod 			const char *string)
4403d8817e4Smiod {
4413d8817e4Smiod   /* Allocate the structure if it has not already been allocated by a
4423d8817e4Smiod      subclass.  */
4433d8817e4Smiod   if (entry == NULL)
4443d8817e4Smiod     {
4453d8817e4Smiod       entry = bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
4463d8817e4Smiod       if (entry == NULL)
4473d8817e4Smiod 	return entry;
4483d8817e4Smiod     }
4493d8817e4Smiod 
4503d8817e4Smiod   /* Call the allocation method of the superclass.  */
4513d8817e4Smiod   entry = bfd_hash_newfunc (entry, table, string);
4523d8817e4Smiod   if (entry)
4533d8817e4Smiod     {
4543d8817e4Smiod       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
4553d8817e4Smiod 
4563d8817e4Smiod       /* Initialize the local fields.  */
4573d8817e4Smiod       h->type = bfd_link_hash_new;
4583d8817e4Smiod       memset (&h->u.undef.next, 0,
4593d8817e4Smiod 	      (sizeof (struct bfd_link_hash_entry)
4603d8817e4Smiod 	       - offsetof (struct bfd_link_hash_entry, u.undef.next)));
4613d8817e4Smiod     }
4623d8817e4Smiod 
4633d8817e4Smiod   return entry;
4643d8817e4Smiod }
4653d8817e4Smiod 
4663d8817e4Smiod /* Initialize a link hash table.  The BFD argument is the one
4673d8817e4Smiod    responsible for creating this table.  */
4683d8817e4Smiod 
4693d8817e4Smiod 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 *),unsigned int entsize)4703d8817e4Smiod _bfd_link_hash_table_init
4713d8817e4Smiod   (struct bfd_link_hash_table *table,
4723d8817e4Smiod    bfd *abfd,
4733d8817e4Smiod    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
4743d8817e4Smiod 				      struct bfd_hash_table *,
4753d8817e4Smiod 				      const char *),
4763d8817e4Smiod    unsigned int entsize)
4773d8817e4Smiod {
4783d8817e4Smiod   table->creator = abfd->xvec;
4793d8817e4Smiod   table->undefs = NULL;
4803d8817e4Smiod   table->undefs_tail = NULL;
4813d8817e4Smiod   table->type = bfd_link_generic_hash_table;
4823d8817e4Smiod 
4833d8817e4Smiod   return bfd_hash_table_init (&table->table, newfunc, entsize);
4843d8817e4Smiod }
4853d8817e4Smiod 
4863d8817e4Smiod /* Look up a symbol in a link hash table.  If follow is TRUE, we
4873d8817e4Smiod    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
4883d8817e4Smiod    the real symbol.  */
4893d8817e4Smiod 
4903d8817e4Smiod 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)4913d8817e4Smiod bfd_link_hash_lookup (struct bfd_link_hash_table *table,
4923d8817e4Smiod 		      const char *string,
4933d8817e4Smiod 		      bfd_boolean create,
4943d8817e4Smiod 		      bfd_boolean copy,
4953d8817e4Smiod 		      bfd_boolean follow)
4963d8817e4Smiod {
4973d8817e4Smiod   struct bfd_link_hash_entry *ret;
4983d8817e4Smiod 
4993d8817e4Smiod   ret = ((struct bfd_link_hash_entry *)
5003d8817e4Smiod 	 bfd_hash_lookup (&table->table, string, create, copy));
5013d8817e4Smiod 
5023d8817e4Smiod   if (follow && ret != NULL)
5033d8817e4Smiod     {
5043d8817e4Smiod       while (ret->type == bfd_link_hash_indirect
5053d8817e4Smiod 	     || ret->type == bfd_link_hash_warning)
5063d8817e4Smiod 	ret = ret->u.i.link;
5073d8817e4Smiod     }
5083d8817e4Smiod 
5093d8817e4Smiod   return ret;
5103d8817e4Smiod }
5113d8817e4Smiod 
5123d8817e4Smiod /* Look up a symbol in the main linker hash table if the symbol might
5133d8817e4Smiod    be wrapped.  This should only be used for references to an
5143d8817e4Smiod    undefined symbol, not for definitions of a symbol.  */
5153d8817e4Smiod 
5163d8817e4Smiod 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)5173d8817e4Smiod bfd_wrapped_link_hash_lookup (bfd *abfd,
5183d8817e4Smiod 			      struct bfd_link_info *info,
5193d8817e4Smiod 			      const char *string,
5203d8817e4Smiod 			      bfd_boolean create,
5213d8817e4Smiod 			      bfd_boolean copy,
5223d8817e4Smiod 			      bfd_boolean follow)
5233d8817e4Smiod {
5243d8817e4Smiod   bfd_size_type amt;
5253d8817e4Smiod 
5263d8817e4Smiod   if (info->wrap_hash != NULL)
5273d8817e4Smiod     {
5283d8817e4Smiod       const char *l;
5293d8817e4Smiod       char prefix = '\0';
5303d8817e4Smiod 
5313d8817e4Smiod       l = string;
5323d8817e4Smiod       if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
5333d8817e4Smiod 	{
5343d8817e4Smiod 	  prefix = *l;
5353d8817e4Smiod 	  ++l;
5363d8817e4Smiod 	}
5373d8817e4Smiod 
5383d8817e4Smiod #undef WRAP
5393d8817e4Smiod #define WRAP "__wrap_"
5403d8817e4Smiod 
5413d8817e4Smiod       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
5423d8817e4Smiod 	{
5433d8817e4Smiod 	  char *n;
5443d8817e4Smiod 	  struct bfd_link_hash_entry *h;
5453d8817e4Smiod 
5463d8817e4Smiod 	  /* This symbol is being wrapped.  We want to replace all
5473d8817e4Smiod              references to SYM with references to __wrap_SYM.  */
5483d8817e4Smiod 
5493d8817e4Smiod 	  amt = strlen (l) + sizeof WRAP + 1;
5503d8817e4Smiod 	  n = bfd_malloc (amt);
5513d8817e4Smiod 	  if (n == NULL)
5523d8817e4Smiod 	    return NULL;
5533d8817e4Smiod 
5543d8817e4Smiod 	  n[0] = prefix;
5553d8817e4Smiod 	  n[1] = '\0';
5563d8817e4Smiod 	  strcat (n, WRAP);
5573d8817e4Smiod 	  strcat (n, l);
5583d8817e4Smiod 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
5593d8817e4Smiod 	  free (n);
5603d8817e4Smiod 	  return h;
5613d8817e4Smiod 	}
5623d8817e4Smiod 
5633d8817e4Smiod #undef WRAP
5643d8817e4Smiod 
5653d8817e4Smiod #undef REAL
5663d8817e4Smiod #define REAL "__real_"
5673d8817e4Smiod 
5683d8817e4Smiod       if (*l == '_'
5693d8817e4Smiod 	  && strncmp (l, REAL, sizeof REAL - 1) == 0
5703d8817e4Smiod 	  && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
5713d8817e4Smiod 			      FALSE, FALSE) != NULL)
5723d8817e4Smiod 	{
5733d8817e4Smiod 	  char *n;
5743d8817e4Smiod 	  struct bfd_link_hash_entry *h;
5753d8817e4Smiod 
5763d8817e4Smiod 	  /* This is a reference to __real_SYM, where SYM is being
5773d8817e4Smiod              wrapped.  We want to replace all references to __real_SYM
5783d8817e4Smiod              with references to SYM.  */
5793d8817e4Smiod 
5803d8817e4Smiod 	  amt = strlen (l + sizeof REAL - 1) + 2;
5813d8817e4Smiod 	  n = bfd_malloc (amt);
5823d8817e4Smiod 	  if (n == NULL)
5833d8817e4Smiod 	    return NULL;
5843d8817e4Smiod 
5853d8817e4Smiod 	  n[0] = prefix;
5863d8817e4Smiod 	  n[1] = '\0';
5873d8817e4Smiod 	  strcat (n, l + sizeof REAL - 1);
5883d8817e4Smiod 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
5893d8817e4Smiod 	  free (n);
5903d8817e4Smiod 	  return h;
5913d8817e4Smiod 	}
5923d8817e4Smiod 
5933d8817e4Smiod #undef REAL
5943d8817e4Smiod     }
5953d8817e4Smiod 
5963d8817e4Smiod   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
5973d8817e4Smiod }
5983d8817e4Smiod 
5993d8817e4Smiod /* Traverse a generic link hash table.  The only reason this is not a
6003d8817e4Smiod    macro is to do better type checking.  This code presumes that an
6013d8817e4Smiod    argument passed as a struct bfd_hash_entry * may be caught as a
6023d8817e4Smiod    struct bfd_link_hash_entry * with no explicit cast required on the
6033d8817e4Smiod    call.  */
6043d8817e4Smiod 
6053d8817e4Smiod void
bfd_link_hash_traverse(struct bfd_link_hash_table * table,bfd_boolean (* func)(struct bfd_link_hash_entry *,void *),void * info)6063d8817e4Smiod bfd_link_hash_traverse
6073d8817e4Smiod   (struct bfd_link_hash_table *table,
6083d8817e4Smiod    bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
6093d8817e4Smiod    void *info)
6103d8817e4Smiod {
6113d8817e4Smiod   bfd_hash_traverse (&table->table,
6123d8817e4Smiod 		     (bfd_boolean (*) (struct bfd_hash_entry *, void *)) func,
6133d8817e4Smiod 		     info);
6143d8817e4Smiod }
6153d8817e4Smiod 
6163d8817e4Smiod /* Add a symbol to the linker hash table undefs list.  */
6173d8817e4Smiod 
6183d8817e4Smiod void
bfd_link_add_undef(struct bfd_link_hash_table * table,struct bfd_link_hash_entry * h)6193d8817e4Smiod bfd_link_add_undef (struct bfd_link_hash_table *table,
6203d8817e4Smiod 		    struct bfd_link_hash_entry *h)
6213d8817e4Smiod {
6223d8817e4Smiod   BFD_ASSERT (h->u.undef.next == NULL);
6233d8817e4Smiod   if (table->undefs_tail != NULL)
6243d8817e4Smiod     table->undefs_tail->u.undef.next = h;
6253d8817e4Smiod   if (table->undefs == NULL)
6263d8817e4Smiod     table->undefs = h;
6273d8817e4Smiod   table->undefs_tail = h;
6283d8817e4Smiod }
6293d8817e4Smiod 
6303d8817e4Smiod /* The undefs list was designed so that in normal use we don't need to
6313d8817e4Smiod    remove entries.  However, if symbols on the list are changed from
6323d8817e4Smiod    bfd_link_hash_undefined to either bfd_link_hash_undefweak or
6333d8817e4Smiod    bfd_link_hash_new for some reason, then they must be removed from the
6343d8817e4Smiod    list.  Failure to do so might result in the linker attempting to add
6353d8817e4Smiod    the symbol to the list again at a later stage.  */
6363d8817e4Smiod 
6373d8817e4Smiod void
bfd_link_repair_undef_list(struct bfd_link_hash_table * table)6383d8817e4Smiod bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
6393d8817e4Smiod {
6403d8817e4Smiod   struct bfd_link_hash_entry **pun;
6413d8817e4Smiod 
6423d8817e4Smiod   pun = &table->undefs;
6433d8817e4Smiod   while (*pun != NULL)
6443d8817e4Smiod     {
6453d8817e4Smiod       struct bfd_link_hash_entry *h = *pun;
6463d8817e4Smiod 
6473d8817e4Smiod       if (h->type == bfd_link_hash_new
6483d8817e4Smiod 	  || h->type == bfd_link_hash_undefweak)
6493d8817e4Smiod 	{
6503d8817e4Smiod 	  *pun = h->u.undef.next;
6513d8817e4Smiod 	  h->u.undef.next = NULL;
6523d8817e4Smiod 	  if (h == table->undefs_tail)
6533d8817e4Smiod 	    {
6543d8817e4Smiod 	      if (pun == &table->undefs)
6553d8817e4Smiod 		table->undefs_tail = NULL;
6563d8817e4Smiod 	      else
6573d8817e4Smiod 		/* pun points at an u.undef.next field.  Go back to
6583d8817e4Smiod 		   the start of the link_hash_entry.  */
6593d8817e4Smiod 		table->undefs_tail = (struct bfd_link_hash_entry *)
6603d8817e4Smiod 		  ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
6613d8817e4Smiod 	      break;
6623d8817e4Smiod 	    }
6633d8817e4Smiod 	}
6643d8817e4Smiod       else
6653d8817e4Smiod 	pun = &h->u.undef.next;
6663d8817e4Smiod     }
6673d8817e4Smiod }
6683d8817e4Smiod 
6693d8817e4Smiod /* Routine to create an entry in a generic link hash table.  */
6703d8817e4Smiod 
6713d8817e4Smiod struct bfd_hash_entry *
_bfd_generic_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)6723d8817e4Smiod _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
6733d8817e4Smiod 				struct bfd_hash_table *table,
6743d8817e4Smiod 				const char *string)
6753d8817e4Smiod {
6763d8817e4Smiod   /* Allocate the structure if it has not already been allocated by a
6773d8817e4Smiod      subclass.  */
6783d8817e4Smiod   if (entry == NULL)
6793d8817e4Smiod     {
6803d8817e4Smiod       entry =
6813d8817e4Smiod 	bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
6823d8817e4Smiod       if (entry == NULL)
6833d8817e4Smiod 	return entry;
6843d8817e4Smiod     }
6853d8817e4Smiod 
6863d8817e4Smiod   /* Call the allocation method of the superclass.  */
6873d8817e4Smiod   entry = _bfd_link_hash_newfunc (entry, table, string);
6883d8817e4Smiod   if (entry)
6893d8817e4Smiod     {
6903d8817e4Smiod       struct generic_link_hash_entry *ret;
6913d8817e4Smiod 
6923d8817e4Smiod       /* Set local fields.  */
6933d8817e4Smiod       ret = (struct generic_link_hash_entry *) entry;
6943d8817e4Smiod       ret->written = FALSE;
6953d8817e4Smiod       ret->sym = NULL;
6963d8817e4Smiod     }
6973d8817e4Smiod 
6983d8817e4Smiod   return entry;
6993d8817e4Smiod }
7003d8817e4Smiod 
7013d8817e4Smiod /* Create a generic link hash table.  */
7023d8817e4Smiod 
7033d8817e4Smiod struct bfd_link_hash_table *
_bfd_generic_link_hash_table_create(bfd * abfd)7043d8817e4Smiod _bfd_generic_link_hash_table_create (bfd *abfd)
7053d8817e4Smiod {
7063d8817e4Smiod   struct generic_link_hash_table *ret;
7073d8817e4Smiod   bfd_size_type amt = sizeof (struct generic_link_hash_table);
7083d8817e4Smiod 
7093d8817e4Smiod   ret = bfd_malloc (amt);
7103d8817e4Smiod   if (ret == NULL)
7113d8817e4Smiod     return NULL;
7123d8817e4Smiod   if (! _bfd_link_hash_table_init (&ret->root, abfd,
7133d8817e4Smiod 				   _bfd_generic_link_hash_newfunc,
7143d8817e4Smiod 				   sizeof (struct generic_link_hash_entry)))
7153d8817e4Smiod     {
7163d8817e4Smiod       free (ret);
7173d8817e4Smiod       return NULL;
7183d8817e4Smiod     }
7193d8817e4Smiod   return &ret->root;
7203d8817e4Smiod }
7213d8817e4Smiod 
7223d8817e4Smiod void
_bfd_generic_link_hash_table_free(struct bfd_link_hash_table * hash)7233d8817e4Smiod _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
7243d8817e4Smiod {
7253d8817e4Smiod   struct generic_link_hash_table *ret
7263d8817e4Smiod     = (struct generic_link_hash_table *) hash;
7273d8817e4Smiod 
7283d8817e4Smiod   bfd_hash_table_free (&ret->root.table);
7293d8817e4Smiod   free (ret);
7303d8817e4Smiod }
7313d8817e4Smiod 
7323d8817e4Smiod /* Grab the symbols for an object file when doing a generic link.  We
7333d8817e4Smiod    store the symbols in the outsymbols field.  We need to keep them
7343d8817e4Smiod    around for the entire link to ensure that we only read them once.
7353d8817e4Smiod    If we read them multiple times, we might wind up with relocs and
7363d8817e4Smiod    the hash table pointing to different instances of the symbol
7373d8817e4Smiod    structure.  */
7383d8817e4Smiod 
7393d8817e4Smiod static bfd_boolean
generic_link_read_symbols(bfd * abfd)7403d8817e4Smiod generic_link_read_symbols (bfd *abfd)
7413d8817e4Smiod {
7423d8817e4Smiod   if (bfd_get_outsymbols (abfd) == NULL)
7433d8817e4Smiod     {
7443d8817e4Smiod       long symsize;
7453d8817e4Smiod       long symcount;
7463d8817e4Smiod 
7473d8817e4Smiod       symsize = bfd_get_symtab_upper_bound (abfd);
7483d8817e4Smiod       if (symsize < 0)
7493d8817e4Smiod 	return FALSE;
7503d8817e4Smiod       bfd_get_outsymbols (abfd) = bfd_alloc (abfd, symsize);
7513d8817e4Smiod       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
7523d8817e4Smiod 	return FALSE;
7533d8817e4Smiod       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
7543d8817e4Smiod       if (symcount < 0)
7553d8817e4Smiod 	return FALSE;
7563d8817e4Smiod       bfd_get_symcount (abfd) = symcount;
7573d8817e4Smiod     }
7583d8817e4Smiod 
7593d8817e4Smiod   return TRUE;
7603d8817e4Smiod }
7613d8817e4Smiod 
7623d8817e4Smiod /* Generic function to add symbols to from an object file to the
7633d8817e4Smiod    global hash table.  This version does not automatically collect
7643d8817e4Smiod    constructors by name.  */
7653d8817e4Smiod 
7663d8817e4Smiod bfd_boolean
_bfd_generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info)7673d8817e4Smiod _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
7683d8817e4Smiod {
7693d8817e4Smiod   return generic_link_add_symbols (abfd, info, FALSE);
7703d8817e4Smiod }
7713d8817e4Smiod 
7723d8817e4Smiod /* Generic function to add symbols from an object file to the global
7733d8817e4Smiod    hash table.  This version automatically collects constructors by
7743d8817e4Smiod    name, as the collect2 program does.  It should be used for any
7753d8817e4Smiod    target which does not provide some other mechanism for setting up
7763d8817e4Smiod    constructors and destructors; these are approximately those targets
7773d8817e4Smiod    for which gcc uses collect2 and do not support stabs.  */
7783d8817e4Smiod 
7793d8817e4Smiod bfd_boolean
_bfd_generic_link_add_symbols_collect(bfd * abfd,struct bfd_link_info * info)7803d8817e4Smiod _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
7813d8817e4Smiod {
7823d8817e4Smiod   return generic_link_add_symbols (abfd, info, TRUE);
7833d8817e4Smiod }
7843d8817e4Smiod 
7853d8817e4Smiod /* Indicate that we are only retrieving symbol values from this
7863d8817e4Smiod    section.  We want the symbols to act as though the values in the
7873d8817e4Smiod    file are absolute.  */
7883d8817e4Smiod 
7893d8817e4Smiod void
_bfd_generic_link_just_syms(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED)7903d8817e4Smiod _bfd_generic_link_just_syms (asection *sec,
7913d8817e4Smiod 			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
7923d8817e4Smiod {
7933d8817e4Smiod   sec->output_section = bfd_abs_section_ptr;
7943d8817e4Smiod   sec->output_offset = sec->vma;
7953d8817e4Smiod }
7963d8817e4Smiod 
7973d8817e4Smiod /* Add symbols from an object file to the global hash table.  */
7983d8817e4Smiod 
7993d8817e4Smiod static bfd_boolean
generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)8003d8817e4Smiod generic_link_add_symbols (bfd *abfd,
8013d8817e4Smiod 			  struct bfd_link_info *info,
8023d8817e4Smiod 			  bfd_boolean collect)
8033d8817e4Smiod {
8043d8817e4Smiod   bfd_boolean ret;
8053d8817e4Smiod 
8063d8817e4Smiod   switch (bfd_get_format (abfd))
8073d8817e4Smiod     {
8083d8817e4Smiod     case bfd_object:
8093d8817e4Smiod       ret = generic_link_add_object_symbols (abfd, info, collect);
8103d8817e4Smiod       break;
8113d8817e4Smiod     case bfd_archive:
8123d8817e4Smiod       ret = (_bfd_generic_link_add_archive_symbols
8133d8817e4Smiod 	     (abfd, info,
8143d8817e4Smiod 	      (collect
8153d8817e4Smiod 	       ? generic_link_check_archive_element_collect
8163d8817e4Smiod 	       : generic_link_check_archive_element_no_collect)));
8173d8817e4Smiod       break;
8183d8817e4Smiod     default:
8193d8817e4Smiod       bfd_set_error (bfd_error_wrong_format);
8203d8817e4Smiod       ret = FALSE;
8213d8817e4Smiod     }
8223d8817e4Smiod 
8233d8817e4Smiod   return ret;
8243d8817e4Smiod }
8253d8817e4Smiod 
8263d8817e4Smiod /* Add symbols from an object file to the global hash table.  */
8273d8817e4Smiod 
8283d8817e4Smiod static bfd_boolean
generic_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)8293d8817e4Smiod generic_link_add_object_symbols (bfd *abfd,
8303d8817e4Smiod 				 struct bfd_link_info *info,
8313d8817e4Smiod 				 bfd_boolean collect)
8323d8817e4Smiod {
8333d8817e4Smiod   bfd_size_type symcount;
8343d8817e4Smiod   struct bfd_symbol **outsyms;
8353d8817e4Smiod 
8363d8817e4Smiod   if (! generic_link_read_symbols (abfd))
8373d8817e4Smiod     return FALSE;
8383d8817e4Smiod   symcount = _bfd_generic_link_get_symcount (abfd);
8393d8817e4Smiod   outsyms = _bfd_generic_link_get_symbols (abfd);
8403d8817e4Smiod   return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
8413d8817e4Smiod }
8423d8817e4Smiod 
8433d8817e4Smiod /* We build a hash table of all symbols defined in an archive.  */
8443d8817e4Smiod 
8453d8817e4Smiod /* An archive symbol may be defined by multiple archive elements.
8463d8817e4Smiod    This linked list is used to hold the elements.  */
8473d8817e4Smiod 
8483d8817e4Smiod struct archive_list
8493d8817e4Smiod {
8503d8817e4Smiod   struct archive_list *next;
8513d8817e4Smiod   unsigned int indx;
8523d8817e4Smiod };
8533d8817e4Smiod 
8543d8817e4Smiod /* An entry in an archive hash table.  */
8553d8817e4Smiod 
8563d8817e4Smiod struct archive_hash_entry
8573d8817e4Smiod {
8583d8817e4Smiod   struct bfd_hash_entry root;
8593d8817e4Smiod   /* Where the symbol is defined.  */
8603d8817e4Smiod   struct archive_list *defs;
8613d8817e4Smiod };
8623d8817e4Smiod 
8633d8817e4Smiod /* An archive hash table itself.  */
8643d8817e4Smiod 
8653d8817e4Smiod struct archive_hash_table
8663d8817e4Smiod {
8673d8817e4Smiod   struct bfd_hash_table table;
8683d8817e4Smiod };
8693d8817e4Smiod 
8703d8817e4Smiod /* Create a new entry for an archive hash table.  */
8713d8817e4Smiod 
8723d8817e4Smiod static struct bfd_hash_entry *
archive_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)8733d8817e4Smiod archive_hash_newfunc (struct bfd_hash_entry *entry,
8743d8817e4Smiod 		      struct bfd_hash_table *table,
8753d8817e4Smiod 		      const char *string)
8763d8817e4Smiod {
8773d8817e4Smiod   struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
8783d8817e4Smiod 
8793d8817e4Smiod   /* Allocate the structure if it has not already been allocated by a
8803d8817e4Smiod      subclass.  */
8813d8817e4Smiod   if (ret == NULL)
8823d8817e4Smiod     ret = bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
8833d8817e4Smiod   if (ret == NULL)
8843d8817e4Smiod     return NULL;
8853d8817e4Smiod 
8863d8817e4Smiod   /* Call the allocation method of the superclass.  */
8873d8817e4Smiod   ret = ((struct archive_hash_entry *)
8883d8817e4Smiod 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
8893d8817e4Smiod 
8903d8817e4Smiod   if (ret)
8913d8817e4Smiod     {
8923d8817e4Smiod       /* Initialize the local fields.  */
8933d8817e4Smiod       ret->defs = NULL;
8943d8817e4Smiod     }
8953d8817e4Smiod 
8963d8817e4Smiod   return &ret->root;
8973d8817e4Smiod }
8983d8817e4Smiod 
8993d8817e4Smiod /* Initialize an archive hash table.  */
9003d8817e4Smiod 
9013d8817e4Smiod static bfd_boolean
archive_hash_table_init(struct archive_hash_table * table,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize)9023d8817e4Smiod archive_hash_table_init
9033d8817e4Smiod   (struct archive_hash_table *table,
9043d8817e4Smiod    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
9053d8817e4Smiod 				      struct bfd_hash_table *,
9063d8817e4Smiod 				      const char *),
9073d8817e4Smiod    unsigned int entsize)
9083d8817e4Smiod {
9093d8817e4Smiod   return bfd_hash_table_init (&table->table, newfunc, entsize);
9103d8817e4Smiod }
9113d8817e4Smiod 
9123d8817e4Smiod /* Look up an entry in an archive hash table.  */
9133d8817e4Smiod 
9143d8817e4Smiod #define archive_hash_lookup(t, string, create, copy) \
9153d8817e4Smiod   ((struct archive_hash_entry *) \
9163d8817e4Smiod    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
9173d8817e4Smiod 
9183d8817e4Smiod /* Allocate space in an archive hash table.  */
9193d8817e4Smiod 
9203d8817e4Smiod #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
9213d8817e4Smiod 
9223d8817e4Smiod /* Free an archive hash table.  */
9233d8817e4Smiod 
9243d8817e4Smiod #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
9253d8817e4Smiod 
9263d8817e4Smiod /* Generic function to add symbols from an archive file to the global
9273d8817e4Smiod    hash file.  This function presumes that the archive symbol table
9283d8817e4Smiod    has already been read in (this is normally done by the
9293d8817e4Smiod    bfd_check_format entry point).  It looks through the undefined and
9303d8817e4Smiod    common symbols and searches the archive symbol table for them.  If
9313d8817e4Smiod    it finds an entry, it includes the associated object file in the
9323d8817e4Smiod    link.
9333d8817e4Smiod 
9343d8817e4Smiod    The old linker looked through the archive symbol table for
9353d8817e4Smiod    undefined symbols.  We do it the other way around, looking through
9363d8817e4Smiod    undefined symbols for symbols defined in the archive.  The
9373d8817e4Smiod    advantage of the newer scheme is that we only have to look through
9383d8817e4Smiod    the list of undefined symbols once, whereas the old method had to
9393d8817e4Smiod    re-search the symbol table each time a new object file was added.
9403d8817e4Smiod 
9413d8817e4Smiod    The CHECKFN argument is used to see if an object file should be
9423d8817e4Smiod    included.  CHECKFN should set *PNEEDED to TRUE if the object file
9433d8817e4Smiod    should be included, and must also call the bfd_link_info
9443d8817e4Smiod    add_archive_element callback function and handle adding the symbols
9453d8817e4Smiod    to the global hash table.  CHECKFN should only return FALSE if some
9463d8817e4Smiod    sort of error occurs.
9473d8817e4Smiod 
9483d8817e4Smiod    For some formats, such as a.out, it is possible to look through an
9493d8817e4Smiod    object file but not actually include it in the link.  The
9503d8817e4Smiod    archive_pass field in a BFD is used to avoid checking the symbols
9513d8817e4Smiod    of an object files too many times.  When an object is included in
9523d8817e4Smiod    the link, archive_pass is set to -1.  If an object is scanned but
9533d8817e4Smiod    not included, archive_pass is set to the pass number.  The pass
9543d8817e4Smiod    number is incremented each time a new object file is included.  The
9553d8817e4Smiod    pass number is used because when a new object file is included it
9563d8817e4Smiod    may create new undefined symbols which cause a previously examined
9573d8817e4Smiod    object file to be included.  */
9583d8817e4Smiod 
9593d8817e4Smiod 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 *))9603d8817e4Smiod _bfd_generic_link_add_archive_symbols
9613d8817e4Smiod   (bfd *abfd,
9623d8817e4Smiod    struct bfd_link_info *info,
9633d8817e4Smiod    bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
9643d8817e4Smiod {
9653d8817e4Smiod   carsym *arsyms;
9663d8817e4Smiod   carsym *arsym_end;
9673d8817e4Smiod   register carsym *arsym;
9683d8817e4Smiod   int pass;
9693d8817e4Smiod   struct archive_hash_table arsym_hash;
9703d8817e4Smiod   unsigned int indx;
9713d8817e4Smiod   struct bfd_link_hash_entry **pundef;
9723d8817e4Smiod 
9733d8817e4Smiod   if (! bfd_has_map (abfd))
9743d8817e4Smiod     {
9753d8817e4Smiod       /* An empty archive is a special case.  */
9763d8817e4Smiod       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
9773d8817e4Smiod 	return TRUE;
9783d8817e4Smiod       bfd_set_error (bfd_error_no_armap);
9793d8817e4Smiod       return FALSE;
9803d8817e4Smiod     }
9813d8817e4Smiod 
9823d8817e4Smiod   arsyms = bfd_ardata (abfd)->symdefs;
9833d8817e4Smiod   arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
9843d8817e4Smiod 
9853d8817e4Smiod   /* In order to quickly determine whether an symbol is defined in
9863d8817e4Smiod      this archive, we build a hash table of the symbols.  */
9873d8817e4Smiod   if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
9883d8817e4Smiod 				 sizeof (struct archive_hash_entry)))
9893d8817e4Smiod     return FALSE;
9903d8817e4Smiod   for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
9913d8817e4Smiod     {
9923d8817e4Smiod       struct archive_hash_entry *arh;
9933d8817e4Smiod       struct archive_list *l, **pp;
9943d8817e4Smiod 
9953d8817e4Smiod       arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
9963d8817e4Smiod       if (arh == NULL)
9973d8817e4Smiod 	goto error_return;
9983d8817e4Smiod       l = ((struct archive_list *)
9993d8817e4Smiod 	   archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
10003d8817e4Smiod       if (l == NULL)
10013d8817e4Smiod 	goto error_return;
10023d8817e4Smiod       l->indx = indx;
10033d8817e4Smiod       for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
10043d8817e4Smiod 	;
10053d8817e4Smiod       *pp = l;
10063d8817e4Smiod       l->next = NULL;
10073d8817e4Smiod     }
10083d8817e4Smiod 
10093d8817e4Smiod   /* The archive_pass field in the archive itself is used to
10103d8817e4Smiod      initialize PASS, sine we may search the same archive multiple
10113d8817e4Smiod      times.  */
10123d8817e4Smiod   pass = abfd->archive_pass + 1;
10133d8817e4Smiod 
10143d8817e4Smiod   /* New undefined symbols are added to the end of the list, so we
10153d8817e4Smiod      only need to look through it once.  */
10163d8817e4Smiod   pundef = &info->hash->undefs;
10173d8817e4Smiod   while (*pundef != NULL)
10183d8817e4Smiod     {
10193d8817e4Smiod       struct bfd_link_hash_entry *h;
10203d8817e4Smiod       struct archive_hash_entry *arh;
10213d8817e4Smiod       struct archive_list *l;
10223d8817e4Smiod 
10233d8817e4Smiod       h = *pundef;
10243d8817e4Smiod 
10253d8817e4Smiod       /* When a symbol is defined, it is not necessarily removed from
10263d8817e4Smiod 	 the list.  */
10273d8817e4Smiod       if (h->type != bfd_link_hash_undefined
10283d8817e4Smiod 	  && h->type != bfd_link_hash_common)
10293d8817e4Smiod 	{
10303d8817e4Smiod 	  /* Remove this entry from the list, for general cleanliness
10313d8817e4Smiod 	     and because we are going to look through the list again
10323d8817e4Smiod 	     if we search any more libraries.  We can't remove the
10333d8817e4Smiod 	     entry if it is the tail, because that would lose any
10343d8817e4Smiod 	     entries we add to the list later on (it would also cause
10353d8817e4Smiod 	     us to lose track of whether the symbol has been
10363d8817e4Smiod 	     referenced).  */
10373d8817e4Smiod 	  if (*pundef != info->hash->undefs_tail)
10383d8817e4Smiod 	    *pundef = (*pundef)->u.undef.next;
10393d8817e4Smiod 	  else
10403d8817e4Smiod 	    pundef = &(*pundef)->u.undef.next;
10413d8817e4Smiod 	  continue;
10423d8817e4Smiod 	}
10433d8817e4Smiod 
10443d8817e4Smiod       /* Look for this symbol in the archive symbol map.  */
10453d8817e4Smiod       arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
10463d8817e4Smiod       if (arh == NULL)
10473d8817e4Smiod 	{
10483d8817e4Smiod 	  /* If we haven't found the exact symbol we're looking for,
10493d8817e4Smiod 	     let's look for its import thunk */
10503d8817e4Smiod 	  if (info->pei386_auto_import)
10513d8817e4Smiod 	    {
10523d8817e4Smiod 	      bfd_size_type amt = strlen (h->root.string) + 10;
10533d8817e4Smiod 	      char *buf = bfd_malloc (amt);
10543d8817e4Smiod 	      if (buf == NULL)
10553d8817e4Smiod 		return FALSE;
10563d8817e4Smiod 
10573d8817e4Smiod 	      sprintf (buf, "__imp_%s", h->root.string);
10583d8817e4Smiod 	      arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
10593d8817e4Smiod 	      free(buf);
10603d8817e4Smiod 	    }
10613d8817e4Smiod 	  if (arh == NULL)
10623d8817e4Smiod 	    {
10633d8817e4Smiod 	      pundef = &(*pundef)->u.undef.next;
10643d8817e4Smiod 	      continue;
10653d8817e4Smiod 	    }
10663d8817e4Smiod 	}
10673d8817e4Smiod       /* Look at all the objects which define this symbol.  */
10683d8817e4Smiod       for (l = arh->defs; l != NULL; l = l->next)
10693d8817e4Smiod 	{
10703d8817e4Smiod 	  bfd *element;
10713d8817e4Smiod 	  bfd_boolean needed;
10723d8817e4Smiod 
10733d8817e4Smiod 	  /* If the symbol has gotten defined along the way, quit.  */
10743d8817e4Smiod 	  if (h->type != bfd_link_hash_undefined
10753d8817e4Smiod 	      && h->type != bfd_link_hash_common)
10763d8817e4Smiod 	    break;
10773d8817e4Smiod 
10783d8817e4Smiod 	  element = bfd_get_elt_at_index (abfd, l->indx);
10793d8817e4Smiod 	  if (element == NULL)
10803d8817e4Smiod 	    goto error_return;
10813d8817e4Smiod 
10823d8817e4Smiod 	  /* If we've already included this element, or if we've
10833d8817e4Smiod 	     already checked it on this pass, continue.  */
10843d8817e4Smiod 	  if (element->archive_pass == -1
10853d8817e4Smiod 	      || element->archive_pass == pass)
10863d8817e4Smiod 	    continue;
10873d8817e4Smiod 
10883d8817e4Smiod 	  /* If we can't figure this element out, just ignore it.  */
10893d8817e4Smiod 	  if (! bfd_check_format (element, bfd_object))
10903d8817e4Smiod 	    {
10913d8817e4Smiod 	      element->archive_pass = -1;
10923d8817e4Smiod 	      continue;
10933d8817e4Smiod 	    }
10943d8817e4Smiod 
10953d8817e4Smiod 	  /* CHECKFN will see if this element should be included, and
10963d8817e4Smiod 	     go ahead and include it if appropriate.  */
10973d8817e4Smiod 	  if (! (*checkfn) (element, info, &needed))
10983d8817e4Smiod 	    goto error_return;
10993d8817e4Smiod 
11003d8817e4Smiod 	  if (! needed)
11013d8817e4Smiod 	    element->archive_pass = pass;
11023d8817e4Smiod 	  else
11033d8817e4Smiod 	    {
11043d8817e4Smiod 	      element->archive_pass = -1;
11053d8817e4Smiod 
11063d8817e4Smiod 	      /* Increment the pass count to show that we may need to
11073d8817e4Smiod 		 recheck object files which were already checked.  */
11083d8817e4Smiod 	      ++pass;
11093d8817e4Smiod 	    }
11103d8817e4Smiod 	}
11113d8817e4Smiod 
11123d8817e4Smiod       pundef = &(*pundef)->u.undef.next;
11133d8817e4Smiod     }
11143d8817e4Smiod 
11153d8817e4Smiod   archive_hash_table_free (&arsym_hash);
11163d8817e4Smiod 
11173d8817e4Smiod   /* Save PASS in case we are called again.  */
11183d8817e4Smiod   abfd->archive_pass = pass;
11193d8817e4Smiod 
11203d8817e4Smiod   return TRUE;
11213d8817e4Smiod 
11223d8817e4Smiod  error_return:
11233d8817e4Smiod   archive_hash_table_free (&arsym_hash);
11243d8817e4Smiod   return FALSE;
11253d8817e4Smiod }
11263d8817e4Smiod 
11273d8817e4Smiod /* See if we should include an archive element.  This version is used
11283d8817e4Smiod    when we do not want to automatically collect constructors based on
11293d8817e4Smiod    the symbol name, presumably because we have some other mechanism
11303d8817e4Smiod    for finding them.  */
11313d8817e4Smiod 
11323d8817e4Smiod static bfd_boolean
generic_link_check_archive_element_no_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)11333d8817e4Smiod generic_link_check_archive_element_no_collect (
11343d8817e4Smiod 					       bfd *abfd,
11353d8817e4Smiod 					       struct bfd_link_info *info,
11363d8817e4Smiod 					       bfd_boolean *pneeded)
11373d8817e4Smiod {
11383d8817e4Smiod   return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
11393d8817e4Smiod }
11403d8817e4Smiod 
11413d8817e4Smiod /* See if we should include an archive element.  This version is used
11423d8817e4Smiod    when we want to automatically collect constructors based on the
11433d8817e4Smiod    symbol name, as collect2 does.  */
11443d8817e4Smiod 
11453d8817e4Smiod static bfd_boolean
generic_link_check_archive_element_collect(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded)11463d8817e4Smiod generic_link_check_archive_element_collect (bfd *abfd,
11473d8817e4Smiod 					    struct bfd_link_info *info,
11483d8817e4Smiod 					    bfd_boolean *pneeded)
11493d8817e4Smiod {
11503d8817e4Smiod   return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
11513d8817e4Smiod }
11523d8817e4Smiod 
11533d8817e4Smiod /* See if we should include an archive element.  Optionally collect
11543d8817e4Smiod    constructors.  */
11553d8817e4Smiod 
11563d8817e4Smiod static bfd_boolean
generic_link_check_archive_element(bfd * abfd,struct bfd_link_info * info,bfd_boolean * pneeded,bfd_boolean collect)11573d8817e4Smiod generic_link_check_archive_element (bfd *abfd,
11583d8817e4Smiod 				    struct bfd_link_info *info,
11593d8817e4Smiod 				    bfd_boolean *pneeded,
11603d8817e4Smiod 				    bfd_boolean collect)
11613d8817e4Smiod {
11623d8817e4Smiod   asymbol **pp, **ppend;
11633d8817e4Smiod 
11643d8817e4Smiod   *pneeded = FALSE;
11653d8817e4Smiod 
11663d8817e4Smiod   if (! generic_link_read_symbols (abfd))
11673d8817e4Smiod     return FALSE;
11683d8817e4Smiod 
11693d8817e4Smiod   pp = _bfd_generic_link_get_symbols (abfd);
11703d8817e4Smiod   ppend = pp + _bfd_generic_link_get_symcount (abfd);
11713d8817e4Smiod   for (; pp < ppend; pp++)
11723d8817e4Smiod     {
11733d8817e4Smiod       asymbol *p;
11743d8817e4Smiod       struct bfd_link_hash_entry *h;
11753d8817e4Smiod 
11763d8817e4Smiod       p = *pp;
11773d8817e4Smiod 
11783d8817e4Smiod       /* We are only interested in globally visible symbols.  */
11793d8817e4Smiod       if (! bfd_is_com_section (p->section)
11803d8817e4Smiod 	  && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
11813d8817e4Smiod 	continue;
11823d8817e4Smiod 
11833d8817e4Smiod       /* We are only interested if we know something about this
11843d8817e4Smiod 	 symbol, and it is undefined or common.  An undefined weak
11853d8817e4Smiod 	 symbol (type bfd_link_hash_undefweak) is not considered to be
11863d8817e4Smiod 	 a reference when pulling files out of an archive.  See the
11873d8817e4Smiod 	 SVR4 ABI, p. 4-27.  */
11883d8817e4Smiod       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
11893d8817e4Smiod 				FALSE, TRUE);
11903d8817e4Smiod       if (h == NULL
11913d8817e4Smiod 	  || (h->type != bfd_link_hash_undefined
11923d8817e4Smiod 	      && h->type != bfd_link_hash_common))
11933d8817e4Smiod 	continue;
11943d8817e4Smiod 
11953d8817e4Smiod       /* P is a symbol we are looking for.  */
11963d8817e4Smiod 
11973d8817e4Smiod       if (! bfd_is_com_section (p->section))
11983d8817e4Smiod 	{
11993d8817e4Smiod 	  bfd_size_type symcount;
12003d8817e4Smiod 	  asymbol **symbols;
12013d8817e4Smiod 
12023d8817e4Smiod 	  /* This object file defines this symbol, so pull it in.  */
12033d8817e4Smiod 	  if (! (*info->callbacks->add_archive_element) (info, abfd,
12043d8817e4Smiod 							 bfd_asymbol_name (p)))
12053d8817e4Smiod 	    return FALSE;
12063d8817e4Smiod 	  symcount = _bfd_generic_link_get_symcount (abfd);
12073d8817e4Smiod 	  symbols = _bfd_generic_link_get_symbols (abfd);
12083d8817e4Smiod 	  if (! generic_link_add_symbol_list (abfd, info, symcount,
12093d8817e4Smiod 					      symbols, collect))
12103d8817e4Smiod 	    return FALSE;
12113d8817e4Smiod 	  *pneeded = TRUE;
12123d8817e4Smiod 	  return TRUE;
12133d8817e4Smiod 	}
12143d8817e4Smiod 
12153d8817e4Smiod       /* P is a common symbol.  */
12163d8817e4Smiod 
12173d8817e4Smiod       if (h->type == bfd_link_hash_undefined)
12183d8817e4Smiod 	{
12193d8817e4Smiod 	  bfd *symbfd;
12203d8817e4Smiod 	  bfd_vma size;
12213d8817e4Smiod 	  unsigned int power;
12223d8817e4Smiod 
12233d8817e4Smiod 	  symbfd = h->u.undef.abfd;
12243d8817e4Smiod 	  if (symbfd == NULL)
12253d8817e4Smiod 	    {
12263d8817e4Smiod 	      /* This symbol was created as undefined from outside
12273d8817e4Smiod 		 BFD.  We assume that we should link in the object
12283d8817e4Smiod 		 file.  This is for the -u option in the linker.  */
12293d8817e4Smiod 	      if (! (*info->callbacks->add_archive_element)
12303d8817e4Smiod 		  (info, abfd, bfd_asymbol_name (p)))
12313d8817e4Smiod 		return FALSE;
12323d8817e4Smiod 	      *pneeded = TRUE;
12333d8817e4Smiod 	      return TRUE;
12343d8817e4Smiod 	    }
12353d8817e4Smiod 
12363d8817e4Smiod 	  /* Turn the symbol into a common symbol but do not link in
12373d8817e4Smiod 	     the object file.  This is how a.out works.  Object
12383d8817e4Smiod 	     formats that require different semantics must implement
12393d8817e4Smiod 	     this function differently.  This symbol is already on the
12403d8817e4Smiod 	     undefs list.  We add the section to a common section
12413d8817e4Smiod 	     attached to symbfd to ensure that it is in a BFD which
12423d8817e4Smiod 	     will be linked in.  */
12433d8817e4Smiod 	  h->type = bfd_link_hash_common;
12443d8817e4Smiod 	  h->u.c.p =
12453d8817e4Smiod 	    bfd_hash_allocate (&info->hash->table,
12463d8817e4Smiod 			       sizeof (struct bfd_link_hash_common_entry));
12473d8817e4Smiod 	  if (h->u.c.p == NULL)
12483d8817e4Smiod 	    return FALSE;
12493d8817e4Smiod 
12503d8817e4Smiod 	  size = bfd_asymbol_value (p);
12513d8817e4Smiod 	  h->u.c.size = size;
12523d8817e4Smiod 
12533d8817e4Smiod 	  power = bfd_log2 (size);
12543d8817e4Smiod 	  if (power > 4)
12553d8817e4Smiod 	    power = 4;
12563d8817e4Smiod 	  h->u.c.p->alignment_power = power;
12573d8817e4Smiod 
12583d8817e4Smiod 	  if (p->section == bfd_com_section_ptr)
12593d8817e4Smiod 	    h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
12603d8817e4Smiod 	  else
12613d8817e4Smiod 	    h->u.c.p->section = bfd_make_section_old_way (symbfd,
12623d8817e4Smiod 							  p->section->name);
12633d8817e4Smiod 	  h->u.c.p->section->flags = SEC_ALLOC;
12643d8817e4Smiod 	}
12653d8817e4Smiod       else
12663d8817e4Smiod 	{
12673d8817e4Smiod 	  /* Adjust the size of the common symbol if necessary.  This
12683d8817e4Smiod 	     is how a.out works.  Object formats that require
12693d8817e4Smiod 	     different semantics must implement this function
12703d8817e4Smiod 	     differently.  */
12713d8817e4Smiod 	  if (bfd_asymbol_value (p) > h->u.c.size)
12723d8817e4Smiod 	    h->u.c.size = bfd_asymbol_value (p);
12733d8817e4Smiod 	}
12743d8817e4Smiod     }
12753d8817e4Smiod 
12763d8817e4Smiod   /* This archive element is not needed.  */
12773d8817e4Smiod   return TRUE;
12783d8817e4Smiod }
12793d8817e4Smiod 
12803d8817e4Smiod /* Add the symbols from an object file to the global hash table.  ABFD
12813d8817e4Smiod    is the object file.  INFO is the linker information.  SYMBOL_COUNT
12823d8817e4Smiod    is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
12833d8817e4Smiod    is TRUE if constructors should be automatically collected by name
12843d8817e4Smiod    as is done by collect2.  */
12853d8817e4Smiod 
12863d8817e4Smiod 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)12873d8817e4Smiod generic_link_add_symbol_list (bfd *abfd,
12883d8817e4Smiod 			      struct bfd_link_info *info,
12893d8817e4Smiod 			      bfd_size_type symbol_count,
12903d8817e4Smiod 			      asymbol **symbols,
12913d8817e4Smiod 			      bfd_boolean collect)
12923d8817e4Smiod {
12933d8817e4Smiod   asymbol **pp, **ppend;
12943d8817e4Smiod 
12953d8817e4Smiod   pp = symbols;
12963d8817e4Smiod   ppend = symbols + symbol_count;
12973d8817e4Smiod   for (; pp < ppend; pp++)
12983d8817e4Smiod     {
12993d8817e4Smiod       asymbol *p;
13003d8817e4Smiod 
13013d8817e4Smiod       p = *pp;
13023d8817e4Smiod 
13033d8817e4Smiod       if ((p->flags & (BSF_INDIRECT
13043d8817e4Smiod 		       | BSF_WARNING
13053d8817e4Smiod 		       | BSF_GLOBAL
13063d8817e4Smiod 		       | BSF_CONSTRUCTOR
13073d8817e4Smiod 		       | BSF_WEAK)) != 0
13083d8817e4Smiod 	  || bfd_is_und_section (bfd_get_section (p))
13093d8817e4Smiod 	  || bfd_is_com_section (bfd_get_section (p))
13103d8817e4Smiod 	  || bfd_is_ind_section (bfd_get_section (p)))
13113d8817e4Smiod 	{
13123d8817e4Smiod 	  const char *name;
13133d8817e4Smiod 	  const char *string;
13143d8817e4Smiod 	  struct generic_link_hash_entry *h;
13153d8817e4Smiod 	  struct bfd_link_hash_entry *bh;
13163d8817e4Smiod 
13173d8817e4Smiod 	  name = bfd_asymbol_name (p);
13183d8817e4Smiod 	  if (((p->flags & BSF_INDIRECT) != 0
13193d8817e4Smiod 	       || bfd_is_ind_section (p->section))
13203d8817e4Smiod 	      && pp + 1 < ppend)
13213d8817e4Smiod 	    {
13223d8817e4Smiod 	      pp++;
13233d8817e4Smiod 	      string = bfd_asymbol_name (*pp);
13243d8817e4Smiod 	    }
13253d8817e4Smiod 	  else if ((p->flags & BSF_WARNING) != 0
13263d8817e4Smiod 		   && pp + 1 < ppend)
13273d8817e4Smiod 	    {
13283d8817e4Smiod 	      /* The name of P is actually the warning string, and the
13293d8817e4Smiod 		 next symbol is the one to warn about.  */
13303d8817e4Smiod 	      string = name;
13313d8817e4Smiod 	      pp++;
13323d8817e4Smiod 	      name = bfd_asymbol_name (*pp);
13333d8817e4Smiod 	    }
13343d8817e4Smiod 	  else
13353d8817e4Smiod 	    string = NULL;
13363d8817e4Smiod 
13373d8817e4Smiod 	  bh = NULL;
13383d8817e4Smiod 	  if (! (_bfd_generic_link_add_one_symbol
13393d8817e4Smiod 		 (info, abfd, name, p->flags, bfd_get_section (p),
13403d8817e4Smiod 		  p->value, string, FALSE, collect, &bh)))
13413d8817e4Smiod 	    return FALSE;
13423d8817e4Smiod 	  h = (struct generic_link_hash_entry *) bh;
13433d8817e4Smiod 
13443d8817e4Smiod 	  /* If this is a constructor symbol, and the linker didn't do
13453d8817e4Smiod              anything with it, then we want to just pass the symbol
13463d8817e4Smiod              through to the output file.  This will happen when
13473d8817e4Smiod              linking with -r.  */
13483d8817e4Smiod 	  if ((p->flags & BSF_CONSTRUCTOR) != 0
13493d8817e4Smiod 	      && (h == NULL || h->root.type == bfd_link_hash_new))
13503d8817e4Smiod 	    {
13513d8817e4Smiod 	      p->udata.p = NULL;
13523d8817e4Smiod 	      continue;
13533d8817e4Smiod 	    }
13543d8817e4Smiod 
13553d8817e4Smiod 	  /* Save the BFD symbol so that we don't lose any backend
13563d8817e4Smiod 	     specific information that may be attached to it.  We only
13573d8817e4Smiod 	     want this one if it gives more information than the
13583d8817e4Smiod 	     existing one; we don't want to replace a defined symbol
13593d8817e4Smiod 	     with an undefined one.  This routine may be called with a
13603d8817e4Smiod 	     hash table other than the generic hash table, so we only
13613d8817e4Smiod 	     do this if we are certain that the hash table is a
13623d8817e4Smiod 	     generic one.  */
13633d8817e4Smiod 	  if (info->hash->creator == abfd->xvec)
13643d8817e4Smiod 	    {
13653d8817e4Smiod 	      if (h->sym == NULL
13663d8817e4Smiod 		  || (! bfd_is_und_section (bfd_get_section (p))
13673d8817e4Smiod 		      && (! bfd_is_com_section (bfd_get_section (p))
13683d8817e4Smiod 			  || bfd_is_und_section (bfd_get_section (h->sym)))))
13693d8817e4Smiod 		{
13703d8817e4Smiod 		  h->sym = p;
13713d8817e4Smiod 		  /* BSF_OLD_COMMON is a hack to support COFF reloc
13723d8817e4Smiod 		     reading, and it should go away when the COFF
13733d8817e4Smiod 		     linker is switched to the new version.  */
13743d8817e4Smiod 		  if (bfd_is_com_section (bfd_get_section (p)))
13753d8817e4Smiod 		    p->flags |= BSF_OLD_COMMON;
13763d8817e4Smiod 		}
13773d8817e4Smiod 	    }
13783d8817e4Smiod 
13793d8817e4Smiod 	  /* Store a back pointer from the symbol to the hash
13803d8817e4Smiod 	     table entry for the benefit of relaxation code until
13813d8817e4Smiod 	     it gets rewritten to not use asymbol structures.
13823d8817e4Smiod 	     Setting this is also used to check whether these
13833d8817e4Smiod 	     symbols were set up by the generic linker.  */
13843d8817e4Smiod 	  p->udata.p = h;
13853d8817e4Smiod 	}
13863d8817e4Smiod     }
13873d8817e4Smiod 
13883d8817e4Smiod   return TRUE;
13893d8817e4Smiod }
13903d8817e4Smiod 
13913d8817e4Smiod /* We use a state table to deal with adding symbols from an object
13923d8817e4Smiod    file.  The first index into the state table describes the symbol
13933d8817e4Smiod    from the object file.  The second index into the state table is the
13943d8817e4Smiod    type of the symbol in the hash table.  */
13953d8817e4Smiod 
13963d8817e4Smiod /* The symbol from the object file is turned into one of these row
13973d8817e4Smiod    values.  */
13983d8817e4Smiod 
13993d8817e4Smiod enum link_row
14003d8817e4Smiod {
14013d8817e4Smiod   UNDEF_ROW,		/* Undefined.  */
14023d8817e4Smiod   UNDEFW_ROW,		/* Weak undefined.  */
14033d8817e4Smiod   DEF_ROW,		/* Defined.  */
14043d8817e4Smiod   DEFW_ROW,		/* Weak defined.  */
14053d8817e4Smiod   COMMON_ROW,		/* Common.  */
14063d8817e4Smiod   INDR_ROW,		/* Indirect.  */
14073d8817e4Smiod   WARN_ROW,		/* Warning.  */
14083d8817e4Smiod   SET_ROW		/* Member of set.  */
14093d8817e4Smiod };
14103d8817e4Smiod 
14113d8817e4Smiod /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
14123d8817e4Smiod #undef FAIL
14133d8817e4Smiod 
14143d8817e4Smiod /* The actions to take in the state table.  */
14153d8817e4Smiod 
14163d8817e4Smiod enum link_action
14173d8817e4Smiod {
14183d8817e4Smiod   FAIL,		/* Abort.  */
14193d8817e4Smiod   UND,		/* Mark symbol undefined.  */
14203d8817e4Smiod   WEAK,		/* Mark symbol weak undefined.  */
14213d8817e4Smiod   DEF,		/* Mark symbol defined.  */
14223d8817e4Smiod   DEFW,		/* Mark symbol weak defined.  */
14233d8817e4Smiod   COM,		/* Mark symbol common.  */
14243d8817e4Smiod   REF,		/* Mark defined symbol referenced.  */
14253d8817e4Smiod   CREF,		/* Possibly warn about common reference to defined symbol.  */
14263d8817e4Smiod   CDEF,		/* Define existing common symbol.  */
14273d8817e4Smiod   NOACT,	/* No action.  */
14283d8817e4Smiod   BIG,		/* Mark symbol common using largest size.  */
14293d8817e4Smiod   MDEF,		/* Multiple definition error.  */
14303d8817e4Smiod   MIND,		/* Multiple indirect symbols.  */
14313d8817e4Smiod   IND,		/* Make indirect symbol.  */
14323d8817e4Smiod   CIND,		/* Make indirect symbol from existing common symbol.  */
14333d8817e4Smiod   SET,		/* Add value to set.  */
14343d8817e4Smiod   MWARN,	/* Make warning symbol.  */
14353d8817e4Smiod   WARN,		/* Issue warning.  */
14363d8817e4Smiod   CWARN,	/* Warn if referenced, else MWARN.  */
14373d8817e4Smiod   CYCLE,	/* Repeat with symbol pointed to.  */
14383d8817e4Smiod   REFC,		/* Mark indirect symbol referenced and then CYCLE.  */
14393d8817e4Smiod   WARNC		/* Issue warning and then CYCLE.  */
14403d8817e4Smiod };
14413d8817e4Smiod 
14423d8817e4Smiod /* The state table itself.  The first index is a link_row and the
14433d8817e4Smiod    second index is a bfd_link_hash_type.  */
14443d8817e4Smiod 
14453d8817e4Smiod static const enum link_action link_action[8][8] =
14463d8817e4Smiod {
14473d8817e4Smiod   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
14483d8817e4Smiod   /* UNDEF_ROW 	*/  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
14493d8817e4Smiod   /* UNDEFW_ROW	*/  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
14503d8817e4Smiod   /* DEF_ROW 	*/  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
14513d8817e4Smiod   /* DEFW_ROW 	*/  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
14523d8817e4Smiod   /* COMMON_ROW	*/  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
14533d8817e4Smiod   /* INDR_ROW	*/  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
14543d8817e4Smiod   /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, NOACT },
14553d8817e4Smiod   /* SET_ROW	*/  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
14563d8817e4Smiod };
14573d8817e4Smiod 
14583d8817e4Smiod /* Most of the entries in the LINK_ACTION table are straightforward,
14593d8817e4Smiod    but a few are somewhat subtle.
14603d8817e4Smiod 
14613d8817e4Smiod    A reference to an indirect symbol (UNDEF_ROW/indr or
14623d8817e4Smiod    UNDEFW_ROW/indr) is counted as a reference both to the indirect
14633d8817e4Smiod    symbol and to the symbol the indirect symbol points to.
14643d8817e4Smiod 
14653d8817e4Smiod    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
14663d8817e4Smiod    causes the warning to be issued.
14673d8817e4Smiod 
14683d8817e4Smiod    A common definition of an indirect symbol (COMMON_ROW/indr) is
14693d8817e4Smiod    treated as a multiple definition error.  Likewise for an indirect
14703d8817e4Smiod    definition of a common symbol (INDR_ROW/com).
14713d8817e4Smiod 
14723d8817e4Smiod    An indirect definition of a warning (INDR_ROW/warn) does not cause
14733d8817e4Smiod    the warning to be issued.
14743d8817e4Smiod 
14753d8817e4Smiod    If a warning is created for an indirect symbol (WARN_ROW/indr) no
14763d8817e4Smiod    warning is created for the symbol the indirect symbol points to.
14773d8817e4Smiod 
14783d8817e4Smiod    Adding an entry to a set does not count as a reference to a set,
14793d8817e4Smiod    and no warning is issued (SET_ROW/warn).  */
14803d8817e4Smiod 
14813d8817e4Smiod /* Return the BFD in which a hash entry has been defined, if known.  */
14823d8817e4Smiod 
14833d8817e4Smiod static bfd *
hash_entry_bfd(struct bfd_link_hash_entry * h)14843d8817e4Smiod hash_entry_bfd (struct bfd_link_hash_entry *h)
14853d8817e4Smiod {
14863d8817e4Smiod   while (h->type == bfd_link_hash_warning)
14873d8817e4Smiod     h = h->u.i.link;
14883d8817e4Smiod   switch (h->type)
14893d8817e4Smiod     {
14903d8817e4Smiod     default:
14913d8817e4Smiod       return NULL;
14923d8817e4Smiod     case bfd_link_hash_undefined:
14933d8817e4Smiod     case bfd_link_hash_undefweak:
14943d8817e4Smiod       return h->u.undef.abfd;
14953d8817e4Smiod     case bfd_link_hash_defined:
14963d8817e4Smiod     case bfd_link_hash_defweak:
14973d8817e4Smiod       return h->u.def.section->owner;
14983d8817e4Smiod     case bfd_link_hash_common:
14993d8817e4Smiod       return h->u.c.p->section->owner;
15003d8817e4Smiod     }
15013d8817e4Smiod   /*NOTREACHED*/
15023d8817e4Smiod }
15033d8817e4Smiod 
15043d8817e4Smiod /* Add a symbol to the global hash table.
15053d8817e4Smiod    ABFD is the BFD the symbol comes from.
15063d8817e4Smiod    NAME is the name of the symbol.
15073d8817e4Smiod    FLAGS is the BSF_* bits associated with the symbol.
15083d8817e4Smiod    SECTION is the section in which the symbol is defined; this may be
15093d8817e4Smiod      bfd_und_section_ptr or bfd_com_section_ptr.
15103d8817e4Smiod    VALUE is the value of the symbol, relative to the section.
15113d8817e4Smiod    STRING is used for either an indirect symbol, in which case it is
15123d8817e4Smiod      the name of the symbol to indirect to, or a warning symbol, in
15133d8817e4Smiod      which case it is the warning string.
15143d8817e4Smiod    COPY is TRUE if NAME or STRING must be copied into locally
15153d8817e4Smiod      allocated memory if they need to be saved.
15163d8817e4Smiod    COLLECT is TRUE if we should automatically collect gcc constructor
15173d8817e4Smiod      or destructor names as collect2 does.
15183d8817e4Smiod    HASHP, if not NULL, is a place to store the created hash table
15193d8817e4Smiod      entry; if *HASHP is not NULL, the caller has already looked up
15203d8817e4Smiod      the hash table entry, and stored it in *HASHP.  */
15213d8817e4Smiod 
15223d8817e4Smiod 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)15233d8817e4Smiod _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
15243d8817e4Smiod 				  bfd *abfd,
15253d8817e4Smiod 				  const char *name,
15263d8817e4Smiod 				  flagword flags,
15273d8817e4Smiod 				  asection *section,
15283d8817e4Smiod 				  bfd_vma value,
15293d8817e4Smiod 				  const char *string,
15303d8817e4Smiod 				  bfd_boolean copy,
15313d8817e4Smiod 				  bfd_boolean collect,
15323d8817e4Smiod 				  struct bfd_link_hash_entry **hashp)
15333d8817e4Smiod {
15343d8817e4Smiod   enum link_row row;
15353d8817e4Smiod   struct bfd_link_hash_entry *h;
15363d8817e4Smiod   bfd_boolean cycle;
15373d8817e4Smiod 
15383d8817e4Smiod   if (bfd_is_ind_section (section)
15393d8817e4Smiod       || (flags & BSF_INDIRECT) != 0)
15403d8817e4Smiod     row = INDR_ROW;
15413d8817e4Smiod   else if ((flags & BSF_WARNING) != 0)
15423d8817e4Smiod     row = WARN_ROW;
15433d8817e4Smiod   else if ((flags & BSF_CONSTRUCTOR) != 0)
15443d8817e4Smiod     row = SET_ROW;
15453d8817e4Smiod   else if (bfd_is_und_section (section))
15463d8817e4Smiod     {
15473d8817e4Smiod       if ((flags & BSF_WEAK) != 0)
15483d8817e4Smiod 	row = UNDEFW_ROW;
15493d8817e4Smiod       else
15503d8817e4Smiod 	row = UNDEF_ROW;
15513d8817e4Smiod     }
15523d8817e4Smiod   else if ((flags & BSF_WEAK) != 0)
15533d8817e4Smiod     row = DEFW_ROW;
15543d8817e4Smiod   else if (bfd_is_com_section (section))
15553d8817e4Smiod     row = COMMON_ROW;
15563d8817e4Smiod   else
15573d8817e4Smiod     row = DEF_ROW;
15583d8817e4Smiod 
15593d8817e4Smiod   if (hashp != NULL && *hashp != NULL)
15603d8817e4Smiod     h = *hashp;
15613d8817e4Smiod   else
15623d8817e4Smiod     {
15633d8817e4Smiod       if (row == UNDEF_ROW || row == UNDEFW_ROW)
15643d8817e4Smiod 	h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
15653d8817e4Smiod       else
15663d8817e4Smiod 	h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
15673d8817e4Smiod       if (h == NULL)
15683d8817e4Smiod 	{
15693d8817e4Smiod 	  if (hashp != NULL)
15703d8817e4Smiod 	    *hashp = NULL;
15713d8817e4Smiod 	  return FALSE;
15723d8817e4Smiod 	}
15733d8817e4Smiod     }
15743d8817e4Smiod 
15753d8817e4Smiod   if (info->notice_all
15763d8817e4Smiod       || (info->notice_hash != NULL
15773d8817e4Smiod 	  && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
15783d8817e4Smiod     {
15793d8817e4Smiod       if (! (*info->callbacks->notice) (info, h->root.string, abfd, section,
15803d8817e4Smiod 					value))
15813d8817e4Smiod 	return FALSE;
15823d8817e4Smiod     }
15833d8817e4Smiod 
15843d8817e4Smiod   if (hashp != NULL)
15853d8817e4Smiod     *hashp = h;
15863d8817e4Smiod 
15873d8817e4Smiod   do
15883d8817e4Smiod     {
15893d8817e4Smiod       enum link_action action;
15903d8817e4Smiod 
15913d8817e4Smiod       cycle = FALSE;
15923d8817e4Smiod       action = link_action[(int) row][(int) h->type];
15933d8817e4Smiod       switch (action)
15943d8817e4Smiod 	{
15953d8817e4Smiod 	case FAIL:
15963d8817e4Smiod 	  abort ();
15973d8817e4Smiod 
15983d8817e4Smiod 	case NOACT:
15993d8817e4Smiod 	  /* Do nothing.  */
16003d8817e4Smiod 	  break;
16013d8817e4Smiod 
16023d8817e4Smiod 	case UND:
16033d8817e4Smiod 	  /* Make a new undefined symbol.  */
16043d8817e4Smiod 	  h->type = bfd_link_hash_undefined;
16053d8817e4Smiod 	  h->u.undef.abfd = abfd;
16063d8817e4Smiod 	  bfd_link_add_undef (info->hash, h);
16073d8817e4Smiod 	  break;
16083d8817e4Smiod 
16093d8817e4Smiod 	case WEAK:
16103d8817e4Smiod 	  /* Make a new weak undefined symbol.  */
16113d8817e4Smiod 	  h->type = bfd_link_hash_undefweak;
16123d8817e4Smiod 	  h->u.undef.abfd = abfd;
16133d8817e4Smiod 	  h->u.undef.weak = abfd;
16143d8817e4Smiod 	  break;
16153d8817e4Smiod 
16163d8817e4Smiod 	case CDEF:
16173d8817e4Smiod 	  /* We have found a definition for a symbol which was
16183d8817e4Smiod 	     previously common.  */
16193d8817e4Smiod 	  BFD_ASSERT (h->type == bfd_link_hash_common);
16203d8817e4Smiod 	  if (! ((*info->callbacks->multiple_common)
16213d8817e4Smiod 		 (info, h->root.string,
16223d8817e4Smiod 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
16233d8817e4Smiod 		  abfd, bfd_link_hash_defined, 0)))
16243d8817e4Smiod 	    return FALSE;
16253d8817e4Smiod 	  /* Fall through.  */
16263d8817e4Smiod 	case DEF:
16273d8817e4Smiod 	case DEFW:
16283d8817e4Smiod 	  {
16293d8817e4Smiod 	    enum bfd_link_hash_type oldtype;
16303d8817e4Smiod 
16313d8817e4Smiod 	    /* Define a symbol.  */
16323d8817e4Smiod 	    oldtype = h->type;
16333d8817e4Smiod 	    if (action == DEFW)
16343d8817e4Smiod 	      h->type = bfd_link_hash_defweak;
16353d8817e4Smiod 	    else
16363d8817e4Smiod 	      h->type = bfd_link_hash_defined;
16373d8817e4Smiod 	    h->u.def.section = section;
16383d8817e4Smiod 	    h->u.def.value = value;
16393d8817e4Smiod 
16403d8817e4Smiod 	    /* If we have been asked to, we act like collect2 and
16413d8817e4Smiod 	       identify all functions that might be global
16423d8817e4Smiod 	       constructors and destructors and pass them up in a
16433d8817e4Smiod 	       callback.  We only do this for certain object file
16443d8817e4Smiod 	       types, since many object file types can handle this
16453d8817e4Smiod 	       automatically.  */
16463d8817e4Smiod 	    if (collect && name[0] == '_')
16473d8817e4Smiod 	      {
16483d8817e4Smiod 		const char *s;
16493d8817e4Smiod 
16503d8817e4Smiod 		/* A constructor or destructor name starts like this:
16513d8817e4Smiod 		   _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
16523d8817e4Smiod 		   the second are the same character (we accept any
16533d8817e4Smiod 		   character there, in case a new object file format
16543d8817e4Smiod 		   comes along with even worse naming restrictions).  */
16553d8817e4Smiod 
16563d8817e4Smiod #define CONS_PREFIX "GLOBAL_"
16573d8817e4Smiod #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
16583d8817e4Smiod 
16593d8817e4Smiod 		s = name + 1;
16603d8817e4Smiod 		while (*s == '_')
16613d8817e4Smiod 		  ++s;
16623d8817e4Smiod 		if (s[0] == 'G'
16633d8817e4Smiod 		    && strncmp (s, CONS_PREFIX, CONS_PREFIX_LEN - 1) == 0)
16643d8817e4Smiod 		  {
16653d8817e4Smiod 		    char c;
16663d8817e4Smiod 
16673d8817e4Smiod 		    c = s[CONS_PREFIX_LEN + 1];
16683d8817e4Smiod 		    if ((c == 'I' || c == 'D')
16693d8817e4Smiod 			&& s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
16703d8817e4Smiod 		      {
16713d8817e4Smiod 			/* If this is a definition of a symbol which
16723d8817e4Smiod                            was previously weakly defined, we are in
16733d8817e4Smiod                            trouble.  We have already added a
16743d8817e4Smiod                            constructor entry for the weak defined
16753d8817e4Smiod                            symbol, and now we are trying to add one
16763d8817e4Smiod                            for the new symbol.  Fortunately, this case
16773d8817e4Smiod                            should never arise in practice.  */
16783d8817e4Smiod 			if (oldtype == bfd_link_hash_defweak)
16793d8817e4Smiod 			  abort ();
16803d8817e4Smiod 
16813d8817e4Smiod 			if (! ((*info->callbacks->constructor)
16823d8817e4Smiod 			       (info, c == 'I',
16833d8817e4Smiod 				h->root.string, abfd, section, value)))
16843d8817e4Smiod 			  return FALSE;
16853d8817e4Smiod 		      }
16863d8817e4Smiod 		  }
16873d8817e4Smiod 	      }
16883d8817e4Smiod 	  }
16893d8817e4Smiod 
16903d8817e4Smiod 	  break;
16913d8817e4Smiod 
16923d8817e4Smiod 	case COM:
16933d8817e4Smiod 	  /* We have found a common definition for a symbol.  */
16943d8817e4Smiod 	  if (h->type == bfd_link_hash_new)
16953d8817e4Smiod 	    bfd_link_add_undef (info->hash, h);
16963d8817e4Smiod 	  h->type = bfd_link_hash_common;
16973d8817e4Smiod 	  h->u.c.p =
16983d8817e4Smiod 	    bfd_hash_allocate (&info->hash->table,
16993d8817e4Smiod 			       sizeof (struct bfd_link_hash_common_entry));
17003d8817e4Smiod 	  if (h->u.c.p == NULL)
17013d8817e4Smiod 	    return FALSE;
17023d8817e4Smiod 
17033d8817e4Smiod 	  h->u.c.size = value;
17043d8817e4Smiod 
17053d8817e4Smiod 	  /* Select a default alignment based on the size.  This may
17063d8817e4Smiod              be overridden by the caller.  */
17073d8817e4Smiod 	  {
17083d8817e4Smiod 	    unsigned int power;
17093d8817e4Smiod 
17103d8817e4Smiod 	    power = bfd_log2 (value);
17113d8817e4Smiod 	    if (power > 4)
17123d8817e4Smiod 	      power = 4;
17133d8817e4Smiod 	    h->u.c.p->alignment_power = power;
17143d8817e4Smiod 	  }
17153d8817e4Smiod 
17163d8817e4Smiod 	  /* The section of a common symbol is only used if the common
17173d8817e4Smiod              symbol is actually allocated.  It basically provides a
17183d8817e4Smiod              hook for the linker script to decide which output section
17193d8817e4Smiod              the common symbols should be put in.  In most cases, the
17203d8817e4Smiod              section of a common symbol will be bfd_com_section_ptr,
17213d8817e4Smiod              the code here will choose a common symbol section named
17223d8817e4Smiod              "COMMON", and the linker script will contain *(COMMON) in
17233d8817e4Smiod              the appropriate place.  A few targets use separate common
17243d8817e4Smiod              sections for small symbols, and they require special
17253d8817e4Smiod              handling.  */
17263d8817e4Smiod 	  if (section == bfd_com_section_ptr)
17273d8817e4Smiod 	    {
17283d8817e4Smiod 	      h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
17293d8817e4Smiod 	      h->u.c.p->section->flags = SEC_ALLOC;
17303d8817e4Smiod 	    }
17313d8817e4Smiod 	  else if (section->owner != abfd)
17323d8817e4Smiod 	    {
17333d8817e4Smiod 	      h->u.c.p->section = bfd_make_section_old_way (abfd,
17343d8817e4Smiod 							    section->name);
17353d8817e4Smiod 	      h->u.c.p->section->flags = SEC_ALLOC;
17363d8817e4Smiod 	    }
17373d8817e4Smiod 	  else
17383d8817e4Smiod 	    h->u.c.p->section = section;
17393d8817e4Smiod 	  break;
17403d8817e4Smiod 
17413d8817e4Smiod 	case REF:
17423d8817e4Smiod 	  /* A reference to a defined symbol.  */
17433d8817e4Smiod 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
17443d8817e4Smiod 	    h->u.undef.next = h;
17453d8817e4Smiod 	  break;
17463d8817e4Smiod 
17473d8817e4Smiod 	case BIG:
17483d8817e4Smiod 	  /* We have found a common definition for a symbol which
17493d8817e4Smiod 	     already had a common definition.  Use the maximum of the
17503d8817e4Smiod 	     two sizes, and use the section required by the larger symbol.  */
17513d8817e4Smiod 	  BFD_ASSERT (h->type == bfd_link_hash_common);
17523d8817e4Smiod 	  if (! ((*info->callbacks->multiple_common)
17533d8817e4Smiod 		 (info, h->root.string,
17543d8817e4Smiod 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
17553d8817e4Smiod 		  abfd, bfd_link_hash_common, value)))
17563d8817e4Smiod 	    return FALSE;
17573d8817e4Smiod 	  if (value > h->u.c.size)
17583d8817e4Smiod 	    {
17593d8817e4Smiod 	      unsigned int power;
17603d8817e4Smiod 
17613d8817e4Smiod 	      h->u.c.size = value;
17623d8817e4Smiod 
17633d8817e4Smiod 	      /* Select a default alignment based on the size.  This may
17643d8817e4Smiod 		 be overridden by the caller.  */
17653d8817e4Smiod 	      power = bfd_log2 (value);
17663d8817e4Smiod 	      if (power > 4)
17673d8817e4Smiod 		power = 4;
17683d8817e4Smiod 	      h->u.c.p->alignment_power = power;
17693d8817e4Smiod 
17703d8817e4Smiod 	      /* Some systems have special treatment for small commons,
17713d8817e4Smiod 		 hence we want to select the section used by the larger
17723d8817e4Smiod 		 symbol.  This makes sure the symbol does not go in a
17733d8817e4Smiod 		 small common section if it is now too large.  */
17743d8817e4Smiod 	      if (section == bfd_com_section_ptr)
17753d8817e4Smiod 		{
17763d8817e4Smiod 		  h->u.c.p->section
17773d8817e4Smiod 		    = bfd_make_section_old_way (abfd, "COMMON");
17783d8817e4Smiod 		  h->u.c.p->section->flags = SEC_ALLOC;
17793d8817e4Smiod 		}
17803d8817e4Smiod 	      else if (section->owner != abfd)
17813d8817e4Smiod 		{
17823d8817e4Smiod 		  h->u.c.p->section
17833d8817e4Smiod 		    = bfd_make_section_old_way (abfd, section->name);
17843d8817e4Smiod 		  h->u.c.p->section->flags = SEC_ALLOC;
17853d8817e4Smiod 		}
17863d8817e4Smiod 	      else
17873d8817e4Smiod 		h->u.c.p->section = section;
17883d8817e4Smiod 	    }
17893d8817e4Smiod 	  break;
17903d8817e4Smiod 
17913d8817e4Smiod 	case CREF:
17923d8817e4Smiod 	  {
17933d8817e4Smiod 	    bfd *obfd;
17943d8817e4Smiod 
17953d8817e4Smiod 	    /* We have found a common definition for a symbol which
17963d8817e4Smiod 	       was already defined.  FIXME: It would nice if we could
17973d8817e4Smiod 	       report the BFD which defined an indirect symbol, but we
17983d8817e4Smiod 	       don't have anywhere to store the information.  */
17993d8817e4Smiod 	    if (h->type == bfd_link_hash_defined
18003d8817e4Smiod 		|| h->type == bfd_link_hash_defweak)
18013d8817e4Smiod 	      obfd = h->u.def.section->owner;
18023d8817e4Smiod 	    else
18033d8817e4Smiod 	      obfd = NULL;
18043d8817e4Smiod 	    if (! ((*info->callbacks->multiple_common)
18053d8817e4Smiod 		   (info, h->root.string, obfd, h->type, 0,
18063d8817e4Smiod 		    abfd, bfd_link_hash_common, value)))
18073d8817e4Smiod 	      return FALSE;
18083d8817e4Smiod 	  }
18093d8817e4Smiod 	  break;
18103d8817e4Smiod 
18113d8817e4Smiod 	case MIND:
18123d8817e4Smiod 	  /* Multiple indirect symbols.  This is OK if they both point
18133d8817e4Smiod 	     to the same symbol.  */
18143d8817e4Smiod 	  if (strcmp (h->u.i.link->root.string, string) == 0)
18153d8817e4Smiod 	    break;
18163d8817e4Smiod 	  /* Fall through.  */
18173d8817e4Smiod 	case MDEF:
18183d8817e4Smiod 	  /* Handle a multiple definition.  */
18193d8817e4Smiod 	  if (!info->allow_multiple_definition)
18203d8817e4Smiod 	    {
18213d8817e4Smiod 	      asection *msec = NULL;
18223d8817e4Smiod 	      bfd_vma mval = 0;
18233d8817e4Smiod 
18243d8817e4Smiod 	      switch (h->type)
18253d8817e4Smiod 		{
18263d8817e4Smiod 		case bfd_link_hash_defined:
18273d8817e4Smiod 		  msec = h->u.def.section;
18283d8817e4Smiod 		  mval = h->u.def.value;
18293d8817e4Smiod 		  break;
18303d8817e4Smiod 	        case bfd_link_hash_indirect:
18313d8817e4Smiod 		  msec = bfd_ind_section_ptr;
18323d8817e4Smiod 		  mval = 0;
18333d8817e4Smiod 		  break;
18343d8817e4Smiod 		default:
18353d8817e4Smiod 		  abort ();
18363d8817e4Smiod 		}
18373d8817e4Smiod 
18383d8817e4Smiod 	      /* Ignore a redefinition of an absolute symbol to the
18393d8817e4Smiod 		 same value; it's harmless.  */
18403d8817e4Smiod 	      if (h->type == bfd_link_hash_defined
18413d8817e4Smiod 		  && bfd_is_abs_section (msec)
18423d8817e4Smiod 		  && bfd_is_abs_section (section)
18433d8817e4Smiod 		  && value == mval)
18443d8817e4Smiod 		break;
18453d8817e4Smiod 
18463d8817e4Smiod 	      if (! ((*info->callbacks->multiple_definition)
18473d8817e4Smiod 		     (info, h->root.string, msec->owner, msec, mval,
18483d8817e4Smiod 		      abfd, section, value)))
18493d8817e4Smiod 		return FALSE;
18503d8817e4Smiod 	    }
18513d8817e4Smiod 	  break;
18523d8817e4Smiod 
18533d8817e4Smiod 	case CIND:
18543d8817e4Smiod 	  /* Create an indirect symbol from an existing common symbol.  */
18553d8817e4Smiod 	  BFD_ASSERT (h->type == bfd_link_hash_common);
18563d8817e4Smiod 	  if (! ((*info->callbacks->multiple_common)
18573d8817e4Smiod 		 (info, h->root.string,
18583d8817e4Smiod 		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
18593d8817e4Smiod 		  abfd, bfd_link_hash_indirect, 0)))
18603d8817e4Smiod 	    return FALSE;
18613d8817e4Smiod 	  /* Fall through.  */
18623d8817e4Smiod 	case IND:
18633d8817e4Smiod 	  /* Create an indirect symbol.  */
18643d8817e4Smiod 	  {
18653d8817e4Smiod 	    struct bfd_link_hash_entry *inh;
18663d8817e4Smiod 
18673d8817e4Smiod 	    /* STRING is the name of the symbol we want to indirect
18683d8817e4Smiod 	       to.  */
18693d8817e4Smiod 	    inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
18703d8817e4Smiod 						copy, FALSE);
18713d8817e4Smiod 	    if (inh == NULL)
18723d8817e4Smiod 	      return FALSE;
18733d8817e4Smiod 	    if (inh->type == bfd_link_hash_indirect
18743d8817e4Smiod 		&& inh->u.i.link == h)
18753d8817e4Smiod 	      {
18763d8817e4Smiod 		(*_bfd_error_handler)
18773d8817e4Smiod 		  (_("%B: indirect symbol `%s' to `%s' is a loop"),
18783d8817e4Smiod 		   abfd, name, string);
18793d8817e4Smiod 		bfd_set_error (bfd_error_invalid_operation);
18803d8817e4Smiod 		return FALSE;
18813d8817e4Smiod 	      }
18823d8817e4Smiod 	    if (inh->type == bfd_link_hash_new)
18833d8817e4Smiod 	      {
18843d8817e4Smiod 		inh->type = bfd_link_hash_undefined;
18853d8817e4Smiod 		inh->u.undef.abfd = abfd;
18863d8817e4Smiod 		bfd_link_add_undef (info->hash, inh);
18873d8817e4Smiod 	      }
18883d8817e4Smiod 
18893d8817e4Smiod 	    /* If the indirect symbol has been referenced, we need to
18903d8817e4Smiod 	       push the reference down to the symbol we are
18913d8817e4Smiod 	       referencing.  */
18923d8817e4Smiod 	    if (h->type != bfd_link_hash_new)
18933d8817e4Smiod 	      {
18943d8817e4Smiod 		row = UNDEF_ROW;
18953d8817e4Smiod 		cycle = TRUE;
18963d8817e4Smiod 	      }
18973d8817e4Smiod 
18983d8817e4Smiod 	    h->type = bfd_link_hash_indirect;
18993d8817e4Smiod 	    h->u.i.link = inh;
19003d8817e4Smiod 	  }
19013d8817e4Smiod 	  break;
19023d8817e4Smiod 
19033d8817e4Smiod 	case SET:
19043d8817e4Smiod 	  /* Add an entry to a set.  */
19053d8817e4Smiod 	  if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
19063d8817e4Smiod 						abfd, section, value))
19073d8817e4Smiod 	    return FALSE;
19083d8817e4Smiod 	  break;
19093d8817e4Smiod 
19103d8817e4Smiod 	case WARNC:
19113d8817e4Smiod 	  /* Issue a warning and cycle.  */
19123d8817e4Smiod 	  if (h->u.i.warning != NULL)
19133d8817e4Smiod 	    {
19143d8817e4Smiod 	      if (! (*info->callbacks->warning) (info, h->u.i.warning,
19153d8817e4Smiod 						 h->root.string, abfd,
19163d8817e4Smiod 						 NULL, 0))
19173d8817e4Smiod 		return FALSE;
19183d8817e4Smiod 	      /* Only issue a warning once.  */
19193d8817e4Smiod 	      h->u.i.warning = NULL;
19203d8817e4Smiod 	    }
19213d8817e4Smiod 	  /* Fall through.  */
19223d8817e4Smiod 	case CYCLE:
19233d8817e4Smiod 	  /* Try again with the referenced symbol.  */
19243d8817e4Smiod 	  h = h->u.i.link;
19253d8817e4Smiod 	  cycle = TRUE;
19263d8817e4Smiod 	  break;
19273d8817e4Smiod 
19283d8817e4Smiod 	case REFC:
19293d8817e4Smiod 	  /* A reference to an indirect symbol.  */
19303d8817e4Smiod 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
19313d8817e4Smiod 	    h->u.undef.next = h;
19323d8817e4Smiod 	  h = h->u.i.link;
19333d8817e4Smiod 	  cycle = TRUE;
19343d8817e4Smiod 	  break;
19353d8817e4Smiod 
19363d8817e4Smiod 	case WARN:
19373d8817e4Smiod 	  /* Issue a warning.  */
19383d8817e4Smiod 	  if (! (*info->callbacks->warning) (info, string, h->root.string,
19393d8817e4Smiod 					     hash_entry_bfd (h), NULL, 0))
19403d8817e4Smiod 	    return FALSE;
19413d8817e4Smiod 	  break;
19423d8817e4Smiod 
19433d8817e4Smiod 	case CWARN:
19443d8817e4Smiod 	  /* Warn if this symbol has been referenced already,
19453d8817e4Smiod 	     otherwise add a warning.  A symbol has been referenced if
19463d8817e4Smiod 	     the u.undef.next field is not NULL, or it is the tail of the
19473d8817e4Smiod 	     undefined symbol list.  The REF case above helps to
19483d8817e4Smiod 	     ensure this.  */
19493d8817e4Smiod 	  if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
19503d8817e4Smiod 	    {
19513d8817e4Smiod 	      if (! (*info->callbacks->warning) (info, string, h->root.string,
19523d8817e4Smiod 						 hash_entry_bfd (h), NULL, 0))
19533d8817e4Smiod 		return FALSE;
19543d8817e4Smiod 	      break;
19553d8817e4Smiod 	    }
19563d8817e4Smiod 	  /* Fall through.  */
19573d8817e4Smiod 	case MWARN:
19583d8817e4Smiod 	  /* Make a warning symbol.  */
19593d8817e4Smiod 	  {
19603d8817e4Smiod 	    struct bfd_link_hash_entry *sub;
19613d8817e4Smiod 
19623d8817e4Smiod 	    /* STRING is the warning to give.  */
19633d8817e4Smiod 	    sub = ((struct bfd_link_hash_entry *)
19643d8817e4Smiod 		   ((*info->hash->table.newfunc)
19653d8817e4Smiod 		    (NULL, &info->hash->table, h->root.string)));
19663d8817e4Smiod 	    if (sub == NULL)
19673d8817e4Smiod 	      return FALSE;
19683d8817e4Smiod 	    *sub = *h;
19693d8817e4Smiod 	    sub->type = bfd_link_hash_warning;
19703d8817e4Smiod 	    sub->u.i.link = h;
19713d8817e4Smiod 	    if (! copy)
19723d8817e4Smiod 	      sub->u.i.warning = string;
19733d8817e4Smiod 	    else
19743d8817e4Smiod 	      {
19753d8817e4Smiod 		char *w;
19763d8817e4Smiod 		size_t len = strlen (string) + 1;
19773d8817e4Smiod 
19783d8817e4Smiod 		w = bfd_hash_allocate (&info->hash->table, len);
19793d8817e4Smiod 		if (w == NULL)
19803d8817e4Smiod 		  return FALSE;
19813d8817e4Smiod 		memcpy (w, string, len);
19823d8817e4Smiod 		sub->u.i.warning = w;
19833d8817e4Smiod 	      }
19843d8817e4Smiod 
19853d8817e4Smiod 	    bfd_hash_replace (&info->hash->table,
19863d8817e4Smiod 			      (struct bfd_hash_entry *) h,
19873d8817e4Smiod 			      (struct bfd_hash_entry *) sub);
19883d8817e4Smiod 	    if (hashp != NULL)
19893d8817e4Smiod 	      *hashp = sub;
19903d8817e4Smiod 	  }
19913d8817e4Smiod 	  break;
19923d8817e4Smiod 	}
19933d8817e4Smiod     }
19943d8817e4Smiod   while (cycle);
19953d8817e4Smiod 
19963d8817e4Smiod   return TRUE;
19973d8817e4Smiod }
19983d8817e4Smiod 
19993d8817e4Smiod /* Generic final link routine.  */
20003d8817e4Smiod 
20013d8817e4Smiod bfd_boolean
_bfd_generic_final_link(bfd * abfd,struct bfd_link_info * info)20023d8817e4Smiod _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
20033d8817e4Smiod {
20043d8817e4Smiod   bfd *sub;
20053d8817e4Smiod   asection *o;
20063d8817e4Smiod   struct bfd_link_order *p;
20073d8817e4Smiod   size_t outsymalloc;
20083d8817e4Smiod   struct generic_write_global_symbol_info wginfo;
20093d8817e4Smiod 
20103d8817e4Smiod   bfd_get_outsymbols (abfd) = NULL;
20113d8817e4Smiod   bfd_get_symcount (abfd) = 0;
20123d8817e4Smiod   outsymalloc = 0;
20133d8817e4Smiod 
20143d8817e4Smiod   /* Mark all sections which will be included in the output file.  */
20153d8817e4Smiod   for (o = abfd->sections; o != NULL; o = o->next)
20163d8817e4Smiod     for (p = o->map_head.link_order; p != NULL; p = p->next)
20173d8817e4Smiod       if (p->type == bfd_indirect_link_order)
20183d8817e4Smiod 	p->u.indirect.section->linker_mark = TRUE;
20193d8817e4Smiod 
20203d8817e4Smiod   /* Build the output symbol table.  */
20213d8817e4Smiod   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
20223d8817e4Smiod     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
20233d8817e4Smiod       return FALSE;
20243d8817e4Smiod 
20253d8817e4Smiod   /* Accumulate the global symbols.  */
20263d8817e4Smiod   wginfo.info = info;
20273d8817e4Smiod   wginfo.output_bfd = abfd;
20283d8817e4Smiod   wginfo.psymalloc = &outsymalloc;
20293d8817e4Smiod   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
20303d8817e4Smiod 				   _bfd_generic_link_write_global_symbol,
20313d8817e4Smiod 				   &wginfo);
20323d8817e4Smiod 
20333d8817e4Smiod   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
20343d8817e4Smiod      shouldn't really need one, since we have SYMCOUNT, but some old
20353d8817e4Smiod      code still expects one.  */
20363d8817e4Smiod   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
20373d8817e4Smiod     return FALSE;
20383d8817e4Smiod 
20393d8817e4Smiod   if (info->relocatable)
20403d8817e4Smiod     {
20413d8817e4Smiod       /* Allocate space for the output relocs for each section.  */
20423d8817e4Smiod       for (o = abfd->sections; o != NULL; o = o->next)
20433d8817e4Smiod 	{
20443d8817e4Smiod 	  o->reloc_count = 0;
20453d8817e4Smiod 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
20463d8817e4Smiod 	    {
20473d8817e4Smiod 	      if (p->type == bfd_section_reloc_link_order
20483d8817e4Smiod 		  || p->type == bfd_symbol_reloc_link_order)
20493d8817e4Smiod 		++o->reloc_count;
20503d8817e4Smiod 	      else if (p->type == bfd_indirect_link_order)
20513d8817e4Smiod 		{
20523d8817e4Smiod 		  asection *input_section;
20533d8817e4Smiod 		  bfd *input_bfd;
20543d8817e4Smiod 		  long relsize;
20553d8817e4Smiod 		  arelent **relocs;
20563d8817e4Smiod 		  asymbol **symbols;
20573d8817e4Smiod 		  long reloc_count;
20583d8817e4Smiod 
20593d8817e4Smiod 		  input_section = p->u.indirect.section;
20603d8817e4Smiod 		  input_bfd = input_section->owner;
20613d8817e4Smiod 		  relsize = bfd_get_reloc_upper_bound (input_bfd,
20623d8817e4Smiod 						       input_section);
20633d8817e4Smiod 		  if (relsize < 0)
20643d8817e4Smiod 		    return FALSE;
20653d8817e4Smiod 		  relocs = bfd_malloc (relsize);
20663d8817e4Smiod 		  if (!relocs && relsize != 0)
20673d8817e4Smiod 		    return FALSE;
20683d8817e4Smiod 		  symbols = _bfd_generic_link_get_symbols (input_bfd);
20693d8817e4Smiod 		  reloc_count = bfd_canonicalize_reloc (input_bfd,
20703d8817e4Smiod 							input_section,
20713d8817e4Smiod 							relocs,
20723d8817e4Smiod 							symbols);
20733d8817e4Smiod 		  free (relocs);
20743d8817e4Smiod 		  if (reloc_count < 0)
20753d8817e4Smiod 		    return FALSE;
20763d8817e4Smiod 		  BFD_ASSERT ((unsigned long) reloc_count
20773d8817e4Smiod 			      == input_section->reloc_count);
20783d8817e4Smiod 		  o->reloc_count += reloc_count;
20793d8817e4Smiod 		}
20803d8817e4Smiod 	    }
20813d8817e4Smiod 	  if (o->reloc_count > 0)
20823d8817e4Smiod 	    {
20833d8817e4Smiod 	      bfd_size_type amt;
20843d8817e4Smiod 
20853d8817e4Smiod 	      amt = o->reloc_count;
20863d8817e4Smiod 	      amt *= sizeof (arelent *);
20873d8817e4Smiod 	      o->orelocation = bfd_alloc (abfd, amt);
20883d8817e4Smiod 	      if (!o->orelocation)
20893d8817e4Smiod 		return FALSE;
20903d8817e4Smiod 	      o->flags |= SEC_RELOC;
20913d8817e4Smiod 	      /* Reset the count so that it can be used as an index
20923d8817e4Smiod 		 when putting in the output relocs.  */
20933d8817e4Smiod 	      o->reloc_count = 0;
20943d8817e4Smiod 	    }
20953d8817e4Smiod 	}
20963d8817e4Smiod     }
20973d8817e4Smiod 
20983d8817e4Smiod   /* Handle all the link order information for the sections.  */
20993d8817e4Smiod   for (o = abfd->sections; o != NULL; o = o->next)
21003d8817e4Smiod     {
21013d8817e4Smiod       for (p = o->map_head.link_order; p != NULL; p = p->next)
21023d8817e4Smiod 	{
21033d8817e4Smiod 	  switch (p->type)
21043d8817e4Smiod 	    {
21053d8817e4Smiod 	    case bfd_section_reloc_link_order:
21063d8817e4Smiod 	    case bfd_symbol_reloc_link_order:
21073d8817e4Smiod 	      if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
21083d8817e4Smiod 		return FALSE;
21093d8817e4Smiod 	      break;
21103d8817e4Smiod 	    case bfd_indirect_link_order:
21113d8817e4Smiod 	      if (! default_indirect_link_order (abfd, info, o, p, TRUE))
21123d8817e4Smiod 		return FALSE;
21133d8817e4Smiod 	      break;
21143d8817e4Smiod 	    default:
21153d8817e4Smiod 	      if (! _bfd_default_link_order (abfd, info, o, p))
21163d8817e4Smiod 		return FALSE;
21173d8817e4Smiod 	      break;
21183d8817e4Smiod 	    }
21193d8817e4Smiod 	}
21203d8817e4Smiod     }
21213d8817e4Smiod 
21223d8817e4Smiod   return TRUE;
21233d8817e4Smiod }
21243d8817e4Smiod 
21253d8817e4Smiod /* Add an output symbol to the output BFD.  */
21263d8817e4Smiod 
21273d8817e4Smiod static bfd_boolean
generic_add_output_symbol(bfd * output_bfd,size_t * psymalloc,asymbol * sym)21283d8817e4Smiod generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
21293d8817e4Smiod {
21303d8817e4Smiod   if (bfd_get_symcount (output_bfd) >= *psymalloc)
21313d8817e4Smiod     {
21323d8817e4Smiod       asymbol **newsyms;
21333d8817e4Smiod       bfd_size_type amt;
21343d8817e4Smiod 
21353d8817e4Smiod       if (*psymalloc == 0)
21363d8817e4Smiod 	*psymalloc = 124;
21373d8817e4Smiod       else
21383d8817e4Smiod 	*psymalloc *= 2;
21393d8817e4Smiod       amt = *psymalloc;
21403d8817e4Smiod       amt *= sizeof (asymbol *);
21413d8817e4Smiod       newsyms = bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
21423d8817e4Smiod       if (newsyms == NULL)
21433d8817e4Smiod 	return FALSE;
21443d8817e4Smiod       bfd_get_outsymbols (output_bfd) = newsyms;
21453d8817e4Smiod     }
21463d8817e4Smiod 
21473d8817e4Smiod   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
21483d8817e4Smiod   if (sym != NULL)
21493d8817e4Smiod     ++ bfd_get_symcount (output_bfd);
21503d8817e4Smiod 
21513d8817e4Smiod   return TRUE;
21523d8817e4Smiod }
21533d8817e4Smiod 
21543d8817e4Smiod /* Handle the symbols for an input BFD.  */
21553d8817e4Smiod 
21563d8817e4Smiod bfd_boolean
_bfd_generic_link_output_symbols(bfd * output_bfd,bfd * input_bfd,struct bfd_link_info * info,size_t * psymalloc)21573d8817e4Smiod _bfd_generic_link_output_symbols (bfd *output_bfd,
21583d8817e4Smiod 				  bfd *input_bfd,
21593d8817e4Smiod 				  struct bfd_link_info *info,
21603d8817e4Smiod 				  size_t *psymalloc)
21613d8817e4Smiod {
21623d8817e4Smiod   asymbol **sym_ptr;
21633d8817e4Smiod   asymbol **sym_end;
21643d8817e4Smiod 
21653d8817e4Smiod   if (! generic_link_read_symbols (input_bfd))
21663d8817e4Smiod     return FALSE;
21673d8817e4Smiod 
21683d8817e4Smiod   /* Create a filename symbol if we are supposed to.  */
21693d8817e4Smiod   if (info->create_object_symbols_section != NULL)
21703d8817e4Smiod     {
21713d8817e4Smiod       asection *sec;
21723d8817e4Smiod 
21733d8817e4Smiod       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
21743d8817e4Smiod 	{
21753d8817e4Smiod 	  if (sec->output_section == info->create_object_symbols_section)
21763d8817e4Smiod 	    {
21773d8817e4Smiod 	      asymbol *newsym;
21783d8817e4Smiod 
21793d8817e4Smiod 	      newsym = bfd_make_empty_symbol (input_bfd);
21803d8817e4Smiod 	      if (!newsym)
21813d8817e4Smiod 		return FALSE;
21823d8817e4Smiod 	      newsym->name = input_bfd->filename;
21833d8817e4Smiod 	      newsym->value = 0;
21843d8817e4Smiod 	      newsym->flags = BSF_LOCAL | BSF_FILE;
21853d8817e4Smiod 	      newsym->section = sec;
21863d8817e4Smiod 
21873d8817e4Smiod 	      if (! generic_add_output_symbol (output_bfd, psymalloc,
21883d8817e4Smiod 					       newsym))
21893d8817e4Smiod 		return FALSE;
21903d8817e4Smiod 
21913d8817e4Smiod 	      break;
21923d8817e4Smiod 	    }
21933d8817e4Smiod 	}
21943d8817e4Smiod     }
21953d8817e4Smiod 
21963d8817e4Smiod   /* Adjust the values of the globally visible symbols, and write out
21973d8817e4Smiod      local symbols.  */
21983d8817e4Smiod   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
21993d8817e4Smiod   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
22003d8817e4Smiod   for (; sym_ptr < sym_end; sym_ptr++)
22013d8817e4Smiod     {
22023d8817e4Smiod       asymbol *sym;
22033d8817e4Smiod       struct generic_link_hash_entry *h;
22043d8817e4Smiod       bfd_boolean output;
22053d8817e4Smiod 
22063d8817e4Smiod       h = NULL;
22073d8817e4Smiod       sym = *sym_ptr;
22083d8817e4Smiod       if ((sym->flags & (BSF_INDIRECT
22093d8817e4Smiod 			 | BSF_WARNING
22103d8817e4Smiod 			 | BSF_GLOBAL
22113d8817e4Smiod 			 | BSF_CONSTRUCTOR
22123d8817e4Smiod 			 | BSF_WEAK)) != 0
22133d8817e4Smiod 	  || bfd_is_und_section (bfd_get_section (sym))
22143d8817e4Smiod 	  || bfd_is_com_section (bfd_get_section (sym))
22153d8817e4Smiod 	  || bfd_is_ind_section (bfd_get_section (sym)))
22163d8817e4Smiod 	{
22173d8817e4Smiod 	  if (sym->udata.p != NULL)
22183d8817e4Smiod 	    h = sym->udata.p;
22193d8817e4Smiod 	  else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
22203d8817e4Smiod 	    {
22213d8817e4Smiod 	      /* This case normally means that the main linker code
22223d8817e4Smiod                  deliberately ignored this constructor symbol.  We
22233d8817e4Smiod                  should just pass it through.  This will screw up if
22243d8817e4Smiod                  the constructor symbol is from a different,
22253d8817e4Smiod                  non-generic, object file format, but the case will
22263d8817e4Smiod                  only arise when linking with -r, which will probably
22273d8817e4Smiod                  fail anyhow, since there will be no way to represent
22283d8817e4Smiod                  the relocs in the output format being used.  */
22293d8817e4Smiod 	      h = NULL;
22303d8817e4Smiod 	    }
22313d8817e4Smiod 	  else if (bfd_is_und_section (bfd_get_section (sym)))
22323d8817e4Smiod 	    h = ((struct generic_link_hash_entry *)
22333d8817e4Smiod 		 bfd_wrapped_link_hash_lookup (output_bfd, info,
22343d8817e4Smiod 					       bfd_asymbol_name (sym),
22353d8817e4Smiod 					       FALSE, FALSE, TRUE));
22363d8817e4Smiod 	  else
22373d8817e4Smiod 	    h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
22383d8817e4Smiod 					       bfd_asymbol_name (sym),
22393d8817e4Smiod 					       FALSE, FALSE, TRUE);
22403d8817e4Smiod 
22413d8817e4Smiod 	  if (h != NULL)
22423d8817e4Smiod 	    {
22433d8817e4Smiod 	      /* Force all references to this symbol to point to
22443d8817e4Smiod 		 the same area in memory.  It is possible that
22453d8817e4Smiod 		 this routine will be called with a hash table
22463d8817e4Smiod 		 other than a generic hash table, so we double
22473d8817e4Smiod 		 check that.  */
22483d8817e4Smiod 	      if (info->hash->creator == input_bfd->xvec)
22493d8817e4Smiod 		{
22503d8817e4Smiod 		  if (h->sym != NULL)
22513d8817e4Smiod 		    *sym_ptr = sym = h->sym;
22523d8817e4Smiod 		}
22533d8817e4Smiod 
22543d8817e4Smiod 	      switch (h->root.type)
22553d8817e4Smiod 		{
22563d8817e4Smiod 		default:
22573d8817e4Smiod 		case bfd_link_hash_new:
22583d8817e4Smiod 		  abort ();
22593d8817e4Smiod 		case bfd_link_hash_undefined:
22603d8817e4Smiod 		  break;
22613d8817e4Smiod 		case bfd_link_hash_undefweak:
22623d8817e4Smiod 		  sym->flags |= BSF_WEAK;
22633d8817e4Smiod 		  break;
22643d8817e4Smiod 		case bfd_link_hash_indirect:
22653d8817e4Smiod 		  h = (struct generic_link_hash_entry *) h->root.u.i.link;
22663d8817e4Smiod 		  /* fall through */
22673d8817e4Smiod 		case bfd_link_hash_defined:
22683d8817e4Smiod 		  sym->flags |= BSF_GLOBAL;
22693d8817e4Smiod 		  sym->flags &=~ BSF_CONSTRUCTOR;
22703d8817e4Smiod 		  sym->value = h->root.u.def.value;
22713d8817e4Smiod 		  sym->section = h->root.u.def.section;
22723d8817e4Smiod 		  break;
22733d8817e4Smiod 		case bfd_link_hash_defweak:
22743d8817e4Smiod 		  sym->flags |= BSF_WEAK;
22753d8817e4Smiod 		  sym->flags &=~ BSF_CONSTRUCTOR;
22763d8817e4Smiod 		  sym->value = h->root.u.def.value;
22773d8817e4Smiod 		  sym->section = h->root.u.def.section;
22783d8817e4Smiod 		  break;
22793d8817e4Smiod 		case bfd_link_hash_common:
22803d8817e4Smiod 		  sym->value = h->root.u.c.size;
22813d8817e4Smiod 		  sym->flags |= BSF_GLOBAL;
22823d8817e4Smiod 		  if (! bfd_is_com_section (sym->section))
22833d8817e4Smiod 		    {
22843d8817e4Smiod 		      BFD_ASSERT (bfd_is_und_section (sym->section));
22853d8817e4Smiod 		      sym->section = bfd_com_section_ptr;
22863d8817e4Smiod 		    }
22873d8817e4Smiod 		  /* We do not set the section of the symbol to
22883d8817e4Smiod 		     h->root.u.c.p->section.  That value was saved so
22893d8817e4Smiod 		     that we would know where to allocate the symbol
22903d8817e4Smiod 		     if it was defined.  In this case the type is
22913d8817e4Smiod 		     still bfd_link_hash_common, so we did not define
22923d8817e4Smiod 		     it, so we do not want to use that section.  */
22933d8817e4Smiod 		  break;
22943d8817e4Smiod 		}
22953d8817e4Smiod 	    }
22963d8817e4Smiod 	}
22973d8817e4Smiod 
22983d8817e4Smiod       /* This switch is straight from the old code in
22993d8817e4Smiod 	 write_file_locals in ldsym.c.  */
23003d8817e4Smiod       if (info->strip == strip_all
23013d8817e4Smiod 	  || (info->strip == strip_some
23023d8817e4Smiod 	      && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
23033d8817e4Smiod 				  FALSE, FALSE) == NULL))
23043d8817e4Smiod 	output = FALSE;
23053d8817e4Smiod       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
23063d8817e4Smiod 	{
23073d8817e4Smiod 	  /* If this symbol is marked as occurring now, rather
23083d8817e4Smiod 	     than at the end, output it now.  This is used for
23093d8817e4Smiod 	     COFF C_EXT FCN symbols.  FIXME: There must be a
23103d8817e4Smiod 	     better way.  */
23113d8817e4Smiod 	  if (bfd_asymbol_bfd (sym) == input_bfd
23123d8817e4Smiod 	      && (sym->flags & BSF_NOT_AT_END) != 0)
23133d8817e4Smiod 	    output = TRUE;
23143d8817e4Smiod 	  else
23153d8817e4Smiod 	    output = FALSE;
23163d8817e4Smiod 	}
23173d8817e4Smiod       else if (bfd_is_ind_section (sym->section))
23183d8817e4Smiod 	output = FALSE;
23193d8817e4Smiod       else if ((sym->flags & BSF_DEBUGGING) != 0)
23203d8817e4Smiod 	{
23213d8817e4Smiod 	  if (info->strip == strip_none)
23223d8817e4Smiod 	    output = TRUE;
23233d8817e4Smiod 	  else
23243d8817e4Smiod 	    output = FALSE;
23253d8817e4Smiod 	}
23263d8817e4Smiod       else if (bfd_is_und_section (sym->section)
23273d8817e4Smiod 	       || bfd_is_com_section (sym->section))
23283d8817e4Smiod 	output = FALSE;
23293d8817e4Smiod       else if ((sym->flags & BSF_LOCAL) != 0)
23303d8817e4Smiod 	{
23313d8817e4Smiod 	  if ((sym->flags & BSF_WARNING) != 0)
23323d8817e4Smiod 	    output = FALSE;
23333d8817e4Smiod 	  else
23343d8817e4Smiod 	    {
23353d8817e4Smiod 	      switch (info->discard)
23363d8817e4Smiod 		{
23373d8817e4Smiod 		default:
23383d8817e4Smiod 		case discard_all:
23393d8817e4Smiod 		  output = FALSE;
23403d8817e4Smiod 		  break;
23413d8817e4Smiod 		case discard_sec_merge:
23423d8817e4Smiod 		  output = TRUE;
23433d8817e4Smiod 		  if (info->relocatable
23443d8817e4Smiod 		      || ! (sym->section->flags & SEC_MERGE))
23453d8817e4Smiod 		    break;
23463d8817e4Smiod 		  /* FALLTHROUGH */
23473d8817e4Smiod 		case discard_l:
23483d8817e4Smiod 		  if (bfd_is_local_label (input_bfd, sym))
23493d8817e4Smiod 		    output = FALSE;
23503d8817e4Smiod 		  else
23513d8817e4Smiod 		    output = TRUE;
23523d8817e4Smiod 		  break;
23533d8817e4Smiod 		case discard_none:
23543d8817e4Smiod 		  output = TRUE;
23553d8817e4Smiod 		  break;
23563d8817e4Smiod 		}
23573d8817e4Smiod 	    }
23583d8817e4Smiod 	}
23593d8817e4Smiod       else if ((sym->flags & BSF_CONSTRUCTOR))
23603d8817e4Smiod 	{
23613d8817e4Smiod 	  if (info->strip != strip_all)
23623d8817e4Smiod 	    output = TRUE;
23633d8817e4Smiod 	  else
23643d8817e4Smiod 	    output = FALSE;
23653d8817e4Smiod 	}
23663d8817e4Smiod       else
23673d8817e4Smiod 	abort ();
23683d8817e4Smiod 
23693d8817e4Smiod       /* If this symbol is in a section which is not being included
23703d8817e4Smiod 	 in the output file, then we don't want to output the
23713d8817e4Smiod 	 symbol.  */
23723d8817e4Smiod       if (!bfd_is_abs_section (sym->section)
23733d8817e4Smiod 	  && bfd_section_removed_from_list (output_bfd,
23743d8817e4Smiod 					    sym->section->output_section))
23753d8817e4Smiod 	output = FALSE;
23763d8817e4Smiod 
23773d8817e4Smiod       if (output)
23783d8817e4Smiod 	{
23793d8817e4Smiod 	  if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
23803d8817e4Smiod 	    return FALSE;
23813d8817e4Smiod 	  if (h != NULL)
23823d8817e4Smiod 	    h->written = TRUE;
23833d8817e4Smiod 	}
23843d8817e4Smiod     }
23853d8817e4Smiod 
23863d8817e4Smiod   return TRUE;
23873d8817e4Smiod }
23883d8817e4Smiod 
23893d8817e4Smiod /* Set the section and value of a generic BFD symbol based on a linker
23903d8817e4Smiod    hash table entry.  */
23913d8817e4Smiod 
23923d8817e4Smiod static void
set_symbol_from_hash(asymbol * sym,struct bfd_link_hash_entry * h)23933d8817e4Smiod set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
23943d8817e4Smiod {
23953d8817e4Smiod   switch (h->type)
23963d8817e4Smiod     {
23973d8817e4Smiod     default:
23983d8817e4Smiod       abort ();
23993d8817e4Smiod       break;
24003d8817e4Smiod     case bfd_link_hash_new:
24013d8817e4Smiod       /* This can happen when a constructor symbol is seen but we are
24023d8817e4Smiod          not building constructors.  */
24033d8817e4Smiod       if (sym->section != NULL)
24043d8817e4Smiod 	{
24053d8817e4Smiod 	  BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
24063d8817e4Smiod 	}
24073d8817e4Smiod       else
24083d8817e4Smiod 	{
24093d8817e4Smiod 	  sym->flags |= BSF_CONSTRUCTOR;
24103d8817e4Smiod 	  sym->section = bfd_abs_section_ptr;
24113d8817e4Smiod 	  sym->value = 0;
24123d8817e4Smiod 	}
24133d8817e4Smiod       break;
24143d8817e4Smiod     case bfd_link_hash_undefined:
24153d8817e4Smiod       sym->section = bfd_und_section_ptr;
24163d8817e4Smiod       sym->value = 0;
24173d8817e4Smiod       break;
24183d8817e4Smiod     case bfd_link_hash_undefweak:
24193d8817e4Smiod       sym->section = bfd_und_section_ptr;
24203d8817e4Smiod       sym->value = 0;
24213d8817e4Smiod       sym->flags |= BSF_WEAK;
24223d8817e4Smiod       break;
24233d8817e4Smiod     case bfd_link_hash_defined:
24243d8817e4Smiod       sym->section = h->u.def.section;
24253d8817e4Smiod       sym->value = h->u.def.value;
24263d8817e4Smiod       break;
24273d8817e4Smiod     case bfd_link_hash_defweak:
24283d8817e4Smiod       sym->flags |= BSF_WEAK;
24293d8817e4Smiod       sym->section = h->u.def.section;
24303d8817e4Smiod       sym->value = h->u.def.value;
24313d8817e4Smiod       break;
24323d8817e4Smiod     case bfd_link_hash_common:
24333d8817e4Smiod       sym->value = h->u.c.size;
24343d8817e4Smiod       if (sym->section == NULL)
24353d8817e4Smiod 	sym->section = bfd_com_section_ptr;
24363d8817e4Smiod       else if (! bfd_is_com_section (sym->section))
24373d8817e4Smiod 	{
24383d8817e4Smiod 	  BFD_ASSERT (bfd_is_und_section (sym->section));
24393d8817e4Smiod 	  sym->section = bfd_com_section_ptr;
24403d8817e4Smiod 	}
24413d8817e4Smiod       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
24423d8817e4Smiod       break;
24433d8817e4Smiod     case bfd_link_hash_indirect:
24443d8817e4Smiod     case bfd_link_hash_warning:
24453d8817e4Smiod       /* FIXME: What should we do here?  */
24463d8817e4Smiod       break;
24473d8817e4Smiod     }
24483d8817e4Smiod }
24493d8817e4Smiod 
24503d8817e4Smiod /* Write out a global symbol, if it hasn't already been written out.
24513d8817e4Smiod    This is called for each symbol in the hash table.  */
24523d8817e4Smiod 
24533d8817e4Smiod bfd_boolean
_bfd_generic_link_write_global_symbol(struct generic_link_hash_entry * h,void * data)24543d8817e4Smiod _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
24553d8817e4Smiod 				       void *data)
24563d8817e4Smiod {
24573d8817e4Smiod   struct generic_write_global_symbol_info *wginfo = data;
24583d8817e4Smiod   asymbol *sym;
24593d8817e4Smiod 
24603d8817e4Smiod   if (h->root.type == bfd_link_hash_warning)
24613d8817e4Smiod     h = (struct generic_link_hash_entry *) h->root.u.i.link;
24623d8817e4Smiod 
24633d8817e4Smiod   if (h->written)
24643d8817e4Smiod     return TRUE;
24653d8817e4Smiod 
24663d8817e4Smiod   h->written = TRUE;
24673d8817e4Smiod 
24683d8817e4Smiod   if (wginfo->info->strip == strip_all
24693d8817e4Smiod       || (wginfo->info->strip == strip_some
24703d8817e4Smiod 	  && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
24713d8817e4Smiod 			      FALSE, FALSE) == NULL))
24723d8817e4Smiod     return TRUE;
24733d8817e4Smiod 
24743d8817e4Smiod   if (h->sym != NULL)
24753d8817e4Smiod     sym = h->sym;
24763d8817e4Smiod   else
24773d8817e4Smiod     {
24783d8817e4Smiod       sym = bfd_make_empty_symbol (wginfo->output_bfd);
24793d8817e4Smiod       if (!sym)
24803d8817e4Smiod 	return FALSE;
24813d8817e4Smiod       sym->name = h->root.root.string;
24823d8817e4Smiod       sym->flags = 0;
24833d8817e4Smiod     }
24843d8817e4Smiod 
24853d8817e4Smiod   set_symbol_from_hash (sym, &h->root);
24863d8817e4Smiod 
24873d8817e4Smiod   sym->flags |= BSF_GLOBAL;
24883d8817e4Smiod 
24893d8817e4Smiod   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
24903d8817e4Smiod 				   sym))
24913d8817e4Smiod     {
24923d8817e4Smiod       /* FIXME: No way to return failure.  */
24933d8817e4Smiod       abort ();
24943d8817e4Smiod     }
24953d8817e4Smiod 
24963d8817e4Smiod   return TRUE;
24973d8817e4Smiod }
24983d8817e4Smiod 
24993d8817e4Smiod /* Create a relocation.  */
25003d8817e4Smiod 
25013d8817e4Smiod bfd_boolean
_bfd_generic_reloc_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)25023d8817e4Smiod _bfd_generic_reloc_link_order (bfd *abfd,
25033d8817e4Smiod 			       struct bfd_link_info *info,
25043d8817e4Smiod 			       asection *sec,
25053d8817e4Smiod 			       struct bfd_link_order *link_order)
25063d8817e4Smiod {
25073d8817e4Smiod   arelent *r;
25083d8817e4Smiod 
25093d8817e4Smiod   if (! info->relocatable)
25103d8817e4Smiod     abort ();
25113d8817e4Smiod   if (sec->orelocation == NULL)
25123d8817e4Smiod     abort ();
25133d8817e4Smiod 
25143d8817e4Smiod   r = bfd_alloc (abfd, sizeof (arelent));
25153d8817e4Smiod   if (r == NULL)
25163d8817e4Smiod     return FALSE;
25173d8817e4Smiod 
25183d8817e4Smiod   r->address = link_order->offset;
25193d8817e4Smiod   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
25203d8817e4Smiod   if (r->howto == 0)
25213d8817e4Smiod     {
25223d8817e4Smiod       bfd_set_error (bfd_error_bad_value);
25233d8817e4Smiod       return FALSE;
25243d8817e4Smiod     }
25253d8817e4Smiod 
25263d8817e4Smiod   /* Get the symbol to use for the relocation.  */
25273d8817e4Smiod   if (link_order->type == bfd_section_reloc_link_order)
25283d8817e4Smiod     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
25293d8817e4Smiod   else
25303d8817e4Smiod     {
25313d8817e4Smiod       struct generic_link_hash_entry *h;
25323d8817e4Smiod 
25333d8817e4Smiod       h = ((struct generic_link_hash_entry *)
25343d8817e4Smiod 	   bfd_wrapped_link_hash_lookup (abfd, info,
25353d8817e4Smiod 					 link_order->u.reloc.p->u.name,
25363d8817e4Smiod 					 FALSE, FALSE, TRUE));
25373d8817e4Smiod       if (h == NULL
25383d8817e4Smiod 	  || ! h->written)
25393d8817e4Smiod 	{
25403d8817e4Smiod 	  if (! ((*info->callbacks->unattached_reloc)
25413d8817e4Smiod 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
25423d8817e4Smiod 	    return FALSE;
25433d8817e4Smiod 	  bfd_set_error (bfd_error_bad_value);
25443d8817e4Smiod 	  return FALSE;
25453d8817e4Smiod 	}
25463d8817e4Smiod       r->sym_ptr_ptr = &h->sym;
25473d8817e4Smiod     }
25483d8817e4Smiod 
25493d8817e4Smiod   /* If this is an inplace reloc, write the addend to the object file.
25503d8817e4Smiod      Otherwise, store it in the reloc addend.  */
25513d8817e4Smiod   if (! r->howto->partial_inplace)
25523d8817e4Smiod     r->addend = link_order->u.reloc.p->addend;
25533d8817e4Smiod   else
25543d8817e4Smiod     {
25553d8817e4Smiod       bfd_size_type size;
25563d8817e4Smiod       bfd_reloc_status_type rstat;
25573d8817e4Smiod       bfd_byte *buf;
25583d8817e4Smiod       bfd_boolean ok;
25593d8817e4Smiod       file_ptr loc;
25603d8817e4Smiod 
25613d8817e4Smiod       size = bfd_get_reloc_size (r->howto);
25623d8817e4Smiod       buf = bfd_zmalloc (size);
25633d8817e4Smiod       if (buf == NULL)
25643d8817e4Smiod 	return FALSE;
25653d8817e4Smiod       rstat = _bfd_relocate_contents (r->howto, abfd,
25663d8817e4Smiod 				      (bfd_vma) link_order->u.reloc.p->addend,
25673d8817e4Smiod 				      buf);
25683d8817e4Smiod       switch (rstat)
25693d8817e4Smiod 	{
25703d8817e4Smiod 	case bfd_reloc_ok:
25713d8817e4Smiod 	  break;
25723d8817e4Smiod 	default:
25733d8817e4Smiod 	case bfd_reloc_outofrange:
25743d8817e4Smiod 	  abort ();
25753d8817e4Smiod 	case bfd_reloc_overflow:
25763d8817e4Smiod 	  if (! ((*info->callbacks->reloc_overflow)
25773d8817e4Smiod 		 (info, NULL,
25783d8817e4Smiod 		  (link_order->type == bfd_section_reloc_link_order
25793d8817e4Smiod 		   ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
25803d8817e4Smiod 		   : link_order->u.reloc.p->u.name),
25813d8817e4Smiod 		  r->howto->name, link_order->u.reloc.p->addend,
25823d8817e4Smiod 		  NULL, NULL, 0)))
25833d8817e4Smiod 	    {
25843d8817e4Smiod 	      free (buf);
25853d8817e4Smiod 	      return FALSE;
25863d8817e4Smiod 	    }
25873d8817e4Smiod 	  break;
25883d8817e4Smiod 	}
25893d8817e4Smiod       loc = link_order->offset * bfd_octets_per_byte (abfd);
25903d8817e4Smiod       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
25913d8817e4Smiod       free (buf);
25923d8817e4Smiod       if (! ok)
25933d8817e4Smiod 	return FALSE;
25943d8817e4Smiod 
25953d8817e4Smiod       r->addend = 0;
25963d8817e4Smiod     }
25973d8817e4Smiod 
25983d8817e4Smiod   sec->orelocation[sec->reloc_count] = r;
25993d8817e4Smiod   ++sec->reloc_count;
26003d8817e4Smiod 
26013d8817e4Smiod   return TRUE;
26023d8817e4Smiod }
26033d8817e4Smiod 
26043d8817e4Smiod /* Allocate a new link_order for a section.  */
26053d8817e4Smiod 
26063d8817e4Smiod struct bfd_link_order *
bfd_new_link_order(bfd * abfd,asection * section)26073d8817e4Smiod bfd_new_link_order (bfd *abfd, asection *section)
26083d8817e4Smiod {
26093d8817e4Smiod   bfd_size_type amt = sizeof (struct bfd_link_order);
26103d8817e4Smiod   struct bfd_link_order *new;
26113d8817e4Smiod 
26123d8817e4Smiod   new = bfd_zalloc (abfd, amt);
26133d8817e4Smiod   if (!new)
26143d8817e4Smiod     return NULL;
26153d8817e4Smiod 
26163d8817e4Smiod   new->type = bfd_undefined_link_order;
26173d8817e4Smiod 
26183d8817e4Smiod   if (section->map_tail.link_order != NULL)
26193d8817e4Smiod     section->map_tail.link_order->next = new;
26203d8817e4Smiod   else
26213d8817e4Smiod     section->map_head.link_order = new;
26223d8817e4Smiod   section->map_tail.link_order = new;
26233d8817e4Smiod 
26243d8817e4Smiod   return new;
26253d8817e4Smiod }
26263d8817e4Smiod 
26273d8817e4Smiod /* Default link order processing routine.  Note that we can not handle
26283d8817e4Smiod    the reloc_link_order types here, since they depend upon the details
26293d8817e4Smiod    of how the particular backends generates relocs.  */
26303d8817e4Smiod 
26313d8817e4Smiod bfd_boolean
_bfd_default_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)26323d8817e4Smiod _bfd_default_link_order (bfd *abfd,
26333d8817e4Smiod 			 struct bfd_link_info *info,
26343d8817e4Smiod 			 asection *sec,
26353d8817e4Smiod 			 struct bfd_link_order *link_order)
26363d8817e4Smiod {
26373d8817e4Smiod   switch (link_order->type)
26383d8817e4Smiod     {
26393d8817e4Smiod     case bfd_undefined_link_order:
26403d8817e4Smiod     case bfd_section_reloc_link_order:
26413d8817e4Smiod     case bfd_symbol_reloc_link_order:
26423d8817e4Smiod     default:
26433d8817e4Smiod       abort ();
26443d8817e4Smiod     case bfd_indirect_link_order:
26453d8817e4Smiod       return default_indirect_link_order (abfd, info, sec, link_order,
26463d8817e4Smiod 					  FALSE);
26473d8817e4Smiod     case bfd_data_link_order:
26483d8817e4Smiod       return default_data_link_order (abfd, info, sec, link_order);
26493d8817e4Smiod     }
26503d8817e4Smiod }
26513d8817e4Smiod 
26523d8817e4Smiod /* Default routine to handle a bfd_data_link_order.  */
26533d8817e4Smiod 
26543d8817e4Smiod static bfd_boolean
default_data_link_order(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_order * link_order)26553d8817e4Smiod default_data_link_order (bfd *abfd,
26563d8817e4Smiod 			 struct bfd_link_info *info ATTRIBUTE_UNUSED,
26573d8817e4Smiod 			 asection *sec,
26583d8817e4Smiod 			 struct bfd_link_order *link_order)
26593d8817e4Smiod {
26603d8817e4Smiod   bfd_size_type size;
26613d8817e4Smiod   size_t fill_size;
26623d8817e4Smiod   bfd_byte *fill;
26633d8817e4Smiod   file_ptr loc;
26643d8817e4Smiod   bfd_boolean result;
26653d8817e4Smiod 
26663d8817e4Smiod   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
26673d8817e4Smiod 
26683d8817e4Smiod   size = link_order->size;
26693d8817e4Smiod   if (size == 0)
26703d8817e4Smiod     return TRUE;
26713d8817e4Smiod 
26723d8817e4Smiod   fill = link_order->u.data.contents;
26733d8817e4Smiod   fill_size = link_order->u.data.size;
26743d8817e4Smiod   if (fill_size != 0 && fill_size < size)
26753d8817e4Smiod     {
26763d8817e4Smiod       bfd_byte *p;
26773d8817e4Smiod       fill = bfd_malloc (size);
26783d8817e4Smiod       if (fill == NULL)
26793d8817e4Smiod 	return FALSE;
26803d8817e4Smiod       p = fill;
26813d8817e4Smiod       if (fill_size == 1)
26823d8817e4Smiod 	memset (p, (int) link_order->u.data.contents[0], (size_t) size);
26833d8817e4Smiod       else
26843d8817e4Smiod 	{
26853d8817e4Smiod 	  do
26863d8817e4Smiod 	    {
26873d8817e4Smiod 	      memcpy (p, link_order->u.data.contents, fill_size);
26883d8817e4Smiod 	      p += fill_size;
26893d8817e4Smiod 	      size -= fill_size;
26903d8817e4Smiod 	    }
26913d8817e4Smiod 	  while (size >= fill_size);
26923d8817e4Smiod 	  if (size != 0)
26933d8817e4Smiod 	    memcpy (p, link_order->u.data.contents, (size_t) size);
26943d8817e4Smiod 	  size = link_order->size;
26953d8817e4Smiod 	}
26963d8817e4Smiod     }
26973d8817e4Smiod 
26983d8817e4Smiod   loc = link_order->offset * bfd_octets_per_byte (abfd);
26993d8817e4Smiod   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
27003d8817e4Smiod 
27013d8817e4Smiod   if (fill != link_order->u.data.contents)
27023d8817e4Smiod     free (fill);
27033d8817e4Smiod   return result;
27043d8817e4Smiod }
27053d8817e4Smiod 
27063d8817e4Smiod /* Default routine to handle a bfd_indirect_link_order.  */
27073d8817e4Smiod 
27083d8817e4Smiod 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)27093d8817e4Smiod default_indirect_link_order (bfd *output_bfd,
27103d8817e4Smiod 			     struct bfd_link_info *info,
27113d8817e4Smiod 			     asection *output_section,
27123d8817e4Smiod 			     struct bfd_link_order *link_order,
27133d8817e4Smiod 			     bfd_boolean generic_linker)
27143d8817e4Smiod {
27153d8817e4Smiod   asection *input_section;
27163d8817e4Smiod   bfd *input_bfd;
27173d8817e4Smiod   bfd_byte *contents = NULL;
27183d8817e4Smiod   bfd_byte *new_contents;
27193d8817e4Smiod   bfd_size_type sec_size;
27203d8817e4Smiod   file_ptr loc;
27213d8817e4Smiod 
27223d8817e4Smiod   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
27233d8817e4Smiod 
27243d8817e4Smiod   input_section = link_order->u.indirect.section;
27253d8817e4Smiod   input_bfd = input_section->owner;
27263d8817e4Smiod   if (input_section->size == 0)
27273d8817e4Smiod     return TRUE;
27283d8817e4Smiod 
27293d8817e4Smiod   BFD_ASSERT (input_section->output_section == output_section);
27303d8817e4Smiod   BFD_ASSERT (input_section->output_offset == link_order->offset);
27313d8817e4Smiod   BFD_ASSERT (input_section->size == link_order->size);
27323d8817e4Smiod 
27333d8817e4Smiod   if (info->relocatable
27343d8817e4Smiod       && input_section->reloc_count > 0
27353d8817e4Smiod       && output_section->orelocation == NULL)
27363d8817e4Smiod     {
27373d8817e4Smiod       /* Space has not been allocated for the output relocations.
27383d8817e4Smiod 	 This can happen when we are called by a specific backend
27393d8817e4Smiod 	 because somebody is attempting to link together different
27403d8817e4Smiod 	 types of object files.  Handling this case correctly is
27413d8817e4Smiod 	 difficult, and sometimes impossible.  */
27423d8817e4Smiod       (*_bfd_error_handler)
27433d8817e4Smiod 	(_("Attempt to do relocatable link with %s input and %s output"),
27443d8817e4Smiod 	 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
27453d8817e4Smiod       bfd_set_error (bfd_error_wrong_format);
27463d8817e4Smiod       return FALSE;
27473d8817e4Smiod     }
27483d8817e4Smiod 
27493d8817e4Smiod   if (! generic_linker)
27503d8817e4Smiod     {
27513d8817e4Smiod       asymbol **sympp;
27523d8817e4Smiod       asymbol **symppend;
27533d8817e4Smiod 
27543d8817e4Smiod       /* Get the canonical symbols.  The generic linker will always
27553d8817e4Smiod 	 have retrieved them by this point, but we are being called by
27563d8817e4Smiod 	 a specific linker, presumably because we are linking
27573d8817e4Smiod 	 different types of object files together.  */
27583d8817e4Smiod       if (! generic_link_read_symbols (input_bfd))
27593d8817e4Smiod 	return FALSE;
27603d8817e4Smiod 
27613d8817e4Smiod       /* Since we have been called by a specific linker, rather than
27623d8817e4Smiod 	 the generic linker, the values of the symbols will not be
27633d8817e4Smiod 	 right.  They will be the values as seen in the input file,
27643d8817e4Smiod 	 not the values of the final link.  We need to fix them up
27653d8817e4Smiod 	 before we can relocate the section.  */
27663d8817e4Smiod       sympp = _bfd_generic_link_get_symbols (input_bfd);
27673d8817e4Smiod       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
27683d8817e4Smiod       for (; sympp < symppend; sympp++)
27693d8817e4Smiod 	{
27703d8817e4Smiod 	  asymbol *sym;
27713d8817e4Smiod 	  struct bfd_link_hash_entry *h;
27723d8817e4Smiod 
27733d8817e4Smiod 	  sym = *sympp;
27743d8817e4Smiod 
27753d8817e4Smiod 	  if ((sym->flags & (BSF_INDIRECT
27763d8817e4Smiod 			     | BSF_WARNING
27773d8817e4Smiod 			     | BSF_GLOBAL
27783d8817e4Smiod 			     | BSF_CONSTRUCTOR
27793d8817e4Smiod 			     | BSF_WEAK)) != 0
27803d8817e4Smiod 	      || bfd_is_und_section (bfd_get_section (sym))
27813d8817e4Smiod 	      || bfd_is_com_section (bfd_get_section (sym))
27823d8817e4Smiod 	      || bfd_is_ind_section (bfd_get_section (sym)))
27833d8817e4Smiod 	    {
27843d8817e4Smiod 	      /* sym->udata may have been set by
27853d8817e4Smiod 		 generic_link_add_symbol_list.  */
27863d8817e4Smiod 	      if (sym->udata.p != NULL)
27873d8817e4Smiod 		h = sym->udata.p;
27883d8817e4Smiod 	      else if (bfd_is_und_section (bfd_get_section (sym)))
27893d8817e4Smiod 		h = bfd_wrapped_link_hash_lookup (output_bfd, info,
27903d8817e4Smiod 						  bfd_asymbol_name (sym),
27913d8817e4Smiod 						  FALSE, FALSE, TRUE);
27923d8817e4Smiod 	      else
27933d8817e4Smiod 		h = bfd_link_hash_lookup (info->hash,
27943d8817e4Smiod 					  bfd_asymbol_name (sym),
27953d8817e4Smiod 					  FALSE, FALSE, TRUE);
27963d8817e4Smiod 	      if (h != NULL)
27973d8817e4Smiod 		set_symbol_from_hash (sym, h);
27983d8817e4Smiod 	    }
27993d8817e4Smiod 	}
28003d8817e4Smiod     }
28013d8817e4Smiod 
28023d8817e4Smiod   /* Get and relocate the section contents.  */
28033d8817e4Smiod   sec_size = (input_section->rawsize > input_section->size
28043d8817e4Smiod 	      ? input_section->rawsize
28053d8817e4Smiod 	      : input_section->size);
28063d8817e4Smiod   contents = bfd_malloc (sec_size);
28073d8817e4Smiod   if (contents == NULL && sec_size != 0)
28083d8817e4Smiod     goto error_return;
28093d8817e4Smiod   new_contents = (bfd_get_relocated_section_contents
28103d8817e4Smiod 		  (output_bfd, info, link_order, contents, info->relocatable,
28113d8817e4Smiod 		   _bfd_generic_link_get_symbols (input_bfd)));
28123d8817e4Smiod   if (!new_contents)
28133d8817e4Smiod     goto error_return;
28143d8817e4Smiod 
28153d8817e4Smiod   /* Output the section contents.  */
28163d8817e4Smiod   loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
28173d8817e4Smiod   if (! bfd_set_section_contents (output_bfd, output_section,
28183d8817e4Smiod 				  new_contents, loc, input_section->size))
28193d8817e4Smiod     goto error_return;
28203d8817e4Smiod 
28213d8817e4Smiod   if (contents != NULL)
28223d8817e4Smiod     free (contents);
28233d8817e4Smiod   return TRUE;
28243d8817e4Smiod 
28253d8817e4Smiod  error_return:
28263d8817e4Smiod   if (contents != NULL)
28273d8817e4Smiod     free (contents);
28283d8817e4Smiod   return FALSE;
28293d8817e4Smiod }
28303d8817e4Smiod 
28313d8817e4Smiod /* A little routine to count the number of relocs in a link_order
28323d8817e4Smiod    list.  */
28333d8817e4Smiod 
28343d8817e4Smiod unsigned int
_bfd_count_link_order_relocs(struct bfd_link_order * link_order)28353d8817e4Smiod _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
28363d8817e4Smiod {
28373d8817e4Smiod   register unsigned int c;
28383d8817e4Smiod   register struct bfd_link_order *l;
28393d8817e4Smiod 
28403d8817e4Smiod   c = 0;
28413d8817e4Smiod   for (l = link_order; l != NULL; l = l->next)
28423d8817e4Smiod     {
28433d8817e4Smiod       if (l->type == bfd_section_reloc_link_order
28443d8817e4Smiod 	  || l->type == bfd_symbol_reloc_link_order)
28453d8817e4Smiod 	++c;
28463d8817e4Smiod     }
28473d8817e4Smiod 
28483d8817e4Smiod   return c;
28493d8817e4Smiod }
28503d8817e4Smiod 
28513d8817e4Smiod /*
28523d8817e4Smiod FUNCTION
28533d8817e4Smiod 	bfd_link_split_section
28543d8817e4Smiod 
28553d8817e4Smiod SYNOPSIS
28563d8817e4Smiod         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
28573d8817e4Smiod 
28583d8817e4Smiod DESCRIPTION
28593d8817e4Smiod 	Return nonzero if @var{sec} should be split during a
28603d8817e4Smiod 	reloceatable or final link.
28613d8817e4Smiod 
28623d8817e4Smiod .#define bfd_link_split_section(abfd, sec) \
28633d8817e4Smiod .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
28643d8817e4Smiod .
28653d8817e4Smiod 
28663d8817e4Smiod */
28673d8817e4Smiod 
28683d8817e4Smiod bfd_boolean
_bfd_generic_link_split_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)28693d8817e4Smiod _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
28703d8817e4Smiod 				 asection *sec ATTRIBUTE_UNUSED)
28713d8817e4Smiod {
28723d8817e4Smiod   return FALSE;
28733d8817e4Smiod }
28743d8817e4Smiod 
28753d8817e4Smiod /*
28763d8817e4Smiod FUNCTION
28773d8817e4Smiod 	bfd_section_already_linked
28783d8817e4Smiod 
28793d8817e4Smiod SYNOPSIS
2880*b8417449Sstefan         void bfd_section_already_linked (bfd *abfd, asection *sec,
2881*b8417449Sstefan 					 struct bfd_link_info *info);
28823d8817e4Smiod 
28833d8817e4Smiod DESCRIPTION
28843d8817e4Smiod 	Check if @var{sec} has been already linked during a reloceatable
28853d8817e4Smiod 	or final link.
28863d8817e4Smiod 
2887*b8417449Sstefan .#define bfd_section_already_linked(abfd, sec, info) \
2888*b8417449Sstefan .       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
28893d8817e4Smiod .
28903d8817e4Smiod 
28913d8817e4Smiod */
28923d8817e4Smiod 
28933d8817e4Smiod /* Sections marked with the SEC_LINK_ONCE flag should only be linked
28943d8817e4Smiod    once into the output.  This routine checks each section, and
28953d8817e4Smiod    arrange to discard it if a section of the same name has already
28963d8817e4Smiod    been linked.  This code assumes that all relevant sections have the
28973d8817e4Smiod    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
28983d8817e4Smiod    section name.  bfd_section_already_linked is called via
28993d8817e4Smiod    bfd_map_over_sections.  */
29003d8817e4Smiod 
29013d8817e4Smiod /* The hash table.  */
29023d8817e4Smiod 
29033d8817e4Smiod static struct bfd_hash_table _bfd_section_already_linked_table;
29043d8817e4Smiod 
29053d8817e4Smiod /* Support routines for the hash table used by section_already_linked,
29063d8817e4Smiod    initialize the table, traverse, lookup, fill in an entry and remove
29073d8817e4Smiod    the table.  */
29083d8817e4Smiod 
29093d8817e4Smiod void
bfd_section_already_linked_table_traverse(bfd_boolean (* func)(struct bfd_section_already_linked_hash_entry *,void *),void * info)29103d8817e4Smiod bfd_section_already_linked_table_traverse
29113d8817e4Smiod   (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
29123d8817e4Smiod 			void *), void *info)
29133d8817e4Smiod {
29143d8817e4Smiod   bfd_hash_traverse (&_bfd_section_already_linked_table,
29153d8817e4Smiod 		     (bfd_boolean (*) (struct bfd_hash_entry *,
29163d8817e4Smiod 				       void *)) func,
29173d8817e4Smiod 		     info);
29183d8817e4Smiod }
29193d8817e4Smiod 
29203d8817e4Smiod struct bfd_section_already_linked_hash_entry *
bfd_section_already_linked_table_lookup(const char * name)29213d8817e4Smiod bfd_section_already_linked_table_lookup (const char *name)
29223d8817e4Smiod {
29233d8817e4Smiod   return ((struct bfd_section_already_linked_hash_entry *)
29243d8817e4Smiod 	  bfd_hash_lookup (&_bfd_section_already_linked_table, name,
29253d8817e4Smiod 			   TRUE, FALSE));
29263d8817e4Smiod }
29273d8817e4Smiod 
29283d8817e4Smiod void
bfd_section_already_linked_table_insert(struct bfd_section_already_linked_hash_entry * already_linked_list,asection * sec)29293d8817e4Smiod bfd_section_already_linked_table_insert
29303d8817e4Smiod   (struct bfd_section_already_linked_hash_entry *already_linked_list,
29313d8817e4Smiod    asection *sec)
29323d8817e4Smiod {
29333d8817e4Smiod   struct bfd_section_already_linked *l;
29343d8817e4Smiod 
29353d8817e4Smiod   /* Allocate the memory from the same obstack as the hash table is
29363d8817e4Smiod      kept in.  */
29373d8817e4Smiod   l = bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
29383d8817e4Smiod   l->sec = sec;
29393d8817e4Smiod   l->next = already_linked_list->entry;
29403d8817e4Smiod   already_linked_list->entry = l;
29413d8817e4Smiod }
29423d8817e4Smiod 
29433d8817e4Smiod static struct bfd_hash_entry *
already_linked_newfunc(struct bfd_hash_entry * entry ATTRIBUTE_UNUSED,struct bfd_hash_table * table,const char * string ATTRIBUTE_UNUSED)29443d8817e4Smiod already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
29453d8817e4Smiod 			struct bfd_hash_table *table,
29463d8817e4Smiod 			const char *string ATTRIBUTE_UNUSED)
29473d8817e4Smiod {
29483d8817e4Smiod   struct bfd_section_already_linked_hash_entry *ret =
29493d8817e4Smiod     bfd_hash_allocate (table, sizeof *ret);
29503d8817e4Smiod 
29513d8817e4Smiod   ret->entry = NULL;
29523d8817e4Smiod 
29533d8817e4Smiod   return &ret->root;
29543d8817e4Smiod }
29553d8817e4Smiod 
29563d8817e4Smiod bfd_boolean
bfd_section_already_linked_table_init(void)29573d8817e4Smiod bfd_section_already_linked_table_init (void)
29583d8817e4Smiod {
29593d8817e4Smiod   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
29603d8817e4Smiod 				already_linked_newfunc,
29613d8817e4Smiod 				sizeof (struct bfd_section_already_linked_hash_entry),
29623d8817e4Smiod 				42);
29633d8817e4Smiod }
29643d8817e4Smiod 
29653d8817e4Smiod void
bfd_section_already_linked_table_free(void)29663d8817e4Smiod bfd_section_already_linked_table_free (void)
29673d8817e4Smiod {
29683d8817e4Smiod   bfd_hash_table_free (&_bfd_section_already_linked_table);
29693d8817e4Smiod }
29703d8817e4Smiod 
29713d8817e4Smiod /* This is used on non-ELF inputs.  */
29723d8817e4Smiod 
29733d8817e4Smiod void
_bfd_generic_section_already_linked(bfd * abfd,asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED)2974*b8417449Sstefan _bfd_generic_section_already_linked (bfd *abfd, asection *sec,
2975*b8417449Sstefan 				     struct bfd_link_info *info ATTRIBUTE_UNUSED)
29763d8817e4Smiod {
29773d8817e4Smiod   flagword flags;
29783d8817e4Smiod   const char *name;
29793d8817e4Smiod   struct bfd_section_already_linked *l;
29803d8817e4Smiod   struct bfd_section_already_linked_hash_entry *already_linked_list;
29813d8817e4Smiod 
29823d8817e4Smiod   flags = sec->flags;
29833d8817e4Smiod   if ((flags & SEC_LINK_ONCE) == 0)
29843d8817e4Smiod     return;
29853d8817e4Smiod 
29863d8817e4Smiod   /* FIXME: When doing a relocatable link, we may have trouble
29873d8817e4Smiod      copying relocations in other sections that refer to local symbols
29883d8817e4Smiod      in the section being discarded.  Those relocations will have to
29893d8817e4Smiod      be converted somehow; as of this writing I'm not sure that any of
29903d8817e4Smiod      the backends handle that correctly.
29913d8817e4Smiod 
29923d8817e4Smiod      It is tempting to instead not discard link once sections when
29933d8817e4Smiod      doing a relocatable link (technically, they should be discarded
29943d8817e4Smiod      whenever we are building constructors).  However, that fails,
29953d8817e4Smiod      because the linker winds up combining all the link once sections
29963d8817e4Smiod      into a single large link once section, which defeats the purpose
29973d8817e4Smiod      of having link once sections in the first place.  */
29983d8817e4Smiod 
29993d8817e4Smiod   name = bfd_get_section_name (abfd, sec);
30003d8817e4Smiod 
30013d8817e4Smiod   already_linked_list = bfd_section_already_linked_table_lookup (name);
30023d8817e4Smiod 
30033d8817e4Smiod   for (l = already_linked_list->entry; l != NULL; l = l->next)
30043d8817e4Smiod     {
30053d8817e4Smiod       bfd_boolean skip = FALSE;
30063d8817e4Smiod       struct coff_comdat_info *s_comdat
30073d8817e4Smiod 	= bfd_coff_get_comdat_section (abfd, sec);
30083d8817e4Smiod       struct coff_comdat_info *l_comdat
30093d8817e4Smiod 	= bfd_coff_get_comdat_section (l->sec->owner, l->sec);
30103d8817e4Smiod 
30113d8817e4Smiod       /* We may have 3 different sections on the list: group section,
30123d8817e4Smiod 	 comdat section and linkonce section. SEC may be a linkonce or
30133d8817e4Smiod 	 comdat section. We always ignore group section. For non-COFF
30143d8817e4Smiod 	 inputs, we also ignore comdat section.
30153d8817e4Smiod 
30163d8817e4Smiod 	 FIXME: Is that safe to match a linkonce section with a comdat
30173d8817e4Smiod 	 section for COFF inputs?  */
30183d8817e4Smiod       if ((l->sec->flags & SEC_GROUP) != 0)
30193d8817e4Smiod 	skip = TRUE;
30203d8817e4Smiod       else if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
30213d8817e4Smiod 	{
30223d8817e4Smiod 	  if (s_comdat != NULL
30233d8817e4Smiod 	      && l_comdat != NULL
30243d8817e4Smiod 	      && strcmp (s_comdat->name, l_comdat->name) != 0)
30253d8817e4Smiod 	    skip = TRUE;
30263d8817e4Smiod 	}
30273d8817e4Smiod       else if (l_comdat != NULL)
30283d8817e4Smiod 	skip = TRUE;
30293d8817e4Smiod 
30303d8817e4Smiod       if (!skip)
30313d8817e4Smiod 	{
30323d8817e4Smiod 	  /* The section has already been linked.  See if we should
30333d8817e4Smiod              issue a warning.  */
30343d8817e4Smiod 	  switch (flags & SEC_LINK_DUPLICATES)
30353d8817e4Smiod 	    {
30363d8817e4Smiod 	    default:
30373d8817e4Smiod 	      abort ();
30383d8817e4Smiod 
30393d8817e4Smiod 	    case SEC_LINK_DUPLICATES_DISCARD:
30403d8817e4Smiod 	      break;
30413d8817e4Smiod 
30423d8817e4Smiod 	    case SEC_LINK_DUPLICATES_ONE_ONLY:
30433d8817e4Smiod 	      (*_bfd_error_handler)
30443d8817e4Smiod 		(_("%B: warning: ignoring duplicate section `%A'\n"),
30453d8817e4Smiod 		 abfd, sec);
30463d8817e4Smiod 	      break;
30473d8817e4Smiod 
30483d8817e4Smiod 	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
30493d8817e4Smiod 	      /* FIXME: We should really dig out the contents of both
30503d8817e4Smiod                  sections and memcmp them.  The COFF/PE spec says that
30513d8817e4Smiod                  the Microsoft linker does not implement this
30523d8817e4Smiod                  correctly, so I'm not going to bother doing it
30533d8817e4Smiod                  either.  */
30543d8817e4Smiod 	      /* Fall through.  */
30553d8817e4Smiod 	    case SEC_LINK_DUPLICATES_SAME_SIZE:
30563d8817e4Smiod 	      if (sec->size != l->sec->size)
30573d8817e4Smiod 		(*_bfd_error_handler)
30583d8817e4Smiod 		  (_("%B: warning: duplicate section `%A' has different size\n"),
30593d8817e4Smiod 		   abfd, sec);
30603d8817e4Smiod 	      break;
30613d8817e4Smiod 	    }
30623d8817e4Smiod 
30633d8817e4Smiod 	  /* Set the output_section field so that lang_add_section
30643d8817e4Smiod 	     does not create a lang_input_section structure for this
30653d8817e4Smiod 	     section.  Since there might be a symbol in the section
30663d8817e4Smiod 	     being discarded, we must retain a pointer to the section
30673d8817e4Smiod 	     which we are really going to use.  */
30683d8817e4Smiod 	  sec->output_section = bfd_abs_section_ptr;
30693d8817e4Smiod 	  sec->kept_section = l->sec;
30703d8817e4Smiod 
30713d8817e4Smiod 	  return;
30723d8817e4Smiod 	}
30733d8817e4Smiod     }
30743d8817e4Smiod 
30753d8817e4Smiod   /* This is the first section with this name.  Record it.  */
30763d8817e4Smiod   bfd_section_already_linked_table_insert (already_linked_list, sec);
30773d8817e4Smiod }
30783d8817e4Smiod 
30793d8817e4Smiod /* Convert symbols in excluded output sections to absolute.  */
30803d8817e4Smiod 
30813d8817e4Smiod static bfd_boolean
fix_syms(struct bfd_link_hash_entry * h,void * data)30823d8817e4Smiod fix_syms (struct bfd_link_hash_entry *h, void *data)
30833d8817e4Smiod {
30843d8817e4Smiod   bfd *obfd = (bfd *) data;
30853d8817e4Smiod 
30863d8817e4Smiod   if (h->type == bfd_link_hash_warning)
30873d8817e4Smiod     h = h->u.i.link;
30883d8817e4Smiod 
30893d8817e4Smiod   if (h->type == bfd_link_hash_defined
30903d8817e4Smiod       || h->type == bfd_link_hash_defweak)
30913d8817e4Smiod     {
30923d8817e4Smiod       asection *s = h->u.def.section;
30933d8817e4Smiod       if (s != NULL
30943d8817e4Smiod 	  && s->output_section != NULL
30953d8817e4Smiod 	  && (s->output_section->flags & SEC_EXCLUDE) != 0
30963d8817e4Smiod 	  && bfd_section_removed_from_list (obfd, s->output_section))
30973d8817e4Smiod 	{
30983d8817e4Smiod 	  h->u.def.value += s->output_offset + s->output_section->vma;
30993d8817e4Smiod 	  h->u.def.section = bfd_abs_section_ptr;
31003d8817e4Smiod 	}
31013d8817e4Smiod     }
31023d8817e4Smiod 
31033d8817e4Smiod   return TRUE;
31043d8817e4Smiod }
31053d8817e4Smiod 
31063d8817e4Smiod void
_bfd_fix_excluded_sec_syms(bfd * obfd,struct bfd_link_info * info)31073d8817e4Smiod _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
31083d8817e4Smiod {
31093d8817e4Smiod   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
31103d8817e4Smiod }
3111