133965Sjdp/* linker.c -- BFD linker routines
2130561Sobrien   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3218822Sdim   2003, 2004, 2005, 2006, 2007
4218822Sdim   Free Software Foundation, Inc.
533965Sjdp   Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
633965Sjdp
7130561Sobrien   This file is part of BFD, the Binary File Descriptor library.
833965Sjdp
9130561Sobrien   This program is free software; you can redistribute it and/or modify
10130561Sobrien   it under the terms of the GNU General Public License as published by
11130561Sobrien   the Free Software Foundation; either version 2 of the License, or
12130561Sobrien   (at your option) any later version.
1333965Sjdp
14130561Sobrien   This program is distributed in the hope that it will be useful,
15130561Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
16130561Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17130561Sobrien   GNU General Public License for more details.
1833965Sjdp
19130561Sobrien   You should have received a copy of the GNU General Public License
20130561Sobrien   along with this program; if not, write to the Free Software
21218822Sdim   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2233965Sjdp
23218822Sdim#include "sysdep.h"
2433965Sjdp#include "bfd.h"
2533965Sjdp#include "libbfd.h"
2633965Sjdp#include "bfdlink.h"
2733965Sjdp#include "genlink.h"
2833965Sjdp
2933965Sjdp/*
3033965SjdpSECTION
3133965Sjdp	Linker Functions
3233965Sjdp
3333965Sjdp@cindex Linker
3433965Sjdp	The linker uses three special entry points in the BFD target
3533965Sjdp	vector.  It is not necessary to write special routines for
3633965Sjdp	these entry points when creating a new BFD back end, since
3733965Sjdp	generic versions are provided.  However, writing them can
3833965Sjdp	speed up linking and make it use significantly less runtime
3933965Sjdp	memory.
4033965Sjdp
4133965Sjdp	The first routine creates a hash table used by the other
4233965Sjdp	routines.  The second routine adds the symbols from an object
4333965Sjdp	file to the hash table.  The third routine takes all the
4433965Sjdp	object files and links them together to create the output
4533965Sjdp	file.  These routines are designed so that the linker proper
4633965Sjdp	does not need to know anything about the symbols in the object
4733965Sjdp	files that it is linking.  The linker merely arranges the
4833965Sjdp	sections as directed by the linker script and lets BFD handle
4933965Sjdp	the details of symbols and relocs.
5033965Sjdp
5133965Sjdp	The second routine and third routines are passed a pointer to
5233965Sjdp	a <<struct bfd_link_info>> structure (defined in
5333965Sjdp	<<bfdlink.h>>) which holds information relevant to the link,
5433965Sjdp	including the linker hash table (which was created by the
5533965Sjdp	first routine) and a set of callback functions to the linker
5633965Sjdp	proper.
5733965Sjdp
5833965Sjdp	The generic linker routines are in <<linker.c>>, and use the
5933965Sjdp	header file <<genlink.h>>.  As of this writing, the only back
6033965Sjdp	ends which have implemented versions of these routines are
6133965Sjdp	a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
6233965Sjdp	routines are used as examples throughout this section.
6333965Sjdp
6477298Sobrien@menu
6533965Sjdp@* Creating a Linker Hash Table::
6633965Sjdp@* Adding Symbols to the Hash Table::
6733965Sjdp@* Performing the Final Link::
6833965Sjdp@end menu
6933965Sjdp
7033965SjdpINODE
7133965SjdpCreating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
7233965SjdpSUBSECTION
7333965Sjdp	Creating a linker hash table
7433965Sjdp
7533965Sjdp@cindex _bfd_link_hash_table_create in target vector
7633965Sjdp@cindex target vector (_bfd_link_hash_table_create)
7733965Sjdp	The linker routines must create a hash table, which must be
7833965Sjdp	derived from <<struct bfd_link_hash_table>> described in
7960484Sobrien	<<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
8033965Sjdp	create a derived hash table.  This entry point is called using
8133965Sjdp	the target vector of the linker output file.
8233965Sjdp
8333965Sjdp	The <<_bfd_link_hash_table_create>> entry point must allocate
8433965Sjdp	and initialize an instance of the desired hash table.  If the
8533965Sjdp	back end does not require any additional information to be
8633965Sjdp	stored with the entries in the hash table, the entry point may
8733965Sjdp	simply create a <<struct bfd_link_hash_table>>.  Most likely,
8833965Sjdp	however, some additional information will be needed.
8933965Sjdp
9033965Sjdp	For example, with each entry in the hash table the a.out
9133965Sjdp	linker keeps the index the symbol has in the final output file
92130561Sobrien	(this index number is used so that when doing a relocatable
9333965Sjdp	link the symbol index used in the output file can be quickly
9433965Sjdp	filled in when copying over a reloc).  The a.out linker code
9533965Sjdp	defines the required structures and functions for a hash table
9633965Sjdp	derived from <<struct bfd_link_hash_table>>.  The a.out linker
9733965Sjdp	hash table is created by the function
9833965Sjdp	<<NAME(aout,link_hash_table_create)>>; it simply allocates
9933965Sjdp	space for the hash table, initializes it, and returns a
10033965Sjdp	pointer to it.
10133965Sjdp
10233965Sjdp	When writing the linker routines for a new back end, you will
10333965Sjdp	generally not know exactly which fields will be required until
10433965Sjdp	you have finished.  You should simply create a new hash table
10533965Sjdp	which defines no additional fields, and then simply add fields
10633965Sjdp	as they become necessary.
10733965Sjdp
10833965SjdpINODE
10933965SjdpAdding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
11033965SjdpSUBSECTION
11133965Sjdp	Adding symbols to the hash table
11233965Sjdp
11333965Sjdp@cindex _bfd_link_add_symbols in target vector
11433965Sjdp@cindex target vector (_bfd_link_add_symbols)
11533965Sjdp	The linker proper will call the <<_bfd_link_add_symbols>>
11633965Sjdp	entry point for each object file or archive which is to be
11733965Sjdp	linked (typically these are the files named on the command
11833965Sjdp	line, but some may also come from the linker script).  The
11933965Sjdp	entry point is responsible for examining the file.  For an
12033965Sjdp	object file, BFD must add any relevant symbol information to
12133965Sjdp	the hash table.  For an archive, BFD must determine which
12233965Sjdp	elements of the archive should be used and adding them to the
12333965Sjdp	link.
12433965Sjdp
12533965Sjdp	The a.out version of this entry point is
12633965Sjdp	<<NAME(aout,link_add_symbols)>>.
12733965Sjdp
12833965Sjdp@menu
12933965Sjdp@* Differing file formats::
13033965Sjdp@* Adding symbols from an object file::
13133965Sjdp@* Adding symbols from an archive::
13233965Sjdp@end menu
13333965Sjdp
13433965SjdpINODE
13533965SjdpDiffering file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
13633965SjdpSUBSUBSECTION
13733965Sjdp	Differing file formats
13833965Sjdp
13933965Sjdp	Normally all the files involved in a link will be of the same
14033965Sjdp	format, but it is also possible to link together different
14133965Sjdp	format object files, and the back end must support that.  The
14233965Sjdp	<<_bfd_link_add_symbols>> entry point is called via the target
14333965Sjdp	vector of the file to be added.  This has an important
14433965Sjdp	consequence: the function may not assume that the hash table
14533965Sjdp	is the type created by the corresponding
14633965Sjdp	<<_bfd_link_hash_table_create>> vector.  All the
14733965Sjdp	<<_bfd_link_add_symbols>> function can assume about the hash
14833965Sjdp	table is that it is derived from <<struct
14933965Sjdp	bfd_link_hash_table>>.
15033965Sjdp
15133965Sjdp	Sometimes the <<_bfd_link_add_symbols>> function must store
15233965Sjdp	some information in the hash table entry to be used by the
15333965Sjdp	<<_bfd_final_link>> function.  In such a case the <<creator>>
15433965Sjdp	field of the hash table must be checked to make sure that the
15533965Sjdp	hash table was created by an object file of the same format.
15633965Sjdp
15733965Sjdp	The <<_bfd_final_link>> routine must be prepared to handle a
15833965Sjdp	hash entry without any extra information added by the
15933965Sjdp	<<_bfd_link_add_symbols>> function.  A hash entry without
16033965Sjdp	extra information will also occur when the linker script
16133965Sjdp	directs the linker to create a symbol.  Note that, regardless
16233965Sjdp	of how a hash table entry is added, all the fields will be
16333965Sjdp	initialized to some sort of null value by the hash table entry
16433965Sjdp	initialization function.
16533965Sjdp
16633965Sjdp	See <<ecoff_link_add_externals>> for an example of how to
16733965Sjdp	check the <<creator>> field before saving information (in this
16833965Sjdp	case, the ECOFF external symbol debugging information) in a
16933965Sjdp	hash table entry.
17033965Sjdp
17133965SjdpINODE
17233965SjdpAdding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
17333965SjdpSUBSUBSECTION
17433965Sjdp	Adding symbols from an object file
17533965Sjdp
17633965Sjdp	When the <<_bfd_link_add_symbols>> routine is passed an object
17733965Sjdp	file, it must add all externally visible symbols in that
17833965Sjdp	object file to the hash table.  The actual work of adding the
17933965Sjdp	symbol to the hash table is normally handled by the function
18033965Sjdp	<<_bfd_generic_link_add_one_symbol>>.  The
18133965Sjdp	<<_bfd_link_add_symbols>> routine is responsible for reading
18233965Sjdp	all the symbols from the object file and passing the correct
18333965Sjdp	information to <<_bfd_generic_link_add_one_symbol>>.
18433965Sjdp
18533965Sjdp	The <<_bfd_link_add_symbols>> routine should not use
18633965Sjdp	<<bfd_canonicalize_symtab>> to read the symbols.  The point of
18733965Sjdp	providing this routine is to avoid the overhead of converting
18833965Sjdp	the symbols into generic <<asymbol>> structures.
18933965Sjdp
19033965Sjdp@findex _bfd_generic_link_add_one_symbol
19133965Sjdp	<<_bfd_generic_link_add_one_symbol>> handles the details of
19233965Sjdp	combining common symbols, warning about multiple definitions,
19333965Sjdp	and so forth.  It takes arguments which describe the symbol to
19433965Sjdp	add, notably symbol flags, a section, and an offset.  The
19533965Sjdp	symbol flags include such things as <<BSF_WEAK>> or
19633965Sjdp	<<BSF_INDIRECT>>.  The section is a section in the object
19733965Sjdp	file, or something like <<bfd_und_section_ptr>> for an undefined
19833965Sjdp	symbol or <<bfd_com_section_ptr>> for a common symbol.
19933965Sjdp
20033965Sjdp	If the <<_bfd_final_link>> routine is also going to need to
20133965Sjdp	read the symbol information, the <<_bfd_link_add_symbols>>
20233965Sjdp	routine should save it somewhere attached to the object file
20333965Sjdp	BFD.  However, the information should only be saved if the
204130561Sobrien	<<keep_memory>> field of the <<info>> argument is TRUE, so
20533965Sjdp	that the <<-no-keep-memory>> linker switch is effective.
20633965Sjdp
20733965Sjdp	The a.out function which adds symbols from an object file is
20833965Sjdp	<<aout_link_add_object_symbols>>, and most of the interesting
20933965Sjdp	work is in <<aout_link_add_symbols>>.  The latter saves
21033965Sjdp	pointers to the hash tables entries created by
21133965Sjdp	<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
21233965Sjdp	so that the <<_bfd_final_link>> routine does not have to call
21333965Sjdp	the hash table lookup routine to locate the entry.
21433965Sjdp
21533965SjdpINODE
21633965SjdpAdding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
21733965SjdpSUBSUBSECTION
21833965Sjdp	Adding symbols from an archive
21933965Sjdp
22033965Sjdp	When the <<_bfd_link_add_symbols>> routine is passed an
22133965Sjdp	archive, it must look through the symbols defined by the
22233965Sjdp	archive and decide which elements of the archive should be
22333965Sjdp	included in the link.  For each such element it must call the
22433965Sjdp	<<add_archive_element>> linker callback, and it must add the
22533965Sjdp	symbols from the object file to the linker hash table.
22633965Sjdp
22733965Sjdp@findex _bfd_generic_link_add_archive_symbols
22833965Sjdp	In most cases the work of looking through the symbols in the
22933965Sjdp	archive should be done by the
23033965Sjdp	<<_bfd_generic_link_add_archive_symbols>> function.  This
23133965Sjdp	function builds a hash table from the archive symbol table and
23233965Sjdp	looks through the list of undefined symbols to see which
23333965Sjdp	elements should be included.
23433965Sjdp	<<_bfd_generic_link_add_archive_symbols>> is passed a function
23533965Sjdp	to call to make the final decision about adding an archive
23633965Sjdp	element to the link and to do the actual work of adding the
23733965Sjdp	symbols to the linker hash table.
23833965Sjdp
23933965Sjdp	The function passed to
24033965Sjdp	<<_bfd_generic_link_add_archive_symbols>> must read the
24133965Sjdp	symbols of the archive element and decide whether the archive
24233965Sjdp	element should be included in the link.  If the element is to
24333965Sjdp	be included, the <<add_archive_element>> linker callback
24433965Sjdp	routine must be called with the element as an argument, and
24533965Sjdp	the elements symbols must be added to the linker hash table
24633965Sjdp	just as though the element had itself been passed to the
24733965Sjdp	<<_bfd_link_add_symbols>> function.
24833965Sjdp
24933965Sjdp	When the a.out <<_bfd_link_add_symbols>> function receives an
25033965Sjdp	archive, it calls <<_bfd_generic_link_add_archive_symbols>>
25133965Sjdp	passing <<aout_link_check_archive_element>> as the function
25233965Sjdp	argument. <<aout_link_check_archive_element>> calls
25333965Sjdp	<<aout_link_check_ar_symbols>>.  If the latter decides to add
25433965Sjdp	the element (an element is only added if it provides a real,
25533965Sjdp	non-common, definition for a previously undefined or common
25633965Sjdp	symbol) it calls the <<add_archive_element>> callback and then
25733965Sjdp	<<aout_link_check_archive_element>> calls
25833965Sjdp	<<aout_link_add_symbols>> to actually add the symbols to the
25933965Sjdp	linker hash table.
26033965Sjdp
26133965Sjdp	The ECOFF back end is unusual in that it does not normally
26233965Sjdp	call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
26333965Sjdp	archives already contain a hash table of symbols.  The ECOFF
26433965Sjdp	back end searches the archive itself to avoid the overhead of
26533965Sjdp	creating a new hash table.
26633965Sjdp
26733965SjdpINODE
26833965SjdpPerforming the Final Link, , Adding Symbols to the Hash Table, Linker Functions
26933965SjdpSUBSECTION
27033965Sjdp	Performing the final link
27133965Sjdp
27233965Sjdp@cindex _bfd_link_final_link in target vector
27333965Sjdp@cindex target vector (_bfd_final_link)
27433965Sjdp	When all the input files have been processed, the linker calls
27533965Sjdp	the <<_bfd_final_link>> entry point of the output BFD.  This
27633965Sjdp	routine is responsible for producing the final output file,
27733965Sjdp	which has several aspects.  It must relocate the contents of
27833965Sjdp	the input sections and copy the data into the output sections.
27933965Sjdp	It must build an output symbol table including any local
28033965Sjdp	symbols from the input files and the global symbols from the
281130561Sobrien	hash table.  When producing relocatable output, it must
28233965Sjdp	modify the input relocs and write them into the output file.
28333965Sjdp	There may also be object format dependent work to be done.
28433965Sjdp
28533965Sjdp	The linker will also call the <<write_object_contents>> entry
28633965Sjdp	point when the BFD is closed.  The two entry points must work
28733965Sjdp	together in order to produce the correct output file.
28833965Sjdp
28933965Sjdp	The details of how this works are inevitably dependent upon
29033965Sjdp	the specific object file format.  The a.out
29133965Sjdp	<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
29233965Sjdp
29333965Sjdp@menu
29433965Sjdp@* Information provided by the linker::
29533965Sjdp@* Relocating the section contents::
29633965Sjdp@* Writing the symbol table::
29733965Sjdp@end menu
29833965Sjdp
29933965SjdpINODE
30033965SjdpInformation provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
30133965SjdpSUBSUBSECTION
30233965Sjdp	Information provided by the linker
30333965Sjdp
30433965Sjdp	Before the linker calls the <<_bfd_final_link>> entry point,
30533965Sjdp	it sets up some data structures for the function to use.
30633965Sjdp
30733965Sjdp	The <<input_bfds>> field of the <<bfd_link_info>> structure
30833965Sjdp	will point to a list of all the input files included in the
30933965Sjdp	link.  These files are linked through the <<link_next>> field
31033965Sjdp	of the <<bfd>> structure.
31133965Sjdp
31233965Sjdp	Each section in the output file will have a list of
313218822Sdim	<<link_order>> structures attached to the <<map_head.link_order>>
31433965Sjdp	field (the <<link_order>> structure is defined in
31533965Sjdp	<<bfdlink.h>>).  These structures describe how to create the
31633965Sjdp	contents of the output section in terms of the contents of
31733965Sjdp	various input sections, fill constants, and, eventually, other
31833965Sjdp	types of information.  They also describe relocs that must be
31933965Sjdp	created by the BFD backend, but do not correspond to any input
32033965Sjdp	file; this is used to support -Ur, which builds constructors
321130561Sobrien	while generating a relocatable object file.
32233965Sjdp
32333965SjdpINODE
32433965SjdpRelocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
32533965SjdpSUBSUBSECTION
32633965Sjdp	Relocating the section contents
32733965Sjdp
32833965Sjdp	The <<_bfd_final_link>> function should look through the
32933965Sjdp	<<link_order>> structures attached to each section of the
33033965Sjdp	output file.  Each <<link_order>> structure should either be
33133965Sjdp	handled specially, or it should be passed to the function
33233965Sjdp	<<_bfd_default_link_order>> which will do the right thing
33333965Sjdp	(<<_bfd_default_link_order>> is defined in <<linker.c>>).
33433965Sjdp
33533965Sjdp	For efficiency, a <<link_order>> of type
33633965Sjdp	<<bfd_indirect_link_order>> whose associated section belongs
33733965Sjdp	to a BFD of the same format as the output BFD must be handled
33833965Sjdp	specially.  This type of <<link_order>> describes part of an
33933965Sjdp	output section in terms of a section belonging to one of the
34033965Sjdp	input files.  The <<_bfd_final_link>> function should read the
34133965Sjdp	contents of the section and any associated relocs, apply the
34233965Sjdp	relocs to the section contents, and write out the modified
343130561Sobrien	section contents.  If performing a relocatable link, the
34433965Sjdp	relocs themselves must also be modified and written out.
34533965Sjdp
34633965Sjdp@findex _bfd_relocate_contents
34733965Sjdp@findex _bfd_final_link_relocate
34833965Sjdp	The functions <<_bfd_relocate_contents>> and
34933965Sjdp	<<_bfd_final_link_relocate>> provide some general support for
35033965Sjdp	performing the actual relocations, notably overflow checking.
35133965Sjdp	Their arguments include information about the symbol the
35233965Sjdp	relocation is against and a <<reloc_howto_type>> argument
35333965Sjdp	which describes the relocation to perform.  These functions
35433965Sjdp	are defined in <<reloc.c>>.
35533965Sjdp
35633965Sjdp	The a.out function which handles reading, relocating, and
35733965Sjdp	writing section contents is <<aout_link_input_section>>.  The
35833965Sjdp	actual relocation is done in <<aout_link_input_section_std>>
35933965Sjdp	and <<aout_link_input_section_ext>>.
36033965Sjdp
36133965SjdpINODE
36233965SjdpWriting the symbol table, , Relocating the section contents, Performing the Final Link
36333965SjdpSUBSUBSECTION
36433965Sjdp	Writing the symbol table
36533965Sjdp
36633965Sjdp	The <<_bfd_final_link>> function must gather all the symbols
36733965Sjdp	in the input files and write them out.  It must also write out
36833965Sjdp	all the symbols in the global hash table.  This must be
36933965Sjdp	controlled by the <<strip>> and <<discard>> fields of the
37033965Sjdp	<<bfd_link_info>> structure.
37133965Sjdp
37233965Sjdp	The local symbols of the input files will not have been
37333965Sjdp	entered into the linker hash table.  The <<_bfd_final_link>>
37433965Sjdp	routine must consider each input file and include the symbols
37533965Sjdp	in the output file.  It may be convenient to do this when
37633965Sjdp	looking through the <<link_order>> structures, or it may be
37733965Sjdp	done by stepping through the <<input_bfds>> list.
37833965Sjdp
37933965Sjdp	The <<_bfd_final_link>> routine must also traverse the global
38033965Sjdp	hash table to gather all the externally visible symbols.  It
38133965Sjdp	is possible that most of the externally visible symbols may be
38233965Sjdp	written out when considering the symbols of each input file,
38333965Sjdp	but it is still necessary to traverse the hash table since the
38433965Sjdp	linker script may have defined some symbols that are not in
38533965Sjdp	any of the input files.
38633965Sjdp
38733965Sjdp	The <<strip>> field of the <<bfd_link_info>> structure
38833965Sjdp	controls which symbols are written out.  The possible values
38933965Sjdp	are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
39033965Sjdp	then the <<keep_hash>> field of the <<bfd_link_info>>
39133965Sjdp	structure is a hash table of symbols to keep; each symbol
39233965Sjdp	should be looked up in this hash table, and only symbols which
39333965Sjdp	are present should be included in the output file.
39433965Sjdp
39533965Sjdp	If the <<strip>> field of the <<bfd_link_info>> structure
39633965Sjdp	permits local symbols to be written out, the <<discard>> field
39733965Sjdp	is used to further controls which local symbols are included
39833965Sjdp	in the output file.  If the value is <<discard_l>>, then all
39933965Sjdp	local symbols which begin with a certain prefix are discarded;
40033965Sjdp	this is controlled by the <<bfd_is_local_label_name>> entry point.
40133965Sjdp
40233965Sjdp	The a.out backend handles symbols by calling
40333965Sjdp	<<aout_link_write_symbols>> on each input BFD and then
40433965Sjdp	traversing the global hash table with the function
40533965Sjdp	<<aout_link_write_other_symbol>>.  It builds a string table
40633965Sjdp	while writing out the symbols, which is written to the output
40733965Sjdp	file at the end of <<NAME(aout,final_link)>>.
40833965Sjdp*/
40933965Sjdp
410130561Sobrienstatic bfd_boolean generic_link_add_object_symbols
411130561Sobrien  (bfd *, struct bfd_link_info *, bfd_boolean collect);
412130561Sobrienstatic bfd_boolean generic_link_add_symbols
413130561Sobrien  (bfd *, struct bfd_link_info *, bfd_boolean);
414130561Sobrienstatic bfd_boolean generic_link_check_archive_element_no_collect
415130561Sobrien  (bfd *, struct bfd_link_info *, bfd_boolean *);
416130561Sobrienstatic bfd_boolean generic_link_check_archive_element_collect
417130561Sobrien  (bfd *, struct bfd_link_info *, bfd_boolean *);
418130561Sobrienstatic bfd_boolean generic_link_check_archive_element
419130561Sobrien  (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
420130561Sobrienstatic bfd_boolean generic_link_add_symbol_list
421130561Sobrien  (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
422130561Sobrien   bfd_boolean);
423130561Sobrienstatic bfd_boolean generic_add_output_symbol
424130561Sobrien  (bfd *, size_t *psymalloc, asymbol *);
425130561Sobrienstatic bfd_boolean default_data_link_order
426130561Sobrien  (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
427130561Sobrienstatic bfd_boolean default_indirect_link_order
428130561Sobrien  (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
429130561Sobrien   bfd_boolean);
43033965Sjdp
43133965Sjdp/* The link hash table structure is defined in bfdlink.h.  It provides
43233965Sjdp   a base hash table which the backend specific hash tables are built
43333965Sjdp   upon.  */
43433965Sjdp
43533965Sjdp/* Routine to create an entry in the link hash table.  */
43633965Sjdp
43733965Sjdpstruct bfd_hash_entry *
438130561Sobrien_bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
439130561Sobrien			struct bfd_hash_table *table,
440130561Sobrien			const char *string)
44133965Sjdp{
44233965Sjdp  /* Allocate the structure if it has not already been allocated by a
44333965Sjdp     subclass.  */
44489857Sobrien  if (entry == NULL)
44589857Sobrien    {
446130561Sobrien      entry = bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
44789857Sobrien      if (entry == NULL)
44889857Sobrien	return entry;
44989857Sobrien    }
45033965Sjdp
45133965Sjdp  /* Call the allocation method of the superclass.  */
45289857Sobrien  entry = bfd_hash_newfunc (entry, table, string);
45389857Sobrien  if (entry)
45489857Sobrien    {
45589857Sobrien      struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
45633965Sjdp
45733965Sjdp      /* Initialize the local fields.  */
45889857Sobrien      h->type = bfd_link_hash_new;
459218822Sdim      memset (&h->u.undef.next, 0,
460218822Sdim	      (sizeof (struct bfd_link_hash_entry)
461218822Sdim	       - offsetof (struct bfd_link_hash_entry, u.undef.next)));
46233965Sjdp    }
46333965Sjdp
46489857Sobrien  return entry;
46533965Sjdp}
46633965Sjdp
46733965Sjdp/* Initialize a link hash table.  The BFD argument is the one
46833965Sjdp   responsible for creating this table.  */
46933965Sjdp
470130561Sobrienbfd_boolean
471130561Sobrien_bfd_link_hash_table_init
472130561Sobrien  (struct bfd_link_hash_table *table,
473130561Sobrien   bfd *abfd,
474130561Sobrien   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
475130561Sobrien				      struct bfd_hash_table *,
476218822Sdim				      const char *),
477218822Sdim   unsigned int entsize)
47833965Sjdp{
47933965Sjdp  table->creator = abfd->xvec;
48033965Sjdp  table->undefs = NULL;
48133965Sjdp  table->undefs_tail = NULL;
48289857Sobrien  table->type = bfd_link_generic_hash_table;
48389857Sobrien
484218822Sdim  return bfd_hash_table_init (&table->table, newfunc, entsize);
48533965Sjdp}
48633965Sjdp
487130561Sobrien/* Look up a symbol in a link hash table.  If follow is TRUE, we
48833965Sjdp   follow bfd_link_hash_indirect and bfd_link_hash_warning links to
48933965Sjdp   the real symbol.  */
49033965Sjdp
49133965Sjdpstruct bfd_link_hash_entry *
492130561Sobrienbfd_link_hash_lookup (struct bfd_link_hash_table *table,
493130561Sobrien		      const char *string,
494130561Sobrien		      bfd_boolean create,
495130561Sobrien		      bfd_boolean copy,
496130561Sobrien		      bfd_boolean follow)
49733965Sjdp{
49833965Sjdp  struct bfd_link_hash_entry *ret;
49933965Sjdp
50033965Sjdp  ret = ((struct bfd_link_hash_entry *)
50133965Sjdp	 bfd_hash_lookup (&table->table, string, create, copy));
50233965Sjdp
503130561Sobrien  if (follow && ret != NULL)
50433965Sjdp    {
50533965Sjdp      while (ret->type == bfd_link_hash_indirect
50633965Sjdp	     || ret->type == bfd_link_hash_warning)
50733965Sjdp	ret = ret->u.i.link;
50833965Sjdp    }
50933965Sjdp
51033965Sjdp  return ret;
51133965Sjdp}
51233965Sjdp
51333965Sjdp/* Look up a symbol in the main linker hash table if the symbol might
51433965Sjdp   be wrapped.  This should only be used for references to an
51533965Sjdp   undefined symbol, not for definitions of a symbol.  */
51633965Sjdp
51733965Sjdpstruct bfd_link_hash_entry *
518130561Sobrienbfd_wrapped_link_hash_lookup (bfd *abfd,
519130561Sobrien			      struct bfd_link_info *info,
520130561Sobrien			      const char *string,
521130561Sobrien			      bfd_boolean create,
522130561Sobrien			      bfd_boolean copy,
523130561Sobrien			      bfd_boolean follow)
52433965Sjdp{
52589857Sobrien  bfd_size_type amt;
52689857Sobrien
52733965Sjdp  if (info->wrap_hash != NULL)
52833965Sjdp    {
52933965Sjdp      const char *l;
530130561Sobrien      char prefix = '\0';
53133965Sjdp
53233965Sjdp      l = string;
533130561Sobrien      if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
534130561Sobrien	{
535130561Sobrien	  prefix = *l;
536130561Sobrien	  ++l;
537130561Sobrien	}
53833965Sjdp
53933965Sjdp#undef WRAP
54033965Sjdp#define WRAP "__wrap_"
54133965Sjdp
542130561Sobrien      if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
54333965Sjdp	{
54433965Sjdp	  char *n;
54533965Sjdp	  struct bfd_link_hash_entry *h;
54633965Sjdp
54733965Sjdp	  /* This symbol is being wrapped.  We want to replace all
54833965Sjdp             references to SYM with references to __wrap_SYM.  */
54933965Sjdp
55089857Sobrien	  amt = strlen (l) + sizeof WRAP + 1;
551130561Sobrien	  n = bfd_malloc (amt);
55233965Sjdp	  if (n == NULL)
55333965Sjdp	    return NULL;
55433965Sjdp
555130561Sobrien	  n[0] = prefix;
55633965Sjdp	  n[1] = '\0';
55733965Sjdp	  strcat (n, WRAP);
55833965Sjdp	  strcat (n, l);
559130561Sobrien	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
56033965Sjdp	  free (n);
56133965Sjdp	  return h;
56233965Sjdp	}
56333965Sjdp
56433965Sjdp#undef WRAP
56533965Sjdp
566218822Sdim#undef  REAL
56733965Sjdp#define REAL "__real_"
56833965Sjdp
56933965Sjdp      if (*l == '_'
570218822Sdim	  && CONST_STRNEQ (l, REAL)
57133965Sjdp	  && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
572130561Sobrien			      FALSE, FALSE) != NULL)
57333965Sjdp	{
57433965Sjdp	  char *n;
57533965Sjdp	  struct bfd_link_hash_entry *h;
57633965Sjdp
57733965Sjdp	  /* This is a reference to __real_SYM, where SYM is being
57833965Sjdp             wrapped.  We want to replace all references to __real_SYM
57933965Sjdp             with references to SYM.  */
58033965Sjdp
58189857Sobrien	  amt = strlen (l + sizeof REAL - 1) + 2;
582130561Sobrien	  n = bfd_malloc (amt);
58333965Sjdp	  if (n == NULL)
58433965Sjdp	    return NULL;
58533965Sjdp
586130561Sobrien	  n[0] = prefix;
58733965Sjdp	  n[1] = '\0';
58833965Sjdp	  strcat (n, l + sizeof REAL - 1);
589130561Sobrien	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
59033965Sjdp	  free (n);
59133965Sjdp	  return h;
59233965Sjdp	}
59333965Sjdp
59433965Sjdp#undef REAL
59533965Sjdp    }
59633965Sjdp
59733965Sjdp  return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
59833965Sjdp}
59933965Sjdp
60033965Sjdp/* Traverse a generic link hash table.  The only reason this is not a
60133965Sjdp   macro is to do better type checking.  This code presumes that an
60233965Sjdp   argument passed as a struct bfd_hash_entry * may be caught as a
60333965Sjdp   struct bfd_link_hash_entry * with no explicit cast required on the
60433965Sjdp   call.  */
60533965Sjdp
60677298Sobrienvoid
607130561Sobrienbfd_link_hash_traverse
608130561Sobrien  (struct bfd_link_hash_table *table,
609130561Sobrien   bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
610130561Sobrien   void *info)
61133965Sjdp{
61233965Sjdp  bfd_hash_traverse (&table->table,
613130561Sobrien		     (bfd_boolean (*) (struct bfd_hash_entry *, void *)) func,
61433965Sjdp		     info);
61533965Sjdp}
61633965Sjdp
61733965Sjdp/* Add a symbol to the linker hash table undefs list.  */
61833965Sjdp
619130561Sobrienvoid
620130561Sobrienbfd_link_add_undef (struct bfd_link_hash_table *table,
621130561Sobrien		    struct bfd_link_hash_entry *h)
62233965Sjdp{
623218822Sdim  BFD_ASSERT (h->u.undef.next == NULL);
624130561Sobrien  if (table->undefs_tail != NULL)
625218822Sdim    table->undefs_tail->u.undef.next = h;
626130561Sobrien  if (table->undefs == NULL)
62733965Sjdp    table->undefs = h;
62833965Sjdp  table->undefs_tail = h;
62933965Sjdp}
630218822Sdim
631218822Sdim/* The undefs list was designed so that in normal use we don't need to
632218822Sdim   remove entries.  However, if symbols on the list are changed from
633218822Sdim   bfd_link_hash_undefined to either bfd_link_hash_undefweak or
634218822Sdim   bfd_link_hash_new for some reason, then they must be removed from the
635218822Sdim   list.  Failure to do so might result in the linker attempting to add
636218822Sdim   the symbol to the list again at a later stage.  */
637218822Sdim
638218822Sdimvoid
639218822Sdimbfd_link_repair_undef_list (struct bfd_link_hash_table *table)
640218822Sdim{
641218822Sdim  struct bfd_link_hash_entry **pun;
642218822Sdim
643218822Sdim  pun = &table->undefs;
644218822Sdim  while (*pun != NULL)
645218822Sdim    {
646218822Sdim      struct bfd_link_hash_entry *h = *pun;
647218822Sdim
648218822Sdim      if (h->type == bfd_link_hash_new
649218822Sdim	  || h->type == bfd_link_hash_undefweak)
650218822Sdim	{
651218822Sdim	  *pun = h->u.undef.next;
652218822Sdim	  h->u.undef.next = NULL;
653218822Sdim	  if (h == table->undefs_tail)
654218822Sdim	    {
655218822Sdim	      if (pun == &table->undefs)
656218822Sdim		table->undefs_tail = NULL;
657218822Sdim	      else
658218822Sdim		/* pun points at an u.undef.next field.  Go back to
659218822Sdim		   the start of the link_hash_entry.  */
660218822Sdim		table->undefs_tail = (struct bfd_link_hash_entry *)
661218822Sdim		  ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
662218822Sdim	      break;
663218822Sdim	    }
664218822Sdim	}
665218822Sdim      else
666218822Sdim	pun = &h->u.undef.next;
667218822Sdim    }
668218822Sdim}
66933965Sjdp
670130561Sobrien/* Routine to create an entry in a generic link hash table.  */
67133965Sjdp
67233965Sjdpstruct bfd_hash_entry *
673130561Sobrien_bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
674130561Sobrien				struct bfd_hash_table *table,
675130561Sobrien				const char *string)
67633965Sjdp{
67733965Sjdp  /* Allocate the structure if it has not already been allocated by a
67833965Sjdp     subclass.  */
67989857Sobrien  if (entry == NULL)
68089857Sobrien    {
681130561Sobrien      entry =
682107492Sobrien	bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
68389857Sobrien      if (entry == NULL)
68489857Sobrien	return entry;
68589857Sobrien    }
68633965Sjdp
68733965Sjdp  /* Call the allocation method of the superclass.  */
68889857Sobrien  entry = _bfd_link_hash_newfunc (entry, table, string);
68989857Sobrien  if (entry)
69089857Sobrien    {
69189857Sobrien      struct generic_link_hash_entry *ret;
69233965Sjdp
69333965Sjdp      /* Set local fields.  */
69489857Sobrien      ret = (struct generic_link_hash_entry *) entry;
695130561Sobrien      ret->written = FALSE;
69633965Sjdp      ret->sym = NULL;
69733965Sjdp    }
69833965Sjdp
69989857Sobrien  return entry;
70033965Sjdp}
70133965Sjdp
702130561Sobrien/* Create a generic link hash table.  */
70333965Sjdp
70433965Sjdpstruct bfd_link_hash_table *
705130561Sobrien_bfd_generic_link_hash_table_create (bfd *abfd)
70633965Sjdp{
70733965Sjdp  struct generic_link_hash_table *ret;
70889857Sobrien  bfd_size_type amt = sizeof (struct generic_link_hash_table);
70933965Sjdp
710130561Sobrien  ret = bfd_malloc (amt);
71133965Sjdp  if (ret == NULL)
712130561Sobrien    return NULL;
71333965Sjdp  if (! _bfd_link_hash_table_init (&ret->root, abfd,
714218822Sdim				   _bfd_generic_link_hash_newfunc,
715218822Sdim				   sizeof (struct generic_link_hash_entry)))
71633965Sjdp    {
71733965Sjdp      free (ret);
718130561Sobrien      return NULL;
71933965Sjdp    }
72033965Sjdp  return &ret->root;
72133965Sjdp}
72233965Sjdp
723104834Sobrienvoid
724130561Sobrien_bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
725104834Sobrien{
726104834Sobrien  struct generic_link_hash_table *ret
727104834Sobrien    = (struct generic_link_hash_table *) hash;
728104834Sobrien
729104834Sobrien  bfd_hash_table_free (&ret->root.table);
730104834Sobrien  free (ret);
731104834Sobrien}
732104834Sobrien
73333965Sjdp/* Grab the symbols for an object file when doing a generic link.  We
73433965Sjdp   store the symbols in the outsymbols field.  We need to keep them
73533965Sjdp   around for the entire link to ensure that we only read them once.
73633965Sjdp   If we read them multiple times, we might wind up with relocs and
73733965Sjdp   the hash table pointing to different instances of the symbol
73833965Sjdp   structure.  */
73933965Sjdp
740130561Sobrienstatic bfd_boolean
741130561Sobriengeneric_link_read_symbols (bfd *abfd)
74233965Sjdp{
743130561Sobrien  if (bfd_get_outsymbols (abfd) == NULL)
74433965Sjdp    {
74533965Sjdp      long symsize;
74633965Sjdp      long symcount;
74733965Sjdp
74833965Sjdp      symsize = bfd_get_symtab_upper_bound (abfd);
74933965Sjdp      if (symsize < 0)
750130561Sobrien	return FALSE;
751130561Sobrien      bfd_get_outsymbols (abfd) = bfd_alloc (abfd, symsize);
75260484Sobrien      if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
753130561Sobrien	return FALSE;
75460484Sobrien      symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
75533965Sjdp      if (symcount < 0)
756130561Sobrien	return FALSE;
75760484Sobrien      bfd_get_symcount (abfd) = symcount;
75833965Sjdp    }
75933965Sjdp
760130561Sobrien  return TRUE;
76133965Sjdp}
76233965Sjdp
76333965Sjdp/* Generic function to add symbols to from an object file to the
76433965Sjdp   global hash table.  This version does not automatically collect
76533965Sjdp   constructors by name.  */
76633965Sjdp
767130561Sobrienbfd_boolean
768130561Sobrien_bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
76933965Sjdp{
770130561Sobrien  return generic_link_add_symbols (abfd, info, FALSE);
77133965Sjdp}
77233965Sjdp
77333965Sjdp/* Generic function to add symbols from an object file to the global
77433965Sjdp   hash table.  This version automatically collects constructors by
77533965Sjdp   name, as the collect2 program does.  It should be used for any
77633965Sjdp   target which does not provide some other mechanism for setting up
77733965Sjdp   constructors and destructors; these are approximately those targets
77833965Sjdp   for which gcc uses collect2 and do not support stabs.  */
77933965Sjdp
780130561Sobrienbfd_boolean
781130561Sobrien_bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
78233965Sjdp{
783130561Sobrien  return generic_link_add_symbols (abfd, info, TRUE);
78433965Sjdp}
78533965Sjdp
786104834Sobrien/* Indicate that we are only retrieving symbol values from this
787104834Sobrien   section.  We want the symbols to act as though the values in the
788104834Sobrien   file are absolute.  */
789104834Sobrien
790104834Sobrienvoid
791130561Sobrien_bfd_generic_link_just_syms (asection *sec,
792130561Sobrien			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
793104834Sobrien{
794104834Sobrien  sec->output_section = bfd_abs_section_ptr;
795104834Sobrien  sec->output_offset = sec->vma;
796104834Sobrien}
797104834Sobrien
79833965Sjdp/* Add symbols from an object file to the global hash table.  */
79933965Sjdp
800130561Sobrienstatic bfd_boolean
801130561Sobriengeneric_link_add_symbols (bfd *abfd,
802130561Sobrien			  struct bfd_link_info *info,
803130561Sobrien			  bfd_boolean collect)
80433965Sjdp{
805130561Sobrien  bfd_boolean ret;
80633965Sjdp
80733965Sjdp  switch (bfd_get_format (abfd))
80833965Sjdp    {
80933965Sjdp    case bfd_object:
81033965Sjdp      ret = generic_link_add_object_symbols (abfd, info, collect);
81133965Sjdp      break;
81233965Sjdp    case bfd_archive:
81333965Sjdp      ret = (_bfd_generic_link_add_archive_symbols
81433965Sjdp	     (abfd, info,
81533965Sjdp	      (collect
81633965Sjdp	       ? generic_link_check_archive_element_collect
81733965Sjdp	       : generic_link_check_archive_element_no_collect)));
81833965Sjdp      break;
81933965Sjdp    default:
82033965Sjdp      bfd_set_error (bfd_error_wrong_format);
821130561Sobrien      ret = FALSE;
82233965Sjdp    }
82333965Sjdp
82433965Sjdp  return ret;
82533965Sjdp}
82633965Sjdp
82733965Sjdp/* Add symbols from an object file to the global hash table.  */
82833965Sjdp
829130561Sobrienstatic bfd_boolean
830130561Sobriengeneric_link_add_object_symbols (bfd *abfd,
831130561Sobrien				 struct bfd_link_info *info,
832130561Sobrien				 bfd_boolean collect)
83333965Sjdp{
83489857Sobrien  bfd_size_type symcount;
835130561Sobrien  struct bfd_symbol **outsyms;
83689857Sobrien
83733965Sjdp  if (! generic_link_read_symbols (abfd))
838130561Sobrien    return FALSE;
83989857Sobrien  symcount = _bfd_generic_link_get_symcount (abfd);
84089857Sobrien  outsyms = _bfd_generic_link_get_symbols (abfd);
84189857Sobrien  return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
84233965Sjdp}
84333965Sjdp
84433965Sjdp/* We build a hash table of all symbols defined in an archive.  */
84533965Sjdp
84633965Sjdp/* An archive symbol may be defined by multiple archive elements.
84733965Sjdp   This linked list is used to hold the elements.  */
84833965Sjdp
84933965Sjdpstruct archive_list
85033965Sjdp{
85133965Sjdp  struct archive_list *next;
85289857Sobrien  unsigned int indx;
85333965Sjdp};
85433965Sjdp
85533965Sjdp/* An entry in an archive hash table.  */
85633965Sjdp
85733965Sjdpstruct archive_hash_entry
85833965Sjdp{
85933965Sjdp  struct bfd_hash_entry root;
86033965Sjdp  /* Where the symbol is defined.  */
86133965Sjdp  struct archive_list *defs;
86233965Sjdp};
86333965Sjdp
86433965Sjdp/* An archive hash table itself.  */
86533965Sjdp
86633965Sjdpstruct archive_hash_table
86733965Sjdp{
86833965Sjdp  struct bfd_hash_table table;
86933965Sjdp};
87033965Sjdp
87133965Sjdp/* Create a new entry for an archive hash table.  */
87233965Sjdp
87333965Sjdpstatic struct bfd_hash_entry *
874130561Sobrienarchive_hash_newfunc (struct bfd_hash_entry *entry,
875130561Sobrien		      struct bfd_hash_table *table,
876130561Sobrien		      const char *string)
87733965Sjdp{
87833965Sjdp  struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
87933965Sjdp
88033965Sjdp  /* Allocate the structure if it has not already been allocated by a
88133965Sjdp     subclass.  */
882130561Sobrien  if (ret == NULL)
883130561Sobrien    ret = bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
884130561Sobrien  if (ret == NULL)
88533965Sjdp    return NULL;
88633965Sjdp
88733965Sjdp  /* Call the allocation method of the superclass.  */
88833965Sjdp  ret = ((struct archive_hash_entry *)
88933965Sjdp	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
89033965Sjdp
89133965Sjdp  if (ret)
89233965Sjdp    {
89333965Sjdp      /* Initialize the local fields.  */
894130561Sobrien      ret->defs = NULL;
89533965Sjdp    }
89633965Sjdp
897130561Sobrien  return &ret->root;
89833965Sjdp}
89933965Sjdp
90033965Sjdp/* Initialize an archive hash table.  */
90133965Sjdp
902130561Sobrienstatic bfd_boolean
903130561Sobrienarchive_hash_table_init
904130561Sobrien  (struct archive_hash_table *table,
905130561Sobrien   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
906130561Sobrien				      struct bfd_hash_table *,
907218822Sdim				      const char *),
908218822Sdim   unsigned int entsize)
90933965Sjdp{
910218822Sdim  return bfd_hash_table_init (&table->table, newfunc, entsize);
91133965Sjdp}
91233965Sjdp
91333965Sjdp/* Look up an entry in an archive hash table.  */
91433965Sjdp
91533965Sjdp#define archive_hash_lookup(t, string, create, copy) \
91633965Sjdp  ((struct archive_hash_entry *) \
91733965Sjdp   bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
91833965Sjdp
91933965Sjdp/* Allocate space in an archive hash table.  */
92033965Sjdp
92133965Sjdp#define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
92233965Sjdp
92333965Sjdp/* Free an archive hash table.  */
92433965Sjdp
92533965Sjdp#define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
92633965Sjdp
92733965Sjdp/* Generic function to add symbols from an archive file to the global
92833965Sjdp   hash file.  This function presumes that the archive symbol table
92933965Sjdp   has already been read in (this is normally done by the
93033965Sjdp   bfd_check_format entry point).  It looks through the undefined and
93133965Sjdp   common symbols and searches the archive symbol table for them.  If
93233965Sjdp   it finds an entry, it includes the associated object file in the
93333965Sjdp   link.
93433965Sjdp
93533965Sjdp   The old linker looked through the archive symbol table for
93633965Sjdp   undefined symbols.  We do it the other way around, looking through
93733965Sjdp   undefined symbols for symbols defined in the archive.  The
93833965Sjdp   advantage of the newer scheme is that we only have to look through
93933965Sjdp   the list of undefined symbols once, whereas the old method had to
94033965Sjdp   re-search the symbol table each time a new object file was added.
94133965Sjdp
94233965Sjdp   The CHECKFN argument is used to see if an object file should be
943130561Sobrien   included.  CHECKFN should set *PNEEDED to TRUE if the object file
94433965Sjdp   should be included, and must also call the bfd_link_info
94533965Sjdp   add_archive_element callback function and handle adding the symbols
946130561Sobrien   to the global hash table.  CHECKFN should only return FALSE if some
94733965Sjdp   sort of error occurs.
94833965Sjdp
94933965Sjdp   For some formats, such as a.out, it is possible to look through an
95033965Sjdp   object file but not actually include it in the link.  The
95133965Sjdp   archive_pass field in a BFD is used to avoid checking the symbols
95233965Sjdp   of an object files too many times.  When an object is included in
95333965Sjdp   the link, archive_pass is set to -1.  If an object is scanned but
95433965Sjdp   not included, archive_pass is set to the pass number.  The pass
95533965Sjdp   number is incremented each time a new object file is included.  The
95633965Sjdp   pass number is used because when a new object file is included it
95733965Sjdp   may create new undefined symbols which cause a previously examined
95833965Sjdp   object file to be included.  */
95933965Sjdp
960130561Sobrienbfd_boolean
961130561Sobrien_bfd_generic_link_add_archive_symbols
962130561Sobrien  (bfd *abfd,
963130561Sobrien   struct bfd_link_info *info,
964130561Sobrien   bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
96533965Sjdp{
96633965Sjdp  carsym *arsyms;
96733965Sjdp  carsym *arsym_end;
96833965Sjdp  register carsym *arsym;
96933965Sjdp  int pass;
97033965Sjdp  struct archive_hash_table arsym_hash;
97189857Sobrien  unsigned int indx;
97233965Sjdp  struct bfd_link_hash_entry **pundef;
97333965Sjdp
97433965Sjdp  if (! bfd_has_map (abfd))
97533965Sjdp    {
97633965Sjdp      /* An empty archive is a special case.  */
977130561Sobrien      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
978130561Sobrien	return TRUE;
97933965Sjdp      bfd_set_error (bfd_error_no_armap);
980130561Sobrien      return FALSE;
98133965Sjdp    }
98233965Sjdp
98333965Sjdp  arsyms = bfd_ardata (abfd)->symdefs;
98433965Sjdp  arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
98533965Sjdp
98633965Sjdp  /* In order to quickly determine whether an symbol is defined in
98733965Sjdp     this archive, we build a hash table of the symbols.  */
988218822Sdim  if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
989218822Sdim				 sizeof (struct archive_hash_entry)))
990130561Sobrien    return FALSE;
99133965Sjdp  for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
99233965Sjdp    {
99333965Sjdp      struct archive_hash_entry *arh;
99433965Sjdp      struct archive_list *l, **pp;
99533965Sjdp
996130561Sobrien      arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
997130561Sobrien      if (arh == NULL)
99833965Sjdp	goto error_return;
99933965Sjdp      l = ((struct archive_list *)
100033965Sjdp	   archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
100133965Sjdp      if (l == NULL)
100233965Sjdp	goto error_return;
100333965Sjdp      l->indx = indx;
1004130561Sobrien      for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
100533965Sjdp	;
100633965Sjdp      *pp = l;
100733965Sjdp      l->next = NULL;
100833965Sjdp    }
100933965Sjdp
101033965Sjdp  /* The archive_pass field in the archive itself is used to
101133965Sjdp     initialize PASS, sine we may search the same archive multiple
101233965Sjdp     times.  */
101333965Sjdp  pass = abfd->archive_pass + 1;
101433965Sjdp
101533965Sjdp  /* New undefined symbols are added to the end of the list, so we
101633965Sjdp     only need to look through it once.  */
101733965Sjdp  pundef = &info->hash->undefs;
1018130561Sobrien  while (*pundef != NULL)
101933965Sjdp    {
102033965Sjdp      struct bfd_link_hash_entry *h;
102133965Sjdp      struct archive_hash_entry *arh;
102233965Sjdp      struct archive_list *l;
102333965Sjdp
102433965Sjdp      h = *pundef;
102533965Sjdp
102633965Sjdp      /* When a symbol is defined, it is not necessarily removed from
102733965Sjdp	 the list.  */
102833965Sjdp      if (h->type != bfd_link_hash_undefined
102933965Sjdp	  && h->type != bfd_link_hash_common)
103033965Sjdp	{
103133965Sjdp	  /* Remove this entry from the list, for general cleanliness
103233965Sjdp	     and because we are going to look through the list again
103333965Sjdp	     if we search any more libraries.  We can't remove the
103433965Sjdp	     entry if it is the tail, because that would lose any
103533965Sjdp	     entries we add to the list later on (it would also cause
103633965Sjdp	     us to lose track of whether the symbol has been
103733965Sjdp	     referenced).  */
103833965Sjdp	  if (*pundef != info->hash->undefs_tail)
1039218822Sdim	    *pundef = (*pundef)->u.undef.next;
104033965Sjdp	  else
1041218822Sdim	    pundef = &(*pundef)->u.undef.next;
104233965Sjdp	  continue;
104333965Sjdp	}
104433965Sjdp
104533965Sjdp      /* Look for this symbol in the archive symbol map.  */
1046130561Sobrien      arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
1047130561Sobrien      if (arh == NULL)
104833965Sjdp	{
104989857Sobrien	  /* If we haven't found the exact symbol we're looking for,
105089857Sobrien	     let's look for its import thunk */
105189857Sobrien	  if (info->pei386_auto_import)
105289857Sobrien	    {
105389857Sobrien	      bfd_size_type amt = strlen (h->root.string) + 10;
1054130561Sobrien	      char *buf = bfd_malloc (amt);
105589857Sobrien	      if (buf == NULL)
1056130561Sobrien		return FALSE;
105789857Sobrien
105889857Sobrien	      sprintf (buf, "__imp_%s", h->root.string);
1059130561Sobrien	      arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
106089857Sobrien	      free(buf);
106189857Sobrien	    }
1062130561Sobrien	  if (arh == NULL)
106389857Sobrien	    {
1064218822Sdim	      pundef = &(*pundef)->u.undef.next;
106589857Sobrien	      continue;
106689857Sobrien	    }
106733965Sjdp	}
106833965Sjdp      /* Look at all the objects which define this symbol.  */
1069130561Sobrien      for (l = arh->defs; l != NULL; l = l->next)
107033965Sjdp	{
107133965Sjdp	  bfd *element;
1072130561Sobrien	  bfd_boolean needed;
107333965Sjdp
107433965Sjdp	  /* If the symbol has gotten defined along the way, quit.  */
107533965Sjdp	  if (h->type != bfd_link_hash_undefined
107633965Sjdp	      && h->type != bfd_link_hash_common)
107733965Sjdp	    break;
107833965Sjdp
107933965Sjdp	  element = bfd_get_elt_at_index (abfd, l->indx);
1080130561Sobrien	  if (element == NULL)
108133965Sjdp	    goto error_return;
108233965Sjdp
108333965Sjdp	  /* If we've already included this element, or if we've
108433965Sjdp	     already checked it on this pass, continue.  */
108533965Sjdp	  if (element->archive_pass == -1
108633965Sjdp	      || element->archive_pass == pass)
108733965Sjdp	    continue;
108833965Sjdp
108933965Sjdp	  /* If we can't figure this element out, just ignore it.  */
109033965Sjdp	  if (! bfd_check_format (element, bfd_object))
109133965Sjdp	    {
109233965Sjdp	      element->archive_pass = -1;
109333965Sjdp	      continue;
109433965Sjdp	    }
109533965Sjdp
109633965Sjdp	  /* CHECKFN will see if this element should be included, and
109733965Sjdp	     go ahead and include it if appropriate.  */
109833965Sjdp	  if (! (*checkfn) (element, info, &needed))
109933965Sjdp	    goto error_return;
110033965Sjdp
110133965Sjdp	  if (! needed)
110233965Sjdp	    element->archive_pass = pass;
110333965Sjdp	  else
110433965Sjdp	    {
110533965Sjdp	      element->archive_pass = -1;
110633965Sjdp
110733965Sjdp	      /* Increment the pass count to show that we may need to
110833965Sjdp		 recheck object files which were already checked.  */
110933965Sjdp	      ++pass;
111033965Sjdp	    }
111133965Sjdp	}
111233965Sjdp
1113218822Sdim      pundef = &(*pundef)->u.undef.next;
111433965Sjdp    }
111533965Sjdp
111633965Sjdp  archive_hash_table_free (&arsym_hash);
111733965Sjdp
111833965Sjdp  /* Save PASS in case we are called again.  */
111933965Sjdp  abfd->archive_pass = pass;
112033965Sjdp
1121130561Sobrien  return TRUE;
112233965Sjdp
112333965Sjdp error_return:
112433965Sjdp  archive_hash_table_free (&arsym_hash);
1125130561Sobrien  return FALSE;
112633965Sjdp}
112733965Sjdp
112833965Sjdp/* See if we should include an archive element.  This version is used
112933965Sjdp   when we do not want to automatically collect constructors based on
113033965Sjdp   the symbol name, presumably because we have some other mechanism
113133965Sjdp   for finding them.  */
113233965Sjdp
1133130561Sobrienstatic bfd_boolean
1134130561Sobriengeneric_link_check_archive_element_no_collect (
1135130561Sobrien					       bfd *abfd,
1136130561Sobrien					       struct bfd_link_info *info,
1137130561Sobrien					       bfd_boolean *pneeded)
113833965Sjdp{
1139130561Sobrien  return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
114033965Sjdp}
114133965Sjdp
114233965Sjdp/* See if we should include an archive element.  This version is used
114333965Sjdp   when we want to automatically collect constructors based on the
114433965Sjdp   symbol name, as collect2 does.  */
114533965Sjdp
1146130561Sobrienstatic bfd_boolean
1147130561Sobriengeneric_link_check_archive_element_collect (bfd *abfd,
1148130561Sobrien					    struct bfd_link_info *info,
1149130561Sobrien					    bfd_boolean *pneeded)
115033965Sjdp{
1151130561Sobrien  return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
115233965Sjdp}
115333965Sjdp
115433965Sjdp/* See if we should include an archive element.  Optionally collect
115533965Sjdp   constructors.  */
115633965Sjdp
1157130561Sobrienstatic bfd_boolean
1158130561Sobriengeneric_link_check_archive_element (bfd *abfd,
1159130561Sobrien				    struct bfd_link_info *info,
1160130561Sobrien				    bfd_boolean *pneeded,
1161130561Sobrien				    bfd_boolean collect)
116233965Sjdp{
116333965Sjdp  asymbol **pp, **ppend;
116433965Sjdp
1165130561Sobrien  *pneeded = FALSE;
116633965Sjdp
116733965Sjdp  if (! generic_link_read_symbols (abfd))
1168130561Sobrien    return FALSE;
116933965Sjdp
117033965Sjdp  pp = _bfd_generic_link_get_symbols (abfd);
117133965Sjdp  ppend = pp + _bfd_generic_link_get_symcount (abfd);
117233965Sjdp  for (; pp < ppend; pp++)
117333965Sjdp    {
117433965Sjdp      asymbol *p;
117533965Sjdp      struct bfd_link_hash_entry *h;
117633965Sjdp
117733965Sjdp      p = *pp;
117833965Sjdp
117933965Sjdp      /* We are only interested in globally visible symbols.  */
118033965Sjdp      if (! bfd_is_com_section (p->section)
118133965Sjdp	  && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
118233965Sjdp	continue;
118333965Sjdp
118433965Sjdp      /* We are only interested if we know something about this
118533965Sjdp	 symbol, and it is undefined or common.  An undefined weak
118633965Sjdp	 symbol (type bfd_link_hash_undefweak) is not considered to be
118733965Sjdp	 a reference when pulling files out of an archive.  See the
118833965Sjdp	 SVR4 ABI, p. 4-27.  */
1189130561Sobrien      h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1190130561Sobrien				FALSE, TRUE);
1191130561Sobrien      if (h == NULL
119233965Sjdp	  || (h->type != bfd_link_hash_undefined
119333965Sjdp	      && h->type != bfd_link_hash_common))
119433965Sjdp	continue;
119533965Sjdp
119633965Sjdp      /* P is a symbol we are looking for.  */
119733965Sjdp
119833965Sjdp      if (! bfd_is_com_section (p->section))
119933965Sjdp	{
120033965Sjdp	  bfd_size_type symcount;
120133965Sjdp	  asymbol **symbols;
120233965Sjdp
120333965Sjdp	  /* This object file defines this symbol, so pull it in.  */
120433965Sjdp	  if (! (*info->callbacks->add_archive_element) (info, abfd,
120533965Sjdp							 bfd_asymbol_name (p)))
1206130561Sobrien	    return FALSE;
120733965Sjdp	  symcount = _bfd_generic_link_get_symcount (abfd);
120833965Sjdp	  symbols = _bfd_generic_link_get_symbols (abfd);
120933965Sjdp	  if (! generic_link_add_symbol_list (abfd, info, symcount,
121033965Sjdp					      symbols, collect))
1211130561Sobrien	    return FALSE;
1212130561Sobrien	  *pneeded = TRUE;
1213130561Sobrien	  return TRUE;
121433965Sjdp	}
121533965Sjdp
121633965Sjdp      /* P is a common symbol.  */
121733965Sjdp
121833965Sjdp      if (h->type == bfd_link_hash_undefined)
121933965Sjdp	{
122033965Sjdp	  bfd *symbfd;
122133965Sjdp	  bfd_vma size;
122233965Sjdp	  unsigned int power;
122333965Sjdp
122433965Sjdp	  symbfd = h->u.undef.abfd;
1225130561Sobrien	  if (symbfd == NULL)
122633965Sjdp	    {
122733965Sjdp	      /* This symbol was created as undefined from outside
122833965Sjdp		 BFD.  We assume that we should link in the object
122933965Sjdp		 file.  This is for the -u option in the linker.  */
123033965Sjdp	      if (! (*info->callbacks->add_archive_element)
123133965Sjdp		  (info, abfd, bfd_asymbol_name (p)))
1232130561Sobrien		return FALSE;
1233130561Sobrien	      *pneeded = TRUE;
1234130561Sobrien	      return TRUE;
123533965Sjdp	    }
123633965Sjdp
123733965Sjdp	  /* Turn the symbol into a common symbol but do not link in
123833965Sjdp	     the object file.  This is how a.out works.  Object
123933965Sjdp	     formats that require different semantics must implement
124033965Sjdp	     this function differently.  This symbol is already on the
124133965Sjdp	     undefs list.  We add the section to a common section
124233965Sjdp	     attached to symbfd to ensure that it is in a BFD which
124333965Sjdp	     will be linked in.  */
124433965Sjdp	  h->type = bfd_link_hash_common;
124533965Sjdp	  h->u.c.p =
1246130561Sobrien	    bfd_hash_allocate (&info->hash->table,
1247130561Sobrien			       sizeof (struct bfd_link_hash_common_entry));
124833965Sjdp	  if (h->u.c.p == NULL)
1249130561Sobrien	    return FALSE;
125033965Sjdp
125133965Sjdp	  size = bfd_asymbol_value (p);
125233965Sjdp	  h->u.c.size = size;
125333965Sjdp
125433965Sjdp	  power = bfd_log2 (size);
125533965Sjdp	  if (power > 4)
125633965Sjdp	    power = 4;
125733965Sjdp	  h->u.c.p->alignment_power = power;
125833965Sjdp
125933965Sjdp	  if (p->section == bfd_com_section_ptr)
126033965Sjdp	    h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
126133965Sjdp	  else
126233965Sjdp	    h->u.c.p->section = bfd_make_section_old_way (symbfd,
126333965Sjdp							  p->section->name);
126433965Sjdp	  h->u.c.p->section->flags = SEC_ALLOC;
126533965Sjdp	}
126633965Sjdp      else
126733965Sjdp	{
126833965Sjdp	  /* Adjust the size of the common symbol if necessary.  This
126933965Sjdp	     is how a.out works.  Object formats that require
127033965Sjdp	     different semantics must implement this function
127133965Sjdp	     differently.  */
127233965Sjdp	  if (bfd_asymbol_value (p) > h->u.c.size)
127333965Sjdp	    h->u.c.size = bfd_asymbol_value (p);
127433965Sjdp	}
127533965Sjdp    }
127633965Sjdp
127733965Sjdp  /* This archive element is not needed.  */
1278130561Sobrien  return TRUE;
127933965Sjdp}
128033965Sjdp
128133965Sjdp/* Add the symbols from an object file to the global hash table.  ABFD
128233965Sjdp   is the object file.  INFO is the linker information.  SYMBOL_COUNT
128333965Sjdp   is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
1284130561Sobrien   is TRUE if constructors should be automatically collected by name
128533965Sjdp   as is done by collect2.  */
128633965Sjdp
1287130561Sobrienstatic bfd_boolean
1288130561Sobriengeneric_link_add_symbol_list (bfd *abfd,
1289130561Sobrien			      struct bfd_link_info *info,
1290130561Sobrien			      bfd_size_type symbol_count,
1291130561Sobrien			      asymbol **symbols,
1292130561Sobrien			      bfd_boolean collect)
129333965Sjdp{
129433965Sjdp  asymbol **pp, **ppend;
129533965Sjdp
129633965Sjdp  pp = symbols;
129733965Sjdp  ppend = symbols + symbol_count;
129833965Sjdp  for (; pp < ppend; pp++)
129933965Sjdp    {
130033965Sjdp      asymbol *p;
130133965Sjdp
130233965Sjdp      p = *pp;
130333965Sjdp
130433965Sjdp      if ((p->flags & (BSF_INDIRECT
130533965Sjdp		       | BSF_WARNING
130633965Sjdp		       | BSF_GLOBAL
130733965Sjdp		       | BSF_CONSTRUCTOR
130833965Sjdp		       | BSF_WEAK)) != 0
130933965Sjdp	  || bfd_is_und_section (bfd_get_section (p))
131033965Sjdp	  || bfd_is_com_section (bfd_get_section (p))
131133965Sjdp	  || bfd_is_ind_section (bfd_get_section (p)))
131233965Sjdp	{
131333965Sjdp	  const char *name;
131433965Sjdp	  const char *string;
131533965Sjdp	  struct generic_link_hash_entry *h;
1316107492Sobrien	  struct bfd_link_hash_entry *bh;
131733965Sjdp
131833965Sjdp	  name = bfd_asymbol_name (p);
131933965Sjdp	  if (((p->flags & BSF_INDIRECT) != 0
132033965Sjdp	       || bfd_is_ind_section (p->section))
132133965Sjdp	      && pp + 1 < ppend)
132233965Sjdp	    {
132333965Sjdp	      pp++;
132433965Sjdp	      string = bfd_asymbol_name (*pp);
132533965Sjdp	    }
132633965Sjdp	  else if ((p->flags & BSF_WARNING) != 0
132733965Sjdp		   && pp + 1 < ppend)
132833965Sjdp	    {
132933965Sjdp	      /* The name of P is actually the warning string, and the
133033965Sjdp		 next symbol is the one to warn about.  */
133133965Sjdp	      string = name;
133233965Sjdp	      pp++;
133333965Sjdp	      name = bfd_asymbol_name (*pp);
133433965Sjdp	    }
133533965Sjdp	  else
133633965Sjdp	    string = NULL;
133733965Sjdp
1338107492Sobrien	  bh = NULL;
133933965Sjdp	  if (! (_bfd_generic_link_add_one_symbol
134033965Sjdp		 (info, abfd, name, p->flags, bfd_get_section (p),
1341130561Sobrien		  p->value, string, FALSE, collect, &bh)))
1342130561Sobrien	    return FALSE;
1343107492Sobrien	  h = (struct generic_link_hash_entry *) bh;
134433965Sjdp
134533965Sjdp	  /* If this is a constructor symbol, and the linker didn't do
134633965Sjdp             anything with it, then we want to just pass the symbol
134733965Sjdp             through to the output file.  This will happen when
134833965Sjdp             linking with -r.  */
134933965Sjdp	  if ((p->flags & BSF_CONSTRUCTOR) != 0
135033965Sjdp	      && (h == NULL || h->root.type == bfd_link_hash_new))
135133965Sjdp	    {
135233965Sjdp	      p->udata.p = NULL;
135333965Sjdp	      continue;
135433965Sjdp	    }
135533965Sjdp
135633965Sjdp	  /* Save the BFD symbol so that we don't lose any backend
135733965Sjdp	     specific information that may be attached to it.  We only
135833965Sjdp	     want this one if it gives more information than the
135933965Sjdp	     existing one; we don't want to replace a defined symbol
136033965Sjdp	     with an undefined one.  This routine may be called with a
136133965Sjdp	     hash table other than the generic hash table, so we only
136233965Sjdp	     do this if we are certain that the hash table is a
136333965Sjdp	     generic one.  */
136433965Sjdp	  if (info->hash->creator == abfd->xvec)
136533965Sjdp	    {
1366130561Sobrien	      if (h->sym == NULL
136733965Sjdp		  || (! bfd_is_und_section (bfd_get_section (p))
136833965Sjdp		      && (! bfd_is_com_section (bfd_get_section (p))
136933965Sjdp			  || bfd_is_und_section (bfd_get_section (h->sym)))))
137033965Sjdp		{
137133965Sjdp		  h->sym = p;
137233965Sjdp		  /* BSF_OLD_COMMON is a hack to support COFF reloc
137333965Sjdp		     reading, and it should go away when the COFF
137433965Sjdp		     linker is switched to the new version.  */
137533965Sjdp		  if (bfd_is_com_section (bfd_get_section (p)))
137633965Sjdp		    p->flags |= BSF_OLD_COMMON;
137733965Sjdp		}
137838889Sjdp	    }
137933965Sjdp
138038889Sjdp	  /* Store a back pointer from the symbol to the hash
138138889Sjdp	     table entry for the benefit of relaxation code until
138238889Sjdp	     it gets rewritten to not use asymbol structures.
138338889Sjdp	     Setting this is also used to check whether these
138438889Sjdp	     symbols were set up by the generic linker.  */
1385130561Sobrien	  p->udata.p = h;
138633965Sjdp	}
138733965Sjdp    }
138833965Sjdp
1389130561Sobrien  return TRUE;
139033965Sjdp}
139133965Sjdp
139233965Sjdp/* We use a state table to deal with adding symbols from an object
139333965Sjdp   file.  The first index into the state table describes the symbol
139433965Sjdp   from the object file.  The second index into the state table is the
139533965Sjdp   type of the symbol in the hash table.  */
139633965Sjdp
139733965Sjdp/* The symbol from the object file is turned into one of these row
139833965Sjdp   values.  */
139933965Sjdp
140033965Sjdpenum link_row
140133965Sjdp{
140233965Sjdp  UNDEF_ROW,		/* Undefined.  */
140333965Sjdp  UNDEFW_ROW,		/* Weak undefined.  */
140433965Sjdp  DEF_ROW,		/* Defined.  */
140533965Sjdp  DEFW_ROW,		/* Weak defined.  */
140633965Sjdp  COMMON_ROW,		/* Common.  */
140733965Sjdp  INDR_ROW,		/* Indirect.  */
140833965Sjdp  WARN_ROW,		/* Warning.  */
140933965Sjdp  SET_ROW		/* Member of set.  */
141033965Sjdp};
141133965Sjdp
141233965Sjdp/* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
141333965Sjdp#undef FAIL
141433965Sjdp
141533965Sjdp/* The actions to take in the state table.  */
141633965Sjdp
141733965Sjdpenum link_action
141833965Sjdp{
141977298Sobrien  FAIL,		/* Abort.  */
142033965Sjdp  UND,		/* Mark symbol undefined.  */
142133965Sjdp  WEAK,		/* Mark symbol weak undefined.  */
142233965Sjdp  DEF,		/* Mark symbol defined.  */
142333965Sjdp  DEFW,		/* Mark symbol weak defined.  */
142433965Sjdp  COM,		/* Mark symbol common.  */
142533965Sjdp  REF,		/* Mark defined symbol referenced.  */
142633965Sjdp  CREF,		/* Possibly warn about common reference to defined symbol.  */
142733965Sjdp  CDEF,		/* Define existing common symbol.  */
142833965Sjdp  NOACT,	/* No action.  */
142933965Sjdp  BIG,		/* Mark symbol common using largest size.  */
143033965Sjdp  MDEF,		/* Multiple definition error.  */
143133965Sjdp  MIND,		/* Multiple indirect symbols.  */
143233965Sjdp  IND,		/* Make indirect symbol.  */
143333965Sjdp  CIND,		/* Make indirect symbol from existing common symbol.  */
143433965Sjdp  SET,		/* Add value to set.  */
143533965Sjdp  MWARN,	/* Make warning symbol.  */
143633965Sjdp  WARN,		/* Issue warning.  */
143733965Sjdp  CWARN,	/* Warn if referenced, else MWARN.  */
143833965Sjdp  CYCLE,	/* Repeat with symbol pointed to.  */
143933965Sjdp  REFC,		/* Mark indirect symbol referenced and then CYCLE.  */
144033965Sjdp  WARNC		/* Issue warning and then CYCLE.  */
144133965Sjdp};
144233965Sjdp
144333965Sjdp/* The state table itself.  The first index is a link_row and the
144433965Sjdp   second index is a bfd_link_hash_type.  */
144533965Sjdp
144633965Sjdpstatic const enum link_action link_action[8][8] =
144733965Sjdp{
144833965Sjdp  /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
144933965Sjdp  /* UNDEF_ROW 	*/  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
145033965Sjdp  /* UNDEFW_ROW	*/  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
145133965Sjdp  /* DEF_ROW 	*/  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
145233965Sjdp  /* DEFW_ROW 	*/  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
145389857Sobrien  /* COMMON_ROW	*/  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
145433965Sjdp  /* INDR_ROW	*/  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
145594536Sobrien  /* WARN_ROW   */  {MWARN, WARN,  WARN,  CWARN, CWARN, WARN,  CWARN, NOACT },
145633965Sjdp  /* SET_ROW	*/  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
145733965Sjdp};
145833965Sjdp
145933965Sjdp/* Most of the entries in the LINK_ACTION table are straightforward,
146033965Sjdp   but a few are somewhat subtle.
146133965Sjdp
146233965Sjdp   A reference to an indirect symbol (UNDEF_ROW/indr or
146333965Sjdp   UNDEFW_ROW/indr) is counted as a reference both to the indirect
146433965Sjdp   symbol and to the symbol the indirect symbol points to.
146533965Sjdp
146633965Sjdp   A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
146733965Sjdp   causes the warning to be issued.
146833965Sjdp
146933965Sjdp   A common definition of an indirect symbol (COMMON_ROW/indr) is
147033965Sjdp   treated as a multiple definition error.  Likewise for an indirect
147133965Sjdp   definition of a common symbol (INDR_ROW/com).
147233965Sjdp
147333965Sjdp   An indirect definition of a warning (INDR_ROW/warn) does not cause
147433965Sjdp   the warning to be issued.
147533965Sjdp
147633965Sjdp   If a warning is created for an indirect symbol (WARN_ROW/indr) no
147733965Sjdp   warning is created for the symbol the indirect symbol points to.
147833965Sjdp
147933965Sjdp   Adding an entry to a set does not count as a reference to a set,
148033965Sjdp   and no warning is issued (SET_ROW/warn).  */
148133965Sjdp
148233965Sjdp/* Return the BFD in which a hash entry has been defined, if known.  */
148333965Sjdp
148433965Sjdpstatic bfd *
1485130561Sobrienhash_entry_bfd (struct bfd_link_hash_entry *h)
148633965Sjdp{
148733965Sjdp  while (h->type == bfd_link_hash_warning)
148833965Sjdp    h = h->u.i.link;
148933965Sjdp  switch (h->type)
149033965Sjdp    {
149133965Sjdp    default:
149233965Sjdp      return NULL;
149333965Sjdp    case bfd_link_hash_undefined:
149433965Sjdp    case bfd_link_hash_undefweak:
149533965Sjdp      return h->u.undef.abfd;
149633965Sjdp    case bfd_link_hash_defined:
149733965Sjdp    case bfd_link_hash_defweak:
149833965Sjdp      return h->u.def.section->owner;
149933965Sjdp    case bfd_link_hash_common:
150033965Sjdp      return h->u.c.p->section->owner;
150133965Sjdp    }
150233965Sjdp  /*NOTREACHED*/
150333965Sjdp}
150433965Sjdp
150533965Sjdp/* Add a symbol to the global hash table.
150633965Sjdp   ABFD is the BFD the symbol comes from.
150733965Sjdp   NAME is the name of the symbol.
150833965Sjdp   FLAGS is the BSF_* bits associated with the symbol.
150933965Sjdp   SECTION is the section in which the symbol is defined; this may be
151033965Sjdp     bfd_und_section_ptr or bfd_com_section_ptr.
151133965Sjdp   VALUE is the value of the symbol, relative to the section.
151233965Sjdp   STRING is used for either an indirect symbol, in which case it is
151333965Sjdp     the name of the symbol to indirect to, or a warning symbol, in
151433965Sjdp     which case it is the warning string.
1515130561Sobrien   COPY is TRUE if NAME or STRING must be copied into locally
151633965Sjdp     allocated memory if they need to be saved.
1517130561Sobrien   COLLECT is TRUE if we should automatically collect gcc constructor
151833965Sjdp     or destructor names as collect2 does.
151933965Sjdp   HASHP, if not NULL, is a place to store the created hash table
152033965Sjdp     entry; if *HASHP is not NULL, the caller has already looked up
152177298Sobrien     the hash table entry, and stored it in *HASHP.  */
152233965Sjdp
1523130561Sobrienbfd_boolean
1524130561Sobrien_bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1525130561Sobrien				  bfd *abfd,
1526130561Sobrien				  const char *name,
1527130561Sobrien				  flagword flags,
1528130561Sobrien				  asection *section,
1529130561Sobrien				  bfd_vma value,
1530130561Sobrien				  const char *string,
1531130561Sobrien				  bfd_boolean copy,
1532130561Sobrien				  bfd_boolean collect,
1533130561Sobrien				  struct bfd_link_hash_entry **hashp)
153433965Sjdp{
153533965Sjdp  enum link_row row;
153633965Sjdp  struct bfd_link_hash_entry *h;
1537130561Sobrien  bfd_boolean cycle;
153833965Sjdp
153933965Sjdp  if (bfd_is_ind_section (section)
154033965Sjdp      || (flags & BSF_INDIRECT) != 0)
154133965Sjdp    row = INDR_ROW;
154233965Sjdp  else if ((flags & BSF_WARNING) != 0)
154333965Sjdp    row = WARN_ROW;
154433965Sjdp  else if ((flags & BSF_CONSTRUCTOR) != 0)
154533965Sjdp    row = SET_ROW;
154633965Sjdp  else if (bfd_is_und_section (section))
154733965Sjdp    {
154833965Sjdp      if ((flags & BSF_WEAK) != 0)
154933965Sjdp	row = UNDEFW_ROW;
155033965Sjdp      else
155133965Sjdp	row = UNDEF_ROW;
155233965Sjdp    }
155333965Sjdp  else if ((flags & BSF_WEAK) != 0)
155433965Sjdp    row = DEFW_ROW;
155533965Sjdp  else if (bfd_is_com_section (section))
155633965Sjdp    row = COMMON_ROW;
155733965Sjdp  else
155833965Sjdp    row = DEF_ROW;
155933965Sjdp
156033965Sjdp  if (hashp != NULL && *hashp != NULL)
156133965Sjdp    h = *hashp;
156233965Sjdp  else
156333965Sjdp    {
156433965Sjdp      if (row == UNDEF_ROW || row == UNDEFW_ROW)
1565130561Sobrien	h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
156633965Sjdp      else
1567130561Sobrien	h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
156833965Sjdp      if (h == NULL)
156933965Sjdp	{
157033965Sjdp	  if (hashp != NULL)
157133965Sjdp	    *hashp = NULL;
1572130561Sobrien	  return FALSE;
157333965Sjdp	}
157433965Sjdp    }
157533965Sjdp
157633965Sjdp  if (info->notice_all
1577130561Sobrien      || (info->notice_hash != NULL
1578130561Sobrien	  && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
157933965Sjdp    {
158033965Sjdp      if (! (*info->callbacks->notice) (info, h->root.string, abfd, section,
158133965Sjdp					value))
1582130561Sobrien	return FALSE;
158333965Sjdp    }
158433965Sjdp
1585130561Sobrien  if (hashp != NULL)
158633965Sjdp    *hashp = h;
158733965Sjdp
158833965Sjdp  do
158933965Sjdp    {
159033965Sjdp      enum link_action action;
159133965Sjdp
1592130561Sobrien      cycle = FALSE;
159333965Sjdp      action = link_action[(int) row][(int) h->type];
159433965Sjdp      switch (action)
159533965Sjdp	{
159633965Sjdp	case FAIL:
159733965Sjdp	  abort ();
159833965Sjdp
159933965Sjdp	case NOACT:
160033965Sjdp	  /* Do nothing.  */
160133965Sjdp	  break;
160233965Sjdp
160333965Sjdp	case UND:
160433965Sjdp	  /* Make a new undefined symbol.  */
160533965Sjdp	  h->type = bfd_link_hash_undefined;
160633965Sjdp	  h->u.undef.abfd = abfd;
160733965Sjdp	  bfd_link_add_undef (info->hash, h);
160833965Sjdp	  break;
160933965Sjdp
161033965Sjdp	case WEAK:
161133965Sjdp	  /* Make a new weak undefined symbol.  */
161233965Sjdp	  h->type = bfd_link_hash_undefweak;
161333965Sjdp	  h->u.undef.abfd = abfd;
1614218822Sdim	  h->u.undef.weak = abfd;
161533965Sjdp	  break;
161633965Sjdp
161733965Sjdp	case CDEF:
161833965Sjdp	  /* We have found a definition for a symbol which was
161933965Sjdp	     previously common.  */
162033965Sjdp	  BFD_ASSERT (h->type == bfd_link_hash_common);
162133965Sjdp	  if (! ((*info->callbacks->multiple_common)
162233965Sjdp		 (info, h->root.string,
162333965Sjdp		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1624130561Sobrien		  abfd, bfd_link_hash_defined, 0)))
1625130561Sobrien	    return FALSE;
162633965Sjdp	  /* Fall through.  */
162733965Sjdp	case DEF:
162833965Sjdp	case DEFW:
162933965Sjdp	  {
163033965Sjdp	    enum bfd_link_hash_type oldtype;
163133965Sjdp
163233965Sjdp	    /* Define a symbol.  */
163333965Sjdp	    oldtype = h->type;
163433965Sjdp	    if (action == DEFW)
163533965Sjdp	      h->type = bfd_link_hash_defweak;
163633965Sjdp	    else
163733965Sjdp	      h->type = bfd_link_hash_defined;
163833965Sjdp	    h->u.def.section = section;
163933965Sjdp	    h->u.def.value = value;
164033965Sjdp
164133965Sjdp	    /* If we have been asked to, we act like collect2 and
164233965Sjdp	       identify all functions that might be global
164333965Sjdp	       constructors and destructors and pass them up in a
164433965Sjdp	       callback.  We only do this for certain object file
164533965Sjdp	       types, since many object file types can handle this
164633965Sjdp	       automatically.  */
164733965Sjdp	    if (collect && name[0] == '_')
164833965Sjdp	      {
164933965Sjdp		const char *s;
165033965Sjdp
165133965Sjdp		/* A constructor or destructor name starts like this:
165233965Sjdp		   _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
165333965Sjdp		   the second are the same character (we accept any
165433965Sjdp		   character there, in case a new object file format
165533965Sjdp		   comes along with even worse naming restrictions).  */
165633965Sjdp
165733965Sjdp#define CONS_PREFIX "GLOBAL_"
165833965Sjdp#define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
165933965Sjdp
166033965Sjdp		s = name + 1;
166133965Sjdp		while (*s == '_')
166233965Sjdp		  ++s;
1663218822Sdim		if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
166433965Sjdp		  {
166533965Sjdp		    char c;
166633965Sjdp
166733965Sjdp		    c = s[CONS_PREFIX_LEN + 1];
166833965Sjdp		    if ((c == 'I' || c == 'D')
166933965Sjdp			&& s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
167033965Sjdp		      {
167133965Sjdp			/* If this is a definition of a symbol which
167233965Sjdp                           was previously weakly defined, we are in
167333965Sjdp                           trouble.  We have already added a
167433965Sjdp                           constructor entry for the weak defined
167533965Sjdp                           symbol, and now we are trying to add one
167633965Sjdp                           for the new symbol.  Fortunately, this case
167733965Sjdp                           should never arise in practice.  */
167833965Sjdp			if (oldtype == bfd_link_hash_defweak)
167933965Sjdp			  abort ();
168033965Sjdp
168133965Sjdp			if (! ((*info->callbacks->constructor)
1682104834Sobrien			       (info, c == 'I',
168333965Sjdp				h->root.string, abfd, section, value)))
1684130561Sobrien			  return FALSE;
168533965Sjdp		      }
168633965Sjdp		  }
168733965Sjdp	      }
168833965Sjdp	  }
168933965Sjdp
169033965Sjdp	  break;
169133965Sjdp
169233965Sjdp	case COM:
169333965Sjdp	  /* We have found a common definition for a symbol.  */
169433965Sjdp	  if (h->type == bfd_link_hash_new)
169533965Sjdp	    bfd_link_add_undef (info->hash, h);
169633965Sjdp	  h->type = bfd_link_hash_common;
169733965Sjdp	  h->u.c.p =
1698130561Sobrien	    bfd_hash_allocate (&info->hash->table,
1699130561Sobrien			       sizeof (struct bfd_link_hash_common_entry));
170033965Sjdp	  if (h->u.c.p == NULL)
1701130561Sobrien	    return FALSE;
170233965Sjdp
170333965Sjdp	  h->u.c.size = value;
170433965Sjdp
170533965Sjdp	  /* Select a default alignment based on the size.  This may
170633965Sjdp             be overridden by the caller.  */
170733965Sjdp	  {
170833965Sjdp	    unsigned int power;
170933965Sjdp
171033965Sjdp	    power = bfd_log2 (value);
171133965Sjdp	    if (power > 4)
171233965Sjdp	      power = 4;
171333965Sjdp	    h->u.c.p->alignment_power = power;
171433965Sjdp	  }
171533965Sjdp
171633965Sjdp	  /* The section of a common symbol is only used if the common
171733965Sjdp             symbol is actually allocated.  It basically provides a
171833965Sjdp             hook for the linker script to decide which output section
171933965Sjdp             the common symbols should be put in.  In most cases, the
172033965Sjdp             section of a common symbol will be bfd_com_section_ptr,
172133965Sjdp             the code here will choose a common symbol section named
172233965Sjdp             "COMMON", and the linker script will contain *(COMMON) in
172333965Sjdp             the appropriate place.  A few targets use separate common
172433965Sjdp             sections for small symbols, and they require special
172533965Sjdp             handling.  */
172633965Sjdp	  if (section == bfd_com_section_ptr)
172733965Sjdp	    {
172833965Sjdp	      h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
172933965Sjdp	      h->u.c.p->section->flags = SEC_ALLOC;
173033965Sjdp	    }
173133965Sjdp	  else if (section->owner != abfd)
173233965Sjdp	    {
173333965Sjdp	      h->u.c.p->section = bfd_make_section_old_way (abfd,
173433965Sjdp							    section->name);
173533965Sjdp	      h->u.c.p->section->flags = SEC_ALLOC;
173633965Sjdp	    }
173733965Sjdp	  else
173833965Sjdp	    h->u.c.p->section = section;
173933965Sjdp	  break;
174033965Sjdp
174133965Sjdp	case REF:
174233965Sjdp	  /* A reference to a defined symbol.  */
1743218822Sdim	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1744218822Sdim	    h->u.undef.next = h;
174533965Sjdp	  break;
174633965Sjdp
174733965Sjdp	case BIG:
174833965Sjdp	  /* We have found a common definition for a symbol which
174933965Sjdp	     already had a common definition.  Use the maximum of the
175089857Sobrien	     two sizes, and use the section required by the larger symbol.  */
175133965Sjdp	  BFD_ASSERT (h->type == bfd_link_hash_common);
175233965Sjdp	  if (! ((*info->callbacks->multiple_common)
175333965Sjdp		 (info, h->root.string,
175433965Sjdp		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
175533965Sjdp		  abfd, bfd_link_hash_common, value)))
1756130561Sobrien	    return FALSE;
175733965Sjdp	  if (value > h->u.c.size)
175833965Sjdp	    {
175933965Sjdp	      unsigned int power;
176033965Sjdp
176133965Sjdp	      h->u.c.size = value;
176233965Sjdp
176333965Sjdp	      /* Select a default alignment based on the size.  This may
176433965Sjdp		 be overridden by the caller.  */
176533965Sjdp	      power = bfd_log2 (value);
176633965Sjdp	      if (power > 4)
176733965Sjdp		power = 4;
176833965Sjdp	      h->u.c.p->alignment_power = power;
176989857Sobrien
177089857Sobrien	      /* Some systems have special treatment for small commons,
177189857Sobrien		 hence we want to select the section used by the larger
177289857Sobrien		 symbol.  This makes sure the symbol does not go in a
177389857Sobrien		 small common section if it is now too large.  */
177489857Sobrien	      if (section == bfd_com_section_ptr)
177589857Sobrien		{
177689857Sobrien		  h->u.c.p->section
177789857Sobrien		    = bfd_make_section_old_way (abfd, "COMMON");
177889857Sobrien		  h->u.c.p->section->flags = SEC_ALLOC;
177989857Sobrien		}
178089857Sobrien	      else if (section->owner != abfd)
178189857Sobrien		{
178289857Sobrien		  h->u.c.p->section
178389857Sobrien		    = bfd_make_section_old_way (abfd, section->name);
178489857Sobrien		  h->u.c.p->section->flags = SEC_ALLOC;
178589857Sobrien		}
178689857Sobrien	      else
178789857Sobrien		h->u.c.p->section = section;
178833965Sjdp	    }
178933965Sjdp	  break;
179033965Sjdp
179133965Sjdp	case CREF:
179233965Sjdp	  {
179333965Sjdp	    bfd *obfd;
179433965Sjdp
179533965Sjdp	    /* We have found a common definition for a symbol which
179633965Sjdp	       was already defined.  FIXME: It would nice if we could
179733965Sjdp	       report the BFD which defined an indirect symbol, but we
179833965Sjdp	       don't have anywhere to store the information.  */
179933965Sjdp	    if (h->type == bfd_link_hash_defined
180033965Sjdp		|| h->type == bfd_link_hash_defweak)
180133965Sjdp	      obfd = h->u.def.section->owner;
180233965Sjdp	    else
180333965Sjdp	      obfd = NULL;
180433965Sjdp	    if (! ((*info->callbacks->multiple_common)
1805130561Sobrien		   (info, h->root.string, obfd, h->type, 0,
180633965Sjdp		    abfd, bfd_link_hash_common, value)))
1807130561Sobrien	      return FALSE;
180833965Sjdp	  }
180933965Sjdp	  break;
181033965Sjdp
181133965Sjdp	case MIND:
181233965Sjdp	  /* Multiple indirect symbols.  This is OK if they both point
181333965Sjdp	     to the same symbol.  */
181433965Sjdp	  if (strcmp (h->u.i.link->root.string, string) == 0)
181533965Sjdp	    break;
181633965Sjdp	  /* Fall through.  */
181733965Sjdp	case MDEF:
181833965Sjdp	  /* Handle a multiple definition.  */
1819104834Sobrien	  if (!info->allow_multiple_definition)
1820104834Sobrien	    {
1821104834Sobrien	      asection *msec = NULL;
1822104834Sobrien	      bfd_vma mval = 0;
182333965Sjdp
1824104834Sobrien	      switch (h->type)
1825104834Sobrien		{
1826104834Sobrien		case bfd_link_hash_defined:
1827104834Sobrien		  msec = h->u.def.section;
1828104834Sobrien		  mval = h->u.def.value;
1829104834Sobrien		  break;
1830104834Sobrien	        case bfd_link_hash_indirect:
1831104834Sobrien		  msec = bfd_ind_section_ptr;
1832104834Sobrien		  mval = 0;
1833104834Sobrien		  break;
1834104834Sobrien		default:
1835104834Sobrien		  abort ();
1836104834Sobrien		}
1837104834Sobrien
1838104834Sobrien	      /* Ignore a redefinition of an absolute symbol to the
1839104834Sobrien		 same value; it's harmless.  */
1840104834Sobrien	      if (h->type == bfd_link_hash_defined
1841104834Sobrien		  && bfd_is_abs_section (msec)
1842104834Sobrien		  && bfd_is_abs_section (section)
1843104834Sobrien		  && value == mval)
184433965Sjdp		break;
184533965Sjdp
1846104834Sobrien	      if (! ((*info->callbacks->multiple_definition)
1847104834Sobrien		     (info, h->root.string, msec->owner, msec, mval,
1848104834Sobrien		      abfd, section, value)))
1849130561Sobrien		return FALSE;
1850104834Sobrien	    }
185133965Sjdp	  break;
185233965Sjdp
185333965Sjdp	case CIND:
185433965Sjdp	  /* Create an indirect symbol from an existing common symbol.  */
185533965Sjdp	  BFD_ASSERT (h->type == bfd_link_hash_common);
185633965Sjdp	  if (! ((*info->callbacks->multiple_common)
185733965Sjdp		 (info, h->root.string,
185833965Sjdp		  h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1859130561Sobrien		  abfd, bfd_link_hash_indirect, 0)))
1860130561Sobrien	    return FALSE;
186133965Sjdp	  /* Fall through.  */
186233965Sjdp	case IND:
186333965Sjdp	  /* Create an indirect symbol.  */
186433965Sjdp	  {
186533965Sjdp	    struct bfd_link_hash_entry *inh;
186633965Sjdp
186733965Sjdp	    /* STRING is the name of the symbol we want to indirect
186833965Sjdp	       to.  */
1869130561Sobrien	    inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1870130561Sobrien						copy, FALSE);
1871130561Sobrien	    if (inh == NULL)
1872130561Sobrien	      return FALSE;
187378828Sobrien	    if (inh->type == bfd_link_hash_indirect
187478828Sobrien		&& inh->u.i.link == h)
187578828Sobrien	      {
187678828Sobrien		(*_bfd_error_handler)
1877218822Sdim		  (_("%B: indirect symbol `%s' to `%s' is a loop"),
1878218822Sdim		   abfd, name, string);
187978828Sobrien		bfd_set_error (bfd_error_invalid_operation);
1880130561Sobrien		return FALSE;
188178828Sobrien	      }
188233965Sjdp	    if (inh->type == bfd_link_hash_new)
188333965Sjdp	      {
188433965Sjdp		inh->type = bfd_link_hash_undefined;
188533965Sjdp		inh->u.undef.abfd = abfd;
188633965Sjdp		bfd_link_add_undef (info->hash, inh);
188733965Sjdp	      }
188833965Sjdp
188933965Sjdp	    /* If the indirect symbol has been referenced, we need to
189033965Sjdp	       push the reference down to the symbol we are
189133965Sjdp	       referencing.  */
189233965Sjdp	    if (h->type != bfd_link_hash_new)
189333965Sjdp	      {
189433965Sjdp		row = UNDEF_ROW;
1895130561Sobrien		cycle = TRUE;
189633965Sjdp	      }
189733965Sjdp
189833965Sjdp	    h->type = bfd_link_hash_indirect;
189933965Sjdp	    h->u.i.link = inh;
190033965Sjdp	  }
190133965Sjdp	  break;
190233965Sjdp
190333965Sjdp	case SET:
190433965Sjdp	  /* Add an entry to a set.  */
190533965Sjdp	  if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
190633965Sjdp						abfd, section, value))
1907130561Sobrien	    return FALSE;
190833965Sjdp	  break;
190933965Sjdp
191033965Sjdp	case WARNC:
191133965Sjdp	  /* Issue a warning and cycle.  */
191233965Sjdp	  if (h->u.i.warning != NULL)
191333965Sjdp	    {
191433965Sjdp	      if (! (*info->callbacks->warning) (info, h->u.i.warning,
191533965Sjdp						 h->root.string, abfd,
1916130561Sobrien						 NULL, 0))
1917130561Sobrien		return FALSE;
191833965Sjdp	      /* Only issue a warning once.  */
191933965Sjdp	      h->u.i.warning = NULL;
192033965Sjdp	    }
192133965Sjdp	  /* Fall through.  */
192233965Sjdp	case CYCLE:
192333965Sjdp	  /* Try again with the referenced symbol.  */
192433965Sjdp	  h = h->u.i.link;
1925130561Sobrien	  cycle = TRUE;
192633965Sjdp	  break;
192733965Sjdp
192833965Sjdp	case REFC:
192933965Sjdp	  /* A reference to an indirect symbol.  */
1930218822Sdim	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1931218822Sdim	    h->u.undef.next = h;
193233965Sjdp	  h = h->u.i.link;
1933130561Sobrien	  cycle = TRUE;
193433965Sjdp	  break;
193533965Sjdp
193633965Sjdp	case WARN:
193733965Sjdp	  /* Issue a warning.  */
193833965Sjdp	  if (! (*info->callbacks->warning) (info, string, h->root.string,
1939130561Sobrien					     hash_entry_bfd (h), NULL, 0))
1940130561Sobrien	    return FALSE;
194133965Sjdp	  break;
194233965Sjdp
194333965Sjdp	case CWARN:
194433965Sjdp	  /* Warn if this symbol has been referenced already,
194533965Sjdp	     otherwise add a warning.  A symbol has been referenced if
1946218822Sdim	     the u.undef.next field is not NULL, or it is the tail of the
194733965Sjdp	     undefined symbol list.  The REF case above helps to
194833965Sjdp	     ensure this.  */
1949218822Sdim	  if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
195033965Sjdp	    {
195133965Sjdp	      if (! (*info->callbacks->warning) (info, string, h->root.string,
1952130561Sobrien						 hash_entry_bfd (h), NULL, 0))
1953130561Sobrien		return FALSE;
195433965Sjdp	      break;
195533965Sjdp	    }
195633965Sjdp	  /* Fall through.  */
195733965Sjdp	case MWARN:
195833965Sjdp	  /* Make a warning symbol.  */
195933965Sjdp	  {
196033965Sjdp	    struct bfd_link_hash_entry *sub;
196133965Sjdp
196233965Sjdp	    /* STRING is the warning to give.  */
196333965Sjdp	    sub = ((struct bfd_link_hash_entry *)
196433965Sjdp		   ((*info->hash->table.newfunc)
1965130561Sobrien		    (NULL, &info->hash->table, h->root.string)));
196633965Sjdp	    if (sub == NULL)
1967130561Sobrien	      return FALSE;
196833965Sjdp	    *sub = *h;
196933965Sjdp	    sub->type = bfd_link_hash_warning;
197033965Sjdp	    sub->u.i.link = h;
197133965Sjdp	    if (! copy)
197233965Sjdp	      sub->u.i.warning = string;
197333965Sjdp	    else
197433965Sjdp	      {
197533965Sjdp		char *w;
1976104834Sobrien		size_t len = strlen (string) + 1;
197733965Sjdp
1978104834Sobrien		w = bfd_hash_allocate (&info->hash->table, len);
197933965Sjdp		if (w == NULL)
1980130561Sobrien		  return FALSE;
1981104834Sobrien		memcpy (w, string, len);
198233965Sjdp		sub->u.i.warning = w;
198333965Sjdp	      }
198433965Sjdp
198533965Sjdp	    bfd_hash_replace (&info->hash->table,
198633965Sjdp			      (struct bfd_hash_entry *) h,
198733965Sjdp			      (struct bfd_hash_entry *) sub);
198833965Sjdp	    if (hashp != NULL)
198933965Sjdp	      *hashp = sub;
199033965Sjdp	  }
199133965Sjdp	  break;
199233965Sjdp	}
199333965Sjdp    }
199433965Sjdp  while (cycle);
199533965Sjdp
1996130561Sobrien  return TRUE;
199733965Sjdp}
199833965Sjdp
199933965Sjdp/* Generic final link routine.  */
200033965Sjdp
2001130561Sobrienbfd_boolean
2002130561Sobrien_bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
200333965Sjdp{
200433965Sjdp  bfd *sub;
200533965Sjdp  asection *o;
200633965Sjdp  struct bfd_link_order *p;
200733965Sjdp  size_t outsymalloc;
200833965Sjdp  struct generic_write_global_symbol_info wginfo;
200933965Sjdp
2010130561Sobrien  bfd_get_outsymbols (abfd) = NULL;
201160484Sobrien  bfd_get_symcount (abfd) = 0;
201233965Sjdp  outsymalloc = 0;
201333965Sjdp
201433965Sjdp  /* Mark all sections which will be included in the output file.  */
201533965Sjdp  for (o = abfd->sections; o != NULL; o = o->next)
2016218822Sdim    for (p = o->map_head.link_order; p != NULL; p = p->next)
201733965Sjdp      if (p->type == bfd_indirect_link_order)
2018130561Sobrien	p->u.indirect.section->linker_mark = TRUE;
201933965Sjdp
202033965Sjdp  /* Build the output symbol table.  */
2021130561Sobrien  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
202233965Sjdp    if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
2023130561Sobrien      return FALSE;
202433965Sjdp
202533965Sjdp  /* Accumulate the global symbols.  */
202633965Sjdp  wginfo.info = info;
202733965Sjdp  wginfo.output_bfd = abfd;
202833965Sjdp  wginfo.psymalloc = &outsymalloc;
202933965Sjdp  _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
203033965Sjdp				   _bfd_generic_link_write_global_symbol,
2031130561Sobrien				   &wginfo);
203233965Sjdp
203360484Sobrien  /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
203460484Sobrien     shouldn't really need one, since we have SYMCOUNT, but some old
203560484Sobrien     code still expects one.  */
203660484Sobrien  if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
2037130561Sobrien    return FALSE;
203860484Sobrien
2039130561Sobrien  if (info->relocatable)
204033965Sjdp    {
204133965Sjdp      /* Allocate space for the output relocs for each section.  */
2042130561Sobrien      for (o = abfd->sections; o != NULL; o = o->next)
204333965Sjdp	{
204433965Sjdp	  o->reloc_count = 0;
2045218822Sdim	  for (p = o->map_head.link_order; p != NULL; p = p->next)
204633965Sjdp	    {
204733965Sjdp	      if (p->type == bfd_section_reloc_link_order
204833965Sjdp		  || p->type == bfd_symbol_reloc_link_order)
204933965Sjdp		++o->reloc_count;
205033965Sjdp	      else if (p->type == bfd_indirect_link_order)
205133965Sjdp		{
205233965Sjdp		  asection *input_section;
205333965Sjdp		  bfd *input_bfd;
205433965Sjdp		  long relsize;
205533965Sjdp		  arelent **relocs;
205633965Sjdp		  asymbol **symbols;
205733965Sjdp		  long reloc_count;
205833965Sjdp
205933965Sjdp		  input_section = p->u.indirect.section;
206033965Sjdp		  input_bfd = input_section->owner;
206133965Sjdp		  relsize = bfd_get_reloc_upper_bound (input_bfd,
206233965Sjdp						       input_section);
206333965Sjdp		  if (relsize < 0)
2064130561Sobrien		    return FALSE;
2065130561Sobrien		  relocs = bfd_malloc (relsize);
206633965Sjdp		  if (!relocs && relsize != 0)
2067130561Sobrien		    return FALSE;
206833965Sjdp		  symbols = _bfd_generic_link_get_symbols (input_bfd);
206933965Sjdp		  reloc_count = bfd_canonicalize_reloc (input_bfd,
207033965Sjdp							input_section,
207133965Sjdp							relocs,
207233965Sjdp							symbols);
2073130561Sobrien		  free (relocs);
207433965Sjdp		  if (reloc_count < 0)
2075130561Sobrien		    return FALSE;
207633965Sjdp		  BFD_ASSERT ((unsigned long) reloc_count
207733965Sjdp			      == input_section->reloc_count);
207833965Sjdp		  o->reloc_count += reloc_count;
207933965Sjdp		}
208033965Sjdp	    }
208133965Sjdp	  if (o->reloc_count > 0)
208233965Sjdp	    {
208389857Sobrien	      bfd_size_type amt;
208489857Sobrien
208589857Sobrien	      amt = o->reloc_count;
208689857Sobrien	      amt *= sizeof (arelent *);
2087130561Sobrien	      o->orelocation = bfd_alloc (abfd, amt);
208833965Sjdp	      if (!o->orelocation)
2089130561Sobrien		return FALSE;
209033965Sjdp	      o->flags |= SEC_RELOC;
209133965Sjdp	      /* Reset the count so that it can be used as an index
209233965Sjdp		 when putting in the output relocs.  */
209333965Sjdp	      o->reloc_count = 0;
209433965Sjdp	    }
209533965Sjdp	}
209633965Sjdp    }
209733965Sjdp
209833965Sjdp  /* Handle all the link order information for the sections.  */
2099130561Sobrien  for (o = abfd->sections; o != NULL; o = o->next)
210033965Sjdp    {
2101218822Sdim      for (p = o->map_head.link_order; p != NULL; p = p->next)
210233965Sjdp	{
210333965Sjdp	  switch (p->type)
210433965Sjdp	    {
210533965Sjdp	    case bfd_section_reloc_link_order:
210633965Sjdp	    case bfd_symbol_reloc_link_order:
210733965Sjdp	      if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2108130561Sobrien		return FALSE;
210933965Sjdp	      break;
211033965Sjdp	    case bfd_indirect_link_order:
2111130561Sobrien	      if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2112130561Sobrien		return FALSE;
211333965Sjdp	      break;
211433965Sjdp	    default:
211533965Sjdp	      if (! _bfd_default_link_order (abfd, info, o, p))
2116130561Sobrien		return FALSE;
211733965Sjdp	      break;
211833965Sjdp	    }
211933965Sjdp	}
212033965Sjdp    }
212177298Sobrien
2122130561Sobrien  return TRUE;
212333965Sjdp}
212433965Sjdp
212533965Sjdp/* Add an output symbol to the output BFD.  */
212633965Sjdp
2127130561Sobrienstatic bfd_boolean
2128130561Sobriengeneric_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
212933965Sjdp{
213060484Sobrien  if (bfd_get_symcount (output_bfd) >= *psymalloc)
213133965Sjdp    {
213233965Sjdp      asymbol **newsyms;
213389857Sobrien      bfd_size_type amt;
213433965Sjdp
213533965Sjdp      if (*psymalloc == 0)
213633965Sjdp	*psymalloc = 124;
213733965Sjdp      else
213833965Sjdp	*psymalloc *= 2;
213989857Sobrien      amt = *psymalloc;
214089857Sobrien      amt *= sizeof (asymbol *);
2141130561Sobrien      newsyms = bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2142130561Sobrien      if (newsyms == NULL)
2143130561Sobrien	return FALSE;
214460484Sobrien      bfd_get_outsymbols (output_bfd) = newsyms;
214533965Sjdp    }
214633965Sjdp
214760484Sobrien  bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
214860484Sobrien  if (sym != NULL)
214960484Sobrien    ++ bfd_get_symcount (output_bfd);
215033965Sjdp
2151130561Sobrien  return TRUE;
215233965Sjdp}
215333965Sjdp
215433965Sjdp/* Handle the symbols for an input BFD.  */
215533965Sjdp
2156130561Sobrienbfd_boolean
2157130561Sobrien_bfd_generic_link_output_symbols (bfd *output_bfd,
2158130561Sobrien				  bfd *input_bfd,
2159130561Sobrien				  struct bfd_link_info *info,
2160130561Sobrien				  size_t *psymalloc)
216133965Sjdp{
216233965Sjdp  asymbol **sym_ptr;
216333965Sjdp  asymbol **sym_end;
216433965Sjdp
216533965Sjdp  if (! generic_link_read_symbols (input_bfd))
2166130561Sobrien    return FALSE;
216733965Sjdp
216833965Sjdp  /* Create a filename symbol if we are supposed to.  */
2169130561Sobrien  if (info->create_object_symbols_section != NULL)
217033965Sjdp    {
217133965Sjdp      asection *sec;
217233965Sjdp
2173130561Sobrien      for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
217433965Sjdp	{
217533965Sjdp	  if (sec->output_section == info->create_object_symbols_section)
217633965Sjdp	    {
217733965Sjdp	      asymbol *newsym;
217833965Sjdp
217933965Sjdp	      newsym = bfd_make_empty_symbol (input_bfd);
218033965Sjdp	      if (!newsym)
2181130561Sobrien		return FALSE;
218233965Sjdp	      newsym->name = input_bfd->filename;
218333965Sjdp	      newsym->value = 0;
218433965Sjdp	      newsym->flags = BSF_LOCAL | BSF_FILE;
218533965Sjdp	      newsym->section = sec;
218633965Sjdp
218733965Sjdp	      if (! generic_add_output_symbol (output_bfd, psymalloc,
218833965Sjdp					       newsym))
2189130561Sobrien		return FALSE;
219033965Sjdp
219133965Sjdp	      break;
219233965Sjdp	    }
219333965Sjdp	}
219433965Sjdp    }
219533965Sjdp
219633965Sjdp  /* Adjust the values of the globally visible symbols, and write out
219733965Sjdp     local symbols.  */
219833965Sjdp  sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
219933965Sjdp  sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
220033965Sjdp  for (; sym_ptr < sym_end; sym_ptr++)
220133965Sjdp    {
220233965Sjdp      asymbol *sym;
220333965Sjdp      struct generic_link_hash_entry *h;
2204130561Sobrien      bfd_boolean output;
220533965Sjdp
2206130561Sobrien      h = NULL;
220733965Sjdp      sym = *sym_ptr;
220833965Sjdp      if ((sym->flags & (BSF_INDIRECT
220933965Sjdp			 | BSF_WARNING
221033965Sjdp			 | BSF_GLOBAL
221133965Sjdp			 | BSF_CONSTRUCTOR
221233965Sjdp			 | BSF_WEAK)) != 0
221333965Sjdp	  || bfd_is_und_section (bfd_get_section (sym))
221433965Sjdp	  || bfd_is_com_section (bfd_get_section (sym))
221533965Sjdp	  || bfd_is_ind_section (bfd_get_section (sym)))
221633965Sjdp	{
221733965Sjdp	  if (sym->udata.p != NULL)
2218130561Sobrien	    h = sym->udata.p;
221933965Sjdp	  else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
222033965Sjdp	    {
222133965Sjdp	      /* This case normally means that the main linker code
222233965Sjdp                 deliberately ignored this constructor symbol.  We
222333965Sjdp                 should just pass it through.  This will screw up if
222433965Sjdp                 the constructor symbol is from a different,
222533965Sjdp                 non-generic, object file format, but the case will
222633965Sjdp                 only arise when linking with -r, which will probably
222733965Sjdp                 fail anyhow, since there will be no way to represent
222833965Sjdp                 the relocs in the output format being used.  */
222933965Sjdp	      h = NULL;
223033965Sjdp	    }
223133965Sjdp	  else if (bfd_is_und_section (bfd_get_section (sym)))
223233965Sjdp	    h = ((struct generic_link_hash_entry *)
223333965Sjdp		 bfd_wrapped_link_hash_lookup (output_bfd, info,
223433965Sjdp					       bfd_asymbol_name (sym),
2235130561Sobrien					       FALSE, FALSE, TRUE));
223633965Sjdp	  else
223733965Sjdp	    h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
223833965Sjdp					       bfd_asymbol_name (sym),
2239130561Sobrien					       FALSE, FALSE, TRUE);
224033965Sjdp
2241130561Sobrien	  if (h != NULL)
224233965Sjdp	    {
224333965Sjdp	      /* Force all references to this symbol to point to
224433965Sjdp		 the same area in memory.  It is possible that
224533965Sjdp		 this routine will be called with a hash table
224633965Sjdp		 other than a generic hash table, so we double
224733965Sjdp		 check that.  */
224833965Sjdp	      if (info->hash->creator == input_bfd->xvec)
224933965Sjdp		{
2250130561Sobrien		  if (h->sym != NULL)
225133965Sjdp		    *sym_ptr = sym = h->sym;
225233965Sjdp		}
225333965Sjdp
225433965Sjdp	      switch (h->root.type)
225533965Sjdp		{
225633965Sjdp		default:
225733965Sjdp		case bfd_link_hash_new:
225833965Sjdp		  abort ();
225933965Sjdp		case bfd_link_hash_undefined:
226033965Sjdp		  break;
226133965Sjdp		case bfd_link_hash_undefweak:
226233965Sjdp		  sym->flags |= BSF_WEAK;
226333965Sjdp		  break;
226433965Sjdp		case bfd_link_hash_indirect:
226533965Sjdp		  h = (struct generic_link_hash_entry *) h->root.u.i.link;
226633965Sjdp		  /* fall through */
226733965Sjdp		case bfd_link_hash_defined:
226833965Sjdp		  sym->flags |= BSF_GLOBAL;
226933965Sjdp		  sym->flags &=~ BSF_CONSTRUCTOR;
227033965Sjdp		  sym->value = h->root.u.def.value;
227133965Sjdp		  sym->section = h->root.u.def.section;
227233965Sjdp		  break;
227333965Sjdp		case bfd_link_hash_defweak:
227433965Sjdp		  sym->flags |= BSF_WEAK;
227533965Sjdp		  sym->flags &=~ BSF_CONSTRUCTOR;
227633965Sjdp		  sym->value = h->root.u.def.value;
227733965Sjdp		  sym->section = h->root.u.def.section;
227833965Sjdp		  break;
227933965Sjdp		case bfd_link_hash_common:
228033965Sjdp		  sym->value = h->root.u.c.size;
228133965Sjdp		  sym->flags |= BSF_GLOBAL;
228233965Sjdp		  if (! bfd_is_com_section (sym->section))
228333965Sjdp		    {
228433965Sjdp		      BFD_ASSERT (bfd_is_und_section (sym->section));
228533965Sjdp		      sym->section = bfd_com_section_ptr;
228633965Sjdp		    }
228733965Sjdp		  /* We do not set the section of the symbol to
228833965Sjdp		     h->root.u.c.p->section.  That value was saved so
228933965Sjdp		     that we would know where to allocate the symbol
229033965Sjdp		     if it was defined.  In this case the type is
229133965Sjdp		     still bfd_link_hash_common, so we did not define
229233965Sjdp		     it, so we do not want to use that section.  */
229333965Sjdp		  break;
229433965Sjdp		}
229533965Sjdp	    }
229633965Sjdp	}
229733965Sjdp
229833965Sjdp      /* This switch is straight from the old code in
229933965Sjdp	 write_file_locals in ldsym.c.  */
230033965Sjdp      if (info->strip == strip_all
230133965Sjdp	  || (info->strip == strip_some
2302130561Sobrien	      && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2303130561Sobrien				  FALSE, FALSE) == NULL))
2304130561Sobrien	output = FALSE;
230533965Sjdp      else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
230633965Sjdp	{
230733965Sjdp	  /* If this symbol is marked as occurring now, rather
230833965Sjdp	     than at the end, output it now.  This is used for
230933965Sjdp	     COFF C_EXT FCN symbols.  FIXME: There must be a
231033965Sjdp	     better way.  */
231133965Sjdp	  if (bfd_asymbol_bfd (sym) == input_bfd
231233965Sjdp	      && (sym->flags & BSF_NOT_AT_END) != 0)
2313130561Sobrien	    output = TRUE;
231433965Sjdp	  else
2315130561Sobrien	    output = FALSE;
231633965Sjdp	}
231733965Sjdp      else if (bfd_is_ind_section (sym->section))
2318130561Sobrien	output = FALSE;
231933965Sjdp      else if ((sym->flags & BSF_DEBUGGING) != 0)
232033965Sjdp	{
232133965Sjdp	  if (info->strip == strip_none)
2322130561Sobrien	    output = TRUE;
232333965Sjdp	  else
2324130561Sobrien	    output = FALSE;
232533965Sjdp	}
232633965Sjdp      else if (bfd_is_und_section (sym->section)
232733965Sjdp	       || bfd_is_com_section (sym->section))
2328130561Sobrien	output = FALSE;
232933965Sjdp      else if ((sym->flags & BSF_LOCAL) != 0)
233033965Sjdp	{
233133965Sjdp	  if ((sym->flags & BSF_WARNING) != 0)
2332130561Sobrien	    output = FALSE;
233333965Sjdp	  else
233433965Sjdp	    {
233533965Sjdp	      switch (info->discard)
233633965Sjdp		{
233733965Sjdp		default:
233833965Sjdp		case discard_all:
2339130561Sobrien		  output = FALSE;
234033965Sjdp		  break;
234189857Sobrien		case discard_sec_merge:
2342130561Sobrien		  output = TRUE;
2343130561Sobrien		  if (info->relocatable
234489857Sobrien		      || ! (sym->section->flags & SEC_MERGE))
234589857Sobrien		    break;
234689857Sobrien		  /* FALLTHROUGH */
234733965Sjdp		case discard_l:
234833965Sjdp		  if (bfd_is_local_label (input_bfd, sym))
2349130561Sobrien		    output = FALSE;
235033965Sjdp		  else
2351130561Sobrien		    output = TRUE;
235233965Sjdp		  break;
235333965Sjdp		case discard_none:
2354130561Sobrien		  output = TRUE;
235533965Sjdp		  break;
235633965Sjdp		}
235733965Sjdp	    }
235833965Sjdp	}
235933965Sjdp      else if ((sym->flags & BSF_CONSTRUCTOR))
236033965Sjdp	{
236133965Sjdp	  if (info->strip != strip_all)
2362130561Sobrien	    output = TRUE;
236333965Sjdp	  else
2364130561Sobrien	    output = FALSE;
236533965Sjdp	}
236633965Sjdp      else
236733965Sjdp	abort ();
236833965Sjdp
236933965Sjdp      /* If this symbol is in a section which is not being included
2370218822Sdim	 in the output file, then we don't want to output the
2371218822Sdim	 symbol.  */
2372218822Sdim      if (!bfd_is_abs_section (sym->section)
2373218822Sdim	  && bfd_section_removed_from_list (output_bfd,
2374218822Sdim					    sym->section->output_section))
2375130561Sobrien	output = FALSE;
237633965Sjdp
237733965Sjdp      if (output)
237833965Sjdp	{
237933965Sjdp	  if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2380130561Sobrien	    return FALSE;
2381130561Sobrien	  if (h != NULL)
2382130561Sobrien	    h->written = TRUE;
238333965Sjdp	}
238433965Sjdp    }
238533965Sjdp
2386130561Sobrien  return TRUE;
238733965Sjdp}
238833965Sjdp
238933965Sjdp/* Set the section and value of a generic BFD symbol based on a linker
239033965Sjdp   hash table entry.  */
239133965Sjdp
239233965Sjdpstatic void
2393130561Sobrienset_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
239433965Sjdp{
239533965Sjdp  switch (h->type)
239633965Sjdp    {
239733965Sjdp    default:
239833965Sjdp      abort ();
239933965Sjdp      break;
240033965Sjdp    case bfd_link_hash_new:
240133965Sjdp      /* This can happen when a constructor symbol is seen but we are
240233965Sjdp         not building constructors.  */
240333965Sjdp      if (sym->section != NULL)
240433965Sjdp	{
240533965Sjdp	  BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
240633965Sjdp	}
240733965Sjdp      else
240833965Sjdp	{
240933965Sjdp	  sym->flags |= BSF_CONSTRUCTOR;
241033965Sjdp	  sym->section = bfd_abs_section_ptr;
241133965Sjdp	  sym->value = 0;
241233965Sjdp	}
241333965Sjdp      break;
241433965Sjdp    case bfd_link_hash_undefined:
241533965Sjdp      sym->section = bfd_und_section_ptr;
241633965Sjdp      sym->value = 0;
241733965Sjdp      break;
241833965Sjdp    case bfd_link_hash_undefweak:
241933965Sjdp      sym->section = bfd_und_section_ptr;
242033965Sjdp      sym->value = 0;
242133965Sjdp      sym->flags |= BSF_WEAK;
242233965Sjdp      break;
242333965Sjdp    case bfd_link_hash_defined:
242433965Sjdp      sym->section = h->u.def.section;
242533965Sjdp      sym->value = h->u.def.value;
242633965Sjdp      break;
242733965Sjdp    case bfd_link_hash_defweak:
242833965Sjdp      sym->flags |= BSF_WEAK;
242933965Sjdp      sym->section = h->u.def.section;
243033965Sjdp      sym->value = h->u.def.value;
243133965Sjdp      break;
243233965Sjdp    case bfd_link_hash_common:
243333965Sjdp      sym->value = h->u.c.size;
243433965Sjdp      if (sym->section == NULL)
243533965Sjdp	sym->section = bfd_com_section_ptr;
243633965Sjdp      else if (! bfd_is_com_section (sym->section))
243733965Sjdp	{
243833965Sjdp	  BFD_ASSERT (bfd_is_und_section (sym->section));
243933965Sjdp	  sym->section = bfd_com_section_ptr;
244033965Sjdp	}
244133965Sjdp      /* Do not set the section; see _bfd_generic_link_output_symbols.  */
244233965Sjdp      break;
244333965Sjdp    case bfd_link_hash_indirect:
244433965Sjdp    case bfd_link_hash_warning:
244533965Sjdp      /* FIXME: What should we do here?  */
244633965Sjdp      break;
244733965Sjdp    }
244833965Sjdp}
244933965Sjdp
245033965Sjdp/* Write out a global symbol, if it hasn't already been written out.
245133965Sjdp   This is called for each symbol in the hash table.  */
245233965Sjdp
2453130561Sobrienbfd_boolean
2454130561Sobrien_bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2455130561Sobrien				       void *data)
245633965Sjdp{
2457130561Sobrien  struct generic_write_global_symbol_info *wginfo = data;
245833965Sjdp  asymbol *sym;
245933965Sjdp
246094536Sobrien  if (h->root.type == bfd_link_hash_warning)
246194536Sobrien    h = (struct generic_link_hash_entry *) h->root.u.i.link;
246294536Sobrien
246333965Sjdp  if (h->written)
2464130561Sobrien    return TRUE;
246533965Sjdp
2466130561Sobrien  h->written = TRUE;
246733965Sjdp
246833965Sjdp  if (wginfo->info->strip == strip_all
246933965Sjdp      || (wginfo->info->strip == strip_some
247033965Sjdp	  && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2471130561Sobrien			      FALSE, FALSE) == NULL))
2472130561Sobrien    return TRUE;
247333965Sjdp
2474130561Sobrien  if (h->sym != NULL)
247533965Sjdp    sym = h->sym;
247633965Sjdp  else
247733965Sjdp    {
247833965Sjdp      sym = bfd_make_empty_symbol (wginfo->output_bfd);
247933965Sjdp      if (!sym)
2480130561Sobrien	return FALSE;
248133965Sjdp      sym->name = h->root.root.string;
248233965Sjdp      sym->flags = 0;
248333965Sjdp    }
248433965Sjdp
248533965Sjdp  set_symbol_from_hash (sym, &h->root);
248633965Sjdp
248733965Sjdp  sym->flags |= BSF_GLOBAL;
248833965Sjdp
248933965Sjdp  if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
249033965Sjdp				   sym))
249133965Sjdp    {
249233965Sjdp      /* FIXME: No way to return failure.  */
249333965Sjdp      abort ();
249433965Sjdp    }
249533965Sjdp
2496130561Sobrien  return TRUE;
249733965Sjdp}
249833965Sjdp
249933965Sjdp/* Create a relocation.  */
250033965Sjdp
2501130561Sobrienbfd_boolean
2502130561Sobrien_bfd_generic_reloc_link_order (bfd *abfd,
2503130561Sobrien			       struct bfd_link_info *info,
2504130561Sobrien			       asection *sec,
2505130561Sobrien			       struct bfd_link_order *link_order)
250633965Sjdp{
250733965Sjdp  arelent *r;
250833965Sjdp
2509130561Sobrien  if (! info->relocatable)
251033965Sjdp    abort ();
2511130561Sobrien  if (sec->orelocation == NULL)
251233965Sjdp    abort ();
251333965Sjdp
2514130561Sobrien  r = bfd_alloc (abfd, sizeof (arelent));
2515130561Sobrien  if (r == NULL)
2516130561Sobrien    return FALSE;
251777298Sobrien
251833965Sjdp  r->address = link_order->offset;
251933965Sjdp  r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
252033965Sjdp  if (r->howto == 0)
252133965Sjdp    {
252233965Sjdp      bfd_set_error (bfd_error_bad_value);
2523130561Sobrien      return FALSE;
252433965Sjdp    }
252533965Sjdp
252633965Sjdp  /* Get the symbol to use for the relocation.  */
252733965Sjdp  if (link_order->type == bfd_section_reloc_link_order)
252833965Sjdp    r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
252933965Sjdp  else
253033965Sjdp    {
253133965Sjdp      struct generic_link_hash_entry *h;
253233965Sjdp
253333965Sjdp      h = ((struct generic_link_hash_entry *)
253433965Sjdp	   bfd_wrapped_link_hash_lookup (abfd, info,
253533965Sjdp					 link_order->u.reloc.p->u.name,
2536130561Sobrien					 FALSE, FALSE, TRUE));
2537130561Sobrien      if (h == NULL
253833965Sjdp	  || ! h->written)
253933965Sjdp	{
254033965Sjdp	  if (! ((*info->callbacks->unattached_reloc)
2541130561Sobrien		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2542130561Sobrien	    return FALSE;
254333965Sjdp	  bfd_set_error (bfd_error_bad_value);
2544130561Sobrien	  return FALSE;
254533965Sjdp	}
254633965Sjdp      r->sym_ptr_ptr = &h->sym;
254733965Sjdp    }
254833965Sjdp
254933965Sjdp  /* If this is an inplace reloc, write the addend to the object file.
255033965Sjdp     Otherwise, store it in the reloc addend.  */
255133965Sjdp  if (! r->howto->partial_inplace)
255233965Sjdp    r->addend = link_order->u.reloc.p->addend;
255333965Sjdp  else
255433965Sjdp    {
255533965Sjdp      bfd_size_type size;
255633965Sjdp      bfd_reloc_status_type rstat;
255733965Sjdp      bfd_byte *buf;
2558130561Sobrien      bfd_boolean ok;
255989857Sobrien      file_ptr loc;
256033965Sjdp
256133965Sjdp      size = bfd_get_reloc_size (r->howto);
2562130561Sobrien      buf = bfd_zmalloc (size);
2563130561Sobrien      if (buf == NULL)
2564130561Sobrien	return FALSE;
256533965Sjdp      rstat = _bfd_relocate_contents (r->howto, abfd,
256689857Sobrien				      (bfd_vma) link_order->u.reloc.p->addend,
256789857Sobrien				      buf);
256833965Sjdp      switch (rstat)
256933965Sjdp	{
257033965Sjdp	case bfd_reloc_ok:
257133965Sjdp	  break;
257233965Sjdp	default:
257333965Sjdp	case bfd_reloc_outofrange:
257433965Sjdp	  abort ();
257533965Sjdp	case bfd_reloc_overflow:
257633965Sjdp	  if (! ((*info->callbacks->reloc_overflow)
2577218822Sdim		 (info, NULL,
257833965Sjdp		  (link_order->type == bfd_section_reloc_link_order
257933965Sjdp		   ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
258033965Sjdp		   : link_order->u.reloc.p->u.name),
258133965Sjdp		  r->howto->name, link_order->u.reloc.p->addend,
2582130561Sobrien		  NULL, NULL, 0)))
258333965Sjdp	    {
258433965Sjdp	      free (buf);
2585130561Sobrien	      return FALSE;
258633965Sjdp	    }
258733965Sjdp	  break;
258833965Sjdp	}
258989857Sobrien      loc = link_order->offset * bfd_octets_per_byte (abfd);
2590130561Sobrien      ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
259133965Sjdp      free (buf);
259233965Sjdp      if (! ok)
2593130561Sobrien	return FALSE;
259433965Sjdp
259533965Sjdp      r->addend = 0;
259633965Sjdp    }
259733965Sjdp
259833965Sjdp  sec->orelocation[sec->reloc_count] = r;
259933965Sjdp  ++sec->reloc_count;
260033965Sjdp
2601130561Sobrien  return TRUE;
260233965Sjdp}
260333965Sjdp
260433965Sjdp/* Allocate a new link_order for a section.  */
260533965Sjdp
260633965Sjdpstruct bfd_link_order *
2607130561Sobrienbfd_new_link_order (bfd *abfd, asection *section)
260833965Sjdp{
260989857Sobrien  bfd_size_type amt = sizeof (struct bfd_link_order);
2610104834Sobrien  struct bfd_link_order *new;
2611104834Sobrien
2612130561Sobrien  new = bfd_zalloc (abfd, amt);
261333965Sjdp  if (!new)
261433965Sjdp    return NULL;
261533965Sjdp
261633965Sjdp  new->type = bfd_undefined_link_order;
261733965Sjdp
2618218822Sdim  if (section->map_tail.link_order != NULL)
2619218822Sdim    section->map_tail.link_order->next = new;
262033965Sjdp  else
2621218822Sdim    section->map_head.link_order = new;
2622218822Sdim  section->map_tail.link_order = new;
262333965Sjdp
262433965Sjdp  return new;
262533965Sjdp}
262633965Sjdp
262733965Sjdp/* Default link order processing routine.  Note that we can not handle
262833965Sjdp   the reloc_link_order types here, since they depend upon the details
262933965Sjdp   of how the particular backends generates relocs.  */
263033965Sjdp
2631130561Sobrienbfd_boolean
2632130561Sobrien_bfd_default_link_order (bfd *abfd,
2633130561Sobrien			 struct bfd_link_info *info,
2634130561Sobrien			 asection *sec,
2635130561Sobrien			 struct bfd_link_order *link_order)
263633965Sjdp{
263733965Sjdp  switch (link_order->type)
263833965Sjdp    {
263933965Sjdp    case bfd_undefined_link_order:
264033965Sjdp    case bfd_section_reloc_link_order:
264133965Sjdp    case bfd_symbol_reloc_link_order:
264233965Sjdp    default:
264333965Sjdp      abort ();
264433965Sjdp    case bfd_indirect_link_order:
264533965Sjdp      return default_indirect_link_order (abfd, info, sec, link_order,
2646130561Sobrien					  FALSE);
264733965Sjdp    case bfd_data_link_order:
2648104834Sobrien      return default_data_link_order (abfd, info, sec, link_order);
264933965Sjdp    }
265033965Sjdp}
265133965Sjdp
2652104834Sobrien/* Default routine to handle a bfd_data_link_order.  */
265333965Sjdp
2654130561Sobrienstatic bfd_boolean
2655130561Sobriendefault_data_link_order (bfd *abfd,
2656130561Sobrien			 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2657130561Sobrien			 asection *sec,
2658130561Sobrien			 struct bfd_link_order *link_order)
265933965Sjdp{
266089857Sobrien  bfd_size_type size;
2661104834Sobrien  size_t fill_size;
2662104834Sobrien  bfd_byte *fill;
266389857Sobrien  file_ptr loc;
2664130561Sobrien  bfd_boolean result;
266533965Sjdp
266633965Sjdp  BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
266733965Sjdp
266889857Sobrien  size = link_order->size;
266989857Sobrien  if (size == 0)
2670130561Sobrien    return TRUE;
267189857Sobrien
2672104834Sobrien  fill = link_order->u.data.contents;
2673104834Sobrien  fill_size = link_order->u.data.size;
2674104834Sobrien  if (fill_size != 0 && fill_size < size)
2675104834Sobrien    {
2676104834Sobrien      bfd_byte *p;
2677130561Sobrien      fill = bfd_malloc (size);
2678104834Sobrien      if (fill == NULL)
2679130561Sobrien	return FALSE;
2680104834Sobrien      p = fill;
2681104834Sobrien      if (fill_size == 1)
2682104834Sobrien	memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2683104834Sobrien      else
2684104834Sobrien	{
2685104834Sobrien	  do
2686104834Sobrien	    {
2687104834Sobrien	      memcpy (p, link_order->u.data.contents, fill_size);
2688104834Sobrien	      p += fill_size;
2689104834Sobrien	      size -= fill_size;
2690104834Sobrien	    }
2691104834Sobrien	  while (size >= fill_size);
2692104834Sobrien	  if (size != 0)
2693104834Sobrien	    memcpy (p, link_order->u.data.contents, (size_t) size);
2694104834Sobrien	  size = link_order->size;
2695104834Sobrien	}
2696104834Sobrien    }
269733965Sjdp
269889857Sobrien  loc = link_order->offset * bfd_octets_per_byte (abfd);
2699104834Sobrien  result = bfd_set_section_contents (abfd, sec, fill, loc, size);
270089857Sobrien
2701104834Sobrien  if (fill != link_order->u.data.contents)
2702104834Sobrien    free (fill);
270333965Sjdp  return result;
270433965Sjdp}
270533965Sjdp
270633965Sjdp/* Default routine to handle a bfd_indirect_link_order.  */
270733965Sjdp
2708130561Sobrienstatic bfd_boolean
2709130561Sobriendefault_indirect_link_order (bfd *output_bfd,
2710130561Sobrien			     struct bfd_link_info *info,
2711130561Sobrien			     asection *output_section,
2712130561Sobrien			     struct bfd_link_order *link_order,
2713130561Sobrien			     bfd_boolean generic_linker)
271433965Sjdp{
271533965Sjdp  asection *input_section;
271633965Sjdp  bfd *input_bfd;
271733965Sjdp  bfd_byte *contents = NULL;
271833965Sjdp  bfd_byte *new_contents;
271989857Sobrien  bfd_size_type sec_size;
272089857Sobrien  file_ptr loc;
272133965Sjdp
272233965Sjdp  BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
272333965Sjdp
272433965Sjdp  input_section = link_order->u.indirect.section;
272533965Sjdp  input_bfd = input_section->owner;
2726218822Sdim  if (input_section->size == 0)
2727218822Sdim    return TRUE;
272833965Sjdp
272933965Sjdp  BFD_ASSERT (input_section->output_section == output_section);
273033965Sjdp  BFD_ASSERT (input_section->output_offset == link_order->offset);
2731218822Sdim  BFD_ASSERT (input_section->size == link_order->size);
273233965Sjdp
2733130561Sobrien  if (info->relocatable
273433965Sjdp      && input_section->reloc_count > 0
2735130561Sobrien      && output_section->orelocation == NULL)
273633965Sjdp    {
273733965Sjdp      /* Space has not been allocated for the output relocations.
273833965Sjdp	 This can happen when we are called by a specific backend
273933965Sjdp	 because somebody is attempting to link together different
274033965Sjdp	 types of object files.  Handling this case correctly is
274133965Sjdp	 difficult, and sometimes impossible.  */
274233965Sjdp      (*_bfd_error_handler)
2743130561Sobrien	(_("Attempt to do relocatable link with %s input and %s output"),
274433965Sjdp	 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
274533965Sjdp      bfd_set_error (bfd_error_wrong_format);
2746130561Sobrien      return FALSE;
274733965Sjdp    }
274833965Sjdp
274933965Sjdp  if (! generic_linker)
275033965Sjdp    {
275133965Sjdp      asymbol **sympp;
275233965Sjdp      asymbol **symppend;
275333965Sjdp
275433965Sjdp      /* Get the canonical symbols.  The generic linker will always
275533965Sjdp	 have retrieved them by this point, but we are being called by
275633965Sjdp	 a specific linker, presumably because we are linking
275733965Sjdp	 different types of object files together.  */
275833965Sjdp      if (! generic_link_read_symbols (input_bfd))
2759130561Sobrien	return FALSE;
276033965Sjdp
276133965Sjdp      /* Since we have been called by a specific linker, rather than
276233965Sjdp	 the generic linker, the values of the symbols will not be
276333965Sjdp	 right.  They will be the values as seen in the input file,
276433965Sjdp	 not the values of the final link.  We need to fix them up
276533965Sjdp	 before we can relocate the section.  */
276633965Sjdp      sympp = _bfd_generic_link_get_symbols (input_bfd);
276733965Sjdp      symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
276833965Sjdp      for (; sympp < symppend; sympp++)
276933965Sjdp	{
277033965Sjdp	  asymbol *sym;
277133965Sjdp	  struct bfd_link_hash_entry *h;
277233965Sjdp
277333965Sjdp	  sym = *sympp;
277433965Sjdp
277533965Sjdp	  if ((sym->flags & (BSF_INDIRECT
277633965Sjdp			     | BSF_WARNING
277733965Sjdp			     | BSF_GLOBAL
277833965Sjdp			     | BSF_CONSTRUCTOR
277933965Sjdp			     | BSF_WEAK)) != 0
278033965Sjdp	      || bfd_is_und_section (bfd_get_section (sym))
278133965Sjdp	      || bfd_is_com_section (bfd_get_section (sym))
278233965Sjdp	      || bfd_is_ind_section (bfd_get_section (sym)))
278333965Sjdp	    {
278433965Sjdp	      /* sym->udata may have been set by
278533965Sjdp		 generic_link_add_symbol_list.  */
278633965Sjdp	      if (sym->udata.p != NULL)
2787130561Sobrien		h = sym->udata.p;
278833965Sjdp	      else if (bfd_is_und_section (bfd_get_section (sym)))
278933965Sjdp		h = bfd_wrapped_link_hash_lookup (output_bfd, info,
279033965Sjdp						  bfd_asymbol_name (sym),
2791130561Sobrien						  FALSE, FALSE, TRUE);
279233965Sjdp	      else
279333965Sjdp		h = bfd_link_hash_lookup (info->hash,
279433965Sjdp					  bfd_asymbol_name (sym),
2795130561Sobrien					  FALSE, FALSE, TRUE);
279633965Sjdp	      if (h != NULL)
279733965Sjdp		set_symbol_from_hash (sym, h);
279833965Sjdp	    }
279977298Sobrien	}
280033965Sjdp    }
280133965Sjdp
280233965Sjdp  /* Get and relocate the section contents.  */
2803218822Sdim  sec_size = (input_section->rawsize > input_section->size
2804218822Sdim	      ? input_section->rawsize
2805218822Sdim	      : input_section->size);
2806130561Sobrien  contents = bfd_malloc (sec_size);
280789857Sobrien  if (contents == NULL && sec_size != 0)
280833965Sjdp    goto error_return;
280933965Sjdp  new_contents = (bfd_get_relocated_section_contents
2810130561Sobrien		  (output_bfd, info, link_order, contents, info->relocatable,
281133965Sjdp		   _bfd_generic_link_get_symbols (input_bfd)));
281233965Sjdp  if (!new_contents)
281333965Sjdp    goto error_return;
281433965Sjdp
281533965Sjdp  /* Output the section contents.  */
2816218822Sdim  loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
281733965Sjdp  if (! bfd_set_section_contents (output_bfd, output_section,
2818218822Sdim				  new_contents, loc, input_section->size))
281933965Sjdp    goto error_return;
282033965Sjdp
282133965Sjdp  if (contents != NULL)
282233965Sjdp    free (contents);
2823130561Sobrien  return TRUE;
282433965Sjdp
282533965Sjdp error_return:
282633965Sjdp  if (contents != NULL)
282733965Sjdp    free (contents);
2828130561Sobrien  return FALSE;
282933965Sjdp}
283033965Sjdp
283133965Sjdp/* A little routine to count the number of relocs in a link_order
283233965Sjdp   list.  */
283333965Sjdp
283433965Sjdpunsigned int
2835130561Sobrien_bfd_count_link_order_relocs (struct bfd_link_order *link_order)
283633965Sjdp{
283733965Sjdp  register unsigned int c;
283833965Sjdp  register struct bfd_link_order *l;
283933965Sjdp
284033965Sjdp  c = 0;
2841130561Sobrien  for (l = link_order; l != NULL; l = l->next)
284233965Sjdp    {
284333965Sjdp      if (l->type == bfd_section_reloc_link_order
284433965Sjdp	  || l->type == bfd_symbol_reloc_link_order)
284533965Sjdp	++c;
284633965Sjdp    }
284733965Sjdp
284833965Sjdp  return c;
284933965Sjdp}
285033965Sjdp
285133965Sjdp/*
285233965SjdpFUNCTION
285333965Sjdp	bfd_link_split_section
285433965Sjdp
285533965SjdpSYNOPSIS
2856130561Sobrien        bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
285733965Sjdp
285833965SjdpDESCRIPTION
285933965Sjdp	Return nonzero if @var{sec} should be split during a
286033965Sjdp	reloceatable or final link.
286133965Sjdp
286233965Sjdp.#define bfd_link_split_section(abfd, sec) \
286333965Sjdp.       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
286433965Sjdp.
286533965Sjdp
286633965Sjdp*/
286733965Sjdp
2868130561Sobrienbfd_boolean
2869130561Sobrien_bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2870130561Sobrien				 asection *sec ATTRIBUTE_UNUSED)
287133965Sjdp{
2872130561Sobrien  return FALSE;
287333965Sjdp}
2874218822Sdim
2875218822Sdim/*
2876218822SdimFUNCTION
2877218822Sdim	bfd_section_already_linked
2878218822Sdim
2879218822SdimSYNOPSIS
2880218822Sdim        void bfd_section_already_linked (bfd *abfd, asection *sec,
2881218822Sdim					 struct bfd_link_info *info);
2882218822Sdim
2883218822SdimDESCRIPTION
2884218822Sdim	Check if @var{sec} has been already linked during a reloceatable
2885218822Sdim	or final link.
2886218822Sdim
2887218822Sdim.#define bfd_section_already_linked(abfd, sec, info) \
2888218822Sdim.       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2889218822Sdim.
2890218822Sdim
2891218822Sdim*/
2892218822Sdim
2893218822Sdim/* Sections marked with the SEC_LINK_ONCE flag should only be linked
2894218822Sdim   once into the output.  This routine checks each section, and
2895218822Sdim   arrange to discard it if a section of the same name has already
2896218822Sdim   been linked.  This code assumes that all relevant sections have the
2897218822Sdim   SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2898218822Sdim   section name.  bfd_section_already_linked is called via
2899218822Sdim   bfd_map_over_sections.  */
2900218822Sdim
2901218822Sdim/* The hash table.  */
2902218822Sdim
2903218822Sdimstatic struct bfd_hash_table _bfd_section_already_linked_table;
2904218822Sdim
2905218822Sdim/* Support routines for the hash table used by section_already_linked,
2906218822Sdim   initialize the table, traverse, lookup, fill in an entry and remove
2907218822Sdim   the table.  */
2908218822Sdim
2909218822Sdimvoid
2910218822Sdimbfd_section_already_linked_table_traverse
2911218822Sdim  (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
2912218822Sdim			void *), void *info)
2913218822Sdim{
2914218822Sdim  bfd_hash_traverse (&_bfd_section_already_linked_table,
2915218822Sdim		     (bfd_boolean (*) (struct bfd_hash_entry *,
2916218822Sdim				       void *)) func,
2917218822Sdim		     info);
2918218822Sdim}
2919218822Sdim
2920218822Sdimstruct bfd_section_already_linked_hash_entry *
2921218822Sdimbfd_section_already_linked_table_lookup (const char *name)
2922218822Sdim{
2923218822Sdim  return ((struct bfd_section_already_linked_hash_entry *)
2924218822Sdim	  bfd_hash_lookup (&_bfd_section_already_linked_table, name,
2925218822Sdim			   TRUE, FALSE));
2926218822Sdim}
2927218822Sdim
2928218822Sdimvoid
2929218822Sdimbfd_section_already_linked_table_insert
2930218822Sdim  (struct bfd_section_already_linked_hash_entry *already_linked_list,
2931218822Sdim   asection *sec)
2932218822Sdim{
2933218822Sdim  struct bfd_section_already_linked *l;
2934218822Sdim
2935218822Sdim  /* Allocate the memory from the same obstack as the hash table is
2936218822Sdim     kept in.  */
2937218822Sdim  l = bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
2938218822Sdim  l->sec = sec;
2939218822Sdim  l->next = already_linked_list->entry;
2940218822Sdim  already_linked_list->entry = l;
2941218822Sdim}
2942218822Sdim
2943218822Sdimstatic struct bfd_hash_entry *
2944218822Sdimalready_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
2945218822Sdim			struct bfd_hash_table *table,
2946218822Sdim			const char *string ATTRIBUTE_UNUSED)
2947218822Sdim{
2948218822Sdim  struct bfd_section_already_linked_hash_entry *ret =
2949218822Sdim    bfd_hash_allocate (table, sizeof *ret);
2950218822Sdim
2951218822Sdim  ret->entry = NULL;
2952218822Sdim
2953218822Sdim  return &ret->root;
2954218822Sdim}
2955218822Sdim
2956218822Sdimbfd_boolean
2957218822Sdimbfd_section_already_linked_table_init (void)
2958218822Sdim{
2959218822Sdim  return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
2960218822Sdim				already_linked_newfunc,
2961218822Sdim				sizeof (struct bfd_section_already_linked_hash_entry),
2962218822Sdim				42);
2963218822Sdim}
2964218822Sdim
2965218822Sdimvoid
2966218822Sdimbfd_section_already_linked_table_free (void)
2967218822Sdim{
2968218822Sdim  bfd_hash_table_free (&_bfd_section_already_linked_table);
2969218822Sdim}
2970218822Sdim
2971218822Sdim/* This is used on non-ELF inputs.  */
2972218822Sdim
2973218822Sdimvoid
2974218822Sdim_bfd_generic_section_already_linked (bfd *abfd, asection *sec,
2975218822Sdim				     struct bfd_link_info *info ATTRIBUTE_UNUSED)
2976218822Sdim{
2977218822Sdim  flagword flags;
2978218822Sdim  const char *name;
2979218822Sdim  struct bfd_section_already_linked *l;
2980218822Sdim  struct bfd_section_already_linked_hash_entry *already_linked_list;
2981218822Sdim
2982218822Sdim  flags = sec->flags;
2983218822Sdim  if ((flags & SEC_LINK_ONCE) == 0)
2984218822Sdim    return;
2985218822Sdim
2986218822Sdim  /* FIXME: When doing a relocatable link, we may have trouble
2987218822Sdim     copying relocations in other sections that refer to local symbols
2988218822Sdim     in the section being discarded.  Those relocations will have to
2989218822Sdim     be converted somehow; as of this writing I'm not sure that any of
2990218822Sdim     the backends handle that correctly.
2991218822Sdim
2992218822Sdim     It is tempting to instead not discard link once sections when
2993218822Sdim     doing a relocatable link (technically, they should be discarded
2994218822Sdim     whenever we are building constructors).  However, that fails,
2995218822Sdim     because the linker winds up combining all the link once sections
2996218822Sdim     into a single large link once section, which defeats the purpose
2997218822Sdim     of having link once sections in the first place.  */
2998218822Sdim
2999218822Sdim  name = bfd_get_section_name (abfd, sec);
3000218822Sdim
3001218822Sdim  already_linked_list = bfd_section_already_linked_table_lookup (name);
3002218822Sdim
3003218822Sdim  for (l = already_linked_list->entry; l != NULL; l = l->next)
3004218822Sdim    {
3005218822Sdim      bfd_boolean skip = FALSE;
3006218822Sdim      struct coff_comdat_info *s_comdat
3007218822Sdim	= bfd_coff_get_comdat_section (abfd, sec);
3008218822Sdim      struct coff_comdat_info *l_comdat
3009218822Sdim	= bfd_coff_get_comdat_section (l->sec->owner, l->sec);
3010218822Sdim
3011218822Sdim      /* We may have 3 different sections on the list: group section,
3012218822Sdim	 comdat section and linkonce section. SEC may be a linkonce or
3013218822Sdim	 comdat section. We always ignore group section. For non-COFF
3014218822Sdim	 inputs, we also ignore comdat section.
3015218822Sdim
3016218822Sdim	 FIXME: Is that safe to match a linkonce section with a comdat
3017218822Sdim	 section for COFF inputs?  */
3018218822Sdim      if ((l->sec->flags & SEC_GROUP) != 0)
3019218822Sdim	skip = TRUE;
3020218822Sdim      else if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
3021218822Sdim	{
3022218822Sdim	  if (s_comdat != NULL
3023218822Sdim	      && l_comdat != NULL
3024218822Sdim	      && strcmp (s_comdat->name, l_comdat->name) != 0)
3025218822Sdim	    skip = TRUE;
3026218822Sdim	}
3027218822Sdim      else if (l_comdat != NULL)
3028218822Sdim	skip = TRUE;
3029218822Sdim
3030218822Sdim      if (!skip)
3031218822Sdim	{
3032218822Sdim	  /* The section has already been linked.  See if we should
3033218822Sdim             issue a warning.  */
3034218822Sdim	  switch (flags & SEC_LINK_DUPLICATES)
3035218822Sdim	    {
3036218822Sdim	    default:
3037218822Sdim	      abort ();
3038218822Sdim
3039218822Sdim	    case SEC_LINK_DUPLICATES_DISCARD:
3040218822Sdim	      break;
3041218822Sdim
3042218822Sdim	    case SEC_LINK_DUPLICATES_ONE_ONLY:
3043218822Sdim	      (*_bfd_error_handler)
3044218822Sdim		(_("%B: warning: ignoring duplicate section `%A'\n"),
3045218822Sdim		 abfd, sec);
3046218822Sdim	      break;
3047218822Sdim
3048218822Sdim	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3049218822Sdim	      /* FIXME: We should really dig out the contents of both
3050218822Sdim                 sections and memcmp them.  The COFF/PE spec says that
3051218822Sdim                 the Microsoft linker does not implement this
3052218822Sdim                 correctly, so I'm not going to bother doing it
3053218822Sdim                 either.  */
3054218822Sdim	      /* Fall through.  */
3055218822Sdim	    case SEC_LINK_DUPLICATES_SAME_SIZE:
3056218822Sdim	      if (sec->size != l->sec->size)
3057218822Sdim		(*_bfd_error_handler)
3058218822Sdim		  (_("%B: warning: duplicate section `%A' has different size\n"),
3059218822Sdim		   abfd, sec);
3060218822Sdim	      break;
3061218822Sdim	    }
3062218822Sdim
3063218822Sdim	  /* Set the output_section field so that lang_add_section
3064218822Sdim	     does not create a lang_input_section structure for this
3065218822Sdim	     section.  Since there might be a symbol in the section
3066218822Sdim	     being discarded, we must retain a pointer to the section
3067218822Sdim	     which we are really going to use.  */
3068218822Sdim	  sec->output_section = bfd_abs_section_ptr;
3069218822Sdim	  sec->kept_section = l->sec;
3070218822Sdim
3071218822Sdim	  return;
3072218822Sdim	}
3073218822Sdim    }
3074218822Sdim
3075218822Sdim  /* This is the first section with this name.  Record it.  */
3076218822Sdim  bfd_section_already_linked_table_insert (already_linked_list, sec);
3077218822Sdim}
3078218822Sdim
3079218822Sdim/* Convert symbols in excluded output sections to use a kept section.  */
3080218822Sdim
3081218822Sdimstatic bfd_boolean
3082218822Sdimfix_syms (struct bfd_link_hash_entry *h, void *data)
3083218822Sdim{
3084218822Sdim  bfd *obfd = (bfd *) data;
3085218822Sdim
3086218822Sdim  if (h->type == bfd_link_hash_warning)
3087218822Sdim    h = h->u.i.link;
3088218822Sdim
3089218822Sdim  if (h->type == bfd_link_hash_defined
3090218822Sdim      || h->type == bfd_link_hash_defweak)
3091218822Sdim    {
3092218822Sdim      asection *s = h->u.def.section;
3093218822Sdim      if (s != NULL
3094218822Sdim	  && s->output_section != NULL
3095218822Sdim	  && (s->output_section->flags & SEC_EXCLUDE) != 0
3096218822Sdim	  && bfd_section_removed_from_list (obfd, s->output_section))
3097218822Sdim	{
3098218822Sdim	  asection *op, *op1;
3099218822Sdim
3100218822Sdim	  h->u.def.value += s->output_offset + s->output_section->vma;
3101218822Sdim
3102218822Sdim	  /* Find preceding kept section.  */
3103218822Sdim	  for (op1 = s->output_section->prev; op1 != NULL; op1 = op1->prev)
3104218822Sdim	    if ((op1->flags & SEC_EXCLUDE) == 0
3105218822Sdim		&& !bfd_section_removed_from_list (obfd, op1))
3106218822Sdim	      break;
3107218822Sdim
3108218822Sdim	  /* Find following kept section.  Start at prev->next because
3109218822Sdim	     other sections may have been added after S was removed.  */
3110218822Sdim	  if (s->output_section->prev != NULL)
3111218822Sdim	    op = s->output_section->prev->next;
3112218822Sdim	  else
3113218822Sdim	    op = s->output_section->owner->sections;
3114218822Sdim	  for (; op != NULL; op = op->next)
3115218822Sdim	    if ((op->flags & SEC_EXCLUDE) == 0
3116218822Sdim		&& !bfd_section_removed_from_list (obfd, op))
3117218822Sdim	      break;
3118218822Sdim
3119218822Sdim	  /* Choose better of two sections, based on flags.  The idea
3120218822Sdim	     is to choose a section that will be in the same segment
3121218822Sdim	     as S would have been if it was kept.  */
3122218822Sdim	  if (op1 == NULL)
3123218822Sdim	    {
3124218822Sdim	      if (op == NULL)
3125218822Sdim		op = bfd_abs_section_ptr;
3126218822Sdim	    }
3127218822Sdim	  else if (op == NULL)
3128218822Sdim	    op = op1;
3129218822Sdim	  else if (((op1->flags ^ op->flags)
3130218822Sdim		    & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0)
3131218822Sdim	    {
3132218822Sdim	      if (((op->flags ^ s->flags)
3133218822Sdim		   & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0)
3134218822Sdim		op = op1;
3135218822Sdim	    }
3136218822Sdim	  else if (((op1->flags ^ op->flags) & SEC_READONLY) != 0)
3137218822Sdim	    {
3138218822Sdim	      if (((op->flags ^ s->flags) & SEC_READONLY) != 0)
3139218822Sdim		op = op1;
3140218822Sdim	    }
3141218822Sdim	  else if (((op1->flags ^ op->flags) & SEC_CODE) != 0)
3142218822Sdim	    {
3143218822Sdim	      if (((op->flags ^ s->flags) & SEC_CODE) != 0)
3144218822Sdim		op = op1;
3145218822Sdim	    }
3146218822Sdim	  else
3147218822Sdim	    {
3148218822Sdim	      /* Flags we care about are the same.  Prefer the following
3149218822Sdim		 section if that will result in a positive valued sym.  */
3150218822Sdim	      if (h->u.def.value < op->vma)
3151218822Sdim		op = op1;
3152218822Sdim	    }
3153218822Sdim
3154218822Sdim	  h->u.def.value -= op->vma;
3155218822Sdim	  h->u.def.section = op;
3156218822Sdim	}
3157218822Sdim    }
3158218822Sdim
3159218822Sdim  return TRUE;
3160218822Sdim}
3161218822Sdim
3162218822Sdimvoid
3163218822Sdim_bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3164218822Sdim{
3165218822Sdim  bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3166218822Sdim}
3167