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