xref: /dflybsd-src/contrib/binutils-2.27/bfd/linker.c (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /* linker.c -- BFD linker routines
2*a9fa9459Szrj    Copyright (C) 1993-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
4*a9fa9459Szrj 
5*a9fa9459Szrj    This file is part of BFD, the Binary File Descriptor library.
6*a9fa9459Szrj 
7*a9fa9459Szrj    This program is free software; you can redistribute it and/or modify
8*a9fa9459Szrj    it under the terms of the GNU General Public License as published by
9*a9fa9459Szrj    the Free Software Foundation; either version 3 of the License, or
10*a9fa9459Szrj    (at your option) any later version.
11*a9fa9459Szrj 
12*a9fa9459Szrj    This program is distributed in the hope that it will be useful,
13*a9fa9459Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a9fa9459Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*a9fa9459Szrj    GNU General Public License for more details.
16*a9fa9459Szrj 
17*a9fa9459Szrj    You should have received a copy of the GNU General Public License
18*a9fa9459Szrj    along with this program; if not, write to the Free Software
19*a9fa9459Szrj    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*a9fa9459Szrj    MA 02110-1301, USA.  */
21*a9fa9459Szrj 
22*a9fa9459Szrj #include "sysdep.h"
23*a9fa9459Szrj #include "bfd.h"
24*a9fa9459Szrj #include "libbfd.h"
25*a9fa9459Szrj #include "bfdlink.h"
26*a9fa9459Szrj #include "genlink.h"
27*a9fa9459Szrj 
28*a9fa9459Szrj /*
29*a9fa9459Szrj SECTION
30*a9fa9459Szrj 	Linker Functions
31*a9fa9459Szrj 
32*a9fa9459Szrj @cindex Linker
33*a9fa9459Szrj 	The linker uses three special entry points in the BFD target
34*a9fa9459Szrj 	vector.  It is not necessary to write special routines for
35*a9fa9459Szrj 	these entry points when creating a new BFD back end, since
36*a9fa9459Szrj 	generic versions are provided.  However, writing them can
37*a9fa9459Szrj 	speed up linking and make it use significantly less runtime
38*a9fa9459Szrj 	memory.
39*a9fa9459Szrj 
40*a9fa9459Szrj 	The first routine creates a hash table used by the other
41*a9fa9459Szrj 	routines.  The second routine adds the symbols from an object
42*a9fa9459Szrj 	file to the hash table.  The third routine takes all the
43*a9fa9459Szrj 	object files and links them together to create the output
44*a9fa9459Szrj 	file.  These routines are designed so that the linker proper
45*a9fa9459Szrj 	does not need to know anything about the symbols in the object
46*a9fa9459Szrj 	files that it is linking.  The linker merely arranges the
47*a9fa9459Szrj 	sections as directed by the linker script and lets BFD handle
48*a9fa9459Szrj 	the details of symbols and relocs.
49*a9fa9459Szrj 
50*a9fa9459Szrj 	The second routine and third routines are passed a pointer to
51*a9fa9459Szrj 	a <<struct bfd_link_info>> structure (defined in
52*a9fa9459Szrj 	<<bfdlink.h>>) which holds information relevant to the link,
53*a9fa9459Szrj 	including the linker hash table (which was created by the
54*a9fa9459Szrj 	first routine) and a set of callback functions to the linker
55*a9fa9459Szrj 	proper.
56*a9fa9459Szrj 
57*a9fa9459Szrj 	The generic linker routines are in <<linker.c>>, and use the
58*a9fa9459Szrj 	header file <<genlink.h>>.  As of this writing, the only back
59*a9fa9459Szrj 	ends which have implemented versions of these routines are
60*a9fa9459Szrj 	a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
61*a9fa9459Szrj 	routines are used as examples throughout this section.
62*a9fa9459Szrj 
63*a9fa9459Szrj @menu
64*a9fa9459Szrj @* Creating a Linker Hash Table::
65*a9fa9459Szrj @* Adding Symbols to the Hash Table::
66*a9fa9459Szrj @* Performing the Final Link::
67*a9fa9459Szrj @end menu
68*a9fa9459Szrj 
69*a9fa9459Szrj INODE
70*a9fa9459Szrj Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
71*a9fa9459Szrj SUBSECTION
72*a9fa9459Szrj 	Creating a linker hash table
73*a9fa9459Szrj 
74*a9fa9459Szrj @cindex _bfd_link_hash_table_create in target vector
75*a9fa9459Szrj @cindex target vector (_bfd_link_hash_table_create)
76*a9fa9459Szrj 	The linker routines must create a hash table, which must be
77*a9fa9459Szrj 	derived from <<struct bfd_link_hash_table>> described in
78*a9fa9459Szrj 	<<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
79*a9fa9459Szrj 	create a derived hash table.  This entry point is called using
80*a9fa9459Szrj 	the target vector of the linker output file.
81*a9fa9459Szrj 
82*a9fa9459Szrj 	The <<_bfd_link_hash_table_create>> entry point must allocate
83*a9fa9459Szrj 	and initialize an instance of the desired hash table.  If the
84*a9fa9459Szrj 	back end does not require any additional information to be
85*a9fa9459Szrj 	stored with the entries in the hash table, the entry point may
86*a9fa9459Szrj 	simply create a <<struct bfd_link_hash_table>>.  Most likely,
87*a9fa9459Szrj 	however, some additional information will be needed.
88*a9fa9459Szrj 
89*a9fa9459Szrj 	For example, with each entry in the hash table the a.out
90*a9fa9459Szrj 	linker keeps the index the symbol has in the final output file
91*a9fa9459Szrj 	(this index number is used so that when doing a relocatable
92*a9fa9459Szrj 	link the symbol index used in the output file can be quickly
93*a9fa9459Szrj 	filled in when copying over a reloc).  The a.out linker code
94*a9fa9459Szrj 	defines the required structures and functions for a hash table
95*a9fa9459Szrj 	derived from <<struct bfd_link_hash_table>>.  The a.out linker
96*a9fa9459Szrj 	hash table is created by the function
97*a9fa9459Szrj 	<<NAME(aout,link_hash_table_create)>>; it simply allocates
98*a9fa9459Szrj 	space for the hash table, initializes it, and returns a
99*a9fa9459Szrj 	pointer to it.
100*a9fa9459Szrj 
101*a9fa9459Szrj 	When writing the linker routines for a new back end, you will
102*a9fa9459Szrj 	generally not know exactly which fields will be required until
103*a9fa9459Szrj 	you have finished.  You should simply create a new hash table
104*a9fa9459Szrj 	which defines no additional fields, and then simply add fields
105*a9fa9459Szrj 	as they become necessary.
106*a9fa9459Szrj 
107*a9fa9459Szrj INODE
108*a9fa9459Szrj Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
109*a9fa9459Szrj SUBSECTION
110*a9fa9459Szrj 	Adding symbols to the hash table
111*a9fa9459Szrj 
112*a9fa9459Szrj @cindex _bfd_link_add_symbols in target vector
113*a9fa9459Szrj @cindex target vector (_bfd_link_add_symbols)
114*a9fa9459Szrj 	The linker proper will call the <<_bfd_link_add_symbols>>
115*a9fa9459Szrj 	entry point for each object file or archive which is to be
116*a9fa9459Szrj 	linked (typically these are the files named on the command
117*a9fa9459Szrj 	line, but some may also come from the linker script).  The
118*a9fa9459Szrj 	entry point is responsible for examining the file.  For an
119*a9fa9459Szrj 	object file, BFD must add any relevant symbol information to
120*a9fa9459Szrj 	the hash table.  For an archive, BFD must determine which
121*a9fa9459Szrj 	elements of the archive should be used and adding them to the
122*a9fa9459Szrj 	link.
123*a9fa9459Szrj 
124*a9fa9459Szrj 	The a.out version of this entry point is
125*a9fa9459Szrj 	<<NAME(aout,link_add_symbols)>>.
126*a9fa9459Szrj 
127*a9fa9459Szrj @menu
128*a9fa9459Szrj @* Differing file formats::
129*a9fa9459Szrj @* Adding symbols from an object file::
130*a9fa9459Szrj @* Adding symbols from an archive::
131*a9fa9459Szrj @end menu
132*a9fa9459Szrj 
133*a9fa9459Szrj INODE
134*a9fa9459Szrj Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
135*a9fa9459Szrj SUBSUBSECTION
136*a9fa9459Szrj 	Differing file formats
137*a9fa9459Szrj 
138*a9fa9459Szrj 	Normally all the files involved in a link will be of the same
139*a9fa9459Szrj 	format, but it is also possible to link together different
140*a9fa9459Szrj 	format object files, and the back end must support that.  The
141*a9fa9459Szrj 	<<_bfd_link_add_symbols>> entry point is called via the target
142*a9fa9459Szrj 	vector of the file to be added.  This has an important
143*a9fa9459Szrj 	consequence: the function may not assume that the hash table
144*a9fa9459Szrj 	is the type created by the corresponding
145*a9fa9459Szrj 	<<_bfd_link_hash_table_create>> vector.  All the
146*a9fa9459Szrj 	<<_bfd_link_add_symbols>> function can assume about the hash
147*a9fa9459Szrj 	table is that it is derived from <<struct
148*a9fa9459Szrj 	bfd_link_hash_table>>.
149*a9fa9459Szrj 
150*a9fa9459Szrj 	Sometimes the <<_bfd_link_add_symbols>> function must store
151*a9fa9459Szrj 	some information in the hash table entry to be used by the
152*a9fa9459Szrj 	<<_bfd_final_link>> function.  In such a case the output bfd
153*a9fa9459Szrj 	xvec must be checked to make sure that the hash table was
154*a9fa9459Szrj 	created by an object file of the same format.
155*a9fa9459Szrj 
156*a9fa9459Szrj 	The <<_bfd_final_link>> routine must be prepared to handle a
157*a9fa9459Szrj 	hash entry without any extra information added by the
158*a9fa9459Szrj 	<<_bfd_link_add_symbols>> function.  A hash entry without
159*a9fa9459Szrj 	extra information will also occur when the linker script
160*a9fa9459Szrj 	directs the linker to create a symbol.  Note that, regardless
161*a9fa9459Szrj 	of how a hash table entry is added, all the fields will be
162*a9fa9459Szrj 	initialized to some sort of null value by the hash table entry
163*a9fa9459Szrj 	initialization function.
164*a9fa9459Szrj 
165*a9fa9459Szrj 	See <<ecoff_link_add_externals>> for an example of how to
166*a9fa9459Szrj 	check the output bfd before saving information (in this
167*a9fa9459Szrj 	case, the ECOFF external symbol debugging information) in a
168*a9fa9459Szrj 	hash table entry.
169*a9fa9459Szrj 
170*a9fa9459Szrj INODE
171*a9fa9459Szrj Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
172*a9fa9459Szrj SUBSUBSECTION
173*a9fa9459Szrj 	Adding symbols from an object file
174*a9fa9459Szrj 
175*a9fa9459Szrj 	When the <<_bfd_link_add_symbols>> routine is passed an object
176*a9fa9459Szrj 	file, it must add all externally visible symbols in that
177*a9fa9459Szrj 	object file to the hash table.  The actual work of adding the
178*a9fa9459Szrj 	symbol to the hash table is normally handled by the function
179*a9fa9459Szrj 	<<_bfd_generic_link_add_one_symbol>>.  The
180*a9fa9459Szrj 	<<_bfd_link_add_symbols>> routine is responsible for reading
181*a9fa9459Szrj 	all the symbols from the object file and passing the correct
182*a9fa9459Szrj 	information to <<_bfd_generic_link_add_one_symbol>>.
183*a9fa9459Szrj 
184*a9fa9459Szrj 	The <<_bfd_link_add_symbols>> routine should not use
185*a9fa9459Szrj 	<<bfd_canonicalize_symtab>> to read the symbols.  The point of
186*a9fa9459Szrj 	providing this routine is to avoid the overhead of converting
187*a9fa9459Szrj 	the symbols into generic <<asymbol>> structures.
188*a9fa9459Szrj 
189*a9fa9459Szrj @findex _bfd_generic_link_add_one_symbol
190*a9fa9459Szrj 	<<_bfd_generic_link_add_one_symbol>> handles the details of
191*a9fa9459Szrj 	combining common symbols, warning about multiple definitions,
192*a9fa9459Szrj 	and so forth.  It takes arguments which describe the symbol to
193*a9fa9459Szrj 	add, notably symbol flags, a section, and an offset.  The
194*a9fa9459Szrj 	symbol flags include such things as <<BSF_WEAK>> or
195*a9fa9459Szrj 	<<BSF_INDIRECT>>.  The section is a section in the object
196*a9fa9459Szrj 	file, or something like <<bfd_und_section_ptr>> for an undefined
197*a9fa9459Szrj 	symbol or <<bfd_com_section_ptr>> for a common symbol.
198*a9fa9459Szrj 
199*a9fa9459Szrj 	If the <<_bfd_final_link>> routine is also going to need to
200*a9fa9459Szrj 	read the symbol information, the <<_bfd_link_add_symbols>>
201*a9fa9459Szrj 	routine should save it somewhere attached to the object file
202*a9fa9459Szrj 	BFD.  However, the information should only be saved if the
203*a9fa9459Szrj 	<<keep_memory>> field of the <<info>> argument is TRUE, so
204*a9fa9459Szrj 	that the <<-no-keep-memory>> linker switch is effective.
205*a9fa9459Szrj 
206*a9fa9459Szrj 	The a.out function which adds symbols from an object file is
207*a9fa9459Szrj 	<<aout_link_add_object_symbols>>, and most of the interesting
208*a9fa9459Szrj 	work is in <<aout_link_add_symbols>>.  The latter saves
209*a9fa9459Szrj 	pointers to the hash tables entries created by
210*a9fa9459Szrj 	<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
211*a9fa9459Szrj 	so that the <<_bfd_final_link>> routine does not have to call
212*a9fa9459Szrj 	the hash table lookup routine to locate the entry.
213*a9fa9459Szrj 
214*a9fa9459Szrj INODE
215*a9fa9459Szrj Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
216*a9fa9459Szrj SUBSUBSECTION
217*a9fa9459Szrj 	Adding symbols from an archive
218*a9fa9459Szrj 
219*a9fa9459Szrj 	When the <<_bfd_link_add_symbols>> routine is passed an
220*a9fa9459Szrj 	archive, it must look through the symbols defined by the
221*a9fa9459Szrj 	archive and decide which elements of the archive should be
222*a9fa9459Szrj 	included in the link.  For each such element it must call the
223*a9fa9459Szrj 	<<add_archive_element>> linker callback, and it must add the
224*a9fa9459Szrj 	symbols from the object file to the linker hash table.  (The
225*a9fa9459Szrj 	callback may in fact indicate that a replacement BFD should be
226*a9fa9459Szrj 	used, in which case the symbols from that BFD should be added
227*a9fa9459Szrj 	to the linker hash table instead.)
228*a9fa9459Szrj 
229*a9fa9459Szrj @findex _bfd_generic_link_add_archive_symbols
230*a9fa9459Szrj 	In most cases the work of looking through the symbols in the
231*a9fa9459Szrj 	archive should be done by the
232*a9fa9459Szrj 	<<_bfd_generic_link_add_archive_symbols>> function.
233*a9fa9459Szrj 	<<_bfd_generic_link_add_archive_symbols>> is passed a function
234*a9fa9459Szrj 	to call to make the final decision about adding an archive
235*a9fa9459Szrj 	element to the link and to do the actual work of adding the
236*a9fa9459Szrj 	symbols to the linker hash table.  If the element is to
237*a9fa9459Szrj 	be included, the <<add_archive_element>> linker callback
238*a9fa9459Szrj 	routine must be called with the element as an argument, and
239*a9fa9459Szrj 	the element's symbols must be added to the linker hash table
240*a9fa9459Szrj 	just as though the element had itself been passed to the
241*a9fa9459Szrj 	<<_bfd_link_add_symbols>> function.
242*a9fa9459Szrj 
243*a9fa9459Szrj 	When the a.out <<_bfd_link_add_symbols>> function receives an
244*a9fa9459Szrj 	archive, it calls <<_bfd_generic_link_add_archive_symbols>>
245*a9fa9459Szrj 	passing <<aout_link_check_archive_element>> as the function
246*a9fa9459Szrj 	argument. <<aout_link_check_archive_element>> calls
247*a9fa9459Szrj 	<<aout_link_check_ar_symbols>>.  If the latter decides to add
248*a9fa9459Szrj 	the element (an element is only added if it provides a real,
249*a9fa9459Szrj 	non-common, definition for a previously undefined or common
250*a9fa9459Szrj 	symbol) it calls the <<add_archive_element>> callback and then
251*a9fa9459Szrj 	<<aout_link_check_archive_element>> calls
252*a9fa9459Szrj 	<<aout_link_add_symbols>> to actually add the symbols to the
253*a9fa9459Szrj 	linker hash table - possibly those of a substitute BFD, if the
254*a9fa9459Szrj 	<<add_archive_element>> callback avails itself of that option.
255*a9fa9459Szrj 
256*a9fa9459Szrj 	The ECOFF back end is unusual in that it does not normally
257*a9fa9459Szrj 	call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
258*a9fa9459Szrj 	archives already contain a hash table of symbols.  The ECOFF
259*a9fa9459Szrj 	back end searches the archive itself to avoid the overhead of
260*a9fa9459Szrj 	creating a new hash table.
261*a9fa9459Szrj 
262*a9fa9459Szrj INODE
263*a9fa9459Szrj Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
264*a9fa9459Szrj SUBSECTION
265*a9fa9459Szrj 	Performing the final link
266*a9fa9459Szrj 
267*a9fa9459Szrj @cindex _bfd_link_final_link in target vector
268*a9fa9459Szrj @cindex target vector (_bfd_final_link)
269*a9fa9459Szrj 	When all the input files have been processed, the linker calls
270*a9fa9459Szrj 	the <<_bfd_final_link>> entry point of the output BFD.  This
271*a9fa9459Szrj 	routine is responsible for producing the final output file,
272*a9fa9459Szrj 	which has several aspects.  It must relocate the contents of
273*a9fa9459Szrj 	the input sections and copy the data into the output sections.
274*a9fa9459Szrj 	It must build an output symbol table including any local
275*a9fa9459Szrj 	symbols from the input files and the global symbols from the
276*a9fa9459Szrj 	hash table.  When producing relocatable output, it must
277*a9fa9459Szrj 	modify the input relocs and write them into the output file.
278*a9fa9459Szrj 	There may also be object format dependent work to be done.
279*a9fa9459Szrj 
280*a9fa9459Szrj 	The linker will also call the <<write_object_contents>> entry
281*a9fa9459Szrj 	point when the BFD is closed.  The two entry points must work
282*a9fa9459Szrj 	together in order to produce the correct output file.
283*a9fa9459Szrj 
284*a9fa9459Szrj 	The details of how this works are inevitably dependent upon
285*a9fa9459Szrj 	the specific object file format.  The a.out
286*a9fa9459Szrj 	<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
287*a9fa9459Szrj 
288*a9fa9459Szrj @menu
289*a9fa9459Szrj @* Information provided by the linker::
290*a9fa9459Szrj @* Relocating the section contents::
291*a9fa9459Szrj @* Writing the symbol table::
292*a9fa9459Szrj @end menu
293*a9fa9459Szrj 
294*a9fa9459Szrj INODE
295*a9fa9459Szrj Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
296*a9fa9459Szrj SUBSUBSECTION
297*a9fa9459Szrj 	Information provided by the linker
298*a9fa9459Szrj 
299*a9fa9459Szrj 	Before the linker calls the <<_bfd_final_link>> entry point,
300*a9fa9459Szrj 	it sets up some data structures for the function to use.
301*a9fa9459Szrj 
302*a9fa9459Szrj 	The <<input_bfds>> field of the <<bfd_link_info>> structure
303*a9fa9459Szrj 	will point to a list of all the input files included in the
304*a9fa9459Szrj 	link.  These files are linked through the <<link.next>> field
305*a9fa9459Szrj 	of the <<bfd>> structure.
306*a9fa9459Szrj 
307*a9fa9459Szrj 	Each section in the output file will have a list of
308*a9fa9459Szrj 	<<link_order>> structures attached to the <<map_head.link_order>>
309*a9fa9459Szrj 	field (the <<link_order>> structure is defined in
310*a9fa9459Szrj 	<<bfdlink.h>>).  These structures describe how to create the
311*a9fa9459Szrj 	contents of the output section in terms of the contents of
312*a9fa9459Szrj 	various input sections, fill constants, and, eventually, other
313*a9fa9459Szrj 	types of information.  They also describe relocs that must be
314*a9fa9459Szrj 	created by the BFD backend, but do not correspond to any input
315*a9fa9459Szrj 	file; this is used to support -Ur, which builds constructors
316*a9fa9459Szrj 	while generating a relocatable object file.
317*a9fa9459Szrj 
318*a9fa9459Szrj INODE
319*a9fa9459Szrj Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
320*a9fa9459Szrj SUBSUBSECTION
321*a9fa9459Szrj 	Relocating the section contents
322*a9fa9459Szrj 
323*a9fa9459Szrj 	The <<_bfd_final_link>> function should look through the
324*a9fa9459Szrj 	<<link_order>> structures attached to each section of the
325*a9fa9459Szrj 	output file.  Each <<link_order>> structure should either be
326*a9fa9459Szrj 	handled specially, or it should be passed to the function
327*a9fa9459Szrj 	<<_bfd_default_link_order>> which will do the right thing
328*a9fa9459Szrj 	(<<_bfd_default_link_order>> is defined in <<linker.c>>).
329*a9fa9459Szrj 
330*a9fa9459Szrj 	For efficiency, a <<link_order>> of type
331*a9fa9459Szrj 	<<bfd_indirect_link_order>> whose associated section belongs
332*a9fa9459Szrj 	to a BFD of the same format as the output BFD must be handled
333*a9fa9459Szrj 	specially.  This type of <<link_order>> describes part of an
334*a9fa9459Szrj 	output section in terms of a section belonging to one of the
335*a9fa9459Szrj 	input files.  The <<_bfd_final_link>> function should read the
336*a9fa9459Szrj 	contents of the section and any associated relocs, apply the
337*a9fa9459Szrj 	relocs to the section contents, and write out the modified
338*a9fa9459Szrj 	section contents.  If performing a relocatable link, the
339*a9fa9459Szrj 	relocs themselves must also be modified and written out.
340*a9fa9459Szrj 
341*a9fa9459Szrj @findex _bfd_relocate_contents
342*a9fa9459Szrj @findex _bfd_final_link_relocate
343*a9fa9459Szrj 	The functions <<_bfd_relocate_contents>> and
344*a9fa9459Szrj 	<<_bfd_final_link_relocate>> provide some general support for
345*a9fa9459Szrj 	performing the actual relocations, notably overflow checking.
346*a9fa9459Szrj 	Their arguments include information about the symbol the
347*a9fa9459Szrj 	relocation is against and a <<reloc_howto_type>> argument
348*a9fa9459Szrj 	which describes the relocation to perform.  These functions
349*a9fa9459Szrj 	are defined in <<reloc.c>>.
350*a9fa9459Szrj 
351*a9fa9459Szrj 	The a.out function which handles reading, relocating, and
352*a9fa9459Szrj 	writing section contents is <<aout_link_input_section>>.  The
353*a9fa9459Szrj 	actual relocation is done in <<aout_link_input_section_std>>
354*a9fa9459Szrj 	and <<aout_link_input_section_ext>>.
355*a9fa9459Szrj 
356*a9fa9459Szrj INODE
357*a9fa9459Szrj Writing the symbol table, , Relocating the section contents, Performing the Final Link
358*a9fa9459Szrj SUBSUBSECTION
359*a9fa9459Szrj 	Writing the symbol table
360*a9fa9459Szrj 
361*a9fa9459Szrj 	The <<_bfd_final_link>> function must gather all the symbols
362*a9fa9459Szrj 	in the input files and write them out.  It must also write out
363*a9fa9459Szrj 	all the symbols in the global hash table.  This must be
364*a9fa9459Szrj 	controlled by the <<strip>> and <<discard>> fields of the
365*a9fa9459Szrj 	<<bfd_link_info>> structure.
366*a9fa9459Szrj 
367*a9fa9459Szrj 	The local symbols of the input files will not have been
368*a9fa9459Szrj 	entered into the linker hash table.  The <<_bfd_final_link>>
369*a9fa9459Szrj 	routine must consider each input file and include the symbols
370*a9fa9459Szrj 	in the output file.  It may be convenient to do this when
371*a9fa9459Szrj 	looking through the <<link_order>> structures, or it may be
372*a9fa9459Szrj 	done by stepping through the <<input_bfds>> list.
373*a9fa9459Szrj 
374*a9fa9459Szrj 	The <<_bfd_final_link>> routine must also traverse the global
375*a9fa9459Szrj 	hash table to gather all the externally visible symbols.  It
376*a9fa9459Szrj 	is possible that most of the externally visible symbols may be
377*a9fa9459Szrj 	written out when considering the symbols of each input file,
378*a9fa9459Szrj 	but it is still necessary to traverse the hash table since the
379*a9fa9459Szrj 	linker script may have defined some symbols that are not in
380*a9fa9459Szrj 	any of the input files.
381*a9fa9459Szrj 
382*a9fa9459Szrj 	The <<strip>> field of the <<bfd_link_info>> structure
383*a9fa9459Szrj 	controls which symbols are written out.  The possible values
384*a9fa9459Szrj 	are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
385*a9fa9459Szrj 	then the <<keep_hash>> field of the <<bfd_link_info>>
386*a9fa9459Szrj 	structure is a hash table of symbols to keep; each symbol
387*a9fa9459Szrj 	should be looked up in this hash table, and only symbols which
388*a9fa9459Szrj 	are present should be included in the output file.
389*a9fa9459Szrj 
390*a9fa9459Szrj 	If the <<strip>> field of the <<bfd_link_info>> structure
391*a9fa9459Szrj 	permits local symbols to be written out, the <<discard>> field
392*a9fa9459Szrj 	is used to further controls which local symbols are included
393*a9fa9459Szrj 	in the output file.  If the value is <<discard_l>>, then all
394*a9fa9459Szrj 	local symbols which begin with a certain prefix are discarded;
395*a9fa9459Szrj 	this is controlled by the <<bfd_is_local_label_name>> entry point.
396*a9fa9459Szrj 
397*a9fa9459Szrj 	The a.out backend handles symbols by calling
398*a9fa9459Szrj 	<<aout_link_write_symbols>> on each input BFD and then
399*a9fa9459Szrj 	traversing the global hash table with the function
400*a9fa9459Szrj 	<<aout_link_write_other_symbol>>.  It builds a string table
401*a9fa9459Szrj 	while writing out the symbols, which is written to the output
402*a9fa9459Szrj 	file at the end of <<NAME(aout,final_link)>>.
403*a9fa9459Szrj */
404*a9fa9459Szrj 
405*a9fa9459Szrj static bfd_boolean generic_link_add_object_symbols
406*a9fa9459Szrj   (bfd *, struct bfd_link_info *, bfd_boolean collect);
407*a9fa9459Szrj static bfd_boolean generic_link_add_symbols
408*a9fa9459Szrj   (bfd *, struct bfd_link_info *, bfd_boolean);
409*a9fa9459Szrj static bfd_boolean generic_link_check_archive_element_no_collect
410*a9fa9459Szrj   (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
411*a9fa9459Szrj    bfd_boolean *);
412*a9fa9459Szrj static bfd_boolean generic_link_check_archive_element_collect
413*a9fa9459Szrj   (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
414*a9fa9459Szrj    bfd_boolean *);
415*a9fa9459Szrj static bfd_boolean generic_link_check_archive_element
416*a9fa9459Szrj   (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
417*a9fa9459Szrj    bfd_boolean *, bfd_boolean);
418*a9fa9459Szrj static bfd_boolean generic_link_add_symbol_list
419*a9fa9459Szrj   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
420*a9fa9459Szrj    bfd_boolean);
421*a9fa9459Szrj static bfd_boolean generic_add_output_symbol
422*a9fa9459Szrj   (bfd *, size_t *psymalloc, asymbol *);
423*a9fa9459Szrj static bfd_boolean default_data_link_order
424*a9fa9459Szrj   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
425*a9fa9459Szrj static bfd_boolean default_indirect_link_order
426*a9fa9459Szrj   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
427*a9fa9459Szrj    bfd_boolean);
428*a9fa9459Szrj 
429*a9fa9459Szrj /* The link hash table structure is defined in bfdlink.h.  It provides
430*a9fa9459Szrj    a base hash table which the backend specific hash tables are built
431*a9fa9459Szrj    upon.  */
432*a9fa9459Szrj 
433*a9fa9459Szrj /* Routine to create an entry in the link hash table.  */
434*a9fa9459Szrj 
435*a9fa9459Szrj struct bfd_hash_entry *
_bfd_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)436*a9fa9459Szrj _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
437*a9fa9459Szrj 			struct bfd_hash_table *table,
438*a9fa9459Szrj 			const char *string)
439*a9fa9459Szrj {
440*a9fa9459Szrj   /* Allocate the structure if it has not already been allocated by a
441*a9fa9459Szrj      subclass.  */
442*a9fa9459Szrj   if (entry == NULL)
443*a9fa9459Szrj     {
444*a9fa9459Szrj       entry = (struct bfd_hash_entry *)
445*a9fa9459Szrj           bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
446*a9fa9459Szrj       if (entry == NULL)
447*a9fa9459Szrj 	return entry;
448*a9fa9459Szrj     }
449*a9fa9459Szrj 
450*a9fa9459Szrj   /* Call the allocation method of the superclass.  */
451*a9fa9459Szrj   entry = bfd_hash_newfunc (entry, table, string);
452*a9fa9459Szrj   if (entry)
453*a9fa9459Szrj     {
454*a9fa9459Szrj       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
455*a9fa9459Szrj 
456*a9fa9459Szrj       /* Initialize the local fields.  */
457*a9fa9459Szrj       memset ((char *) &h->root + sizeof (h->root), 0,
458*a9fa9459Szrj 	      sizeof (*h) - sizeof (h->root));
459*a9fa9459Szrj     }
460*a9fa9459Szrj 
461*a9fa9459Szrj   return entry;
462*a9fa9459Szrj }
463*a9fa9459Szrj 
464*a9fa9459Szrj /* Initialize a link hash table.  The BFD argument is the one
465*a9fa9459Szrj    responsible for creating this table.  */
466*a9fa9459Szrj 
467*a9fa9459Szrj bfd_boolean
_bfd_link_hash_table_init(struct bfd_link_hash_table * table,bfd * abfd ATTRIBUTE_UNUSED,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize)468*a9fa9459Szrj _bfd_link_hash_table_init
469*a9fa9459Szrj   (struct bfd_link_hash_table *table,
470*a9fa9459Szrj    bfd *abfd ATTRIBUTE_UNUSED,
471*a9fa9459Szrj    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
472*a9fa9459Szrj 				      struct bfd_hash_table *,
473*a9fa9459Szrj 				      const char *),
474*a9fa9459Szrj    unsigned int entsize)
475*a9fa9459Szrj {
476*a9fa9459Szrj   bfd_boolean ret;
477*a9fa9459Szrj 
478*a9fa9459Szrj   BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
479*a9fa9459Szrj   table->undefs = NULL;
480*a9fa9459Szrj   table->undefs_tail = NULL;
481*a9fa9459Szrj   table->type = bfd_link_generic_hash_table;
482*a9fa9459Szrj 
483*a9fa9459Szrj   ret = bfd_hash_table_init (&table->table, newfunc, entsize);
484*a9fa9459Szrj   if (ret)
485*a9fa9459Szrj     {
486*a9fa9459Szrj       /* Arrange for destruction of this hash table on closing ABFD.  */
487*a9fa9459Szrj       table->hash_table_free = _bfd_generic_link_hash_table_free;
488*a9fa9459Szrj       abfd->link.hash = table;
489*a9fa9459Szrj       abfd->is_linker_output = TRUE;
490*a9fa9459Szrj     }
491*a9fa9459Szrj   return ret;
492*a9fa9459Szrj }
493*a9fa9459Szrj 
494*a9fa9459Szrj /* Look up a symbol in a link hash table.  If follow is TRUE, we
495*a9fa9459Szrj    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
496*a9fa9459Szrj    the real symbol.  */
497*a9fa9459Szrj 
498*a9fa9459Szrj struct bfd_link_hash_entry *
bfd_link_hash_lookup(struct bfd_link_hash_table * table,const char * string,bfd_boolean create,bfd_boolean copy,bfd_boolean follow)499*a9fa9459Szrj bfd_link_hash_lookup (struct bfd_link_hash_table *table,
500*a9fa9459Szrj 		      const char *string,
501*a9fa9459Szrj 		      bfd_boolean create,
502*a9fa9459Szrj 		      bfd_boolean copy,
503*a9fa9459Szrj 		      bfd_boolean follow)
504*a9fa9459Szrj {
505*a9fa9459Szrj   struct bfd_link_hash_entry *ret;
506*a9fa9459Szrj 
507*a9fa9459Szrj   ret = ((struct bfd_link_hash_entry *)
508*a9fa9459Szrj 	 bfd_hash_lookup (&table->table, string, create, copy));
509*a9fa9459Szrj 
510*a9fa9459Szrj   if (follow && ret != NULL)
511*a9fa9459Szrj     {
512*a9fa9459Szrj       while (ret->type == bfd_link_hash_indirect
513*a9fa9459Szrj 	     || ret->type == bfd_link_hash_warning)
514*a9fa9459Szrj 	ret = ret->u.i.link;
515*a9fa9459Szrj     }
516*a9fa9459Szrj 
517*a9fa9459Szrj   return ret;
518*a9fa9459Szrj }
519*a9fa9459Szrj 
520*a9fa9459Szrj /* Look up a symbol in the main linker hash table if the symbol might
521*a9fa9459Szrj    be wrapped.  This should only be used for references to an
522*a9fa9459Szrj    undefined symbol, not for definitions of a symbol.  */
523*a9fa9459Szrj 
524*a9fa9459Szrj struct bfd_link_hash_entry *
bfd_wrapped_link_hash_lookup(bfd * abfd,struct bfd_link_info * info,const char * string,bfd_boolean create,bfd_boolean copy,bfd_boolean follow)525*a9fa9459Szrj bfd_wrapped_link_hash_lookup (bfd *abfd,
526*a9fa9459Szrj 			      struct bfd_link_info *info,
527*a9fa9459Szrj 			      const char *string,
528*a9fa9459Szrj 			      bfd_boolean create,
529*a9fa9459Szrj 			      bfd_boolean copy,
530*a9fa9459Szrj 			      bfd_boolean follow)
531*a9fa9459Szrj {
532*a9fa9459Szrj   bfd_size_type amt;
533*a9fa9459Szrj 
534*a9fa9459Szrj   if (info->wrap_hash != NULL)
535*a9fa9459Szrj     {
536*a9fa9459Szrj       const char *l;
537*a9fa9459Szrj       char prefix = '\0';
538*a9fa9459Szrj 
539*a9fa9459Szrj       l = string;
540*a9fa9459Szrj       if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
541*a9fa9459Szrj 	{
542*a9fa9459Szrj 	  prefix = *l;
543*a9fa9459Szrj 	  ++l;
544*a9fa9459Szrj 	}
545*a9fa9459Szrj 
546*a9fa9459Szrj #undef WRAP
547*a9fa9459Szrj #define WRAP "__wrap_"
548*a9fa9459Szrj 
549*a9fa9459Szrj       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
550*a9fa9459Szrj 	{
551*a9fa9459Szrj 	  char *n;
552*a9fa9459Szrj 	  struct bfd_link_hash_entry *h;
553*a9fa9459Szrj 
554*a9fa9459Szrj 	  /* This symbol is being wrapped.  We want to replace all
555*a9fa9459Szrj              references to SYM with references to __wrap_SYM.  */
556*a9fa9459Szrj 
557*a9fa9459Szrj 	  amt = strlen (l) + sizeof WRAP + 1;
558*a9fa9459Szrj 	  n = (char *) bfd_malloc (amt);
559*a9fa9459Szrj 	  if (n == NULL)
560*a9fa9459Szrj 	    return NULL;
561*a9fa9459Szrj 
562*a9fa9459Szrj 	  n[0] = prefix;
563*a9fa9459Szrj 	  n[1] = '\0';
564*a9fa9459Szrj 	  strcat (n, WRAP);
565*a9fa9459Szrj 	  strcat (n, l);
566*a9fa9459Szrj 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
567*a9fa9459Szrj 	  free (n);
568*a9fa9459Szrj 	  return h;
569*a9fa9459Szrj 	}
570*a9fa9459Szrj 
571*a9fa9459Szrj #undef  REAL
572*a9fa9459Szrj #define REAL "__real_"
573*a9fa9459Szrj 
574*a9fa9459Szrj       if (*l == '_'
575*a9fa9459Szrj 	  && CONST_STRNEQ (l, REAL)
576*a9fa9459Szrj 	  && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
577*a9fa9459Szrj 			      FALSE, FALSE) != NULL)
578*a9fa9459Szrj 	{
579*a9fa9459Szrj 	  char *n;
580*a9fa9459Szrj 	  struct bfd_link_hash_entry *h;
581*a9fa9459Szrj 
582*a9fa9459Szrj 	  /* This is a reference to __real_SYM, where SYM is being
583*a9fa9459Szrj              wrapped.  We want to replace all references to __real_SYM
584*a9fa9459Szrj              with references to SYM.  */
585*a9fa9459Szrj 
586*a9fa9459Szrj 	  amt = strlen (l + sizeof REAL - 1) + 2;
587*a9fa9459Szrj 	  n = (char *) bfd_malloc (amt);
588*a9fa9459Szrj 	  if (n == NULL)
589*a9fa9459Szrj 	    return NULL;
590*a9fa9459Szrj 
591*a9fa9459Szrj 	  n[0] = prefix;
592*a9fa9459Szrj 	  n[1] = '\0';
593*a9fa9459Szrj 	  strcat (n, l + sizeof REAL - 1);
594*a9fa9459Szrj 	  h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
595*a9fa9459Szrj 	  free (n);
596*a9fa9459Szrj 	  return h;
597*a9fa9459Szrj 	}
598*a9fa9459Szrj 
599*a9fa9459Szrj #undef REAL
600*a9fa9459Szrj     }
601*a9fa9459Szrj 
602*a9fa9459Szrj   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
603*a9fa9459Szrj }
604*a9fa9459Szrj 
605*a9fa9459Szrj /* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
606*a9fa9459Szrj    and the remainder is found in wrap_hash, return the real symbol.  */
607*a9fa9459Szrj 
608*a9fa9459Szrj struct bfd_link_hash_entry *
unwrap_hash_lookup(struct bfd_link_info * info,bfd * input_bfd,struct bfd_link_hash_entry * h)609*a9fa9459Szrj unwrap_hash_lookup (struct bfd_link_info *info,
610*a9fa9459Szrj 		    bfd *input_bfd,
611*a9fa9459Szrj 		    struct bfd_link_hash_entry *h)
612*a9fa9459Szrj {
613*a9fa9459Szrj   const char *l = h->root.string;
614*a9fa9459Szrj 
615*a9fa9459Szrj   if (*l == bfd_get_symbol_leading_char (input_bfd)
616*a9fa9459Szrj       || *l == info->wrap_char)
617*a9fa9459Szrj     ++l;
618*a9fa9459Szrj 
619*a9fa9459Szrj   if (CONST_STRNEQ (l, WRAP))
620*a9fa9459Szrj     {
621*a9fa9459Szrj       l += sizeof WRAP - 1;
622*a9fa9459Szrj 
623*a9fa9459Szrj       if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
624*a9fa9459Szrj 	{
625*a9fa9459Szrj 	  char save = 0;
626*a9fa9459Szrj 	  if (l - (sizeof WRAP - 1) != h->root.string)
627*a9fa9459Szrj 	    {
628*a9fa9459Szrj 	      --l;
629*a9fa9459Szrj 	      save = *l;
630*a9fa9459Szrj 	      *(char *) l = *h->root.string;
631*a9fa9459Szrj 	    }
632*a9fa9459Szrj 	  h = bfd_link_hash_lookup (info->hash, l, FALSE, FALSE, FALSE);
633*a9fa9459Szrj 	  if (save)
634*a9fa9459Szrj 	    *(char *) l = save;
635*a9fa9459Szrj 	}
636*a9fa9459Szrj     }
637*a9fa9459Szrj   return h;
638*a9fa9459Szrj }
639*a9fa9459Szrj #undef WRAP
640*a9fa9459Szrj 
641*a9fa9459Szrj /* Traverse a generic link hash table.  Differs from bfd_hash_traverse
642*a9fa9459Szrj    in the treatment of warning symbols.  When warning symbols are
643*a9fa9459Szrj    created they replace the real symbol, so you don't get to see the
644*a9fa9459Szrj    real symbol in a bfd_hash_travere.  This traversal calls func with
645*a9fa9459Szrj    the real symbol.  */
646*a9fa9459Szrj 
647*a9fa9459Szrj void
bfd_link_hash_traverse(struct bfd_link_hash_table * htab,bfd_boolean (* func)(struct bfd_link_hash_entry *,void *),void * info)648*a9fa9459Szrj bfd_link_hash_traverse
649*a9fa9459Szrj   (struct bfd_link_hash_table *htab,
650*a9fa9459Szrj    bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
651*a9fa9459Szrj    void *info)
652*a9fa9459Szrj {
653*a9fa9459Szrj   unsigned int i;
654*a9fa9459Szrj 
655*a9fa9459Szrj   htab->table.frozen = 1;
656*a9fa9459Szrj   for (i = 0; i < htab->table.size; i++)
657*a9fa9459Szrj     {
658*a9fa9459Szrj       struct bfd_link_hash_entry *p;
659*a9fa9459Szrj 
660*a9fa9459Szrj       p = (struct bfd_link_hash_entry *) htab->table.table[i];
661*a9fa9459Szrj       for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
662*a9fa9459Szrj 	if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
663*a9fa9459Szrj 	  goto out;
664*a9fa9459Szrj     }
665*a9fa9459Szrj  out:
666*a9fa9459Szrj   htab->table.frozen = 0;
667*a9fa9459Szrj }
668*a9fa9459Szrj 
669*a9fa9459Szrj /* Add a symbol to the linker hash table undefs list.  */
670*a9fa9459Szrj 
671*a9fa9459Szrj void
bfd_link_add_undef(struct bfd_link_hash_table * table,struct bfd_link_hash_entry * h)672*a9fa9459Szrj bfd_link_add_undef (struct bfd_link_hash_table *table,
673*a9fa9459Szrj 		    struct bfd_link_hash_entry *h)
674*a9fa9459Szrj {
675*a9fa9459Szrj   BFD_ASSERT (h->u.undef.next == NULL);
676*a9fa9459Szrj   if (table->undefs_tail != NULL)
677*a9fa9459Szrj     table->undefs_tail->u.undef.next = h;
678*a9fa9459Szrj   if (table->undefs == NULL)
679*a9fa9459Szrj     table->undefs = h;
680*a9fa9459Szrj   table->undefs_tail = h;
681*a9fa9459Szrj }
682*a9fa9459Szrj 
683*a9fa9459Szrj /* The undefs list was designed so that in normal use we don't need to
684*a9fa9459Szrj    remove entries.  However, if symbols on the list are changed from
685*a9fa9459Szrj    bfd_link_hash_undefined to either bfd_link_hash_undefweak or
686*a9fa9459Szrj    bfd_link_hash_new for some reason, then they must be removed from the
687*a9fa9459Szrj    list.  Failure to do so might result in the linker attempting to add
688*a9fa9459Szrj    the symbol to the list again at a later stage.  */
689*a9fa9459Szrj 
690*a9fa9459Szrj void
bfd_link_repair_undef_list(struct bfd_link_hash_table * table)691*a9fa9459Szrj bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
692*a9fa9459Szrj {
693*a9fa9459Szrj   struct bfd_link_hash_entry **pun;
694*a9fa9459Szrj 
695*a9fa9459Szrj   pun = &table->undefs;
696*a9fa9459Szrj   while (*pun != NULL)
697*a9fa9459Szrj     {
698*a9fa9459Szrj       struct bfd_link_hash_entry *h = *pun;
699*a9fa9459Szrj 
700*a9fa9459Szrj       if (h->type == bfd_link_hash_new
701*a9fa9459Szrj 	  || h->type == bfd_link_hash_undefweak)
702*a9fa9459Szrj 	{
703*a9fa9459Szrj 	  *pun = h->u.undef.next;
704*a9fa9459Szrj 	  h->u.undef.next = NULL;
705*a9fa9459Szrj 	  if (h == table->undefs_tail)
706*a9fa9459Szrj 	    {
707*a9fa9459Szrj 	      if (pun == &table->undefs)
708*a9fa9459Szrj 		table->undefs_tail = NULL;
709*a9fa9459Szrj 	      else
710*a9fa9459Szrj 		/* pun points at an u.undef.next field.  Go back to
711*a9fa9459Szrj 		   the start of the link_hash_entry.  */
712*a9fa9459Szrj 		table->undefs_tail = (struct bfd_link_hash_entry *)
713*a9fa9459Szrj 		  ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
714*a9fa9459Szrj 	      break;
715*a9fa9459Szrj 	    }
716*a9fa9459Szrj 	}
717*a9fa9459Szrj       else
718*a9fa9459Szrj 	pun = &h->u.undef.next;
719*a9fa9459Szrj     }
720*a9fa9459Szrj }
721*a9fa9459Szrj 
722*a9fa9459Szrj /* Routine to create an entry in a generic link hash table.  */
723*a9fa9459Szrj 
724*a9fa9459Szrj struct bfd_hash_entry *
_bfd_generic_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)725*a9fa9459Szrj _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
726*a9fa9459Szrj 				struct bfd_hash_table *table,
727*a9fa9459Szrj 				const char *string)
728*a9fa9459Szrj {
729*a9fa9459Szrj   /* Allocate the structure if it has not already been allocated by a
730*a9fa9459Szrj      subclass.  */
731*a9fa9459Szrj   if (entry == NULL)
732*a9fa9459Szrj     {
733*a9fa9459Szrj       entry = (struct bfd_hash_entry *)
734*a9fa9459Szrj 	bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
735*a9fa9459Szrj       if (entry == NULL)
736*a9fa9459Szrj 	return entry;
737*a9fa9459Szrj     }
738*a9fa9459Szrj 
739*a9fa9459Szrj   /* Call the allocation method of the superclass.  */
740*a9fa9459Szrj   entry = _bfd_link_hash_newfunc (entry, table, string);
741*a9fa9459Szrj   if (entry)
742*a9fa9459Szrj     {
743*a9fa9459Szrj       struct generic_link_hash_entry *ret;
744*a9fa9459Szrj 
745*a9fa9459Szrj       /* Set local fields.  */
746*a9fa9459Szrj       ret = (struct generic_link_hash_entry *) entry;
747*a9fa9459Szrj       ret->written = FALSE;
748*a9fa9459Szrj       ret->sym = NULL;
749*a9fa9459Szrj     }
750*a9fa9459Szrj 
751*a9fa9459Szrj   return entry;
752*a9fa9459Szrj }
753*a9fa9459Szrj 
754*a9fa9459Szrj /* Create a generic link hash table.  */
755*a9fa9459Szrj 
756*a9fa9459Szrj struct bfd_link_hash_table *
_bfd_generic_link_hash_table_create(bfd * abfd)757*a9fa9459Szrj _bfd_generic_link_hash_table_create (bfd *abfd)
758*a9fa9459Szrj {
759*a9fa9459Szrj   struct generic_link_hash_table *ret;
760*a9fa9459Szrj   bfd_size_type amt = sizeof (struct generic_link_hash_table);
761*a9fa9459Szrj 
762*a9fa9459Szrj   ret = (struct generic_link_hash_table *) bfd_malloc (amt);
763*a9fa9459Szrj   if (ret == NULL)
764*a9fa9459Szrj     return NULL;
765*a9fa9459Szrj   if (! _bfd_link_hash_table_init (&ret->root, abfd,
766*a9fa9459Szrj 				   _bfd_generic_link_hash_newfunc,
767*a9fa9459Szrj 				   sizeof (struct generic_link_hash_entry)))
768*a9fa9459Szrj     {
769*a9fa9459Szrj       free (ret);
770*a9fa9459Szrj       return NULL;
771*a9fa9459Szrj     }
772*a9fa9459Szrj   return &ret->root;
773*a9fa9459Szrj }
774*a9fa9459Szrj 
775*a9fa9459Szrj void
_bfd_generic_link_hash_table_free(bfd * obfd)776*a9fa9459Szrj _bfd_generic_link_hash_table_free (bfd *obfd)
777*a9fa9459Szrj {
778*a9fa9459Szrj   struct generic_link_hash_table *ret;
779*a9fa9459Szrj 
780*a9fa9459Szrj   BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
781*a9fa9459Szrj   ret = (struct generic_link_hash_table *) obfd->link.hash;
782*a9fa9459Szrj   bfd_hash_table_free (&ret->root.table);
783*a9fa9459Szrj   free (ret);
784*a9fa9459Szrj   obfd->link.hash = NULL;
785*a9fa9459Szrj   obfd->is_linker_output = FALSE;
786*a9fa9459Szrj }
787*a9fa9459Szrj 
788*a9fa9459Szrj /* Grab the symbols for an object file when doing a generic link.  We
789*a9fa9459Szrj    store the symbols in the outsymbols field.  We need to keep them
790*a9fa9459Szrj    around for the entire link to ensure that we only read them once.
791*a9fa9459Szrj    If we read them multiple times, we might wind up with relocs and
792*a9fa9459Szrj    the hash table pointing to different instances of the symbol
793*a9fa9459Szrj    structure.  */
794*a9fa9459Szrj 
795*a9fa9459Szrj bfd_boolean
bfd_generic_link_read_symbols(bfd * abfd)796*a9fa9459Szrj bfd_generic_link_read_symbols (bfd *abfd)
797*a9fa9459Szrj {
798*a9fa9459Szrj   if (bfd_get_outsymbols (abfd) == NULL)
799*a9fa9459Szrj     {
800*a9fa9459Szrj       long symsize;
801*a9fa9459Szrj       long symcount;
802*a9fa9459Szrj 
803*a9fa9459Szrj       symsize = bfd_get_symtab_upper_bound (abfd);
804*a9fa9459Szrj       if (symsize < 0)
805*a9fa9459Szrj 	return FALSE;
806*a9fa9459Szrj       bfd_get_outsymbols (abfd) = (struct bfd_symbol **) bfd_alloc (abfd,
807*a9fa9459Szrj                                                                     symsize);
808*a9fa9459Szrj       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
809*a9fa9459Szrj 	return FALSE;
810*a9fa9459Szrj       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
811*a9fa9459Szrj       if (symcount < 0)
812*a9fa9459Szrj 	return FALSE;
813*a9fa9459Szrj       bfd_get_symcount (abfd) = symcount;
814*a9fa9459Szrj     }
815*a9fa9459Szrj 
816*a9fa9459Szrj   return TRUE;
817*a9fa9459Szrj }
818*a9fa9459Szrj 
819*a9fa9459Szrj /* Generic function to add symbols to from an object file to the
820*a9fa9459Szrj    global hash table.  This version does not automatically collect
821*a9fa9459Szrj    constructors by name.  */
822*a9fa9459Szrj 
823*a9fa9459Szrj bfd_boolean
_bfd_generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info)824*a9fa9459Szrj _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
825*a9fa9459Szrj {
826*a9fa9459Szrj   return generic_link_add_symbols (abfd, info, FALSE);
827*a9fa9459Szrj }
828*a9fa9459Szrj 
829*a9fa9459Szrj /* Generic function to add symbols from an object file to the global
830*a9fa9459Szrj    hash table.  This version automatically collects constructors by
831*a9fa9459Szrj    name, as the collect2 program does.  It should be used for any
832*a9fa9459Szrj    target which does not provide some other mechanism for setting up
833*a9fa9459Szrj    constructors and destructors; these are approximately those targets
834*a9fa9459Szrj    for which gcc uses collect2 and do not support stabs.  */
835*a9fa9459Szrj 
836*a9fa9459Szrj bfd_boolean
_bfd_generic_link_add_symbols_collect(bfd * abfd,struct bfd_link_info * info)837*a9fa9459Szrj _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
838*a9fa9459Szrj {
839*a9fa9459Szrj   return generic_link_add_symbols (abfd, info, TRUE);
840*a9fa9459Szrj }
841*a9fa9459Szrj 
842*a9fa9459Szrj /* Indicate that we are only retrieving symbol values from this
843*a9fa9459Szrj    section.  We want the symbols to act as though the values in the
844*a9fa9459Szrj    file are absolute.  */
845*a9fa9459Szrj 
846*a9fa9459Szrj void
_bfd_generic_link_just_syms(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED)847*a9fa9459Szrj _bfd_generic_link_just_syms (asection *sec,
848*a9fa9459Szrj 			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
849*a9fa9459Szrj {
850*a9fa9459Szrj   sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
851*a9fa9459Szrj   sec->output_section = bfd_abs_section_ptr;
852*a9fa9459Szrj   sec->output_offset = sec->vma;
853*a9fa9459Szrj }
854*a9fa9459Szrj 
855*a9fa9459Szrj /* Copy the symbol type and other attributes for a linker script
856*a9fa9459Szrj    assignment from HSRC to HDEST.
857*a9fa9459Szrj    The default implementation does nothing.  */
858*a9fa9459Szrj void
_bfd_generic_copy_link_hash_symbol_type(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * hdest ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * hsrc ATTRIBUTE_UNUSED)859*a9fa9459Szrj _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
860*a9fa9459Szrj     struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
861*a9fa9459Szrj     struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
862*a9fa9459Szrj {
863*a9fa9459Szrj }
864*a9fa9459Szrj 
865*a9fa9459Szrj /* Add symbols from an object file to the global hash table.  */
866*a9fa9459Szrj 
867*a9fa9459Szrj static bfd_boolean
generic_link_add_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)868*a9fa9459Szrj generic_link_add_symbols (bfd *abfd,
869*a9fa9459Szrj 			  struct bfd_link_info *info,
870*a9fa9459Szrj 			  bfd_boolean collect)
871*a9fa9459Szrj {
872*a9fa9459Szrj   bfd_boolean ret;
873*a9fa9459Szrj 
874*a9fa9459Szrj   switch (bfd_get_format (abfd))
875*a9fa9459Szrj     {
876*a9fa9459Szrj     case bfd_object:
877*a9fa9459Szrj       ret = generic_link_add_object_symbols (abfd, info, collect);
878*a9fa9459Szrj       break;
879*a9fa9459Szrj     case bfd_archive:
880*a9fa9459Szrj       ret = (_bfd_generic_link_add_archive_symbols
881*a9fa9459Szrj 	     (abfd, info,
882*a9fa9459Szrj 	      (collect
883*a9fa9459Szrj 	       ? generic_link_check_archive_element_collect
884*a9fa9459Szrj 	       : generic_link_check_archive_element_no_collect)));
885*a9fa9459Szrj       break;
886*a9fa9459Szrj     default:
887*a9fa9459Szrj       bfd_set_error (bfd_error_wrong_format);
888*a9fa9459Szrj       ret = FALSE;
889*a9fa9459Szrj     }
890*a9fa9459Szrj 
891*a9fa9459Szrj   return ret;
892*a9fa9459Szrj }
893*a9fa9459Szrj 
894*a9fa9459Szrj /* Add symbols from an object file to the global hash table.  */
895*a9fa9459Szrj 
896*a9fa9459Szrj static bfd_boolean
generic_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean collect)897*a9fa9459Szrj generic_link_add_object_symbols (bfd *abfd,
898*a9fa9459Szrj 				 struct bfd_link_info *info,
899*a9fa9459Szrj 				 bfd_boolean collect)
900*a9fa9459Szrj {
901*a9fa9459Szrj   bfd_size_type symcount;
902*a9fa9459Szrj   struct bfd_symbol **outsyms;
903*a9fa9459Szrj 
904*a9fa9459Szrj   if (!bfd_generic_link_read_symbols (abfd))
905*a9fa9459Szrj     return FALSE;
906*a9fa9459Szrj   symcount = _bfd_generic_link_get_symcount (abfd);
907*a9fa9459Szrj   outsyms = _bfd_generic_link_get_symbols (abfd);
908*a9fa9459Szrj   return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
909*a9fa9459Szrj }
910*a9fa9459Szrj 
911*a9fa9459Szrj /* Generic function to add symbols from an archive file to the global
912*a9fa9459Szrj    hash file.  This function presumes that the archive symbol table
913*a9fa9459Szrj    has already been read in (this is normally done by the
914*a9fa9459Szrj    bfd_check_format entry point).  It looks through the archive symbol
915*a9fa9459Szrj    table for symbols that are undefined or common in the linker global
916*a9fa9459Szrj    symbol hash table.  When one is found, the CHECKFN argument is used
917*a9fa9459Szrj    to see if an object file should be included.  This allows targets
918*a9fa9459Szrj    to customize common symbol behaviour.  CHECKFN should set *PNEEDED
919*a9fa9459Szrj    to TRUE if the object file should be included, and must also call
920*a9fa9459Szrj    the bfd_link_info add_archive_element callback function and handle
921*a9fa9459Szrj    adding the symbols to the global hash table.  CHECKFN must notice
922*a9fa9459Szrj    if the callback indicates a substitute BFD, and arrange to add
923*a9fa9459Szrj    those symbols instead if it does so.  CHECKFN should only return
924*a9fa9459Szrj    FALSE if some sort of error occurs.  */
925*a9fa9459Szrj 
926*a9fa9459Szrj bfd_boolean
_bfd_generic_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info,bfd_boolean (* checkfn)(bfd *,struct bfd_link_info *,struct bfd_link_hash_entry *,const char *,bfd_boolean *))927*a9fa9459Szrj _bfd_generic_link_add_archive_symbols
928*a9fa9459Szrj   (bfd *abfd,
929*a9fa9459Szrj    struct bfd_link_info *info,
930*a9fa9459Szrj    bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *,
931*a9fa9459Szrj 			   struct bfd_link_hash_entry *, const char *,
932*a9fa9459Szrj 			   bfd_boolean *))
933*a9fa9459Szrj {
934*a9fa9459Szrj   bfd_boolean loop;
935*a9fa9459Szrj   bfd_size_type amt;
936*a9fa9459Szrj   unsigned char *included;
937*a9fa9459Szrj 
938*a9fa9459Szrj   if (! bfd_has_map (abfd))
939*a9fa9459Szrj     {
940*a9fa9459Szrj       /* An empty archive is a special case.  */
941*a9fa9459Szrj       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
942*a9fa9459Szrj 	return TRUE;
943*a9fa9459Szrj       bfd_set_error (bfd_error_no_armap);
944*a9fa9459Szrj       return FALSE;
945*a9fa9459Szrj     }
946*a9fa9459Szrj 
947*a9fa9459Szrj   amt = bfd_ardata (abfd)->symdef_count;
948*a9fa9459Szrj   if (amt == 0)
949*a9fa9459Szrj     return TRUE;
950*a9fa9459Szrj   amt *= sizeof (*included);
951*a9fa9459Szrj   included = (unsigned char *) bfd_zmalloc (amt);
952*a9fa9459Szrj   if (included == NULL)
953*a9fa9459Szrj     return FALSE;
954*a9fa9459Szrj 
955*a9fa9459Szrj   do
956*a9fa9459Szrj     {
957*a9fa9459Szrj       carsym *arsyms;
958*a9fa9459Szrj       carsym *arsym_end;
959*a9fa9459Szrj       carsym *arsym;
960*a9fa9459Szrj       unsigned int indx;
961*a9fa9459Szrj       file_ptr last_ar_offset = -1;
962*a9fa9459Szrj       bfd_boolean needed = FALSE;
963*a9fa9459Szrj       bfd *element = NULL;
964*a9fa9459Szrj 
965*a9fa9459Szrj       loop = FALSE;
966*a9fa9459Szrj       arsyms = bfd_ardata (abfd)->symdefs;
967*a9fa9459Szrj       arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
968*a9fa9459Szrj       for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
969*a9fa9459Szrj 	{
970*a9fa9459Szrj 	  struct bfd_link_hash_entry *h;
971*a9fa9459Szrj 	  struct bfd_link_hash_entry *undefs_tail;
972*a9fa9459Szrj 
973*a9fa9459Szrj 	  if (included[indx])
974*a9fa9459Szrj 	    continue;
975*a9fa9459Szrj 	  if (needed && arsym->file_offset == last_ar_offset)
976*a9fa9459Szrj 	    {
977*a9fa9459Szrj 	      included[indx] = 1;
978*a9fa9459Szrj 	      continue;
979*a9fa9459Szrj 	    }
980*a9fa9459Szrj 
981*a9fa9459Szrj 	  h = bfd_link_hash_lookup (info->hash, arsym->name,
982*a9fa9459Szrj 				    FALSE, FALSE, TRUE);
983*a9fa9459Szrj 
984*a9fa9459Szrj 	  if (h == NULL
985*a9fa9459Szrj 	      && info->pei386_auto_import
986*a9fa9459Szrj 	      && CONST_STRNEQ (arsym->name, "__imp_"))
987*a9fa9459Szrj 	    h = bfd_link_hash_lookup (info->hash, arsym->name + 6,
988*a9fa9459Szrj 				      FALSE, FALSE, TRUE);
989*a9fa9459Szrj 	  if (h == NULL)
990*a9fa9459Szrj 	    continue;
991*a9fa9459Szrj 
992*a9fa9459Szrj 	  if (h->type != bfd_link_hash_undefined
993*a9fa9459Szrj 	      && h->type != bfd_link_hash_common)
994*a9fa9459Szrj 	    {
995*a9fa9459Szrj 	      if (h->type != bfd_link_hash_undefweak)
996*a9fa9459Szrj 		/* Symbol must be defined.  Don't check it again.  */
997*a9fa9459Szrj 		included[indx] = 1;
998*a9fa9459Szrj 	      continue;
999*a9fa9459Szrj 	    }
1000*a9fa9459Szrj 
1001*a9fa9459Szrj 	  if (last_ar_offset != arsym->file_offset)
1002*a9fa9459Szrj 	    {
1003*a9fa9459Szrj 	      last_ar_offset = arsym->file_offset;
1004*a9fa9459Szrj 	      element = _bfd_get_elt_at_filepos (abfd, last_ar_offset);
1005*a9fa9459Szrj 	      if (element == NULL
1006*a9fa9459Szrj 		  || !bfd_check_format (element, bfd_object))
1007*a9fa9459Szrj 		goto error_return;
1008*a9fa9459Szrj 	    }
1009*a9fa9459Szrj 
1010*a9fa9459Szrj 	  undefs_tail = info->hash->undefs_tail;
1011*a9fa9459Szrj 
1012*a9fa9459Szrj 	  /* CHECKFN will see if this element should be included, and
1013*a9fa9459Szrj 	     go ahead and include it if appropriate.  */
1014*a9fa9459Szrj 	  if (! (*checkfn) (element, info, h, arsym->name, &needed))
1015*a9fa9459Szrj 	    goto error_return;
1016*a9fa9459Szrj 
1017*a9fa9459Szrj 	  if (needed)
1018*a9fa9459Szrj 	    {
1019*a9fa9459Szrj 	      unsigned int mark;
1020*a9fa9459Szrj 
1021*a9fa9459Szrj 	      /* Look backward to mark all symbols from this object file
1022*a9fa9459Szrj 		 which we have already seen in this pass.  */
1023*a9fa9459Szrj 	      mark = indx;
1024*a9fa9459Szrj 	      do
1025*a9fa9459Szrj 		{
1026*a9fa9459Szrj 		  included[mark] = 1;
1027*a9fa9459Szrj 		  if (mark == 0)
1028*a9fa9459Szrj 		    break;
1029*a9fa9459Szrj 		  --mark;
1030*a9fa9459Szrj 		}
1031*a9fa9459Szrj 	      while (arsyms[mark].file_offset == last_ar_offset);
1032*a9fa9459Szrj 
1033*a9fa9459Szrj 	      if (undefs_tail != info->hash->undefs_tail)
1034*a9fa9459Szrj 		loop = TRUE;
1035*a9fa9459Szrj 	    }
1036*a9fa9459Szrj 	}
1037*a9fa9459Szrj     } while (loop);
1038*a9fa9459Szrj 
1039*a9fa9459Szrj   free (included);
1040*a9fa9459Szrj   return TRUE;
1041*a9fa9459Szrj 
1042*a9fa9459Szrj  error_return:
1043*a9fa9459Szrj   free (included);
1044*a9fa9459Szrj   return FALSE;
1045*a9fa9459Szrj }
1046*a9fa9459Szrj 
1047*a9fa9459Szrj /* See if we should include an archive element.  This version is used
1048*a9fa9459Szrj    when we do not want to automatically collect constructors based on
1049*a9fa9459Szrj    the symbol name, presumably because we have some other mechanism
1050*a9fa9459Szrj    for finding them.  */
1051*a9fa9459Szrj 
1052*a9fa9459Szrj static bfd_boolean
generic_link_check_archive_element_no_collect(bfd * abfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h,const char * name,bfd_boolean * pneeded)1053*a9fa9459Szrj generic_link_check_archive_element_no_collect (bfd *abfd,
1054*a9fa9459Szrj 					       struct bfd_link_info *info,
1055*a9fa9459Szrj 					       struct bfd_link_hash_entry *h,
1056*a9fa9459Szrj 					       const char *name,
1057*a9fa9459Szrj 					       bfd_boolean *pneeded)
1058*a9fa9459Szrj {
1059*a9fa9459Szrj   return generic_link_check_archive_element (abfd, info, h, name, pneeded,
1060*a9fa9459Szrj 					     FALSE);
1061*a9fa9459Szrj }
1062*a9fa9459Szrj 
1063*a9fa9459Szrj /* See if we should include an archive element.  This version is used
1064*a9fa9459Szrj    when we want to automatically collect constructors based on the
1065*a9fa9459Szrj    symbol name, as collect2 does.  */
1066*a9fa9459Szrj 
1067*a9fa9459Szrj static bfd_boolean
generic_link_check_archive_element_collect(bfd * abfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h,const char * name,bfd_boolean * pneeded)1068*a9fa9459Szrj generic_link_check_archive_element_collect (bfd *abfd,
1069*a9fa9459Szrj 					    struct bfd_link_info *info,
1070*a9fa9459Szrj 					    struct bfd_link_hash_entry *h,
1071*a9fa9459Szrj 					    const char *name,
1072*a9fa9459Szrj 					    bfd_boolean *pneeded)
1073*a9fa9459Szrj {
1074*a9fa9459Szrj   return generic_link_check_archive_element (abfd, info, h, name, pneeded,
1075*a9fa9459Szrj 					     TRUE);
1076*a9fa9459Szrj }
1077*a9fa9459Szrj 
1078*a9fa9459Szrj /* See if we should include an archive element.  Optionally collect
1079*a9fa9459Szrj    constructors.  */
1080*a9fa9459Szrj 
1081*a9fa9459Szrj static bfd_boolean
generic_link_check_archive_element(bfd * abfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h,const char * name ATTRIBUTE_UNUSED,bfd_boolean * pneeded,bfd_boolean collect)1082*a9fa9459Szrj generic_link_check_archive_element (bfd *abfd,
1083*a9fa9459Szrj 				    struct bfd_link_info *info,
1084*a9fa9459Szrj 				    struct bfd_link_hash_entry *h,
1085*a9fa9459Szrj 				    const char *name ATTRIBUTE_UNUSED,
1086*a9fa9459Szrj 				    bfd_boolean *pneeded,
1087*a9fa9459Szrj 				    bfd_boolean collect)
1088*a9fa9459Szrj {
1089*a9fa9459Szrj   asymbol **pp, **ppend;
1090*a9fa9459Szrj 
1091*a9fa9459Szrj   *pneeded = FALSE;
1092*a9fa9459Szrj 
1093*a9fa9459Szrj   if (!bfd_generic_link_read_symbols (abfd))
1094*a9fa9459Szrj     return FALSE;
1095*a9fa9459Szrj 
1096*a9fa9459Szrj   pp = _bfd_generic_link_get_symbols (abfd);
1097*a9fa9459Szrj   ppend = pp + _bfd_generic_link_get_symcount (abfd);
1098*a9fa9459Szrj   for (; pp < ppend; pp++)
1099*a9fa9459Szrj     {
1100*a9fa9459Szrj       asymbol *p;
1101*a9fa9459Szrj 
1102*a9fa9459Szrj       p = *pp;
1103*a9fa9459Szrj 
1104*a9fa9459Szrj       /* We are only interested in globally visible symbols.  */
1105*a9fa9459Szrj       if (! bfd_is_com_section (p->section)
1106*a9fa9459Szrj 	  && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1107*a9fa9459Szrj 	continue;
1108*a9fa9459Szrj 
1109*a9fa9459Szrj       /* We are only interested if we know something about this
1110*a9fa9459Szrj 	 symbol, and it is undefined or common.  An undefined weak
1111*a9fa9459Szrj 	 symbol (type bfd_link_hash_undefweak) is not considered to be
1112*a9fa9459Szrj 	 a reference when pulling files out of an archive.  See the
1113*a9fa9459Szrj 	 SVR4 ABI, p. 4-27.  */
1114*a9fa9459Szrj       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1115*a9fa9459Szrj 				FALSE, TRUE);
1116*a9fa9459Szrj       if (h == NULL
1117*a9fa9459Szrj 	  || (h->type != bfd_link_hash_undefined
1118*a9fa9459Szrj 	      && h->type != bfd_link_hash_common))
1119*a9fa9459Szrj 	continue;
1120*a9fa9459Szrj 
1121*a9fa9459Szrj       /* P is a symbol we are looking for.  */
1122*a9fa9459Szrj 
1123*a9fa9459Szrj       if (! bfd_is_com_section (p->section)
1124*a9fa9459Szrj 	  || (h->type == bfd_link_hash_undefined
1125*a9fa9459Szrj 	      && h->u.undef.abfd == NULL))
1126*a9fa9459Szrj 	{
1127*a9fa9459Szrj 	  /* P is not a common symbol, or an undefined reference was
1128*a9fa9459Szrj 	     created from outside BFD such as from a linker -u option.
1129*a9fa9459Szrj 	     This object file defines the symbol, so pull it in.  */
1130*a9fa9459Szrj 	  *pneeded = TRUE;
1131*a9fa9459Szrj 	  if (!(*info->callbacks
1132*a9fa9459Szrj 		->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1133*a9fa9459Szrj 					&abfd))
1134*a9fa9459Szrj 	    return FALSE;
1135*a9fa9459Szrj 	  /* Potentially, the add_archive_element hook may have set a
1136*a9fa9459Szrj 	     substitute BFD for us.  */
1137*a9fa9459Szrj 	  return generic_link_add_object_symbols (abfd, info, collect);
1138*a9fa9459Szrj 	}
1139*a9fa9459Szrj 
1140*a9fa9459Szrj       /* P is a common symbol.  */
1141*a9fa9459Szrj 
1142*a9fa9459Szrj       if (h->type == bfd_link_hash_undefined)
1143*a9fa9459Szrj 	{
1144*a9fa9459Szrj 	  bfd *symbfd;
1145*a9fa9459Szrj 	  bfd_vma size;
1146*a9fa9459Szrj 	  unsigned int power;
1147*a9fa9459Szrj 
1148*a9fa9459Szrj 	  /* Turn the symbol into a common symbol but do not link in
1149*a9fa9459Szrj 	     the object file.  This is how a.out works.  Object
1150*a9fa9459Szrj 	     formats that require different semantics must implement
1151*a9fa9459Szrj 	     this function differently.  This symbol is already on the
1152*a9fa9459Szrj 	     undefs list.  We add the section to a common section
1153*a9fa9459Szrj 	     attached to symbfd to ensure that it is in a BFD which
1154*a9fa9459Szrj 	     will be linked in.  */
1155*a9fa9459Szrj 	  symbfd = h->u.undef.abfd;
1156*a9fa9459Szrj 	  h->type = bfd_link_hash_common;
1157*a9fa9459Szrj 	  h->u.c.p = (struct bfd_link_hash_common_entry *)
1158*a9fa9459Szrj 	    bfd_hash_allocate (&info->hash->table,
1159*a9fa9459Szrj 			       sizeof (struct bfd_link_hash_common_entry));
1160*a9fa9459Szrj 	  if (h->u.c.p == NULL)
1161*a9fa9459Szrj 	    return FALSE;
1162*a9fa9459Szrj 
1163*a9fa9459Szrj 	  size = bfd_asymbol_value (p);
1164*a9fa9459Szrj 	  h->u.c.size = size;
1165*a9fa9459Szrj 
1166*a9fa9459Szrj 	  power = bfd_log2 (size);
1167*a9fa9459Szrj 	  if (power > 4)
1168*a9fa9459Szrj 	    power = 4;
1169*a9fa9459Szrj 	  h->u.c.p->alignment_power = power;
1170*a9fa9459Szrj 
1171*a9fa9459Szrj 	  if (p->section == bfd_com_section_ptr)
1172*a9fa9459Szrj 	    h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
1173*a9fa9459Szrj 	  else
1174*a9fa9459Szrj 	    h->u.c.p->section = bfd_make_section_old_way (symbfd,
1175*a9fa9459Szrj 							  p->section->name);
1176*a9fa9459Szrj 	  h->u.c.p->section->flags |= SEC_ALLOC;
1177*a9fa9459Szrj 	}
1178*a9fa9459Szrj       else
1179*a9fa9459Szrj 	{
1180*a9fa9459Szrj 	  /* Adjust the size of the common symbol if necessary.  This
1181*a9fa9459Szrj 	     is how a.out works.  Object formats that require
1182*a9fa9459Szrj 	     different semantics must implement this function
1183*a9fa9459Szrj 	     differently.  */
1184*a9fa9459Szrj 	  if (bfd_asymbol_value (p) > h->u.c.size)
1185*a9fa9459Szrj 	    h->u.c.size = bfd_asymbol_value (p);
1186*a9fa9459Szrj 	}
1187*a9fa9459Szrj     }
1188*a9fa9459Szrj 
1189*a9fa9459Szrj   /* This archive element is not needed.  */
1190*a9fa9459Szrj   return TRUE;
1191*a9fa9459Szrj }
1192*a9fa9459Szrj 
1193*a9fa9459Szrj /* Add the symbols from an object file to the global hash table.  ABFD
1194*a9fa9459Szrj    is the object file.  INFO is the linker information.  SYMBOL_COUNT
1195*a9fa9459Szrj    is the number of symbols.  SYMBOLS is the list of symbols.  COLLECT
1196*a9fa9459Szrj    is TRUE if constructors should be automatically collected by name
1197*a9fa9459Szrj    as is done by collect2.  */
1198*a9fa9459Szrj 
1199*a9fa9459Szrj static bfd_boolean
generic_link_add_symbol_list(bfd * abfd,struct bfd_link_info * info,bfd_size_type symbol_count,asymbol ** symbols,bfd_boolean collect)1200*a9fa9459Szrj generic_link_add_symbol_list (bfd *abfd,
1201*a9fa9459Szrj 			      struct bfd_link_info *info,
1202*a9fa9459Szrj 			      bfd_size_type symbol_count,
1203*a9fa9459Szrj 			      asymbol **symbols,
1204*a9fa9459Szrj 			      bfd_boolean collect)
1205*a9fa9459Szrj {
1206*a9fa9459Szrj   asymbol **pp, **ppend;
1207*a9fa9459Szrj 
1208*a9fa9459Szrj   pp = symbols;
1209*a9fa9459Szrj   ppend = symbols + symbol_count;
1210*a9fa9459Szrj   for (; pp < ppend; pp++)
1211*a9fa9459Szrj     {
1212*a9fa9459Szrj       asymbol *p;
1213*a9fa9459Szrj 
1214*a9fa9459Szrj       p = *pp;
1215*a9fa9459Szrj 
1216*a9fa9459Szrj       if ((p->flags & (BSF_INDIRECT
1217*a9fa9459Szrj 		       | BSF_WARNING
1218*a9fa9459Szrj 		       | BSF_GLOBAL
1219*a9fa9459Szrj 		       | BSF_CONSTRUCTOR
1220*a9fa9459Szrj 		       | BSF_WEAK)) != 0
1221*a9fa9459Szrj 	  || bfd_is_und_section (bfd_get_section (p))
1222*a9fa9459Szrj 	  || bfd_is_com_section (bfd_get_section (p))
1223*a9fa9459Szrj 	  || bfd_is_ind_section (bfd_get_section (p)))
1224*a9fa9459Szrj 	{
1225*a9fa9459Szrj 	  const char *name;
1226*a9fa9459Szrj 	  const char *string;
1227*a9fa9459Szrj 	  struct generic_link_hash_entry *h;
1228*a9fa9459Szrj 	  struct bfd_link_hash_entry *bh;
1229*a9fa9459Szrj 
1230*a9fa9459Szrj 	  string = name = bfd_asymbol_name (p);
1231*a9fa9459Szrj 	  if (((p->flags & BSF_INDIRECT) != 0
1232*a9fa9459Szrj 	       || bfd_is_ind_section (p->section))
1233*a9fa9459Szrj 	      && pp + 1 < ppend)
1234*a9fa9459Szrj 	    {
1235*a9fa9459Szrj 	      pp++;
1236*a9fa9459Szrj 	      string = bfd_asymbol_name (*pp);
1237*a9fa9459Szrj 	    }
1238*a9fa9459Szrj 	  else if ((p->flags & BSF_WARNING) != 0
1239*a9fa9459Szrj 		   && pp + 1 < ppend)
1240*a9fa9459Szrj 	    {
1241*a9fa9459Szrj 	      /* The name of P is actually the warning string, and the
1242*a9fa9459Szrj 		 next symbol is the one to warn about.  */
1243*a9fa9459Szrj 	      pp++;
1244*a9fa9459Szrj 	      name = bfd_asymbol_name (*pp);
1245*a9fa9459Szrj 	    }
1246*a9fa9459Szrj 
1247*a9fa9459Szrj 	  bh = NULL;
1248*a9fa9459Szrj 	  if (! (_bfd_generic_link_add_one_symbol
1249*a9fa9459Szrj 		 (info, abfd, name, p->flags, bfd_get_section (p),
1250*a9fa9459Szrj 		  p->value, string, FALSE, collect, &bh)))
1251*a9fa9459Szrj 	    return FALSE;
1252*a9fa9459Szrj 	  h = (struct generic_link_hash_entry *) bh;
1253*a9fa9459Szrj 
1254*a9fa9459Szrj 	  /* If this is a constructor symbol, and the linker didn't do
1255*a9fa9459Szrj              anything with it, then we want to just pass the symbol
1256*a9fa9459Szrj              through to the output file.  This will happen when
1257*a9fa9459Szrj              linking with -r.  */
1258*a9fa9459Szrj 	  if ((p->flags & BSF_CONSTRUCTOR) != 0
1259*a9fa9459Szrj 	      && (h == NULL || h->root.type == bfd_link_hash_new))
1260*a9fa9459Szrj 	    {
1261*a9fa9459Szrj 	      p->udata.p = NULL;
1262*a9fa9459Szrj 	      continue;
1263*a9fa9459Szrj 	    }
1264*a9fa9459Szrj 
1265*a9fa9459Szrj 	  /* Save the BFD symbol so that we don't lose any backend
1266*a9fa9459Szrj 	     specific information that may be attached to it.  We only
1267*a9fa9459Szrj 	     want this one if it gives more information than the
1268*a9fa9459Szrj 	     existing one; we don't want to replace a defined symbol
1269*a9fa9459Szrj 	     with an undefined one.  This routine may be called with a
1270*a9fa9459Szrj 	     hash table other than the generic hash table, so we only
1271*a9fa9459Szrj 	     do this if we are certain that the hash table is a
1272*a9fa9459Szrj 	     generic one.  */
1273*a9fa9459Szrj 	  if (info->output_bfd->xvec == abfd->xvec)
1274*a9fa9459Szrj 	    {
1275*a9fa9459Szrj 	      if (h->sym == NULL
1276*a9fa9459Szrj 		  || (! bfd_is_und_section (bfd_get_section (p))
1277*a9fa9459Szrj 		      && (! bfd_is_com_section (bfd_get_section (p))
1278*a9fa9459Szrj 			  || bfd_is_und_section (bfd_get_section (h->sym)))))
1279*a9fa9459Szrj 		{
1280*a9fa9459Szrj 		  h->sym = p;
1281*a9fa9459Szrj 		  /* BSF_OLD_COMMON is a hack to support COFF reloc
1282*a9fa9459Szrj 		     reading, and it should go away when the COFF
1283*a9fa9459Szrj 		     linker is switched to the new version.  */
1284*a9fa9459Szrj 		  if (bfd_is_com_section (bfd_get_section (p)))
1285*a9fa9459Szrj 		    p->flags |= BSF_OLD_COMMON;
1286*a9fa9459Szrj 		}
1287*a9fa9459Szrj 	    }
1288*a9fa9459Szrj 
1289*a9fa9459Szrj 	  /* Store a back pointer from the symbol to the hash
1290*a9fa9459Szrj 	     table entry for the benefit of relaxation code until
1291*a9fa9459Szrj 	     it gets rewritten to not use asymbol structures.
1292*a9fa9459Szrj 	     Setting this is also used to check whether these
1293*a9fa9459Szrj 	     symbols were set up by the generic linker.  */
1294*a9fa9459Szrj 	  p->udata.p = h;
1295*a9fa9459Szrj 	}
1296*a9fa9459Szrj     }
1297*a9fa9459Szrj 
1298*a9fa9459Szrj   return TRUE;
1299*a9fa9459Szrj }
1300*a9fa9459Szrj 
1301*a9fa9459Szrj /* We use a state table to deal with adding symbols from an object
1302*a9fa9459Szrj    file.  The first index into the state table describes the symbol
1303*a9fa9459Szrj    from the object file.  The second index into the state table is the
1304*a9fa9459Szrj    type of the symbol in the hash table.  */
1305*a9fa9459Szrj 
1306*a9fa9459Szrj /* The symbol from the object file is turned into one of these row
1307*a9fa9459Szrj    values.  */
1308*a9fa9459Szrj 
1309*a9fa9459Szrj enum link_row
1310*a9fa9459Szrj {
1311*a9fa9459Szrj   UNDEF_ROW,		/* Undefined.  */
1312*a9fa9459Szrj   UNDEFW_ROW,		/* Weak undefined.  */
1313*a9fa9459Szrj   DEF_ROW,		/* Defined.  */
1314*a9fa9459Szrj   DEFW_ROW,		/* Weak defined.  */
1315*a9fa9459Szrj   COMMON_ROW,		/* Common.  */
1316*a9fa9459Szrj   INDR_ROW,		/* Indirect.  */
1317*a9fa9459Szrj   WARN_ROW,		/* Warning.  */
1318*a9fa9459Szrj   SET_ROW		/* Member of set.  */
1319*a9fa9459Szrj };
1320*a9fa9459Szrj 
1321*a9fa9459Szrj /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1322*a9fa9459Szrj #undef FAIL
1323*a9fa9459Szrj 
1324*a9fa9459Szrj /* The actions to take in the state table.  */
1325*a9fa9459Szrj 
1326*a9fa9459Szrj enum link_action
1327*a9fa9459Szrj {
1328*a9fa9459Szrj   FAIL,		/* Abort.  */
1329*a9fa9459Szrj   UND,		/* Mark symbol undefined.  */
1330*a9fa9459Szrj   WEAK,		/* Mark symbol weak undefined.  */
1331*a9fa9459Szrj   DEF,		/* Mark symbol defined.  */
1332*a9fa9459Szrj   DEFW,		/* Mark symbol weak defined.  */
1333*a9fa9459Szrj   COM,		/* Mark symbol common.  */
1334*a9fa9459Szrj   REF,		/* Mark defined symbol referenced.  */
1335*a9fa9459Szrj   CREF,		/* Possibly warn about common reference to defined symbol.  */
1336*a9fa9459Szrj   CDEF,		/* Define existing common symbol.  */
1337*a9fa9459Szrj   NOACT,	/* No action.  */
1338*a9fa9459Szrj   BIG,		/* Mark symbol common using largest size.  */
1339*a9fa9459Szrj   MDEF,		/* Multiple definition error.  */
1340*a9fa9459Szrj   MIND,		/* Multiple indirect symbols.  */
1341*a9fa9459Szrj   IND,		/* Make indirect symbol.  */
1342*a9fa9459Szrj   CIND,		/* Make indirect symbol from existing common symbol.  */
1343*a9fa9459Szrj   SET,		/* Add value to set.  */
1344*a9fa9459Szrj   MWARN,	/* Make warning symbol.  */
1345*a9fa9459Szrj   WARN,		/* Warn if referenced, else MWARN.  */
1346*a9fa9459Szrj   CYCLE,	/* Repeat with symbol pointed to.  */
1347*a9fa9459Szrj   REFC,		/* Mark indirect symbol referenced and then CYCLE.  */
1348*a9fa9459Szrj   WARNC		/* Issue warning and then CYCLE.  */
1349*a9fa9459Szrj };
1350*a9fa9459Szrj 
1351*a9fa9459Szrj /* The state table itself.  The first index is a link_row and the
1352*a9fa9459Szrj    second index is a bfd_link_hash_type.  */
1353*a9fa9459Szrj 
1354*a9fa9459Szrj static const enum link_action link_action[8][8] =
1355*a9fa9459Szrj {
1356*a9fa9459Szrj   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
1357*a9fa9459Szrj   /* UNDEF_ROW 	*/  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
1358*a9fa9459Szrj   /* UNDEFW_ROW	*/  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
1359*a9fa9459Szrj   /* DEF_ROW 	*/  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MDEF,  CYCLE },
1360*a9fa9459Szrj   /* DEFW_ROW 	*/  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
1361*a9fa9459Szrj   /* COMMON_ROW	*/  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
1362*a9fa9459Szrj   /* INDR_ROW	*/  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
1363*a9fa9459Szrj   /* WARN_ROW   */  {MWARN, WARN,  WARN,  WARN,  WARN,  WARN,  WARN,  NOACT },
1364*a9fa9459Szrj   /* SET_ROW	*/  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
1365*a9fa9459Szrj };
1366*a9fa9459Szrj 
1367*a9fa9459Szrj /* Most of the entries in the LINK_ACTION table are straightforward,
1368*a9fa9459Szrj    but a few are somewhat subtle.
1369*a9fa9459Szrj 
1370*a9fa9459Szrj    A reference to an indirect symbol (UNDEF_ROW/indr or
1371*a9fa9459Szrj    UNDEFW_ROW/indr) is counted as a reference both to the indirect
1372*a9fa9459Szrj    symbol and to the symbol the indirect symbol points to.
1373*a9fa9459Szrj 
1374*a9fa9459Szrj    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1375*a9fa9459Szrj    causes the warning to be issued.
1376*a9fa9459Szrj 
1377*a9fa9459Szrj    A common definition of an indirect symbol (COMMON_ROW/indr) is
1378*a9fa9459Szrj    treated as a multiple definition error.  Likewise for an indirect
1379*a9fa9459Szrj    definition of a common symbol (INDR_ROW/com).
1380*a9fa9459Szrj 
1381*a9fa9459Szrj    An indirect definition of a warning (INDR_ROW/warn) does not cause
1382*a9fa9459Szrj    the warning to be issued.
1383*a9fa9459Szrj 
1384*a9fa9459Szrj    If a warning is created for an indirect symbol (WARN_ROW/indr) no
1385*a9fa9459Szrj    warning is created for the symbol the indirect symbol points to.
1386*a9fa9459Szrj 
1387*a9fa9459Szrj    Adding an entry to a set does not count as a reference to a set,
1388*a9fa9459Szrj    and no warning is issued (SET_ROW/warn).  */
1389*a9fa9459Szrj 
1390*a9fa9459Szrj /* Return the BFD in which a hash entry has been defined, if known.  */
1391*a9fa9459Szrj 
1392*a9fa9459Szrj static bfd *
hash_entry_bfd(struct bfd_link_hash_entry * h)1393*a9fa9459Szrj hash_entry_bfd (struct bfd_link_hash_entry *h)
1394*a9fa9459Szrj {
1395*a9fa9459Szrj   while (h->type == bfd_link_hash_warning)
1396*a9fa9459Szrj     h = h->u.i.link;
1397*a9fa9459Szrj   switch (h->type)
1398*a9fa9459Szrj     {
1399*a9fa9459Szrj     default:
1400*a9fa9459Szrj       return NULL;
1401*a9fa9459Szrj     case bfd_link_hash_undefined:
1402*a9fa9459Szrj     case bfd_link_hash_undefweak:
1403*a9fa9459Szrj       return h->u.undef.abfd;
1404*a9fa9459Szrj     case bfd_link_hash_defined:
1405*a9fa9459Szrj     case bfd_link_hash_defweak:
1406*a9fa9459Szrj       return h->u.def.section->owner;
1407*a9fa9459Szrj     case bfd_link_hash_common:
1408*a9fa9459Szrj       return h->u.c.p->section->owner;
1409*a9fa9459Szrj     }
1410*a9fa9459Szrj   /*NOTREACHED*/
1411*a9fa9459Szrj }
1412*a9fa9459Szrj 
1413*a9fa9459Szrj /* Add a symbol to the global hash table.
1414*a9fa9459Szrj    ABFD is the BFD the symbol comes from.
1415*a9fa9459Szrj    NAME is the name of the symbol.
1416*a9fa9459Szrj    FLAGS is the BSF_* bits associated with the symbol.
1417*a9fa9459Szrj    SECTION is the section in which the symbol is defined; this may be
1418*a9fa9459Szrj      bfd_und_section_ptr or bfd_com_section_ptr.
1419*a9fa9459Szrj    VALUE is the value of the symbol, relative to the section.
1420*a9fa9459Szrj    STRING is used for either an indirect symbol, in which case it is
1421*a9fa9459Szrj      the name of the symbol to indirect to, or a warning symbol, in
1422*a9fa9459Szrj      which case it is the warning string.
1423*a9fa9459Szrj    COPY is TRUE if NAME or STRING must be copied into locally
1424*a9fa9459Szrj      allocated memory if they need to be saved.
1425*a9fa9459Szrj    COLLECT is TRUE if we should automatically collect gcc constructor
1426*a9fa9459Szrj      or destructor names as collect2 does.
1427*a9fa9459Szrj    HASHP, if not NULL, is a place to store the created hash table
1428*a9fa9459Szrj      entry; if *HASHP is not NULL, the caller has already looked up
1429*a9fa9459Szrj      the hash table entry, and stored it in *HASHP.  */
1430*a9fa9459Szrj 
1431*a9fa9459Szrj bfd_boolean
_bfd_generic_link_add_one_symbol(struct bfd_link_info * info,bfd * abfd,const char * name,flagword flags,asection * section,bfd_vma value,const char * string,bfd_boolean copy,bfd_boolean collect,struct bfd_link_hash_entry ** hashp)1432*a9fa9459Szrj _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1433*a9fa9459Szrj 				  bfd *abfd,
1434*a9fa9459Szrj 				  const char *name,
1435*a9fa9459Szrj 				  flagword flags,
1436*a9fa9459Szrj 				  asection *section,
1437*a9fa9459Szrj 				  bfd_vma value,
1438*a9fa9459Szrj 				  const char *string,
1439*a9fa9459Szrj 				  bfd_boolean copy,
1440*a9fa9459Szrj 				  bfd_boolean collect,
1441*a9fa9459Szrj 				  struct bfd_link_hash_entry **hashp)
1442*a9fa9459Szrj {
1443*a9fa9459Szrj   enum link_row row;
1444*a9fa9459Szrj   struct bfd_link_hash_entry *h;
1445*a9fa9459Szrj   struct bfd_link_hash_entry *inh = NULL;
1446*a9fa9459Szrj   bfd_boolean cycle;
1447*a9fa9459Szrj 
1448*a9fa9459Szrj   BFD_ASSERT (section != NULL);
1449*a9fa9459Szrj 
1450*a9fa9459Szrj   if (bfd_is_ind_section (section)
1451*a9fa9459Szrj       || (flags & BSF_INDIRECT) != 0)
1452*a9fa9459Szrj     {
1453*a9fa9459Szrj       row = INDR_ROW;
1454*a9fa9459Szrj       /* Create the indirect symbol here.  This is for the benefit of
1455*a9fa9459Szrj 	 the plugin "notice" function.
1456*a9fa9459Szrj 	 STRING is the name of the symbol we want to indirect to.  */
1457*a9fa9459Szrj       inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1458*a9fa9459Szrj 					  copy, FALSE);
1459*a9fa9459Szrj       if (inh == NULL)
1460*a9fa9459Szrj 	return FALSE;
1461*a9fa9459Szrj     }
1462*a9fa9459Szrj   else if ((flags & BSF_WARNING) != 0)
1463*a9fa9459Szrj     row = WARN_ROW;
1464*a9fa9459Szrj   else if ((flags & BSF_CONSTRUCTOR) != 0)
1465*a9fa9459Szrj     row = SET_ROW;
1466*a9fa9459Szrj   else if (bfd_is_und_section (section))
1467*a9fa9459Szrj     {
1468*a9fa9459Szrj       if ((flags & BSF_WEAK) != 0)
1469*a9fa9459Szrj 	row = UNDEFW_ROW;
1470*a9fa9459Szrj       else
1471*a9fa9459Szrj 	row = UNDEF_ROW;
1472*a9fa9459Szrj     }
1473*a9fa9459Szrj   else if ((flags & BSF_WEAK) != 0)
1474*a9fa9459Szrj     row = DEFW_ROW;
1475*a9fa9459Szrj   else if (bfd_is_com_section (section))
1476*a9fa9459Szrj     {
1477*a9fa9459Szrj       row = COMMON_ROW;
1478*a9fa9459Szrj       if (!bfd_link_relocatable (info)
1479*a9fa9459Szrj 	  && strcmp (name, "__gnu_lto_slim") == 0)
1480*a9fa9459Szrj 	(*_bfd_error_handler)
1481*a9fa9459Szrj 	  (_("%s: plugin needed to handle lto object"),
1482*a9fa9459Szrj 	   bfd_get_filename (abfd));
1483*a9fa9459Szrj     }
1484*a9fa9459Szrj   else
1485*a9fa9459Szrj     row = DEF_ROW;
1486*a9fa9459Szrj 
1487*a9fa9459Szrj   if (hashp != NULL && *hashp != NULL)
1488*a9fa9459Szrj     h = *hashp;
1489*a9fa9459Szrj   else
1490*a9fa9459Szrj     {
1491*a9fa9459Szrj       if (row == UNDEF_ROW || row == UNDEFW_ROW)
1492*a9fa9459Szrj 	h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1493*a9fa9459Szrj       else
1494*a9fa9459Szrj 	h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
1495*a9fa9459Szrj       if (h == NULL)
1496*a9fa9459Szrj 	{
1497*a9fa9459Szrj 	  if (hashp != NULL)
1498*a9fa9459Szrj 	    *hashp = NULL;
1499*a9fa9459Szrj 	  return FALSE;
1500*a9fa9459Szrj 	}
1501*a9fa9459Szrj     }
1502*a9fa9459Szrj 
1503*a9fa9459Szrj   if (info->notice_all
1504*a9fa9459Szrj       || (info->notice_hash != NULL
1505*a9fa9459Szrj 	  && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1506*a9fa9459Szrj     {
1507*a9fa9459Szrj       if (! (*info->callbacks->notice) (info, h, inh,
1508*a9fa9459Szrj 					abfd, section, value, flags))
1509*a9fa9459Szrj 	return FALSE;
1510*a9fa9459Szrj     }
1511*a9fa9459Szrj 
1512*a9fa9459Szrj   if (hashp != NULL)
1513*a9fa9459Szrj     *hashp = h;
1514*a9fa9459Szrj 
1515*a9fa9459Szrj   do
1516*a9fa9459Szrj     {
1517*a9fa9459Szrj       enum link_action action;
1518*a9fa9459Szrj 
1519*a9fa9459Szrj       cycle = FALSE;
1520*a9fa9459Szrj       action = link_action[(int) row][(int) h->type];
1521*a9fa9459Szrj       switch (action)
1522*a9fa9459Szrj 	{
1523*a9fa9459Szrj 	case FAIL:
1524*a9fa9459Szrj 	  abort ();
1525*a9fa9459Szrj 
1526*a9fa9459Szrj 	case NOACT:
1527*a9fa9459Szrj 	  /* Do nothing.  */
1528*a9fa9459Szrj 	  break;
1529*a9fa9459Szrj 
1530*a9fa9459Szrj 	case UND:
1531*a9fa9459Szrj 	  /* Make a new undefined symbol.  */
1532*a9fa9459Szrj 	  h->type = bfd_link_hash_undefined;
1533*a9fa9459Szrj 	  h->u.undef.abfd = abfd;
1534*a9fa9459Szrj 	  bfd_link_add_undef (info->hash, h);
1535*a9fa9459Szrj 	  break;
1536*a9fa9459Szrj 
1537*a9fa9459Szrj 	case WEAK:
1538*a9fa9459Szrj 	  /* Make a new weak undefined symbol.  */
1539*a9fa9459Szrj 	  h->type = bfd_link_hash_undefweak;
1540*a9fa9459Szrj 	  h->u.undef.abfd = abfd;
1541*a9fa9459Szrj 	  break;
1542*a9fa9459Szrj 
1543*a9fa9459Szrj 	case CDEF:
1544*a9fa9459Szrj 	  /* We have found a definition for a symbol which was
1545*a9fa9459Szrj 	     previously common.  */
1546*a9fa9459Szrj 	  BFD_ASSERT (h->type == bfd_link_hash_common);
1547*a9fa9459Szrj 	  (*info->callbacks->multiple_common) (info, h, abfd,
1548*a9fa9459Szrj 					       bfd_link_hash_defined, 0);
1549*a9fa9459Szrj 	  /* Fall through.  */
1550*a9fa9459Szrj 	case DEF:
1551*a9fa9459Szrj 	case DEFW:
1552*a9fa9459Szrj 	  {
1553*a9fa9459Szrj 	    enum bfd_link_hash_type oldtype;
1554*a9fa9459Szrj 
1555*a9fa9459Szrj 	    /* Define a symbol.  */
1556*a9fa9459Szrj 	    oldtype = h->type;
1557*a9fa9459Szrj 	    if (action == DEFW)
1558*a9fa9459Szrj 	      h->type = bfd_link_hash_defweak;
1559*a9fa9459Szrj 	    else
1560*a9fa9459Szrj 	      h->type = bfd_link_hash_defined;
1561*a9fa9459Szrj 	    h->u.def.section = section;
1562*a9fa9459Szrj 	    h->u.def.value = value;
1563*a9fa9459Szrj 	    h->linker_def = 0;
1564*a9fa9459Szrj 
1565*a9fa9459Szrj 	    /* If we have been asked to, we act like collect2 and
1566*a9fa9459Szrj 	       identify all functions that might be global
1567*a9fa9459Szrj 	       constructors and destructors and pass them up in a
1568*a9fa9459Szrj 	       callback.  We only do this for certain object file
1569*a9fa9459Szrj 	       types, since many object file types can handle this
1570*a9fa9459Szrj 	       automatically.  */
1571*a9fa9459Szrj 	    if (collect && name[0] == '_')
1572*a9fa9459Szrj 	      {
1573*a9fa9459Szrj 		const char *s;
1574*a9fa9459Szrj 
1575*a9fa9459Szrj 		/* A constructor or destructor name starts like this:
1576*a9fa9459Szrj 		   _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1577*a9fa9459Szrj 		   the second are the same character (we accept any
1578*a9fa9459Szrj 		   character there, in case a new object file format
1579*a9fa9459Szrj 		   comes along with even worse naming restrictions).  */
1580*a9fa9459Szrj 
1581*a9fa9459Szrj #define CONS_PREFIX "GLOBAL_"
1582*a9fa9459Szrj #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1583*a9fa9459Szrj 
1584*a9fa9459Szrj 		s = name + 1;
1585*a9fa9459Szrj 		while (*s == '_')
1586*a9fa9459Szrj 		  ++s;
1587*a9fa9459Szrj 		if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
1588*a9fa9459Szrj 		  {
1589*a9fa9459Szrj 		    char c;
1590*a9fa9459Szrj 
1591*a9fa9459Szrj 		    c = s[CONS_PREFIX_LEN + 1];
1592*a9fa9459Szrj 		    if ((c == 'I' || c == 'D')
1593*a9fa9459Szrj 			&& s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1594*a9fa9459Szrj 		      {
1595*a9fa9459Szrj 			/* If this is a definition of a symbol which
1596*a9fa9459Szrj                            was previously weakly defined, we are in
1597*a9fa9459Szrj                            trouble.  We have already added a
1598*a9fa9459Szrj                            constructor entry for the weak defined
1599*a9fa9459Szrj                            symbol, and now we are trying to add one
1600*a9fa9459Szrj                            for the new symbol.  Fortunately, this case
1601*a9fa9459Szrj                            should never arise in practice.  */
1602*a9fa9459Szrj 			if (oldtype == bfd_link_hash_defweak)
1603*a9fa9459Szrj 			  abort ();
1604*a9fa9459Szrj 
1605*a9fa9459Szrj 			(*info->callbacks->constructor) (info, c == 'I',
1606*a9fa9459Szrj 							 h->root.string, abfd,
1607*a9fa9459Szrj 							 section, value);
1608*a9fa9459Szrj 		      }
1609*a9fa9459Szrj 		  }
1610*a9fa9459Szrj 	      }
1611*a9fa9459Szrj 	  }
1612*a9fa9459Szrj 
1613*a9fa9459Szrj 	  break;
1614*a9fa9459Szrj 
1615*a9fa9459Szrj 	case COM:
1616*a9fa9459Szrj 	  /* We have found a common definition for a symbol.  */
1617*a9fa9459Szrj 	  if (h->type == bfd_link_hash_new)
1618*a9fa9459Szrj 	    bfd_link_add_undef (info->hash, h);
1619*a9fa9459Szrj 	  h->type = bfd_link_hash_common;
1620*a9fa9459Szrj 	  h->u.c.p = (struct bfd_link_hash_common_entry *)
1621*a9fa9459Szrj 	    bfd_hash_allocate (&info->hash->table,
1622*a9fa9459Szrj 			       sizeof (struct bfd_link_hash_common_entry));
1623*a9fa9459Szrj 	  if (h->u.c.p == NULL)
1624*a9fa9459Szrj 	    return FALSE;
1625*a9fa9459Szrj 
1626*a9fa9459Szrj 	  h->u.c.size = value;
1627*a9fa9459Szrj 
1628*a9fa9459Szrj 	  /* Select a default alignment based on the size.  This may
1629*a9fa9459Szrj              be overridden by the caller.  */
1630*a9fa9459Szrj 	  {
1631*a9fa9459Szrj 	    unsigned int power;
1632*a9fa9459Szrj 
1633*a9fa9459Szrj 	    power = bfd_log2 (value);
1634*a9fa9459Szrj 	    if (power > 4)
1635*a9fa9459Szrj 	      power = 4;
1636*a9fa9459Szrj 	    h->u.c.p->alignment_power = power;
1637*a9fa9459Szrj 	  }
1638*a9fa9459Szrj 
1639*a9fa9459Szrj 	  /* The section of a common symbol is only used if the common
1640*a9fa9459Szrj              symbol is actually allocated.  It basically provides a
1641*a9fa9459Szrj              hook for the linker script to decide which output section
1642*a9fa9459Szrj              the common symbols should be put in.  In most cases, the
1643*a9fa9459Szrj              section of a common symbol will be bfd_com_section_ptr,
1644*a9fa9459Szrj              the code here will choose a common symbol section named
1645*a9fa9459Szrj              "COMMON", and the linker script will contain *(COMMON) in
1646*a9fa9459Szrj              the appropriate place.  A few targets use separate common
1647*a9fa9459Szrj              sections for small symbols, and they require special
1648*a9fa9459Szrj              handling.  */
1649*a9fa9459Szrj 	  if (section == bfd_com_section_ptr)
1650*a9fa9459Szrj 	    {
1651*a9fa9459Szrj 	      h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1652*a9fa9459Szrj 	      h->u.c.p->section->flags |= SEC_ALLOC;
1653*a9fa9459Szrj 	    }
1654*a9fa9459Szrj 	  else if (section->owner != abfd)
1655*a9fa9459Szrj 	    {
1656*a9fa9459Szrj 	      h->u.c.p->section = bfd_make_section_old_way (abfd,
1657*a9fa9459Szrj 							    section->name);
1658*a9fa9459Szrj 	      h->u.c.p->section->flags |= SEC_ALLOC;
1659*a9fa9459Szrj 	    }
1660*a9fa9459Szrj 	  else
1661*a9fa9459Szrj 	    h->u.c.p->section = section;
1662*a9fa9459Szrj 	  h->linker_def = 0;
1663*a9fa9459Szrj 	  break;
1664*a9fa9459Szrj 
1665*a9fa9459Szrj 	case REF:
1666*a9fa9459Szrj 	  /* A reference to a defined symbol.  */
1667*a9fa9459Szrj 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1668*a9fa9459Szrj 	    h->u.undef.next = h;
1669*a9fa9459Szrj 	  break;
1670*a9fa9459Szrj 
1671*a9fa9459Szrj 	case BIG:
1672*a9fa9459Szrj 	  /* We have found a common definition for a symbol which
1673*a9fa9459Szrj 	     already had a common definition.  Use the maximum of the
1674*a9fa9459Szrj 	     two sizes, and use the section required by the larger symbol.  */
1675*a9fa9459Szrj 	  BFD_ASSERT (h->type == bfd_link_hash_common);
1676*a9fa9459Szrj 	  (*info->callbacks->multiple_common) (info, h, abfd,
1677*a9fa9459Szrj 					       bfd_link_hash_common, value);
1678*a9fa9459Szrj 	  if (value > h->u.c.size)
1679*a9fa9459Szrj 	    {
1680*a9fa9459Szrj 	      unsigned int power;
1681*a9fa9459Szrj 
1682*a9fa9459Szrj 	      h->u.c.size = value;
1683*a9fa9459Szrj 
1684*a9fa9459Szrj 	      /* Select a default alignment based on the size.  This may
1685*a9fa9459Szrj 		 be overridden by the caller.  */
1686*a9fa9459Szrj 	      power = bfd_log2 (value);
1687*a9fa9459Szrj 	      if (power > 4)
1688*a9fa9459Szrj 		power = 4;
1689*a9fa9459Szrj 	      h->u.c.p->alignment_power = power;
1690*a9fa9459Szrj 
1691*a9fa9459Szrj 	      /* Some systems have special treatment for small commons,
1692*a9fa9459Szrj 		 hence we want to select the section used by the larger
1693*a9fa9459Szrj 		 symbol.  This makes sure the symbol does not go in a
1694*a9fa9459Szrj 		 small common section if it is now too large.  */
1695*a9fa9459Szrj 	      if (section == bfd_com_section_ptr)
1696*a9fa9459Szrj 		{
1697*a9fa9459Szrj 		  h->u.c.p->section
1698*a9fa9459Szrj 		    = bfd_make_section_old_way (abfd, "COMMON");
1699*a9fa9459Szrj 		  h->u.c.p->section->flags |= SEC_ALLOC;
1700*a9fa9459Szrj 		}
1701*a9fa9459Szrj 	      else if (section->owner != abfd)
1702*a9fa9459Szrj 		{
1703*a9fa9459Szrj 		  h->u.c.p->section
1704*a9fa9459Szrj 		    = bfd_make_section_old_way (abfd, section->name);
1705*a9fa9459Szrj 		  h->u.c.p->section->flags |= SEC_ALLOC;
1706*a9fa9459Szrj 		}
1707*a9fa9459Szrj 	      else
1708*a9fa9459Szrj 		h->u.c.p->section = section;
1709*a9fa9459Szrj 	    }
1710*a9fa9459Szrj 	  break;
1711*a9fa9459Szrj 
1712*a9fa9459Szrj 	case CREF:
1713*a9fa9459Szrj 	  /* We have found a common definition for a symbol which
1714*a9fa9459Szrj 	     was already defined.  */
1715*a9fa9459Szrj 	  (*info->callbacks->multiple_common) (info, h, abfd,
1716*a9fa9459Szrj 					       bfd_link_hash_common, value);
1717*a9fa9459Szrj 	  break;
1718*a9fa9459Szrj 
1719*a9fa9459Szrj 	case MIND:
1720*a9fa9459Szrj 	  /* Multiple indirect symbols.  This is OK if they both point
1721*a9fa9459Szrj 	     to the same symbol.  */
1722*a9fa9459Szrj 	  if (strcmp (h->u.i.link->root.string, string) == 0)
1723*a9fa9459Szrj 	    break;
1724*a9fa9459Szrj 	  /* Fall through.  */
1725*a9fa9459Szrj 	case MDEF:
1726*a9fa9459Szrj 	  /* Handle a multiple definition.  */
1727*a9fa9459Szrj 	  (*info->callbacks->multiple_definition) (info, h,
1728*a9fa9459Szrj 						   abfd, section, value);
1729*a9fa9459Szrj 	  break;
1730*a9fa9459Szrj 
1731*a9fa9459Szrj 	case CIND:
1732*a9fa9459Szrj 	  /* Create an indirect symbol from an existing common symbol.  */
1733*a9fa9459Szrj 	  BFD_ASSERT (h->type == bfd_link_hash_common);
1734*a9fa9459Szrj 	  (*info->callbacks->multiple_common) (info, h, abfd,
1735*a9fa9459Szrj 					       bfd_link_hash_indirect, 0);
1736*a9fa9459Szrj 	  /* Fall through.  */
1737*a9fa9459Szrj 	case IND:
1738*a9fa9459Szrj 	  if (inh->type == bfd_link_hash_indirect
1739*a9fa9459Szrj 	      && inh->u.i.link == h)
1740*a9fa9459Szrj 	    {
1741*a9fa9459Szrj 	      (*_bfd_error_handler)
1742*a9fa9459Szrj 		(_("%B: indirect symbol `%s' to `%s' is a loop"),
1743*a9fa9459Szrj 		 abfd, name, string);
1744*a9fa9459Szrj 	      bfd_set_error (bfd_error_invalid_operation);
1745*a9fa9459Szrj 	      return FALSE;
1746*a9fa9459Szrj 	    }
1747*a9fa9459Szrj 	  if (inh->type == bfd_link_hash_new)
1748*a9fa9459Szrj 	    {
1749*a9fa9459Szrj 	      inh->type = bfd_link_hash_undefined;
1750*a9fa9459Szrj 	      inh->u.undef.abfd = abfd;
1751*a9fa9459Szrj 	      bfd_link_add_undef (info->hash, inh);
1752*a9fa9459Szrj 	    }
1753*a9fa9459Szrj 
1754*a9fa9459Szrj 	  /* If the indirect symbol has been referenced, we need to
1755*a9fa9459Szrj 	     push the reference down to the symbol we are referencing.  */
1756*a9fa9459Szrj 	  if (h->type != bfd_link_hash_new)
1757*a9fa9459Szrj 	    {
1758*a9fa9459Szrj 	      /* ??? If inh->type == bfd_link_hash_undefweak this
1759*a9fa9459Szrj 		 converts inh to bfd_link_hash_undefined.  */
1760*a9fa9459Szrj 	      row = UNDEF_ROW;
1761*a9fa9459Szrj 	      cycle = TRUE;
1762*a9fa9459Szrj 	    }
1763*a9fa9459Szrj 
1764*a9fa9459Szrj 	  h->type = bfd_link_hash_indirect;
1765*a9fa9459Szrj 	  h->u.i.link = inh;
1766*a9fa9459Szrj 	  /* Not setting h = h->u.i.link here means that when cycle is
1767*a9fa9459Szrj 	     set above we'll always go to REFC, and then cycle again
1768*a9fa9459Szrj 	     to the indirected symbol.  This means that any successful
1769*a9fa9459Szrj 	     change of an existing symbol to indirect counts as a
1770*a9fa9459Szrj 	     reference.  ??? That may not be correct when the existing
1771*a9fa9459Szrj 	     symbol was defweak.  */
1772*a9fa9459Szrj 	  break;
1773*a9fa9459Szrj 
1774*a9fa9459Szrj 	case SET:
1775*a9fa9459Szrj 	  /* Add an entry to a set.  */
1776*a9fa9459Szrj 	  (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1777*a9fa9459Szrj 					  abfd, section, value);
1778*a9fa9459Szrj 	  break;
1779*a9fa9459Szrj 
1780*a9fa9459Szrj 	case WARNC:
1781*a9fa9459Szrj 	  /* Issue a warning and cycle, except when the reference is
1782*a9fa9459Szrj 	     in LTO IR.  */
1783*a9fa9459Szrj 	  if (h->u.i.warning != NULL
1784*a9fa9459Szrj 	      && (abfd->flags & BFD_PLUGIN) == 0)
1785*a9fa9459Szrj 	    {
1786*a9fa9459Szrj 	      (*info->callbacks->warning) (info, h->u.i.warning,
1787*a9fa9459Szrj 					   h->root.string, abfd, NULL, 0);
1788*a9fa9459Szrj 	      /* Only issue a warning once.  */
1789*a9fa9459Szrj 	      h->u.i.warning = NULL;
1790*a9fa9459Szrj 	    }
1791*a9fa9459Szrj 	  /* Fall through.  */
1792*a9fa9459Szrj 	case CYCLE:
1793*a9fa9459Szrj 	  /* Try again with the referenced symbol.  */
1794*a9fa9459Szrj 	  h = h->u.i.link;
1795*a9fa9459Szrj 	  cycle = TRUE;
1796*a9fa9459Szrj 	  break;
1797*a9fa9459Szrj 
1798*a9fa9459Szrj 	case REFC:
1799*a9fa9459Szrj 	  /* A reference to an indirect symbol.  */
1800*a9fa9459Szrj 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1801*a9fa9459Szrj 	    h->u.undef.next = h;
1802*a9fa9459Szrj 	  h = h->u.i.link;
1803*a9fa9459Szrj 	  cycle = TRUE;
1804*a9fa9459Szrj 	  break;
1805*a9fa9459Szrj 
1806*a9fa9459Szrj 	case WARN:
1807*a9fa9459Szrj 	  /* Warn if this symbol has been referenced already from non-IR,
1808*a9fa9459Szrj 	     otherwise add a warning.  */
1809*a9fa9459Szrj 	  if ((!info->lto_plugin_active
1810*a9fa9459Szrj 	       && (h->u.undef.next != NULL || info->hash->undefs_tail == h))
1811*a9fa9459Szrj 	      || h->non_ir_ref)
1812*a9fa9459Szrj 	    {
1813*a9fa9459Szrj 	      (*info->callbacks->warning) (info, string, h->root.string,
1814*a9fa9459Szrj 					   hash_entry_bfd (h), NULL, 0);
1815*a9fa9459Szrj 	      break;
1816*a9fa9459Szrj 	    }
1817*a9fa9459Szrj 	  /* Fall through.  */
1818*a9fa9459Szrj 	case MWARN:
1819*a9fa9459Szrj 	  /* Make a warning symbol.  */
1820*a9fa9459Szrj 	  {
1821*a9fa9459Szrj 	    struct bfd_link_hash_entry *sub;
1822*a9fa9459Szrj 
1823*a9fa9459Szrj 	    /* STRING is the warning to give.  */
1824*a9fa9459Szrj 	    sub = ((struct bfd_link_hash_entry *)
1825*a9fa9459Szrj 		   ((*info->hash->table.newfunc)
1826*a9fa9459Szrj 		    (NULL, &info->hash->table, h->root.string)));
1827*a9fa9459Szrj 	    if (sub == NULL)
1828*a9fa9459Szrj 	      return FALSE;
1829*a9fa9459Szrj 	    *sub = *h;
1830*a9fa9459Szrj 	    sub->type = bfd_link_hash_warning;
1831*a9fa9459Szrj 	    sub->u.i.link = h;
1832*a9fa9459Szrj 	    if (! copy)
1833*a9fa9459Szrj 	      sub->u.i.warning = string;
1834*a9fa9459Szrj 	    else
1835*a9fa9459Szrj 	      {
1836*a9fa9459Szrj 		char *w;
1837*a9fa9459Szrj 		size_t len = strlen (string) + 1;
1838*a9fa9459Szrj 
1839*a9fa9459Szrj 		w = (char *) bfd_hash_allocate (&info->hash->table, len);
1840*a9fa9459Szrj 		if (w == NULL)
1841*a9fa9459Szrj 		  return FALSE;
1842*a9fa9459Szrj 		memcpy (w, string, len);
1843*a9fa9459Szrj 		sub->u.i.warning = w;
1844*a9fa9459Szrj 	      }
1845*a9fa9459Szrj 
1846*a9fa9459Szrj 	    bfd_hash_replace (&info->hash->table,
1847*a9fa9459Szrj 			      (struct bfd_hash_entry *) h,
1848*a9fa9459Szrj 			      (struct bfd_hash_entry *) sub);
1849*a9fa9459Szrj 	    if (hashp != NULL)
1850*a9fa9459Szrj 	      *hashp = sub;
1851*a9fa9459Szrj 	  }
1852*a9fa9459Szrj 	  break;
1853*a9fa9459Szrj 	}
1854*a9fa9459Szrj     }
1855*a9fa9459Szrj   while (cycle);
1856*a9fa9459Szrj 
1857*a9fa9459Szrj   return TRUE;
1858*a9fa9459Szrj }
1859*a9fa9459Szrj 
1860*a9fa9459Szrj /* Generic final link routine.  */
1861*a9fa9459Szrj 
1862*a9fa9459Szrj bfd_boolean
_bfd_generic_final_link(bfd * abfd,struct bfd_link_info * info)1863*a9fa9459Szrj _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
1864*a9fa9459Szrj {
1865*a9fa9459Szrj   bfd *sub;
1866*a9fa9459Szrj   asection *o;
1867*a9fa9459Szrj   struct bfd_link_order *p;
1868*a9fa9459Szrj   size_t outsymalloc;
1869*a9fa9459Szrj   struct generic_write_global_symbol_info wginfo;
1870*a9fa9459Szrj 
1871*a9fa9459Szrj   bfd_get_outsymbols (abfd) = NULL;
1872*a9fa9459Szrj   bfd_get_symcount (abfd) = 0;
1873*a9fa9459Szrj   outsymalloc = 0;
1874*a9fa9459Szrj 
1875*a9fa9459Szrj   /* Mark all sections which will be included in the output file.  */
1876*a9fa9459Szrj   for (o = abfd->sections; o != NULL; o = o->next)
1877*a9fa9459Szrj     for (p = o->map_head.link_order; p != NULL; p = p->next)
1878*a9fa9459Szrj       if (p->type == bfd_indirect_link_order)
1879*a9fa9459Szrj 	p->u.indirect.section->linker_mark = TRUE;
1880*a9fa9459Szrj 
1881*a9fa9459Szrj   /* Build the output symbol table.  */
1882*a9fa9459Szrj   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
1883*a9fa9459Szrj     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1884*a9fa9459Szrj       return FALSE;
1885*a9fa9459Szrj 
1886*a9fa9459Szrj   /* Accumulate the global symbols.  */
1887*a9fa9459Szrj   wginfo.info = info;
1888*a9fa9459Szrj   wginfo.output_bfd = abfd;
1889*a9fa9459Szrj   wginfo.psymalloc = &outsymalloc;
1890*a9fa9459Szrj   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
1891*a9fa9459Szrj 				   _bfd_generic_link_write_global_symbol,
1892*a9fa9459Szrj 				   &wginfo);
1893*a9fa9459Szrj 
1894*a9fa9459Szrj   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
1895*a9fa9459Szrj      shouldn't really need one, since we have SYMCOUNT, but some old
1896*a9fa9459Szrj      code still expects one.  */
1897*a9fa9459Szrj   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
1898*a9fa9459Szrj     return FALSE;
1899*a9fa9459Szrj 
1900*a9fa9459Szrj   if (bfd_link_relocatable (info))
1901*a9fa9459Szrj     {
1902*a9fa9459Szrj       /* Allocate space for the output relocs for each section.  */
1903*a9fa9459Szrj       for (o = abfd->sections; o != NULL; o = o->next)
1904*a9fa9459Szrj 	{
1905*a9fa9459Szrj 	  o->reloc_count = 0;
1906*a9fa9459Szrj 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
1907*a9fa9459Szrj 	    {
1908*a9fa9459Szrj 	      if (p->type == bfd_section_reloc_link_order
1909*a9fa9459Szrj 		  || p->type == bfd_symbol_reloc_link_order)
1910*a9fa9459Szrj 		++o->reloc_count;
1911*a9fa9459Szrj 	      else if (p->type == bfd_indirect_link_order)
1912*a9fa9459Szrj 		{
1913*a9fa9459Szrj 		  asection *input_section;
1914*a9fa9459Szrj 		  bfd *input_bfd;
1915*a9fa9459Szrj 		  long relsize;
1916*a9fa9459Szrj 		  arelent **relocs;
1917*a9fa9459Szrj 		  asymbol **symbols;
1918*a9fa9459Szrj 		  long reloc_count;
1919*a9fa9459Szrj 
1920*a9fa9459Szrj 		  input_section = p->u.indirect.section;
1921*a9fa9459Szrj 		  input_bfd = input_section->owner;
1922*a9fa9459Szrj 		  relsize = bfd_get_reloc_upper_bound (input_bfd,
1923*a9fa9459Szrj 						       input_section);
1924*a9fa9459Szrj 		  if (relsize < 0)
1925*a9fa9459Szrj 		    return FALSE;
1926*a9fa9459Szrj 		  relocs = (arelent **) bfd_malloc (relsize);
1927*a9fa9459Szrj 		  if (!relocs && relsize != 0)
1928*a9fa9459Szrj 		    return FALSE;
1929*a9fa9459Szrj 		  symbols = _bfd_generic_link_get_symbols (input_bfd);
1930*a9fa9459Szrj 		  reloc_count = bfd_canonicalize_reloc (input_bfd,
1931*a9fa9459Szrj 							input_section,
1932*a9fa9459Szrj 							relocs,
1933*a9fa9459Szrj 							symbols);
1934*a9fa9459Szrj 		  free (relocs);
1935*a9fa9459Szrj 		  if (reloc_count < 0)
1936*a9fa9459Szrj 		    return FALSE;
1937*a9fa9459Szrj 		  BFD_ASSERT ((unsigned long) reloc_count
1938*a9fa9459Szrj 			      == input_section->reloc_count);
1939*a9fa9459Szrj 		  o->reloc_count += reloc_count;
1940*a9fa9459Szrj 		}
1941*a9fa9459Szrj 	    }
1942*a9fa9459Szrj 	  if (o->reloc_count > 0)
1943*a9fa9459Szrj 	    {
1944*a9fa9459Szrj 	      bfd_size_type amt;
1945*a9fa9459Szrj 
1946*a9fa9459Szrj 	      amt = o->reloc_count;
1947*a9fa9459Szrj 	      amt *= sizeof (arelent *);
1948*a9fa9459Szrj 	      o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
1949*a9fa9459Szrj 	      if (!o->orelocation)
1950*a9fa9459Szrj 		return FALSE;
1951*a9fa9459Szrj 	      o->flags |= SEC_RELOC;
1952*a9fa9459Szrj 	      /* Reset the count so that it can be used as an index
1953*a9fa9459Szrj 		 when putting in the output relocs.  */
1954*a9fa9459Szrj 	      o->reloc_count = 0;
1955*a9fa9459Szrj 	    }
1956*a9fa9459Szrj 	}
1957*a9fa9459Szrj     }
1958*a9fa9459Szrj 
1959*a9fa9459Szrj   /* Handle all the link order information for the sections.  */
1960*a9fa9459Szrj   for (o = abfd->sections; o != NULL; o = o->next)
1961*a9fa9459Szrj     {
1962*a9fa9459Szrj       for (p = o->map_head.link_order; p != NULL; p = p->next)
1963*a9fa9459Szrj 	{
1964*a9fa9459Szrj 	  switch (p->type)
1965*a9fa9459Szrj 	    {
1966*a9fa9459Szrj 	    case bfd_section_reloc_link_order:
1967*a9fa9459Szrj 	    case bfd_symbol_reloc_link_order:
1968*a9fa9459Szrj 	      if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
1969*a9fa9459Szrj 		return FALSE;
1970*a9fa9459Szrj 	      break;
1971*a9fa9459Szrj 	    case bfd_indirect_link_order:
1972*a9fa9459Szrj 	      if (! default_indirect_link_order (abfd, info, o, p, TRUE))
1973*a9fa9459Szrj 		return FALSE;
1974*a9fa9459Szrj 	      break;
1975*a9fa9459Szrj 	    default:
1976*a9fa9459Szrj 	      if (! _bfd_default_link_order (abfd, info, o, p))
1977*a9fa9459Szrj 		return FALSE;
1978*a9fa9459Szrj 	      break;
1979*a9fa9459Szrj 	    }
1980*a9fa9459Szrj 	}
1981*a9fa9459Szrj     }
1982*a9fa9459Szrj 
1983*a9fa9459Szrj   return TRUE;
1984*a9fa9459Szrj }
1985*a9fa9459Szrj 
1986*a9fa9459Szrj /* Add an output symbol to the output BFD.  */
1987*a9fa9459Szrj 
1988*a9fa9459Szrj static bfd_boolean
generic_add_output_symbol(bfd * output_bfd,size_t * psymalloc,asymbol * sym)1989*a9fa9459Szrj generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
1990*a9fa9459Szrj {
1991*a9fa9459Szrj   if (bfd_get_symcount (output_bfd) >= *psymalloc)
1992*a9fa9459Szrj     {
1993*a9fa9459Szrj       asymbol **newsyms;
1994*a9fa9459Szrj       bfd_size_type amt;
1995*a9fa9459Szrj 
1996*a9fa9459Szrj       if (*psymalloc == 0)
1997*a9fa9459Szrj 	*psymalloc = 124;
1998*a9fa9459Szrj       else
1999*a9fa9459Szrj 	*psymalloc *= 2;
2000*a9fa9459Szrj       amt = *psymalloc;
2001*a9fa9459Szrj       amt *= sizeof (asymbol *);
2002*a9fa9459Szrj       newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2003*a9fa9459Szrj       if (newsyms == NULL)
2004*a9fa9459Szrj 	return FALSE;
2005*a9fa9459Szrj       bfd_get_outsymbols (output_bfd) = newsyms;
2006*a9fa9459Szrj     }
2007*a9fa9459Szrj 
2008*a9fa9459Szrj   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2009*a9fa9459Szrj   if (sym != NULL)
2010*a9fa9459Szrj     ++ bfd_get_symcount (output_bfd);
2011*a9fa9459Szrj 
2012*a9fa9459Szrj   return TRUE;
2013*a9fa9459Szrj }
2014*a9fa9459Szrj 
2015*a9fa9459Szrj /* Handle the symbols for an input BFD.  */
2016*a9fa9459Szrj 
2017*a9fa9459Szrj bfd_boolean
_bfd_generic_link_output_symbols(bfd * output_bfd,bfd * input_bfd,struct bfd_link_info * info,size_t * psymalloc)2018*a9fa9459Szrj _bfd_generic_link_output_symbols (bfd *output_bfd,
2019*a9fa9459Szrj 				  bfd *input_bfd,
2020*a9fa9459Szrj 				  struct bfd_link_info *info,
2021*a9fa9459Szrj 				  size_t *psymalloc)
2022*a9fa9459Szrj {
2023*a9fa9459Szrj   asymbol **sym_ptr;
2024*a9fa9459Szrj   asymbol **sym_end;
2025*a9fa9459Szrj 
2026*a9fa9459Szrj   if (!bfd_generic_link_read_symbols (input_bfd))
2027*a9fa9459Szrj     return FALSE;
2028*a9fa9459Szrj 
2029*a9fa9459Szrj   /* Create a filename symbol if we are supposed to.  */
2030*a9fa9459Szrj   if (info->create_object_symbols_section != NULL)
2031*a9fa9459Szrj     {
2032*a9fa9459Szrj       asection *sec;
2033*a9fa9459Szrj 
2034*a9fa9459Szrj       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2035*a9fa9459Szrj 	{
2036*a9fa9459Szrj 	  if (sec->output_section == info->create_object_symbols_section)
2037*a9fa9459Szrj 	    {
2038*a9fa9459Szrj 	      asymbol *newsym;
2039*a9fa9459Szrj 
2040*a9fa9459Szrj 	      newsym = bfd_make_empty_symbol (input_bfd);
2041*a9fa9459Szrj 	      if (!newsym)
2042*a9fa9459Szrj 		return FALSE;
2043*a9fa9459Szrj 	      newsym->name = input_bfd->filename;
2044*a9fa9459Szrj 	      newsym->value = 0;
2045*a9fa9459Szrj 	      newsym->flags = BSF_LOCAL | BSF_FILE;
2046*a9fa9459Szrj 	      newsym->section = sec;
2047*a9fa9459Szrj 
2048*a9fa9459Szrj 	      if (! generic_add_output_symbol (output_bfd, psymalloc,
2049*a9fa9459Szrj 					       newsym))
2050*a9fa9459Szrj 		return FALSE;
2051*a9fa9459Szrj 
2052*a9fa9459Szrj 	      break;
2053*a9fa9459Szrj 	    }
2054*a9fa9459Szrj 	}
2055*a9fa9459Szrj     }
2056*a9fa9459Szrj 
2057*a9fa9459Szrj   /* Adjust the values of the globally visible symbols, and write out
2058*a9fa9459Szrj      local symbols.  */
2059*a9fa9459Szrj   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2060*a9fa9459Szrj   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2061*a9fa9459Szrj   for (; sym_ptr < sym_end; sym_ptr++)
2062*a9fa9459Szrj     {
2063*a9fa9459Szrj       asymbol *sym;
2064*a9fa9459Szrj       struct generic_link_hash_entry *h;
2065*a9fa9459Szrj       bfd_boolean output;
2066*a9fa9459Szrj 
2067*a9fa9459Szrj       h = NULL;
2068*a9fa9459Szrj       sym = *sym_ptr;
2069*a9fa9459Szrj       if ((sym->flags & (BSF_INDIRECT
2070*a9fa9459Szrj 			 | BSF_WARNING
2071*a9fa9459Szrj 			 | BSF_GLOBAL
2072*a9fa9459Szrj 			 | BSF_CONSTRUCTOR
2073*a9fa9459Szrj 			 | BSF_WEAK)) != 0
2074*a9fa9459Szrj 	  || bfd_is_und_section (bfd_get_section (sym))
2075*a9fa9459Szrj 	  || bfd_is_com_section (bfd_get_section (sym))
2076*a9fa9459Szrj 	  || bfd_is_ind_section (bfd_get_section (sym)))
2077*a9fa9459Szrj 	{
2078*a9fa9459Szrj 	  if (sym->udata.p != NULL)
2079*a9fa9459Szrj 	    h = (struct generic_link_hash_entry *) sym->udata.p;
2080*a9fa9459Szrj 	  else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2081*a9fa9459Szrj 	    {
2082*a9fa9459Szrj 	      /* This case normally means that the main linker code
2083*a9fa9459Szrj                  deliberately ignored this constructor symbol.  We
2084*a9fa9459Szrj                  should just pass it through.  This will screw up if
2085*a9fa9459Szrj                  the constructor symbol is from a different,
2086*a9fa9459Szrj                  non-generic, object file format, but the case will
2087*a9fa9459Szrj                  only arise when linking with -r, which will probably
2088*a9fa9459Szrj                  fail anyhow, since there will be no way to represent
2089*a9fa9459Szrj                  the relocs in the output format being used.  */
2090*a9fa9459Szrj 	      h = NULL;
2091*a9fa9459Szrj 	    }
2092*a9fa9459Szrj 	  else if (bfd_is_und_section (bfd_get_section (sym)))
2093*a9fa9459Szrj 	    h = ((struct generic_link_hash_entry *)
2094*a9fa9459Szrj 		 bfd_wrapped_link_hash_lookup (output_bfd, info,
2095*a9fa9459Szrj 					       bfd_asymbol_name (sym),
2096*a9fa9459Szrj 					       FALSE, FALSE, TRUE));
2097*a9fa9459Szrj 	  else
2098*a9fa9459Szrj 	    h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2099*a9fa9459Szrj 					       bfd_asymbol_name (sym),
2100*a9fa9459Szrj 					       FALSE, FALSE, TRUE);
2101*a9fa9459Szrj 
2102*a9fa9459Szrj 	  if (h != NULL)
2103*a9fa9459Szrj 	    {
2104*a9fa9459Szrj 	      /* Force all references to this symbol to point to
2105*a9fa9459Szrj 		 the same area in memory.  It is possible that
2106*a9fa9459Szrj 		 this routine will be called with a hash table
2107*a9fa9459Szrj 		 other than a generic hash table, so we double
2108*a9fa9459Szrj 		 check that.  */
2109*a9fa9459Szrj 	      if (info->output_bfd->xvec == input_bfd->xvec)
2110*a9fa9459Szrj 		{
2111*a9fa9459Szrj 		  if (h->sym != NULL)
2112*a9fa9459Szrj 		    *sym_ptr = sym = h->sym;
2113*a9fa9459Szrj 		}
2114*a9fa9459Szrj 
2115*a9fa9459Szrj 	      switch (h->root.type)
2116*a9fa9459Szrj 		{
2117*a9fa9459Szrj 		default:
2118*a9fa9459Szrj 		case bfd_link_hash_new:
2119*a9fa9459Szrj 		  abort ();
2120*a9fa9459Szrj 		case bfd_link_hash_undefined:
2121*a9fa9459Szrj 		  break;
2122*a9fa9459Szrj 		case bfd_link_hash_undefweak:
2123*a9fa9459Szrj 		  sym->flags |= BSF_WEAK;
2124*a9fa9459Szrj 		  break;
2125*a9fa9459Szrj 		case bfd_link_hash_indirect:
2126*a9fa9459Szrj 		  h = (struct generic_link_hash_entry *) h->root.u.i.link;
2127*a9fa9459Szrj 		  /* fall through */
2128*a9fa9459Szrj 		case bfd_link_hash_defined:
2129*a9fa9459Szrj 		  sym->flags |= BSF_GLOBAL;
2130*a9fa9459Szrj 		  sym->flags &=~ (BSF_WEAK | BSF_CONSTRUCTOR);
2131*a9fa9459Szrj 		  sym->value = h->root.u.def.value;
2132*a9fa9459Szrj 		  sym->section = h->root.u.def.section;
2133*a9fa9459Szrj 		  break;
2134*a9fa9459Szrj 		case bfd_link_hash_defweak:
2135*a9fa9459Szrj 		  sym->flags |= BSF_WEAK;
2136*a9fa9459Szrj 		  sym->flags &=~ BSF_CONSTRUCTOR;
2137*a9fa9459Szrj 		  sym->value = h->root.u.def.value;
2138*a9fa9459Szrj 		  sym->section = h->root.u.def.section;
2139*a9fa9459Szrj 		  break;
2140*a9fa9459Szrj 		case bfd_link_hash_common:
2141*a9fa9459Szrj 		  sym->value = h->root.u.c.size;
2142*a9fa9459Szrj 		  sym->flags |= BSF_GLOBAL;
2143*a9fa9459Szrj 		  if (! bfd_is_com_section (sym->section))
2144*a9fa9459Szrj 		    {
2145*a9fa9459Szrj 		      BFD_ASSERT (bfd_is_und_section (sym->section));
2146*a9fa9459Szrj 		      sym->section = bfd_com_section_ptr;
2147*a9fa9459Szrj 		    }
2148*a9fa9459Szrj 		  /* We do not set the section of the symbol to
2149*a9fa9459Szrj 		     h->root.u.c.p->section.  That value was saved so
2150*a9fa9459Szrj 		     that we would know where to allocate the symbol
2151*a9fa9459Szrj 		     if it was defined.  In this case the type is
2152*a9fa9459Szrj 		     still bfd_link_hash_common, so we did not define
2153*a9fa9459Szrj 		     it, so we do not want to use that section.  */
2154*a9fa9459Szrj 		  break;
2155*a9fa9459Szrj 		}
2156*a9fa9459Szrj 	    }
2157*a9fa9459Szrj 	}
2158*a9fa9459Szrj 
2159*a9fa9459Szrj       /* This switch is straight from the old code in
2160*a9fa9459Szrj 	 write_file_locals in ldsym.c.  */
2161*a9fa9459Szrj       if (info->strip == strip_all
2162*a9fa9459Szrj 	  || (info->strip == strip_some
2163*a9fa9459Szrj 	      && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2164*a9fa9459Szrj 				  FALSE, FALSE) == NULL))
2165*a9fa9459Szrj 	output = FALSE;
2166*a9fa9459Szrj       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2167*a9fa9459Szrj 	{
2168*a9fa9459Szrj 	  /* If this symbol is marked as occurring now, rather
2169*a9fa9459Szrj 	     than at the end, output it now.  This is used for
2170*a9fa9459Szrj 	     COFF C_EXT FCN symbols.  FIXME: There must be a
2171*a9fa9459Szrj 	     better way.  */
2172*a9fa9459Szrj 	  if (bfd_asymbol_bfd (sym) == input_bfd
2173*a9fa9459Szrj 	      && (sym->flags & BSF_NOT_AT_END) != 0)
2174*a9fa9459Szrj 	    output = TRUE;
2175*a9fa9459Szrj 	  else
2176*a9fa9459Szrj 	    output = FALSE;
2177*a9fa9459Szrj 	}
2178*a9fa9459Szrj       else if (bfd_is_ind_section (sym->section))
2179*a9fa9459Szrj 	output = FALSE;
2180*a9fa9459Szrj       else if ((sym->flags & BSF_DEBUGGING) != 0)
2181*a9fa9459Szrj 	{
2182*a9fa9459Szrj 	  if (info->strip == strip_none)
2183*a9fa9459Szrj 	    output = TRUE;
2184*a9fa9459Szrj 	  else
2185*a9fa9459Szrj 	    output = FALSE;
2186*a9fa9459Szrj 	}
2187*a9fa9459Szrj       else if (bfd_is_und_section (sym->section)
2188*a9fa9459Szrj 	       || bfd_is_com_section (sym->section))
2189*a9fa9459Szrj 	output = FALSE;
2190*a9fa9459Szrj       else if ((sym->flags & BSF_LOCAL) != 0)
2191*a9fa9459Szrj 	{
2192*a9fa9459Szrj 	  if ((sym->flags & BSF_WARNING) != 0)
2193*a9fa9459Szrj 	    output = FALSE;
2194*a9fa9459Szrj 	  else
2195*a9fa9459Szrj 	    {
2196*a9fa9459Szrj 	      switch (info->discard)
2197*a9fa9459Szrj 		{
2198*a9fa9459Szrj 		default:
2199*a9fa9459Szrj 		case discard_all:
2200*a9fa9459Szrj 		  output = FALSE;
2201*a9fa9459Szrj 		  break;
2202*a9fa9459Szrj 		case discard_sec_merge:
2203*a9fa9459Szrj 		  output = TRUE;
2204*a9fa9459Szrj 		  if (bfd_link_relocatable (info)
2205*a9fa9459Szrj 		      || ! (sym->section->flags & SEC_MERGE))
2206*a9fa9459Szrj 		    break;
2207*a9fa9459Szrj 		  /* FALLTHROUGH */
2208*a9fa9459Szrj 		case discard_l:
2209*a9fa9459Szrj 		  if (bfd_is_local_label (input_bfd, sym))
2210*a9fa9459Szrj 		    output = FALSE;
2211*a9fa9459Szrj 		  else
2212*a9fa9459Szrj 		    output = TRUE;
2213*a9fa9459Szrj 		  break;
2214*a9fa9459Szrj 		case discard_none:
2215*a9fa9459Szrj 		  output = TRUE;
2216*a9fa9459Szrj 		  break;
2217*a9fa9459Szrj 		}
2218*a9fa9459Szrj 	    }
2219*a9fa9459Szrj 	}
2220*a9fa9459Szrj       else if ((sym->flags & BSF_CONSTRUCTOR))
2221*a9fa9459Szrj 	{
2222*a9fa9459Szrj 	  if (info->strip != strip_all)
2223*a9fa9459Szrj 	    output = TRUE;
2224*a9fa9459Szrj 	  else
2225*a9fa9459Szrj 	    output = FALSE;
2226*a9fa9459Szrj 	}
2227*a9fa9459Szrj       else if (sym->flags == 0
2228*a9fa9459Szrj 	       && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2229*a9fa9459Szrj 	/* LTO doesn't set symbol information.  We get here with the
2230*a9fa9459Szrj 	   generic linker for a symbol that was "common" but no longer
2231*a9fa9459Szrj 	   needs to be global.  */
2232*a9fa9459Szrj 	output = FALSE;
2233*a9fa9459Szrj       else
2234*a9fa9459Szrj 	abort ();
2235*a9fa9459Szrj 
2236*a9fa9459Szrj       /* If this symbol is in a section which is not being included
2237*a9fa9459Szrj 	 in the output file, then we don't want to output the
2238*a9fa9459Szrj 	 symbol.  */
2239*a9fa9459Szrj       if (!bfd_is_abs_section (sym->section)
2240*a9fa9459Szrj 	  && bfd_section_removed_from_list (output_bfd,
2241*a9fa9459Szrj 					    sym->section->output_section))
2242*a9fa9459Szrj 	output = FALSE;
2243*a9fa9459Szrj 
2244*a9fa9459Szrj       if (output)
2245*a9fa9459Szrj 	{
2246*a9fa9459Szrj 	  if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2247*a9fa9459Szrj 	    return FALSE;
2248*a9fa9459Szrj 	  if (h != NULL)
2249*a9fa9459Szrj 	    h->written = TRUE;
2250*a9fa9459Szrj 	}
2251*a9fa9459Szrj     }
2252*a9fa9459Szrj 
2253*a9fa9459Szrj   return TRUE;
2254*a9fa9459Szrj }
2255*a9fa9459Szrj 
2256*a9fa9459Szrj /* Set the section and value of a generic BFD symbol based on a linker
2257*a9fa9459Szrj    hash table entry.  */
2258*a9fa9459Szrj 
2259*a9fa9459Szrj static void
set_symbol_from_hash(asymbol * sym,struct bfd_link_hash_entry * h)2260*a9fa9459Szrj set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2261*a9fa9459Szrj {
2262*a9fa9459Szrj   switch (h->type)
2263*a9fa9459Szrj     {
2264*a9fa9459Szrj     default:
2265*a9fa9459Szrj       abort ();
2266*a9fa9459Szrj       break;
2267*a9fa9459Szrj     case bfd_link_hash_new:
2268*a9fa9459Szrj       /* This can happen when a constructor symbol is seen but we are
2269*a9fa9459Szrj          not building constructors.  */
2270*a9fa9459Szrj       if (sym->section != NULL)
2271*a9fa9459Szrj 	{
2272*a9fa9459Szrj 	  BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2273*a9fa9459Szrj 	}
2274*a9fa9459Szrj       else
2275*a9fa9459Szrj 	{
2276*a9fa9459Szrj 	  sym->flags |= BSF_CONSTRUCTOR;
2277*a9fa9459Szrj 	  sym->section = bfd_abs_section_ptr;
2278*a9fa9459Szrj 	  sym->value = 0;
2279*a9fa9459Szrj 	}
2280*a9fa9459Szrj       break;
2281*a9fa9459Szrj     case bfd_link_hash_undefined:
2282*a9fa9459Szrj       sym->section = bfd_und_section_ptr;
2283*a9fa9459Szrj       sym->value = 0;
2284*a9fa9459Szrj       break;
2285*a9fa9459Szrj     case bfd_link_hash_undefweak:
2286*a9fa9459Szrj       sym->section = bfd_und_section_ptr;
2287*a9fa9459Szrj       sym->value = 0;
2288*a9fa9459Szrj       sym->flags |= BSF_WEAK;
2289*a9fa9459Szrj       break;
2290*a9fa9459Szrj     case bfd_link_hash_defined:
2291*a9fa9459Szrj       sym->section = h->u.def.section;
2292*a9fa9459Szrj       sym->value = h->u.def.value;
2293*a9fa9459Szrj       break;
2294*a9fa9459Szrj     case bfd_link_hash_defweak:
2295*a9fa9459Szrj       sym->flags |= BSF_WEAK;
2296*a9fa9459Szrj       sym->section = h->u.def.section;
2297*a9fa9459Szrj       sym->value = h->u.def.value;
2298*a9fa9459Szrj       break;
2299*a9fa9459Szrj     case bfd_link_hash_common:
2300*a9fa9459Szrj       sym->value = h->u.c.size;
2301*a9fa9459Szrj       if (sym->section == NULL)
2302*a9fa9459Szrj 	sym->section = bfd_com_section_ptr;
2303*a9fa9459Szrj       else if (! bfd_is_com_section (sym->section))
2304*a9fa9459Szrj 	{
2305*a9fa9459Szrj 	  BFD_ASSERT (bfd_is_und_section (sym->section));
2306*a9fa9459Szrj 	  sym->section = bfd_com_section_ptr;
2307*a9fa9459Szrj 	}
2308*a9fa9459Szrj       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
2309*a9fa9459Szrj       break;
2310*a9fa9459Szrj     case bfd_link_hash_indirect:
2311*a9fa9459Szrj     case bfd_link_hash_warning:
2312*a9fa9459Szrj       /* FIXME: What should we do here?  */
2313*a9fa9459Szrj       break;
2314*a9fa9459Szrj     }
2315*a9fa9459Szrj }
2316*a9fa9459Szrj 
2317*a9fa9459Szrj /* Write out a global symbol, if it hasn't already been written out.
2318*a9fa9459Szrj    This is called for each symbol in the hash table.  */
2319*a9fa9459Szrj 
2320*a9fa9459Szrj bfd_boolean
_bfd_generic_link_write_global_symbol(struct generic_link_hash_entry * h,void * data)2321*a9fa9459Szrj _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2322*a9fa9459Szrj 				       void *data)
2323*a9fa9459Szrj {
2324*a9fa9459Szrj   struct generic_write_global_symbol_info *wginfo =
2325*a9fa9459Szrj       (struct generic_write_global_symbol_info *) data;
2326*a9fa9459Szrj   asymbol *sym;
2327*a9fa9459Szrj 
2328*a9fa9459Szrj   if (h->written)
2329*a9fa9459Szrj     return TRUE;
2330*a9fa9459Szrj 
2331*a9fa9459Szrj   h->written = TRUE;
2332*a9fa9459Szrj 
2333*a9fa9459Szrj   if (wginfo->info->strip == strip_all
2334*a9fa9459Szrj       || (wginfo->info->strip == strip_some
2335*a9fa9459Szrj 	  && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2336*a9fa9459Szrj 			      FALSE, FALSE) == NULL))
2337*a9fa9459Szrj     return TRUE;
2338*a9fa9459Szrj 
2339*a9fa9459Szrj   if (h->sym != NULL)
2340*a9fa9459Szrj     sym = h->sym;
2341*a9fa9459Szrj   else
2342*a9fa9459Szrj     {
2343*a9fa9459Szrj       sym = bfd_make_empty_symbol (wginfo->output_bfd);
2344*a9fa9459Szrj       if (!sym)
2345*a9fa9459Szrj 	return FALSE;
2346*a9fa9459Szrj       sym->name = h->root.root.string;
2347*a9fa9459Szrj       sym->flags = 0;
2348*a9fa9459Szrj     }
2349*a9fa9459Szrj 
2350*a9fa9459Szrj   set_symbol_from_hash (sym, &h->root);
2351*a9fa9459Szrj 
2352*a9fa9459Szrj   sym->flags |= BSF_GLOBAL;
2353*a9fa9459Szrj 
2354*a9fa9459Szrj   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2355*a9fa9459Szrj 				   sym))
2356*a9fa9459Szrj     {
2357*a9fa9459Szrj       /* FIXME: No way to return failure.  */
2358*a9fa9459Szrj       abort ();
2359*a9fa9459Szrj     }
2360*a9fa9459Szrj 
2361*a9fa9459Szrj   return TRUE;
2362*a9fa9459Szrj }
2363*a9fa9459Szrj 
2364*a9fa9459Szrj /* Create a relocation.  */
2365*a9fa9459Szrj 
2366*a9fa9459Szrj bfd_boolean
_bfd_generic_reloc_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)2367*a9fa9459Szrj _bfd_generic_reloc_link_order (bfd *abfd,
2368*a9fa9459Szrj 			       struct bfd_link_info *info,
2369*a9fa9459Szrj 			       asection *sec,
2370*a9fa9459Szrj 			       struct bfd_link_order *link_order)
2371*a9fa9459Szrj {
2372*a9fa9459Szrj   arelent *r;
2373*a9fa9459Szrj 
2374*a9fa9459Szrj   if (! bfd_link_relocatable (info))
2375*a9fa9459Szrj     abort ();
2376*a9fa9459Szrj   if (sec->orelocation == NULL)
2377*a9fa9459Szrj     abort ();
2378*a9fa9459Szrj 
2379*a9fa9459Szrj   r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2380*a9fa9459Szrj   if (r == NULL)
2381*a9fa9459Szrj     return FALSE;
2382*a9fa9459Szrj 
2383*a9fa9459Szrj   r->address = link_order->offset;
2384*a9fa9459Szrj   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2385*a9fa9459Szrj   if (r->howto == 0)
2386*a9fa9459Szrj     {
2387*a9fa9459Szrj       bfd_set_error (bfd_error_bad_value);
2388*a9fa9459Szrj       return FALSE;
2389*a9fa9459Szrj     }
2390*a9fa9459Szrj 
2391*a9fa9459Szrj   /* Get the symbol to use for the relocation.  */
2392*a9fa9459Szrj   if (link_order->type == bfd_section_reloc_link_order)
2393*a9fa9459Szrj     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2394*a9fa9459Szrj   else
2395*a9fa9459Szrj     {
2396*a9fa9459Szrj       struct generic_link_hash_entry *h;
2397*a9fa9459Szrj 
2398*a9fa9459Szrj       h = ((struct generic_link_hash_entry *)
2399*a9fa9459Szrj 	   bfd_wrapped_link_hash_lookup (abfd, info,
2400*a9fa9459Szrj 					 link_order->u.reloc.p->u.name,
2401*a9fa9459Szrj 					 FALSE, FALSE, TRUE));
2402*a9fa9459Szrj       if (h == NULL
2403*a9fa9459Szrj 	  || ! h->written)
2404*a9fa9459Szrj 	{
2405*a9fa9459Szrj 	  (*info->callbacks->unattached_reloc)
2406*a9fa9459Szrj 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
2407*a9fa9459Szrj 	  bfd_set_error (bfd_error_bad_value);
2408*a9fa9459Szrj 	  return FALSE;
2409*a9fa9459Szrj 	}
2410*a9fa9459Szrj       r->sym_ptr_ptr = &h->sym;
2411*a9fa9459Szrj     }
2412*a9fa9459Szrj 
2413*a9fa9459Szrj   /* If this is an inplace reloc, write the addend to the object file.
2414*a9fa9459Szrj      Otherwise, store it in the reloc addend.  */
2415*a9fa9459Szrj   if (! r->howto->partial_inplace)
2416*a9fa9459Szrj     r->addend = link_order->u.reloc.p->addend;
2417*a9fa9459Szrj   else
2418*a9fa9459Szrj     {
2419*a9fa9459Szrj       bfd_size_type size;
2420*a9fa9459Szrj       bfd_reloc_status_type rstat;
2421*a9fa9459Szrj       bfd_byte *buf;
2422*a9fa9459Szrj       bfd_boolean ok;
2423*a9fa9459Szrj       file_ptr loc;
2424*a9fa9459Szrj 
2425*a9fa9459Szrj       size = bfd_get_reloc_size (r->howto);
2426*a9fa9459Szrj       buf = (bfd_byte *) bfd_zmalloc (size);
2427*a9fa9459Szrj       if (buf == NULL && size != 0)
2428*a9fa9459Szrj 	return FALSE;
2429*a9fa9459Szrj       rstat = _bfd_relocate_contents (r->howto, abfd,
2430*a9fa9459Szrj 				      (bfd_vma) link_order->u.reloc.p->addend,
2431*a9fa9459Szrj 				      buf);
2432*a9fa9459Szrj       switch (rstat)
2433*a9fa9459Szrj 	{
2434*a9fa9459Szrj 	case bfd_reloc_ok:
2435*a9fa9459Szrj 	  break;
2436*a9fa9459Szrj 	default:
2437*a9fa9459Szrj 	case bfd_reloc_outofrange:
2438*a9fa9459Szrj 	  abort ();
2439*a9fa9459Szrj 	case bfd_reloc_overflow:
2440*a9fa9459Szrj 	  (*info->callbacks->reloc_overflow)
2441*a9fa9459Szrj 	    (info, NULL,
2442*a9fa9459Szrj 	     (link_order->type == bfd_section_reloc_link_order
2443*a9fa9459Szrj 	      ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2444*a9fa9459Szrj 	      : link_order->u.reloc.p->u.name),
2445*a9fa9459Szrj 	     r->howto->name, link_order->u.reloc.p->addend,
2446*a9fa9459Szrj 	     NULL, NULL, 0);
2447*a9fa9459Szrj 	  break;
2448*a9fa9459Szrj 	}
2449*a9fa9459Szrj       loc = link_order->offset * bfd_octets_per_byte (abfd);
2450*a9fa9459Szrj       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2451*a9fa9459Szrj       free (buf);
2452*a9fa9459Szrj       if (! ok)
2453*a9fa9459Szrj 	return FALSE;
2454*a9fa9459Szrj 
2455*a9fa9459Szrj       r->addend = 0;
2456*a9fa9459Szrj     }
2457*a9fa9459Szrj 
2458*a9fa9459Szrj   sec->orelocation[sec->reloc_count] = r;
2459*a9fa9459Szrj   ++sec->reloc_count;
2460*a9fa9459Szrj 
2461*a9fa9459Szrj   return TRUE;
2462*a9fa9459Szrj }
2463*a9fa9459Szrj 
2464*a9fa9459Szrj /* Allocate a new link_order for a section.  */
2465*a9fa9459Szrj 
2466*a9fa9459Szrj struct bfd_link_order *
bfd_new_link_order(bfd * abfd,asection * section)2467*a9fa9459Szrj bfd_new_link_order (bfd *abfd, asection *section)
2468*a9fa9459Szrj {
2469*a9fa9459Szrj   bfd_size_type amt = sizeof (struct bfd_link_order);
2470*a9fa9459Szrj   struct bfd_link_order *new_lo;
2471*a9fa9459Szrj 
2472*a9fa9459Szrj   new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
2473*a9fa9459Szrj   if (!new_lo)
2474*a9fa9459Szrj     return NULL;
2475*a9fa9459Szrj 
2476*a9fa9459Szrj   new_lo->type = bfd_undefined_link_order;
2477*a9fa9459Szrj 
2478*a9fa9459Szrj   if (section->map_tail.link_order != NULL)
2479*a9fa9459Szrj     section->map_tail.link_order->next = new_lo;
2480*a9fa9459Szrj   else
2481*a9fa9459Szrj     section->map_head.link_order = new_lo;
2482*a9fa9459Szrj   section->map_tail.link_order = new_lo;
2483*a9fa9459Szrj 
2484*a9fa9459Szrj   return new_lo;
2485*a9fa9459Szrj }
2486*a9fa9459Szrj 
2487*a9fa9459Szrj /* Default link order processing routine.  Note that we can not handle
2488*a9fa9459Szrj    the reloc_link_order types here, since they depend upon the details
2489*a9fa9459Szrj    of how the particular backends generates relocs.  */
2490*a9fa9459Szrj 
2491*a9fa9459Szrj bfd_boolean
_bfd_default_link_order(bfd * abfd,struct bfd_link_info * info,asection * sec,struct bfd_link_order * link_order)2492*a9fa9459Szrj _bfd_default_link_order (bfd *abfd,
2493*a9fa9459Szrj 			 struct bfd_link_info *info,
2494*a9fa9459Szrj 			 asection *sec,
2495*a9fa9459Szrj 			 struct bfd_link_order *link_order)
2496*a9fa9459Szrj {
2497*a9fa9459Szrj   switch (link_order->type)
2498*a9fa9459Szrj     {
2499*a9fa9459Szrj     case bfd_undefined_link_order:
2500*a9fa9459Szrj     case bfd_section_reloc_link_order:
2501*a9fa9459Szrj     case bfd_symbol_reloc_link_order:
2502*a9fa9459Szrj     default:
2503*a9fa9459Szrj       abort ();
2504*a9fa9459Szrj     case bfd_indirect_link_order:
2505*a9fa9459Szrj       return default_indirect_link_order (abfd, info, sec, link_order,
2506*a9fa9459Szrj 					  FALSE);
2507*a9fa9459Szrj     case bfd_data_link_order:
2508*a9fa9459Szrj       return default_data_link_order (abfd, info, sec, link_order);
2509*a9fa9459Szrj     }
2510*a9fa9459Szrj }
2511*a9fa9459Szrj 
2512*a9fa9459Szrj /* Default routine to handle a bfd_data_link_order.  */
2513*a9fa9459Szrj 
2514*a9fa9459Szrj static bfd_boolean
default_data_link_order(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_order * link_order)2515*a9fa9459Szrj default_data_link_order (bfd *abfd,
2516*a9fa9459Szrj 			 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2517*a9fa9459Szrj 			 asection *sec,
2518*a9fa9459Szrj 			 struct bfd_link_order *link_order)
2519*a9fa9459Szrj {
2520*a9fa9459Szrj   bfd_size_type size;
2521*a9fa9459Szrj   size_t fill_size;
2522*a9fa9459Szrj   bfd_byte *fill;
2523*a9fa9459Szrj   file_ptr loc;
2524*a9fa9459Szrj   bfd_boolean result;
2525*a9fa9459Szrj 
2526*a9fa9459Szrj   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2527*a9fa9459Szrj 
2528*a9fa9459Szrj   size = link_order->size;
2529*a9fa9459Szrj   if (size == 0)
2530*a9fa9459Szrj     return TRUE;
2531*a9fa9459Szrj 
2532*a9fa9459Szrj   fill = link_order->u.data.contents;
2533*a9fa9459Szrj   fill_size = link_order->u.data.size;
2534*a9fa9459Szrj   if (fill_size == 0)
2535*a9fa9459Szrj     {
2536*a9fa9459Szrj       fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
2537*a9fa9459Szrj 				    (sec->flags & SEC_CODE) != 0);
2538*a9fa9459Szrj       if (fill == NULL)
2539*a9fa9459Szrj 	return FALSE;
2540*a9fa9459Szrj     }
2541*a9fa9459Szrj   else if (fill_size < size)
2542*a9fa9459Szrj     {
2543*a9fa9459Szrj       bfd_byte *p;
2544*a9fa9459Szrj       fill = (bfd_byte *) bfd_malloc (size);
2545*a9fa9459Szrj       if (fill == NULL)
2546*a9fa9459Szrj 	return FALSE;
2547*a9fa9459Szrj       p = fill;
2548*a9fa9459Szrj       if (fill_size == 1)
2549*a9fa9459Szrj 	memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2550*a9fa9459Szrj       else
2551*a9fa9459Szrj 	{
2552*a9fa9459Szrj 	  do
2553*a9fa9459Szrj 	    {
2554*a9fa9459Szrj 	      memcpy (p, link_order->u.data.contents, fill_size);
2555*a9fa9459Szrj 	      p += fill_size;
2556*a9fa9459Szrj 	      size -= fill_size;
2557*a9fa9459Szrj 	    }
2558*a9fa9459Szrj 	  while (size >= fill_size);
2559*a9fa9459Szrj 	  if (size != 0)
2560*a9fa9459Szrj 	    memcpy (p, link_order->u.data.contents, (size_t) size);
2561*a9fa9459Szrj 	  size = link_order->size;
2562*a9fa9459Szrj 	}
2563*a9fa9459Szrj     }
2564*a9fa9459Szrj 
2565*a9fa9459Szrj   loc = link_order->offset * bfd_octets_per_byte (abfd);
2566*a9fa9459Szrj   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2567*a9fa9459Szrj 
2568*a9fa9459Szrj   if (fill != link_order->u.data.contents)
2569*a9fa9459Szrj     free (fill);
2570*a9fa9459Szrj   return result;
2571*a9fa9459Szrj }
2572*a9fa9459Szrj 
2573*a9fa9459Szrj /* Default routine to handle a bfd_indirect_link_order.  */
2574*a9fa9459Szrj 
2575*a9fa9459Szrj static bfd_boolean
default_indirect_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order,bfd_boolean generic_linker)2576*a9fa9459Szrj default_indirect_link_order (bfd *output_bfd,
2577*a9fa9459Szrj 			     struct bfd_link_info *info,
2578*a9fa9459Szrj 			     asection *output_section,
2579*a9fa9459Szrj 			     struct bfd_link_order *link_order,
2580*a9fa9459Szrj 			     bfd_boolean generic_linker)
2581*a9fa9459Szrj {
2582*a9fa9459Szrj   asection *input_section;
2583*a9fa9459Szrj   bfd *input_bfd;
2584*a9fa9459Szrj   bfd_byte *contents = NULL;
2585*a9fa9459Szrj   bfd_byte *new_contents;
2586*a9fa9459Szrj   bfd_size_type sec_size;
2587*a9fa9459Szrj   file_ptr loc;
2588*a9fa9459Szrj 
2589*a9fa9459Szrj   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2590*a9fa9459Szrj 
2591*a9fa9459Szrj   input_section = link_order->u.indirect.section;
2592*a9fa9459Szrj   input_bfd = input_section->owner;
2593*a9fa9459Szrj   if (input_section->size == 0)
2594*a9fa9459Szrj     return TRUE;
2595*a9fa9459Szrj 
2596*a9fa9459Szrj   BFD_ASSERT (input_section->output_section == output_section);
2597*a9fa9459Szrj   BFD_ASSERT (input_section->output_offset == link_order->offset);
2598*a9fa9459Szrj   BFD_ASSERT (input_section->size == link_order->size);
2599*a9fa9459Szrj 
2600*a9fa9459Szrj   if (bfd_link_relocatable (info)
2601*a9fa9459Szrj       && input_section->reloc_count > 0
2602*a9fa9459Szrj       && output_section->orelocation == NULL)
2603*a9fa9459Szrj     {
2604*a9fa9459Szrj       /* Space has not been allocated for the output relocations.
2605*a9fa9459Szrj 	 This can happen when we are called by a specific backend
2606*a9fa9459Szrj 	 because somebody is attempting to link together different
2607*a9fa9459Szrj 	 types of object files.  Handling this case correctly is
2608*a9fa9459Szrj 	 difficult, and sometimes impossible.  */
2609*a9fa9459Szrj       (*_bfd_error_handler)
2610*a9fa9459Szrj 	(_("Attempt to do relocatable link with %s input and %s output"),
2611*a9fa9459Szrj 	 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2612*a9fa9459Szrj       bfd_set_error (bfd_error_wrong_format);
2613*a9fa9459Szrj       return FALSE;
2614*a9fa9459Szrj     }
2615*a9fa9459Szrj 
2616*a9fa9459Szrj   if (! generic_linker)
2617*a9fa9459Szrj     {
2618*a9fa9459Szrj       asymbol **sympp;
2619*a9fa9459Szrj       asymbol **symppend;
2620*a9fa9459Szrj 
2621*a9fa9459Szrj       /* Get the canonical symbols.  The generic linker will always
2622*a9fa9459Szrj 	 have retrieved them by this point, but we are being called by
2623*a9fa9459Szrj 	 a specific linker, presumably because we are linking
2624*a9fa9459Szrj 	 different types of object files together.  */
2625*a9fa9459Szrj       if (!bfd_generic_link_read_symbols (input_bfd))
2626*a9fa9459Szrj 	return FALSE;
2627*a9fa9459Szrj 
2628*a9fa9459Szrj       /* Since we have been called by a specific linker, rather than
2629*a9fa9459Szrj 	 the generic linker, the values of the symbols will not be
2630*a9fa9459Szrj 	 right.  They will be the values as seen in the input file,
2631*a9fa9459Szrj 	 not the values of the final link.  We need to fix them up
2632*a9fa9459Szrj 	 before we can relocate the section.  */
2633*a9fa9459Szrj       sympp = _bfd_generic_link_get_symbols (input_bfd);
2634*a9fa9459Szrj       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2635*a9fa9459Szrj       for (; sympp < symppend; sympp++)
2636*a9fa9459Szrj 	{
2637*a9fa9459Szrj 	  asymbol *sym;
2638*a9fa9459Szrj 	  struct bfd_link_hash_entry *h;
2639*a9fa9459Szrj 
2640*a9fa9459Szrj 	  sym = *sympp;
2641*a9fa9459Szrj 
2642*a9fa9459Szrj 	  if ((sym->flags & (BSF_INDIRECT
2643*a9fa9459Szrj 			     | BSF_WARNING
2644*a9fa9459Szrj 			     | BSF_GLOBAL
2645*a9fa9459Szrj 			     | BSF_CONSTRUCTOR
2646*a9fa9459Szrj 			     | BSF_WEAK)) != 0
2647*a9fa9459Szrj 	      || bfd_is_und_section (bfd_get_section (sym))
2648*a9fa9459Szrj 	      || bfd_is_com_section (bfd_get_section (sym))
2649*a9fa9459Szrj 	      || bfd_is_ind_section (bfd_get_section (sym)))
2650*a9fa9459Szrj 	    {
2651*a9fa9459Szrj 	      /* sym->udata may have been set by
2652*a9fa9459Szrj 		 generic_link_add_symbol_list.  */
2653*a9fa9459Szrj 	      if (sym->udata.p != NULL)
2654*a9fa9459Szrj 		h = (struct bfd_link_hash_entry *) sym->udata.p;
2655*a9fa9459Szrj 	      else if (bfd_is_und_section (bfd_get_section (sym)))
2656*a9fa9459Szrj 		h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2657*a9fa9459Szrj 						  bfd_asymbol_name (sym),
2658*a9fa9459Szrj 						  FALSE, FALSE, TRUE);
2659*a9fa9459Szrj 	      else
2660*a9fa9459Szrj 		h = bfd_link_hash_lookup (info->hash,
2661*a9fa9459Szrj 					  bfd_asymbol_name (sym),
2662*a9fa9459Szrj 					  FALSE, FALSE, TRUE);
2663*a9fa9459Szrj 	      if (h != NULL)
2664*a9fa9459Szrj 		set_symbol_from_hash (sym, h);
2665*a9fa9459Szrj 	    }
2666*a9fa9459Szrj 	}
2667*a9fa9459Szrj     }
2668*a9fa9459Szrj 
2669*a9fa9459Szrj   if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
2670*a9fa9459Szrj       && input_section->size != 0)
2671*a9fa9459Szrj     {
2672*a9fa9459Szrj       /* Group section contents are set by bfd_elf_set_group_contents.  */
2673*a9fa9459Szrj       if (!output_bfd->output_has_begun)
2674*a9fa9459Szrj 	{
2675*a9fa9459Szrj 	  /* FIXME: This hack ensures bfd_elf_set_group_contents is called.  */
2676*a9fa9459Szrj 	  if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
2677*a9fa9459Szrj 	    goto error_return;
2678*a9fa9459Szrj 	}
2679*a9fa9459Szrj       new_contents = output_section->contents;
2680*a9fa9459Szrj       BFD_ASSERT (new_contents != NULL);
2681*a9fa9459Szrj       BFD_ASSERT (input_section->output_offset == 0);
2682*a9fa9459Szrj     }
2683*a9fa9459Szrj   else
2684*a9fa9459Szrj     {
2685*a9fa9459Szrj       /* Get and relocate the section contents.  */
2686*a9fa9459Szrj       sec_size = (input_section->rawsize > input_section->size
2687*a9fa9459Szrj 		  ? input_section->rawsize
2688*a9fa9459Szrj 		  : input_section->size);
2689*a9fa9459Szrj       contents = (bfd_byte *) bfd_malloc (sec_size);
2690*a9fa9459Szrj       if (contents == NULL && sec_size != 0)
2691*a9fa9459Szrj 	goto error_return;
2692*a9fa9459Szrj       new_contents = (bfd_get_relocated_section_contents
2693*a9fa9459Szrj 		      (output_bfd, info, link_order, contents,
2694*a9fa9459Szrj 		       bfd_link_relocatable (info),
2695*a9fa9459Szrj 		       _bfd_generic_link_get_symbols (input_bfd)));
2696*a9fa9459Szrj       if (!new_contents)
2697*a9fa9459Szrj 	goto error_return;
2698*a9fa9459Szrj     }
2699*a9fa9459Szrj 
2700*a9fa9459Szrj   /* Output the section contents.  */
2701*a9fa9459Szrj   loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
2702*a9fa9459Szrj   if (! bfd_set_section_contents (output_bfd, output_section,
2703*a9fa9459Szrj 				  new_contents, loc, input_section->size))
2704*a9fa9459Szrj     goto error_return;
2705*a9fa9459Szrj 
2706*a9fa9459Szrj   if (contents != NULL)
2707*a9fa9459Szrj     free (contents);
2708*a9fa9459Szrj   return TRUE;
2709*a9fa9459Szrj 
2710*a9fa9459Szrj  error_return:
2711*a9fa9459Szrj   if (contents != NULL)
2712*a9fa9459Szrj     free (contents);
2713*a9fa9459Szrj   return FALSE;
2714*a9fa9459Szrj }
2715*a9fa9459Szrj 
2716*a9fa9459Szrj /* A little routine to count the number of relocs in a link_order
2717*a9fa9459Szrj    list.  */
2718*a9fa9459Szrj 
2719*a9fa9459Szrj unsigned int
_bfd_count_link_order_relocs(struct bfd_link_order * link_order)2720*a9fa9459Szrj _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2721*a9fa9459Szrj {
2722*a9fa9459Szrj   register unsigned int c;
2723*a9fa9459Szrj   register struct bfd_link_order *l;
2724*a9fa9459Szrj 
2725*a9fa9459Szrj   c = 0;
2726*a9fa9459Szrj   for (l = link_order; l != NULL; l = l->next)
2727*a9fa9459Szrj     {
2728*a9fa9459Szrj       if (l->type == bfd_section_reloc_link_order
2729*a9fa9459Szrj 	  || l->type == bfd_symbol_reloc_link_order)
2730*a9fa9459Szrj 	++c;
2731*a9fa9459Szrj     }
2732*a9fa9459Szrj 
2733*a9fa9459Szrj   return c;
2734*a9fa9459Szrj }
2735*a9fa9459Szrj 
2736*a9fa9459Szrj /*
2737*a9fa9459Szrj FUNCTION
2738*a9fa9459Szrj 	bfd_link_split_section
2739*a9fa9459Szrj 
2740*a9fa9459Szrj SYNOPSIS
2741*a9fa9459Szrj         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
2742*a9fa9459Szrj 
2743*a9fa9459Szrj DESCRIPTION
2744*a9fa9459Szrj 	Return nonzero if @var{sec} should be split during a
2745*a9fa9459Szrj 	reloceatable or final link.
2746*a9fa9459Szrj 
2747*a9fa9459Szrj .#define bfd_link_split_section(abfd, sec) \
2748*a9fa9459Szrj .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2749*a9fa9459Szrj .
2750*a9fa9459Szrj 
2751*a9fa9459Szrj */
2752*a9fa9459Szrj 
2753*a9fa9459Szrj bfd_boolean
_bfd_generic_link_split_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)2754*a9fa9459Szrj _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2755*a9fa9459Szrj 				 asection *sec ATTRIBUTE_UNUSED)
2756*a9fa9459Szrj {
2757*a9fa9459Szrj   return FALSE;
2758*a9fa9459Szrj }
2759*a9fa9459Szrj 
2760*a9fa9459Szrj /*
2761*a9fa9459Szrj FUNCTION
2762*a9fa9459Szrj 	bfd_section_already_linked
2763*a9fa9459Szrj 
2764*a9fa9459Szrj SYNOPSIS
2765*a9fa9459Szrj         bfd_boolean bfd_section_already_linked (bfd *abfd,
2766*a9fa9459Szrj 						asection *sec,
2767*a9fa9459Szrj 						struct bfd_link_info *info);
2768*a9fa9459Szrj 
2769*a9fa9459Szrj DESCRIPTION
2770*a9fa9459Szrj 	Check if @var{data} has been already linked during a reloceatable
2771*a9fa9459Szrj 	or final link.  Return TRUE if it has.
2772*a9fa9459Szrj 
2773*a9fa9459Szrj .#define bfd_section_already_linked(abfd, sec, info) \
2774*a9fa9459Szrj .       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2775*a9fa9459Szrj .
2776*a9fa9459Szrj 
2777*a9fa9459Szrj */
2778*a9fa9459Szrj 
2779*a9fa9459Szrj /* Sections marked with the SEC_LINK_ONCE flag should only be linked
2780*a9fa9459Szrj    once into the output.  This routine checks each section, and
2781*a9fa9459Szrj    arrange to discard it if a section of the same name has already
2782*a9fa9459Szrj    been linked.  This code assumes that all relevant sections have the
2783*a9fa9459Szrj    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2784*a9fa9459Szrj    section name.  bfd_section_already_linked is called via
2785*a9fa9459Szrj    bfd_map_over_sections.  */
2786*a9fa9459Szrj 
2787*a9fa9459Szrj /* The hash table.  */
2788*a9fa9459Szrj 
2789*a9fa9459Szrj static struct bfd_hash_table _bfd_section_already_linked_table;
2790*a9fa9459Szrj 
2791*a9fa9459Szrj /* Support routines for the hash table used by section_already_linked,
2792*a9fa9459Szrj    initialize the table, traverse, lookup, fill in an entry and remove
2793*a9fa9459Szrj    the table.  */
2794*a9fa9459Szrj 
2795*a9fa9459Szrj void
bfd_section_already_linked_table_traverse(bfd_boolean (* func)(struct bfd_section_already_linked_hash_entry *,void *),void * info)2796*a9fa9459Szrj bfd_section_already_linked_table_traverse
2797*a9fa9459Szrj   (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
2798*a9fa9459Szrj 			void *), void *info)
2799*a9fa9459Szrj {
2800*a9fa9459Szrj   bfd_hash_traverse (&_bfd_section_already_linked_table,
2801*a9fa9459Szrj 		     (bfd_boolean (*) (struct bfd_hash_entry *,
2802*a9fa9459Szrj 				       void *)) func,
2803*a9fa9459Szrj 		     info);
2804*a9fa9459Szrj }
2805*a9fa9459Szrj 
2806*a9fa9459Szrj struct bfd_section_already_linked_hash_entry *
bfd_section_already_linked_table_lookup(const char * name)2807*a9fa9459Szrj bfd_section_already_linked_table_lookup (const char *name)
2808*a9fa9459Szrj {
2809*a9fa9459Szrj   return ((struct bfd_section_already_linked_hash_entry *)
2810*a9fa9459Szrj 	  bfd_hash_lookup (&_bfd_section_already_linked_table, name,
2811*a9fa9459Szrj 			   TRUE, FALSE));
2812*a9fa9459Szrj }
2813*a9fa9459Szrj 
2814*a9fa9459Szrj bfd_boolean
bfd_section_already_linked_table_insert(struct bfd_section_already_linked_hash_entry * already_linked_list,asection * sec)2815*a9fa9459Szrj bfd_section_already_linked_table_insert
2816*a9fa9459Szrj   (struct bfd_section_already_linked_hash_entry *already_linked_list,
2817*a9fa9459Szrj    asection *sec)
2818*a9fa9459Szrj {
2819*a9fa9459Szrj   struct bfd_section_already_linked *l;
2820*a9fa9459Szrj 
2821*a9fa9459Szrj   /* Allocate the memory from the same obstack as the hash table is
2822*a9fa9459Szrj      kept in.  */
2823*a9fa9459Szrj   l = (struct bfd_section_already_linked *)
2824*a9fa9459Szrj       bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
2825*a9fa9459Szrj   if (l == NULL)
2826*a9fa9459Szrj     return FALSE;
2827*a9fa9459Szrj   l->sec = sec;
2828*a9fa9459Szrj   l->next = already_linked_list->entry;
2829*a9fa9459Szrj   already_linked_list->entry = l;
2830*a9fa9459Szrj   return TRUE;
2831*a9fa9459Szrj }
2832*a9fa9459Szrj 
2833*a9fa9459Szrj static struct bfd_hash_entry *
already_linked_newfunc(struct bfd_hash_entry * entry ATTRIBUTE_UNUSED,struct bfd_hash_table * table,const char * string ATTRIBUTE_UNUSED)2834*a9fa9459Szrj already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
2835*a9fa9459Szrj 			struct bfd_hash_table *table,
2836*a9fa9459Szrj 			const char *string ATTRIBUTE_UNUSED)
2837*a9fa9459Szrj {
2838*a9fa9459Szrj   struct bfd_section_already_linked_hash_entry *ret =
2839*a9fa9459Szrj     (struct bfd_section_already_linked_hash_entry *)
2840*a9fa9459Szrj       bfd_hash_allocate (table, sizeof *ret);
2841*a9fa9459Szrj 
2842*a9fa9459Szrj   if (ret == NULL)
2843*a9fa9459Szrj     return NULL;
2844*a9fa9459Szrj 
2845*a9fa9459Szrj   ret->entry = NULL;
2846*a9fa9459Szrj 
2847*a9fa9459Szrj   return &ret->root;
2848*a9fa9459Szrj }
2849*a9fa9459Szrj 
2850*a9fa9459Szrj bfd_boolean
bfd_section_already_linked_table_init(void)2851*a9fa9459Szrj bfd_section_already_linked_table_init (void)
2852*a9fa9459Szrj {
2853*a9fa9459Szrj   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
2854*a9fa9459Szrj 				already_linked_newfunc,
2855*a9fa9459Szrj 				sizeof (struct bfd_section_already_linked_hash_entry),
2856*a9fa9459Szrj 				42);
2857*a9fa9459Szrj }
2858*a9fa9459Szrj 
2859*a9fa9459Szrj void
bfd_section_already_linked_table_free(void)2860*a9fa9459Szrj bfd_section_already_linked_table_free (void)
2861*a9fa9459Szrj {
2862*a9fa9459Szrj   bfd_hash_table_free (&_bfd_section_already_linked_table);
2863*a9fa9459Szrj }
2864*a9fa9459Szrj 
2865*a9fa9459Szrj /* Report warnings as appropriate for duplicate section SEC.
2866*a9fa9459Szrj    Return FALSE if we decide to keep SEC after all.  */
2867*a9fa9459Szrj 
2868*a9fa9459Szrj bfd_boolean
_bfd_handle_already_linked(asection * sec,struct bfd_section_already_linked * l,struct bfd_link_info * info)2869*a9fa9459Szrj _bfd_handle_already_linked (asection *sec,
2870*a9fa9459Szrj 			    struct bfd_section_already_linked *l,
2871*a9fa9459Szrj 			    struct bfd_link_info *info)
2872*a9fa9459Szrj {
2873*a9fa9459Szrj   switch (sec->flags & SEC_LINK_DUPLICATES)
2874*a9fa9459Szrj     {
2875*a9fa9459Szrj     default:
2876*a9fa9459Szrj       abort ();
2877*a9fa9459Szrj 
2878*a9fa9459Szrj     case SEC_LINK_DUPLICATES_DISCARD:
2879*a9fa9459Szrj       /* If we found an LTO IR match for this comdat group on
2880*a9fa9459Szrj 	 the first pass, replace it with the LTO output on the
2881*a9fa9459Szrj 	 second pass.  We can't simply choose real object
2882*a9fa9459Szrj 	 files over IR because the first pass may contain a
2883*a9fa9459Szrj 	 mix of LTO and normal objects and we must keep the
2884*a9fa9459Szrj 	 first match, be it IR or real.  */
2885*a9fa9459Szrj       if (sec->owner->lto_output
2886*a9fa9459Szrj 	  && (l->sec->owner->flags & BFD_PLUGIN) != 0)
2887*a9fa9459Szrj 	{
2888*a9fa9459Szrj 	  l->sec = sec;
2889*a9fa9459Szrj 	  return FALSE;
2890*a9fa9459Szrj 	}
2891*a9fa9459Szrj       break;
2892*a9fa9459Szrj 
2893*a9fa9459Szrj     case SEC_LINK_DUPLICATES_ONE_ONLY:
2894*a9fa9459Szrj       info->callbacks->einfo
2895*a9fa9459Szrj 	(_("%B: ignoring duplicate section `%A'\n"),
2896*a9fa9459Szrj 	 sec->owner, sec);
2897*a9fa9459Szrj       break;
2898*a9fa9459Szrj 
2899*a9fa9459Szrj     case SEC_LINK_DUPLICATES_SAME_SIZE:
2900*a9fa9459Szrj       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
2901*a9fa9459Szrj 	;
2902*a9fa9459Szrj       else if (sec->size != l->sec->size)
2903*a9fa9459Szrj 	info->callbacks->einfo
2904*a9fa9459Szrj 	  (_("%B: duplicate section `%A' has different size\n"),
2905*a9fa9459Szrj 	   sec->owner, sec);
2906*a9fa9459Szrj       break;
2907*a9fa9459Szrj 
2908*a9fa9459Szrj     case SEC_LINK_DUPLICATES_SAME_CONTENTS:
2909*a9fa9459Szrj       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
2910*a9fa9459Szrj 	;
2911*a9fa9459Szrj       else if (sec->size != l->sec->size)
2912*a9fa9459Szrj 	info->callbacks->einfo
2913*a9fa9459Szrj 	  (_("%B: duplicate section `%A' has different size\n"),
2914*a9fa9459Szrj 	   sec->owner, sec);
2915*a9fa9459Szrj       else if (sec->size != 0)
2916*a9fa9459Szrj 	{
2917*a9fa9459Szrj 	  bfd_byte *sec_contents, *l_sec_contents = NULL;
2918*a9fa9459Szrj 
2919*a9fa9459Szrj 	  if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
2920*a9fa9459Szrj 	    info->callbacks->einfo
2921*a9fa9459Szrj 	      (_("%B: could not read contents of section `%A'\n"),
2922*a9fa9459Szrj 	       sec->owner, sec);
2923*a9fa9459Szrj 	  else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
2924*a9fa9459Szrj 						&l_sec_contents))
2925*a9fa9459Szrj 	    info->callbacks->einfo
2926*a9fa9459Szrj 	      (_("%B: could not read contents of section `%A'\n"),
2927*a9fa9459Szrj 	       l->sec->owner, l->sec);
2928*a9fa9459Szrj 	  else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
2929*a9fa9459Szrj 	    info->callbacks->einfo
2930*a9fa9459Szrj 	      (_("%B: duplicate section `%A' has different contents\n"),
2931*a9fa9459Szrj 	       sec->owner, sec);
2932*a9fa9459Szrj 
2933*a9fa9459Szrj 	  if (sec_contents)
2934*a9fa9459Szrj 	    free (sec_contents);
2935*a9fa9459Szrj 	  if (l_sec_contents)
2936*a9fa9459Szrj 	    free (l_sec_contents);
2937*a9fa9459Szrj 	}
2938*a9fa9459Szrj       break;
2939*a9fa9459Szrj     }
2940*a9fa9459Szrj 
2941*a9fa9459Szrj   /* Set the output_section field so that lang_add_section
2942*a9fa9459Szrj      does not create a lang_input_section structure for this
2943*a9fa9459Szrj      section.  Since there might be a symbol in the section
2944*a9fa9459Szrj      being discarded, we must retain a pointer to the section
2945*a9fa9459Szrj      which we are really going to use.  */
2946*a9fa9459Szrj   sec->output_section = bfd_abs_section_ptr;
2947*a9fa9459Szrj   sec->kept_section = l->sec;
2948*a9fa9459Szrj   return TRUE;
2949*a9fa9459Szrj }
2950*a9fa9459Szrj 
2951*a9fa9459Szrj /* This is used on non-ELF inputs.  */
2952*a9fa9459Szrj 
2953*a9fa9459Szrj bfd_boolean
_bfd_generic_section_already_linked(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,struct bfd_link_info * info)2954*a9fa9459Szrj _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
2955*a9fa9459Szrj 				     asection *sec,
2956*a9fa9459Szrj 				     struct bfd_link_info *info)
2957*a9fa9459Szrj {
2958*a9fa9459Szrj   const char *name;
2959*a9fa9459Szrj   struct bfd_section_already_linked *l;
2960*a9fa9459Szrj   struct bfd_section_already_linked_hash_entry *already_linked_list;
2961*a9fa9459Szrj 
2962*a9fa9459Szrj   if ((sec->flags & SEC_LINK_ONCE) == 0)
2963*a9fa9459Szrj     return FALSE;
2964*a9fa9459Szrj 
2965*a9fa9459Szrj   /* The generic linker doesn't handle section groups.  */
2966*a9fa9459Szrj   if ((sec->flags & SEC_GROUP) != 0)
2967*a9fa9459Szrj     return FALSE;
2968*a9fa9459Szrj 
2969*a9fa9459Szrj   /* FIXME: When doing a relocatable link, we may have trouble
2970*a9fa9459Szrj      copying relocations in other sections that refer to local symbols
2971*a9fa9459Szrj      in the section being discarded.  Those relocations will have to
2972*a9fa9459Szrj      be converted somehow; as of this writing I'm not sure that any of
2973*a9fa9459Szrj      the backends handle that correctly.
2974*a9fa9459Szrj 
2975*a9fa9459Szrj      It is tempting to instead not discard link once sections when
2976*a9fa9459Szrj      doing a relocatable link (technically, they should be discarded
2977*a9fa9459Szrj      whenever we are building constructors).  However, that fails,
2978*a9fa9459Szrj      because the linker winds up combining all the link once sections
2979*a9fa9459Szrj      into a single large link once section, which defeats the purpose
2980*a9fa9459Szrj      of having link once sections in the first place.  */
2981*a9fa9459Szrj 
2982*a9fa9459Szrj   name = bfd_get_section_name (abfd, sec);
2983*a9fa9459Szrj 
2984*a9fa9459Szrj   already_linked_list = bfd_section_already_linked_table_lookup (name);
2985*a9fa9459Szrj 
2986*a9fa9459Szrj   l = already_linked_list->entry;
2987*a9fa9459Szrj   if (l != NULL)
2988*a9fa9459Szrj     {
2989*a9fa9459Szrj       /* The section has already been linked.  See if we should
2990*a9fa9459Szrj 	 issue a warning.  */
2991*a9fa9459Szrj       return _bfd_handle_already_linked (sec, l, info);
2992*a9fa9459Szrj     }
2993*a9fa9459Szrj 
2994*a9fa9459Szrj   /* This is the first section with this name.  Record it.  */
2995*a9fa9459Szrj   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2996*a9fa9459Szrj     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2997*a9fa9459Szrj   return FALSE;
2998*a9fa9459Szrj }
2999*a9fa9459Szrj 
3000*a9fa9459Szrj /* Choose a neighbouring section to S in OBFD that will be output, or
3001*a9fa9459Szrj    the absolute section if ADDR is out of bounds of the neighbours.  */
3002*a9fa9459Szrj 
3003*a9fa9459Szrj asection *
_bfd_nearby_section(bfd * obfd,asection * s,bfd_vma addr)3004*a9fa9459Szrj _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3005*a9fa9459Szrj {
3006*a9fa9459Szrj   asection *next, *prev, *best;
3007*a9fa9459Szrj 
3008*a9fa9459Szrj   /* Find preceding kept section.  */
3009*a9fa9459Szrj   for (prev = s->prev; prev != NULL; prev = prev->prev)
3010*a9fa9459Szrj     if ((prev->flags & SEC_EXCLUDE) == 0
3011*a9fa9459Szrj 	&& !bfd_section_removed_from_list (obfd, prev))
3012*a9fa9459Szrj       break;
3013*a9fa9459Szrj 
3014*a9fa9459Szrj   /* Find following kept section.  Start at prev->next because
3015*a9fa9459Szrj      other sections may have been added after S was removed.  */
3016*a9fa9459Szrj   if (s->prev != NULL)
3017*a9fa9459Szrj     next = s->prev->next;
3018*a9fa9459Szrj   else
3019*a9fa9459Szrj     next = s->owner->sections;
3020*a9fa9459Szrj   for (; next != NULL; next = next->next)
3021*a9fa9459Szrj     if ((next->flags & SEC_EXCLUDE) == 0
3022*a9fa9459Szrj 	&& !bfd_section_removed_from_list (obfd, next))
3023*a9fa9459Szrj       break;
3024*a9fa9459Szrj 
3025*a9fa9459Szrj   /* Choose better of two sections, based on flags.  The idea
3026*a9fa9459Szrj      is to choose a section that will be in the same segment
3027*a9fa9459Szrj      as S would have been if it was kept.  */
3028*a9fa9459Szrj   best = next;
3029*a9fa9459Szrj   if (prev == NULL)
3030*a9fa9459Szrj     {
3031*a9fa9459Szrj       if (next == NULL)
3032*a9fa9459Szrj 	best = bfd_abs_section_ptr;
3033*a9fa9459Szrj     }
3034*a9fa9459Szrj   else if (next == NULL)
3035*a9fa9459Szrj     best = prev;
3036*a9fa9459Szrj   else if (((prev->flags ^ next->flags)
3037*a9fa9459Szrj 	    & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3038*a9fa9459Szrj     {
3039*a9fa9459Szrj       if (((next->flags ^ s->flags)
3040*a9fa9459Szrj 	   & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3041*a9fa9459Szrj 	  /* We prefer to choose a loaded section.  Section S
3042*a9fa9459Szrj 	     doesn't have SEC_LOAD set (it being excluded, that
3043*a9fa9459Szrj 	     part of the flag processing didn't happen) so we
3044*a9fa9459Szrj 	     can't compare that flag to those of NEXT and PREV.  */
3045*a9fa9459Szrj 	  || ((prev->flags & SEC_LOAD) != 0
3046*a9fa9459Szrj 	      && (next->flags & SEC_LOAD) == 0))
3047*a9fa9459Szrj 	best = prev;
3048*a9fa9459Szrj     }
3049*a9fa9459Szrj   else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3050*a9fa9459Szrj     {
3051*a9fa9459Szrj       if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3052*a9fa9459Szrj 	best = prev;
3053*a9fa9459Szrj     }
3054*a9fa9459Szrj   else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3055*a9fa9459Szrj     {
3056*a9fa9459Szrj       if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3057*a9fa9459Szrj 	best = prev;
3058*a9fa9459Szrj     }
3059*a9fa9459Szrj   else
3060*a9fa9459Szrj     {
3061*a9fa9459Szrj       /* Flags we care about are the same.  Prefer the following
3062*a9fa9459Szrj 	 section if that will result in a positive valued sym.  */
3063*a9fa9459Szrj       if (addr < next->vma)
3064*a9fa9459Szrj 	best = prev;
3065*a9fa9459Szrj     }
3066*a9fa9459Szrj 
3067*a9fa9459Szrj   return best;
3068*a9fa9459Szrj }
3069*a9fa9459Szrj 
3070*a9fa9459Szrj /* Convert symbols in excluded output sections to use a kept section.  */
3071*a9fa9459Szrj 
3072*a9fa9459Szrj static bfd_boolean
fix_syms(struct bfd_link_hash_entry * h,void * data)3073*a9fa9459Szrj fix_syms (struct bfd_link_hash_entry *h, void *data)
3074*a9fa9459Szrj {
3075*a9fa9459Szrj   bfd *obfd = (bfd *) data;
3076*a9fa9459Szrj 
3077*a9fa9459Szrj   if (h->type == bfd_link_hash_defined
3078*a9fa9459Szrj       || h->type == bfd_link_hash_defweak)
3079*a9fa9459Szrj     {
3080*a9fa9459Szrj       asection *s = h->u.def.section;
3081*a9fa9459Szrj       if (s != NULL
3082*a9fa9459Szrj 	  && s->output_section != NULL
3083*a9fa9459Szrj 	  && (s->output_section->flags & SEC_EXCLUDE) != 0
3084*a9fa9459Szrj 	  && bfd_section_removed_from_list (obfd, s->output_section))
3085*a9fa9459Szrj 	{
3086*a9fa9459Szrj 	  asection *op;
3087*a9fa9459Szrj 
3088*a9fa9459Szrj 	  h->u.def.value += s->output_offset + s->output_section->vma;
3089*a9fa9459Szrj 	  op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
3090*a9fa9459Szrj 	  h->u.def.value -= op->vma;
3091*a9fa9459Szrj 	  h->u.def.section = op;
3092*a9fa9459Szrj 	}
3093*a9fa9459Szrj     }
3094*a9fa9459Szrj 
3095*a9fa9459Szrj   return TRUE;
3096*a9fa9459Szrj }
3097*a9fa9459Szrj 
3098*a9fa9459Szrj void
_bfd_fix_excluded_sec_syms(bfd * obfd,struct bfd_link_info * info)3099*a9fa9459Szrj _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3100*a9fa9459Szrj {
3101*a9fa9459Szrj   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3102*a9fa9459Szrj }
3103*a9fa9459Szrj 
3104*a9fa9459Szrj /*
3105*a9fa9459Szrj FUNCTION
3106*a9fa9459Szrj 	bfd_generic_define_common_symbol
3107*a9fa9459Szrj 
3108*a9fa9459Szrj SYNOPSIS
3109*a9fa9459Szrj 	bfd_boolean bfd_generic_define_common_symbol
3110*a9fa9459Szrj 	  (bfd *output_bfd, struct bfd_link_info *info,
3111*a9fa9459Szrj 	   struct bfd_link_hash_entry *h);
3112*a9fa9459Szrj 
3113*a9fa9459Szrj DESCRIPTION
3114*a9fa9459Szrj 	Convert common symbol @var{h} into a defined symbol.
3115*a9fa9459Szrj 	Return TRUE on success and FALSE on failure.
3116*a9fa9459Szrj 
3117*a9fa9459Szrj .#define bfd_define_common_symbol(output_bfd, info, h) \
3118*a9fa9459Szrj .       BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
3119*a9fa9459Szrj .
3120*a9fa9459Szrj */
3121*a9fa9459Szrj 
3122*a9fa9459Szrj bfd_boolean
bfd_generic_define_common_symbol(bfd * output_bfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * h)3123*a9fa9459Szrj bfd_generic_define_common_symbol (bfd *output_bfd,
3124*a9fa9459Szrj 				  struct bfd_link_info *info ATTRIBUTE_UNUSED,
3125*a9fa9459Szrj 				  struct bfd_link_hash_entry *h)
3126*a9fa9459Szrj {
3127*a9fa9459Szrj   unsigned int power_of_two;
3128*a9fa9459Szrj   bfd_vma alignment, size;
3129*a9fa9459Szrj   asection *section;
3130*a9fa9459Szrj 
3131*a9fa9459Szrj   BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
3132*a9fa9459Szrj 
3133*a9fa9459Szrj   size = h->u.c.size;
3134*a9fa9459Szrj   power_of_two = h->u.c.p->alignment_power;
3135*a9fa9459Szrj   section = h->u.c.p->section;
3136*a9fa9459Szrj 
3137*a9fa9459Szrj   /* Increase the size of the section to align the common symbol.
3138*a9fa9459Szrj      The alignment must be a power of two.  */
3139*a9fa9459Szrj   alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
3140*a9fa9459Szrj   BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
3141*a9fa9459Szrj   section->size += alignment - 1;
3142*a9fa9459Szrj   section->size &= -alignment;
3143*a9fa9459Szrj 
3144*a9fa9459Szrj   /* Adjust the section's overall alignment if necessary.  */
3145*a9fa9459Szrj   if (power_of_two > section->alignment_power)
3146*a9fa9459Szrj     section->alignment_power = power_of_two;
3147*a9fa9459Szrj 
3148*a9fa9459Szrj   /* Change the symbol from common to defined.  */
3149*a9fa9459Szrj   h->type = bfd_link_hash_defined;
3150*a9fa9459Szrj   h->u.def.section = section;
3151*a9fa9459Szrj   h->u.def.value = section->size;
3152*a9fa9459Szrj 
3153*a9fa9459Szrj   /* Increase the size of the section.  */
3154*a9fa9459Szrj   section->size += size;
3155*a9fa9459Szrj 
3156*a9fa9459Szrj   /* Make sure the section is allocated in memory, and make sure that
3157*a9fa9459Szrj      it is no longer a common section.  */
3158*a9fa9459Szrj   section->flags |= SEC_ALLOC;
3159*a9fa9459Szrj   section->flags &= ~SEC_IS_COMMON;
3160*a9fa9459Szrj   return TRUE;
3161*a9fa9459Szrj }
3162*a9fa9459Szrj 
3163*a9fa9459Szrj /*
3164*a9fa9459Szrj FUNCTION
3165*a9fa9459Szrj 	bfd_find_version_for_sym
3166*a9fa9459Szrj 
3167*a9fa9459Szrj SYNOPSIS
3168*a9fa9459Szrj 	struct bfd_elf_version_tree * bfd_find_version_for_sym
3169*a9fa9459Szrj 	  (struct bfd_elf_version_tree *verdefs,
3170*a9fa9459Szrj 	   const char *sym_name, bfd_boolean *hide);
3171*a9fa9459Szrj 
3172*a9fa9459Szrj DESCRIPTION
3173*a9fa9459Szrj 	Search an elf version script tree for symbol versioning
3174*a9fa9459Szrj 	info and export / don't-export status for a given symbol.
3175*a9fa9459Szrj 	Return non-NULL on success and NULL on failure; also sets
3176*a9fa9459Szrj 	the output @samp{hide} boolean parameter.
3177*a9fa9459Szrj 
3178*a9fa9459Szrj */
3179*a9fa9459Szrj 
3180*a9fa9459Szrj struct bfd_elf_version_tree *
bfd_find_version_for_sym(struct bfd_elf_version_tree * verdefs,const char * sym_name,bfd_boolean * hide)3181*a9fa9459Szrj bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
3182*a9fa9459Szrj 			  const char *sym_name,
3183*a9fa9459Szrj 			  bfd_boolean *hide)
3184*a9fa9459Szrj {
3185*a9fa9459Szrj   struct bfd_elf_version_tree *t;
3186*a9fa9459Szrj   struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
3187*a9fa9459Szrj   struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
3188*a9fa9459Szrj 
3189*a9fa9459Szrj   local_ver = NULL;
3190*a9fa9459Szrj   global_ver = NULL;
3191*a9fa9459Szrj   star_local_ver = NULL;
3192*a9fa9459Szrj   star_global_ver = NULL;
3193*a9fa9459Szrj   exist_ver = NULL;
3194*a9fa9459Szrj   for (t = verdefs; t != NULL; t = t->next)
3195*a9fa9459Szrj     {
3196*a9fa9459Szrj       if (t->globals.list != NULL)
3197*a9fa9459Szrj 	{
3198*a9fa9459Szrj 	  struct bfd_elf_version_expr *d = NULL;
3199*a9fa9459Szrj 
3200*a9fa9459Szrj 	  while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
3201*a9fa9459Szrj 	    {
3202*a9fa9459Szrj 	      if (d->literal || strcmp (d->pattern, "*") != 0)
3203*a9fa9459Szrj 		global_ver = t;
3204*a9fa9459Szrj 	      else
3205*a9fa9459Szrj 		star_global_ver = t;
3206*a9fa9459Szrj 	      if (d->symver)
3207*a9fa9459Szrj 		exist_ver = t;
3208*a9fa9459Szrj 	      d->script = 1;
3209*a9fa9459Szrj 	      /* If the match is a wildcard pattern, keep looking for
3210*a9fa9459Szrj 		 a more explicit, perhaps even local, match.  */
3211*a9fa9459Szrj 	      if (d->literal)
3212*a9fa9459Szrj 		break;
3213*a9fa9459Szrj 	    }
3214*a9fa9459Szrj 
3215*a9fa9459Szrj 	  if (d != NULL)
3216*a9fa9459Szrj 	    break;
3217*a9fa9459Szrj 	}
3218*a9fa9459Szrj 
3219*a9fa9459Szrj       if (t->locals.list != NULL)
3220*a9fa9459Szrj 	{
3221*a9fa9459Szrj 	  struct bfd_elf_version_expr *d = NULL;
3222*a9fa9459Szrj 
3223*a9fa9459Szrj 	  while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
3224*a9fa9459Szrj 	    {
3225*a9fa9459Szrj 	      if (d->literal || strcmp (d->pattern, "*") != 0)
3226*a9fa9459Szrj 		local_ver = t;
3227*a9fa9459Szrj 	      else
3228*a9fa9459Szrj 		star_local_ver = t;
3229*a9fa9459Szrj 	      /* If the match is a wildcard pattern, keep looking for
3230*a9fa9459Szrj 		 a more explicit, perhaps even global, match.  */
3231*a9fa9459Szrj 	      if (d->literal)
3232*a9fa9459Szrj 		{
3233*a9fa9459Szrj 		  /* An exact match overrides a global wildcard.  */
3234*a9fa9459Szrj 		  global_ver = NULL;
3235*a9fa9459Szrj 		  star_global_ver = NULL;
3236*a9fa9459Szrj 		  break;
3237*a9fa9459Szrj 		}
3238*a9fa9459Szrj 	    }
3239*a9fa9459Szrj 
3240*a9fa9459Szrj 	  if (d != NULL)
3241*a9fa9459Szrj 	    break;
3242*a9fa9459Szrj 	}
3243*a9fa9459Szrj     }
3244*a9fa9459Szrj 
3245*a9fa9459Szrj   if (global_ver == NULL && local_ver == NULL)
3246*a9fa9459Szrj     global_ver = star_global_ver;
3247*a9fa9459Szrj 
3248*a9fa9459Szrj   if (global_ver != NULL)
3249*a9fa9459Szrj     {
3250*a9fa9459Szrj       /* If we already have a versioned symbol that matches the
3251*a9fa9459Szrj 	 node for this symbol, then we don't want to create a
3252*a9fa9459Szrj 	 duplicate from the unversioned symbol.  Instead hide the
3253*a9fa9459Szrj 	 unversioned symbol.  */
3254*a9fa9459Szrj       *hide = exist_ver == global_ver;
3255*a9fa9459Szrj       return global_ver;
3256*a9fa9459Szrj     }
3257*a9fa9459Szrj 
3258*a9fa9459Szrj   if (local_ver == NULL)
3259*a9fa9459Szrj     local_ver = star_local_ver;
3260*a9fa9459Szrj 
3261*a9fa9459Szrj   if (local_ver != NULL)
3262*a9fa9459Szrj     {
3263*a9fa9459Szrj       *hide = TRUE;
3264*a9fa9459Szrj       return local_ver;
3265*a9fa9459Szrj     }
3266*a9fa9459Szrj 
3267*a9fa9459Szrj   return NULL;
3268*a9fa9459Szrj }
3269*a9fa9459Szrj 
3270*a9fa9459Szrj /*
3271*a9fa9459Szrj FUNCTION
3272*a9fa9459Szrj 	bfd_hide_sym_by_version
3273*a9fa9459Szrj 
3274*a9fa9459Szrj SYNOPSIS
3275*a9fa9459Szrj 	bfd_boolean bfd_hide_sym_by_version
3276*a9fa9459Szrj 	  (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3277*a9fa9459Szrj 
3278*a9fa9459Szrj DESCRIPTION
3279*a9fa9459Szrj 	Search an elf version script tree for symbol versioning
3280*a9fa9459Szrj 	info for a given symbol.  Return TRUE if the symbol is hidden.
3281*a9fa9459Szrj 
3282*a9fa9459Szrj */
3283*a9fa9459Szrj 
3284*a9fa9459Szrj bfd_boolean
bfd_hide_sym_by_version(struct bfd_elf_version_tree * verdefs,const char * sym_name)3285*a9fa9459Szrj bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3286*a9fa9459Szrj 			 const char *sym_name)
3287*a9fa9459Szrj {
3288*a9fa9459Szrj   bfd_boolean hidden = FALSE;
3289*a9fa9459Szrj   bfd_find_version_for_sym (verdefs, sym_name, &hidden);
3290*a9fa9459Szrj   return hidden;
3291*a9fa9459Szrj }
3292*a9fa9459Szrj 
3293*a9fa9459Szrj /*
3294*a9fa9459Szrj FUNCTION
3295*a9fa9459Szrj 	bfd_link_check_relocs
3296*a9fa9459Szrj 
3297*a9fa9459Szrj SYNOPSIS
3298*a9fa9459Szrj 	bfd_boolean bfd_link_check_relocs
3299*a9fa9459Szrj 	  (bfd *abfd, struct bfd_link_info *info);
3300*a9fa9459Szrj 
3301*a9fa9459Szrj DESCRIPTION
3302*a9fa9459Szrj 	Checks the relocs in ABFD for validity.
3303*a9fa9459Szrj 	Does not execute the relocs.
3304*a9fa9459Szrj 	Return TRUE if everything is OK, FALSE otherwise.
3305*a9fa9459Szrj 	This is the external entry point to this code.
3306*a9fa9459Szrj */
3307*a9fa9459Szrj 
3308*a9fa9459Szrj bfd_boolean
bfd_link_check_relocs(bfd * abfd,struct bfd_link_info * info)3309*a9fa9459Szrj bfd_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3310*a9fa9459Szrj {
3311*a9fa9459Szrj   return BFD_SEND (abfd, _bfd_link_check_relocs, (abfd, info));
3312*a9fa9459Szrj }
3313*a9fa9459Szrj 
3314*a9fa9459Szrj /*
3315*a9fa9459Szrj FUNCTION
3316*a9fa9459Szrj 	_bfd_generic_link_check_relocs
3317*a9fa9459Szrj 
3318*a9fa9459Szrj SYNOPSIS
3319*a9fa9459Szrj 	bfd_boolean _bfd_generic_link_check_relocs
3320*a9fa9459Szrj 	  (bfd *abfd, struct bfd_link_info *info);
3321*a9fa9459Szrj 
3322*a9fa9459Szrj DESCRIPTION
3323*a9fa9459Szrj         Stub function for targets that do not implement reloc checking.
3324*a9fa9459Szrj 	Return TRUE.
3325*a9fa9459Szrj 	This is an internal function.  It should not be called from
3326*a9fa9459Szrj 	outside the BFD library.
3327*a9fa9459Szrj */
3328*a9fa9459Szrj 
3329*a9fa9459Szrj bfd_boolean
_bfd_generic_link_check_relocs(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED)3330*a9fa9459Szrj _bfd_generic_link_check_relocs (bfd *abfd ATTRIBUTE_UNUSED,
3331*a9fa9459Szrj 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
3332*a9fa9459Szrj {
3333*a9fa9459Szrj   return TRUE;
3334*a9fa9459Szrj }
3335