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