OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [bfd/] [linker.c] - Blame information for rev 859

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

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

powered by: WebSVN 2.1.0

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