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