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