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