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