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