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