OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [bfd/] [linker.c] - Blame information for rev 302

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.