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