Coverage Report

Created: 2023-08-28 06:26

/src/binutils-gdb/bfd/elflink.c
Line
Count
Source (jump to first uncovered line)
1
/* ELF linking support for BFD.
2
   Copyright (C) 1995-2023 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "bfdlink.h"
24
#include "libbfd.h"
25
#define ARCH_SIZE 0
26
#include "elf-bfd.h"
27
#include "safe-ctype.h"
28
#include "libiberty.h"
29
#include "objalloc.h"
30
#if BFD_SUPPORTS_PLUGINS
31
#include "plugin-api.h"
32
#include "plugin.h"
33
#endif
34
35
#include <limits.h>
36
#ifndef CHAR_BIT
37
#define CHAR_BIT 8
38
#endif
39
40
/* This struct is used to pass information to routines called via
41
   elf_link_hash_traverse which must return failure.  */
42
43
struct elf_info_failed
44
{
45
  struct bfd_link_info *info;
46
  bool failed;
47
};
48
49
/* This structure is used to pass information to
50
   _bfd_elf_link_find_version_dependencies.  */
51
52
struct elf_find_verdep_info
53
{
54
  /* General link information.  */
55
  struct bfd_link_info *info;
56
  /* The number of dependencies.  */
57
  unsigned int vers;
58
  /* Whether we had a failure.  */
59
  bool failed;
60
};
61
62
static bool _bfd_elf_fix_symbol_flags
63
  (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65
asection *
66
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67
           unsigned long r_symndx,
68
           bool discard)
69
0
{
70
0
  if (r_symndx >= cookie->locsymcount
71
0
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72
0
    {
73
0
      struct elf_link_hash_entry *h;
74
75
0
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77
0
      while (h->root.type == bfd_link_hash_indirect
78
0
       || h->root.type == bfd_link_hash_warning)
79
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81
0
      if ((h->root.type == bfd_link_hash_defined
82
0
     || h->root.type == bfd_link_hash_defweak)
83
0
     && discarded_section (h->root.u.def.section))
84
0
  return h->root.u.def.section;
85
0
      else
86
0
  return NULL;
87
0
    }
88
0
  else
89
0
    {
90
      /* It's not a relocation against a global symbol,
91
   but it could be a relocation against a local
92
   symbol for a discarded section.  */
93
0
      asection *isec;
94
0
      Elf_Internal_Sym *isym;
95
96
      /* Need to: get the symbol; get the section.  */
97
0
      isym = &cookie->locsyms[r_symndx];
98
0
      isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99
0
      if (isec != NULL
100
0
    && discard ? discarded_section (isec) : 1)
101
0
  return isec;
102
0
     }
103
0
  return NULL;
104
0
}
105
106
/* Define a symbol in a dynamic linkage section.  */
107
108
struct elf_link_hash_entry *
109
_bfd_elf_define_linkage_sym (bfd *abfd,
110
           struct bfd_link_info *info,
111
           asection *sec,
112
           const char *name)
113
0
{
114
0
  struct elf_link_hash_entry *h;
115
0
  struct bfd_link_hash_entry *bh;
116
0
  const struct elf_backend_data *bed;
117
118
0
  h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119
0
  if (h != NULL)
120
0
    {
121
      /* Zap symbol defined in an as-needed lib that wasn't linked.
122
   This is a symptom of a larger problem:  Absolute symbols
123
   defined in shared libraries can't be overridden, because we
124
   lose the link to the bfd which is via the symbol section.  */
125
0
      h->root.type = bfd_link_hash_new;
126
0
      bh = &h->root;
127
0
    }
128
0
  else
129
0
    bh = NULL;
130
131
0
  bed = get_elf_backend_data (abfd);
132
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133
0
           sec, 0, NULL, false, bed->collect,
134
0
           &bh))
135
0
    return NULL;
136
0
  h = (struct elf_link_hash_entry *) bh;
137
0
  BFD_ASSERT (h != NULL);
138
0
  h->def_regular = 1;
139
0
  h->non_elf = 0;
140
0
  h->root.linker_def = 1;
141
0
  h->type = STT_OBJECT;
142
0
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143
0
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
146
0
  return h;
147
0
}
148
149
bool
150
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151
0
{
152
0
  flagword flags;
153
0
  asection *s;
154
0
  struct elf_link_hash_entry *h;
155
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
157
158
  /* This function may be called more than once.  */
159
0
  if (htab->sgot != NULL)
160
0
    return true;
161
162
0
  flags = bed->dynamic_sec_flags;
163
164
0
  s = bfd_make_section_anyway_with_flags (abfd,
165
0
            (bed->rela_plts_and_copies_p
166
0
             ? ".rela.got" : ".rel.got"),
167
0
            (bed->dynamic_sec_flags
168
0
             | SEC_READONLY));
169
0
  if (s == NULL
170
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
171
0
    return false;
172
0
  htab->srelgot = s;
173
174
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175
0
  if (s == NULL
176
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
177
0
    return false;
178
0
  htab->sgot = s;
179
180
0
  if (bed->want_got_plt)
181
0
    {
182
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183
0
      if (s == NULL
184
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
185
0
  return false;
186
0
      htab->sgotplt = s;
187
0
    }
188
189
  /* The first bit of the global offset table is the header.  */
190
0
  s->size += bed->got_header_size;
191
192
0
  if (bed->want_got_sym)
193
0
    {
194
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195
   (or .got.plt) section.  We don't do this in the linker script
196
   because we don't want to define the symbol if we are not creating
197
   a global offset table.  */
198
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
199
0
               "_GLOBAL_OFFSET_TABLE_");
200
0
      elf_hash_table (info)->hgot = h;
201
0
      if (h == NULL)
202
0
  return false;
203
0
    }
204
205
0
  return true;
206
0
}
207

208
/* Create a strtab to hold the dynamic symbol names.  */
209
static bool
210
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211
0
{
212
0
  struct elf_link_hash_table *hash_table;
213
214
0
  hash_table = elf_hash_table (info);
215
0
  if (hash_table->dynobj == NULL)
216
0
    {
217
      /* We may not set dynobj, an input file holding linker created
218
   dynamic sections to abfd, which may be a dynamic object with
219
   its own dynamic sections.  We need to find a normal input file
220
   to hold linker created sections if possible.  */
221
0
      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222
0
  {
223
0
    bfd *ibfd;
224
0
    asection *s;
225
0
    for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226
0
      if ((ibfd->flags
227
0
     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228
0
    && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229
0
    && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230
0
    && !((s = ibfd->sections) != NULL
231
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232
0
        {
233
0
    abfd = ibfd;
234
0
    break;
235
0
        }
236
0
  }
237
0
      hash_table->dynobj = abfd;
238
0
    }
239
240
0
  if (hash_table->dynstr == NULL)
241
0
    {
242
0
      hash_table->dynstr = _bfd_elf_strtab_init ();
243
0
      if (hash_table->dynstr == NULL)
244
0
  return false;
245
0
    }
246
0
  return true;
247
0
}
248
249
/* Create some sections which will be filled in with dynamic linking
250
   information.  ABFD is an input file which requires dynamic sections
251
   to be created.  The dynamic sections take up virtual memory space
252
   when the final executable is run, so we need to create them before
253
   addresses are assigned to the output sections.  We work out the
254
   actual contents and size of these sections later.  */
255
256
bool
257
_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258
0
{
259
0
  flagword flags;
260
0
  asection *s;
261
0
  const struct elf_backend_data *bed;
262
0
  struct elf_link_hash_entry *h;
263
264
0
  if (! is_elf_hash_table (info->hash))
265
0
    return false;
266
267
0
  if (elf_hash_table (info)->dynamic_sections_created)
268
0
    return true;
269
270
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271
0
    return false;
272
273
0
  abfd = elf_hash_table (info)->dynobj;
274
0
  bed = get_elf_backend_data (abfd);
275
276
0
  flags = bed->dynamic_sec_flags;
277
278
  /* A dynamically linked executable has a .interp section, but a
279
     shared library does not.  */
280
0
  if (bfd_link_executable (info) && !info->nointerp)
281
0
    {
282
0
      s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283
0
                flags | SEC_READONLY);
284
0
      if (s == NULL)
285
0
  return false;
286
0
    }
287
288
  /* Create sections to hold version informations.  These are removed
289
     if they are not needed.  */
290
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291
0
            flags | SEC_READONLY);
292
0
  if (s == NULL
293
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
294
0
    return false;
295
296
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297
0
            flags | SEC_READONLY);
298
0
  if (s == NULL
299
0
      || !bfd_set_section_alignment (s, 1))
300
0
    return false;
301
302
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303
0
            flags | SEC_READONLY);
304
0
  if (s == NULL
305
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
306
0
    return false;
307
308
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309
0
            flags | SEC_READONLY);
310
0
  if (s == NULL
311
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
312
0
    return false;
313
0
  elf_hash_table (info)->dynsym = s;
314
315
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316
0
            flags | SEC_READONLY);
317
0
  if (s == NULL)
318
0
    return false;
319
320
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321
0
  if (s == NULL
322
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
323
0
    return false;
324
325
  /* The special symbol _DYNAMIC is always set to the start of the
326
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
327
     only want to define it if we are, in fact, creating a .dynamic
328
     section.  We don't want to define it if there is no .dynamic
329
     section, since on some ELF platforms the start up code examines it
330
     to decide how to initialize the process.  */
331
0
  h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332
0
  elf_hash_table (info)->hdynamic = h;
333
0
  if (h == NULL)
334
0
    return false;
335
336
0
  if (info->emit_hash)
337
0
    {
338
0
      s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339
0
                flags | SEC_READONLY);
340
0
      if (s == NULL
341
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
342
0
  return false;
343
0
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344
0
    }
345
346
0
  if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347
0
    {
348
0
      s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349
0
                flags | SEC_READONLY);
350
0
      if (s == NULL
351
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
352
0
  return false;
353
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354
   4 32-bit words followed by variable count of 64-bit words, then
355
   variable count of 32-bit words.  */
356
0
      if (bed->s->arch_size == 64)
357
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
358
0
      else
359
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
360
0
    }
361
362
0
  if (info->enable_dt_relr)
363
0
    {
364
0
      s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365
0
                (bed->dynamic_sec_flags
366
0
                 | SEC_READONLY));
367
0
      if (s == NULL
368
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
369
0
  return false;
370
0
      elf_hash_table (info)->srelrdyn = s;
371
0
    }
372
373
  /* Let the backend create the rest of the sections.  This lets the
374
     backend set the right flags.  The backend will normally create
375
     the .got and .plt sections.  */
376
0
  if (bed->elf_backend_create_dynamic_sections == NULL
377
0
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378
0
    return false;
379
380
0
  elf_hash_table (info)->dynamic_sections_created = true;
381
382
0
  return true;
383
0
}
384
385
/* Create dynamic sections when linking against a dynamic object.  */
386
387
bool
388
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389
0
{
390
0
  flagword flags, pltflags;
391
0
  struct elf_link_hash_entry *h;
392
0
  asection *s;
393
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
395
396
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397
     .rel[a].bss sections.  */
398
0
  flags = bed->dynamic_sec_flags;
399
400
0
  pltflags = flags;
401
0
  if (bed->plt_not_loaded)
402
    /* We do not clear SEC_ALLOC here because we still want the OS to
403
       allocate space for the section; it's just that there's nothing
404
       to read in from the object file.  */
405
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406
0
  else
407
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408
0
  if (bed->plt_readonly)
409
0
    pltflags |= SEC_READONLY;
410
411
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412
0
  if (s == NULL
413
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
414
0
    return false;
415
0
  htab->splt = s;
416
417
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418
     .plt section.  */
419
0
  if (bed->want_plt_sym)
420
0
    {
421
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
422
0
               "_PROCEDURE_LINKAGE_TABLE_");
423
0
      elf_hash_table (info)->hplt = h;
424
0
      if (h == NULL)
425
0
  return false;
426
0
    }
427
428
0
  s = bfd_make_section_anyway_with_flags (abfd,
429
0
            (bed->rela_plts_and_copies_p
430
0
             ? ".rela.plt" : ".rel.plt"),
431
0
            flags | SEC_READONLY);
432
0
  if (s == NULL
433
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
434
0
    return false;
435
0
  htab->srelplt = s;
436
437
0
  if (! _bfd_elf_create_got_section (abfd, info))
438
0
    return false;
439
440
0
  if (bed->want_dynbss)
441
0
    {
442
      /* The .dynbss section is a place to put symbols which are defined
443
   by dynamic objects, are referenced by regular objects, and are
444
   not functions.  We must allocate space for them in the process
445
   image and use a R_*_COPY reloc to tell the dynamic linker to
446
   initialize them at run time.  The linker script puts the .dynbss
447
   section into the .bss section of the final image.  */
448
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449
0
                SEC_ALLOC | SEC_LINKER_CREATED);
450
0
      if (s == NULL)
451
0
  return false;
452
0
      htab->sdynbss = s;
453
454
0
      if (bed->want_dynrelro)
455
0
  {
456
    /* Similarly, but for symbols that were originally in read-only
457
       sections.  This section doesn't really need to have contents,
458
       but make it like other .data.rel.ro sections.  */
459
0
    s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460
0
              flags);
461
0
    if (s == NULL)
462
0
      return false;
463
0
    htab->sdynrelro = s;
464
0
  }
465
466
      /* The .rel[a].bss section holds copy relocs.  This section is not
467
   normally needed.  We need to create it here, though, so that the
468
   linker will map it to an output section.  We can't just create it
469
   only if we need it, because we will not know whether we need it
470
   until we have seen all the input files, and the first time the
471
   main linker code calls BFD after examining all the input files
472
   (size_dynamic_sections) the input sections have already been
473
   mapped to the output sections.  If the section turns out not to
474
   be needed, we can discard it later.  We will never need this
475
   section when generating a shared object, since they do not use
476
   copy relocs.  */
477
0
      if (bfd_link_executable (info))
478
0
  {
479
0
    s = bfd_make_section_anyway_with_flags (abfd,
480
0
              (bed->rela_plts_and_copies_p
481
0
               ? ".rela.bss" : ".rel.bss"),
482
0
              flags | SEC_READONLY);
483
0
    if (s == NULL
484
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
485
0
      return false;
486
0
    htab->srelbss = s;
487
488
0
    if (bed->want_dynrelro)
489
0
      {
490
0
        s = (bfd_make_section_anyway_with_flags
491
0
       (abfd, (bed->rela_plts_and_copies_p
492
0
         ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493
0
        flags | SEC_READONLY));
494
0
        if (s == NULL
495
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
496
0
    return false;
497
0
        htab->sreldynrelro = s;
498
0
      }
499
0
  }
500
0
    }
501
502
0
  return true;
503
0
}
504

505
/* Record a new dynamic symbol.  We record the dynamic symbols as we
506
   read the input files, since we need to have a list of all of them
507
   before we can determine the final sizes of the output sections.
508
   Note that we may actually call this function even though we are not
509
   going to output any dynamic symbols; in some cases we know that a
510
   symbol should be in the dynamic symbol table, but only if there is
511
   one.  */
512
513
bool
514
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515
            struct elf_link_hash_entry *h)
516
0
{
517
0
  if (h->dynindx == -1)
518
0
    {
519
0
      struct elf_strtab_hash *dynstr;
520
0
      char *p;
521
0
      const char *name;
522
0
      size_t indx;
523
524
0
      if (h->root.type == bfd_link_hash_defined
525
0
    || h->root.type == bfd_link_hash_defweak)
526
0
  {
527
    /* An IR symbol should not be made dynamic.  */
528
0
    if (h->root.u.def.section != NULL
529
0
        && h->root.u.def.section->owner != NULL
530
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531
0
      return true;
532
0
  }
533
534
      /* XXX: The ABI draft says the linker must turn hidden and
535
   internal symbols into STB_LOCAL symbols when producing the
536
   DSO. However, if ld.so honors st_other in the dynamic table,
537
   this would not be necessary.  */
538
0
      switch (ELF_ST_VISIBILITY (h->other))
539
0
  {
540
0
  case STV_INTERNAL:
541
0
  case STV_HIDDEN:
542
0
    if (h->root.type != bfd_link_hash_undefined
543
0
        && h->root.type != bfd_link_hash_undefweak)
544
0
      {
545
0
        h->forced_local = 1;
546
0
        if (!elf_hash_table (info)->is_relocatable_executable
547
0
      || ((h->root.type == bfd_link_hash_defined
548
0
           || h->root.type == bfd_link_hash_defweak)
549
0
          && h->root.u.def.section->owner != NULL
550
0
          && h->root.u.def.section->owner->no_export)
551
0
      || (h->root.type == bfd_link_hash_common
552
0
          && h->root.u.c.p->section->owner != NULL
553
0
          && h->root.u.c.p->section->owner->no_export))
554
0
    return true;
555
0
      }
556
557
0
  default:
558
0
    break;
559
0
  }
560
561
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
562
0
      ++elf_hash_table (info)->dynsymcount;
563
564
0
      dynstr = elf_hash_table (info)->dynstr;
565
0
      if (dynstr == NULL)
566
0
  {
567
    /* Create a strtab to hold the dynamic symbol names.  */
568
0
    elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569
0
    if (dynstr == NULL)
570
0
      return false;
571
0
  }
572
573
      /* We don't put any version information in the dynamic string
574
   table.  */
575
0
      name = h->root.root.string;
576
0
      p = strchr (name, ELF_VER_CHR);
577
0
      if (p != NULL)
578
  /* We know that the p points into writable memory.  In fact,
579
     there are only a few symbols that have read-only names, being
580
     those like _GLOBAL_OFFSET_TABLE_ that are created specially
581
     by the backends.  Most symbols will have names pointing into
582
     an ELF string table read from a file, or to objalloc memory.  */
583
0
  *p = 0;
584
585
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587
0
      if (p != NULL)
588
0
  *p = ELF_VER_CHR;
589
590
0
      if (indx == (size_t) -1)
591
0
  return false;
592
0
      h->dynstr_index = indx;
593
0
    }
594
595
0
  return true;
596
0
}
597

598
/* Mark a symbol dynamic.  */
599
600
static void
601
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602
          struct elf_link_hash_entry *h,
603
          Elf_Internal_Sym *sym)
604
0
{
605
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607
  /* It may be called more than once on the same H.  */
608
0
  if(h->dynamic || bfd_link_relocatable (info))
609
0
    return;
610
611
0
  if ((info->dynamic_data
612
0
       && (h->type == STT_OBJECT
613
0
     || h->type == STT_COMMON
614
0
     || (sym != NULL
615
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617
0
      || (d != NULL
618
0
    && h->non_elf
619
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
620
0
    {
621
0
      h->dynamic = 1;
622
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
623
   non-IR reference.  */
624
0
      h->root.non_ir_ref_dynamic = 1;
625
0
    }
626
0
}
627
628
/* Record an assignment to a symbol made by a linker script.  We need
629
   this in case some dynamic object refers to this symbol.  */
630
631
bool
632
bfd_elf_record_link_assignment (bfd *output_bfd,
633
        struct bfd_link_info *info,
634
        const char *name,
635
        bool provide,
636
        bool hidden)
637
0
{
638
0
  struct elf_link_hash_entry *h, *hv;
639
0
  struct elf_link_hash_table *htab;
640
0
  const struct elf_backend_data *bed;
641
642
0
  if (!is_elf_hash_table (info->hash))
643
0
    return true;
644
645
0
  htab = elf_hash_table (info);
646
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
647
0
  if (h == NULL)
648
0
    return provide;
649
650
0
  if (h->root.type == bfd_link_hash_warning)
651
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653
0
  if (h->versioned == unknown)
654
0
    {
655
      /* Set versioned if symbol version is unknown.  */
656
0
      char *version = strrchr (name, ELF_VER_CHR);
657
0
      if (version)
658
0
  {
659
0
    if (version > name && version[-1] != ELF_VER_CHR)
660
0
      h->versioned = versioned_hidden;
661
0
    else
662
0
      h->versioned = versioned;
663
0
  }
664
0
    }
665
666
  /* Symbols defined in a linker script but not referenced anywhere
667
     else will have non_elf set.  */
668
0
  if (h->non_elf)
669
0
    {
670
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671
0
      h->non_elf = 0;
672
0
    }
673
674
0
  switch (h->root.type)
675
0
    {
676
0
    case bfd_link_hash_defined:
677
0
    case bfd_link_hash_defweak:
678
0
    case bfd_link_hash_common:
679
0
      break;
680
0
    case bfd_link_hash_undefweak:
681
0
    case bfd_link_hash_undefined:
682
      /* Since we're defining the symbol, don't let it seem to have not
683
   been defined.  record_dynamic_symbol and size_dynamic_sections
684
   may depend on this.  */
685
0
      h->root.type = bfd_link_hash_new;
686
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687
0
  bfd_link_repair_undef_list (&htab->root);
688
0
      break;
689
0
    case bfd_link_hash_new:
690
0
      break;
691
0
    case bfd_link_hash_indirect:
692
      /* We had a versioned symbol in a dynamic library.  We make the
693
   the versioned symbol point to this one.  */
694
0
      bed = get_elf_backend_data (output_bfd);
695
0
      hv = h;
696
0
      while (hv->root.type == bfd_link_hash_indirect
697
0
       || hv->root.type == bfd_link_hash_warning)
698
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699
      /* We don't need to update h->root.u since linker will set them
700
   later.  */
701
0
      h->root.type = bfd_link_hash_undefined;
702
0
      hv->root.type = bfd_link_hash_indirect;
703
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705
0
      break;
706
0
    default:
707
0
      BFD_FAIL ();
708
0
      return false;
709
0
    }
710
711
  /* If this symbol is being provided by the linker script, and it is
712
     currently defined by a dynamic object, but not by a regular
713
     object, then mark it as undefined so that the generic linker will
714
     force the correct value.  */
715
0
  if (provide
716
0
      && h->def_dynamic
717
0
      && !h->def_regular)
718
0
    h->root.type = bfd_link_hash_undefined;
719
720
  /* If this symbol is currently defined by a dynamic object, but not
721
     by a regular object, then clear out any version information because
722
     the symbol will not be associated with the dynamic object any
723
     more.  */
724
0
  if (h->def_dynamic && !h->def_regular)
725
0
    h->verinfo.verdef = NULL;
726
727
  /* Make sure this symbol is not garbage collected.  */
728
0
  h->mark = 1;
729
730
0
  h->def_regular = 1;
731
732
0
  if (hidden)
733
0
    {
734
0
      bed = get_elf_backend_data (output_bfd);
735
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
738
0
    }
739
740
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741
     and executables.  */
742
0
  if (!bfd_link_relocatable (info)
743
0
      && h->dynindx != -1
744
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746
0
    h->forced_local = 1;
747
748
0
  if ((h->def_dynamic
749
0
       || h->ref_dynamic
750
0
       || bfd_link_dll (info)
751
0
       || elf_hash_table (info)->is_relocatable_executable)
752
0
      && !h->forced_local
753
0
      && h->dynindx == -1)
754
0
    {
755
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
756
0
  return false;
757
758
      /* If this is a weak defined symbol, and we know a corresponding
759
   real symbol from the same dynamic object, make sure the real
760
   symbol is also made into a dynamic symbol.  */
761
0
      if (h->is_weakalias)
762
0
  {
763
0
    struct elf_link_hash_entry *def = weakdef (h);
764
765
0
    if (def->dynindx == -1
766
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
767
0
      return false;
768
0
  }
769
0
    }
770
771
0
  return true;
772
0
}
773
774
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
775
   success, and 2 on a failure caused by attempting to record a symbol
776
   in a discarded section, eg. a discarded link-once section symbol.  */
777
778
int
779
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780
            bfd *input_bfd,
781
            long input_indx)
782
0
{
783
0
  size_t amt;
784
0
  struct elf_link_local_dynamic_entry *entry;
785
0
  struct elf_link_hash_table *eht;
786
0
  struct elf_strtab_hash *dynstr;
787
0
  size_t dynstr_index;
788
0
  char *name;
789
0
  Elf_External_Sym_Shndx eshndx;
790
0
  char esym[sizeof (Elf64_External_Sym)];
791
792
0
  if (! is_elf_hash_table (info->hash))
793
0
    return 0;
794
795
  /* See if the entry exists already.  */
796
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798
0
      return 1;
799
800
0
  amt = sizeof (*entry);
801
0
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802
0
  if (entry == NULL)
803
0
    return 0;
804
805
  /* Go find the symbol, so that we can find it's name.  */
806
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807
0
           1, input_indx, &entry->isym, esym, &eshndx))
808
0
    {
809
0
      bfd_release (input_bfd, entry);
810
0
      return 0;
811
0
    }
812
813
0
  if (entry->isym.st_shndx != SHN_UNDEF
814
0
      && entry->isym.st_shndx < SHN_LORESERVE)
815
0
    {
816
0
      asection *s;
817
818
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
820
0
  {
821
    /* We can still bfd_release here as nothing has done another
822
       bfd_alloc.  We can't do this later in this function.  */
823
0
    bfd_release (input_bfd, entry);
824
0
    return 2;
825
0
  }
826
0
    }
827
828
0
  name = (bfd_elf_string_from_elf_section
829
0
    (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830
0
     entry->isym.st_name));
831
832
0
  dynstr = elf_hash_table (info)->dynstr;
833
0
  if (dynstr == NULL)
834
0
    {
835
      /* Create a strtab to hold the dynamic symbol names.  */
836
0
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837
0
      if (dynstr == NULL)
838
0
  return 0;
839
0
    }
840
841
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842
0
  if (dynstr_index == (size_t) -1)
843
0
    return 0;
844
0
  entry->isym.st_name = dynstr_index;
845
846
0
  eht = elf_hash_table (info);
847
848
0
  entry->next = eht->dynlocal;
849
0
  eht->dynlocal = entry;
850
0
  entry->input_bfd = input_bfd;
851
0
  entry->input_indx = input_indx;
852
0
  eht->dynsymcount++;
853
854
  /* Whatever binding the symbol had before, it's now local.  */
855
0
  entry->isym.st_info
856
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858
  /* The dynindx will be set at the end of size_dynamic_sections.  */
859
860
0
  return 1;
861
0
}
862
863
/* Return the dynindex of a local dynamic symbol.  */
864
865
long
866
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867
            bfd *input_bfd,
868
            long input_indx)
869
0
{
870
0
  struct elf_link_local_dynamic_entry *e;
871
872
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874
0
      return e->dynindx;
875
0
  return -1;
876
0
}
877
878
/* This function is used to renumber the dynamic symbols, if some of
879
   them are removed because they are marked as local.  This is called
880
   via elf_link_hash_traverse.  */
881
882
static bool
883
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884
              void *data)
885
0
{
886
0
  size_t *count = (size_t *) data;
887
888
0
  if (h->forced_local)
889
0
    return true;
890
891
0
  if (h->dynindx != -1)
892
0
    h->dynindx = ++(*count);
893
894
0
  return true;
895
0
}
896
897
898
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899
   STB_LOCAL binding.  */
900
901
static bool
902
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903
              void *data)
904
0
{
905
0
  size_t *count = (size_t *) data;
906
907
0
  if (!h->forced_local)
908
0
    return true;
909
910
0
  if (h->dynindx != -1)
911
0
    h->dynindx = ++(*count);
912
913
0
  return true;
914
0
}
915
916
/* Return true if the dynamic symbol for a given section should be
917
   omitted when creating a shared library.  */
918
bool
919
_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920
              struct bfd_link_info *info,
921
              asection *p)
922
0
{
923
0
  struct elf_link_hash_table *htab;
924
0
  asection *ip;
925
926
0
  switch (elf_section_data (p)->this_hdr.sh_type)
927
0
    {
928
0
    case SHT_PROGBITS:
929
0
    case SHT_NOBITS:
930
      /* If sh_type is yet undecided, assume it could be
931
   SHT_PROGBITS/SHT_NOBITS.  */
932
0
    case SHT_NULL:
933
0
      htab = elf_hash_table (info);
934
0
      if (htab->text_index_section != NULL)
935
0
  return p != htab->text_index_section && p != htab->data_index_section;
936
937
0
      return (htab->dynobj != NULL
938
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939
0
        && ip->output_section == p);
940
941
      /* There shouldn't be section relative relocations
942
   against any other section.  */
943
0
    default:
944
0
      return true;
945
0
    }
946
0
}
947
948
bool
949
_bfd_elf_omit_section_dynsym_all
950
    (bfd *output_bfd ATTRIBUTE_UNUSED,
951
     struct bfd_link_info *info ATTRIBUTE_UNUSED,
952
     asection *p ATTRIBUTE_UNUSED)
953
0
{
954
0
  return true;
955
0
}
956
957
/* Assign dynsym indices.  In a shared library we generate a section
958
   symbol for each output section, which come first.  Next come symbols
959
   which have been forced to local binding.  Then all of the back-end
960
   allocated local dynamic syms, followed by the rest of the global
961
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962
   (This prevents the early call before elf_backend_init_index_section
963
   and strip_excluded_output_sections setting dynindx for sections
964
   that are stripped.)  */
965
966
static unsigned long
967
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968
        struct bfd_link_info *info,
969
        unsigned long *section_sym_count)
970
0
{
971
0
  unsigned long dynsymcount = 0;
972
0
  bool do_sec = section_sym_count != NULL;
973
974
0
  if (bfd_link_pic (info)
975
0
      || elf_hash_table (info)->is_relocatable_executable)
976
0
    {
977
0
      const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978
0
      asection *p;
979
0
      for (p = output_bfd->sections; p ; p = p->next)
980
0
  if ((p->flags & SEC_EXCLUDE) == 0
981
0
      && (p->flags & SEC_ALLOC) != 0
982
0
      && elf_hash_table (info)->dynamic_relocs
983
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984
0
    {
985
0
      ++dynsymcount;
986
0
      if (do_sec)
987
0
        elf_section_data (p)->dynindx = dynsymcount;
988
0
    }
989
0
  else if (do_sec)
990
0
    elf_section_data (p)->dynindx = 0;
991
0
    }
992
0
  if (do_sec)
993
0
    *section_sym_count = dynsymcount;
994
995
0
  elf_link_hash_traverse (elf_hash_table (info),
996
0
        elf_link_renumber_local_hash_table_dynsyms,
997
0
        &dynsymcount);
998
999
0
  if (elf_hash_table (info)->dynlocal)
1000
0
    {
1001
0
      struct elf_link_local_dynamic_entry *p;
1002
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003
0
  p->dynindx = ++dynsymcount;
1004
0
    }
1005
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007
0
  elf_link_hash_traverse (elf_hash_table (info),
1008
0
        elf_link_renumber_hash_table_dynsyms,
1009
0
        &dynsymcount);
1010
1011
  /* There is an unused NULL entry at the head of the table which we
1012
     must account for in our count even if the table is empty since it
1013
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014
     .dynamic section.  */
1015
0
  dynsymcount++;
1016
1017
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1018
0
  return dynsymcount;
1019
0
}
1020
1021
/* Merge st_other field.  */
1022
1023
static void
1024
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025
        unsigned int st_other, asection *sec,
1026
        bool definition, bool dynamic)
1027
0
{
1028
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030
  /* If st_other has a processor-specific meaning, specific
1031
     code might be needed here.  */
1032
0
  if (bed->elf_backend_merge_symbol_attribute)
1033
0
    (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034
0
            dynamic);
1035
1036
0
  if (!dynamic)
1037
0
    {
1038
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041
      /* Keep the most constraining visibility.  Leave the remainder
1042
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1043
0
      if (symvis - 1 < hvis - 1)
1044
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045
0
    }
1046
0
  else if (definition
1047
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048
0
     && (sec->flags & SEC_READONLY) == 0)
1049
0
    h->protected_def = 1;
1050
0
}
1051
1052
/* This function is called when we want to merge a new symbol with an
1053
   existing symbol.  It handles the various cases which arise when we
1054
   find a definition in a dynamic object, or when there is already a
1055
   definition in a dynamic object.  The new symbol is described by
1056
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1057
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1058
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1059
   of an old common symbol.  We set OVERRIDE if the old symbol is
1060
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1061
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1062
   to change.  By OK to change, we mean that we shouldn't warn if the
1063
   type or size does change.  */
1064
1065
static bool
1066
_bfd_elf_merge_symbol (bfd *abfd,
1067
           struct bfd_link_info *info,
1068
           const char *name,
1069
           Elf_Internal_Sym *sym,
1070
           asection **psec,
1071
           bfd_vma *pvalue,
1072
           struct elf_link_hash_entry **sym_hash,
1073
           bfd **poldbfd,
1074
           bool *pold_weak,
1075
           unsigned int *pold_alignment,
1076
           bool *skip,
1077
           bfd **override,
1078
           bool *type_change_ok,
1079
           bool *size_change_ok,
1080
           bool *matched)
1081
0
{
1082
0
  asection *sec, *oldsec;
1083
0
  struct elf_link_hash_entry *h;
1084
0
  struct elf_link_hash_entry *hi;
1085
0
  struct elf_link_hash_entry *flip;
1086
0
  int bind;
1087
0
  bfd *oldbfd;
1088
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089
0
  bool newweak, oldweak, newfunc, oldfunc;
1090
0
  const struct elf_backend_data *bed;
1091
0
  char *new_version;
1092
0
  bool default_sym = *matched;
1093
0
  struct elf_link_hash_table *htab;
1094
1095
0
  *skip = false;
1096
0
  *override = NULL;
1097
1098
0
  sec = *psec;
1099
0
  bind = ELF_ST_BIND (sym->st_info);
1100
1101
0
  if (! bfd_is_und_section (sec))
1102
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1103
0
  else
1104
0
    h = ((struct elf_link_hash_entry *)
1105
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1106
0
  if (h == NULL)
1107
0
    return false;
1108
0
  *sym_hash = h;
1109
1110
0
  bed = get_elf_backend_data (abfd);
1111
1112
  /* NEW_VERSION is the symbol version of the new symbol.  */
1113
0
  if (h->versioned != unversioned)
1114
0
    {
1115
      /* Symbol version is unknown or versioned.  */
1116
0
      new_version = strrchr (name, ELF_VER_CHR);
1117
0
      if (new_version)
1118
0
  {
1119
0
    if (h->versioned == unknown)
1120
0
      {
1121
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1122
0
    h->versioned = versioned_hidden;
1123
0
        else
1124
0
    h->versioned = versioned;
1125
0
      }
1126
0
    new_version += 1;
1127
0
    if (new_version[0] == '\0')
1128
0
      new_version = NULL;
1129
0
  }
1130
0
      else
1131
0
  h->versioned = unversioned;
1132
0
    }
1133
0
  else
1134
0
    new_version = NULL;
1135
1136
  /* For merging, we only care about real symbols.  But we need to make
1137
     sure that indirect symbol dynamic flags are updated.  */
1138
0
  hi = h;
1139
0
  while (h->root.type == bfd_link_hash_indirect
1140
0
   || h->root.type == bfd_link_hash_warning)
1141
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1142
1143
0
  if (!*matched)
1144
0
    {
1145
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1146
0
  *matched = true;
1147
0
      else
1148
0
  {
1149
    /* OLD_HIDDEN is true if the existing symbol is only visible
1150
       to the symbol with the same symbol version.  NEW_HIDDEN is
1151
       true if the new symbol is only visible to the symbol with
1152
       the same symbol version.  */
1153
0
    bool old_hidden = h->versioned == versioned_hidden;
1154
0
    bool new_hidden = hi->versioned == versioned_hidden;
1155
0
    if (!old_hidden && !new_hidden)
1156
      /* The new symbol matches the existing symbol if both
1157
         aren't hidden.  */
1158
0
      *matched = true;
1159
0
    else
1160
0
      {
1161
        /* OLD_VERSION is the symbol version of the existing
1162
     symbol. */
1163
0
        char *old_version;
1164
1165
0
        if (h->versioned >= versioned)
1166
0
    old_version = strrchr (h->root.root.string,
1167
0
               ELF_VER_CHR) + 1;
1168
0
        else
1169
0
     old_version = NULL;
1170
1171
        /* The new symbol matches the existing symbol if they
1172
     have the same symbol version.  */
1173
0
        *matched = (old_version == new_version
1174
0
        || (old_version != NULL
1175
0
            && new_version != NULL
1176
0
            && strcmp (old_version, new_version) == 0));
1177
0
      }
1178
0
  }
1179
0
    }
1180
1181
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1182
     existing symbol.  */
1183
1184
0
  oldbfd = NULL;
1185
0
  oldsec = NULL;
1186
0
  switch (h->root.type)
1187
0
    {
1188
0
    default:
1189
0
      break;
1190
1191
0
    case bfd_link_hash_undefined:
1192
0
    case bfd_link_hash_undefweak:
1193
0
      oldbfd = h->root.u.undef.abfd;
1194
0
      break;
1195
1196
0
    case bfd_link_hash_defined:
1197
0
    case bfd_link_hash_defweak:
1198
0
      oldbfd = h->root.u.def.section->owner;
1199
0
      oldsec = h->root.u.def.section;
1200
0
      break;
1201
1202
0
    case bfd_link_hash_common:
1203
0
      oldbfd = h->root.u.c.p->section->owner;
1204
0
      oldsec = h->root.u.c.p->section;
1205
0
      if (pold_alignment)
1206
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1207
0
      break;
1208
0
    }
1209
0
  if (poldbfd && *poldbfd == NULL)
1210
0
    *poldbfd = oldbfd;
1211
1212
  /* Differentiate strong and weak symbols.  */
1213
0
  newweak = bind == STB_WEAK;
1214
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1215
0
       || h->root.type == bfd_link_hash_undefweak);
1216
0
  if (pold_weak)
1217
0
    *pold_weak = oldweak;
1218
1219
  /* We have to check it for every instance since the first few may be
1220
     references and not all compilers emit symbol type for undefined
1221
     symbols.  */
1222
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1223
1224
0
  htab = elf_hash_table (info);
1225
1226
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227
     respectively, is from a dynamic object.  */
1228
1229
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1230
1231
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232
     syms and defined syms in dynamic libraries respectively.
1233
     ref_dynamic on the other hand can be set for a symbol defined in
1234
     a dynamic library, and def_dynamic may not be set;  When the
1235
     definition in a dynamic lib is overridden by a definition in the
1236
     executable use of the symbol in the dynamic lib becomes a
1237
     reference to the executable symbol.  */
1238
0
  if (newdyn)
1239
0
    {
1240
0
      if (bfd_is_und_section (sec))
1241
0
  {
1242
0
    if (bind != STB_WEAK)
1243
0
      {
1244
0
        h->ref_dynamic_nonweak = 1;
1245
0
        hi->ref_dynamic_nonweak = 1;
1246
0
      }
1247
0
  }
1248
0
      else
1249
0
  {
1250
    /* Update the existing symbol only if they match. */
1251
0
    if (*matched)
1252
0
      h->dynamic_def = 1;
1253
0
    hi->dynamic_def = 1;
1254
0
  }
1255
0
    }
1256
1257
  /* If we just created the symbol, mark it as being an ELF symbol.
1258
     Other than that, there is nothing to do--there is no merge issue
1259
     with a newly defined symbol--so we just return.  */
1260
1261
0
  if (h->root.type == bfd_link_hash_new)
1262
0
    {
1263
0
      h->non_elf = 0;
1264
0
      return true;
1265
0
    }
1266
1267
  /* In cases involving weak versioned symbols, we may wind up trying
1268
     to merge a symbol with itself.  Catch that here, to avoid the
1269
     confusion that results if we try to override a symbol with
1270
     itself.  The additional tests catch cases like
1271
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272
     dynamic object, which we do want to handle here.  */
1273
0
  if (abfd == oldbfd
1274
0
      && (newweak || oldweak)
1275
0
      && ((abfd->flags & DYNAMIC) == 0
1276
0
    || !h->def_regular))
1277
0
    return true;
1278
1279
0
  olddyn = false;
1280
0
  if (oldbfd != NULL)
1281
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1282
0
  else if (oldsec != NULL)
1283
0
    {
1284
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285
   indices used by MIPS ELF.  */
1286
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1287
0
    }
1288
1289
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1290
0
  if (!htab->handling_dt_needed
1291
0
      && oldbfd != NULL
1292
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1293
0
    {
1294
0
      if (newdyn != olddyn)
1295
0
  {
1296
    /* Handle a case where plugin_notice won't be called and thus
1297
       won't set the non_ir_ref flags on the first pass over
1298
       symbols.  */
1299
0
    h->root.non_ir_ref_dynamic = true;
1300
0
    hi->root.non_ir_ref_dynamic = true;
1301
0
  }
1302
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1303
0
         && hi->root.type == bfd_link_hash_indirect)
1304
0
  {
1305
    /* Change indirect symbol from IR to undefined.  */
1306
0
    hi->root.type = bfd_link_hash_undefined;
1307
0
    hi->root.u.undef.abfd = oldbfd;
1308
0
  }
1309
0
    }
1310
1311
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312
     respectively, appear to be a definition rather than reference.  */
1313
1314
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1315
1316
0
  olddef = (h->root.type != bfd_link_hash_undefined
1317
0
      && h->root.type != bfd_link_hash_undefweak
1318
0
      && h->root.type != bfd_link_hash_common);
1319
1320
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321
     respectively, appear to be a function.  */
1322
1323
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1324
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1325
1326
0
  oldfunc = (h->type != STT_NOTYPE
1327
0
       && bed->is_function_type (h->type));
1328
1329
0
  if (!(newfunc && oldfunc)
1330
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1331
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1332
0
      && h->type != STT_NOTYPE
1333
0
      && (newdef || bfd_is_com_section (sec))
1334
0
      && (olddef || h->root.type == bfd_link_hash_common))
1335
0
    {
1336
      /* If creating a default indirect symbol ("foo" or "foo@") from
1337
   a dynamic versioned definition ("foo@@") skip doing so if
1338
   there is an existing regular definition with a different
1339
   type.  We don't want, for example, a "time" variable in the
1340
   executable overriding a "time" function in a shared library.  */
1341
0
      if (newdyn
1342
0
    && !olddyn)
1343
0
  {
1344
0
    *skip = true;
1345
0
    return true;
1346
0
  }
1347
1348
      /* When adding a symbol from a regular object file after we have
1349
   created indirect symbols, undo the indirection and any
1350
   dynamic state.  */
1351
0
      if (hi != h
1352
0
    && !newdyn
1353
0
    && olddyn)
1354
0
  {
1355
0
    h = hi;
1356
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1357
0
    h->forced_local = 0;
1358
0
    h->ref_dynamic = 0;
1359
0
    h->def_dynamic = 0;
1360
0
    h->dynamic_def = 0;
1361
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1362
0
      {
1363
0
        h->root.type = bfd_link_hash_undefined;
1364
0
        h->root.u.undef.abfd = abfd;
1365
0
      }
1366
0
    else
1367
0
      {
1368
0
        h->root.type = bfd_link_hash_new;
1369
0
        h->root.u.undef.abfd = NULL;
1370
0
      }
1371
0
    return true;
1372
0
  }
1373
0
    }
1374
1375
  /* Check TLS symbols.  We don't check undefined symbols introduced
1376
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1377
     check symbols from plugins because they also have no type.  */
1378
0
  if (oldbfd != NULL
1379
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1380
0
      && (abfd->flags & BFD_PLUGIN) == 0
1381
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1382
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1383
0
    {
1384
0
      bfd *ntbfd, *tbfd;
1385
0
      bool ntdef, tdef;
1386
0
      asection *ntsec, *tsec;
1387
1388
0
      if (h->type == STT_TLS)
1389
0
  {
1390
0
    ntbfd = abfd;
1391
0
    ntsec = sec;
1392
0
    ntdef = newdef;
1393
0
    tbfd = oldbfd;
1394
0
    tsec = oldsec;
1395
0
    tdef = olddef;
1396
0
  }
1397
0
      else
1398
0
  {
1399
0
    ntbfd = oldbfd;
1400
0
    ntsec = oldsec;
1401
0
    ntdef = olddef;
1402
0
    tbfd = abfd;
1403
0
    tsec = sec;
1404
0
    tdef = newdef;
1405
0
  }
1406
1407
0
      if (tdef && ntdef)
1408
0
  _bfd_error_handler
1409
    /* xgettext:c-format */
1410
0
    (_("%s: TLS definition in %pB section %pA "
1411
0
       "mismatches non-TLS definition in %pB section %pA"),
1412
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1413
0
      else if (!tdef && !ntdef)
1414
0
  _bfd_error_handler
1415
    /* xgettext:c-format */
1416
0
    (_("%s: TLS reference in %pB "
1417
0
       "mismatches non-TLS reference in %pB"),
1418
0
     h->root.root.string, tbfd, ntbfd);
1419
0
      else if (tdef)
1420
0
  _bfd_error_handler
1421
    /* xgettext:c-format */
1422
0
    (_("%s: TLS definition in %pB section %pA "
1423
0
       "mismatches non-TLS reference in %pB"),
1424
0
     h->root.root.string, tbfd, tsec, ntbfd);
1425
0
      else
1426
0
  _bfd_error_handler
1427
    /* xgettext:c-format */
1428
0
    (_("%s: TLS reference in %pB "
1429
0
       "mismatches non-TLS definition in %pB section %pA"),
1430
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1431
1432
0
      bfd_set_error (bfd_error_bad_value);
1433
0
      return false;
1434
0
    }
1435
1436
  /* If the old symbol has non-default visibility, we ignore the new
1437
     definition from a dynamic object.  */
1438
0
  if (newdyn
1439
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440
0
      && !bfd_is_und_section (sec))
1441
0
    {
1442
0
      *skip = true;
1443
      /* Make sure this symbol is dynamic.  */
1444
0
      h->ref_dynamic = 1;
1445
0
      hi->ref_dynamic = 1;
1446
      /* A protected symbol has external availability. Make sure it is
1447
   recorded as dynamic.
1448
1449
   FIXME: Should we check type and size for protected symbol?  */
1450
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1451
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1452
0
      else
1453
0
  return true;
1454
0
    }
1455
0
  else if (!newdyn
1456
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1457
0
     && h->def_dynamic)
1458
0
    {
1459
      /* If the new symbol with non-default visibility comes from a
1460
   relocatable file and the old definition comes from a dynamic
1461
   object, we remove the old definition.  */
1462
0
      if (hi->root.type == bfd_link_hash_indirect)
1463
0
  {
1464
    /* Handle the case where the old dynamic definition is
1465
       default versioned.  We need to copy the symbol info from
1466
       the symbol with default version to the normal one if it
1467
       was referenced before.  */
1468
0
    if (h->ref_regular)
1469
0
      {
1470
0
        hi->root.type = h->root.type;
1471
0
        h->root.type = bfd_link_hash_indirect;
1472
0
        (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1473
1474
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1475
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1476
0
    {
1477
      /* If the new symbol is hidden or internal, completely undo
1478
         any dynamic link state.  */
1479
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1480
0
      h->forced_local = 0;
1481
0
      h->ref_dynamic = 0;
1482
0
    }
1483
0
        else
1484
0
    h->ref_dynamic = 1;
1485
1486
0
        h->def_dynamic = 0;
1487
        /* FIXME: Should we check type and size for protected symbol?  */
1488
0
        h->size = 0;
1489
0
        h->type = 0;
1490
1491
0
        h = hi;
1492
0
      }
1493
0
    else
1494
0
      h = hi;
1495
0
  }
1496
1497
      /* If the old symbol was undefined before, then it will still be
1498
   on the undefs list.  If the new symbol is undefined or
1499
   common, we can't make it bfd_link_hash_new here, because new
1500
   undefined or common symbols will be added to the undefs list
1501
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1502
   added twice to the undefs list.  Also, if the new symbol is
1503
   undefweak then we don't want to lose the strong undef.  */
1504
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1505
0
  {
1506
0
    h->root.type = bfd_link_hash_undefined;
1507
0
    h->root.u.undef.abfd = abfd;
1508
0
  }
1509
0
      else
1510
0
  {
1511
0
    h->root.type = bfd_link_hash_new;
1512
0
    h->root.u.undef.abfd = NULL;
1513
0
  }
1514
1515
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1516
0
  {
1517
    /* If the new symbol is hidden or internal, completely undo
1518
       any dynamic link state.  */
1519
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1520
0
    h->forced_local = 0;
1521
0
    h->ref_dynamic = 0;
1522
0
  }
1523
0
      else
1524
0
  h->ref_dynamic = 1;
1525
0
      h->def_dynamic = 0;
1526
      /* FIXME: Should we check type and size for protected symbol?  */
1527
0
      h->size = 0;
1528
0
      h->type = 0;
1529
0
      return true;
1530
0
    }
1531
1532
  /* If a new weak symbol definition comes from a regular file and the
1533
     old symbol comes from a dynamic library, we treat the new one as
1534
     strong.  Similarly, an old weak symbol definition from a regular
1535
     file is treated as strong when the new symbol comes from a dynamic
1536
     library.  Further, an old weak symbol from a dynamic library is
1537
     treated as strong if the new symbol is from a dynamic library.
1538
     This reflects the way glibc's ld.so works.
1539
1540
     Also allow a weak symbol to override a linker script symbol
1541
     defined by an early pass over the script.  This is done so the
1542
     linker knows the symbol is defined in an object file, for the
1543
     DEFINED script function.
1544
1545
     Do this before setting *type_change_ok or *size_change_ok so that
1546
     we warn properly when dynamic library symbols are overridden.  */
1547
1548
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1549
0
    newweak = false;
1550
0
  if (olddef && newdyn)
1551
0
    oldweak = false;
1552
1553
  /* Allow changes between different types of function symbol.  */
1554
0
  if (newfunc && oldfunc)
1555
0
    *type_change_ok = true;
1556
1557
  /* It's OK to change the type if either the existing symbol or the
1558
     new symbol is weak.  A type change is also OK if the old symbol
1559
     is undefined and the new symbol is defined.  */
1560
1561
0
  if (oldweak
1562
0
      || newweak
1563
0
      || (newdef
1564
0
    && h->root.type == bfd_link_hash_undefined))
1565
0
    *type_change_ok = true;
1566
1567
  /* It's OK to change the size if either the existing symbol or the
1568
     new symbol is weak, or if the old symbol is undefined.  */
1569
1570
0
  if (*type_change_ok
1571
0
      || h->root.type == bfd_link_hash_undefined)
1572
0
    *size_change_ok = true;
1573
1574
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575
     symbol, respectively, appears to be a common symbol in a dynamic
1576
     object.  If a symbol appears in an uninitialized section, and is
1577
     not weak, and is not a function, then it may be a common symbol
1578
     which was resolved when the dynamic object was created.  We want
1579
     to treat such symbols specially, because they raise special
1580
     considerations when setting the symbol size: if the symbol
1581
     appears as a common symbol in a regular object, and the size in
1582
     the regular object is larger, we must make sure that we use the
1583
     larger size.  This problematic case can always be avoided in C,
1584
     but it must be handled correctly when using Fortran shared
1585
     libraries.
1586
1587
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588
     likewise for OLDDYNCOMMON and OLDDEF.
1589
1590
     Note that this test is just a heuristic, and that it is quite
1591
     possible to have an uninitialized symbol in a shared object which
1592
     is really a definition, rather than a common symbol.  This could
1593
     lead to some minor confusion when the symbol really is a common
1594
     symbol in some regular object.  However, I think it will be
1595
     harmless.  */
1596
1597
0
  if (newdyn
1598
0
      && newdef
1599
0
      && !newweak
1600
0
      && (sec->flags & SEC_ALLOC) != 0
1601
0
      && (sec->flags & SEC_LOAD) == 0
1602
0
      && sym->st_size > 0
1603
0
      && !newfunc)
1604
0
    newdyncommon = true;
1605
0
  else
1606
0
    newdyncommon = false;
1607
1608
0
  if (olddyn
1609
0
      && olddef
1610
0
      && h->root.type == bfd_link_hash_defined
1611
0
      && h->def_dynamic
1612
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1613
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1614
0
      && h->size > 0
1615
0
      && !oldfunc)
1616
0
    olddyncommon = true;
1617
0
  else
1618
0
    olddyncommon = false;
1619
1620
  /* We now know everything about the old and new symbols.  We ask the
1621
     backend to check if we can merge them.  */
1622
0
  if (bed->merge_symbol != NULL)
1623
0
    {
1624
0
      if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1625
0
  return false;
1626
0
      sec = *psec;
1627
0
    }
1628
1629
  /* There are multiple definitions of a normal symbol.  Skip the
1630
     default symbol as well as definition from an IR object.  */
1631
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1632
0
      && !default_sym && h->def_regular
1633
0
      && !(oldbfd != NULL
1634
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1635
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1636
0
    {
1637
      /* Handle a multiple definition.  */
1638
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1639
0
                 abfd, sec, *pvalue);
1640
0
      *skip = true;
1641
0
      return true;
1642
0
    }
1643
1644
  /* If both the old and the new symbols look like common symbols in a
1645
     dynamic object, set the size of the symbol to the larger of the
1646
     two.  */
1647
1648
0
  if (olddyncommon
1649
0
      && newdyncommon
1650
0
      && sym->st_size != h->size)
1651
0
    {
1652
      /* Since we think we have two common symbols, issue a multiple
1653
   common warning if desired.  Note that we only warn if the
1654
   size is different.  If the size is the same, we simply let
1655
   the old symbol override the new one as normally happens with
1656
   symbols defined in dynamic objects.  */
1657
1658
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1659
0
             bfd_link_hash_common, sym->st_size);
1660
0
      if (sym->st_size > h->size)
1661
0
  h->size = sym->st_size;
1662
1663
0
      *size_change_ok = true;
1664
0
    }
1665
1666
  /* If we are looking at a dynamic object, and we have found a
1667
     definition, we need to see if the symbol was already defined by
1668
     some other object.  If so, we want to use the existing
1669
     definition, and we do not want to report a multiple symbol
1670
     definition error; we do this by clobbering *PSEC to be
1671
     bfd_und_section_ptr.
1672
1673
     We treat a common symbol as a definition if the symbol in the
1674
     shared library is a function, since common symbols always
1675
     represent variables; this can cause confusion in principle, but
1676
     any such confusion would seem to indicate an erroneous program or
1677
     shared library.  We also permit a common symbol in a regular
1678
     object to override a weak symbol in a shared object.  */
1679
1680
0
  if (newdyn
1681
0
      && newdef
1682
0
      && (olddef
1683
0
    || (h->root.type == bfd_link_hash_common
1684
0
        && (newweak || newfunc))))
1685
0
    {
1686
0
      *override = abfd;
1687
0
      newdef = false;
1688
0
      newdyncommon = false;
1689
1690
0
      *psec = sec = bfd_und_section_ptr;
1691
0
      *size_change_ok = true;
1692
1693
      /* If we get here when the old symbol is a common symbol, then
1694
   we are explicitly letting it override a weak symbol or
1695
   function in a dynamic object, and we don't want to warn about
1696
   a type change.  If the old symbol is a defined symbol, a type
1697
   change warning may still be appropriate.  */
1698
1699
0
      if (h->root.type == bfd_link_hash_common)
1700
0
  *type_change_ok = true;
1701
0
    }
1702
1703
  /* Handle the special case of an old common symbol merging with a
1704
     new symbol which looks like a common symbol in a shared object.
1705
     We change *PSEC and *PVALUE to make the new symbol look like a
1706
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1707
     right thing.  */
1708
1709
0
  if (newdyncommon
1710
0
      && h->root.type == bfd_link_hash_common)
1711
0
    {
1712
0
      *override = oldbfd;
1713
0
      newdef = false;
1714
0
      newdyncommon = false;
1715
0
      *pvalue = sym->st_size;
1716
0
      *psec = sec = bed->common_section (oldsec);
1717
0
      *size_change_ok = true;
1718
0
    }
1719
1720
  /* Skip weak definitions of symbols that are already defined.  */
1721
0
  if (newdef && olddef && newweak)
1722
0
    {
1723
      /* Don't skip new non-IR weak syms.  */
1724
0
      if (!(oldbfd != NULL
1725
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1726
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1727
0
  {
1728
0
    newdef = false;
1729
0
    *skip = true;
1730
0
  }
1731
1732
      /* Merge st_other.  If the symbol already has a dynamic index,
1733
   but visibility says it should not be visible, turn it into a
1734
   local symbol.  */
1735
0
      elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1736
0
      if (h->dynindx != -1)
1737
0
  switch (ELF_ST_VISIBILITY (h->other))
1738
0
    {
1739
0
    case STV_INTERNAL:
1740
0
    case STV_HIDDEN:
1741
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1742
0
      break;
1743
0
    }
1744
0
    }
1745
1746
  /* If the old symbol is from a dynamic object, and the new symbol is
1747
     a definition which is not from a dynamic object, then the new
1748
     symbol overrides the old symbol.  Symbols from regular files
1749
     always take precedence over symbols from dynamic objects, even if
1750
     they are defined after the dynamic object in the link.
1751
1752
     As above, we again permit a common symbol in a regular object to
1753
     override a definition in a shared object if the shared object
1754
     symbol is a function or is weak.  */
1755
1756
0
  flip = NULL;
1757
0
  if (!newdyn
1758
0
      && (newdef
1759
0
    || (bfd_is_com_section (sec)
1760
0
        && (oldweak || oldfunc)))
1761
0
      && olddyn
1762
0
      && olddef
1763
0
      && h->def_dynamic)
1764
0
    {
1765
      /* Change the hash table entry to undefined, and let
1766
   _bfd_generic_link_add_one_symbol do the right thing with the
1767
   new definition.  */
1768
1769
0
      h->root.type = bfd_link_hash_undefined;
1770
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1771
0
      *size_change_ok = true;
1772
1773
0
      olddef = false;
1774
0
      olddyncommon = false;
1775
1776
      /* We again permit a type change when a common symbol may be
1777
   overriding a function.  */
1778
1779
0
      if (bfd_is_com_section (sec))
1780
0
  {
1781
0
    if (oldfunc)
1782
0
      {
1783
        /* If a common symbol overrides a function, make sure
1784
     that it isn't defined dynamically nor has type
1785
     function.  */
1786
0
        h->def_dynamic = 0;
1787
0
        h->type = STT_NOTYPE;
1788
0
      }
1789
0
    *type_change_ok = true;
1790
0
  }
1791
1792
0
      if (hi->root.type == bfd_link_hash_indirect)
1793
0
  flip = hi;
1794
0
      else
1795
  /* This union may have been set to be non-NULL when this symbol
1796
     was seen in a dynamic object.  We must force the union to be
1797
     NULL, so that it is correct for a regular symbol.  */
1798
0
  h->verinfo.vertree = NULL;
1799
0
    }
1800
1801
  /* Handle the special case of a new common symbol merging with an
1802
     old symbol that looks like it might be a common symbol defined in
1803
     a shared object.  Note that we have already handled the case in
1804
     which a new common symbol should simply override the definition
1805
     in the shared library.  */
1806
1807
0
  if (! newdyn
1808
0
      && bfd_is_com_section (sec)
1809
0
      && olddyncommon)
1810
0
    {
1811
      /* It would be best if we could set the hash table entry to a
1812
   common symbol, but we don't know what to use for the section
1813
   or the alignment.  */
1814
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1815
0
             bfd_link_hash_common, sym->st_size);
1816
1817
      /* If the presumed common symbol in the dynamic object is
1818
   larger, pretend that the new symbol has its size.  */
1819
1820
0
      if (h->size > *pvalue)
1821
0
  *pvalue = h->size;
1822
1823
      /* We need to remember the alignment required by the symbol
1824
   in the dynamic object.  */
1825
0
      BFD_ASSERT (pold_alignment);
1826
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1827
1828
0
      olddef = false;
1829
0
      olddyncommon = false;
1830
1831
0
      h->root.type = bfd_link_hash_undefined;
1832
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1833
1834
0
      *size_change_ok = true;
1835
0
      *type_change_ok = true;
1836
1837
0
      if (hi->root.type == bfd_link_hash_indirect)
1838
0
  flip = hi;
1839
0
      else
1840
0
  h->verinfo.vertree = NULL;
1841
0
    }
1842
1843
0
  if (flip != NULL)
1844
0
    {
1845
      /* Handle the case where we had a versioned symbol in a dynamic
1846
   library and now find a definition in a normal object.  In this
1847
   case, we make the versioned symbol point to the normal one.  */
1848
0
      flip->root.type = h->root.type;
1849
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1850
0
      h->root.type = bfd_link_hash_indirect;
1851
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1852
0
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1853
0
      if (h->def_dynamic)
1854
0
  {
1855
0
    h->def_dynamic = 0;
1856
0
    flip->ref_dynamic = 1;
1857
0
  }
1858
0
    }
1859
1860
0
  return true;
1861
0
}
1862
1863
/* This function is called to create an indirect symbol from the
1864
   default for the symbol with the default version if needed. The
1865
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1866
   set DYNSYM if the new indirect symbol is dynamic.  */
1867
1868
static bool
1869
_bfd_elf_add_default_symbol (bfd *abfd,
1870
           struct bfd_link_info *info,
1871
           struct elf_link_hash_entry *h,
1872
           const char *name,
1873
           Elf_Internal_Sym *sym,
1874
           asection *sec,
1875
           bfd_vma value,
1876
           bfd **poldbfd,
1877
           bool *dynsym)
1878
0
{
1879
0
  bool type_change_ok;
1880
0
  bool size_change_ok;
1881
0
  bool skip;
1882
0
  char *shortname;
1883
0
  struct elf_link_hash_entry *hi;
1884
0
  struct bfd_link_hash_entry *bh;
1885
0
  const struct elf_backend_data *bed;
1886
0
  bool collect;
1887
0
  bool dynamic;
1888
0
  bfd *override;
1889
0
  char *p;
1890
0
  size_t len, shortlen;
1891
0
  asection *tmp_sec;
1892
0
  bool matched;
1893
1894
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1895
0
    return true;
1896
1897
  /* If this symbol has a version, and it is the default version, we
1898
     create an indirect symbol from the default name to the fully
1899
     decorated name.  This will cause external references which do not
1900
     specify a version to be bound to this version of the symbol.  */
1901
0
  p = strchr (name, ELF_VER_CHR);
1902
0
  if (h->versioned == unknown)
1903
0
    {
1904
0
      if (p == NULL)
1905
0
  {
1906
0
    h->versioned = unversioned;
1907
0
    return true;
1908
0
  }
1909
0
      else
1910
0
  {
1911
0
    if (p[1] != ELF_VER_CHR)
1912
0
      {
1913
0
        h->versioned = versioned_hidden;
1914
0
        return true;
1915
0
      }
1916
0
    else
1917
0
      h->versioned = versioned;
1918
0
  }
1919
0
    }
1920
0
  else
1921
0
    {
1922
      /* PR ld/19073: We may see an unversioned definition after the
1923
   default version.  */
1924
0
      if (p == NULL)
1925
0
  return true;
1926
0
    }
1927
1928
0
  bed = get_elf_backend_data (abfd);
1929
0
  collect = bed->collect;
1930
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
1931
1932
0
  shortlen = p - name;
1933
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1934
0
  if (shortname == NULL)
1935
0
    return false;
1936
0
  memcpy (shortname, name, shortlen);
1937
0
  shortname[shortlen] = '\0';
1938
1939
  /* We are going to create a new symbol.  Merge it with any existing
1940
     symbol with this name.  For the purposes of the merge, act as
1941
     though we were defining the symbol we just defined, although we
1942
     actually going to define an indirect symbol.  */
1943
0
  type_change_ok = false;
1944
0
  size_change_ok = false;
1945
0
  matched = true;
1946
0
  tmp_sec = sec;
1947
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1948
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
1949
0
            &type_change_ok, &size_change_ok, &matched))
1950
0
    return false;
1951
1952
0
  if (skip)
1953
0
    goto nondefault;
1954
1955
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1956
0
    {
1957
      /* If the undecorated symbol will have a version added by a
1958
   script different to H, then don't indirect to/from the
1959
   undecorated symbol.  This isn't ideal because we may not yet
1960
   have seen symbol versions, if given by a script on the
1961
   command line rather than via --version-script.  */
1962
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1963
0
  {
1964
0
    bool hide;
1965
1966
0
    hi->verinfo.vertree
1967
0
      = bfd_find_version_for_sym (info->version_info,
1968
0
          hi->root.root.string, &hide);
1969
0
    if (hi->verinfo.vertree != NULL && hide)
1970
0
      {
1971
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
1972
0
        goto nondefault;
1973
0
      }
1974
0
  }
1975
0
      if (hi->verinfo.vertree != NULL
1976
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1977
0
  goto nondefault;
1978
0
    }
1979
1980
0
  if (! override)
1981
0
    {
1982
      /* Add the default symbol if not performing a relocatable link.  */
1983
0
      if (! bfd_link_relocatable (info))
1984
0
  {
1985
0
    bh = &hi->root;
1986
0
    if (bh->type == bfd_link_hash_defined
1987
0
        && bh->u.def.section->owner != NULL
1988
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1989
0
      {
1990
        /* Mark the previous definition from IR object as
1991
     undefined so that the generic linker will override
1992
     it.  */
1993
0
        bh->type = bfd_link_hash_undefined;
1994
0
        bh->u.undef.abfd = bh->u.def.section->owner;
1995
0
      }
1996
0
    if (! (_bfd_generic_link_add_one_symbol
1997
0
     (info, abfd, shortname, BSF_INDIRECT,
1998
0
      bfd_ind_section_ptr,
1999
0
      0, name, false, collect, &bh)))
2000
0
      return false;
2001
0
    hi = (struct elf_link_hash_entry *) bh;
2002
0
  }
2003
0
    }
2004
0
  else
2005
0
    {
2006
      /* In this case the symbol named SHORTNAME is overriding the
2007
   indirect symbol we want to add.  We were planning on making
2008
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2009
   is the name without a version.  NAME is the fully versioned
2010
   name, and it is the default version.
2011
2012
   Overriding means that we already saw a definition for the
2013
   symbol SHORTNAME in a regular object, and it is overriding
2014
   the symbol defined in the dynamic object.
2015
2016
   When this happens, we actually want to change NAME, the
2017
   symbol we just added, to refer to SHORTNAME.  This will cause
2018
   references to NAME in the shared object to become references
2019
   to SHORTNAME in the regular object.  This is what we expect
2020
   when we override a function in a shared object: that the
2021
   references in the shared object will be mapped to the
2022
   definition in the regular object.  */
2023
2024
0
      while (hi->root.type == bfd_link_hash_indirect
2025
0
       || hi->root.type == bfd_link_hash_warning)
2026
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2027
2028
0
      h->root.type = bfd_link_hash_indirect;
2029
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2030
0
      if (h->def_dynamic)
2031
0
  {
2032
0
    h->def_dynamic = 0;
2033
0
    hi->ref_dynamic = 1;
2034
0
    if (hi->ref_regular
2035
0
        || hi->def_regular)
2036
0
      {
2037
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2038
0
    return false;
2039
0
      }
2040
0
  }
2041
2042
      /* Now set HI to H, so that the following code will set the
2043
   other fields correctly.  */
2044
0
      hi = h;
2045
0
    }
2046
2047
  /* Check if HI is a warning symbol.  */
2048
0
  if (hi->root.type == bfd_link_hash_warning)
2049
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2050
2051
  /* If there is a duplicate definition somewhere, then HI may not
2052
     point to an indirect symbol.  We will have reported an error to
2053
     the user in that case.  */
2054
2055
0
  if (hi->root.type == bfd_link_hash_indirect)
2056
0
    {
2057
0
      struct elf_link_hash_entry *ht;
2058
2059
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2060
0
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2061
2062
      /* If we first saw a reference to SHORTNAME with non-default
2063
   visibility, merge that visibility to the @@VER symbol.  */
2064
0
      elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2065
2066
      /* A reference to the SHORTNAME symbol from a dynamic library
2067
   will be satisfied by the versioned symbol at runtime.  In
2068
   effect, we have a reference to the versioned symbol.  */
2069
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2070
0
      hi->dynamic_def |= ht->dynamic_def;
2071
2072
      /* See if the new flags lead us to realize that the symbol must
2073
   be dynamic.  */
2074
0
      if (! *dynsym)
2075
0
  {
2076
0
    if (! dynamic)
2077
0
      {
2078
0
        if (! bfd_link_executable (info)
2079
0
      || hi->def_dynamic
2080
0
      || hi->ref_dynamic)
2081
0
    *dynsym = true;
2082
0
      }
2083
0
    else
2084
0
      {
2085
0
        if (hi->ref_regular)
2086
0
    *dynsym = true;
2087
0
      }
2088
0
  }
2089
0
    }
2090
2091
  /* We also need to define an indirection from the nondefault version
2092
     of the symbol.  */
2093
2094
0
 nondefault:
2095
0
  len = strlen (name);
2096
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2097
0
  if (shortname == NULL)
2098
0
    return false;
2099
0
  memcpy (shortname, name, shortlen);
2100
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2101
2102
  /* Once again, merge with any existing symbol.  */
2103
0
  type_change_ok = false;
2104
0
  size_change_ok = false;
2105
0
  tmp_sec = sec;
2106
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2107
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2108
0
            &type_change_ok, &size_change_ok, &matched))
2109
0
    return false;
2110
2111
0
  if (skip)
2112
0
    {
2113
0
      if (!dynamic
2114
0
    && h->root.type == bfd_link_hash_defweak
2115
0
    && hi->root.type == bfd_link_hash_defined)
2116
0
  {
2117
    /* We are handling a weak sym@@ver and attempting to define
2118
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119
       new weak sym@ver because there is already a strong sym@ver.
2120
       However, sym@ver and sym@@ver are really the same symbol.
2121
       The existing strong sym@ver ought to override sym@@ver.  */
2122
0
    h->root.type = bfd_link_hash_defined;
2123
0
    h->root.u.def.section = hi->root.u.def.section;
2124
0
    h->root.u.def.value = hi->root.u.def.value;
2125
0
    hi->root.type = bfd_link_hash_indirect;
2126
0
    hi->root.u.i.link = &h->root;
2127
0
  }
2128
0
      else
2129
0
  return true;
2130
0
    }
2131
0
  else if (override)
2132
0
    {
2133
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2134
   the type of override we do in the case above unless it is
2135
   overridden by a versioned definition.  */
2136
0
      if (hi->root.type != bfd_link_hash_defined
2137
0
    && hi->root.type != bfd_link_hash_defweak)
2138
0
  _bfd_error_handler
2139
    /* xgettext:c-format */
2140
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2141
0
     abfd, shortname);
2142
0
      return true;
2143
0
    }
2144
0
  else
2145
0
    {
2146
0
      bh = &hi->root;
2147
0
      if (! (_bfd_generic_link_add_one_symbol
2148
0
       (info, abfd, shortname, BSF_INDIRECT,
2149
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2150
0
  return false;
2151
0
      hi = (struct elf_link_hash_entry *) bh;
2152
0
    }
2153
2154
  /* If there is a duplicate definition somewhere, then HI may not
2155
     point to an indirect symbol.  We will have reported an error
2156
     to the user in that case.  */
2157
0
  if (hi->root.type == bfd_link_hash_indirect)
2158
0
    {
2159
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2160
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2161
0
      hi->dynamic_def |= h->dynamic_def;
2162
2163
      /* If we first saw a reference to @VER symbol with
2164
   non-default visibility, merge that visibility to the
2165
   @@VER symbol.  */
2166
0
      elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2167
2168
      /* See if the new flags lead us to realize that the symbol
2169
   must be dynamic.  */
2170
0
      if (! *dynsym)
2171
0
  {
2172
0
    if (! dynamic)
2173
0
      {
2174
0
        if (! bfd_link_executable (info)
2175
0
      || hi->ref_dynamic)
2176
0
    *dynsym = true;
2177
0
      }
2178
0
    else
2179
0
      {
2180
0
        if (hi->ref_regular)
2181
0
    *dynsym = true;
2182
0
      }
2183
0
  }
2184
0
    }
2185
2186
0
  return true;
2187
0
}
2188

2189
/* This routine is used to export all defined symbols into the dynamic
2190
   symbol table.  It is called via elf_link_hash_traverse.  */
2191
2192
static bool
2193
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2194
0
{
2195
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2196
2197
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2198
0
  if (h->root.type == bfd_link_hash_indirect)
2199
0
    return true;
2200
2201
  /* Ignore this if we won't export it.  */
2202
0
  if (!eif->info->export_dynamic && !h->dynamic)
2203
0
    return true;
2204
2205
0
  if (h->dynindx == -1
2206
0
      && (h->def_regular || h->ref_regular)
2207
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2208
0
            h->root.root.string))
2209
0
    {
2210
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2211
0
  {
2212
0
    eif->failed = true;
2213
0
    return false;
2214
0
  }
2215
0
    }
2216
2217
0
  return true;
2218
0
}
2219

2220
/* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221
   dependencies successfully.  GLIBC_ABI_DT_RELR will be put into the
2222
   .gnu.version_r section.  */
2223
2224
static bool
2225
elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2226
0
{
2227
0
  bfd *glibc_bfd = NULL;
2228
0
  Elf_Internal_Verneed *t;
2229
0
  Elf_Internal_Vernaux *a;
2230
0
  size_t amt;
2231
0
  const char *relr = "GLIBC_ABI_DT_RELR";
2232
2233
  /* See if we already know about GLIBC_PRIVATE_DT_RELR.  */
2234
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2235
0
       t != NULL;
2236
0
       t = t->vn_nextref)
2237
0
    {
2238
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2239
      /* Skip the shared library if it isn't libc.so.  */
2240
0
      if (!soname || !startswith (soname, "libc.so."))
2241
0
  continue;
2242
2243
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2244
0
  {
2245
    /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2246
       added.  */
2247
0
    if (a->vna_nodename == relr
2248
0
        || strcmp (a->vna_nodename, relr) == 0)
2249
0
      return true;
2250
2251
    /* Check if libc.so provides GLIBC_2.XX version.  */
2252
0
    if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2253
0
      glibc_bfd = t->vn_bfd;
2254
0
  }
2255
2256
0
      break;
2257
0
    }
2258
2259
  /* Skip if it isn't linked against glibc.  */
2260
0
  if (glibc_bfd == NULL)
2261
0
    return true;
2262
2263
  /* This is a new version.  Add it to tree we are building.  */
2264
0
  if (t == NULL)
2265
0
    {
2266
0
      amt = sizeof *t;
2267
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2268
0
                 amt);
2269
0
      if (t == NULL)
2270
0
  {
2271
0
    rinfo->failed = true;
2272
0
    return false;
2273
0
  }
2274
2275
0
      t->vn_bfd = glibc_bfd;
2276
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2277
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2278
0
    }
2279
2280
0
  amt = sizeof *a;
2281
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2282
0
  if (a == NULL)
2283
0
    {
2284
0
      rinfo->failed = true;
2285
0
      return false;
2286
0
    }
2287
2288
0
  a->vna_nodename = relr;
2289
0
  a->vna_flags = 0;
2290
0
  a->vna_nextptr = t->vn_auxptr;
2291
0
  a->vna_other = rinfo->vers + 1;
2292
0
  ++rinfo->vers;
2293
2294
0
  t->vn_auxptr = a;
2295
2296
0
  return true;
2297
0
}
2298
2299
/* Look through the symbols which are defined in other shared
2300
   libraries and referenced here.  Update the list of version
2301
   dependencies.  This will be put into the .gnu.version_r section.
2302
   This function is called via elf_link_hash_traverse.  */
2303
2304
static bool
2305
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2306
           void *data)
2307
0
{
2308
0
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2309
0
  Elf_Internal_Verneed *t;
2310
0
  Elf_Internal_Vernaux *a;
2311
0
  size_t amt;
2312
2313
  /* We only care about symbols defined in shared objects with version
2314
     information.  */
2315
0
  if (!h->def_dynamic
2316
0
      || h->def_regular
2317
0
      || h->dynindx == -1
2318
0
      || h->verinfo.verdef == NULL
2319
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2320
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2321
0
    return true;
2322
2323
  /* See if we already know about this version.  */
2324
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2325
0
       t != NULL;
2326
0
       t = t->vn_nextref)
2327
0
    {
2328
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2329
0
  continue;
2330
2331
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2332
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2333
0
    return true;
2334
2335
0
      break;
2336
0
    }
2337
2338
  /* This is a new version.  Add it to tree we are building.  */
2339
2340
0
  if (t == NULL)
2341
0
    {
2342
0
      amt = sizeof *t;
2343
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2344
0
      if (t == NULL)
2345
0
  {
2346
0
    rinfo->failed = true;
2347
0
    return false;
2348
0
  }
2349
2350
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2351
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2352
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2353
0
    }
2354
2355
0
  amt = sizeof *a;
2356
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2357
0
  if (a == NULL)
2358
0
    {
2359
0
      rinfo->failed = true;
2360
0
      return false;
2361
0
    }
2362
2363
  /* Note that we are copying a string pointer here, and testing it
2364
     above.  If bfd_elf_string_from_elf_section is ever changed to
2365
     discard the string data when low in memory, this will have to be
2366
     fixed.  */
2367
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2368
2369
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2370
0
  a->vna_nextptr = t->vn_auxptr;
2371
2372
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2373
0
  ++rinfo->vers;
2374
2375
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2376
2377
0
  t->vn_auxptr = a;
2378
2379
0
  return true;
2380
0
}
2381
2382
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383
   hidden.  Set *T_P to NULL if there is no match.  */
2384
2385
static bool
2386
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2387
             struct elf_link_hash_entry *h,
2388
             const char *version_p,
2389
             struct bfd_elf_version_tree **t_p,
2390
             bool *hide)
2391
0
{
2392
0
  struct bfd_elf_version_tree *t;
2393
2394
  /* Look for the version.  If we find it, it is no longer weak.  */
2395
0
  for (t = info->version_info; t != NULL; t = t->next)
2396
0
    {
2397
0
      if (strcmp (t->name, version_p) == 0)
2398
0
  {
2399
0
    size_t len;
2400
0
    char *alc;
2401
0
    struct bfd_elf_version_expr *d;
2402
2403
0
    len = version_p - h->root.root.string;
2404
0
    alc = (char *) bfd_malloc (len);
2405
0
    if (alc == NULL)
2406
0
      return false;
2407
0
    memcpy (alc, h->root.root.string, len - 1);
2408
0
    alc[len - 1] = '\0';
2409
0
    if (alc[len - 2] == ELF_VER_CHR)
2410
0
      alc[len - 2] = '\0';
2411
2412
0
    h->verinfo.vertree = t;
2413
0
    t->used = true;
2414
0
    d = NULL;
2415
2416
0
    if (t->globals.list != NULL)
2417
0
      d = (*t->match) (&t->globals, NULL, alc);
2418
2419
    /* See if there is anything to force this symbol to
2420
       local scope.  */
2421
0
    if (d == NULL && t->locals.list != NULL)
2422
0
      {
2423
0
        d = (*t->match) (&t->locals, NULL, alc);
2424
0
        if (d != NULL
2425
0
      && h->dynindx != -1
2426
0
      && ! info->export_dynamic)
2427
0
    *hide = true;
2428
0
      }
2429
2430
0
    free (alc);
2431
0
    break;
2432
0
  }
2433
0
    }
2434
2435
0
  *t_p = t;
2436
2437
0
  return true;
2438
0
}
2439
2440
/* Return TRUE if the symbol H is hidden by version script.  */
2441
2442
bool
2443
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2444
           struct elf_link_hash_entry *h)
2445
0
{
2446
0
  const char *p;
2447
0
  bool hide = false;
2448
0
  const struct elf_backend_data *bed
2449
0
    = get_elf_backend_data (info->output_bfd);
2450
2451
  /* Version script only hides symbols defined in regular objects.  */
2452
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2453
0
    return true;
2454
2455
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2456
0
  if (p != NULL && h->verinfo.vertree == NULL)
2457
0
    {
2458
0
      struct bfd_elf_version_tree *t;
2459
2460
0
      ++p;
2461
0
      if (*p == ELF_VER_CHR)
2462
0
  ++p;
2463
2464
0
      if (*p != '\0'
2465
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2466
0
    && hide)
2467
0
  {
2468
0
    if (hide)
2469
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
2470
0
    return true;
2471
0
  }
2472
0
    }
2473
2474
  /* If we don't have a version for this symbol, see if we can find
2475
     something.  */
2476
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2477
0
    {
2478
0
      h->verinfo.vertree
2479
0
  = bfd_find_version_for_sym (info->version_info,
2480
0
            h->root.root.string, &hide);
2481
0
      if (h->verinfo.vertree != NULL && hide)
2482
0
  {
2483
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
2484
0
    return true;
2485
0
  }
2486
0
    }
2487
2488
0
  return false;
2489
0
}
2490
2491
/* Figure out appropriate versions for all the symbols.  We may not
2492
   have the version number script until we have read all of the input
2493
   files, so until that point we don't know which symbols should be
2494
   local.  This function is called via elf_link_hash_traverse.  */
2495
2496
static bool
2497
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2498
0
{
2499
0
  struct elf_info_failed *sinfo;
2500
0
  struct bfd_link_info *info;
2501
0
  const struct elf_backend_data *bed;
2502
0
  struct elf_info_failed eif;
2503
0
  char *p;
2504
0
  bool hide;
2505
2506
0
  sinfo = (struct elf_info_failed *) data;
2507
0
  info = sinfo->info;
2508
2509
  /* Fix the symbol flags.  */
2510
0
  eif.failed = false;
2511
0
  eif.info = info;
2512
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2513
0
    {
2514
0
      if (eif.failed)
2515
0
  sinfo->failed = true;
2516
0
      return false;
2517
0
    }
2518
2519
0
  bed = get_elf_backend_data (info->output_bfd);
2520
2521
  /* We only need version numbers for symbols defined in regular
2522
     objects.  */
2523
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2524
0
    {
2525
      /* Hide symbols defined in discarded input sections.  */
2526
0
      if ((h->root.type == bfd_link_hash_defined
2527
0
     || h->root.type == bfd_link_hash_defweak)
2528
0
    && discarded_section (h->root.u.def.section))
2529
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2530
0
      return true;
2531
0
    }
2532
2533
0
  hide = false;
2534
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2535
0
  if (p != NULL && h->verinfo.vertree == NULL)
2536
0
    {
2537
0
      struct bfd_elf_version_tree *t;
2538
2539
0
      ++p;
2540
0
      if (*p == ELF_VER_CHR)
2541
0
  ++p;
2542
2543
      /* If there is no version string, we can just return out.  */
2544
0
      if (*p == '\0')
2545
0
  return true;
2546
2547
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2548
0
  {
2549
0
    sinfo->failed = true;
2550
0
    return false;
2551
0
  }
2552
2553
0
      if (hide)
2554
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2555
2556
      /* If we are building an application, we need to create a
2557
   version node for this version.  */
2558
0
      if (t == NULL && bfd_link_executable (info))
2559
0
  {
2560
0
    struct bfd_elf_version_tree **pp;
2561
0
    int version_index;
2562
2563
    /* If we aren't going to export this symbol, we don't need
2564
       to worry about it.  */
2565
0
    if (h->dynindx == -1)
2566
0
      return true;
2567
2568
0
    t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2569
0
                sizeof *t);
2570
0
    if (t == NULL)
2571
0
      {
2572
0
        sinfo->failed = true;
2573
0
        return false;
2574
0
      }
2575
2576
0
    t->name = p;
2577
0
    t->name_indx = (unsigned int) -1;
2578
0
    t->used = true;
2579
2580
0
    version_index = 1;
2581
    /* Don't count anonymous version tag.  */
2582
0
    if (sinfo->info->version_info != NULL
2583
0
        && sinfo->info->version_info->vernum == 0)
2584
0
      version_index = 0;
2585
0
    for (pp = &sinfo->info->version_info;
2586
0
         *pp != NULL;
2587
0
         pp = &(*pp)->next)
2588
0
      ++version_index;
2589
0
    t->vernum = version_index;
2590
2591
0
    *pp = t;
2592
2593
0
    h->verinfo.vertree = t;
2594
0
  }
2595
0
      else if (t == NULL)
2596
0
  {
2597
    /* We could not find the version for a symbol when
2598
       generating a shared archive.  Return an error.  */
2599
0
    _bfd_error_handler
2600
      /* xgettext:c-format */
2601
0
      (_("%pB: version node not found for symbol %s"),
2602
0
       info->output_bfd, h->root.root.string);
2603
0
    bfd_set_error (bfd_error_bad_value);
2604
0
    sinfo->failed = true;
2605
0
    return false;
2606
0
  }
2607
0
    }
2608
2609
  /* If we don't have a version for this symbol, see if we can find
2610
     something.  */
2611
0
  if (!hide
2612
0
      && h->verinfo.vertree == NULL
2613
0
      && sinfo->info->version_info != NULL)
2614
0
    {
2615
0
      h->verinfo.vertree
2616
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2617
0
            h->root.root.string, &hide);
2618
0
      if (h->verinfo.vertree != NULL && hide)
2619
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2620
0
    }
2621
2622
0
  return true;
2623
0
}
2624

2625
/* Read and swap the relocs from the section indicated by SHDR.  This
2626
   may be either a REL or a RELA section.  The relocations are
2627
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2628
   which should have already been allocated to contain enough space.
2629
   The EXTERNAL_RELOCS are a buffer where the external form of the
2630
   relocations should be stored.
2631
2632
   Returns FALSE if something goes wrong.  */
2633
2634
static bool
2635
elf_link_read_relocs_from_section (bfd *abfd,
2636
           asection *sec,
2637
           Elf_Internal_Shdr *shdr,
2638
           void *external_relocs,
2639
           Elf_Internal_Rela *internal_relocs)
2640
0
{
2641
0
  const struct elf_backend_data *bed;
2642
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2643
0
  const bfd_byte *erela;
2644
0
  const bfd_byte *erelaend;
2645
0
  Elf_Internal_Rela *irela;
2646
0
  Elf_Internal_Shdr *symtab_hdr;
2647
0
  size_t nsyms;
2648
2649
  /* Position ourselves at the start of the section.  */
2650
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2651
0
    return false;
2652
2653
  /* Read the relocations.  */
2654
0
  if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2655
0
    return false;
2656
2657
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2658
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2659
2660
0
  bed = get_elf_backend_data (abfd);
2661
2662
  /* Convert the external relocations to the internal format.  */
2663
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2664
0
    swap_in = bed->s->swap_reloc_in;
2665
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2666
0
    swap_in = bed->s->swap_reloca_in;
2667
0
  else
2668
0
    {
2669
0
      bfd_set_error (bfd_error_wrong_format);
2670
0
      return false;
2671
0
    }
2672
2673
0
  erela = (const bfd_byte *) external_relocs;
2674
  /* Setting erelaend like this and comparing with <= handles case of
2675
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2676
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2677
0
  irela = internal_relocs;
2678
0
  while (erela <= erelaend)
2679
0
    {
2680
0
      bfd_vma r_symndx;
2681
2682
0
      (*swap_in) (abfd, erela, irela);
2683
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2684
0
      if (bed->s->arch_size == 64)
2685
0
  r_symndx >>= 24;
2686
0
      if (nsyms > 0)
2687
0
  {
2688
0
    if ((size_t) r_symndx >= nsyms)
2689
0
      {
2690
0
        _bfd_error_handler
2691
    /* xgettext:c-format */
2692
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2693
0
       " for offset %#" PRIx64 " in section `%pA'"),
2694
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2695
0
     (uint64_t) irela->r_offset, sec);
2696
0
        bfd_set_error (bfd_error_bad_value);
2697
0
        return false;
2698
0
      }
2699
0
  }
2700
0
      else if (r_symndx != STN_UNDEF)
2701
0
  {
2702
0
    _bfd_error_handler
2703
      /* xgettext:c-format */
2704
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2705
0
         " for offset %#" PRIx64 " in section `%pA'"
2706
0
         " when the object file has no symbol table"),
2707
0
       abfd, (uint64_t) r_symndx,
2708
0
       (uint64_t) irela->r_offset, sec);
2709
0
    bfd_set_error (bfd_error_bad_value);
2710
0
    return false;
2711
0
  }
2712
0
      irela += bed->s->int_rels_per_ext_rel;
2713
0
      erela += shdr->sh_entsize;
2714
0
    }
2715
2716
0
  return true;
2717
0
}
2718
2719
/* Read and swap the relocs for a section O.  They may have been
2720
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721
   not NULL, they are used as buffers to read into.  They are known to
2722
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2723
   the return value is allocated using either malloc or bfd_alloc,
2724
   according to the KEEP_MEMORY argument.  If O has two relocation
2725
   sections (both REL and RELA relocations), then the REL_HDR
2726
   relocations will appear first in INTERNAL_RELOCS, followed by the
2727
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2728
   update cache_size.  */
2729
2730
Elf_Internal_Rela *
2731
_bfd_elf_link_info_read_relocs (bfd *abfd,
2732
        struct bfd_link_info *info,
2733
        asection *o,
2734
        void *external_relocs,
2735
        Elf_Internal_Rela *internal_relocs,
2736
        bool keep_memory)
2737
0
{
2738
0
  void *alloc1 = NULL;
2739
0
  Elf_Internal_Rela *alloc2 = NULL;
2740
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2741
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2742
0
  Elf_Internal_Rela *internal_rela_relocs;
2743
2744
0
  if (esdo->relocs != NULL)
2745
0
    return esdo->relocs;
2746
2747
0
  if (o->reloc_count == 0)
2748
0
    return NULL;
2749
2750
0
  if (internal_relocs == NULL)
2751
0
    {
2752
0
      bfd_size_type size;
2753
2754
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2755
0
      if (keep_memory)
2756
0
  {
2757
0
    internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2758
0
    if (info)
2759
0
      info->cache_size += size;
2760
0
  }
2761
0
      else
2762
0
  internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2763
0
      if (internal_relocs == NULL)
2764
0
  goto error_return;
2765
0
    }
2766
2767
0
  if (external_relocs == NULL)
2768
0
    {
2769
0
      bfd_size_type size = 0;
2770
2771
0
      if (esdo->rel.hdr)
2772
0
  size += esdo->rel.hdr->sh_size;
2773
0
      if (esdo->rela.hdr)
2774
0
  size += esdo->rela.hdr->sh_size;
2775
2776
0
      alloc1 = bfd_malloc (size);
2777
0
      if (alloc1 == NULL)
2778
0
  goto error_return;
2779
0
      external_relocs = alloc1;
2780
0
    }
2781
2782
0
  internal_rela_relocs = internal_relocs;
2783
0
  if (esdo->rel.hdr)
2784
0
    {
2785
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2786
0
                external_relocs,
2787
0
                internal_relocs))
2788
0
  goto error_return;
2789
0
      external_relocs = (((bfd_byte *) external_relocs)
2790
0
       + esdo->rel.hdr->sh_size);
2791
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2792
0
             * bed->s->int_rels_per_ext_rel);
2793
0
    }
2794
2795
0
  if (esdo->rela.hdr
2796
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2797
0
                external_relocs,
2798
0
                internal_rela_relocs)))
2799
0
    goto error_return;
2800
2801
  /* Cache the results for next time, if we can.  */
2802
0
  if (keep_memory)
2803
0
    esdo->relocs = internal_relocs;
2804
2805
0
  free (alloc1);
2806
2807
  /* Don't free alloc2, since if it was allocated we are passing it
2808
     back (under the name of internal_relocs).  */
2809
2810
0
  return internal_relocs;
2811
2812
0
 error_return:
2813
0
  free (alloc1);
2814
0
  if (alloc2 != NULL)
2815
0
    {
2816
0
      if (keep_memory)
2817
0
  bfd_release (abfd, alloc2);
2818
0
      else
2819
0
  free (alloc2);
2820
0
    }
2821
0
  return NULL;
2822
0
}
2823
2824
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826
   struct bfd_link_info.  */
2827
2828
Elf_Internal_Rela *
2829
_bfd_elf_link_read_relocs (bfd *abfd,
2830
         asection *o,
2831
         void *external_relocs,
2832
         Elf_Internal_Rela *internal_relocs,
2833
         bool keep_memory)
2834
0
{
2835
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2836
0
           internal_relocs, keep_memory);
2837
2838
0
}
2839
2840
/* Compute the size of, and allocate space for, REL_HDR which is the
2841
   section header for a section containing relocations for O.  */
2842
2843
static bool
2844
_bfd_elf_link_size_reloc_section (bfd *abfd,
2845
          struct bfd_elf_section_reloc_data *reldata)
2846
0
{
2847
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2848
2849
  /* That allows us to calculate the size of the section.  */
2850
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2851
2852
  /* The contents field must last into write_object_contents, so we
2853
     allocate it with bfd_alloc rather than malloc.  Also since we
2854
     cannot be sure that the contents will actually be filled in,
2855
     we zero the allocated space.  */
2856
0
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2857
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2858
0
    return false;
2859
2860
0
  if (reldata->hashes == NULL && reldata->count)
2861
0
    {
2862
0
      struct elf_link_hash_entry **p;
2863
2864
0
      p = ((struct elf_link_hash_entry **)
2865
0
     bfd_zmalloc (reldata->count * sizeof (*p)));
2866
0
      if (p == NULL)
2867
0
  return false;
2868
2869
0
      reldata->hashes = p;
2870
0
    }
2871
2872
0
  return true;
2873
0
}
2874
2875
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876
   originated from the section given by INPUT_REL_HDR) to the
2877
   OUTPUT_BFD.  */
2878
2879
bool
2880
_bfd_elf_link_output_relocs (bfd *output_bfd,
2881
           asection *input_section,
2882
           Elf_Internal_Shdr *input_rel_hdr,
2883
           Elf_Internal_Rela *internal_relocs,
2884
           struct elf_link_hash_entry **rel_hash
2885
             ATTRIBUTE_UNUSED)
2886
0
{
2887
0
  Elf_Internal_Rela *irela;
2888
0
  Elf_Internal_Rela *irelaend;
2889
0
  bfd_byte *erel;
2890
0
  struct bfd_elf_section_reloc_data *output_reldata;
2891
0
  asection *output_section;
2892
0
  const struct elf_backend_data *bed;
2893
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2894
0
  struct bfd_elf_section_data *esdo;
2895
2896
0
  output_section = input_section->output_section;
2897
2898
0
  bed = get_elf_backend_data (output_bfd);
2899
0
  esdo = elf_section_data (output_section);
2900
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2901
0
    {
2902
0
      output_reldata = &esdo->rel;
2903
0
      swap_out = bed->s->swap_reloc_out;
2904
0
    }
2905
0
  else if (esdo->rela.hdr
2906
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2907
0
    {
2908
0
      output_reldata = &esdo->rela;
2909
0
      swap_out = bed->s->swap_reloca_out;
2910
0
    }
2911
0
  else
2912
0
    {
2913
0
      _bfd_error_handler
2914
  /* xgettext:c-format */
2915
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
2916
0
   output_bfd, input_section->owner, input_section);
2917
0
      bfd_set_error (bfd_error_wrong_format);
2918
0
      return false;
2919
0
    }
2920
2921
0
  erel = output_reldata->hdr->contents;
2922
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
2923
0
  irela = internal_relocs;
2924
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2925
0
          * bed->s->int_rels_per_ext_rel);
2926
0
  while (irela < irelaend)
2927
0
    {
2928
0
      (*swap_out) (output_bfd, irela, erel);
2929
0
      irela += bed->s->int_rels_per_ext_rel;
2930
0
      erel += input_rel_hdr->sh_entsize;
2931
0
    }
2932
2933
  /* Bump the counter, so that we know where to add the next set of
2934
     relocations.  */
2935
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2936
2937
0
  return true;
2938
0
}
2939

2940
/* Make weak undefined symbols in PIE dynamic.  */
2941
2942
bool
2943
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2944
         struct elf_link_hash_entry *h)
2945
0
{
2946
0
  if (bfd_link_pie (info)
2947
0
      && h->dynindx == -1
2948
0
      && h->root.type == bfd_link_hash_undefweak)
2949
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
2950
2951
0
  return true;
2952
0
}
2953
2954
/* Fix up the flags for a symbol.  This handles various cases which
2955
   can only be fixed after all the input files are seen.  This is
2956
   currently called by both adjust_dynamic_symbol and
2957
   assign_sym_version, which is unnecessary but perhaps more robust in
2958
   the face of future changes.  */
2959
2960
static bool
2961
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2962
         struct elf_info_failed *eif)
2963
0
{
2964
0
  const struct elf_backend_data *bed;
2965
2966
  /* If this symbol was mentioned in a non-ELF file, try to set
2967
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2968
     permit a non-ELF file to correctly refer to a symbol defined in
2969
     an ELF dynamic object.  */
2970
0
  if (h->non_elf)
2971
0
    {
2972
0
      while (h->root.type == bfd_link_hash_indirect)
2973
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975
0
      if (h->root.type != bfd_link_hash_defined
2976
0
    && h->root.type != bfd_link_hash_defweak)
2977
0
  {
2978
0
    h->ref_regular = 1;
2979
0
    h->ref_regular_nonweak = 1;
2980
0
  }
2981
0
      else
2982
0
  {
2983
0
    if (h->root.u.def.section->owner != NULL
2984
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
2985
0
      == bfd_target_elf_flavour))
2986
0
      {
2987
0
        h->ref_regular = 1;
2988
0
        h->ref_regular_nonweak = 1;
2989
0
      }
2990
0
    else
2991
0
      h->def_regular = 1;
2992
0
  }
2993
2994
0
      if (h->dynindx == -1
2995
0
    && (h->def_dynamic
2996
0
        || h->ref_dynamic))
2997
0
  {
2998
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999
0
      {
3000
0
        eif->failed = true;
3001
0
        return false;
3002
0
      }
3003
0
  }
3004
0
    }
3005
0
  else
3006
0
    {
3007
      /* Unfortunately, NON_ELF is only correct if the symbol
3008
   was first seen in a non-ELF file.  Fortunately, if the symbol
3009
   was first seen in an ELF file, we're probably OK unless the
3010
   symbol was defined in a non-ELF file.  Catch that case here.
3011
   FIXME: We're still in trouble if the symbol was first seen in
3012
   a dynamic object, and then later in a non-ELF regular object.  */
3013
0
      if ((h->root.type == bfd_link_hash_defined
3014
0
     || h->root.type == bfd_link_hash_defweak)
3015
0
    && !h->def_regular
3016
0
    && (h->root.u.def.section->owner != NULL
3017
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3018
0
     != bfd_target_elf_flavour)
3019
0
        : (bfd_is_abs_section (h->root.u.def.section)
3020
0
     && !h->def_dynamic)))
3021
0
  h->def_regular = 1;
3022
0
    }
3023
3024
  /* Backend specific symbol fixup.  */
3025
0
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3026
0
  if (bed->elf_backend_fixup_symbol
3027
0
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3028
0
    return false;
3029
3030
  /* If this is a final link, and the symbol was defined as a common
3031
     symbol in a regular object file, and there was no definition in
3032
     any dynamic object, then the linker will have allocated space for
3033
     the symbol in a common section but the DEF_REGULAR
3034
     flag will not have been set.  */
3035
0
  if (h->root.type == bfd_link_hash_defined
3036
0
      && !h->def_regular
3037
0
      && h->ref_regular
3038
0
      && !h->def_dynamic
3039
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3040
0
    h->def_regular = 1;
3041
3042
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3043
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3044
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3045
3046
  /* If a weak undefined symbol has non-default visibility, we also
3047
     hide it from the dynamic linker.  */
3048
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3049
0
     && h->root.type == bfd_link_hash_undefweak)
3050
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3051
3052
  /* A hidden versioned symbol in executable should be forced local if
3053
     it is is locally defined, not referenced by shared library and not
3054
     exported.  */
3055
0
  else if (bfd_link_executable (eif->info)
3056
0
     && h->versioned == versioned_hidden
3057
0
     && !eif->info->export_dynamic
3058
0
     && !h->dynamic
3059
0
     && !h->ref_dynamic
3060
0
     && h->def_regular)
3061
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3062
3063
  /* If -Bsymbolic was used (which means to bind references to global
3064
     symbols to the definition within the shared object), and this
3065
     symbol was defined in a regular object, then it actually doesn't
3066
     need a PLT entry.  Likewise, if the symbol has non-default
3067
     visibility.  If the symbol has hidden or internal visibility, we
3068
     will force it local.  */
3069
0
  else if (h->needs_plt
3070
0
     && bfd_link_pic (eif->info)
3071
0
     && is_elf_hash_table (eif->info->hash)
3072
0
     && (SYMBOLIC_BIND (eif->info, h)
3073
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3074
0
     && h->def_regular)
3075
0
    {
3076
0
      bool force_local;
3077
3078
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3079
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3080
0
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3081
0
    }
3082
3083
  /* If this is a weak defined symbol in a dynamic object, and we know
3084
     the real definition in the dynamic object, copy interesting flags
3085
     over to the real definition.  */
3086
0
  if (h->is_weakalias)
3087
0
    {
3088
0
      struct elf_link_hash_entry *def = weakdef (h);
3089
3090
      /* If the real definition is defined by a regular object file,
3091
   don't do anything special.  See the longer description in
3092
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3093
   bfd_link_hash_defined as it was when put on the alias list
3094
   then it must have originally been a versioned symbol (for
3095
   which a non-versioned indirect symbol is created) and later
3096
   a definition for the non-versioned symbol is found.  In that
3097
   case the indirection is flipped with the versioned symbol
3098
   becoming an indirect pointing at the non-versioned symbol.
3099
   Thus, not an alias any more.  */
3100
0
      if (def->def_regular
3101
0
    || def->root.type != bfd_link_hash_defined)
3102
0
  {
3103
0
    h = def;
3104
0
    while ((h = h->u.alias) != def)
3105
0
      h->is_weakalias = 0;
3106
0
  }
3107
0
      else
3108
0
  {
3109
0
    while (h->root.type == bfd_link_hash_indirect)
3110
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3111
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3112
0
          || h->root.type == bfd_link_hash_defweak);
3113
0
    BFD_ASSERT (def->def_dynamic);
3114
0
    (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3115
0
  }
3116
0
    }
3117
3118
0
  return true;
3119
0
}
3120
3121
/* Make the backend pick a good value for a dynamic symbol.  This is
3122
   called via elf_link_hash_traverse, and also calls itself
3123
   recursively.  */
3124
3125
static bool
3126
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3127
0
{
3128
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
3129
0
  struct elf_link_hash_table *htab;
3130
0
  const struct elf_backend_data *bed;
3131
3132
0
  if (! is_elf_hash_table (eif->info->hash))
3133
0
    return false;
3134
3135
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3136
0
  if (h->root.type == bfd_link_hash_indirect)
3137
0
    return true;
3138
3139
  /* Fix the symbol flags.  */
3140
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3141
0
    return false;
3142
3143
0
  htab = elf_hash_table (eif->info);
3144
0
  bed = get_elf_backend_data (htab->dynobj);
3145
3146
0
  if (h->root.type == bfd_link_hash_undefweak)
3147
0
    {
3148
0
      if (eif->info->dynamic_undefined_weak == 0)
3149
0
  (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3150
0
      else if (eif->info->dynamic_undefined_weak > 0
3151
0
         && h->ref_regular
3152
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3153
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3154
0
              h->root.root.string))
3155
0
  {
3156
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3157
0
      {
3158
0
        eif->failed = true;
3159
0
        return false;
3160
0
      }
3161
0
  }
3162
0
    }
3163
3164
  /* If this symbol does not require a PLT entry, and it is not
3165
     defined by a dynamic object, or is not referenced by a regular
3166
     object, ignore it.  We do have to handle a weak defined symbol,
3167
     even if no regular object refers to it, if we decided to add it
3168
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3169
     about symbols which are defined by one dynamic object and
3170
     referenced by another one?  */
3171
0
  if (!h->needs_plt
3172
0
      && h->type != STT_GNU_IFUNC
3173
0
      && (h->def_regular
3174
0
    || !h->def_dynamic
3175
0
    || (!h->ref_regular
3176
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3177
0
    {
3178
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3179
0
      return true;
3180
0
    }
3181
3182
  /* If we've already adjusted this symbol, don't do it again.  This
3183
     can happen via a recursive call.  */
3184
0
  if (h->dynamic_adjusted)
3185
0
    return true;
3186
3187
  /* Don't look at this symbol again.  Note that we must set this
3188
     after checking the above conditions, because we may look at a
3189
     symbol once, decide not to do anything, and then get called
3190
     recursively later after REF_REGULAR is set below.  */
3191
0
  h->dynamic_adjusted = 1;
3192
3193
  /* If this is a weak definition, and we know a real definition, and
3194
     the real symbol is not itself defined by a regular object file,
3195
     then get a good value for the real definition.  We handle the
3196
     real symbol first, for the convenience of the backend routine.
3197
3198
     Note that there is a confusing case here.  If the real definition
3199
     is defined by a regular object file, we don't get the real symbol
3200
     from the dynamic object, but we do get the weak symbol.  If the
3201
     processor backend uses a COPY reloc, then if some routine in the
3202
     dynamic object changes the real symbol, we will not see that
3203
     change in the corresponding weak symbol.  This is the way other
3204
     ELF linkers work as well, and seems to be a result of the shared
3205
     library model.
3206
3207
     I will clarify this issue.  Most SVR4 shared libraries define the
3208
     variable _timezone and define timezone as a weak synonym.  The
3209
     tzset call changes _timezone.  If you write
3210
       extern int timezone;
3211
       int _timezone = 5;
3212
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3213
     you might expect that, since timezone is a synonym for _timezone,
3214
     the same number will print both times.  However, if the processor
3215
     backend uses a COPY reloc, then actually timezone will be copied
3216
     into your process image, and, since you define _timezone
3217
     yourself, _timezone will not.  Thus timezone and _timezone will
3218
     wind up at different memory locations.  The tzset call will set
3219
     _timezone, leaving timezone unchanged.  */
3220
3221
0
  if (h->is_weakalias)
3222
0
    {
3223
0
      struct elf_link_hash_entry *def = weakdef (h);
3224
3225
      /* If we get to this point, there is an implicit reference to
3226
   the alias by a regular object file via the weak symbol H.  */
3227
0
      def->ref_regular = 1;
3228
3229
      /* Ensure that the backend adjust_dynamic_symbol function sees
3230
   the strong alias before H by recursively calling ourselves.  */
3231
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3232
0
  return false;
3233
0
    }
3234
3235
  /* If a symbol has no type and no size and does not require a PLT
3236
     entry, then we are probably about to do the wrong thing here: we
3237
     are probably going to create a COPY reloc for an empty object.
3238
     This case can arise when a shared object is built with assembly
3239
     code, and the assembly code fails to set the symbol type.  */
3240
0
  if (h->size == 0
3241
0
      && h->type == STT_NOTYPE
3242
0
      && !h->needs_plt)
3243
0
    _bfd_error_handler
3244
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3245
0
       h->root.root.string);
3246
3247
0
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3248
0
    {
3249
0
      eif->failed = true;
3250
0
      return false;
3251
0
    }
3252
3253
0
  return true;
3254
0
}
3255
3256
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3257
   DYNBSS.  */
3258
3259
bool
3260
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3261
            struct elf_link_hash_entry *h,
3262
            asection *dynbss)
3263
0
{
3264
0
  unsigned int power_of_two;
3265
0
  bfd_vma mask;
3266
0
  asection *sec = h->root.u.def.section;
3267
3268
  /* The section alignment of the definition is the maximum alignment
3269
     requirement of symbols defined in the section.  Since we don't
3270
     know the symbol alignment requirement, we start with the
3271
     maximum alignment and check low bits of the symbol address
3272
     for the minimum alignment.  */
3273
0
  power_of_two = bfd_section_alignment (sec);
3274
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3275
0
  while ((h->root.u.def.value & mask) != 0)
3276
0
    {
3277
0
       mask >>= 1;
3278
0
       --power_of_two;
3279
0
    }
3280
3281
0
  if (power_of_two > bfd_section_alignment (dynbss))
3282
0
    {
3283
      /* Adjust the section alignment if needed.  */
3284
0
      if (!bfd_set_section_alignment (dynbss, power_of_two))
3285
0
  return false;
3286
0
    }
3287
3288
  /* We make sure that the symbol will be aligned properly.  */
3289
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3290
3291
  /* Define the symbol as being at this point in DYNBSS.  */
3292
0
  h->root.u.def.section = dynbss;
3293
0
  h->root.u.def.value = dynbss->size;
3294
3295
  /* Increment the size of DYNBSS to make room for the symbol.  */
3296
0
  dynbss->size += h->size;
3297
3298
  /* No error if extern_protected_data is true.  */
3299
0
  if (h->protected_def
3300
0
      && (!info->extern_protected_data
3301
0
    || (info->extern_protected_data < 0
3302
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3303
0
    info->callbacks->einfo
3304
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3305
0
       h->root.root.string);
3306
3307
0
  return true;
3308
0
}
3309
3310
/* Adjust all external symbols pointing into SEC_MERGE sections
3311
   to reflect the object merging within the sections.  */
3312
3313
static bool
3314
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3315
0
{
3316
0
  asection *sec;
3317
3318
0
  if ((h->root.type == bfd_link_hash_defined
3319
0
       || h->root.type == bfd_link_hash_defweak)
3320
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3321
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3322
0
    {
3323
0
      bfd *output_bfd = (bfd *) data;
3324
3325
0
      h->root.u.def.value =
3326
0
  _bfd_merged_section_offset (output_bfd,
3327
0
            &h->root.u.def.section,
3328
0
            elf_section_data (sec)->sec_info,
3329
0
            h->root.u.def.value);
3330
0
    }
3331
3332
0
  return true;
3333
0
}
3334
3335
/* Returns false if the symbol referred to by H should be considered
3336
   to resolve local to the current module, and true if it should be
3337
   considered to bind dynamically.  */
3338
3339
bool
3340
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3341
         struct bfd_link_info *info,
3342
         bool not_local_protected)
3343
0
{
3344
0
  bool binding_stays_local_p;
3345
0
  const struct elf_backend_data *bed;
3346
0
  struct elf_link_hash_table *hash_table;
3347
3348
0
  if (h == NULL)
3349
0
    return false;
3350
3351
0
  while (h->root.type == bfd_link_hash_indirect
3352
0
   || h->root.type == bfd_link_hash_warning)
3353
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3354
3355
  /* If it was forced local, then clearly it's not dynamic.  */
3356
0
  if (h->dynindx == -1)
3357
0
    return false;
3358
0
  if (h->forced_local)
3359
0
    return false;
3360
3361
  /* Identify the cases where name binding rules say that a
3362
     visible symbol resolves locally.  */
3363
0
  binding_stays_local_p = (bfd_link_executable (info)
3364
0
         || SYMBOLIC_BIND (info, h));
3365
3366
0
  switch (ELF_ST_VISIBILITY (h->other))
3367
0
    {
3368
0
    case STV_INTERNAL:
3369
0
    case STV_HIDDEN:
3370
0
      return false;
3371
3372
0
    case STV_PROTECTED:
3373
0
      hash_table = elf_hash_table (info);
3374
0
      if (!is_elf_hash_table (&hash_table->root))
3375
0
  return false;
3376
3377
0
      bed = get_elf_backend_data (hash_table->dynobj);
3378
3379
      /* Proper resolution for function pointer equality may require
3380
   that these symbols perhaps be resolved dynamically, even though
3381
   we should be resolving them to the current module.  */
3382
0
      if (!not_local_protected || !bed->is_function_type (h->type))
3383
0
  binding_stays_local_p = true;
3384
0
      break;
3385
3386
0
    default:
3387
0
      break;
3388
0
    }
3389
3390
  /* If it isn't defined locally, then clearly it's dynamic.  */
3391
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3392
0
    return true;
3393
3394
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3395
     us that it remains local.  */
3396
0
  return !binding_stays_local_p;
3397
0
}
3398
3399
/* Return true if the symbol referred to by H should be considered
3400
   to resolve local to the current module, and false otherwise.  Differs
3401
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3402
   undefined symbols.  The two functions are virtually identical except
3403
   for the place where dynindx == -1 is tested.  If that test is true,
3404
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3405
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3406
   defined symbols.
3407
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3408
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3409
   treatment of undefined weak symbols.  For those that do not make
3410
   undefined weak symbols dynamic, both functions may return false.  */
3411
3412
bool
3413
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3414
            struct bfd_link_info *info,
3415
            bool local_protected)
3416
0
{
3417
0
  const struct elf_backend_data *bed;
3418
0
  struct elf_link_hash_table *hash_table;
3419
3420
  /* If it's a local sym, of course we resolve locally.  */
3421
0
  if (h == NULL)
3422
0
    return true;
3423
3424
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3425
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3426
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3427
0
    return true;
3428
3429
  /* Forced local symbols resolve locally.  */
3430
0
  if (h->forced_local)
3431
0
    return true;
3432
3433
  /* Common symbols that become definitions don't get the DEF_REGULAR
3434
     flag set, so test it first, and don't bail out.  */
3435
0
  if (ELF_COMMON_DEF_P (h))
3436
0
    /* Do nothing.  */;
3437
  /* If we don't have a definition in a regular file, then we can't
3438
     resolve locally.  The sym is either undefined or dynamic.  */
3439
0
  else if (!h->def_regular)
3440
0
    return false;
3441
3442
  /* Non-dynamic symbols resolve locally.  */
3443
0
  if (h->dynindx == -1)
3444
0
    return true;
3445
3446
  /* At this point, we know the symbol is defined and dynamic.  In an
3447
     executable it must resolve locally, likewise when building symbolic
3448
     shared libraries.  */
3449
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3450
0
    return true;
3451
3452
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3453
     with default visibility might not resolve locally.  */
3454
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3455
0
    return false;
3456
3457
0
  hash_table = elf_hash_table (info);
3458
0
  if (!is_elf_hash_table (&hash_table->root))
3459
0
    return true;
3460
3461
  /* STV_PROTECTED symbols with indirect external access are local. */
3462
0
  if (info->indirect_extern_access > 0)
3463
0
    return true;
3464
3465
0
  bed = get_elf_backend_data (hash_table->dynobj);
3466
3467
  /* If extern_protected_data is false, STV_PROTECTED non-function
3468
     symbols are local.  */
3469
0
  if ((!info->extern_protected_data
3470
0
       || (info->extern_protected_data < 0
3471
0
     && !bed->extern_protected_data))
3472
0
      && !bed->is_function_type (h->type))
3473
0
    return true;
3474
3475
  /* Function pointer equality tests may require that STV_PROTECTED
3476
     symbols be treated as dynamic symbols.  If the address of a
3477
     function not defined in an executable is set to that function's
3478
     plt entry in the executable, then the address of the function in
3479
     a shared library must also be the plt entry in the executable.  */
3480
0
  return local_protected;
3481
0
}
3482
3483
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3484
   aligned.  Returns the first TLS output section.  */
3485
3486
struct bfd_section *
3487
_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3488
0
{
3489
0
  struct bfd_section *sec, *tls;
3490
0
  unsigned int align = 0;
3491
3492
0
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
3493
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3494
0
      break;
3495
0
  tls = sec;
3496
3497
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3498
0
    if (sec->alignment_power > align)
3499
0
      align = sec->alignment_power;
3500
3501
0
  elf_hash_table (info)->tls_sec = tls;
3502
3503
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3504
     alignment, so that the tls segment starts aligned.  */
3505
0
  if (tls != NULL)
3506
0
    tls->alignment_power = align;
3507
3508
0
  return tls;
3509
0
}
3510
3511
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3512
static bool
3513
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3514
          Elf_Internal_Sym *sym)
3515
0
{
3516
0
  const struct elf_backend_data *bed;
3517
3518
  /* Local symbols do not count, but target specific ones might.  */
3519
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3520
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3521
0
    return false;
3522
3523
0
  bed = get_elf_backend_data (abfd);
3524
  /* Function symbols do not count.  */
3525
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3526
0
    return false;
3527
3528
  /* If the section is undefined, then so is the symbol.  */
3529
0
  if (sym->st_shndx == SHN_UNDEF)
3530
0
    return false;
3531
3532
  /* If the symbol is defined in the common section, then
3533
     it is a common definition and so does not count.  */
3534
0
  if (bed->common_definition (sym))
3535
0
    return false;
3536
3537
  /* If the symbol is in a target specific section then we
3538
     must rely upon the backend to tell us what it is.  */
3539
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3540
    /* FIXME - this function is not coded yet:
3541
3542
       return _bfd_is_global_symbol_definition (abfd, sym);
3543
3544
       Instead for now assume that the definition is not global,
3545
       Even if this is wrong, at least the linker will behave
3546
       in the same way that it used to do.  */
3547
0
    return false;
3548
3549
0
  return true;
3550
0
}
3551
3552
/* Search the symbol table of the archive element of the archive ABFD
3553
   whose archive map contains a mention of SYMDEF, and determine if
3554
   the symbol is defined in this element.  */
3555
static bool
3556
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3557
0
{
3558
0
  Elf_Internal_Shdr * hdr;
3559
0
  size_t symcount;
3560
0
  size_t extsymcount;
3561
0
  size_t extsymoff;
3562
0
  Elf_Internal_Sym *isymbuf;
3563
0
  Elf_Internal_Sym *isym;
3564
0
  Elf_Internal_Sym *isymend;
3565
0
  bool result;
3566
3567
0
  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3568
0
  if (abfd == NULL)
3569
0
    return false;
3570
3571
0
  if (! bfd_check_format (abfd, bfd_object))
3572
0
    return false;
3573
3574
0
  if (elf_use_dt_symtab_p (abfd))
3575
0
    {
3576
0
      bfd_set_error (bfd_error_wrong_format);
3577
0
      return false;
3578
0
    }
3579
3580
  /* Select the appropriate symbol table.  If we don't know if the
3581
     object file is an IR object, give linker LTO plugin a chance to
3582
     get the correct symbol table.  */
3583
0
  if (abfd->plugin_format == bfd_plugin_yes
3584
0
#if BFD_SUPPORTS_PLUGINS
3585
0
      || (abfd->plugin_format == bfd_plugin_unknown
3586
0
    && bfd_link_plugin_object_p (abfd))
3587
0
#endif
3588
0
      )
3589
0
    {
3590
      /* Use the IR symbol table if the object has been claimed by
3591
   plugin.  */
3592
0
      abfd = abfd->plugin_dummy_bfd;
3593
0
      hdr = &elf_tdata (abfd)->symtab_hdr;
3594
0
    }
3595
0
  else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3596
0
    hdr = &elf_tdata (abfd)->symtab_hdr;
3597
0
  else
3598
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3599
3600
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3601
3602
  /* The sh_info field of the symtab header tells us where the
3603
     external symbols start.  We don't care about the local symbols.  */
3604
0
  if (elf_bad_symtab (abfd))
3605
0
    {
3606
0
      extsymcount = symcount;
3607
0
      extsymoff = 0;
3608
0
    }
3609
0
  else
3610
0
    {
3611
0
      extsymcount = symcount - hdr->sh_info;
3612
0
      extsymoff = hdr->sh_info;
3613
0
    }
3614
3615
0
  if (extsymcount == 0)
3616
0
    return false;
3617
3618
  /* Read in the symbol table.  */
3619
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3620
0
          NULL, NULL, NULL);
3621
0
  if (isymbuf == NULL)
3622
0
    return false;
3623
3624
  /* Scan the symbol table looking for SYMDEF.  */
3625
0
  result = false;
3626
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3627
0
    {
3628
0
      const char *name;
3629
3630
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3631
0
                isym->st_name);
3632
0
      if (name == NULL)
3633
0
  break;
3634
3635
0
      if (strcmp (name, symdef->name) == 0)
3636
0
  {
3637
0
    result = is_global_data_symbol_definition (abfd, isym);
3638
0
    break;
3639
0
  }
3640
0
    }
3641
3642
0
  free (isymbuf);
3643
3644
0
  return result;
3645
0
}
3646

3647
/* Add an entry to the .dynamic table.  */
3648
3649
bool
3650
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3651
          bfd_vma tag,
3652
          bfd_vma val)
3653
0
{
3654
0
  struct elf_link_hash_table *hash_table;
3655
0
  const struct elf_backend_data *bed;
3656
0
  asection *s;
3657
0
  bfd_size_type newsize;
3658
0
  bfd_byte *newcontents;
3659
0
  Elf_Internal_Dyn dyn;
3660
3661
0
  hash_table = elf_hash_table (info);
3662
0
  if (! is_elf_hash_table (&hash_table->root))
3663
0
    return false;
3664
3665
0
  if (tag == DT_RELA || tag == DT_REL)
3666
0
    hash_table->dynamic_relocs = true;
3667
3668
0
  bed = get_elf_backend_data (hash_table->dynobj);
3669
0
  s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3670
0
  BFD_ASSERT (s != NULL);
3671
3672
0
  newsize = s->size + bed->s->sizeof_dyn;
3673
0
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3674
0
  if (newcontents == NULL)
3675
0
    return false;
3676
3677
0
  dyn.d_tag = tag;
3678
0
  dyn.d_un.d_val = val;
3679
0
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3680
3681
0
  s->size = newsize;
3682
0
  s->contents = newcontents;
3683
3684
0
  return true;
3685
0
}
3686
3687
/* Strip zero-sized dynamic sections.  */
3688
3689
bool
3690
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3691
0
{
3692
0
  struct elf_link_hash_table *hash_table;
3693
0
  const struct elf_backend_data *bed;
3694
0
  asection *s, *sdynamic, **pp;
3695
0
  asection *rela_dyn, *rel_dyn;
3696
0
  Elf_Internal_Dyn dyn;
3697
0
  bfd_byte *extdyn, *next;
3698
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3699
0
  bool strip_zero_sized;
3700
0
  bool strip_zero_sized_plt;
3701
3702
0
  if (bfd_link_relocatable (info))
3703
0
    return true;
3704
3705
0
  hash_table = elf_hash_table (info);
3706
0
  if (!is_elf_hash_table (&hash_table->root))
3707
0
    return false;
3708
3709
0
  if (!hash_table->dynobj)
3710
0
    return true;
3711
3712
0
  sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3713
0
  if (!sdynamic)
3714
0
    return true;
3715
3716
0
  bed = get_elf_backend_data (hash_table->dynobj);
3717
0
  swap_dyn_in = bed->s->swap_dyn_in;
3718
3719
0
  strip_zero_sized = false;
3720
0
  strip_zero_sized_plt = false;
3721
3722
  /* Strip zero-sized dynamic sections.  */
3723
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3724
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3725
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3726
0
    if (s->size == 0
3727
0
  && (s == rela_dyn
3728
0
      || s == rel_dyn
3729
0
      || s == hash_table->srelplt->output_section
3730
0
      || s == hash_table->splt->output_section))
3731
0
      {
3732
0
  *pp = s->next;
3733
0
  info->output_bfd->section_count--;
3734
0
  strip_zero_sized = true;
3735
0
  if (s == rela_dyn)
3736
0
    s = rela_dyn;
3737
0
  if (s == rel_dyn)
3738
0
    s = rel_dyn;
3739
0
  else if (s == hash_table->splt->output_section)
3740
0
    {
3741
0
      s = hash_table->splt;
3742
0
      strip_zero_sized_plt = true;
3743
0
    }
3744
0
  else
3745
0
    s = hash_table->srelplt;
3746
0
  s->flags |= SEC_EXCLUDE;
3747
0
  s->output_section = bfd_abs_section_ptr;
3748
0
      }
3749
0
    else
3750
0
      pp = &s->next;
3751
3752
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3753
0
    for (extdyn = sdynamic->contents;
3754
0
   extdyn < sdynamic->contents + sdynamic->size;
3755
0
   extdyn = next)
3756
0
      {
3757
0
  next = extdyn + bed->s->sizeof_dyn;
3758
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3759
0
  switch (dyn.d_tag)
3760
0
    {
3761
0
    default:
3762
0
      break;
3763
0
    case DT_JMPREL:
3764
0
    case DT_PLTRELSZ:
3765
0
    case DT_PLTREL:
3766
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3767
         the procedure linkage table (the .plt section) has been
3768
         removed.  */
3769
0
      memmove (extdyn, next,
3770
0
         sdynamic->size - (next - sdynamic->contents));
3771
0
      next = extdyn;
3772
0
    }
3773
0
      }
3774
3775
0
  if (strip_zero_sized)
3776
0
    {
3777
      /* Regenerate program headers.  */
3778
0
      elf_seg_map (info->output_bfd) = NULL;
3779
0
      return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3780
0
            NULL);
3781
0
    }
3782
3783
0
  return true;
3784
0
}
3785
3786
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3787
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3788
3789
int
3790
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3791
0
{
3792
0
  struct elf_link_hash_table *hash_table;
3793
0
  size_t strindex;
3794
0
  const char *soname;
3795
3796
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3797
0
    return -1;
3798
3799
0
  hash_table = elf_hash_table (info);
3800
0
  soname = elf_dt_name (abfd);
3801
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3802
0
  if (strindex == (size_t) -1)
3803
0
    return -1;
3804
3805
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3806
0
    {
3807
0
      asection *sdyn;
3808
0
      const struct elf_backend_data *bed;
3809
0
      bfd_byte *extdyn;
3810
3811
0
      bed = get_elf_backend_data (hash_table->dynobj);
3812
0
      sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3813
0
      if (sdyn != NULL && sdyn->size != 0)
3814
0
  for (extdyn = sdyn->contents;
3815
0
       extdyn < sdyn->contents + sdyn->size;
3816
0
       extdyn += bed->s->sizeof_dyn)
3817
0
    {
3818
0
      Elf_Internal_Dyn dyn;
3819
3820
0
      bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3821
0
      if (dyn.d_tag == DT_NEEDED
3822
0
    && dyn.d_un.d_val == strindex)
3823
0
        {
3824
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3825
0
    return 1;
3826
0
        }
3827
0
    }
3828
0
    }
3829
3830
0
  if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3831
0
    return -1;
3832
3833
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3834
0
    return -1;
3835
3836
0
  return 0;
3837
0
}
3838
3839
/* Return true if SONAME is on the needed list between NEEDED and STOP
3840
   (or the end of list if STOP is NULL), and needed by a library that
3841
   will be loaded.  */
3842
3843
static bool
3844
on_needed_list (const char *soname,
3845
    struct bfd_link_needed_list *needed,
3846
    struct bfd_link_needed_list *stop)
3847
0
{
3848
0
  struct bfd_link_needed_list *look;
3849
0
  for (look = needed; look != stop; look = look->next)
3850
0
    if (strcmp (soname, look->name) == 0
3851
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3852
      /* If needed by a library that itself is not directly
3853
         needed, recursively check whether that library is
3854
         indirectly needed.  Since we add DT_NEEDED entries to
3855
         the end of the list, library dependencies appear after
3856
         the library.  Therefore search prior to the current
3857
         LOOK, preventing possible infinite recursion.  */
3858
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
3859
0
      return true;
3860
3861
0
  return false;
3862
0
}
3863
3864
/* Sort symbol by value, section, size, and type.  */
3865
static int
3866
elf_sort_symbol (const void *arg1, const void *arg2)
3867
0
{
3868
0
  const struct elf_link_hash_entry *h1;
3869
0
  const struct elf_link_hash_entry *h2;
3870
0
  bfd_signed_vma vdiff;
3871
0
  int sdiff;
3872
0
  const char *n1;
3873
0
  const char *n2;
3874
3875
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
3876
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
3877
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
3878
0
  if (vdiff != 0)
3879
0
    return vdiff > 0 ? 1 : -1;
3880
3881
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3882
0
  if (sdiff != 0)
3883
0
    return sdiff;
3884
3885
  /* Sort so that sized symbols are selected over zero size symbols.  */
3886
0
  vdiff = h1->size - h2->size;
3887
0
  if (vdiff != 0)
3888
0
    return vdiff > 0 ? 1 : -1;
3889
3890
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
3891
0
  if (h1->type != h2->type)
3892
0
    return h1->type - h2->type;
3893
3894
  /* If symbols are properly sized and typed, and multiple strong
3895
     aliases are not defined in a shared library by the user we
3896
     shouldn't get here.  Unfortunately linker script symbols like
3897
     __bss_start sometimes match a user symbol defined at the start of
3898
     .bss without proper size and type.  We'd like to preference the
3899
     user symbol over reserved system symbols.  Sort on leading
3900
     underscores.  */
3901
0
  n1 = h1->root.root.string;
3902
0
  n2 = h2->root.root.string;
3903
0
  while (*n1 == *n2)
3904
0
    {
3905
0
      if (*n1 == 0)
3906
0
  break;
3907
0
      ++n1;
3908
0
      ++n2;
3909
0
    }
3910
0
  if (*n1 == '_')
3911
0
    return -1;
3912
0
  if (*n2 == '_')
3913
0
    return 1;
3914
3915
  /* Final sort on name selects user symbols like '_u' over reserved
3916
     system symbols like '_Z' and also will avoid qsort instability.  */
3917
0
  return *n1 - *n2;
3918
0
}
3919
3920
/* This function is used to adjust offsets into .dynstr for
3921
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
3922
3923
static bool
3924
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3925
0
{
3926
0
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3927
3928
0
  if (h->dynindx != -1)
3929
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3930
0
  return true;
3931
0
}
3932
3933
/* Assign string offsets in .dynstr, update all structures referencing
3934
   them.  */
3935
3936
static bool
3937
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3938
0
{
3939
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
3940
0
  struct elf_link_local_dynamic_entry *entry;
3941
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
3942
0
  bfd *dynobj = hash_table->dynobj;
3943
0
  asection *sdyn;
3944
0
  bfd_size_type size;
3945
0
  const struct elf_backend_data *bed;
3946
0
  bfd_byte *extdyn;
3947
3948
0
  _bfd_elf_strtab_finalize (dynstr);
3949
0
  size = _bfd_elf_strtab_size (dynstr);
3950
3951
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
3952
3953
0
  if (info->callbacks->examine_strtab)
3954
0
    info->callbacks->examine_strtab (dynstr);
3955
3956
0
  bed = get_elf_backend_data (dynobj);
3957
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3958
0
  BFD_ASSERT (sdyn != NULL);
3959
3960
  /* Update all .dynamic entries referencing .dynstr strings.  */
3961
0
  for (extdyn = sdyn->contents;
3962
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3963
0
       extdyn += bed->s->sizeof_dyn)
3964
0
    {
3965
0
      Elf_Internal_Dyn dyn;
3966
3967
0
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3968
0
      switch (dyn.d_tag)
3969
0
  {
3970
0
  case DT_STRSZ:
3971
0
    dyn.d_un.d_val = size;
3972
0
    break;
3973
0
  case DT_NEEDED:
3974
0
  case DT_SONAME:
3975
0
  case DT_RPATH:
3976
0
  case DT_RUNPATH:
3977
0
  case DT_FILTER:
3978
0
  case DT_AUXILIARY:
3979
0
  case DT_AUDIT:
3980
0
  case DT_DEPAUDIT:
3981
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3982
0
    break;
3983
0
  default:
3984
0
    continue;
3985
0
  }
3986
0
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3987
0
    }
3988
3989
  /* Now update local dynamic symbols.  */
3990
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3991
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3992
0
              entry->isym.st_name);
3993
3994
  /* And the rest of dynamic symbols.  */
3995
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3996
3997
  /* Adjust version definitions.  */
3998
0
  if (elf_tdata (output_bfd)->cverdefs)
3999
0
    {
4000
0
      asection *s;
4001
0
      bfd_byte *p;
4002
0
      size_t i;
4003
0
      Elf_Internal_Verdef def;
4004
0
      Elf_Internal_Verdaux defaux;
4005
4006
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4007
0
      p = s->contents;
4008
0
      do
4009
0
  {
4010
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4011
0
           &def);
4012
0
    p += sizeof (Elf_External_Verdef);
4013
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4014
0
      continue;
4015
0
    for (i = 0; i < def.vd_cnt; ++i)
4016
0
      {
4017
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4018
0
          (Elf_External_Verdaux *) p, &defaux);
4019
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4020
0
              defaux.vda_name);
4021
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4022
0
           &defaux, (Elf_External_Verdaux *) p);
4023
0
        p += sizeof (Elf_External_Verdaux);
4024
0
      }
4025
0
  }
4026
0
      while (def.vd_next);
4027
0
    }
4028
4029
  /* Adjust version references.  */
4030
0
  if (elf_tdata (output_bfd)->verref)
4031
0
    {
4032
0
      asection *s;
4033
0
      bfd_byte *p;
4034
0
      size_t i;
4035
0
      Elf_Internal_Verneed need;
4036
0
      Elf_Internal_Vernaux needaux;
4037
4038
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4039
0
      p = s->contents;
4040
0
      do
4041
0
  {
4042
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4043
0
            &need);
4044
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4045
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4046
0
             (Elf_External_Verneed *) p);
4047
0
    p += sizeof (Elf_External_Verneed);
4048
0
    for (i = 0; i < need.vn_cnt; ++i)
4049
0
      {
4050
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4051
0
          (Elf_External_Vernaux *) p, &needaux);
4052
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4053
0
               needaux.vna_name);
4054
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4055
0
           &needaux,
4056
0
           (Elf_External_Vernaux *) p);
4057
0
        p += sizeof (Elf_External_Vernaux);
4058
0
      }
4059
0
  }
4060
0
      while (need.vn_next);
4061
0
    }
4062
4063
0
  return true;
4064
0
}
4065

4066
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4067
   The default is to only match when the INPUT and OUTPUT are exactly
4068
   the same target.  */
4069
4070
bool
4071
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4072
            const bfd_target *output)
4073
0
{
4074
0
  return input == output;
4075
0
}
4076
4077
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4078
   This version is used when different targets for the same architecture
4079
   are virtually identical.  */
4080
4081
bool
4082
_bfd_elf_relocs_compatible (const bfd_target *input,
4083
          const bfd_target *output)
4084
0
{
4085
0
  const struct elf_backend_data *obed, *ibed;
4086
4087
0
  if (input == output)
4088
0
    return true;
4089
4090
0
  ibed = xvec_get_elf_backend_data (input);
4091
0
  obed = xvec_get_elf_backend_data (output);
4092
4093
0
  if (ibed->arch != obed->arch)
4094
0
    return false;
4095
4096
  /* If both backends are using this function, deem them compatible.  */
4097
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4098
0
}
4099
4100
/* Make a special call to the linker "notice" function to tell it that
4101
   we are about to handle an as-needed lib, or have finished
4102
   processing the lib.  */
4103
4104
bool
4105
_bfd_elf_notice_as_needed (bfd *ibfd,
4106
         struct bfd_link_info *info,
4107
         enum notice_asneeded_action act)
4108
0
{
4109
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4110
0
}
4111
4112
/* Call ACTION on each relocation in an ELF object file.  */
4113
4114
bool
4115
_bfd_elf_link_iterate_on_relocs
4116
  (bfd *abfd, struct bfd_link_info *info,
4117
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4118
       const Elf_Internal_Rela *))
4119
0
{
4120
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4121
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4122
4123
  /* If this object is the same format as the output object, and it is
4124
     not a shared library, then let the backend look through the
4125
     relocs.
4126
4127
     This is required to build global offset table entries and to
4128
     arrange for dynamic relocs.  It is not required for the
4129
     particular common case of linking non PIC code, even when linking
4130
     against shared libraries, but unfortunately there is no way of
4131
     knowing whether an object file has been compiled PIC or not.
4132
     Looking through the relocs is not particularly time consuming.
4133
     The problem is that we must either (1) keep the relocs in memory,
4134
     which causes the linker to require additional runtime memory or
4135
     (2) read the relocs twice from the input file, which wastes time.
4136
     This would be a good case for using mmap.
4137
4138
     I have no idea how to handle linking PIC code into a file of a
4139
     different format.  It probably can't be done.  */
4140
0
  if ((abfd->flags & DYNAMIC) == 0
4141
0
      && is_elf_hash_table (&htab->root)
4142
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4143
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4144
0
    {
4145
0
      asection *o;
4146
4147
0
      for (o = abfd->sections; o != NULL; o = o->next)
4148
0
  {
4149
0
    Elf_Internal_Rela *internal_relocs;
4150
0
    bool ok;
4151
4152
    /* Don't check relocations in excluded sections.  Don't do
4153
       anything special with non-loaded, non-alloced sections.
4154
       In particular, any relocs in such sections should not
4155
       affect GOT and PLT reference counting (ie.  we don't
4156
       allow them to create GOT or PLT entries), there's no
4157
       possibility or desire to optimize TLS relocs, and
4158
       there's not much point in propagating relocs to shared
4159
       libs that the dynamic linker won't relocate.  */
4160
0
    if ((o->flags & SEC_ALLOC) == 0
4161
0
        || (o->flags & SEC_RELOC) == 0
4162
0
        || (o->flags & SEC_EXCLUDE) != 0
4163
0
        || o->reloc_count == 0
4164
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4165
0
      && (o->flags & SEC_DEBUGGING) != 0)
4166
0
        || bfd_is_abs_section (o->output_section))
4167
0
      continue;
4168
4169
0
    internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4170
0
                  o, NULL,
4171
0
                  NULL,
4172
0
                  _bfd_link_keep_memory (info));
4173
0
    if (internal_relocs == NULL)
4174
0
      return false;
4175
4176
0
    ok = action (abfd, info, o, internal_relocs);
4177
4178
0
    if (elf_section_data (o)->relocs != internal_relocs)
4179
0
      free (internal_relocs);
4180
4181
0
    if (! ok)
4182
0
      return false;
4183
0
  }
4184
0
    }
4185
4186
0
  return true;
4187
0
}
4188
4189
/* Check relocations in an ELF object file.  This is called after
4190
   all input files have been opened.  */
4191
4192
bool
4193
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4194
0
{
4195
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4196
0
  if (bed->check_relocs != NULL)
4197
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4198
0
              bed->check_relocs);
4199
0
  return true;
4200
0
}
4201
4202
/* Add symbols from an ELF object file to the linker hash table.  */
4203
4204
static bool
4205
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4206
0
{
4207
0
  Elf_Internal_Ehdr *ehdr;
4208
0
  Elf_Internal_Shdr *hdr;
4209
0
  size_t symcount;
4210
0
  size_t extsymcount;
4211
0
  size_t extsymoff;
4212
0
  struct elf_link_hash_entry **sym_hash;
4213
0
  bool dynamic;
4214
0
  Elf_External_Versym *extversym = NULL;
4215
0
  Elf_External_Versym *extversym_end = NULL;
4216
0
  Elf_External_Versym *ever;
4217
0
  struct elf_link_hash_entry *weaks;
4218
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4219
0
  size_t nondeflt_vers_cnt = 0;
4220
0
  Elf_Internal_Sym *isymbuf = NULL;
4221
0
  Elf_Internal_Sym *isym;
4222
0
  Elf_Internal_Sym *isymend;
4223
0
  const struct elf_backend_data *bed;
4224
0
  bool add_needed;
4225
0
  struct elf_link_hash_table *htab;
4226
0
  void *alloc_mark = NULL;
4227
0
  struct bfd_hash_entry **old_table = NULL;
4228
0
  unsigned int old_size = 0;
4229
0
  unsigned int old_count = 0;
4230
0
  void *old_tab = NULL;
4231
0
  void *old_ent;
4232
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4233
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4234
0
  void *old_strtab = NULL;
4235
0
  size_t tabsize = 0;
4236
0
  asection *s;
4237
0
  bool just_syms;
4238
4239
0
  htab = elf_hash_table (info);
4240
0
  bed = get_elf_backend_data (abfd);
4241
4242
0
  if (elf_use_dt_symtab_p (abfd))
4243
0
    {
4244
0
      bfd_set_error (bfd_error_wrong_format);
4245
0
      return false;
4246
0
    }
4247
4248
0
  if ((abfd->flags & DYNAMIC) == 0)
4249
0
    dynamic = false;
4250
0
  else
4251
0
    {
4252
0
      dynamic = true;
4253
4254
      /* You can't use -r against a dynamic object.  Also, there's no
4255
   hope of using a dynamic object which does not exactly match
4256
   the format of the output file.  */
4257
0
      if (bfd_link_relocatable (info)
4258
0
    || !is_elf_hash_table (&htab->root)
4259
0
    || info->output_bfd->xvec != abfd->xvec)
4260
0
  {
4261
0
    if (bfd_link_relocatable (info))
4262
0
      bfd_set_error (bfd_error_invalid_operation);
4263
0
    else
4264
0
      bfd_set_error (bfd_error_wrong_format);
4265
0
    goto error_return;
4266
0
  }
4267
0
    }
4268
4269
0
  ehdr = elf_elfheader (abfd);
4270
0
  if (info->warn_alternate_em
4271
0
      && bed->elf_machine_code != ehdr->e_machine
4272
0
      && ((bed->elf_machine_alt1 != 0
4273
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4274
0
    || (bed->elf_machine_alt2 != 0
4275
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4276
0
    _bfd_error_handler
4277
      /* xgettext:c-format */
4278
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4279
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4280
4281
  /* As a GNU extension, any input sections which are named
4282
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4283
     symbol.  This differs from .gnu.warning sections, which generate
4284
     warnings when they are included in an output file.  */
4285
  /* PR 12761: Also generate this warning when building shared libraries.  */
4286
0
  for (s = abfd->sections; s != NULL; s = s->next)
4287
0
    {
4288
0
      const char *name;
4289
4290
0
      name = bfd_section_name (s);
4291
0
      if (startswith (name, ".gnu.warning."))
4292
0
  {
4293
0
    char *msg;
4294
0
    bfd_size_type sz;
4295
4296
0
    name += sizeof ".gnu.warning." - 1;
4297
4298
    /* If this is a shared object, then look up the symbol
4299
       in the hash table.  If it is there, and it is already
4300
       been defined, then we will not be using the entry
4301
       from this shared object, so we don't need to warn.
4302
       FIXME: If we see the definition in a regular object
4303
       later on, we will warn, but we shouldn't.  The only
4304
       fix is to keep track of what warnings we are supposed
4305
       to emit, and then handle them all at the end of the
4306
       link.  */
4307
0
    if (dynamic)
4308
0
      {
4309
0
        struct elf_link_hash_entry *h;
4310
4311
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4312
4313
        /* FIXME: What about bfd_link_hash_common?  */
4314
0
        if (h != NULL
4315
0
      && (h->root.type == bfd_link_hash_defined
4316
0
          || h->root.type == bfd_link_hash_defweak))
4317
0
    continue;
4318
0
      }
4319
4320
0
    sz = s->size;
4321
0
    msg = (char *) bfd_alloc (abfd, sz + 1);
4322
0
    if (msg == NULL)
4323
0
      goto error_return;
4324
4325
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4326
0
      goto error_return;
4327
4328
0
    msg[sz] = '\0';
4329
4330
0
    if (! (_bfd_generic_link_add_one_symbol
4331
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4332
0
      false, bed->collect, NULL)))
4333
0
      goto error_return;
4334
4335
0
    if (bfd_link_executable (info))
4336
0
      {
4337
        /* Clobber the section size so that the warning does
4338
     not get copied into the output file.  */
4339
0
        s->size = 0;
4340
4341
        /* Also set SEC_EXCLUDE, so that symbols defined in
4342
     the warning section don't get copied to the output.  */
4343
0
        s->flags |= SEC_EXCLUDE;
4344
0
      }
4345
0
  }
4346
0
    }
4347
4348
0
  just_syms = ((s = abfd->sections) != NULL
4349
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4350
4351
0
  add_needed = true;
4352
0
  if (! dynamic)
4353
0
    {
4354
      /* If we are creating a shared library, create all the dynamic
4355
   sections immediately.  We need to attach them to something,
4356
   so we attach them to this BFD, provided it is the right
4357
   format and is not from ld --just-symbols.  Always create the
4358
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4359
   are no input BFD's of the same format as the output, we can't
4360
   make a shared library.  */
4361
0
      if (!just_syms
4362
0
    && (bfd_link_pic (info)
4363
0
        || (!bfd_link_relocatable (info)
4364
0
      && info->nointerp
4365
0
      && (info->export_dynamic || info->dynamic)))
4366
0
    && is_elf_hash_table (&htab->root)
4367
0
    && info->output_bfd->xvec == abfd->xvec
4368
0
    && !htab->dynamic_sections_created)
4369
0
  {
4370
0
    if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4371
0
      goto error_return;
4372
0
  }
4373
0
    }
4374
0
  else if (!is_elf_hash_table (&htab->root))
4375
0
    goto error_return;
4376
0
  else
4377
0
    {
4378
0
      const char *soname = NULL;
4379
0
      char *audit = NULL;
4380
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4381
0
      const Elf_Internal_Phdr *phdr;
4382
0
      struct elf_link_loaded_list *loaded_lib;
4383
4384
      /* ld --just-symbols and dynamic objects don't mix very well.
4385
   ld shouldn't allow it.  */
4386
0
      if (just_syms)
4387
0
  abort ();
4388
4389
      /* If this dynamic lib was specified on the command line with
4390
   --as-needed in effect, then we don't want to add a DT_NEEDED
4391
   tag unless the lib is actually used.  Similary for libs brought
4392
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4393
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4394
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4395
   all.  */
4396
0
      add_needed = (elf_dyn_lib_class (abfd)
4397
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4398
0
           | DYN_NO_NEEDED)) == 0;
4399
4400
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4401
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4402
0
  {
4403
0
    bfd_byte *dynbuf;
4404
0
    bfd_byte *extdyn;
4405
0
    unsigned int elfsec;
4406
0
    unsigned long shlink;
4407
4408
0
    if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4409
0
      {
4410
0
      error_free_dyn:
4411
0
        free (dynbuf);
4412
0
        goto error_return;
4413
0
      }
4414
4415
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4416
0
    if (elfsec == SHN_BAD)
4417
0
      goto error_free_dyn;
4418
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4419
4420
0
    for (extdyn = dynbuf;
4421
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4422
0
         extdyn += bed->s->sizeof_dyn)
4423
0
      {
4424
0
        Elf_Internal_Dyn dyn;
4425
4426
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4427
0
        if (dyn.d_tag == DT_SONAME)
4428
0
    {
4429
0
      unsigned int tagv = dyn.d_un.d_val;
4430
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4431
0
      if (soname == NULL)
4432
0
        goto error_free_dyn;
4433
0
    }
4434
0
        if (dyn.d_tag == DT_NEEDED)
4435
0
    {
4436
0
      struct bfd_link_needed_list *n, **pn;
4437
0
      char *fnm, *anm;
4438
0
      unsigned int tagv = dyn.d_un.d_val;
4439
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4440
4441
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4442
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4443
0
      if (n == NULL || fnm == NULL)
4444
0
        goto error_free_dyn;
4445
0
      amt = strlen (fnm) + 1;
4446
0
      anm = (char *) bfd_alloc (abfd, amt);
4447
0
      if (anm == NULL)
4448
0
        goto error_free_dyn;
4449
0
      memcpy (anm, fnm, amt);
4450
0
      n->name = anm;
4451
0
      n->by = abfd;
4452
0
      n->next = NULL;
4453
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4454
0
        ;
4455
0
      *pn = n;
4456
0
    }
4457
0
        if (dyn.d_tag == DT_RUNPATH)
4458
0
    {
4459
0
      struct bfd_link_needed_list *n, **pn;
4460
0
      char *fnm, *anm;
4461
0
      unsigned int tagv = dyn.d_un.d_val;
4462
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4463
4464
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4465
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4466
0
      if (n == NULL || fnm == NULL)
4467
0
        goto error_free_dyn;
4468
0
      amt = strlen (fnm) + 1;
4469
0
      anm = (char *) bfd_alloc (abfd, amt);
4470
0
      if (anm == NULL)
4471
0
        goto error_free_dyn;
4472
0
      memcpy (anm, fnm, amt);
4473
0
      n->name = anm;
4474
0
      n->by = abfd;
4475
0
      n->next = NULL;
4476
0
      for (pn = & runpath;
4477
0
           *pn != NULL;
4478
0
           pn = &(*pn)->next)
4479
0
        ;
4480
0
      *pn = n;
4481
0
    }
4482
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4483
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4484
0
    {
4485
0
      struct bfd_link_needed_list *n, **pn;
4486
0
      char *fnm, *anm;
4487
0
      unsigned int tagv = dyn.d_un.d_val;
4488
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4489
4490
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4491
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4492
0
      if (n == NULL || fnm == NULL)
4493
0
        goto error_free_dyn;
4494
0
      amt = strlen (fnm) + 1;
4495
0
      anm = (char *) bfd_alloc (abfd, amt);
4496
0
      if (anm == NULL)
4497
0
        goto error_free_dyn;
4498
0
      memcpy (anm, fnm, amt);
4499
0
      n->name = anm;
4500
0
      n->by = abfd;
4501
0
      n->next = NULL;
4502
0
      for (pn = & rpath;
4503
0
           *pn != NULL;
4504
0
           pn = &(*pn)->next)
4505
0
        ;
4506
0
      *pn = n;
4507
0
    }
4508
0
        if (dyn.d_tag == DT_AUDIT)
4509
0
    {
4510
0
      unsigned int tagv = dyn.d_un.d_val;
4511
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4512
0
    }
4513
0
        if (dyn.d_tag == DT_FLAGS_1)
4514
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4515
0
      }
4516
4517
0
    free (dynbuf);
4518
0
  }
4519
4520
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4521
   frees all more recently bfd_alloc'd blocks as well.  */
4522
0
      if (runpath)
4523
0
  rpath = runpath;
4524
4525
0
      if (rpath)
4526
0
  {
4527
0
    struct bfd_link_needed_list **pn;
4528
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4529
0
      ;
4530
0
    *pn = rpath;
4531
0
  }
4532
4533
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4534
   all sections contained fully therein.  This makes relro
4535
   shared library sections appear as they will at run-time.  */
4536
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4537
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4538
0
  if (phdr->p_type == PT_GNU_RELRO)
4539
0
    {
4540
0
      for (s = abfd->sections; s != NULL; s = s->next)
4541
0
        {
4542
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4543
4544
0
    if ((s->flags & SEC_ALLOC) != 0
4545
0
        && s->vma * opb >= phdr->p_vaddr
4546
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4547
0
      s->flags |= SEC_READONLY;
4548
0
        }
4549
0
      break;
4550
0
    }
4551
4552
      /* We do not want to include any of the sections in a dynamic
4553
   object in the output file.  We hack by simply clobbering the
4554
   list of sections in the BFD.  This could be handled more
4555
   cleanly by, say, a new section flag; the existing
4556
   SEC_NEVER_LOAD flag is not the one we want, because that one
4557
   still implies that the section takes up space in the output
4558
   file.  */
4559
0
      bfd_section_list_clear (abfd);
4560
4561
      /* Find the name to use in a DT_NEEDED entry that refers to this
4562
   object.  If the object has a DT_SONAME entry, we use it.
4563
   Otherwise, if the generic linker stuck something in
4564
   elf_dt_name, we use that.  Otherwise, we just use the file
4565
   name.  */
4566
0
      if (soname == NULL || *soname == '\0')
4567
0
  {
4568
0
    soname = elf_dt_name (abfd);
4569
0
    if (soname == NULL || *soname == '\0')
4570
0
      soname = bfd_get_filename (abfd);
4571
0
  }
4572
4573
      /* Save the SONAME because sometimes the linker emulation code
4574
   will need to know it.  */
4575
0
      elf_dt_name (abfd) = soname;
4576
4577
      /* If we have already included this dynamic object in the
4578
   link, just ignore it.  There is no reason to include a
4579
   particular dynamic object more than once.  */
4580
0
      for (loaded_lib = htab->dyn_loaded;
4581
0
     loaded_lib != NULL;
4582
0
     loaded_lib = loaded_lib->next)
4583
0
  {
4584
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4585
0
      return true;
4586
0
  }
4587
4588
      /* Create dynamic sections for backends that require that be done
4589
   before setup_gnu_properties.  */
4590
0
      if (add_needed
4591
0
    && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4592
0
  return false;
4593
4594
      /* Save the DT_AUDIT entry for the linker emulation code. */
4595
0
      elf_dt_audit (abfd) = audit;
4596
0
    }
4597
4598
  /* If this is a dynamic object, we always link against the .dynsym
4599
     symbol table, not the .symtab symbol table.  The dynamic linker
4600
     will only see the .dynsym symbol table, so there is no reason to
4601
     look at .symtab for a dynamic object.  */
4602
4603
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4604
0
    hdr = &elf_tdata (abfd)->symtab_hdr;
4605
0
  else
4606
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4607
4608
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4609
4610
  /* The sh_info field of the symtab header tells us where the
4611
     external symbols start.  We don't care about the local symbols at
4612
     this point.  */
4613
0
  if (elf_bad_symtab (abfd))
4614
0
    {
4615
0
      extsymcount = symcount;
4616
0
      extsymoff = 0;
4617
0
    }
4618
0
  else
4619
0
    {
4620
0
      extsymcount = symcount - hdr->sh_info;
4621
0
      extsymoff = hdr->sh_info;
4622
0
    }
4623
4624
0
  sym_hash = elf_sym_hashes (abfd);
4625
0
  if (extsymcount != 0)
4626
0
    {
4627
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4628
0
              NULL, NULL, NULL);
4629
0
      if (isymbuf == NULL)
4630
0
  goto error_return;
4631
4632
0
      if (sym_hash == NULL)
4633
0
  {
4634
    /* We store a pointer to the hash table entry for each
4635
       external symbol.  */
4636
0
    size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4637
0
    sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4638
0
    if (sym_hash == NULL)
4639
0
      goto error_free_sym;
4640
0
    elf_sym_hashes (abfd) = sym_hash;
4641
0
  }
4642
0
    }
4643
4644
0
  if (dynamic)
4645
0
    {
4646
      /* Read in any version definitions.  */
4647
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4648
0
            info->default_imported_symver))
4649
0
  goto error_free_sym;
4650
4651
      /* Read in the symbol versions, but don't bother to convert them
4652
   to internal format.  */
4653
0
      if (elf_dynversym (abfd) != 0)
4654
0
  {
4655
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4656
0
    bfd_size_type amt = versymhdr->sh_size;
4657
4658
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4659
0
      goto error_free_sym;
4660
0
    extversym = (Elf_External_Versym *)
4661
0
      _bfd_malloc_and_read (abfd, amt, amt);
4662
0
    if (extversym == NULL)
4663
0
      goto error_free_sym;
4664
0
    extversym_end = extversym + amt / sizeof (*extversym);
4665
0
  }
4666
0
    }
4667
4668
  /* If we are loading an as-needed shared lib, save the symbol table
4669
     state before we start adding symbols.  If the lib turns out
4670
     to be unneeded, restore the state.  */
4671
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4672
0
    {
4673
0
      unsigned int i;
4674
0
      size_t entsize;
4675
4676
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4677
0
  {
4678
0
    struct bfd_hash_entry *p;
4679
0
    struct elf_link_hash_entry *h;
4680
4681
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4682
0
      {
4683
0
        h = (struct elf_link_hash_entry *) p;
4684
0
        entsize += htab->root.table.entsize;
4685
0
        if (h->root.type == bfd_link_hash_warning)
4686
0
    {
4687
0
      entsize += htab->root.table.entsize;
4688
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4689
0
    }
4690
0
        if (h->root.type == bfd_link_hash_common)
4691
0
    entsize += sizeof (*h->root.u.c.p);
4692
0
      }
4693
0
  }
4694
4695
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4696
0
      old_tab = bfd_malloc (tabsize + entsize);
4697
0
      if (old_tab == NULL)
4698
0
  goto error_free_vers;
4699
4700
      /* Remember the current objalloc pointer, so that all mem for
4701
   symbols added can later be reclaimed.  */
4702
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4703
0
      if (alloc_mark == NULL)
4704
0
  goto error_free_vers;
4705
4706
      /* Make a special call to the linker "notice" function to
4707
   tell it that we are about to handle an as-needed lib.  */
4708
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4709
0
  goto error_free_vers;
4710
4711
      /* Clone the symbol table.  Remember some pointers into the
4712
   symbol table, and dynamic symbol count.  */
4713
0
      old_ent = (char *) old_tab + tabsize;
4714
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4715
0
      old_undefs = htab->root.undefs;
4716
0
      old_undefs_tail = htab->root.undefs_tail;
4717
0
      old_table = htab->root.table.table;
4718
0
      old_size = htab->root.table.size;
4719
0
      old_count = htab->root.table.count;
4720
0
      old_strtab = NULL;
4721
0
      if (htab->dynstr != NULL)
4722
0
  {
4723
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4724
0
    if (old_strtab == NULL)
4725
0
      goto error_free_vers;
4726
0
  }
4727
4728
0
      for (i = 0; i < htab->root.table.size; i++)
4729
0
  {
4730
0
    struct bfd_hash_entry *p;
4731
0
    struct elf_link_hash_entry *h;
4732
4733
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4734
0
      {
4735
0
        h = (struct elf_link_hash_entry *) p;
4736
0
        memcpy (old_ent, h, htab->root.table.entsize);
4737
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4738
0
        if (h->root.type == bfd_link_hash_warning)
4739
0
    {
4740
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4741
0
      memcpy (old_ent, h, htab->root.table.entsize);
4742
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4743
0
    }
4744
0
        if (h->root.type == bfd_link_hash_common)
4745
0
    {
4746
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4747
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4748
0
    }
4749
0
      }
4750
0
  }
4751
0
    }
4752
4753
0
  weaks = NULL;
4754
0
  if (extversym == NULL)
4755
0
    ever = NULL;
4756
0
  else if (extversym + extsymoff < extversym_end)
4757
0
    ever = extversym + extsymoff;
4758
0
  else
4759
0
    {
4760
      /* xgettext:c-format */
4761
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4762
0
        abfd, (long) extsymoff,
4763
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4764
0
      bfd_set_error (bfd_error_bad_value);
4765
0
      goto error_free_vers;
4766
0
    }
4767
4768
0
  if (!bfd_link_relocatable (info)
4769
0
      && abfd->lto_slim_object)
4770
0
    {
4771
0
      _bfd_error_handler
4772
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4773
0
    }
4774
4775
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4776
0
       isym < isymend;
4777
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4778
0
    {
4779
0
      int bind;
4780
0
      bfd_vma value;
4781
0
      asection *sec, *new_sec;
4782
0
      flagword flags;
4783
0
      const char *name;
4784
0
      struct elf_link_hash_entry *h;
4785
0
      struct elf_link_hash_entry *hi;
4786
0
      bool definition;
4787
0
      bool size_change_ok;
4788
0
      bool type_change_ok;
4789
0
      bool new_weak;
4790
0
      bool old_weak;
4791
0
      bfd *override;
4792
0
      bool common;
4793
0
      bool discarded;
4794
0
      unsigned int old_alignment;
4795
0
      unsigned int shindex;
4796
0
      bfd *old_bfd;
4797
0
      bool matched;
4798
4799
0
      override = NULL;
4800
4801
0
      flags = BSF_NO_FLAGS;
4802
0
      sec = NULL;
4803
0
      value = isym->st_value;
4804
0
      common = bed->common_definition (isym);
4805
0
      if (common && info->inhibit_common_definition)
4806
0
  {
4807
    /* Treat common symbol as undefined for --no-define-common.  */
4808
0
    isym->st_shndx = SHN_UNDEF;
4809
0
    common = false;
4810
0
  }
4811
0
      discarded = false;
4812
4813
0
      bind = ELF_ST_BIND (isym->st_info);
4814
0
      switch (bind)
4815
0
  {
4816
0
  case STB_LOCAL:
4817
    /* This should be impossible, since ELF requires that all
4818
       global symbols follow all local symbols, and that sh_info
4819
       point to the first global symbol.  Unfortunately, Irix 5
4820
       screws this up.  */
4821
0
    if (elf_bad_symtab (abfd))
4822
0
      continue;
4823
4824
    /* If we aren't prepared to handle locals within the globals
4825
       then we'll likely segfault on a NULL symbol hash if the
4826
       symbol is ever referenced in relocations.  */
4827
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
4828
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4829
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4830
0
        " (>= sh_info of %lu)"),
4831
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
4832
0
            (long) extsymoff);
4833
4834
    /* Dynamic object relocations are not processed by ld, so
4835
       ld won't run into the problem mentioned above.  */
4836
0
    if (dynamic)
4837
0
      continue;
4838
0
    bfd_set_error (bfd_error_bad_value);
4839
0
    goto error_free_vers;
4840
4841
0
  case STB_GLOBAL:
4842
0
    if (isym->st_shndx != SHN_UNDEF && !common)
4843
0
      flags = BSF_GLOBAL;
4844
0
    break;
4845
4846
0
  case STB_WEAK:
4847
0
    flags = BSF_WEAK;
4848
0
    break;
4849
4850
0
  case STB_GNU_UNIQUE:
4851
0
    flags = BSF_GNU_UNIQUE;
4852
0
    break;
4853
4854
0
  default:
4855
    /* Leave it up to the processor backend.  */
4856
0
    break;
4857
0
  }
4858
4859
0
      if (isym->st_shndx == SHN_UNDEF)
4860
0
  sec = bfd_und_section_ptr;
4861
0
      else if (isym->st_shndx == SHN_ABS)
4862
0
  sec = bfd_abs_section_ptr;
4863
0
      else if (isym->st_shndx == SHN_COMMON)
4864
0
  {
4865
0
    sec = bfd_com_section_ptr;
4866
    /* What ELF calls the size we call the value.  What ELF
4867
       calls the value we call the alignment.  */
4868
0
    value = isym->st_size;
4869
0
  }
4870
0
      else
4871
0
  {
4872
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4873
0
    if (sec == NULL)
4874
0
      sec = bfd_abs_section_ptr;
4875
0
    else if (discarded_section (sec))
4876
0
      {
4877
        /* Symbols from discarded section are undefined.  We keep
4878
     its visibility.  */
4879
0
        sec = bfd_und_section_ptr;
4880
0
        discarded = true;
4881
0
        isym->st_shndx = SHN_UNDEF;
4882
0
      }
4883
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4884
0
      value -= sec->vma;
4885
0
  }
4886
4887
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4888
0
                isym->st_name);
4889
0
      if (name == NULL)
4890
0
  goto error_free_vers;
4891
4892
0
      if (isym->st_shndx == SHN_COMMON
4893
0
    && (abfd->flags & BFD_PLUGIN) != 0)
4894
0
  {
4895
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4896
4897
0
    if (xc == NULL)
4898
0
      {
4899
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4900
0
         | SEC_EXCLUDE);
4901
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4902
0
        if (xc == NULL)
4903
0
    goto error_free_vers;
4904
0
      }
4905
0
    sec = xc;
4906
0
  }
4907
0
      else if (isym->st_shndx == SHN_COMMON
4908
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
4909
0
         && !bfd_link_relocatable (info))
4910
0
  {
4911
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4912
4913
0
    if (tcomm == NULL)
4914
0
      {
4915
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4916
0
         | SEC_LINKER_CREATED);
4917
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4918
0
        if (tcomm == NULL)
4919
0
    goto error_free_vers;
4920
0
      }
4921
0
    sec = tcomm;
4922
0
  }
4923
0
      else if (bed->elf_add_symbol_hook)
4924
0
  {
4925
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4926
0
               &sec, &value))
4927
0
      goto error_free_vers;
4928
4929
    /* The hook function sets the name to NULL if this symbol
4930
       should be skipped for some reason.  */
4931
0
    if (name == NULL)
4932
0
      continue;
4933
0
  }
4934
4935
      /* Sanity check that all possibilities were handled.  */
4936
0
      if (sec == NULL)
4937
0
  abort ();
4938
4939
      /* Silently discard TLS symbols from --just-syms.  There's
4940
   no way to combine a static TLS block with a new TLS block
4941
   for this executable.  */
4942
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4943
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4944
0
  continue;
4945
4946
0
      if (bfd_is_und_section (sec)
4947
0
    || bfd_is_com_section (sec))
4948
0
  definition = false;
4949
0
      else
4950
0
  definition = true;
4951
4952
0
      size_change_ok = false;
4953
0
      type_change_ok = bed->type_change_ok;
4954
0
      old_weak = false;
4955
0
      matched = false;
4956
0
      old_alignment = 0;
4957
0
      old_bfd = NULL;
4958
0
      new_sec = sec;
4959
4960
0
      if (is_elf_hash_table (&htab->root))
4961
0
  {
4962
0
    Elf_Internal_Versym iver;
4963
0
    unsigned int vernum = 0;
4964
0
    bool skip;
4965
4966
0
    if (ever == NULL)
4967
0
      {
4968
0
        if (info->default_imported_symver)
4969
    /* Use the default symbol version created earlier.  */
4970
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
4971
0
        else
4972
0
    iver.vs_vers = 0;
4973
0
      }
4974
0
    else if (ever >= extversym_end)
4975
0
      {
4976
        /* xgettext:c-format */
4977
0
        _bfd_error_handler (_("%pB: not enough version information"),
4978
0
          abfd);
4979
0
        bfd_set_error (bfd_error_bad_value);
4980
0
        goto error_free_vers;
4981
0
      }
4982
0
    else
4983
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
4984
4985
0
    vernum = iver.vs_vers & VERSYM_VERSION;
4986
4987
    /* If this is a hidden symbol, or if it is not version
4988
       1, we append the version name to the symbol name.
4989
       However, we do not modify a non-hidden absolute symbol
4990
       if it is not a function, because it might be the version
4991
       symbol itself.  FIXME: What if it isn't?  */
4992
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4993
0
        || (vernum > 1
4994
0
      && (!bfd_is_abs_section (sec)
4995
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4996
0
      {
4997
0
        const char *verstr;
4998
0
        size_t namelen, verlen, newlen;
4999
0
        char *newname, *p;
5000
5001
0
        if (isym->st_shndx != SHN_UNDEF)
5002
0
    {
5003
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5004
0
        verstr = NULL;
5005
0
      else if (vernum > 1)
5006
0
        verstr =
5007
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5008
0
      else
5009
0
        verstr = "";
5010
5011
0
      if (verstr == NULL)
5012
0
        {
5013
0
          _bfd_error_handler
5014
      /* xgettext:c-format */
5015
0
      (_("%pB: %s: invalid version %u (max %d)"),
5016
0
       abfd, name, vernum,
5017
0
       elf_tdata (abfd)->cverdefs);
5018
0
          bfd_set_error (bfd_error_bad_value);
5019
0
          goto error_free_vers;
5020
0
        }
5021
0
    }
5022
0
        else
5023
0
    {
5024
      /* We cannot simply test for the number of
5025
         entries in the VERNEED section since the
5026
         numbers for the needed versions do not start
5027
         at 0.  */
5028
0
      Elf_Internal_Verneed *t;
5029
5030
0
      verstr = NULL;
5031
0
      for (t = elf_tdata (abfd)->verref;
5032
0
           t != NULL;
5033
0
           t = t->vn_nextref)
5034
0
        {
5035
0
          Elf_Internal_Vernaux *a;
5036
5037
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5038
0
      {
5039
0
        if (a->vna_other == vernum)
5040
0
          {
5041
0
            verstr = a->vna_nodename;
5042
0
            break;
5043
0
          }
5044
0
      }
5045
0
          if (a != NULL)
5046
0
      break;
5047
0
        }
5048
0
      if (verstr == NULL)
5049
0
        {
5050
0
          _bfd_error_handler
5051
      /* xgettext:c-format */
5052
0
      (_("%pB: %s: invalid needed version %d"),
5053
0
       abfd, name, vernum);
5054
0
          bfd_set_error (bfd_error_bad_value);
5055
0
          goto error_free_vers;
5056
0
        }
5057
0
    }
5058
5059
0
        namelen = strlen (name);
5060
0
        verlen = strlen (verstr);
5061
0
        newlen = namelen + verlen + 2;
5062
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5063
0
      && isym->st_shndx != SHN_UNDEF)
5064
0
    ++newlen;
5065
5066
0
        newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5067
0
        if (newname == NULL)
5068
0
    goto error_free_vers;
5069
0
        memcpy (newname, name, namelen);
5070
0
        p = newname + namelen;
5071
0
        *p++ = ELF_VER_CHR;
5072
        /* If this is a defined non-hidden version symbol,
5073
     we add another @ to the name.  This indicates the
5074
     default version of the symbol.  */
5075
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5076
0
      && isym->st_shndx != SHN_UNDEF)
5077
0
    *p++ = ELF_VER_CHR;
5078
0
        memcpy (p, verstr, verlen + 1);
5079
5080
0
        name = newname;
5081
0
      }
5082
5083
    /* If this symbol has default visibility and the user has
5084
       requested we not re-export it, then mark it as hidden.  */
5085
0
    if (!bfd_is_und_section (sec)
5086
0
        && !dynamic
5087
0
        && abfd->no_export
5088
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5089
0
      isym->st_other = (STV_HIDDEN
5090
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5091
5092
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5093
0
              sym_hash, &old_bfd, &old_weak,
5094
0
              &old_alignment, &skip, &override,
5095
0
              &type_change_ok, &size_change_ok,
5096
0
              &matched))
5097
0
      goto error_free_vers;
5098
5099
0
    if (skip)
5100
0
      continue;
5101
5102
    /* Override a definition only if the new symbol matches the
5103
       existing one.  */
5104
0
    if (override && matched)
5105
0
      definition = false;
5106
5107
0
    h = *sym_hash;
5108
0
    while (h->root.type == bfd_link_hash_indirect
5109
0
     || h->root.type == bfd_link_hash_warning)
5110
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5111
5112
0
    if (h->versioned != unversioned
5113
0
        && elf_tdata (abfd)->verdef != NULL
5114
0
        && vernum > 1
5115
0
        && definition)
5116
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5117
0
  }
5118
5119
0
      if (! (_bfd_generic_link_add_one_symbol
5120
0
       (info, override ? override : abfd, name, flags, sec, value,
5121
0
        NULL, false, bed->collect,
5122
0
        (struct bfd_link_hash_entry **) sym_hash)))
5123
0
  goto error_free_vers;
5124
5125
0
      h = *sym_hash;
5126
      /* We need to make sure that indirect symbol dynamic flags are
5127
   updated.  */
5128
0
      hi = h;
5129
0
      while (h->root.type == bfd_link_hash_indirect
5130
0
       || h->root.type == bfd_link_hash_warning)
5131
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5132
5133
0
      *sym_hash = h;
5134
5135
      /* Setting the index to -3 tells elf_link_output_extsym that
5136
   this symbol is defined in a discarded section.  */
5137
0
      if (discarded && is_elf_hash_table (&htab->root))
5138
0
  h->indx = -3;
5139
5140
0
      new_weak = (flags & BSF_WEAK) != 0;
5141
0
      if (dynamic
5142
0
    && definition
5143
0
    && new_weak
5144
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5145
0
    && is_elf_hash_table (&htab->root)
5146
0
    && h->u.alias == NULL)
5147
0
  {
5148
    /* Keep a list of all weak defined non function symbols from
5149
       a dynamic object, using the alias field.  Later in this
5150
       function we will set the alias field to the correct
5151
       value.  We only put non-function symbols from dynamic
5152
       objects on this list, because that happens to be the only
5153
       time we need to know the normal symbol corresponding to a
5154
       weak symbol, and the information is time consuming to
5155
       figure out.  If the alias field is not already NULL,
5156
       then this symbol was already defined by some previous
5157
       dynamic object, and we will be using that previous
5158
       definition anyhow.  */
5159
5160
0
    h->u.alias = weaks;
5161
0
    weaks = h;
5162
0
  }
5163
5164
      /* Set the alignment of a common symbol.  */
5165
0
      if ((common || bfd_is_com_section (sec))
5166
0
    && h->root.type == bfd_link_hash_common)
5167
0
  {
5168
0
    unsigned int align;
5169
5170
0
    if (common)
5171
0
      align = bfd_log2 (isym->st_value);
5172
0
    else
5173
0
      {
5174
        /* The new symbol is a common symbol in a shared object.
5175
     We need to get the alignment from the section.  */
5176
0
        align = new_sec->alignment_power;
5177
0
      }
5178
0
    if (align > old_alignment)
5179
0
      h->root.u.c.p->alignment_power = align;
5180
0
    else
5181
0
      h->root.u.c.p->alignment_power = old_alignment;
5182
0
  }
5183
5184
0
      if (is_elf_hash_table (&htab->root))
5185
0
  {
5186
    /* Set a flag in the hash table entry indicating the type of
5187
       reference or definition we just found.  A dynamic symbol
5188
       is one which is referenced or defined by both a regular
5189
       object and a shared object.  */
5190
0
    bool dynsym = false;
5191
5192
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5193
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5194
0
      {
5195
        /* Except for this flag to track nonweak references.  */
5196
0
        if (!definition
5197
0
      && bind != STB_WEAK)
5198
0
    h->ref_ir_nonweak = 1;
5199
0
      }
5200
0
    else if (!dynamic)
5201
0
      {
5202
0
        if (! definition)
5203
0
    {
5204
0
      h->ref_regular = 1;
5205
0
      if (bind != STB_WEAK)
5206
0
        h->ref_regular_nonweak = 1;
5207
0
    }
5208
0
        else
5209
0
    {
5210
0
      h->def_regular = 1;
5211
0
      if (h->def_dynamic)
5212
0
        {
5213
0
          h->def_dynamic = 0;
5214
0
          h->ref_dynamic = 1;
5215
0
        }
5216
0
    }
5217
0
      }
5218
0
    else
5219
0
      {
5220
0
        if (! definition)
5221
0
    {
5222
0
      h->ref_dynamic = 1;
5223
0
      hi->ref_dynamic = 1;
5224
0
    }
5225
0
        else
5226
0
    {
5227
0
      h->def_dynamic = 1;
5228
0
      hi->def_dynamic = 1;
5229
0
    }
5230
0
      }
5231
5232
    /* If an indirect symbol has been forced local, don't
5233
       make the real symbol dynamic.  */
5234
0
    if (h != hi && hi->forced_local)
5235
0
      ;
5236
0
    else if (!dynamic)
5237
0
      {
5238
0
        if (bfd_link_dll (info)
5239
0
      || h->def_dynamic
5240
0
      || h->ref_dynamic)
5241
0
    dynsym = true;
5242
0
      }
5243
0
    else
5244
0
      {
5245
0
        if (h->def_regular
5246
0
      || h->ref_regular
5247
0
      || (h->is_weakalias
5248
0
          && weakdef (h)->dynindx != -1))
5249
0
    dynsym = true;
5250
0
      }
5251
5252
    /* Check to see if we need to add an indirect symbol for
5253
       the default name.  */
5254
0
    if ((definition
5255
0
         || (!override && h->root.type == bfd_link_hash_common))
5256
0
        && !(hi != h
5257
0
       && hi->versioned == versioned_hidden))
5258
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5259
0
                sec, value, &old_bfd, &dynsym))
5260
0
        goto error_free_vers;
5261
5262
    /* Check the alignment when a common symbol is involved. This
5263
       can change when a common symbol is overridden by a normal
5264
       definition or a common symbol is ignored due to the old
5265
       normal definition. We need to make sure the maximum
5266
       alignment is maintained.  */
5267
0
    if ((old_alignment || common)
5268
0
        && h->root.type != bfd_link_hash_common)
5269
0
      {
5270
0
        unsigned int common_align;
5271
0
        unsigned int normal_align;
5272
0
        unsigned int symbol_align;
5273
0
        bfd *normal_bfd;
5274
0
        bfd *common_bfd;
5275
5276
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5277
0
        || h->root.type == bfd_link_hash_defweak);
5278
5279
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5280
0
        if (h->root.u.def.section->owner != NULL
5281
0
      && (h->root.u.def.section->owner->flags
5282
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5283
0
    {
5284
0
      normal_align = h->root.u.def.section->alignment_power;
5285
0
      if (normal_align > symbol_align)
5286
0
        normal_align = symbol_align;
5287
0
    }
5288
0
        else
5289
0
    normal_align = symbol_align;
5290
5291
0
        if (old_alignment)
5292
0
    {
5293
0
      common_align = old_alignment;
5294
0
      common_bfd = old_bfd;
5295
0
      normal_bfd = abfd;
5296
0
    }
5297
0
        else
5298
0
    {
5299
0
      common_align = bfd_log2 (isym->st_value);
5300
0
      common_bfd = abfd;
5301
0
      normal_bfd = old_bfd;
5302
0
    }
5303
5304
0
        if (normal_align < common_align)
5305
0
    {
5306
      /* PR binutils/2735 */
5307
0
      if (normal_bfd == NULL)
5308
0
        _bfd_error_handler
5309
          /* xgettext:c-format */
5310
0
          (_("warning: alignment %u of common symbol `%s' in %pB is"
5311
0
       " greater than the alignment (%u) of its section %pA"),
5312
0
           1 << common_align, name, common_bfd,
5313
0
           1 << normal_align, h->root.u.def.section);
5314
0
      else
5315
0
        _bfd_error_handler
5316
          /* xgettext:c-format */
5317
0
          (_("warning: alignment %u of normal symbol `%s' in %pB"
5318
0
       " is smaller than %u used by the common definition in %pB"),
5319
0
           1 << normal_align, name, normal_bfd,
5320
0
           1 << common_align, common_bfd);
5321
5322
      /* PR 30499: make sure that users understand that this warning is serious.  */
5323
0
      _bfd_error_handler
5324
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5325
0
    }
5326
0
      }
5327
5328
    /* Remember the symbol size if it isn't undefined.  */
5329
0
    if (isym->st_size != 0
5330
0
        && isym->st_shndx != SHN_UNDEF
5331
0
        && (definition || h->size == 0))
5332
0
      {
5333
0
        if (h->size != 0
5334
0
      && h->size != isym->st_size
5335
0
      && ! size_change_ok)
5336
0
    {
5337
0
      _bfd_error_handler
5338
        /* xgettext:c-format */
5339
0
        (_("warning: size of symbol `%s' changed"
5340
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5341
0
         name, (uint64_t) h->size, old_bfd,
5342
0
         (uint64_t) isym->st_size, abfd);
5343
5344
      /* PR 30499: make sure that users understand that this warning is serious.  */
5345
0
      _bfd_error_handler
5346
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5347
0
    }
5348
5349
0
        h->size = isym->st_size;
5350
0
      }
5351
5352
    /* If this is a common symbol, then we always want H->SIZE
5353
       to be the size of the common symbol.  The code just above
5354
       won't fix the size if a common symbol becomes larger.  We
5355
       don't warn about a size change here, because that is
5356
       covered by --warn-common.  Allow changes between different
5357
       function types.  */
5358
0
    if (h->root.type == bfd_link_hash_common)
5359
0
      h->size = h->root.u.c.size;
5360
5361
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5362
0
        && ((definition && !new_weak)
5363
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5364
0
      || h->type == STT_NOTYPE))
5365
0
      {
5366
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5367
5368
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5369
     symbol.  */
5370
0
        if (type == STT_GNU_IFUNC
5371
0
      && (abfd->flags & DYNAMIC) != 0)
5372
0
    type = STT_FUNC;
5373
5374
0
        if (h->type != type)
5375
0
    {
5376
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5377
        /* xgettext:c-format */
5378
0
        _bfd_error_handler
5379
0
          (_("warning: type of symbol `%s' changed"
5380
0
       " from %d to %d in %pB"),
5381
0
           name, h->type, type, abfd);
5382
5383
0
      h->type = type;
5384
0
    }
5385
0
      }
5386
5387
    /* Merge st_other field.  */
5388
0
    elf_merge_st_other (abfd, h, isym->st_other, sec,
5389
0
            definition, dynamic);
5390
5391
    /* We don't want to make debug symbol dynamic.  */
5392
0
    if (definition
5393
0
        && (sec->flags & SEC_DEBUGGING)
5394
0
        && !bfd_link_relocatable (info))
5395
0
      dynsym = false;
5396
5397
    /* Nor should we make plugin symbols dynamic.  */
5398
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5399
0
      dynsym = false;
5400
5401
0
    if (definition)
5402
0
      {
5403
0
        h->target_internal = isym->st_target_internal;
5404
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5405
0
      }
5406
5407
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5408
       in IR.  Since all data or text symbols in IR have the
5409
       same type, value and section, we can't tell if a symbol
5410
       is an alias of another symbol by their types, values and
5411
       sections.  */
5412
0
    if (definition
5413
0
        && !dynamic
5414
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5415
0
      {
5416
0
        char *p = strchr (name, ELF_VER_CHR);
5417
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5418
0
    {
5419
      /* Queue non-default versions so that .symver x, x@FOO
5420
         aliases can be checked.  */
5421
0
      if (!nondeflt_vers)
5422
0
        {
5423
0
          size_t amt = ((isymend - isym + 1)
5424
0
            * sizeof (struct elf_link_hash_entry *));
5425
0
          nondeflt_vers
5426
0
      = (struct elf_link_hash_entry **) bfd_malloc (amt);
5427
0
          if (!nondeflt_vers)
5428
0
      goto error_free_vers;
5429
0
        }
5430
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5431
0
    }
5432
0
      }
5433
5434
0
    if (dynsym && h->dynindx == -1)
5435
0
      {
5436
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5437
0
    goto error_free_vers;
5438
0
        if (h->is_weakalias
5439
0
      && weakdef (h)->dynindx == -1)
5440
0
    {
5441
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5442
0
        goto error_free_vers;
5443
0
    }
5444
0
      }
5445
0
    else if (h->dynindx != -1)
5446
      /* If the symbol already has a dynamic index, but
5447
         visibility says it should not be visible, turn it into
5448
         a local symbol.  */
5449
0
      switch (ELF_ST_VISIBILITY (h->other))
5450
0
        {
5451
0
        case STV_INTERNAL:
5452
0
        case STV_HIDDEN:
5453
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
5454
0
    dynsym = false;
5455
0
    break;
5456
0
        }
5457
5458
0
    if (!add_needed
5459
0
        && matched
5460
0
        && definition
5461
0
        && h->root.type != bfd_link_hash_indirect
5462
0
        && ((dynsym
5463
0
       && h->ref_regular_nonweak)
5464
0
      || (old_bfd != NULL
5465
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5466
0
          && h->ref_ir_nonweak
5467
0
          && !info->lto_all_symbols_read)
5468
0
      || (h->ref_dynamic_nonweak
5469
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5470
0
          && !on_needed_list (elf_dt_name (abfd),
5471
0
            htab->needed, NULL))))
5472
0
      {
5473
0
        const char *soname = elf_dt_name (abfd);
5474
5475
0
        info->callbacks->minfo ("%!", soname, old_bfd,
5476
0
              h->root.root.string);
5477
5478
        /* A symbol from a library loaded via DT_NEEDED of some
5479
     other library is referenced by a regular object.
5480
     Add a DT_NEEDED entry for it.  Issue an error if
5481
     --no-add-needed is used and the reference was not
5482
     a weak one.  */
5483
0
        if (old_bfd != NULL
5484
0
      && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5485
0
    {
5486
0
      _bfd_error_handler
5487
        /* xgettext:c-format */
5488
0
        (_("%pB: undefined reference to symbol '%s'"),
5489
0
         old_bfd, name);
5490
0
      bfd_set_error (bfd_error_missing_dso);
5491
0
      goto error_free_vers;
5492
0
    }
5493
5494
0
        elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5495
0
    (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5496
5497
        /* Create dynamic sections for backends that require
5498
     that be done before setup_gnu_properties.  */
5499
0
        if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5500
0
    return false;
5501
0
        add_needed = true;
5502
0
      }
5503
0
  }
5504
0
    }
5505
5506
0
  if (info->lto_plugin_active
5507
0
      && !bfd_link_relocatable (info)
5508
0
      && (abfd->flags & BFD_PLUGIN) == 0
5509
0
      && !just_syms
5510
0
      && extsymcount)
5511
0
    {
5512
0
      int r_sym_shift;
5513
5514
0
      if (bed->s->arch_size == 32)
5515
0
  r_sym_shift = 8;
5516
0
      else
5517
0
  r_sym_shift = 32;
5518
5519
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5520
   referenced in regular objects so that linker plugin will get
5521
   the correct symbol resolution.  */
5522
5523
0
      sym_hash = elf_sym_hashes (abfd);
5524
0
      for (s = abfd->sections; s != NULL; s = s->next)
5525
0
  {
5526
0
    Elf_Internal_Rela *internal_relocs;
5527
0
    Elf_Internal_Rela *rel, *relend;
5528
5529
    /* Don't check relocations in excluded sections.  */
5530
0
    if ((s->flags & SEC_RELOC) == 0
5531
0
        || s->reloc_count == 0
5532
0
        || (s->flags & SEC_EXCLUDE) != 0
5533
0
        || ((info->strip == strip_all
5534
0
       || info->strip == strip_debugger)
5535
0
      && (s->flags & SEC_DEBUGGING) != 0))
5536
0
      continue;
5537
5538
0
    internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5539
0
                  s, NULL,
5540
0
                  NULL,
5541
0
                  _bfd_link_keep_memory (info));
5542
0
    if (internal_relocs == NULL)
5543
0
      goto error_free_vers;
5544
5545
0
    rel = internal_relocs;
5546
0
    relend = rel + s->reloc_count;
5547
0
    for ( ; rel < relend; rel++)
5548
0
      {
5549
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5550
0
        struct elf_link_hash_entry *h;
5551
5552
        /* Skip local symbols.  */
5553
0
        if (r_symndx < extsymoff)
5554
0
    continue;
5555
5556
0
        h = sym_hash[r_symndx - extsymoff];
5557
0
        if (h != NULL)
5558
0
    h->root.non_ir_ref_regular = 1;
5559
0
      }
5560
5561
0
    if (elf_section_data (s)->relocs != internal_relocs)
5562
0
      free (internal_relocs);
5563
0
  }
5564
0
    }
5565
5566
0
  free (extversym);
5567
0
  extversym = NULL;
5568
0
  free (isymbuf);
5569
0
  isymbuf = NULL;
5570
5571
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5572
0
    {
5573
0
      unsigned int i;
5574
5575
      /* Restore the symbol table.  */
5576
0
      old_ent = (char *) old_tab + tabsize;
5577
0
      memset (elf_sym_hashes (abfd), 0,
5578
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5579
0
      htab->root.table.table = old_table;
5580
0
      htab->root.table.size = old_size;
5581
0
      htab->root.table.count = old_count;
5582
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5583
0
      htab->root.undefs = old_undefs;
5584
0
      htab->root.undefs_tail = old_undefs_tail;
5585
0
      if (htab->dynstr != NULL)
5586
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5587
0
      free (old_strtab);
5588
0
      old_strtab = NULL;
5589
0
      for (i = 0; i < htab->root.table.size; i++)
5590
0
  {
5591
0
    struct bfd_hash_entry *p;
5592
0
    struct elf_link_hash_entry *h;
5593
0
    unsigned int non_ir_ref_dynamic;
5594
5595
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5596
0
      {
5597
        /* Preserve non_ir_ref_dynamic so that this symbol
5598
     will be exported when the dynamic lib becomes needed
5599
     in the second pass.  */
5600
0
        h = (struct elf_link_hash_entry *) p;
5601
0
        if (h->root.type == bfd_link_hash_warning)
5602
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5603
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5604
5605
0
        h = (struct elf_link_hash_entry *) p;
5606
0
        memcpy (h, old_ent, htab->root.table.entsize);
5607
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5608
0
        if (h->root.type == bfd_link_hash_warning)
5609
0
    {
5610
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5611
0
      memcpy (h, old_ent, htab->root.table.entsize);
5612
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5613
0
    }
5614
0
        if (h->root.type == bfd_link_hash_common)
5615
0
    {
5616
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5617
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5618
0
    }
5619
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5620
0
      }
5621
0
  }
5622
5623
      /* Make a special call to the linker "notice" function to
5624
   tell it that symbols added for crefs may need to be removed.  */
5625
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5626
0
  goto error_free_vers;
5627
5628
0
      free (old_tab);
5629
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5630
0
         alloc_mark);
5631
0
      free (nondeflt_vers);
5632
0
      return true;
5633
0
    }
5634
5635
0
  if (old_tab != NULL)
5636
0
    {
5637
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5638
0
  goto error_free_vers;
5639
0
      free (old_tab);
5640
0
      old_tab = NULL;
5641
0
    }
5642
5643
  /* Now that all the symbols from this input file are created, if
5644
     not performing a relocatable link, handle .symver foo, foo@BAR
5645
     such that any relocs against foo become foo@BAR.  */
5646
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5647
0
    {
5648
0
      size_t cnt, symidx;
5649
5650
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5651
0
  {
5652
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5653
0
    char *shortname, *p;
5654
0
    size_t amt;
5655
5656
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5657
0
    if (p == NULL
5658
0
        || (h->root.type != bfd_link_hash_defined
5659
0
      && h->root.type != bfd_link_hash_defweak))
5660
0
      continue;
5661
5662
0
    amt = p - h->root.root.string;
5663
0
    shortname = (char *) bfd_malloc (amt + 1);
5664
0
    if (!shortname)
5665
0
      goto error_free_vers;
5666
0
    memcpy (shortname, h->root.root.string, amt);
5667
0
    shortname[amt] = '\0';
5668
5669
0
    hi = (struct elf_link_hash_entry *)
5670
0
         bfd_link_hash_lookup (&htab->root, shortname,
5671
0
             false, false, false);
5672
0
    if (hi != NULL
5673
0
        && hi->root.type == h->root.type
5674
0
        && hi->root.u.def.value == h->root.u.def.value
5675
0
        && hi->root.u.def.section == h->root.u.def.section)
5676
0
      {
5677
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
5678
0
        hi->root.type = bfd_link_hash_indirect;
5679
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5680
0
        (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5681
0
        sym_hash = elf_sym_hashes (abfd);
5682
0
        if (sym_hash)
5683
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5684
0
      if (sym_hash[symidx] == hi)
5685
0
        {
5686
0
          sym_hash[symidx] = h;
5687
0
          break;
5688
0
        }
5689
0
      }
5690
0
    free (shortname);
5691
0
  }
5692
0
      free (nondeflt_vers);
5693
0
      nondeflt_vers = NULL;
5694
0
    }
5695
5696
  /* Now set the alias field correctly for all the weak defined
5697
     symbols we found.  The only way to do this is to search all the
5698
     symbols.  Since we only need the information for non functions in
5699
     dynamic objects, that's the only time we actually put anything on
5700
     the list WEAKS.  We need this information so that if a regular
5701
     object refers to a symbol defined weakly in a dynamic object, the
5702
     real symbol in the dynamic object is also put in the dynamic
5703
     symbols; we also must arrange for both symbols to point to the
5704
     same memory location.  We could handle the general case of symbol
5705
     aliasing, but a general symbol alias can only be generated in
5706
     assembler code, handling it correctly would be very time
5707
     consuming, and other ELF linkers don't handle general aliasing
5708
     either.  */
5709
0
  if (weaks != NULL)
5710
0
    {
5711
0
      struct elf_link_hash_entry **hpp;
5712
0
      struct elf_link_hash_entry **hppend;
5713
0
      struct elf_link_hash_entry **sorted_sym_hash;
5714
0
      struct elf_link_hash_entry *h;
5715
0
      size_t sym_count, amt;
5716
5717
      /* Since we have to search the whole symbol list for each weak
5718
   defined symbol, search time for N weak defined symbols will be
5719
   O(N^2). Binary search will cut it down to O(NlogN).  */
5720
0
      amt = extsymcount * sizeof (*sorted_sym_hash);
5721
0
      sorted_sym_hash = bfd_malloc (amt);
5722
0
      if (sorted_sym_hash == NULL)
5723
0
  goto error_return;
5724
0
      sym_hash = sorted_sym_hash;
5725
0
      hpp = elf_sym_hashes (abfd);
5726
0
      hppend = hpp + extsymcount;
5727
0
      sym_count = 0;
5728
0
      for (; hpp < hppend; hpp++)
5729
0
  {
5730
0
    h = *hpp;
5731
0
    if (h != NULL
5732
0
        && h->root.type == bfd_link_hash_defined
5733
0
        && !bed->is_function_type (h->type))
5734
0
      {
5735
0
        *sym_hash = h;
5736
0
        sym_hash++;
5737
0
        sym_count++;
5738
0
      }
5739
0
  }
5740
5741
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5742
0
       elf_sort_symbol);
5743
5744
0
      while (weaks != NULL)
5745
0
  {
5746
0
    struct elf_link_hash_entry *hlook;
5747
0
    asection *slook;
5748
0
    bfd_vma vlook;
5749
0
    size_t i, j, idx = 0;
5750
5751
0
    hlook = weaks;
5752
0
    weaks = hlook->u.alias;
5753
0
    hlook->u.alias = NULL;
5754
5755
0
    if (hlook->root.type != bfd_link_hash_defined
5756
0
        && hlook->root.type != bfd_link_hash_defweak)
5757
0
      continue;
5758
5759
0
    slook = hlook->root.u.def.section;
5760
0
    vlook = hlook->root.u.def.value;
5761
5762
0
    i = 0;
5763
0
    j = sym_count;
5764
0
    while (i != j)
5765
0
      {
5766
0
        bfd_signed_vma vdiff;
5767
0
        idx = (i + j) / 2;
5768
0
        h = sorted_sym_hash[idx];
5769
0
        vdiff = vlook - h->root.u.def.value;
5770
0
        if (vdiff < 0)
5771
0
    j = idx;
5772
0
        else if (vdiff > 0)
5773
0
    i = idx + 1;
5774
0
        else
5775
0
    {
5776
0
      int sdiff = slook->id - h->root.u.def.section->id;
5777
0
      if (sdiff < 0)
5778
0
        j = idx;
5779
0
      else if (sdiff > 0)
5780
0
        i = idx + 1;
5781
0
      else
5782
0
        break;
5783
0
    }
5784
0
      }
5785
5786
    /* We didn't find a value/section match.  */
5787
0
    if (i == j)
5788
0
      continue;
5789
5790
    /* With multiple aliases, or when the weak symbol is already
5791
       strongly defined, we have multiple matching symbols and
5792
       the binary search above may land on any of them.  Step
5793
       one past the matching symbol(s).  */
5794
0
    while (++idx != j)
5795
0
      {
5796
0
        h = sorted_sym_hash[idx];
5797
0
        if (h->root.u.def.section != slook
5798
0
      || h->root.u.def.value != vlook)
5799
0
    break;
5800
0
      }
5801
5802
    /* Now look back over the aliases.  Since we sorted by size
5803
       as well as value and section, we'll choose the one with
5804
       the largest size.  */
5805
0
    while (idx-- != i)
5806
0
      {
5807
0
        h = sorted_sym_hash[idx];
5808
5809
        /* Stop if value or section doesn't match.  */
5810
0
        if (h->root.u.def.section != slook
5811
0
      || h->root.u.def.value != vlook)
5812
0
    break;
5813
0
        else if (h != hlook)
5814
0
    {
5815
0
      struct elf_link_hash_entry *t;
5816
5817
0
      hlook->u.alias = h;
5818
0
      hlook->is_weakalias = 1;
5819
0
      t = h;
5820
0
      if (t->u.alias != NULL)
5821
0
        while (t->u.alias != h)
5822
0
          t = t->u.alias;
5823
0
      t->u.alias = hlook;
5824
5825
      /* If the weak definition is in the list of dynamic
5826
         symbols, make sure the real definition is put
5827
         there as well.  */
5828
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
5829
0
        {
5830
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
5831
0
      {
5832
0
      err_free_sym_hash:
5833
0
        free (sorted_sym_hash);
5834
0
        goto error_return;
5835
0
      }
5836
0
        }
5837
5838
      /* If the real definition is in the list of dynamic
5839
         symbols, make sure the weak definition is put
5840
         there as well.  If we don't do this, then the
5841
         dynamic loader might not merge the entries for the
5842
         real definition and the weak definition.  */
5843
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
5844
0
        {
5845
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5846
0
      goto err_free_sym_hash;
5847
0
        }
5848
0
      break;
5849
0
    }
5850
0
      }
5851
0
  }
5852
5853
0
      free (sorted_sym_hash);
5854
0
    }
5855
5856
0
  if (bed->check_directives
5857
0
      && !(*bed->check_directives) (abfd, info))
5858
0
    return false;
5859
5860
  /* If this is a non-traditional link, try to optimize the handling
5861
     of the .stab/.stabstr sections.  */
5862
0
  if (! dynamic
5863
0
      && ! info->traditional_format
5864
0
      && is_elf_hash_table (&htab->root)
5865
0
      && (info->strip != strip_all && info->strip != strip_debugger))
5866
0
    {
5867
0
      asection *stabstr;
5868
5869
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5870
0
      if (stabstr != NULL)
5871
0
  {
5872
0
    bfd_size_type string_offset = 0;
5873
0
    asection *stab;
5874
5875
0
    for (stab = abfd->sections; stab; stab = stab->next)
5876
0
      if (startswith (stab->name, ".stab")
5877
0
    && (!stab->name[5] ||
5878
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5879
0
    && (stab->flags & SEC_MERGE) == 0
5880
0
    && !bfd_is_abs_section (stab->output_section))
5881
0
        {
5882
0
    struct bfd_elf_section_data *secdata;
5883
5884
0
    secdata = elf_section_data (stab);
5885
0
    if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5886
0
                 stabstr, &secdata->sec_info,
5887
0
                 &string_offset))
5888
0
      goto error_return;
5889
0
    if (secdata->sec_info)
5890
0
      stab->sec_info_type = SEC_INFO_TYPE_STABS;
5891
0
      }
5892
0
  }
5893
0
    }
5894
5895
0
  if (dynamic && add_needed)
5896
0
    {
5897
      /* Add this bfd to the loaded list.  */
5898
0
      struct elf_link_loaded_list *n;
5899
5900
0
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5901
0
      if (n == NULL)
5902
0
  goto error_return;
5903
0
      n->abfd = abfd;
5904
0
      n->next = htab->dyn_loaded;
5905
0
      htab->dyn_loaded = n;
5906
0
    }
5907
0
  if (dynamic && !add_needed
5908
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5909
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5910
5911
0
  return true;
5912
5913
0
 error_free_vers:
5914
0
  free (old_tab);
5915
0
  free (old_strtab);
5916
0
  free (nondeflt_vers);
5917
0
  free (extversym);
5918
0
 error_free_sym:
5919
0
  free (isymbuf);
5920
0
 error_return:
5921
0
  return false;
5922
0
}
5923
5924
/* Return the linker hash table entry of a symbol that might be
5925
   satisfied by an archive symbol.  Return -1 on error.  */
5926
5927
struct bfd_link_hash_entry *
5928
_bfd_elf_archive_symbol_lookup (bfd *abfd,
5929
        struct bfd_link_info *info,
5930
        const char *name)
5931
0
{
5932
0
  struct bfd_link_hash_entry *h;
5933
0
  char *p, *copy;
5934
0
  size_t len, first;
5935
5936
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5937
0
  if (h != NULL)
5938
0
    return h;
5939
5940
  /* If this is a default version (the name contains @@), look up the
5941
     symbol again with only one `@' as well as without the version.
5942
     The effect is that references to the symbol with and without the
5943
     version will be matched by the default symbol in the archive.  */
5944
5945
0
  p = strchr (name, ELF_VER_CHR);
5946
0
  if (p == NULL || p[1] != ELF_VER_CHR)
5947
0
    return h;
5948
5949
  /* First check with only one `@'.  */
5950
0
  len = strlen (name);
5951
0
  copy = (char *) bfd_alloc (abfd, len);
5952
0
  if (copy == NULL)
5953
0
    return (struct bfd_link_hash_entry *) -1;
5954
5955
0
  first = p - name + 1;
5956
0
  memcpy (copy, name, first);
5957
0
  memcpy (copy + first, name + first + 1, len - first);
5958
5959
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5960
0
  if (h == NULL)
5961
0
    {
5962
      /* We also need to check references to the symbol without the
5963
   version.  */
5964
0
      copy[first - 1] = '\0';
5965
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5966
0
    }
5967
5968
0
  bfd_release (abfd, copy);
5969
0
  return h;
5970
0
}
5971
5972
/* Add symbols from an ELF archive file to the linker hash table.  We
5973
   don't use _bfd_generic_link_add_archive_symbols because we need to
5974
   handle versioned symbols.
5975
5976
   Fortunately, ELF archive handling is simpler than that done by
5977
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5978
   oddities.  In ELF, if we find a symbol in the archive map, and the
5979
   symbol is currently undefined, we know that we must pull in that
5980
   object file.
5981
5982
   Unfortunately, we do have to make multiple passes over the symbol
5983
   table until nothing further is resolved.  */
5984
5985
static bool
5986
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5987
0
{
5988
0
  symindex c;
5989
0
  unsigned char *included = NULL;
5990
0
  carsym *symdefs;
5991
0
  bool loop;
5992
0
  size_t amt;
5993
0
  const struct elf_backend_data *bed;
5994
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
5995
0
    (bfd *, struct bfd_link_info *, const char *);
5996
5997
0
  if (! bfd_has_map (abfd))
5998
0
    {
5999
      /* An empty archive is a special case.  */
6000
0
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6001
0
  return true;
6002
0
      bfd_set_error (bfd_error_no_armap);
6003
0
      return false;
6004
0
    }
6005
6006
  /* Keep track of all symbols we know to be already defined, and all
6007
     files we know to be already included.  This is to speed up the
6008
     second and subsequent passes.  */
6009
0
  c = bfd_ardata (abfd)->symdef_count;
6010
0
  if (c == 0)
6011
0
    return true;
6012
0
  amt = c * sizeof (*included);
6013
0
  included = (unsigned char *) bfd_zmalloc (amt);
6014
0
  if (included == NULL)
6015
0
    return false;
6016
6017
0
  symdefs = bfd_ardata (abfd)->symdefs;
6018
0
  bed = get_elf_backend_data (abfd);
6019
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6020
6021
0
  do
6022
0
    {
6023
0
      file_ptr last;
6024
0
      symindex i;
6025
0
      carsym *symdef;
6026
0
      carsym *symdefend;
6027
6028
0
      loop = false;
6029
0
      last = -1;
6030
6031
0
      symdef = symdefs;
6032
0
      symdefend = symdef + c;
6033
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6034
0
  {
6035
0
    struct bfd_link_hash_entry *h;
6036
0
    bfd *element;
6037
0
    struct bfd_link_hash_entry *undefs_tail;
6038
0
    symindex mark;
6039
6040
0
    if (included[i])
6041
0
      continue;
6042
0
    if (symdef->file_offset == last)
6043
0
      {
6044
0
        included[i] = true;
6045
0
        continue;
6046
0
      }
6047
6048
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6049
0
    if (h == (struct bfd_link_hash_entry *) -1)
6050
0
      goto error_return;
6051
6052
0
    if (h == NULL)
6053
0
      continue;
6054
6055
0
    if (h->type == bfd_link_hash_undefined)
6056
0
      {
6057
        /* If the archive element has already been loaded then one
6058
     of the symbols defined by that element might have been
6059
     made undefined due to being in a discarded section.  */
6060
0
        if (is_elf_hash_table (info->hash)
6061
0
      && ((struct elf_link_hash_entry *) h)->indx == -3)
6062
0
    continue;
6063
0
      }
6064
0
    else if (h->type == bfd_link_hash_common)
6065
0
      {
6066
        /* We currently have a common symbol.  The archive map contains
6067
     a reference to this symbol, so we may want to include it.  We
6068
     only want to include it however, if this archive element
6069
     contains a definition of the symbol, not just another common
6070
     declaration of it.
6071
6072
     Unfortunately some archivers (including GNU ar) will put
6073
     declarations of common symbols into their archive maps, as
6074
     well as real definitions, so we cannot just go by the archive
6075
     map alone.  Instead we must read in the element's symbol
6076
     table and check that to see what kind of symbol definition
6077
     this is.  */
6078
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6079
0
    continue;
6080
0
      }
6081
0
    else
6082
0
      {
6083
0
        if (h->type != bfd_link_hash_undefweak)
6084
    /* Symbol must be defined.  Don't check it again.  */
6085
0
    included[i] = true;
6086
0
        continue;
6087
0
      }
6088
6089
    /* We need to include this archive member.  */
6090
0
    element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6091
0
               info);
6092
0
    if (element == NULL)
6093
0
      goto error_return;
6094
6095
0
    if (! bfd_check_format (element, bfd_object))
6096
0
      goto error_return;
6097
6098
0
    undefs_tail = info->hash->undefs_tail;
6099
6100
0
    if (!(*info->callbacks
6101
0
    ->add_archive_element) (info, element, symdef->name, &element))
6102
0
      continue;
6103
0
    if (!bfd_link_add_symbols (element, info))
6104
0
      goto error_return;
6105
6106
    /* If there are any new undefined symbols, we need to make
6107
       another pass through the archive in order to see whether
6108
       they can be defined.  FIXME: This isn't perfect, because
6109
       common symbols wind up on undefs_tail and because an
6110
       undefined symbol which is defined later on in this pass
6111
       does not require another pass.  This isn't a bug, but it
6112
       does make the code less efficient than it could be.  */
6113
0
    if (undefs_tail != info->hash->undefs_tail)
6114
0
      loop = true;
6115
6116
    /* Look backward to mark all symbols from this object file
6117
       which we have already seen in this pass.  */
6118
0
    mark = i;
6119
0
    do
6120
0
      {
6121
0
        included[mark] = true;
6122
0
        if (mark == 0)
6123
0
    break;
6124
0
        --mark;
6125
0
      }
6126
0
    while (symdefs[mark].file_offset == symdef->file_offset);
6127
6128
    /* We mark subsequent symbols from this object file as we go
6129
       on through the loop.  */
6130
0
    last = symdef->file_offset;
6131
0
  }
6132
0
    }
6133
0
  while (loop);
6134
6135
0
  free (included);
6136
0
  return true;
6137
6138
0
 error_return:
6139
0
  free (included);
6140
0
  return false;
6141
0
}
6142
6143
/* Given an ELF BFD, add symbols to the global hash table as
6144
   appropriate.  */
6145
6146
bool
6147
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6148
0
{
6149
0
  switch (bfd_get_format (abfd))
6150
0
    {
6151
0
    case bfd_object:
6152
0
      return elf_link_add_object_symbols (abfd, info);
6153
0
    case bfd_archive:
6154
0
      return elf_link_add_archive_symbols (abfd, info);
6155
0
    default:
6156
0
      bfd_set_error (bfd_error_wrong_format);
6157
0
      return false;
6158
0
    }
6159
0
}
6160

6161
struct hash_codes_info
6162
{
6163
  unsigned long *hashcodes;
6164
  bool error;
6165
};
6166
6167
/* This function will be called though elf_link_hash_traverse to store
6168
   all hash value of the exported symbols in an array.  */
6169
6170
static bool
6171
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6172
0
{
6173
0
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
6174
0
  const char *name;
6175
0
  unsigned long ha;
6176
0
  char *alc = NULL;
6177
6178
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6179
0
  if (h->dynindx == -1)
6180
0
    return true;
6181
6182
0
  name = h->root.root.string;
6183
0
  if (h->versioned >= versioned)
6184
0
    {
6185
0
      char *p = strchr (name, ELF_VER_CHR);
6186
0
      if (p != NULL)
6187
0
  {
6188
0
    alc = (char *) bfd_malloc (p - name + 1);
6189
0
    if (alc == NULL)
6190
0
      {
6191
0
        inf->error = true;
6192
0
        return false;
6193
0
      }
6194
0
    memcpy (alc, name, p - name);
6195
0
    alc[p - name] = '\0';
6196
0
    name = alc;
6197
0
  }
6198
0
    }
6199
6200
  /* Compute the hash value.  */
6201
0
  ha = bfd_elf_hash (name);
6202
6203
  /* Store the found hash value in the array given as the argument.  */
6204
0
  *(inf->hashcodes)++ = ha;
6205
6206
  /* And store it in the struct so that we can put it in the hash table
6207
     later.  */
6208
0
  h->u.elf_hash_value = ha;
6209
6210
0
  free (alc);
6211
0
  return true;
6212
0
}
6213
6214
struct collect_gnu_hash_codes
6215
{
6216
  bfd *output_bfd;
6217
  const struct elf_backend_data *bed;
6218
  unsigned long int nsyms;
6219
  unsigned long int maskbits;
6220
  unsigned long int *hashcodes;
6221
  unsigned long int *hashval;
6222
  unsigned long int *indx;
6223
  unsigned long int *counts;
6224
  bfd_vma *bitmask;
6225
  bfd_byte *contents;
6226
  bfd_size_type xlat;
6227
  long int min_dynindx;
6228
  unsigned long int bucketcount;
6229
  unsigned long int symindx;
6230
  long int local_indx;
6231
  long int shift1, shift2;
6232
  unsigned long int mask;
6233
  bool error;
6234
};
6235
6236
/* This function will be called though elf_link_hash_traverse to store
6237
   all hash value of the exported symbols in an array.  */
6238
6239
static bool
6240
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6241
0
{
6242
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6243
0
  const char *name;
6244
0
  unsigned long ha;
6245
0
  char *alc = NULL;
6246
6247
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6248
0
  if (h->dynindx == -1)
6249
0
    return true;
6250
6251
  /* Ignore also local symbols and undefined symbols.  */
6252
0
  if (! (*s->bed->elf_hash_symbol) (h))
6253
0
    return true;
6254
6255
0
  name = h->root.root.string;
6256
0
  if (h->versioned >= versioned)
6257
0
    {
6258
0
      char *p = strchr (name, ELF_VER_CHR);
6259
0
      if (p != NULL)
6260
0
  {
6261
0
    alc = (char *) bfd_malloc (p - name + 1);
6262
0
    if (alc == NULL)
6263
0
      {
6264
0
        s->error = true;
6265
0
        return false;
6266
0
      }
6267
0
    memcpy (alc, name, p - name);
6268
0
    alc[p - name] = '\0';
6269
0
    name = alc;
6270
0
  }
6271
0
    }
6272
6273
  /* Compute the hash value.  */
6274
0
  ha = bfd_elf_gnu_hash (name);
6275
6276
  /* Store the found hash value in the array for compute_bucket_count,
6277
     and also for .dynsym reordering purposes.  */
6278
0
  s->hashcodes[s->nsyms] = ha;
6279
0
  s->hashval[h->dynindx] = ha;
6280
0
  ++s->nsyms;
6281
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6282
0
    s->min_dynindx = h->dynindx;
6283
6284
0
  free (alc);
6285
0
  return true;
6286
0
}
6287
6288
/* This function will be called though elf_link_hash_traverse to do
6289
   final dynamic symbol renumbering in case of .gnu.hash.
6290
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6291
   to the translation table.  */
6292
6293
static bool
6294
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6295
0
{
6296
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6297
0
  unsigned long int bucket;
6298
0
  unsigned long int val;
6299
6300
  /* Ignore indirect symbols.  */
6301
0
  if (h->dynindx == -1)
6302
0
    return true;
6303
6304
  /* Ignore also local symbols and undefined symbols.  */
6305
0
  if (! (*s->bed->elf_hash_symbol) (h))
6306
0
    {
6307
0
      if (h->dynindx >= s->min_dynindx)
6308
0
  {
6309
0
    if (s->bed->record_xhash_symbol != NULL)
6310
0
      {
6311
0
        (*s->bed->record_xhash_symbol) (h, 0);
6312
0
        s->local_indx++;
6313
0
      }
6314
0
    else
6315
0
      h->dynindx = s->local_indx++;
6316
0
  }
6317
0
      return true;
6318
0
    }
6319
6320
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6321
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6322
0
  & ((s->maskbits >> s->shift1) - 1);
6323
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6324
0
  s->bitmask[val]
6325
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6326
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6327
0
  if (s->counts[bucket] == 1)
6328
    /* Last element terminates the chain.  */
6329
0
    val |= 1;
6330
0
  bfd_put_32 (s->output_bfd, val,
6331
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6332
0
  --s->counts[bucket];
6333
0
  if (s->bed->record_xhash_symbol != NULL)
6334
0
    {
6335
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6336
6337
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6338
0
    }
6339
0
  else
6340
0
    h->dynindx = s->indx[bucket]++;
6341
0
  return true;
6342
0
}
6343
6344
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6345
6346
bool
6347
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6348
0
{
6349
0
  return !(h->forced_local
6350
0
     || h->root.type == bfd_link_hash_undefined
6351
0
     || h->root.type == bfd_link_hash_undefweak
6352
0
     || ((h->root.type == bfd_link_hash_defined
6353
0
    || h->root.type == bfd_link_hash_defweak)
6354
0
         && h->root.u.def.section->output_section == NULL));
6355
0
}
6356
6357
/* Array used to determine the number of hash table buckets to use
6358
   based on the number of symbols there are.  If there are fewer than
6359
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6360
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6361
   than 32771 buckets.  */
6362
6363
static const size_t elf_buckets[] =
6364
{
6365
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6366
  16411, 32771, 0
6367
};
6368
6369
/* Compute bucket count for hashing table.  We do not use a static set
6370
   of possible tables sizes anymore.  Instead we determine for all
6371
   possible reasonable sizes of the table the outcome (i.e., the
6372
   number of collisions etc) and choose the best solution.  The
6373
   weighting functions are not too simple to allow the table to grow
6374
   without bounds.  Instead one of the weighting factors is the size.
6375
   Therefore the result is always a good payoff between few collisions
6376
   (= short chain lengths) and table size.  */
6377
static size_t
6378
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6379
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6380
          unsigned long int nsyms,
6381
          int gnu_hash)
6382
0
{
6383
0
  size_t best_size = 0;
6384
0
  unsigned long int i;
6385
6386
0
  if (info->optimize)
6387
0
    {
6388
0
      size_t minsize;
6389
0
      size_t maxsize;
6390
0
      uint64_t best_chlen = ~((uint64_t) 0);
6391
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6392
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6393
0
      const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6394
0
      unsigned long int *counts;
6395
0
      bfd_size_type amt;
6396
0
      unsigned int no_improvement_count = 0;
6397
6398
      /* Possible optimization parameters: if we have NSYMS symbols we say
6399
   that the hashing table must at least have NSYMS/4 and at most
6400
   2*NSYMS buckets.  */
6401
0
      minsize = nsyms / 4;
6402
0
      if (minsize == 0)
6403
0
  minsize = 1;
6404
0
      best_size = maxsize = nsyms * 2;
6405
0
      if (gnu_hash)
6406
0
  {
6407
0
    if (minsize < 2)
6408
0
      minsize = 2;
6409
0
    if ((best_size & 31) == 0)
6410
0
      ++best_size;
6411
0
  }
6412
6413
      /* Create array where we count the collisions in.  We must use bfd_malloc
6414
   since the size could be large.  */
6415
0
      amt = maxsize;
6416
0
      amt *= sizeof (unsigned long int);
6417
0
      counts = (unsigned long int *) bfd_malloc (amt);
6418
0
      if (counts == NULL)
6419
0
  return 0;
6420
6421
      /* Compute the "optimal" size for the hash table.  The criteria is a
6422
   minimal chain length.  The minor criteria is (of course) the size
6423
   of the table.  */
6424
0
      for (i = minsize; i < maxsize; ++i)
6425
0
  {
6426
    /* Walk through the array of hashcodes and count the collisions.  */
6427
0
    uint64_t max;
6428
0
    unsigned long int j;
6429
0
    unsigned long int fact;
6430
6431
0
    if (gnu_hash && (i & 31) == 0)
6432
0
      continue;
6433
6434
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6435
6436
    /* Determine how often each hash bucket is used.  */
6437
0
    for (j = 0; j < nsyms; ++j)
6438
0
      ++counts[hashcodes[j] % i];
6439
6440
    /* For the weight function we need some information about the
6441
       pagesize on the target.  This is information need not be 100%
6442
       accurate.  Since this information is not available (so far) we
6443
       define it here to a reasonable default value.  If it is crucial
6444
       to have a better value some day simply define this value.  */
6445
0
# ifndef BFD_TARGET_PAGESIZE
6446
0
#  define BFD_TARGET_PAGESIZE (4096)
6447
0
# endif
6448
6449
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6450
       and the chains.  */
6451
0
    max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6452
6453
0
# if 1
6454
    /* Variant 1: optimize for short chains.  We add the squares
6455
       of all the chain lengths (which favors many small chain
6456
       over a few long chains).  */
6457
0
    for (j = 0; j < i; ++j)
6458
0
      max += counts[j] * counts[j];
6459
6460
    /* This adds penalties for the overall size of the table.  */
6461
0
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6462
0
    max *= fact * fact;
6463
# else
6464
    /* Variant 2: Optimize a lot more for small table.  Here we
6465
       also add squares of the size but we also add penalties for
6466
       empty slots (the +1 term).  */
6467
    for (j = 0; j < i; ++j)
6468
      max += (1 + counts[j]) * (1 + counts[j]);
6469
6470
    /* The overall size of the table is considered, but not as
6471
       strong as in variant 1, where it is squared.  */
6472
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6473
    max *= fact;
6474
# endif
6475
6476
    /* Compare with current best results.  */
6477
0
    if (max < best_chlen)
6478
0
      {
6479
0
        best_chlen = max;
6480
0
        best_size = i;
6481
0
        no_improvement_count = 0;
6482
0
      }
6483
    /* PR 11843: Avoid futile long searches for the best bucket size
6484
       when there are a large number of symbols.  */
6485
0
    else if (++no_improvement_count == 100)
6486
0
      break;
6487
0
  }
6488
6489
0
      free (counts);
6490
0
    }
6491
0
  else
6492
0
    {
6493
0
      for (i = 0; elf_buckets[i] != 0; i++)
6494
0
  {
6495
0
    best_size = elf_buckets[i];
6496
0
    if (nsyms < elf_buckets[i + 1])
6497
0
      break;
6498
0
  }
6499
0
      if (gnu_hash && best_size < 2)
6500
0
  best_size = 2;
6501
0
    }
6502
6503
0
  return best_size;
6504
0
}
6505
6506
/* Size any SHT_GROUP section for ld -r.  */
6507
6508
bool
6509
_bfd_elf_size_group_sections (struct bfd_link_info *info)
6510
0
{
6511
0
  bfd *ibfd;
6512
0
  asection *s;
6513
6514
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6515
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6516
0
  && (s = ibfd->sections) != NULL
6517
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6518
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6519
0
      return false;
6520
0
  return true;
6521
0
}
6522
6523
/* Set a default stack segment size.  The value in INFO wins.  If it
6524
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6525
   undefined it is initialized.  */
6526
6527
bool
6528
bfd_elf_stack_segment_size (bfd *output_bfd,
6529
          struct bfd_link_info *info,
6530
          const char *legacy_symbol,
6531
          bfd_vma default_size)
6532
0
{
6533
0
  struct elf_link_hash_entry *h = NULL;
6534
6535
  /* Look for legacy symbol.  */
6536
0
  if (legacy_symbol)
6537
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6538
0
            false, false, false);
6539
0
  if (h && (h->root.type == bfd_link_hash_defined
6540
0
      || h->root.type == bfd_link_hash_defweak)
6541
0
      && h->def_regular
6542
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6543
0
    {
6544
      /* The symbol has no type if specified on the command line.  */
6545
0
      h->type = STT_OBJECT;
6546
0
      if (info->stacksize)
6547
  /* xgettext:c-format */
6548
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6549
0
          output_bfd, legacy_symbol);
6550
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6551
  /* xgettext:c-format */
6552
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6553
0
          output_bfd, legacy_symbol);
6554
0
      else
6555
0
  info->stacksize = h->root.u.def.value;
6556
0
    }
6557
6558
0
  if (!info->stacksize)
6559
    /* If the user didn't set a size, or explicitly inhibit the
6560
       size, set it now.  */
6561
0
    info->stacksize = default_size;
6562
6563
  /* Provide the legacy symbol, if it is referenced.  */
6564
0
  if (h && (h->root.type == bfd_link_hash_undefined
6565
0
      || h->root.type == bfd_link_hash_undefweak))
6566
0
    {
6567
0
      struct bfd_link_hash_entry *bh = NULL;
6568
6569
0
      if (!(_bfd_generic_link_add_one_symbol
6570
0
      (info, output_bfd, legacy_symbol,
6571
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6572
0
       info->stacksize >= 0 ? info->stacksize : 0,
6573
0
       NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6574
0
  return false;
6575
6576
0
      h = (struct elf_link_hash_entry *) bh;
6577
0
      h->def_regular = 1;
6578
0
      h->type = STT_OBJECT;
6579
0
    }
6580
6581
0
  return true;
6582
0
}
6583
6584
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6585
6586
struct elf_gc_sweep_symbol_info
6587
{
6588
  struct bfd_link_info *info;
6589
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6590
           bool);
6591
};
6592
6593
static bool
6594
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6595
0
{
6596
0
  if (!h->mark
6597
0
      && (((h->root.type == bfd_link_hash_defined
6598
0
      || h->root.type == bfd_link_hash_defweak)
6599
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6600
0
    && h->root.u.def.section->gc_mark))
6601
0
    || h->root.type == bfd_link_hash_undefined
6602
0
    || h->root.type == bfd_link_hash_undefweak))
6603
0
    {
6604
0
      struct elf_gc_sweep_symbol_info *inf;
6605
6606
0
      inf = (struct elf_gc_sweep_symbol_info *) data;
6607
0
      (*inf->hide_symbol) (inf->info, h, true);
6608
0
      h->def_regular = 0;
6609
0
      h->ref_regular = 0;
6610
0
      h->ref_regular_nonweak = 0;
6611
0
    }
6612
6613
0
  return true;
6614
0
}
6615
6616
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6617
   called by the ELF linker emulation before_allocation routine.  We
6618
   must set the sizes of the sections before the linker sets the
6619
   addresses of the various sections.  */
6620
6621
bool
6622
bfd_elf_size_dynamic_sections (bfd *output_bfd,
6623
             const char *soname,
6624
             const char *rpath,
6625
             const char *filter_shlib,
6626
             const char *audit,
6627
             const char *depaudit,
6628
             const char * const *auxiliary_filters,
6629
             struct bfd_link_info *info,
6630
             asection **sinterpptr)
6631
0
{
6632
0
  bfd *dynobj;
6633
0
  const struct elf_backend_data *bed;
6634
6635
0
  *sinterpptr = NULL;
6636
6637
0
  if (!is_elf_hash_table (info->hash))
6638
0
    return true;
6639
6640
  /* Any syms created from now on start with -1 in
6641
     got.refcount/offset and plt.refcount/offset.  */
6642
0
  elf_hash_table (info)->init_got_refcount
6643
0
    = elf_hash_table (info)->init_got_offset;
6644
0
  elf_hash_table (info)->init_plt_refcount
6645
0
    = elf_hash_table (info)->init_plt_offset;
6646
6647
0
  bed = get_elf_backend_data (output_bfd);
6648
6649
  /* The backend may have to create some sections regardless of whether
6650
     we're dynamic or not.  */
6651
0
  if (bed->elf_backend_always_size_sections
6652
0
      && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6653
0
    return false;
6654
6655
0
  dynobj = elf_hash_table (info)->dynobj;
6656
6657
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6658
0
    {
6659
0
      struct bfd_elf_version_tree *verdefs;
6660
0
      struct elf_info_failed asvinfo;
6661
0
      struct bfd_elf_version_tree *t;
6662
0
      struct bfd_elf_version_expr *d;
6663
0
      asection *s;
6664
0
      size_t soname_indx;
6665
6666
      /* If we are supposed to export all symbols into the dynamic symbol
6667
   table (this is not the normal case), then do so.  */
6668
0
      if (info->export_dynamic
6669
0
    || (bfd_link_executable (info) && info->dynamic))
6670
0
  {
6671
0
    struct elf_info_failed eif;
6672
6673
0
    eif.info = info;
6674
0
    eif.failed = false;
6675
0
    elf_link_hash_traverse (elf_hash_table (info),
6676
0
          _bfd_elf_export_symbol,
6677
0
          &eif);
6678
0
    if (eif.failed)
6679
0
      return false;
6680
0
  }
6681
6682
0
      if (soname != NULL)
6683
0
  {
6684
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685
0
               soname, true);
6686
0
    if (soname_indx == (size_t) -1
6687
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6688
0
      return false;
6689
0
  }
6690
0
      else
6691
0
  soname_indx = (size_t) -1;
6692
6693
      /* Make all global versions with definition.  */
6694
0
      for (t = info->version_info; t != NULL; t = t->next)
6695
0
  for (d = t->globals.list; d != NULL; d = d->next)
6696
0
    if (!d->symver && d->literal)
6697
0
      {
6698
0
        const char *verstr, *name;
6699
0
        size_t namelen, verlen, newlen;
6700
0
        char *newname, *p, leading_char;
6701
0
        struct elf_link_hash_entry *newh;
6702
6703
0
        leading_char = bfd_get_symbol_leading_char (output_bfd);
6704
0
        name = d->pattern;
6705
0
        namelen = strlen (name) + (leading_char != '\0');
6706
0
        verstr = t->name;
6707
0
        verlen = strlen (verstr);
6708
0
        newlen = namelen + verlen + 3;
6709
6710
0
        newname = (char *) bfd_malloc (newlen);
6711
0
        if (newname == NULL)
6712
0
    return false;
6713
0
        newname[0] = leading_char;
6714
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
6715
6716
        /* Check the hidden versioned definition.  */
6717
0
        p = newname + namelen;
6718
0
        *p++ = ELF_VER_CHR;
6719
0
        memcpy (p, verstr, verlen + 1);
6720
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
6721
0
             newname, false, false,
6722
0
             false);
6723
0
        if (newh == NULL
6724
0
      || (newh->root.type != bfd_link_hash_defined
6725
0
          && newh->root.type != bfd_link_hash_defweak))
6726
0
    {
6727
      /* Check the default versioned definition.  */
6728
0
      *p++ = ELF_VER_CHR;
6729
0
      memcpy (p, verstr, verlen + 1);
6730
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
6731
0
                 newname, false, false,
6732
0
                 false);
6733
0
    }
6734
0
        free (newname);
6735
6736
        /* Mark this version if there is a definition and it is
6737
     not defined in a shared object.  */
6738
0
        if (newh != NULL
6739
0
      && !newh->def_dynamic
6740
0
      && (newh->root.type == bfd_link_hash_defined
6741
0
          || newh->root.type == bfd_link_hash_defweak))
6742
0
    d->symver = 1;
6743
0
      }
6744
6745
      /* Attach all the symbols to their version information.  */
6746
0
      asvinfo.info = info;
6747
0
      asvinfo.failed = false;
6748
6749
0
      elf_link_hash_traverse (elf_hash_table (info),
6750
0
            _bfd_elf_link_assign_sym_version,
6751
0
            &asvinfo);
6752
0
      if (asvinfo.failed)
6753
0
  return false;
6754
6755
0
      if (!info->allow_undefined_version)
6756
0
  {
6757
    /* Check if all global versions have a definition.  */
6758
0
    bool all_defined = true;
6759
0
    for (t = info->version_info; t != NULL; t = t->next)
6760
0
      for (d = t->globals.list; d != NULL; d = d->next)
6761
0
        if (d->literal && !d->symver && !d->script)
6762
0
    {
6763
0
      _bfd_error_handler
6764
0
        (_("%s: undefined version: %s"),
6765
0
         d->pattern, t->name);
6766
0
      all_defined = false;
6767
0
    }
6768
6769
0
    if (!all_defined)
6770
0
      {
6771
0
        bfd_set_error (bfd_error_bad_value);
6772
0
        return false;
6773
0
      }
6774
0
  }
6775
6776
      /* Set up the version definition section.  */
6777
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6778
0
      BFD_ASSERT (s != NULL);
6779
6780
      /* We may have created additional version definitions if we are
6781
   just linking a regular application.  */
6782
0
      verdefs = info->version_info;
6783
6784
      /* Skip anonymous version tag.  */
6785
0
      if (verdefs != NULL && verdefs->vernum == 0)
6786
0
  verdefs = verdefs->next;
6787
6788
0
      if (verdefs == NULL && !info->create_default_symver)
6789
0
  s->flags |= SEC_EXCLUDE;
6790
0
      else
6791
0
  {
6792
0
    unsigned int cdefs;
6793
0
    bfd_size_type size;
6794
0
    bfd_byte *p;
6795
0
    Elf_Internal_Verdef def;
6796
0
    Elf_Internal_Verdaux defaux;
6797
0
    struct bfd_link_hash_entry *bh;
6798
0
    struct elf_link_hash_entry *h;
6799
0
    const char *name;
6800
6801
0
    cdefs = 0;
6802
0
    size = 0;
6803
6804
    /* Make space for the base version.  */
6805
0
    size += sizeof (Elf_External_Verdef);
6806
0
    size += sizeof (Elf_External_Verdaux);
6807
0
    ++cdefs;
6808
6809
    /* Make space for the default version.  */
6810
0
    if (info->create_default_symver)
6811
0
      {
6812
0
        size += sizeof (Elf_External_Verdef);
6813
0
        ++cdefs;
6814
0
      }
6815
6816
0
    for (t = verdefs; t != NULL; t = t->next)
6817
0
      {
6818
0
        struct bfd_elf_version_deps *n;
6819
6820
        /* Don't emit base version twice.  */
6821
0
        if (t->vernum == 0)
6822
0
    continue;
6823
6824
0
        size += sizeof (Elf_External_Verdef);
6825
0
        size += sizeof (Elf_External_Verdaux);
6826
0
        ++cdefs;
6827
6828
0
        for (n = t->deps; n != NULL; n = n->next)
6829
0
    size += sizeof (Elf_External_Verdaux);
6830
0
      }
6831
6832
0
    s->size = size;
6833
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6834
0
    if (s->contents == NULL && s->size != 0)
6835
0
      return false;
6836
6837
    /* Fill in the version definition section.  */
6838
6839
0
    p = s->contents;
6840
6841
0
    def.vd_version = VER_DEF_CURRENT;
6842
0
    def.vd_flags = VER_FLG_BASE;
6843
0
    def.vd_ndx = 1;
6844
0
    def.vd_cnt = 1;
6845
0
    if (info->create_default_symver)
6846
0
      {
6847
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6848
0
        def.vd_next = sizeof (Elf_External_Verdef);
6849
0
      }
6850
0
    else
6851
0
      {
6852
0
        def.vd_aux = sizeof (Elf_External_Verdef);
6853
0
        def.vd_next = (sizeof (Elf_External_Verdef)
6854
0
           + sizeof (Elf_External_Verdaux));
6855
0
      }
6856
6857
0
    if (soname_indx != (size_t) -1)
6858
0
      {
6859
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6860
0
              soname_indx);
6861
0
        def.vd_hash = bfd_elf_hash (soname);
6862
0
        defaux.vda_name = soname_indx;
6863
0
        name = soname;
6864
0
      }
6865
0
    else
6866
0
      {
6867
0
        size_t indx;
6868
6869
0
        name = lbasename (bfd_get_filename (output_bfd));
6870
0
        def.vd_hash = bfd_elf_hash (name);
6871
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6872
0
            name, false);
6873
0
        if (indx == (size_t) -1)
6874
0
    return false;
6875
0
        defaux.vda_name = indx;
6876
0
      }
6877
0
    defaux.vda_next = 0;
6878
6879
0
    _bfd_elf_swap_verdef_out (output_bfd, &def,
6880
0
            (Elf_External_Verdef *) p);
6881
0
    p += sizeof (Elf_External_Verdef);
6882
0
    if (info->create_default_symver)
6883
0
      {
6884
        /* Add a symbol representing this version.  */
6885
0
        bh = NULL;
6886
0
        if (! (_bfd_generic_link_add_one_symbol
6887
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6888
0
          0, NULL, false,
6889
0
          get_elf_backend_data (dynobj)->collect, &bh)))
6890
0
    return false;
6891
0
        h = (struct elf_link_hash_entry *) bh;
6892
0
        h->non_elf = 0;
6893
0
        h->def_regular = 1;
6894
0
        h->type = STT_OBJECT;
6895
0
        h->verinfo.vertree = NULL;
6896
6897
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
6898
0
    return false;
6899
6900
        /* Create a duplicate of the base version with the same
6901
     aux block, but different flags.  */
6902
0
        def.vd_flags = 0;
6903
0
        def.vd_ndx = 2;
6904
0
        def.vd_aux = sizeof (Elf_External_Verdef);
6905
0
        if (verdefs)
6906
0
    def.vd_next = (sizeof (Elf_External_Verdef)
6907
0
             + sizeof (Elf_External_Verdaux));
6908
0
        else
6909
0
    def.vd_next = 0;
6910
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
6911
0
          (Elf_External_Verdef *) p);
6912
0
        p += sizeof (Elf_External_Verdef);
6913
0
      }
6914
0
    _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6915
0
             (Elf_External_Verdaux *) p);
6916
0
    p += sizeof (Elf_External_Verdaux);
6917
6918
0
    for (t = verdefs; t != NULL; t = t->next)
6919
0
      {
6920
0
        unsigned int cdeps;
6921
0
        struct bfd_elf_version_deps *n;
6922
6923
        /* Don't emit the base version twice.  */
6924
0
        if (t->vernum == 0)
6925
0
    continue;
6926
6927
0
        cdeps = 0;
6928
0
        for (n = t->deps; n != NULL; n = n->next)
6929
0
    ++cdeps;
6930
6931
        /* Add a symbol representing this version.  */
6932
0
        bh = NULL;
6933
0
        if (! (_bfd_generic_link_add_one_symbol
6934
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6935
0
          0, NULL, false,
6936
0
          get_elf_backend_data (dynobj)->collect, &bh)))
6937
0
    return false;
6938
0
        h = (struct elf_link_hash_entry *) bh;
6939
0
        h->non_elf = 0;
6940
0
        h->def_regular = 1;
6941
0
        h->type = STT_OBJECT;
6942
0
        h->verinfo.vertree = t;
6943
6944
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
6945
0
    return false;
6946
6947
0
        def.vd_version = VER_DEF_CURRENT;
6948
0
        def.vd_flags = 0;
6949
0
        if (t->globals.list == NULL
6950
0
      && t->locals.list == NULL
6951
0
      && ! t->used)
6952
0
    def.vd_flags |= VER_FLG_WEAK;
6953
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6954
0
        def.vd_cnt = cdeps + 1;
6955
0
        def.vd_hash = bfd_elf_hash (t->name);
6956
0
        def.vd_aux = sizeof (Elf_External_Verdef);
6957
0
        def.vd_next = 0;
6958
6959
        /* If a basever node is next, it *must* be the last node in
6960
     the chain, otherwise Verdef construction breaks.  */
6961
0
        if (t->next != NULL && t->next->vernum == 0)
6962
0
    BFD_ASSERT (t->next->next == NULL);
6963
6964
0
        if (t->next != NULL && t->next->vernum != 0)
6965
0
    def.vd_next = (sizeof (Elf_External_Verdef)
6966
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6967
6968
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
6969
0
          (Elf_External_Verdef *) p);
6970
0
        p += sizeof (Elf_External_Verdef);
6971
6972
0
        defaux.vda_name = h->dynstr_index;
6973
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6974
0
              h->dynstr_index);
6975
0
        defaux.vda_next = 0;
6976
0
        if (t->deps != NULL)
6977
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
6978
0
        t->name_indx = defaux.vda_name;
6979
6980
0
        _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6981
0
           (Elf_External_Verdaux *) p);
6982
0
        p += sizeof (Elf_External_Verdaux);
6983
6984
0
        for (n = t->deps; n != NULL; n = n->next)
6985
0
    {
6986
0
      if (n->version_needed == NULL)
6987
0
        {
6988
          /* This can happen if there was an error in the
6989
       version script.  */
6990
0
          defaux.vda_name = 0;
6991
0
        }
6992
0
      else
6993
0
        {
6994
0
          defaux.vda_name = n->version_needed->name_indx;
6995
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6996
0
                defaux.vda_name);
6997
0
        }
6998
0
      if (n->next == NULL)
6999
0
        defaux.vda_next = 0;
7000
0
      else
7001
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7002
7003
0
      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7004
0
               (Elf_External_Verdaux *) p);
7005
0
      p += sizeof (Elf_External_Verdaux);
7006
0
    }
7007
0
      }
7008
7009
0
    elf_tdata (output_bfd)->cverdefs = cdefs;
7010
0
  }
7011
0
    }
7012
7013
0
  if (info->gc_sections && bed->can_gc_sections)
7014
0
    {
7015
0
      struct elf_gc_sweep_symbol_info sweep_info;
7016
7017
      /* Remove the symbols that were in the swept sections from the
7018
   dynamic symbol table.  */
7019
0
      sweep_info.info = info;
7020
0
      sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7021
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7022
0
            &sweep_info);
7023
0
    }
7024
7025
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7026
0
    {
7027
0
      asection *s;
7028
0
      struct elf_find_verdep_info sinfo;
7029
7030
      /* Work out the size of the version reference section.  */
7031
7032
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7033
0
      BFD_ASSERT (s != NULL);
7034
7035
0
      sinfo.info = info;
7036
0
      sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7037
0
      if (sinfo.vers == 0)
7038
0
  sinfo.vers = 1;
7039
0
      sinfo.failed = false;
7040
7041
0
      elf_link_hash_traverse (elf_hash_table (info),
7042
0
            _bfd_elf_link_find_version_dependencies,
7043
0
            &sinfo);
7044
0
      if (sinfo.failed)
7045
0
  return false;
7046
7047
0
      if (info->enable_dt_relr)
7048
0
  {
7049
0
    elf_link_add_dt_relr_dependency (&sinfo);
7050
0
    if (sinfo.failed)
7051
0
      return false;
7052
0
  }
7053
7054
0
      if (elf_tdata (output_bfd)->verref == NULL)
7055
0
  s->flags |= SEC_EXCLUDE;
7056
0
      else
7057
0
  {
7058
0
    Elf_Internal_Verneed *vn;
7059
0
    unsigned int size;
7060
0
    unsigned int crefs;
7061
0
    bfd_byte *p;
7062
7063
    /* Build the version dependency section.  */
7064
0
    size = 0;
7065
0
    crefs = 0;
7066
0
    for (vn = elf_tdata (output_bfd)->verref;
7067
0
         vn != NULL;
7068
0
         vn = vn->vn_nextref)
7069
0
      {
7070
0
        Elf_Internal_Vernaux *a;
7071
7072
0
        size += sizeof (Elf_External_Verneed);
7073
0
        ++crefs;
7074
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7075
0
    size += sizeof (Elf_External_Vernaux);
7076
0
      }
7077
7078
0
    s->size = size;
7079
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7080
0
    if (s->contents == NULL)
7081
0
      return false;
7082
7083
0
    p = s->contents;
7084
0
    for (vn = elf_tdata (output_bfd)->verref;
7085
0
         vn != NULL;
7086
0
         vn = vn->vn_nextref)
7087
0
      {
7088
0
        unsigned int caux;
7089
0
        Elf_Internal_Vernaux *a;
7090
0
        size_t indx;
7091
7092
0
        caux = 0;
7093
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7094
0
    ++caux;
7095
7096
0
        vn->vn_version = VER_NEED_CURRENT;
7097
0
        vn->vn_cnt = caux;
7098
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7099
0
            elf_dt_name (vn->vn_bfd) != NULL
7100
0
            ? elf_dt_name (vn->vn_bfd)
7101
0
            : lbasename (bfd_get_filename
7102
0
                   (vn->vn_bfd)),
7103
0
            false);
7104
0
        if (indx == (size_t) -1)
7105
0
    return false;
7106
0
        vn->vn_file = indx;
7107
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7108
0
        if (vn->vn_nextref == NULL)
7109
0
    vn->vn_next = 0;
7110
0
        else
7111
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7112
0
             + caux * sizeof (Elf_External_Vernaux));
7113
7114
0
        _bfd_elf_swap_verneed_out (output_bfd, vn,
7115
0
           (Elf_External_Verneed *) p);
7116
0
        p += sizeof (Elf_External_Verneed);
7117
7118
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7119
0
    {
7120
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7121
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7122
0
                a->vna_nodename, false);
7123
0
      if (indx == (size_t) -1)
7124
0
        return false;
7125
0
      a->vna_name = indx;
7126
0
      if (a->vna_nextptr == NULL)
7127
0
        a->vna_next = 0;
7128
0
      else
7129
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7130
7131
0
      _bfd_elf_swap_vernaux_out (output_bfd, a,
7132
0
               (Elf_External_Vernaux *) p);
7133
0
      p += sizeof (Elf_External_Vernaux);
7134
0
    }
7135
0
      }
7136
7137
0
    elf_tdata (output_bfd)->cverrefs = crefs;
7138
0
  }
7139
0
    }
7140
7141
0
  if (bfd_link_relocatable (info)
7142
0
      && !_bfd_elf_size_group_sections (info))
7143
0
    return false;
7144
7145
  /* Determine any GNU_STACK segment requirements, after the backend
7146
     has had a chance to set a default segment size.  */
7147
0
  if (info->execstack)
7148
0
    {
7149
      /* If the user has explicitly requested warnings, then generate one even
7150
   though the choice is the result of another command line option.  */
7151
0
      if (info->warn_execstack == 1)
7152
0
  _bfd_error_handler
7153
0
    (_("\
7154
0
warning: enabling an executable stack because of -z execstack command line option"));
7155
0
      elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7156
0
    }
7157
0
  else if (info->noexecstack)
7158
0
    elf_stack_flags (output_bfd) = PF_R | PF_W;
7159
0
  else
7160
0
    {
7161
0
      bfd *inputobj;
7162
0
      asection *notesec = NULL;
7163
0
      bfd *noteobj = NULL;
7164
0
      bfd *emptyobj = NULL;
7165
0
      int exec = 0;
7166
7167
0
      for (inputobj = info->input_bfds;
7168
0
     inputobj;
7169
0
     inputobj = inputobj->link.next)
7170
0
  {
7171
0
    asection *s;
7172
7173
0
    if (inputobj->flags
7174
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7175
0
      continue;
7176
0
    s = inputobj->sections;
7177
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7178
0
      continue;
7179
7180
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7181
0
    if (s)
7182
0
      {
7183
0
        notesec = s;
7184
0
        if (s->flags & SEC_CODE)
7185
0
    {
7186
0
      noteobj = inputobj;
7187
0
      exec = PF_X;
7188
      /* There is no point in scanning the remaining bfds.  */
7189
0
      break;
7190
0
    }
7191
0
      }
7192
0
    else if (bed->default_execstack && info->default_execstack)
7193
0
      {
7194
0
        exec = PF_X;
7195
0
        emptyobj = inputobj;
7196
0
      }
7197
0
  }
7198
7199
0
      if (notesec || info->stacksize > 0)
7200
0
  {
7201
0
    if (exec)
7202
0
      {
7203
0
        if (info->warn_execstack != 0)
7204
0
    {
7205
      /* PR 29072: Because an executable stack is a serious
7206
         security risk, make sure that the user knows that it is
7207
         being enabled despite the fact that it was not requested
7208
         on the command line.  */
7209
0
      if (noteobj)
7210
0
        _bfd_error_handler (_("\
7211
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7212
0
           bfd_get_filename (noteobj));
7213
0
      else if (emptyobj)
7214
0
        {
7215
0
          _bfd_error_handler (_("\
7216
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7217
0
            bfd_get_filename (emptyobj));
7218
0
          _bfd_error_handler (_("\
7219
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7220
0
        }
7221
0
    }
7222
0
      }
7223
0
    elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7224
0
  }
7225
7226
0
      if (notesec && exec && bfd_link_relocatable (info)
7227
0
    && notesec->output_section != bfd_abs_section_ptr)
7228
0
  notesec->output_section->flags |= SEC_CODE;
7229
0
    }
7230
7231
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7232
0
    {
7233
0
      struct elf_info_failed eif;
7234
0
      struct elf_link_hash_entry *h;
7235
0
      asection *dynstr;
7236
0
      asection *s;
7237
7238
0
      *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7239
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7240
7241
0
      if (info->symbolic)
7242
0
  {
7243
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7244
0
      return false;
7245
0
    info->flags |= DF_SYMBOLIC;
7246
0
  }
7247
7248
0
      if (rpath != NULL)
7249
0
  {
7250
0
    size_t indx;
7251
0
    bfd_vma tag;
7252
7253
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7254
0
              true);
7255
0
    if (indx == (size_t) -1)
7256
0
      return false;
7257
7258
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7259
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7260
0
      return false;
7261
0
  }
7262
7263
0
      if (filter_shlib != NULL)
7264
0
  {
7265
0
    size_t indx;
7266
7267
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7268
0
              filter_shlib, true);
7269
0
    if (indx == (size_t) -1
7270
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7271
0
      return false;
7272
0
  }
7273
7274
0
      if (auxiliary_filters != NULL)
7275
0
  {
7276
0
    const char * const *p;
7277
7278
0
    for (p = auxiliary_filters; *p != NULL; p++)
7279
0
      {
7280
0
        size_t indx;
7281
7282
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7283
0
            *p, true);
7284
0
        if (indx == (size_t) -1
7285
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7286
0
    return false;
7287
0
      }
7288
0
  }
7289
7290
0
      if (audit != NULL)
7291
0
  {
7292
0
    size_t indx;
7293
7294
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7295
0
              true);
7296
0
    if (indx == (size_t) -1
7297
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7298
0
      return false;
7299
0
  }
7300
7301
0
      if (depaudit != NULL)
7302
0
  {
7303
0
    size_t indx;
7304
7305
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7306
0
              true);
7307
0
    if (indx == (size_t) -1
7308
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7309
0
      return false;
7310
0
  }
7311
7312
0
      eif.info = info;
7313
0
      eif.failed = false;
7314
7315
      /* Find all symbols which were defined in a dynamic object and make
7316
   the backend pick a reasonable value for them.  */
7317
0
      elf_link_hash_traverse (elf_hash_table (info),
7318
0
            _bfd_elf_adjust_dynamic_symbol,
7319
0
            &eif);
7320
0
      if (eif.failed)
7321
0
  return false;
7322
7323
      /* Add some entries to the .dynamic section.  We fill in some of the
7324
   values later, in bfd_elf_final_link, but we must add the entries
7325
   now so that we know the final size of the .dynamic section.  */
7326
7327
      /* If there are initialization and/or finalization functions to
7328
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7329
0
      h = (info->init_function
7330
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7331
0
           info->init_function, false,
7332
0
           false, false)
7333
0
     : NULL);
7334
0
      if (h != NULL
7335
0
    && (h->ref_regular
7336
0
        || h->def_regular))
7337
0
  {
7338
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7339
0
      return false;
7340
0
  }
7341
0
      h = (info->fini_function
7342
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7343
0
           info->fini_function, false,
7344
0
           false, false)
7345
0
     : NULL);
7346
0
      if (h != NULL
7347
0
    && (h->ref_regular
7348
0
        || h->def_regular))
7349
0
  {
7350
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7351
0
      return false;
7352
0
  }
7353
7354
0
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7355
0
      if (s != NULL && s->linker_has_input)
7356
0
  {
7357
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7358
0
    if (! bfd_link_executable (info))
7359
0
      {
7360
0
        bfd *sub;
7361
0
        asection *o;
7362
7363
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7364
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7365
0
        && (o = sub->sections) != NULL
7366
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7367
0
      for (o = sub->sections; o != NULL; o = o->next)
7368
0
        if (elf_section_data (o)->this_hdr.sh_type
7369
0
      == SHT_PREINIT_ARRAY)
7370
0
          {
7371
0
      _bfd_error_handler
7372
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7373
0
         sub);
7374
0
      break;
7375
0
          }
7376
7377
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7378
0
        return false;
7379
0
      }
7380
7381
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7382
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7383
0
      return false;
7384
0
  }
7385
0
      s = bfd_get_section_by_name (output_bfd, ".init_array");
7386
0
      if (s != NULL && s->linker_has_input)
7387
0
  {
7388
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7389
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7390
0
      return false;
7391
0
  }
7392
0
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
7393
0
      if (s != NULL && s->linker_has_input)
7394
0
  {
7395
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7396
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7397
0
      return false;
7398
0
  }
7399
7400
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7401
      /* If .dynstr is excluded from the link, we don't want any of
7402
   these tags.  Strictly, we should be checking each section
7403
   individually;  This quick check covers for the case where
7404
   someone does a /DISCARD/ : { *(*) }.  */
7405
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7406
0
  {
7407
0
    bfd_size_type strsize;
7408
7409
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7410
0
    if ((info->emit_hash
7411
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7412
0
        || (info->emit_gnu_hash
7413
0
      && (bed->record_xhash_symbol == NULL
7414
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7415
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7416
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7417
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7418
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7419
0
                bed->s->sizeof_sym)
7420
0
        || (info->gnu_flags_1
7421
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7422
0
              info->gnu_flags_1)))
7423
0
      return false;
7424
0
  }
7425
0
    }
7426
7427
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7428
0
    return false;
7429
7430
  /* The backend must work out the sizes of all the other dynamic
7431
     sections.  */
7432
0
  if (dynobj != NULL
7433
0
      && bed->elf_backend_size_dynamic_sections != NULL
7434
0
      && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7435
0
    return false;
7436
7437
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7438
0
    {
7439
0
      if (elf_tdata (output_bfd)->cverdefs)
7440
0
  {
7441
0
    unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7442
7443
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7444
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7445
0
      return false;
7446
0
  }
7447
7448
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7449
0
  {
7450
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7451
0
      return false;
7452
0
  }
7453
0
      else if (info->flags & DF_BIND_NOW)
7454
0
  {
7455
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7456
0
      return false;
7457
0
  }
7458
7459
0
      if (info->flags_1)
7460
0
  {
7461
0
    if (bfd_link_executable (info))
7462
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7463
0
        | DF_1_NODELETE
7464
0
        | DF_1_NOOPEN);
7465
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7466
0
      return false;
7467
0
  }
7468
7469
0
      if (elf_tdata (output_bfd)->cverrefs)
7470
0
  {
7471
0
    unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7472
7473
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7474
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7475
0
      return false;
7476
0
  }
7477
7478
0
      if ((elf_tdata (output_bfd)->cverrefs == 0
7479
0
     && elf_tdata (output_bfd)->cverdefs == 0)
7480
0
    || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7481
0
  {
7482
0
    asection *s;
7483
7484
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7485
0
    s->flags |= SEC_EXCLUDE;
7486
0
  }
7487
0
    }
7488
0
  return true;
7489
0
}
7490
7491
/* Find the first non-excluded output section.  We'll use its
7492
   section symbol for some emitted relocs.  */
7493
void
7494
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7495
0
{
7496
0
  asection *s;
7497
0
  asection *found = NULL;
7498
7499
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7500
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7501
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7502
0
      {
7503
0
  found = s;
7504
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7505
0
    break;
7506
0
      }
7507
0
  elf_hash_table (info)->text_index_section = found;
7508
0
}
7509
7510
/* Find two non-excluded output sections, one for code, one for data.
7511
   We'll use their section symbols for some emitted relocs.  */
7512
void
7513
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7514
0
{
7515
0
  asection *s;
7516
0
  asection *found = NULL;
7517
7518
  /* Data first, since setting text_index_section changes
7519
     _bfd_elf_omit_section_dynsym_default.  */
7520
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7521
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7522
0
  && !(s->flags & SEC_READONLY)
7523
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7524
0
      {
7525
0
  found = s;
7526
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7527
0
    break;
7528
0
      }
7529
0
  elf_hash_table (info)->data_index_section = found;
7530
7531
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7532
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7533
0
  && (s->flags & SEC_READONLY)
7534
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7535
0
      {
7536
0
  found = s;
7537
0
  break;
7538
0
      }
7539
0
  elf_hash_table (info)->text_index_section = found;
7540
0
}
7541
7542
#define GNU_HASH_SECTION_NAME(bed)          \
7543
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7544
7545
bool
7546
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7547
0
{
7548
0
  const struct elf_backend_data *bed;
7549
0
  unsigned long section_sym_count;
7550
0
  bfd_size_type dynsymcount = 0;
7551
7552
0
  if (!is_elf_hash_table (info->hash))
7553
0
    return true;
7554
7555
0
  bed = get_elf_backend_data (output_bfd);
7556
0
  (*bed->elf_backend_init_index_section) (output_bfd, info);
7557
7558
  /* Assign dynsym indices.  In a shared library we generate a section
7559
     symbol for each output section, which come first.  Next come all
7560
     of the back-end allocated local dynamic syms, followed by the rest
7561
     of the global symbols.
7562
7563
     This is usually not needed for static binaries, however backends
7564
     can request to always do it, e.g. the MIPS backend uses dynamic
7565
     symbol counts to lay out GOT, which will be produced in the
7566
     presence of GOT relocations even in static binaries (holding fixed
7567
     data in that case, to satisfy those relocations).  */
7568
7569
0
  if (elf_hash_table (info)->dynamic_sections_created
7570
0
      || bed->always_renumber_dynsyms)
7571
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7572
0
              &section_sym_count);
7573
7574
0
  if (elf_hash_table (info)->dynamic_sections_created)
7575
0
    {
7576
0
      bfd *dynobj;
7577
0
      asection *s;
7578
0
      unsigned int dtagcount;
7579
7580
0
      dynobj = elf_hash_table (info)->dynobj;
7581
7582
      /* Work out the size of the symbol version section.  */
7583
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7584
0
      BFD_ASSERT (s != NULL);
7585
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7586
0
  {
7587
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7588
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7589
0
    if (s->contents == NULL)
7590
0
      return false;
7591
7592
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7593
0
      return false;
7594
0
  }
7595
7596
      /* Set the size of the .dynsym and .hash sections.  We counted
7597
   the number of dynamic symbols in elf_link_add_object_symbols.
7598
   We will build the contents of .dynsym and .hash when we build
7599
   the final symbol table, because until then we do not know the
7600
   correct value to give the symbols.  We built the .dynstr
7601
   section as we went along in elf_link_add_object_symbols.  */
7602
0
      s = elf_hash_table (info)->dynsym;
7603
0
      BFD_ASSERT (s != NULL);
7604
0
      s->size = dynsymcount * bed->s->sizeof_sym;
7605
7606
0
      s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7607
0
      if (s->contents == NULL)
7608
0
  return false;
7609
7610
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7611
   section syms, in case we don't output them all.  */
7612
0
      ++section_sym_count;
7613
0
      memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7614
7615
0
      elf_hash_table (info)->bucketcount = 0;
7616
7617
      /* Compute the size of the hashing table.  As a side effect this
7618
   computes the hash values for all the names we export.  */
7619
0
      if (info->emit_hash)
7620
0
  {
7621
0
    unsigned long int *hashcodes;
7622
0
    struct hash_codes_info hashinf;
7623
0
    bfd_size_type amt;
7624
0
    unsigned long int nsyms;
7625
0
    size_t bucketcount;
7626
0
    size_t hash_entry_size;
7627
7628
    /* Compute the hash values for all exported symbols.  At the same
7629
       time store the values in an array so that we could use them for
7630
       optimizations.  */
7631
0
    amt = dynsymcount * sizeof (unsigned long int);
7632
0
    hashcodes = (unsigned long int *) bfd_malloc (amt);
7633
0
    if (hashcodes == NULL)
7634
0
      return false;
7635
0
    hashinf.hashcodes = hashcodes;
7636
0
    hashinf.error = false;
7637
7638
    /* Put all hash values in HASHCODES.  */
7639
0
    elf_link_hash_traverse (elf_hash_table (info),
7640
0
          elf_collect_hash_codes, &hashinf);
7641
0
    if (hashinf.error)
7642
0
      {
7643
0
        free (hashcodes);
7644
0
        return false;
7645
0
      }
7646
7647
0
    nsyms = hashinf.hashcodes - hashcodes;
7648
0
    bucketcount
7649
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7650
0
    free (hashcodes);
7651
7652
0
    if (bucketcount == 0 && nsyms > 0)
7653
0
      return false;
7654
7655
0
    elf_hash_table (info)->bucketcount = bucketcount;
7656
7657
0
    s = bfd_get_linker_section (dynobj, ".hash");
7658
0
    BFD_ASSERT (s != NULL);
7659
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7660
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7661
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7662
0
    if (s->contents == NULL)
7663
0
      return false;
7664
7665
0
    bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7666
0
    bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7667
0
       s->contents + hash_entry_size);
7668
0
  }
7669
7670
0
      if (info->emit_gnu_hash)
7671
0
  {
7672
0
    size_t i, cnt;
7673
0
    unsigned char *contents;
7674
0
    struct collect_gnu_hash_codes cinfo;
7675
0
    bfd_size_type amt;
7676
0
    size_t bucketcount;
7677
7678
0
    memset (&cinfo, 0, sizeof (cinfo));
7679
7680
    /* Compute the hash values for all exported symbols.  At the same
7681
       time store the values in an array so that we could use them for
7682
       optimizations.  */
7683
0
    amt = dynsymcount * 2 * sizeof (unsigned long int);
7684
0
    cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7685
0
    if (cinfo.hashcodes == NULL)
7686
0
      return false;
7687
7688
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
7689
0
    cinfo.min_dynindx = -1;
7690
0
    cinfo.output_bfd = output_bfd;
7691
0
    cinfo.bed = bed;
7692
7693
    /* Put all hash values in HASHCODES.  */
7694
0
    elf_link_hash_traverse (elf_hash_table (info),
7695
0
          elf_collect_gnu_hash_codes, &cinfo);
7696
0
    if (cinfo.error)
7697
0
      {
7698
0
        free (cinfo.hashcodes);
7699
0
        return false;
7700
0
      }
7701
7702
0
    bucketcount
7703
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7704
7705
0
    if (bucketcount == 0)
7706
0
      {
7707
0
        free (cinfo.hashcodes);
7708
0
        return false;
7709
0
      }
7710
7711
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7712
0
    BFD_ASSERT (s != NULL);
7713
7714
0
    if (cinfo.nsyms == 0)
7715
0
      {
7716
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
7717
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
7718
0
        free (cinfo.hashcodes);
7719
0
        s->size = 5 * 4 + bed->s->arch_size / 8;
7720
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7721
0
        if (contents == NULL)
7722
0
    return false;
7723
0
        s->contents = contents;
7724
        /* 1 empty bucket.  */
7725
0
        bfd_put_32 (output_bfd, 1, contents);
7726
        /* SYMIDX above the special symbol 0.  */
7727
0
        bfd_put_32 (output_bfd, 1, contents + 4);
7728
        /* Just one word for bitmask.  */
7729
0
        bfd_put_32 (output_bfd, 1, contents + 8);
7730
        /* Only hash fn bloom filter.  */
7731
0
        bfd_put_32 (output_bfd, 0, contents + 12);
7732
        /* No hashes are valid - empty bitmask.  */
7733
0
        bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7734
        /* No hashes in the only bucket.  */
7735
0
        bfd_put_32 (output_bfd, 0,
7736
0
        contents + 16 + bed->s->arch_size / 8);
7737
0
      }
7738
0
    else
7739
0
      {
7740
0
        unsigned long int maskwords, maskbitslog2, x;
7741
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
7742
7743
0
        x = cinfo.nsyms;
7744
0
        maskbitslog2 = 1;
7745
0
        while ((x >>= 1) != 0)
7746
0
    ++maskbitslog2;
7747
0
        if (maskbitslog2 < 3)
7748
0
    maskbitslog2 = 5;
7749
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7750
0
    maskbitslog2 = maskbitslog2 + 3;
7751
0
        else
7752
0
    maskbitslog2 = maskbitslog2 + 2;
7753
0
        if (bed->s->arch_size == 64)
7754
0
    {
7755
0
      if (maskbitslog2 == 5)
7756
0
        maskbitslog2 = 6;
7757
0
      cinfo.shift1 = 6;
7758
0
    }
7759
0
        else
7760
0
    cinfo.shift1 = 5;
7761
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
7762
0
        cinfo.shift2 = maskbitslog2;
7763
0
        cinfo.maskbits = 1 << maskbitslog2;
7764
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7765
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
7766
0
        amt += maskwords * sizeof (bfd_vma);
7767
0
        cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7768
0
        if (cinfo.bitmask == NULL)
7769
0
    {
7770
0
      free (cinfo.hashcodes);
7771
0
      return false;
7772
0
    }
7773
7774
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7775
0
        cinfo.indx = cinfo.counts + bucketcount;
7776
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
7777
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7778
7779
        /* Determine how often each hash bucket is used.  */
7780
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7781
0
        for (i = 0; i < cinfo.nsyms; ++i)
7782
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7783
7784
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7785
0
    if (cinfo.counts[i] != 0)
7786
0
      {
7787
0
        cinfo.indx[i] = cnt;
7788
0
        cnt += cinfo.counts[i];
7789
0
      }
7790
0
        BFD_ASSERT (cnt == dynsymcount);
7791
0
        cinfo.bucketcount = bucketcount;
7792
0
        cinfo.local_indx = cinfo.min_dynindx;
7793
7794
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7795
0
        s->size += cinfo.maskbits / 8;
7796
0
        if (bed->record_xhash_symbol != NULL)
7797
0
    s->size += cinfo.nsyms * 4;
7798
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7799
0
        if (contents == NULL)
7800
0
    {
7801
0
      free (cinfo.bitmask);
7802
0
      free (cinfo.hashcodes);
7803
0
      return false;
7804
0
    }
7805
7806
0
        s->contents = contents;
7807
0
        bfd_put_32 (output_bfd, bucketcount, contents);
7808
0
        bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7809
0
        bfd_put_32 (output_bfd, maskwords, contents + 8);
7810
0
        bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7811
0
        contents += 16 + cinfo.maskbits / 8;
7812
7813
0
        for (i = 0; i < bucketcount; ++i)
7814
0
    {
7815
0
      if (cinfo.counts[i] == 0)
7816
0
        bfd_put_32 (output_bfd, 0, contents);
7817
0
      else
7818
0
        bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7819
0
      contents += 4;
7820
0
    }
7821
7822
0
        cinfo.contents = contents;
7823
7824
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7825
        /* Renumber dynamic symbols, if populating .gnu.hash section.
7826
     If using .MIPS.xhash, populate the translation table.  */
7827
0
        elf_link_hash_traverse (elf_hash_table (info),
7828
0
              elf_gnu_hash_process_symidx, &cinfo);
7829
7830
0
        contents = s->contents + 16;
7831
0
        for (i = 0; i < maskwords; ++i)
7832
0
    {
7833
0
      bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7834
0
         contents);
7835
0
      contents += bed->s->arch_size / 8;
7836
0
    }
7837
7838
0
        free (cinfo.bitmask);
7839
0
        free (cinfo.hashcodes);
7840
0
      }
7841
0
  }
7842
7843
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
7844
0
      BFD_ASSERT (s != NULL);
7845
7846
0
      elf_finalize_dynstr (output_bfd, info);
7847
7848
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7849
7850
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7851
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7852
0
    return false;
7853
0
    }
7854
7855
0
  return true;
7856
0
}
7857

7858
/* Make sure sec_info_type is cleared if sec_info is cleared too.  */
7859
7860
static void
7861
merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7862
          asection *sec)
7863
0
{
7864
0
  BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7865
0
  sec->sec_info_type = SEC_INFO_TYPE_NONE;
7866
0
}
7867
7868
/* Finish SHF_MERGE section merging.  */
7869
7870
bool
7871
_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7872
0
{
7873
0
  bfd *ibfd;
7874
0
  asection *sec;
7875
7876
0
  if (!is_elf_hash_table (info->hash))
7877
0
    return false;
7878
7879
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7880
0
    if ((ibfd->flags & DYNAMIC) == 0
7881
0
  && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7882
0
  && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7883
0
      == get_elf_backend_data (obfd)->s->elfclass))
7884
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7885
0
  if ((sec->flags & SEC_MERGE) != 0
7886
0
      && !bfd_is_abs_section (sec->output_section))
7887
0
    {
7888
0
      struct bfd_elf_section_data *secdata;
7889
7890
0
      secdata = elf_section_data (sec);
7891
0
      if (! _bfd_add_merge_section (obfd,
7892
0
            &elf_hash_table (info)->merge_info,
7893
0
            sec, &secdata->sec_info))
7894
0
        return false;
7895
0
      else if (secdata->sec_info)
7896
0
        sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7897
0
    }
7898
7899
0
  if (elf_hash_table (info)->merge_info != NULL)
7900
0
    _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7901
0
       merge_sections_remove_hook);
7902
0
  return true;
7903
0
}
7904
7905
/* Create an entry in an ELF linker hash table.  */
7906
7907
struct bfd_hash_entry *
7908
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7909
          struct bfd_hash_table *table,
7910
          const char *string)
7911
0
{
7912
  /* Allocate the structure if it has not already been allocated by a
7913
     subclass.  */
7914
0
  if (entry == NULL)
7915
0
    {
7916
0
      entry = (struct bfd_hash_entry *)
7917
0
  bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7918
0
      if (entry == NULL)
7919
0
  return entry;
7920
0
    }
7921
7922
  /* Call the allocation method of the superclass.  */
7923
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
7924
0
  if (entry != NULL)
7925
0
    {
7926
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7927
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7928
7929
      /* Set local fields.  */
7930
0
      ret->indx = -1;
7931
0
      ret->dynindx = -1;
7932
0
      ret->got = htab->init_got_refcount;
7933
0
      ret->plt = htab->init_plt_refcount;
7934
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7935
0
            - offsetof (struct elf_link_hash_entry, size)));
7936
      /* Assume that we have been called by a non-ELF symbol reader.
7937
   This flag is then reset by the code which reads an ELF input
7938
   file.  This ensures that a symbol created by a non-ELF symbol
7939
   reader will have the flag set correctly.  */
7940
0
      ret->non_elf = 1;
7941
0
    }
7942
7943
0
  return entry;
7944
0
}
7945
7946
/* Copy data from an indirect symbol to its direct symbol, hiding the
7947
   old indirect symbol.  Also used for copying flags to a weakdef.  */
7948
7949
void
7950
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7951
          struct elf_link_hash_entry *dir,
7952
          struct elf_link_hash_entry *ind)
7953
0
{
7954
0
  struct elf_link_hash_table *htab;
7955
7956
0
  if (ind->dyn_relocs != NULL)
7957
0
    {
7958
0
      if (dir->dyn_relocs != NULL)
7959
0
  {
7960
0
    struct elf_dyn_relocs **pp;
7961
0
    struct elf_dyn_relocs *p;
7962
7963
    /* Add reloc counts against the indirect sym to the direct sym
7964
       list.  Merge any entries against the same section.  */
7965
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7966
0
      {
7967
0
        struct elf_dyn_relocs *q;
7968
7969
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
7970
0
    if (q->sec == p->sec)
7971
0
      {
7972
0
        q->pc_count += p->pc_count;
7973
0
        q->count += p->count;
7974
0
        *pp = p->next;
7975
0
        break;
7976
0
      }
7977
0
        if (q == NULL)
7978
0
    pp = &p->next;
7979
0
      }
7980
0
    *pp = dir->dyn_relocs;
7981
0
  }
7982
7983
0
      dir->dyn_relocs = ind->dyn_relocs;
7984
0
      ind->dyn_relocs = NULL;
7985
0
    }
7986
7987
  /* Copy down any references that we may have already seen to the
7988
     symbol which just became indirect.  */
7989
7990
0
  if (dir->versioned != versioned_hidden)
7991
0
    dir->ref_dynamic |= ind->ref_dynamic;
7992
0
  dir->ref_regular |= ind->ref_regular;
7993
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7994
0
  dir->non_got_ref |= ind->non_got_ref;
7995
0
  dir->needs_plt |= ind->needs_plt;
7996
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
7997
7998
0
  if (ind->root.type != bfd_link_hash_indirect)
7999
0
    return;
8000
8001
  /* Copy over the global and procedure linkage table refcount entries.
8002
     These may have been already set up by a check_relocs routine.  */
8003
0
  htab = elf_hash_table (info);
8004
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8005
0
    {
8006
0
      if (dir->got.refcount < 0)
8007
0
  dir->got.refcount = 0;
8008
0
      dir->got.refcount += ind->got.refcount;
8009
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8010
0
    }
8011
8012
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8013
0
    {
8014
0
      if (dir->plt.refcount < 0)
8015
0
  dir->plt.refcount = 0;
8016
0
      dir->plt.refcount += ind->plt.refcount;
8017
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8018
0
    }
8019
8020
0
  if (ind->dynindx != -1)
8021
0
    {
8022
0
      if (dir->dynindx != -1)
8023
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8024
0
      dir->dynindx = ind->dynindx;
8025
0
      dir->dynstr_index = ind->dynstr_index;
8026
0
      ind->dynindx = -1;
8027
0
      ind->dynstr_index = 0;
8028
0
    }
8029
0
}
8030
8031
void
8032
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8033
        struct elf_link_hash_entry *h,
8034
        bool force_local)
8035
0
{
8036
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8037
0
  if (h->type != STT_GNU_IFUNC)
8038
0
    {
8039
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8040
0
      h->needs_plt = 0;
8041
0
    }
8042
0
  if (force_local)
8043
0
    {
8044
0
      h->forced_local = 1;
8045
0
      if (h->dynindx != -1)
8046
0
  {
8047
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8048
0
          h->dynstr_index);
8049
0
    h->dynindx = -1;
8050
0
    h->dynstr_index = 0;
8051
0
  }
8052
0
    }
8053
0
}
8054
8055
/* Hide a symbol. */
8056
8057
void
8058
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8059
         struct bfd_link_info *info,
8060
         struct bfd_link_hash_entry *h)
8061
0
{
8062
0
  if (is_elf_hash_table (info->hash))
8063
0
    {
8064
0
      const struct elf_backend_data *bed
8065
0
  = get_elf_backend_data (output_bfd);
8066
0
      struct elf_link_hash_entry *eh
8067
0
  = (struct elf_link_hash_entry *) h;
8068
0
      bed->elf_backend_hide_symbol (info, eh, true);
8069
0
      eh->def_dynamic = 0;
8070
0
      eh->ref_dynamic = 0;
8071
0
      eh->dynamic_def = 0;
8072
0
    }
8073
0
}
8074
8075
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8076
   caller.  */
8077
8078
bool
8079
_bfd_elf_link_hash_table_init
8080
  (struct elf_link_hash_table *table,
8081
   bfd *abfd,
8082
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8083
              struct bfd_hash_table *,
8084
              const char *),
8085
   unsigned int entsize,
8086
   enum elf_target_id target_id)
8087
0
{
8088
0
  bool ret;
8089
0
  int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8090
8091
0
  table->init_got_refcount.refcount = can_refcount - 1;
8092
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8093
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8094
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8095
  /* The first dynamic symbol is a dummy.  */
8096
0
  table->dynsymcount = 1;
8097
8098
0
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8099
8100
0
  table->root.type = bfd_link_elf_hash_table;
8101
0
  table->hash_table_id = target_id;
8102
0
  table->target_os = get_elf_backend_data (abfd)->target_os;
8103
8104
0
  return ret;
8105
0
}
8106
8107
/* Create an ELF linker hash table.  */
8108
8109
struct bfd_link_hash_table *
8110
_bfd_elf_link_hash_table_create (bfd *abfd)
8111
0
{
8112
0
  struct elf_link_hash_table *ret;
8113
0
  size_t amt = sizeof (struct elf_link_hash_table);
8114
8115
0
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8116
0
  if (ret == NULL)
8117
0
    return NULL;
8118
8119
0
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8120
0
               sizeof (struct elf_link_hash_entry),
8121
0
               GENERIC_ELF_DATA))
8122
0
    {
8123
0
      free (ret);
8124
0
      return NULL;
8125
0
    }
8126
0
  ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8127
8128
0
  return &ret->root;
8129
0
}
8130
8131
/* Destroy an ELF linker hash table.  */
8132
8133
void
8134
_bfd_elf_link_hash_table_free (bfd *obfd)
8135
0
{
8136
0
  struct elf_link_hash_table *htab;
8137
8138
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8139
0
  if (htab->dynstr != NULL)
8140
0
    _bfd_elf_strtab_free (htab->dynstr);
8141
0
  _bfd_merge_sections_free (htab->merge_info);
8142
0
  _bfd_generic_link_hash_table_free (obfd);
8143
0
}
8144
8145
/* This is a hook for the ELF emulation code in the generic linker to
8146
   tell the backend linker what file name to use for the DT_NEEDED
8147
   entry for a dynamic object.  */
8148
8149
void
8150
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8151
0
{
8152
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8153
0
      && bfd_get_format (abfd) == bfd_object)
8154
0
    elf_dt_name (abfd) = name;
8155
0
}
8156
8157
int
8158
bfd_elf_get_dyn_lib_class (bfd *abfd)
8159
0
{
8160
0
  int lib_class;
8161
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8162
0
      && bfd_get_format (abfd) == bfd_object)
8163
0
    lib_class = elf_dyn_lib_class (abfd);
8164
0
  else
8165
0
    lib_class = 0;
8166
0
  return lib_class;
8167
0
}
8168
8169
void
8170
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8171
0
{
8172
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8173
0
      && bfd_get_format (abfd) == bfd_object)
8174
0
    elf_dyn_lib_class (abfd) = lib_class;
8175
0
}
8176
8177
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8178
   the linker ELF emulation code.  */
8179
8180
struct bfd_link_needed_list *
8181
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8182
       struct bfd_link_info *info)
8183
0
{
8184
0
  if (! is_elf_hash_table (info->hash))
8185
0
    return NULL;
8186
0
  return elf_hash_table (info)->needed;
8187
0
}
8188
8189
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8190
   hook for the linker ELF emulation code.  */
8191
8192
struct bfd_link_needed_list *
8193
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8194
        struct bfd_link_info *info)
8195
0
{
8196
0
  if (! is_elf_hash_table (info->hash))
8197
0
    return NULL;
8198
0
  return elf_hash_table (info)->runpath;
8199
0
}
8200
8201
/* Get the name actually used for a dynamic object for a link.  This
8202
   is the SONAME entry if there is one.  Otherwise, it is the string
8203
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8204
8205
const char *
8206
bfd_elf_get_dt_soname (bfd *abfd)
8207
0
{
8208
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8209
0
      && bfd_get_format (abfd) == bfd_object)
8210
0
    return elf_dt_name (abfd);
8211
0
  return NULL;
8212
0
}
8213
8214
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8215
   the ELF linker emulation code.  */
8216
8217
bool
8218
bfd_elf_get_bfd_needed_list (bfd *abfd,
8219
           struct bfd_link_needed_list **pneeded)
8220
0
{
8221
0
  asection *s;
8222
0
  bfd_byte *dynbuf = NULL;
8223
0
  unsigned int elfsec;
8224
0
  unsigned long shlink;
8225
0
  bfd_byte *extdyn, *extdynend;
8226
0
  size_t extdynsize;
8227
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8228
8229
0
  *pneeded = NULL;
8230
8231
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8232
0
      || bfd_get_format (abfd) != bfd_object)
8233
0
    return true;
8234
8235
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8236
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8237
0
    return true;
8238
8239
0
  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8240
0
    goto error_return;
8241
8242
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8243
0
  if (elfsec == SHN_BAD)
8244
0
    goto error_return;
8245
8246
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8247
8248
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8249
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8250
8251
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8252
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8253
0
       extdyn += extdynsize)
8254
0
    {
8255
0
      Elf_Internal_Dyn dyn;
8256
8257
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8258
8259
0
      if (dyn.d_tag == DT_NULL)
8260
0
  break;
8261
8262
0
      if (dyn.d_tag == DT_NEEDED)
8263
0
  {
8264
0
    const char *string;
8265
0
    struct bfd_link_needed_list *l;
8266
0
    unsigned int tagv = dyn.d_un.d_val;
8267
0
    size_t amt;
8268
8269
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8270
0
    if (string == NULL)
8271
0
      goto error_return;
8272
8273
0
    amt = sizeof *l;
8274
0
    l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8275
0
    if (l == NULL)
8276
0
      goto error_return;
8277
8278
0
    l->by = abfd;
8279
0
    l->name = string;
8280
0
    l->next = *pneeded;
8281
0
    *pneeded = l;
8282
0
  }
8283
0
    }
8284
8285
0
  free (dynbuf);
8286
8287
0
  return true;
8288
8289
0
 error_return:
8290
0
  free (dynbuf);
8291
0
  return false;
8292
0
}
8293
8294
struct elf_symbuf_symbol
8295
{
8296
  unsigned long st_name;  /* Symbol name, index in string tbl */
8297
  unsigned char st_info;  /* Type and binding attributes */
8298
  unsigned char st_other; /* Visibilty, and target specific */
8299
};
8300
8301
struct elf_symbuf_head
8302
{
8303
  struct elf_symbuf_symbol *ssym;
8304
  size_t count;
8305
  unsigned int st_shndx;
8306
};
8307
8308
struct elf_symbol
8309
{
8310
  union
8311
    {
8312
      Elf_Internal_Sym *isym;
8313
      struct elf_symbuf_symbol *ssym;
8314
      void *p;
8315
    } u;
8316
  const char *name;
8317
};
8318
8319
/* Sort references to symbols by ascending section number.  */
8320
8321
static int
8322
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8323
0
{
8324
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8325
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8326
8327
0
  if (s1->st_shndx != s2->st_shndx)
8328
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8329
  /* Final sort by the address of the sym in the symbuf ensures
8330
     a stable sort.  */
8331
0
  if (s1 != s2)
8332
0
    return s1 > s2 ? 1 : -1;
8333
0
  return 0;
8334
0
}
8335
8336
static int
8337
elf_sym_name_compare (const void *arg1, const void *arg2)
8338
0
{
8339
0
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8340
0
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8341
0
  int ret = strcmp (s1->name, s2->name);
8342
0
  if (ret != 0)
8343
0
    return ret;
8344
0
  if (s1->u.p != s2->u.p)
8345
0
    return s1->u.p > s2->u.p ? 1 : -1;
8346
0
  return 0;
8347
0
}
8348
8349
static struct elf_symbuf_head *
8350
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8351
0
{
8352
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8353
0
  struct elf_symbuf_symbol *ssym;
8354
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8355
0
  size_t i, shndx_count, total_size, amt;
8356
8357
0
  amt = symcount * sizeof (*indbuf);
8358
0
  indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8359
0
  if (indbuf == NULL)
8360
0
    return NULL;
8361
8362
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8363
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8364
0
      *ind++ = &isymbuf[i];
8365
0
  indbufend = ind;
8366
8367
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8368
0
   elf_sort_elf_symbol);
8369
8370
0
  shndx_count = 0;
8371
0
  if (indbufend > indbuf)
8372
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8373
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8374
0
  shndx_count++;
8375
8376
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8377
0
    + (indbufend - indbuf) * sizeof (*ssym));
8378
0
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8379
0
  if (ssymbuf == NULL)
8380
0
    {
8381
0
      free (indbuf);
8382
0
      return NULL;
8383
0
    }
8384
8385
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8386
0
  ssymbuf->ssym = NULL;
8387
0
  ssymbuf->count = shndx_count;
8388
0
  ssymbuf->st_shndx = 0;
8389
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8390
0
    {
8391
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8392
0
  {
8393
0
    ssymhead++;
8394
0
    ssymhead->ssym = ssym;
8395
0
    ssymhead->count = 0;
8396
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8397
0
  }
8398
0
      ssym->st_name = (*ind)->st_name;
8399
0
      ssym->st_info = (*ind)->st_info;
8400
0
      ssym->st_other = (*ind)->st_other;
8401
0
      ssymhead->count++;
8402
0
    }
8403
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8404
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8405
8406
0
  free (indbuf);
8407
0
  return ssymbuf;
8408
0
}
8409
8410
/* Check if 2 sections define the same set of local and global
8411
   symbols.  */
8412
8413
static bool
8414
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8415
           struct bfd_link_info *info)
8416
0
{
8417
0
  bfd *bfd1, *bfd2;
8418
0
  const struct elf_backend_data *bed1, *bed2;
8419
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8420
0
  size_t symcount1, symcount2;
8421
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8422
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8423
0
  Elf_Internal_Sym *isym, *isymend;
8424
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8425
0
  size_t count1, count2, sec_count1, sec_count2, i;
8426
0
  unsigned int shndx1, shndx2;
8427
0
  bool result;
8428
0
  bool ignore_section_symbol_p;
8429
8430
0
  bfd1 = sec1->owner;
8431
0
  bfd2 = sec2->owner;
8432
8433
  /* Both sections have to be in ELF.  */
8434
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8435
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8436
0
    return false;
8437
8438
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8439
0
    return false;
8440
8441
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8442
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8443
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8444
0
    return false;
8445
8446
0
  bed1 = get_elf_backend_data (bfd1);
8447
0
  bed2 = get_elf_backend_data (bfd2);
8448
0
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8449
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8450
0
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8451
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8452
8453
0
  if (symcount1 == 0 || symcount2 == 0)
8454
0
    return false;
8455
8456
0
  result = false;
8457
0
  isymbuf1 = NULL;
8458
0
  isymbuf2 = NULL;
8459
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8460
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8461
8462
  /* Ignore section symbols only when matching non-debugging sections
8463
     or linkonce section with comdat section.  */
8464
0
  ignore_section_symbol_p
8465
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8466
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8467
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8468
8469
0
  if (ssymbuf1 == NULL)
8470
0
    {
8471
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8472
0
               NULL, NULL, NULL);
8473
0
      if (isymbuf1 == NULL)
8474
0
  goto done;
8475
8476
0
      if (info != NULL && !info->reduce_memory_overheads)
8477
0
  {
8478
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8479
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8480
0
  }
8481
0
    }
8482
8483
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8484
0
    {
8485
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8486
0
               NULL, NULL, NULL);
8487
0
      if (isymbuf2 == NULL)
8488
0
  goto done;
8489
8490
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8491
0
  {
8492
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8493
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8494
0
  }
8495
0
    }
8496
8497
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8498
0
    {
8499
      /* Optimized faster version.  */
8500
0
      size_t lo, hi, mid;
8501
0
      struct elf_symbol *symp;
8502
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8503
8504
0
      lo = 0;
8505
0
      hi = ssymbuf1->count;
8506
0
      ssymbuf1++;
8507
0
      count1 = 0;
8508
0
      sec_count1 = 0;
8509
0
      while (lo < hi)
8510
0
  {
8511
0
    mid = (lo + hi) / 2;
8512
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8513
0
      hi = mid;
8514
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8515
0
      lo = mid + 1;
8516
0
    else
8517
0
      {
8518
0
        count1 = ssymbuf1[mid].count;
8519
0
        ssymbuf1 += mid;
8520
0
        break;
8521
0
      }
8522
0
  }
8523
0
      if (ignore_section_symbol_p)
8524
0
  {
8525
0
    for (i = 0; i < count1; i++)
8526
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8527
0
        sec_count1++;
8528
0
    count1 -= sec_count1;
8529
0
  }
8530
8531
0
      lo = 0;
8532
0
      hi = ssymbuf2->count;
8533
0
      ssymbuf2++;
8534
0
      count2 = 0;
8535
0
      sec_count2 = 0;
8536
0
      while (lo < hi)
8537
0
  {
8538
0
    mid = (lo + hi) / 2;
8539
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8540
0
      hi = mid;
8541
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8542
0
      lo = mid + 1;
8543
0
    else
8544
0
      {
8545
0
        count2 = ssymbuf2[mid].count;
8546
0
        ssymbuf2 += mid;
8547
0
        break;
8548
0
      }
8549
0
  }
8550
0
      if (ignore_section_symbol_p)
8551
0
  {
8552
0
    for (i = 0; i < count2; i++)
8553
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8554
0
        sec_count2++;
8555
0
    count2 -= sec_count2;
8556
0
  }
8557
8558
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8559
0
  goto done;
8560
8561
0
      symtable1
8562
0
  = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8563
0
      symtable2
8564
0
  = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8565
0
      if (symtable1 == NULL || symtable2 == NULL)
8566
0
  goto done;
8567
8568
0
      symp = symtable1;
8569
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8570
0
     ssym < ssymend; ssym++)
8571
0
  if (sec_count1 == 0
8572
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8573
0
    {
8574
0
      symp->u.ssym = ssym;
8575
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8576
0
                hdr1->sh_link,
8577
0
                ssym->st_name);
8578
0
      symp++;
8579
0
    }
8580
8581
0
      symp = symtable2;
8582
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8583
0
     ssym < ssymend; ssym++)
8584
0
  if (sec_count2 == 0
8585
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8586
0
    {
8587
0
      symp->u.ssym = ssym;
8588
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8589
0
                hdr2->sh_link,
8590
0
                ssym->st_name);
8591
0
      symp++;
8592
0
    }
8593
8594
      /* Sort symbol by name.  */
8595
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8596
0
       elf_sym_name_compare);
8597
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8598
0
       elf_sym_name_compare);
8599
8600
0
      for (i = 0; i < count1; i++)
8601
  /* Two symbols must have the same binding, type and name.  */
8602
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8603
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8604
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8605
0
    goto done;
8606
8607
0
      result = true;
8608
0
      goto done;
8609
0
    }
8610
8611
0
  symtable1 = (struct elf_symbol *)
8612
0
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8613
0
  symtable2 = (struct elf_symbol *)
8614
0
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8615
0
  if (symtable1 == NULL || symtable2 == NULL)
8616
0
    goto done;
8617
8618
  /* Count definitions in the section.  */
8619
0
  count1 = 0;
8620
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8621
0
    if (isym->st_shndx == shndx1
8622
0
  && (!ignore_section_symbol_p
8623
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8624
0
      symtable1[count1++].u.isym = isym;
8625
8626
0
  count2 = 0;
8627
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8628
0
    if (isym->st_shndx == shndx2
8629
0
  && (!ignore_section_symbol_p
8630
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8631
0
      symtable2[count2++].u.isym = isym;
8632
8633
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8634
0
    goto done;
8635
8636
0
  for (i = 0; i < count1; i++)
8637
0
    symtable1[i].name
8638
0
      = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8639
0
           symtable1[i].u.isym->st_name);
8640
8641
0
  for (i = 0; i < count2; i++)
8642
0
    symtable2[i].name
8643
0
      = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8644
0
           symtable2[i].u.isym->st_name);
8645
8646
  /* Sort symbol by name.  */
8647
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8648
0
   elf_sym_name_compare);
8649
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8650
0
   elf_sym_name_compare);
8651
8652
0
  for (i = 0; i < count1; i++)
8653
    /* Two symbols must have the same binding, type and name.  */
8654
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8655
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8656
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8657
0
      goto done;
8658
8659
0
  result = true;
8660
8661
0
 done:
8662
0
  free (symtable1);
8663
0
  free (symtable2);
8664
0
  free (isymbuf1);
8665
0
  free (isymbuf2);
8666
8667
0
  return result;
8668
0
}
8669
8670
/* Return TRUE if 2 section types are compatible.  */
8671
8672
bool
8673
_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8674
         bfd *bbfd, const asection *bsec)
8675
0
{
8676
0
  if (asec == NULL
8677
0
      || bsec == NULL
8678
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
8679
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
8680
0
    return true;
8681
8682
0
  return elf_section_type (asec) == elf_section_type (bsec);
8683
0
}
8684

8685
/* Final phase of ELF linker.  */
8686
8687
/* A structure we use to avoid passing large numbers of arguments.  */
8688
8689
struct elf_final_link_info
8690
{
8691
  /* General link information.  */
8692
  struct bfd_link_info *info;
8693
  /* Output BFD.  */
8694
  bfd *output_bfd;
8695
  /* Symbol string table.  */
8696
  struct elf_strtab_hash *symstrtab;
8697
  /* .hash section.  */
8698
  asection *hash_sec;
8699
  /* symbol version section (.gnu.version).  */
8700
  asection *symver_sec;
8701
  /* Buffer large enough to hold contents of any section.  */
8702
  bfd_byte *contents;
8703
  /* Buffer large enough to hold external relocs of any section.  */
8704
  void *external_relocs;
8705
  /* Buffer large enough to hold internal relocs of any section.  */
8706
  Elf_Internal_Rela *internal_relocs;
8707
  /* Buffer large enough to hold external local symbols of any input
8708
     BFD.  */
8709
  bfd_byte *external_syms;
8710
  /* And a buffer for symbol section indices.  */
8711
  Elf_External_Sym_Shndx *locsym_shndx;
8712
  /* Buffer large enough to hold internal local symbols of any input
8713
     BFD.  */
8714
  Elf_Internal_Sym *internal_syms;
8715
  /* Array large enough to hold a symbol index for each local symbol
8716
     of any input BFD.  */
8717
  long *indices;
8718
  /* Array large enough to hold a section pointer for each local
8719
     symbol of any input BFD.  */
8720
  asection **sections;
8721
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
8722
  Elf_External_Sym_Shndx *symshndxbuf;
8723
  /* Number of STT_FILE syms seen.  */
8724
  size_t filesym_count;
8725
  /* Local symbol hash table.  */
8726
  struct bfd_hash_table local_hash_table;
8727
};
8728
8729
struct local_hash_entry
8730
{
8731
  /* Base hash table entry structure.  */
8732
  struct bfd_hash_entry root;
8733
  /* Size of the local symbol name.  */
8734
  size_t size;
8735
  /* Number of the duplicated local symbol names.  */
8736
  long count;
8737
};
8738
8739
/* Create an entry in the local symbol hash table.  */
8740
8741
static struct bfd_hash_entry *
8742
local_hash_newfunc (struct bfd_hash_entry *entry,
8743
        struct bfd_hash_table *table,
8744
        const char *string)
8745
0
{
8746
8747
  /* Allocate the structure if it has not already been allocated by a
8748
     subclass.  */
8749
0
  if (entry == NULL)
8750
0
    {
8751
0
      entry = bfd_hash_allocate (table,
8752
0
         sizeof (struct local_hash_entry));
8753
0
      if (entry == NULL)
8754
0
        return entry;
8755
0
    }
8756
8757
  /* Call the allocation method of the superclass.  */
8758
0
  entry = bfd_hash_newfunc (entry, table, string);
8759
0
  if (entry != NULL)
8760
0
    {
8761
0
      ((struct local_hash_entry *) entry)->count = 0;
8762
0
      ((struct local_hash_entry *) entry)->size = 0;
8763
0
    }
8764
8765
0
  return entry;
8766
0
}
8767
8768
/* This struct is used to pass information to elf_link_output_extsym.  */
8769
8770
struct elf_outext_info
8771
{
8772
  bool failed;
8773
  bool localsyms;
8774
  bool file_sym_done;
8775
  struct elf_final_link_info *flinfo;
8776
};
8777
8778
8779
/* Support for evaluating a complex relocation.
8780
8781
   Complex relocations are generalized, self-describing relocations.  The
8782
   implementation of them consists of two parts: complex symbols, and the
8783
   relocations themselves.
8784
8785
   The relocations use a reserved elf-wide relocation type code (R_RELC
8786
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
8787
   information (start bit, end bit, word width, etc) into the addend.  This
8788
   information is extracted from CGEN-generated operand tables within gas.
8789
8790
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8791
   internal) representing prefix-notation expressions, including but not
8792
   limited to those sorts of expressions normally encoded as addends in the
8793
   addend field.  The symbol mangling format is:
8794
8795
   <node> := <literal>
8796
    |  <unary-operator> ':' <node>
8797
    |  <binary-operator> ':' <node> ':' <node>
8798
    ;
8799
8800
   <literal> := 's' <digits=N> ':' <N character symbol name>
8801
       |  'S' <digits=N> ':' <N character section name>
8802
       |  '#' <hexdigits>
8803
       ;
8804
8805
   <binary-operator> := as in C
8806
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
8807
8808
static void
8809
set_symbol_value (bfd *bfd_with_globals,
8810
      Elf_Internal_Sym *isymbuf,
8811
      size_t locsymcount,
8812
      size_t symidx,
8813
      bfd_vma val)
8814
0
{
8815
0
  struct elf_link_hash_entry **sym_hashes;
8816
0
  struct elf_link_hash_entry *h;
8817
0
  size_t extsymoff = locsymcount;
8818
8819
0
  if (symidx < locsymcount)
8820
0
    {
8821
0
      Elf_Internal_Sym *sym;
8822
8823
0
      sym = isymbuf + symidx;
8824
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8825
0
  {
8826
    /* It is a local symbol: move it to the
8827
       "absolute" section and give it a value.  */
8828
0
    sym->st_shndx = SHN_ABS;
8829
0
    sym->st_value = val;
8830
0
    return;
8831
0
  }
8832
0
      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8833
0
      extsymoff = 0;
8834
0
    }
8835
8836
  /* It is a global symbol: set its link type
8837
     to "defined" and give it a value.  */
8838
8839
0
  sym_hashes = elf_sym_hashes (bfd_with_globals);
8840
0
  h = sym_hashes [symidx - extsymoff];
8841
0
  while (h->root.type == bfd_link_hash_indirect
8842
0
   || h->root.type == bfd_link_hash_warning)
8843
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8844
0
  h->root.type = bfd_link_hash_defined;
8845
0
  h->root.u.def.value = val;
8846
0
  h->root.u.def.section = bfd_abs_section_ptr;
8847
0
}
8848
8849
static bool
8850
resolve_symbol (const char *name,
8851
    bfd *input_bfd,
8852
    struct elf_final_link_info *flinfo,
8853
    bfd_vma *result,
8854
    Elf_Internal_Sym *isymbuf,
8855
    size_t locsymcount)
8856
0
{
8857
0
  Elf_Internal_Sym *sym;
8858
0
  struct bfd_link_hash_entry *global_entry;
8859
0
  const char *candidate = NULL;
8860
0
  Elf_Internal_Shdr *symtab_hdr;
8861
0
  size_t i;
8862
8863
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8864
8865
0
  for (i = 0; i < locsymcount; ++ i)
8866
0
    {
8867
0
      sym = isymbuf + i;
8868
8869
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8870
0
  continue;
8871
8872
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
8873
0
               symtab_hdr->sh_link,
8874
0
               sym->st_name);
8875
#ifdef DEBUG
8876
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8877
        name, candidate, (unsigned long) sym->st_value);
8878
#endif
8879
0
      if (candidate && strcmp (candidate, name) == 0)
8880
0
  {
8881
0
    asection *sec = flinfo->sections [i];
8882
8883
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8884
0
    *result += sec->output_offset + sec->output_section->vma;
8885
#ifdef DEBUG
8886
    printf ("Found symbol with value %8.8lx\n",
8887
      (unsigned long) *result);
8888
#endif
8889
0
    return true;
8890
0
  }
8891
0
    }
8892
8893
  /* Hmm, haven't found it yet. perhaps it is a global.  */
8894
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8895
0
               false, false, true);
8896
0
  if (!global_entry)
8897
0
    return false;
8898
8899
0
  if (global_entry->type == bfd_link_hash_defined
8900
0
      || global_entry->type == bfd_link_hash_defweak)
8901
0
    {
8902
0
      *result = (global_entry->u.def.value
8903
0
     + global_entry->u.def.section->output_section->vma
8904
0
     + global_entry->u.def.section->output_offset);
8905
#ifdef DEBUG
8906
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8907
        global_entry->root.string, (unsigned long) *result);
8908
#endif
8909
0
      return true;
8910
0
    }
8911
8912
0
  return false;
8913
0
}
8914
8915
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
8916
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
8917
   names like "foo.end" which is the end address of section "foo".  */
8918
8919
static bool
8920
resolve_section (const char *name,
8921
     asection *sections,
8922
     bfd_vma *result,
8923
     bfd * abfd)
8924
0
{
8925
0
  asection *curr;
8926
0
  unsigned int len;
8927
8928
0
  for (curr = sections; curr; curr = curr->next)
8929
0
    if (strcmp (curr->name, name) == 0)
8930
0
      {
8931
0
  *result = curr->vma;
8932
0
  return true;
8933
0
      }
8934
8935
  /* Hmm. still haven't found it. try pseudo-section names.  */
8936
  /* FIXME: This could be coded more efficiently...  */
8937
0
  for (curr = sections; curr; curr = curr->next)
8938
0
    {
8939
0
      len = strlen (curr->name);
8940
0
      if (len > strlen (name))
8941
0
  continue;
8942
8943
0
      if (strncmp (curr->name, name, len) == 0)
8944
0
  {
8945
0
    if (startswith (name + len, ".end"))
8946
0
      {
8947
0
        *result = (curr->vma
8948
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
8949
0
        return true;
8950
0
      }
8951
8952
    /* Insert more pseudo-section names here, if you like.  */
8953
0
  }
8954
0
    }
8955
8956
0
  return false;
8957
0
}
8958
8959
static void
8960
undefined_reference (const char *reftype, const char *name)
8961
0
{
8962
  /* xgettext:c-format */
8963
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8964
0
          reftype, name);
8965
0
  bfd_set_error (bfd_error_bad_value);
8966
0
}
8967
8968
static bool
8969
eval_symbol (bfd_vma *result,
8970
       const char **symp,
8971
       bfd *input_bfd,
8972
       struct elf_final_link_info *flinfo,
8973
       bfd_vma dot,
8974
       Elf_Internal_Sym *isymbuf,
8975
       size_t locsymcount,
8976
       int signed_p)
8977
0
{
8978
0
  size_t len;
8979
0
  size_t symlen;
8980
0
  bfd_vma a;
8981
0
  bfd_vma b;
8982
0
  char symbuf[4096];
8983
0
  const char *sym = *symp;
8984
0
  const char *symend;
8985
0
  bool symbol_is_section = false;
8986
8987
0
  len = strlen (sym);
8988
0
  symend = sym + len;
8989
8990
0
  if (len < 1 || len > sizeof (symbuf))
8991
0
    {
8992
0
      bfd_set_error (bfd_error_invalid_operation);
8993
0
      return false;
8994
0
    }
8995
8996
0
  switch (* sym)
8997
0
    {
8998
0
    case '.':
8999
0
      *result = dot;
9000
0
      *symp = sym + 1;
9001
0
      return true;
9002
9003
0
    case '#':
9004
0
      ++sym;
9005
0
      *result = strtoul (sym, (char **) symp, 16);
9006
0
      return true;
9007
9008
0
    case 'S':
9009
0
      symbol_is_section = true;
9010
      /* Fall through.  */
9011
0
    case 's':
9012
0
      ++sym;
9013
0
      symlen = strtol (sym, (char **) symp, 10);
9014
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9015
9016
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9017
0
  {
9018
0
    bfd_set_error (bfd_error_invalid_operation);
9019
0
    return false;
9020
0
  }
9021
9022
0
      memcpy (symbuf, sym, symlen);
9023
0
      symbuf[symlen] = '\0';
9024
0
      *symp = sym + symlen;
9025
9026
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9027
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9028
   interpretation here; section means "try section first", not "must be a
9029
   section", and likewise with symbol.  */
9030
9031
0
      if (symbol_is_section)
9032
0
  {
9033
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9034
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9035
0
          isymbuf, locsymcount))
9036
0
      {
9037
0
        undefined_reference ("section", symbuf);
9038
0
        return false;
9039
0
      }
9040
0
  }
9041
0
      else
9042
0
  {
9043
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9044
0
             isymbuf, locsymcount)
9045
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9046
0
           result, input_bfd))
9047
0
      {
9048
0
        undefined_reference ("symbol", symbuf);
9049
0
        return false;
9050
0
      }
9051
0
  }
9052
9053
0
      return true;
9054
9055
      /* All that remains are operators.  */
9056
9057
0
#define UNARY_OP(op)            \
9058
0
  if (startswith (sym, #op))         \
9059
0
    {               \
9060
0
      sym += strlen (#op);          \
9061
0
      if (*sym == ':')           \
9062
0
  ++sym;             \
9063
0
      *symp = sym;            \
9064
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9065
0
      isymbuf, locsymcount, signed_p)) \
9066
0
  return false;           \
9067
0
      if (signed_p)           \
9068
0
  *result = op ((bfd_signed_vma) a);     \
9069
0
      else              \
9070
0
  *result = op a;           \
9071
0
      return true;            \
9072
0
    }
9073
9074
0
#define BINARY_OP_HEAD(op)          \
9075
0
  if (startswith (sym, #op))         \
9076
0
    {               \
9077
0
      sym += strlen (#op);          \
9078
0
      if (*sym == ':')           \
9079
0
  ++sym;             \
9080
0
      *symp = sym;            \
9081
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9082
0
      isymbuf, locsymcount, signed_p)) \
9083
0
  return false;           \
9084
0
      ++*symp;              \
9085
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9086
0
      isymbuf, locsymcount, signed_p)) \
9087
0
  return false;
9088
0
#define BINARY_OP_TAIL(op)          \
9089
0
      if (signed_p)           \
9090
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9091
0
      else              \
9092
0
  *result = a op b;         \
9093
0
      return true;            \
9094
0
    }
9095
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9096
9097
0
    default:
9098
0
      UNARY_OP  (0-);
9099
0
      BINARY_OP_HEAD (<<);
9100
0
      if (b >= sizeof (a) * CHAR_BIT)
9101
0
  {
9102
0
    *result = 0;
9103
0
    return true;
9104
0
  }
9105
0
      signed_p = 0;
9106
0
      BINARY_OP_TAIL (<<);
9107
0
      BINARY_OP_HEAD (>>);
9108
0
      if (b >= sizeof (a) * CHAR_BIT)
9109
0
  {
9110
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9111
0
    return true;
9112
0
  }
9113
0
      BINARY_OP_TAIL (>>);
9114
0
      BINARY_OP (==);
9115
0
      BINARY_OP (!=);
9116
0
      BINARY_OP (<=);
9117
0
      BINARY_OP (>=);
9118
0
      BINARY_OP (&&);
9119
0
      BINARY_OP (||);
9120
0
      UNARY_OP  (~);
9121
0
      UNARY_OP  (!);
9122
0
      BINARY_OP (*);
9123
0
      BINARY_OP_HEAD (/);
9124
0
      if (b == 0)
9125
0
  {
9126
0
    _bfd_error_handler (_("division by zero"));
9127
0
    bfd_set_error (bfd_error_bad_value);
9128
0
    return false;
9129
0
  }
9130
0
      BINARY_OP_TAIL (/);
9131
0
      BINARY_OP_HEAD (%);
9132
0
      if (b == 0)
9133
0
  {
9134
0
    _bfd_error_handler (_("division by zero"));
9135
0
    bfd_set_error (bfd_error_bad_value);
9136
0
    return false;
9137
0
  }
9138
0
      BINARY_OP_TAIL (%);
9139
0
      BINARY_OP (^);
9140
0
      BINARY_OP (|);
9141
0
      BINARY_OP (&);
9142
0
      BINARY_OP (+);
9143
0
      BINARY_OP (-);
9144
0
      BINARY_OP (<);
9145
0
      BINARY_OP (>);
9146
0
#undef UNARY_OP
9147
0
#undef BINARY_OP
9148
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9149
0
      bfd_set_error (bfd_error_invalid_operation);
9150
0
      return false;
9151
0
    }
9152
0
}
9153
9154
static void
9155
put_value (bfd_vma size,
9156
     unsigned long chunksz,
9157
     bfd *input_bfd,
9158
     bfd_vma x,
9159
     bfd_byte *location)
9160
0
{
9161
0
  location += (size - chunksz);
9162
9163
0
  for (; size; size -= chunksz, location -= chunksz)
9164
0
    {
9165
0
      switch (chunksz)
9166
0
  {
9167
0
  case 1:
9168
0
    bfd_put_8 (input_bfd, x, location);
9169
0
    x >>= 8;
9170
0
    break;
9171
0
  case 2:
9172
0
    bfd_put_16 (input_bfd, x, location);
9173
0
    x >>= 16;
9174
0
    break;
9175
0
  case 4:
9176
0
    bfd_put_32 (input_bfd, x, location);
9177
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9178
0
    x >>= 16;
9179
0
    x >>= 16;
9180
0
    break;
9181
0
#ifdef BFD64
9182
0
  case 8:
9183
0
    bfd_put_64 (input_bfd, x, location);
9184
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9185
0
    x >>= 32;
9186
0
    x >>= 32;
9187
0
    break;
9188
0
#endif
9189
0
  default:
9190
0
    abort ();
9191
0
    break;
9192
0
  }
9193
0
    }
9194
0
}
9195
9196
static bfd_vma
9197
get_value (bfd_vma size,
9198
     unsigned long chunksz,
9199
     bfd *input_bfd,
9200
     bfd_byte *location)
9201
0
{
9202
0
  int shift;
9203
0
  bfd_vma x = 0;
9204
9205
  /* Sanity checks.  */
9206
0
  BFD_ASSERT (chunksz <= sizeof (x)
9207
0
        && size >= chunksz
9208
0
        && chunksz != 0
9209
0
        && (size % chunksz) == 0
9210
0
        && input_bfd != NULL
9211
0
        && location != NULL);
9212
9213
0
  if (chunksz == sizeof (x))
9214
0
    {
9215
0
      BFD_ASSERT (size == chunksz);
9216
9217
      /* Make sure that we do not perform an undefined shift operation.
9218
   We know that size == chunksz so there will only be one iteration
9219
   of the loop below.  */
9220
0
      shift = 0;
9221
0
    }
9222
0
  else
9223
0
    shift = 8 * chunksz;
9224
9225
0
  for (; size; size -= chunksz, location += chunksz)
9226
0
    {
9227
0
      switch (chunksz)
9228
0
  {
9229
0
  case 1:
9230
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9231
0
    break;
9232
0
  case 2:
9233
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9234
0
    break;
9235
0
  case 4:
9236
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9237
0
    break;
9238
0
#ifdef BFD64
9239
0
  case 8:
9240
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9241
0
    break;
9242
0
#endif
9243
0
  default:
9244
0
    abort ();
9245
0
  }
9246
0
    }
9247
0
  return x;
9248
0
}
9249
9250
static void
9251
decode_complex_addend (unsigned long *start,   /* in bits */
9252
           unsigned long *oplen,   /* in bits */
9253
           unsigned long *len,     /* in bits */
9254
           unsigned long *wordsz,  /* in bytes */
9255
           unsigned long *chunksz, /* in bytes */
9256
           unsigned long *lsb0_p,
9257
           unsigned long *signed_p,
9258
           unsigned long *trunc_p,
9259
           unsigned long encoded)
9260
0
{
9261
0
  * start     =  encoded  & 0x3F;
9262
0
  * len       = (encoded >>  6) & 0x3F;
9263
0
  * oplen     = (encoded >> 12) & 0x3F;
9264
0
  * wordsz    = (encoded >> 18) & 0xF;
9265
0
  * chunksz   = (encoded >> 22) & 0xF;
9266
0
  * lsb0_p    = (encoded >> 27) & 1;
9267
0
  * signed_p  = (encoded >> 28) & 1;
9268
0
  * trunc_p   = (encoded >> 29) & 1;
9269
0
}
9270
9271
bfd_reloc_status_type
9272
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9273
            asection *input_section,
9274
            bfd_byte *contents,
9275
            Elf_Internal_Rela *rel,
9276
            bfd_vma relocation)
9277
0
{
9278
0
  bfd_vma shift, x, mask;
9279
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9280
0
  bfd_reloc_status_type r;
9281
0
  bfd_size_type octets;
9282
9283
  /*  Perform this reloc, since it is complex.
9284
      (this is not to say that it necessarily refers to a complex
9285
      symbol; merely that it is a self-describing CGEN based reloc.
9286
      i.e. the addend has the complete reloc information (bit start, end,
9287
      word size, etc) encoded within it.).  */
9288
9289
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9290
0
       &chunksz, &lsb0_p, &signed_p,
9291
0
       &trunc_p, rel->r_addend);
9292
9293
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9294
9295
0
  if (lsb0_p)
9296
0
    shift = (start + 1) - len;
9297
0
  else
9298
0
    shift = (8 * wordsz) - (start + len);
9299
9300
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9301
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9302
9303
#ifdef DEBUG
9304
  printf ("Doing complex reloc: "
9305
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9306
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9307
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9308
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9309
    oplen, (unsigned long) x, (unsigned long) mask,
9310
    (unsigned long) relocation);
9311
#endif
9312
9313
0
  r = bfd_reloc_ok;
9314
0
  if (! trunc_p)
9315
    /* Now do an overflow check.  */
9316
0
    r = bfd_check_overflow ((signed_p
9317
0
           ? complain_overflow_signed
9318
0
           : complain_overflow_unsigned),
9319
0
          len, 0, (8 * wordsz),
9320
0
          relocation);
9321
9322
  /* Do the deed.  */
9323
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9324
9325
#ifdef DEBUG
9326
  printf ("           relocation: %8.8lx\n"
9327
    "         shifted mask: %8.8lx\n"
9328
    " shifted/masked reloc: %8.8lx\n"
9329
    "               result: %8.8lx\n",
9330
    (unsigned long) relocation, (unsigned long) (mask << shift),
9331
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9332
#endif
9333
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9334
0
  return r;
9335
0
}
9336
9337
/* Functions to read r_offset from external (target order) reloc
9338
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9339
   know the value is aligned.  */
9340
9341
static bfd_vma
9342
ext32l_r_offset (const void *p)
9343
0
{
9344
0
  union aligned32
9345
0
  {
9346
0
    uint32_t v;
9347
0
    unsigned char c[4];
9348
0
  };
9349
0
  const union aligned32 *a
9350
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9351
9352
0
  uint32_t aval = (  (uint32_t) a->c[0]
9353
0
       | (uint32_t) a->c[1] << 8
9354
0
       | (uint32_t) a->c[2] << 16
9355
0
       | (uint32_t) a->c[3] << 24);
9356
0
  return aval;
9357
0
}
9358
9359
static bfd_vma
9360
ext32b_r_offset (const void *p)
9361
0
{
9362
0
  union aligned32
9363
0
  {
9364
0
    uint32_t v;
9365
0
    unsigned char c[4];
9366
0
  };
9367
0
  const union aligned32 *a
9368
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9369
9370
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9371
0
       | (uint32_t) a->c[1] << 16
9372
0
       | (uint32_t) a->c[2] << 8
9373
0
       | (uint32_t) a->c[3]);
9374
0
  return aval;
9375
0
}
9376
9377
static bfd_vma
9378
ext64l_r_offset (const void *p)
9379
0
{
9380
0
  union aligned64
9381
0
  {
9382
0
    uint64_t v;
9383
0
    unsigned char c[8];
9384
0
  };
9385
0
  const union aligned64 *a
9386
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9387
9388
0
  uint64_t aval = (  (uint64_t) a->c[0]
9389
0
       | (uint64_t) a->c[1] << 8
9390
0
       | (uint64_t) a->c[2] << 16
9391
0
       | (uint64_t) a->c[3] << 24
9392
0
       | (uint64_t) a->c[4] << 32
9393
0
       | (uint64_t) a->c[5] << 40
9394
0
       | (uint64_t) a->c[6] << 48
9395
0
       | (uint64_t) a->c[7] << 56);
9396
0
  return aval;
9397
0
}
9398
9399
static bfd_vma
9400
ext64b_r_offset (const void *p)
9401
0
{
9402
0
  union aligned64
9403
0
  {
9404
0
    uint64_t v;
9405
0
    unsigned char c[8];
9406
0
  };
9407
0
  const union aligned64 *a
9408
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9409
9410
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9411
0
       | (uint64_t) a->c[1] << 48
9412
0
       | (uint64_t) a->c[2] << 40
9413
0
       | (uint64_t) a->c[3] << 32
9414
0
       | (uint64_t) a->c[4] << 24
9415
0
       | (uint64_t) a->c[5] << 16
9416
0
       | (uint64_t) a->c[6] << 8
9417
0
       | (uint64_t) a->c[7]);
9418
0
  return aval;
9419
0
}
9420
9421
/* When performing a relocatable link, the input relocations are
9422
   preserved.  But, if they reference global symbols, the indices
9423
   referenced must be updated.  Update all the relocations found in
9424
   RELDATA.  */
9425
9426
static bool
9427
elf_link_adjust_relocs (bfd *abfd,
9428
      asection *sec,
9429
      struct bfd_elf_section_reloc_data *reldata,
9430
      bool sort,
9431
      struct bfd_link_info *info)
9432
0
{
9433
0
  unsigned int i;
9434
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9435
0
  bfd_byte *erela;
9436
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9437
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9438
0
  bfd_vma r_type_mask;
9439
0
  int r_sym_shift;
9440
0
  unsigned int count = reldata->count;
9441
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9442
9443
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9444
0
    {
9445
0
      swap_in = bed->s->swap_reloc_in;
9446
0
      swap_out = bed->s->swap_reloc_out;
9447
0
    }
9448
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9449
0
    {
9450
0
      swap_in = bed->s->swap_reloca_in;
9451
0
      swap_out = bed->s->swap_reloca_out;
9452
0
    }
9453
0
  else
9454
0
    abort ();
9455
9456
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9457
0
    abort ();
9458
9459
0
  if (bed->s->arch_size == 32)
9460
0
    {
9461
0
      r_type_mask = 0xff;
9462
0
      r_sym_shift = 8;
9463
0
    }
9464
0
  else
9465
0
    {
9466
0
      r_type_mask = 0xffffffff;
9467
0
      r_sym_shift = 32;
9468
0
    }
9469
9470
0
  erela = reldata->hdr->contents;
9471
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9472
0
    {
9473
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9474
0
      unsigned int j;
9475
9476
0
      if (*rel_hash == NULL)
9477
0
  continue;
9478
9479
0
      if ((*rel_hash)->indx == -2
9480
0
    && info->gc_sections
9481
0
    && ! info->gc_keep_exported)
9482
0
  {
9483
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9484
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9485
0
            abfd, sec,
9486
0
            (*rel_hash)->root.root.string);
9487
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9488
0
            abfd, sec);
9489
0
    bfd_set_error (bfd_error_invalid_operation);
9490
0
    return false;
9491
0
  }
9492
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9493
9494
0
      (*swap_in) (abfd, erela, irela);
9495
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9496
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9497
0
         | (irela[j].r_info & r_type_mask));
9498
0
      (*swap_out) (abfd, irela, erela);
9499
0
    }
9500
9501
0
  if (bed->elf_backend_update_relocs)
9502
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9503
9504
0
  if (sort && count != 0)
9505
0
    {
9506
0
      bfd_vma (*ext_r_off) (const void *);
9507
0
      bfd_vma r_off;
9508
0
      size_t elt_size;
9509
0
      bfd_byte *base, *end, *p, *loc;
9510
0
      bfd_byte *buf = NULL;
9511
9512
0
      if (bed->s->arch_size == 32)
9513
0
  {
9514
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9515
0
      ext_r_off = ext32l_r_offset;
9516
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9517
0
      ext_r_off = ext32b_r_offset;
9518
0
    else
9519
0
      abort ();
9520
0
  }
9521
0
      else
9522
0
  {
9523
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9524
0
      ext_r_off = ext64l_r_offset;
9525
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9526
0
      ext_r_off = ext64b_r_offset;
9527
0
    else
9528
0
      abort ();
9529
0
  }
9530
9531
      /*  Must use a stable sort here.  A modified insertion sort,
9532
    since the relocs are mostly sorted already.  */
9533
0
      elt_size = reldata->hdr->sh_entsize;
9534
0
      base = reldata->hdr->contents;
9535
0
      end = base + count * elt_size;
9536
0
      if (elt_size > sizeof (Elf64_External_Rela))
9537
0
  abort ();
9538
9539
      /* Ensure the first element is lowest.  This acts as a sentinel,
9540
   speeding the main loop below.  */
9541
0
      r_off = (*ext_r_off) (base);
9542
0
      for (p = loc = base; (p += elt_size) < end; )
9543
0
  {
9544
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9545
0
    if (r_off > r_off2)
9546
0
      {
9547
0
        r_off = r_off2;
9548
0
        loc = p;
9549
0
      }
9550
0
  }
9551
0
      if (loc != base)
9552
0
  {
9553
    /* Don't just swap *base and *loc as that changes the order
9554
       of the original base[0] and base[1] if they happen to
9555
       have the same r_offset.  */
9556
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9557
0
    memcpy (onebuf, loc, elt_size);
9558
0
    memmove (base + elt_size, base, loc - base);
9559
0
    memcpy (base, onebuf, elt_size);
9560
0
  }
9561
9562
0
      for (p = base + elt_size; (p += elt_size) < end; )
9563
0
  {
9564
    /* base to p is sorted, *p is next to insert.  */
9565
0
    r_off = (*ext_r_off) (p);
9566
    /* Search the sorted region for location to insert.  */
9567
0
    loc = p - elt_size;
9568
0
    while (r_off < (*ext_r_off) (loc))
9569
0
      loc -= elt_size;
9570
0
    loc += elt_size;
9571
0
    if (loc != p)
9572
0
      {
9573
        /* Chances are there is a run of relocs to insert here,
9574
     from one of more input files.  Files are not always
9575
     linked in order due to the way elf_link_input_bfd is
9576
     called.  See pr17666.  */
9577
0
        size_t sortlen = p - loc;
9578
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9579
0
        size_t runlen = elt_size;
9580
0
        bfd_vma r_off_runend = r_off;
9581
0
        bfd_vma r_off_runend_next;
9582
0
        size_t buf_size = 96 * 1024;
9583
0
        while (p + runlen < end
9584
0
         && (sortlen <= buf_size
9585
0
       || runlen + elt_size <= buf_size)
9586
         /* run must not break the ordering of base..loc+1 */
9587
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9588
         /* run must be already sorted */
9589
0
         && r_off_runend_next >= r_off_runend)
9590
0
    {
9591
0
      runlen += elt_size;
9592
0
      r_off_runend = r_off_runend_next;
9593
0
    }
9594
0
        if (buf == NULL)
9595
0
    {
9596
0
      buf = bfd_malloc (buf_size);
9597
0
      if (buf == NULL)
9598
0
        return false;
9599
0
    }
9600
0
        if (runlen < sortlen)
9601
0
    {
9602
0
      memcpy (buf, p, runlen);
9603
0
      memmove (loc + runlen, loc, sortlen);
9604
0
      memcpy (loc, buf, runlen);
9605
0
    }
9606
0
        else
9607
0
    {
9608
0
      memcpy (buf, loc, sortlen);
9609
0
      memmove (loc, p, runlen);
9610
0
      memcpy (loc + runlen, buf, sortlen);
9611
0
    }
9612
0
        p += runlen - elt_size;
9613
0
      }
9614
0
  }
9615
      /* Hashes are no longer valid.  */
9616
0
      free (reldata->hashes);
9617
0
      reldata->hashes = NULL;
9618
0
      free (buf);
9619
0
    }
9620
0
  return true;
9621
0
}
9622
9623
struct elf_link_sort_rela
9624
{
9625
  union {
9626
    bfd_vma offset;
9627
    bfd_vma sym_mask;
9628
  } u;
9629
  enum elf_reloc_type_class type;
9630
  /* We use this as an array of size int_rels_per_ext_rel.  */
9631
  Elf_Internal_Rela rela[1];
9632
};
9633
9634
/* qsort stability here and for cmp2 is only an issue if multiple
9635
   dynamic relocations are emitted at the same address.  But targets
9636
   that apply a series of dynamic relocations each operating on the
9637
   result of the prior relocation can't use -z combreloc as
9638
   implemented anyway.  Such schemes tend to be broken by sorting on
9639
   symbol index.  That leaves dynamic NONE relocs as the only other
9640
   case where ld might emit multiple relocs at the same address, and
9641
   those are only emitted due to target bugs.  */
9642
9643
static int
9644
elf_link_sort_cmp1 (const void *A, const void *B)
9645
0
{
9646
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9647
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9648
0
  int relativea, relativeb;
9649
9650
0
  relativea = a->type == reloc_class_relative;
9651
0
  relativeb = b->type == reloc_class_relative;
9652
9653
0
  if (relativea < relativeb)
9654
0
    return 1;
9655
0
  if (relativea > relativeb)
9656
0
    return -1;
9657
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9658
0
    return -1;
9659
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9660
0
    return 1;
9661
0
  if (a->rela->r_offset < b->rela->r_offset)
9662
0
    return -1;
9663
0
  if (a->rela->r_offset > b->rela->r_offset)
9664
0
    return 1;
9665
0
  return 0;
9666
0
}
9667
9668
static int
9669
elf_link_sort_cmp2 (const void *A, const void *B)
9670
0
{
9671
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9672
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9673
9674
0
  if (a->type < b->type)
9675
0
    return -1;
9676
0
  if (a->type > b->type)
9677
0
    return 1;
9678
0
  if (a->u.offset < b->u.offset)
9679
0
    return -1;
9680
0
  if (a->u.offset > b->u.offset)
9681
0
    return 1;
9682
0
  if (a->rela->r_offset < b->rela->r_offset)
9683
0
    return -1;
9684
0
  if (a->rela->r_offset > b->rela->r_offset)
9685
0
    return 1;
9686
0
  return 0;
9687
0
}
9688
9689
static size_t
9690
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9691
0
{
9692
0
  asection *dynamic_relocs;
9693
0
  asection *rela_dyn;
9694
0
  asection *rel_dyn;
9695
0
  bfd_size_type count, size;
9696
0
  size_t i, ret, sort_elt, ext_size;
9697
0
  bfd_byte *sort, *s_non_relative, *p;
9698
0
  struct elf_link_sort_rela *sq;
9699
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9700
0
  int i2e = bed->s->int_rels_per_ext_rel;
9701
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9702
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9703
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9704
0
  struct bfd_link_order *lo;
9705
0
  bfd_vma r_sym_mask;
9706
0
  bool use_rela;
9707
9708
  /* Find a dynamic reloc section.  */
9709
0
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9710
0
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
9711
0
  if (rela_dyn != NULL && rela_dyn->size > 0
9712
0
      && rel_dyn != NULL && rel_dyn->size > 0)
9713
0
    {
9714
0
      bool use_rela_initialised = false;
9715
9716
      /* This is just here to stop gcc from complaining.
9717
   Its initialization checking code is not perfect.  */
9718
0
      use_rela = true;
9719
9720
      /* Both sections are present.  Examine the sizes
9721
   of the indirect sections to help us choose.  */
9722
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9723
0
  if (lo->type == bfd_indirect_link_order)
9724
0
    {
9725
0
      asection *o = lo->u.indirect.section;
9726
9727
0
      if ((o->size % bed->s->sizeof_rela) == 0)
9728
0
        {
9729
0
    if ((o->size % bed->s->sizeof_rel) == 0)
9730
      /* Section size is divisible by both rel and rela sizes.
9731
         It is of no help to us.  */
9732
0
      ;
9733
0
    else
9734
0
      {
9735
        /* Section size is only divisible by rela.  */
9736
0
        if (use_rela_initialised && !use_rela)
9737
0
          {
9738
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
9739
0
                "they are in more than one size"),
9740
0
              abfd);
9741
0
      bfd_set_error (bfd_error_invalid_operation);
9742
0
      return 0;
9743
0
          }
9744
0
        else
9745
0
          {
9746
0
      use_rela = true;
9747
0
      use_rela_initialised = true;
9748
0
          }
9749
0
      }
9750
0
        }
9751
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
9752
0
        {
9753
    /* Section size is only divisible by rel.  */
9754
0
    if (use_rela_initialised && use_rela)
9755
0
      {
9756
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
9757
0
            "they are in more than one size"),
9758
0
          abfd);
9759
0
        bfd_set_error (bfd_error_invalid_operation);
9760
0
        return 0;
9761
0
      }
9762
0
    else
9763
0
      {
9764
0
        use_rela = false;
9765
0
        use_rela_initialised = true;
9766
0
      }
9767
0
        }
9768
0
      else
9769
0
        {
9770
    /* The section size is not divisible by either -
9771
       something is wrong.  */
9772
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
9773
0
              "they are of an unknown size"), abfd);
9774
0
    bfd_set_error (bfd_error_invalid_operation);
9775
0
    return 0;
9776
0
        }
9777
0
    }
9778
9779
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9780
0
  if (lo->type == bfd_indirect_link_order)
9781
0
    {
9782
0
      asection *o = lo->u.indirect.section;
9783
9784
0
      if ((o->size % bed->s->sizeof_rela) == 0)
9785
0
        {
9786
0
    if ((o->size % bed->s->sizeof_rel) == 0)
9787
      /* Section size is divisible by both rel and rela sizes.
9788
         It is of no help to us.  */
9789
0
      ;
9790
0
    else
9791
0
      {
9792
        /* Section size is only divisible by rela.  */
9793
0
        if (use_rela_initialised && !use_rela)
9794
0
          {
9795
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
9796
0
                "they are in more than one size"),
9797
0
              abfd);
9798
0
      bfd_set_error (bfd_error_invalid_operation);
9799
0
      return 0;
9800
0
          }
9801
0
        else
9802
0
          {
9803
0
      use_rela = true;
9804
0
      use_rela_initialised = true;
9805
0
          }
9806
0
      }
9807
0
        }
9808
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
9809
0
        {
9810
    /* Section size is only divisible by rel.  */
9811
0
    if (use_rela_initialised && use_rela)
9812
0
      {
9813
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
9814
0
            "they are in more than one size"),
9815
0
          abfd);
9816
0
        bfd_set_error (bfd_error_invalid_operation);
9817
0
        return 0;
9818
0
      }
9819
0
    else
9820
0
      {
9821
0
        use_rela = false;
9822
0
        use_rela_initialised = true;
9823
0
      }
9824
0
        }
9825
0
      else
9826
0
        {
9827
    /* The section size is not divisible by either -
9828
       something is wrong.  */
9829
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
9830
0
              "they are of an unknown size"), abfd);
9831
0
    bfd_set_error (bfd_error_invalid_operation);
9832
0
    return 0;
9833
0
        }
9834
0
    }
9835
9836
0
      if (! use_rela_initialised)
9837
  /* Make a guess.  */
9838
0
  use_rela = true;
9839
0
    }
9840
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
9841
0
    use_rela = true;
9842
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
9843
0
    use_rela = false;
9844
0
  else
9845
0
    return 0;
9846
9847
0
  if (use_rela)
9848
0
    {
9849
0
      dynamic_relocs = rela_dyn;
9850
0
      ext_size = bed->s->sizeof_rela;
9851
0
      swap_in = bed->s->swap_reloca_in;
9852
0
      swap_out = bed->s->swap_reloca_out;
9853
0
    }
9854
0
  else
9855
0
    {
9856
0
      dynamic_relocs = rel_dyn;
9857
0
      ext_size = bed->s->sizeof_rel;
9858
0
      swap_in = bed->s->swap_reloc_in;
9859
0
      swap_out = bed->s->swap_reloc_out;
9860
0
    }
9861
9862
0
  size = 0;
9863
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9864
0
    if (lo->type == bfd_indirect_link_order)
9865
0
      size += lo->u.indirect.section->size;
9866
9867
0
  if (size != dynamic_relocs->size)
9868
0
    return 0;
9869
9870
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
9871
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
9872
9873
0
  count = dynamic_relocs->size / ext_size;
9874
0
  if (count == 0)
9875
0
    return 0;
9876
0
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9877
9878
0
  if (sort == NULL)
9879
0
    {
9880
0
      (*info->callbacks->warning)
9881
0
  (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9882
0
      return 0;
9883
0
    }
9884
9885
0
  if (bed->s->arch_size == 32)
9886
0
    r_sym_mask = ~(bfd_vma) 0xff;
9887
0
  else
9888
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
9889
9890
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9891
0
    if (lo->type == bfd_indirect_link_order)
9892
0
      {
9893
0
  bfd_byte *erel, *erelend;
9894
0
  asection *o = lo->u.indirect.section;
9895
9896
0
  if (o->contents == NULL && o->size != 0)
9897
0
    {
9898
      /* This is a reloc section that is being handled as a normal
9899
         section.  See bfd_section_from_shdr.  We can't combine
9900
         relocs in this case.  */
9901
0
      free (sort);
9902
0
      return 0;
9903
0
    }
9904
0
  erel = o->contents;
9905
0
  erelend = o->contents + o->size;
9906
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
9907
9908
0
  while (erel < erelend)
9909
0
    {
9910
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9911
9912
0
      (*swap_in) (abfd, erel, s->rela);
9913
0
      s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9914
0
      s->u.sym_mask = r_sym_mask;
9915
0
      p += sort_elt;
9916
0
      erel += ext_size;
9917
0
    }
9918
0
      }
9919
9920
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9921
9922
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
9923
0
    {
9924
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9925
0
      if (s->type != reloc_class_relative)
9926
0
  break;
9927
0
    }
9928
0
  ret = i;
9929
0
  s_non_relative = p;
9930
9931
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
9932
0
  for (; i < count; i++, p += sort_elt)
9933
0
    {
9934
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9935
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9936
0
  sq = sp;
9937
0
      sp->u.offset = sq->rela->r_offset;
9938
0
    }
9939
9940
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9941
9942
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
9943
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9944
0
    {
9945
      /* We have plt relocs in .rela.dyn.  */
9946
0
      sq = (struct elf_link_sort_rela *) sort;
9947
0
      for (i = 0; i < count; i++)
9948
0
  if (sq[count - i - 1].type != reloc_class_plt)
9949
0
    break;
9950
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
9951
0
  {
9952
0
    struct bfd_link_order **plo;
9953
    /* Put srelplt link_order last.  This is so the output_offset
9954
       set in the next loop is correct for DT_JMPREL.  */
9955
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9956
0
      if ((*plo)->type == bfd_indirect_link_order
9957
0
    && (*plo)->u.indirect.section == htab->srelplt)
9958
0
        {
9959
0
    lo = *plo;
9960
0
    *plo = lo->next;
9961
0
        }
9962
0
      else
9963
0
        plo = &(*plo)->next;
9964
0
    *plo = lo;
9965
0
    lo->next = NULL;
9966
0
    dynamic_relocs->map_tail.link_order = lo;
9967
0
  }
9968
0
    }
9969
9970
0
  p = sort;
9971
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9972
0
    if (lo->type == bfd_indirect_link_order)
9973
0
      {
9974
0
  bfd_byte *erel, *erelend;
9975
0
  asection *o = lo->u.indirect.section;
9976
9977
0
  erel = o->contents;
9978
0
  erelend = o->contents + o->size;
9979
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9980
0
  while (erel < erelend)
9981
0
    {
9982
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9983
0
      (*swap_out) (abfd, s->rela, erel);
9984
0
      p += sort_elt;
9985
0
      erel += ext_size;
9986
0
    }
9987
0
      }
9988
9989
0
  free (sort);
9990
0
  *psec = dynamic_relocs;
9991
0
  return ret;
9992
0
}
9993
9994
/* Add a symbol to the output symbol string table.  */
9995
9996
static int
9997
elf_link_output_symstrtab (void *finf,
9998
         const char *name,
9999
         Elf_Internal_Sym *elfsym,
10000
         asection *input_sec,
10001
         struct elf_link_hash_entry *h)
10002
0
{
10003
0
  struct elf_final_link_info *flinfo = finf;
10004
0
  int (*output_symbol_hook)
10005
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10006
0
     struct elf_link_hash_entry *);
10007
0
  struct elf_link_hash_table *hash_table;
10008
0
  const struct elf_backend_data *bed;
10009
0
  bfd_size_type strtabsize;
10010
10011
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10012
10013
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10014
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10015
0
  if (output_symbol_hook != NULL)
10016
0
    {
10017
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10018
0
      if (ret != 1)
10019
0
  return ret;
10020
0
    }
10021
10022
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10023
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10024
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10025
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10026
10027
0
  if (name == NULL || *name == '\0')
10028
0
    elfsym->st_name = (unsigned long) -1;
10029
0
  else
10030
0
    {
10031
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10032
   to get the final offset for st_name.  */
10033
0
      char *versioned_name = (char *) name;
10034
0
      if (h != NULL)
10035
0
  {
10036
0
    if (h->versioned == versioned && h->def_dynamic)
10037
0
      {
10038
        /* Keep only one '@' for versioned symbols defined in
10039
           shared objects.  */
10040
0
        char *version = strrchr (name, ELF_VER_CHR);
10041
0
        char *base_end = strchr (name, ELF_VER_CHR);
10042
0
        if (version != base_end)
10043
0
    {
10044
0
      size_t base_len;
10045
0
      size_t len = strlen (name);
10046
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10047
0
      if (versioned_name == NULL)
10048
0
        return 0;
10049
0
      base_len = base_end - name;
10050
0
      memcpy (versioned_name, name, base_len);
10051
0
      memcpy (versioned_name + base_len, version,
10052
0
        len - base_len);
10053
0
    }
10054
0
      }
10055
0
  }
10056
0
      else if (flinfo->info->unique_symbol
10057
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10058
0
  {
10059
0
    struct local_hash_entry *lh;
10060
0
    size_t count_len;
10061
0
    size_t base_len;
10062
0
    char buf[30];
10063
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10064
0
      {
10065
0
      case STT_FILE:
10066
0
      case STT_SECTION:
10067
0
        break;
10068
0
      default:
10069
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10070
0
         (&flinfo->local_hash_table, name, true, false);
10071
0
        if (lh == NULL)
10072
0
    return 0;
10073
        /* Always append ".COUNT" to local symbols to avoid
10074
     potential conflicts with local symbol "XXX.COUNT".  */
10075
0
        sprintf (buf, "%lx", lh->count);
10076
0
        base_len = lh->size;
10077
0
        if (!base_len)
10078
0
    {
10079
0
      base_len = strlen (name);
10080
0
      lh->size = base_len;
10081
0
    }
10082
0
        count_len = strlen (buf);
10083
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10084
0
            base_len + count_len + 2);
10085
0
        if (versioned_name == NULL)
10086
0
    return 0;
10087
0
        memcpy (versioned_name, name, base_len);
10088
0
        versioned_name[base_len] = '.';
10089
0
        memcpy (versioned_name + base_len + 1, buf,
10090
0
          count_len + 1);
10091
0
        lh->count++;
10092
0
        break;
10093
0
      }
10094
0
  }
10095
0
      elfsym->st_name
10096
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10097
0
                 versioned_name, false);
10098
0
      if (elfsym->st_name == (unsigned long) -1)
10099
0
  return 0;
10100
0
    }
10101
10102
0
  hash_table = elf_hash_table (flinfo->info);
10103
0
  strtabsize = hash_table->strtabsize;
10104
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10105
0
    {
10106
0
      strtabsize += strtabsize;
10107
0
      hash_table->strtabsize = strtabsize;
10108
0
      strtabsize *= sizeof (*hash_table->strtab);
10109
0
      hash_table->strtab
10110
0
  = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10111
0
             strtabsize);
10112
0
      if (hash_table->strtab == NULL)
10113
0
  return 0;
10114
0
    }
10115
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10116
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10117
0
    = flinfo->output_bfd->symcount;
10118
0
  flinfo->output_bfd->symcount += 1;
10119
10120
0
  return 1;
10121
0
}
10122
10123
/* Swap symbols out to the symbol table and flush the output symbols to
10124
   the file.  */
10125
10126
static bool
10127
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10128
0
{
10129
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10130
0
  size_t amt;
10131
0
  size_t i;
10132
0
  const struct elf_backend_data *bed;
10133
0
  bfd_byte *symbuf;
10134
0
  Elf_Internal_Shdr *hdr;
10135
0
  file_ptr pos;
10136
0
  bool ret;
10137
10138
0
  if (flinfo->output_bfd->symcount == 0)
10139
0
    return true;
10140
10141
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10142
10143
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10144
10145
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10146
0
  symbuf = (bfd_byte *) bfd_malloc (amt);
10147
0
  if (symbuf == NULL)
10148
0
    return false;
10149
10150
0
  if (flinfo->symshndxbuf)
10151
0
    {
10152
0
      amt = sizeof (Elf_External_Sym_Shndx);
10153
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10154
0
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10155
0
      if (flinfo->symshndxbuf == NULL)
10156
0
  {
10157
0
    free (symbuf);
10158
0
    return false;
10159
0
  }
10160
0
    }
10161
10162
  /* Now swap out the symbols.  */
10163
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10164
0
    {
10165
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10166
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10167
0
  elfsym->sym.st_name = 0;
10168
0
      else
10169
0
  elfsym->sym.st_name
10170
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10171
0
                elfsym->sym.st_name);
10172
10173
      /* Inform the linker of the addition of this symbol.  */
10174
10175
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10176
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10177
0
             &elfsym->sym);
10178
10179
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10180
0
             ((bfd_byte *) symbuf
10181
0
        + (elfsym->dest_index
10182
0
           * bed->s->sizeof_sym)),
10183
0
             NPTR_ADD (flinfo->symshndxbuf,
10184
0
           elfsym->dest_index));
10185
0
    }
10186
10187
0
  hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10188
0
  pos = hdr->sh_offset + hdr->sh_size;
10189
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10190
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10191
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10192
0
    {
10193
0
      hdr->sh_size += amt;
10194
0
      ret = true;
10195
0
    }
10196
0
  else
10197
0
    ret = false;
10198
10199
0
  free (symbuf);
10200
10201
0
  free (hash_table->strtab);
10202
0
  hash_table->strtab = NULL;
10203
10204
0
  return ret;
10205
0
}
10206
10207
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
10208
10209
static bool
10210
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10211
0
{
10212
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10213
0
      && sym->st_shndx < SHN_LORESERVE)
10214
0
    {
10215
      /* The gABI doesn't support dynamic symbols in output sections
10216
   beyond 64k.  */
10217
0
      _bfd_error_handler
10218
  /* xgettext:c-format */
10219
0
  (_("%pB: too many sections: %d (>= %d)"),
10220
0
   abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10221
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10222
0
      return false;
10223
0
    }
10224
0
  return true;
10225
0
}
10226
10227
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10228
   allowing an unsatisfied unversioned symbol in the DSO to match a
10229
   versioned symbol that would normally require an explicit version.
10230
   We also handle the case that a DSO references a hidden symbol
10231
   which may be satisfied by a versioned symbol in another DSO.  */
10232
10233
static bool
10234
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10235
         const struct elf_backend_data *bed,
10236
         struct elf_link_hash_entry *h)
10237
0
{
10238
0
  bfd *abfd;
10239
0
  struct elf_link_loaded_list *loaded;
10240
10241
0
  if (!is_elf_hash_table (info->hash))
10242
0
    return false;
10243
10244
  /* Check indirect symbol.  */
10245
0
  while (h->root.type == bfd_link_hash_indirect)
10246
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10247
10248
0
  switch (h->root.type)
10249
0
    {
10250
0
    default:
10251
0
      abfd = NULL;
10252
0
      break;
10253
10254
0
    case bfd_link_hash_undefined:
10255
0
    case bfd_link_hash_undefweak:
10256
0
      abfd = h->root.u.undef.abfd;
10257
0
      if (abfd == NULL
10258
0
    || (abfd->flags & DYNAMIC) == 0
10259
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10260
0
  return false;
10261
0
      break;
10262
10263
0
    case bfd_link_hash_defined:
10264
0
    case bfd_link_hash_defweak:
10265
0
      abfd = h->root.u.def.section->owner;
10266
0
      break;
10267
10268
0
    case bfd_link_hash_common:
10269
0
      abfd = h->root.u.c.p->section->owner;
10270
0
      break;
10271
0
    }
10272
0
  BFD_ASSERT (abfd != NULL);
10273
10274
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10275
0
       loaded != NULL;
10276
0
       loaded = loaded->next)
10277
0
    {
10278
0
      bfd *input;
10279
0
      Elf_Internal_Shdr *hdr;
10280
0
      size_t symcount;
10281
0
      size_t extsymcount;
10282
0
      size_t extsymoff;
10283
0
      Elf_Internal_Shdr *versymhdr;
10284
0
      Elf_Internal_Sym *isym;
10285
0
      Elf_Internal_Sym *isymend;
10286
0
      Elf_Internal_Sym *isymbuf;
10287
0
      Elf_External_Versym *ever;
10288
0
      Elf_External_Versym *extversym;
10289
10290
0
      input = loaded->abfd;
10291
10292
      /* We check each DSO for a possible hidden versioned definition.  */
10293
0
      if (input == abfd
10294
0
    || elf_dynversym (input) == 0)
10295
0
  continue;
10296
10297
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10298
10299
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10300
0
      if (elf_bad_symtab (input))
10301
0
  {
10302
0
    extsymcount = symcount;
10303
0
    extsymoff = 0;
10304
0
  }
10305
0
      else
10306
0
  {
10307
0
    extsymcount = symcount - hdr->sh_info;
10308
0
    extsymoff = hdr->sh_info;
10309
0
  }
10310
10311
0
      if (extsymcount == 0)
10312
0
  continue;
10313
10314
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10315
0
              NULL, NULL, NULL);
10316
0
      if (isymbuf == NULL)
10317
0
  return false;
10318
10319
      /* Read in any version definitions.  */
10320
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10321
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10322
0
    || (extversym = (Elf_External_Versym *)
10323
0
        _bfd_malloc_and_read (input, versymhdr->sh_size,
10324
0
            versymhdr->sh_size)) == NULL)
10325
0
  {
10326
0
    free (isymbuf);
10327
0
    return false;
10328
0
  }
10329
10330
0
      ever = extversym + extsymoff;
10331
0
      isymend = isymbuf + extsymcount;
10332
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10333
0
  {
10334
0
    const char *name;
10335
0
    Elf_Internal_Versym iver;
10336
0
    unsigned short version_index;
10337
10338
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10339
0
        || isym->st_shndx == SHN_UNDEF)
10340
0
      continue;
10341
10342
0
    name = bfd_elf_string_from_elf_section (input,
10343
0
              hdr->sh_link,
10344
0
              isym->st_name);
10345
0
    if (strcmp (name, h->root.root.string) != 0)
10346
0
      continue;
10347
10348
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10349
10350
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10351
0
        && !(h->def_regular
10352
0
       && h->forced_local))
10353
0
      {
10354
        /* If we have a non-hidden versioned sym, then it should
10355
     have provided a definition for the undefined sym unless
10356
     it is defined in a non-shared object and forced local.
10357
         */
10358
0
        abort ();
10359
0
      }
10360
10361
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10362
0
    if (version_index == 1 || version_index == 2)
10363
0
      {
10364
        /* This is the base or first version.  We can use it.  */
10365
0
        free (extversym);
10366
0
        free (isymbuf);
10367
0
        return true;
10368
0
      }
10369
0
  }
10370
10371
0
      free (extversym);
10372
0
      free (isymbuf);
10373
0
    }
10374
10375
0
  return false;
10376
0
}
10377
10378
/* Convert ELF common symbol TYPE.  */
10379
10380
static int
10381
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10382
0
{
10383
  /* Commom symbol can only appear in relocatable link.  */
10384
0
  if (!bfd_link_relocatable (info))
10385
0
    abort ();
10386
0
  switch (info->elf_stt_common)
10387
0
    {
10388
0
    case unchanged:
10389
0
      break;
10390
0
    case elf_stt_common:
10391
0
      type = STT_COMMON;
10392
0
      break;
10393
0
    case no_elf_stt_common:
10394
0
      type = STT_OBJECT;
10395
0
      break;
10396
0
    }
10397
0
  return type;
10398
0
}
10399
10400
/* Add an external symbol to the symbol table.  This is called from
10401
   the hash table traversal routine.  When generating a shared object,
10402
   we go through the symbol table twice.  The first time we output
10403
   anything that might have been forced to local scope in a version
10404
   script.  The second time we output the symbols that are still
10405
   global symbols.  */
10406
10407
static bool
10408
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10409
0
{
10410
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10411
0
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10412
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10413
0
  bool strip;
10414
0
  Elf_Internal_Sym sym;
10415
0
  asection *input_sec;
10416
0
  const struct elf_backend_data *bed;
10417
0
  long indx;
10418
0
  int ret;
10419
0
  unsigned int type;
10420
10421
0
  if (h->root.type == bfd_link_hash_warning)
10422
0
    {
10423
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10424
0
      if (h->root.type == bfd_link_hash_new)
10425
0
  return true;
10426
0
    }
10427
10428
  /* Decide whether to output this symbol in this pass.  */
10429
0
  if (eoinfo->localsyms)
10430
0
    {
10431
0
      if (!h->forced_local)
10432
0
  return true;
10433
0
    }
10434
0
  else
10435
0
    {
10436
0
      if (h->forced_local)
10437
0
  return true;
10438
0
    }
10439
10440
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10441
10442
0
  if (h->root.type == bfd_link_hash_undefined)
10443
0
    {
10444
      /* If we have an undefined symbol reference here then it must have
10445
   come from a shared library that is being linked in.  (Undefined
10446
   references in regular files have already been handled unless
10447
   they are in unreferenced sections which are removed by garbage
10448
   collection).  */
10449
0
      bool ignore_undef = false;
10450
10451
      /* Some symbols may be special in that the fact that they're
10452
   undefined can be safely ignored - let backend determine that.  */
10453
0
      if (bed->elf_backend_ignore_undef_symbol)
10454
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10455
10456
      /* If we are reporting errors for this situation then do so now.  */
10457
0
      if (!ignore_undef
10458
0
    && h->ref_dynamic_nonweak
10459
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10460
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10461
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10462
0
  {
10463
0
    flinfo->info->callbacks->undefined_symbol
10464
0
      (flinfo->info, h->root.root.string,
10465
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10466
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10467
0
       && !flinfo->info->warn_unresolved_syms);
10468
0
  }
10469
10470
      /* Strip a global symbol defined in a discarded section.  */
10471
0
      if (h->indx == -3)
10472
0
  return true;
10473
0
    }
10474
10475
  /* We should also warn if a forced local symbol is referenced from
10476
     shared libraries.  */
10477
0
  if (bfd_link_executable (flinfo->info)
10478
0
      && h->forced_local
10479
0
      && h->ref_dynamic
10480
0
      && h->def_regular
10481
0
      && !h->dynamic_def
10482
0
      && h->ref_dynamic_nonweak
10483
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10484
0
    {
10485
0
      bfd *def_bfd;
10486
0
      const char *msg;
10487
0
      struct elf_link_hash_entry *hi = h;
10488
10489
      /* Check indirect symbol.  */
10490
0
      while (hi->root.type == bfd_link_hash_indirect)
10491
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10492
10493
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10494
  /* xgettext:c-format */
10495
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10496
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10497
  /* xgettext:c-format */
10498
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10499
0
      else
10500
  /* xgettext:c-format */
10501
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10502
0
      def_bfd = flinfo->output_bfd;
10503
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10504
0
  def_bfd = hi->root.u.def.section->owner;
10505
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10506
0
        h->root.root.string, def_bfd);
10507
0
      bfd_set_error (bfd_error_bad_value);
10508
0
      eoinfo->failed = true;
10509
0
      return false;
10510
0
    }
10511
10512
  /* We don't want to output symbols that have never been mentioned by
10513
     a regular file, or that we have been told to strip.  However, if
10514
     h->indx is set to -2, the symbol is used by a reloc and we must
10515
     output it.  */
10516
0
  strip = false;
10517
0
  if (h->indx == -2)
10518
0
    ;
10519
0
  else if ((h->def_dynamic
10520
0
      || h->ref_dynamic
10521
0
      || h->root.type == bfd_link_hash_new)
10522
0
     && !h->def_regular
10523
0
     && !h->ref_regular)
10524
0
    strip = true;
10525
0
  else if (flinfo->info->strip == strip_all)
10526
0
    strip = true;
10527
0
  else if (flinfo->info->strip == strip_some
10528
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10529
0
             h->root.root.string, false, false) == NULL)
10530
0
    strip = true;
10531
0
  else if ((h->root.type == bfd_link_hash_defined
10532
0
      || h->root.type == bfd_link_hash_defweak)
10533
0
     && ((flinfo->info->strip_discarded
10534
0
    && discarded_section (h->root.u.def.section))
10535
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10536
0
       && h->root.u.def.section->owner != NULL
10537
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10538
0
    strip = true;
10539
0
  else if ((h->root.type == bfd_link_hash_undefined
10540
0
      || h->root.type == bfd_link_hash_undefweak)
10541
0
     && h->root.u.undef.abfd != NULL
10542
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10543
0
    strip = true;
10544
10545
0
  type = h->type;
10546
10547
  /* If we're stripping it, and it's not a dynamic symbol, there's
10548
     nothing else to do.   However, if it is a forced local symbol or
10549
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10550
     function a chance to make it dynamic.  */
10551
0
  if (strip
10552
0
      && h->dynindx == -1
10553
0
      && type != STT_GNU_IFUNC
10554
0
      && !h->forced_local)
10555
0
    return true;
10556
10557
0
  sym.st_value = 0;
10558
0
  sym.st_size = h->size;
10559
0
  sym.st_other = h->other;
10560
0
  switch (h->root.type)
10561
0
    {
10562
0
    default:
10563
0
    case bfd_link_hash_new:
10564
0
    case bfd_link_hash_warning:
10565
0
      abort ();
10566
0
      return false;
10567
10568
0
    case bfd_link_hash_undefined:
10569
0
    case bfd_link_hash_undefweak:
10570
0
      input_sec = bfd_und_section_ptr;
10571
0
      sym.st_shndx = SHN_UNDEF;
10572
0
      break;
10573
10574
0
    case bfd_link_hash_defined:
10575
0
    case bfd_link_hash_defweak:
10576
0
      {
10577
0
  input_sec = h->root.u.def.section;
10578
0
  if (input_sec->output_section != NULL)
10579
0
    {
10580
0
      sym.st_shndx =
10581
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10582
0
             input_sec->output_section);
10583
0
      if (sym.st_shndx == SHN_BAD)
10584
0
        {
10585
0
    _bfd_error_handler
10586
      /* xgettext:c-format */
10587
0
      (_("%pB: could not find output section %pA for input section %pA"),
10588
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10589
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10590
0
    eoinfo->failed = true;
10591
0
    return false;
10592
0
        }
10593
10594
      /* ELF symbols in relocatable files are section relative,
10595
         but in nonrelocatable files they are virtual
10596
         addresses.  */
10597
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10598
0
      if (!bfd_link_relocatable (flinfo->info))
10599
0
        {
10600
0
    sym.st_value += input_sec->output_section->vma;
10601
0
    if (h->type == STT_TLS)
10602
0
      {
10603
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10604
0
        if (tls_sec != NULL)
10605
0
          sym.st_value -= tls_sec->vma;
10606
0
      }
10607
0
        }
10608
0
    }
10609
0
  else
10610
0
    {
10611
0
      BFD_ASSERT (input_sec->owner == NULL
10612
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10613
0
      sym.st_shndx = SHN_UNDEF;
10614
0
      input_sec = bfd_und_section_ptr;
10615
0
    }
10616
0
      }
10617
0
      break;
10618
10619
0
    case bfd_link_hash_common:
10620
0
      input_sec = h->root.u.c.p->section;
10621
0
      sym.st_shndx = bed->common_section_index (input_sec);
10622
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10623
0
      break;
10624
10625
0
    case bfd_link_hash_indirect:
10626
      /* These symbols are created by symbol versioning.  They point
10627
   to the decorated version of the name.  For example, if the
10628
   symbol foo@@GNU_1.2 is the default, which should be used when
10629
   foo is used with no version, then we add an indirect symbol
10630
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10631
   since the indirected symbol is already in the hash table.  */
10632
0
      return true;
10633
0
    }
10634
10635
0
  if (type == STT_COMMON || type == STT_OBJECT)
10636
0
    switch (h->root.type)
10637
0
      {
10638
0
      case bfd_link_hash_common:
10639
0
  type = elf_link_convert_common_type (flinfo->info, type);
10640
0
  break;
10641
0
      case bfd_link_hash_defined:
10642
0
      case bfd_link_hash_defweak:
10643
0
  if (bed->common_definition (&sym))
10644
0
    type = elf_link_convert_common_type (flinfo->info, type);
10645
0
  else
10646
0
    type = STT_OBJECT;
10647
0
  break;
10648
0
      case bfd_link_hash_undefined:
10649
0
      case bfd_link_hash_undefweak:
10650
0
  break;
10651
0
      default:
10652
0
  abort ();
10653
0
      }
10654
10655
0
  if (h->forced_local)
10656
0
    {
10657
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10658
      /* Turn off visibility on local symbol.  */
10659
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10660
0
    }
10661
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
10662
0
  else if (h->unique_global && h->def_regular)
10663
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10664
0
  else if (h->root.type == bfd_link_hash_undefweak
10665
0
     || h->root.type == bfd_link_hash_defweak)
10666
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10667
0
  else
10668
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10669
0
  sym.st_target_internal = h->target_internal;
10670
10671
  /* Give the processor backend a chance to tweak the symbol value,
10672
     and also to finish up anything that needs to be done for this
10673
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
10674
     forced local syms when non-shared is due to a historical quirk.
10675
     STT_GNU_IFUNC symbol must go through PLT.  */
10676
0
  if ((h->type == STT_GNU_IFUNC
10677
0
       && h->def_regular
10678
0
       && !bfd_link_relocatable (flinfo->info))
10679
0
      || ((h->dynindx != -1
10680
0
     || h->forced_local)
10681
0
    && ((bfd_link_pic (flinfo->info)
10682
0
         && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10683
0
       || h->root.type != bfd_link_hash_undefweak))
10684
0
        || !h->forced_local)
10685
0
    && elf_hash_table (flinfo->info)->dynamic_sections_created))
10686
0
    {
10687
0
      if (! ((*bed->elf_backend_finish_dynamic_symbol)
10688
0
       (flinfo->output_bfd, flinfo->info, h, &sym)))
10689
0
  {
10690
0
    eoinfo->failed = true;
10691
0
    return false;
10692
0
  }
10693
0
    }
10694
10695
  /* If we are marking the symbol as undefined, and there are no
10696
     non-weak references to this symbol from a regular object, then
10697
     mark the symbol as weak undefined; if there are non-weak
10698
     references, mark the symbol as strong.  We can't do this earlier,
10699
     because it might not be marked as undefined until the
10700
     finish_dynamic_symbol routine gets through with it.  */
10701
0
  if (sym.st_shndx == SHN_UNDEF
10702
0
      && h->ref_regular
10703
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10704
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10705
0
    {
10706
0
      int bindtype;
10707
0
      type = ELF_ST_TYPE (sym.st_info);
10708
10709
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10710
0
      if (type == STT_GNU_IFUNC)
10711
0
  type = STT_FUNC;
10712
10713
0
      if (h->ref_regular_nonweak)
10714
0
  bindtype = STB_GLOBAL;
10715
0
      else
10716
0
  bindtype = STB_WEAK;
10717
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
10718
0
    }
10719
10720
  /* If this is a symbol defined in a dynamic library, don't use the
10721
     symbol size from the dynamic library.  Relinking an executable
10722
     against a new library may introduce gratuitous changes in the
10723
     executable's symbols if we keep the size.  */
10724
0
  if (sym.st_shndx == SHN_UNDEF
10725
0
      && !h->def_regular
10726
0
      && h->def_dynamic)
10727
0
    sym.st_size = 0;
10728
10729
  /* If a non-weak symbol with non-default visibility is not defined
10730
     locally, it is a fatal error.  */
10731
0
  if (!bfd_link_relocatable (flinfo->info)
10732
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10733
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
10734
0
      && h->root.type == bfd_link_hash_undefined
10735
0
      && !h->def_regular)
10736
0
    {
10737
0
      const char *msg;
10738
10739
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10740
  /* xgettext:c-format */
10741
0
  msg = _("%pB: protected symbol `%s' isn't defined");
10742
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10743
  /* xgettext:c-format */
10744
0
  msg = _("%pB: internal symbol `%s' isn't defined");
10745
0
      else
10746
  /* xgettext:c-format */
10747
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
10748
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10749
0
      bfd_set_error (bfd_error_bad_value);
10750
0
      eoinfo->failed = true;
10751
0
      return false;
10752
0
    }
10753
10754
  /* If this symbol should be put in the .dynsym section, then put it
10755
     there now.  We already know the symbol index.  We also fill in
10756
     the entry in the .hash section.  */
10757
0
  if (h->dynindx != -1
10758
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
10759
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
10760
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10761
0
    {
10762
0
      bfd_byte *esym;
10763
10764
      /* Since there is no version information in the dynamic string,
10765
   if there is no version info in symbol version section, we will
10766
   have a run-time problem if not linking executable, referenced
10767
   by shared library, or not bound locally.  */
10768
0
      if (h->verinfo.verdef == NULL
10769
0
    && (!bfd_link_executable (flinfo->info)
10770
0
        || h->ref_dynamic
10771
0
        || !h->def_regular))
10772
0
  {
10773
0
    char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10774
10775
0
    if (p && p [1] != '\0')
10776
0
      {
10777
0
        _bfd_error_handler
10778
    /* xgettext:c-format */
10779
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
10780
0
     flinfo->output_bfd, h->root.root.string);
10781
0
        eoinfo->failed = true;
10782
0
        return false;
10783
0
      }
10784
0
  }
10785
10786
0
      sym.st_name = h->dynstr_index;
10787
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
10788
0
        + h->dynindx * bed->s->sizeof_sym);
10789
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
10790
0
  {
10791
0
    eoinfo->failed = true;
10792
0
    return false;
10793
0
  }
10794
10795
      /* Inform the linker of the addition of this symbol.  */
10796
10797
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
10798
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10799
10800
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10801
10802
0
      if (flinfo->hash_sec != NULL)
10803
0
  {
10804
0
    size_t hash_entry_size;
10805
0
    bfd_byte *bucketpos;
10806
0
    bfd_vma chain;
10807
0
    size_t bucketcount;
10808
0
    size_t bucket;
10809
10810
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10811
0
    bucket = h->u.elf_hash_value % bucketcount;
10812
10813
0
    hash_entry_size
10814
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10815
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10816
0
           + (bucket + 2) * hash_entry_size);
10817
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10818
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10819
0
       bucketpos);
10820
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10821
0
       ((bfd_byte *) flinfo->hash_sec->contents
10822
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10823
0
  }
10824
10825
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10826
0
  {
10827
0
    Elf_Internal_Versym iversym;
10828
0
    Elf_External_Versym *eversym;
10829
10830
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10831
0
      {
10832
0
        if (h->verinfo.verdef == NULL
10833
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10834
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10835
0
    iversym.vs_vers = 1;
10836
0
        else
10837
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10838
0
      }
10839
0
    else
10840
0
      {
10841
0
        if (h->verinfo.vertree == NULL)
10842
0
    iversym.vs_vers = 1;
10843
0
        else
10844
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10845
0
        if (flinfo->info->create_default_symver)
10846
0
    iversym.vs_vers++;
10847
0
      }
10848
10849
    /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10850
       defined locally.  */
10851
0
    if (h->versioned == versioned_hidden && h->def_regular)
10852
0
      iversym.vs_vers |= VERSYM_HIDDEN;
10853
10854
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10855
0
    eversym += h->dynindx;
10856
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10857
0
  }
10858
0
    }
10859
10860
  /* If the symbol is undefined, and we didn't output it to .dynsym,
10861
     strip it from .symtab too.  Obviously we can't do this for
10862
     relocatable output or when needed for --emit-relocs.  */
10863
0
  else if (input_sec == bfd_und_section_ptr
10864
0
     && h->indx != -2
10865
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
10866
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10867
0
     && !bfd_link_relocatable (flinfo->info))
10868
0
    return true;
10869
10870
  /* Also strip others that we couldn't earlier due to dynamic symbol
10871
     processing.  */
10872
0
  if (strip)
10873
0
    return true;
10874
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
10875
0
    return true;
10876
10877
  /* Output a FILE symbol so that following locals are not associated
10878
     with the wrong input file.  We need one for forced local symbols
10879
     if we've seen more than one FILE symbol or when we have exactly
10880
     one FILE symbol but global symbols are present in a file other
10881
     than the one with the FILE symbol.  We also need one if linker
10882
     defined symbols are present.  In practice these conditions are
10883
     always met, so just emit the FILE symbol unconditionally.  */
10884
0
  if (eoinfo->localsyms
10885
0
      && !eoinfo->file_sym_done
10886
0
      && eoinfo->flinfo->filesym_count != 0)
10887
0
    {
10888
0
      Elf_Internal_Sym fsym;
10889
10890
0
      memset (&fsym, 0, sizeof (fsym));
10891
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10892
0
      fsym.st_shndx = SHN_ABS;
10893
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10894
0
              bfd_und_section_ptr, NULL))
10895
0
  return false;
10896
10897
0
      eoinfo->file_sym_done = true;
10898
0
    }
10899
10900
0
  indx = bfd_get_symcount (flinfo->output_bfd);
10901
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10902
0
           input_sec, h);
10903
0
  if (ret == 0)
10904
0
    {
10905
0
      eoinfo->failed = true;
10906
0
      return false;
10907
0
    }
10908
0
  else if (ret == 1)
10909
0
    h->indx = indx;
10910
0
  else if (h->indx == -2)
10911
0
    abort();
10912
10913
0
  return true;
10914
0
}
10915
10916
/* Return TRUE if special handling is done for relocs in SEC against
10917
   symbols defined in discarded sections.  */
10918
10919
static bool
10920
elf_section_ignore_discarded_relocs (asection *sec)
10921
0
{
10922
0
  const struct elf_backend_data *bed;
10923
10924
0
  switch (sec->sec_info_type)
10925
0
    {
10926
0
    case SEC_INFO_TYPE_STABS:
10927
0
    case SEC_INFO_TYPE_EH_FRAME:
10928
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10929
0
    case SEC_INFO_TYPE_SFRAME:
10930
0
      return true;
10931
0
    default:
10932
0
      break;
10933
0
    }
10934
10935
0
  bed = get_elf_backend_data (sec->owner);
10936
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
10937
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10938
0
    return true;
10939
10940
0
  return false;
10941
0
}
10942
10943
/* Return a mask saying how ld should treat relocations in SEC against
10944
   symbols defined in discarded sections.  If this function returns
10945
   COMPLAIN set, ld will issue a warning message.  If this function
10946
   returns PRETEND set, and the discarded section was link-once and the
10947
   same size as the kept link-once section, ld will pretend that the
10948
   symbol was actually defined in the kept section.  Otherwise ld will
10949
   zero the reloc (at least that is the intent, but some cooperation by
10950
   the target dependent code is needed, particularly for REL targets).  */
10951
10952
unsigned int
10953
_bfd_elf_default_action_discarded (asection *sec)
10954
0
{
10955
0
  const struct elf_backend_data *bed;
10956
0
  bed = get_elf_backend_data (sec->owner);
10957
10958
0
  if (sec->flags & SEC_DEBUGGING)
10959
0
    return PRETEND;
10960
10961
0
  if (strcmp (".eh_frame", sec->name) == 0)
10962
0
    return 0;
10963
10964
0
  if (bed->elf_backend_can_make_multiple_eh_frame
10965
0
      && strncmp (sec->name, ".eh_frame.", 10) == 0)
10966
0
    return 0;
10967
10968
0
  if (strcmp (".sframe", sec->name) == 0)
10969
0
    return 0;
10970
10971
0
  if (strcmp (".gcc_except_table", sec->name) == 0)
10972
0
    return 0;
10973
10974
0
  return COMPLAIN | PRETEND;
10975
0
}
10976
10977
/* Find a match between a section and a member of a section group.  */
10978
10979
static asection *
10980
match_group_member (asection *sec, asection *group,
10981
        struct bfd_link_info *info)
10982
0
{
10983
0
  asection *first = elf_next_in_group (group);
10984
0
  asection *s = first;
10985
10986
0
  while (s != NULL)
10987
0
    {
10988
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
10989
0
  return s;
10990
10991
0
      s = elf_next_in_group (s);
10992
0
      if (s == first)
10993
0
  break;
10994
0
    }
10995
10996
0
  return NULL;
10997
0
}
10998
10999
/* Check if the kept section of a discarded section SEC can be used
11000
   to replace it.  Return the replacement if it is OK.  Otherwise return
11001
   NULL.  */
11002
11003
asection *
11004
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11005
0
{
11006
0
  asection *kept;
11007
11008
0
  kept = sec->kept_section;
11009
0
  if (kept != NULL)
11010
0
    {
11011
0
      if ((kept->flags & SEC_GROUP) != 0)
11012
0
  kept = match_group_member (sec, kept, info);
11013
0
      if (kept != NULL)
11014
0
  {
11015
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11016
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11017
0
      kept = NULL;
11018
0
    else
11019
0
      {
11020
        /* Get the real kept section.  */
11021
0
        asection *next;
11022
0
        for (next = kept->kept_section;
11023
0
       next != NULL;
11024
0
       next = next->kept_section)
11025
0
    kept = next;
11026
0
      }
11027
0
  }
11028
0
      sec->kept_section = kept;
11029
0
    }
11030
0
  return kept;
11031
0
}
11032
11033
/* Link an input file into the linker output file.  This function
11034
   handles all the sections and relocations of the input file at once.
11035
   This is so that we only have to read the local symbols once, and
11036
   don't have to keep them in memory.  */
11037
11038
static bool
11039
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11040
0
{
11041
0
  int (*relocate_section)
11042
0
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11043
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11044
0
  bfd *output_bfd;
11045
0
  Elf_Internal_Shdr *symtab_hdr;
11046
0
  size_t locsymcount;
11047
0
  size_t extsymoff;
11048
0
  Elf_Internal_Sym *isymbuf;
11049
0
  Elf_Internal_Sym *isym;
11050
0
  Elf_Internal_Sym *isymend;
11051
0
  long *pindex;
11052
0
  asection **ppsection;
11053
0
  asection *o;
11054
0
  const struct elf_backend_data *bed;
11055
0
  struct elf_link_hash_entry **sym_hashes;
11056
0
  bfd_size_type address_size;
11057
0
  bfd_vma r_type_mask;
11058
0
  int r_sym_shift;
11059
0
  bool have_file_sym = false;
11060
11061
0
  output_bfd = flinfo->output_bfd;
11062
0
  bed = get_elf_backend_data (output_bfd);
11063
0
  relocate_section = bed->elf_backend_relocate_section;
11064
11065
  /* If this is a dynamic object, we don't want to do anything here:
11066
     we don't want the local symbols, and we don't want the section
11067
     contents.  */
11068
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11069
0
    return true;
11070
11071
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11072
0
  if (elf_bad_symtab (input_bfd))
11073
0
    {
11074
0
      locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11075
0
      extsymoff = 0;
11076
0
    }
11077
0
  else
11078
0
    {
11079
0
      locsymcount = symtab_hdr->sh_info;
11080
0
      extsymoff = symtab_hdr->sh_info;
11081
0
    }
11082
11083
  /* Enable GNU OSABI features in the output BFD that are used in the input
11084
     BFD.  */
11085
0
  if (bed->elf_osabi == ELFOSABI_NONE
11086
0
      || bed->elf_osabi == ELFOSABI_GNU
11087
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11088
0
    elf_tdata (output_bfd)->has_gnu_osabi
11089
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11090
0
    & (bfd_link_relocatable (flinfo->info)
11091
0
       ? -1 : ~elf_gnu_osabi_retain));
11092
11093
  /* Read the local symbols.  */
11094
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11095
0
  if (isymbuf == NULL && locsymcount != 0)
11096
0
    {
11097
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11098
0
              flinfo->internal_syms,
11099
0
              flinfo->external_syms,
11100
0
              flinfo->locsym_shndx);
11101
0
      if (isymbuf == NULL)
11102
0
  return false;
11103
0
    }
11104
11105
  /* Find local symbol sections and adjust values of symbols in
11106
     SEC_MERGE sections.  Write out those local symbols we know are
11107
     going into the output file.  */
11108
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11109
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11110
0
       isym < isymend;
11111
0
       isym++, pindex++, ppsection++)
11112
0
    {
11113
0
      asection *isec;
11114
0
      const char *name;
11115
0
      Elf_Internal_Sym osym;
11116
0
      long indx;
11117
0
      int ret;
11118
11119
0
      *pindex = -1;
11120
11121
0
      if (elf_bad_symtab (input_bfd))
11122
0
  {
11123
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11124
0
      {
11125
0
        *ppsection = NULL;
11126
0
        continue;
11127
0
      }
11128
0
  }
11129
11130
0
      if (isym->st_shndx == SHN_UNDEF)
11131
0
  isec = bfd_und_section_ptr;
11132
0
      else if (isym->st_shndx == SHN_ABS)
11133
0
  isec = bfd_abs_section_ptr;
11134
0
      else if (isym->st_shndx == SHN_COMMON)
11135
0
  isec = bfd_com_section_ptr;
11136
0
      else
11137
0
  {
11138
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11139
0
    if (isec == NULL)
11140
0
      {
11141
        /* Don't attempt to output symbols with st_shnx in the
11142
     reserved range other than SHN_ABS and SHN_COMMON.  */
11143
0
        isec = bfd_und_section_ptr;
11144
0
      }
11145
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11146
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11147
0
      isym->st_value =
11148
0
        _bfd_merged_section_offset (output_bfd, &isec,
11149
0
            elf_section_data (isec)->sec_info,
11150
0
            isym->st_value);
11151
0
  }
11152
11153
0
      *ppsection = isec;
11154
11155
      /* Don't output the first, undefined, symbol.  In fact, don't
11156
   output any undefined local symbol.  */
11157
0
      if (isec == bfd_und_section_ptr)
11158
0
  continue;
11159
11160
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11161
0
  {
11162
    /* We never output section symbols.  Instead, we use the
11163
       section symbol of the corresponding section in the output
11164
       file.  */
11165
0
    continue;
11166
0
  }
11167
11168
      /* If we are stripping all symbols, we don't want to output this
11169
   one.  */
11170
0
      if (flinfo->info->strip == strip_all)
11171
0
  continue;
11172
11173
      /* If we are discarding all local symbols, we don't want to
11174
   output this one.  If we are generating a relocatable output
11175
   file, then some of the local symbols may be required by
11176
   relocs; we output them below as we discover that they are
11177
   needed.  */
11178
0
      if (flinfo->info->discard == discard_all)
11179
0
  continue;
11180
11181
      /* If this symbol is defined in a section which we are
11182
   discarding, we don't need to keep it.  */
11183
0
      if (isym->st_shndx < SHN_LORESERVE
11184
0
    && (isec->output_section == NULL
11185
0
        || bfd_section_removed_from_list (output_bfd,
11186
0
            isec->output_section)))
11187
0
  continue;
11188
11189
      /* Get the name of the symbol.  */
11190
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11191
0
                isym->st_name);
11192
0
      if (name == NULL)
11193
0
  return false;
11194
11195
      /* See if we are discarding symbols with this name.  */
11196
0
      if ((flinfo->info->strip == strip_some
11197
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11198
0
         == NULL))
11199
0
    || (((flinfo->info->discard == discard_sec_merge
11200
0
    && (isec->flags & SEC_MERGE)
11201
0
    && !bfd_link_relocatable (flinfo->info))
11202
0
         || flinfo->info->discard == discard_l)
11203
0
        && bfd_is_local_label_name (input_bfd, name)))
11204
0
  continue;
11205
11206
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11207
0
  {
11208
0
    if (input_bfd->lto_output)
11209
      /* -flto puts a temp file name here.  This means builds
11210
         are not reproducible.  Discard the symbol.  */
11211
0
      continue;
11212
0
    have_file_sym = true;
11213
0
    flinfo->filesym_count += 1;
11214
0
  }
11215
0
      if (!have_file_sym)
11216
0
  {
11217
    /* In the absence of debug info, bfd_find_nearest_line uses
11218
       FILE symbols to determine the source file for local
11219
       function symbols.  Provide a FILE symbol here if input
11220
       files lack such, so that their symbols won't be
11221
       associated with a previous input file.  It's not the
11222
       source file, but the best we can do.  */
11223
0
    const char *filename;
11224
0
    have_file_sym = true;
11225
0
    flinfo->filesym_count += 1;
11226
0
    memset (&osym, 0, sizeof (osym));
11227
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11228
0
    osym.st_shndx = SHN_ABS;
11229
0
    if (input_bfd->lto_output)
11230
0
      filename = NULL;
11231
0
    else
11232
0
      filename = lbasename (bfd_get_filename (input_bfd));
11233
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11234
0
            bfd_abs_section_ptr, NULL))
11235
0
      return false;
11236
0
  }
11237
11238
0
      osym = *isym;
11239
11240
      /* Adjust the section index for the output file.  */
11241
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11242
0
               isec->output_section);
11243
0
      if (osym.st_shndx == SHN_BAD)
11244
0
  return false;
11245
11246
      /* ELF symbols in relocatable files are section relative, but
11247
   in executable files they are virtual addresses.  Note that
11248
   this code assumes that all ELF sections have an associated
11249
   BFD section with a reasonable value for output_offset; below
11250
   we assume that they also have a reasonable value for
11251
   output_section.  Any special sections must be set up to meet
11252
   these requirements.  */
11253
0
      osym.st_value += isec->output_offset;
11254
0
      if (!bfd_link_relocatable (flinfo->info))
11255
0
  {
11256
0
    osym.st_value += isec->output_section->vma;
11257
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11258
0
      {
11259
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11260
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11261
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11262
0
        else
11263
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11264
0
              STT_NOTYPE);
11265
0
      }
11266
0
  }
11267
11268
0
      indx = bfd_get_symcount (output_bfd);
11269
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11270
0
      if (ret == 0)
11271
0
  return false;
11272
0
      else if (ret == 1)
11273
0
  *pindex = indx;
11274
0
    }
11275
11276
0
  if (bed->s->arch_size == 32)
11277
0
    {
11278
0
      r_type_mask = 0xff;
11279
0
      r_sym_shift = 8;
11280
0
      address_size = 4;
11281
0
    }
11282
0
  else
11283
0
    {
11284
0
      r_type_mask = 0xffffffff;
11285
0
      r_sym_shift = 32;
11286
0
      address_size = 8;
11287
0
    }
11288
11289
  /* Relocate the contents of each section.  */
11290
0
  sym_hashes = elf_sym_hashes (input_bfd);
11291
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11292
0
    {
11293
0
      bfd_byte *contents;
11294
11295
0
      if (! o->linker_mark)
11296
0
  {
11297
    /* This section was omitted from the link.  */
11298
0
    continue;
11299
0
  }
11300
11301
0
      if (!flinfo->info->resolve_section_groups
11302
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11303
0
  {
11304
    /* Deal with the group signature symbol.  */
11305
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11306
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11307
0
    asection *osec = o->output_section;
11308
11309
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11310
0
    if (symndx >= locsymcount
11311
0
        || (elf_bad_symtab (input_bfd)
11312
0
      && flinfo->sections[symndx] == NULL))
11313
0
      {
11314
0
        struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11315
0
        while (h->root.type == bfd_link_hash_indirect
11316
0
         || h->root.type == bfd_link_hash_warning)
11317
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
11318
        /* Arrange for symbol to be output.  */
11319
0
        h->indx = -2;
11320
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11321
0
      }
11322
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11323
0
      {
11324
        /* We'll use the output section target_index.  */
11325
0
        asection *sec = flinfo->sections[symndx]->output_section;
11326
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11327
0
      }
11328
0
    else
11329
0
      {
11330
0
        if (flinfo->indices[symndx] == -1)
11331
0
    {
11332
      /* Otherwise output the local symbol now.  */
11333
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11334
0
      asection *sec = flinfo->sections[symndx]->output_section;
11335
0
      const char *name;
11336
0
      long indx;
11337
0
      int ret;
11338
11339
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11340
0
                symtab_hdr->sh_link,
11341
0
                sym.st_name);
11342
0
      if (name == NULL)
11343
0
        return false;
11344
11345
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11346
0
                    sec);
11347
0
      if (sym.st_shndx == SHN_BAD)
11348
0
        return false;
11349
11350
0
      sym.st_value += o->output_offset;
11351
11352
0
      indx = bfd_get_symcount (output_bfd);
11353
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11354
0
               NULL);
11355
0
      if (ret == 0)
11356
0
        return false;
11357
0
      else if (ret == 1)
11358
0
        flinfo->indices[symndx] = indx;
11359
0
      else
11360
0
        abort ();
11361
0
    }
11362
0
        elf_section_data (osec)->this_hdr.sh_info
11363
0
    = flinfo->indices[symndx];
11364
0
      }
11365
0
  }
11366
11367
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11368
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11369
0
  continue;
11370
11371
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11372
0
  {
11373
    /* Section was created by _bfd_elf_link_create_dynamic_sections
11374
       or somesuch.  */
11375
0
    continue;
11376
0
  }
11377
11378
      /* Get the contents of the section.  They have been cached by a
11379
   relaxation routine.  Note that o is a section in an input
11380
   file, so the contents field will not have been set by any of
11381
   the routines which work on output files.  */
11382
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11383
0
  {
11384
0
    contents = elf_section_data (o)->this_hdr.contents;
11385
0
    if (bed->caches_rawsize
11386
0
        && o->rawsize != 0
11387
0
        && o->rawsize < o->size)
11388
0
      {
11389
0
        memcpy (flinfo->contents, contents, o->rawsize);
11390
0
        contents = flinfo->contents;
11391
0
      }
11392
0
  }
11393
0
      else if (!(o->flags & SEC_RELOC)
11394
0
         && !bed->elf_backend_write_section
11395
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11396
  /* A MERGE section that has no relocations doesn't need the
11397
     contents anymore, they have been recorded earlier.  Except
11398
     if the backend has special provisions for writing sections.  */
11399
0
  contents = NULL;
11400
0
      else
11401
0
  {
11402
0
    contents = flinfo->contents;
11403
0
    if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11404
0
      return false;
11405
0
  }
11406
11407
0
      if ((o->flags & SEC_RELOC) != 0)
11408
0
  {
11409
0
    Elf_Internal_Rela *internal_relocs;
11410
0
    Elf_Internal_Rela *rel, *relend;
11411
0
    int action_discarded;
11412
0
    int ret;
11413
11414
    /* Get the swapped relocs.  */
11415
0
    internal_relocs
11416
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11417
0
                flinfo->external_relocs,
11418
0
                flinfo->internal_relocs,
11419
0
                false);
11420
0
    if (internal_relocs == NULL
11421
0
        && o->reloc_count > 0)
11422
0
      return false;
11423
11424
0
    action_discarded = -1;
11425
0
    if (!elf_section_ignore_discarded_relocs (o))
11426
0
      action_discarded = (*bed->action_discarded) (o);
11427
11428
    /* Run through the relocs evaluating complex reloc symbols and
11429
       looking for relocs against symbols from discarded sections
11430
       or section symbols from removed link-once sections.
11431
       Complain about relocs against discarded sections.  Zero
11432
       relocs against removed link-once sections.  */
11433
11434
0
    rel = internal_relocs;
11435
0
    relend = rel + o->reloc_count;
11436
0
    for ( ; rel < relend; rel++)
11437
0
      {
11438
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11439
0
        unsigned int s_type;
11440
0
        asection **ps, *sec;
11441
0
        struct elf_link_hash_entry *h = NULL;
11442
0
        const char *sym_name;
11443
11444
0
        if (r_symndx == STN_UNDEF)
11445
0
    continue;
11446
11447
0
        if (r_symndx >= locsymcount
11448
0
      || (elf_bad_symtab (input_bfd)
11449
0
          && flinfo->sections[r_symndx] == NULL))
11450
0
    {
11451
0
      h = sym_hashes[r_symndx - extsymoff];
11452
11453
      /* Badly formatted input files can contain relocs that
11454
         reference non-existant symbols.  Check here so that
11455
         we do not seg fault.  */
11456
0
      if (h == NULL)
11457
0
        {
11458
0
          _bfd_error_handler
11459
      /* xgettext:c-format */
11460
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11461
0
         "that references a non-existent global symbol"),
11462
0
       input_bfd, (uint64_t) rel->r_info, o);
11463
0
          bfd_set_error (bfd_error_bad_value);
11464
0
          return false;
11465
0
        }
11466
11467
0
      while (h->root.type == bfd_link_hash_indirect
11468
0
       || h->root.type == bfd_link_hash_warning)
11469
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
11470
11471
0
      s_type = h->type;
11472
11473
      /* If a plugin symbol is referenced from a non-IR file,
11474
         mark the symbol as undefined.  Note that the
11475
         linker may attach linker created dynamic sections
11476
         to the plugin bfd.  Symbols defined in linker
11477
         created sections are not plugin symbols.  */
11478
0
      if ((h->root.non_ir_ref_regular
11479
0
           || h->root.non_ir_ref_dynamic)
11480
0
          && (h->root.type == bfd_link_hash_defined
11481
0
        || h->root.type == bfd_link_hash_defweak)
11482
0
          && (h->root.u.def.section->flags
11483
0
        & SEC_LINKER_CREATED) == 0
11484
0
          && h->root.u.def.section->owner != NULL
11485
0
          && (h->root.u.def.section->owner->flags
11486
0
        & BFD_PLUGIN) != 0)
11487
0
        {
11488
0
          h->root.type = bfd_link_hash_undefined;
11489
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11490
0
        }
11491
11492
0
      ps = NULL;
11493
0
      if (h->root.type == bfd_link_hash_defined
11494
0
          || h->root.type == bfd_link_hash_defweak)
11495
0
        ps = &h->root.u.def.section;
11496
11497
0
      sym_name = h->root.root.string;
11498
0
    }
11499
0
        else
11500
0
    {
11501
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11502
11503
0
      s_type = ELF_ST_TYPE (sym->st_info);
11504
0
      ps = &flinfo->sections[r_symndx];
11505
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11506
0
                 sym, *ps);
11507
0
    }
11508
11509
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11510
0
      && !bfd_link_relocatable (flinfo->info))
11511
0
    {
11512
0
      bfd_vma val;
11513
0
      bfd_vma dot = (rel->r_offset
11514
0
         + o->output_offset + o->output_section->vma);
11515
#ifdef DEBUG
11516
      printf ("Encountered a complex symbol!");
11517
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11518
        bfd_get_filename (input_bfd), o->name,
11519
        (long) (rel - internal_relocs));
11520
      printf (" symbol: idx  %8.8lx, name %s\n",
11521
        r_symndx, sym_name);
11522
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11523
        (unsigned long) rel->r_info,
11524
        (unsigned long) rel->r_offset);
11525
#endif
11526
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11527
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11528
0
        return false;
11529
11530
      /* Symbol evaluated OK.  Update to absolute value.  */
11531
0
      set_symbol_value (input_bfd, isymbuf, locsymcount,
11532
0
            r_symndx, val);
11533
0
      continue;
11534
0
    }
11535
11536
0
        if (action_discarded != -1 && ps != NULL)
11537
0
    {
11538
      /* Complain if the definition comes from a
11539
         discarded section.  */
11540
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11541
0
        {
11542
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11543
0
          if (action_discarded & COMPLAIN)
11544
0
      (*flinfo->info->callbacks->einfo)
11545
        /* xgettext:c-format */
11546
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11547
0
           "defined in discarded section `%pA' of %pB\n"),
11548
0
         sym_name, o, input_bfd, sec, sec->owner);
11549
11550
          /* Try to do the best we can to support buggy old
11551
       versions of gcc.  Pretend that the symbol is
11552
       really defined in the kept linkonce section.
11553
       FIXME: This is quite broken.  Modifying the
11554
       symbol here means we will be changing all later
11555
       uses of the symbol, not just in this section.  */
11556
0
          if (action_discarded & PRETEND)
11557
0
      {
11558
0
        asection *kept;
11559
11560
0
        kept = _bfd_elf_check_kept_section (sec,
11561
0
                    flinfo->info);
11562
0
        if (kept != NULL)
11563
0
          {
11564
0
            *ps = kept;
11565
0
            continue;
11566
0
          }
11567
0
      }
11568
0
        }
11569
0
    }
11570
0
      }
11571
11572
    /* Relocate the section by invoking a back end routine.
11573
11574
       The back end routine is responsible for adjusting the
11575
       section contents as necessary, and (if using Rela relocs
11576
       and generating a relocatable output file) adjusting the
11577
       reloc addend as necessary.
11578
11579
       The back end routine does not have to worry about setting
11580
       the reloc address or the reloc symbol index.
11581
11582
       The back end routine is given a pointer to the swapped in
11583
       internal symbols, and can access the hash table entries
11584
       for the external symbols via elf_sym_hashes (input_bfd).
11585
11586
       When generating relocatable output, the back end routine
11587
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11588
       output symbol is going to be a section symbol
11589
       corresponding to the output section, which will require
11590
       the addend to be adjusted.  */
11591
11592
0
    ret = (*relocate_section) (output_bfd, flinfo->info,
11593
0
             input_bfd, o, contents,
11594
0
             internal_relocs,
11595
0
             isymbuf,
11596
0
             flinfo->sections);
11597
0
    if (!ret)
11598
0
      return false;
11599
11600
0
    if (ret == 2
11601
0
        || bfd_link_relocatable (flinfo->info)
11602
0
        || flinfo->info->emitrelocations)
11603
0
      {
11604
0
        Elf_Internal_Rela *irela;
11605
0
        Elf_Internal_Rela *irelaend, *irelamid;
11606
0
        bfd_vma last_offset;
11607
0
        struct elf_link_hash_entry **rel_hash;
11608
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11609
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11610
0
        unsigned int next_erel;
11611
0
        bool rela_normal;
11612
0
        struct bfd_elf_section_data *esdi, *esdo;
11613
11614
0
        esdi = elf_section_data (o);
11615
0
        esdo = elf_section_data (o->output_section);
11616
0
        rela_normal = false;
11617
11618
        /* Adjust the reloc addresses and symbol indices.  */
11619
11620
0
        irela = internal_relocs;
11621
0
        irelaend = irela + o->reloc_count;
11622
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11623
        /* We start processing the REL relocs, if any.  When we reach
11624
     IRELAMID in the loop, we switch to the RELA relocs.  */
11625
0
        irelamid = irela;
11626
0
        if (esdi->rel.hdr != NULL)
11627
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11628
0
           * bed->s->int_rels_per_ext_rel);
11629
0
        rel_hash_list = rel_hash;
11630
0
        rela_hash_list = NULL;
11631
0
        last_offset = o->output_offset;
11632
0
        if (!bfd_link_relocatable (flinfo->info))
11633
0
    last_offset += o->output_section->vma;
11634
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11635
0
    {
11636
0
      unsigned long r_symndx;
11637
0
      asection *sec;
11638
0
      Elf_Internal_Sym sym;
11639
11640
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
11641
0
        {
11642
0
          rel_hash++;
11643
0
          next_erel = 0;
11644
0
        }
11645
11646
0
      if (irela == irelamid)
11647
0
        {
11648
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11649
0
          rela_hash_list = rel_hash;
11650
0
          rela_normal = bed->rela_normal;
11651
0
        }
11652
11653
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
11654
0
                   flinfo->info, o,
11655
0
                   irela->r_offset);
11656
0
      if (irela->r_offset >= (bfd_vma) -2)
11657
0
        {
11658
          /* This is a reloc for a deleted entry or somesuch.
11659
       Turn it into an R_*_NONE reloc, at the same
11660
       offset as the last reloc.  elf_eh_frame.c and
11661
       bfd_elf_discard_info rely on reloc offsets
11662
       being ordered.  */
11663
0
          irela->r_offset = last_offset;
11664
0
          irela->r_info = 0;
11665
0
          irela->r_addend = 0;
11666
0
          continue;
11667
0
        }
11668
11669
0
      irela->r_offset += o->output_offset;
11670
11671
      /* Relocs in an executable have to be virtual addresses.  */
11672
0
      if (!bfd_link_relocatable (flinfo->info))
11673
0
        irela->r_offset += o->output_section->vma;
11674
11675
0
      last_offset = irela->r_offset;
11676
11677
0
      r_symndx = irela->r_info >> r_sym_shift;
11678
0
      if (r_symndx == STN_UNDEF)
11679
0
        continue;
11680
11681
0
      if (r_symndx >= locsymcount
11682
0
          || (elf_bad_symtab (input_bfd)
11683
0
        && flinfo->sections[r_symndx] == NULL))
11684
0
        {
11685
0
          struct elf_link_hash_entry *rh;
11686
0
          unsigned long indx;
11687
11688
          /* This is a reloc against a global symbol.  We
11689
       have not yet output all the local symbols, so
11690
       we do not know the symbol index of any global
11691
       symbol.  We set the rel_hash entry for this
11692
       reloc to point to the global hash table entry
11693
       for this symbol.  The symbol index is then
11694
       set at the end of bfd_elf_final_link.  */
11695
0
          indx = r_symndx - extsymoff;
11696
0
          rh = elf_sym_hashes (input_bfd)[indx];
11697
0
          while (rh->root.type == bfd_link_hash_indirect
11698
0
           || rh->root.type == bfd_link_hash_warning)
11699
0
      rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11700
11701
          /* Setting the index to -2 tells
11702
       elf_link_output_extsym that this symbol is
11703
       used by a reloc.  */
11704
0
          BFD_ASSERT (rh->indx < 0);
11705
0
          rh->indx = -2;
11706
0
          *rel_hash = rh;
11707
11708
0
          continue;
11709
0
        }
11710
11711
      /* This is a reloc against a local symbol.  */
11712
11713
0
      *rel_hash = NULL;
11714
0
      sym = isymbuf[r_symndx];
11715
0
      sec = flinfo->sections[r_symndx];
11716
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11717
0
        {
11718
          /* I suppose the backend ought to fill in the
11719
       section of any STT_SECTION symbol against a
11720
       processor specific section.  */
11721
0
          r_symndx = STN_UNDEF;
11722
0
          if (bfd_is_abs_section (sec))
11723
0
      ;
11724
0
          else if (sec == NULL || sec->owner == NULL)
11725
0
      {
11726
0
        bfd_set_error (bfd_error_bad_value);
11727
0
        return false;
11728
0
      }
11729
0
          else
11730
0
      {
11731
0
        asection *osec = sec->output_section;
11732
11733
        /* If we have discarded a section, the output
11734
           section will be the absolute section.  In
11735
           case of discarded SEC_MERGE sections, use
11736
           the kept section.  relocate_section should
11737
           have already handled discarded linkonce
11738
           sections.  */
11739
0
        if (bfd_is_abs_section (osec)
11740
0
            && sec->kept_section != NULL
11741
0
            && sec->kept_section->output_section != NULL)
11742
0
          {
11743
0
            osec = sec->kept_section->output_section;
11744
0
            irela->r_addend -= osec->vma;
11745
0
          }
11746
11747
0
        if (!bfd_is_abs_section (osec))
11748
0
          {
11749
0
            r_symndx = osec->target_index;
11750
0
            if (r_symndx == STN_UNDEF)
11751
0
        {
11752
0
          irela->r_addend += osec->vma;
11753
0
          osec = _bfd_nearby_section (output_bfd, osec,
11754
0
                    osec->vma);
11755
0
          irela->r_addend -= osec->vma;
11756
0
          r_symndx = osec->target_index;
11757
0
        }
11758
0
          }
11759
0
      }
11760
11761
          /* Adjust the addend according to where the
11762
       section winds up in the output section.  */
11763
0
          if (rela_normal)
11764
0
      irela->r_addend += sec->output_offset;
11765
0
        }
11766
0
      else
11767
0
        {
11768
0
          if (flinfo->indices[r_symndx] == -1)
11769
0
      {
11770
0
        unsigned long shlink;
11771
0
        const char *name;
11772
0
        asection *osec;
11773
0
        long indx;
11774
11775
0
        if (flinfo->info->strip == strip_all)
11776
0
          {
11777
            /* You can't do ld -r -s.  */
11778
0
            bfd_set_error (bfd_error_invalid_operation);
11779
0
            return false;
11780
0
          }
11781
11782
        /* This symbol was skipped earlier, but
11783
           since it is needed by a reloc, we
11784
           must output it now.  */
11785
0
        shlink = symtab_hdr->sh_link;
11786
0
        name = (bfd_elf_string_from_elf_section
11787
0
          (input_bfd, shlink, sym.st_name));
11788
0
        if (name == NULL)
11789
0
          return false;
11790
11791
0
        osec = sec->output_section;
11792
0
        sym.st_shndx =
11793
0
          _bfd_elf_section_from_bfd_section (output_bfd,
11794
0
                     osec);
11795
0
        if (sym.st_shndx == SHN_BAD)
11796
0
          return false;
11797
11798
0
        sym.st_value += sec->output_offset;
11799
0
        if (!bfd_link_relocatable (flinfo->info))
11800
0
          {
11801
0
            sym.st_value += osec->vma;
11802
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11803
0
        {
11804
0
          struct elf_link_hash_table *htab
11805
0
            = elf_hash_table (flinfo->info);
11806
11807
          /* STT_TLS symbols are relative to PT_TLS
11808
             segment base.  */
11809
0
          if (htab->tls_sec != NULL)
11810
0
            sym.st_value -= htab->tls_sec->vma;
11811
0
          else
11812
0
            sym.st_info
11813
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11814
0
                 STT_NOTYPE);
11815
0
        }
11816
0
          }
11817
11818
0
        indx = bfd_get_symcount (output_bfd);
11819
0
        ret = elf_link_output_symstrtab (flinfo, name,
11820
0
                 &sym, sec,
11821
0
                 NULL);
11822
0
        if (ret == 0)
11823
0
          return false;
11824
0
        else if (ret == 1)
11825
0
          flinfo->indices[r_symndx] = indx;
11826
0
        else
11827
0
          abort ();
11828
0
      }
11829
11830
0
          r_symndx = flinfo->indices[r_symndx];
11831
0
        }
11832
11833
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11834
0
           | (irela->r_info & r_type_mask));
11835
0
    }
11836
11837
        /* Swap out the relocs.  */
11838
0
        input_rel_hdr = esdi->rel.hdr;
11839
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11840
0
    {
11841
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
11842
0
                 input_rel_hdr,
11843
0
                 internal_relocs,
11844
0
                 rel_hash_list))
11845
0
        return false;
11846
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11847
0
              * bed->s->int_rels_per_ext_rel);
11848
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11849
0
    }
11850
11851
0
        input_rela_hdr = esdi->rela.hdr;
11852
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11853
0
    {
11854
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
11855
0
                 input_rela_hdr,
11856
0
                 internal_relocs,
11857
0
                 rela_hash_list))
11858
0
        return false;
11859
0
    }
11860
0
      }
11861
0
  }
11862
11863
      /* Write out the modified section contents.  */
11864
0
      if (bed->elf_backend_write_section
11865
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11866
0
            contents))
11867
0
  {
11868
    /* Section written out.  */
11869
0
  }
11870
0
      else switch (o->sec_info_type)
11871
0
  {
11872
0
  case SEC_INFO_TYPE_STABS:
11873
0
    if (! (_bfd_write_section_stabs
11874
0
     (output_bfd,
11875
0
      &elf_hash_table (flinfo->info)->stab_info,
11876
0
      o, &elf_section_data (o)->sec_info, contents)))
11877
0
      return false;
11878
0
    break;
11879
0
  case SEC_INFO_TYPE_MERGE:
11880
0
    if (! _bfd_write_merged_section (output_bfd, o,
11881
0
             elf_section_data (o)->sec_info))
11882
0
      return false;
11883
0
    break;
11884
0
  case SEC_INFO_TYPE_EH_FRAME:
11885
0
    {
11886
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11887
0
               o, contents))
11888
0
        return false;
11889
0
    }
11890
0
    break;
11891
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11892
0
    {
11893
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11894
0
               flinfo->info,
11895
0
               o, contents))
11896
0
        return false;
11897
0
    }
11898
0
    break;
11899
0
  case SEC_INFO_TYPE_SFRAME:
11900
0
      {
11901
        /* Merge .sframe sections into the ctf frame encoder
11902
     context of the output_bfd's section.  The final .sframe
11903
     output section will be written out later.  */
11904
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11905
0
              o, contents))
11906
0
    return false;
11907
0
      }
11908
0
      break;
11909
0
  default:
11910
0
    {
11911
0
      if (! (o->flags & SEC_EXCLUDE))
11912
0
        {
11913
0
    file_ptr offset = (file_ptr) o->output_offset;
11914
0
    bfd_size_type todo = o->size;
11915
11916
0
    offset *= bfd_octets_per_byte (output_bfd, o);
11917
11918
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
11919
0
        && o->size > address_size)
11920
0
      {
11921
        /* Reverse-copy input section to output.  */
11922
11923
0
        if ((o->size & (address_size - 1)) != 0
11924
0
      || (o->reloc_count != 0
11925
0
          && (o->size * bed->s->int_rels_per_ext_rel
11926
0
        != o->reloc_count * address_size)))
11927
0
          {
11928
0
      _bfd_error_handler
11929
        /* xgettext:c-format */
11930
0
        (_("error: %pB: size of section %pA is not "
11931
0
           "multiple of address size"),
11932
0
         input_bfd, o);
11933
0
      bfd_set_error (bfd_error_bad_value);
11934
0
      return false;
11935
0
          }
11936
11937
0
        do
11938
0
          {
11939
0
      todo -= address_size;
11940
0
      if (! bfd_set_section_contents (output_bfd,
11941
0
              o->output_section,
11942
0
              contents + todo,
11943
0
              offset,
11944
0
              address_size))
11945
0
        return false;
11946
0
      if (todo == 0)
11947
0
        break;
11948
0
      offset += address_size;
11949
0
          }
11950
0
        while (1);
11951
0
      }
11952
0
    else if (! bfd_set_section_contents (output_bfd,
11953
0
                 o->output_section,
11954
0
                 contents,
11955
0
                 offset, todo))
11956
0
      return false;
11957
0
        }
11958
0
    }
11959
0
    break;
11960
0
  }
11961
0
    }
11962
11963
0
  return true;
11964
0
}
11965
11966
/* Generate a reloc when linking an ELF file.  This is a reloc
11967
   requested by the linker, and does not come from any input file.  This
11968
   is used to build constructor and destructor tables when linking
11969
   with -Ur.  */
11970
11971
static bool
11972
elf_reloc_link_order (bfd *output_bfd,
11973
          struct bfd_link_info *info,
11974
          asection *output_section,
11975
          struct bfd_link_order *link_order)
11976
0
{
11977
0
  reloc_howto_type *howto;
11978
0
  long indx;
11979
0
  bfd_vma offset;
11980
0
  bfd_vma addend;
11981
0
  struct bfd_elf_section_reloc_data *reldata;
11982
0
  struct elf_link_hash_entry **rel_hash_ptr;
11983
0
  Elf_Internal_Shdr *rel_hdr;
11984
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11985
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11986
0
  bfd_byte *erel;
11987
0
  unsigned int i;
11988
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11989
11990
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11991
0
  if (howto == NULL)
11992
0
    {
11993
0
      bfd_set_error (bfd_error_bad_value);
11994
0
      return false;
11995
0
    }
11996
11997
0
  addend = link_order->u.reloc.p->addend;
11998
11999
0
  if (esdo->rel.hdr)
12000
0
    reldata = &esdo->rel;
12001
0
  else if (esdo->rela.hdr)
12002
0
    reldata = &esdo->rela;
12003
0
  else
12004
0
    {
12005
0
      reldata = NULL;
12006
0
      BFD_ASSERT (0);
12007
0
    }
12008
12009
  /* Figure out the symbol index.  */
12010
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12011
0
  if (link_order->type == bfd_section_reloc_link_order)
12012
0
    {
12013
0
      indx = link_order->u.reloc.p->u.section->target_index;
12014
0
      BFD_ASSERT (indx != 0);
12015
0
      *rel_hash_ptr = NULL;
12016
0
    }
12017
0
  else
12018
0
    {
12019
0
      struct elf_link_hash_entry *h;
12020
12021
      /* Treat a reloc against a defined symbol as though it were
12022
   actually against the section.  */
12023
0
      h = ((struct elf_link_hash_entry *)
12024
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12025
0
           link_order->u.reloc.p->u.name,
12026
0
           false, false, true));
12027
0
      if (h != NULL
12028
0
    && (h->root.type == bfd_link_hash_defined
12029
0
        || h->root.type == bfd_link_hash_defweak))
12030
0
  {
12031
0
    asection *section;
12032
12033
0
    section = h->root.u.def.section;
12034
0
    indx = section->output_section->target_index;
12035
0
    *rel_hash_ptr = NULL;
12036
    /* It seems that we ought to add the symbol value to the
12037
       addend here, but in practice it has already been added
12038
       because it was passed to constructor_callback.  */
12039
0
    addend += section->output_section->vma + section->output_offset;
12040
0
  }
12041
0
      else if (h != NULL)
12042
0
  {
12043
    /* Setting the index to -2 tells elf_link_output_extsym that
12044
       this symbol is used by a reloc.  */
12045
0
    h->indx = -2;
12046
0
    *rel_hash_ptr = h;
12047
0
    indx = 0;
12048
0
  }
12049
0
      else
12050
0
  {
12051
0
    (*info->callbacks->unattached_reloc)
12052
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12053
0
    indx = 0;
12054
0
  }
12055
0
    }
12056
12057
  /* If this is an inplace reloc, we must write the addend into the
12058
     object file.  */
12059
0
  if (howto->partial_inplace && addend != 0)
12060
0
    {
12061
0
      bfd_size_type size;
12062
0
      bfd_reloc_status_type rstat;
12063
0
      bfd_byte *buf;
12064
0
      bool ok;
12065
0
      const char *sym_name;
12066
0
      bfd_size_type octets;
12067
12068
0
      size = (bfd_size_type) bfd_get_reloc_size (howto);
12069
0
      buf = (bfd_byte *) bfd_zmalloc (size);
12070
0
      if (buf == NULL && size != 0)
12071
0
  return false;
12072
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12073
0
      switch (rstat)
12074
0
  {
12075
0
  case bfd_reloc_ok:
12076
0
    break;
12077
12078
0
  default:
12079
0
  case bfd_reloc_outofrange:
12080
0
    abort ();
12081
12082
0
  case bfd_reloc_overflow:
12083
0
    if (link_order->type == bfd_section_reloc_link_order)
12084
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12085
0
    else
12086
0
      sym_name = link_order->u.reloc.p->u.name;
12087
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12088
0
                howto->name, addend, NULL, NULL,
12089
0
                (bfd_vma) 0);
12090
0
    break;
12091
0
  }
12092
12093
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12094
0
               output_section);
12095
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12096
0
             octets, size);
12097
0
      free (buf);
12098
0
      if (! ok)
12099
0
  return false;
12100
0
    }
12101
12102
  /* The address of a reloc is relative to the section in a
12103
     relocatable file, and is a virtual address in an executable
12104
     file.  */
12105
0
  offset = link_order->offset;
12106
0
  if (! bfd_link_relocatable (info))
12107
0
    offset += output_section->vma;
12108
12109
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12110
0
    {
12111
0
      irel[i].r_offset = offset;
12112
0
      irel[i].r_info = 0;
12113
0
      irel[i].r_addend = 0;
12114
0
    }
12115
0
  if (bed->s->arch_size == 32)
12116
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12117
0
  else
12118
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12119
12120
0
  rel_hdr = reldata->hdr;
12121
0
  erel = rel_hdr->contents;
12122
0
  if (rel_hdr->sh_type == SHT_REL)
12123
0
    {
12124
0
      erel += reldata->count * bed->s->sizeof_rel;
12125
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12126
0
    }
12127
0
  else
12128
0
    {
12129
0
      irel[0].r_addend = addend;
12130
0
      erel += reldata->count * bed->s->sizeof_rela;
12131
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12132
0
    }
12133
12134
0
  ++reldata->count;
12135
12136
0
  return true;
12137
0
}
12138
12139
/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12140
   Returns TRUE upon success, FALSE otherwise.  */
12141
12142
static bool
12143
elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12144
0
{
12145
0
  bool ret = false;
12146
0
  bfd *implib_bfd;
12147
0
  const struct elf_backend_data *bed;
12148
0
  flagword flags;
12149
0
  enum bfd_architecture arch;
12150
0
  unsigned int mach;
12151
0
  asymbol **sympp = NULL;
12152
0
  long symsize;
12153
0
  long symcount;
12154
0
  long src_count;
12155
0
  elf_symbol_type *osymbuf;
12156
0
  size_t amt;
12157
12158
0
  implib_bfd = info->out_implib_bfd;
12159
0
  bed = get_elf_backend_data (abfd);
12160
12161
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12162
0
    return false;
12163
12164
  /* Use flag from executable but make it a relocatable object.  */
12165
0
  flags = bfd_get_file_flags (abfd);
12166
0
  flags &= ~HAS_RELOC;
12167
0
  if (!bfd_set_start_address (implib_bfd, 0)
12168
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12169
0
    return false;
12170
12171
  /* Copy architecture of output file to import library file.  */
12172
0
  arch = bfd_get_arch (abfd);
12173
0
  mach = bfd_get_mach (abfd);
12174
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12175
0
      && (abfd->target_defaulted
12176
0
    || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12177
0
    return false;
12178
12179
  /* Get symbol table size.  */
12180
0
  symsize = bfd_get_symtab_upper_bound (abfd);
12181
0
  if (symsize < 0)
12182
0
    return false;
12183
12184
  /* Read in the symbol table.  */
12185
0
  sympp = (asymbol **) bfd_malloc (symsize);
12186
0
  if (sympp == NULL)
12187
0
    return false;
12188
12189
0
  symcount = bfd_canonicalize_symtab (abfd, sympp);
12190
0
  if (symcount < 0)
12191
0
    goto free_sym_buf;
12192
12193
  /* Allow the BFD backend to copy any private header data it
12194
     understands from the output BFD to the import library BFD.  */
12195
0
  if (! bfd_copy_private_header_data (abfd, implib_bfd))
12196
0
    goto free_sym_buf;
12197
12198
  /* Filter symbols to appear in the import library.  */
12199
0
  if (bed->elf_backend_filter_implib_symbols)
12200
0
    symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12201
0
                   symcount);
12202
0
  else
12203
0
    symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12204
0
  if (symcount == 0)
12205
0
    {
12206
0
      bfd_set_error (bfd_error_no_symbols);
12207
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12208
0
        implib_bfd);
12209
0
      goto free_sym_buf;
12210
0
    }
12211
12212
12213
  /* Make symbols absolute.  */
12214
0
  amt = symcount * sizeof (*osymbuf);
12215
0
  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12216
0
  if (osymbuf == NULL)
12217
0
    goto free_sym_buf;
12218
12219
0
  for (src_count = 0; src_count < symcount; src_count++)
12220
0
    {
12221
0
      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12222
0
        sizeof (*osymbuf));
12223
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12224
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12225
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12226
0
      osymbuf[src_count].internal_elf_sym.st_value =
12227
0
  osymbuf[src_count].symbol.value;
12228
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12229
0
    }
12230
12231
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12232
12233
  /* Allow the BFD backend to copy any private data it understands
12234
     from the output BFD to the import library BFD.  This is done last
12235
     to permit the routine to look at the filtered symbol table.  */
12236
0
  if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12237
0
    goto free_sym_buf;
12238
12239
0
  if (!bfd_close (implib_bfd))
12240
0
    goto free_sym_buf;
12241
12242
0
  ret = true;
12243
12244
0
 free_sym_buf:
12245
0
  free (sympp);
12246
0
  return ret;
12247
0
}
12248
12249
static void
12250
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12251
0
{
12252
0
  asection *o;
12253
12254
0
  if (flinfo->symstrtab != NULL)
12255
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12256
0
  free (flinfo->contents);
12257
0
  free (flinfo->external_relocs);
12258
0
  free (flinfo->internal_relocs);
12259
0
  free (flinfo->external_syms);
12260
0
  free (flinfo->locsym_shndx);
12261
0
  free (flinfo->internal_syms);
12262
0
  free (flinfo->indices);
12263
0
  free (flinfo->sections);
12264
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12265
0
    free (flinfo->symshndxbuf);
12266
0
  for (o = obfd->sections; o != NULL; o = o->next)
12267
0
    {
12268
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12269
0
      free (esdo->rel.hashes);
12270
0
      free (esdo->rela.hashes);
12271
0
    }
12272
0
}
12273
12274
/* Do the final step of an ELF link.  */
12275
12276
bool
12277
bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12278
0
{
12279
0
  bool dynamic;
12280
0
  bool emit_relocs;
12281
0
  bfd *dynobj;
12282
0
  struct elf_final_link_info flinfo;
12283
0
  asection *o;
12284
0
  struct bfd_link_order *p;
12285
0
  bfd *sub;
12286
0
  bfd_size_type max_contents_size;
12287
0
  bfd_size_type max_external_reloc_size;
12288
0
  bfd_size_type max_internal_reloc_count;
12289
0
  bfd_size_type max_sym_count;
12290
0
  bfd_size_type max_sym_shndx_count;
12291
0
  Elf_Internal_Sym elfsym;
12292
0
  unsigned int i;
12293
0
  Elf_Internal_Shdr *symtab_hdr;
12294
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12295
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12296
0
  struct elf_outext_info eoinfo;
12297
0
  bool merged;
12298
0
  size_t relativecount;
12299
0
  size_t relr_entsize;
12300
0
  asection *reldyn = 0;
12301
0
  bfd_size_type amt;
12302
0
  asection *attr_section = NULL;
12303
0
  bfd_vma attr_size = 0;
12304
0
  const char *std_attrs_section;
12305
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12306
0
  bool sections_removed;
12307
0
  bool ret;
12308
12309
0
  if (!is_elf_hash_table (&htab->root))
12310
0
    return false;
12311
12312
0
  if (bfd_link_pic (info))
12313
0
    abfd->flags |= DYNAMIC;
12314
12315
0
  dynamic = htab->dynamic_sections_created;
12316
0
  dynobj = htab->dynobj;
12317
12318
0
  emit_relocs = (bfd_link_relocatable (info)
12319
0
     || info->emitrelocations);
12320
12321
0
  memset (&flinfo, 0, sizeof (flinfo));
12322
0
  flinfo.info = info;
12323
0
  flinfo.output_bfd = abfd;
12324
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12325
0
  if (flinfo.symstrtab == NULL)
12326
0
    return false;
12327
12328
0
  if (! dynamic)
12329
0
    {
12330
0
      flinfo.hash_sec = NULL;
12331
0
      flinfo.symver_sec = NULL;
12332
0
    }
12333
0
  else
12334
0
    {
12335
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12336
      /* Note that dynsym_sec can be NULL (on VMS).  */
12337
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12338
      /* Note that it is OK if symver_sec is NULL.  */
12339
0
    }
12340
12341
0
  if (info->unique_symbol
12342
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12343
0
             local_hash_newfunc,
12344
0
             sizeof (struct local_hash_entry)))
12345
0
    return false;
12346
12347
  /* The object attributes have been merged.  Remove the input
12348
     sections from the link, and set the contents of the output
12349
     section.  */
12350
0
  sections_removed = false;
12351
0
  std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12352
0
  for (o = abfd->sections; o != NULL; o = o->next)
12353
0
    {
12354
0
      bool remove_section = false;
12355
12356
0
      if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12357
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12358
0
  {
12359
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12360
0
      {
12361
0
        asection *input_section;
12362
12363
0
        if (p->type != bfd_indirect_link_order)
12364
0
    continue;
12365
0
        input_section = p->u.indirect.section;
12366
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12367
     elf_link_input_bfd ignores this section.  */
12368
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12369
0
      }
12370
12371
0
    attr_size = bfd_elf_obj_attr_size (abfd);
12372
0
    bfd_set_section_size (o, attr_size);
12373
    /* Skip this section later on.  */
12374
0
    o->map_head.link_order = NULL;
12375
0
    if (attr_size)
12376
0
      attr_section = o;
12377
0
    else
12378
0
      remove_section = true;
12379
0
  }
12380
0
      else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12381
0
  {
12382
    /* Remove empty group section from linker output.  */
12383
0
    remove_section = true;
12384
0
  }
12385
0
      if (remove_section)
12386
0
  {
12387
0
    o->flags |= SEC_EXCLUDE;
12388
0
    bfd_section_list_remove (abfd, o);
12389
0
    abfd->section_count--;
12390
0
    sections_removed = true;
12391
0
  }
12392
0
    }
12393
0
  if (sections_removed)
12394
0
    _bfd_fix_excluded_sec_syms (abfd, info);
12395
12396
  /* Count up the number of relocations we will output for each output
12397
     section, so that we know the sizes of the reloc sections.  We
12398
     also figure out some maximum sizes.  */
12399
0
  max_contents_size = 0;
12400
0
  max_external_reloc_size = 0;
12401
0
  max_internal_reloc_count = 0;
12402
0
  max_sym_count = 0;
12403
0
  max_sym_shndx_count = 0;
12404
0
  merged = false;
12405
0
  for (o = abfd->sections; o != NULL; o = o->next)
12406
0
    {
12407
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12408
0
      o->reloc_count = 0;
12409
12410
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12411
0
  {
12412
0
    unsigned int reloc_count = 0;
12413
0
    unsigned int additional_reloc_count = 0;
12414
0
    struct bfd_elf_section_data *esdi = NULL;
12415
12416
0
    if (p->type == bfd_section_reloc_link_order
12417
0
        || p->type == bfd_symbol_reloc_link_order)
12418
0
      reloc_count = 1;
12419
0
    else if (p->type == bfd_indirect_link_order)
12420
0
      {
12421
0
        asection *sec;
12422
12423
0
        sec = p->u.indirect.section;
12424
12425
        /* Mark all sections which are to be included in the
12426
     link.  This will normally be every section.  We need
12427
     to do this so that we can identify any sections which
12428
     the linker has decided to not include.  */
12429
0
        sec->linker_mark = true;
12430
12431
0
        if (sec->flags & SEC_MERGE)
12432
0
    merged = true;
12433
12434
0
        if (sec->rawsize > max_contents_size)
12435
0
    max_contents_size = sec->rawsize;
12436
0
        if (sec->size > max_contents_size)
12437
0
    max_contents_size = sec->size;
12438
12439
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12440
0
      && (sec->owner->flags & DYNAMIC) == 0)
12441
0
    {
12442
0
      size_t sym_count;
12443
12444
      /* We are interested in just local symbols, not all
12445
         symbols.  */
12446
0
      if (elf_bad_symtab (sec->owner))
12447
0
        sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12448
0
         / bed->s->sizeof_sym);
12449
0
      else
12450
0
        sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12451
12452
0
      if (sym_count > max_sym_count)
12453
0
        max_sym_count = sym_count;
12454
12455
0
      if (sym_count > max_sym_shndx_count
12456
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12457
0
        max_sym_shndx_count = sym_count;
12458
12459
0
      esdi = elf_section_data (sec);
12460
12461
0
      if (esdi->this_hdr.sh_type == SHT_REL
12462
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12463
        /* Some backends use reloc_count in relocation sections
12464
           to count particular types of relocs.  Of course,
12465
           reloc sections themselves can't have relocations.  */
12466
0
        ;
12467
0
      else if (emit_relocs)
12468
0
        {
12469
0
          reloc_count = sec->reloc_count;
12470
0
          if (bed->elf_backend_count_additional_relocs)
12471
0
      {
12472
0
        int c;
12473
0
        c = (*bed->elf_backend_count_additional_relocs) (sec);
12474
0
        additional_reloc_count += c;
12475
0
      }
12476
0
        }
12477
0
      else if (bed->elf_backend_count_relocs)
12478
0
        reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12479
12480
0
      if ((sec->flags & SEC_RELOC) != 0)
12481
0
        {
12482
0
          size_t ext_size = 0;
12483
12484
0
          if (esdi->rel.hdr != NULL)
12485
0
      ext_size = esdi->rel.hdr->sh_size;
12486
0
          if (esdi->rela.hdr != NULL)
12487
0
      ext_size += esdi->rela.hdr->sh_size;
12488
12489
0
          if (ext_size > max_external_reloc_size)
12490
0
      max_external_reloc_size = ext_size;
12491
0
          if (sec->reloc_count > max_internal_reloc_count)
12492
0
      max_internal_reloc_count = sec->reloc_count;
12493
0
        }
12494
0
    }
12495
0
      }
12496
12497
0
    if (reloc_count == 0)
12498
0
      continue;
12499
12500
0
    reloc_count += additional_reloc_count;
12501
0
    o->reloc_count += reloc_count;
12502
12503
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12504
0
      {
12505
0
        if (esdi->rel.hdr)
12506
0
    {
12507
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12508
0
      esdo->rel.count += additional_reloc_count;
12509
0
    }
12510
0
        if (esdi->rela.hdr)
12511
0
    {
12512
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12513
0
      esdo->rela.count += additional_reloc_count;
12514
0
    }
12515
0
      }
12516
0
    else
12517
0
      {
12518
0
        if (o->use_rela_p)
12519
0
    esdo->rela.count += reloc_count;
12520
0
        else
12521
0
    esdo->rel.count += reloc_count;
12522
0
      }
12523
0
  }
12524
12525
0
      if (o->reloc_count > 0)
12526
0
  o->flags |= SEC_RELOC;
12527
0
      else
12528
0
  {
12529
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12530
       set it (this is probably a bug) and if it is set
12531
       assign_section_numbers will create a reloc section.  */
12532
0
    o->flags &=~ SEC_RELOC;
12533
0
  }
12534
12535
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12536
   zero.  This is done in elf_fake_sections as well, but forcing
12537
   the VMA to 0 here will ensure that relocs against these
12538
   sections are handled correctly.  */
12539
0
      if ((o->flags & SEC_ALLOC) == 0
12540
0
    && ! o->user_set_vma)
12541
0
  o->vma = 0;
12542
0
    }
12543
12544
0
  if (! bfd_link_relocatable (info) && merged)
12545
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12546
12547
  /* Figure out the file positions for everything but the symbol table
12548
     and the relocs.  We set symcount to force assign_section_numbers
12549
     to create a symbol table.  */
12550
0
  abfd->symcount = info->strip != strip_all || emit_relocs;
12551
0
  BFD_ASSERT (! abfd->output_has_begun);
12552
0
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
12553
0
    goto error_return;
12554
12555
  /* Set sizes, and assign file positions for reloc sections.  */
12556
0
  for (o = abfd->sections; o != NULL; o = o->next)
12557
0
    {
12558
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12559
0
      if ((o->flags & SEC_RELOC) != 0)
12560
0
  {
12561
0
    if (esdo->rel.hdr
12562
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12563
0
      goto error_return;
12564
12565
0
    if (esdo->rela.hdr
12566
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12567
0
      goto error_return;
12568
0
  }
12569
12570
      /* _bfd_elf_compute_section_file_positions makes temporary use
12571
   of target_index.  Reset it.  */
12572
0
      o->target_index = 0;
12573
12574
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12575
   to count upwards while actually outputting the relocations.  */
12576
0
      esdo->rel.count = 0;
12577
0
      esdo->rela.count = 0;
12578
12579
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12580
0
    && !bfd_section_is_ctf (o))
12581
0
  {
12582
    /* Cache the section contents so that they can be compressed
12583
       later.  Use bfd_malloc since it will be freed by
12584
       bfd_compress_section_contents.  */
12585
0
    unsigned char *contents = esdo->this_hdr.contents;
12586
0
    if (contents != NULL)
12587
0
      abort ();
12588
0
    contents
12589
0
      = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12590
0
    if (contents == NULL)
12591
0
      goto error_return;
12592
0
    esdo->this_hdr.contents = contents;
12593
0
  }
12594
0
    }
12595
12596
  /* We have now assigned file positions for all the sections except .symtab,
12597
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
12598
     the .symtab section at the current file position, and write directly to it.
12599
     We build the .strtab section in memory.  */
12600
0
  abfd->symcount = 0;
12601
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12602
  /* sh_name is set in prep_headers.  */
12603
0
  symtab_hdr->sh_type = SHT_SYMTAB;
12604
  /* sh_flags, sh_addr and sh_size all start off zero.  */
12605
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12606
  /* sh_link is set in assign_section_numbers.  */
12607
  /* sh_info is set below.  */
12608
  /* sh_offset is set just below.  */
12609
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12610
12611
0
  if (max_sym_count < 20)
12612
0
    max_sym_count = 20;
12613
0
  htab->strtabsize = max_sym_count;
12614
0
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
12615
0
  htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12616
0
  if (htab->strtab == NULL)
12617
0
    goto error_return;
12618
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
12619
0
  flinfo.symshndxbuf
12620
0
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12621
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
12622
12623
0
  if (info->strip != strip_all || emit_relocs)
12624
0
    {
12625
0
      file_ptr off = elf_next_file_pos (abfd);
12626
12627
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12628
12629
      /* Note that at this point elf_next_file_pos (abfd) is
12630
   incorrect.  We do not yet know the size of the .symtab section.
12631
   We correct next_file_pos below, after we do know the size.  */
12632
12633
      /* Start writing out the symbol table.  The first symbol is always a
12634
   dummy symbol.  */
12635
0
      elfsym.st_value = 0;
12636
0
      elfsym.st_size = 0;
12637
0
      elfsym.st_info = 0;
12638
0
      elfsym.st_other = 0;
12639
0
      elfsym.st_shndx = SHN_UNDEF;
12640
0
      elfsym.st_target_internal = 0;
12641
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12642
0
             bfd_und_section_ptr, NULL) != 1)
12643
0
  goto error_return;
12644
12645
      /* Output a symbol for each section if asked or they are used for
12646
   relocs.  These symbols usually have no names.  We store the
12647
   index of each one in the index field of the section, so that
12648
   we can find it again when outputting relocs.  */
12649
12650
0
      if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12651
0
  {
12652
0
    bool name_local_sections
12653
0
      = (bed->elf_backend_name_local_section_symbols
12654
0
         && bed->elf_backend_name_local_section_symbols (abfd));
12655
0
    const char *name = NULL;
12656
12657
0
    elfsym.st_size = 0;
12658
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12659
0
    elfsym.st_other = 0;
12660
0
    elfsym.st_value = 0;
12661
0
    elfsym.st_target_internal = 0;
12662
0
    for (i = 1; i < elf_numsections (abfd); i++)
12663
0
      {
12664
0
        o = bfd_section_from_elf_index (abfd, i);
12665
0
        if (o != NULL)
12666
0
    {
12667
0
      o->target_index = bfd_get_symcount (abfd);
12668
0
      elfsym.st_shndx = i;
12669
0
      if (!bfd_link_relocatable (info))
12670
0
        elfsym.st_value = o->vma;
12671
0
      if (name_local_sections)
12672
0
        name = o->name;
12673
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12674
0
             NULL) != 1)
12675
0
        goto error_return;
12676
0
    }
12677
0
      }
12678
0
  }
12679
0
    }
12680
12681
  /* On some targets like Irix 5 the symbol split between local and global
12682
     ones recorded in the sh_info field needs to be done between section
12683
     and all other symbols.  */
12684
0
  if (bed->elf_backend_elfsym_local_is_section
12685
0
      && bed->elf_backend_elfsym_local_is_section (abfd))
12686
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
12687
12688
  /* Allocate some memory to hold information read in from the input
12689
     files.  */
12690
0
  if (max_contents_size != 0)
12691
0
    {
12692
0
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12693
0
      if (flinfo.contents == NULL)
12694
0
  goto error_return;
12695
0
    }
12696
12697
0
  if (max_external_reloc_size != 0)
12698
0
    {
12699
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12700
0
      if (flinfo.external_relocs == NULL)
12701
0
  goto error_return;
12702
0
    }
12703
12704
0
  if (max_internal_reloc_count != 0)
12705
0
    {
12706
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12707
0
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12708
0
      if (flinfo.internal_relocs == NULL)
12709
0
  goto error_return;
12710
0
    }
12711
12712
0
  if (max_sym_count != 0)
12713
0
    {
12714
0
      amt = max_sym_count * bed->s->sizeof_sym;
12715
0
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12716
0
      if (flinfo.external_syms == NULL)
12717
0
  goto error_return;
12718
12719
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
12720
0
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12721
0
      if (flinfo.internal_syms == NULL)
12722
0
  goto error_return;
12723
12724
0
      amt = max_sym_count * sizeof (long);
12725
0
      flinfo.indices = (long int *) bfd_malloc (amt);
12726
0
      if (flinfo.indices == NULL)
12727
0
  goto error_return;
12728
12729
0
      amt = max_sym_count * sizeof (asection *);
12730
0
      flinfo.sections = (asection **) bfd_malloc (amt);
12731
0
      if (flinfo.sections == NULL)
12732
0
  goto error_return;
12733
0
    }
12734
12735
0
  if (max_sym_shndx_count != 0)
12736
0
    {
12737
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12738
0
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12739
0
      if (flinfo.locsym_shndx == NULL)
12740
0
  goto error_return;
12741
0
    }
12742
12743
0
  if (htab->tls_sec)
12744
0
    {
12745
0
      bfd_vma base, end = 0;  /* Both bytes.  */
12746
0
      asection *sec;
12747
12748
0
      for (sec = htab->tls_sec;
12749
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
12750
0
     sec = sec->next)
12751
0
  {
12752
0
    bfd_size_type size = sec->size;
12753
0
    unsigned int opb = bfd_octets_per_byte (abfd, sec);
12754
12755
0
    if (size == 0
12756
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
12757
0
      {
12758
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
12759
12760
0
        if (ord != NULL)
12761
0
    size = ord->offset * opb + ord->size;
12762
0
      }
12763
0
    end = sec->vma + size / opb;
12764
0
  }
12765
0
      base = htab->tls_sec->vma;
12766
      /* Only align end of TLS section if static TLS doesn't have special
12767
   alignment requirements.  */
12768
0
      if (bed->static_tls_alignment == 1)
12769
0
  end = align_power (end, htab->tls_sec->alignment_power);
12770
0
      htab->tls_size = end - base;
12771
0
    }
12772
12773
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
12774
0
    return false;
12775
12776
  /* Finish relative relocations here after regular symbol processing
12777
     is finished if DT_RELR is enabled.  */
12778
0
  if (info->enable_dt_relr
12779
0
      && bed->finish_relative_relocs
12780
0
      && !bed->finish_relative_relocs (info))
12781
0
    info->callbacks->einfo
12782
0
      (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12783
12784
  /* Since ELF permits relocations to be against local symbols, we
12785
     must have the local symbols available when we do the relocations.
12786
     Since we would rather only read the local symbols once, and we
12787
     would rather not keep them in memory, we handle all the
12788
     relocations for a single input file at the same time.
12789
12790
     Unfortunately, there is no way to know the total number of local
12791
     symbols until we have seen all of them, and the local symbol
12792
     indices precede the global symbol indices.  This means that when
12793
     we are generating relocatable output, and we see a reloc against
12794
     a global symbol, we can not know the symbol index until we have
12795
     finished examining all the local symbols to see which ones we are
12796
     going to output.  To deal with this, we keep the relocations in
12797
     memory, and don't output them until the end of the link.  This is
12798
     an unfortunate waste of memory, but I don't see a good way around
12799
     it.  Fortunately, it only happens when performing a relocatable
12800
     link, which is not the common case.  FIXME: If keep_memory is set
12801
     we could write the relocs out and then read them again; I don't
12802
     know how bad the memory loss will be.  */
12803
12804
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12805
0
    sub->output_has_begun = false;
12806
0
  for (o = abfd->sections; o != NULL; o = o->next)
12807
0
    {
12808
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12809
0
  {
12810
0
    if (p->type == bfd_indirect_link_order
12811
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12812
0
      == bfd_target_elf_flavour)
12813
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12814
0
      {
12815
0
        if (! sub->output_has_begun)
12816
0
    {
12817
0
      if (! elf_link_input_bfd (&flinfo, sub))
12818
0
        goto error_return;
12819
0
      sub->output_has_begun = true;
12820
0
    }
12821
0
      }
12822
0
    else if (p->type == bfd_section_reloc_link_order
12823
0
       || p->type == bfd_symbol_reloc_link_order)
12824
0
      {
12825
0
        if (! elf_reloc_link_order (abfd, info, o, p))
12826
0
    goto error_return;
12827
0
      }
12828
0
    else
12829
0
      {
12830
0
        if (! _bfd_default_link_order (abfd, info, o, p))
12831
0
    {
12832
0
      if (p->type == bfd_indirect_link_order
12833
0
          && (bfd_get_flavour (sub)
12834
0
        == bfd_target_elf_flavour)
12835
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
12836
0
        != bed->s->elfclass))
12837
0
        {
12838
0
          const char *iclass, *oclass;
12839
12840
0
          switch (bed->s->elfclass)
12841
0
      {
12842
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
12843
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
12844
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12845
0
      default: abort ();
12846
0
      }
12847
12848
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12849
0
      {
12850
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
12851
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
12852
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12853
0
      default: abort ();
12854
0
      }
12855
12856
0
          bfd_set_error (bfd_error_wrong_format);
12857
0
          _bfd_error_handler
12858
      /* xgettext:c-format */
12859
0
      (_("%pB: file class %s incompatible with %s"),
12860
0
       sub, iclass, oclass);
12861
0
        }
12862
12863
0
      goto error_return;
12864
0
    }
12865
0
      }
12866
0
  }
12867
0
    }
12868
12869
  /* Free symbol buffer if needed.  */
12870
0
  if (!info->reduce_memory_overheads)
12871
0
    {
12872
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12873
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12874
0
    {
12875
0
      free (elf_tdata (sub)->symbuf);
12876
0
      elf_tdata (sub)->symbuf = NULL;
12877
0
    }
12878
0
    }
12879
12880
0
  ret = true;
12881
12882
  /* Output any global symbols that got converted to local in a
12883
     version script or due to symbol visibility.  We do this in a
12884
     separate step since ELF requires all local symbols to appear
12885
     prior to any global symbols.  FIXME: We should only do this if
12886
     some global symbols were, in fact, converted to become local.
12887
     FIXME: Will this work correctly with the Irix 5 linker?  */
12888
0
  eoinfo.failed = false;
12889
0
  eoinfo.flinfo = &flinfo;
12890
0
  eoinfo.localsyms = true;
12891
0
  eoinfo.file_sym_done = false;
12892
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12893
0
  if (eoinfo.failed)
12894
0
    {
12895
0
      ret = false;
12896
0
      goto return_local_hash_table;
12897
0
    }
12898
12899
  /* If backend needs to output some local symbols not present in the hash
12900
     table, do it now.  */
12901
0
  if (bed->elf_backend_output_arch_local_syms)
12902
0
    {
12903
0
      if (! ((*bed->elf_backend_output_arch_local_syms)
12904
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
12905
0
  {
12906
0
    ret = false;
12907
0
    goto return_local_hash_table;
12908
0
  }
12909
0
    }
12910
12911
  /* That wrote out all the local symbols.  Finish up the symbol table
12912
     with the global symbols. Even if we want to strip everything we
12913
     can, we still need to deal with those global symbols that got
12914
     converted to local in a version script.  */
12915
12916
  /* The sh_info field records the index of the first non local symbol.  */
12917
0
  if (!symtab_hdr->sh_info)
12918
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
12919
12920
0
  if (dynamic
12921
0
      && htab->dynsym != NULL
12922
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
12923
0
    {
12924
0
      Elf_Internal_Sym sym;
12925
0
      bfd_byte *dynsym = htab->dynsym->contents;
12926
12927
0
      o = htab->dynsym->output_section;
12928
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12929
12930
      /* Write out the section symbols for the output sections.  */
12931
0
      if (bfd_link_pic (info)
12932
0
    || htab->is_relocatable_executable)
12933
0
  {
12934
0
    asection *s;
12935
12936
0
    sym.st_size = 0;
12937
0
    sym.st_name = 0;
12938
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12939
0
    sym.st_other = 0;
12940
0
    sym.st_target_internal = 0;
12941
12942
0
    for (s = abfd->sections; s != NULL; s = s->next)
12943
0
      {
12944
0
        int indx;
12945
0
        bfd_byte *dest;
12946
0
        long dynindx;
12947
12948
0
        dynindx = elf_section_data (s)->dynindx;
12949
0
        if (dynindx <= 0)
12950
0
    continue;
12951
0
        indx = elf_section_data (s)->this_idx;
12952
0
        BFD_ASSERT (indx > 0);
12953
0
        sym.st_shndx = indx;
12954
0
        if (! check_dynsym (abfd, &sym))
12955
0
    {
12956
0
      ret = false;
12957
0
      goto return_local_hash_table;
12958
0
    }
12959
0
        sym.st_value = s->vma;
12960
0
        dest = dynsym + dynindx * bed->s->sizeof_sym;
12961
12962
        /* Inform the linker of the addition of this symbol.  */
12963
12964
0
        if (info->callbacks->ctf_new_dynsym)
12965
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
12966
12967
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12968
0
      }
12969
0
  }
12970
12971
      /* Write out the local dynsyms.  */
12972
0
      if (htab->dynlocal)
12973
0
  {
12974
0
    struct elf_link_local_dynamic_entry *e;
12975
0
    for (e = htab->dynlocal; e ; e = e->next)
12976
0
      {
12977
0
        asection *s;
12978
0
        bfd_byte *dest;
12979
12980
        /* Copy the internal symbol and turn off visibility.
12981
     Note that we saved a word of storage and overwrote
12982
     the original st_name with the dynstr_index.  */
12983
0
        sym = e->isym;
12984
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12985
0
        sym.st_shndx = SHN_UNDEF;
12986
12987
0
        s = bfd_section_from_elf_index (e->input_bfd,
12988
0
                e->isym.st_shndx);
12989
0
        if (s != NULL
12990
0
      && s->output_section != NULL
12991
0
      && elf_section_data (s->output_section) != NULL)
12992
0
    {
12993
0
      sym.st_shndx =
12994
0
        elf_section_data (s->output_section)->this_idx;
12995
0
      if (! check_dynsym (abfd, &sym))
12996
0
        {
12997
0
          ret = false;
12998
0
          goto return_local_hash_table;
12999
0
        }
13000
0
      sym.st_value = (s->output_section->vma
13001
0
          + s->output_offset
13002
0
          + e->isym.st_value);
13003
0
    }
13004
13005
        /* Inform the linker of the addition of this symbol.  */
13006
13007
0
        if (info->callbacks->ctf_new_dynsym)
13008
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13009
13010
0
        dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13011
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13012
0
      }
13013
0
  }
13014
0
    }
13015
13016
  /* We get the global symbols from the hash table.  */
13017
0
  eoinfo.failed = false;
13018
0
  eoinfo.localsyms = false;
13019
0
  eoinfo.flinfo = &flinfo;
13020
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13021
0
  if (eoinfo.failed)
13022
0
    {
13023
0
      ret = false;
13024
0
      goto return_local_hash_table;
13025
0
    }
13026
13027
  /* If backend needs to output some symbols not present in the hash
13028
     table, do it now.  */
13029
0
  if (bed->elf_backend_output_arch_syms
13030
0
      && (info->strip != strip_all || emit_relocs))
13031
0
    {
13032
0
      if (! ((*bed->elf_backend_output_arch_syms)
13033
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13034
0
  {
13035
0
    ret = false;
13036
0
    goto return_local_hash_table;
13037
0
  }
13038
0
    }
13039
13040
  /* Finalize the .strtab section.  */
13041
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13042
13043
  /* Swap out the .strtab section. */
13044
0
  if (!elf_link_swap_symbols_out (&flinfo))
13045
0
    {
13046
0
      ret = false;
13047
0
      goto return_local_hash_table;
13048
0
    }
13049
13050
  /* Now we know the size of the symtab section.  */
13051
0
  if (bfd_get_symcount (abfd) > 0)
13052
0
    {
13053
      /* Finish up and write out the symbol string table (.strtab)
13054
   section.  */
13055
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13056
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13057
13058
0
      if (elf_symtab_shndx_list (abfd))
13059
0
  {
13060
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13061
13062
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13063
0
      {
13064
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13065
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13066
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13067
0
        amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13068
0
        symtab_shndx_hdr->sh_size = amt;
13069
13070
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13071
0
                     off, true);
13072
13073
0
        if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13074
0
      || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13075
0
    {
13076
0
      ret = false;
13077
0
      goto return_local_hash_table;
13078
0
    }
13079
0
      }
13080
0
  }
13081
13082
0
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13083
      /* sh_name was set in prep_headers.  */
13084
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13085
0
      symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13086
0
      symstrtab_hdr->sh_addr = 0;
13087
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13088
0
      symstrtab_hdr->sh_entsize = 0;
13089
0
      symstrtab_hdr->sh_link = 0;
13090
0
      symstrtab_hdr->sh_info = 0;
13091
      /* sh_offset is set just below.  */
13092
0
      symstrtab_hdr->sh_addralign = 1;
13093
13094
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13095
0
                   off, true);
13096
0
      elf_next_file_pos (abfd) = off;
13097
13098
0
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13099
0
    || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13100
0
  {
13101
0
    ret = false;
13102
0
    goto return_local_hash_table;
13103
0
  }
13104
0
    }
13105
13106
0
  if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13107
0
    {
13108
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13109
0
        info->out_implib_bfd);
13110
0
      ret = false;
13111
0
      goto return_local_hash_table;
13112
0
    }
13113
13114
  /* Adjust the relocs to have the correct symbol indices.  */
13115
0
  for (o = abfd->sections; o != NULL; o = o->next)
13116
0
    {
13117
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13118
0
      bool sort;
13119
13120
0
      if ((o->flags & SEC_RELOC) == 0)
13121
0
  continue;
13122
13123
0
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13124
0
      if (esdo->rel.hdr != NULL
13125
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13126
0
  {
13127
0
    ret = false;
13128
0
    goto return_local_hash_table;
13129
0
  }
13130
0
      if (esdo->rela.hdr != NULL
13131
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13132
0
  {
13133
0
    ret = false;
13134
0
    goto return_local_hash_table;
13135
0
  }
13136
13137
      /* Set the reloc_count field to 0 to prevent write_relocs from
13138
   trying to swap the relocs out itself.  */
13139
0
      o->reloc_count = 0;
13140
0
    }
13141
13142
0
  relativecount = 0;
13143
0
  if (dynamic && info->combreloc && dynobj != NULL)
13144
0
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13145
13146
0
  relr_entsize = 0;
13147
0
  if (htab->srelrdyn != NULL
13148
0
      && htab->srelrdyn->output_section != NULL
13149
0
      && htab->srelrdyn->size != 0)
13150
0
    {
13151
0
      asection *s = htab->srelrdyn->output_section;
13152
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13153
0
      if (relr_entsize == 0)
13154
0
  {
13155
0
    relr_entsize = bed->s->arch_size / 8;
13156
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13157
0
  }
13158
0
    }
13159
13160
  /* If we are linking against a dynamic object, or generating a
13161
     shared library, finish up the dynamic linking information.  */
13162
0
  if (dynamic)
13163
0
    {
13164
0
      bfd_byte *dyncon, *dynconend;
13165
13166
      /* Fix up .dynamic entries.  */
13167
0
      o = bfd_get_linker_section (dynobj, ".dynamic");
13168
0
      BFD_ASSERT (o != NULL);
13169
13170
0
      dyncon = o->contents;
13171
0
      dynconend = PTR_ADD (o->contents, o->size);
13172
0
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13173
0
  {
13174
0
    Elf_Internal_Dyn dyn;
13175
0
    const char *name;
13176
0
    unsigned int type;
13177
0
    bfd_size_type sh_size;
13178
0
    bfd_vma sh_addr;
13179
13180
0
    bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13181
13182
0
    switch (dyn.d_tag)
13183
0
      {
13184
0
      default:
13185
0
        continue;
13186
0
      case DT_NULL:
13187
0
        if (relativecount != 0)
13188
0
    {
13189
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13190
0
        {
13191
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13192
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13193
0
        }
13194
0
      if (dyn.d_tag != DT_NULL
13195
0
          && dynconend - dyncon >= bed->s->sizeof_dyn)
13196
0
        {
13197
0
          dyn.d_un.d_val = relativecount;
13198
0
          relativecount = 0;
13199
0
          break;
13200
0
        }
13201
0
      relativecount = 0;
13202
0
    }
13203
0
        if (relr_entsize != 0)
13204
0
    {
13205
0
      if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13206
0
        {
13207
0
          asection *s = htab->srelrdyn;
13208
0
          dyn.d_tag = DT_RELR;
13209
0
          dyn.d_un.d_ptr
13210
0
      = s->output_section->vma + s->output_offset;
13211
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13212
0
          dyncon += bed->s->sizeof_dyn;
13213
13214
0
          dyn.d_tag = DT_RELRSZ;
13215
0
          dyn.d_un.d_val = s->size;
13216
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13217
0
          dyncon += bed->s->sizeof_dyn;
13218
13219
0
          dyn.d_tag = DT_RELRENT;
13220
0
          dyn.d_un.d_val = relr_entsize;
13221
0
          relr_entsize = 0;
13222
0
          break;
13223
0
        }
13224
0
      relr_entsize = 0;
13225
0
    }
13226
0
        continue;
13227
13228
0
      case DT_INIT:
13229
0
        name = info->init_function;
13230
0
        goto get_sym;
13231
0
      case DT_FINI:
13232
0
        name = info->fini_function;
13233
0
      get_sym:
13234
0
        {
13235
0
    struct elf_link_hash_entry *h;
13236
13237
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13238
0
    if (h != NULL
13239
0
        && (h->root.type == bfd_link_hash_defined
13240
0
      || h->root.type == bfd_link_hash_defweak))
13241
0
      {
13242
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13243
0
        o = h->root.u.def.section;
13244
0
        if (o->output_section != NULL)
13245
0
          dyn.d_un.d_ptr += (o->output_section->vma
13246
0
           + o->output_offset);
13247
0
        else
13248
0
          {
13249
      /* The symbol is imported from another shared
13250
         library and does not apply to this one.  */
13251
0
      dyn.d_un.d_ptr = 0;
13252
0
          }
13253
0
        break;
13254
0
      }
13255
0
        }
13256
0
        continue;
13257
13258
0
      case DT_PREINIT_ARRAYSZ:
13259
0
        name = ".preinit_array";
13260
0
        goto get_out_size;
13261
0
      case DT_INIT_ARRAYSZ:
13262
0
        name = ".init_array";
13263
0
        goto get_out_size;
13264
0
      case DT_FINI_ARRAYSZ:
13265
0
        name = ".fini_array";
13266
0
      get_out_size:
13267
0
        o = bfd_get_section_by_name (abfd, name);
13268
0
        if (o == NULL)
13269
0
    {
13270
0
      _bfd_error_handler
13271
0
        (_("could not find section %s"), name);
13272
0
      goto error_return;
13273
0
    }
13274
0
        if (o->size == 0)
13275
0
    _bfd_error_handler
13276
0
      (_("warning: %s section has zero size"), name);
13277
0
        dyn.d_un.d_val = o->size;
13278
0
        break;
13279
13280
0
      case DT_PREINIT_ARRAY:
13281
0
        name = ".preinit_array";
13282
0
        goto get_out_vma;
13283
0
      case DT_INIT_ARRAY:
13284
0
        name = ".init_array";
13285
0
        goto get_out_vma;
13286
0
      case DT_FINI_ARRAY:
13287
0
        name = ".fini_array";
13288
0
      get_out_vma:
13289
0
        o = bfd_get_section_by_name (abfd, name);
13290
0
        goto do_vma;
13291
13292
0
      case DT_HASH:
13293
0
        name = ".hash";
13294
0
        goto get_vma;
13295
0
      case DT_GNU_HASH:
13296
0
        name = ".gnu.hash";
13297
0
        goto get_vma;
13298
0
      case DT_STRTAB:
13299
0
        name = ".dynstr";
13300
0
        goto get_vma;
13301
0
      case DT_SYMTAB:
13302
0
        name = ".dynsym";
13303
0
        goto get_vma;
13304
0
      case DT_VERDEF:
13305
0
        name = ".gnu.version_d";
13306
0
        goto get_vma;
13307
0
      case DT_VERNEED:
13308
0
        name = ".gnu.version_r";
13309
0
        goto get_vma;
13310
0
      case DT_VERSYM:
13311
0
        name = ".gnu.version";
13312
0
      get_vma:
13313
0
        o = bfd_get_linker_section (dynobj, name);
13314
0
      do_vma:
13315
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13316
0
    {
13317
0
      _bfd_error_handler
13318
0
        (_("could not find section %s"), name);
13319
0
      goto error_return;
13320
0
    }
13321
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13322
0
    {
13323
0
      _bfd_error_handler
13324
0
        (_("warning: section '%s' is being made into a note"), name);
13325
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13326
0
      goto error_return;
13327
0
    }
13328
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13329
0
        break;
13330
13331
0
      case DT_REL:
13332
0
      case DT_RELA:
13333
0
      case DT_RELSZ:
13334
0
      case DT_RELASZ:
13335
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13336
0
    type = SHT_REL;
13337
0
        else
13338
0
    type = SHT_RELA;
13339
0
        sh_size = 0;
13340
0
        sh_addr = 0;
13341
0
        for (i = 1; i < elf_numsections (abfd); i++)
13342
0
    {
13343
0
      Elf_Internal_Shdr *hdr;
13344
13345
0
      hdr = elf_elfsections (abfd)[i];
13346
0
      if (hdr->sh_type == type
13347
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13348
0
        {
13349
0
          sh_size += hdr->sh_size;
13350
0
          if (sh_addr == 0
13351
0
        || sh_addr > hdr->sh_addr)
13352
0
      sh_addr = hdr->sh_addr;
13353
0
        }
13354
0
    }
13355
13356
0
        if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13357
0
    {
13358
0
      unsigned int opb = bfd_octets_per_byte (abfd, o);
13359
13360
      /* Don't count procedure linkage table relocs in the
13361
         overall reloc count.  */
13362
0
      sh_size -= htab->srelplt->size;
13363
0
      if (sh_size == 0)
13364
        /* If the size is zero, make the address zero too.
13365
           This is to avoid a glibc bug.  If the backend
13366
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13367
           zero, then we'll put DT_RELA at the end of
13368
           DT_JMPREL.  glibc will interpret the end of
13369
           DT_RELA matching the end of DT_JMPREL as the
13370
           case where DT_RELA includes DT_JMPREL, and for
13371
           LD_BIND_NOW will decide that processing DT_RELA
13372
           will process the PLT relocs too.  Net result:
13373
           No PLT relocs applied.  */
13374
0
        sh_addr = 0;
13375
13376
      /* If .rela.plt is the first .rela section, exclude
13377
         it from DT_RELA.  */
13378
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13379
0
               + htab->srelplt->output_offset) * opb)
13380
0
        sh_addr += htab->srelplt->size;
13381
0
    }
13382
13383
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13384
0
    dyn.d_un.d_val = sh_size;
13385
0
        else
13386
0
    dyn.d_un.d_ptr = sh_addr;
13387
0
        break;
13388
0
      }
13389
0
    bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13390
0
  }
13391
0
    }
13392
13393
  /* If we have created any dynamic sections, then output them.  */
13394
0
  if (dynobj != NULL)
13395
0
    {
13396
0
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13397
0
  goto error_return;
13398
13399
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13400
0
      if (bfd_link_textrel_check (info)
13401
0
    && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13402
0
    && o->size != 0)
13403
0
  {
13404
0
    bfd_byte *dyncon, *dynconend;
13405
13406
0
    dyncon = o->contents;
13407
0
    dynconend = o->contents + o->size;
13408
0
    for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13409
0
      {
13410
0
        Elf_Internal_Dyn dyn;
13411
13412
0
        bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13413
13414
0
        if (dyn.d_tag == DT_TEXTREL)
13415
0
    {
13416
0
      if (info->textrel_check == textrel_check_error)
13417
0
        info->callbacks->einfo
13418
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13419
0
      else if (bfd_link_dll (info))
13420
0
        info->callbacks->einfo
13421
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13422
0
      else if (bfd_link_pde (info))
13423
0
        info->callbacks->einfo
13424
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13425
0
      else
13426
0
        info->callbacks->einfo
13427
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13428
0
      break;
13429
0
    }
13430
0
      }
13431
0
  }
13432
13433
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13434
0
  {
13435
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13436
0
        || o->size == 0
13437
0
        || o->output_section == bfd_abs_section_ptr)
13438
0
      continue;
13439
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13440
0
      {
13441
        /* At this point, we are only interested in sections
13442
     created by _bfd_elf_link_create_dynamic_sections.  */
13443
0
        continue;
13444
0
      }
13445
0
    if (htab->stab_info.stabstr == o)
13446
0
      continue;
13447
0
    if (htab->eh_info.hdr_sec == o)
13448
0
      continue;
13449
0
    if (strcmp (o->name, ".dynstr") != 0)
13450
0
      {
13451
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13452
0
              * bfd_octets_per_byte (abfd, o));
13453
0
        if (!bfd_set_section_contents (abfd, o->output_section,
13454
0
               o->contents, octets, o->size))
13455
0
    goto error_return;
13456
0
      }
13457
0
    else
13458
0
      {
13459
        /* The contents of the .dynstr section are actually in a
13460
     stringtab.  */
13461
0
        file_ptr off;
13462
13463
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13464
0
        if (bfd_seek (abfd, off, SEEK_SET) != 0
13465
0
      || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13466
0
    goto error_return;
13467
0
      }
13468
0
  }
13469
0
    }
13470
13471
0
  if (!info->resolve_section_groups)
13472
0
    {
13473
0
      bool failed = false;
13474
13475
0
      BFD_ASSERT (bfd_link_relocatable (info));
13476
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13477
0
      if (failed)
13478
0
  goto error_return;
13479
0
    }
13480
13481
  /* If we have optimized stabs strings, output them.  */
13482
0
  if (htab->stab_info.stabstr != NULL)
13483
0
    {
13484
0
      if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13485
0
  goto error_return;
13486
0
    }
13487
13488
0
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13489
0
    goto error_return;
13490
13491
0
  if (! _bfd_elf_write_section_sframe (abfd, info))
13492
0
    goto error_return;
13493
13494
0
  if (info->callbacks->emit_ctf)
13495
0
      info->callbacks->emit_ctf ();
13496
13497
0
  elf_final_link_free (abfd, &flinfo);
13498
13499
0
  if (attr_section)
13500
0
    {
13501
0
      bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13502
0
      if (contents == NULL)
13503
0
  {
13504
    /* Bail out and fail.  */
13505
0
    ret = false;
13506
0
    goto return_local_hash_table;
13507
0
  }
13508
0
      bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13509
0
      bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13510
0
      free (contents);
13511
0
    }
13512
13513
0
 return_local_hash_table:
13514
0
  if (info->unique_symbol)
13515
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13516
0
  return ret;
13517
13518
0
 error_return:
13519
0
  elf_final_link_free (abfd, &flinfo);
13520
0
  ret = false;
13521
0
  goto return_local_hash_table;
13522
0
}
13523

13524
/* Initialize COOKIE for input bfd ABFD.  */
13525
13526
static bool
13527
init_reloc_cookie (struct elf_reloc_cookie *cookie,
13528
       struct bfd_link_info *info, bfd *abfd)
13529
0
{
13530
0
  Elf_Internal_Shdr *symtab_hdr;
13531
0
  const struct elf_backend_data *bed;
13532
13533
0
  bed = get_elf_backend_data (abfd);
13534
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13535
13536
0
  cookie->abfd = abfd;
13537
0
  cookie->sym_hashes = elf_sym_hashes (abfd);
13538
0
  cookie->bad_symtab = elf_bad_symtab (abfd);
13539
0
  if (cookie->bad_symtab)
13540
0
    {
13541
0
      cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13542
0
      cookie->extsymoff = 0;
13543
0
    }
13544
0
  else
13545
0
    {
13546
0
      cookie->locsymcount = symtab_hdr->sh_info;
13547
0
      cookie->extsymoff = symtab_hdr->sh_info;
13548
0
    }
13549
13550
0
  if (bed->s->arch_size == 32)
13551
0
    cookie->r_sym_shift = 8;
13552
0
  else
13553
0
    cookie->r_sym_shift = 32;
13554
13555
0
  cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13556
0
  if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13557
0
    {
13558
0
      cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13559
0
                cookie->locsymcount, 0,
13560
0
                NULL, NULL, NULL);
13561
0
      if (cookie->locsyms == NULL)
13562
0
  {
13563
0
    info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13564
0
    return false;
13565
0
  }
13566
0
      if (_bfd_link_keep_memory (info) )
13567
0
  {
13568
0
    symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13569
0
    info->cache_size += (cookie->locsymcount
13570
0
             * sizeof (Elf_External_Sym_Shndx));
13571
0
  }
13572
0
    }
13573
0
  return true;
13574
0
}
13575
13576
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13577
13578
static void
13579
fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13580
0
{
13581
0
  Elf_Internal_Shdr *symtab_hdr;
13582
13583
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13584
0
  if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13585
0
    free (cookie->locsyms);
13586
0
}
13587
13588
/* Initialize the relocation information in COOKIE for input section SEC
13589
   of input bfd ABFD.  */
13590
13591
static bool
13592
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13593
      struct bfd_link_info *info, bfd *abfd,
13594
      asection *sec)
13595
0
{
13596
0
  if (sec->reloc_count == 0)
13597
0
    {
13598
0
      cookie->rels = NULL;
13599
0
      cookie->relend = NULL;
13600
0
    }
13601
0
  else
13602
0
    {
13603
0
      cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13604
0
                 NULL, NULL,
13605
0
                 _bfd_link_keep_memory (info));
13606
0
      if (cookie->rels == NULL)
13607
0
  return false;
13608
0
      cookie->rel = cookie->rels;
13609
0
      cookie->relend = cookie->rels + sec->reloc_count;
13610
0
    }
13611
0
  cookie->rel = cookie->rels;
13612
0
  return true;
13613
0
}
13614
13615
/* Free the memory allocated by init_reloc_cookie_rels,
13616
   if appropriate.  */
13617
13618
static void
13619
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13620
      asection *sec)
13621
0
{
13622
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13623
0
    free (cookie->rels);
13624
0
}
13625
13626
/* Initialize the whole of COOKIE for input section SEC.  */
13627
13628
static bool
13629
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13630
             struct bfd_link_info *info,
13631
             asection *sec)
13632
0
{
13633
0
  if (!init_reloc_cookie (cookie, info, sec->owner))
13634
0
    goto error1;
13635
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13636
0
    goto error2;
13637
0
  return true;
13638
13639
0
 error2:
13640
0
  fini_reloc_cookie (cookie, sec->owner);
13641
0
 error1:
13642
0
  return false;
13643
0
}
13644
13645
/* Free the memory allocated by init_reloc_cookie_for_section,
13646
   if appropriate.  */
13647
13648
static void
13649
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13650
             asection *sec)
13651
0
{
13652
0
  fini_reloc_cookie_rels (cookie, sec);
13653
0
  fini_reloc_cookie (cookie, sec->owner);
13654
0
}
13655

13656
/* Garbage collect unused sections.  */
13657
13658
/* Default gc_mark_hook.  */
13659
13660
asection *
13661
_bfd_elf_gc_mark_hook (asection *sec,
13662
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
13663
           Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13664
           struct elf_link_hash_entry *h,
13665
           Elf_Internal_Sym *sym)
13666
0
{
13667
0
  if (h != NULL)
13668
0
    {
13669
0
      switch (h->root.type)
13670
0
  {
13671
0
  case bfd_link_hash_defined:
13672
0
  case bfd_link_hash_defweak:
13673
0
    return h->root.u.def.section;
13674
13675
0
  case bfd_link_hash_common:
13676
0
    return h->root.u.c.p->section;
13677
13678
0
  default:
13679
0
    break;
13680
0
  }
13681
0
    }
13682
0
  else
13683
0
    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13684
13685
0
  return NULL;
13686
0
}
13687
13688
/* Return the debug definition section.  */
13689
13690
static asection *
13691
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13692
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
13693
         Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13694
         struct elf_link_hash_entry *h,
13695
         Elf_Internal_Sym *sym)
13696
0
{
13697
0
  if (h != NULL)
13698
0
    {
13699
      /* Return the global debug definition section.  */
13700
0
      if ((h->root.type == bfd_link_hash_defined
13701
0
     || h->root.type == bfd_link_hash_defweak)
13702
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13703
0
  return h->root.u.def.section;
13704
0
    }
13705
0
  else
13706
0
    {
13707
      /* Return the local debug definition section.  */
13708
0
      asection *isec = bfd_section_from_elf_index (sec->owner,
13709
0
               sym->st_shndx);
13710
0
      if ((isec->flags & SEC_DEBUGGING) != 0)
13711
0
  return isec;
13712
0
    }
13713
13714
0
  return NULL;
13715
0
}
13716
13717
/* COOKIE->rel describes a relocation against section SEC, which is
13718
   a section we've decided to keep.  Return the section that contains
13719
   the relocation symbol, or NULL if no section contains it.  */
13720
13721
asection *
13722
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13723
           elf_gc_mark_hook_fn gc_mark_hook,
13724
           struct elf_reloc_cookie *cookie,
13725
           bool *start_stop)
13726
0
{
13727
0
  unsigned long r_symndx;
13728
0
  struct elf_link_hash_entry *h, *hw;
13729
13730
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13731
0
  if (r_symndx == STN_UNDEF)
13732
0
    return NULL;
13733
13734
0
  if (r_symndx >= cookie->locsymcount
13735
0
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13736
0
    {
13737
0
      bool was_marked;
13738
13739
0
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13740
0
      if (h == NULL)
13741
0
  {
13742
0
    info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13743
0
          sec->owner);
13744
0
    return NULL;
13745
0
  }
13746
0
      while (h->root.type == bfd_link_hash_indirect
13747
0
       || h->root.type == bfd_link_hash_warning)
13748
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
13749
13750
0
      was_marked = h->mark;
13751
0
      h->mark = 1;
13752
      /* Keep all aliases of the symbol too.  If an object symbol
13753
   needs to be copied into .dynbss then all of its aliases
13754
   should be present as dynamic symbols, not just the one used
13755
   on the copy relocation.  */
13756
0
      hw = h;
13757
0
      while (hw->is_weakalias)
13758
0
  {
13759
0
    hw = hw->u.alias;
13760
0
    hw->mark = 1;
13761
0
  }
13762
13763
0
      if (!was_marked && h->start_stop && !h->root.ldscript_def)
13764
0
  {
13765
0
    if (info->start_stop_gc)
13766
0
      return NULL;
13767
13768
    /* To work around a glibc bug, mark XXX input sections
13769
       when there is a reference to __start_XXX or __stop_XXX
13770
       symbols.  */
13771
0
    else if (start_stop != NULL)
13772
0
      {
13773
0
        asection *s = h->u2.start_stop_section;
13774
0
        *start_stop = true;
13775
0
        return s;
13776
0
      }
13777
0
  }
13778
13779
0
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13780
0
    }
13781
13782
0
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13783
0
        &cookie->locsyms[r_symndx]);
13784
0
}
13785
13786
/* COOKIE->rel describes a relocation against section SEC, which is
13787
   a section we've decided to keep.  Mark the section that contains
13788
   the relocation symbol.  */
13789
13790
bool
13791
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13792
      asection *sec,
13793
      elf_gc_mark_hook_fn gc_mark_hook,
13794
      struct elf_reloc_cookie *cookie)
13795
0
{
13796
0
  asection *rsec;
13797
0
  bool start_stop = false;
13798
13799
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13800
0
  while (rsec != NULL)
13801
0
    {
13802
0
      if (!rsec->gc_mark)
13803
0
  {
13804
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13805
0
        || (rsec->owner->flags & DYNAMIC) != 0)
13806
0
      rsec->gc_mark = 1;
13807
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13808
0
      return false;
13809
0
  }
13810
0
      if (!start_stop)
13811
0
  break;
13812
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13813
0
    }
13814
0
  return true;
13815
0
}
13816
13817
/* The mark phase of garbage collection.  For a given section, mark
13818
   it and any sections in this section's group, and all the sections
13819
   which define symbols to which it refers.  */
13820
13821
bool
13822
_bfd_elf_gc_mark (struct bfd_link_info *info,
13823
      asection *sec,
13824
      elf_gc_mark_hook_fn gc_mark_hook)
13825
0
{
13826
0
  bool ret;
13827
0
  asection *group_sec, *eh_frame;
13828
13829
0
  sec->gc_mark = 1;
13830
13831
  /* Mark all the sections in the group.  */
13832
0
  group_sec = elf_section_data (sec)->next_in_group;
13833
0
  if (group_sec && !group_sec->gc_mark)
13834
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13835
0
      return false;
13836
13837
  /* Look through the section relocs.  */
13838
0
  ret = true;
13839
0
  eh_frame = elf_eh_frame_section (sec->owner);
13840
0
  if ((sec->flags & SEC_RELOC) != 0
13841
0
      && sec->reloc_count > 0
13842
0
      && sec != eh_frame)
13843
0
    {
13844
0
      struct elf_reloc_cookie cookie;
13845
13846
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec))
13847
0
  ret = false;
13848
0
      else
13849
0
  {
13850
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
13851
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13852
0
        {
13853
0
    ret = false;
13854
0
    break;
13855
0
        }
13856
0
    fini_reloc_cookie_for_section (&cookie, sec);
13857
0
  }
13858
0
    }
13859
13860
0
  if (ret && eh_frame && elf_fde_list (sec))
13861
0
    {
13862
0
      struct elf_reloc_cookie cookie;
13863
13864
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13865
0
  ret = false;
13866
0
      else
13867
0
  {
13868
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13869
0
              gc_mark_hook, &cookie))
13870
0
      ret = false;
13871
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
13872
0
  }
13873
0
    }
13874
13875
0
  eh_frame = elf_section_eh_frame_entry (sec);
13876
0
  if (ret && eh_frame && !eh_frame->gc_mark)
13877
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13878
0
      ret = false;
13879
13880
0
  return ret;
13881
0
}
13882
13883
/* Scan and mark sections in a special or debug section group.  */
13884
13885
static void
13886
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13887
0
{
13888
  /* Point to first section of section group.  */
13889
0
  asection *ssec;
13890
  /* Used to iterate the section group.  */
13891
0
  asection *msec;
13892
13893
0
  bool is_special_grp = true;
13894
0
  bool is_debug_grp = true;
13895
13896
  /* First scan to see if group contains any section other than debug
13897
     and special section.  */
13898
0
  ssec = msec = elf_next_in_group (grp);
13899
0
  do
13900
0
    {
13901
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
13902
0
  is_debug_grp = false;
13903
13904
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13905
0
  is_special_grp = false;
13906
13907
0
      msec = elf_next_in_group (msec);
13908
0
    }
13909
0
  while (msec != ssec);
13910
13911
  /* If this is a pure debug section group or pure special section group,
13912
     keep all sections in this group.  */
13913
0
  if (is_debug_grp || is_special_grp)
13914
0
    {
13915
0
      do
13916
0
  {
13917
0
    msec->gc_mark = 1;
13918
0
    msec = elf_next_in_group (msec);
13919
0
  }
13920
0
      while (msec != ssec);
13921
0
    }
13922
0
}
13923
13924
/* Keep debug and special sections.  */
13925
13926
bool
13927
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13928
         elf_gc_mark_hook_fn mark_hook)
13929
0
{
13930
0
  bfd *ibfd;
13931
13932
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13933
0
    {
13934
0
      asection *isec;
13935
0
      bool some_kept;
13936
0
      bool debug_frag_seen;
13937
0
      bool has_kept_debug_info;
13938
13939
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13940
0
  continue;
13941
0
      isec = ibfd->sections;
13942
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13943
0
  continue;
13944
13945
      /* Ensure all linker created sections are kept,
13946
   see if any other section is already marked,
13947
   and note if we have any fragmented debug sections.  */
13948
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
13949
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13950
0
  {
13951
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
13952
0
      isec->gc_mark = 1;
13953
0
    else if (isec->gc_mark
13954
0
       && (isec->flags & SEC_ALLOC) != 0
13955
0
       && elf_section_type (isec) != SHT_NOTE)
13956
0
      some_kept = true;
13957
0
    else
13958
0
      {
13959
        /* Since all sections, except for backend specific ones,
13960
     have been garbage collected, call mark_hook on this
13961
     section if any of its linked-to sections is marked.  */
13962
0
        asection *linked_to_sec;
13963
0
        for (linked_to_sec = elf_linked_to_section (isec);
13964
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
13965
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
13966
0
    {
13967
0
      if (linked_to_sec->gc_mark)
13968
0
        {
13969
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13970
0
      return false;
13971
0
          break;
13972
0
        }
13973
0
      linked_to_sec->linker_mark = 1;
13974
0
    }
13975
0
        for (linked_to_sec = elf_linked_to_section (isec);
13976
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
13977
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
13978
0
    linked_to_sec->linker_mark = 0;
13979
0
      }
13980
13981
0
    if (!debug_frag_seen
13982
0
        && (isec->flags & SEC_DEBUGGING)
13983
0
        && startswith (isec->name, ".debug_line."))
13984
0
      debug_frag_seen = true;
13985
0
    else if (strcmp (bfd_section_name (isec),
13986
0
         "__patchable_function_entries") == 0
13987
0
       && elf_linked_to_section (isec) == NULL)
13988
0
        info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13989
0
          "need linked-to section "
13990
0
          "for --gc-sections\n"),
13991
0
              isec->owner, isec);
13992
0
  }
13993
13994
      /* If no non-note alloc section in this file will be kept, then
13995
   we can toss out the debug and special sections.  */
13996
0
      if (!some_kept)
13997
0
  continue;
13998
13999
      /* Keep debug and special sections like .comment when they are
14000
   not part of a group.  Also keep section groups that contain
14001
   just debug sections or special sections.  NB: Sections with
14002
   linked-to section has been handled above.  */
14003
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14004
0
  {
14005
0
    if ((isec->flags & SEC_GROUP) != 0)
14006
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14007
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14008
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14009
0
       && elf_next_in_group (isec) == NULL
14010
0
       && elf_linked_to_section (isec) == NULL)
14011
0
      isec->gc_mark = 1;
14012
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14013
0
      has_kept_debug_info = true;
14014
0
  }
14015
14016
      /* Look for CODE sections which are going to be discarded,
14017
   and find and discard any fragmented debug sections which
14018
   are associated with that code section.  */
14019
0
      if (debug_frag_seen)
14020
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14021
0
    if ((isec->flags & SEC_CODE) != 0
14022
0
        && isec->gc_mark == 0)
14023
0
      {
14024
0
        unsigned int ilen;
14025
0
        asection *dsec;
14026
14027
0
        ilen = strlen (isec->name);
14028
14029
        /* Association is determined by the name of the debug
14030
     section containing the name of the code section as
14031
     a suffix.  For example .debug_line.text.foo is a
14032
     debug section associated with .text.foo.  */
14033
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14034
0
    {
14035
0
      unsigned int dlen;
14036
14037
0
      if (dsec->gc_mark == 0
14038
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14039
0
        continue;
14040
14041
0
      dlen = strlen (dsec->name);
14042
14043
0
      if (dlen > ilen
14044
0
          && strncmp (dsec->name + (dlen - ilen),
14045
0
          isec->name, ilen) == 0)
14046
0
        dsec->gc_mark = 0;
14047
0
    }
14048
0
    }
14049
14050
      /* Mark debug sections referenced by kept debug sections.  */
14051
0
      if (has_kept_debug_info)
14052
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14053
0
    if (isec->gc_mark
14054
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14055
0
      if (!_bfd_elf_gc_mark (info, isec,
14056
0
           elf_gc_mark_debug_section))
14057
0
        return false;
14058
0
    }
14059
0
  return true;
14060
0
}
14061
14062
static bool
14063
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14064
0
{
14065
0
  bfd *sub;
14066
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14067
14068
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14069
0
    {
14070
0
      asection *o;
14071
14072
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14073
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14074
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14075
0
  continue;
14076
0
      o = sub->sections;
14077
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14078
0
  continue;
14079
14080
0
      for (o = sub->sections; o != NULL; o = o->next)
14081
0
  {
14082
    /* When any section in a section group is kept, we keep all
14083
       sections in the section group.  If the first member of
14084
       the section group is excluded, we will also exclude the
14085
       group section.  */
14086
0
    if (o->flags & SEC_GROUP)
14087
0
      {
14088
0
        asection *first = elf_next_in_group (o);
14089
0
        o->gc_mark = first->gc_mark;
14090
0
      }
14091
14092
0
    if (o->gc_mark)
14093
0
      continue;
14094
14095
    /* Skip sweeping sections already excluded.  */
14096
0
    if (o->flags & SEC_EXCLUDE)
14097
0
      continue;
14098
14099
    /* Since this is early in the link process, it is simple
14100
       to remove a section from the output.  */
14101
0
    o->flags |= SEC_EXCLUDE;
14102
14103
0
    if (info->print_gc_sections && o->size != 0)
14104
      /* xgettext:c-format */
14105
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14106
0
        o, sub);
14107
0
  }
14108
0
    }
14109
14110
0
  return true;
14111
0
}
14112
14113
/* Propagate collected vtable information.  This is called through
14114
   elf_link_hash_traverse.  */
14115
14116
static bool
14117
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14118
0
{
14119
  /* Those that are not vtables.  */
14120
0
  if (h->start_stop
14121
0
      || h->u2.vtable == NULL
14122
0
      || h->u2.vtable->parent == NULL)
14123
0
    return true;
14124
14125
  /* Those vtables that do not have parents, we cannot merge.  */
14126
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14127
0
    return true;
14128
14129
  /* If we've already been done, exit.  */
14130
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14131
0
    return true;
14132
14133
  /* Make sure the parent's table is up to date.  */
14134
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14135
14136
0
  if (h->u2.vtable->used == NULL)
14137
0
    {
14138
      /* None of this table's entries were referenced.  Re-use the
14139
   parent's table.  */
14140
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14141
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14142
0
    }
14143
0
  else
14144
0
    {
14145
0
      size_t n;
14146
0
      bool *cu, *pu;
14147
14148
      /* Or the parent's entries into ours.  */
14149
0
      cu = h->u2.vtable->used;
14150
0
      cu[-1] = true;
14151
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14152
0
      if (pu != NULL)
14153
0
  {
14154
0
    const struct elf_backend_data *bed;
14155
0
    unsigned int log_file_align;
14156
14157
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14158
0
    log_file_align = bed->s->log_file_align;
14159
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14160
0
    while (n--)
14161
0
      {
14162
0
        if (*pu)
14163
0
    *cu = true;
14164
0
        pu++;
14165
0
        cu++;
14166
0
      }
14167
0
  }
14168
0
    }
14169
14170
0
  return true;
14171
0
}
14172
14173
struct link_info_ok
14174
{
14175
  struct bfd_link_info *info;
14176
  bool ok;
14177
};
14178
14179
static bool
14180
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14181
            void *ptr)
14182
0
{
14183
0
  asection *sec;
14184
0
  bfd_vma hstart, hend;
14185
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14186
0
  const struct elf_backend_data *bed;
14187
0
  unsigned int log_file_align;
14188
0
  struct link_info_ok *info = (struct link_info_ok *) ptr;
14189
14190
  /* Take care of both those symbols that do not describe vtables as
14191
     well as those that are not loaded.  */
14192
0
  if (h->start_stop
14193
0
      || h->u2.vtable == NULL
14194
0
      || h->u2.vtable->parent == NULL)
14195
0
    return true;
14196
14197
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14198
0
        || h->root.type == bfd_link_hash_defweak);
14199
14200
0
  sec = h->root.u.def.section;
14201
0
  hstart = h->root.u.def.value;
14202
0
  hend = hstart + h->size;
14203
14204
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14205
0
               sec, NULL, NULL, true);
14206
0
  if (!relstart)
14207
0
    return info->ok = false;
14208
0
  bed = get_elf_backend_data (sec->owner);
14209
0
  log_file_align = bed->s->log_file_align;
14210
14211
0
  relend = relstart + sec->reloc_count;
14212
14213
0
  for (rel = relstart; rel < relend; ++rel)
14214
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14215
0
      {
14216
  /* If the entry is in use, do nothing.  */
14217
0
  if (h->u2.vtable->used
14218
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14219
0
    {
14220
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14221
0
      if (h->u2.vtable->used[entry])
14222
0
        continue;
14223
0
    }
14224
  /* Otherwise, kill it.  */
14225
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14226
0
      }
14227
14228
0
  return true;
14229
0
}
14230
14231
/* Mark sections containing dynamically referenced symbols.  When
14232
   building shared libraries, we must assume that any visible symbol is
14233
   referenced.  */
14234
14235
bool
14236
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14237
0
{
14238
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
14239
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14240
14241
0
  if ((h->root.type == bfd_link_hash_defined
14242
0
       || h->root.type == bfd_link_hash_defweak)
14243
0
      && (!h->start_stop
14244
0
    || h->root.ldscript_def
14245
0
    || !info->start_stop_gc)
14246
0
      && ((h->ref_dynamic && !h->forced_local)
14247
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14248
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14249
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14250
0
        && (!bfd_link_executable (info)
14251
0
      || info->gc_keep_exported
14252
0
      || info->export_dynamic
14253
0
      || (h->dynamic
14254
0
          && d != NULL
14255
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14256
0
        && (h->versioned >= versioned
14257
0
      || !bfd_hide_sym_by_version (info->version_info,
14258
0
                 h->root.root.string)))))
14259
0
    h->root.u.def.section->flags |= SEC_KEEP;
14260
14261
0
  return true;
14262
0
}
14263
14264
/* Keep all sections containing symbols undefined on the command-line,
14265
   and the section containing the entry symbol.  */
14266
14267
void
14268
_bfd_elf_gc_keep (struct bfd_link_info *info)
14269
0
{
14270
0
  struct bfd_sym_chain *sym;
14271
14272
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14273
0
    {
14274
0
      struct elf_link_hash_entry *h;
14275
14276
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14277
0
        false, false, false);
14278
14279
0
      if (h != NULL
14280
0
    && (h->root.type == bfd_link_hash_defined
14281
0
        || h->root.type == bfd_link_hash_defweak)
14282
0
    && !bfd_is_const_section (h->root.u.def.section))
14283
0
  h->root.u.def.section->flags |= SEC_KEEP;
14284
0
    }
14285
0
}
14286
14287
bool
14288
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14289
        struct bfd_link_info *info)
14290
0
{
14291
0
  bfd *ibfd = info->input_bfds;
14292
14293
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14294
0
    {
14295
0
      asection *sec;
14296
0
      struct elf_reloc_cookie cookie;
14297
14298
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14299
0
  continue;
14300
0
      sec = ibfd->sections;
14301
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14302
0
  continue;
14303
14304
0
      if (!init_reloc_cookie (&cookie, info, ibfd))
14305
0
  return false;
14306
14307
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14308
0
  {
14309
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14310
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14311
0
      {
14312
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14313
0
        fini_reloc_cookie_rels (&cookie, sec);
14314
0
      }
14315
0
  }
14316
0
    }
14317
0
  return true;
14318
0
}
14319
14320
/* Do mark and sweep of unused sections.  */
14321
14322
bool
14323
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14324
0
{
14325
0
  bool ok = true;
14326
0
  bfd *sub;
14327
0
  elf_gc_mark_hook_fn gc_mark_hook;
14328
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14329
0
  struct elf_link_hash_table *htab;
14330
0
  struct link_info_ok info_ok;
14331
14332
0
  if (!bed->can_gc_sections
14333
0
      || !is_elf_hash_table (info->hash))
14334
0
    {
14335
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14336
0
      return true;
14337
0
    }
14338
14339
0
  bed->gc_keep (info);
14340
0
  htab = elf_hash_table (info);
14341
14342
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14343
     at the .eh_frame section if we can mark the FDEs individually.  */
14344
0
  for (sub = info->input_bfds;
14345
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14346
0
       sub = sub->link.next)
14347
0
    {
14348
0
      asection *sec;
14349
0
      struct elf_reloc_cookie cookie;
14350
14351
0
      sec = sub->sections;
14352
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14353
0
  continue;
14354
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14355
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14356
0
  {
14357
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14358
0
    if (elf_section_data (sec)->sec_info
14359
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14360
0
      elf_eh_frame_section (sub) = sec;
14361
0
    fini_reloc_cookie_for_section (&cookie, sec);
14362
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14363
0
  }
14364
0
    }
14365
14366
  /* Apply transitive closure to the vtable entry usage info.  */
14367
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14368
0
  if (!ok)
14369
0
    return false;
14370
14371
  /* Kill the vtable relocations that were not used.  */
14372
0
  info_ok.info = info;
14373
0
  info_ok.ok = true;
14374
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14375
0
  if (!info_ok.ok)
14376
0
    return false;
14377
14378
  /* Mark dynamically referenced symbols.  */
14379
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14380
0
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14381
14382
  /* Grovel through relocs to find out who stays ...  */
14383
0
  gc_mark_hook = bed->gc_mark_hook;
14384
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14385
0
    {
14386
0
      asection *o;
14387
14388
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14389
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14390
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14391
0
  continue;
14392
14393
0
      o = sub->sections;
14394
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14395
0
  continue;
14396
14397
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14398
   Also treat note sections as a root, if the section is not part
14399
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14400
   well as FINI_ARRAY sections for ld -r.  */
14401
0
      for (o = sub->sections; o != NULL; o = o->next)
14402
0
  if (!o->gc_mark
14403
0
      && (o->flags & SEC_EXCLUDE) == 0
14404
0
      && ((o->flags & SEC_KEEP) != 0
14405
0
    || (bfd_link_relocatable (info)
14406
0
        && ((elf_section_data (o)->this_hdr.sh_type
14407
0
       == SHT_PREINIT_ARRAY)
14408
0
      || (elf_section_data (o)->this_hdr.sh_type
14409
0
          == SHT_INIT_ARRAY)
14410
0
      || (elf_section_data (o)->this_hdr.sh_type
14411
0
          == SHT_FINI_ARRAY)))
14412
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14413
0
        && elf_next_in_group (o) == NULL
14414
0
        && elf_linked_to_section (o) == NULL)
14415
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14416
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14417
0
    {
14418
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14419
0
        return false;
14420
0
    }
14421
0
    }
14422
14423
  /* Allow the backend to mark additional target specific sections.  */
14424
0
  bed->gc_mark_extra_sections (info, gc_mark_hook);
14425
14426
  /* ... and mark SEC_EXCLUDE for those that go.  */
14427
0
  return elf_gc_sweep (abfd, info);
14428
0
}
14429

14430
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14431
14432
bool
14433
bfd_elf_gc_record_vtinherit (bfd *abfd,
14434
           asection *sec,
14435
           struct elf_link_hash_entry *h,
14436
           bfd_vma offset)
14437
0
{
14438
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14439
0
  struct elf_link_hash_entry **search, *child;
14440
0
  size_t extsymcount;
14441
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14442
14443
  /* The sh_info field of the symtab header tells us where the
14444
     external symbols start.  We don't care about the local symbols at
14445
     this point.  */
14446
0
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14447
0
  if (!elf_bad_symtab (abfd))
14448
0
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14449
14450
0
  sym_hashes = elf_sym_hashes (abfd);
14451
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14452
14453
  /* Hunt down the child symbol, which is in this section at the same
14454
     offset as the relocation.  */
14455
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14456
0
    {
14457
0
      if ((child = *search) != NULL
14458
0
    && (child->root.type == bfd_link_hash_defined
14459
0
        || child->root.type == bfd_link_hash_defweak)
14460
0
    && child->root.u.def.section == sec
14461
0
    && child->root.u.def.value == offset)
14462
0
  goto win;
14463
0
    }
14464
14465
  /* xgettext:c-format */
14466
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14467
0
          abfd, sec, (uint64_t) offset);
14468
0
  bfd_set_error (bfd_error_invalid_operation);
14469
0
  return false;
14470
14471
0
 win:
14472
0
  if (!child->u2.vtable)
14473
0
    {
14474
0
      child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14475
0
        bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14476
0
      if (!child->u2.vtable)
14477
0
  return false;
14478
0
    }
14479
0
  if (!h)
14480
0
    {
14481
      /* This *should* only be the absolute section.  It could potentially
14482
   be that someone has defined a non-global vtable though, which
14483
   would be bad.  It isn't worth paging in the local symbols to be
14484
   sure though; that case should simply be handled by the assembler.  */
14485
14486
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14487
0
    }
14488
0
  else
14489
0
    child->u2.vtable->parent = h;
14490
14491
0
  return true;
14492
0
}
14493
14494
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14495
14496
bool
14497
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14498
         struct elf_link_hash_entry *h,
14499
         bfd_vma addend)
14500
0
{
14501
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14502
0
  unsigned int log_file_align = bed->s->log_file_align;
14503
14504
0
  if (!h)
14505
0
    {
14506
      /* xgettext:c-format */
14507
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14508
0
        abfd, sec);
14509
0
      bfd_set_error (bfd_error_bad_value);
14510
0
      return false;
14511
0
    }
14512
14513
0
  if (!h->u2.vtable)
14514
0
    {
14515
0
      h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14516
0
          bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14517
0
      if (!h->u2.vtable)
14518
0
  return false;
14519
0
    }
14520
14521
0
  if (addend >= h->u2.vtable->size)
14522
0
    {
14523
0
      size_t size, bytes, file_align;
14524
0
      bool *ptr = h->u2.vtable->used;
14525
14526
      /* While the symbol is undefined, we have to be prepared to handle
14527
   a zero size.  */
14528
0
      file_align = 1 << log_file_align;
14529
0
      if (h->root.type == bfd_link_hash_undefined)
14530
0
  size = addend + file_align;
14531
0
      else
14532
0
  {
14533
0
    size = h->size;
14534
0
    if (addend >= size)
14535
0
      {
14536
        /* Oops!  We've got a reference past the defined end of
14537
     the table.  This is probably a bug -- shall we warn?  */
14538
0
        size = addend + file_align;
14539
0
      }
14540
0
  }
14541
0
      size = (size + file_align - 1) & -file_align;
14542
14543
      /* Allocate one extra entry for use as a "done" flag for the
14544
   consolidation pass.  */
14545
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14546
14547
0
      if (ptr)
14548
0
  {
14549
0
    ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14550
14551
0
    if (ptr != NULL)
14552
0
      {
14553
0
        size_t oldbytes;
14554
14555
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14556
0
        * sizeof (bool));
14557
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14558
0
      }
14559
0
  }
14560
0
      else
14561
0
  ptr = (bool *) bfd_zmalloc (bytes);
14562
14563
0
      if (ptr == NULL)
14564
0
  return false;
14565
14566
      /* And arrange for that done flag to be at index -1.  */
14567
0
      h->u2.vtable->used = ptr + 1;
14568
0
      h->u2.vtable->size = size;
14569
0
    }
14570
14571
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14572
14573
0
  return true;
14574
0
}
14575
14576
/* Map an ELF section header flag to its corresponding string.  */
14577
typedef struct
14578
{
14579
  char *flag_name;
14580
  flagword flag_value;
14581
} elf_flags_to_name_table;
14582
14583
static const elf_flags_to_name_table elf_flags_to_names [] =
14584
{
14585
  { "SHF_WRITE", SHF_WRITE },
14586
  { "SHF_ALLOC", SHF_ALLOC },
14587
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14588
  { "SHF_MERGE", SHF_MERGE },
14589
  { "SHF_STRINGS", SHF_STRINGS },
14590
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14591
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14592
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14593
  { "SHF_GROUP", SHF_GROUP },
14594
  { "SHF_TLS", SHF_TLS },
14595
  { "SHF_MASKOS", SHF_MASKOS },
14596
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14597
};
14598
14599
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14600
bool
14601
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14602
            struct flag_info *flaginfo,
14603
            asection *section)
14604
0
{
14605
0
  const bfd_vma sh_flags = elf_section_flags (section);
14606
14607
0
  if (!flaginfo->flags_initialized)
14608
0
    {
14609
0
      bfd *obfd = info->output_bfd;
14610
0
      const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14611
0
      struct flag_info_list *tf = flaginfo->flag_list;
14612
0
      int with_hex = 0;
14613
0
      int without_hex = 0;
14614
14615
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14616
0
  {
14617
0
    unsigned i;
14618
0
    flagword (*lookup) (char *);
14619
14620
0
    lookup = bed->elf_backend_lookup_section_flags_hook;
14621
0
    if (lookup != NULL)
14622
0
      {
14623
0
        flagword hexval = (*lookup) ((char *) tf->name);
14624
14625
0
        if (hexval != 0)
14626
0
    {
14627
0
      if (tf->with == with_flags)
14628
0
        with_hex |= hexval;
14629
0
      else if (tf->with == without_flags)
14630
0
        without_hex |= hexval;
14631
0
      tf->valid = true;
14632
0
      continue;
14633
0
    }
14634
0
      }
14635
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14636
0
      {
14637
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14638
0
    {
14639
0
      if (tf->with == with_flags)
14640
0
        with_hex |= elf_flags_to_names[i].flag_value;
14641
0
      else if (tf->with == without_flags)
14642
0
        without_hex |= elf_flags_to_names[i].flag_value;
14643
0
      tf->valid = true;
14644
0
      break;
14645
0
    }
14646
0
      }
14647
0
    if (!tf->valid)
14648
0
      {
14649
0
        info->callbacks->einfo
14650
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14651
0
        return false;
14652
0
      }
14653
0
  }
14654
0
      flaginfo->flags_initialized = true;
14655
0
      flaginfo->only_with_flags |= with_hex;
14656
0
      flaginfo->not_with_flags |= without_hex;
14657
0
    }
14658
14659
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14660
0
    return false;
14661
14662
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
14663
0
    return false;
14664
14665
0
  return true;
14666
0
}
14667
14668
struct alloc_got_off_arg {
14669
  bfd_vma gotoff;
14670
  struct bfd_link_info *info;
14671
};
14672
14673
/* We need a special top-level link routine to convert got reference counts
14674
   to real got offsets.  */
14675
14676
static bool
14677
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14678
0
{
14679
0
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14680
0
  bfd *obfd = gofarg->info->output_bfd;
14681
0
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14682
14683
0
  if (h->got.refcount > 0)
14684
0
    {
14685
0
      h->got.offset = gofarg->gotoff;
14686
0
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14687
0
    }
14688
0
  else
14689
0
    h->got.offset = (bfd_vma) -1;
14690
14691
0
  return true;
14692
0
}
14693
14694
/* And an accompanying bit to work out final got entry offsets once
14695
   we're done.  Should be called from final_link.  */
14696
14697
bool
14698
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14699
          struct bfd_link_info *info)
14700
0
{
14701
0
  bfd *i;
14702
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14703
0
  bfd_vma gotoff;
14704
0
  struct alloc_got_off_arg gofarg;
14705
14706
0
  BFD_ASSERT (abfd == info->output_bfd);
14707
14708
0
  if (! is_elf_hash_table (info->hash))
14709
0
    return false;
14710
14711
  /* The GOT offset is relative to the .got section, but the GOT header is
14712
     put into the .got.plt section, if the backend uses it.  */
14713
0
  if (bed->want_got_plt)
14714
0
    gotoff = 0;
14715
0
  else
14716
0
    gotoff = bed->got_header_size;
14717
14718
  /* Do the local .got entries first.  */
14719
0
  for (i = info->input_bfds; i; i = i->link.next)
14720
0
    {
14721
0
      bfd_signed_vma *local_got;
14722
0
      size_t j, locsymcount;
14723
0
      Elf_Internal_Shdr *symtab_hdr;
14724
14725
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14726
0
  continue;
14727
14728
0
      local_got = elf_local_got_refcounts (i);
14729
0
      if (!local_got)
14730
0
  continue;
14731
14732
0
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
14733
0
      if (elf_bad_symtab (i))
14734
0
  locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14735
0
      else
14736
0
  locsymcount = symtab_hdr->sh_info;
14737
14738
0
      for (j = 0; j < locsymcount; ++j)
14739
0
  {
14740
0
    if (local_got[j] > 0)
14741
0
      {
14742
0
        local_got[j] = gotoff;
14743
0
        gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14744
0
      }
14745
0
    else
14746
0
      local_got[j] = (bfd_vma) -1;
14747
0
  }
14748
0
    }
14749
14750
  /* Then the global .got entries.  .plt refcounts are handled by
14751
     adjust_dynamic_symbol  */
14752
0
  gofarg.gotoff = gotoff;
14753
0
  gofarg.info = info;
14754
0
  elf_link_hash_traverse (elf_hash_table (info),
14755
0
        elf_gc_allocate_got_offsets,
14756
0
        &gofarg);
14757
0
  return true;
14758
0
}
14759
14760
/* Many folk need no more in the way of final link than this, once
14761
   got entry reference counting is enabled.  */
14762
14763
bool
14764
bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14765
0
{
14766
0
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14767
0
    return false;
14768
14769
  /* Invoke the regular ELF backend linker to do all the work.  */
14770
0
  return bfd_elf_final_link (abfd, info);
14771
0
}
14772
14773
bool
14774
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14775
0
{
14776
0
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14777
14778
0
  if (rcookie->bad_symtab)
14779
0
    rcookie->rel = rcookie->rels;
14780
14781
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14782
0
    {
14783
0
      unsigned long r_symndx;
14784
14785
0
      if (! rcookie->bad_symtab)
14786
0
  if (rcookie->rel->r_offset > offset)
14787
0
    return false;
14788
0
      if (rcookie->rel->r_offset != offset)
14789
0
  continue;
14790
14791
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14792
0
      if (r_symndx == STN_UNDEF)
14793
0
  return true;
14794
14795
0
      if (r_symndx >= rcookie->locsymcount
14796
0
    || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14797
0
  {
14798
0
    struct elf_link_hash_entry *h;
14799
14800
0
    h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14801
14802
0
    while (h->root.type == bfd_link_hash_indirect
14803
0
     || h->root.type == bfd_link_hash_warning)
14804
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
14805
14806
0
    if ((h->root.type == bfd_link_hash_defined
14807
0
         || h->root.type == bfd_link_hash_defweak)
14808
0
        && (h->root.u.def.section->owner != rcookie->abfd
14809
0
      || h->root.u.def.section->kept_section != NULL
14810
0
      || discarded_section (h->root.u.def.section)))
14811
0
      return true;
14812
0
  }
14813
0
      else
14814
0
  {
14815
    /* It's not a relocation against a global symbol,
14816
       but it could be a relocation against a local
14817
       symbol for a discarded section.  */
14818
0
    asection *isec;
14819
0
    Elf_Internal_Sym *isym;
14820
14821
    /* Need to: get the symbol; get the section.  */
14822
0
    isym = &rcookie->locsyms[r_symndx];
14823
0
    isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14824
0
    if (isec != NULL
14825
0
        && (isec->kept_section != NULL
14826
0
      || discarded_section (isec)))
14827
0
      return true;
14828
0
  }
14829
0
      return false;
14830
0
    }
14831
0
  return false;
14832
0
}
14833
14834
/* Discard unneeded references to discarded sections.
14835
   Returns -1 on error, 1 if any section's size was changed, 0 if
14836
   nothing changed.  This function assumes that the relocations are in
14837
   sorted order, which is true for all known assemblers.  */
14838
14839
int
14840
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14841
0
{
14842
0
  struct elf_reloc_cookie cookie;
14843
0
  asection *o;
14844
0
  bfd *abfd;
14845
0
  int changed = 0;
14846
14847
0
  if (info->traditional_format
14848
0
      || !is_elf_hash_table (info->hash))
14849
0
    return 0;
14850
14851
0
  o = bfd_get_section_by_name (output_bfd, ".stab");
14852
0
  if (o != NULL)
14853
0
    {
14854
0
      asection *i;
14855
14856
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14857
0
  {
14858
0
    if (i->size == 0
14859
0
        || i->reloc_count == 0
14860
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
14861
0
      continue;
14862
14863
0
    abfd = i->owner;
14864
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14865
0
      continue;
14866
14867
0
    if (!init_reloc_cookie_for_section (&cookie, info, i))
14868
0
      return -1;
14869
14870
0
    if (_bfd_discard_section_stabs (abfd, i,
14871
0
            elf_section_data (i)->sec_info,
14872
0
            bfd_elf_reloc_symbol_deleted_p,
14873
0
            &cookie))
14874
0
      changed = 1;
14875
14876
0
    fini_reloc_cookie_for_section (&cookie, i);
14877
0
  }
14878
0
    }
14879
14880
0
  o = NULL;
14881
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14882
0
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14883
0
  if (o != NULL)
14884
0
    {
14885
0
      asection *i;
14886
0
      int eh_changed = 0;
14887
0
      unsigned int eh_alignment;  /* Octets.  */
14888
14889
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14890
0
  {
14891
0
    if (i->size == 0)
14892
0
      continue;
14893
14894
0
    abfd = i->owner;
14895
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14896
0
      continue;
14897
14898
0
    if (!init_reloc_cookie_for_section (&cookie, info, i))
14899
0
      return -1;
14900
14901
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14902
0
    if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14903
0
             bfd_elf_reloc_symbol_deleted_p,
14904
0
             &cookie))
14905
0
      {
14906
0
        eh_changed = 1;
14907
0
        if (i->size != i->rawsize)
14908
0
    changed = 1;
14909
0
      }
14910
14911
0
    fini_reloc_cookie_for_section (&cookie, i);
14912
0
  }
14913
14914
0
      eh_alignment = ((1 << o->alignment_power)
14915
0
          * bfd_octets_per_byte (output_bfd, o));
14916
      /* Skip over zero terminator, and prevent empty sections from
14917
   adding alignment padding at the end.  */
14918
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14919
0
  if (i->size == 0)
14920
0
    i->flags |= SEC_EXCLUDE;
14921
0
  else if (i->size > 4)
14922
0
    break;
14923
      /* The last non-empty eh_frame section doesn't need padding.  */
14924
0
      if (i != NULL)
14925
0
  i = i->map_tail.s;
14926
      /* Any prior sections must pad the last FDE out to the output
14927
   section alignment.  Otherwise we might have zero padding
14928
   between sections, which would be seen as a terminator.  */
14929
0
      for (; i != NULL; i = i->map_tail.s)
14930
0
  if (i->size == 4)
14931
    /* All but the last zero terminator should have been removed.  */
14932
0
    BFD_FAIL ();
14933
0
  else
14934
0
    {
14935
0
      bfd_size_type size
14936
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
14937
0
      if (i->size != size)
14938
0
        {
14939
0
    i->size = size;
14940
0
    changed = 1;
14941
0
    eh_changed = 1;
14942
0
        }
14943
0
    }
14944
0
      if (eh_changed)
14945
0
  elf_link_hash_traverse (elf_hash_table (info),
14946
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14947
0
    }
14948
14949
0
  o = bfd_get_section_by_name (output_bfd, ".sframe");
14950
0
  if (o != NULL)
14951
0
    {
14952
0
      asection *i;
14953
14954
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14955
0
  {
14956
0
    if (i->size == 0)
14957
0
      continue;
14958
14959
0
    abfd = i->owner;
14960
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14961
0
      continue;
14962
14963
0
    if (!init_reloc_cookie_for_section (&cookie, info, i))
14964
0
      return -1;
14965
14966
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
14967
0
      {
14968
0
        if (_bfd_elf_discard_section_sframe (i,
14969
0
               bfd_elf_reloc_symbol_deleted_p,
14970
0
               &cookie))
14971
0
    {
14972
0
      if (i->size != i->rawsize)
14973
0
        changed = 1;
14974
0
    }
14975
0
      }
14976
0
    fini_reloc_cookie_for_section (&cookie, i);
14977
0
  }
14978
      /* Update the reference to the output .sframe section.  Used to
14979
   determine later if PT_GNU_SFRAME segment is to be generated.  */
14980
0
      if (!_bfd_elf_set_section_sframe (output_bfd, info))
14981
0
  return -1;
14982
0
    }
14983
14984
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14985
0
    {
14986
0
      const struct elf_backend_data *bed;
14987
0
      asection *s;
14988
14989
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14990
0
  continue;
14991
0
      s = abfd->sections;
14992
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14993
0
  continue;
14994
14995
0
      bed = get_elf_backend_data (abfd);
14996
14997
0
      if (bed->elf_backend_discard_info != NULL)
14998
0
  {
14999
0
    if (!init_reloc_cookie (&cookie, info, abfd))
15000
0
      return -1;
15001
15002
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15003
0
      changed = 1;
15004
15005
0
    fini_reloc_cookie (&cookie, abfd);
15006
0
  }
15007
0
    }
15008
15009
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15010
0
    _bfd_elf_end_eh_frame_parsing (info);
15011
15012
0
  if (info->eh_frame_hdr_type
15013
0
      && !bfd_link_relocatable (info)
15014
0
      && _bfd_elf_discard_section_eh_frame_hdr (info))
15015
0
    changed = 1;
15016
15017
0
  return changed;
15018
0
}
15019
15020
bool
15021
_bfd_elf_section_already_linked (bfd *abfd,
15022
         asection *sec,
15023
         struct bfd_link_info *info)
15024
0
{
15025
0
  flagword flags;
15026
0
  const char *name, *key;
15027
0
  struct bfd_section_already_linked *l;
15028
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15029
15030
0
  if (sec->output_section == bfd_abs_section_ptr)
15031
0
    return false;
15032
15033
0
  flags = sec->flags;
15034
15035
  /* Return if it isn't a linkonce section.  A comdat group section
15036
     also has SEC_LINK_ONCE set.  */
15037
0
  if ((flags & SEC_LINK_ONCE) == 0)
15038
0
    return false;
15039
15040
  /* Don't put group member sections on our list of already linked
15041
     sections.  They are handled as a group via their group section.  */
15042
0
  if (elf_sec_group (sec) != NULL)
15043
0
    return false;
15044
15045
  /* For a SHT_GROUP section, use the group signature as the key.  */
15046
0
  name = sec->name;
15047
0
  if ((flags & SEC_GROUP) != 0
15048
0
      && elf_next_in_group (sec) != NULL
15049
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15050
0
    key = elf_group_name (elf_next_in_group (sec));
15051
0
  else
15052
0
    {
15053
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15054
0
      if (startswith (name, ".gnu.linkonce.")
15055
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15056
0
  key++;
15057
0
      else
15058
  /* Must be a user linkonce section that doesn't follow gcc's
15059
     naming convention.  In this case we won't be matching
15060
     single member groups.  */
15061
0
  key = name;
15062
0
    }
15063
15064
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15065
15066
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15067
0
    {
15068
      /* We may have 2 different types of sections on the list: group
15069
   sections with a signature of <key> (<key> is some string),
15070
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15071
   Match like sections.  LTO plugin sections are an exception.
15072
   They are always named .gnu.linkonce.t.<key> and match either
15073
   type of section.  */
15074
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15075
0
     && ((flags & SEC_GROUP) != 0
15076
0
         || strcmp (name, l->sec->name) == 0))
15077
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15078
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15079
0
  {
15080
    /* The section has already been linked.  See if we should
15081
       issue a warning.  */
15082
0
    if (!_bfd_handle_already_linked (sec, l, info))
15083
0
      return false;
15084
15085
0
    if (flags & SEC_GROUP)
15086
0
      {
15087
0
        asection *first = elf_next_in_group (sec);
15088
0
        asection *s = first;
15089
15090
0
        while (s != NULL)
15091
0
    {
15092
0
      s->output_section = bfd_abs_section_ptr;
15093
      /* Record which group discards it.  */
15094
0
      s->kept_section = l->sec;
15095
0
      s = elf_next_in_group (s);
15096
      /* These lists are circular.  */
15097
0
      if (s == first)
15098
0
        break;
15099
0
    }
15100
0
      }
15101
15102
0
    return true;
15103
0
  }
15104
0
    }
15105
15106
  /* A single member comdat group section may be discarded by a
15107
     linkonce section and vice versa.  */
15108
0
  if ((flags & SEC_GROUP) != 0)
15109
0
    {
15110
0
      asection *first = elf_next_in_group (sec);
15111
15112
0
      if (first != NULL && elf_next_in_group (first) == first)
15113
  /* Check this single member group against linkonce sections.  */
15114
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15115
0
    if ((l->sec->flags & SEC_GROUP) == 0
15116
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15117
0
      {
15118
0
        first->output_section = bfd_abs_section_ptr;
15119
0
        first->kept_section = l->sec;
15120
0
        sec->output_section = bfd_abs_section_ptr;
15121
0
        break;
15122
0
      }
15123
0
    }
15124
0
  else
15125
    /* Check this linkonce section against single member groups.  */
15126
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15127
0
      if (l->sec->flags & SEC_GROUP)
15128
0
  {
15129
0
    asection *first = elf_next_in_group (l->sec);
15130
15131
0
    if (first != NULL
15132
0
        && elf_next_in_group (first) == first
15133
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15134
0
      {
15135
0
        sec->output_section = bfd_abs_section_ptr;
15136
0
        sec->kept_section = first;
15137
0
        break;
15138
0
      }
15139
0
  }
15140
15141
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15142
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15143
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15144
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15145
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15146
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15147
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15148
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15149
     The reverse order cannot happen as there is never a bfd with only the
15150
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15151
     matter as here were are looking only for cross-bfd sections.  */
15152
15153
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15154
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15155
0
      if ((l->sec->flags & SEC_GROUP) == 0
15156
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15157
0
  {
15158
0
    if (abfd != l->sec->owner)
15159
0
      sec->output_section = bfd_abs_section_ptr;
15160
0
    break;
15161
0
  }
15162
15163
  /* This is the first section with this name.  Record it.  */
15164
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15165
0
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15166
0
  return sec->output_section == bfd_abs_section_ptr;
15167
0
}
15168
15169
bool
15170
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15171
0
{
15172
0
  return sym->st_shndx == SHN_COMMON;
15173
0
}
15174
15175
unsigned int
15176
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15177
0
{
15178
0
  return SHN_COMMON;
15179
0
}
15180
15181
asection *
15182
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15183
0
{
15184
0
  return bfd_com_section_ptr;
15185
0
}
15186
15187
bfd_vma
15188
_bfd_elf_default_got_elt_size (bfd *abfd,
15189
             struct bfd_link_info *info ATTRIBUTE_UNUSED,
15190
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15191
             bfd *ibfd ATTRIBUTE_UNUSED,
15192
             unsigned long symndx ATTRIBUTE_UNUSED)
15193
0
{
15194
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15195
0
  return bed->s->arch_size / 8;
15196
0
}
15197
15198
/* Routines to support the creation of dynamic relocs.  */
15199
15200
/* Returns the name of the dynamic reloc section associated with SEC.  */
15201
15202
static const char *
15203
get_dynamic_reloc_section_name (bfd *       abfd,
15204
        asection *  sec,
15205
        bool is_rela)
15206
0
{
15207
0
  char *name;
15208
0
  const char *old_name = bfd_section_name (sec);
15209
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15210
15211
0
  if (old_name == NULL)
15212
0
    return NULL;
15213
15214
0
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15215
0
  sprintf (name, "%s%s", prefix, old_name);
15216
15217
0
  return name;
15218
0
}
15219
15220
/* Returns the dynamic reloc section associated with SEC.
15221
   If necessary compute the name of the dynamic reloc section based
15222
   on SEC's name (looked up in ABFD's string table) and the setting
15223
   of IS_RELA.  */
15224
15225
asection *
15226
_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15227
            asection *sec,
15228
            bool is_rela)
15229
0
{
15230
0
  asection *reloc_sec = elf_section_data (sec)->sreloc;
15231
15232
0
  if (reloc_sec == NULL)
15233
0
    {
15234
0
      const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15235
15236
0
      if (name != NULL)
15237
0
  {
15238
0
    reloc_sec = bfd_get_linker_section (abfd, name);
15239
15240
0
    if (reloc_sec != NULL)
15241
0
      elf_section_data (sec)->sreloc = reloc_sec;
15242
0
  }
15243
0
    }
15244
15245
0
  return reloc_sec;
15246
0
}
15247
15248
/* Returns the dynamic reloc section associated with SEC.  If the
15249
   section does not exist it is created and attached to the DYNOBJ
15250
   bfd and stored in the SRELOC field of SEC's elf_section_data
15251
   structure.
15252
15253
   ALIGNMENT is the alignment for the newly created section and
15254
   IS_RELA defines whether the name should be .rela.<SEC's name>
15255
   or .rel.<SEC's name>.  The section name is looked up in the
15256
   string table associated with ABFD.  */
15257
15258
asection *
15259
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15260
             bfd *dynobj,
15261
             unsigned int alignment,
15262
             bfd *abfd,
15263
             bool is_rela)
15264
0
{
15265
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15266
15267
0
  if (reloc_sec == NULL)
15268
0
    {
15269
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15270
15271
0
      if (name == NULL)
15272
0
  return NULL;
15273
15274
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15275
15276
0
      if (reloc_sec == NULL)
15277
0
  {
15278
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15279
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15280
0
    if ((sec->flags & SEC_ALLOC) != 0)
15281
0
      flags |= SEC_ALLOC | SEC_LOAD;
15282
15283
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15284
0
    if (reloc_sec != NULL)
15285
0
      {
15286
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15287
     name.  Override as it may be wrong, eg. for a user
15288
     section named "auto" we'll get ".relauto" which is
15289
     seen to be a .rela section.  */
15290
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15291
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15292
0
    reloc_sec = NULL;
15293
0
      }
15294
0
  }
15295
15296
0
      elf_section_data (sec)->sreloc = reloc_sec;
15297
0
    }
15298
15299
0
  return reloc_sec;
15300
0
}
15301
15302
/* Copy the ELF symbol type and other attributes for a linker script
15303
   assignment from HSRC to HDEST.  Generally this should be treated as
15304
   if we found a strong non-dynamic definition for HDEST (except that
15305
   ld ignores multiple definition errors).  */
15306
void
15307
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15308
             struct bfd_link_hash_entry *hdest,
15309
             struct bfd_link_hash_entry *hsrc)
15310
0
{
15311
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15312
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15313
0
  Elf_Internal_Sym isym;
15314
15315
0
  ehdest->type = ehsrc->type;
15316
0
  ehdest->target_internal = ehsrc->target_internal;
15317
15318
0
  isym.st_other = ehsrc->other;
15319
0
  elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15320
0
}
15321
15322
/* Append a RELA relocation REL to section S in BFD.  */
15323
15324
void
15325
elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15326
0
{
15327
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15328
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15329
0
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15330
0
  bed->s->swap_reloca_out (abfd, rel, loc);
15331
0
}
15332
15333
/* Append a REL relocation REL to section S in BFD.  */
15334
15335
void
15336
elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15337
0
{
15338
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15339
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15340
0
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15341
0
  bed->s->swap_reloc_out (abfd, rel, loc);
15342
0
}
15343
15344
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15345
15346
struct bfd_link_hash_entry *
15347
bfd_elf_define_start_stop (struct bfd_link_info *info,
15348
         const char *symbol, asection *sec)
15349
0
{
15350
0
  struct elf_link_hash_entry *h;
15351
15352
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15353
0
          false, false, true);
15354
  /* NB: Common symbols will be turned into definition later.  */
15355
0
  if (h != NULL
15356
0
      && !h->root.ldscript_def
15357
0
      && (h->root.type == bfd_link_hash_undefined
15358
0
    || h->root.type == bfd_link_hash_undefweak
15359
0
    || ((h->ref_regular || h->def_dynamic)
15360
0
        && !h->def_regular
15361
0
        && h->root.type != bfd_link_hash_common)))
15362
0
    {
15363
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15364
0
      h->verinfo.verdef = NULL;
15365
0
      h->root.type = bfd_link_hash_defined;
15366
0
      h->root.u.def.section = sec;
15367
0
      h->root.u.def.value = 0;
15368
0
      h->def_regular = 1;
15369
0
      h->def_dynamic = 0;
15370
0
      h->start_stop = 1;
15371
0
      h->u2.start_stop_section = sec;
15372
0
      if (symbol[0] == '.')
15373
0
  {
15374
    /* .startof. and .sizeof. symbols are local.  */
15375
0
    const struct elf_backend_data *bed;
15376
0
    bed = get_elf_backend_data (info->output_bfd);
15377
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
15378
0
  }
15379
0
      else
15380
0
  {
15381
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15382
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15383
0
      | info->start_stop_visibility);
15384
0
    if (was_dynamic)
15385
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15386
0
  }
15387
0
      return &h->root;
15388
0
    }
15389
0
  return NULL;
15390
0
}
15391
15392
/* Find dynamic relocs for H that apply to read-only sections.  */
15393
15394
asection *
15395
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15396
0
{
15397
0
  struct elf_dyn_relocs *p;
15398
15399
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15400
0
    {
15401
0
      asection *s = p->sec->output_section;
15402
15403
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15404
0
  return p->sec;
15405
0
    }
15406
0
  return NULL;
15407
0
}
15408
15409
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15410
   read-only sections.  */
15411
15412
bool
15413
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15414
0
{
15415
0
  asection *sec;
15416
15417
0
  if (h->root.type == bfd_link_hash_indirect)
15418
0
    return true;
15419
15420
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15421
0
  if (sec != NULL)
15422
0
    {
15423
0
      struct bfd_link_info *info = (struct bfd_link_info *) inf;
15424
15425
0
      info->flags |= DF_TEXTREL;
15426
      /* xgettext:c-format */
15427
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15428
0
        "in read-only section `%pA'\n"),
15429
0
            sec->owner, h->root.root.string, sec);
15430
15431
0
      if (bfd_link_textrel_check (info))
15432
  /* xgettext:c-format */
15433
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15434
0
          "in read-only section `%pA'\n"),
15435
0
        sec->owner, h->root.root.string, sec);
15436
15437
      /* Not an error, just cut short the traversal.  */
15438
0
      return false;
15439
0
    }
15440
0
  return true;
15441
0
}
15442
15443
/* Add dynamic tags.  */
15444
15445
bool
15446
_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15447
         bool need_dynamic_reloc)
15448
0
{
15449
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15450
15451
0
  if (htab->dynamic_sections_created)
15452
0
    {
15453
      /* Add some entries to the .dynamic section.  We fill in the
15454
   values later, in finish_dynamic_sections, but we must add
15455
   the entries now so that we get the correct size for the
15456
   .dynamic section.  The DT_DEBUG entry is filled in by the
15457
   dynamic linker and used by the debugger.  */
15458
0
#define add_dynamic_entry(TAG, VAL) \
15459
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15460
15461
0
      const struct elf_backend_data *bed
15462
0
  = get_elf_backend_data (output_bfd);
15463
15464
0
      if (bfd_link_executable (info))
15465
0
  {
15466
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15467
0
      return false;
15468
0
  }
15469
15470
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15471
0
  {
15472
    /* DT_PLTGOT is used by prelink even if there is no PLT
15473
       relocation.  */
15474
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15475
0
      return false;
15476
0
  }
15477
15478
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15479
0
  {
15480
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15481
0
        || !add_dynamic_entry (DT_PLTREL,
15482
0
             (bed->rela_plts_and_copies_p
15483
0
              ? DT_RELA : DT_REL))
15484
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15485
0
      return false;
15486
0
  }
15487
15488
0
      if (htab->tlsdesc_plt
15489
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15490
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15491
0
  return false;
15492
15493
0
      if (need_dynamic_reloc)
15494
0
  {
15495
0
    if (bed->rela_plts_and_copies_p)
15496
0
      {
15497
0
        if (!add_dynamic_entry (DT_RELA, 0)
15498
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15499
0
      || !add_dynamic_entry (DT_RELAENT,
15500
0
           bed->s->sizeof_rela))
15501
0
    return false;
15502
0
      }
15503
0
    else
15504
0
      {
15505
0
        if (!add_dynamic_entry (DT_REL, 0)
15506
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15507
0
      || !add_dynamic_entry (DT_RELENT,
15508
0
           bed->s->sizeof_rel))
15509
0
    return false;
15510
0
      }
15511
15512
    /* If any dynamic relocs apply to a read-only section,
15513
       then we need a DT_TEXTREL entry.  */
15514
0
    if ((info->flags & DF_TEXTREL) == 0)
15515
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15516
0
            info);
15517
15518
0
    if ((info->flags & DF_TEXTREL) != 0)
15519
0
      {
15520
0
        if (htab->ifunc_resolvers)
15521
0
    info->callbacks->einfo
15522
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15523
0
         "may result in a segfault at runtime; recompile with %s\n"),
15524
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15525
15526
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15527
0
    return false;
15528
0
      }
15529
0
  }
15530
0
    }
15531
0
#undef add_dynamic_entry
15532
15533
0
  return true;
15534
0
}