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