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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [bfd/] [linker.c] - Blame information for rev 438

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

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

powered by: WebSVN 2.1.0

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