Coverage Report

Created: 2026-03-10 08:46

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