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