fix record "run" regression
[external/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     row = COMMON_ROW;
1646   else
1647     row = DEF_ROW;
1648
1649   if (hashp != NULL && *hashp != NULL)
1650     h = *hashp;
1651   else
1652     {
1653       if (row == UNDEF_ROW || row == UNDEFW_ROW)
1654         h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1655       else
1656         h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
1657       if (h == NULL)
1658         {
1659           if (hashp != NULL)
1660             *hashp = NULL;
1661           return FALSE;
1662         }
1663     }
1664
1665   if (info->notice_all
1666       || (info->notice_hash != NULL
1667           && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1668     {
1669       if (! (*info->callbacks->notice) (info, h,
1670                                         abfd, section, value, flags, string))
1671         return FALSE;
1672     }
1673
1674   if (hashp != NULL)
1675     *hashp = h;
1676
1677   do
1678     {
1679       enum link_action action;
1680
1681       cycle = FALSE;
1682       action = link_action[(int) row][(int) h->type];
1683       switch (action)
1684         {
1685         case FAIL:
1686           abort ();
1687
1688         case NOACT:
1689           /* Do nothing.  */
1690           break;
1691
1692         case UND:
1693           /* Make a new undefined symbol.  */
1694           h->type = bfd_link_hash_undefined;
1695           h->u.undef.abfd = abfd;
1696           bfd_link_add_undef (info->hash, h);
1697           break;
1698
1699         case WEAK:
1700           /* Make a new weak undefined symbol.  */
1701           h->type = bfd_link_hash_undefweak;
1702           h->u.undef.abfd = abfd;
1703           break;
1704
1705         case CDEF:
1706           /* We have found a definition for a symbol which was
1707              previously common.  */
1708           BFD_ASSERT (h->type == bfd_link_hash_common);
1709           if (! ((*info->callbacks->multiple_common)
1710                  (info, h, abfd, bfd_link_hash_defined, 0)))
1711             return FALSE;
1712           /* Fall through.  */
1713         case DEF:
1714         case DEFW:
1715           {
1716             enum bfd_link_hash_type oldtype;
1717
1718             /* Define a symbol.  */
1719             oldtype = h->type;
1720             if (action == DEFW)
1721               h->type = bfd_link_hash_defweak;
1722             else
1723               h->type = bfd_link_hash_defined;
1724             h->u.def.section = section;
1725             h->u.def.value = value;
1726
1727             /* If we have been asked to, we act like collect2 and
1728                identify all functions that might be global
1729                constructors and destructors and pass them up in a
1730                callback.  We only do this for certain object file
1731                types, since many object file types can handle this
1732                automatically.  */
1733             if (collect && name[0] == '_')
1734               {
1735                 const char *s;
1736
1737                 /* A constructor or destructor name starts like this:
1738                    _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1739                    the second are the same character (we accept any
1740                    character there, in case a new object file format
1741                    comes along with even worse naming restrictions).  */
1742
1743 #define CONS_PREFIX "GLOBAL_"
1744 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1745
1746                 s = name + 1;
1747                 while (*s == '_')
1748                   ++s;
1749                 if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
1750                   {
1751                     char c;
1752
1753                     c = s[CONS_PREFIX_LEN + 1];
1754                     if ((c == 'I' || c == 'D')
1755                         && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1756                       {
1757                         /* If this is a definition of a symbol which
1758                            was previously weakly defined, we are in
1759                            trouble.  We have already added a
1760                            constructor entry for the weak defined
1761                            symbol, and now we are trying to add one
1762                            for the new symbol.  Fortunately, this case
1763                            should never arise in practice.  */
1764                         if (oldtype == bfd_link_hash_defweak)
1765                           abort ();
1766
1767                         if (! ((*info->callbacks->constructor)
1768                                (info, c == 'I',
1769                                 h->root.string, abfd, section, value)))
1770                           return FALSE;
1771                       }
1772                   }
1773               }
1774           }
1775
1776           break;
1777
1778         case COM:
1779           /* We have found a common definition for a symbol.  */
1780           if (h->type == bfd_link_hash_new)
1781             bfd_link_add_undef (info->hash, h);
1782           h->type = bfd_link_hash_common;
1783           h->u.c.p = (struct bfd_link_hash_common_entry *)
1784             bfd_hash_allocate (&info->hash->table,
1785                                sizeof (struct bfd_link_hash_common_entry));
1786           if (h->u.c.p == NULL)
1787             return FALSE;
1788
1789           h->u.c.size = value;
1790
1791           /* Select a default alignment based on the size.  This may
1792              be overridden by the caller.  */
1793           {
1794             unsigned int power;
1795
1796             power = bfd_log2 (value);
1797             if (power > 4)
1798               power = 4;
1799             h->u.c.p->alignment_power = power;
1800           }
1801
1802           /* The section of a common symbol is only used if the common
1803              symbol is actually allocated.  It basically provides a
1804              hook for the linker script to decide which output section
1805              the common symbols should be put in.  In most cases, the
1806              section of a common symbol will be bfd_com_section_ptr,
1807              the code here will choose a common symbol section named
1808              "COMMON", and the linker script will contain *(COMMON) in
1809              the appropriate place.  A few targets use separate common
1810              sections for small symbols, and they require special
1811              handling.  */
1812           if (section == bfd_com_section_ptr)
1813             {
1814               h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1815               h->u.c.p->section->flags |= SEC_ALLOC;
1816             }
1817           else if (section->owner != abfd)
1818             {
1819               h->u.c.p->section = bfd_make_section_old_way (abfd,
1820                                                             section->name);
1821               h->u.c.p->section->flags |= SEC_ALLOC;
1822             }
1823           else
1824             h->u.c.p->section = section;
1825           break;
1826
1827         case REF:
1828           /* A reference to a defined symbol.  */
1829           if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1830             h->u.undef.next = h;
1831           break;
1832
1833         case BIG:
1834           /* We have found a common definition for a symbol which
1835              already had a common definition.  Use the maximum of the
1836              two sizes, and use the section required by the larger symbol.  */
1837           BFD_ASSERT (h->type == bfd_link_hash_common);
1838           if (! ((*info->callbacks->multiple_common)
1839                  (info, h, abfd, bfd_link_hash_common, value)))
1840             return FALSE;
1841           if (value > h->u.c.size)
1842             {
1843               unsigned int power;
1844
1845               h->u.c.size = value;
1846
1847               /* Select a default alignment based on the size.  This may
1848                  be overridden by the caller.  */
1849               power = bfd_log2 (value);
1850               if (power > 4)
1851                 power = 4;
1852               h->u.c.p->alignment_power = power;
1853
1854               /* Some systems have special treatment for small commons,
1855                  hence we want to select the section used by the larger
1856                  symbol.  This makes sure the symbol does not go in a
1857                  small common section if it is now too large.  */
1858               if (section == bfd_com_section_ptr)
1859                 {
1860                   h->u.c.p->section
1861                     = bfd_make_section_old_way (abfd, "COMMON");
1862                   h->u.c.p->section->flags |= SEC_ALLOC;
1863                 }
1864               else if (section->owner != abfd)
1865                 {
1866                   h->u.c.p->section
1867                     = bfd_make_section_old_way (abfd, section->name);
1868                   h->u.c.p->section->flags |= SEC_ALLOC;
1869                 }
1870               else
1871                 h->u.c.p->section = section;
1872             }
1873           break;
1874
1875         case CREF:
1876           /* We have found a common definition for a symbol which
1877              was already defined.  */
1878           if (! ((*info->callbacks->multiple_common)
1879                  (info, h, abfd, bfd_link_hash_common, value)))
1880             return FALSE;
1881           break;
1882
1883         case MIND:
1884           /* Multiple indirect symbols.  This is OK if they both point
1885              to the same symbol.  */
1886           if (strcmp (h->u.i.link->root.string, string) == 0)
1887             break;
1888           /* Fall through.  */
1889         case MDEF:
1890           /* Handle a multiple definition.  */
1891           if (! ((*info->callbacks->multiple_definition)
1892                  (info, h, abfd, section, value)))
1893             return FALSE;
1894           break;
1895
1896         case CIND:
1897           /* Create an indirect symbol from an existing common symbol.  */
1898           BFD_ASSERT (h->type == bfd_link_hash_common);
1899           if (! ((*info->callbacks->multiple_common)
1900                  (info, h, abfd, bfd_link_hash_indirect, 0)))
1901             return FALSE;
1902           /* Fall through.  */
1903         case IND:
1904           /* Create an indirect symbol.  */
1905           {
1906             struct bfd_link_hash_entry *inh;
1907
1908             /* STRING is the name of the symbol we want to indirect
1909                to.  */
1910             inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1911                                                 copy, FALSE);
1912             if (inh == NULL)
1913               return FALSE;
1914             if (inh->type == bfd_link_hash_indirect
1915                 && inh->u.i.link == h)
1916               {
1917                 (*_bfd_error_handler)
1918                   (_("%B: indirect symbol `%s' to `%s' is a loop"),
1919                    abfd, name, string);
1920                 bfd_set_error (bfd_error_invalid_operation);
1921                 return FALSE;
1922               }
1923             if (inh->type == bfd_link_hash_new)
1924               {
1925                 inh->type = bfd_link_hash_undefined;
1926                 inh->u.undef.abfd = abfd;
1927                 bfd_link_add_undef (info->hash, inh);
1928               }
1929
1930             /* If the indirect symbol has been referenced, we need to
1931                push the reference down to the symbol we are
1932                referencing.  */
1933             if (h->type != bfd_link_hash_new)
1934               {
1935                 row = UNDEF_ROW;
1936                 cycle = TRUE;
1937               }
1938
1939             h->type = bfd_link_hash_indirect;
1940             h->u.i.link = inh;
1941           }
1942           break;
1943
1944         case SET:
1945           /* Add an entry to a set.  */
1946           if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1947                                                 abfd, section, value))
1948             return FALSE;
1949           break;
1950
1951         case WARNC:
1952           /* Issue a warning and cycle.  */
1953           if (h->u.i.warning != NULL)
1954             {
1955               if (! (*info->callbacks->warning) (info, h->u.i.warning,
1956                                                  h->root.string, abfd,
1957                                                  NULL, 0))
1958                 return FALSE;
1959               /* Only issue a warning once.  */
1960               h->u.i.warning = NULL;
1961             }
1962           /* Fall through.  */
1963         case CYCLE:
1964           /* Try again with the referenced symbol.  */
1965           h = h->u.i.link;
1966           cycle = TRUE;
1967           break;
1968
1969         case REFC:
1970           /* A reference to an indirect symbol.  */
1971           if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1972             h->u.undef.next = h;
1973           h = h->u.i.link;
1974           cycle = TRUE;
1975           break;
1976
1977         case WARN:
1978           /* Issue a warning.  */
1979           if (! (*info->callbacks->warning) (info, string, h->root.string,
1980                                              hash_entry_bfd (h), NULL, 0))
1981             return FALSE;
1982           break;
1983
1984         case CWARN:
1985           /* Warn if this symbol has been referenced already,
1986              otherwise add a warning.  A symbol has been referenced if
1987              the u.undef.next field is not NULL, or it is the tail of the
1988              undefined symbol list.  The REF case above helps to
1989              ensure this.  */
1990           if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
1991             {
1992               if (! (*info->callbacks->warning) (info, string, h->root.string,
1993                                                  hash_entry_bfd (h), NULL, 0))
1994                 return FALSE;
1995               break;
1996             }
1997           /* Fall through.  */
1998         case MWARN:
1999           /* Make a warning symbol.  */
2000           {
2001             struct bfd_link_hash_entry *sub;
2002
2003             /* STRING is the warning to give.  */
2004             sub = ((struct bfd_link_hash_entry *)
2005                    ((*info->hash->table.newfunc)
2006                     (NULL, &info->hash->table, h->root.string)));
2007             if (sub == NULL)
2008               return FALSE;
2009             *sub = *h;
2010             sub->type = bfd_link_hash_warning;
2011             sub->u.i.link = h;
2012             if (! copy)
2013               sub->u.i.warning = string;
2014             else
2015               {
2016                 char *w;
2017                 size_t len = strlen (string) + 1;
2018
2019                 w = (char *) bfd_hash_allocate (&info->hash->table, len);
2020                 if (w == NULL)
2021                   return FALSE;
2022                 memcpy (w, string, len);
2023                 sub->u.i.warning = w;
2024               }
2025
2026             bfd_hash_replace (&info->hash->table,
2027                               (struct bfd_hash_entry *) h,
2028                               (struct bfd_hash_entry *) sub);
2029             if (hashp != NULL)
2030               *hashp = sub;
2031           }
2032           break;
2033         }
2034     }
2035   while (cycle);
2036
2037   return TRUE;
2038 }
2039 \f
2040 /* Generic final link routine.  */
2041
2042 bfd_boolean
2043 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
2044 {
2045   bfd *sub;
2046   asection *o;
2047   struct bfd_link_order *p;
2048   size_t outsymalloc;
2049   struct generic_write_global_symbol_info wginfo;
2050
2051   bfd_get_outsymbols (abfd) = NULL;
2052   bfd_get_symcount (abfd) = 0;
2053   outsymalloc = 0;
2054
2055   /* Mark all sections which will be included in the output file.  */
2056   for (o = abfd->sections; o != NULL; o = o->next)
2057     for (p = o->map_head.link_order; p != NULL; p = p->next)
2058       if (p->type == bfd_indirect_link_order)
2059         p->u.indirect.section->linker_mark = TRUE;
2060
2061   /* Build the output symbol table.  */
2062   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2063     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
2064       return FALSE;
2065
2066   /* Accumulate the global symbols.  */
2067   wginfo.info = info;
2068   wginfo.output_bfd = abfd;
2069   wginfo.psymalloc = &outsymalloc;
2070   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
2071                                    _bfd_generic_link_write_global_symbol,
2072                                    &wginfo);
2073
2074   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
2075      shouldn't really need one, since we have SYMCOUNT, but some old
2076      code still expects one.  */
2077   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
2078     return FALSE;
2079
2080   if (info->relocatable)
2081     {
2082       /* Allocate space for the output relocs for each section.  */
2083       for (o = abfd->sections; o != NULL; o = o->next)
2084         {
2085           o->reloc_count = 0;
2086           for (p = o->map_head.link_order; p != NULL; p = p->next)
2087             {
2088               if (p->type == bfd_section_reloc_link_order
2089                   || p->type == bfd_symbol_reloc_link_order)
2090                 ++o->reloc_count;
2091               else if (p->type == bfd_indirect_link_order)
2092                 {
2093                   asection *input_section;
2094                   bfd *input_bfd;
2095                   long relsize;
2096                   arelent **relocs;
2097                   asymbol **symbols;
2098                   long reloc_count;
2099
2100                   input_section = p->u.indirect.section;
2101                   input_bfd = input_section->owner;
2102                   relsize = bfd_get_reloc_upper_bound (input_bfd,
2103                                                        input_section);
2104                   if (relsize < 0)
2105                     return FALSE;
2106                   relocs = (arelent **) bfd_malloc (relsize);
2107                   if (!relocs && relsize != 0)
2108                     return FALSE;
2109                   symbols = _bfd_generic_link_get_symbols (input_bfd);
2110                   reloc_count = bfd_canonicalize_reloc (input_bfd,
2111                                                         input_section,
2112                                                         relocs,
2113                                                         symbols);
2114                   free (relocs);
2115                   if (reloc_count < 0)
2116                     return FALSE;
2117                   BFD_ASSERT ((unsigned long) reloc_count
2118                               == input_section->reloc_count);
2119                   o->reloc_count += reloc_count;
2120                 }
2121             }
2122           if (o->reloc_count > 0)
2123             {
2124               bfd_size_type amt;
2125
2126               amt = o->reloc_count;
2127               amt *= sizeof (arelent *);
2128               o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
2129               if (!o->orelocation)
2130                 return FALSE;
2131               o->flags |= SEC_RELOC;
2132               /* Reset the count so that it can be used as an index
2133                  when putting in the output relocs.  */
2134               o->reloc_count = 0;
2135             }
2136         }
2137     }
2138
2139   /* Handle all the link order information for the sections.  */
2140   for (o = abfd->sections; o != NULL; o = o->next)
2141     {
2142       for (p = o->map_head.link_order; p != NULL; p = p->next)
2143         {
2144           switch (p->type)
2145             {
2146             case bfd_section_reloc_link_order:
2147             case bfd_symbol_reloc_link_order:
2148               if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2149                 return FALSE;
2150               break;
2151             case bfd_indirect_link_order:
2152               if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2153                 return FALSE;
2154               break;
2155             default:
2156               if (! _bfd_default_link_order (abfd, info, o, p))
2157                 return FALSE;
2158               break;
2159             }
2160         }
2161     }
2162
2163   return TRUE;
2164 }
2165
2166 /* Add an output symbol to the output BFD.  */
2167
2168 static bfd_boolean
2169 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
2170 {
2171   if (bfd_get_symcount (output_bfd) >= *psymalloc)
2172     {
2173       asymbol **newsyms;
2174       bfd_size_type amt;
2175
2176       if (*psymalloc == 0)
2177         *psymalloc = 124;
2178       else
2179         *psymalloc *= 2;
2180       amt = *psymalloc;
2181       amt *= sizeof (asymbol *);
2182       newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2183       if (newsyms == NULL)
2184         return FALSE;
2185       bfd_get_outsymbols (output_bfd) = newsyms;
2186     }
2187
2188   bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2189   if (sym != NULL)
2190     ++ bfd_get_symcount (output_bfd);
2191
2192   return TRUE;
2193 }
2194
2195 /* Handle the symbols for an input BFD.  */
2196
2197 bfd_boolean
2198 _bfd_generic_link_output_symbols (bfd *output_bfd,
2199                                   bfd *input_bfd,
2200                                   struct bfd_link_info *info,
2201                                   size_t *psymalloc)
2202 {
2203   asymbol **sym_ptr;
2204   asymbol **sym_end;
2205
2206   if (!bfd_generic_link_read_symbols (input_bfd))
2207     return FALSE;
2208
2209   /* Create a filename symbol if we are supposed to.  */
2210   if (info->create_object_symbols_section != NULL)
2211     {
2212       asection *sec;
2213
2214       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2215         {
2216           if (sec->output_section == info->create_object_symbols_section)
2217             {
2218               asymbol *newsym;
2219
2220               newsym = bfd_make_empty_symbol (input_bfd);
2221               if (!newsym)
2222                 return FALSE;
2223               newsym->name = input_bfd->filename;
2224               newsym->value = 0;
2225               newsym->flags = BSF_LOCAL | BSF_FILE;
2226               newsym->section = sec;
2227
2228               if (! generic_add_output_symbol (output_bfd, psymalloc,
2229                                                newsym))
2230                 return FALSE;
2231
2232               break;
2233             }
2234         }
2235     }
2236
2237   /* Adjust the values of the globally visible symbols, and write out
2238      local symbols.  */
2239   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2240   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2241   for (; sym_ptr < sym_end; sym_ptr++)
2242     {
2243       asymbol *sym;
2244       struct generic_link_hash_entry *h;
2245       bfd_boolean output;
2246
2247       h = NULL;
2248       sym = *sym_ptr;
2249       if ((sym->flags & (BSF_INDIRECT
2250                          | BSF_WARNING
2251                          | BSF_GLOBAL
2252                          | BSF_CONSTRUCTOR
2253                          | BSF_WEAK)) != 0
2254           || bfd_is_und_section (bfd_get_section (sym))
2255           || bfd_is_com_section (bfd_get_section (sym))
2256           || bfd_is_ind_section (bfd_get_section (sym)))
2257         {
2258           if (sym->udata.p != NULL)
2259             h = (struct generic_link_hash_entry *) sym->udata.p;
2260           else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2261             {
2262               /* This case normally means that the main linker code
2263                  deliberately ignored this constructor symbol.  We
2264                  should just pass it through.  This will screw up if
2265                  the constructor symbol is from a different,
2266                  non-generic, object file format, but the case will
2267                  only arise when linking with -r, which will probably
2268                  fail anyhow, since there will be no way to represent
2269                  the relocs in the output format being used.  */
2270               h = NULL;
2271             }
2272           else if (bfd_is_und_section (bfd_get_section (sym)))
2273             h = ((struct generic_link_hash_entry *)
2274                  bfd_wrapped_link_hash_lookup (output_bfd, info,
2275                                                bfd_asymbol_name (sym),
2276                                                FALSE, FALSE, TRUE));
2277           else
2278             h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2279                                                bfd_asymbol_name (sym),
2280                                                FALSE, FALSE, TRUE);
2281
2282           if (h != NULL)
2283             {
2284               /* Force all references to this symbol to point to
2285                  the same area in memory.  It is possible that
2286                  this routine will be called with a hash table
2287                  other than a generic hash table, so we double
2288                  check that.  */
2289               if (info->output_bfd->xvec == input_bfd->xvec)
2290                 {
2291                   if (h->sym != NULL)
2292                     *sym_ptr = sym = h->sym;
2293                 }
2294
2295               switch (h->root.type)
2296                 {
2297                 default:
2298                 case bfd_link_hash_new:
2299                   abort ();
2300                 case bfd_link_hash_undefined:
2301                   break;
2302                 case bfd_link_hash_undefweak:
2303                   sym->flags |= BSF_WEAK;
2304                   break;
2305                 case bfd_link_hash_indirect:
2306                   h = (struct generic_link_hash_entry *) h->root.u.i.link;
2307                   /* fall through */
2308                 case bfd_link_hash_defined:
2309                   sym->flags |= BSF_GLOBAL;
2310                   sym->flags &=~ BSF_CONSTRUCTOR;
2311                   sym->value = h->root.u.def.value;
2312                   sym->section = h->root.u.def.section;
2313                   break;
2314                 case bfd_link_hash_defweak:
2315                   sym->flags |= BSF_WEAK;
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_common:
2321                   sym->value = h->root.u.c.size;
2322                   sym->flags |= BSF_GLOBAL;
2323                   if (! bfd_is_com_section (sym->section))
2324                     {
2325                       BFD_ASSERT (bfd_is_und_section (sym->section));
2326                       sym->section = bfd_com_section_ptr;
2327                     }
2328                   /* We do not set the section of the symbol to
2329                      h->root.u.c.p->section.  That value was saved so
2330                      that we would know where to allocate the symbol
2331                      if it was defined.  In this case the type is
2332                      still bfd_link_hash_common, so we did not define
2333                      it, so we do not want to use that section.  */
2334                   break;
2335                 }
2336             }
2337         }
2338
2339       /* This switch is straight from the old code in
2340          write_file_locals in ldsym.c.  */
2341       if (info->strip == strip_all
2342           || (info->strip == strip_some
2343               && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2344                                   FALSE, FALSE) == NULL))
2345         output = FALSE;
2346       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2347         {
2348           /* If this symbol is marked as occurring now, rather
2349              than at the end, output it now.  This is used for
2350              COFF C_EXT FCN symbols.  FIXME: There must be a
2351              better way.  */
2352           if (bfd_asymbol_bfd (sym) == input_bfd
2353               && (sym->flags & BSF_NOT_AT_END) != 0)
2354             output = TRUE;
2355           else
2356             output = FALSE;
2357         }
2358       else if (bfd_is_ind_section (sym->section))
2359         output = FALSE;
2360       else if ((sym->flags & BSF_DEBUGGING) != 0)
2361         {
2362           if (info->strip == strip_none)
2363             output = TRUE;
2364           else
2365             output = FALSE;
2366         }
2367       else if (bfd_is_und_section (sym->section)
2368                || bfd_is_com_section (sym->section))
2369         output = FALSE;
2370       else if ((sym->flags & BSF_LOCAL) != 0)
2371         {
2372           if ((sym->flags & BSF_WARNING) != 0)
2373             output = FALSE;
2374           else
2375             {
2376               switch (info->discard)
2377                 {
2378                 default:
2379                 case discard_all:
2380                   output = FALSE;
2381                   break;
2382                 case discard_sec_merge:
2383                   output = TRUE;
2384                   if (info->relocatable
2385                       || ! (sym->section->flags & SEC_MERGE))
2386                     break;
2387                   /* FALLTHROUGH */
2388                 case discard_l:
2389                   if (bfd_is_local_label (input_bfd, sym))
2390                     output = FALSE;
2391                   else
2392                     output = TRUE;
2393                   break;
2394                 case discard_none:
2395                   output = TRUE;
2396                   break;
2397                 }
2398             }
2399         }
2400       else if ((sym->flags & BSF_CONSTRUCTOR))
2401         {
2402           if (info->strip != strip_all)
2403             output = TRUE;
2404           else
2405             output = FALSE;
2406         }
2407       else if (sym->flags == 0
2408                && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2409         /* LTO doesn't set symbol information.  We get here with the
2410            generic linker for a symbol that was "common" but no longer
2411            needs to be global.  */
2412         output = FALSE;
2413       else
2414         abort ();
2415
2416       /* If this symbol is in a section which is not being included
2417          in the output file, then we don't want to output the
2418          symbol.  */
2419       if (!bfd_is_abs_section (sym->section)
2420           && bfd_section_removed_from_list (output_bfd,
2421                                             sym->section->output_section))
2422         output = FALSE;
2423
2424       if (output)
2425         {
2426           if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2427             return FALSE;
2428           if (h != NULL)
2429             h->written = TRUE;
2430         }
2431     }
2432
2433   return TRUE;
2434 }
2435
2436 /* Set the section and value of a generic BFD symbol based on a linker
2437    hash table entry.  */
2438
2439 static void
2440 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2441 {
2442   switch (h->type)
2443     {
2444     default:
2445       abort ();
2446       break;
2447     case bfd_link_hash_new:
2448       /* This can happen when a constructor symbol is seen but we are
2449          not building constructors.  */
2450       if (sym->section != NULL)
2451         {
2452           BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2453         }
2454       else
2455         {
2456           sym->flags |= BSF_CONSTRUCTOR;
2457           sym->section = bfd_abs_section_ptr;
2458           sym->value = 0;
2459         }
2460       break;
2461     case bfd_link_hash_undefined:
2462       sym->section = bfd_und_section_ptr;
2463       sym->value = 0;
2464       break;
2465     case bfd_link_hash_undefweak:
2466       sym->section = bfd_und_section_ptr;
2467       sym->value = 0;
2468       sym->flags |= BSF_WEAK;
2469       break;
2470     case bfd_link_hash_defined:
2471       sym->section = h->u.def.section;
2472       sym->value = h->u.def.value;
2473       break;
2474     case bfd_link_hash_defweak:
2475       sym->flags |= BSF_WEAK;
2476       sym->section = h->u.def.section;
2477       sym->value = h->u.def.value;
2478       break;
2479     case bfd_link_hash_common:
2480       sym->value = h->u.c.size;
2481       if (sym->section == NULL)
2482         sym->section = bfd_com_section_ptr;
2483       else if (! bfd_is_com_section (sym->section))
2484         {
2485           BFD_ASSERT (bfd_is_und_section (sym->section));
2486           sym->section = bfd_com_section_ptr;
2487         }
2488       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
2489       break;
2490     case bfd_link_hash_indirect:
2491     case bfd_link_hash_warning:
2492       /* FIXME: What should we do here?  */
2493       break;
2494     }
2495 }
2496
2497 /* Write out a global symbol, if it hasn't already been written out.
2498    This is called for each symbol in the hash table.  */
2499
2500 bfd_boolean
2501 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2502                                        void *data)
2503 {
2504   struct generic_write_global_symbol_info *wginfo =
2505       (struct generic_write_global_symbol_info *) data;
2506   asymbol *sym;
2507
2508   if (h->written)
2509     return TRUE;
2510
2511   h->written = TRUE;
2512
2513   if (wginfo->info->strip == strip_all
2514       || (wginfo->info->strip == strip_some
2515           && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2516                               FALSE, FALSE) == NULL))
2517     return TRUE;
2518
2519   if (h->sym != NULL)
2520     sym = h->sym;
2521   else
2522     {
2523       sym = bfd_make_empty_symbol (wginfo->output_bfd);
2524       if (!sym)
2525         return FALSE;
2526       sym->name = h->root.root.string;
2527       sym->flags = 0;
2528     }
2529
2530   set_symbol_from_hash (sym, &h->root);
2531
2532   sym->flags |= BSF_GLOBAL;
2533
2534   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2535                                    sym))
2536     {
2537       /* FIXME: No way to return failure.  */
2538       abort ();
2539     }
2540
2541   return TRUE;
2542 }
2543
2544 /* Create a relocation.  */
2545
2546 bfd_boolean
2547 _bfd_generic_reloc_link_order (bfd *abfd,
2548                                struct bfd_link_info *info,
2549                                asection *sec,
2550                                struct bfd_link_order *link_order)
2551 {
2552   arelent *r;
2553
2554   if (! info->relocatable)
2555     abort ();
2556   if (sec->orelocation == NULL)
2557     abort ();
2558
2559   r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2560   if (r == NULL)
2561     return FALSE;
2562
2563   r->address = link_order->offset;
2564   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2565   if (r->howto == 0)
2566     {
2567       bfd_set_error (bfd_error_bad_value);
2568       return FALSE;
2569     }
2570
2571   /* Get the symbol to use for the relocation.  */
2572   if (link_order->type == bfd_section_reloc_link_order)
2573     r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2574   else
2575     {
2576       struct generic_link_hash_entry *h;
2577
2578       h = ((struct generic_link_hash_entry *)
2579            bfd_wrapped_link_hash_lookup (abfd, info,
2580                                          link_order->u.reloc.p->u.name,
2581                                          FALSE, FALSE, TRUE));
2582       if (h == NULL
2583           || ! h->written)
2584         {
2585           if (! ((*info->callbacks->unattached_reloc)
2586                  (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2587             return FALSE;
2588           bfd_set_error (bfd_error_bad_value);
2589           return FALSE;
2590         }
2591       r->sym_ptr_ptr = &h->sym;
2592     }
2593
2594   /* If this is an inplace reloc, write the addend to the object file.
2595      Otherwise, store it in the reloc addend.  */
2596   if (! r->howto->partial_inplace)
2597     r->addend = link_order->u.reloc.p->addend;
2598   else
2599     {
2600       bfd_size_type size;
2601       bfd_reloc_status_type rstat;
2602       bfd_byte *buf;
2603       bfd_boolean ok;
2604       file_ptr loc;
2605
2606       size = bfd_get_reloc_size (r->howto);
2607       buf = (bfd_byte *) bfd_zmalloc (size);
2608       if (buf == NULL)
2609         return FALSE;
2610       rstat = _bfd_relocate_contents (r->howto, abfd,
2611                                       (bfd_vma) link_order->u.reloc.p->addend,
2612                                       buf);
2613       switch (rstat)
2614         {
2615         case bfd_reloc_ok:
2616           break;
2617         default:
2618         case bfd_reloc_outofrange:
2619           abort ();
2620         case bfd_reloc_overflow:
2621           if (! ((*info->callbacks->reloc_overflow)
2622                  (info, NULL,
2623                   (link_order->type == bfd_section_reloc_link_order
2624                    ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2625                    : link_order->u.reloc.p->u.name),
2626                   r->howto->name, link_order->u.reloc.p->addend,
2627                   NULL, NULL, 0)))
2628             {
2629               free (buf);
2630               return FALSE;
2631             }
2632           break;
2633         }
2634       loc = link_order->offset * bfd_octets_per_byte (abfd);
2635       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2636       free (buf);
2637       if (! ok)
2638         return FALSE;
2639
2640       r->addend = 0;
2641     }
2642
2643   sec->orelocation[sec->reloc_count] = r;
2644   ++sec->reloc_count;
2645
2646   return TRUE;
2647 }
2648 \f
2649 /* Allocate a new link_order for a section.  */
2650
2651 struct bfd_link_order *
2652 bfd_new_link_order (bfd *abfd, asection *section)
2653 {
2654   bfd_size_type amt = sizeof (struct bfd_link_order);
2655   struct bfd_link_order *new_lo;
2656
2657   new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
2658   if (!new_lo)
2659     return NULL;
2660
2661   new_lo->type = bfd_undefined_link_order;
2662
2663   if (section->map_tail.link_order != NULL)
2664     section->map_tail.link_order->next = new_lo;
2665   else
2666     section->map_head.link_order = new_lo;
2667   section->map_tail.link_order = new_lo;
2668
2669   return new_lo;
2670 }
2671
2672 /* Default link order processing routine.  Note that we can not handle
2673    the reloc_link_order types here, since they depend upon the details
2674    of how the particular backends generates relocs.  */
2675
2676 bfd_boolean
2677 _bfd_default_link_order (bfd *abfd,
2678                          struct bfd_link_info *info,
2679                          asection *sec,
2680                          struct bfd_link_order *link_order)
2681 {
2682   switch (link_order->type)
2683     {
2684     case bfd_undefined_link_order:
2685     case bfd_section_reloc_link_order:
2686     case bfd_symbol_reloc_link_order:
2687     default:
2688       abort ();
2689     case bfd_indirect_link_order:
2690       return default_indirect_link_order (abfd, info, sec, link_order,
2691                                           FALSE);
2692     case bfd_data_link_order:
2693       return default_data_link_order (abfd, info, sec, link_order);
2694     }
2695 }
2696
2697 /* Default routine to handle a bfd_data_link_order.  */
2698
2699 static bfd_boolean
2700 default_data_link_order (bfd *abfd,
2701                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
2702                          asection *sec,
2703                          struct bfd_link_order *link_order)
2704 {
2705   bfd_size_type size;
2706   size_t fill_size;
2707   bfd_byte *fill;
2708   file_ptr loc;
2709   bfd_boolean result;
2710
2711   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2712
2713   size = link_order->size;
2714   if (size == 0)
2715     return TRUE;
2716
2717   fill = link_order->u.data.contents;
2718   fill_size = link_order->u.data.size;
2719   if (fill_size == 0)
2720     {
2721       fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
2722                                     (sec->flags & SEC_CODE) != 0);
2723       if (fill == NULL)
2724         return FALSE;
2725     }
2726   else if (fill_size < size)
2727     {
2728       bfd_byte *p;
2729       fill = (bfd_byte *) bfd_malloc (size);
2730       if (fill == NULL)
2731         return FALSE;
2732       p = fill;
2733       if (fill_size == 1)
2734         memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2735       else
2736         {
2737           do
2738             {
2739               memcpy (p, link_order->u.data.contents, fill_size);
2740               p += fill_size;
2741               size -= fill_size;
2742             }
2743           while (size >= fill_size);
2744           if (size != 0)
2745             memcpy (p, link_order->u.data.contents, (size_t) size);
2746           size = link_order->size;
2747         }
2748     }
2749
2750   loc = link_order->offset * bfd_octets_per_byte (abfd);
2751   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2752
2753   if (fill != link_order->u.data.contents)
2754     free (fill);
2755   return result;
2756 }
2757
2758 /* Default routine to handle a bfd_indirect_link_order.  */
2759
2760 static bfd_boolean
2761 default_indirect_link_order (bfd *output_bfd,
2762                              struct bfd_link_info *info,
2763                              asection *output_section,
2764                              struct bfd_link_order *link_order,
2765                              bfd_boolean generic_linker)
2766 {
2767   asection *input_section;
2768   bfd *input_bfd;
2769   bfd_byte *contents = NULL;
2770   bfd_byte *new_contents;
2771   bfd_size_type sec_size;
2772   file_ptr loc;
2773
2774   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2775
2776   input_section = link_order->u.indirect.section;
2777   input_bfd = input_section->owner;
2778   if (input_section->size == 0)
2779     return TRUE;
2780
2781   BFD_ASSERT (input_section->output_section == output_section);
2782   BFD_ASSERT (input_section->output_offset == link_order->offset);
2783   BFD_ASSERT (input_section->size == link_order->size);
2784
2785   if (info->relocatable
2786       && input_section->reloc_count > 0
2787       && output_section->orelocation == NULL)
2788     {
2789       /* Space has not been allocated for the output relocations.
2790          This can happen when we are called by a specific backend
2791          because somebody is attempting to link together different
2792          types of object files.  Handling this case correctly is
2793          difficult, and sometimes impossible.  */
2794       (*_bfd_error_handler)
2795         (_("Attempt to do relocatable link with %s input and %s output"),
2796          bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2797       bfd_set_error (bfd_error_wrong_format);
2798       return FALSE;
2799     }
2800
2801   if (! generic_linker)
2802     {
2803       asymbol **sympp;
2804       asymbol **symppend;
2805
2806       /* Get the canonical symbols.  The generic linker will always
2807          have retrieved them by this point, but we are being called by
2808          a specific linker, presumably because we are linking
2809          different types of object files together.  */
2810       if (!bfd_generic_link_read_symbols (input_bfd))
2811         return FALSE;
2812
2813       /* Since we have been called by a specific linker, rather than
2814          the generic linker, the values of the symbols will not be
2815          right.  They will be the values as seen in the input file,
2816          not the values of the final link.  We need to fix them up
2817          before we can relocate the section.  */
2818       sympp = _bfd_generic_link_get_symbols (input_bfd);
2819       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2820       for (; sympp < symppend; sympp++)
2821         {
2822           asymbol *sym;
2823           struct bfd_link_hash_entry *h;
2824
2825           sym = *sympp;
2826
2827           if ((sym->flags & (BSF_INDIRECT
2828                              | BSF_WARNING
2829                              | BSF_GLOBAL
2830                              | BSF_CONSTRUCTOR
2831                              | BSF_WEAK)) != 0
2832               || bfd_is_und_section (bfd_get_section (sym))
2833               || bfd_is_com_section (bfd_get_section (sym))
2834               || bfd_is_ind_section (bfd_get_section (sym)))
2835             {
2836               /* sym->udata may have been set by
2837                  generic_link_add_symbol_list.  */
2838               if (sym->udata.p != NULL)
2839                 h = (struct bfd_link_hash_entry *) sym->udata.p;
2840               else if (bfd_is_und_section (bfd_get_section (sym)))
2841                 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2842                                                   bfd_asymbol_name (sym),
2843                                                   FALSE, FALSE, TRUE);
2844               else
2845                 h = bfd_link_hash_lookup (info->hash,
2846                                           bfd_asymbol_name (sym),
2847                                           FALSE, FALSE, TRUE);
2848               if (h != NULL)
2849                 set_symbol_from_hash (sym, h);
2850             }
2851         }
2852     }
2853
2854   if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
2855       && input_section->size != 0)
2856     {
2857       /* Group section contents are set by bfd_elf_set_group_contents.  */
2858       if (!output_bfd->output_has_begun)
2859         {
2860           /* FIXME: This hack ensures bfd_elf_set_group_contents is called.  */
2861           if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
2862             goto error_return;
2863         }
2864       new_contents = output_section->contents;
2865       BFD_ASSERT (new_contents != NULL);
2866       BFD_ASSERT (input_section->output_offset == 0);
2867     }
2868   else
2869     {
2870       /* Get and relocate the section contents.  */
2871       sec_size = (input_section->rawsize > input_section->size
2872                   ? input_section->rawsize
2873                   : input_section->size);
2874       contents = (bfd_byte *) bfd_malloc (sec_size);
2875       if (contents == NULL && sec_size != 0)
2876         goto error_return;
2877       new_contents = (bfd_get_relocated_section_contents
2878                       (output_bfd, info, link_order, contents,
2879                        info->relocatable,
2880                        _bfd_generic_link_get_symbols (input_bfd)));
2881       if (!new_contents)
2882         goto error_return;
2883     }
2884
2885   /* Output the section contents.  */
2886   loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
2887   if (! bfd_set_section_contents (output_bfd, output_section,
2888                                   new_contents, loc, input_section->size))
2889     goto error_return;
2890
2891   if (contents != NULL)
2892     free (contents);
2893   return TRUE;
2894
2895  error_return:
2896   if (contents != NULL)
2897     free (contents);
2898   return FALSE;
2899 }
2900
2901 /* A little routine to count the number of relocs in a link_order
2902    list.  */
2903
2904 unsigned int
2905 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2906 {
2907   register unsigned int c;
2908   register struct bfd_link_order *l;
2909
2910   c = 0;
2911   for (l = link_order; l != NULL; l = l->next)
2912     {
2913       if (l->type == bfd_section_reloc_link_order
2914           || l->type == bfd_symbol_reloc_link_order)
2915         ++c;
2916     }
2917
2918   return c;
2919 }
2920
2921 /*
2922 FUNCTION
2923         bfd_link_split_section
2924
2925 SYNOPSIS
2926         bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
2927
2928 DESCRIPTION
2929         Return nonzero if @var{sec} should be split during a
2930         reloceatable or final link.
2931
2932 .#define bfd_link_split_section(abfd, sec) \
2933 .       BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2934 .
2935
2936 */
2937
2938 bfd_boolean
2939 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2940                                  asection *sec ATTRIBUTE_UNUSED)
2941 {
2942   return FALSE;
2943 }
2944
2945 /*
2946 FUNCTION
2947         bfd_section_already_linked
2948
2949 SYNOPSIS
2950         bfd_boolean bfd_section_already_linked (bfd *abfd,
2951                                                 asection *sec,
2952                                                 struct bfd_link_info *info);
2953
2954 DESCRIPTION
2955         Check if @var{data} has been already linked during a reloceatable
2956         or final link.  Return TRUE if it has.
2957
2958 .#define bfd_section_already_linked(abfd, sec, info) \
2959 .       BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2960 .
2961
2962 */
2963
2964 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
2965    once into the output.  This routine checks each section, and
2966    arrange to discard it if a section of the same name has already
2967    been linked.  This code assumes that all relevant sections have the
2968    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2969    section name.  bfd_section_already_linked is called via
2970    bfd_map_over_sections.  */
2971
2972 /* The hash table.  */
2973
2974 static struct bfd_hash_table _bfd_section_already_linked_table;
2975
2976 /* Support routines for the hash table used by section_already_linked,
2977    initialize the table, traverse, lookup, fill in an entry and remove
2978    the table.  */
2979
2980 void
2981 bfd_section_already_linked_table_traverse
2982   (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
2983                         void *), void *info)
2984 {
2985   bfd_hash_traverse (&_bfd_section_already_linked_table,
2986                      (bfd_boolean (*) (struct bfd_hash_entry *,
2987                                        void *)) func,
2988                      info);
2989 }
2990
2991 struct bfd_section_already_linked_hash_entry *
2992 bfd_section_already_linked_table_lookup (const char *name)
2993 {
2994   return ((struct bfd_section_already_linked_hash_entry *)
2995           bfd_hash_lookup (&_bfd_section_already_linked_table, name,
2996                            TRUE, FALSE));
2997 }
2998
2999 bfd_boolean
3000 bfd_section_already_linked_table_insert
3001   (struct bfd_section_already_linked_hash_entry *already_linked_list,
3002    asection *sec)
3003 {
3004   struct bfd_section_already_linked *l;
3005
3006   /* Allocate the memory from the same obstack as the hash table is
3007      kept in.  */
3008   l = (struct bfd_section_already_linked *)
3009       bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
3010   if (l == NULL)
3011     return FALSE;
3012   l->sec = sec;
3013   l->next = already_linked_list->entry;
3014   already_linked_list->entry = l;
3015   return TRUE;
3016 }
3017
3018 static struct bfd_hash_entry *
3019 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
3020                         struct bfd_hash_table *table,
3021                         const char *string ATTRIBUTE_UNUSED)
3022 {
3023   struct bfd_section_already_linked_hash_entry *ret =
3024     (struct bfd_section_already_linked_hash_entry *)
3025       bfd_hash_allocate (table, sizeof *ret);
3026
3027   if (ret == NULL)
3028     return NULL;
3029
3030   ret->entry = NULL;
3031
3032   return &ret->root;
3033 }
3034
3035 bfd_boolean
3036 bfd_section_already_linked_table_init (void)
3037 {
3038   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
3039                                 already_linked_newfunc,
3040                                 sizeof (struct bfd_section_already_linked_hash_entry),
3041                                 42);
3042 }
3043
3044 void
3045 bfd_section_already_linked_table_free (void)
3046 {
3047   bfd_hash_table_free (&_bfd_section_already_linked_table);
3048 }
3049
3050 /* Report warnings as appropriate for duplicate section SEC.
3051    Return FALSE if we decide to keep SEC after all.  */
3052
3053 bfd_boolean
3054 _bfd_handle_already_linked (asection *sec,
3055                             struct bfd_section_already_linked *l,
3056                             struct bfd_link_info *info)
3057 {
3058   switch (sec->flags & SEC_LINK_DUPLICATES)
3059     {
3060     default:
3061       abort ();
3062
3063     case SEC_LINK_DUPLICATES_DISCARD:
3064       /* If we found an LTO IR match for this comdat group on
3065          the first pass, replace it with the LTO output on the
3066          second pass.  We can't simply choose real object
3067          files over IR because the first pass may contain a
3068          mix of LTO and normal objects and we must keep the
3069          first match, be it IR or real.  */
3070       if (info->loading_lto_outputs
3071           && (l->sec->owner->flags & BFD_PLUGIN) != 0)
3072         {
3073           l->sec = sec;
3074           return FALSE;
3075         }
3076       break;
3077
3078     case SEC_LINK_DUPLICATES_ONE_ONLY:
3079       info->callbacks->einfo
3080         (_("%B: ignoring duplicate section `%A'\n"),
3081          sec->owner, sec);
3082       break;
3083
3084     case SEC_LINK_DUPLICATES_SAME_SIZE:
3085       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3086         ;
3087       else if (sec->size != l->sec->size)
3088         info->callbacks->einfo
3089           (_("%B: duplicate section `%A' has different size\n"),
3090            sec->owner, sec);
3091       break;
3092
3093     case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3094       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3095         ;
3096       else if (sec->size != l->sec->size)
3097         info->callbacks->einfo
3098           (_("%B: duplicate section `%A' has different size\n"),
3099            sec->owner, sec);
3100       else if (sec->size != 0)
3101         {
3102           bfd_byte *sec_contents, *l_sec_contents = NULL;
3103
3104           if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
3105             info->callbacks->einfo
3106               (_("%B: could not read contents of section `%A'\n"),
3107                sec->owner, sec);
3108           else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
3109                                                 &l_sec_contents))
3110             info->callbacks->einfo
3111               (_("%B: could not read contents of section `%A'\n"),
3112                l->sec->owner, l->sec);
3113           else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
3114             info->callbacks->einfo
3115               (_("%B: duplicate section `%A' has different contents\n"),
3116                sec->owner, sec);
3117
3118           if (sec_contents)
3119             free (sec_contents);
3120           if (l_sec_contents)
3121             free (l_sec_contents);
3122         }
3123       break;
3124     }
3125
3126   /* Set the output_section field so that lang_add_section
3127      does not create a lang_input_section structure for this
3128      section.  Since there might be a symbol in the section
3129      being discarded, we must retain a pointer to the section
3130      which we are really going to use.  */
3131   sec->output_section = bfd_abs_section_ptr;
3132   sec->kept_section = l->sec;
3133   return TRUE;
3134 }
3135
3136 /* This is used on non-ELF inputs.  */
3137
3138 bfd_boolean
3139 _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
3140                                      asection *sec,
3141                                      struct bfd_link_info *info)
3142 {
3143   const char *name;
3144   struct bfd_section_already_linked *l;
3145   struct bfd_section_already_linked_hash_entry *already_linked_list;
3146
3147   if ((sec->flags & SEC_LINK_ONCE) == 0)
3148     return FALSE;
3149
3150   /* The generic linker doesn't handle section groups.  */
3151   if ((sec->flags & SEC_GROUP) != 0)
3152     return FALSE;
3153
3154   /* FIXME: When doing a relocatable link, we may have trouble
3155      copying relocations in other sections that refer to local symbols
3156      in the section being discarded.  Those relocations will have to
3157      be converted somehow; as of this writing I'm not sure that any of
3158      the backends handle that correctly.
3159
3160      It is tempting to instead not discard link once sections when
3161      doing a relocatable link (technically, they should be discarded
3162      whenever we are building constructors).  However, that fails,
3163      because the linker winds up combining all the link once sections
3164      into a single large link once section, which defeats the purpose
3165      of having link once sections in the first place.  */
3166
3167   name = bfd_get_section_name (abfd, sec);
3168
3169   already_linked_list = bfd_section_already_linked_table_lookup (name);
3170
3171   l = already_linked_list->entry;
3172   if (l != NULL)
3173     {
3174       /* The section has already been linked.  See if we should
3175          issue a warning.  */
3176       return _bfd_handle_already_linked (sec, l, info);
3177     }
3178
3179   /* This is the first section with this name.  Record it.  */
3180   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
3181     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
3182   return FALSE;
3183 }
3184
3185 /* Choose a neighbouring section to S in OBFD that will be output, or
3186    the absolute section if ADDR is out of bounds of the neighbours.  */
3187
3188 asection *
3189 _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3190 {
3191   asection *next, *prev, *best;
3192
3193   /* Find preceding kept section.  */
3194   for (prev = s->prev; prev != NULL; prev = prev->prev)
3195     if ((prev->flags & SEC_EXCLUDE) == 0
3196         && !bfd_section_removed_from_list (obfd, prev))
3197       break;
3198
3199   /* Find following kept section.  Start at prev->next because
3200      other sections may have been added after S was removed.  */
3201   if (s->prev != NULL)
3202     next = s->prev->next;
3203   else
3204     next = s->owner->sections;
3205   for (; next != NULL; next = next->next)
3206     if ((next->flags & SEC_EXCLUDE) == 0
3207         && !bfd_section_removed_from_list (obfd, next))
3208       break;
3209
3210   /* Choose better of two sections, based on flags.  The idea
3211      is to choose a section that will be in the same segment
3212      as S would have been if it was kept.  */
3213   best = next;
3214   if (prev == NULL)
3215     {
3216       if (next == NULL)
3217         best = bfd_abs_section_ptr;
3218     }
3219   else if (next == NULL)
3220     best = prev;
3221   else if (((prev->flags ^ next->flags)
3222             & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3223     {
3224       if (((next->flags ^ s->flags)
3225            & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3226           /* We prefer to choose a loaded section.  Section S
3227              doesn't have SEC_LOAD set (it being excluded, that
3228              part of the flag processing didn't happen) so we
3229              can't compare that flag to those of NEXT and PREV.  */
3230           || ((prev->flags & SEC_LOAD) != 0
3231               && (next->flags & SEC_LOAD) == 0))
3232         best = prev;
3233     }
3234   else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3235     {
3236       if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3237         best = prev;
3238     }
3239   else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3240     {
3241       if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3242         best = prev;
3243     }
3244   else
3245     {
3246       /* Flags we care about are the same.  Prefer the following
3247          section if that will result in a positive valued sym.  */
3248       if (addr < next->vma)
3249         best = prev;
3250     }
3251
3252   return best;
3253 }
3254
3255 /* Convert symbols in excluded output sections to use a kept section.  */
3256
3257 static bfd_boolean
3258 fix_syms (struct bfd_link_hash_entry *h, void *data)
3259 {
3260   bfd *obfd = (bfd *) data;
3261
3262   if (h->type == bfd_link_hash_defined
3263       || h->type == bfd_link_hash_defweak)
3264     {
3265       asection *s = h->u.def.section;
3266       if (s != NULL
3267           && s->output_section != NULL
3268           && (s->output_section->flags & SEC_EXCLUDE) != 0
3269           && bfd_section_removed_from_list (obfd, s->output_section))
3270         {
3271           asection *op;
3272
3273           h->u.def.value += s->output_offset + s->output_section->vma;
3274           op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
3275           h->u.def.value -= op->vma;
3276           h->u.def.section = op;
3277         }
3278     }
3279
3280   return TRUE;
3281 }
3282
3283 void
3284 _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3285 {
3286   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3287 }
3288
3289 /*
3290 FUNCTION
3291         bfd_generic_define_common_symbol
3292
3293 SYNOPSIS
3294         bfd_boolean bfd_generic_define_common_symbol
3295           (bfd *output_bfd, struct bfd_link_info *info,
3296            struct bfd_link_hash_entry *h);
3297
3298 DESCRIPTION
3299         Convert common symbol @var{h} into a defined symbol.
3300         Return TRUE on success and FALSE on failure.
3301
3302 .#define bfd_define_common_symbol(output_bfd, info, h) \
3303 .       BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
3304 .
3305 */
3306
3307 bfd_boolean
3308 bfd_generic_define_common_symbol (bfd *output_bfd,
3309                                   struct bfd_link_info *info ATTRIBUTE_UNUSED,
3310                                   struct bfd_link_hash_entry *h)
3311 {
3312   unsigned int power_of_two;
3313   bfd_vma alignment, size;
3314   asection *section;
3315
3316   BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
3317
3318   size = h->u.c.size;
3319   power_of_two = h->u.c.p->alignment_power;
3320   section = h->u.c.p->section;
3321
3322   /* Increase the size of the section to align the common symbol.
3323      The alignment must be a power of two.  */
3324   alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
3325   BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
3326   section->size += alignment - 1;
3327   section->size &= -alignment;
3328
3329   /* Adjust the section's overall alignment if necessary.  */
3330   if (power_of_two > section->alignment_power)
3331     section->alignment_power = power_of_two;
3332
3333   /* Change the symbol from common to defined.  */
3334   h->type = bfd_link_hash_defined;
3335   h->u.def.section = section;
3336   h->u.def.value = section->size;
3337
3338   /* Increase the size of the section.  */
3339   section->size += size;
3340
3341   /* Make sure the section is allocated in memory, and make sure that
3342      it is no longer a common section.  */
3343   section->flags |= SEC_ALLOC;
3344   section->flags &= ~SEC_IS_COMMON;
3345   return TRUE;
3346 }
3347
3348 /*
3349 FUNCTION
3350         bfd_find_version_for_sym
3351
3352 SYNOPSIS
3353         struct bfd_elf_version_tree * bfd_find_version_for_sym
3354           (struct bfd_elf_version_tree *verdefs,
3355            const char *sym_name, bfd_boolean *hide);
3356
3357 DESCRIPTION
3358         Search an elf version script tree for symbol versioning
3359         info and export / don't-export status for a given symbol.
3360         Return non-NULL on success and NULL on failure; also sets
3361         the output @samp{hide} boolean parameter.
3362
3363 */
3364
3365 struct bfd_elf_version_tree *
3366 bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
3367                           const char *sym_name,
3368                           bfd_boolean *hide)
3369 {
3370   struct bfd_elf_version_tree *t;
3371   struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
3372   struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
3373
3374   local_ver = NULL;
3375   global_ver = NULL;
3376   star_local_ver = NULL;
3377   star_global_ver = NULL;
3378   exist_ver = NULL;
3379   for (t = verdefs; t != NULL; t = t->next)
3380     {
3381       if (t->globals.list != NULL)
3382         {
3383           struct bfd_elf_version_expr *d = NULL;
3384
3385           while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
3386             {
3387               if (d->literal || strcmp (d->pattern, "*") != 0)
3388                 global_ver = t;
3389               else
3390                 star_global_ver = t;
3391               if (d->symver)
3392                 exist_ver = t;
3393               d->script = 1;
3394               /* If the match is a wildcard pattern, keep looking for
3395                  a more explicit, perhaps even local, match.  */
3396               if (d->literal)
3397                 break;
3398             }
3399
3400           if (d != NULL)
3401             break;
3402         }
3403
3404       if (t->locals.list != NULL)
3405         {
3406           struct bfd_elf_version_expr *d = NULL;
3407
3408           while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
3409             {
3410               if (d->literal || strcmp (d->pattern, "*") != 0)
3411                 local_ver = t;
3412               else
3413                 star_local_ver = t;
3414               /* If the match is a wildcard pattern, keep looking for
3415                  a more explicit, perhaps even global, match.  */
3416               if (d->literal)
3417                 {
3418                   /* An exact match overrides a global wildcard.  */
3419                   global_ver = NULL;
3420                   star_global_ver = NULL;
3421                   break;
3422                 }
3423             }
3424
3425           if (d != NULL)
3426             break;
3427         }
3428     }
3429
3430   if (global_ver == NULL && local_ver == NULL)
3431     global_ver = star_global_ver;
3432
3433   if (global_ver != NULL)
3434     {
3435       /* If we already have a versioned symbol that matches the
3436          node for this symbol, then we don't want to create a
3437          duplicate from the unversioned symbol.  Instead hide the
3438          unversioned symbol.  */
3439       *hide = exist_ver == global_ver;
3440       return global_ver;
3441     }
3442
3443   if (local_ver == NULL)
3444     local_ver = star_local_ver;
3445
3446   if (local_ver != NULL)
3447     {
3448       *hide = TRUE;
3449       return local_ver;
3450     }
3451
3452   return NULL;
3453 }
3454
3455 /*
3456 FUNCTION
3457         bfd_hide_sym_by_version
3458
3459 SYNOPSIS
3460         bfd_boolean bfd_hide_sym_by_version
3461           (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3462
3463 DESCRIPTION
3464         Search an elf version script tree for symbol versioning
3465         info for a given symbol.  Return TRUE if the symbol is hidden.
3466
3467 */
3468
3469 bfd_boolean
3470 bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3471                          const char *sym_name)
3472 {
3473   bfd_boolean hidden = FALSE;
3474   bfd_find_version_for_sym (verdefs, sym_name, &hidden);
3475   return hidden;
3476 }