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