Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elflink.c
Line
Count
Source
1
/* ELF linking support for BFD.
2
   Copyright (C) 1995-2026 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 "sframe-api.h"
28
#include "safe-ctype.h"
29
#include "libiberty.h"
30
#include "objalloc.h"
31
#include "plugin.h"
32
33
#include <limits.h>
34
#ifndef CHAR_BIT
35
#define CHAR_BIT 8
36
#endif
37
38
/* This struct is used to pass information to routines called via
39
   elf_link_hash_traverse which must return failure.  */
40
41
struct elf_info_failed
42
{
43
  struct bfd_link_info *info;
44
  bool failed;
45
};
46
47
static bool _bfd_elf_fix_symbol_flags
48
  (struct elf_link_hash_entry *, struct elf_info_failed *);
49
50
/* Return false if linker should avoid caching relocation information
51
   and symbol tables of input files in memory.  */
52
53
static bool
54
_bfd_elf_link_keep_memory (struct bfd_link_info *info)
55
0
{
56
0
#ifdef USE_MMAP
57
  /* Don't cache symbol nor relocation tables if they are mapped in.
58
     NB: Since the --no-keep-memory linker option causes:
59
60
     https://sourceware.org/bugzilla/show_bug.cgi?id=31458
61
62
     this is opt-in by each backend.  */
63
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
64
0
  if (bed != NULL && bed->use_mmap)
65
0
    return false;
66
0
#endif
67
0
  bfd *abfd;
68
0
  bfd_size_type size;
69
70
0
  if (!info->keep_memory)
71
0
    return false;
72
73
0
  if (info->max_cache_size == (bfd_size_type) -1)
74
0
    return true;
75
76
0
  abfd = info->input_bfds;
77
0
  size = info->cache_size;
78
0
  do
79
0
    {
80
0
      if (size >= info->max_cache_size)
81
0
  {
82
    /* Over the limit.  Reduce the memory usage.  */
83
0
    info->keep_memory = false;
84
0
    return false;
85
0
  }
86
0
      if (!abfd)
87
0
  break;
88
0
      size += abfd->alloc_size;
89
0
      abfd = abfd->link.next;
90
0
    }
91
0
  while (1);
92
93
0
  return true;
94
0
}
95
96
struct elf_link_hash_entry *
97
_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **sym_hashes,
98
            unsigned int symndx,
99
            unsigned int ext_sym_start,
100
            unsigned int num_sym)
101
0
{
102
0
  if (sym_hashes == NULL
103
      /* Guard against corrupt input.  See PR 32636 for an example.  */
104
0
      || symndx < ext_sym_start
105
0
      || symndx >= num_sym)
106
0
    return NULL;
107
108
0
  struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
109
110
  /* The hash might be empty when bad_symtab.  Also see PR32641.  */
111
0
  if (h == NULL)
112
0
    return NULL;
113
114
0
  while (h->root.type == bfd_link_hash_indirect
115
0
   || h->root.type == bfd_link_hash_warning)
116
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
117
118
0
  return h;
119
0
}
120
121
static struct elf_link_hash_entry *
122
get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie,
123
            unsigned int symndx)
124
0
{
125
0
  if (cookie == NULL)
126
0
    return NULL;
127
128
0
  return _bfd_elf_get_link_hash_entry (elf_sym_hashes (cookie->abfd), symndx,
129
0
               cookie->extsymoff, cookie->num_sym);
130
0
}
131
132
asection *
133
_bfd_get_local_sym_section (struct elf_reloc_cookie *cookie,
134
          unsigned int symndx)
135
0
{
136
0
  if (symndx >= cookie->locsymcount)
137
0
    return NULL;
138
139
0
  bfd *abfd = cookie->abfd;
140
0
  if (elf_loc_shndx (abfd) == NULL)
141
0
    {
142
0
      Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
143
0
      Elf_Internal_Sym *locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
144
0
              cookie->locsymcount, 0,
145
0
              NULL, NULL, NULL);
146
0
      if (locsyms == NULL)
147
0
  return NULL;
148
0
      unsigned int *loc_shndx
149
0
  = bfd_alloc (abfd, cookie->locsymcount * sizeof (*loc_shndx));
150
0
      if (loc_shndx == NULL)
151
0
  return NULL;
152
0
      elf_loc_shndx (abfd) = loc_shndx;
153
0
      for (unsigned int i = 0; i < cookie->locsymcount; i++)
154
0
  {
155
0
    loc_shndx[i] = locsyms[i].st_shndx;
156
0
    if (ELF_ST_BIND (locsyms[i].st_info) != STB_LOCAL)
157
0
      loc_shndx[i] = SHN_BAD;
158
0
  }
159
0
      free (locsyms);
160
0
    }
161
162
0
  return bfd_section_from_elf_index (abfd, elf_loc_shndx (abfd)[symndx]);
163
0
}
164
165
asection *
166
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
167
           unsigned long r_symndx)
168
0
{
169
0
  struct elf_link_hash_entry *h;
170
171
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
172
173
0
  if (h != NULL)
174
0
    {
175
0
      if (h->root.type == bfd_link_hash_defined
176
0
    || h->root.type == bfd_link_hash_defweak)
177
0
  return h->root.u.def.section;
178
0
      else
179
0
  return NULL;
180
0
    }
181
182
0
  return _bfd_get_local_sym_section (cookie, r_symndx);
183
0
}
184
185
/* Define a symbol in a dynamic linkage section.  */
186
187
struct elf_link_hash_entry *
188
_bfd_elf_define_linkage_sym (bfd *abfd,
189
           struct bfd_link_info *info,
190
           asection *sec,
191
           const char *name)
192
0
{
193
0
  struct elf_link_hash_entry *h;
194
0
  struct bfd_link_hash_entry *bh;
195
0
  elf_backend_data *bed;
196
197
0
  h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
198
0
  if (h != NULL)
199
0
    {
200
      /* Zap symbol defined in an as-needed lib that wasn't linked.
201
   This is a symptom of a larger problem:  Absolute symbols
202
   defined in shared libraries can't be overridden, because we
203
   lose the link to the bfd which is via the symbol section.  */
204
0
      h->root.type = bfd_link_hash_new;
205
0
      bh = &h->root;
206
0
    }
207
0
  else
208
0
    bh = NULL;
209
210
0
  bed = get_elf_backend_data (abfd);
211
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
212
0
           sec, 0, NULL, false, bed->collect,
213
0
           &bh))
214
0
    return NULL;
215
0
  h = (struct elf_link_hash_entry *) bh;
216
0
  BFD_ASSERT (h != NULL);
217
0
  h->def_regular = 1;
218
0
  h->non_elf = 0;
219
0
  h->root.linker_def = 1;
220
0
  h->type = STT_OBJECT;
221
0
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
222
0
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
223
224
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
225
0
  return h;
226
0
}
227
228
bool
229
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
230
0
{
231
0
  flagword flags;
232
0
  asection *s;
233
0
  struct elf_link_hash_entry *h;
234
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
235
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
236
237
  /* This function may be called more than once.  */
238
0
  if (htab->sgot != NULL)
239
0
    return true;
240
241
0
  flags = bed->dynamic_sec_flags;
242
243
0
  s = bfd_make_section_anyway_with_flags (abfd,
244
0
            (bed->rela_plts_and_copies_p
245
0
             ? ".rela.got" : ".rel.got"),
246
0
            flags | SEC_READONLY);
247
0
  if (s == NULL
248
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
249
0
    return false;
250
0
  htab->srelgot = s;
251
252
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
253
0
  if (s == NULL
254
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
255
0
    return false;
256
0
  htab->sgot = s;
257
258
0
  if (bed->want_got_plt)
259
0
    {
260
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
261
0
      if (s == NULL
262
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
263
0
  return false;
264
0
      htab->sgotplt = s;
265
0
    }
266
267
  /* The first bit of the global offset table is the header.  */
268
0
  s->size += bed->got_header_size;
269
270
0
  if (bed->want_got_sym)
271
0
    {
272
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
273
   (or .got.plt) section.  We don't do this in the linker script
274
   because we don't want to define the symbol if we are not creating
275
   a global offset table.  */
276
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
277
0
               "_GLOBAL_OFFSET_TABLE_");
278
0
      elf_hash_table (info)->hgot = h;
279
0
      if (h == NULL)
280
0
  return false;
281
0
    }
282
283
0
  return true;
284
0
}
285

286
/* Create a strtab to hold the dynamic symbol names.  */
287
static bool
288
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
289
0
{
290
0
  struct elf_link_hash_table *hash_table;
291
292
0
  hash_table = elf_hash_table (info);
293
0
  if (hash_table->dynobj == NULL)
294
0
    {
295
      /* We may not set dynobj, an input file holding linker created
296
   dynamic sections to abfd, which may be a dynamic object with
297
   its own dynamic sections.  We need to find a normal input file
298
   to hold linker created sections if possible.  */
299
0
      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
300
0
  {
301
0
    bfd *ibfd;
302
0
    asection *s;
303
0
    for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
304
0
      if ((ibfd->flags
305
0
     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
306
0
    && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
307
0
    && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
308
0
    && !((s = ibfd->sections) != NULL
309
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
310
0
        {
311
0
    abfd = ibfd;
312
0
    break;
313
0
        }
314
0
  }
315
0
      hash_table->dynobj = abfd;
316
0
    }
317
318
0
  if (hash_table->dynstr == NULL)
319
0
    {
320
0
      hash_table->dynstr = _bfd_elf_strtab_init ();
321
0
      if (hash_table->dynstr == NULL)
322
0
  return false;
323
0
    }
324
0
  return true;
325
0
}
326
327
/* Create some sections which will be filled in with dynamic linking
328
   information.  ABFD is an input file which requires dynamic sections
329
   to be created.  The dynamic sections take up virtual memory space
330
   when the final executable is run, so we need to create them before
331
   addresses are assigned to the output sections.  We work out the
332
   actual contents and size of these sections later.  */
333
334
bool
335
bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
336
0
{
337
0
  flagword flags;
338
0
  asection *s;
339
0
  elf_backend_data *bed;
340
0
  struct elf_link_hash_entry *h;
341
342
0
  if (! is_elf_hash_table (info->hash))
343
0
    return false;
344
345
0
  if (elf_hash_table (info)->dynamic_sections_created)
346
0
    return true;
347
348
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
349
0
    return false;
350
351
0
  abfd = elf_hash_table (info)->dynobj;
352
0
  bed = get_elf_backend_data (abfd);
353
354
0
  flags = bed->dynamic_sec_flags;
355
356
  /* A dynamically linked executable has a .interp section, but a
357
     shared library does not.  */
358
0
  if (bfd_link_executable (info) && !info->nointerp)
359
0
    {
360
0
      s = bfd_make_section_anyway_with_flags (abfd, ".interp",
361
0
                flags | SEC_READONLY);
362
0
      if (s == NULL)
363
0
  return false;
364
0
      elf_hash_table (info)->interp = s;
365
0
    }
366
367
  /* Create sections to hold version informations.  These are removed
368
     if they are not needed.  */
369
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
370
0
            flags | SEC_READONLY);
371
0
  if (s == NULL
372
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
373
0
    return false;
374
375
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
376
0
            flags | SEC_READONLY);
377
0
  if (s == NULL
378
0
      || !bfd_set_section_alignment (s, 1))
379
0
    return false;
380
381
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
382
0
            flags | SEC_READONLY);
383
0
  if (s == NULL
384
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
385
0
    return false;
386
387
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
388
0
            flags | SEC_READONLY);
389
0
  if (s == NULL
390
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
391
0
    return false;
392
0
  elf_hash_table (info)->dynsym = s;
393
394
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
395
0
            flags | SEC_READONLY);
396
0
  if (s == NULL)
397
0
    return false;
398
399
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
400
0
  if (s == NULL
401
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
402
0
    return false;
403
0
  elf_hash_table (info)->dynamic = s;
404
405
  /* The special symbol _DYNAMIC is always set to the start of the
406
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
407
     only want to define it if we are, in fact, creating a .dynamic
408
     section.  We don't want to define it if there is no .dynamic
409
     section, since on some ELF platforms the start up code examines it
410
     to decide how to initialize the process.  */
411
0
  h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
412
0
  elf_hash_table (info)->hdynamic = h;
413
0
  if (h == NULL)
414
0
    return false;
415
416
0
  if (info->emit_hash)
417
0
    {
418
0
      s = bfd_make_section_anyway_with_flags (abfd, ".hash",
419
0
                flags | SEC_READONLY);
420
0
      if (s == NULL
421
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
422
0
  return false;
423
0
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
424
0
    }
425
426
0
  if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
427
0
    {
428
0
      s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
429
0
                flags | SEC_READONLY);
430
0
      if (s == NULL
431
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
432
0
  return false;
433
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
434
   4 32-bit words followed by variable count of 64-bit words, then
435
   variable count of 32-bit words.  */
436
0
      if (bed->s->arch_size == 64)
437
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
438
0
      else
439
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
440
0
    }
441
442
0
  if (info->enable_dt_relr)
443
0
    {
444
0
      s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
445
0
                flags | SEC_READONLY);
446
0
      if (s == NULL
447
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
448
0
  return false;
449
0
      elf_hash_table (info)->srelrdyn = s;
450
0
    }
451
452
  /* Let the backend create the rest of the sections.  This lets the
453
     backend set the right flags.  The backend will normally create
454
     the .got and .plt sections.  */
455
0
  if (bed->elf_backend_create_dynamic_sections == NULL
456
0
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
457
0
    return false;
458
459
0
  elf_hash_table (info)->dynamic_sections_created = true;
460
461
0
  return true;
462
0
}
463
464
/* Create dynamic sections when linking against a dynamic object.  */
465
466
bool
467
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
468
0
{
469
0
  flagword flags, pltflags;
470
0
  struct elf_link_hash_entry *h;
471
0
  asection *s;
472
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
473
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
474
475
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
476
     .rel[a].bss sections.  */
477
0
  flags = bed->dynamic_sec_flags;
478
479
0
  pltflags = flags;
480
0
  if (bed->plt_not_loaded)
481
    /* We do not clear SEC_ALLOC here because we still want the OS to
482
       allocate space for the section; it's just that there's nothing
483
       to read in from the object file.  */
484
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
485
0
  else
486
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
487
0
  if (bed->plt_readonly)
488
0
    pltflags |= SEC_READONLY;
489
490
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
491
0
  if (s == NULL
492
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
493
0
    return false;
494
0
  htab->splt = s;
495
496
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
497
     .plt section.  */
498
0
  if (bed->want_plt_sym)
499
0
    {
500
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
501
0
               "_PROCEDURE_LINKAGE_TABLE_");
502
0
      elf_hash_table (info)->hplt = h;
503
0
      if (h == NULL)
504
0
  return false;
505
0
    }
506
507
0
  s = bfd_make_section_anyway_with_flags (abfd,
508
0
            (bed->rela_plts_and_copies_p
509
0
             ? ".rela.plt" : ".rel.plt"),
510
0
            flags | SEC_READONLY);
511
0
  if (s == NULL
512
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
513
0
    return false;
514
0
  htab->srelplt = s;
515
516
0
  if (! _bfd_elf_create_got_section (abfd, info))
517
0
    return false;
518
519
0
  if (bed->want_dynbss)
520
0
    {
521
      /* The .dynbss section is a place to put symbols which are defined
522
   by dynamic objects, are referenced by regular objects, and are
523
   not functions.  We must allocate space for them in the process
524
   image and use a R_*_COPY reloc to tell the dynamic linker to
525
   initialize them at run time.  The linker script puts the .dynbss
526
   section into the .bss section of the final image.  */
527
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
528
0
                SEC_ALLOC | SEC_LINKER_CREATED);
529
0
      if (s == NULL)
530
0
  return false;
531
0
      htab->sdynbss = s;
532
533
0
      if (bed->want_dynrelro)
534
0
  {
535
    /* Similarly, but for symbols that were originally in read-only
536
       sections.  This section doesn't really need to have contents,
537
       but make it like other .data.rel.ro sections.  */
538
0
    s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
539
0
              flags);
540
0
    if (s == NULL)
541
0
      return false;
542
0
    htab->sdynrelro = s;
543
0
  }
544
545
      /* The .rel[a].bss section holds copy relocs.  This section is not
546
   normally needed.  We need to create it here, though, so that the
547
   linker will map it to an output section.  We can't just create it
548
   only if we need it, because we will not know whether we need it
549
   until we have seen all the input files, and the first time the
550
   main linker code calls BFD after examining all the input files
551
   (size_dynamic_sections) the input sections have already been
552
   mapped to the output sections.  If the section turns out not to
553
   be needed, we can discard it later.  We will never need this
554
   section when generating a shared object, since they do not use
555
   copy relocs.  */
556
0
      if (bfd_link_executable (info))
557
0
  {
558
0
    s = bfd_make_section_anyway_with_flags (abfd,
559
0
              (bed->rela_plts_and_copies_p
560
0
               ? ".rela.bss" : ".rel.bss"),
561
0
              flags | SEC_READONLY);
562
0
    if (s == NULL
563
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
564
0
      return false;
565
0
    htab->srelbss = s;
566
567
0
    if (bed->want_dynrelro)
568
0
      {
569
0
        s = (bfd_make_section_anyway_with_flags
570
0
       (abfd, (bed->rela_plts_and_copies_p
571
0
         ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
572
0
        flags | SEC_READONLY));
573
0
        if (s == NULL
574
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
575
0
    return false;
576
0
        htab->sreldynrelro = s;
577
0
      }
578
0
  }
579
0
    }
580
581
0
  return true;
582
0
}
583

584
/* Record a new dynamic symbol.  We record the dynamic symbols as we
585
   read the input files, since we need to have a list of all of them
586
   before we can determine the final sizes of the output sections.
587
   Note that we may actually call this function even though we are not
588
   going to output any dynamic symbols; in some cases we know that a
589
   symbol should be in the dynamic symbol table, but only if there is
590
   one.  */
591
592
bool
593
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
594
            struct elf_link_hash_entry *h)
595
0
{
596
0
  if (h->dynindx == -1)
597
0
    {
598
0
      struct elf_strtab_hash *dynstr;
599
0
      const char *p;
600
0
      const char *name;
601
0
      size_t indx;
602
603
0
      if (h->root.type == bfd_link_hash_defined
604
0
    || h->root.type == bfd_link_hash_defweak)
605
0
  {
606
    /* An IR symbol should not be made dynamic.  */
607
0
    if (h->root.u.def.section != NULL
608
0
        && h->root.u.def.section->owner != NULL
609
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
610
0
      return true;
611
0
  }
612
613
      /* XXX: The ABI draft says the linker must turn hidden and
614
   internal symbols into STB_LOCAL symbols when producing the
615
   DSO. However, if ld.so honors st_other in the dynamic table,
616
   this would not be necessary.  */
617
0
      switch (ELF_ST_VISIBILITY (h->other))
618
0
  {
619
0
  case STV_INTERNAL:
620
0
  case STV_HIDDEN:
621
0
    if (h->root.type != bfd_link_hash_undefined
622
0
        && h->root.type != bfd_link_hash_undefweak)
623
0
      {
624
0
        h->forced_local = 1;
625
0
        return true;
626
0
      }
627
628
0
  default:
629
0
    break;
630
0
  }
631
632
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
633
0
      if (h->forced_local)
634
0
  elf_hash_table (info)->has_local_dynsyms = true;
635
0
      ++elf_hash_table (info)->dynsymcount;
636
637
0
      dynstr = elf_hash_table (info)->dynstr;
638
0
      if (dynstr == NULL)
639
0
  {
640
    /* Create a strtab to hold the dynamic symbol names.  */
641
0
    elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
642
0
    if (dynstr == NULL)
643
0
      return false;
644
0
  }
645
646
0
      char *unversioned_name = NULL;
647
648
      /* We don't put any version information in the dynamic string
649
   table.  */
650
0
      name = h->root.root.string;
651
0
      p = strchr (name, ELF_VER_CHR);
652
0
      if (p != NULL)
653
0
  {
654
0
    unversioned_name = bfd_malloc (p - name + 1);
655
0
    memcpy (unversioned_name, name, p - name);
656
0
    unversioned_name[p - name] = 0;
657
0
    name = unversioned_name;
658
0
  }
659
660
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
661
662
0
      if (p != NULL)
663
0
  free (unversioned_name);
664
665
0
      if (indx == (size_t) -1)
666
0
  return false;
667
0
      h->dynstr_index = indx;
668
0
    }
669
670
0
  return true;
671
0
}
672

673
/* Mark a symbol dynamic.  */
674
675
static void
676
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
677
          struct elf_link_hash_entry *h,
678
          Elf_Internal_Sym *sym)
679
0
{
680
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
681
682
  /* It may be called more than once on the same H.  */
683
0
  if(h->dynamic || bfd_link_relocatable (info))
684
0
    return;
685
686
0
  if ((info->dynamic_data
687
0
       && (h->type == STT_OBJECT
688
0
     || h->type == STT_COMMON
689
0
     || (sym != NULL
690
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
691
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
692
0
      || (d != NULL
693
0
    && h->non_elf
694
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
695
0
    {
696
0
      h->dynamic = 1;
697
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
698
   non-IR reference.  */
699
0
      h->root.non_ir_ref_dynamic = 1;
700
0
    }
701
0
}
702
703
/* Record an assignment to a symbol made by a linker script.  We need
704
   this in case some dynamic object refers to this symbol.  */
705
706
bool
707
bfd_elf_record_link_assignment (bfd *output_bfd,
708
        struct bfd_link_info *info,
709
        const char *name,
710
        bool provide,
711
        bool hidden)
712
0
{
713
0
  struct elf_link_hash_entry *h, *hv;
714
0
  struct elf_link_hash_table *htab;
715
0
  elf_backend_data *bed;
716
717
0
  if (!is_elf_hash_table (info->hash))
718
0
    return true;
719
720
0
  htab = elf_hash_table (info);
721
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
722
0
  if (h == NULL)
723
0
    return provide;
724
725
0
  if (h->root.type == bfd_link_hash_warning)
726
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
727
728
0
  if (h->versioned == unknown)
729
0
    {
730
      /* Set versioned if symbol version is unknown.  */
731
0
      const char *version = strrchr (name, ELF_VER_CHR);
732
0
      if (version)
733
0
  {
734
0
    if (version > name && version[-1] != ELF_VER_CHR)
735
0
      h->versioned = versioned_hidden;
736
0
    else
737
0
      h->versioned = versioned;
738
0
  }
739
0
    }
740
741
  /* Symbols defined in a linker script but not referenced anywhere
742
     else will have non_elf set.  */
743
0
  if (h->non_elf)
744
0
    {
745
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
746
0
      h->non_elf = 0;
747
0
    }
748
749
0
  switch (h->root.type)
750
0
    {
751
0
    case bfd_link_hash_defined:
752
0
    case bfd_link_hash_defweak:
753
0
    case bfd_link_hash_common:
754
0
      break;
755
0
    case bfd_link_hash_undefweak:
756
0
    case bfd_link_hash_undefined:
757
      /* Since we're defining the symbol, don't let it seem to have not
758
   been defined.  record_dynamic_symbol and size_dynamic_sections
759
   may depend on this.  */
760
0
      h->root.type = bfd_link_hash_new;
761
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
762
0
  bfd_link_repair_undef_list (&htab->root);
763
0
      break;
764
0
    case bfd_link_hash_new:
765
0
      break;
766
0
    case bfd_link_hash_indirect:
767
      /* We had a versioned symbol in a dynamic library.  We make the
768
   the versioned symbol point to this one.  */
769
0
      bed = get_elf_backend_data (output_bfd);
770
0
      hv = h;
771
0
      while (hv->root.type == bfd_link_hash_indirect
772
0
       || hv->root.type == bfd_link_hash_warning)
773
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
774
      /* We don't need to update h->root.u since linker will set them
775
   later.  */
776
0
      h->root.type = bfd_link_hash_undefined;
777
0
      hv->root.type = bfd_link_hash_indirect;
778
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
779
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
780
0
      break;
781
0
    default:
782
0
      BFD_FAIL ();
783
0
      return false;
784
0
    }
785
786
  /* If this symbol is being provided by the linker script, and it is
787
     currently defined by a dynamic object, but not by a regular
788
     object, then mark it as undefined so that the generic linker will
789
     force the correct value.  */
790
0
  if (provide
791
0
      && h->def_dynamic
792
0
      && !h->def_regular)
793
0
    h->root.type = bfd_link_hash_undefined;
794
795
  /* If this symbol is currently defined by a dynamic object, but not
796
     by a regular object, then clear out any version information because
797
     the symbol will not be associated with the dynamic object any
798
     more.  */
799
0
  if (h->def_dynamic && !h->def_regular)
800
0
    h->verinfo.verdef = NULL;
801
802
  /* Make sure this symbol is not garbage collected.  */
803
0
  h->mark = 1;
804
805
0
  h->def_regular = 1;
806
807
0
  if (hidden)
808
0
    {
809
0
      bed = get_elf_backend_data (output_bfd);
810
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
811
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
812
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
813
0
    }
814
815
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
816
     and executables.  */
817
0
  if (!bfd_link_relocatable (info)
818
0
      && h->dynindx != -1
819
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
820
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
821
0
    h->forced_local = 1;
822
823
0
  if ((h->def_dynamic
824
0
       || h->ref_dynamic
825
0
       || bfd_link_dll (info))
826
0
      && !h->forced_local
827
0
      && h->dynindx == -1)
828
0
    {
829
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
830
0
  return false;
831
832
      /* If this is a weak defined symbol, and we know a corresponding
833
   real symbol from the same dynamic object, make sure the real
834
   symbol is also made into a dynamic symbol.  */
835
0
      if (h->is_weakalias)
836
0
  {
837
0
    struct elf_link_hash_entry *def = weakdef (h);
838
839
0
    if (def->dynindx == -1
840
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
841
0
      return false;
842
0
  }
843
0
    }
844
845
0
  return true;
846
0
}
847
848
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
849
   success, and 2 on a failure caused by attempting to record a symbol
850
   in a discarded section, eg. a discarded link-once section symbol.  */
851
852
int
853
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
854
            bfd *input_bfd,
855
            long input_indx)
856
0
{
857
0
  size_t amt;
858
0
  struct elf_link_local_dynamic_entry *entry;
859
0
  struct elf_link_hash_table *eht;
860
0
  struct elf_strtab_hash *dynstr;
861
0
  size_t dynstr_index;
862
0
  char *name;
863
0
  Elf_External_Sym_Shndx eshndx;
864
0
  char esym[sizeof (Elf64_External_Sym)];
865
866
0
  if (! is_elf_hash_table (info->hash))
867
0
    return 0;
868
869
  /* See if the entry exists already.  */
870
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
871
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
872
0
      return 1;
873
874
0
  amt = sizeof (*entry);
875
0
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
876
0
  if (entry == NULL)
877
0
    return 0;
878
879
  /* Go find the symbol, so that we can find it's name.  */
880
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
881
0
           1, input_indx, &entry->isym, esym, &eshndx))
882
0
    {
883
0
      bfd_release (input_bfd, entry);
884
0
      return 0;
885
0
    }
886
887
0
  if (entry->isym.st_shndx != SHN_UNDEF
888
0
      && entry->isym.st_shndx < SHN_LORESERVE)
889
0
    {
890
0
      asection *s;
891
892
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
893
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
894
0
  {
895
    /* We can still bfd_release here as nothing has done another
896
       bfd_alloc.  We can't do this later in this function.  */
897
0
    bfd_release (input_bfd, entry);
898
0
    return 2;
899
0
  }
900
0
    }
901
902
0
  name = (bfd_elf_string_from_elf_section
903
0
    (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
904
0
     entry->isym.st_name));
905
906
0
  dynstr = elf_hash_table (info)->dynstr;
907
0
  if (dynstr == NULL)
908
0
    {
909
      /* Create a strtab to hold the dynamic symbol names.  */
910
0
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
911
0
      if (dynstr == NULL)
912
0
  return 0;
913
0
    }
914
915
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
916
0
  if (dynstr_index == (size_t) -1)
917
0
    return 0;
918
0
  entry->isym.st_name = dynstr_index;
919
920
0
  eht = elf_hash_table (info);
921
922
0
  entry->next = eht->dynlocal;
923
0
  eht->dynlocal = entry;
924
0
  entry->input_bfd = input_bfd;
925
0
  entry->input_indx = input_indx;
926
0
  eht->dynsymcount++;
927
928
  /* Whatever binding the symbol had before, it's now local.  */
929
0
  entry->isym.st_info
930
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
931
932
  /* The dynindx will be set at the end of size_dynamic_sections.  */
933
934
0
  return 1;
935
0
}
936
937
/* Return the dynindex of a local dynamic symbol.  */
938
939
long
940
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
941
            bfd *input_bfd,
942
            long input_indx)
943
0
{
944
0
  struct elf_link_local_dynamic_entry *e;
945
946
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
947
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
948
0
      return e->dynindx;
949
0
  return -1;
950
0
}
951
952
/* This function is used to renumber the dynamic symbols, if some of
953
   them are removed because they are marked as local.  This is called
954
   via elf_link_hash_traverse.  */
955
956
static bool
957
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
958
              void *data)
959
0
{
960
0
  size_t *count = (size_t *) data;
961
962
0
  if (h->forced_local)
963
0
    return true;
964
965
0
  if (h->dynindx != -1)
966
0
    h->dynindx = ++(*count);
967
968
0
  return true;
969
0
}
970
971
972
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
973
   STB_LOCAL binding.  */
974
975
static bool
976
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
977
              void *data)
978
0
{
979
0
  size_t *count = (size_t *) data;
980
981
0
  if (!h->forced_local)
982
0
    return true;
983
984
0
  if (h->dynindx != -1)
985
0
    h->dynindx = ++(*count);
986
987
0
  return true;
988
0
}
989
990
/* Return true if the dynamic symbol for a given section should be
991
   omitted when creating a shared library.  */
992
bool
993
_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
994
              struct bfd_link_info *info,
995
              asection *p)
996
0
{
997
0
  struct elf_link_hash_table *htab;
998
0
  asection *ip;
999
1000
0
  switch (elf_section_data (p)->this_hdr.sh_type)
1001
0
    {
1002
0
    case SHT_PROGBITS:
1003
0
    case SHT_NOBITS:
1004
      /* If sh_type is yet undecided, assume it could be
1005
   SHT_PROGBITS/SHT_NOBITS.  */
1006
0
    case SHT_NULL:
1007
0
      htab = elf_hash_table (info);
1008
0
      if (htab->text_index_section != NULL)
1009
0
  return p != htab->text_index_section && p != htab->data_index_section;
1010
1011
0
      return (htab->dynobj != NULL
1012
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1013
0
        && ip->output_section == p);
1014
1015
      /* There shouldn't be section relative relocations
1016
   against any other section.  */
1017
0
    default:
1018
0
      return true;
1019
0
    }
1020
0
}
1021
1022
bool
1023
_bfd_elf_omit_section_dynsym_all
1024
    (bfd *output_bfd ATTRIBUTE_UNUSED,
1025
     struct bfd_link_info *info ATTRIBUTE_UNUSED,
1026
     asection *p ATTRIBUTE_UNUSED)
1027
0
{
1028
0
  return true;
1029
0
}
1030
1031
/* Assign dynsym indices.  In a shared library we generate a section
1032
   symbol for each output section, which come first.  Next come symbols
1033
   which have been forced to local binding.  Then all of the back-end
1034
   allocated local dynamic syms, followed by the rest of the global
1035
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1036
   (This prevents the early call before elf_backend_init_index_section
1037
   and strip_excluded_output_sections setting dynindx for sections
1038
   that are stripped.)  */
1039
1040
static unsigned long
1041
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
1042
        struct bfd_link_info *info,
1043
        unsigned long *section_sym_count)
1044
0
{
1045
0
  unsigned long dynsymcount = 0;
1046
0
  bool do_sec = section_sym_count != NULL;
1047
1048
0
  if (bfd_link_pic (info)
1049
0
      || elf_hash_table (info)->is_relocatable_executable)
1050
0
    {
1051
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
1052
0
      asection *p;
1053
0
      for (p = output_bfd->sections; p ; p = p->next)
1054
0
  if ((p->flags & SEC_EXCLUDE) == 0
1055
0
      && (p->flags & SEC_ALLOC) != 0
1056
0
      && elf_hash_table (info)->dynamic_relocs
1057
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1058
0
    {
1059
0
      ++dynsymcount;
1060
0
      if (do_sec)
1061
0
        elf_section_data (p)->dynindx = dynsymcount;
1062
0
    }
1063
0
  else if (do_sec)
1064
0
    elf_section_data (p)->dynindx = 0;
1065
0
    }
1066
0
  if (do_sec)
1067
0
    *section_sym_count = dynsymcount;
1068
1069
0
  if (elf_hash_table (info)->has_local_dynsyms)
1070
0
    elf_link_hash_traverse (elf_hash_table (info),
1071
0
          elf_link_renumber_local_hash_table_dynsyms,
1072
0
          &dynsymcount);
1073
1074
0
  if (elf_hash_table (info)->dynlocal)
1075
0
    {
1076
0
      struct elf_link_local_dynamic_entry *p;
1077
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1078
0
  p->dynindx = ++dynsymcount;
1079
0
    }
1080
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1081
1082
0
  elf_link_hash_traverse (elf_hash_table (info),
1083
0
        elf_link_renumber_hash_table_dynsyms,
1084
0
        &dynsymcount);
1085
1086
  /* There is an unused NULL entry at the head of the table which we
1087
     must account for in our count even if the table is empty since it
1088
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1089
     .dynamic section.  */
1090
0
  dynsymcount++;
1091
1092
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1093
0
  return dynsymcount;
1094
0
}
1095
1096
/* Merge st_other field.  */
1097
1098
static void
1099
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1100
        unsigned int st_other, asection *sec,
1101
        bool definition, bool dynamic)
1102
0
{
1103
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
1104
1105
  /* If st_other has a processor-specific meaning, specific
1106
     code might be needed here.  */
1107
0
  if (bed->elf_backend_merge_symbol_attribute)
1108
0
    (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1109
0
            dynamic);
1110
1111
0
  if (!dynamic)
1112
0
    {
1113
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1114
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1115
1116
      /* Keep the most constraining visibility.  Leave the remainder
1117
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1118
0
      if (symvis - 1 < hvis - 1)
1119
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1120
0
    }
1121
0
  else if (definition
1122
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1123
0
     && (sec->flags & SEC_READONLY) == 0)
1124
0
    h->protected_def = 1;
1125
0
}
1126
1127
/* This function is called when we want to merge a new symbol with an
1128
   existing symbol.  It handles the various cases which arise when we
1129
   find a definition in a dynamic object, or when there is already a
1130
   definition in a dynamic object.  The new symbol is described by
1131
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1132
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1133
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1134
   of an old common symbol.  We set OVERRIDE if the old symbol is
1135
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1136
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1137
   to change.  By OK to change, we mean that we shouldn't warn if the
1138
   type or size does change.  */
1139
1140
static bool
1141
_bfd_elf_merge_symbol (bfd *abfd,
1142
           struct bfd_link_info *info,
1143
           const char *name,
1144
           Elf_Internal_Sym *sym,
1145
           asection **psec,
1146
           bfd_vma *pvalue,
1147
           struct elf_link_hash_entry **sym_hash,
1148
           bfd **poldbfd,
1149
           bool *pold_weak,
1150
           unsigned int *pold_alignment,
1151
           bool *skip,
1152
           bfd **override,
1153
           bool *type_change_ok,
1154
           bool *size_change_ok,
1155
           bool *matched)
1156
0
{
1157
0
  asection *sec, *oldsec;
1158
0
  struct elf_link_hash_entry *h;
1159
0
  struct elf_link_hash_entry *hi;
1160
0
  struct elf_link_hash_entry *flip;
1161
0
  int bind;
1162
0
  bfd *oldbfd;
1163
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1164
0
  bool newweak, oldweak, newfunc, oldfunc;
1165
0
  elf_backend_data *bed;
1166
0
  const char *new_version;
1167
0
  bool default_sym = *matched;
1168
0
  struct elf_link_hash_table *htab;
1169
1170
0
  *skip = false;
1171
0
  *override = NULL;
1172
1173
0
  sec = *psec;
1174
0
  bind = ELF_ST_BIND (sym->st_info);
1175
1176
0
  if (! bfd_is_und_section (sec))
1177
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1178
0
  else
1179
0
    h = ((struct elf_link_hash_entry *)
1180
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1181
0
  if (h == NULL)
1182
0
    return false;
1183
0
  *sym_hash = h;
1184
1185
0
  bed = get_elf_backend_data (abfd);
1186
1187
0
  htab = elf_hash_table (info);
1188
1189
  /* NEW_VERSION is the symbol version of the new symbol.  */
1190
0
  if (h->versioned != unversioned)
1191
0
    {
1192
      /* Symbol version is unknown or versioned.  */
1193
0
      new_version = strrchr (name, ELF_VER_CHR);
1194
0
      if (new_version)
1195
0
  {
1196
0
    if (h->versioned == unknown)
1197
0
      {
1198
        /* The base symbol has an empty version.  */
1199
0
        if (new_version[1] == '\0')
1200
0
    {
1201
0
      htab->has_base_symbols = true;
1202
0
      h->base_symbol = 1;
1203
0
    }
1204
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1205
0
    h->versioned = versioned_hidden;
1206
0
        else
1207
0
    h->versioned = versioned;
1208
0
      }
1209
0
    new_version += 1;
1210
0
    if (new_version[0] == '\0')
1211
0
      new_version = NULL;
1212
0
  }
1213
0
      else
1214
0
  h->versioned = unversioned;
1215
0
    }
1216
0
  else
1217
0
    new_version = NULL;
1218
1219
  /* For merging, we only care about real symbols.  But we need to make
1220
     sure that indirect symbol dynamic flags are updated.  */
1221
0
  hi = h;
1222
0
  while (h->root.type == bfd_link_hash_indirect
1223
0
   || h->root.type == bfd_link_hash_warning)
1224
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1225
1226
0
  if (!*matched)
1227
0
    {
1228
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1229
0
  *matched = true;
1230
0
      else
1231
0
  {
1232
    /* OLD_HIDDEN is true if the existing symbol is only visible
1233
       to the symbol with the same symbol version.  NEW_HIDDEN is
1234
       true if the new symbol is only visible to the symbol with
1235
       the same symbol version.  */
1236
0
    bool old_hidden = h->versioned == versioned_hidden;
1237
0
    bool new_hidden = hi->versioned == versioned_hidden;
1238
0
    if (!old_hidden && !new_hidden)
1239
      /* The new symbol matches the existing symbol if both
1240
         aren't hidden.  */
1241
0
      *matched = true;
1242
0
    else
1243
0
      {
1244
        /* OLD_VERSION is the symbol version of the existing
1245
     symbol. */
1246
0
        const char *old_version;
1247
1248
0
        if (h->versioned >= versioned)
1249
0
    old_version = strrchr (h->root.root.string,
1250
0
               ELF_VER_CHR) + 1;
1251
0
        else
1252
0
     old_version = NULL;
1253
1254
        /* The new symbol matches the existing symbol if they
1255
     have the same symbol version.  */
1256
0
        *matched = (old_version == new_version
1257
0
        || (old_version != NULL
1258
0
            && new_version != NULL
1259
0
            && strcmp (old_version, new_version) == 0));
1260
0
      }
1261
0
  }
1262
0
    }
1263
1264
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1265
     existing symbol.  */
1266
1267
0
  oldbfd = NULL;
1268
0
  oldsec = NULL;
1269
0
  switch (h->root.type)
1270
0
    {
1271
0
    default:
1272
0
      break;
1273
1274
0
    case bfd_link_hash_undefined:
1275
0
    case bfd_link_hash_undefweak:
1276
0
      oldbfd = h->root.u.undef.abfd;
1277
0
      break;
1278
1279
0
    case bfd_link_hash_defined:
1280
0
    case bfd_link_hash_defweak:
1281
0
      oldbfd = h->root.u.def.section->owner;
1282
0
      oldsec = h->root.u.def.section;
1283
0
      break;
1284
1285
0
    case bfd_link_hash_common:
1286
0
      oldbfd = h->root.u.c.p->section->owner;
1287
0
      oldsec = h->root.u.c.p->section;
1288
0
      if (pold_alignment)
1289
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1290
0
      break;
1291
0
    }
1292
0
  if (poldbfd && *poldbfd == NULL)
1293
0
    *poldbfd = oldbfd;
1294
1295
  /* Differentiate strong and weak symbols.  */
1296
0
  newweak = bind == STB_WEAK;
1297
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1298
0
       || h->root.type == bfd_link_hash_undefweak);
1299
0
  if (pold_weak)
1300
0
    *pold_weak = oldweak;
1301
1302
  /* We have to check it for every instance since the first few may be
1303
     references and not all compilers emit symbol type for undefined
1304
     symbols.  */
1305
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1306
1307
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1308
     respectively, is from a dynamic object.  */
1309
1310
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1311
1312
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1313
     syms and defined syms in dynamic libraries respectively.
1314
     ref_dynamic on the other hand can be set for a symbol defined in
1315
     a dynamic library, and def_dynamic may not be set;  When the
1316
     definition in a dynamic lib is overridden by a definition in the
1317
     executable use of the symbol in the dynamic lib becomes a
1318
     reference to the executable symbol.  */
1319
0
  if (newdyn)
1320
0
    {
1321
0
      if (bfd_is_und_section (sec))
1322
0
  {
1323
0
    if (bind != STB_WEAK)
1324
0
      {
1325
0
        h->ref_dynamic_nonweak = 1;
1326
0
        hi->ref_dynamic_nonweak = 1;
1327
0
      }
1328
0
  }
1329
0
      else
1330
0
  {
1331
    /* Update the existing symbol only if they match. */
1332
0
    if (*matched)
1333
0
      h->dynamic_def = 1;
1334
0
    hi->dynamic_def = 1;
1335
0
  }
1336
0
    }
1337
1338
  /* If we just created the symbol, mark it as being an ELF symbol.
1339
     Other than that, there is nothing to do--there is no merge issue
1340
     with a newly defined symbol--so we just return.  */
1341
1342
0
  if (h->root.type == bfd_link_hash_new)
1343
0
    {
1344
0
      h->non_elf = 0;
1345
0
      return true;
1346
0
    }
1347
1348
  /* In cases involving weak versioned symbols, we may wind up trying
1349
     to merge a symbol with itself.  Catch that here, to avoid the
1350
     confusion that results if we try to override a symbol with
1351
     itself.  The additional tests catch cases like
1352
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1353
     dynamic object, which we do want to handle here.  */
1354
0
  if (abfd == oldbfd
1355
0
      && (newweak || oldweak)
1356
0
      && ((abfd->flags & DYNAMIC) == 0
1357
0
    || !h->def_regular))
1358
0
    return true;
1359
1360
0
  olddyn = false;
1361
0
  if (oldbfd != NULL)
1362
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1363
0
  else if (oldsec != NULL)
1364
0
    {
1365
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1366
   indices used by MIPS ELF.  */
1367
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1368
0
    }
1369
1370
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1371
0
  if (!htab->handling_dt_needed
1372
0
      && oldbfd != NULL
1373
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1374
0
    {
1375
0
      if (newdyn != olddyn)
1376
0
  {
1377
    /* Handle a case where plugin_notice won't be called and thus
1378
       won't set the non_ir_ref flags on the first pass over
1379
       symbols.  */
1380
0
    h->root.non_ir_ref_dynamic = true;
1381
0
    hi->root.non_ir_ref_dynamic = true;
1382
0
  }
1383
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1384
0
         && hi->root.type == bfd_link_hash_indirect)
1385
0
  {
1386
    /* Change indirect symbol from IR to undefined.  */
1387
0
    hi->root.type = bfd_link_hash_undefined;
1388
0
    hi->root.u.undef.abfd = oldbfd;
1389
0
  }
1390
0
    }
1391
1392
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1393
     respectively, appear to be a definition rather than reference.  */
1394
1395
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1396
1397
0
  olddef = (h->root.type != bfd_link_hash_undefined
1398
0
      && h->root.type != bfd_link_hash_undefweak
1399
0
      && h->root.type != bfd_link_hash_common);
1400
1401
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1402
     respectively, appear to be a function.  */
1403
1404
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1405
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1406
1407
0
  oldfunc = (h->type != STT_NOTYPE
1408
0
       && bed->is_function_type (h->type));
1409
1410
0
  if (!(newfunc && oldfunc)
1411
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1412
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1413
0
      && h->type != STT_NOTYPE
1414
0
      && (newdef || bfd_is_com_section (sec))
1415
0
      && (olddef || h->root.type == bfd_link_hash_common))
1416
0
    {
1417
      /* If creating a default indirect symbol ("foo" or "foo@") from
1418
   a dynamic versioned definition ("foo@@") skip doing so if
1419
   there is an existing regular definition with a different
1420
   type.  We don't want, for example, a "time" variable in the
1421
   executable overriding a "time" function in a shared library.  */
1422
0
      if (newdyn
1423
0
    && !olddyn)
1424
0
  {
1425
0
    *skip = true;
1426
0
    return true;
1427
0
  }
1428
1429
      /* When adding a symbol from a regular object file after we have
1430
   created indirect symbols, undo the indirection and any
1431
   dynamic state.  */
1432
0
      if (hi != h
1433
0
    && !newdyn
1434
0
    && olddyn)
1435
0
  {
1436
0
    h = hi;
1437
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1438
0
    h->forced_local = 0;
1439
0
    h->ref_dynamic = 0;
1440
0
    h->def_dynamic = 0;
1441
0
    h->dynamic_def = 0;
1442
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1443
0
      {
1444
0
        h->root.type = bfd_link_hash_undefined;
1445
0
        h->root.u.undef.abfd = abfd;
1446
0
      }
1447
0
    else
1448
0
      {
1449
0
        h->root.type = bfd_link_hash_new;
1450
0
        h->root.u.undef.abfd = NULL;
1451
0
      }
1452
0
    return true;
1453
0
  }
1454
0
    }
1455
1456
  /* Check TLS symbols.  We don't check undefined symbols introduced
1457
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1458
     check symbols from plugins because they also have no type.  */
1459
0
  if (oldbfd != NULL
1460
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1461
0
      && (abfd->flags & BFD_PLUGIN) == 0
1462
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1463
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1464
0
    {
1465
0
      bfd *ntbfd, *tbfd;
1466
0
      bool ntdef, tdef;
1467
0
      asection *ntsec, *tsec;
1468
1469
0
      if (h->type == STT_TLS)
1470
0
  {
1471
0
    ntbfd = abfd;
1472
0
    ntsec = sec;
1473
0
    ntdef = newdef;
1474
0
    tbfd = oldbfd;
1475
0
    tsec = oldsec;
1476
0
    tdef = olddef;
1477
0
  }
1478
0
      else
1479
0
  {
1480
0
    ntbfd = oldbfd;
1481
0
    ntsec = oldsec;
1482
0
    ntdef = olddef;
1483
0
    tbfd = abfd;
1484
0
    tsec = sec;
1485
0
    tdef = newdef;
1486
0
  }
1487
1488
0
      if (tdef && ntdef)
1489
0
  _bfd_error_handler
1490
    /* xgettext:c-format */
1491
0
    (_("%s: TLS definition in %pB section %pA "
1492
0
       "mismatches non-TLS definition in %pB section %pA"),
1493
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1494
0
      else if (!tdef && !ntdef)
1495
0
  _bfd_error_handler
1496
    /* xgettext:c-format */
1497
0
    (_("%s: TLS reference in %pB "
1498
0
       "mismatches non-TLS reference in %pB"),
1499
0
     h->root.root.string, tbfd, ntbfd);
1500
0
      else if (tdef)
1501
0
  _bfd_error_handler
1502
    /* xgettext:c-format */
1503
0
    (_("%s: TLS definition in %pB section %pA "
1504
0
       "mismatches non-TLS reference in %pB"),
1505
0
     h->root.root.string, tbfd, tsec, ntbfd);
1506
0
      else
1507
0
  _bfd_error_handler
1508
    /* xgettext:c-format */
1509
0
    (_("%s: TLS reference in %pB "
1510
0
       "mismatches non-TLS definition in %pB section %pA"),
1511
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1512
1513
0
      bfd_set_error (bfd_error_bad_value);
1514
0
      return false;
1515
0
    }
1516
1517
  /* If the old symbol has non-default visibility, we ignore the new
1518
     definition from a dynamic object.  */
1519
0
  if (newdyn
1520
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1521
0
      && !bfd_is_und_section (sec))
1522
0
    {
1523
0
      *skip = true;
1524
      /* Make sure this symbol is dynamic.  */
1525
0
      h->ref_dynamic = 1;
1526
0
      hi->ref_dynamic = 1;
1527
      /* A protected symbol has external availability. Make sure it is
1528
   recorded as dynamic.
1529
1530
   FIXME: Should we check type and size for protected symbol?  */
1531
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1532
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1533
0
      else
1534
0
  return true;
1535
0
    }
1536
0
  else if (!newdyn
1537
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1538
0
     && h->def_dynamic)
1539
0
    {
1540
      /* If the new symbol with non-default visibility comes from a
1541
   relocatable file and the old definition comes from a dynamic
1542
   object, we remove the old definition.  */
1543
0
      if (hi->root.type == bfd_link_hash_indirect)
1544
0
  {
1545
    /* Handle the case where the old dynamic definition is
1546
       default versioned.  We need to copy the symbol info from
1547
       the symbol with default version to the normal one if it
1548
       was referenced before.  */
1549
0
    if (h->ref_regular)
1550
0
      {
1551
0
        hi->root.type = h->root.type;
1552
0
        h->root.type = bfd_link_hash_indirect;
1553
0
        (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1554
1555
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1556
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1557
0
    {
1558
      /* If the new symbol is hidden or internal, completely undo
1559
         any dynamic link state.  */
1560
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1561
0
      h->forced_local = 0;
1562
0
      h->ref_dynamic = 0;
1563
0
    }
1564
0
        else
1565
0
    h->ref_dynamic = 1;
1566
1567
0
        h->def_dynamic = 0;
1568
        /* FIXME: Should we check type and size for protected symbol?  */
1569
0
        h->size = 0;
1570
0
        h->type = 0;
1571
1572
0
        h = hi;
1573
0
      }
1574
0
    else
1575
0
      h = hi;
1576
0
  }
1577
1578
      /* If the old symbol was undefined before, then it will still be
1579
   on the undefs list.  If the new symbol is undefined or
1580
   common, we can't make it bfd_link_hash_new here, because new
1581
   undefined or common symbols will be added to the undefs list
1582
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1583
   added twice to the undefs list.  Also, if the new symbol is
1584
   undefweak then we don't want to lose the strong undef.  */
1585
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1586
0
  {
1587
0
    h->root.type = bfd_link_hash_undefined;
1588
0
    h->root.u.undef.abfd = abfd;
1589
0
  }
1590
0
      else
1591
0
  {
1592
0
    h->root.type = bfd_link_hash_new;
1593
0
    h->root.u.undef.abfd = NULL;
1594
0
  }
1595
1596
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1597
0
  {
1598
    /* If the new symbol is hidden or internal, completely undo
1599
       any dynamic link state.  */
1600
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1601
0
    h->forced_local = 0;
1602
0
    h->ref_dynamic = 0;
1603
0
  }
1604
0
      else
1605
0
  h->ref_dynamic = 1;
1606
0
      h->def_dynamic = 0;
1607
      /* FIXME: Should we check type and size for protected symbol?  */
1608
0
      h->size = 0;
1609
0
      h->type = 0;
1610
0
      return true;
1611
0
    }
1612
1613
  /* If a new weak symbol definition comes from a regular file and the
1614
     old symbol comes from a dynamic library, we treat the new one as
1615
     strong.  Similarly, an old weak symbol definition from a regular
1616
     file is treated as strong when the new symbol comes from a dynamic
1617
     library.  Further, an old weak symbol from a dynamic library is
1618
     treated as strong if the new symbol is from a dynamic library.
1619
     This reflects the way glibc's ld.so works.
1620
1621
     Also allow a weak symbol to override a linker script symbol
1622
     defined by an early pass over the script.  This is done so the
1623
     linker knows the symbol is defined in an object file, for the
1624
     DEFINED script function.
1625
1626
     Do this before setting *type_change_ok or *size_change_ok so that
1627
     we warn properly when dynamic library symbols are overridden.  */
1628
1629
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1630
0
    newweak = false;
1631
0
  if (olddef && newdyn)
1632
0
    oldweak = false;
1633
1634
  /* Allow changes between different types of function symbol.  */
1635
0
  if (newfunc && oldfunc)
1636
0
    *type_change_ok = true;
1637
1638
  /* It's OK to change the type if either the existing symbol or the
1639
     new symbol is weak.  A type change is also OK if the old symbol
1640
     is undefined and the new symbol is defined.  */
1641
1642
0
  if (oldweak
1643
0
      || newweak
1644
0
      || (newdef
1645
0
    && h->root.type == bfd_link_hash_undefined))
1646
0
    *type_change_ok = true;
1647
1648
  /* It's OK to change the size if either the existing symbol or the
1649
     new symbol is weak, or if the old symbol is undefined.  */
1650
1651
0
  if (*type_change_ok
1652
0
      || h->root.type == bfd_link_hash_undefined)
1653
0
    *size_change_ok = true;
1654
1655
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1656
     symbol, respectively, appears to be a common symbol in a dynamic
1657
     object.  If a symbol appears in an uninitialized section, and is
1658
     not weak, and is not a function, then it may be a common symbol
1659
     which was resolved when the dynamic object was created.  We want
1660
     to treat such symbols specially, because they raise special
1661
     considerations when setting the symbol size: if the symbol
1662
     appears as a common symbol in a regular object, and the size in
1663
     the regular object is larger, we must make sure that we use the
1664
     larger size.  This problematic case can always be avoided in C,
1665
     but it must be handled correctly when using Fortran shared
1666
     libraries.
1667
1668
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1669
     likewise for OLDDYNCOMMON and OLDDEF.
1670
1671
     Note that this test is just a heuristic, and that it is quite
1672
     possible to have an uninitialized symbol in a shared object which
1673
     is really a definition, rather than a common symbol.  This could
1674
     lead to some minor confusion when the symbol really is a common
1675
     symbol in some regular object.  However, I think it will be
1676
     harmless.  */
1677
1678
0
  if (newdyn
1679
0
      && newdef
1680
0
      && !newweak
1681
0
      && (sec->flags & SEC_ALLOC) != 0
1682
0
      && (sec->flags & SEC_LOAD) == 0
1683
0
      && sym->st_size > 0
1684
0
      && !newfunc)
1685
0
    newdyncommon = true;
1686
0
  else
1687
0
    newdyncommon = false;
1688
1689
0
  if (olddyn
1690
0
      && olddef
1691
0
      && h->root.type == bfd_link_hash_defined
1692
0
      && h->def_dynamic
1693
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1694
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1695
0
      && h->size > 0
1696
0
      && !oldfunc)
1697
0
    olddyncommon = true;
1698
0
  else
1699
0
    olddyncommon = false;
1700
1701
  /* We now know everything about the old and new symbols.  We ask the
1702
     backend to check if we can merge them.  */
1703
0
  if (bed->merge_symbol != NULL)
1704
0
    {
1705
0
      if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1706
0
  return false;
1707
0
      sec = *psec;
1708
0
    }
1709
1710
  /* There are multiple definitions of a normal symbol.  Skip the
1711
     default symbol as well as definition from an IR object.  */
1712
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1713
0
      && !default_sym && h->def_regular
1714
0
      && !(oldbfd != NULL
1715
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1716
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1717
0
    {
1718
      /* Handle a multiple definition.  */
1719
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1720
0
                 abfd, sec, *pvalue);
1721
0
      *skip = true;
1722
0
      return true;
1723
0
    }
1724
1725
  /* If both the old and the new symbols look like common symbols in a
1726
     dynamic object, set the size of the symbol to the larger of the
1727
     two.  */
1728
1729
0
  if (olddyncommon
1730
0
      && newdyncommon
1731
0
      && sym->st_size != h->size)
1732
0
    {
1733
      /* Since we think we have two common symbols, issue a multiple
1734
   common warning if desired.  Note that we only warn if the
1735
   size is different.  If the size is the same, we simply let
1736
   the old symbol override the new one as normally happens with
1737
   symbols defined in dynamic objects.  */
1738
1739
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1740
0
             bfd_link_hash_common, sym->st_size);
1741
0
      if (sym->st_size > h->size)
1742
0
  h->size = sym->st_size;
1743
1744
0
      *size_change_ok = true;
1745
0
    }
1746
1747
  /* If we are looking at a dynamic object, and we have found a
1748
     definition, we need to see if the symbol was already defined by
1749
     some other object.  If so, we want to use the existing
1750
     definition, and we do not want to report a multiple symbol
1751
     definition error; we do this by clobbering *PSEC to be
1752
     bfd_und_section_ptr.
1753
1754
     We treat a common symbol as a definition if the symbol in the
1755
     shared library is a function, since common symbols always
1756
     represent variables; this can cause confusion in principle, but
1757
     any such confusion would seem to indicate an erroneous program or
1758
     shared library.  We also permit a common symbol in a regular
1759
     object to override a weak symbol in a shared object.  */
1760
1761
0
  if (newdyn
1762
0
      && newdef
1763
0
      && (olddef
1764
0
    || (h->root.type == bfd_link_hash_common
1765
0
        && (newweak || newfunc))))
1766
0
    {
1767
0
      *override = abfd;
1768
0
      newdef = false;
1769
0
      newdyncommon = false;
1770
1771
0
      *psec = sec = bfd_und_section_ptr;
1772
0
      *size_change_ok = true;
1773
1774
      /* If we get here when the old symbol is a common symbol, then
1775
   we are explicitly letting it override a weak symbol or
1776
   function in a dynamic object, and we don't want to warn about
1777
   a type change.  If the old symbol is a defined symbol, a type
1778
   change warning may still be appropriate.  */
1779
1780
0
      if (h->root.type == bfd_link_hash_common)
1781
0
  *type_change_ok = true;
1782
0
    }
1783
1784
  /* Handle the special case of an old common symbol merging with a
1785
     new symbol which looks like a common symbol in a shared object.
1786
     We change *PSEC and *PVALUE to make the new symbol look like a
1787
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1788
     right thing.  */
1789
1790
0
  if (newdyncommon
1791
0
      && h->root.type == bfd_link_hash_common)
1792
0
    {
1793
0
      *override = oldbfd;
1794
0
      newdef = false;
1795
0
      newdyncommon = false;
1796
0
      *pvalue = sym->st_size;
1797
0
      *psec = sec = bed->common_section (oldsec);
1798
0
      *size_change_ok = true;
1799
0
    }
1800
1801
  /* Skip weak definitions of symbols that are already defined.  */
1802
0
  if (newdef && olddef && newweak)
1803
0
    {
1804
      /* Don't skip new non-IR weak syms.  */
1805
0
      if (!(oldbfd != NULL
1806
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1807
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1808
0
  {
1809
0
    newdef = false;
1810
0
    *skip = true;
1811
0
  }
1812
1813
      /* Merge st_other.  If the symbol already has a dynamic index,
1814
   but visibility says it should not be visible, turn it into a
1815
   local symbol.  */
1816
0
      elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1817
0
      if (h->dynindx != -1)
1818
0
  switch (ELF_ST_VISIBILITY (h->other))
1819
0
    {
1820
0
    case STV_INTERNAL:
1821
0
    case STV_HIDDEN:
1822
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1823
0
      break;
1824
0
    }
1825
0
    }
1826
1827
  /* If the old symbol is from a dynamic object, and the new symbol is
1828
     a definition which is not from a dynamic object, then the new
1829
     symbol overrides the old symbol.  Symbols from regular files
1830
     always take precedence over symbols from dynamic objects, even if
1831
     they are defined after the dynamic object in the link.
1832
1833
     As above, we again permit a common symbol in a regular object to
1834
     override a definition in a shared object if the shared object
1835
     symbol is a function or is weak.  */
1836
1837
0
  flip = NULL;
1838
0
  if (!newdyn
1839
0
      && (newdef
1840
0
    || (bfd_is_com_section (sec)
1841
0
        && (oldweak || oldfunc)))
1842
0
      && olddyn
1843
0
      && olddef
1844
0
      && h->def_dynamic)
1845
0
    {
1846
      /* Change the hash table entry to undefined, and let
1847
   _bfd_generic_link_add_one_symbol do the right thing with the
1848
   new definition.  */
1849
1850
0
      h->root.type = bfd_link_hash_undefined;
1851
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1852
0
      *size_change_ok = true;
1853
1854
0
      olddef = false;
1855
0
      olddyncommon = false;
1856
1857
      /* We again permit a type change when a common symbol may be
1858
   overriding a function.  */
1859
1860
0
      if (bfd_is_com_section (sec))
1861
0
  {
1862
0
    if (oldfunc)
1863
0
      {
1864
        /* If a common symbol overrides a function, make sure
1865
     that it isn't defined dynamically nor has type
1866
     function.  */
1867
0
        h->def_dynamic = 0;
1868
0
        h->type = STT_NOTYPE;
1869
0
      }
1870
0
    *type_change_ok = true;
1871
0
  }
1872
1873
0
      if (hi->root.type == bfd_link_hash_indirect)
1874
0
  flip = hi;
1875
0
      else
1876
  /* This union may have been set to be non-NULL when this symbol
1877
     was seen in a dynamic object.  We must force the union to be
1878
     NULL, so that it is correct for a regular symbol.  */
1879
0
  h->verinfo.vertree = NULL;
1880
0
    }
1881
1882
  /* Handle the special case of a new common symbol merging with an
1883
     old symbol that looks like it might be a common symbol defined in
1884
     a shared object.  Note that we have already handled the case in
1885
     which a new common symbol should simply override the definition
1886
     in the shared library.  */
1887
1888
0
  if (! newdyn
1889
0
      && bfd_is_com_section (sec)
1890
0
      && olddyncommon)
1891
0
    {
1892
      /* It would be best if we could set the hash table entry to a
1893
   common symbol, but we don't know what to use for the section
1894
   or the alignment.  */
1895
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1896
0
             bfd_link_hash_common, sym->st_size);
1897
1898
      /* If the presumed common symbol in the dynamic object is
1899
   larger, pretend that the new symbol has its size.  */
1900
1901
0
      if (h->size > *pvalue)
1902
0
  *pvalue = h->size;
1903
1904
      /* We need to remember the alignment required by the symbol
1905
   in the dynamic object.  */
1906
0
      BFD_ASSERT (pold_alignment);
1907
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1908
1909
0
      olddef = false;
1910
0
      olddyncommon = false;
1911
1912
0
      h->root.type = bfd_link_hash_undefined;
1913
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1914
1915
0
      *size_change_ok = true;
1916
0
      *type_change_ok = true;
1917
1918
0
      if (hi->root.type == bfd_link_hash_indirect)
1919
0
  flip = hi;
1920
0
      else
1921
0
  h->verinfo.vertree = NULL;
1922
0
    }
1923
1924
0
  if (flip != NULL)
1925
0
    {
1926
      /* Handle the case where we had a versioned symbol in a dynamic
1927
   library and now find a definition in a normal object.  In this
1928
   case, we make the versioned symbol point to the normal one.  */
1929
0
      flip->root.type = h->root.type;
1930
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1931
0
      h->root.type = bfd_link_hash_indirect;
1932
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1933
0
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1934
0
      if (h->def_dynamic)
1935
0
  {
1936
0
    h->def_dynamic = 0;
1937
0
    flip->ref_dynamic = 1;
1938
0
  }
1939
0
    }
1940
1941
0
  return true;
1942
0
}
1943
1944
/* This function is called to create an indirect symbol from the
1945
   default for the symbol with the default version if needed. The
1946
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1947
   set DYNSYM if the new indirect symbol is dynamic.  */
1948
1949
static bool
1950
_bfd_elf_add_default_symbol (bfd *abfd,
1951
           struct bfd_link_info *info,
1952
           struct elf_link_hash_entry *h,
1953
           const char *name,
1954
           Elf_Internal_Sym *sym,
1955
           asection *sec,
1956
           bfd_vma value,
1957
           bfd **poldbfd,
1958
           bool *dynsym)
1959
0
{
1960
0
  bool type_change_ok;
1961
0
  bool size_change_ok;
1962
0
  bool skip;
1963
0
  char *shortname;
1964
0
  struct elf_link_hash_entry *hi;
1965
0
  struct bfd_link_hash_entry *bh;
1966
0
  elf_backend_data *bed;
1967
0
  bool collect;
1968
0
  bool dynamic;
1969
0
  bfd *override;
1970
0
  const char *p;
1971
0
  size_t len, shortlen;
1972
0
  asection *tmp_sec;
1973
0
  bool matched;
1974
1975
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1976
0
    return true;
1977
1978
  /* If this symbol has a version, and it is the default version, we
1979
     create an indirect symbol from the default name to the fully
1980
     decorated name.  This will cause external references which do not
1981
     specify a version to be bound to this version of the symbol.  */
1982
0
  p = strchr (name, ELF_VER_CHR);
1983
0
  if (h->versioned == unknown)
1984
0
    {
1985
0
      if (p == NULL)
1986
0
  {
1987
0
    h->versioned = unversioned;
1988
0
    return true;
1989
0
  }
1990
0
      else
1991
0
  {
1992
0
    if (p[1] != ELF_VER_CHR)
1993
0
      {
1994
0
        h->versioned = versioned_hidden;
1995
0
        return true;
1996
0
      }
1997
0
    else
1998
0
      h->versioned = versioned;
1999
0
  }
2000
0
    }
2001
0
  else
2002
0
    {
2003
      /* PR ld/19073: We may see an unversioned definition after the
2004
   default version.  */
2005
0
      if (p == NULL)
2006
0
  return true;
2007
0
    }
2008
2009
0
  bed = get_elf_backend_data (abfd);
2010
0
  collect = bed->collect;
2011
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
2012
2013
0
  shortlen = p - name;
2014
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
2015
0
  if (shortname == NULL)
2016
0
    return false;
2017
0
  memcpy (shortname, name, shortlen);
2018
0
  shortname[shortlen] = '\0';
2019
2020
  /* We are going to create a new symbol.  Merge it with any existing
2021
     symbol with this name.  For the purposes of the merge, act as
2022
     though we were defining the symbol we just defined, although we
2023
     actually going to define an indirect symbol.  */
2024
0
  type_change_ok = false;
2025
0
  size_change_ok = false;
2026
0
  matched = true;
2027
0
  tmp_sec = sec;
2028
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2029
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2030
0
            &type_change_ok, &size_change_ok, &matched))
2031
0
    return false;
2032
2033
0
  if (skip)
2034
0
    goto nondefault;
2035
2036
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2037
0
    {
2038
      /* If the undecorated symbol will have a version added by a
2039
   script different to H, then don't indirect to/from the
2040
   undecorated symbol.  This isn't ideal because we may not yet
2041
   have seen symbol versions, if given by a script on the
2042
   command line rather than via --version-script.  */
2043
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2044
0
  {
2045
0
    bool hide;
2046
2047
0
    hi->verinfo.vertree
2048
0
      = bfd_find_version_for_sym (info->version_info,
2049
0
          hi->root.root.string, &hide);
2050
0
    if (hi->verinfo.vertree != NULL && hide)
2051
0
      {
2052
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
2053
0
        goto nondefault;
2054
0
      }
2055
0
  }
2056
0
      if (hi->verinfo.vertree != NULL
2057
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2058
0
  goto nondefault;
2059
0
    }
2060
2061
0
  if (! override)
2062
0
    {
2063
      /* Add the default symbol if not performing a relocatable link.  */
2064
0
      if (! bfd_link_relocatable (info))
2065
0
  {
2066
0
    bh = &hi->root;
2067
0
    if (bh->type == bfd_link_hash_defined
2068
0
        && bh->u.def.section->owner != NULL
2069
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2070
0
      {
2071
        /* Mark the previous definition from IR object as
2072
     undefined so that the generic linker will override
2073
     it.  */
2074
0
        bh->type = bfd_link_hash_undefined;
2075
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2076
0
      }
2077
0
    if (! (_bfd_generic_link_add_one_symbol
2078
0
     (info, abfd, shortname, BSF_INDIRECT,
2079
0
      bfd_ind_section_ptr,
2080
0
      0, name, false, collect, &bh)))
2081
0
      return false;
2082
0
    hi = (struct elf_link_hash_entry *) bh;
2083
0
  }
2084
0
    }
2085
0
  else
2086
0
    {
2087
      /* In this case the symbol named SHORTNAME is overriding the
2088
   indirect symbol we want to add.  We were planning on making
2089
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2090
   is the name without a version.  NAME is the fully versioned
2091
   name, and it is the default version.
2092
2093
   Overriding means that we already saw a definition for the
2094
   symbol SHORTNAME in a regular object, and it is overriding
2095
   the symbol defined in the dynamic object.
2096
2097
   When this happens, we actually want to change NAME, the
2098
   symbol we just added, to refer to SHORTNAME.  This will cause
2099
   references to NAME in the shared object to become references
2100
   to SHORTNAME in the regular object.  This is what we expect
2101
   when we override a function in a shared object: that the
2102
   references in the shared object will be mapped to the
2103
   definition in the regular object.  */
2104
2105
0
      while (hi->root.type == bfd_link_hash_indirect
2106
0
       || hi->root.type == bfd_link_hash_warning)
2107
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2108
2109
0
      h->root.type = bfd_link_hash_indirect;
2110
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2111
0
      if (h->def_dynamic)
2112
0
  {
2113
0
    h->def_dynamic = 0;
2114
0
    hi->ref_dynamic = 1;
2115
0
    if (hi->ref_regular
2116
0
        || hi->def_regular)
2117
0
      {
2118
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2119
0
    return false;
2120
0
      }
2121
0
  }
2122
2123
      /* Now set HI to H, so that the following code will set the
2124
   other fields correctly.  */
2125
0
      hi = h;
2126
0
    }
2127
2128
  /* Check if HI is a warning symbol.  */
2129
0
  if (hi->root.type == bfd_link_hash_warning)
2130
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2131
2132
  /* If there is a duplicate definition somewhere, then HI may not
2133
     point to an indirect symbol.  We will have reported an error to
2134
     the user in that case.  */
2135
2136
0
  if (hi->root.type == bfd_link_hash_indirect)
2137
0
    {
2138
0
      struct elf_link_hash_entry *ht;
2139
2140
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2141
0
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2142
2143
      /* If we first saw a reference to SHORTNAME with non-default
2144
   visibility, merge that visibility to the @@VER symbol.  */
2145
0
      elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2146
2147
      /* A reference to the SHORTNAME symbol from a dynamic library
2148
   will be satisfied by the versioned symbol at runtime.  In
2149
   effect, we have a reference to the versioned symbol.  */
2150
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2151
0
      hi->dynamic_def |= ht->dynamic_def;
2152
2153
      /* See if the new flags lead us to realize that the symbol must
2154
   be dynamic.  */
2155
0
      if (! *dynsym)
2156
0
  {
2157
0
    if (! dynamic)
2158
0
      {
2159
0
        if (! bfd_link_executable (info)
2160
0
      || hi->def_dynamic
2161
0
      || hi->ref_dynamic)
2162
0
    *dynsym = true;
2163
0
      }
2164
0
    else
2165
0
      {
2166
0
        if (hi->ref_regular)
2167
0
    *dynsym = true;
2168
0
      }
2169
0
  }
2170
0
    }
2171
2172
  /* We also need to define an indirection from the nondefault version
2173
     of the symbol.  */
2174
2175
0
 nondefault:
2176
0
  len = strlen (name);
2177
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2178
0
  if (shortname == NULL)
2179
0
    return false;
2180
0
  memcpy (shortname, name, shortlen);
2181
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2182
2183
  /* Once again, merge with any existing symbol.  */
2184
0
  type_change_ok = false;
2185
0
  size_change_ok = false;
2186
0
  tmp_sec = sec;
2187
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2188
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2189
0
            &type_change_ok, &size_change_ok, &matched))
2190
0
    return false;
2191
2192
0
  if (skip)
2193
0
    {
2194
0
      if (!dynamic
2195
0
    && h->root.type == bfd_link_hash_defweak
2196
0
    && hi->root.type == bfd_link_hash_defined)
2197
0
  {
2198
    /* We are handling a weak sym@@ver and attempting to define
2199
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2200
       new weak sym@ver because there is already a strong sym@ver.
2201
       However, sym@ver and sym@@ver are really the same symbol.
2202
       The existing strong sym@ver ought to override sym@@ver.  */
2203
0
    h->root.type = bfd_link_hash_defined;
2204
0
    h->root.u.def.section = hi->root.u.def.section;
2205
0
    h->root.u.def.value = hi->root.u.def.value;
2206
0
    hi->root.type = bfd_link_hash_indirect;
2207
0
    hi->root.u.i.link = &h->root;
2208
0
  }
2209
0
      else
2210
0
  return true;
2211
0
    }
2212
0
  else if (override)
2213
0
    {
2214
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2215
   the type of override we do in the case above unless it is
2216
   overridden by a versioned definition.  */
2217
0
      if (hi->root.type != bfd_link_hash_defined
2218
0
    && hi->root.type != bfd_link_hash_defweak)
2219
0
  _bfd_error_handler
2220
    /* xgettext:c-format */
2221
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2222
0
     abfd, shortname);
2223
0
      return true;
2224
0
    }
2225
0
  else
2226
0
    {
2227
0
      bh = &hi->root;
2228
0
      if (! (_bfd_generic_link_add_one_symbol
2229
0
       (info, abfd, shortname, BSF_INDIRECT,
2230
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2231
0
  return false;
2232
0
      hi = (struct elf_link_hash_entry *) bh;
2233
0
    }
2234
2235
  /* If there is a duplicate definition somewhere, then HI may not
2236
     point to an indirect symbol.  We will have reported an error
2237
     to the user in that case.  */
2238
0
  if (hi->root.type == bfd_link_hash_indirect)
2239
0
    {
2240
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2241
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2242
0
      hi->dynamic_def |= h->dynamic_def;
2243
2244
      /* If we first saw a reference to @VER symbol with
2245
   non-default visibility, merge that visibility to the
2246
   @@VER symbol.  */
2247
0
      elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2248
2249
      /* See if the new flags lead us to realize that the symbol
2250
   must be dynamic.  */
2251
0
      if (! *dynsym)
2252
0
  {
2253
0
    if (! dynamic)
2254
0
      {
2255
0
        if (! bfd_link_executable (info)
2256
0
      || hi->ref_dynamic)
2257
0
    *dynsym = true;
2258
0
      }
2259
0
    else
2260
0
      {
2261
0
        if (hi->ref_regular)
2262
0
    *dynsym = true;
2263
0
      }
2264
0
  }
2265
0
    }
2266
2267
0
  return true;
2268
0
}
2269

2270
/* This routine is used to export all defined symbols into the dynamic
2271
   symbol table.  It is called via elf_link_hash_traverse.  */
2272
2273
static bool
2274
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2275
0
{
2276
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2277
2278
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2279
0
  if (h->root.type == bfd_link_hash_indirect)
2280
0
    return true;
2281
2282
  /* Ignore this if we won't export it.  */
2283
0
  if (!eif->info->export_dynamic && !h->dynamic)
2284
0
    return true;
2285
2286
0
  if (h->dynindx == -1
2287
0
      && (h->def_regular || h->ref_regular)
2288
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2289
0
            h->root.root.string))
2290
0
    {
2291
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2292
0
  {
2293
0
    eif->failed = true;
2294
0
    return false;
2295
0
  }
2296
0
    }
2297
2298
0
  return true;
2299
0
}
2300

2301
/* Return true if linked against glibc.  Otherwise return false.  If
2302
   linked against glibc, add VERSION_DEP to the list of glibc version
2303
   dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
2304
   true, add VERSION_DEP to the version dependency list only if libc.so
2305
   defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
2306
   minor base version.  */
2307
2308
static bool
2309
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2310
          const char *version_dep,
2311
          int *glibc_minor_base,
2312
          bool *auto_version)
2313
0
{
2314
0
  Elf_Internal_Verneed *t;
2315
0
  Elf_Internal_Vernaux *a;
2316
0
  size_t amt;
2317
0
  int minor_version = -1;
2318
0
  bool added = false;
2319
0
  bool glibc = false;
2320
2321
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2322
0
       t != NULL;
2323
0
       t = t->vn_nextref)
2324
0
    {
2325
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2326
0
      if (soname != NULL && startswith (soname, "libc.so."))
2327
0
  break;
2328
0
    }
2329
2330
  /* Skip the shared library if it isn't libc.so.  */
2331
0
  if (t == NULL)
2332
0
    goto update_auto_version_and_return;
2333
2334
0
  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2335
0
    {
2336
      /* Return if VERSION_DEP dependency has been added.  */
2337
0
      if (a->vna_nodename == version_dep
2338
0
    || strcmp (a->vna_nodename, version_dep) == 0)
2339
0
  {
2340
0
    glibc = true;
2341
0
    goto update_auto_version_and_return;
2342
0
  }
2343
2344
      /* Check if libc.so provides GLIBC_2.XX version.  */
2345
0
      if (startswith (a->vna_nodename, "GLIBC_2."))
2346
0
  {
2347
0
    minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2348
0
    if (minor_version < *glibc_minor_base)
2349
0
      *glibc_minor_base = minor_version;
2350
0
  }
2351
0
    }
2352
2353
  /* Skip if it isn't linked against glibc.  */
2354
0
  if (minor_version < 0)
2355
0
    goto update_auto_version_and_return;
2356
2357
0
  glibc = true;
2358
2359
0
  if (auto_version && *auto_version)
2360
0
    {
2361
      /* Add VERSION_DEP to the version dependency list only if
2362
   libc.so defines VERSION_DEP.  */
2363
2364
0
      bool defined = false;
2365
0
      Elf_Internal_Verdef *d;
2366
2367
0
      for (d = elf_tdata (t->vn_bfd)->verdef;
2368
0
     d != NULL;
2369
0
     d = d->vd_nextdef)
2370
0
  if (strcmp (d->vd_nodename, version_dep) == 0)
2371
0
    {
2372
0
      defined = true;
2373
0
      break;
2374
0
    }
2375
2376
      /* Set *AUTO_VERSION to false and return true to indicate that
2377
   libc.so doesn't define VERSION_DEP.  */
2378
0
      if (!defined)
2379
0
  goto update_auto_version_and_return;
2380
0
    }
2381
2382
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2383
0
  if (startswith (version_dep, "GLIBC_2."))
2384
0
    {
2385
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2386
0
      if (minor_version <= *glibc_minor_base)
2387
0
  goto update_auto_version_and_return;
2388
0
    }
2389
2390
0
  amt = sizeof *a;
2391
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2392
0
  if (a == NULL)
2393
0
    {
2394
0
      rinfo->failed = true;
2395
0
      glibc = false;
2396
0
      goto update_auto_version_and_return;
2397
0
    }
2398
2399
0
  a->vna_nodename = version_dep;
2400
0
  a->vna_flags = 0;
2401
0
  a->vna_nextptr = t->vn_auxptr;
2402
0
  a->vna_other = rinfo->vers + 1;
2403
0
  ++rinfo->vers;
2404
2405
0
  t->vn_auxptr = a;
2406
2407
0
  added = true;
2408
2409
0
 update_auto_version_and_return:
2410
0
  if (auto_version)
2411
0
    *auto_version = added;
2412
2413
0
  return glibc;
2414
0
}
2415
2416
/* Add VERSION_DEP to the list of version dependencies when linked
2417
   against glibc.  */
2418
2419
bool
2420
_bfd_elf_link_add_glibc_version_dependency
2421
  (struct elf_find_verdep_info *rinfo,
2422
   const char *const version_dep[],
2423
   bool *auto_version)
2424
0
{
2425
0
  int glibc_minor_base = INT_MAX;
2426
2427
0
  do
2428
0
    {
2429
      /* Return if not linked against glibc.  */
2430
0
      if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
2431
0
               &glibc_minor_base, auto_version))
2432
0
  return false;
2433
0
      version_dep++;
2434
0
      auto_version++;
2435
0
    }
2436
0
  while (*version_dep != NULL);
2437
2438
0
  return true;
2439
0
}
2440
2441
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2442
   linked against glibc.  */
2443
2444
void
2445
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2446
0
{
2447
0
  if (rinfo->info->enable_dt_relr)
2448
0
    {
2449
0
      static const char *const version[] =
2450
0
  {
2451
0
    "GLIBC_ABI_DT_RELR",
2452
0
    NULL
2453
0
  };
2454
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
2455
0
    }
2456
0
}
2457
2458
/* Look through the symbols which are defined in other shared
2459
   libraries and referenced here.  Update the list of version
2460
   dependencies.  This will be put into the .gnu.version_r section.
2461
   This function is called via elf_link_hash_traverse.  */
2462
2463
static bool
2464
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2465
           void *data)
2466
0
{
2467
0
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2468
0
  Elf_Internal_Verneed *t;
2469
0
  Elf_Internal_Vernaux *a;
2470
0
  size_t amt;
2471
2472
  /* We only care about symbols defined in shared objects with version
2473
     information.  */
2474
0
  if (!h->def_dynamic
2475
0
      || h->def_regular
2476
0
      || h->dynindx == -1
2477
0
      || h->verinfo.verdef == NULL
2478
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2479
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2480
0
    return true;
2481
2482
  /* See if we already know about this version.  */
2483
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2484
0
       t != NULL;
2485
0
       t = t->vn_nextref)
2486
0
    {
2487
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2488
0
  continue;
2489
2490
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2491
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2492
0
    return true;
2493
2494
0
      break;
2495
0
    }
2496
2497
  /* This is a new version.  Add it to tree we are building.  */
2498
2499
0
  if (t == NULL)
2500
0
    {
2501
0
      amt = sizeof *t;
2502
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2503
0
      if (t == NULL)
2504
0
  {
2505
0
    rinfo->failed = true;
2506
0
    return false;
2507
0
  }
2508
2509
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2510
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2511
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2512
0
    }
2513
2514
0
  amt = sizeof *a;
2515
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2516
0
  if (a == NULL)
2517
0
    {
2518
0
      rinfo->failed = true;
2519
0
      return false;
2520
0
    }
2521
2522
  /* Note that we are copying a string pointer here, and testing it
2523
     above.  If bfd_elf_string_from_elf_section is ever changed to
2524
     discard the string data when low in memory, this will have to be
2525
     fixed.  */
2526
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2527
2528
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2529
0
  a->vna_nextptr = t->vn_auxptr;
2530
2531
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2532
0
  ++rinfo->vers;
2533
2534
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2535
2536
0
  t->vn_auxptr = a;
2537
2538
0
  return true;
2539
0
}
2540
2541
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2542
   hidden.  Set *T_P to NULL if there is no match.  */
2543
2544
static bool
2545
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2546
             struct elf_link_hash_entry *h,
2547
             const char *version_p,
2548
             struct bfd_elf_version_tree **t_p,
2549
             bool *hide)
2550
0
{
2551
0
  struct bfd_elf_version_tree *t;
2552
2553
  /* Look for the version.  If we find it, it is no longer weak.  */
2554
0
  for (t = info->version_info; t != NULL; t = t->next)
2555
0
    {
2556
0
      if (strcmp (t->name, version_p) == 0)
2557
0
  {
2558
0
    size_t len;
2559
0
    char *alc;
2560
0
    struct bfd_elf_version_expr *d;
2561
2562
0
    len = version_p - h->root.root.string;
2563
0
    alc = (char *) bfd_malloc (len);
2564
0
    if (alc == NULL)
2565
0
      return false;
2566
0
    memcpy (alc, h->root.root.string, len - 1);
2567
0
    alc[len - 1] = '\0';
2568
0
    if (alc[len - 2] == ELF_VER_CHR)
2569
0
      alc[len - 2] = '\0';
2570
2571
0
    h->verinfo.vertree = t;
2572
0
    t->used = true;
2573
0
    d = NULL;
2574
2575
0
    if (t->globals.list != NULL)
2576
0
      d = (*t->match) (&t->globals, NULL, alc);
2577
2578
    /* See if there is anything to force this symbol to
2579
       local scope.  */
2580
0
    if (d == NULL && t->locals.list != NULL)
2581
0
      {
2582
0
        d = (*t->match) (&t->locals, NULL, alc);
2583
0
        if (d != NULL
2584
0
      && h->dynindx != -1
2585
0
      && ! info->export_dynamic)
2586
0
    *hide = true;
2587
0
      }
2588
2589
0
    free (alc);
2590
0
    break;
2591
0
  }
2592
0
    }
2593
2594
0
  *t_p = t;
2595
2596
0
  return true;
2597
0
}
2598
2599
/* Return TRUE if the symbol H is hidden by version script.  */
2600
2601
bool
2602
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2603
           struct elf_link_hash_entry *h)
2604
0
{
2605
0
  const char *p;
2606
0
  bool hide = false;
2607
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2608
2609
  /* Version script only hides symbols defined in regular objects.  */
2610
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2611
0
    return true;
2612
2613
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2614
0
  if (p != NULL && h->verinfo.vertree == NULL)
2615
0
    {
2616
0
      struct bfd_elf_version_tree *t;
2617
2618
0
      ++p;
2619
0
      if (*p == ELF_VER_CHR)
2620
0
  ++p;
2621
2622
0
      if (*p != '\0'
2623
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2624
0
    && hide)
2625
0
  {
2626
0
    if (hide)
2627
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
2628
0
    return true;
2629
0
  }
2630
0
    }
2631
2632
  /* If we don't have a version for this symbol, see if we can find
2633
     something.  */
2634
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2635
0
    {
2636
0
      h->verinfo.vertree
2637
0
  = bfd_find_version_for_sym (info->version_info,
2638
0
            h->root.root.string, &hide);
2639
0
      if (h->verinfo.vertree != NULL && hide)
2640
0
  {
2641
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
2642
0
    return true;
2643
0
  }
2644
0
    }
2645
2646
0
  return false;
2647
0
}
2648
2649
/* Figure out appropriate versions for all the symbols.  We may not
2650
   have the version number script until we have read all of the input
2651
   files, so until that point we don't know which symbols should be
2652
   local.  This function is called via elf_link_hash_traverse.  */
2653
2654
static bool
2655
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2656
0
{
2657
0
  struct elf_info_failed *sinfo;
2658
0
  struct bfd_link_info *info;
2659
0
  elf_backend_data *bed;
2660
0
  struct elf_info_failed eif;
2661
0
  const char *p;
2662
0
  bool hide;
2663
2664
0
  sinfo = (struct elf_info_failed *) data;
2665
0
  info = sinfo->info;
2666
2667
  /* Fix the symbol flags.  */
2668
0
  eif.failed = false;
2669
0
  eif.info = info;
2670
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2671
0
    {
2672
0
      if (eif.failed)
2673
0
  sinfo->failed = true;
2674
0
      return false;
2675
0
    }
2676
2677
0
  bed = get_elf_backend_data (info->output_bfd);
2678
2679
  /* We only need version numbers for symbols defined in regular
2680
     objects.  */
2681
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2682
0
    {
2683
      /* Hide symbols defined in discarded input sections.  */
2684
0
      if ((h->root.type == bfd_link_hash_defined
2685
0
     || h->root.type == bfd_link_hash_defweak)
2686
0
    && discarded_section (h->root.u.def.section))
2687
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2688
0
      return true;
2689
0
    }
2690
2691
0
  hide = false;
2692
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2693
0
  if (p != NULL && h->verinfo.vertree == NULL)
2694
0
    {
2695
0
      struct bfd_elf_version_tree *t;
2696
2697
0
      ++p;
2698
0
      if (*p == ELF_VER_CHR)
2699
0
  ++p;
2700
2701
      /* If there is no version string, we can just return out.  */
2702
0
      if (*p == '\0')
2703
0
  return true;
2704
2705
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2706
0
  {
2707
0
    sinfo->failed = true;
2708
0
    return false;
2709
0
  }
2710
2711
0
      if (hide)
2712
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2713
2714
      /* If we are building an application, we need to create a
2715
   version node for this version.  */
2716
0
      if (t == NULL && bfd_link_executable (info))
2717
0
  {
2718
0
    struct bfd_elf_version_tree **pp;
2719
0
    int version_index;
2720
2721
    /* If we aren't going to export this symbol, we don't need
2722
       to worry about it.  */
2723
0
    if (h->dynindx == -1)
2724
0
      return true;
2725
2726
0
    t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2727
0
                sizeof *t);
2728
0
    if (t == NULL)
2729
0
      {
2730
0
        sinfo->failed = true;
2731
0
        return false;
2732
0
      }
2733
2734
0
    t->name = p;
2735
0
    t->name_indx = (unsigned int) -1;
2736
0
    t->used = true;
2737
2738
0
    version_index = 1;
2739
    /* Don't count anonymous version tag.  */
2740
0
    if (sinfo->info->version_info != NULL
2741
0
        && sinfo->info->version_info->vernum == 0)
2742
0
      version_index = 0;
2743
0
    for (pp = &sinfo->info->version_info;
2744
0
         *pp != NULL;
2745
0
         pp = &(*pp)->next)
2746
0
      ++version_index;
2747
0
    t->vernum = version_index;
2748
2749
0
    *pp = t;
2750
2751
0
    h->verinfo.vertree = t;
2752
0
  }
2753
0
      else if (t == NULL)
2754
0
  {
2755
    /* We could not find the version for a symbol when
2756
       generating a shared archive.  Return an error.  */
2757
0
    _bfd_error_handler
2758
      /* xgettext:c-format */
2759
0
      (_("%pB: version node not found for symbol %s"),
2760
0
       info->output_bfd, h->root.root.string);
2761
0
    bfd_set_error (bfd_error_bad_value);
2762
0
    sinfo->failed = true;
2763
0
    return false;
2764
0
  }
2765
0
    }
2766
2767
  /* If we don't have a version for this symbol, see if we can find
2768
     something.  */
2769
0
  if (!hide
2770
0
      && h->verinfo.vertree == NULL
2771
0
      && sinfo->info->version_info != NULL)
2772
0
    {
2773
0
      h->verinfo.vertree
2774
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2775
0
            h->root.root.string, &hide);
2776
0
      if (h->verinfo.vertree != NULL && hide)
2777
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2778
0
    }
2779
2780
0
  return true;
2781
0
}
2782

2783
/* Read and swap the relocs from the section indicated by SHDR.  This
2784
   may be either a REL or a RELA section.  The relocations are
2785
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2786
   which should have already been allocated to contain enough space.
2787
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2788
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2789
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2790
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2791
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2792
2793
   Returns FALSE if something goes wrong.  */
2794
2795
static bool
2796
elf_link_read_relocs_from_section (bfd *abfd,
2797
           const asection *sec,
2798
           Elf_Internal_Shdr *shdr,
2799
           void **external_relocs_addr,
2800
           size_t *external_relocs_size,
2801
           Elf_Internal_Rela *internal_relocs)
2802
0
{
2803
0
  elf_backend_data *bed;
2804
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2805
0
  const bfd_byte *erela;
2806
0
  const bfd_byte *erelaend;
2807
0
  Elf_Internal_Rela *irela;
2808
0
  Elf_Internal_Shdr *symtab_hdr;
2809
0
  size_t nsyms;
2810
0
  void *external_relocs = *external_relocs_addr;
2811
2812
  /* Position ourselves at the start of the section.  */
2813
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2814
0
    return false;
2815
2816
  /* Read the relocations.  */
2817
0
  *external_relocs_size = shdr->sh_size;
2818
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2819
0
         external_relocs_size,
2820
0
         external_relocs_addr, abfd, true))
2821
0
    return false;
2822
2823
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2824
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2825
2826
0
  bed = get_elf_backend_data (abfd);
2827
2828
  /* Convert the external relocations to the internal format.  */
2829
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2830
0
    swap_in = bed->s->swap_reloc_in;
2831
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2832
0
    swap_in = bed->s->swap_reloca_in;
2833
0
  else
2834
0
    {
2835
0
      bfd_set_error (bfd_error_wrong_format);
2836
0
      return false;
2837
0
    }
2838
2839
0
  erela = (const bfd_byte *) external_relocs;
2840
  /* Setting erelaend like this and comparing with <= handles case of
2841
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2842
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2843
0
  irela = internal_relocs;
2844
0
  while (erela <= erelaend)
2845
0
    {
2846
0
      bfd_vma r_symndx;
2847
2848
0
      (*swap_in) (abfd, erela, irela);
2849
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2850
0
      if (bed->s->arch_size == 64)
2851
0
  r_symndx >>= 24;
2852
0
      if (nsyms > 0)
2853
0
  {
2854
0
    if ((size_t) r_symndx >= nsyms)
2855
0
      {
2856
0
        _bfd_error_handler
2857
    /* xgettext:c-format */
2858
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2859
0
       " for offset %#" PRIx64 " in section `%pA'"),
2860
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2861
0
     (uint64_t) irela->r_offset, sec);
2862
0
        bfd_set_error (bfd_error_bad_value);
2863
0
        return false;
2864
0
      }
2865
0
  }
2866
0
      else if (r_symndx != STN_UNDEF)
2867
0
  {
2868
0
    _bfd_error_handler
2869
      /* xgettext:c-format */
2870
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2871
0
         " for offset %#" PRIx64 " in section `%pA'"
2872
0
         " when the object file has no symbol table"),
2873
0
       abfd, (uint64_t) r_symndx,
2874
0
       (uint64_t) irela->r_offset, sec);
2875
0
    bfd_set_error (bfd_error_bad_value);
2876
0
    return false;
2877
0
  }
2878
0
      irela += bed->s->int_rels_per_ext_rel;
2879
0
      erela += shdr->sh_entsize;
2880
0
    }
2881
2882
0
  return true;
2883
0
}
2884
2885
/* Read and swap the relocs for a section O.  They may have been
2886
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2887
   not NULL, they are used as buffers to read into.  They are known to
2888
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2889
   the return value is allocated using either malloc or bfd_alloc,
2890
   according to the KEEP_MEMORY argument.  If O has two relocation
2891
   sections (both REL and RELA relocations), then the REL_HDR
2892
   relocations will appear first in INTERNAL_RELOCS, followed by the
2893
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2894
   update cache_size.  */
2895
2896
Elf_Internal_Rela *
2897
_bfd_elf_link_info_read_relocs (bfd *abfd,
2898
        struct bfd_link_info *info,
2899
        const asection *o,
2900
        void *external_relocs,
2901
        Elf_Internal_Rela *internal_relocs,
2902
        bool keep_memory)
2903
0
{
2904
0
  void *alloc1 = NULL;
2905
0
  size_t alloc1_size;
2906
0
  Elf_Internal_Rela *alloc2 = NULL;
2907
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2908
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2909
0
  Elf_Internal_Rela *internal_rela_relocs;
2910
2911
0
  if (esdo->relocs != NULL)
2912
0
    return esdo->relocs;
2913
2914
0
  if (o->reloc_count == 0)
2915
0
    return NULL;
2916
2917
0
  if (internal_relocs == NULL)
2918
0
    {
2919
0
      bfd_size_type size;
2920
2921
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2922
0
      if (keep_memory && info)
2923
0
  info->cache_size += size;
2924
0
      internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2925
0
      if (internal_relocs == NULL)
2926
0
  return NULL;
2927
0
    }
2928
2929
0
  alloc1 = external_relocs;
2930
0
  internal_rela_relocs = internal_relocs;
2931
0
  if (esdo->rel.hdr)
2932
0
    {
2933
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2934
0
                &alloc1, &alloc1_size,
2935
0
                internal_relocs))
2936
0
  goto error_return;
2937
0
      external_relocs = (((bfd_byte *) external_relocs)
2938
0
       + esdo->rel.hdr->sh_size);
2939
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2940
0
             * bed->s->int_rels_per_ext_rel);
2941
0
    }
2942
2943
0
  if (esdo->rela.hdr
2944
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2945
0
                &alloc1, &alloc1_size,
2946
0
                internal_rela_relocs)))
2947
0
    goto error_return;
2948
2949
  /* Cache the results for next time, if we can.  */
2950
0
  if (keep_memory)
2951
0
    esdo->relocs = internal_relocs;
2952
2953
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2954
2955
  /* Don't free alloc2, since if it was allocated we are passing it
2956
     back (under the name of internal_relocs).  */
2957
2958
0
  return internal_relocs;
2959
2960
0
 error_return:
2961
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2962
0
  free (alloc2);
2963
0
  return NULL;
2964
0
}
2965
2966
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2967
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2968
   struct bfd_link_info.  */
2969
2970
Elf_Internal_Rela *
2971
_bfd_elf_link_read_relocs (bfd *abfd,
2972
         const asection *o,
2973
         void *external_relocs,
2974
         Elf_Internal_Rela *internal_relocs,
2975
         bool keep_memory)
2976
0
{
2977
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2978
0
           internal_relocs, keep_memory);
2979
2980
0
}
2981
2982
/* Compute the size of, and allocate space for, REL_HDR which is the
2983
   section header for a section containing relocations for O.  */
2984
2985
static bool
2986
_bfd_elf_link_size_reloc_section (bfd *abfd,
2987
          struct bfd_elf_section_reloc_data *reldata)
2988
0
{
2989
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2990
2991
  /* That allows us to calculate the size of the section.  */
2992
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2993
2994
  /* The contents field must last into write_object_contents, so we
2995
     allocate it with bfd_alloc rather than malloc.  Also since we
2996
     cannot be sure that the contents will actually be filled in,
2997
     we zero the allocated space.  */
2998
0
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2999
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3000
0
    return false;
3001
3002
0
  if (reldata->hashes == NULL && reldata->count)
3003
0
    {
3004
0
      struct elf_link_hash_entry **p;
3005
3006
0
      p = ((struct elf_link_hash_entry **)
3007
0
     bfd_zmalloc (reldata->count * sizeof (*p)));
3008
0
      if (p == NULL)
3009
0
  return false;
3010
3011
0
      reldata->hashes = p;
3012
0
    }
3013
3014
0
  return true;
3015
0
}
3016
3017
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3018
   originated from the section given by INPUT_REL_HDR) to the
3019
   OUTPUT_BFD.  */
3020
3021
bool
3022
_bfd_elf_link_output_relocs (bfd *output_bfd,
3023
           asection *input_section,
3024
           Elf_Internal_Shdr *input_rel_hdr,
3025
           Elf_Internal_Rela *internal_relocs,
3026
           struct elf_link_hash_entry **rel_hash)
3027
0
{
3028
0
  Elf_Internal_Rela *irela;
3029
0
  Elf_Internal_Rela *irelaend;
3030
0
  bfd_byte *erel;
3031
0
  struct bfd_elf_section_reloc_data *output_reldata;
3032
0
  asection *output_section;
3033
0
  elf_backend_data *bed;
3034
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3035
0
  struct bfd_elf_section_data *esdo;
3036
3037
0
  output_section = input_section->output_section;
3038
3039
0
  bed = get_elf_backend_data (output_bfd);
3040
0
  esdo = elf_section_data (output_section);
3041
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3042
0
    {
3043
0
      output_reldata = &esdo->rel;
3044
0
      swap_out = bed->s->swap_reloc_out;
3045
0
    }
3046
0
  else if (esdo->rela.hdr
3047
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3048
0
    {
3049
0
      output_reldata = &esdo->rela;
3050
0
      swap_out = bed->s->swap_reloca_out;
3051
0
    }
3052
0
  else
3053
0
    {
3054
0
      _bfd_error_handler
3055
  /* xgettext:c-format */
3056
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3057
0
   output_bfd, input_section->owner, input_section);
3058
0
      bfd_set_error (bfd_error_wrong_format);
3059
0
      return false;
3060
0
    }
3061
3062
0
  erel = output_reldata->hdr->contents;
3063
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3064
0
  irela = internal_relocs;
3065
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3066
0
          * bed->s->int_rels_per_ext_rel);
3067
0
  while (irela < irelaend)
3068
0
    {
3069
0
      if (rel_hash && *rel_hash)
3070
0
  (*rel_hash)->has_reloc = 1;
3071
0
      (*swap_out) (output_bfd, irela, erel);
3072
0
      irela += bed->s->int_rels_per_ext_rel;
3073
0
      erel += input_rel_hdr->sh_entsize;
3074
0
      if (rel_hash)
3075
0
  rel_hash++;
3076
0
    }
3077
3078
  /* Bump the counter, so that we know where to add the next set of
3079
     relocations.  */
3080
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3081
3082
0
  return true;
3083
0
}
3084

3085
/* Make weak undefined symbols in PIE dynamic.  */
3086
3087
bool
3088
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3089
         struct elf_link_hash_entry *h)
3090
0
{
3091
0
  if (bfd_link_pie (info)
3092
0
      && h->dynindx == -1
3093
0
      && h->root.type == bfd_link_hash_undefweak)
3094
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3095
3096
0
  return true;
3097
0
}
3098
3099
/* Fix up the flags for a symbol.  This handles various cases which
3100
   can only be fixed after all the input files are seen.  This is
3101
   currently called by both adjust_dynamic_symbol and
3102
   assign_sym_version, which is unnecessary but perhaps more robust in
3103
   the face of future changes.  */
3104
3105
static bool
3106
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3107
         struct elf_info_failed *eif)
3108
0
{
3109
0
  elf_backend_data *bed;
3110
3111
  /* If this symbol was mentioned in a non-ELF file, try to set
3112
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3113
     permit a non-ELF file to correctly refer to a symbol defined in
3114
     an ELF dynamic object.  */
3115
0
  if (h->non_elf)
3116
0
    {
3117
0
      while (h->root.type == bfd_link_hash_indirect)
3118
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3119
3120
0
      if (h->root.type != bfd_link_hash_defined
3121
0
    && h->root.type != bfd_link_hash_defweak)
3122
0
  {
3123
0
    h->ref_regular = 1;
3124
0
    h->ref_regular_nonweak = 1;
3125
0
  }
3126
0
      else
3127
0
  {
3128
0
    if (h->root.u.def.section->owner != NULL
3129
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3130
0
      == bfd_target_elf_flavour))
3131
0
      {
3132
0
        h->ref_regular = 1;
3133
0
        h->ref_regular_nonweak = 1;
3134
0
      }
3135
0
    else
3136
0
      h->def_regular = 1;
3137
0
  }
3138
3139
0
      if (h->dynindx == -1
3140
0
    && (h->def_dynamic
3141
0
        || h->ref_dynamic))
3142
0
  {
3143
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3144
0
      {
3145
0
        eif->failed = true;
3146
0
        return false;
3147
0
      }
3148
0
  }
3149
0
    }
3150
0
  else
3151
0
    {
3152
      /* Unfortunately, NON_ELF is only correct if the symbol
3153
   was first seen in a non-ELF file.  Fortunately, if the symbol
3154
   was first seen in an ELF file, we're probably OK unless the
3155
   symbol was defined in a non-ELF file.  Catch that case here.
3156
   FIXME: We're still in trouble if the symbol was first seen in
3157
   a dynamic object, and then later in a non-ELF regular object.  */
3158
0
      if ((h->root.type == bfd_link_hash_defined
3159
0
     || h->root.type == bfd_link_hash_defweak)
3160
0
    && !h->def_regular
3161
0
    && (h->root.u.def.section->owner != NULL
3162
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3163
0
     != bfd_target_elf_flavour)
3164
0
        : (bfd_is_abs_section (h->root.u.def.section)
3165
0
     && !h->def_dynamic)))
3166
0
  h->def_regular = 1;
3167
0
    }
3168
3169
  /* Backend specific symbol fixup.  */
3170
0
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3171
0
  if (bed->elf_backend_fixup_symbol
3172
0
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3173
0
    return false;
3174
3175
  /* If this is a final link, and the symbol was defined as a common
3176
     symbol in a regular object file, and there was no definition in
3177
     any dynamic object, then the linker will have allocated space for
3178
     the symbol in a common section but the DEF_REGULAR
3179
     flag will not have been set.  */
3180
0
  if (h->root.type == bfd_link_hash_defined
3181
0
      && !h->def_regular
3182
0
      && h->ref_regular
3183
0
      && !h->def_dynamic
3184
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3185
0
    h->def_regular = 1;
3186
3187
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3188
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3189
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3190
3191
  /* If a weak undefined symbol has non-default visibility, we also
3192
     hide it from the dynamic linker.  */
3193
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3194
0
     && h->root.type == bfd_link_hash_undefweak)
3195
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3196
3197
  /* A hidden versioned symbol in executable should be forced local if
3198
     it is is locally defined, not referenced by shared library and not
3199
     exported.  */
3200
0
  else if (bfd_link_executable (eif->info)
3201
0
     && h->versioned == versioned_hidden
3202
0
     && !eif->info->export_dynamic
3203
0
     && !h->dynamic
3204
0
     && !h->ref_dynamic
3205
0
     && h->def_regular)
3206
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3207
3208
  /* If -Bsymbolic was used (which means to bind references to global
3209
     symbols to the definition within the shared object), and this
3210
     symbol was defined in a regular object, then it actually doesn't
3211
     need a PLT entry.  Likewise, if the symbol has non-default
3212
     visibility.  If the symbol has hidden or internal visibility, we
3213
     will force it local.  */
3214
0
  else if (h->needs_plt
3215
0
     && bfd_link_pic (eif->info)
3216
0
     && is_elf_hash_table (eif->info->hash)
3217
0
     && (SYMBOLIC_BIND (eif->info, h)
3218
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3219
0
     && h->def_regular)
3220
0
    {
3221
0
      bool force_local;
3222
3223
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3224
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3225
0
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3226
0
    }
3227
3228
  /* If this is a weak defined symbol in a dynamic object, and we know
3229
     the real definition in the dynamic object, copy interesting flags
3230
     over to the real definition.  */
3231
0
  if (h->is_weakalias)
3232
0
    {
3233
0
      struct elf_link_hash_entry *def = weakdef (h);
3234
3235
      /* If the real definition is defined by a regular object file,
3236
   don't do anything special.  See the longer description in
3237
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3238
   bfd_link_hash_defined as it was when put on the alias list
3239
   then it must have originally been a versioned symbol (for
3240
   which a non-versioned indirect symbol is created) and later
3241
   a definition for the non-versioned symbol is found.  In that
3242
   case the indirection is flipped with the versioned symbol
3243
   becoming an indirect pointing at the non-versioned symbol.
3244
   Thus, not an alias any more.  */
3245
0
      if (def->def_regular
3246
0
    || def->root.type != bfd_link_hash_defined)
3247
0
  {
3248
0
    h = def;
3249
0
    while ((h = h->u.alias) != def)
3250
0
      h->is_weakalias = 0;
3251
0
  }
3252
0
      else
3253
0
  {
3254
0
    while (h->root.type == bfd_link_hash_indirect)
3255
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3256
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3257
0
          || h->root.type == bfd_link_hash_defweak);
3258
0
    BFD_ASSERT (def->def_dynamic);
3259
0
    (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3260
0
  }
3261
0
    }
3262
3263
0
  return true;
3264
0
}
3265
3266
/* Make the backend pick a good value for a dynamic symbol.  This is
3267
   called via elf_link_hash_traverse, and also calls itself
3268
   recursively.  */
3269
3270
static bool
3271
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3272
0
{
3273
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
3274
0
  struct elf_link_hash_table *htab;
3275
0
  elf_backend_data *bed;
3276
3277
0
  if (! is_elf_hash_table (eif->info->hash))
3278
0
    return false;
3279
3280
0
  htab = elf_hash_table (eif->info);
3281
0
  if (h->forced_local && h->dynindx != -1)
3282
0
    htab->has_local_dynsyms = true;
3283
3284
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3285
0
  if (h->root.type == bfd_link_hash_indirect)
3286
0
    return true;
3287
3288
  /* Fix the symbol flags.  */
3289
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3290
0
    return false;
3291
3292
0
  bed = get_elf_backend_data (htab->dynobj);
3293
3294
0
  if (h->root.type == bfd_link_hash_undefweak)
3295
0
    {
3296
0
      if (eif->info->dynamic_undefined_weak == 0)
3297
0
  (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3298
0
      else if (eif->info->dynamic_undefined_weak > 0
3299
0
         && h->ref_regular
3300
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3301
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3302
0
              h->root.root.string))
3303
0
  {
3304
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3305
0
      {
3306
0
        eif->failed = true;
3307
0
        return false;
3308
0
      }
3309
0
  }
3310
0
    }
3311
3312
  /* If this symbol does not require a PLT entry, and it is not
3313
     defined by a dynamic object, or is not referenced by a regular
3314
     object, ignore it.  We do have to handle a weak defined symbol,
3315
     even if no regular object refers to it, if we decided to add it
3316
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3317
     about symbols which are defined by one dynamic object and
3318
     referenced by another one?  */
3319
0
  if (!h->needs_plt
3320
0
      && h->type != STT_GNU_IFUNC
3321
0
      && (h->def_regular
3322
0
    || !h->def_dynamic
3323
0
    || (!h->ref_regular
3324
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3325
0
    {
3326
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3327
0
      return true;
3328
0
    }
3329
3330
  /* If we've already adjusted this symbol, don't do it again.  This
3331
     can happen via a recursive call.  */
3332
0
  if (h->dynamic_adjusted)
3333
0
    return true;
3334
3335
  /* Don't look at this symbol again.  Note that we must set this
3336
     after checking the above conditions, because we may look at a
3337
     symbol once, decide not to do anything, and then get called
3338
     recursively later after REF_REGULAR is set below.  */
3339
0
  h->dynamic_adjusted = 1;
3340
3341
  /* If this is a weak definition, and we know a real definition, and
3342
     the real symbol is not itself defined by a regular object file,
3343
     then get a good value for the real definition.  We handle the
3344
     real symbol first, for the convenience of the backend routine.
3345
3346
     Note that there is a confusing case here.  If the real definition
3347
     is defined by a regular object file, we don't get the real symbol
3348
     from the dynamic object, but we do get the weak symbol.  If the
3349
     processor backend uses a COPY reloc, then if some routine in the
3350
     dynamic object changes the real symbol, we will not see that
3351
     change in the corresponding weak symbol.  This is the way other
3352
     ELF linkers work as well, and seems to be a result of the shared
3353
     library model.
3354
3355
     I will clarify this issue.  Most SVR4 shared libraries define the
3356
     variable _timezone and define timezone as a weak synonym.  The
3357
     tzset call changes _timezone.  If you write
3358
       extern int timezone;
3359
       int _timezone = 5;
3360
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3361
     you might expect that, since timezone is a synonym for _timezone,
3362
     the same number will print both times.  However, if the processor
3363
     backend uses a COPY reloc, then actually timezone will be copied
3364
     into your process image, and, since you define _timezone
3365
     yourself, _timezone will not.  Thus timezone and _timezone will
3366
     wind up at different memory locations.  The tzset call will set
3367
     _timezone, leaving timezone unchanged.  */
3368
3369
0
  if (h->is_weakalias)
3370
0
    {
3371
0
      struct elf_link_hash_entry *def = weakdef (h);
3372
3373
      /* If we get to this point, there is an implicit reference to
3374
   the alias by a regular object file via the weak symbol H.  */
3375
0
      def->ref_regular = 1;
3376
3377
      /* Ensure that the backend adjust_dynamic_symbol function sees
3378
   the strong alias before H by recursively calling ourselves.  */
3379
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3380
0
  return false;
3381
0
    }
3382
3383
  /* If a symbol has no type and no size and does not require a PLT
3384
     entry, then we are probably about to do the wrong thing here: we
3385
     are probably going to create a COPY reloc for an empty object.
3386
     This case can arise when a shared object is built with assembly
3387
     code, and the assembly code fails to set the symbol type.  */
3388
0
  if (h->size == 0
3389
0
      && h->type == STT_NOTYPE
3390
0
      && !h->needs_plt)
3391
0
    _bfd_error_handler
3392
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3393
0
       h->root.root.string);
3394
3395
0
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3396
0
    {
3397
0
      eif->failed = true;
3398
0
      return false;
3399
0
    }
3400
3401
0
  return true;
3402
0
}
3403
3404
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3405
   DYNBSS.  */
3406
3407
bool
3408
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3409
            struct elf_link_hash_entry *h,
3410
            asection *dynbss)
3411
0
{
3412
0
  unsigned int power_of_two;
3413
0
  bfd_vma mask;
3414
0
  asection *sec = h->root.u.def.section;
3415
3416
  /* The section alignment of the definition is the maximum alignment
3417
     requirement of symbols defined in the section.  Since we don't
3418
     know the symbol alignment requirement, we start with the
3419
     maximum alignment and check low bits of the symbol address
3420
     for the minimum alignment.  */
3421
0
  power_of_two = bfd_section_alignment (sec);
3422
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3423
0
  while ((h->root.u.def.value & mask) != 0)
3424
0
    {
3425
0
       mask >>= 1;
3426
0
       --power_of_two;
3427
0
    }
3428
3429
  /* Adjust the section alignment if needed.  */
3430
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3431
0
    return false;
3432
3433
  /* We make sure that the symbol will be aligned properly.  */
3434
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3435
3436
  /* Define the symbol as being at this point in DYNBSS.  */
3437
0
  h->root.u.def.section = dynbss;
3438
0
  h->root.u.def.value = dynbss->size;
3439
3440
  /* Increment the size of DYNBSS to make room for the symbol.  */
3441
0
  dynbss->size += h->size;
3442
3443
  /* No error if extern_protected_data is true.  */
3444
0
  if (h->protected_def
3445
0
      && (!info->extern_protected_data
3446
0
    || (info->extern_protected_data < 0
3447
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3448
0
    info->callbacks->einfo
3449
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3450
0
       h->root.root.string);
3451
3452
0
  return true;
3453
0
}
3454
3455
/* Adjust all external symbols pointing into SEC_MERGE sections
3456
   to reflect the object merging within the sections.  */
3457
3458
static bool
3459
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3460
0
{
3461
0
  asection *sec;
3462
3463
0
  if ((h->root.type == bfd_link_hash_defined
3464
0
       || h->root.type == bfd_link_hash_defweak)
3465
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3466
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3467
0
    {
3468
0
      bfd *output_bfd = (bfd *) data;
3469
3470
0
      h->root.u.def.value =
3471
0
  _bfd_merged_section_offset (output_bfd,
3472
0
            &h->root.u.def.section,
3473
0
            h->root.u.def.value);
3474
0
    }
3475
3476
0
  return true;
3477
0
}
3478
3479
/* Returns false if the symbol referred to by H should be considered
3480
   to resolve local to the current module, and true if it should be
3481
   considered to bind dynamically.  */
3482
3483
bool
3484
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3485
         struct bfd_link_info *info,
3486
         bool not_local_protected)
3487
0
{
3488
0
  bool binding_stays_local_p;
3489
0
  elf_backend_data *bed;
3490
0
  struct elf_link_hash_table *hash_table;
3491
3492
0
  if (h == NULL)
3493
0
    return false;
3494
3495
0
  while (h->root.type == bfd_link_hash_indirect
3496
0
   || h->root.type == bfd_link_hash_warning)
3497
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3498
3499
  /* If it was forced local, then clearly it's not dynamic.  */
3500
0
  if (h->dynindx == -1)
3501
0
    return false;
3502
0
  if (h->forced_local)
3503
0
    return false;
3504
3505
  /* Identify the cases where name binding rules say that a
3506
     visible symbol resolves locally.  */
3507
0
  binding_stays_local_p = (bfd_link_executable (info)
3508
0
         || SYMBOLIC_BIND (info, h));
3509
3510
0
  switch (ELF_ST_VISIBILITY (h->other))
3511
0
    {
3512
0
    case STV_INTERNAL:
3513
0
    case STV_HIDDEN:
3514
0
      return false;
3515
3516
0
    case STV_PROTECTED:
3517
0
      hash_table = elf_hash_table (info);
3518
0
      if (!is_elf_hash_table (&hash_table->root))
3519
0
  return false;
3520
3521
0
      bed = get_elf_backend_data (hash_table->dynobj);
3522
3523
      /* Proper resolution for function pointer equality may require
3524
   that these symbols perhaps be resolved dynamically, even though
3525
   we should be resolving them to the current module.  */
3526
0
      if (!not_local_protected || !bed->is_function_type (h->type))
3527
0
  binding_stays_local_p = true;
3528
0
      break;
3529
3530
0
    default:
3531
0
      break;
3532
0
    }
3533
3534
  /* If it isn't defined locally, then clearly it's dynamic.  */
3535
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3536
0
    return true;
3537
3538
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3539
     us that it remains local.  */
3540
0
  return !binding_stays_local_p;
3541
0
}
3542
3543
/* Return true if the symbol referred to by H should be considered
3544
   to resolve local to the current module, and false otherwise.  Differs
3545
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3546
   undefined symbols.  The two functions are virtually identical except
3547
   for the place where dynindx == -1 is tested.  If that test is true,
3548
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3549
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3550
   defined symbols.
3551
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3552
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3553
   treatment of undefined weak symbols.  For those that do not make
3554
   undefined weak symbols dynamic, both functions may return false.  */
3555
3556
bool
3557
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3558
            struct bfd_link_info *info,
3559
            bool local_protected)
3560
0
{
3561
0
  elf_backend_data *bed;
3562
0
  struct elf_link_hash_table *hash_table;
3563
3564
  /* If it's a local sym, of course we resolve locally.  */
3565
0
  if (h == NULL)
3566
0
    return true;
3567
3568
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3569
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3570
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3571
0
    return true;
3572
3573
  /* Forced local symbols resolve locally.  */
3574
0
  if (h->forced_local)
3575
0
    return true;
3576
3577
  /* Common symbols that become definitions don't get the DEF_REGULAR
3578
     flag set, so test it first, and don't bail out.  */
3579
0
  if (ELF_COMMON_DEF_P (h))
3580
0
    /* Do nothing.  */;
3581
  /* If we don't have a definition in a regular file, then we can't
3582
     resolve locally.  The sym is either undefined or dynamic.  */
3583
0
  else if (!h->def_regular)
3584
0
    return false;
3585
3586
  /* Non-dynamic symbols resolve locally.  */
3587
0
  if (h->dynindx == -1)
3588
0
    return true;
3589
3590
  /* At this point, we know the symbol is defined and dynamic.  In an
3591
     executable it must resolve locally, likewise when building symbolic
3592
     shared libraries.  */
3593
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3594
0
    return true;
3595
3596
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3597
     with default visibility might not resolve locally.  */
3598
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3599
0
    return false;
3600
3601
0
  hash_table = elf_hash_table (info);
3602
0
  if (!is_elf_hash_table (&hash_table->root))
3603
0
    return true;
3604
3605
  /* STV_PROTECTED symbols with indirect external access are local. */
3606
0
  if (info->indirect_extern_access > 0)
3607
0
    return true;
3608
3609
0
  bed = get_elf_backend_data (hash_table->dynobj);
3610
3611
  /* If extern_protected_data is false, STV_PROTECTED non-function
3612
     symbols are local.  */
3613
0
  if ((!info->extern_protected_data
3614
0
       || (info->extern_protected_data < 0
3615
0
     && !bed->extern_protected_data))
3616
0
      && !bed->is_function_type (h->type))
3617
0
    return true;
3618
3619
  /* Function pointer equality tests may require that STV_PROTECTED
3620
     symbols be treated as dynamic symbols.  If the address of a
3621
     function not defined in an executable is set to that function's
3622
     plt entry in the executable, then the address of the function in
3623
     a shared library must also be the plt entry in the executable.  */
3624
0
  return local_protected;
3625
0
}
3626
3627
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3628
   aligned.  Returns the first TLS output section.  */
3629
3630
struct bfd_section *
3631
bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3632
0
{
3633
0
  struct bfd_section *sec, *tls;
3634
0
  unsigned int align = 0;
3635
3636
0
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
3637
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3638
0
      break;
3639
0
  tls = sec;
3640
3641
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3642
0
    if (sec->alignment_power > align)
3643
0
      align = sec->alignment_power;
3644
3645
0
  elf_hash_table (info)->tls_sec = tls;
3646
3647
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3648
     alignment, so that the tls segment starts aligned.  */
3649
0
  if (tls != NULL)
3650
0
    (void) bfd_link_align_section (tls, align);
3651
3652
0
  return tls;
3653
0
}
3654
3655
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3656
static bool
3657
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3658
          Elf_Internal_Sym *sym)
3659
0
{
3660
0
  elf_backend_data *bed;
3661
3662
  /* Local symbols do not count, but target specific ones might.  */
3663
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3664
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3665
0
    return false;
3666
3667
0
  bed = get_elf_backend_data (abfd);
3668
  /* Function symbols do not count.  */
3669
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3670
0
    return false;
3671
3672
  /* If the section is undefined, then so is the symbol.  */
3673
0
  if (sym->st_shndx == SHN_UNDEF)
3674
0
    return false;
3675
3676
  /* If the symbol is defined in the common section, then
3677
     it is a common definition and so does not count.  */
3678
0
  if (bed->common_definition (sym))
3679
0
    return false;
3680
3681
  /* If the symbol is in a target specific section then we
3682
     must rely upon the backend to tell us what it is.  */
3683
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3684
    /* FIXME - this function is not coded yet:
3685
3686
       return _bfd_is_global_symbol_definition (abfd, sym);
3687
3688
       Instead for now assume that the definition is not global,
3689
       Even if this is wrong, at least the linker will behave
3690
       in the same way that it used to do.  */
3691
0
    return false;
3692
3693
0
  return true;
3694
0
}
3695
3696
/* Search the symbol table of the archive element of the archive ABFD
3697
   whose archive map contains a mention of SYMDEF, and determine if
3698
   the symbol is defined in this element.  */
3699
static bool
3700
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3701
0
{
3702
0
  Elf_Internal_Shdr * hdr;
3703
0
  size_t symcount;
3704
0
  size_t extsymcount;
3705
0
  size_t extsymoff;
3706
0
  Elf_Internal_Sym *isymbuf;
3707
0
  Elf_Internal_Sym *isym;
3708
0
  Elf_Internal_Sym *isymend;
3709
0
  bool result;
3710
3711
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3712
0
  if (abfd == NULL)
3713
0
    return false;
3714
3715
0
  if (! bfd_check_format (abfd, bfd_object))
3716
0
    return false;
3717
3718
  /* Select the appropriate symbol table.  If we don't know if the
3719
     object file is an IR object, give linker LTO plugin a chance to
3720
     get the correct symbol table.  */
3721
0
  if (abfd->plugin_format == bfd_plugin_yes
3722
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3723
0
      || (abfd->plugin_format == bfd_plugin_unknown
3724
0
    && bfd_link_plugin_object_p (abfd)))
3725
0
    {
3726
      /* Use the IR symbol table if the object has been claimed by
3727
   plugin.  */
3728
0
      abfd = abfd->plugin_dummy_bfd;
3729
0
      hdr = &elf_tdata (abfd)->symtab_hdr;
3730
0
    }
3731
0
  else
3732
0
    {
3733
0
      if (elf_use_dt_symtab_p (abfd))
3734
0
  {
3735
0
    bfd_set_error (bfd_error_wrong_format);
3736
0
    return false;
3737
0
  }
3738
3739
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3740
0
  hdr = &elf_tdata (abfd)->symtab_hdr;
3741
0
      else
3742
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3743
0
    }
3744
3745
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3746
3747
  /* The sh_info field of the symtab header tells us where the
3748
     external symbols start.  We don't care about the local symbols.  */
3749
0
  if (elf_bad_symtab (abfd))
3750
0
    {
3751
0
      extsymcount = symcount;
3752
0
      extsymoff = 0;
3753
0
    }
3754
0
  else
3755
0
    {
3756
0
      extsymcount = symcount - hdr->sh_info;
3757
0
      extsymoff = hdr->sh_info;
3758
0
    }
3759
3760
0
  if (extsymcount == 0)
3761
0
    return false;
3762
3763
  /* Read in the symbol table.  */
3764
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3765
0
          NULL, NULL, NULL);
3766
0
  if (isymbuf == NULL)
3767
0
    return false;
3768
3769
  /* Scan the symbol table looking for SYMDEF.  */
3770
0
  result = false;
3771
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3772
0
    {
3773
0
      const char *name;
3774
3775
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3776
0
                isym->st_name);
3777
0
      if (name == NULL)
3778
0
  break;
3779
3780
0
      if (strcmp (name, symdef->name) == 0)
3781
0
  {
3782
0
    result = is_global_data_symbol_definition (abfd, isym);
3783
0
    break;
3784
0
  }
3785
0
    }
3786
3787
0
  free (isymbuf);
3788
3789
0
  return result;
3790
0
}
3791

3792
/* Add an entry to the .dynamic table.  */
3793
3794
bool
3795
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3796
          bfd_vma tag,
3797
          bfd_vma val)
3798
0
{
3799
0
  struct elf_link_hash_table *hash_table;
3800
0
  elf_backend_data *bed;
3801
0
  asection *s;
3802
0
  bfd_size_type newsize;
3803
0
  bfd_byte *newcontents;
3804
0
  Elf_Internal_Dyn dyn;
3805
3806
0
  hash_table = elf_hash_table (info);
3807
0
  if (! is_elf_hash_table (&hash_table->root))
3808
0
    return false;
3809
3810
0
  if (tag == DT_RELA || tag == DT_REL)
3811
0
    hash_table->dynamic_relocs = true;
3812
3813
0
  bed = get_elf_backend_data (hash_table->dynobj);
3814
0
  s = hash_table->dynamic;
3815
0
  BFD_ASSERT (s != NULL);
3816
3817
0
  newsize = s->size + bed->s->sizeof_dyn;
3818
0
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3819
0
  if (newcontents == NULL)
3820
0
    return false;
3821
3822
0
  dyn.d_tag = tag;
3823
0
  dyn.d_un.d_val = val;
3824
0
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3825
3826
0
  s->size = newsize;
3827
0
  s->contents = newcontents;
3828
3829
0
  return true;
3830
0
}
3831
3832
/* Strip zero-sized dynamic sections.  */
3833
3834
bool
3835
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3836
0
{
3837
0
  struct elf_link_hash_table *hash_table;
3838
0
  elf_backend_data *bed;
3839
0
  asection *s, *sdynamic, **pp;
3840
0
  asection *rela_dyn, *rel_dyn;
3841
0
  Elf_Internal_Dyn dyn;
3842
0
  bfd_byte *extdyn, *next;
3843
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3844
0
  bool strip_zero_sized;
3845
0
  bool strip_zero_sized_plt;
3846
3847
0
  if (bfd_link_relocatable (info))
3848
0
    return true;
3849
3850
0
  hash_table = elf_hash_table (info);
3851
0
  if (!is_elf_hash_table (&hash_table->root))
3852
0
    return false;
3853
3854
0
  if (!hash_table->dynobj)
3855
0
    return true;
3856
3857
0
  sdynamic= hash_table->dynamic;
3858
0
  if (!sdynamic)
3859
0
    return true;
3860
3861
0
  bed = get_elf_backend_data (hash_table->dynobj);
3862
0
  swap_dyn_in = bed->s->swap_dyn_in;
3863
3864
0
  strip_zero_sized = false;
3865
0
  strip_zero_sized_plt = false;
3866
3867
  /* Strip zero-sized dynamic sections.  */
3868
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3869
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3870
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3871
0
    if (s->size == 0
3872
0
  && (s == rela_dyn
3873
0
      || s == rel_dyn
3874
0
      || s == hash_table->srelplt->output_section
3875
0
      || s == hash_table->splt->output_section))
3876
0
      {
3877
0
  *pp = s->next;
3878
0
  info->output_bfd->section_count--;
3879
0
  strip_zero_sized = true;
3880
0
  if (s == rela_dyn)
3881
0
    s = rela_dyn;
3882
0
  if (s == rel_dyn)
3883
0
    s = rel_dyn;
3884
0
  else if (s == hash_table->splt->output_section)
3885
0
    {
3886
0
      s = hash_table->splt;
3887
0
      strip_zero_sized_plt = true;
3888
0
    }
3889
0
  else
3890
0
    s = hash_table->srelplt;
3891
0
  s->flags |= SEC_EXCLUDE;
3892
0
  s->output_section = bfd_abs_section_ptr;
3893
0
      }
3894
0
    else
3895
0
      pp = &s->next;
3896
3897
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3898
0
    for (extdyn = sdynamic->contents;
3899
0
   extdyn < sdynamic->contents + sdynamic->size;
3900
0
   extdyn = next)
3901
0
      {
3902
0
  next = extdyn + bed->s->sizeof_dyn;
3903
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3904
0
  switch (dyn.d_tag)
3905
0
    {
3906
0
    default:
3907
0
      break;
3908
0
    case DT_JMPREL:
3909
0
    case DT_PLTRELSZ:
3910
0
    case DT_PLTREL:
3911
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3912
         the procedure linkage table (the .plt section) has been
3913
         removed.  */
3914
0
      memmove (extdyn, next,
3915
0
         sdynamic->size - (next - sdynamic->contents));
3916
0
      next = extdyn;
3917
0
    }
3918
0
      }
3919
3920
0
  if (strip_zero_sized)
3921
0
    {
3922
      /* Regenerate program headers.  */
3923
0
      elf_seg_map (info->output_bfd) = NULL;
3924
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3925
0
    }
3926
3927
0
  return true;
3928
0
}
3929
3930
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3931
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3932
3933
int
3934
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3935
0
{
3936
0
  struct elf_link_hash_table *hash_table;
3937
0
  size_t strindex;
3938
0
  const char *soname;
3939
3940
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3941
0
    return -1;
3942
3943
0
  hash_table = elf_hash_table (info);
3944
0
  soname = elf_dt_name (abfd);
3945
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3946
0
  if (strindex == (size_t) -1)
3947
0
    return -1;
3948
3949
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3950
0
    {
3951
0
      asection *sdyn;
3952
0
      elf_backend_data *bed;
3953
0
      bfd_byte *extdyn;
3954
3955
0
      bed = get_elf_backend_data (hash_table->dynobj);
3956
0
      sdyn = hash_table->dynamic;
3957
0
      if (sdyn != NULL && sdyn->size != 0)
3958
0
  for (extdyn = sdyn->contents;
3959
0
       extdyn < sdyn->contents + sdyn->size;
3960
0
       extdyn += bed->s->sizeof_dyn)
3961
0
    {
3962
0
      Elf_Internal_Dyn dyn;
3963
3964
0
      bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3965
0
      if (dyn.d_tag == DT_NEEDED
3966
0
    && dyn.d_un.d_val == strindex)
3967
0
        {
3968
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3969
0
    return 1;
3970
0
        }
3971
0
    }
3972
0
    }
3973
3974
0
  if (!bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3975
0
    return -1;
3976
3977
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3978
0
    return -1;
3979
3980
0
  return 0;
3981
0
}
3982
3983
/* Return true if SONAME is on the needed list between NEEDED and STOP
3984
   (or the end of list if STOP is NULL), and needed by a library that
3985
   will be loaded.  */
3986
3987
static bool
3988
on_needed_list (const char *soname,
3989
    struct bfd_link_needed_list *needed,
3990
    struct bfd_link_needed_list *stop)
3991
0
{
3992
0
  struct bfd_link_needed_list *look;
3993
0
  for (look = needed; look != stop; look = look->next)
3994
0
    if (strcmp (soname, look->name) == 0
3995
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3996
      /* If needed by a library that itself is not directly
3997
         needed, recursively check whether that library is
3998
         indirectly needed.  Since we add DT_NEEDED entries to
3999
         the end of the list, library dependencies appear after
4000
         the library.  Therefore search prior to the current
4001
         LOOK, preventing possible infinite recursion.  */
4002
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4003
0
      return true;
4004
4005
0
  return false;
4006
0
}
4007
4008
/* Sort symbol by value, section, size, and type.  */
4009
static int
4010
elf_sort_symbol (const void *arg1, const void *arg2)
4011
0
{
4012
0
  const struct elf_link_hash_entry *h1;
4013
0
  const struct elf_link_hash_entry *h2;
4014
0
  bfd_signed_vma vdiff;
4015
0
  int sdiff;
4016
0
  const char *n1;
4017
0
  const char *n2;
4018
4019
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4020
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4021
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4022
0
  if (vdiff != 0)
4023
0
    return vdiff > 0 ? 1 : -1;
4024
4025
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4026
0
  if (sdiff != 0)
4027
0
    return sdiff;
4028
4029
  /* Sort so that sized symbols are selected over zero size symbols.  */
4030
0
  vdiff = h1->size - h2->size;
4031
0
  if (vdiff != 0)
4032
0
    return vdiff > 0 ? 1 : -1;
4033
4034
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4035
0
  if (h1->type != h2->type)
4036
0
    return h1->type - h2->type;
4037
4038
  /* If symbols are properly sized and typed, and multiple strong
4039
     aliases are not defined in a shared library by the user we
4040
     shouldn't get here.  Unfortunately linker script symbols like
4041
     __bss_start sometimes match a user symbol defined at the start of
4042
     .bss without proper size and type.  We'd like to preference the
4043
     user symbol over reserved system symbols.  Sort on leading
4044
     underscores.  */
4045
0
  n1 = h1->root.root.string;
4046
0
  n2 = h2->root.root.string;
4047
0
  while (*n1 == *n2)
4048
0
    {
4049
0
      if (*n1 == 0)
4050
0
  break;
4051
0
      ++n1;
4052
0
      ++n2;
4053
0
    }
4054
0
  if (*n1 == '_')
4055
0
    return -1;
4056
0
  if (*n2 == '_')
4057
0
    return 1;
4058
4059
  /* Final sort on name selects user symbols like '_u' over reserved
4060
     system symbols like '_Z' and also will avoid qsort instability.  */
4061
0
  return *n1 - *n2;
4062
0
}
4063
4064
/* This function is used to adjust offsets into .dynstr for
4065
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4066
4067
static bool
4068
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4069
0
{
4070
0
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
4071
4072
0
  if (h->dynindx != -1)
4073
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4074
0
  return true;
4075
0
}
4076
4077
/* Assign string offsets in .dynstr, update all structures referencing
4078
   them.  */
4079
4080
static bool
4081
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4082
0
{
4083
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4084
0
  struct elf_link_local_dynamic_entry *entry;
4085
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4086
0
  bfd *dynobj = hash_table->dynobj;
4087
0
  asection *sdyn;
4088
0
  bfd_size_type size;
4089
0
  elf_backend_data *bed;
4090
0
  bfd_byte *extdyn;
4091
4092
0
  _bfd_elf_strtab_finalize (dynstr);
4093
0
  size = _bfd_elf_strtab_size (dynstr);
4094
4095
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4096
4097
0
  if (info->callbacks->examine_strtab)
4098
0
    info->callbacks->examine_strtab (dynstr);
4099
4100
0
  bed = get_elf_backend_data (dynobj);
4101
0
  sdyn = hash_table->dynamic;
4102
0
  BFD_ASSERT (sdyn != NULL);
4103
4104
  /* Update all .dynamic entries referencing .dynstr strings.  */
4105
0
  for (extdyn = sdyn->contents;
4106
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4107
0
       extdyn += bed->s->sizeof_dyn)
4108
0
    {
4109
0
      Elf_Internal_Dyn dyn;
4110
4111
0
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4112
0
      switch (dyn.d_tag)
4113
0
  {
4114
0
  case DT_STRSZ:
4115
0
    dyn.d_un.d_val = size;
4116
0
    break;
4117
0
  case DT_NEEDED:
4118
0
  case DT_SONAME:
4119
0
  case DT_RPATH:
4120
0
  case DT_RUNPATH:
4121
0
  case DT_FILTER:
4122
0
  case DT_AUXILIARY:
4123
0
  case DT_AUDIT:
4124
0
  case DT_DEPAUDIT:
4125
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4126
0
    break;
4127
0
  default:
4128
0
    continue;
4129
0
  }
4130
0
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4131
0
    }
4132
4133
  /* Now update local dynamic symbols.  */
4134
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4135
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4136
0
              entry->isym.st_name);
4137
4138
  /* And the rest of dynamic symbols.  */
4139
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4140
4141
  /* Adjust version definitions.  */
4142
0
  if (elf_tdata (output_bfd)->cverdefs)
4143
0
    {
4144
0
      asection *s;
4145
0
      bfd_byte *p;
4146
0
      size_t i;
4147
0
      Elf_Internal_Verdef def;
4148
0
      Elf_Internal_Verdaux defaux;
4149
4150
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4151
0
      p = s->contents;
4152
0
      do
4153
0
  {
4154
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4155
0
           &def);
4156
0
    p += sizeof (Elf_External_Verdef);
4157
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4158
0
      continue;
4159
0
    for (i = 0; i < def.vd_cnt; ++i)
4160
0
      {
4161
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4162
0
          (Elf_External_Verdaux *) p, &defaux);
4163
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4164
0
              defaux.vda_name);
4165
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4166
0
           &defaux, (Elf_External_Verdaux *) p);
4167
0
        p += sizeof (Elf_External_Verdaux);
4168
0
      }
4169
0
  }
4170
0
      while (def.vd_next);
4171
0
    }
4172
4173
  /* Adjust version references.  */
4174
0
  if (elf_tdata (output_bfd)->verref)
4175
0
    {
4176
0
      asection *s;
4177
0
      bfd_byte *p;
4178
0
      size_t i;
4179
0
      Elf_Internal_Verneed need;
4180
0
      Elf_Internal_Vernaux needaux;
4181
4182
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4183
0
      p = s->contents;
4184
0
      do
4185
0
  {
4186
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4187
0
            &need);
4188
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4189
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4190
0
             (Elf_External_Verneed *) p);
4191
0
    p += sizeof (Elf_External_Verneed);
4192
0
    for (i = 0; i < need.vn_cnt; ++i)
4193
0
      {
4194
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4195
0
          (Elf_External_Vernaux *) p, &needaux);
4196
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4197
0
               needaux.vna_name);
4198
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4199
0
           &needaux,
4200
0
           (Elf_External_Vernaux *) p);
4201
0
        p += sizeof (Elf_External_Vernaux);
4202
0
      }
4203
0
  }
4204
0
      while (need.vn_next);
4205
0
    }
4206
4207
0
  return true;
4208
0
}
4209

4210
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4211
   The default is to only match when the INPUT and OUTPUT are exactly
4212
   the same target.  */
4213
4214
bool
4215
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4216
            const bfd_target *output)
4217
0
{
4218
0
  return input == output;
4219
0
}
4220
4221
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4222
   This version is used when different targets for the same architecture
4223
   are virtually identical.  */
4224
4225
bool
4226
_bfd_elf_relocs_compatible (const bfd_target *input,
4227
          const bfd_target *output)
4228
0
{
4229
0
  elf_backend_data *obed, *ibed;
4230
4231
0
  if (input == output)
4232
0
    return true;
4233
4234
0
  ibed = xvec_get_elf_backend_data (input);
4235
0
  obed = xvec_get_elf_backend_data (output);
4236
4237
0
  if (ibed->arch != obed->arch)
4238
0
    return false;
4239
4240
  /* If both backends are using this function, deem them compatible.  */
4241
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4242
0
}
4243
4244
/* Make a special call to the linker "notice" function to tell it that
4245
   we are about to handle an as-needed lib, or have finished
4246
   processing the lib.  */
4247
4248
bool
4249
_bfd_elf_notice_as_needed (bfd *ibfd,
4250
         struct bfd_link_info *info,
4251
         enum notice_asneeded_action act)
4252
0
{
4253
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4254
0
}
4255
4256
/* Call ACTION on each relocation in an ELF object file.  */
4257
4258
bool
4259
_bfd_elf_link_iterate_on_relocs
4260
  (bfd *abfd, struct bfd_link_info *info,
4261
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4262
       const Elf_Internal_Rela *))
4263
0
{
4264
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4265
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4266
4267
  /* If this object is the same format as the output object, and it is
4268
     not a shared library, then let the backend look through the
4269
     relocs.
4270
4271
     This is required to build global offset table entries and to
4272
     arrange for dynamic relocs.  It is not required for the
4273
     particular common case of linking non PIC code, even when linking
4274
     against shared libraries, but unfortunately there is no way of
4275
     knowing whether an object file has been compiled PIC or not.
4276
     Looking through the relocs is not particularly time consuming.
4277
     The problem is that we must either (1) keep the relocs in memory,
4278
     which causes the linker to require additional runtime memory or
4279
     (2) read the relocs twice from the input file, which wastes time.
4280
     This would be a good case for using mmap.
4281
4282
     I have no idea how to handle linking PIC code into a file of a
4283
     different format.  It probably can't be done.  */
4284
0
  if ((abfd->flags & DYNAMIC) == 0
4285
0
      && is_elf_hash_table (&htab->root)
4286
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4287
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4288
0
    {
4289
0
      asection *o;
4290
4291
0
      for (o = abfd->sections; o != NULL; o = o->next)
4292
0
  {
4293
0
    Elf_Internal_Rela *internal_relocs;
4294
0
    bool ok;
4295
4296
    /* Don't check relocations in excluded sections.  Don't do
4297
       anything special with non-loaded, non-alloced sections.
4298
       In particular, any relocs in such sections should not
4299
       affect GOT and PLT reference counting (ie.  we don't
4300
       allow them to create GOT or PLT entries), there's no
4301
       possibility or desire to optimize TLS relocs, and
4302
       there's not much point in propagating relocs to shared
4303
       libs that the dynamic linker won't relocate.  */
4304
0
    if ((o->flags & SEC_ALLOC) == 0
4305
0
        || (o->flags & SEC_RELOC) == 0
4306
0
        || (o->flags & SEC_EXCLUDE) != 0
4307
0
        || o->reloc_count == 0
4308
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4309
0
      && (o->flags & SEC_DEBUGGING) != 0)
4310
0
        || bfd_is_abs_section (o->output_section))
4311
0
      continue;
4312
4313
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4314
0
      (abfd, info, o, NULL, NULL,
4315
0
       _bfd_elf_link_keep_memory (info));
4316
0
    if (internal_relocs == NULL)
4317
0
      return false;
4318
4319
0
    ok = action (abfd, info, o, internal_relocs);
4320
4321
0
    if (elf_section_data (o)->relocs != internal_relocs)
4322
0
      free (internal_relocs);
4323
4324
0
    if (! ok)
4325
0
      return false;
4326
0
  }
4327
0
    }
4328
4329
0
  return true;
4330
0
}
4331
4332
/* Check relocations in an ELF object file.  This is called after
4333
   all input files have been opened.  */
4334
4335
bool
4336
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4337
0
{
4338
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4339
0
  if (bed->check_relocs != NULL)
4340
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4341
0
              bed->check_relocs);
4342
0
  return true;
4343
0
}
4344
4345
/* An entry in the first definition hash table.  */
4346
4347
struct elf_link_first_hash_entry
4348
{
4349
  struct bfd_hash_entry root;
4350
  /* The object of the first definition.  */
4351
  bfd *abfd;
4352
};
4353
4354
/* The function to create a new entry in the first definition hash
4355
   table.  */
4356
4357
static struct bfd_hash_entry *
4358
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4359
           struct bfd_hash_table *table,
4360
           const char *string)
4361
0
{
4362
0
  struct elf_link_first_hash_entry *ret =
4363
0
    (struct elf_link_first_hash_entry *) entry;
4364
4365
  /* Allocate the structure if it has not already been allocated by a
4366
     subclass.  */
4367
0
  if (ret == NULL)
4368
0
    ret = (struct elf_link_first_hash_entry *)
4369
0
  bfd_hash_allocate (table,
4370
0
         sizeof (struct elf_link_first_hash_entry));
4371
0
  if (ret == NULL)
4372
0
    return NULL;
4373
4374
  /* Call the allocation method of the superclass.  */
4375
0
  ret = ((struct elf_link_first_hash_entry *)
4376
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4377
0
         string));
4378
0
  if (ret != NULL)
4379
0
    ret->abfd = NULL;
4380
4381
0
  return (struct bfd_hash_entry *) ret;
4382
0
}
4383
4384
/* Add the symbol NAME from ABFD to first hash.  */
4385
4386
static void
4387
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4388
          const char *name, bool copy)
4389
0
{
4390
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4391
  /* Skip if there is no first hash.  */
4392
0
  if (htab->first_hash == NULL)
4393
0
    return;
4394
4395
0
  struct elf_link_first_hash_entry *e
4396
0
    = ((struct elf_link_first_hash_entry *)
4397
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4398
0
  if (e == NULL)
4399
0
    info->callbacks->fatal
4400
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4401
4402
0
  if (e->abfd == NULL)
4403
    /* Store ABFD in abfd.  */
4404
0
    e->abfd = abfd;
4405
0
}
4406
4407
/* Add symbols from an ELF object file to the linker hash table.  */
4408
4409
static bool
4410
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4411
0
{
4412
0
  Elf_Internal_Ehdr *ehdr;
4413
0
  Elf_Internal_Shdr *hdr;
4414
0
  size_t symcount;
4415
0
  size_t extsymcount;
4416
0
  size_t extsymoff;
4417
0
  struct elf_link_hash_entry **sym_hash;
4418
0
  bool dynamic;
4419
0
  Elf_External_Versym *extversym = NULL;
4420
0
  Elf_External_Versym *extversym_end = NULL;
4421
0
  Elf_External_Versym *ever;
4422
0
  struct elf_link_hash_entry *weaks;
4423
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4424
0
  size_t nondeflt_vers_cnt = 0;
4425
0
  Elf_Internal_Sym *isymbuf = NULL;
4426
0
  Elf_Internal_Sym *isym;
4427
0
  Elf_Internal_Sym *isymend;
4428
0
  elf_backend_data *bed;
4429
0
  bool add_needed;
4430
0
  struct elf_link_hash_table *htab;
4431
0
  void *alloc_mark = NULL;
4432
0
  struct bfd_hash_entry **old_table = NULL;
4433
0
  unsigned int old_size = 0;
4434
0
  unsigned int old_count = 0;
4435
0
  void *old_tab = NULL;
4436
0
  void *old_ent;
4437
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4438
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4439
0
  void *old_strtab = NULL;
4440
0
  size_t tabsize = 0;
4441
0
  asection *s;
4442
0
  bool just_syms;
4443
4444
0
  htab = elf_hash_table (info);
4445
0
  bed = get_elf_backend_data (abfd);
4446
4447
0
  if (elf_use_dt_symtab_p (abfd))
4448
0
    {
4449
0
      bfd_set_error (bfd_error_wrong_format);
4450
0
      return false;
4451
0
    }
4452
4453
0
  if ((abfd->flags & DYNAMIC) == 0)
4454
0
    {
4455
0
      dynamic = false;
4456
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4457
0
    && is_elf_hash_table (&htab->root)
4458
0
    && htab->first_hash == NULL)
4459
0
  {
4460
    /* Initialize first_hash for an IR input.  */
4461
0
    htab->first_hash = (struct bfd_hash_table *)
4462
0
      bfd_malloc (sizeof (struct bfd_hash_table));
4463
0
    if (htab->first_hash == NULL
4464
0
        || !bfd_hash_table_init
4465
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4466
0
        sizeof (struct elf_link_first_hash_entry)))
4467
0
      info->callbacks->fatal
4468
0
        (_("%P: first_hash failed to create: %E\n"));
4469
0
  }
4470
0
    }
4471
0
  else
4472
0
    {
4473
0
      dynamic = true;
4474
4475
      /* You can't use -r against a dynamic object.  Also, there's no
4476
   hope of using a dynamic object which does not exactly match
4477
   the format of the output file.  */
4478
0
      if (bfd_link_relocatable (info)
4479
0
    || !is_elf_hash_table (&htab->root)
4480
0
    || info->output_bfd->xvec != abfd->xvec)
4481
0
  {
4482
0
    if (bfd_link_relocatable (info))
4483
0
      bfd_set_error (bfd_error_invalid_operation);
4484
0
    else
4485
0
      bfd_set_error (bfd_error_wrong_format);
4486
0
    goto error_return;
4487
0
  }
4488
0
    }
4489
4490
0
  ehdr = elf_elfheader (abfd);
4491
0
  if (info->warn_alternate_em
4492
0
      && bed->elf_machine_code != ehdr->e_machine
4493
0
      && ((bed->elf_machine_alt1 != 0
4494
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4495
0
    || (bed->elf_machine_alt2 != 0
4496
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4497
0
    _bfd_error_handler
4498
      /* xgettext:c-format */
4499
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4500
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4501
4502
  /* As a GNU extension, any input sections which are named
4503
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4504
     symbol.  This differs from .gnu.warning sections, which generate
4505
     warnings when they are included in an output file.  */
4506
  /* PR 12761: Also generate this warning when building shared libraries.  */
4507
0
  for (s = abfd->sections; s != NULL; s = s->next)
4508
0
    {
4509
0
      const char *name;
4510
4511
0
      name = bfd_section_name (s);
4512
0
      if (startswith (name, ".gnu.warning."))
4513
0
  {
4514
0
    char *msg;
4515
0
    bfd_size_type sz;
4516
4517
0
    name += sizeof ".gnu.warning." - 1;
4518
4519
    /* If this is a shared object, then look up the symbol
4520
       in the hash table.  If it is there, and it is already
4521
       been defined, then we will not be using the entry
4522
       from this shared object, so we don't need to warn.
4523
       FIXME: If we see the definition in a regular object
4524
       later on, we will warn, but we shouldn't.  The only
4525
       fix is to keep track of what warnings we are supposed
4526
       to emit, and then handle them all at the end of the
4527
       link.  */
4528
0
    if (dynamic)
4529
0
      {
4530
0
        struct elf_link_hash_entry *h;
4531
4532
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4533
4534
        /* FIXME: What about bfd_link_hash_common?  */
4535
0
        if (h != NULL
4536
0
      && (h->root.type == bfd_link_hash_defined
4537
0
          || h->root.type == bfd_link_hash_defweak))
4538
0
    continue;
4539
0
      }
4540
4541
0
    sz = s->size;
4542
0
    msg = (char *) bfd_alloc (abfd, sz + 1);
4543
0
    if (msg == NULL)
4544
0
      goto error_return;
4545
4546
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4547
0
      goto error_return;
4548
4549
0
    msg[sz] = '\0';
4550
4551
0
    if (! (_bfd_generic_link_add_one_symbol
4552
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4553
0
      false, bed->collect, NULL)))
4554
0
      goto error_return;
4555
4556
0
    if (bfd_link_executable (info))
4557
0
      {
4558
        /* Clobber the section size so that the warning does
4559
     not get copied into the output file.  */
4560
0
        s->size = 0;
4561
4562
        /* Also set SEC_EXCLUDE, so that symbols defined in
4563
     the warning section don't get copied to the output.  */
4564
0
        s->flags |= SEC_EXCLUDE;
4565
0
      }
4566
0
  }
4567
0
    }
4568
4569
0
  just_syms = ((s = abfd->sections) != NULL
4570
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4571
4572
0
  add_needed = true;
4573
0
  if (! dynamic)
4574
0
    {
4575
      /* If we are creating a shared library, create all the dynamic
4576
   sections immediately.  We need to attach them to something,
4577
   so we attach them to this BFD, provided it is the right
4578
   format and is not from ld --just-symbols.  Always create the
4579
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4580
   are no input BFD's of the same format as the output, we can't
4581
   make a shared library.  */
4582
0
      if (!just_syms
4583
0
    && (bfd_link_pic (info)
4584
0
        || (!bfd_link_relocatable (info)
4585
0
      && info->nointerp
4586
0
      && (info->export_dynamic || info->dynamic)))
4587
0
    && is_elf_hash_table (&htab->root)
4588
0
    && info->output_bfd->xvec == abfd->xvec
4589
0
    && !htab->dynamic_sections_created)
4590
0
  {
4591
0
    if (!bfd_elf_link_create_dynamic_sections (abfd, info))
4592
0
      goto error_return;
4593
0
  }
4594
0
    }
4595
0
  else if (!is_elf_hash_table (&htab->root))
4596
0
    goto error_return;
4597
0
  else
4598
0
    {
4599
0
      const char *soname = NULL;
4600
0
      char *audit = NULL;
4601
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4602
0
      const Elf_Internal_Phdr *phdr;
4603
0
      struct elf_link_loaded_list *loaded_lib;
4604
4605
      /* ld --just-symbols and dynamic objects don't mix very well.
4606
   ld shouldn't allow it.  */
4607
0
      if (just_syms)
4608
0
  abort ();
4609
4610
      /* If this dynamic lib was specified on the command line with
4611
   --as-needed in effect, then we don't want to add a DT_NEEDED
4612
   tag unless the lib is actually used.  Similary for libs brought
4613
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4614
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4615
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4616
   all.  */
4617
0
      add_needed = (elf_dyn_lib_class (abfd)
4618
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4619
0
           | DYN_NO_NEEDED)) == 0;
4620
4621
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4622
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4623
0
  {
4624
0
    bfd_byte *dynbuf;
4625
0
    bfd_byte *extdyn;
4626
0
    unsigned int elfsec;
4627
0
    unsigned long shlink;
4628
4629
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4630
0
      {
4631
0
      error_free_dyn:
4632
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4633
0
        goto error_return;
4634
0
      }
4635
4636
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4637
0
    if (elfsec == SHN_BAD)
4638
0
      goto error_free_dyn;
4639
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4640
4641
0
    for (extdyn = dynbuf;
4642
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4643
0
         extdyn += bed->s->sizeof_dyn)
4644
0
      {
4645
0
        Elf_Internal_Dyn dyn;
4646
4647
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4648
0
        if (dyn.d_tag == DT_SONAME)
4649
0
    {
4650
0
      unsigned int tagv = dyn.d_un.d_val;
4651
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4652
0
      if (soname == NULL)
4653
0
        goto error_free_dyn;
4654
0
    }
4655
0
        if (dyn.d_tag == DT_NEEDED)
4656
0
    {
4657
0
      struct bfd_link_needed_list *n, **pn;
4658
0
      char *fnm, *anm;
4659
0
      unsigned int tagv = dyn.d_un.d_val;
4660
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4661
4662
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4663
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4664
0
      if (n == NULL || fnm == NULL)
4665
0
        goto error_free_dyn;
4666
0
      amt = strlen (fnm) + 1;
4667
0
      anm = (char *) bfd_alloc (abfd, amt);
4668
0
      if (anm == NULL)
4669
0
        goto error_free_dyn;
4670
0
      memcpy (anm, fnm, amt);
4671
0
      n->name = anm;
4672
0
      n->by = abfd;
4673
0
      n->next = NULL;
4674
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4675
0
        ;
4676
0
      *pn = n;
4677
0
    }
4678
0
        if (dyn.d_tag == DT_RUNPATH)
4679
0
    {
4680
0
      struct bfd_link_needed_list *n, **pn;
4681
0
      char *fnm, *anm;
4682
0
      unsigned int tagv = dyn.d_un.d_val;
4683
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4684
4685
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4686
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4687
0
      if (n == NULL || fnm == NULL)
4688
0
        goto error_free_dyn;
4689
0
      amt = strlen (fnm) + 1;
4690
0
      anm = (char *) bfd_alloc (abfd, amt);
4691
0
      if (anm == NULL)
4692
0
        goto error_free_dyn;
4693
0
      memcpy (anm, fnm, amt);
4694
0
      n->name = anm;
4695
0
      n->by = abfd;
4696
0
      n->next = NULL;
4697
0
      for (pn = & runpath;
4698
0
           *pn != NULL;
4699
0
           pn = &(*pn)->next)
4700
0
        ;
4701
0
      *pn = n;
4702
0
    }
4703
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4704
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4705
0
    {
4706
0
      struct bfd_link_needed_list *n, **pn;
4707
0
      char *fnm, *anm;
4708
0
      unsigned int tagv = dyn.d_un.d_val;
4709
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4710
4711
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4712
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4713
0
      if (n == NULL || fnm == NULL)
4714
0
        goto error_free_dyn;
4715
0
      amt = strlen (fnm) + 1;
4716
0
      anm = (char *) bfd_alloc (abfd, amt);
4717
0
      if (anm == NULL)
4718
0
        goto error_free_dyn;
4719
0
      memcpy (anm, fnm, amt);
4720
0
      n->name = anm;
4721
0
      n->by = abfd;
4722
0
      n->next = NULL;
4723
0
      for (pn = & rpath;
4724
0
           *pn != NULL;
4725
0
           pn = &(*pn)->next)
4726
0
        ;
4727
0
      *pn = n;
4728
0
    }
4729
0
        if (dyn.d_tag == DT_AUDIT)
4730
0
    {
4731
0
      unsigned int tagv = dyn.d_un.d_val;
4732
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4733
0
    }
4734
0
        if (dyn.d_tag == DT_FLAGS_1)
4735
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4736
0
      }
4737
4738
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4739
0
  }
4740
4741
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4742
   frees all more recently bfd_alloc'd blocks as well.  */
4743
0
      if (runpath)
4744
0
  rpath = runpath;
4745
4746
0
      if (rpath)
4747
0
  {
4748
0
    struct bfd_link_needed_list **pn;
4749
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4750
0
      ;
4751
0
    *pn = rpath;
4752
0
  }
4753
4754
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4755
   all sections contained fully therein.  This makes relro
4756
   shared library sections appear as they will at run-time.  */
4757
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4758
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4759
0
  if (phdr->p_type == PT_GNU_RELRO)
4760
0
    {
4761
0
      for (s = abfd->sections; s != NULL; s = s->next)
4762
0
        {
4763
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4764
4765
0
    if ((s->flags & SEC_ALLOC) != 0
4766
0
        && s->vma * opb >= phdr->p_vaddr
4767
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4768
0
      s->flags |= SEC_READONLY;
4769
0
        }
4770
0
      break;
4771
0
    }
4772
4773
      /* We do not want to include any of the sections in a dynamic
4774
   object in the output file.  We hack by simply clobbering the
4775
   list of sections in the BFD.  This could be handled more
4776
   cleanly by, say, a new section flag; the existing
4777
   SEC_NEVER_LOAD flag is not the one we want, because that one
4778
   still implies that the section takes up space in the output
4779
   file.  */
4780
0
      bfd_section_list_clear (abfd);
4781
4782
      /* Find the name to use in a DT_NEEDED entry that refers to this
4783
   object.  If the object has a DT_SONAME entry, we use it.
4784
   Otherwise, if the generic linker stuck something in
4785
   elf_dt_name, we use that.  Otherwise, we just use the file
4786
   name.  */
4787
0
      if (soname == NULL || *soname == '\0')
4788
0
  {
4789
0
    soname = elf_dt_name (abfd);
4790
0
    if (soname == NULL || *soname == '\0')
4791
0
      soname = bfd_get_filename (abfd);
4792
0
  }
4793
4794
      /* Save the SONAME because sometimes the linker emulation code
4795
   will need to know it.  */
4796
0
      elf_dt_name (abfd) = soname;
4797
4798
      /* If we have already included this dynamic object in the
4799
   link, just ignore it.  There is no reason to include a
4800
   particular dynamic object more than once.  */
4801
0
      for (loaded_lib = htab->dyn_loaded;
4802
0
     loaded_lib != NULL;
4803
0
     loaded_lib = loaded_lib->next)
4804
0
  {
4805
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4806
0
      return true;
4807
0
  }
4808
4809
      /* Create dynamic sections for backends that require that be done
4810
   before setup_gnu_properties.  */
4811
0
      if (add_needed
4812
0
    && !bfd_elf_link_create_dynamic_sections (abfd, info))
4813
0
  return false;
4814
4815
      /* Save the DT_AUDIT entry for the linker emulation code. */
4816
0
      elf_dt_audit (abfd) = audit;
4817
0
    }
4818
4819
  /* If this is a dynamic object, we always link against the .dynsym
4820
     symbol table, not the .symtab symbol table.  The dynamic linker
4821
     will only see the .dynsym symbol table, so there is no reason to
4822
     look at .symtab for a dynamic object.  */
4823
4824
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4825
0
    hdr = &elf_tdata (abfd)->symtab_hdr;
4826
0
  else
4827
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4828
4829
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4830
4831
  /* The sh_info field of the symtab header tells us where the
4832
     external symbols start.  We don't care about the local symbols at
4833
     this point.  */
4834
0
  if (elf_bad_symtab (abfd))
4835
0
    {
4836
0
      extsymcount = symcount;
4837
0
      extsymoff = 0;
4838
0
    }
4839
0
  else
4840
0
    {
4841
0
      extsymcount = symcount - hdr->sh_info;
4842
0
      extsymoff = hdr->sh_info;
4843
0
    }
4844
4845
0
  sym_hash = elf_sym_hashes (abfd);
4846
0
  if (extsymcount != 0)
4847
0
    {
4848
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4849
0
              NULL, NULL, NULL);
4850
0
      if (isymbuf == NULL)
4851
0
  goto error_return;
4852
4853
0
      if (sym_hash == NULL)
4854
0
  {
4855
    /* We store a pointer to the hash table entry for each
4856
       external symbol.  */
4857
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4858
0
    if (sym_hash == NULL)
4859
0
      goto error_free_sym;
4860
0
    elf_sym_hashes (abfd) = sym_hash;
4861
0
  }
4862
0
    }
4863
4864
0
  if (dynamic)
4865
0
    {
4866
      /* Read in any version definitions.  */
4867
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4868
0
            info->default_imported_symver))
4869
0
  goto error_free_sym;
4870
4871
      /* Read in the symbol versions, but don't bother to convert them
4872
   to internal format.  */
4873
0
      if (elf_dynversym (abfd) != 0)
4874
0
  {
4875
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4876
0
    bfd_size_type amt = versymhdr->sh_size;
4877
4878
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4879
0
      goto error_free_sym;
4880
0
    extversym = (Elf_External_Versym *)
4881
0
      _bfd_malloc_and_read (abfd, amt, amt);
4882
0
    if (extversym == NULL)
4883
0
      goto error_free_sym;
4884
0
    extversym_end = extversym + amt / sizeof (*extversym);
4885
0
  }
4886
0
    }
4887
4888
  /* If we are loading an as-needed shared lib, save the symbol table
4889
     state before we start adding symbols.  If the lib turns out
4890
     to be unneeded, restore the state.  */
4891
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4892
0
    {
4893
0
      unsigned int i;
4894
0
      size_t entsize;
4895
4896
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4897
0
  {
4898
0
    struct bfd_hash_entry *p;
4899
0
    struct elf_link_hash_entry *h;
4900
4901
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4902
0
      {
4903
0
        h = (struct elf_link_hash_entry *) p;
4904
0
        entsize += htab->root.table.entsize;
4905
0
        if (h->root.type == bfd_link_hash_warning)
4906
0
    {
4907
0
      entsize += htab->root.table.entsize;
4908
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4909
0
    }
4910
0
        if (h->root.type == bfd_link_hash_common)
4911
0
    entsize += sizeof (*h->root.u.c.p);
4912
0
      }
4913
0
  }
4914
4915
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4916
0
      old_tab = bfd_malloc (tabsize + entsize);
4917
0
      if (old_tab == NULL)
4918
0
  goto error_free_vers;
4919
4920
      /* Remember the current objalloc pointer, so that all mem for
4921
   symbols added can later be reclaimed.  */
4922
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4923
0
      if (alloc_mark == NULL)
4924
0
  goto error_free_vers;
4925
4926
      /* Make a special call to the linker "notice" function to
4927
   tell it that we are about to handle an as-needed lib.  */
4928
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4929
0
  goto error_free_vers;
4930
4931
      /* Clone the symbol table.  Remember some pointers into the
4932
   symbol table, and dynamic symbol count.  */
4933
0
      old_ent = (char *) old_tab + tabsize;
4934
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4935
0
      old_undefs = htab->root.undefs;
4936
0
      old_undefs_tail = htab->root.undefs_tail;
4937
0
      old_table = htab->root.table.table;
4938
0
      old_size = htab->root.table.size;
4939
0
      old_count = htab->root.table.count;
4940
0
      old_strtab = NULL;
4941
0
      if (htab->dynstr != NULL)
4942
0
  {
4943
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4944
0
    if (old_strtab == NULL)
4945
0
      goto error_free_vers;
4946
0
  }
4947
4948
0
      for (i = 0; i < htab->root.table.size; i++)
4949
0
  {
4950
0
    struct bfd_hash_entry *p;
4951
0
    struct elf_link_hash_entry *h;
4952
4953
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4954
0
      {
4955
0
        h = (struct elf_link_hash_entry *) p;
4956
0
        memcpy (old_ent, h, htab->root.table.entsize);
4957
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4958
0
        if (h->root.type == bfd_link_hash_warning)
4959
0
    {
4960
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4961
0
      memcpy (old_ent, h, htab->root.table.entsize);
4962
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4963
0
    }
4964
0
        if (h->root.type == bfd_link_hash_common)
4965
0
    {
4966
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4967
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4968
0
    }
4969
0
      }
4970
0
  }
4971
0
    }
4972
4973
0
  weaks = NULL;
4974
0
  if (extversym == NULL)
4975
0
    ever = NULL;
4976
0
  else if (extversym + extsymoff < extversym_end)
4977
0
    ever = extversym + extsymoff;
4978
0
  else
4979
0
    {
4980
      /* xgettext:c-format */
4981
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4982
0
        abfd, (long) extsymoff,
4983
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4984
0
      bfd_set_error (bfd_error_bad_value);
4985
0
      goto error_free_vers;
4986
0
    }
4987
4988
0
  if (!bfd_link_relocatable (info)
4989
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4990
0
    {
4991
0
      _bfd_error_handler
4992
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4993
0
    }
4994
4995
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4996
0
       isym < isymend;
4997
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4998
0
    {
4999
0
      int bind;
5000
0
      bfd_vma value;
5001
0
      asection *sec, *new_sec;
5002
0
      flagword flags;
5003
0
      const char *name;
5004
0
      const char *defvername;
5005
0
      bool must_copy_name = false;
5006
0
      struct elf_link_hash_entry *h;
5007
0
      struct elf_link_hash_entry *hi;
5008
0
      bool definition;
5009
0
      bool size_change_ok;
5010
0
      bool type_change_ok;
5011
0
      bool new_weak;
5012
0
      bool old_weak;
5013
0
      bfd *override;
5014
0
      bool common;
5015
0
      bool discarded;
5016
0
      unsigned int old_alignment;
5017
0
      unsigned int shindex;
5018
0
      bfd *old_bfd;
5019
0
      bool matched;
5020
5021
0
      override = NULL;
5022
5023
0
      flags = BSF_NO_FLAGS;
5024
0
      sec = NULL;
5025
0
      value = isym->st_value;
5026
0
      common = bed->common_definition (isym);
5027
0
      if (common && info->inhibit_common_definition)
5028
0
  {
5029
    /* Treat common symbol as undefined for --no-define-common.  */
5030
0
    isym->st_shndx = SHN_UNDEF;
5031
0
    common = false;
5032
0
  }
5033
0
      discarded = false;
5034
5035
0
      bind = ELF_ST_BIND (isym->st_info);
5036
0
      switch (bind)
5037
0
  {
5038
0
  case STB_LOCAL:
5039
    /* This should be impossible, since ELF requires that all
5040
       global symbols follow all local symbols, and that sh_info
5041
       point to the first global symbol.  Unfortunately, Irix 5
5042
       screws this up.  */
5043
0
    if (elf_bad_symtab (abfd))
5044
0
      continue;
5045
5046
    /* If we aren't prepared to handle locals within the globals
5047
       then we'll likely segfault on a NULL symbol hash if the
5048
       symbol is ever referenced in relocations.  */
5049
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5050
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5051
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5052
0
        " (>= sh_info of %lu)"),
5053
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5054
0
            (long) extsymoff);
5055
5056
    /* Dynamic object relocations are not processed by ld, so
5057
       ld won't run into the problem mentioned above.  */
5058
0
    if (dynamic)
5059
0
      continue;
5060
0
    bfd_set_error (bfd_error_bad_value);
5061
0
    goto error_free_vers;
5062
5063
0
  case STB_GLOBAL:
5064
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5065
0
      flags = BSF_GLOBAL;
5066
0
    break;
5067
5068
0
  case STB_WEAK:
5069
0
    flags = BSF_WEAK;
5070
0
    break;
5071
5072
0
  case STB_GNU_UNIQUE:
5073
0
    flags = BSF_GNU_UNIQUE;
5074
0
    break;
5075
5076
0
  default:
5077
    /* Leave it up to the processor backend.  */
5078
0
    break;
5079
0
  }
5080
5081
0
      if (isym->st_shndx == SHN_UNDEF)
5082
0
  sec = bfd_und_section_ptr;
5083
0
      else if (isym->st_shndx == SHN_ABS)
5084
0
  sec = bfd_abs_section_ptr;
5085
0
      else if (isym->st_shndx == SHN_COMMON)
5086
0
  {
5087
0
    sec = bfd_com_section_ptr;
5088
    /* What ELF calls the size we call the value.  What ELF
5089
       calls the value we call the alignment.  */
5090
0
    value = isym->st_size;
5091
0
  }
5092
0
      else
5093
0
  {
5094
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5095
0
    if (sec == NULL)
5096
0
      sec = bfd_abs_section_ptr;
5097
0
    else if (discarded_section (sec))
5098
0
      {
5099
        /* Symbols from discarded section are undefined.  We keep
5100
     its visibility.  */
5101
0
        sec = bfd_und_section_ptr;
5102
0
        discarded = true;
5103
0
        isym->st_shndx = SHN_UNDEF;
5104
0
      }
5105
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5106
0
      value -= sec->vma;
5107
0
  }
5108
5109
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5110
0
                isym->st_name);
5111
0
      if (name == NULL)
5112
0
  goto error_free_vers;
5113
5114
0
      if (isym->st_shndx == SHN_COMMON
5115
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5116
0
  {
5117
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5118
5119
0
    if (xc == NULL)
5120
0
      {
5121
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5122
0
         | SEC_EXCLUDE);
5123
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5124
0
        if (xc == NULL)
5125
0
    goto error_free_vers;
5126
0
      }
5127
0
    sec = xc;
5128
0
  }
5129
0
      else if (isym->st_shndx == SHN_COMMON
5130
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5131
0
         && !bfd_link_relocatable (info))
5132
0
  {
5133
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5134
5135
0
    if (tcomm == NULL)
5136
0
      {
5137
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5138
0
         | SEC_LINKER_CREATED);
5139
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5140
0
        if (tcomm == NULL)
5141
0
    goto error_free_vers;
5142
0
      }
5143
0
    sec = tcomm;
5144
0
  }
5145
0
      else if (bed->elf_add_symbol_hook)
5146
0
  {
5147
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5148
0
               &sec, &value))
5149
0
      goto error_free_vers;
5150
5151
    /* The hook function sets the name to NULL if this symbol
5152
       should be skipped for some reason.  */
5153
0
    if (name == NULL)
5154
0
      continue;
5155
0
  }
5156
5157
0
      if (name[0] == '\0')
5158
0
  {
5159
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5160
0
    bfd_set_error (bfd_error_bad_value);
5161
0
    goto error_free_vers;
5162
0
  }
5163
5164
      /* Sanity check that all possibilities were handled.  */
5165
0
      if (sec == NULL)
5166
0
  abort ();
5167
5168
      /* Silently discard TLS symbols from --just-syms.  There's
5169
   no way to combine a static TLS block with a new TLS block
5170
   for this executable.  */
5171
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5172
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5173
0
  continue;
5174
5175
0
      if (bfd_is_und_section (sec)
5176
0
    || bfd_is_com_section (sec))
5177
0
  definition = false;
5178
0
      else
5179
0
  definition = true;
5180
5181
0
      size_change_ok = false;
5182
0
      type_change_ok = bed->type_change_ok;
5183
0
      old_weak = false;
5184
0
      matched = false;
5185
0
      old_alignment = 0;
5186
0
      old_bfd = NULL;
5187
0
      new_sec = sec;
5188
0
      defvername = NULL;
5189
5190
0
      if (is_elf_hash_table (&htab->root))
5191
0
  {
5192
0
    Elf_Internal_Versym iver;
5193
0
    unsigned int vernum = 0;
5194
0
    bool skip;
5195
5196
0
    if (ever == NULL)
5197
0
      {
5198
0
        if (info->default_imported_symver)
5199
    /* Use the default symbol version created earlier.  */
5200
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5201
0
        else
5202
0
    iver.vs_vers = 0;
5203
0
      }
5204
0
    else if (ever >= extversym_end)
5205
0
      {
5206
        /* xgettext:c-format */
5207
0
        _bfd_error_handler (_("%pB: not enough version information"),
5208
0
          abfd);
5209
0
        bfd_set_error (bfd_error_bad_value);
5210
0
        goto error_free_vers;
5211
0
      }
5212
0
    else
5213
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5214
5215
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5216
5217
    /* If this is a hidden symbol, or if it is not version
5218
       1, we append the version name to the symbol name.
5219
       However, we do not modify a non-hidden absolute symbol
5220
       if it is not a function, because it might be the version
5221
       symbol itself.  FIXME: What if it isn't?  */
5222
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5223
0
        || (vernum > 1
5224
0
      && (!bfd_is_abs_section (sec)
5225
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5226
0
      {
5227
0
        const char *verstr;
5228
0
        size_t namelen, verlen, newlen;
5229
0
        char *newname, *p;
5230
5231
0
        if (isym->st_shndx != SHN_UNDEF)
5232
0
    {
5233
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5234
0
        verstr = NULL;
5235
0
      else if (vernum > 1)
5236
0
        verstr =
5237
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5238
0
      else
5239
0
        verstr = "";
5240
5241
0
      if (verstr == NULL)
5242
0
        {
5243
0
          _bfd_error_handler
5244
      /* xgettext:c-format */
5245
0
      (_("%pB: %s: invalid version %u (max %d)"),
5246
0
       abfd, name, vernum,
5247
0
       elf_tdata (abfd)->cverdefs);
5248
0
          bfd_set_error (bfd_error_bad_value);
5249
0
          goto error_free_vers;
5250
0
        }
5251
0
    }
5252
0
        else
5253
0
    {
5254
      /* We cannot simply test for the number of
5255
         entries in the VERNEED section since the
5256
         numbers for the needed versions do not start
5257
         at 0.  */
5258
0
      Elf_Internal_Verneed *t;
5259
5260
0
      verstr = NULL;
5261
0
      for (t = elf_tdata (abfd)->verref;
5262
0
           t != NULL;
5263
0
           t = t->vn_nextref)
5264
0
        {
5265
0
          Elf_Internal_Vernaux *a;
5266
5267
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5268
0
      {
5269
0
        if (a->vna_other == vernum)
5270
0
          {
5271
0
            verstr = a->vna_nodename;
5272
0
            break;
5273
0
          }
5274
0
      }
5275
0
          if (a != NULL)
5276
0
      break;
5277
0
        }
5278
0
      if (verstr == NULL)
5279
0
        {
5280
0
          _bfd_error_handler
5281
      /* xgettext:c-format */
5282
0
      (_("%pB: %s: invalid needed version %d"),
5283
0
       abfd, name, vernum);
5284
0
          bfd_set_error (bfd_error_bad_value);
5285
0
          goto error_free_vers;
5286
0
        }
5287
0
    }
5288
5289
0
        namelen = strlen (name);
5290
0
        verlen = strlen (verstr);
5291
0
        newlen = namelen + verlen + 2;
5292
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5293
0
      && isym->st_shndx != SHN_UNDEF)
5294
0
    ++newlen;
5295
5296
0
        newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5297
0
        if (newname == NULL)
5298
0
    goto error_free_vers;
5299
0
        memcpy (newname, name, namelen);
5300
0
        p = newname + namelen;
5301
0
        *p++ = ELF_VER_CHR;
5302
        /* If this is a defined non-hidden version symbol,
5303
     we add another @ to the name.  This indicates the
5304
     default version of the symbol.  */
5305
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5306
0
      && isym->st_shndx != SHN_UNDEF)
5307
0
    *p++ = ELF_VER_CHR, defvername = name;
5308
0
        memcpy (p, verstr, verlen + 1);
5309
5310
0
        name = newname;
5311
        /* Since bfd_hash_alloc is used for "name", the string
5312
     must be copied if added to first_hash.  The string
5313
     memory can be freed when an --as-needed library is
5314
     not needed.  */
5315
0
        must_copy_name = true;
5316
0
      }
5317
5318
    /* If this symbol has default visibility and the user has
5319
       requested we not re-export it, then mark it as hidden.  */
5320
0
    if (!bfd_is_und_section (sec)
5321
0
        && !dynamic
5322
0
        && abfd->no_export
5323
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5324
0
      isym->st_other = (STV_HIDDEN
5325
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5326
5327
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5328
0
              sym_hash, &old_bfd, &old_weak,
5329
0
              &old_alignment, &skip, &override,
5330
0
              &type_change_ok, &size_change_ok,
5331
0
              &matched))
5332
0
      goto error_free_vers;
5333
5334
0
    if (skip)
5335
0
      continue;
5336
5337
0
    h = *sym_hash;
5338
0
    while (h->root.type == bfd_link_hash_indirect
5339
0
     || h->root.type == bfd_link_hash_warning)
5340
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5341
5342
    /* Override a definition only if the new symbol matches the
5343
       existing one.  */
5344
0
    if (override && matched)
5345
0
      {
5346
0
        definition = false;
5347
0
        if (htab->first_hash != NULL
5348
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5349
0
      && h->root.non_ir_ref_regular)
5350
0
    {
5351
      /* When reloading --as-needed shared objects for new
5352
         symbols added from IR inputs, if this shared object
5353
         has the first definition, use it.  */
5354
0
      struct elf_link_first_hash_entry *e
5355
0
        = ((struct elf_link_first_hash_entry *)
5356
0
           bfd_hash_lookup (htab->first_hash, name, false,
5357
0
          false));
5358
0
      if (e != NULL && e->abfd == abfd)
5359
0
        definition = true;
5360
0
    }
5361
0
      }
5362
5363
0
    if (h->versioned != unversioned
5364
0
        && elf_tdata (abfd)->verdef != NULL
5365
0
        && vernum > 1
5366
0
        && definition)
5367
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5368
0
  }
5369
5370
0
      if (! (_bfd_generic_link_add_one_symbol
5371
0
       (info, override ? override : abfd, name, flags, sec, value,
5372
0
        NULL, false, bed->collect,
5373
0
        (struct bfd_link_hash_entry **) sym_hash)))
5374
0
  goto error_free_vers;
5375
5376
0
      h = *sym_hash;
5377
      /* We need to make sure that indirect symbol dynamic flags are
5378
   updated.  */
5379
0
      hi = h;
5380
0
      while (h->root.type == bfd_link_hash_indirect
5381
0
       || h->root.type == bfd_link_hash_warning)
5382
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5383
5384
0
      *sym_hash = h;
5385
5386
      /* Setting the index to -3 tells elf_link_output_extsym that
5387
   this symbol is defined in a discarded section.  */
5388
0
      if (discarded && is_elf_hash_table (&htab->root))
5389
0
  h->indx = -3;
5390
5391
0
      new_weak = (flags & BSF_WEAK) != 0;
5392
0
      if (dynamic
5393
0
    && definition
5394
0
    && new_weak
5395
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5396
0
    && is_elf_hash_table (&htab->root)
5397
0
    && h->u.alias == NULL)
5398
0
  {
5399
    /* Keep a list of all weak defined non function symbols from
5400
       a dynamic object, using the alias field.  Later in this
5401
       function we will set the alias field to the correct
5402
       value.  We only put non-function symbols from dynamic
5403
       objects on this list, because that happens to be the only
5404
       time we need to know the normal symbol corresponding to a
5405
       weak symbol, and the information is time consuming to
5406
       figure out.  If the alias field is not already NULL,
5407
       then this symbol was already defined by some previous
5408
       dynamic object, and we will be using that previous
5409
       definition anyhow.  */
5410
5411
0
    h->u.alias = weaks;
5412
0
    weaks = h;
5413
0
  }
5414
5415
      /* Set the alignment of a common symbol.  */
5416
0
      if ((common || bfd_is_com_section (sec))
5417
0
    && h->root.type == bfd_link_hash_common)
5418
0
  {
5419
0
    unsigned int align;
5420
5421
0
    if (common)
5422
0
      align = bfd_log2 (isym->st_value);
5423
0
    else
5424
0
      {
5425
        /* The new symbol is a common symbol in a shared object.
5426
     We need to get the alignment from the section.  */
5427
0
        align = new_sec->alignment_power;
5428
0
      }
5429
0
    if (align > old_alignment)
5430
0
      h->root.u.c.p->alignment_power = align;
5431
0
    else
5432
0
      h->root.u.c.p->alignment_power = old_alignment;
5433
0
  }
5434
5435
0
      if (is_elf_hash_table (&htab->root))
5436
0
  {
5437
    /* Set a flag in the hash table entry indicating the type of
5438
       reference or definition we just found.  A dynamic symbol
5439
       is one which is referenced or defined by both a regular
5440
       object and a shared object.  */
5441
0
    bool dynsym = false;
5442
5443
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5444
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5445
0
      {
5446
        /* Except for this flag to track nonweak references.  */
5447
0
        if (!definition
5448
0
      && bind != STB_WEAK)
5449
0
    h->ref_ir_nonweak = 1;
5450
0
      }
5451
0
    else if (!dynamic)
5452
0
      {
5453
0
        if (! definition)
5454
0
    {
5455
0
      h->ref_regular = 1;
5456
0
      if (bind != STB_WEAK)
5457
0
        h->ref_regular_nonweak = 1;
5458
0
    }
5459
0
        else
5460
0
    {
5461
0
      h->def_regular = 1;
5462
0
      if (h->def_dynamic)
5463
0
        {
5464
0
          h->def_dynamic = 0;
5465
0
          h->ref_dynamic = 1;
5466
0
        }
5467
0
    }
5468
0
      }
5469
0
    else
5470
0
      {
5471
0
        if (! definition)
5472
0
    {
5473
0
      h->ref_dynamic = 1;
5474
0
      hi->ref_dynamic = 1;
5475
0
    }
5476
0
        else
5477
0
    {
5478
0
      h->def_dynamic = 1;
5479
0
      hi->def_dynamic = 1;
5480
0
    }
5481
0
      }
5482
5483
    /* If an indirect symbol has been forced local, don't
5484
       make the real symbol dynamic.  */
5485
0
    if (h != hi && hi->forced_local)
5486
0
      ;
5487
0
    else if (!dynamic)
5488
0
      {
5489
0
        if (bfd_link_dll (info)
5490
0
      || h->def_dynamic
5491
0
      || h->ref_dynamic)
5492
0
    dynsym = true;
5493
0
      }
5494
0
    else
5495
0
      {
5496
0
        if (h->def_regular
5497
0
      || h->ref_regular
5498
0
      || (h->is_weakalias
5499
0
          && weakdef (h)->dynindx != -1))
5500
0
    dynsym = true;
5501
0
      }
5502
5503
    /* Check to see if we need to add an indirect symbol for
5504
       the default name.  */
5505
0
    if ((definition
5506
0
         || (!override && h->root.type == bfd_link_hash_common))
5507
0
        && !(hi != h
5508
0
       && hi->versioned == versioned_hidden))
5509
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5510
0
                sec, value, &old_bfd, &dynsym))
5511
0
        goto error_free_vers;
5512
5513
    /* Check the alignment when a common symbol is involved. This
5514
       can change when a common symbol is overridden by a normal
5515
       definition or a common symbol is ignored due to the old
5516
       normal definition. We need to make sure the maximum
5517
       alignment is maintained.  */
5518
0
    if ((old_alignment || common)
5519
0
        && h->root.type != bfd_link_hash_common)
5520
0
      {
5521
0
        unsigned int common_align;
5522
0
        unsigned int normal_align;
5523
0
        unsigned int symbol_align;
5524
0
        bfd *normal_bfd;
5525
0
        bfd *common_bfd;
5526
5527
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5528
0
        || h->root.type == bfd_link_hash_defweak);
5529
5530
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5531
0
        if (h->root.u.def.section->owner != NULL
5532
0
      && (h->root.u.def.section->owner->flags
5533
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5534
0
    {
5535
0
      normal_align = h->root.u.def.section->alignment_power;
5536
0
      if (normal_align > symbol_align)
5537
0
        normal_align = symbol_align;
5538
0
    }
5539
0
        else
5540
0
    normal_align = symbol_align;
5541
5542
0
        if (old_alignment)
5543
0
    {
5544
0
      common_align = old_alignment;
5545
0
      common_bfd = old_bfd;
5546
0
      normal_bfd = abfd;
5547
0
    }
5548
0
        else
5549
0
    {
5550
0
      common_align = bfd_log2 (isym->st_value);
5551
0
      common_bfd = abfd;
5552
0
      normal_bfd = old_bfd;
5553
0
    }
5554
5555
0
        if (normal_align < common_align)
5556
0
    {
5557
      /* PR binutils/2735 */
5558
0
      uint64_t c_align = UINT64_C (1) << common_align;
5559
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5560
0
      if (normal_bfd == NULL)
5561
0
        _bfd_error_handler
5562
          /* xgettext:c-format */
5563
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5564
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5565
0
           c_align, name, common_bfd,
5566
0
           n_align, h->root.u.def.section);
5567
0
      else
5568
0
        _bfd_error_handler
5569
          /* xgettext:c-format */
5570
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5571
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5572
0
           n_align, name, normal_bfd,
5573
0
           c_align, common_bfd);
5574
5575
      /* PR 30499: make sure that users understand that this warning is serious.  */
5576
0
      _bfd_error_handler
5577
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5578
0
    }
5579
0
      }
5580
5581
    /* Remember the symbol size if it isn't undefined.  */
5582
0
    if (isym->st_size != 0
5583
0
        && isym->st_shndx != SHN_UNDEF
5584
0
        && (definition || h->size == 0))
5585
0
      {
5586
0
        if (h->size != 0
5587
0
      && h->size != isym->st_size
5588
0
      && ! size_change_ok)
5589
0
    {
5590
0
      _bfd_error_handler
5591
        /* xgettext:c-format */
5592
0
        (_("warning: size of symbol `%s' changed"
5593
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5594
0
         name, (uint64_t) h->size, old_bfd,
5595
0
         (uint64_t) isym->st_size, abfd);
5596
5597
      /* PR 30499: make sure that users understand that this warning is serious.  */
5598
0
      _bfd_error_handler
5599
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5600
0
    }
5601
5602
0
        h->size = isym->st_size;
5603
0
      }
5604
5605
    /* If this is a common symbol, then we always want H->SIZE
5606
       to be the size of the common symbol.  The code just above
5607
       won't fix the size if a common symbol becomes larger.  We
5608
       don't warn about a size change here, because that is
5609
       covered by --warn-common.  Allow changes between different
5610
       function types.  */
5611
0
    if (h->root.type == bfd_link_hash_common)
5612
0
      h->size = h->root.u.c.size;
5613
5614
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5615
0
        && ((definition && !new_weak)
5616
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5617
0
      || h->type == STT_NOTYPE))
5618
0
      {
5619
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5620
5621
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5622
     symbol.  */
5623
0
        if (type == STT_GNU_IFUNC
5624
0
      && (abfd->flags & DYNAMIC) != 0)
5625
0
    type = STT_FUNC;
5626
5627
0
        if (h->type != type)
5628
0
    {
5629
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5630
        /* xgettext:c-format */
5631
0
        _bfd_error_handler
5632
0
          (_("warning: type of symbol `%s' changed"
5633
0
       " from %d to %d in %pB"),
5634
0
           name, h->type, type, abfd);
5635
5636
0
      h->type = type;
5637
0
    }
5638
0
      }
5639
5640
    /* Merge st_other field.  */
5641
0
    elf_merge_st_other (abfd, h, isym->st_other, sec,
5642
0
            definition, dynamic);
5643
5644
    /* We don't want to make debug symbol dynamic.  */
5645
0
    if (definition
5646
0
        && (sec->flags & SEC_DEBUGGING)
5647
0
        && !bfd_link_relocatable (info))
5648
0
      dynsym = false;
5649
5650
    /* Nor should we make plugin symbols dynamic.  */
5651
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5652
0
      dynsym = false;
5653
5654
0
    if (definition)
5655
0
      {
5656
0
        h->target_internal = isym->st_target_internal;
5657
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5658
0
      }
5659
5660
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5661
       in IR.  Since all data or text symbols in IR have the
5662
       same type, value and section, we can't tell if a symbol
5663
       is an alias of another symbol by their types, values and
5664
       sections.  */
5665
0
    if (definition
5666
0
        && !dynamic
5667
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5668
0
      {
5669
0
        const char *p = strchr (name, ELF_VER_CHR);
5670
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5671
0
    {
5672
      /* Queue non-default versions so that .symver x, x@FOO
5673
         aliases can be checked.  */
5674
0
      if (!nondeflt_vers)
5675
0
        {
5676
0
          size_t amt = ((isymend - isym + 1)
5677
0
            * sizeof (struct elf_link_hash_entry *));
5678
0
          nondeflt_vers
5679
0
      = (struct elf_link_hash_entry **) bfd_malloc (amt);
5680
0
          if (!nondeflt_vers)
5681
0
      goto error_free_vers;
5682
0
        }
5683
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5684
0
    }
5685
0
      }
5686
5687
0
    if (dynsym && h->dynindx == -1)
5688
0
      {
5689
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5690
0
    goto error_free_vers;
5691
0
        if (h->is_weakalias
5692
0
      && weakdef (h)->dynindx == -1)
5693
0
    {
5694
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5695
0
        goto error_free_vers;
5696
0
    }
5697
0
      }
5698
0
    else if (h->dynindx != -1)
5699
      /* If the symbol already has a dynamic index, but
5700
         visibility says it should not be visible, turn it into
5701
         a local symbol.  */
5702
0
      switch (ELF_ST_VISIBILITY (h->other))
5703
0
        {
5704
0
        case STV_INTERNAL:
5705
0
        case STV_HIDDEN:
5706
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
5707
0
    dynsym = false;
5708
0
    break;
5709
0
        }
5710
5711
0
    if (!add_needed
5712
0
        && matched
5713
0
        && definition
5714
0
        && h->root.type != bfd_link_hash_indirect)
5715
0
      {
5716
0
        if ((dynsym
5717
0
       && h->ref_regular_nonweak)
5718
0
      || (old_bfd != NULL
5719
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5720
0
          && h->ref_ir_nonweak
5721
0
          && !info->lto_all_symbols_read)
5722
0
      || (h->ref_dynamic_nonweak
5723
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5724
0
          && !on_needed_list (elf_dt_name (abfd),
5725
0
            htab->needed, NULL)))
5726
0
    {
5727
0
      const char *soname = elf_dt_name (abfd);
5728
5729
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5730
0
            h->root.root.string);
5731
5732
      /* A symbol from a library loaded via DT_NEEDED of some
5733
         other library is referenced by a regular object.
5734
         Add a DT_NEEDED entry for it.  Issue an error if
5735
         --no-add-needed is used and the reference was not
5736
         a weak one.  */
5737
0
      if (old_bfd != NULL
5738
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5739
0
        {
5740
0
          _bfd_error_handler
5741
      /* xgettext:c-format */
5742
0
      (_("%pB: undefined reference to symbol '%s'"),
5743
0
       old_bfd, name);
5744
0
          bfd_set_error (bfd_error_missing_dso);
5745
0
          goto error_free_vers;
5746
0
        }
5747
5748
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5749
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5750
5751
      /* Create dynamic sections for backends that require
5752
         that be done before setup_gnu_properties.  */
5753
0
      if (!bfd_elf_link_create_dynamic_sections (abfd, info))
5754
0
        goto error_free_vers;
5755
0
      add_needed = true;
5756
0
    }
5757
0
        else if (dynamic
5758
0
           && h->root.u.def.section->owner == abfd)
5759
0
    {
5760
      /* Add this symbol to first hash if this shared
5761
         object has the first definition.  */
5762
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5763
      /* And if it was the default symbol version definition,
5764
         also add the short name.  */
5765
0
      if (defvername)
5766
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5767
0
    }
5768
0
      }
5769
0
  }
5770
0
    }
5771
5772
0
  if (info->lto_plugin_active
5773
0
      && !bfd_link_relocatable (info)
5774
0
      && (abfd->flags & BFD_PLUGIN) == 0
5775
0
      && !just_syms
5776
0
      && extsymcount != 0
5777
0
      && is_elf_hash_table (&htab->root))
5778
0
    {
5779
0
      int r_sym_shift;
5780
5781
0
      if (bed->s->arch_size == 32)
5782
0
  r_sym_shift = 8;
5783
0
      else
5784
0
  r_sym_shift = 32;
5785
5786
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5787
   referenced in regular objects so that linker plugin will get
5788
   the correct symbol resolution.  */
5789
5790
0
      sym_hash = elf_sym_hashes (abfd);
5791
0
      for (s = abfd->sections; s != NULL; s = s->next)
5792
0
  {
5793
0
    Elf_Internal_Rela *internal_relocs;
5794
0
    Elf_Internal_Rela *rel, *relend;
5795
5796
    /* Don't check relocations in excluded sections.  */
5797
0
    if ((s->flags & SEC_RELOC) == 0
5798
0
        || s->reloc_count == 0
5799
0
        || (s->flags & SEC_EXCLUDE) != 0
5800
0
        || (s->flags & SEC_DEBUGGING) != 0)
5801
0
      continue;
5802
5803
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5804
0
      (abfd, info, s, NULL, NULL,
5805
0
       _bfd_elf_link_keep_memory (info));
5806
0
    if (internal_relocs == NULL)
5807
0
      goto error_free_vers;
5808
5809
0
    rel = internal_relocs;
5810
0
    relend = rel + s->reloc_count;
5811
0
    for ( ; rel < relend; rel++)
5812
0
      {
5813
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5814
0
        struct elf_link_hash_entry *h;
5815
5816
        /* Skip local symbols.  */
5817
0
        if (r_symndx < extsymoff)
5818
0
    continue;
5819
5820
0
        h = sym_hash[r_symndx - extsymoff];
5821
0
        if (h != NULL)
5822
0
    h->root.non_ir_ref_regular = 1;
5823
0
      }
5824
5825
0
    if (elf_section_data (s)->relocs != internal_relocs)
5826
0
      free (internal_relocs);
5827
0
  }
5828
0
    }
5829
5830
0
  free (extversym);
5831
0
  extversym = NULL;
5832
0
  free (isymbuf);
5833
0
  isymbuf = NULL;
5834
5835
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5836
0
    {
5837
0
      unsigned int i;
5838
5839
      /* Restore the symbol table.  */
5840
0
      old_ent = (char *) old_tab + tabsize;
5841
0
      memset (elf_sym_hashes (abfd), 0,
5842
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5843
0
      htab->root.table.table = old_table;
5844
0
      htab->root.table.size = old_size;
5845
0
      htab->root.table.count = old_count;
5846
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5847
0
      htab->root.undefs = old_undefs;
5848
0
      htab->root.undefs_tail = old_undefs_tail;
5849
0
      if (htab->dynstr != NULL)
5850
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5851
0
      free (old_strtab);
5852
0
      old_strtab = NULL;
5853
0
      for (i = 0; i < htab->root.table.size; i++)
5854
0
  {
5855
0
    struct bfd_hash_entry *p;
5856
0
    struct elf_link_hash_entry *h;
5857
0
    unsigned int non_ir_ref_dynamic;
5858
5859
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5860
0
      {
5861
        /* Preserve non_ir_ref_dynamic so that this symbol
5862
     will be exported when the dynamic lib becomes needed
5863
     in the second pass.  */
5864
0
        h = (struct elf_link_hash_entry *) p;
5865
0
        if (h->root.type == bfd_link_hash_warning)
5866
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5867
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5868
5869
0
        h = (struct elf_link_hash_entry *) p;
5870
0
        memcpy (h, old_ent, htab->root.table.entsize);
5871
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5872
0
        if (h->root.type == bfd_link_hash_warning)
5873
0
    {
5874
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5875
0
      memcpy (h, old_ent, htab->root.table.entsize);
5876
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5877
0
    }
5878
0
        if (h->root.type == bfd_link_hash_common)
5879
0
    {
5880
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5881
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5882
0
    }
5883
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5884
0
      }
5885
0
  }
5886
5887
      /* Make a special call to the linker "notice" function to
5888
   tell it that symbols added for crefs may need to be removed.  */
5889
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5890
0
  goto error_free_vers;
5891
5892
0
      free (old_tab);
5893
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5894
0
         alloc_mark);
5895
0
      free (nondeflt_vers);
5896
0
      return true;
5897
0
    }
5898
5899
0
  free (old_strtab);
5900
0
  old_strtab = NULL;
5901
0
  if (old_tab != NULL)
5902
0
    {
5903
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5904
0
  goto error_free_vers;
5905
0
      free (old_tab);
5906
0
      old_tab = NULL;
5907
0
    }
5908
5909
  /* Now that all the symbols from this input file are created, if
5910
     not performing a relocatable link, handle .symver foo, foo@BAR
5911
     such that any relocs against foo become foo@BAR.  */
5912
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5913
0
    {
5914
0
      size_t cnt, symidx;
5915
5916
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5917
0
  {
5918
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5919
0
    char *shortname;
5920
0
    const char *p;
5921
0
    size_t amt;
5922
5923
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5924
0
    if (p == NULL
5925
0
        || (h->root.type != bfd_link_hash_defined
5926
0
      && h->root.type != bfd_link_hash_defweak))
5927
0
      continue;
5928
5929
0
    amt = p - h->root.root.string;
5930
0
    shortname = (char *) bfd_malloc (amt + 1);
5931
0
    if (!shortname)
5932
0
      goto error_free_vers;
5933
0
    memcpy (shortname, h->root.root.string, amt);
5934
0
    shortname[amt] = '\0';
5935
5936
0
    hi = (struct elf_link_hash_entry *)
5937
0
         bfd_link_hash_lookup (&htab->root, shortname,
5938
0
             false, false, false);
5939
0
    if (hi != NULL
5940
0
        && hi->root.type == h->root.type
5941
0
        && hi->root.u.def.value == h->root.u.def.value
5942
0
        && hi->root.u.def.section == h->root.u.def.section)
5943
0
      {
5944
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
5945
0
        hi->root.type = bfd_link_hash_indirect;
5946
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5947
0
        (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5948
0
        sym_hash = elf_sym_hashes (abfd);
5949
0
        if (sym_hash)
5950
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5951
0
      if (sym_hash[symidx] == hi)
5952
0
        {
5953
0
          sym_hash[symidx] = h;
5954
0
          break;
5955
0
        }
5956
0
      }
5957
0
    free (shortname);
5958
0
  }
5959
0
    }
5960
0
  free (nondeflt_vers);
5961
0
  nondeflt_vers = NULL;
5962
5963
  /* Now set the alias field correctly for all the weak defined
5964
     symbols we found.  The only way to do this is to search all the
5965
     symbols.  Since we only need the information for non functions in
5966
     dynamic objects, that's the only time we actually put anything on
5967
     the list WEAKS.  We need this information so that if a regular
5968
     object refers to a symbol defined weakly in a dynamic object, the
5969
     real symbol in the dynamic object is also put in the dynamic
5970
     symbols; we also must arrange for both symbols to point to the
5971
     same memory location.  We could handle the general case of symbol
5972
     aliasing, but a general symbol alias can only be generated in
5973
     assembler code, handling it correctly would be very time
5974
     consuming, and other ELF linkers don't handle general aliasing
5975
     either.  */
5976
0
  if (weaks != NULL)
5977
0
    {
5978
0
      struct elf_link_hash_entry **hpp;
5979
0
      struct elf_link_hash_entry **hppend;
5980
0
      struct elf_link_hash_entry **sorted_sym_hash;
5981
0
      struct elf_link_hash_entry *h;
5982
0
      size_t sym_count, amt;
5983
5984
      /* Since we have to search the whole symbol list for each weak
5985
   defined symbol, search time for N weak defined symbols will be
5986
   O(N^2). Binary search will cut it down to O(NlogN).  */
5987
0
      amt = extsymcount * sizeof (*sorted_sym_hash);
5988
0
      sorted_sym_hash = bfd_malloc (amt);
5989
0
      if (sorted_sym_hash == NULL)
5990
0
  goto error_return;
5991
0
      sym_hash = sorted_sym_hash;
5992
0
      hpp = elf_sym_hashes (abfd);
5993
0
      hppend = hpp + extsymcount;
5994
0
      sym_count = 0;
5995
0
      for (; hpp < hppend; hpp++)
5996
0
  {
5997
0
    h = *hpp;
5998
0
    if (h != NULL
5999
0
        && h->root.type == bfd_link_hash_defined
6000
0
        && !bed->is_function_type (h->type))
6001
0
      {
6002
0
        *sym_hash = h;
6003
0
        sym_hash++;
6004
0
        sym_count++;
6005
0
      }
6006
0
  }
6007
6008
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6009
0
       elf_sort_symbol);
6010
6011
0
      while (weaks != NULL)
6012
0
  {
6013
0
    struct elf_link_hash_entry *hlook;
6014
0
    asection *slook;
6015
0
    bfd_vma vlook;
6016
0
    size_t i, j, idx = 0;
6017
6018
0
    hlook = weaks;
6019
0
    weaks = hlook->u.alias;
6020
0
    hlook->u.alias = NULL;
6021
6022
0
    if (hlook->root.type != bfd_link_hash_defined
6023
0
        && hlook->root.type != bfd_link_hash_defweak)
6024
0
      continue;
6025
6026
0
    slook = hlook->root.u.def.section;
6027
0
    vlook = hlook->root.u.def.value;
6028
6029
0
    i = 0;
6030
0
    j = sym_count;
6031
0
    while (i != j)
6032
0
      {
6033
0
        bfd_signed_vma vdiff;
6034
0
        idx = (i + j) / 2;
6035
0
        h = sorted_sym_hash[idx];
6036
0
        vdiff = vlook - h->root.u.def.value;
6037
0
        if (vdiff < 0)
6038
0
    j = idx;
6039
0
        else if (vdiff > 0)
6040
0
    i = idx + 1;
6041
0
        else
6042
0
    {
6043
0
      int sdiff = slook->id - h->root.u.def.section->id;
6044
0
      if (sdiff < 0)
6045
0
        j = idx;
6046
0
      else if (sdiff > 0)
6047
0
        i = idx + 1;
6048
0
      else
6049
0
        break;
6050
0
    }
6051
0
      }
6052
6053
    /* We didn't find a value/section match.  */
6054
0
    if (i == j)
6055
0
      continue;
6056
6057
    /* With multiple aliases, or when the weak symbol is already
6058
       strongly defined, we have multiple matching symbols and
6059
       the binary search above may land on any of them.  Step
6060
       one past the matching symbol(s).  */
6061
0
    while (++idx != j)
6062
0
      {
6063
0
        h = sorted_sym_hash[idx];
6064
0
        if (h->root.u.def.section != slook
6065
0
      || h->root.u.def.value != vlook)
6066
0
    break;
6067
0
      }
6068
6069
    /* Now look back over the aliases.  Since we sorted by size
6070
       as well as value and section, we'll choose the one with
6071
       the largest size.  */
6072
0
    while (idx-- != i)
6073
0
      {
6074
0
        h = sorted_sym_hash[idx];
6075
6076
        /* Stop if value or section doesn't match.  */
6077
0
        if (h->root.u.def.section != slook
6078
0
      || h->root.u.def.value != vlook)
6079
0
    break;
6080
0
        else if (h != hlook)
6081
0
    {
6082
0
      struct elf_link_hash_entry *t;
6083
6084
0
      hlook->u.alias = h;
6085
0
      hlook->is_weakalias = 1;
6086
0
      t = h;
6087
0
      if (t->u.alias != NULL)
6088
0
        while (t->u.alias != h)
6089
0
          t = t->u.alias;
6090
0
      t->u.alias = hlook;
6091
6092
      /* If the weak definition is in the list of dynamic
6093
         symbols, make sure the real definition is put
6094
         there as well.  */
6095
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6096
0
        {
6097
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6098
0
      {
6099
0
      err_free_sym_hash:
6100
0
        free (sorted_sym_hash);
6101
0
        goto error_return;
6102
0
      }
6103
0
        }
6104
6105
      /* If the real definition is in the list of dynamic
6106
         symbols, make sure the weak definition is put
6107
         there as well.  If we don't do this, then the
6108
         dynamic loader might not merge the entries for the
6109
         real definition and the weak definition.  */
6110
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6111
0
        {
6112
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6113
0
      goto err_free_sym_hash;
6114
0
        }
6115
0
      break;
6116
0
    }
6117
0
      }
6118
0
  }
6119
6120
0
      free (sorted_sym_hash);
6121
0
    }
6122
6123
0
  if (bed->check_directives
6124
0
      && !(*bed->check_directives) (abfd, info))
6125
0
    goto error_return;
6126
6127
  /* If this is a non-traditional link, try to optimize the handling
6128
     of the .stab/.stabstr sections.  */
6129
0
  if (! dynamic
6130
0
      && ! info->traditional_format
6131
0
      && is_elf_hash_table (&htab->root)
6132
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6133
0
    {
6134
0
      asection *stabstr;
6135
6136
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6137
0
      if (stabstr != NULL)
6138
0
  {
6139
0
    bfd_size_type string_offset = 0;
6140
0
    asection *stab;
6141
6142
0
    for (stab = abfd->sections; stab; stab = stab->next)
6143
0
      if (startswith (stab->name, ".stab")
6144
0
    && (!stab->name[5] ||
6145
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6146
0
    && (stab->flags & SEC_MERGE) == 0
6147
0
    && !bfd_is_abs_section (stab->output_section)
6148
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6149
0
               stabstr, &string_offset))
6150
0
        goto error_return;
6151
0
  }
6152
0
    }
6153
6154
0
  if (dynamic && add_needed)
6155
0
    {
6156
      /* Add this bfd to the loaded list.  */
6157
0
      struct elf_link_loaded_list *n;
6158
6159
0
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6160
0
      if (n == NULL)
6161
0
  goto error_return;
6162
0
      n->abfd = abfd;
6163
0
      n->next = htab->dyn_loaded;
6164
0
      htab->dyn_loaded = n;
6165
0
    }
6166
0
  if (dynamic && !add_needed
6167
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6168
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6169
6170
0
  return true;
6171
6172
0
 error_free_vers:
6173
0
  free (old_tab);
6174
0
  free (old_strtab);
6175
0
  free (nondeflt_vers);
6176
0
  free (extversym);
6177
0
 error_free_sym:
6178
0
  free (isymbuf);
6179
0
 error_return:
6180
0
  return false;
6181
0
}
6182
6183
/* Return the linker hash table entry of a symbol that might be
6184
   satisfied by an archive symbol.  Return -1 on error.  */
6185
6186
struct bfd_link_hash_entry *
6187
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6188
        struct bfd_link_info *info,
6189
        const char *name)
6190
0
{
6191
0
  struct bfd_link_hash_entry *h;
6192
0
  const char *p;
6193
0
  char *copy;
6194
0
  size_t len, first;
6195
6196
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6197
0
  if (h != NULL)
6198
0
    return h;
6199
6200
  /* If this is a default version (the name contains @@), look up the
6201
     symbol again with only one `@' as well as without the version.
6202
     The effect is that references to the symbol with and without the
6203
     version will be matched by the default symbol in the archive.  */
6204
6205
0
  p = strchr (name, ELF_VER_CHR);
6206
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6207
0
    {
6208
      /* Add this symbol to first hash if this archive has the first
6209
   definition.  */
6210
0
      if (is_elf_hash_table (info->hash))
6211
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6212
0
      return h;
6213
0
    }
6214
6215
  /* First check with only one `@'.  */
6216
0
  len = strlen (name);
6217
0
  copy = (char *) bfd_alloc (abfd, len);
6218
0
  if (copy == NULL)
6219
0
    return (struct bfd_link_hash_entry *) -1;
6220
6221
0
  first = p - name + 1;
6222
0
  memcpy (copy, name, first);
6223
0
  memcpy (copy + first, name + first + 1, len - first);
6224
6225
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6226
0
  if (h == NULL)
6227
0
    {
6228
      /* We also need to check references to the symbol without the
6229
   version.  */
6230
0
      copy[first - 1] = '\0';
6231
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6232
0
    }
6233
6234
0
  bfd_release (abfd, copy);
6235
0
  return h;
6236
0
}
6237
6238
/* Add symbols from an ELF archive file to the linker hash table.  We
6239
   don't use _bfd_generic_link_add_archive_symbols because we need to
6240
   handle versioned symbols.
6241
6242
   Fortunately, ELF archive handling is simpler than that done by
6243
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6244
   oddities.  In ELF, if we find a symbol in the archive map, and the
6245
   symbol is currently undefined, we know that we must pull in that
6246
   object file.
6247
6248
   Unfortunately, we do have to make multiple passes over the symbol
6249
   table until nothing further is resolved.  */
6250
6251
static bool
6252
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6253
0
{
6254
0
  symindex c;
6255
0
  unsigned char *included = NULL;
6256
0
  carsym *symdefs;
6257
0
  bool loop;
6258
0
  size_t amt;
6259
0
  elf_backend_data *bed;
6260
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6261
0
    (bfd *, struct bfd_link_info *, const char *);
6262
6263
0
  if (! bfd_has_map (abfd))
6264
0
    {
6265
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6266
6267
      /* An empty archive is a special case.  */
6268
0
      if (first_one == NULL)
6269
0
  return true;
6270
6271
0
      if (!_bfd_make_armap (abfd, first_one))
6272
0
  return false;
6273
0
    }
6274
6275
  /* Keep track of all symbols we know to be already defined, and all
6276
     files we know to be already included.  This is to speed up the
6277
     second and subsequent passes.  */
6278
0
  c = bfd_ardata (abfd)->symdef_count;
6279
0
  if (c == 0)
6280
0
    return true;
6281
0
  amt = c * sizeof (*included);
6282
0
  included = (unsigned char *) bfd_zmalloc (amt);
6283
0
  if (included == NULL)
6284
0
    return false;
6285
6286
0
  symdefs = bfd_ardata (abfd)->symdefs;
6287
0
  bed = get_elf_backend_data (abfd);
6288
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6289
6290
0
  do
6291
0
    {
6292
0
      ufile_ptr_or_bfd last;
6293
0
      symindex i;
6294
0
      carsym *symdef;
6295
0
      carsym *symdefend;
6296
6297
0
      loop = false;
6298
0
      last = _bfd_elt_nil (abfd);
6299
6300
0
      symdef = symdefs;
6301
0
      symdefend = symdef + c;
6302
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6303
0
  {
6304
0
    struct bfd_link_hash_entry *h;
6305
0
    bfd *element;
6306
0
    struct bfd_link_hash_entry *undefs_tail;
6307
0
    symindex mark;
6308
6309
0
    if (included[i])
6310
0
      continue;
6311
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6312
0
      {
6313
0
        included[i] = true;
6314
0
        continue;
6315
0
      }
6316
6317
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6318
0
    if (h == (struct bfd_link_hash_entry *) -1)
6319
0
      goto error_return;
6320
6321
0
    if (h == NULL)
6322
0
      continue;
6323
6324
0
    if (h->type == bfd_link_hash_undefined)
6325
0
      {
6326
0
        if (is_elf_hash_table (info->hash))
6327
0
    {
6328
      /* If the archive element has already been loaded then one
6329
         of the symbols defined by that element might have been
6330
         made undefined due to being in a discarded section.  */
6331
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6332
0
        continue;
6333
6334
      /* In the pre-LTO-plugin pass we must not mistakenly
6335
         include this archive member if an earlier shared
6336
         library defined this symbol.  */
6337
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6338
0
      if (htab->first_hash)
6339
0
        {
6340
0
          struct elf_link_first_hash_entry *e
6341
0
        = ((struct elf_link_first_hash_entry *)
6342
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6343
0
                false, false));
6344
0
          if (e
6345
0
        && (e->abfd->flags & DYNAMIC) != 0
6346
0
        && e->abfd != abfd)
6347
0
      continue;
6348
0
        }
6349
0
    }
6350
0
      }
6351
0
    else if (h->type == bfd_link_hash_common)
6352
0
      {
6353
        /* We currently have a common symbol.  The archive map contains
6354
     a reference to this symbol, so we may want to include it.  We
6355
     only want to include it however, if this archive element
6356
     contains a definition of the symbol, not just another common
6357
     declaration of it.
6358
6359
     Unfortunately some archivers (including GNU ar) will put
6360
     declarations of common symbols into their archive maps, as
6361
     well as real definitions, so we cannot just go by the archive
6362
     map alone.  Instead we must read in the element's symbol
6363
     table and check that to see what kind of symbol definition
6364
     this is.  */
6365
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6366
0
    continue;
6367
0
      }
6368
0
    else
6369
0
      {
6370
0
        if (h->type != bfd_link_hash_undefweak)
6371
    /* Symbol must be defined.  Don't check it again.  */
6372
0
    included[i] = true;
6373
6374
0
        if (!is_elf_hash_table (info->hash))
6375
0
    continue;
6376
0
        struct elf_link_hash_entry *eh
6377
0
    = (struct elf_link_hash_entry *) h;
6378
        /* Ignore the archive if the symbol isn't referenced by a
6379
     regular object or isn't defined in a shared object.  */
6380
0
        if (!eh->ref_regular || !eh->def_dynamic)
6381
0
    continue;
6382
        /* Ignore the dynamic definition if symbol is first
6383
     defined in this archive.  */
6384
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6385
0
        if (htab->first_hash == NULL)
6386
0
    continue;
6387
0
        struct elf_link_first_hash_entry *e
6388
0
    = ((struct elf_link_first_hash_entry *)
6389
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6390
0
            false, false));
6391
0
        if (e == NULL || e->abfd != abfd)
6392
0
    continue;
6393
0
      }
6394
6395
    /* We need to include this archive member.  */
6396
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6397
0
    if (element == NULL)
6398
0
      goto error_return;
6399
6400
0
    if (! bfd_check_format (element, bfd_object))
6401
0
      goto error_return;
6402
6403
0
    undefs_tail = info->hash->undefs_tail;
6404
6405
0
    if (!(*info->callbacks
6406
0
    ->add_archive_element) (info, element, symdef->name, &element))
6407
0
      continue;
6408
0
    if (!bfd_link_add_symbols (element, info))
6409
0
      goto error_return;
6410
6411
    /* If there are any new undefined symbols, we need to make
6412
       another pass through the archive in order to see whether
6413
       they can be defined.  FIXME: This isn't perfect, because
6414
       common symbols wind up on undefs_tail and because an
6415
       undefined symbol which is defined later on in this pass
6416
       does not require another pass.  This isn't a bug, but it
6417
       does make the code less efficient than it could be.  */
6418
0
    if (undefs_tail != info->hash->undefs_tail)
6419
0
      loop = true;
6420
6421
    /* Look backward to mark all symbols from this object file
6422
       which we have already seen in this pass.  */
6423
0
    mark = i;
6424
0
    do
6425
0
      {
6426
0
        included[mark] = true;
6427
0
        if (mark == 0)
6428
0
    break;
6429
0
        --mark;
6430
0
      }
6431
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6432
6433
    /* We mark subsequent symbols from this object file as we go
6434
       on through the loop.  */
6435
0
    last = symdef->u;
6436
0
  }
6437
0
    }
6438
0
  while (loop);
6439
6440
0
  free (included);
6441
0
  return true;
6442
6443
0
 error_return:
6444
0
  free (included);
6445
0
  return false;
6446
0
}
6447
6448
/* Given an ELF BFD, add symbols to the global hash table as
6449
   appropriate.  */
6450
6451
bool
6452
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6453
0
{
6454
0
  switch (bfd_get_format (abfd))
6455
0
    {
6456
0
    case bfd_object:
6457
0
      return elf_link_add_object_symbols (abfd, info);
6458
0
    case bfd_archive:
6459
0
      return elf_link_add_archive_symbols (abfd, info);
6460
0
    default:
6461
0
      bfd_set_error (bfd_error_wrong_format);
6462
0
      return false;
6463
0
    }
6464
0
}
6465

6466
struct hash_codes_info
6467
{
6468
  unsigned long *hashcodes;
6469
  bool error;
6470
};
6471
6472
/* This function will be called though elf_link_hash_traverse to store
6473
   all hash value of the exported symbols in an array.  */
6474
6475
static bool
6476
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6477
0
{
6478
0
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
6479
0
  const char *name;
6480
0
  unsigned long ha;
6481
0
  char *alc = NULL;
6482
6483
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6484
0
  if (h->dynindx == -1)
6485
0
    return true;
6486
6487
0
  name = h->root.root.string;
6488
0
  if (h->versioned >= versioned)
6489
0
    {
6490
0
      const char *p = strchr (name, ELF_VER_CHR);
6491
0
      if (p != NULL)
6492
0
  {
6493
0
    alc = (char *) bfd_malloc (p - name + 1);
6494
0
    if (alc == NULL)
6495
0
      {
6496
0
        inf->error = true;
6497
0
        return false;
6498
0
      }
6499
0
    memcpy (alc, name, p - name);
6500
0
    alc[p - name] = '\0';
6501
0
    name = alc;
6502
0
  }
6503
0
    }
6504
6505
  /* Compute the hash value.  */
6506
0
  ha = bfd_elf_hash (name);
6507
6508
  /* Store the found hash value in the array given as the argument.  */
6509
0
  *(inf->hashcodes)++ = ha;
6510
6511
  /* And store it in the struct so that we can put it in the hash table
6512
     later.  */
6513
0
  h->u.elf_hash_value = ha;
6514
6515
0
  free (alc);
6516
0
  return true;
6517
0
}
6518
6519
struct collect_gnu_hash_codes
6520
{
6521
  bfd *output_bfd;
6522
  elf_backend_data *bed;
6523
  unsigned long int nsyms;
6524
  unsigned long int maskbits;
6525
  unsigned long int *hashcodes;
6526
  unsigned long int *hashval;
6527
  unsigned long int *indx;
6528
  unsigned long int *counts;
6529
  bfd_vma *bitmask;
6530
  bfd_byte *contents;
6531
  bfd_size_type xlat;
6532
  long int min_dynindx;
6533
  unsigned long int bucketcount;
6534
  unsigned long int symindx;
6535
  long int local_indx;
6536
  long int shift1, shift2;
6537
  unsigned long int mask;
6538
  bool error;
6539
  bool base_symbol;
6540
};
6541
6542
/* This function will be called though elf_link_hash_traverse to store
6543
   all hash value of the exported symbols in an array.  */
6544
6545
static bool
6546
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6547
0
{
6548
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6549
0
  const char *name;
6550
0
  unsigned long ha;
6551
0
  char *alc = NULL;
6552
6553
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6554
0
  if (h->dynindx == -1)
6555
0
    return true;
6556
6557
  /* Ignore also local symbols and undefined symbols.  */
6558
0
  if (! (*s->bed->elf_hash_symbol) (h))
6559
0
    return true;
6560
6561
0
  name = h->root.root.string;
6562
0
  if (h->versioned >= versioned)
6563
0
    {
6564
0
      const char *p = strchr (name, ELF_VER_CHR);
6565
0
      if (p != NULL)
6566
0
  {
6567
0
    alc = (char *) bfd_malloc (p - name + 1);
6568
0
    if (alc == NULL)
6569
0
      {
6570
0
        s->error = true;
6571
0
        return false;
6572
0
      }
6573
0
    memcpy (alc, name, p - name);
6574
0
    alc[p - name] = '\0';
6575
0
    name = alc;
6576
0
  }
6577
0
    }
6578
6579
  /* Compute the hash value.  */
6580
0
  ha = bfd_elf_gnu_hash (name);
6581
6582
  /* Store the found hash value in the array for compute_bucket_count,
6583
     and also for .dynsym reordering purposes.  */
6584
0
  s->hashcodes[s->nsyms] = ha;
6585
0
  s->hashval[h->dynindx] = ha;
6586
0
  ++s->nsyms;
6587
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6588
0
    s->min_dynindx = h->dynindx;
6589
6590
0
  free (alc);
6591
0
  return true;
6592
0
}
6593
6594
/* This function will be called though elf_link_hash_traverse to do
6595
   final dynamic symbol renumbering in case of .gnu.hash.
6596
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6597
   to the translation table.  */
6598
6599
static bool
6600
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6601
0
{
6602
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6603
0
  unsigned long int bucket;
6604
0
  unsigned long int val;
6605
6606
  /* Ignore indirect symbols.  */
6607
0
  if (h->dynindx == -1)
6608
0
    return true;
6609
6610
  /* Skip if base symbol doesn't match.  */
6611
0
  if (s->base_symbol != !!h->base_symbol)
6612
0
    return true;
6613
6614
  /* Ignore also local symbols and undefined symbols.  */
6615
0
  if (! (*s->bed->elf_hash_symbol) (h))
6616
0
    {
6617
0
      if (h->dynindx >= s->min_dynindx)
6618
0
  {
6619
0
    if (s->bed->record_xhash_symbol != NULL)
6620
0
      {
6621
0
        (*s->bed->record_xhash_symbol) (h, 0);
6622
0
        s->local_indx++;
6623
0
      }
6624
0
    else
6625
0
      h->dynindx = s->local_indx++;
6626
0
  }
6627
0
      return true;
6628
0
    }
6629
6630
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6631
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6632
0
  & ((s->maskbits >> s->shift1) - 1);
6633
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6634
0
  s->bitmask[val]
6635
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6636
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6637
0
  if (s->counts[bucket] == 1)
6638
    /* Last element terminates the chain.  */
6639
0
    val |= 1;
6640
0
  bfd_put_32 (s->output_bfd, val,
6641
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6642
0
  --s->counts[bucket];
6643
0
  if (s->bed->record_xhash_symbol != NULL)
6644
0
    {
6645
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6646
6647
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6648
0
    }
6649
0
  else
6650
0
    h->dynindx = s->indx[bucket]++;
6651
0
  return true;
6652
0
}
6653
6654
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6655
6656
bool
6657
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6658
0
{
6659
0
  return !(h->forced_local
6660
0
     || h->root.type == bfd_link_hash_undefined
6661
0
     || h->root.type == bfd_link_hash_undefweak
6662
0
     || ((h->root.type == bfd_link_hash_defined
6663
0
    || h->root.type == bfd_link_hash_defweak)
6664
0
         && h->root.u.def.section->output_section == NULL));
6665
0
}
6666
6667
/* Array used to determine the number of hash table buckets to use
6668
   based on the number of symbols there are.  If there are fewer than
6669
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6670
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6671
   than 32771 buckets.  */
6672
6673
static const size_t elf_buckets[] =
6674
{
6675
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6676
  16411, 32771, 0
6677
};
6678
6679
/* Compute bucket count for hashing table.  We do not use a static set
6680
   of possible tables sizes anymore.  Instead we determine for all
6681
   possible reasonable sizes of the table the outcome (i.e., the
6682
   number of collisions etc) and choose the best solution.  The
6683
   weighting functions are not too simple to allow the table to grow
6684
   without bounds.  Instead one of the weighting factors is the size.
6685
   Therefore the result is always a good payoff between few collisions
6686
   (= short chain lengths) and table size.  */
6687
static size_t
6688
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6689
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6690
          unsigned long int nsyms,
6691
          int gnu_hash)
6692
0
{
6693
0
  size_t best_size = 0;
6694
0
  unsigned long int i;
6695
6696
0
  if (info->optimize)
6697
0
    {
6698
0
      size_t minsize;
6699
0
      size_t maxsize;
6700
0
      uint64_t best_chlen = ~((uint64_t) 0);
6701
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6702
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6703
0
      elf_backend_data *bed = get_elf_backend_data (dynobj);
6704
0
      unsigned long int *counts;
6705
0
      bfd_size_type amt;
6706
0
      unsigned int no_improvement_count = 0;
6707
6708
      /* Possible optimization parameters: if we have NSYMS symbols we say
6709
   that the hashing table must at least have NSYMS/4 and at most
6710
   2*NSYMS buckets.  */
6711
0
      minsize = nsyms / 4;
6712
0
      if (minsize == 0)
6713
0
  minsize = 1;
6714
0
      best_size = maxsize = nsyms * 2;
6715
0
      if (gnu_hash)
6716
0
  {
6717
0
    if (minsize < 2)
6718
0
      minsize = 2;
6719
0
    if ((best_size & 31) == 0)
6720
0
      ++best_size;
6721
0
  }
6722
6723
      /* Create array where we count the collisions in.  We must use bfd_malloc
6724
   since the size could be large.  */
6725
0
      amt = maxsize;
6726
0
      amt *= sizeof (unsigned long int);
6727
0
      counts = (unsigned long int *) bfd_malloc (amt);
6728
0
      if (counts == NULL)
6729
0
  return 0;
6730
6731
      /* Compute the "optimal" size for the hash table.  The criteria is a
6732
   minimal chain length.  The minor criteria is (of course) the size
6733
   of the table.  */
6734
0
      for (i = minsize; i < maxsize; ++i)
6735
0
  {
6736
    /* Walk through the array of hashcodes and count the collisions.  */
6737
0
    uint64_t max;
6738
0
    unsigned long int j;
6739
0
    unsigned long int fact;
6740
6741
0
    if (gnu_hash && (i & 31) == 0)
6742
0
      continue;
6743
6744
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6745
6746
    /* Determine how often each hash bucket is used.  */
6747
0
    for (j = 0; j < nsyms; ++j)
6748
0
      ++counts[hashcodes[j] % i];
6749
6750
    /* For the weight function we need some information about the
6751
       pagesize on the target.  This is information need not be 100%
6752
       accurate.  Since this information is not available (so far) we
6753
       define it here to a reasonable default value.  If it is crucial
6754
       to have a better value some day simply define this value.  */
6755
0
# ifndef BFD_TARGET_PAGESIZE
6756
0
#  define BFD_TARGET_PAGESIZE (4096)
6757
0
# endif
6758
6759
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6760
       and the chains.  */
6761
0
    max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6762
6763
0
# if 1
6764
    /* Variant 1: optimize for short chains.  We add the squares
6765
       of all the chain lengths (which favors many small chain
6766
       over a few long chains).  */
6767
0
    for (j = 0; j < i; ++j)
6768
0
      max += counts[j] * counts[j];
6769
6770
    /* This adds penalties for the overall size of the table.  */
6771
0
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6772
0
    max *= fact * fact;
6773
# else
6774
    /* Variant 2: Optimize a lot more for small table.  Here we
6775
       also add squares of the size but we also add penalties for
6776
       empty slots (the +1 term).  */
6777
    for (j = 0; j < i; ++j)
6778
      max += (1 + counts[j]) * (1 + counts[j]);
6779
6780
    /* The overall size of the table is considered, but not as
6781
       strong as in variant 1, where it is squared.  */
6782
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6783
    max *= fact;
6784
# endif
6785
6786
    /* Compare with current best results.  */
6787
0
    if (max < best_chlen)
6788
0
      {
6789
0
        best_chlen = max;
6790
0
        best_size = i;
6791
0
        no_improvement_count = 0;
6792
0
      }
6793
    /* PR 11843: Avoid futile long searches for the best bucket size
6794
       when there are a large number of symbols.  */
6795
0
    else if (++no_improvement_count == 100)
6796
0
      break;
6797
0
  }
6798
6799
0
      free (counts);
6800
0
    }
6801
0
  else
6802
0
    {
6803
0
      for (i = 0; elf_buckets[i] != 0; i++)
6804
0
  {
6805
0
    best_size = elf_buckets[i];
6806
0
    if (nsyms < elf_buckets[i + 1])
6807
0
      break;
6808
0
  }
6809
0
      if (gnu_hash && best_size < 2)
6810
0
  best_size = 2;
6811
0
    }
6812
6813
0
  return best_size;
6814
0
}
6815
6816
/* Size any SHT_GROUP section for ld -r.  */
6817
6818
bool
6819
bfd_elf_size_group_sections (struct bfd_link_info *info)
6820
0
{
6821
0
  bfd *ibfd;
6822
0
  asection *s;
6823
6824
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6825
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6826
0
  && (s = ibfd->sections) != NULL
6827
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6828
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6829
0
      return false;
6830
0
  return true;
6831
0
}
6832
6833
/* Set a default stack segment size.  The value in INFO wins.  If it
6834
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6835
   undefined it is initialized.  */
6836
6837
bool
6838
bfd_elf_stack_segment_size (bfd *output_bfd,
6839
          struct bfd_link_info *info,
6840
          const char *legacy_symbol,
6841
          bfd_vma default_size)
6842
0
{
6843
0
  struct elf_link_hash_entry *h = NULL;
6844
6845
  /* Look for legacy symbol.  */
6846
0
  if (legacy_symbol)
6847
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6848
0
            false, false, false);
6849
0
  if (h && (h->root.type == bfd_link_hash_defined
6850
0
      || h->root.type == bfd_link_hash_defweak)
6851
0
      && h->def_regular
6852
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6853
0
    {
6854
      /* The symbol has no type if specified on the command line.  */
6855
0
      h->type = STT_OBJECT;
6856
0
      if (info->stacksize)
6857
  /* xgettext:c-format */
6858
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6859
0
          output_bfd, legacy_symbol);
6860
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6861
  /* xgettext:c-format */
6862
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6863
0
          output_bfd, legacy_symbol);
6864
0
      else
6865
0
  info->stacksize = h->root.u.def.value;
6866
0
    }
6867
6868
0
  if (!info->stacksize)
6869
    /* If the user didn't set a size, or explicitly inhibit the
6870
       size, set it now.  */
6871
0
    info->stacksize = default_size;
6872
6873
  /* Provide the legacy symbol, if it is referenced.  */
6874
0
  if (h && (h->root.type == bfd_link_hash_undefined
6875
0
      || h->root.type == bfd_link_hash_undefweak))
6876
0
    {
6877
0
      struct bfd_link_hash_entry *bh = NULL;
6878
6879
0
      if (!(_bfd_generic_link_add_one_symbol
6880
0
      (info, output_bfd, legacy_symbol,
6881
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6882
0
       info->stacksize >= 0 ? info->stacksize : 0,
6883
0
       NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6884
0
  return false;
6885
6886
0
      h = (struct elf_link_hash_entry *) bh;
6887
0
      h->def_regular = 1;
6888
0
      h->type = STT_OBJECT;
6889
0
    }
6890
6891
0
  return true;
6892
0
}
6893
6894
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6895
6896
struct elf_gc_sweep_symbol_info
6897
{
6898
  struct bfd_link_info *info;
6899
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6900
           bool);
6901
};
6902
6903
static bool
6904
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6905
0
{
6906
0
  if (!h->mark
6907
0
      && (((h->root.type == bfd_link_hash_defined
6908
0
      || h->root.type == bfd_link_hash_defweak)
6909
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6910
0
    && h->root.u.def.section->gc_mark))
6911
0
    || h->root.type == bfd_link_hash_undefined
6912
0
    || h->root.type == bfd_link_hash_undefweak))
6913
0
    {
6914
0
      struct elf_gc_sweep_symbol_info *inf;
6915
6916
0
      inf = (struct elf_gc_sweep_symbol_info *) data;
6917
0
      (*inf->hide_symbol) (inf->info, h, true);
6918
0
      h->def_regular = 0;
6919
0
      h->ref_regular = 0;
6920
0
      h->ref_regular_nonweak = 0;
6921
0
    }
6922
6923
0
  return true;
6924
0
}
6925
6926
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6927
   called by the ELF linker emulation before_allocation routine.  We
6928
   must set the sizes of the sections before the linker sets the
6929
   addresses of the various sections.  */
6930
6931
bool
6932
bfd_elf_size_dynamic_sections (bfd *output_bfd,
6933
             const char *soname,
6934
             const char *rpath,
6935
             const char *filter_shlib,
6936
             const char *audit,
6937
             const char *depaudit,
6938
             const char * const *auxiliary_filters,
6939
             struct bfd_link_info *info,
6940
             asection **sinterpptr)
6941
0
{
6942
0
  bfd *dynobj;
6943
0
  elf_backend_data *bed;
6944
6945
0
  *sinterpptr = NULL;
6946
6947
0
  if (!is_elf_hash_table (info->hash))
6948
0
    return true;
6949
6950
  /* Any syms created from now on start with -1 in
6951
     got.refcount/offset and plt.refcount/offset.  */
6952
0
  elf_hash_table (info)->init_got_refcount
6953
0
    = elf_hash_table (info)->init_got_offset;
6954
0
  elf_hash_table (info)->init_plt_refcount
6955
0
    = elf_hash_table (info)->init_plt_offset;
6956
6957
0
  bed = get_elf_backend_data (output_bfd);
6958
6959
  /* The backend may have to create some sections regardless of whether
6960
     we're dynamic or not.  */
6961
0
  if (bed->elf_backend_early_size_sections
6962
0
      && !bed->elf_backend_early_size_sections (output_bfd, info))
6963
0
    return false;
6964
6965
0
  dynobj = elf_hash_table (info)->dynobj;
6966
6967
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6968
0
    {
6969
0
      struct bfd_elf_version_tree *verdefs;
6970
0
      struct elf_info_failed asvinfo;
6971
0
      struct bfd_elf_version_tree *t;
6972
0
      struct bfd_elf_version_expr *d;
6973
0
      asection *s;
6974
0
      size_t soname_indx;
6975
6976
      /* If we are supposed to export all symbols into the dynamic symbol
6977
   table (this is not the normal case), then do so.  */
6978
0
      if (info->export_dynamic
6979
0
    || (bfd_link_executable (info) && info->dynamic))
6980
0
  {
6981
0
    struct elf_info_failed eif;
6982
6983
0
    eif.info = info;
6984
0
    eif.failed = false;
6985
0
    elf_link_hash_traverse (elf_hash_table (info),
6986
0
          _bfd_elf_export_symbol,
6987
0
          &eif);
6988
0
    if (eif.failed)
6989
0
      return false;
6990
0
  }
6991
6992
0
      if (soname != NULL)
6993
0
  {
6994
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6995
0
               soname, true);
6996
0
    if (soname_indx == (size_t) -1
6997
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6998
0
      return false;
6999
0
  }
7000
0
      else
7001
0
  soname_indx = (size_t) -1;
7002
7003
      /* Make all global versions with definition.  */
7004
0
      for (t = info->version_info; t != NULL; t = t->next)
7005
0
  for (d = t->globals.list; d != NULL; d = d->next)
7006
0
    if (!d->symver && d->literal)
7007
0
      {
7008
0
        const char *verstr, *name;
7009
0
        size_t namelen, verlen, newlen;
7010
0
        char *newname, *p, leading_char;
7011
0
        struct elf_link_hash_entry *newh;
7012
7013
0
        leading_char = bfd_get_symbol_leading_char (output_bfd);
7014
0
        name = d->pattern;
7015
0
        namelen = strlen (name) + (leading_char != '\0');
7016
0
        verstr = t->name;
7017
0
        verlen = strlen (verstr);
7018
0
        newlen = namelen + verlen + 3;
7019
7020
0
        newname = (char *) bfd_malloc (newlen);
7021
0
        if (newname == NULL)
7022
0
    return false;
7023
0
        newname[0] = leading_char;
7024
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7025
7026
        /* Check the hidden versioned definition.  */
7027
0
        p = newname + namelen;
7028
0
        *p++ = ELF_VER_CHR;
7029
0
        memcpy (p, verstr, verlen + 1);
7030
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7031
0
             newname, false, false,
7032
0
             false);
7033
0
        if (newh == NULL
7034
0
      || (newh->root.type != bfd_link_hash_defined
7035
0
          && newh->root.type != bfd_link_hash_defweak))
7036
0
    {
7037
      /* Check the default versioned definition.  */
7038
0
      *p++ = ELF_VER_CHR;
7039
0
      memcpy (p, verstr, verlen + 1);
7040
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7041
0
                 newname, false, false,
7042
0
                 false);
7043
0
    }
7044
0
        free (newname);
7045
7046
        /* Mark this version if there is a definition and it is
7047
     not defined in a shared object.  */
7048
0
        if (newh != NULL
7049
0
      && !newh->def_dynamic
7050
0
      && (newh->root.type == bfd_link_hash_defined
7051
0
          || newh->root.type == bfd_link_hash_defweak))
7052
0
    d->symver = 1;
7053
0
      }
7054
7055
      /* Attach all the symbols to their version information.  */
7056
0
      asvinfo.info = info;
7057
0
      asvinfo.failed = false;
7058
7059
0
      elf_link_hash_traverse (elf_hash_table (info),
7060
0
            _bfd_elf_link_assign_sym_version,
7061
0
            &asvinfo);
7062
0
      if (asvinfo.failed)
7063
0
  return false;
7064
7065
0
      if (!info->allow_undefined_version)
7066
0
  {
7067
    /* Check if all global versions have a definition.  */
7068
0
    bool all_defined = true;
7069
0
    for (t = info->version_info; t != NULL; t = t->next)
7070
0
      for (d = t->globals.list; d != NULL; d = d->next)
7071
0
        if (d->literal && !d->symver && !d->script)
7072
0
    {
7073
0
      _bfd_error_handler
7074
0
        (_("%s: undefined version: %s"),
7075
0
         d->pattern, t->name);
7076
0
      all_defined = false;
7077
0
    }
7078
7079
0
    if (!all_defined)
7080
0
      {
7081
0
        bfd_set_error (bfd_error_bad_value);
7082
0
        return false;
7083
0
      }
7084
0
  }
7085
7086
      /* Set up the version definition section.  */
7087
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7088
0
      BFD_ASSERT (s != NULL);
7089
7090
      /* We may have created additional version definitions if we are
7091
   just linking a regular application.  */
7092
0
      verdefs = info->version_info;
7093
7094
      /* Skip anonymous version tag.  */
7095
0
      if (verdefs != NULL && verdefs->vernum == 0)
7096
0
  verdefs = verdefs->next;
7097
7098
0
      if (verdefs == NULL && !info->create_default_symver)
7099
0
  s->flags |= SEC_EXCLUDE;
7100
0
      else
7101
0
  {
7102
0
    unsigned int cdefs;
7103
0
    bfd_size_type size;
7104
0
    bfd_byte *p;
7105
0
    Elf_Internal_Verdef def;
7106
0
    Elf_Internal_Verdaux defaux;
7107
0
    struct bfd_link_hash_entry *bh;
7108
0
    struct elf_link_hash_entry *h;
7109
0
    const char *name;
7110
7111
0
    cdefs = 0;
7112
0
    size = 0;
7113
7114
    /* Make space for the base version.  */
7115
0
    size += sizeof (Elf_External_Verdef);
7116
0
    size += sizeof (Elf_External_Verdaux);
7117
0
    ++cdefs;
7118
7119
    /* Make space for the default version.  */
7120
0
    if (info->create_default_symver)
7121
0
      {
7122
0
        size += sizeof (Elf_External_Verdef);
7123
0
        ++cdefs;
7124
0
      }
7125
7126
0
    for (t = verdefs; t != NULL; t = t->next)
7127
0
      {
7128
0
        struct bfd_elf_version_deps *n;
7129
7130
        /* Don't emit base version twice.  */
7131
0
        if (t->vernum == 0)
7132
0
    continue;
7133
7134
0
        size += sizeof (Elf_External_Verdef);
7135
0
        size += sizeof (Elf_External_Verdaux);
7136
0
        ++cdefs;
7137
7138
0
        for (n = t->deps; n != NULL; n = n->next)
7139
0
    size += sizeof (Elf_External_Verdaux);
7140
0
      }
7141
7142
0
    s->size = size;
7143
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7144
0
    if (s->contents == NULL && s->size != 0)
7145
0
      return false;
7146
0
    s->alloced = 1;
7147
7148
    /* Fill in the version definition section.  */
7149
7150
0
    p = s->contents;
7151
7152
0
    def.vd_version = VER_DEF_CURRENT;
7153
0
    def.vd_flags = VER_FLG_BASE;
7154
0
    def.vd_ndx = 1;
7155
0
    def.vd_cnt = 1;
7156
0
    if (info->create_default_symver)
7157
0
      {
7158
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7159
0
        def.vd_next = sizeof (Elf_External_Verdef);
7160
0
      }
7161
0
    else
7162
0
      {
7163
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7164
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7165
0
           + sizeof (Elf_External_Verdaux));
7166
0
      }
7167
7168
0
    if (soname_indx != (size_t) -1)
7169
0
      {
7170
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7171
0
              soname_indx);
7172
0
        def.vd_hash = bfd_elf_hash (soname);
7173
0
        defaux.vda_name = soname_indx;
7174
0
        name = soname;
7175
0
      }
7176
0
    else
7177
0
      {
7178
0
        size_t indx;
7179
7180
0
        name = lbasename (bfd_get_filename (output_bfd));
7181
0
        def.vd_hash = bfd_elf_hash (name);
7182
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7183
0
            name, false);
7184
0
        if (indx == (size_t) -1)
7185
0
    return false;
7186
0
        defaux.vda_name = indx;
7187
0
      }
7188
0
    defaux.vda_next = 0;
7189
7190
0
    _bfd_elf_swap_verdef_out (output_bfd, &def,
7191
0
            (Elf_External_Verdef *) p);
7192
0
    p += sizeof (Elf_External_Verdef);
7193
0
    if (info->create_default_symver)
7194
0
      {
7195
        /* Add a symbol representing this version.  */
7196
0
        bh = NULL;
7197
0
        if (! (_bfd_generic_link_add_one_symbol
7198
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7199
0
          0, NULL, false,
7200
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7201
0
    return false;
7202
0
        h = (struct elf_link_hash_entry *) bh;
7203
0
        h->non_elf = 0;
7204
0
        h->def_regular = 1;
7205
0
        h->type = STT_OBJECT;
7206
0
        h->verinfo.vertree = NULL;
7207
7208
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7209
0
    return false;
7210
7211
        /* Create a duplicate of the base version with the same
7212
     aux block, but different flags.  */
7213
0
        def.vd_flags = 0;
7214
0
        def.vd_ndx = 2;
7215
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7216
0
        if (verdefs)
7217
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7218
0
             + sizeof (Elf_External_Verdaux));
7219
0
        else
7220
0
    def.vd_next = 0;
7221
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7222
0
          (Elf_External_Verdef *) p);
7223
0
        p += sizeof (Elf_External_Verdef);
7224
0
      }
7225
0
    _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7226
0
             (Elf_External_Verdaux *) p);
7227
0
    p += sizeof (Elf_External_Verdaux);
7228
7229
0
    for (t = verdefs; t != NULL; t = t->next)
7230
0
      {
7231
0
        unsigned int cdeps;
7232
0
        struct bfd_elf_version_deps *n;
7233
7234
        /* Don't emit the base version twice.  */
7235
0
        if (t->vernum == 0)
7236
0
    continue;
7237
7238
0
        cdeps = 0;
7239
0
        for (n = t->deps; n != NULL; n = n->next)
7240
0
    ++cdeps;
7241
7242
        /* Add a symbol representing this version.  */
7243
0
        bh = NULL;
7244
0
        if (! (_bfd_generic_link_add_one_symbol
7245
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7246
0
          0, NULL, false,
7247
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7248
0
    return false;
7249
0
        h = (struct elf_link_hash_entry *) bh;
7250
0
        h->non_elf = 0;
7251
0
        h->def_regular = 1;
7252
0
        h->type = STT_OBJECT;
7253
0
        h->verinfo.vertree = t;
7254
7255
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7256
0
    return false;
7257
7258
0
        def.vd_version = VER_DEF_CURRENT;
7259
0
        def.vd_flags = 0;
7260
0
        if (t->globals.list == NULL
7261
0
      && t->locals.list == NULL
7262
0
      && ! t->used)
7263
0
    def.vd_flags |= VER_FLG_WEAK;
7264
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7265
0
        def.vd_cnt = cdeps + 1;
7266
0
        def.vd_hash = bfd_elf_hash (t->name);
7267
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7268
0
        def.vd_next = 0;
7269
7270
        /* If a basever node is next, it *must* be the last node in
7271
     the chain, otherwise Verdef construction breaks.  */
7272
0
        if (t->next != NULL && t->next->vernum == 0)
7273
0
    BFD_ASSERT (t->next->next == NULL);
7274
7275
0
        if (t->next != NULL && t->next->vernum != 0)
7276
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7277
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7278
7279
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7280
0
          (Elf_External_Verdef *) p);
7281
0
        p += sizeof (Elf_External_Verdef);
7282
7283
0
        defaux.vda_name = h->dynstr_index;
7284
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7285
0
              h->dynstr_index);
7286
0
        defaux.vda_next = 0;
7287
0
        if (t->deps != NULL)
7288
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7289
0
        t->name_indx = defaux.vda_name;
7290
7291
0
        _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7292
0
           (Elf_External_Verdaux *) p);
7293
0
        p += sizeof (Elf_External_Verdaux);
7294
7295
0
        for (n = t->deps; n != NULL; n = n->next)
7296
0
    {
7297
0
      if (n->version_needed == NULL)
7298
0
        {
7299
          /* This can happen if there was an error in the
7300
       version script.  */
7301
0
          defaux.vda_name = 0;
7302
0
        }
7303
0
      else
7304
0
        {
7305
0
          defaux.vda_name = n->version_needed->name_indx;
7306
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7307
0
                defaux.vda_name);
7308
0
        }
7309
0
      if (n->next == NULL)
7310
0
        defaux.vda_next = 0;
7311
0
      else
7312
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7313
7314
0
      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7315
0
               (Elf_External_Verdaux *) p);
7316
0
      p += sizeof (Elf_External_Verdaux);
7317
0
    }
7318
0
      }
7319
7320
0
    elf_tdata (output_bfd)->cverdefs = cdefs;
7321
0
  }
7322
0
    }
7323
7324
0
  if (info->gc_sections && bed->can_gc_sections)
7325
0
    {
7326
0
      struct elf_gc_sweep_symbol_info sweep_info;
7327
7328
      /* Remove the symbols that were in the swept sections from the
7329
   dynamic symbol table.  */
7330
0
      sweep_info.info = info;
7331
0
      sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7332
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7333
0
            &sweep_info);
7334
0
    }
7335
7336
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7337
0
    {
7338
0
      asection *s;
7339
0
      struct elf_find_verdep_info sinfo;
7340
7341
      /* Work out the size of the version reference section.  */
7342
7343
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7344
0
      BFD_ASSERT (s != NULL);
7345
7346
0
      sinfo.info = info;
7347
0
      sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7348
0
      if (sinfo.vers == 0)
7349
0
  sinfo.vers = 1;
7350
0
      sinfo.failed = false;
7351
7352
0
      elf_link_hash_traverse (elf_hash_table (info),
7353
0
            _bfd_elf_link_find_version_dependencies,
7354
0
            &sinfo);
7355
0
      if (sinfo.failed)
7356
0
  return false;
7357
7358
0
      bed->elf_backend_add_glibc_version_dependency (&sinfo);
7359
0
      if (sinfo.failed)
7360
0
  return false;
7361
7362
0
      if (elf_tdata (output_bfd)->verref == NULL)
7363
0
  s->flags |= SEC_EXCLUDE;
7364
0
      else
7365
0
  {
7366
0
    Elf_Internal_Verneed *vn;
7367
0
    unsigned int size;
7368
0
    unsigned int crefs;
7369
0
    bfd_byte *p;
7370
7371
    /* Build the version dependency section.  */
7372
0
    size = 0;
7373
0
    crefs = 0;
7374
0
    for (vn = elf_tdata (output_bfd)->verref;
7375
0
         vn != NULL;
7376
0
         vn = vn->vn_nextref)
7377
0
      {
7378
0
        Elf_Internal_Vernaux *a;
7379
7380
0
        size += sizeof (Elf_External_Verneed);
7381
0
        ++crefs;
7382
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7383
0
    size += sizeof (Elf_External_Vernaux);
7384
0
      }
7385
7386
0
    s->size = size;
7387
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7388
0
    if (s->contents == NULL)
7389
0
      return false;
7390
0
    s->alloced = 1;
7391
7392
0
    p = s->contents;
7393
0
    for (vn = elf_tdata (output_bfd)->verref;
7394
0
         vn != NULL;
7395
0
         vn = vn->vn_nextref)
7396
0
      {
7397
0
        unsigned int caux;
7398
0
        Elf_Internal_Vernaux *a;
7399
0
        size_t indx;
7400
7401
0
        caux = 0;
7402
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7403
0
    ++caux;
7404
7405
0
        vn->vn_version = VER_NEED_CURRENT;
7406
0
        vn->vn_cnt = caux;
7407
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7408
0
            elf_dt_name (vn->vn_bfd) != NULL
7409
0
            ? elf_dt_name (vn->vn_bfd)
7410
0
            : lbasename (bfd_get_filename
7411
0
                   (vn->vn_bfd)),
7412
0
            false);
7413
0
        if (indx == (size_t) -1)
7414
0
    return false;
7415
0
        vn->vn_file = indx;
7416
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7417
0
        if (vn->vn_nextref == NULL)
7418
0
    vn->vn_next = 0;
7419
0
        else
7420
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7421
0
             + caux * sizeof (Elf_External_Vernaux));
7422
7423
0
        _bfd_elf_swap_verneed_out (output_bfd, vn,
7424
0
           (Elf_External_Verneed *) p);
7425
0
        p += sizeof (Elf_External_Verneed);
7426
7427
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7428
0
    {
7429
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7430
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7431
0
                a->vna_nodename, false);
7432
0
      if (indx == (size_t) -1)
7433
0
        return false;
7434
0
      a->vna_name = indx;
7435
0
      if (a->vna_nextptr == NULL)
7436
0
        a->vna_next = 0;
7437
0
      else
7438
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7439
7440
0
      _bfd_elf_swap_vernaux_out (output_bfd, a,
7441
0
               (Elf_External_Vernaux *) p);
7442
0
      p += sizeof (Elf_External_Vernaux);
7443
0
    }
7444
0
      }
7445
7446
0
    elf_tdata (output_bfd)->cverrefs = crefs;
7447
0
  }
7448
0
    }
7449
7450
0
  if (bfd_link_relocatable (info)
7451
0
      && !bfd_elf_size_group_sections (info))
7452
0
    return false;
7453
7454
  /* Determine any GNU_STACK segment requirements, after the backend
7455
     has had a chance to set a default segment size.  */
7456
0
  if (info->execstack)
7457
0
    {
7458
      /* If the user has explicitly requested warnings, then generate one even
7459
   though the choice is the result of another command line option.  */
7460
0
      if (info->warn_execstack == 1)
7461
0
  {
7462
0
    if (info->error_execstack)
7463
0
      {
7464
0
        _bfd_error_handler
7465
0
    (_("\
7466
0
error: creating an executable stack because of -z execstack command line option"));
7467
0
        return false;
7468
0
      }
7469
7470
0
    _bfd_error_handler
7471
0
      (_("\
7472
0
warning: enabling an executable stack because of -z execstack command line option"));
7473
0
  }
7474
7475
0
      elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7476
0
    }
7477
0
  else if (info->noexecstack)
7478
0
    elf_stack_flags (output_bfd) = PF_R | PF_W;
7479
0
  else
7480
0
    {
7481
0
      bfd *inputobj;
7482
0
      asection *notesec = NULL;
7483
0
      bfd *noteobj = NULL;
7484
0
      bfd *emptyobj = NULL;
7485
0
      int exec = 0;
7486
7487
0
      for (inputobj = info->input_bfds;
7488
0
     inputobj;
7489
0
     inputobj = inputobj->link.next)
7490
0
  {
7491
0
    asection *s;
7492
7493
0
    if (inputobj->flags
7494
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7495
0
      continue;
7496
0
    s = inputobj->sections;
7497
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7498
0
      continue;
7499
7500
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7501
0
    if (s)
7502
0
      {
7503
0
        notesec = s;
7504
0
        if (s->flags & SEC_CODE)
7505
0
    {
7506
0
      noteobj = inputobj;
7507
0
      exec = PF_X;
7508
      /* There is no point in scanning the remaining bfds.  */
7509
0
      break;
7510
0
    }
7511
0
      }
7512
0
    else if (bed->default_execstack && info->default_execstack)
7513
0
      {
7514
0
        exec = PF_X;
7515
0
        emptyobj = inputobj;
7516
0
      }
7517
0
  }
7518
7519
0
      if (notesec || info->stacksize > 0)
7520
0
  {
7521
0
    if (exec)
7522
0
      {
7523
0
        if (info->warn_execstack != 0)
7524
0
    {
7525
      /* PR 29072: Because an executable stack is a serious
7526
         security risk, make sure that the user knows that it is
7527
         being enabled despite the fact that it was not requested
7528
         on the command line.  */
7529
0
      if (noteobj)
7530
0
        {
7531
0
          if (info->error_execstack)
7532
0
      {
7533
0
        _bfd_error_handler (_("\
7534
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7535
0
                bfd_get_filename (noteobj));
7536
0
        return false;
7537
0
      }
7538
7539
0
          _bfd_error_handler (_("\
7540
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7541
0
           bfd_get_filename (noteobj));
7542
0
        }
7543
0
      else if (emptyobj)
7544
0
        {
7545
0
          if (info->error_execstack)
7546
0
      {
7547
0
        _bfd_error_handler (_("\
7548
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7549
0
                bfd_get_filename (emptyobj));
7550
0
        return false;
7551
0
      }
7552
7553
0
          _bfd_error_handler (_("\
7554
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7555
0
            bfd_get_filename (emptyobj));
7556
0
          _bfd_error_handler (_("\
7557
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7558
0
        }
7559
0
    }
7560
0
      }
7561
0
    elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7562
0
  }
7563
7564
0
      if (notesec && exec && bfd_link_relocatable (info)
7565
0
    && notesec->output_section != bfd_abs_section_ptr)
7566
0
  notesec->output_section->flags |= SEC_CODE;
7567
0
    }
7568
7569
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7570
0
    {
7571
0
      struct elf_info_failed eif;
7572
0
      struct elf_link_hash_entry *h;
7573
0
      asection *dynstr;
7574
0
      asection *s;
7575
7576
0
      *sinterpptr = elf_hash_table (info)->interp;
7577
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7578
7579
0
      if (info->symbolic)
7580
0
  {
7581
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7582
0
      return false;
7583
0
    info->flags |= DF_SYMBOLIC;
7584
0
  }
7585
7586
0
      if (rpath != NULL)
7587
0
  {
7588
0
    size_t indx;
7589
0
    bfd_vma tag;
7590
7591
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7592
0
              true);
7593
0
    if (indx == (size_t) -1)
7594
0
      return false;
7595
7596
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7597
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7598
0
      return false;
7599
0
  }
7600
7601
0
      if (filter_shlib != NULL)
7602
0
  {
7603
0
    size_t indx;
7604
7605
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7606
0
              filter_shlib, true);
7607
0
    if (indx == (size_t) -1
7608
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7609
0
      return false;
7610
0
  }
7611
7612
0
      if (auxiliary_filters != NULL)
7613
0
  {
7614
0
    const char * const *p;
7615
7616
0
    for (p = auxiliary_filters; *p != NULL; p++)
7617
0
      {
7618
0
        size_t indx;
7619
7620
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7621
0
            *p, true);
7622
0
        if (indx == (size_t) -1
7623
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7624
0
    return false;
7625
0
      }
7626
0
  }
7627
7628
0
      if (audit != NULL)
7629
0
  {
7630
0
    size_t indx;
7631
7632
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7633
0
              true);
7634
0
    if (indx == (size_t) -1
7635
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7636
0
      return false;
7637
0
  }
7638
7639
0
      if (depaudit != NULL)
7640
0
  {
7641
0
    size_t indx;
7642
7643
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7644
0
              true);
7645
0
    if (indx == (size_t) -1
7646
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7647
0
      return false;
7648
0
  }
7649
7650
0
      eif.info = info;
7651
0
      eif.failed = false;
7652
7653
      /* Find all symbols which were defined in a dynamic object and make
7654
   the backend pick a reasonable value for them.  */
7655
0
      elf_link_hash_traverse (elf_hash_table (info),
7656
0
            _bfd_elf_adjust_dynamic_symbol,
7657
0
            &eif);
7658
0
      if (eif.failed)
7659
0
  return false;
7660
7661
      /* Add some entries to the .dynamic section.  We fill in some of the
7662
   values later, in bfd_elf_final_link, but we must add the entries
7663
   now so that we know the final size of the .dynamic section.  */
7664
7665
      /* If there are initialization and/or finalization functions to
7666
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7667
0
      h = (info->init_function
7668
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7669
0
           info->init_function, false,
7670
0
           false, false)
7671
0
     : NULL);
7672
0
      if (h != NULL
7673
0
    && (h->ref_regular
7674
0
        || h->def_regular))
7675
0
  {
7676
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7677
0
      return false;
7678
0
  }
7679
0
      h = (info->fini_function
7680
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7681
0
           info->fini_function, false,
7682
0
           false, false)
7683
0
     : NULL);
7684
0
      if (h != NULL
7685
0
    && (h->ref_regular
7686
0
        || h->def_regular))
7687
0
  {
7688
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7689
0
      return false;
7690
0
  }
7691
7692
0
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7693
0
      if (s != NULL && s->linker_has_input)
7694
0
  {
7695
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7696
0
    if (! bfd_link_executable (info))
7697
0
      {
7698
0
        bfd *sub;
7699
0
        asection *o;
7700
7701
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7702
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7703
0
        && (o = sub->sections) != NULL
7704
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7705
0
      for (o = sub->sections; o != NULL; o = o->next)
7706
0
        if (elf_section_data (o)->this_hdr.sh_type
7707
0
      == SHT_PREINIT_ARRAY)
7708
0
          {
7709
0
      _bfd_error_handler
7710
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7711
0
         sub);
7712
0
      break;
7713
0
          }
7714
7715
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7716
0
        return false;
7717
0
      }
7718
7719
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7720
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7721
0
      return false;
7722
0
  }
7723
0
      s = bfd_get_section_by_name (output_bfd, ".init_array");
7724
0
      if (s != NULL && s->linker_has_input)
7725
0
  {
7726
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7727
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7728
0
      return false;
7729
0
  }
7730
0
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
7731
0
      if (s != NULL && s->linker_has_input)
7732
0
  {
7733
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7734
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7735
0
      return false;
7736
0
  }
7737
7738
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7739
      /* If .dynstr is excluded from the link, we don't want any of
7740
   these tags.  Strictly, we should be checking each section
7741
   individually;  This quick check covers for the case where
7742
   someone does a /DISCARD/ : { *(*) }.  */
7743
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7744
0
  {
7745
0
    bfd_size_type strsize;
7746
7747
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7748
0
    if ((info->emit_hash
7749
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7750
0
        || (info->emit_gnu_hash
7751
0
      && (bed->record_xhash_symbol == NULL
7752
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7753
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7754
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7755
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7756
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7757
0
                bed->s->sizeof_sym)
7758
0
        || (info->gnu_flags_1
7759
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7760
0
              info->gnu_flags_1)))
7761
0
      return false;
7762
0
  }
7763
0
    }
7764
7765
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7766
0
    return false;
7767
7768
  /* The backend must work out the sizes of all the other dynamic
7769
     sections.  */
7770
0
  if (bed->elf_backend_late_size_sections != NULL
7771
0
      && !bed->elf_backend_late_size_sections (output_bfd, info))
7772
0
    return false;
7773
7774
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7775
0
    {
7776
0
      if (elf_tdata (output_bfd)->cverdefs)
7777
0
  {
7778
0
    unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7779
7780
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7781
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7782
0
      return false;
7783
0
  }
7784
7785
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7786
0
  {
7787
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7788
0
      return false;
7789
0
  }
7790
0
      else if (info->flags & DF_BIND_NOW)
7791
0
  {
7792
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7793
0
      return false;
7794
0
  }
7795
7796
0
      if (info->flags_1)
7797
0
  {
7798
0
    if (bfd_link_executable (info))
7799
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7800
0
        | DF_1_NODELETE
7801
0
        | DF_1_NOOPEN);
7802
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7803
0
      return false;
7804
0
  }
7805
7806
0
      if (elf_tdata (output_bfd)->cverrefs)
7807
0
  {
7808
0
    unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7809
7810
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7811
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7812
0
      return false;
7813
0
  }
7814
7815
0
      if ((elf_tdata (output_bfd)->cverrefs == 0
7816
0
     && elf_tdata (output_bfd)->cverdefs == 0)
7817
0
    || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7818
0
  {
7819
0
    asection *s;
7820
7821
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7822
0
    s->flags |= SEC_EXCLUDE;
7823
0
  }
7824
0
    }
7825
0
  return true;
7826
0
}
7827
7828
/* Find the first non-excluded output section.  We'll use its
7829
   section symbol for some emitted relocs.  */
7830
void
7831
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7832
0
{
7833
0
  asection *s;
7834
0
  asection *found = NULL;
7835
7836
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7837
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7838
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7839
0
      {
7840
0
  found = s;
7841
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7842
0
    break;
7843
0
      }
7844
0
  elf_hash_table (info)->text_index_section = found;
7845
0
}
7846
7847
/* Find two non-excluded output sections, one for code, one for data.
7848
   We'll use their section symbols for some emitted relocs.  */
7849
void
7850
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7851
0
{
7852
0
  asection *s;
7853
0
  asection *found = NULL;
7854
7855
  /* Data first, since setting text_index_section changes
7856
     _bfd_elf_omit_section_dynsym_default.  */
7857
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7858
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7859
0
  && !(s->flags & SEC_READONLY)
7860
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7861
0
      {
7862
0
  found = s;
7863
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7864
0
    break;
7865
0
      }
7866
0
  elf_hash_table (info)->data_index_section = found;
7867
7868
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7869
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7870
0
  && (s->flags & SEC_READONLY)
7871
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7872
0
      {
7873
0
  found = s;
7874
0
  break;
7875
0
      }
7876
0
  elf_hash_table (info)->text_index_section = found;
7877
0
}
7878
7879
#define GNU_HASH_SECTION_NAME(bed)          \
7880
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7881
7882
bool
7883
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7884
0
{
7885
0
  elf_backend_data *bed;
7886
0
  unsigned long section_sym_count;
7887
0
  bfd_size_type dynsymcount = 0;
7888
7889
0
  if (!is_elf_hash_table (info->hash))
7890
0
    return true;
7891
7892
0
  bed = get_elf_backend_data (output_bfd);
7893
0
  (*bed->elf_backend_init_index_section) (output_bfd, info);
7894
7895
  /* Assign dynsym indices.  In a shared library we generate a section
7896
     symbol for each output section, which come first.  Next come all
7897
     of the back-end allocated local dynamic syms, followed by the rest
7898
     of the global symbols.
7899
7900
     This is usually not needed for static binaries, however backends
7901
     can request to always do it, e.g. the MIPS backend uses dynamic
7902
     symbol counts to lay out GOT, which will be produced in the
7903
     presence of GOT relocations even in static binaries (holding fixed
7904
     data in that case, to satisfy those relocations).  */
7905
7906
0
  if (elf_hash_table (info)->dynamic_sections_created
7907
0
      || bed->always_renumber_dynsyms)
7908
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7909
0
              &section_sym_count);
7910
7911
0
  if (elf_hash_table (info)->dynamic_sections_created)
7912
0
    {
7913
0
      bfd *dynobj;
7914
0
      asection *s;
7915
0
      unsigned int dtagcount;
7916
7917
0
      dynobj = elf_hash_table (info)->dynobj;
7918
7919
      /* Work out the size of the symbol version section.  */
7920
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7921
0
      BFD_ASSERT (s != NULL);
7922
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7923
0
  {
7924
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7925
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7926
0
    if (s->contents == NULL)
7927
0
      return false;
7928
0
    s->alloced = 1;
7929
7930
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7931
0
      return false;
7932
0
  }
7933
7934
      /* Set the size of the .dynsym and .hash sections.  We counted
7935
   the number of dynamic symbols in elf_link_add_object_symbols.
7936
   We will build the contents of .dynsym and .hash when we build
7937
   the final symbol table, because until then we do not know the
7938
   correct value to give the symbols.  We built the .dynstr
7939
   section as we went along in elf_link_add_object_symbols.  */
7940
0
      s = elf_hash_table (info)->dynsym;
7941
0
      BFD_ASSERT (s != NULL);
7942
0
      s->size = dynsymcount * bed->s->sizeof_sym;
7943
7944
0
      s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7945
0
      if (s->contents == NULL)
7946
0
  return false;
7947
0
      s->alloced = 1;
7948
7949
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7950
   section syms, in case we don't output them all.  */
7951
0
      ++section_sym_count;
7952
0
      memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7953
7954
0
      elf_hash_table (info)->bucketcount = 0;
7955
7956
      /* Compute the size of the hashing table.  As a side effect this
7957
   computes the hash values for all the names we export.  */
7958
0
      if (info->emit_hash)
7959
0
  {
7960
0
    unsigned long int *hashcodes;
7961
0
    struct hash_codes_info hashinf;
7962
0
    bfd_size_type amt;
7963
0
    unsigned long int nsyms;
7964
0
    size_t bucketcount;
7965
0
    size_t hash_entry_size;
7966
7967
    /* Compute the hash values for all exported symbols.  At the same
7968
       time store the values in an array so that we could use them for
7969
       optimizations.  */
7970
0
    amt = dynsymcount * sizeof (unsigned long int);
7971
0
    hashcodes = (unsigned long int *) bfd_malloc (amt);
7972
0
    if (hashcodes == NULL)
7973
0
      return false;
7974
0
    hashinf.hashcodes = hashcodes;
7975
0
    hashinf.error = false;
7976
7977
    /* Put all hash values in HASHCODES.  */
7978
0
    elf_link_hash_traverse (elf_hash_table (info),
7979
0
          elf_collect_hash_codes, &hashinf);
7980
0
    if (hashinf.error)
7981
0
      {
7982
0
        free (hashcodes);
7983
0
        return false;
7984
0
      }
7985
7986
0
    nsyms = hashinf.hashcodes - hashcodes;
7987
0
    bucketcount
7988
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7989
0
    free (hashcodes);
7990
7991
0
    if (bucketcount == 0 && nsyms > 0)
7992
0
      return false;
7993
7994
0
    elf_hash_table (info)->bucketcount = bucketcount;
7995
7996
0
    s = bfd_get_linker_section (dynobj, ".hash");
7997
0
    BFD_ASSERT (s != NULL);
7998
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7999
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
8000
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8001
0
    if (s->contents == NULL)
8002
0
      return false;
8003
0
    s->alloced = 1;
8004
8005
0
    bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
8006
0
    bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
8007
0
       s->contents + hash_entry_size);
8008
0
  }
8009
8010
0
      if (info->emit_gnu_hash)
8011
0
  {
8012
0
    size_t i, cnt;
8013
0
    unsigned char *contents;
8014
0
    struct collect_gnu_hash_codes cinfo;
8015
0
    bfd_size_type amt;
8016
0
    size_t bucketcount;
8017
8018
0
    memset (&cinfo, 0, sizeof (cinfo));
8019
8020
    /* Compute the hash values for all exported symbols.  At the same
8021
       time store the values in an array so that we could use them for
8022
       optimizations.  */
8023
0
    amt = dynsymcount * 2 * sizeof (unsigned long int);
8024
0
    cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
8025
0
    if (cinfo.hashcodes == NULL)
8026
0
      return false;
8027
8028
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8029
0
    cinfo.min_dynindx = -1;
8030
0
    cinfo.output_bfd = output_bfd;
8031
0
    cinfo.bed = bed;
8032
8033
    /* Put all hash values in HASHCODES.  */
8034
0
    elf_link_hash_traverse (elf_hash_table (info),
8035
0
          elf_collect_gnu_hash_codes, &cinfo);
8036
0
    if (cinfo.error)
8037
0
      {
8038
0
        free (cinfo.hashcodes);
8039
0
        return false;
8040
0
      }
8041
8042
0
    bucketcount
8043
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8044
8045
0
    if (bucketcount == 0)
8046
0
      {
8047
0
        free (cinfo.hashcodes);
8048
0
        return false;
8049
0
      }
8050
8051
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
8052
0
    BFD_ASSERT (s != NULL);
8053
8054
0
    if (cinfo.nsyms == 0)
8055
0
      {
8056
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8057
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8058
0
        free (cinfo.hashcodes);
8059
0
        s->size = 5 * 4 + bed->s->arch_size / 8;
8060
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8061
0
        if (contents == NULL)
8062
0
    return false;
8063
0
        s->contents = contents;
8064
0
        s->alloced = 1;
8065
        /* 1 empty bucket.  */
8066
0
        bfd_put_32 (output_bfd, 1, contents);
8067
        /* SYMIDX above the special symbol 0.  */
8068
0
        bfd_put_32 (output_bfd, 1, contents + 4);
8069
        /* Just one word for bitmask.  */
8070
0
        bfd_put_32 (output_bfd, 1, contents + 8);
8071
        /* Only hash fn bloom filter.  */
8072
0
        bfd_put_32 (output_bfd, 0, contents + 12);
8073
        /* No hashes are valid - empty bitmask.  */
8074
0
        bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
8075
        /* No hashes in the only bucket.  */
8076
0
        bfd_put_32 (output_bfd, 0,
8077
0
        contents + 16 + bed->s->arch_size / 8);
8078
0
      }
8079
0
    else
8080
0
      {
8081
0
        unsigned long int maskwords, maskbitslog2, x;
8082
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8083
8084
0
        x = cinfo.nsyms;
8085
0
        maskbitslog2 = 1;
8086
0
        while ((x >>= 1) != 0)
8087
0
    ++maskbitslog2;
8088
0
        if (maskbitslog2 < 3)
8089
0
    maskbitslog2 = 5;
8090
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8091
0
    maskbitslog2 = maskbitslog2 + 3;
8092
0
        else
8093
0
    maskbitslog2 = maskbitslog2 + 2;
8094
0
        if (bed->s->arch_size == 64)
8095
0
    {
8096
0
      if (maskbitslog2 == 5)
8097
0
        maskbitslog2 = 6;
8098
0
      cinfo.shift1 = 6;
8099
0
    }
8100
0
        else
8101
0
    cinfo.shift1 = 5;
8102
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8103
0
        cinfo.shift2 = maskbitslog2;
8104
0
        cinfo.maskbits = 1 << maskbitslog2;
8105
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8106
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8107
0
        amt += maskwords * sizeof (bfd_vma);
8108
0
        cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8109
0
        if (cinfo.bitmask == NULL)
8110
0
    {
8111
0
      free (cinfo.hashcodes);
8112
0
      return false;
8113
0
    }
8114
8115
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8116
0
        cinfo.indx = cinfo.counts + bucketcount;
8117
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8118
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8119
8120
        /* Determine how often each hash bucket is used.  */
8121
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8122
0
        for (i = 0; i < cinfo.nsyms; ++i)
8123
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8124
8125
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8126
0
    if (cinfo.counts[i] != 0)
8127
0
      {
8128
0
        cinfo.indx[i] = cnt;
8129
0
        cnt += cinfo.counts[i];
8130
0
      }
8131
0
        BFD_ASSERT (cnt == dynsymcount);
8132
0
        cinfo.bucketcount = bucketcount;
8133
0
        cinfo.local_indx = cinfo.min_dynindx;
8134
8135
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8136
0
        s->size += cinfo.maskbits / 8;
8137
0
        if (bed->record_xhash_symbol != NULL)
8138
0
    s->size += cinfo.nsyms * 4;
8139
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8140
0
        if (contents == NULL)
8141
0
    {
8142
0
      free (cinfo.bitmask);
8143
0
      free (cinfo.hashcodes);
8144
0
      return false;
8145
0
    }
8146
8147
0
        s->contents = contents;
8148
0
        s->alloced = 1;
8149
0
        bfd_put_32 (output_bfd, bucketcount, contents);
8150
0
        bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8151
0
        bfd_put_32 (output_bfd, maskwords, contents + 8);
8152
0
        bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8153
0
        contents += 16 + cinfo.maskbits / 8;
8154
8155
0
        for (i = 0; i < bucketcount; ++i)
8156
0
    {
8157
0
      if (cinfo.counts[i] == 0)
8158
0
        bfd_put_32 (output_bfd, 0, contents);
8159
0
      else
8160
0
        bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8161
0
      contents += 4;
8162
0
    }
8163
8164
0
        cinfo.contents = contents;
8165
8166
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8167
8168
0
        if (elf_hash_table (info)->has_base_symbols)
8169
0
    {
8170
      /* Output base symbols first in DT_GNU_HASH so that
8171
         they will be picked before non-base symbols at
8172
         run-time.  */
8173
0
      cinfo.base_symbol = true;
8174
8175
      /* Renumber dynamic symbols, if populating .gnu.hash
8176
         section.  If using .MIPS.xhash, populate the
8177
         translation table.  */
8178
0
      elf_link_hash_traverse (elf_hash_table (info),
8179
0
            elf_gnu_hash_process_symidx, &cinfo);
8180
0
    }
8181
8182
        /* Output non-base symbols last.  */
8183
0
        cinfo.base_symbol = false;
8184
0
        elf_link_hash_traverse (elf_hash_table (info),
8185
0
              elf_gnu_hash_process_symidx, &cinfo);
8186
8187
0
        contents = s->contents + 16;
8188
0
        for (i = 0; i < maskwords; ++i)
8189
0
    {
8190
0
      bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8191
0
         contents);
8192
0
      contents += bed->s->arch_size / 8;
8193
0
    }
8194
8195
0
        free (cinfo.bitmask);
8196
0
        free (cinfo.hashcodes);
8197
0
      }
8198
0
  }
8199
8200
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8201
0
      BFD_ASSERT (s != NULL);
8202
8203
0
      elf_finalize_dynstr (output_bfd, info);
8204
8205
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8206
8207
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8208
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8209
0
    return false;
8210
0
    }
8211
8212
0
  return true;
8213
0
}
8214

8215
/* Create an entry in an ELF linker hash table.  */
8216
8217
struct bfd_hash_entry *
8218
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8219
          struct bfd_hash_table *table,
8220
          const char *string)
8221
0
{
8222
  /* Allocate the structure if it has not already been allocated by a
8223
     subclass.  */
8224
0
  if (entry == NULL)
8225
0
    {
8226
0
      entry = (struct bfd_hash_entry *)
8227
0
  bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8228
0
      if (entry == NULL)
8229
0
  return entry;
8230
0
    }
8231
8232
  /* Call the allocation method of the superclass.  */
8233
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8234
0
  if (entry != NULL)
8235
0
    {
8236
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8237
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8238
8239
      /* Set local fields.  */
8240
0
      ret->indx = -1;
8241
0
      ret->dynindx = -1;
8242
0
      ret->got = htab->init_got_refcount;
8243
0
      ret->plt = htab->init_plt_refcount;
8244
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8245
0
            - offsetof (struct elf_link_hash_entry, size)));
8246
      /* Assume that we have been called by a non-ELF symbol reader.
8247
   This flag is then reset by the code which reads an ELF input
8248
   file.  This ensures that a symbol created by a non-ELF symbol
8249
   reader will have the flag set correctly.  */
8250
0
      ret->non_elf = 1;
8251
0
    }
8252
8253
0
  return entry;
8254
0
}
8255
8256
/* Copy data from an indirect symbol to its direct symbol, hiding the
8257
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8258
8259
void
8260
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8261
          struct elf_link_hash_entry *dir,
8262
          struct elf_link_hash_entry *ind)
8263
0
{
8264
0
  struct elf_link_hash_table *htab;
8265
8266
0
  if (ind->dyn_relocs != NULL)
8267
0
    {
8268
0
      if (dir->dyn_relocs != NULL)
8269
0
  {
8270
0
    struct elf_dyn_relocs **pp;
8271
0
    struct elf_dyn_relocs *p;
8272
8273
    /* Add reloc counts against the indirect sym to the direct sym
8274
       list.  Merge any entries against the same section.  */
8275
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8276
0
      {
8277
0
        struct elf_dyn_relocs *q;
8278
8279
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8280
0
    if (q->sec == p->sec)
8281
0
      {
8282
0
        q->pc_count += p->pc_count;
8283
0
        q->count += p->count;
8284
0
        *pp = p->next;
8285
0
        break;
8286
0
      }
8287
0
        if (q == NULL)
8288
0
    pp = &p->next;
8289
0
      }
8290
0
    *pp = dir->dyn_relocs;
8291
0
  }
8292
8293
0
      dir->dyn_relocs = ind->dyn_relocs;
8294
0
      ind->dyn_relocs = NULL;
8295
0
    }
8296
8297
  /* Copy down any references that we may have already seen to the
8298
     symbol which just became indirect.  */
8299
8300
0
  if (dir->versioned != versioned_hidden)
8301
0
    dir->ref_dynamic |= ind->ref_dynamic;
8302
0
  dir->ref_regular |= ind->ref_regular;
8303
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8304
0
  dir->non_got_ref |= ind->non_got_ref;
8305
0
  dir->needs_plt |= ind->needs_plt;
8306
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8307
8308
0
  if (ind->root.type != bfd_link_hash_indirect)
8309
0
    return;
8310
8311
  /* Copy over the global and procedure linkage table refcount entries.
8312
     These may have been already set up by a check_relocs routine.  */
8313
0
  htab = elf_hash_table (info);
8314
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8315
0
    {
8316
0
      if (dir->got.refcount < 0)
8317
0
  dir->got.refcount = 0;
8318
0
      dir->got.refcount += ind->got.refcount;
8319
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8320
0
    }
8321
8322
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8323
0
    {
8324
0
      if (dir->plt.refcount < 0)
8325
0
  dir->plt.refcount = 0;
8326
0
      dir->plt.refcount += ind->plt.refcount;
8327
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8328
0
    }
8329
8330
0
  if (ind->dynindx != -1)
8331
0
    {
8332
0
      if (dir->dynindx != -1)
8333
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8334
0
      dir->dynindx = ind->dynindx;
8335
0
      dir->dynstr_index = ind->dynstr_index;
8336
0
      ind->dynindx = -1;
8337
0
      ind->dynstr_index = 0;
8338
0
    }
8339
0
}
8340
8341
void
8342
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8343
        struct elf_link_hash_entry *h,
8344
        bool force_local)
8345
0
{
8346
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8347
0
  if (h->type != STT_GNU_IFUNC)
8348
0
    {
8349
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8350
0
      h->needs_plt = 0;
8351
0
    }
8352
0
  if (force_local)
8353
0
    {
8354
0
      h->forced_local = 1;
8355
0
      if (h->dynindx != -1)
8356
0
  {
8357
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8358
0
          h->dynstr_index);
8359
0
    h->dynindx = -1;
8360
0
    h->dynstr_index = 0;
8361
0
  }
8362
0
    }
8363
0
}
8364
8365
/* Hide a symbol. */
8366
8367
void
8368
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8369
         struct bfd_link_info *info,
8370
         struct bfd_link_hash_entry *h)
8371
0
{
8372
0
  if (is_elf_hash_table (info->hash))
8373
0
    {
8374
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
8375
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8376
0
      eh->def_dynamic = 0;
8377
0
      eh->ref_dynamic = 0;
8378
0
      eh->dynamic_def = 0;
8379
0
      bed->elf_backend_hide_symbol (info, eh, true);
8380
0
    }
8381
0
}
8382
8383
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8384
   caller.  */
8385
8386
bool
8387
_bfd_elf_link_hash_table_init
8388
  (struct elf_link_hash_table *table,
8389
   bfd *abfd,
8390
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8391
              struct bfd_hash_table *,
8392
              const char *),
8393
   unsigned int entsize)
8394
0
{
8395
0
  bool ret;
8396
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
8397
0
  int can_refcount = bed->can_refcount;
8398
8399
0
  table->init_got_refcount.refcount = can_refcount - 1;
8400
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8401
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8402
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8403
  /* The first dynamic symbol is a dummy.  */
8404
0
  table->dynsymcount = 1;
8405
8406
0
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8407
8408
0
  table->root.type = bfd_link_elf_hash_table;
8409
0
  table->hash_table_id = bed->target_id;
8410
0
  table->target_os = bed->target_os;
8411
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8412
8413
0
  return ret;
8414
0
}
8415
8416
/* Create an ELF linker hash table.  */
8417
8418
struct bfd_link_hash_table *
8419
_bfd_elf_link_hash_table_create (bfd *abfd)
8420
0
{
8421
0
  struct elf_link_hash_table *ret;
8422
0
  size_t amt = sizeof (struct elf_link_hash_table);
8423
8424
0
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8425
0
  if (ret == NULL)
8426
0
    return NULL;
8427
8428
0
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8429
0
               sizeof (struct elf_link_hash_entry)))
8430
0
    {
8431
0
      free (ret);
8432
0
      return NULL;
8433
0
    }
8434
8435
0
  return &ret->root;
8436
0
}
8437
8438
/* Destroy an ELF linker hash table.  */
8439
8440
void
8441
_bfd_elf_link_hash_table_free (bfd *obfd)
8442
0
{
8443
0
  struct elf_link_hash_table *htab;
8444
8445
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8446
0
  if (htab->dynstr != NULL)
8447
0
    _bfd_elf_strtab_free (htab->dynstr);
8448
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8449
0
  if (htab->dynamic != NULL)
8450
0
    {
8451
0
      free (htab->dynamic->contents);
8452
0
      htab->dynamic->contents = NULL;
8453
0
    }
8454
0
  if (htab->first_hash != NULL)
8455
0
    {
8456
0
      bfd_hash_table_free (htab->first_hash);
8457
0
      free (htab->first_hash);
8458
0
    }
8459
0
  if (htab->eh_info.frame_hdr_is_compact)
8460
0
    free (htab->eh_info.u.compact.entries);
8461
0
  else
8462
0
    free (htab->eh_info.u.dwarf.array);
8463
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8464
0
  _bfd_generic_link_hash_table_free (obfd);
8465
0
}
8466
8467
/* This is a hook for the ELF emulation code in the generic linker to
8468
   tell the backend linker what file name to use for the DT_NEEDED
8469
   entry for a dynamic object.  */
8470
8471
void
8472
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8473
0
{
8474
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8475
0
      && bfd_get_format (abfd) == bfd_object)
8476
0
    elf_dt_name (abfd) = name;
8477
0
}
8478
8479
int
8480
bfd_elf_get_dyn_lib_class (bfd *abfd)
8481
0
{
8482
0
  int lib_class;
8483
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8484
0
      && bfd_get_format (abfd) == bfd_object)
8485
0
    lib_class = elf_dyn_lib_class (abfd);
8486
0
  else
8487
0
    lib_class = 0;
8488
0
  return lib_class;
8489
0
}
8490
8491
void
8492
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8493
0
{
8494
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8495
0
      && bfd_get_format (abfd) == bfd_object)
8496
0
    elf_dyn_lib_class (abfd) = lib_class;
8497
0
}
8498
8499
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8500
   the linker ELF emulation code.  */
8501
8502
struct bfd_link_needed_list *
8503
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8504
       struct bfd_link_info *info)
8505
0
{
8506
0
  if (! is_elf_hash_table (info->hash))
8507
0
    return NULL;
8508
0
  return elf_hash_table (info)->needed;
8509
0
}
8510
8511
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8512
   hook for the linker ELF emulation code.  */
8513
8514
struct bfd_link_needed_list *
8515
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8516
        struct bfd_link_info *info)
8517
0
{
8518
0
  if (! is_elf_hash_table (info->hash))
8519
0
    return NULL;
8520
0
  return elf_hash_table (info)->runpath;
8521
0
}
8522
8523
/* Get the name actually used for a dynamic object for a link.  This
8524
   is the SONAME entry if there is one.  Otherwise, it is the string
8525
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8526
8527
const char *
8528
bfd_elf_get_dt_soname (bfd *abfd)
8529
0
{
8530
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8531
0
      && bfd_get_format (abfd) == bfd_object)
8532
0
    return elf_dt_name (abfd);
8533
0
  return NULL;
8534
0
}
8535
8536
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8537
   the ELF linker emulation code.  */
8538
8539
bool
8540
bfd_elf_get_bfd_needed_list (bfd *abfd,
8541
           struct bfd_link_needed_list **pneeded)
8542
0
{
8543
0
  asection *s;
8544
0
  bfd_byte *dynbuf = NULL;
8545
0
  unsigned int elfsec;
8546
0
  unsigned long shlink;
8547
0
  bfd_byte *extdyn, *extdynend;
8548
0
  size_t extdynsize;
8549
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8550
8551
0
  *pneeded = NULL;
8552
8553
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8554
0
      || bfd_get_format (abfd) != bfd_object)
8555
0
    return true;
8556
8557
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8558
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8559
0
    return true;
8560
8561
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8562
0
    goto error_return;
8563
8564
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8565
0
  if (elfsec == SHN_BAD)
8566
0
    goto error_return;
8567
8568
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8569
8570
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8571
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8572
8573
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8574
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8575
0
       extdyn += extdynsize)
8576
0
    {
8577
0
      Elf_Internal_Dyn dyn;
8578
8579
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8580
8581
0
      if (dyn.d_tag == DT_NULL)
8582
0
  break;
8583
8584
0
      if (dyn.d_tag == DT_NEEDED)
8585
0
  {
8586
0
    const char *string;
8587
0
    struct bfd_link_needed_list *l;
8588
0
    unsigned int tagv = dyn.d_un.d_val;
8589
0
    size_t amt;
8590
8591
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8592
0
    if (string == NULL)
8593
0
      goto error_return;
8594
8595
0
    amt = sizeof *l;
8596
0
    l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8597
0
    if (l == NULL)
8598
0
      goto error_return;
8599
8600
0
    l->by = abfd;
8601
0
    l->name = string;
8602
0
    l->next = *pneeded;
8603
0
    *pneeded = l;
8604
0
  }
8605
0
    }
8606
8607
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8608
8609
0
  return true;
8610
8611
0
 error_return:
8612
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8613
0
  return false;
8614
0
}
8615
8616
struct elf_symbuf_symbol
8617
{
8618
  unsigned long st_name;  /* Symbol name, index in string tbl */
8619
  unsigned char st_info;  /* Type and binding attributes */
8620
  unsigned char st_other; /* Visibilty, and target specific */
8621
};
8622
8623
struct elf_symbuf_head
8624
{
8625
  struct elf_symbuf_symbol *ssym;
8626
  size_t count;
8627
  unsigned int st_shndx;
8628
};
8629
8630
struct elf_symbol
8631
{
8632
  union
8633
    {
8634
      Elf_Internal_Sym *isym;
8635
      struct elf_symbuf_symbol *ssym;
8636
      void *p;
8637
    } u;
8638
  const char *name;
8639
};
8640
8641
/* Sort references to symbols by ascending section number.  */
8642
8643
static int
8644
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8645
0
{
8646
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8647
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8648
8649
0
  if (s1->st_shndx != s2->st_shndx)
8650
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8651
  /* Final sort by the address of the sym in the symbuf ensures
8652
     a stable sort.  */
8653
0
  if (s1 != s2)
8654
0
    return s1 > s2 ? 1 : -1;
8655
0
  return 0;
8656
0
}
8657
8658
static int
8659
elf_sym_name_compare (const void *arg1, const void *arg2)
8660
0
{
8661
0
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8662
0
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8663
0
  int ret = strcmp (s1->name, s2->name);
8664
0
  if (ret != 0)
8665
0
    return ret;
8666
0
  if (s1->u.p != s2->u.p)
8667
0
    return s1->u.p > s2->u.p ? 1 : -1;
8668
0
  return 0;
8669
0
}
8670
8671
static struct elf_symbuf_head *
8672
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8673
0
{
8674
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8675
0
  struct elf_symbuf_symbol *ssym;
8676
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8677
0
  size_t i, shndx_count, total_size, amt;
8678
8679
0
  amt = symcount * sizeof (*indbuf);
8680
0
  indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8681
0
  if (indbuf == NULL)
8682
0
    return NULL;
8683
8684
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8685
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8686
0
      *ind++ = &isymbuf[i];
8687
0
  indbufend = ind;
8688
8689
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8690
0
   elf_sort_elf_symbol);
8691
8692
0
  shndx_count = 0;
8693
0
  if (indbufend > indbuf)
8694
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8695
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8696
0
  shndx_count++;
8697
8698
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8699
0
    + (indbufend - indbuf) * sizeof (*ssym));
8700
0
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8701
0
  if (ssymbuf == NULL)
8702
0
    {
8703
0
      free (indbuf);
8704
0
      return NULL;
8705
0
    }
8706
8707
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8708
0
  ssymbuf->ssym = NULL;
8709
0
  ssymbuf->count = shndx_count;
8710
0
  ssymbuf->st_shndx = 0;
8711
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8712
0
    {
8713
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8714
0
  {
8715
0
    ssymhead++;
8716
0
    ssymhead->ssym = ssym;
8717
0
    ssymhead->count = 0;
8718
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8719
0
  }
8720
0
      ssym->st_name = (*ind)->st_name;
8721
0
      ssym->st_info = (*ind)->st_info;
8722
0
      ssym->st_other = (*ind)->st_other;
8723
0
      ssymhead->count++;
8724
0
    }
8725
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8726
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8727
8728
0
  free (indbuf);
8729
0
  return ssymbuf;
8730
0
}
8731
8732
/* Check if 2 sections define the same set of local and global
8733
   symbols.  */
8734
8735
static bool
8736
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8737
           struct bfd_link_info *info)
8738
0
{
8739
0
  bfd *bfd1, *bfd2;
8740
0
  elf_backend_data *bed1, *bed2;
8741
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8742
0
  size_t symcount1, symcount2;
8743
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8744
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8745
0
  Elf_Internal_Sym *isym, *isymend;
8746
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8747
0
  size_t count1, count2, sec_count1, sec_count2, i;
8748
0
  unsigned int shndx1, shndx2;
8749
0
  bool result;
8750
0
  bool ignore_section_symbol_p;
8751
8752
0
  bfd1 = sec1->owner;
8753
0
  bfd2 = sec2->owner;
8754
8755
  /* Both sections have to be in ELF.  */
8756
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8757
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8758
0
    return false;
8759
8760
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8761
0
    return false;
8762
8763
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8764
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8765
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8766
0
    return false;
8767
8768
0
  bed1 = get_elf_backend_data (bfd1);
8769
0
  bed2 = get_elf_backend_data (bfd2);
8770
0
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8771
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8772
0
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8773
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8774
8775
0
  if (symcount1 == 0 || symcount2 == 0)
8776
0
    return false;
8777
8778
0
  result = false;
8779
0
  isymbuf1 = NULL;
8780
0
  isymbuf2 = NULL;
8781
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8782
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8783
8784
  /* Ignore section symbols only when matching non-debugging sections
8785
     or linkonce section with comdat section.  */
8786
0
  ignore_section_symbol_p
8787
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8788
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8789
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8790
8791
0
  if (ssymbuf1 == NULL)
8792
0
    {
8793
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8794
0
               NULL, NULL, NULL);
8795
0
      if (isymbuf1 == NULL)
8796
0
  goto done;
8797
8798
0
      if (info != NULL && !info->reduce_memory_overheads)
8799
0
  {
8800
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8801
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8802
0
  }
8803
0
    }
8804
8805
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8806
0
    {
8807
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8808
0
               NULL, NULL, NULL);
8809
0
      if (isymbuf2 == NULL)
8810
0
  goto done;
8811
8812
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8813
0
  {
8814
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8815
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8816
0
  }
8817
0
    }
8818
8819
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8820
0
    {
8821
      /* Optimized faster version.  */
8822
0
      size_t lo, hi, mid;
8823
0
      struct elf_symbol *symp;
8824
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8825
8826
0
      lo = 0;
8827
0
      hi = ssymbuf1->count;
8828
0
      ssymbuf1++;
8829
0
      count1 = 0;
8830
0
      sec_count1 = 0;
8831
0
      while (lo < hi)
8832
0
  {
8833
0
    mid = (lo + hi) / 2;
8834
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8835
0
      hi = mid;
8836
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8837
0
      lo = mid + 1;
8838
0
    else
8839
0
      {
8840
0
        count1 = ssymbuf1[mid].count;
8841
0
        ssymbuf1 += mid;
8842
0
        break;
8843
0
      }
8844
0
  }
8845
0
      if (ignore_section_symbol_p)
8846
0
  {
8847
0
    for (i = 0; i < count1; i++)
8848
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8849
0
        sec_count1++;
8850
0
    count1 -= sec_count1;
8851
0
  }
8852
8853
0
      lo = 0;
8854
0
      hi = ssymbuf2->count;
8855
0
      ssymbuf2++;
8856
0
      count2 = 0;
8857
0
      sec_count2 = 0;
8858
0
      while (lo < hi)
8859
0
  {
8860
0
    mid = (lo + hi) / 2;
8861
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8862
0
      hi = mid;
8863
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8864
0
      lo = mid + 1;
8865
0
    else
8866
0
      {
8867
0
        count2 = ssymbuf2[mid].count;
8868
0
        ssymbuf2 += mid;
8869
0
        break;
8870
0
      }
8871
0
  }
8872
0
      if (ignore_section_symbol_p)
8873
0
  {
8874
0
    for (i = 0; i < count2; i++)
8875
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8876
0
        sec_count2++;
8877
0
    count2 -= sec_count2;
8878
0
  }
8879
8880
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8881
0
  goto done;
8882
8883
0
      symtable1
8884
0
  = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8885
0
      symtable2
8886
0
  = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8887
0
      if (symtable1 == NULL || symtable2 == NULL)
8888
0
  goto done;
8889
8890
0
      symp = symtable1;
8891
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8892
0
     ssym < ssymend; ssym++)
8893
0
  if (sec_count1 == 0
8894
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8895
0
    {
8896
0
      symp->u.ssym = ssym;
8897
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8898
0
                hdr1->sh_link,
8899
0
                ssym->st_name);
8900
0
      if (symp->name == NULL)
8901
0
        goto done;
8902
0
      symp++;
8903
0
    }
8904
8905
0
      symp = symtable2;
8906
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8907
0
     ssym < ssymend; ssym++)
8908
0
  if (sec_count2 == 0
8909
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8910
0
    {
8911
0
      symp->u.ssym = ssym;
8912
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8913
0
                hdr2->sh_link,
8914
0
                ssym->st_name);
8915
0
      if (symp->name == NULL)
8916
0
        goto done;
8917
0
      symp++;
8918
0
    }
8919
8920
      /* Sort symbol by name.  */
8921
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8922
0
       elf_sym_name_compare);
8923
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8924
0
       elf_sym_name_compare);
8925
8926
0
      for (i = 0; i < count1; i++)
8927
  /* Two symbols must have the same binding, type and name.  */
8928
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8929
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8930
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8931
0
    goto done;
8932
8933
0
      result = true;
8934
0
      goto done;
8935
0
    }
8936
8937
0
  symtable1 = (struct elf_symbol *)
8938
0
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8939
0
  symtable2 = (struct elf_symbol *)
8940
0
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8941
0
  if (symtable1 == NULL || symtable2 == NULL)
8942
0
    goto done;
8943
8944
  /* Count definitions in the section.  */
8945
0
  count1 = 0;
8946
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8947
0
    if (isym->st_shndx == shndx1
8948
0
  && (!ignore_section_symbol_p
8949
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8950
0
      symtable1[count1++].u.isym = isym;
8951
8952
0
  count2 = 0;
8953
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8954
0
    if (isym->st_shndx == shndx2
8955
0
  && (!ignore_section_symbol_p
8956
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8957
0
      symtable2[count2++].u.isym = isym;
8958
8959
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8960
0
    goto done;
8961
8962
0
  for (i = 0; i < count1; i++)
8963
0
    {
8964
0
      symtable1[i].name
8965
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8966
0
             symtable1[i].u.isym->st_name);
8967
0
      if (symtable1[i].name == NULL)
8968
0
  goto done;
8969
0
    }
8970
8971
0
  for (i = 0; i < count2; i++)
8972
0
    {
8973
0
      symtable2[i].name
8974
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8975
0
             symtable2[i].u.isym->st_name);
8976
0
      if (symtable2[i].name == NULL)
8977
0
  goto done;
8978
0
    }
8979
8980
  /* Sort symbol by name.  */
8981
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8982
0
   elf_sym_name_compare);
8983
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8984
0
   elf_sym_name_compare);
8985
8986
0
  for (i = 0; i < count1; i++)
8987
    /* Two symbols must have the same binding, type and name.  */
8988
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8989
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8990
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8991
0
      goto done;
8992
8993
0
  result = true;
8994
8995
0
 done:
8996
0
  free (symtable1);
8997
0
  free (symtable2);
8998
0
  free (isymbuf1);
8999
0
  free (isymbuf2);
9000
9001
0
  return result;
9002
0
}
9003
9004
/* Return TRUE if 2 section types are compatible.  */
9005
9006
bool
9007
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9008
        bfd *bbfd, const asection *bsec)
9009
0
{
9010
0
  if (asec == NULL
9011
0
      || bsec == NULL
9012
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9013
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9014
0
    return true;
9015
9016
0
  return elf_section_type (asec) == elf_section_type (bsec);
9017
0
}
9018

9019
/* Final phase of ELF linker.  */
9020
9021
/* A structure we use to avoid passing large numbers of arguments.  */
9022
9023
struct elf_final_link_info
9024
{
9025
  /* General link information.  */
9026
  struct bfd_link_info *info;
9027
  /* Output BFD.  */
9028
  bfd *output_bfd;
9029
  /* Symbol string table.  */
9030
  struct elf_strtab_hash *symstrtab;
9031
  /* .hash section.  */
9032
  asection *hash_sec;
9033
  /* symbol version section (.gnu.version).  */
9034
  asection *symver_sec;
9035
  /* Buffer large enough to hold contents of any section.  */
9036
  bfd_byte *contents;
9037
  /* Buffer large enough to hold external relocs of any section.  */
9038
  void *external_relocs;
9039
  /* Buffer large enough to hold internal relocs of any section.  */
9040
  Elf_Internal_Rela *internal_relocs;
9041
  /* Buffer large enough to hold external local symbols of any input
9042
     BFD.  */
9043
  bfd_byte *external_syms;
9044
  /* And a buffer for symbol section indices.  */
9045
  Elf_External_Sym_Shndx *locsym_shndx;
9046
  /* Buffer large enough to hold internal local symbols of any input
9047
     BFD.  */
9048
  Elf_Internal_Sym *internal_syms;
9049
  /* Array large enough to hold a symbol index for each local symbol
9050
     of any input BFD.  */
9051
  long *indices;
9052
  /* Array large enough to hold a section pointer for each local
9053
     symbol of any input BFD.  */
9054
  asection **sections;
9055
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9056
  Elf_External_Sym_Shndx *symshndxbuf;
9057
  /* Number of STT_FILE syms seen.  */
9058
  size_t filesym_count;
9059
  /* Local symbol hash table.  */
9060
  struct bfd_hash_table local_hash_table;
9061
};
9062
9063
struct local_hash_entry
9064
{
9065
  /* Base hash table entry structure.  */
9066
  struct bfd_hash_entry root;
9067
  /* Size of the local symbol name.  */
9068
  size_t size;
9069
  /* Number of the duplicated local symbol names.  */
9070
  long count;
9071
};
9072
9073
/* Create an entry in the local symbol hash table.  */
9074
9075
static struct bfd_hash_entry *
9076
local_hash_newfunc (struct bfd_hash_entry *entry,
9077
        struct bfd_hash_table *table,
9078
        const char *string)
9079
0
{
9080
9081
  /* Allocate the structure if it has not already been allocated by a
9082
     subclass.  */
9083
0
  if (entry == NULL)
9084
0
    {
9085
0
      entry = bfd_hash_allocate (table,
9086
0
         sizeof (struct local_hash_entry));
9087
0
      if (entry == NULL)
9088
0
        return entry;
9089
0
    }
9090
9091
  /* Call the allocation method of the superclass.  */
9092
0
  entry = bfd_hash_newfunc (entry, table, string);
9093
0
  if (entry != NULL)
9094
0
    {
9095
0
      ((struct local_hash_entry *) entry)->count = 0;
9096
0
      ((struct local_hash_entry *) entry)->size = 0;
9097
0
    }
9098
9099
0
  return entry;
9100
0
}
9101
9102
/* This struct is used to pass information to elf_link_output_extsym.  */
9103
9104
struct elf_outext_info
9105
{
9106
  bool failed;
9107
  bool localsyms;
9108
  bool file_sym_done;
9109
  bool base_symbol;
9110
  struct elf_final_link_info *flinfo;
9111
};
9112
9113
9114
/* Support for evaluating a complex relocation.
9115
9116
   Complex relocations are generalized, self-describing relocations.  The
9117
   implementation of them consists of two parts: complex symbols, and the
9118
   relocations themselves.
9119
9120
   The relocations use a reserved elf-wide relocation type code (R_RELC
9121
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9122
   information (start bit, end bit, word width, etc) into the addend.  This
9123
   information is extracted from CGEN-generated operand tables within gas.
9124
9125
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9126
   internal) representing prefix-notation expressions, including but not
9127
   limited to those sorts of expressions normally encoded as addends in the
9128
   addend field.  The symbol mangling format is:
9129
9130
   <node> := <literal>
9131
    |  <unary-operator> ':' <node>
9132
    |  <binary-operator> ':' <node> ':' <node>
9133
    ;
9134
9135
   <literal> := 's' <digits=N> ':' <N character symbol name>
9136
       |  'S' <digits=N> ':' <N character section name>
9137
       |  '#' <hexdigits>
9138
       ;
9139
9140
   <binary-operator> := as in C
9141
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9142
9143
static bool
9144
set_symbol_value (bfd *bfd_with_globals,
9145
      Elf_Internal_Sym *isymbuf,
9146
      size_t locsymcount,
9147
      size_t num_sym,
9148
      size_t symidx,
9149
      bfd_vma val)
9150
0
{
9151
0
  struct elf_link_hash_entry *h;
9152
0
  size_t extsymoff = locsymcount;
9153
9154
0
  if (symidx < locsymcount)
9155
0
    {
9156
0
      Elf_Internal_Sym *sym;
9157
9158
0
      sym = isymbuf + symidx;
9159
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9160
0
  {
9161
    /* It is a local symbol: move it to the
9162
       "absolute" section and give it a value.  */
9163
0
    sym->st_shndx = SHN_ABS;
9164
0
    sym->st_value = val;
9165
0
    return true;
9166
0
  }
9167
0
      if (!elf_bad_symtab (bfd_with_globals))
9168
0
  {
9169
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9170
0
            bfd_with_globals);
9171
0
    bfd_set_error (bfd_error_bad_value);
9172
0
    return false;
9173
0
  }
9174
0
      extsymoff = 0;
9175
0
    }
9176
9177
  /* It is a global symbol: set its link type
9178
     to "defined" and give it a value.  */
9179
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9180
0
            extsymoff, num_sym);
9181
0
  if (h == NULL)
9182
0
    return false;
9183
0
  h->root.type = bfd_link_hash_defined;
9184
0
  h->root.u.def.value = val;
9185
0
  h->root.u.def.section = bfd_abs_section_ptr;
9186
0
  return true;
9187
0
}
9188
9189
static bool
9190
resolve_symbol (const char *name,
9191
    bfd *input_bfd,
9192
    struct elf_final_link_info *flinfo,
9193
    bfd_vma *result,
9194
    Elf_Internal_Sym *isymbuf,
9195
    size_t locsymcount)
9196
0
{
9197
0
  Elf_Internal_Sym *sym;
9198
0
  struct bfd_link_hash_entry *global_entry;
9199
0
  const char *candidate = NULL;
9200
0
  Elf_Internal_Shdr *symtab_hdr;
9201
0
  size_t i;
9202
9203
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9204
9205
0
  for (i = 0; i < locsymcount; ++ i)
9206
0
    {
9207
0
      sym = isymbuf + i;
9208
9209
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9210
0
  continue;
9211
9212
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9213
0
               symtab_hdr->sh_link,
9214
0
               sym->st_name);
9215
#ifdef DEBUG
9216
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9217
        name, candidate, (unsigned long) sym->st_value);
9218
#endif
9219
0
      if (candidate && strcmp (candidate, name) == 0)
9220
0
  {
9221
0
    asection *sec = flinfo->sections [i];
9222
9223
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9224
0
    *result += sec->output_offset + sec->output_section->vma;
9225
#ifdef DEBUG
9226
    printf ("Found symbol with value %8.8lx\n",
9227
      (unsigned long) *result);
9228
#endif
9229
0
    return true;
9230
0
  }
9231
0
    }
9232
9233
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9234
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9235
0
               false, false, true);
9236
0
  if (!global_entry)
9237
0
    return false;
9238
9239
0
  if (global_entry->type == bfd_link_hash_defined
9240
0
      || global_entry->type == bfd_link_hash_defweak)
9241
0
    {
9242
0
      *result = (global_entry->u.def.value
9243
0
     + global_entry->u.def.section->output_section->vma
9244
0
     + global_entry->u.def.section->output_offset);
9245
#ifdef DEBUG
9246
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9247
        global_entry->root.string, (unsigned long) *result);
9248
#endif
9249
0
      return true;
9250
0
    }
9251
9252
0
  return false;
9253
0
}
9254
9255
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9256
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9257
   names like "foo.end" which is the end address of section "foo".  */
9258
9259
static bool
9260
resolve_section (const char *name,
9261
     asection *sections,
9262
     bfd_vma *result,
9263
     bfd * abfd)
9264
0
{
9265
0
  asection *curr;
9266
0
  unsigned int len;
9267
9268
0
  for (curr = sections; curr; curr = curr->next)
9269
0
    if (strcmp (curr->name, name) == 0)
9270
0
      {
9271
0
  *result = curr->vma;
9272
0
  return true;
9273
0
      }
9274
9275
  /* Hmm. still haven't found it. try pseudo-section names.  */
9276
  /* FIXME: This could be coded more efficiently...  */
9277
0
  for (curr = sections; curr; curr = curr->next)
9278
0
    {
9279
0
      len = strlen (curr->name);
9280
0
      if (len > strlen (name))
9281
0
  continue;
9282
9283
0
      if (strncmp (curr->name, name, len) == 0)
9284
0
  {
9285
0
    if (startswith (name + len, ".end"))
9286
0
      {
9287
0
        *result = (curr->vma
9288
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9289
0
        return true;
9290
0
      }
9291
9292
    /* Insert more pseudo-section names here, if you like.  */
9293
0
  }
9294
0
    }
9295
9296
0
  return false;
9297
0
}
9298
9299
static void
9300
undefined_reference (const char *reftype, const char *name)
9301
0
{
9302
  /* xgettext:c-format */
9303
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9304
0
          reftype, name);
9305
0
  bfd_set_error (bfd_error_bad_value);
9306
0
}
9307
9308
static bool
9309
eval_symbol (bfd_vma *result,
9310
       const char **symp,
9311
       bfd *input_bfd,
9312
       struct elf_final_link_info *flinfo,
9313
       bfd_vma dot,
9314
       Elf_Internal_Sym *isymbuf,
9315
       size_t locsymcount,
9316
       int signed_p)
9317
0
{
9318
0
  size_t len;
9319
0
  size_t symlen;
9320
0
  bfd_vma a;
9321
0
  bfd_vma b;
9322
0
  char symbuf[4096];
9323
0
  const char *sym = *symp;
9324
0
  const char *symend;
9325
0
  bool symbol_is_section = false;
9326
9327
0
  len = strlen (sym);
9328
0
  symend = sym + len;
9329
9330
0
  if (len < 1 || len > sizeof (symbuf))
9331
0
    {
9332
0
      bfd_set_error (bfd_error_invalid_operation);
9333
0
      return false;
9334
0
    }
9335
9336
0
  switch (* sym)
9337
0
    {
9338
0
    case '.':
9339
0
      *result = dot;
9340
0
      *symp = sym + 1;
9341
0
      return true;
9342
9343
0
    case '#':
9344
0
      ++sym;
9345
0
      *result = strtoul (sym, (char **) symp, 16);
9346
0
      return true;
9347
9348
0
    case 'S':
9349
0
      symbol_is_section = true;
9350
      /* Fall through.  */
9351
0
    case 's':
9352
0
      ++sym;
9353
0
      symlen = strtol (sym, (char **) symp, 10);
9354
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9355
9356
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9357
0
  {
9358
0
    bfd_set_error (bfd_error_invalid_operation);
9359
0
    return false;
9360
0
  }
9361
9362
0
      memcpy (symbuf, sym, symlen);
9363
0
      symbuf[symlen] = '\0';
9364
0
      *symp = sym + symlen;
9365
9366
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9367
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9368
   interpretation here; section means "try section first", not "must be a
9369
   section", and likewise with symbol.  */
9370
9371
0
      if (symbol_is_section)
9372
0
  {
9373
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9374
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9375
0
          isymbuf, locsymcount))
9376
0
      {
9377
0
        undefined_reference ("section", symbuf);
9378
0
        return false;
9379
0
      }
9380
0
  }
9381
0
      else
9382
0
  {
9383
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9384
0
             isymbuf, locsymcount)
9385
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9386
0
           result, input_bfd))
9387
0
      {
9388
0
        undefined_reference ("symbol", symbuf);
9389
0
        return false;
9390
0
      }
9391
0
  }
9392
9393
0
      return true;
9394
9395
      /* All that remains are operators.  */
9396
9397
0
#define UNARY_OP(op)            \
9398
0
  if (startswith (sym, #op))          \
9399
0
    {               \
9400
0
      sym += strlen (#op);          \
9401
0
      if (*sym == ':')           \
9402
0
  ++sym;             \
9403
0
      *symp = sym;            \
9404
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9405
0
      isymbuf, locsymcount, signed_p)) \
9406
0
  return false;           \
9407
0
      if (signed_p)           \
9408
0
  *result = op ((bfd_signed_vma) a);     \
9409
0
      else              \
9410
0
  *result = op a;           \
9411
0
      return true;            \
9412
0
    }
9413
9414
0
#define BINARY_OP_HEAD(op)          \
9415
0
  if (startswith (sym, #op))          \
9416
0
    {               \
9417
0
      sym += strlen (#op);          \
9418
0
      if (*sym == ':')           \
9419
0
  ++sym;             \
9420
0
      *symp = sym;            \
9421
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9422
0
      isymbuf, locsymcount, signed_p)) \
9423
0
  return false;           \
9424
0
      ++*symp;              \
9425
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9426
0
      isymbuf, locsymcount, signed_p)) \
9427
0
  return false;
9428
0
#define BINARY_OP_TAIL(op)          \
9429
0
      if (signed_p)           \
9430
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9431
0
      else              \
9432
0
  *result = a op b;         \
9433
0
      return true;            \
9434
0
    }
9435
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9436
9437
0
    default:
9438
0
      UNARY_OP  (0-);
9439
0
      BINARY_OP_HEAD (<<);
9440
0
      if (b >= sizeof (a) * CHAR_BIT)
9441
0
  {
9442
0
    *result = 0;
9443
0
    return true;
9444
0
  }
9445
0
      signed_p = 0;
9446
0
      BINARY_OP_TAIL (<<);
9447
0
      BINARY_OP_HEAD (>>);
9448
0
      if (b >= sizeof (a) * CHAR_BIT)
9449
0
  {
9450
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9451
0
    return true;
9452
0
  }
9453
0
      BINARY_OP_TAIL (>>);
9454
0
      BINARY_OP (==);
9455
0
      BINARY_OP (!=);
9456
0
      BINARY_OP (<=);
9457
0
      BINARY_OP (>=);
9458
0
      BINARY_OP (&&);
9459
0
      BINARY_OP (||);
9460
0
      UNARY_OP  (~);
9461
0
      UNARY_OP  (!);
9462
0
      BINARY_OP (*);
9463
0
      BINARY_OP_HEAD (/);
9464
0
      if (b == 0)
9465
0
  {
9466
0
    _bfd_error_handler (_("division by zero"));
9467
0
    bfd_set_error (bfd_error_bad_value);
9468
0
    return false;
9469
0
  }
9470
0
      BINARY_OP_TAIL (/);
9471
0
      BINARY_OP_HEAD (%);
9472
0
      if (b == 0)
9473
0
  {
9474
0
    _bfd_error_handler (_("division by zero"));
9475
0
    bfd_set_error (bfd_error_bad_value);
9476
0
    return false;
9477
0
  }
9478
0
      BINARY_OP_TAIL (%);
9479
0
      BINARY_OP (^);
9480
0
      BINARY_OP (|);
9481
0
      BINARY_OP (&);
9482
0
      BINARY_OP (+);
9483
0
      BINARY_OP (-);
9484
0
      BINARY_OP (<);
9485
0
      BINARY_OP (>);
9486
0
#undef UNARY_OP
9487
0
#undef BINARY_OP
9488
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9489
0
      bfd_set_error (bfd_error_invalid_operation);
9490
0
      return false;
9491
0
    }
9492
0
}
9493
9494
static void
9495
put_value (bfd_vma size,
9496
     unsigned long chunksz,
9497
     bfd *input_bfd,
9498
     bfd_vma x,
9499
     bfd_byte *location)
9500
0
{
9501
0
  location += (size - chunksz);
9502
9503
0
  for (; size; size -= chunksz, location -= chunksz)
9504
0
    {
9505
0
      switch (chunksz)
9506
0
  {
9507
0
  case 1:
9508
0
    bfd_put_8 (input_bfd, x, location);
9509
0
    x >>= 8;
9510
0
    break;
9511
0
  case 2:
9512
0
    bfd_put_16 (input_bfd, x, location);
9513
0
    x >>= 16;
9514
0
    break;
9515
0
  case 4:
9516
0
    bfd_put_32 (input_bfd, x, location);
9517
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9518
0
    x >>= 16;
9519
0
    x >>= 16;
9520
0
    break;
9521
0
#ifdef BFD64
9522
0
  case 8:
9523
0
    bfd_put_64 (input_bfd, x, location);
9524
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9525
0
    x >>= 32;
9526
0
    x >>= 32;
9527
0
    break;
9528
0
#endif
9529
0
  default:
9530
0
    abort ();
9531
0
    break;
9532
0
  }
9533
0
    }
9534
0
}
9535
9536
static bfd_vma
9537
get_value (bfd_vma size,
9538
     unsigned long chunksz,
9539
     bfd *input_bfd,
9540
     bfd_byte *location)
9541
0
{
9542
0
  int shift;
9543
0
  bfd_vma x = 0;
9544
9545
  /* Sanity checks.  */
9546
0
  BFD_ASSERT (chunksz <= sizeof (x)
9547
0
        && size >= chunksz
9548
0
        && chunksz != 0
9549
0
        && (size % chunksz) == 0
9550
0
        && input_bfd != NULL
9551
0
        && location != NULL);
9552
9553
0
  if (chunksz == sizeof (x))
9554
0
    {
9555
0
      BFD_ASSERT (size == chunksz);
9556
9557
      /* Make sure that we do not perform an undefined shift operation.
9558
   We know that size == chunksz so there will only be one iteration
9559
   of the loop below.  */
9560
0
      shift = 0;
9561
0
    }
9562
0
  else
9563
0
    shift = 8 * chunksz;
9564
9565
0
  for (; size; size -= chunksz, location += chunksz)
9566
0
    {
9567
0
      switch (chunksz)
9568
0
  {
9569
0
  case 1:
9570
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9571
0
    break;
9572
0
  case 2:
9573
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9574
0
    break;
9575
0
  case 4:
9576
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9577
0
    break;
9578
0
#ifdef BFD64
9579
0
  case 8:
9580
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9581
0
    break;
9582
0
#endif
9583
0
  default:
9584
0
    abort ();
9585
0
  }
9586
0
    }
9587
0
  return x;
9588
0
}
9589
9590
static void
9591
decode_complex_addend (unsigned long *start,   /* in bits */
9592
           unsigned long *oplen,   /* in bits */
9593
           unsigned long *len,     /* in bits */
9594
           unsigned long *wordsz,  /* in bytes */
9595
           unsigned long *chunksz, /* in bytes */
9596
           unsigned long *lsb0_p,
9597
           unsigned long *signed_p,
9598
           unsigned long *trunc_p,
9599
           unsigned long encoded)
9600
0
{
9601
0
  * start     =  encoded  & 0x3F;
9602
0
  * len       = (encoded >>  6) & 0x3F;
9603
0
  * oplen     = (encoded >> 12) & 0x3F;
9604
0
  * wordsz    = (encoded >> 18) & 0xF;
9605
0
  * chunksz   = (encoded >> 22) & 0xF;
9606
0
  * lsb0_p    = (encoded >> 27) & 1;
9607
0
  * signed_p  = (encoded >> 28) & 1;
9608
0
  * trunc_p   = (encoded >> 29) & 1;
9609
0
}
9610
9611
bfd_reloc_status_type
9612
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9613
            asection *input_section,
9614
            bfd_byte *contents,
9615
            Elf_Internal_Rela *rel,
9616
            bfd_vma relocation)
9617
0
{
9618
0
  bfd_vma shift, x, mask;
9619
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9620
0
  bfd_reloc_status_type r;
9621
0
  bfd_size_type octets;
9622
9623
  /*  Perform this reloc, since it is complex.
9624
      (this is not to say that it necessarily refers to a complex
9625
      symbol; merely that it is a self-describing CGEN based reloc.
9626
      i.e. the addend has the complete reloc information (bit start, end,
9627
      word size, etc) encoded within it.).  */
9628
9629
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9630
0
       &chunksz, &lsb0_p, &signed_p,
9631
0
       &trunc_p, rel->r_addend);
9632
9633
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9634
9635
0
  if (lsb0_p)
9636
0
    shift = (start + 1) - len;
9637
0
  else
9638
0
    shift = (8 * wordsz) - (start + len);
9639
9640
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9641
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9642
9643
#ifdef DEBUG
9644
  printf ("Doing complex reloc: "
9645
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9646
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9647
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9648
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9649
    oplen, (unsigned long) x, (unsigned long) mask,
9650
    (unsigned long) relocation);
9651
#endif
9652
9653
0
  r = bfd_reloc_ok;
9654
0
  if (! trunc_p)
9655
    /* Now do an overflow check.  */
9656
0
    r = bfd_check_overflow ((signed_p
9657
0
           ? complain_overflow_signed
9658
0
           : complain_overflow_unsigned),
9659
0
          len, 0, (8 * wordsz),
9660
0
          relocation);
9661
9662
  /* Do the deed.  */
9663
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9664
9665
#ifdef DEBUG
9666
  printf ("           relocation: %8.8lx\n"
9667
    "         shifted mask: %8.8lx\n"
9668
    " shifted/masked reloc: %8.8lx\n"
9669
    "               result: %8.8lx\n",
9670
    (unsigned long) relocation, (unsigned long) (mask << shift),
9671
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9672
#endif
9673
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9674
0
  return r;
9675
0
}
9676
9677
/* Functions to read r_offset from external (target order) reloc
9678
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9679
   know the value is aligned.  */
9680
9681
static bfd_vma
9682
ext32l_r_offset (const void *p)
9683
0
{
9684
0
  union aligned32
9685
0
  {
9686
0
    uint32_t v;
9687
0
    unsigned char c[4];
9688
0
  };
9689
0
  const union aligned32 *a
9690
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9691
9692
0
  uint32_t aval = (  (uint32_t) a->c[0]
9693
0
       | (uint32_t) a->c[1] << 8
9694
0
       | (uint32_t) a->c[2] << 16
9695
0
       | (uint32_t) a->c[3] << 24);
9696
0
  return aval;
9697
0
}
9698
9699
static bfd_vma
9700
ext32b_r_offset (const void *p)
9701
0
{
9702
0
  union aligned32
9703
0
  {
9704
0
    uint32_t v;
9705
0
    unsigned char c[4];
9706
0
  };
9707
0
  const union aligned32 *a
9708
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9709
9710
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9711
0
       | (uint32_t) a->c[1] << 16
9712
0
       | (uint32_t) a->c[2] << 8
9713
0
       | (uint32_t) a->c[3]);
9714
0
  return aval;
9715
0
}
9716
9717
static bfd_vma
9718
ext64l_r_offset (const void *p)
9719
0
{
9720
0
  union aligned64
9721
0
  {
9722
0
    uint64_t v;
9723
0
    unsigned char c[8];
9724
0
  };
9725
0
  const union aligned64 *a
9726
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9727
9728
0
  uint64_t aval = (  (uint64_t) a->c[0]
9729
0
       | (uint64_t) a->c[1] << 8
9730
0
       | (uint64_t) a->c[2] << 16
9731
0
       | (uint64_t) a->c[3] << 24
9732
0
       | (uint64_t) a->c[4] << 32
9733
0
       | (uint64_t) a->c[5] << 40
9734
0
       | (uint64_t) a->c[6] << 48
9735
0
       | (uint64_t) a->c[7] << 56);
9736
0
  return aval;
9737
0
}
9738
9739
static bfd_vma
9740
ext64b_r_offset (const void *p)
9741
0
{
9742
0
  union aligned64
9743
0
  {
9744
0
    uint64_t v;
9745
0
    unsigned char c[8];
9746
0
  };
9747
0
  const union aligned64 *a
9748
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9749
9750
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9751
0
       | (uint64_t) a->c[1] << 48
9752
0
       | (uint64_t) a->c[2] << 40
9753
0
       | (uint64_t) a->c[3] << 32
9754
0
       | (uint64_t) a->c[4] << 24
9755
0
       | (uint64_t) a->c[5] << 16
9756
0
       | (uint64_t) a->c[6] << 8
9757
0
       | (uint64_t) a->c[7]);
9758
0
  return aval;
9759
0
}
9760
9761
/* When performing a relocatable link, the input relocations are
9762
   preserved.  But, if they reference global symbols, the indices
9763
   referenced must be updated.  Update all the relocations found in
9764
   RELDATA.  */
9765
9766
static bool
9767
elf_link_adjust_relocs (bfd *abfd,
9768
      asection *sec,
9769
      struct bfd_elf_section_reloc_data *reldata,
9770
      bool sort,
9771
      struct bfd_link_info *info)
9772
0
{
9773
0
  unsigned int i;
9774
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9775
0
  bfd_byte *erela;
9776
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9777
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9778
0
  bfd_vma r_type_mask;
9779
0
  int r_sym_shift;
9780
0
  unsigned int count = reldata->count;
9781
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9782
9783
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9784
0
    {
9785
0
      swap_in = bed->s->swap_reloc_in;
9786
0
      swap_out = bed->s->swap_reloc_out;
9787
0
    }
9788
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9789
0
    {
9790
0
      swap_in = bed->s->swap_reloca_in;
9791
0
      swap_out = bed->s->swap_reloca_out;
9792
0
    }
9793
0
  else
9794
0
    abort ();
9795
9796
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9797
0
    abort ();
9798
9799
0
  if (bed->s->arch_size == 32)
9800
0
    {
9801
0
      r_type_mask = 0xff;
9802
0
      r_sym_shift = 8;
9803
0
    }
9804
0
  else
9805
0
    {
9806
0
      r_type_mask = 0xffffffff;
9807
0
      r_sym_shift = 32;
9808
0
    }
9809
9810
0
  erela = reldata->hdr->contents;
9811
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9812
0
    {
9813
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9814
0
      unsigned int j;
9815
9816
0
      if (*rel_hash == NULL)
9817
0
  continue;
9818
9819
0
      if ((*rel_hash)->indx == -2
9820
0
    && info->gc_sections
9821
0
    && ! info->gc_keep_exported)
9822
0
  {
9823
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9824
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9825
0
            abfd, sec,
9826
0
            (*rel_hash)->root.root.string);
9827
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9828
0
            abfd, sec);
9829
0
    bfd_set_error (bfd_error_invalid_operation);
9830
0
    return false;
9831
0
  }
9832
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9833
9834
0
      (*swap_in) (abfd, erela, irela);
9835
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9836
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9837
0
         | (irela[j].r_info & r_type_mask));
9838
0
      (*swap_out) (abfd, irela, erela);
9839
0
    }
9840
9841
0
  if (bed->elf_backend_update_relocs)
9842
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9843
9844
0
  if (sort && count != 0)
9845
0
    {
9846
0
      bfd_vma (*ext_r_off) (const void *);
9847
0
      bfd_vma r_off;
9848
0
      size_t elt_size;
9849
0
      bfd_byte *base, *end, *p, *loc;
9850
0
      bfd_byte *buf = NULL;
9851
9852
0
      if (bed->s->arch_size == 32)
9853
0
  {
9854
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9855
0
      ext_r_off = ext32l_r_offset;
9856
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9857
0
      ext_r_off = ext32b_r_offset;
9858
0
    else
9859
0
      abort ();
9860
0
  }
9861
0
      else
9862
0
  {
9863
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9864
0
      ext_r_off = ext64l_r_offset;
9865
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9866
0
      ext_r_off = ext64b_r_offset;
9867
0
    else
9868
0
      abort ();
9869
0
  }
9870
9871
      /*  Must use a stable sort here.  A modified insertion sort,
9872
    since the relocs are mostly sorted already.  */
9873
0
      elt_size = reldata->hdr->sh_entsize;
9874
0
      base = reldata->hdr->contents;
9875
0
      end = base + count * elt_size;
9876
0
      if (elt_size > sizeof (Elf64_External_Rela))
9877
0
  abort ();
9878
9879
      /* Ensure the first element is lowest.  This acts as a sentinel,
9880
   speeding the main loop below.  */
9881
0
      r_off = (*ext_r_off) (base);
9882
0
      for (p = loc = base; (p += elt_size) < end; )
9883
0
  {
9884
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9885
0
    if (r_off > r_off2)
9886
0
      {
9887
0
        r_off = r_off2;
9888
0
        loc = p;
9889
0
      }
9890
0
  }
9891
0
      if (loc != base)
9892
0
  {
9893
    /* Don't just swap *base and *loc as that changes the order
9894
       of the original base[0] and base[1] if they happen to
9895
       have the same r_offset.  */
9896
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9897
0
    memcpy (onebuf, loc, elt_size);
9898
0
    memmove (base + elt_size, base, loc - base);
9899
0
    memcpy (base, onebuf, elt_size);
9900
0
  }
9901
9902
0
      for (p = base + elt_size; (p += elt_size) < end; )
9903
0
  {
9904
    /* base to p is sorted, *p is next to insert.  */
9905
0
    r_off = (*ext_r_off) (p);
9906
    /* Search the sorted region for location to insert.  */
9907
0
    loc = p - elt_size;
9908
0
    while (r_off < (*ext_r_off) (loc))
9909
0
      loc -= elt_size;
9910
0
    loc += elt_size;
9911
0
    if (loc != p)
9912
0
      {
9913
        /* Chances are there is a run of relocs to insert here,
9914
     from one of more input files.  Files are not always
9915
     linked in order due to the way elf_link_input_bfd is
9916
     called.  See pr17666.  */
9917
0
        size_t sortlen = p - loc;
9918
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9919
0
        size_t runlen = elt_size;
9920
0
        bfd_vma r_off_runend = r_off;
9921
0
        bfd_vma r_off_runend_next;
9922
0
        size_t buf_size = 96 * 1024;
9923
0
        while (p + runlen < end
9924
0
         && (sortlen <= buf_size
9925
0
       || runlen + elt_size <= buf_size)
9926
         /* run must not break the ordering of base..loc+1 */
9927
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9928
         /* run must be already sorted */
9929
0
         && r_off_runend_next >= r_off_runend)
9930
0
    {
9931
0
      runlen += elt_size;
9932
0
      r_off_runend = r_off_runend_next;
9933
0
    }
9934
0
        if (buf == NULL)
9935
0
    {
9936
0
      buf = bfd_malloc (buf_size);
9937
0
      if (buf == NULL)
9938
0
        return false;
9939
0
    }
9940
0
        if (runlen < sortlen)
9941
0
    {
9942
0
      memcpy (buf, p, runlen);
9943
0
      memmove (loc + runlen, loc, sortlen);
9944
0
      memcpy (loc, buf, runlen);
9945
0
    }
9946
0
        else
9947
0
    {
9948
0
      memcpy (buf, loc, sortlen);
9949
0
      memmove (loc, p, runlen);
9950
0
      memcpy (loc + runlen, buf, sortlen);
9951
0
    }
9952
0
        p += runlen - elt_size;
9953
0
      }
9954
0
  }
9955
      /* Hashes are no longer valid.  */
9956
0
      free (reldata->hashes);
9957
0
      reldata->hashes = NULL;
9958
0
      free (buf);
9959
0
    }
9960
0
  return true;
9961
0
}
9962
9963
struct elf_link_sort_rela
9964
{
9965
  union {
9966
    bfd_vma offset;
9967
    bfd_vma sym_mask;
9968
  } u;
9969
  enum elf_reloc_type_class type;
9970
  /* We use this as an array of size int_rels_per_ext_rel.  */
9971
  Elf_Internal_Rela rela[1];
9972
};
9973
9974
/* qsort stability here and for cmp2 is only an issue if multiple
9975
   dynamic relocations are emitted at the same address.  But targets
9976
   that apply a series of dynamic relocations each operating on the
9977
   result of the prior relocation can't use -z combreloc as
9978
   implemented anyway.  Such schemes tend to be broken by sorting on
9979
   symbol index.  That leaves dynamic NONE relocs as the only other
9980
   case where ld might emit multiple relocs at the same address, and
9981
   those are only emitted due to target bugs.  */
9982
9983
static int
9984
elf_link_sort_cmp1 (const void *A, const void *B)
9985
0
{
9986
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9987
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9988
0
  int relativea, relativeb;
9989
9990
0
  relativea = a->type == reloc_class_relative;
9991
0
  relativeb = b->type == reloc_class_relative;
9992
9993
0
  if (relativea < relativeb)
9994
0
    return 1;
9995
0
  if (relativea > relativeb)
9996
0
    return -1;
9997
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9998
0
    return -1;
9999
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
10000
0
    return 1;
10001
0
  if (a->rela->r_offset < b->rela->r_offset)
10002
0
    return -1;
10003
0
  if (a->rela->r_offset > b->rela->r_offset)
10004
0
    return 1;
10005
0
  return 0;
10006
0
}
10007
10008
static int
10009
elf_link_sort_cmp2 (const void *A, const void *B)
10010
0
{
10011
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
10012
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
10013
10014
0
  if (a->type < b->type)
10015
0
    return -1;
10016
0
  if (a->type > b->type)
10017
0
    return 1;
10018
0
  if (a->u.offset < b->u.offset)
10019
0
    return -1;
10020
0
  if (a->u.offset > b->u.offset)
10021
0
    return 1;
10022
0
  if (a->rela->r_offset < b->rela->r_offset)
10023
0
    return -1;
10024
0
  if (a->rela->r_offset > b->rela->r_offset)
10025
0
    return 1;
10026
0
  return 0;
10027
0
}
10028
10029
static size_t
10030
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
10031
0
{
10032
0
  asection *dynamic_relocs;
10033
0
  asection *rela_dyn;
10034
0
  asection *rel_dyn;
10035
0
  bfd_size_type count, size;
10036
0
  size_t i, ret, sort_elt, ext_size;
10037
0
  bfd_byte *sort, *s_non_relative, *p;
10038
0
  struct elf_link_sort_rela *sq;
10039
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
10040
0
  int i2e = bed->s->int_rels_per_ext_rel;
10041
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
10042
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10043
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10044
0
  struct bfd_link_order *lo;
10045
0
  bfd_vma r_sym_mask;
10046
0
  bool use_rela;
10047
10048
  /* Find a dynamic reloc section.  */
10049
0
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
10050
0
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
10051
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10052
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10053
0
    {
10054
0
      bool use_rela_initialised = false;
10055
10056
      /* This is just here to stop gcc from complaining.
10057
   Its initialization checking code is not perfect.  */
10058
0
      use_rela = true;
10059
10060
      /* Both sections are present.  Examine the sizes
10061
   of the indirect sections to help us choose.  */
10062
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10063
0
  if (lo->type == bfd_indirect_link_order)
10064
0
    {
10065
0
      asection *o = lo->u.indirect.section;
10066
10067
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10068
0
        {
10069
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10070
      /* Section size is divisible by both rel and rela sizes.
10071
         It is of no help to us.  */
10072
0
      ;
10073
0
    else
10074
0
      {
10075
        /* Section size is only divisible by rela.  */
10076
0
        if (use_rela_initialised && !use_rela)
10077
0
          {
10078
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10079
0
                "they are in more than one size"),
10080
0
              abfd);
10081
0
      bfd_set_error (bfd_error_invalid_operation);
10082
0
      return 0;
10083
0
          }
10084
0
        else
10085
0
          {
10086
0
      use_rela = true;
10087
0
      use_rela_initialised = true;
10088
0
          }
10089
0
      }
10090
0
        }
10091
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10092
0
        {
10093
    /* Section size is only divisible by rel.  */
10094
0
    if (use_rela_initialised && use_rela)
10095
0
      {
10096
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10097
0
            "they are in more than one size"),
10098
0
          abfd);
10099
0
        bfd_set_error (bfd_error_invalid_operation);
10100
0
        return 0;
10101
0
      }
10102
0
    else
10103
0
      {
10104
0
        use_rela = false;
10105
0
        use_rela_initialised = true;
10106
0
      }
10107
0
        }
10108
0
      else
10109
0
        {
10110
    /* The section size is not divisible by either -
10111
       something is wrong.  */
10112
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10113
0
              "they are of an unknown size"), abfd);
10114
0
    bfd_set_error (bfd_error_invalid_operation);
10115
0
    return 0;
10116
0
        }
10117
0
    }
10118
10119
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10120
0
  if (lo->type == bfd_indirect_link_order)
10121
0
    {
10122
0
      asection *o = lo->u.indirect.section;
10123
10124
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10125
0
        {
10126
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10127
      /* Section size is divisible by both rel and rela sizes.
10128
         It is of no help to us.  */
10129
0
      ;
10130
0
    else
10131
0
      {
10132
        /* Section size is only divisible by rela.  */
10133
0
        if (use_rela_initialised && !use_rela)
10134
0
          {
10135
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10136
0
                "they are in more than one size"),
10137
0
              abfd);
10138
0
      bfd_set_error (bfd_error_invalid_operation);
10139
0
      return 0;
10140
0
          }
10141
0
        else
10142
0
          {
10143
0
      use_rela = true;
10144
0
      use_rela_initialised = true;
10145
0
          }
10146
0
      }
10147
0
        }
10148
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10149
0
        {
10150
    /* Section size is only divisible by rel.  */
10151
0
    if (use_rela_initialised && use_rela)
10152
0
      {
10153
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10154
0
            "they are in more than one size"),
10155
0
          abfd);
10156
0
        bfd_set_error (bfd_error_invalid_operation);
10157
0
        return 0;
10158
0
      }
10159
0
    else
10160
0
      {
10161
0
        use_rela = false;
10162
0
        use_rela_initialised = true;
10163
0
      }
10164
0
        }
10165
0
      else
10166
0
        {
10167
    /* The section size is not divisible by either -
10168
       something is wrong.  */
10169
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10170
0
              "they are of an unknown size"), abfd);
10171
0
    bfd_set_error (bfd_error_invalid_operation);
10172
0
    return 0;
10173
0
        }
10174
0
    }
10175
10176
0
      if (! use_rela_initialised)
10177
  /* Make a guess.  */
10178
0
  use_rela = true;
10179
0
    }
10180
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10181
0
    use_rela = true;
10182
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10183
0
    use_rela = false;
10184
0
  else
10185
0
    return 0;
10186
10187
0
  if (use_rela)
10188
0
    {
10189
0
      dynamic_relocs = rela_dyn;
10190
0
      ext_size = bed->s->sizeof_rela;
10191
0
      swap_in = bed->s->swap_reloca_in;
10192
0
      swap_out = bed->s->swap_reloca_out;
10193
0
    }
10194
0
  else
10195
0
    {
10196
0
      dynamic_relocs = rel_dyn;
10197
0
      ext_size = bed->s->sizeof_rel;
10198
0
      swap_in = bed->s->swap_reloc_in;
10199
0
      swap_out = bed->s->swap_reloc_out;
10200
0
    }
10201
10202
0
  size = 0;
10203
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10204
0
    if (lo->type == bfd_indirect_link_order)
10205
0
      size += lo->u.indirect.section->size;
10206
10207
0
  if (size != dynamic_relocs->size)
10208
0
    return 0;
10209
10210
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10211
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10212
10213
0
  count = dynamic_relocs->size / ext_size;
10214
0
  if (count == 0)
10215
0
    return 0;
10216
0
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10217
10218
0
  if (sort == NULL)
10219
0
    {
10220
0
      (*info->callbacks->warning)
10221
0
  (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10222
0
      return 0;
10223
0
    }
10224
10225
0
  if (bed->s->arch_size == 32)
10226
0
    r_sym_mask = ~(bfd_vma) 0xff;
10227
0
  else
10228
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10229
10230
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10231
0
    if (lo->type == bfd_indirect_link_order)
10232
0
      {
10233
0
  bfd_byte *erel, *erelend;
10234
0
  asection *o = lo->u.indirect.section;
10235
10236
0
  if (o->contents == NULL && o->size != 0)
10237
0
    {
10238
      /* This is a reloc section that is being handled as a normal
10239
         section.  See bfd_section_from_shdr.  We can't combine
10240
         relocs in this case.  */
10241
0
      free (sort);
10242
0
      return 0;
10243
0
    }
10244
0
  erel = o->contents;
10245
0
  erelend = o->contents + o->size;
10246
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10247
10248
0
  while (erel < erelend)
10249
0
    {
10250
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10251
10252
0
      (*swap_in) (abfd, erel, s->rela);
10253
0
      s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10254
0
      s->u.sym_mask = r_sym_mask;
10255
0
      p += sort_elt;
10256
0
      erel += ext_size;
10257
0
    }
10258
0
      }
10259
10260
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10261
10262
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10263
0
    {
10264
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10265
0
      if (s->type != reloc_class_relative)
10266
0
  break;
10267
0
    }
10268
0
  ret = i;
10269
0
  s_non_relative = p;
10270
10271
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10272
0
  for (; i < count; i++, p += sort_elt)
10273
0
    {
10274
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10275
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10276
0
  sq = sp;
10277
0
      sp->u.offset = sq->rela->r_offset;
10278
0
    }
10279
10280
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10281
10282
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10283
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10284
0
    {
10285
      /* We have plt relocs in .rela.dyn.  */
10286
0
      sq = (struct elf_link_sort_rela *) sort;
10287
0
      for (i = 0; i < count; i++)
10288
0
  if (sq[count - i - 1].type != reloc_class_plt)
10289
0
    break;
10290
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10291
0
  {
10292
0
    struct bfd_link_order **plo;
10293
    /* Put srelplt link_order last.  This is so the output_offset
10294
       set in the next loop is correct for DT_JMPREL.  */
10295
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10296
0
      if ((*plo)->type == bfd_indirect_link_order
10297
0
    && (*plo)->u.indirect.section == htab->srelplt)
10298
0
        {
10299
0
    lo = *plo;
10300
0
    *plo = lo->next;
10301
0
        }
10302
0
      else
10303
0
        plo = &(*plo)->next;
10304
0
    *plo = lo;
10305
0
    lo->next = NULL;
10306
0
    dynamic_relocs->map_tail.link_order = lo;
10307
0
  }
10308
0
    }
10309
10310
0
  p = sort;
10311
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10312
0
    if (lo->type == bfd_indirect_link_order)
10313
0
      {
10314
0
  bfd_byte *erel, *erelend;
10315
0
  asection *o = lo->u.indirect.section;
10316
10317
0
  erel = o->contents;
10318
0
  erelend = o->contents + o->size;
10319
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10320
0
  while (erel < erelend)
10321
0
    {
10322
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10323
0
      (*swap_out) (abfd, s->rela, erel);
10324
0
      p += sort_elt;
10325
0
      erel += ext_size;
10326
0
    }
10327
0
      }
10328
10329
0
  free (sort);
10330
0
  *psec = dynamic_relocs;
10331
0
  return ret;
10332
0
}
10333
10334
/* Add a symbol to the output symbol string table.  */
10335
10336
static int
10337
elf_link_output_symstrtab (void *finf,
10338
         const char *name,
10339
         Elf_Internal_Sym *elfsym,
10340
         asection *input_sec,
10341
         struct elf_link_hash_entry *h)
10342
0
{
10343
0
  struct elf_final_link_info *flinfo = finf;
10344
0
  int (*output_symbol_hook)
10345
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10346
0
     struct elf_link_hash_entry *);
10347
0
  struct elf_link_hash_table *hash_table;
10348
0
  elf_backend_data *bed;
10349
0
  bfd_size_type strtabsize;
10350
10351
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10352
10353
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10354
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10355
0
  if (output_symbol_hook != NULL)
10356
0
    {
10357
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10358
0
      if (ret != 1)
10359
0
  return ret;
10360
0
    }
10361
10362
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10363
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10364
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10365
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10366
10367
0
  if (name == NULL || *name == '\0')
10368
0
    elfsym->st_name = (unsigned long) -1;
10369
0
  else
10370
0
    {
10371
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10372
   to get the final offset for st_name.  */
10373
0
      char *versioned_name = (char *) name;
10374
0
      if (h != NULL)
10375
0
  {
10376
0
    if (h->versioned == versioned && h->def_dynamic)
10377
0
      {
10378
        /* Keep only one '@' for versioned symbols defined in
10379
           shared objects.  */
10380
0
        const char *version = strrchr (name, ELF_VER_CHR);
10381
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10382
0
        if (version != base_end)
10383
0
    {
10384
0
      size_t base_len;
10385
0
      size_t len = strlen (name);
10386
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10387
0
      if (versioned_name == NULL)
10388
0
        return 0;
10389
0
      base_len = base_end - name;
10390
0
      memcpy (versioned_name, name, base_len);
10391
0
      memcpy (versioned_name + base_len, version,
10392
0
        len - base_len);
10393
0
    }
10394
0
      }
10395
0
  }
10396
0
      else if (flinfo->info->unique_symbol
10397
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10398
0
  {
10399
0
    struct local_hash_entry *lh;
10400
0
    size_t count_len;
10401
0
    size_t base_len;
10402
0
    char buf[30];
10403
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10404
0
      {
10405
0
      case STT_FILE:
10406
0
      case STT_SECTION:
10407
0
        break;
10408
0
      default:
10409
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10410
0
         (&flinfo->local_hash_table, name, true, false);
10411
0
        if (lh == NULL)
10412
0
    return 0;
10413
        /* Always append ".COUNT" to local symbols to avoid
10414
     potential conflicts with local symbol "XXX.COUNT".  */
10415
0
        sprintf (buf, "%lx", lh->count);
10416
0
        base_len = lh->size;
10417
0
        if (!base_len)
10418
0
    {
10419
0
      base_len = strlen (name);
10420
0
      lh->size = base_len;
10421
0
    }
10422
0
        count_len = strlen (buf);
10423
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10424
0
            base_len + count_len + 2);
10425
0
        if (versioned_name == NULL)
10426
0
    return 0;
10427
0
        memcpy (versioned_name, name, base_len);
10428
0
        versioned_name[base_len] = '.';
10429
0
        memcpy (versioned_name + base_len + 1, buf,
10430
0
          count_len + 1);
10431
0
        lh->count++;
10432
0
        break;
10433
0
      }
10434
0
  }
10435
0
      elfsym->st_name
10436
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10437
0
                 versioned_name, false);
10438
0
      if (elfsym->st_name == (unsigned long) -1)
10439
0
  return 0;
10440
0
    }
10441
10442
0
  hash_table = elf_hash_table (flinfo->info);
10443
0
  strtabsize = hash_table->strtabsize;
10444
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10445
0
    {
10446
0
      strtabsize += strtabsize;
10447
0
      hash_table->strtabsize = strtabsize;
10448
0
      strtabsize *= sizeof (*hash_table->strtab);
10449
0
      hash_table->strtab
10450
0
  = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10451
0
             strtabsize);
10452
0
      if (hash_table->strtab == NULL)
10453
0
  return 0;
10454
0
    }
10455
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10456
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10457
0
    = flinfo->output_bfd->symcount;
10458
0
  flinfo->output_bfd->symcount += 1;
10459
10460
0
  return 1;
10461
0
}
10462
10463
/* Swap symbols out to the symbol table and flush the output symbols to
10464
   the file.  */
10465
10466
static bool
10467
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10468
0
{
10469
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10470
0
  size_t amt;
10471
0
  size_t i;
10472
0
  elf_backend_data *bed;
10473
0
  bfd_byte *symbuf;
10474
0
  Elf_Internal_Shdr *hdr;
10475
0
  file_ptr pos;
10476
0
  bool ret;
10477
10478
0
  if (flinfo->output_bfd->symcount == 0)
10479
0
    return true;
10480
10481
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10482
10483
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10484
10485
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10486
0
  symbuf = (bfd_byte *) bfd_malloc (amt);
10487
0
  if (symbuf == NULL)
10488
0
    return false;
10489
10490
0
  if (flinfo->symshndxbuf)
10491
0
    {
10492
0
      amt = sizeof (Elf_External_Sym_Shndx);
10493
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10494
0
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10495
0
      if (flinfo->symshndxbuf == NULL)
10496
0
  {
10497
0
    free (symbuf);
10498
0
    return false;
10499
0
  }
10500
0
    }
10501
10502
  /* Now swap out the symbols.  */
10503
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10504
0
    {
10505
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10506
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10507
0
  elfsym->sym.st_name = 0;
10508
0
      else
10509
0
  elfsym->sym.st_name
10510
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10511
0
                elfsym->sym.st_name);
10512
10513
      /* Inform the linker of the addition of this symbol.  */
10514
10515
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10516
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10517
0
             &elfsym->sym);
10518
10519
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10520
0
             ((bfd_byte *) symbuf
10521
0
        + (elfsym->dest_index
10522
0
           * bed->s->sizeof_sym)),
10523
0
             NPTR_ADD (flinfo->symshndxbuf,
10524
0
           elfsym->dest_index));
10525
0
    }
10526
10527
0
  hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10528
0
  pos = hdr->sh_offset + hdr->sh_size;
10529
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10530
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10531
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10532
0
    {
10533
0
      hdr->sh_size += amt;
10534
0
      ret = true;
10535
0
    }
10536
0
  else
10537
0
    ret = false;
10538
10539
0
  free (symbuf);
10540
0
  return ret;
10541
0
}
10542
10543
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
10544
10545
static bool
10546
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10547
0
{
10548
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10549
0
      && sym->st_shndx < SHN_LORESERVE)
10550
0
    {
10551
      /* The gABI doesn't support dynamic symbols in output sections
10552
   beyond 64k.  */
10553
0
      _bfd_error_handler
10554
  /* xgettext:c-format */
10555
0
  (_("%pB: too many sections: %d (>= %d)"),
10556
0
   abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10557
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10558
0
      return false;
10559
0
    }
10560
0
  return true;
10561
0
}
10562
10563
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10564
   allowing an unsatisfied unversioned symbol in the DSO to match a
10565
   versioned symbol that would normally require an explicit version.
10566
   We also handle the case that a DSO references a hidden symbol
10567
   which may be satisfied by a versioned symbol in another DSO.  */
10568
10569
static bool
10570
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10571
         elf_backend_data *bed,
10572
         struct elf_link_hash_entry *h)
10573
0
{
10574
0
  bfd *abfd;
10575
0
  struct elf_link_loaded_list *loaded;
10576
10577
0
  if (!is_elf_hash_table (info->hash))
10578
0
    return false;
10579
10580
  /* Check indirect symbol.  */
10581
0
  while (h->root.type == bfd_link_hash_indirect)
10582
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10583
10584
0
  switch (h->root.type)
10585
0
    {
10586
0
    default:
10587
0
      abfd = NULL;
10588
0
      break;
10589
10590
0
    case bfd_link_hash_undefined:
10591
0
    case bfd_link_hash_undefweak:
10592
0
      abfd = h->root.u.undef.abfd;
10593
0
      if (abfd == NULL
10594
0
    || (abfd->flags & DYNAMIC) == 0
10595
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10596
0
  return false;
10597
0
      break;
10598
10599
0
    case bfd_link_hash_defined:
10600
0
    case bfd_link_hash_defweak:
10601
0
      abfd = h->root.u.def.section->owner;
10602
0
      break;
10603
10604
0
    case bfd_link_hash_common:
10605
0
      abfd = h->root.u.c.p->section->owner;
10606
0
      break;
10607
0
    }
10608
0
  BFD_ASSERT (abfd != NULL);
10609
10610
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10611
0
       loaded != NULL;
10612
0
       loaded = loaded->next)
10613
0
    {
10614
0
      bfd *input;
10615
0
      Elf_Internal_Shdr *hdr;
10616
0
      size_t symcount;
10617
0
      size_t extsymcount;
10618
0
      size_t extsymoff;
10619
0
      Elf_Internal_Shdr *versymhdr;
10620
0
      Elf_Internal_Sym *isym;
10621
0
      Elf_Internal_Sym *isymend;
10622
0
      Elf_Internal_Sym *isymbuf;
10623
0
      Elf_External_Versym *ever;
10624
0
      Elf_External_Versym *extversym;
10625
10626
0
      input = loaded->abfd;
10627
10628
      /* We check each DSO for a possible hidden versioned definition.  */
10629
0
      if (input == abfd
10630
0
    || elf_dynversym (input) == 0)
10631
0
  continue;
10632
10633
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10634
10635
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10636
0
      if (elf_bad_symtab (input))
10637
0
  {
10638
0
    extsymcount = symcount;
10639
0
    extsymoff = 0;
10640
0
  }
10641
0
      else
10642
0
  {
10643
0
    extsymcount = symcount - hdr->sh_info;
10644
0
    extsymoff = hdr->sh_info;
10645
0
  }
10646
10647
0
      if (extsymcount == 0)
10648
0
  continue;
10649
10650
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10651
0
              NULL, NULL, NULL);
10652
0
      if (isymbuf == NULL)
10653
0
  return false;
10654
10655
      /* Read in any version definitions.  */
10656
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10657
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10658
0
    || (extversym = (Elf_External_Versym *)
10659
0
        _bfd_malloc_and_read (input, versymhdr->sh_size,
10660
0
            versymhdr->sh_size)) == NULL)
10661
0
  {
10662
0
    free (isymbuf);
10663
0
    return false;
10664
0
  }
10665
10666
0
      ever = extversym + extsymoff;
10667
0
      isymend = isymbuf + extsymcount;
10668
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10669
0
  {
10670
0
    const char *name;
10671
0
    Elf_Internal_Versym iver;
10672
0
    unsigned short version_index;
10673
10674
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10675
0
        || isym->st_shndx == SHN_UNDEF)
10676
0
      continue;
10677
10678
0
    name = bfd_elf_string_from_elf_section (input,
10679
0
              hdr->sh_link,
10680
0
              isym->st_name);
10681
0
    if (strcmp (name, h->root.root.string) != 0)
10682
0
      continue;
10683
10684
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10685
10686
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10687
0
        && !(h->def_regular
10688
0
       && h->forced_local))
10689
0
      {
10690
        /* If we have a non-hidden versioned sym, then it should
10691
     have provided a definition for the undefined sym unless
10692
     it is defined in a non-shared object and forced local.
10693
         */
10694
0
        abort ();
10695
0
      }
10696
10697
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10698
0
    if (version_index == 1 || version_index == 2)
10699
0
      {
10700
        /* This is the base or first version.  We can use it.  */
10701
0
        free (extversym);
10702
0
        free (isymbuf);
10703
0
        return true;
10704
0
      }
10705
0
  }
10706
10707
0
      free (extversym);
10708
0
      free (isymbuf);
10709
0
    }
10710
10711
0
  return false;
10712
0
}
10713
10714
/* Convert ELF common symbol TYPE.  */
10715
10716
static int
10717
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10718
0
{
10719
  /* Commom symbol can only appear in relocatable link.  */
10720
0
  if (!bfd_link_relocatable (info))
10721
0
    abort ();
10722
0
  switch (info->elf_stt_common)
10723
0
    {
10724
0
    case unchanged:
10725
0
      break;
10726
0
    case elf_stt_common:
10727
0
      type = STT_COMMON;
10728
0
      break;
10729
0
    case no_elf_stt_common:
10730
0
      type = STT_OBJECT;
10731
0
      break;
10732
0
    }
10733
0
  return type;
10734
0
}
10735
10736
/* Add an external symbol to the symbol table.  This is called from
10737
   the hash table traversal routine.  When generating a shared object,
10738
   we go through the symbol table twice.  The first time we output
10739
   anything that might have been forced to local scope in a version
10740
   script.  The second time we output the symbols that are still
10741
   global symbols.  */
10742
10743
static bool
10744
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10745
0
{
10746
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10747
0
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10748
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10749
0
  bool strip;
10750
0
  Elf_Internal_Sym sym;
10751
0
  asection *input_sec;
10752
0
  elf_backend_data *bed;
10753
0
  long indx;
10754
0
  int ret;
10755
0
  unsigned int type;
10756
10757
  /* Skip if base symbol doesn't match.  */
10758
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10759
0
    return true;
10760
10761
0
  if (h->root.type == bfd_link_hash_warning)
10762
0
    {
10763
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10764
0
      if (h->root.type == bfd_link_hash_new)
10765
0
  return true;
10766
0
    }
10767
10768
  /* Decide whether to output this symbol in this pass.  */
10769
0
  if (eoinfo->localsyms)
10770
0
    {
10771
0
      if (!h->forced_local)
10772
0
  return true;
10773
0
    }
10774
0
  else
10775
0
    {
10776
0
      if (h->forced_local)
10777
0
  return true;
10778
0
    }
10779
10780
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10781
10782
0
  if (h->root.type == bfd_link_hash_undefined)
10783
0
    {
10784
      /* If we have an undefined symbol reference here then it must have
10785
   come from a shared library that is being linked in.  (Undefined
10786
   references in regular files have already been handled unless
10787
   they are in unreferenced sections which are removed by garbage
10788
   collection).  */
10789
0
      bool ignore_undef = false;
10790
10791
      /* Some symbols may be special in that the fact that they're
10792
   undefined can be safely ignored - let backend determine that.  */
10793
0
      if (bed->elf_backend_ignore_undef_symbol)
10794
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10795
10796
      /* If we are reporting errors for this situation then do so now.  */
10797
0
      if (!ignore_undef
10798
0
    && h->ref_dynamic_nonweak
10799
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10800
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10801
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10802
0
  {
10803
0
    flinfo->info->callbacks->undefined_symbol
10804
0
      (flinfo->info, h->root.root.string,
10805
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10806
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10807
0
       && !flinfo->info->warn_unresolved_syms);
10808
0
  }
10809
10810
      /* Strip a global symbol defined in a discarded section.  */
10811
0
      if (h->indx == -3)
10812
0
  return true;
10813
0
    }
10814
10815
  /* We should also warn if a forced local symbol is referenced from
10816
     shared libraries.  */
10817
0
  if (bfd_link_executable (flinfo->info)
10818
0
      && h->forced_local
10819
0
      && h->ref_dynamic
10820
0
      && h->def_regular
10821
0
      && !h->dynamic_def
10822
0
      && h->ref_dynamic_nonweak
10823
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10824
0
    {
10825
0
      bfd *def_bfd;
10826
0
      const char *msg;
10827
0
      struct elf_link_hash_entry *hi = h;
10828
10829
      /* Check indirect symbol.  */
10830
0
      while (hi->root.type == bfd_link_hash_indirect)
10831
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10832
10833
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10834
  /* xgettext:c-format */
10835
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10836
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10837
  /* xgettext:c-format */
10838
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10839
0
      else
10840
  /* xgettext:c-format */
10841
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10842
0
      def_bfd = flinfo->output_bfd;
10843
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10844
0
  def_bfd = hi->root.u.def.section->owner;
10845
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10846
0
        h->root.root.string, def_bfd);
10847
0
      bfd_set_error (bfd_error_bad_value);
10848
0
      eoinfo->failed = true;
10849
0
      return false;
10850
0
    }
10851
10852
  /* We don't want to output symbols that have never been mentioned by
10853
     a regular file, or that we have been told to strip.  However, if
10854
     h->indx is set to -2, the symbol is used by a reloc and we must
10855
     output it.  */
10856
0
  strip = false;
10857
0
  if (h->indx == -2)
10858
0
    ;
10859
0
  else if ((h->def_dynamic
10860
0
      || h->ref_dynamic
10861
0
      || h->root.type == bfd_link_hash_new)
10862
0
     && !h->def_regular
10863
0
     && !h->ref_regular)
10864
0
    strip = true;
10865
0
  else if (flinfo->info->strip == strip_all)
10866
0
    strip = true;
10867
0
  else if (flinfo->info->strip == strip_some
10868
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10869
0
             h->root.root.string, false, false) == NULL)
10870
0
    strip = true;
10871
0
  else if ((h->root.type == bfd_link_hash_defined
10872
0
      || h->root.type == bfd_link_hash_defweak)
10873
0
     && ((flinfo->info->strip_discarded
10874
0
    && discarded_section (h->root.u.def.section))
10875
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10876
0
       && h->root.u.def.section->owner != NULL
10877
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10878
0
    strip = true;
10879
0
  else if ((h->root.type == bfd_link_hash_undefined
10880
0
      || h->root.type == bfd_link_hash_undefweak)
10881
0
     && h->root.u.undef.abfd != NULL
10882
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10883
0
    strip = true;
10884
10885
  /* Remember if this symbol should be stripped.  */
10886
0
  bool should_strip = strip;
10887
10888
  /* Strip undefined weak symbols link if they don't have relocation.  */
10889
0
  if (!strip)
10890
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10891
10892
0
  type = h->type;
10893
10894
  /* If we're stripping it, and it's not a dynamic symbol, there's
10895
     nothing else to do.   However, if it is a forced local symbol or
10896
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10897
     function a chance to make it dynamic.  */
10898
0
  if (strip
10899
0
      && h->dynindx == -1
10900
0
      && type != STT_GNU_IFUNC
10901
0
      && !h->forced_local)
10902
0
    return true;
10903
10904
0
  sym.st_value = 0;
10905
0
  sym.st_size = h->size;
10906
0
  sym.st_other = h->other;
10907
0
  switch (h->root.type)
10908
0
    {
10909
0
    default:
10910
0
    case bfd_link_hash_new:
10911
0
    case bfd_link_hash_warning:
10912
0
      abort ();
10913
0
      return false;
10914
10915
0
    case bfd_link_hash_undefined:
10916
0
    case bfd_link_hash_undefweak:
10917
0
      input_sec = bfd_und_section_ptr;
10918
0
      sym.st_shndx = SHN_UNDEF;
10919
0
      break;
10920
10921
0
    case bfd_link_hash_defined:
10922
0
    case bfd_link_hash_defweak:
10923
0
      {
10924
0
  input_sec = h->root.u.def.section;
10925
0
  if (input_sec->output_section != NULL)
10926
0
    {
10927
0
      sym.st_shndx =
10928
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10929
0
             input_sec->output_section);
10930
0
      if (sym.st_shndx == SHN_BAD)
10931
0
        {
10932
0
    _bfd_error_handler
10933
      /* xgettext:c-format */
10934
0
      (_("%pB: could not find output section %pA for input section %pA"),
10935
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10936
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10937
0
    eoinfo->failed = true;
10938
0
    return false;
10939
0
        }
10940
10941
      /* ELF symbols in relocatable files are section relative,
10942
         but in nonrelocatable files they are virtual
10943
         addresses.  */
10944
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10945
0
      if (!bfd_link_relocatable (flinfo->info))
10946
0
        {
10947
0
    sym.st_value += input_sec->output_section->vma;
10948
0
    if (h->type == STT_TLS)
10949
0
      {
10950
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10951
0
        if (tls_sec != NULL)
10952
0
          sym.st_value -= tls_sec->vma;
10953
0
      }
10954
0
        }
10955
0
    }
10956
0
  else
10957
0
    {
10958
0
      BFD_ASSERT (input_sec->owner == NULL
10959
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10960
0
      sym.st_shndx = SHN_UNDEF;
10961
0
      input_sec = bfd_und_section_ptr;
10962
0
    }
10963
0
      }
10964
0
      break;
10965
10966
0
    case bfd_link_hash_common:
10967
0
      input_sec = h->root.u.c.p->section;
10968
0
      sym.st_shndx = bed->common_section_index (input_sec);
10969
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10970
0
      break;
10971
10972
0
    case bfd_link_hash_indirect:
10973
      /* These symbols are created by symbol versioning.  They point
10974
   to the decorated version of the name.  For example, if the
10975
   symbol foo@@GNU_1.2 is the default, which should be used when
10976
   foo is used with no version, then we add an indirect symbol
10977
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10978
   since the indirected symbol is already in the hash table.  */
10979
0
      return true;
10980
0
    }
10981
10982
0
  if (type == STT_COMMON || type == STT_OBJECT)
10983
0
    switch (h->root.type)
10984
0
      {
10985
0
      case bfd_link_hash_common:
10986
0
  type = elf_link_convert_common_type (flinfo->info, type);
10987
0
  break;
10988
0
      case bfd_link_hash_defined:
10989
0
      case bfd_link_hash_defweak:
10990
0
  if (bed->common_definition (&sym))
10991
0
    type = elf_link_convert_common_type (flinfo->info, type);
10992
0
  else
10993
0
    type = STT_OBJECT;
10994
0
  break;
10995
0
      case bfd_link_hash_undefined:
10996
0
      case bfd_link_hash_undefweak:
10997
0
  break;
10998
0
      default:
10999
0
  abort ();
11000
0
      }
11001
11002
0
  if (h->forced_local)
11003
0
    {
11004
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
11005
      /* Turn off visibility on local symbol.  */
11006
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11007
0
    }
11008
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
11009
0
  else if (h->unique_global && h->def_regular)
11010
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
11011
0
  else if (h->root.type == bfd_link_hash_undefweak
11012
0
     || h->root.type == bfd_link_hash_defweak)
11013
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
11014
0
  else
11015
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
11016
0
  sym.st_target_internal = h->target_internal;
11017
11018
  /* Give the processor backend a chance to tweak the symbol value,
11019
     and also to finish up anything that needs to be done for this
11020
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
11021
     forced local syms when non-shared is due to a historical quirk.
11022
     STT_GNU_IFUNC symbol must go through PLT.  */
11023
0
  if (((h->type == STT_GNU_IFUNC
11024
0
  && h->def_regular
11025
0
  && !bfd_link_relocatable (flinfo->info))
11026
0
       || ((h->dynindx != -1
11027
0
      || h->forced_local)
11028
0
     && ((bfd_link_pic (flinfo->info)
11029
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11030
0
        || h->root.type != bfd_link_hash_undefweak))
11031
0
         || !h->forced_local)
11032
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11033
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11034
0
    {
11035
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->output_bfd,
11036
0
               flinfo->info, h, &sym))
11037
0
  {
11038
0
    eoinfo->failed = true;
11039
0
    return false;
11040
0
  }
11041
      /* If a symbol is in the dynamic symbol table and isn't a
11042
   should-strip symbol, also keep it in the symbol table.  */
11043
0
      if (!should_strip)
11044
0
  strip = false;
11045
0
    }
11046
11047
  /* If we are marking the symbol as undefined, and there are no
11048
     non-weak references to this symbol from a regular object, then
11049
     mark the symbol as weak undefined; if there are non-weak
11050
     references, mark the symbol as strong.  We can't do this earlier,
11051
     because it might not be marked as undefined until the
11052
     finish_dynamic_symbol routine gets through with it.  */
11053
0
  if (sym.st_shndx == SHN_UNDEF
11054
0
      && h->ref_regular
11055
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11056
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11057
0
    {
11058
0
      int bindtype;
11059
0
      type = ELF_ST_TYPE (sym.st_info);
11060
11061
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11062
0
      if (type == STT_GNU_IFUNC)
11063
0
  type = STT_FUNC;
11064
11065
0
      if (h->ref_regular_nonweak)
11066
0
  bindtype = STB_GLOBAL;
11067
0
      else
11068
0
  bindtype = STB_WEAK;
11069
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11070
0
    }
11071
11072
  /* If this is a symbol defined in a dynamic library, don't use the
11073
     symbol size from the dynamic library.  Relinking an executable
11074
     against a new library may introduce gratuitous changes in the
11075
     executable's symbols if we keep the size.  */
11076
0
  if (sym.st_shndx == SHN_UNDEF
11077
0
      && !h->def_regular
11078
0
      && h->def_dynamic)
11079
0
    sym.st_size = 0;
11080
11081
  /* If a non-weak symbol with non-default visibility is not defined
11082
     locally, it is a fatal error.  */
11083
0
  if (!bfd_link_relocatable (flinfo->info)
11084
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11085
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11086
0
      && h->root.type == bfd_link_hash_undefined
11087
0
      && !h->def_regular)
11088
0
    {
11089
0
      const char *msg;
11090
11091
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11092
  /* xgettext:c-format */
11093
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11094
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11095
  /* xgettext:c-format */
11096
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11097
0
      else
11098
  /* xgettext:c-format */
11099
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11100
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11101
0
      bfd_set_error (bfd_error_bad_value);
11102
0
      eoinfo->failed = true;
11103
0
      return false;
11104
0
    }
11105
11106
  /* If this symbol should be put in the .dynsym section, then put it
11107
     there now.  We already know the symbol index.  We also fill in
11108
     the entry in the .hash section.  */
11109
0
  if (h->dynindx != -1
11110
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11111
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11112
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11113
0
    {
11114
0
      bfd_byte *esym;
11115
11116
      /* Since there is no version information in the dynamic string,
11117
   if there is no version info in symbol version section, we will
11118
   have a run-time problem if not linking executable, referenced
11119
   by shared library, or not bound locally.  */
11120
0
      if (h->verinfo.verdef == NULL
11121
0
    && (!bfd_link_executable (flinfo->info)
11122
0
        || h->ref_dynamic
11123
0
        || !h->def_regular))
11124
0
  {
11125
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11126
11127
0
    if (p && p [1] != '\0')
11128
0
      {
11129
0
        _bfd_error_handler
11130
    /* xgettext:c-format */
11131
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11132
0
     flinfo->output_bfd, h->root.root.string);
11133
0
        eoinfo->failed = true;
11134
0
        return false;
11135
0
      }
11136
0
  }
11137
11138
0
      sym.st_name = h->dynstr_index;
11139
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11140
0
        + h->dynindx * bed->s->sizeof_sym);
11141
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11142
0
  {
11143
0
    eoinfo->failed = true;
11144
0
    return false;
11145
0
  }
11146
11147
      /* Inform the linker of the addition of this symbol.  */
11148
11149
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11150
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11151
11152
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11153
11154
0
      if (flinfo->hash_sec != NULL)
11155
0
  {
11156
0
    size_t hash_entry_size;
11157
0
    bfd_byte *bucketpos;
11158
0
    bfd_vma chain;
11159
0
    size_t bucketcount;
11160
0
    size_t bucket;
11161
11162
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11163
0
    bucket = h->u.elf_hash_value % bucketcount;
11164
11165
0
    hash_entry_size
11166
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11167
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11168
0
           + (bucket + 2) * hash_entry_size);
11169
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11170
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11171
0
       bucketpos);
11172
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11173
0
       ((bfd_byte *) flinfo->hash_sec->contents
11174
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11175
0
  }
11176
11177
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11178
0
  {
11179
0
    Elf_Internal_Versym iversym;
11180
0
    Elf_External_Versym *eversym;
11181
11182
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11183
0
      {
11184
0
        if (h->verinfo.verdef == NULL
11185
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11186
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11187
0
    {
11188
0
      iversym.vs_vers = 1;
11189
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11190
        /* Referenced symbol without ELF_VER_CHR has no
11191
           version.  */
11192
0
        iversym.vs_vers = 0;
11193
0
    }
11194
0
        else
11195
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11196
0
      }
11197
0
    else
11198
0
      {
11199
0
        if (h->verinfo.vertree == NULL)
11200
0
    iversym.vs_vers = 1;
11201
0
        else
11202
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11203
0
        if (flinfo->info->create_default_symver)
11204
0
    iversym.vs_vers++;
11205
11206
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11207
     symbol is defined locally.  */
11208
0
        if (h->versioned == versioned_hidden && h->def_regular)
11209
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11210
0
      }
11211
11212
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11213
0
    eversym += h->dynindx;
11214
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11215
0
  }
11216
0
    }
11217
11218
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11219
     strip it from .symtab too.  Obviously we can't do this for
11220
     relocatable output or when needed for --emit-relocs.  */
11221
0
  else if (input_sec == bfd_und_section_ptr
11222
0
     && h->indx != -2
11223
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11224
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11225
0
     && !bfd_link_relocatable (flinfo->info))
11226
0
    return true;
11227
11228
  /* Also strip others that we couldn't earlier due to dynamic symbol
11229
     processing.  */
11230
0
  if (strip)
11231
0
    return true;
11232
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11233
0
    return true;
11234
11235
  /* Output a FILE symbol so that following locals are not associated
11236
     with the wrong input file.  We need one for forced local symbols
11237
     if we've seen more than one FILE symbol or when we have exactly
11238
     one FILE symbol but global symbols are present in a file other
11239
     than the one with the FILE symbol.  We also need one if linker
11240
     defined symbols are present.  In practice these conditions are
11241
     always met, so just emit the FILE symbol unconditionally.  */
11242
0
  if (eoinfo->localsyms
11243
0
      && !eoinfo->file_sym_done
11244
0
      && eoinfo->flinfo->filesym_count != 0)
11245
0
    {
11246
0
      Elf_Internal_Sym fsym;
11247
11248
0
      memset (&fsym, 0, sizeof (fsym));
11249
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11250
0
      fsym.st_shndx = SHN_ABS;
11251
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11252
0
              bfd_und_section_ptr, NULL))
11253
0
  return false;
11254
11255
0
      eoinfo->file_sym_done = true;
11256
0
    }
11257
11258
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11259
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11260
0
           input_sec, h);
11261
0
  if (ret == 0)
11262
0
    {
11263
0
      eoinfo->failed = true;
11264
0
      return false;
11265
0
    }
11266
0
  else if (ret == 1)
11267
0
    h->indx = indx;
11268
0
  else if (h->indx == -2)
11269
0
    abort();
11270
11271
0
  return true;
11272
0
}
11273
11274
/* Return TRUE if special handling is done for relocs in SEC against
11275
   symbols defined in discarded sections.  */
11276
11277
static bool
11278
elf_section_ignore_discarded_relocs (asection *sec)
11279
0
{
11280
0
  elf_backend_data *bed;
11281
11282
0
  switch (sec->sec_info_type)
11283
0
    {
11284
0
    case SEC_INFO_TYPE_STABS:
11285
0
    case SEC_INFO_TYPE_EH_FRAME:
11286
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11287
0
    case SEC_INFO_TYPE_SFRAME:
11288
0
      return true;
11289
0
    default:
11290
0
      break;
11291
0
    }
11292
11293
0
  bed = get_elf_backend_data (sec->owner);
11294
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11295
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11296
0
    return true;
11297
11298
0
  return false;
11299
0
}
11300
11301
/* Return a mask saying how ld should treat relocations in SEC against
11302
   symbols defined in discarded sections.  If this function returns
11303
   COMPLAIN set, ld will issue a warning message.  If this function
11304
   returns PRETEND set, and the discarded section was link-once and the
11305
   same size as the kept link-once section, ld will pretend that the
11306
   symbol was actually defined in the kept section.  Otherwise ld will
11307
   zero the reloc (at least that is the intent, but some cooperation by
11308
   the target dependent code is needed, particularly for REL targets).  */
11309
11310
unsigned int
11311
_bfd_elf_default_action_discarded (asection *sec)
11312
0
{
11313
0
  if (sec->flags & SEC_DEBUGGING)
11314
0
    return PRETEND;
11315
11316
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11317
0
    return 0;
11318
11319
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11320
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11321
0
    return 0;
11322
11323
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11324
0
    return 0;
11325
11326
0
  return COMPLAIN | PRETEND;
11327
0
}
11328
11329
/* Find a match between a section and a member of a section group.  */
11330
11331
static asection *
11332
match_group_member (asection *sec, asection *group,
11333
        struct bfd_link_info *info)
11334
0
{
11335
0
  asection *first = elf_next_in_group (group);
11336
0
  asection *s = first;
11337
11338
0
  while (s != NULL)
11339
0
    {
11340
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11341
0
  return s;
11342
11343
0
      s = elf_next_in_group (s);
11344
0
      if (s == first)
11345
0
  break;
11346
0
    }
11347
11348
0
  return NULL;
11349
0
}
11350
11351
/* Check if the kept section of a discarded section SEC can be used
11352
   to replace it.  Return the replacement if it is OK.  Otherwise return
11353
   NULL.  */
11354
11355
asection *
11356
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11357
0
{
11358
0
  asection *kept;
11359
11360
0
  kept = sec->kept_section;
11361
0
  if (kept != NULL)
11362
0
    {
11363
0
      if ((kept->flags & SEC_GROUP) != 0)
11364
0
  kept = match_group_member (sec, kept, info);
11365
0
      if (kept != NULL)
11366
0
  {
11367
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11368
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11369
0
      kept = NULL;
11370
0
    else
11371
0
      {
11372
        /* Get the real kept section.  */
11373
0
        asection *next;
11374
0
        for (next = kept->kept_section;
11375
0
       next != NULL;
11376
0
       next = next->kept_section)
11377
0
    kept = next;
11378
0
      }
11379
0
  }
11380
0
      sec->kept_section = kept;
11381
0
    }
11382
0
  return kept;
11383
0
}
11384
11385
/* Link an input file into the linker output file.  This function
11386
   handles all the sections and relocations of the input file at once.
11387
   This is so that we only have to read the local symbols once, and
11388
   don't have to keep them in memory.  */
11389
11390
static bool
11391
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11392
0
{
11393
0
  int (*relocate_section)
11394
0
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11395
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11396
0
  bfd *output_bfd;
11397
0
  Elf_Internal_Shdr *symtab_hdr;
11398
0
  size_t locsymcount;
11399
0
  size_t extsymoff;
11400
0
  size_t num_sym;
11401
0
  Elf_Internal_Sym *isymbuf;
11402
0
  Elf_Internal_Sym *isym;
11403
0
  Elf_Internal_Sym *isymend;
11404
0
  long *pindex;
11405
0
  asection **ppsection;
11406
0
  asection *o;
11407
0
  elf_backend_data *bed;
11408
0
  struct elf_link_hash_entry **sym_hashes;
11409
0
  bfd_size_type address_size;
11410
0
  bfd_vma r_type_mask;
11411
0
  int r_sym_shift;
11412
0
  bool have_file_sym = false;
11413
11414
0
  output_bfd = flinfo->output_bfd;
11415
0
  bed = get_elf_backend_data (output_bfd);
11416
0
  relocate_section = bed->elf_backend_relocate_section;
11417
11418
  /* If this is a dynamic object, we don't want to do anything here:
11419
     we don't want the local symbols, and we don't want the section
11420
     contents.  */
11421
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11422
0
    return true;
11423
11424
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11425
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11426
0
  if (elf_bad_symtab (input_bfd))
11427
0
    {
11428
0
      locsymcount = num_sym;
11429
0
      extsymoff = 0;
11430
0
    }
11431
0
  else
11432
0
    {
11433
0
      locsymcount = symtab_hdr->sh_info;
11434
0
      extsymoff = symtab_hdr->sh_info;
11435
0
    }
11436
11437
  /* Enable GNU OSABI features in the output BFD that are used in the input
11438
     BFD.  */
11439
0
  if (bed->elf_osabi == ELFOSABI_NONE
11440
0
      || bed->elf_osabi == ELFOSABI_GNU
11441
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11442
0
    elf_tdata (output_bfd)->has_gnu_osabi
11443
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11444
0
    & (bfd_link_relocatable (flinfo->info)
11445
0
       ? -1 : ~elf_gnu_osabi_retain));
11446
11447
  /* Read the local symbols.  */
11448
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11449
0
  if (isymbuf == NULL && locsymcount != 0)
11450
0
    {
11451
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11452
0
              flinfo->internal_syms,
11453
0
              flinfo->external_syms,
11454
0
              flinfo->locsym_shndx);
11455
0
      if (isymbuf == NULL)
11456
0
  return false;
11457
0
    }
11458
11459
  /* Find local symbol sections and adjust values of symbols in
11460
     SEC_MERGE sections.  Write out those local symbols we know are
11461
     going into the output file.  */
11462
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11463
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11464
0
       isym < isymend;
11465
0
       isym++, pindex++, ppsection++)
11466
0
    {
11467
0
      asection *isec;
11468
0
      const char *name;
11469
0
      Elf_Internal_Sym osym;
11470
0
      long indx;
11471
0
      int ret;
11472
11473
0
      *pindex = -1;
11474
11475
0
      if (elf_bad_symtab (input_bfd))
11476
0
  {
11477
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11478
0
      {
11479
0
        *ppsection = NULL;
11480
0
        continue;
11481
0
      }
11482
0
  }
11483
11484
0
      if (isym->st_shndx == SHN_UNDEF)
11485
0
  isec = bfd_und_section_ptr;
11486
0
      else if (isym->st_shndx == SHN_ABS)
11487
0
  isec = bfd_abs_section_ptr;
11488
0
      else if (isym->st_shndx == SHN_COMMON)
11489
0
  isec = bfd_com_section_ptr;
11490
0
      else
11491
0
  {
11492
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11493
0
    if (isec == NULL)
11494
0
      {
11495
        /* Don't attempt to output symbols with st_shnx in the
11496
     reserved range other than SHN_ABS and SHN_COMMON.  */
11497
0
        isec = bfd_und_section_ptr;
11498
0
      }
11499
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11500
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11501
0
      isym->st_value =
11502
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11503
0
  }
11504
11505
0
      *ppsection = isec;
11506
11507
      /* Don't output the first, undefined, symbol.  In fact, don't
11508
   output any undefined local symbol.  */
11509
0
      if (isec == bfd_und_section_ptr)
11510
0
  continue;
11511
11512
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11513
0
  {
11514
    /* We never output section symbols.  Instead, we use the
11515
       section symbol of the corresponding section in the output
11516
       file.  */
11517
0
    continue;
11518
0
  }
11519
11520
      /* If we are stripping all symbols, we don't want to output this
11521
   one.  */
11522
0
      if (flinfo->info->strip == strip_all)
11523
0
  continue;
11524
11525
      /* If we are discarding all local symbols, we don't want to
11526
   output this one.  If we are generating a relocatable output
11527
   file, then some of the local symbols may be required by
11528
   relocs; we output them below as we discover that they are
11529
   needed.  */
11530
0
      if (flinfo->info->discard == discard_all)
11531
0
  continue;
11532
11533
      /* If this symbol is defined in a section which we are
11534
   discarding, we don't need to keep it.  */
11535
0
      if (isym->st_shndx < SHN_LORESERVE
11536
0
    && (isec->output_section == NULL
11537
0
        || bfd_section_removed_from_list (output_bfd,
11538
0
            isec->output_section)))
11539
0
  continue;
11540
11541
      /* Get the name of the symbol.  */
11542
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11543
0
                isym->st_name);
11544
0
      if (name == NULL)
11545
0
  return false;
11546
11547
      /* See if we are discarding symbols with this name.  */
11548
0
      if ((flinfo->info->strip == strip_some
11549
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11550
0
         == NULL))
11551
0
    || (((flinfo->info->discard == discard_sec_merge
11552
0
    && (isec->flags & SEC_MERGE)
11553
0
    && !bfd_link_relocatable (flinfo->info))
11554
0
         || flinfo->info->discard == discard_l)
11555
0
        && bfd_is_local_label_name (input_bfd, name)))
11556
0
  continue;
11557
11558
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11559
0
  {
11560
0
    if (input_bfd->lto_output)
11561
      /* -flto puts a temp file name here.  This means builds
11562
         are not reproducible.  Discard the symbol.  */
11563
0
      continue;
11564
0
    have_file_sym = true;
11565
0
    flinfo->filesym_count += 1;
11566
0
  }
11567
0
      if (!have_file_sym)
11568
0
  {
11569
    /* In the absence of debug info, bfd_find_nearest_line uses
11570
       FILE symbols to determine the source file for local
11571
       function symbols.  Provide a FILE symbol here if input
11572
       files lack such, so that their symbols won't be
11573
       associated with a previous input file.  It's not the
11574
       source file, but the best we can do.  */
11575
0
    const char *filename;
11576
0
    have_file_sym = true;
11577
0
    flinfo->filesym_count += 1;
11578
0
    memset (&osym, 0, sizeof (osym));
11579
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11580
0
    osym.st_shndx = SHN_ABS;
11581
0
    if (input_bfd->lto_output)
11582
0
      filename = NULL;
11583
0
    else
11584
0
      filename = lbasename (bfd_get_filename (input_bfd));
11585
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11586
0
            bfd_abs_section_ptr, NULL))
11587
0
      return false;
11588
0
  }
11589
11590
0
      osym = *isym;
11591
11592
      /* Adjust the section index for the output file.  */
11593
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11594
0
               isec->output_section);
11595
0
      if (osym.st_shndx == SHN_BAD)
11596
0
  return false;
11597
11598
      /* ELF symbols in relocatable files are section relative, but
11599
   in executable files they are virtual addresses.  Note that
11600
   this code assumes that all ELF sections have an associated
11601
   BFD section with a reasonable value for output_offset; below
11602
   we assume that they also have a reasonable value for
11603
   output_section.  Any special sections must be set up to meet
11604
   these requirements.  */
11605
0
      osym.st_value += isec->output_offset;
11606
0
      if (!bfd_link_relocatable (flinfo->info))
11607
0
  {
11608
0
    osym.st_value += isec->output_section->vma;
11609
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11610
0
      {
11611
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11612
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11613
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11614
0
        else
11615
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11616
0
              STT_NOTYPE);
11617
0
      }
11618
0
  }
11619
11620
0
      indx = bfd_get_symcount (output_bfd);
11621
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11622
0
      if (ret == 0)
11623
0
  return false;
11624
0
      else if (ret == 1)
11625
0
  *pindex = indx;
11626
0
    }
11627
11628
0
  if (bed->s->arch_size == 32)
11629
0
    {
11630
0
      r_type_mask = 0xff;
11631
0
      r_sym_shift = 8;
11632
0
      address_size = 4;
11633
0
    }
11634
0
  else
11635
0
    {
11636
0
      r_type_mask = 0xffffffff;
11637
0
      r_sym_shift = 32;
11638
0
      address_size = 8;
11639
0
    }
11640
11641
  /* Relocate the contents of each section.  */
11642
0
  sym_hashes = elf_sym_hashes (input_bfd);
11643
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11644
0
    {
11645
0
      bfd_byte *contents;
11646
11647
0
      if (! o->linker_mark)
11648
0
  {
11649
    /* This section was omitted from the link.  */
11650
0
    continue;
11651
0
  }
11652
11653
0
      if (!flinfo->info->resolve_section_groups
11654
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11655
0
  {
11656
    /* Deal with the group signature symbol.  */
11657
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11658
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11659
0
    asection *osec = o->output_section;
11660
11661
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11662
0
    if (symndx >= locsymcount
11663
0
        || (elf_bad_symtab (input_bfd)
11664
0
      && flinfo->sections[symndx] == NULL))
11665
0
      {
11666
0
        struct elf_link_hash_entry *h;
11667
11668
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11669
0
            extsymoff, num_sym);
11670
0
        if (h == NULL)
11671
0
    {
11672
0
      _bfd_error_handler
11673
        /* xgettext:c-format */
11674
0
        (_("error: %pB: unable to create group section symbol"),
11675
0
         input_bfd);
11676
0
      bfd_set_error (bfd_error_bad_value);
11677
0
      return false;
11678
0
    }
11679
11680
        /* Arrange for symbol to be output.  */
11681
0
        h->indx = -2;
11682
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11683
0
      }
11684
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11685
0
      {
11686
        /* We'll use the output section target_index.  */
11687
0
        asection *sec = flinfo->sections[symndx]->output_section;
11688
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11689
0
      }
11690
0
    else
11691
0
      {
11692
0
        if (flinfo->indices[symndx] == -1)
11693
0
    {
11694
      /* Otherwise output the local symbol now.  */
11695
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11696
0
      asection *sec = flinfo->sections[symndx]->output_section;
11697
0
      const char *name;
11698
0
      long indx;
11699
0
      int ret;
11700
11701
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11702
0
                symtab_hdr->sh_link,
11703
0
                sym.st_name);
11704
0
      if (name == NULL)
11705
0
        return false;
11706
11707
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11708
0
                    sec);
11709
0
      if (sym.st_shndx == SHN_BAD)
11710
0
        return false;
11711
11712
0
      sym.st_value += o->output_offset;
11713
11714
0
      indx = bfd_get_symcount (output_bfd);
11715
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11716
0
               NULL);
11717
0
      if (ret == 0)
11718
0
        return false;
11719
0
      else if (ret == 1)
11720
0
        flinfo->indices[symndx] = indx;
11721
0
      else
11722
0
        abort ();
11723
0
    }
11724
0
        elf_section_data (osec)->this_hdr.sh_info
11725
0
    = flinfo->indices[symndx];
11726
0
      }
11727
0
  }
11728
11729
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11730
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11731
0
  continue;
11732
11733
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11734
0
  {
11735
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11736
       or somesuch.  */
11737
0
    continue;
11738
0
  }
11739
11740
      /* Get the contents of the section.  They have been cached by a
11741
   relaxation routine.  Note that o is a section in an input
11742
   file, so the contents field will not have been set by any of
11743
   the routines which work on output files.  */
11744
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11745
0
  {
11746
0
    contents = elf_section_data (o)->this_hdr.contents;
11747
0
    if (bed->caches_rawsize
11748
0
        && o->rawsize != 0
11749
0
        && o->rawsize < o->size)
11750
0
      {
11751
0
        memcpy (flinfo->contents, contents, o->rawsize);
11752
0
        contents = flinfo->contents;
11753
0
      }
11754
0
  }
11755
0
      else if (!(o->flags & SEC_RELOC)
11756
0
         && !bed->elf_backend_write_section
11757
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11758
  /* A MERGE section that has no relocations doesn't need the
11759
     contents anymore, they have been recorded earlier.  Except
11760
     if the backend has special provisions for writing sections.  */
11761
0
  contents = NULL;
11762
0
      else
11763
0
  {
11764
0
    contents = flinfo->contents;
11765
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11766
0
                 &contents))
11767
0
      return false;
11768
0
  }
11769
11770
0
      if ((o->flags & SEC_RELOC) != 0)
11771
0
  {
11772
0
    Elf_Internal_Rela *internal_relocs;
11773
0
    Elf_Internal_Rela *rel, *relend;
11774
0
    int action_discarded;
11775
0
    int ret;
11776
11777
    /* Get the swapped relocs.  */
11778
0
    internal_relocs
11779
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11780
0
                flinfo->external_relocs,
11781
0
                flinfo->internal_relocs,
11782
0
                false);
11783
0
    if (internal_relocs == NULL
11784
0
        && o->reloc_count > 0)
11785
0
      return false;
11786
11787
0
    action_discarded = -1;
11788
0
    if (!elf_section_ignore_discarded_relocs (o))
11789
0
      action_discarded = (*bed->action_discarded) (o);
11790
11791
    /* Run through the relocs evaluating complex reloc symbols and
11792
       looking for relocs against symbols from discarded sections
11793
       or section symbols from removed link-once sections.
11794
       Complain about relocs against discarded sections.  Zero
11795
       relocs against removed link-once sections.  */
11796
11797
0
    rel = internal_relocs;
11798
0
    relend = rel + o->reloc_count;
11799
0
    for ( ; rel < relend; rel++)
11800
0
      {
11801
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11802
0
        unsigned int s_type;
11803
0
        asection **ps, *sec;
11804
0
        struct elf_link_hash_entry *h = NULL;
11805
0
        const char *sym_name;
11806
11807
0
        if (r_symndx == STN_UNDEF)
11808
0
    continue;
11809
11810
0
        if (r_symndx >= locsymcount
11811
0
      || (elf_bad_symtab (input_bfd)
11812
0
          && flinfo->sections[r_symndx] == NULL))
11813
0
    {
11814
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11815
0
                extsymoff, num_sym);
11816
11817
      /* Badly formatted input files can contain relocs that
11818
         reference non-existant symbols.  Check here so that
11819
         we do not seg fault.  */
11820
0
      if (h == NULL)
11821
0
        {
11822
0
          _bfd_error_handler
11823
      /* xgettext:c-format */
11824
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11825
0
         "that references a non-existent global symbol"),
11826
0
       input_bfd, (uint64_t) rel->r_info, o);
11827
0
          bfd_set_error (bfd_error_bad_value);
11828
0
          return false;
11829
0
        }
11830
11831
0
      s_type = h->type;
11832
11833
      /* If a plugin symbol is referenced from a non-IR file,
11834
         mark the symbol as undefined.  Note that the
11835
         linker may attach linker created dynamic sections
11836
         to the plugin bfd.  Symbols defined in linker
11837
         created sections are not plugin symbols.  */
11838
0
      if ((h->root.non_ir_ref_regular
11839
0
           || h->root.non_ir_ref_dynamic)
11840
0
          && (h->root.type == bfd_link_hash_defined
11841
0
        || h->root.type == bfd_link_hash_defweak)
11842
0
          && (h->root.u.def.section->flags
11843
0
        & SEC_LINKER_CREATED) == 0
11844
0
          && h->root.u.def.section->owner != NULL
11845
0
          && (h->root.u.def.section->owner->flags
11846
0
        & BFD_PLUGIN) != 0)
11847
0
        {
11848
0
          h->root.type = bfd_link_hash_undefined;
11849
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11850
0
        }
11851
11852
0
      ps = NULL;
11853
0
      if (h->root.type == bfd_link_hash_defined
11854
0
          || h->root.type == bfd_link_hash_defweak)
11855
0
        ps = &h->root.u.def.section;
11856
11857
0
      sym_name = h->root.root.string;
11858
0
    }
11859
0
        else
11860
0
    {
11861
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11862
11863
0
      s_type = ELF_ST_TYPE (sym->st_info);
11864
0
      ps = &flinfo->sections[r_symndx];
11865
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11866
0
                 sym, *ps);
11867
0
    }
11868
11869
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11870
0
      && !bfd_link_relocatable (flinfo->info))
11871
0
    {
11872
0
      bfd_vma val;
11873
0
      bfd_vma dot = (rel->r_offset
11874
0
         + o->output_offset + o->output_section->vma);
11875
#ifdef DEBUG
11876
      printf ("Encountered a complex symbol!");
11877
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11878
        bfd_get_filename (input_bfd), o->name,
11879
        (long) (rel - internal_relocs));
11880
      printf (" symbol: idx  %8.8lx, name %s\n",
11881
        r_symndx, sym_name);
11882
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11883
        (unsigned long) rel->r_info,
11884
        (unsigned long) rel->r_offset);
11885
#endif
11886
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11887
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11888
0
        return false;
11889
11890
      /* Symbol evaluated OK.  Update to absolute value.  */
11891
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11892
0
           num_sym, r_symndx, val))
11893
0
        return false;
11894
11895
0
      continue;
11896
0
    }
11897
11898
0
        if (action_discarded != -1 && ps != NULL)
11899
0
    {
11900
      /* Complain if the definition comes from a
11901
         discarded section.  */
11902
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11903
0
        {
11904
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11905
0
          if (action_discarded & COMPLAIN)
11906
0
      (*flinfo->info->callbacks->einfo)
11907
        /* xgettext:c-format */
11908
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11909
0
           "defined in discarded section `%pA' of %pB\n"),
11910
0
         sym_name, o, input_bfd, sec, sec->owner);
11911
11912
          /* Try to do the best we can to support buggy old
11913
       versions of gcc.  Pretend that the symbol is
11914
       really defined in the kept linkonce section.
11915
       FIXME: This is quite broken.  Modifying the
11916
       symbol here means we will be changing all later
11917
       uses of the symbol, not just in this section.  */
11918
0
          if (action_discarded & PRETEND)
11919
0
      {
11920
0
        asection *kept;
11921
11922
0
        kept = _bfd_elf_check_kept_section (sec,
11923
0
                    flinfo->info);
11924
0
        if (kept != NULL)
11925
0
          {
11926
0
            *ps = kept;
11927
0
            continue;
11928
0
          }
11929
0
      }
11930
0
        }
11931
0
    }
11932
0
      }
11933
11934
    /* Relocate the section by invoking a back end routine.
11935
11936
       The back end routine is responsible for adjusting the
11937
       section contents as necessary, and (if using Rela relocs
11938
       and generating a relocatable output file) adjusting the
11939
       reloc addend as necessary.
11940
11941
       The back end routine does not have to worry about setting
11942
       the reloc address or the reloc symbol index.
11943
11944
       The back end routine is given a pointer to the swapped in
11945
       internal symbols, and can access the hash table entries
11946
       for the external symbols via elf_sym_hashes (input_bfd).
11947
11948
       When generating relocatable output, the back end routine
11949
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11950
       output symbol is going to be a section symbol
11951
       corresponding to the output section, which will require
11952
       the addend to be adjusted.  */
11953
11954
0
    ret = (*relocate_section) (output_bfd, flinfo->info,
11955
0
             input_bfd, o, contents,
11956
0
             internal_relocs,
11957
0
             isymbuf,
11958
0
             flinfo->sections);
11959
0
    if (!ret)
11960
0
      return false;
11961
11962
0
    if (ret == 2
11963
0
        || bfd_link_relocatable (flinfo->info)
11964
0
        || flinfo->info->emitrelocations)
11965
0
      {
11966
0
        Elf_Internal_Rela *irela;
11967
0
        Elf_Internal_Rela *irelaend, *irelamid;
11968
0
        bfd_vma last_offset;
11969
0
        struct elf_link_hash_entry **rel_hash;
11970
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11971
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11972
0
        unsigned int next_erel;
11973
0
        bool rela_normal;
11974
0
        struct bfd_elf_section_data *esdi, *esdo;
11975
11976
0
        esdi = elf_section_data (o);
11977
0
        esdo = elf_section_data (o->output_section);
11978
0
        rela_normal = false;
11979
11980
        /* Adjust the reloc addresses and symbol indices.  */
11981
11982
0
        irela = internal_relocs;
11983
0
        irelaend = irela + o->reloc_count;
11984
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11985
        /* We start processing the REL relocs, if any.  When we reach
11986
     IRELAMID in the loop, we switch to the RELA relocs.  */
11987
0
        irelamid = irela;
11988
0
        if (esdi->rel.hdr != NULL)
11989
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11990
0
           * bed->s->int_rels_per_ext_rel);
11991
0
        rel_hash_list = rel_hash;
11992
0
        rela_hash_list = NULL;
11993
0
        last_offset = o->output_offset;
11994
0
        if (!bfd_link_relocatable (flinfo->info))
11995
0
    last_offset += o->output_section->vma;
11996
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11997
0
    {
11998
0
      unsigned long r_symndx;
11999
0
      asection *sec;
12000
0
      Elf_Internal_Sym sym;
12001
12002
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
12003
0
        {
12004
0
          rel_hash++;
12005
0
          next_erel = 0;
12006
0
        }
12007
12008
0
      if (irela == irelamid)
12009
0
        {
12010
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
12011
0
          rela_hash_list = rel_hash;
12012
0
          rela_normal = bed->rela_normal;
12013
0
        }
12014
12015
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
12016
0
                   flinfo->info, o,
12017
0
                   irela->r_offset);
12018
0
      if (irela->r_offset >= (bfd_vma) -2)
12019
0
        {
12020
          /* This is a reloc for a deleted entry or somesuch.
12021
       Turn it into an R_*_NONE reloc, at the same
12022
       offset as the last reloc.  elf_eh_frame.c and
12023
       bfd_elf_discard_info rely on reloc offsets
12024
       being ordered.  */
12025
0
          irela->r_offset = last_offset;
12026
0
          irela->r_info = 0;
12027
0
          irela->r_addend = 0;
12028
0
          continue;
12029
0
        }
12030
12031
0
      irela->r_offset += o->output_offset;
12032
12033
      /* Relocs in an executable have to be virtual addresses.  */
12034
0
      if (!bfd_link_relocatable (flinfo->info))
12035
0
        irela->r_offset += o->output_section->vma;
12036
12037
0
      last_offset = irela->r_offset;
12038
12039
0
      r_symndx = irela->r_info >> r_sym_shift;
12040
0
      if (r_symndx == STN_UNDEF)
12041
0
        continue;
12042
12043
0
      if (r_symndx >= locsymcount
12044
0
          || (elf_bad_symtab (input_bfd)
12045
0
        && flinfo->sections[r_symndx] == NULL))
12046
0
        {
12047
0
          struct elf_link_hash_entry *rh;
12048
12049
          /* This is a reloc against a global symbol.  We
12050
       have not yet output all the local symbols, so
12051
       we do not know the symbol index of any global
12052
       symbol.  We set the rel_hash entry for this
12053
       reloc to point to the global hash table entry
12054
       for this symbol.  The symbol index is then
12055
       set at the end of bfd_elf_final_link.  */
12056
0
          rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12057
0
               extsymoff, num_sym);
12058
0
          if (rh == NULL)
12059
0
      {
12060
        /* FIXME: Generate an error ?  */
12061
0
        continue;
12062
0
      }
12063
12064
          /* Setting the index to -2 tells elf_link_output_extsym
12065
       that this symbol is used by a reloc.  */
12066
0
          BFD_ASSERT (rh->indx < 0);
12067
0
          rh->indx = -2;
12068
0
          *rel_hash = rh;
12069
12070
0
          continue;
12071
0
        }
12072
12073
      /* This is a reloc against a local symbol.  */
12074
12075
0
      *rel_hash = NULL;
12076
0
      sym = isymbuf[r_symndx];
12077
0
      sec = flinfo->sections[r_symndx];
12078
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12079
0
        {
12080
          /* I suppose the backend ought to fill in the
12081
       section of any STT_SECTION symbol against a
12082
       processor specific section.  */
12083
0
          r_symndx = STN_UNDEF;
12084
0
          if (bfd_is_abs_section (sec))
12085
0
      ;
12086
0
          else if (sec == NULL || sec->owner == NULL)
12087
0
      {
12088
0
        bfd_set_error (bfd_error_bad_value);
12089
0
        return false;
12090
0
      }
12091
0
          else
12092
0
      {
12093
0
        asection *osec = sec->output_section;
12094
12095
        /* If we have discarded a section, the output
12096
           section will be the absolute section.  In
12097
           case of discarded SEC_MERGE sections, use
12098
           the kept section.  relocate_section should
12099
           have already handled discarded linkonce
12100
           sections.  */
12101
0
        if (bfd_is_abs_section (osec)
12102
0
            && sec->kept_section != NULL
12103
0
            && sec->kept_section->output_section != NULL)
12104
0
          {
12105
0
            osec = sec->kept_section->output_section;
12106
0
            irela->r_addend -= osec->vma;
12107
0
          }
12108
12109
0
        if (!bfd_is_abs_section (osec))
12110
0
          {
12111
0
            r_symndx = osec->target_index;
12112
0
            if (r_symndx == STN_UNDEF)
12113
0
        {
12114
0
          irela->r_addend += osec->vma;
12115
0
          osec = flinfo->info->callbacks->nearby_section
12116
0
            (output_bfd, osec, osec->vma);
12117
0
          irela->r_addend -= osec->vma;
12118
0
          r_symndx = osec->target_index;
12119
0
        }
12120
0
          }
12121
0
      }
12122
12123
          /* Adjust the addend according to where the
12124
       section winds up in the output section.  */
12125
0
          if (rela_normal)
12126
0
      irela->r_addend += sec->output_offset;
12127
0
        }
12128
0
      else
12129
0
        {
12130
0
          if (flinfo->indices[r_symndx] == -1)
12131
0
      {
12132
0
        unsigned long shlink;
12133
0
        const char *name;
12134
0
        asection *osec;
12135
0
        long indx;
12136
12137
0
        if (flinfo->info->strip == strip_all)
12138
0
          {
12139
            /* You can't do ld -r -s.  */
12140
0
            bfd_set_error (bfd_error_invalid_operation);
12141
0
            return false;
12142
0
          }
12143
12144
        /* This symbol was skipped earlier, but
12145
           since it is needed by a reloc, we
12146
           must output it now.  */
12147
0
        shlink = symtab_hdr->sh_link;
12148
0
        name = (bfd_elf_string_from_elf_section
12149
0
          (input_bfd, shlink, sym.st_name));
12150
0
        if (name == NULL)
12151
0
          return false;
12152
12153
0
        osec = sec->output_section;
12154
0
        sym.st_shndx =
12155
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12156
0
                     osec);
12157
0
        if (sym.st_shndx == SHN_BAD)
12158
0
          return false;
12159
12160
0
        sym.st_value += sec->output_offset;
12161
0
        if (!bfd_link_relocatable (flinfo->info))
12162
0
          {
12163
0
            sym.st_value += osec->vma;
12164
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12165
0
        {
12166
0
          struct elf_link_hash_table *htab
12167
0
            = elf_hash_table (flinfo->info);
12168
12169
          /* STT_TLS symbols are relative to PT_TLS
12170
             segment base.  */
12171
0
          if (htab->tls_sec != NULL)
12172
0
            sym.st_value -= htab->tls_sec->vma;
12173
0
          else
12174
0
            sym.st_info
12175
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12176
0
                 STT_NOTYPE);
12177
0
        }
12178
0
          }
12179
12180
0
        indx = bfd_get_symcount (output_bfd);
12181
0
        ret = elf_link_output_symstrtab (flinfo, name,
12182
0
                 &sym, sec,
12183
0
                 NULL);
12184
0
        if (ret == 0)
12185
0
          return false;
12186
0
        else if (ret == 1)
12187
0
          flinfo->indices[r_symndx] = indx;
12188
0
        else
12189
0
          abort ();
12190
0
      }
12191
12192
0
          r_symndx = flinfo->indices[r_symndx];
12193
0
        }
12194
12195
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12196
0
           | (irela->r_info & r_type_mask));
12197
0
    }
12198
12199
        /* Swap out the relocs.  */
12200
0
        input_rel_hdr = esdi->rel.hdr;
12201
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12202
0
    {
12203
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12204
0
                 input_rel_hdr,
12205
0
                 internal_relocs,
12206
0
                 rel_hash_list))
12207
0
        return false;
12208
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12209
0
              * bed->s->int_rels_per_ext_rel);
12210
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12211
0
    }
12212
12213
0
        input_rela_hdr = esdi->rela.hdr;
12214
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12215
0
    {
12216
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12217
0
                 input_rela_hdr,
12218
0
                 internal_relocs,
12219
0
                 rela_hash_list))
12220
0
        return false;
12221
0
    }
12222
0
      }
12223
0
  }
12224
12225
      /* Write out the modified section contents.  */
12226
0
      if (bed->elf_backend_write_section
12227
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12228
0
            contents))
12229
0
  {
12230
    /* Section written out.  */
12231
0
  }
12232
0
      else switch (o->sec_info_type)
12233
0
  {
12234
0
  case SEC_INFO_TYPE_STABS:
12235
0
    if (! (_bfd_write_section_stabs
12236
0
     (output_bfd,
12237
0
      &elf_hash_table (flinfo->info)->stab_info, o, contents)))
12238
0
      return false;
12239
0
    break;
12240
0
  case SEC_INFO_TYPE_MERGE:
12241
0
    if (! _bfd_write_merged_section (output_bfd, o))
12242
0
      return false;
12243
0
    break;
12244
0
  case SEC_INFO_TYPE_EH_FRAME:
12245
0
    {
12246
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12247
0
               o, contents))
12248
0
        return false;
12249
0
    }
12250
0
    break;
12251
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12252
0
    {
12253
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12254
0
               flinfo->info,
12255
0
               o, contents))
12256
0
        return false;
12257
0
    }
12258
0
    break;
12259
0
  case SEC_INFO_TYPE_SFRAME:
12260
0
      {
12261
        /* Merge SFrame section into the SFrame encoder context of the
12262
     output_bfd's section.  The final .sframe output section will
12263
     be written out later.  */
12264
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12265
0
              o, contents))
12266
0
    return false;
12267
0
      }
12268
0
      break;
12269
0
  default:
12270
0
    {
12271
0
      if (! (o->flags & SEC_EXCLUDE))
12272
0
        {
12273
0
    file_ptr offset = (file_ptr) o->output_offset;
12274
0
    bfd_size_type todo = o->size;
12275
12276
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12277
12278
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12279
0
        && o->size > address_size)
12280
0
      {
12281
        /* Reverse-copy input section to output.  */
12282
12283
0
        if ((o->size & (address_size - 1)) != 0
12284
0
      || (o->reloc_count != 0
12285
0
          && (o->size * bed->s->int_rels_per_ext_rel
12286
0
        != o->reloc_count * address_size)))
12287
0
          {
12288
0
      _bfd_error_handler
12289
        /* xgettext:c-format */
12290
0
        (_("error: %pB: size of section %pA is not "
12291
0
           "multiple of address size"),
12292
0
         input_bfd, o);
12293
0
      bfd_set_error (bfd_error_bad_value);
12294
0
      return false;
12295
0
          }
12296
12297
0
        do
12298
0
          {
12299
0
      todo -= address_size;
12300
0
      if (! bfd_set_section_contents (output_bfd,
12301
0
              o->output_section,
12302
0
              contents + todo,
12303
0
              offset,
12304
0
              address_size))
12305
0
        return false;
12306
0
      if (todo == 0)
12307
0
        break;
12308
0
      offset += address_size;
12309
0
          }
12310
0
        while (1);
12311
0
      }
12312
0
    else if (! bfd_set_section_contents (output_bfd,
12313
0
                 o->output_section,
12314
0
                 contents,
12315
0
                 offset, todo))
12316
0
      return false;
12317
0
        }
12318
0
    }
12319
0
    break;
12320
0
  }
12321
12322
      /* Munmap the section contents for each input section.  */
12323
0
      _bfd_elf_link_munmap_section_contents (o);
12324
0
    }
12325
12326
0
  return true;
12327
0
}
12328
12329
/* Generate a reloc when linking an ELF file.  This is a reloc
12330
   requested by the linker, and does not come from any input file.  This
12331
   is used to build constructor and destructor tables when linking
12332
   with -Ur.  */
12333
12334
static bool
12335
elf_reloc_link_order (bfd *output_bfd,
12336
          struct bfd_link_info *info,
12337
          asection *output_section,
12338
          struct bfd_link_order *link_order)
12339
0
{
12340
0
  reloc_howto_type *howto;
12341
0
  long indx;
12342
0
  bfd_vma offset;
12343
0
  bfd_vma addend;
12344
0
  struct bfd_elf_section_reloc_data *reldata;
12345
0
  struct elf_link_hash_entry **rel_hash_ptr;
12346
0
  Elf_Internal_Shdr *rel_hdr;
12347
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12348
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12349
0
  bfd_byte *erel;
12350
0
  unsigned int i;
12351
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12352
12353
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12354
0
  if (howto == NULL)
12355
0
    {
12356
0
      bfd_set_error (bfd_error_bad_value);
12357
0
      return false;
12358
0
    }
12359
12360
0
  addend = link_order->u.reloc.p->addend;
12361
12362
0
  if (esdo->rel.hdr)
12363
0
    reldata = &esdo->rel;
12364
0
  else if (esdo->rela.hdr)
12365
0
    reldata = &esdo->rela;
12366
0
  else
12367
0
    {
12368
0
      reldata = NULL;
12369
0
      BFD_ASSERT (0);
12370
0
    }
12371
12372
  /* Figure out the symbol index.  */
12373
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12374
0
  if (link_order->type == bfd_section_reloc_link_order)
12375
0
    {
12376
0
      indx = link_order->u.reloc.p->u.section->target_index;
12377
0
      BFD_ASSERT (indx != 0);
12378
0
      *rel_hash_ptr = NULL;
12379
0
    }
12380
0
  else
12381
0
    {
12382
0
      struct elf_link_hash_entry *h;
12383
12384
      /* Treat a reloc against a defined symbol as though it were
12385
   actually against the section.  */
12386
0
      h = ((struct elf_link_hash_entry *)
12387
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12388
0
           link_order->u.reloc.p->u.name,
12389
0
           false, false, true));
12390
0
      if (h != NULL
12391
0
    && (h->root.type == bfd_link_hash_defined
12392
0
        || h->root.type == bfd_link_hash_defweak))
12393
0
  {
12394
0
    asection *section;
12395
12396
0
    section = h->root.u.def.section;
12397
0
    indx = section->output_section->target_index;
12398
0
    *rel_hash_ptr = NULL;
12399
    /* It seems that we ought to add the symbol value to the
12400
       addend here, but in practice it has already been added
12401
       because it was passed to constructor_callback.  */
12402
0
    addend += section->output_section->vma + section->output_offset;
12403
0
  }
12404
0
      else if (h != NULL)
12405
0
  {
12406
    /* Setting the index to -2 tells elf_link_output_extsym that
12407
       this symbol is used by a reloc.  */
12408
0
    h->indx = -2;
12409
0
    *rel_hash_ptr = h;
12410
0
    indx = 0;
12411
0
  }
12412
0
      else
12413
0
  {
12414
0
    (*info->callbacks->unattached_reloc)
12415
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12416
0
    indx = 0;
12417
0
  }
12418
0
    }
12419
12420
  /* If this is an inplace reloc, we must write the addend into the
12421
     object file.  */
12422
0
  if (howto->partial_inplace && addend != 0)
12423
0
    {
12424
0
      bfd_size_type size;
12425
0
      bfd_reloc_status_type rstat;
12426
0
      bfd_byte *buf;
12427
0
      bool ok;
12428
0
      const char *sym_name;
12429
0
      bfd_size_type octets;
12430
12431
0
      size = (bfd_size_type) bfd_get_reloc_size (howto);
12432
0
      buf = (bfd_byte *) bfd_zmalloc (size);
12433
0
      if (buf == NULL && size != 0)
12434
0
  return false;
12435
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12436
0
      switch (rstat)
12437
0
  {
12438
0
  case bfd_reloc_ok:
12439
0
    break;
12440
12441
0
  default:
12442
0
  case bfd_reloc_outofrange:
12443
0
    abort ();
12444
12445
0
  case bfd_reloc_overflow:
12446
0
    if (link_order->type == bfd_section_reloc_link_order)
12447
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12448
0
    else
12449
0
      sym_name = link_order->u.reloc.p->u.name;
12450
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12451
0
                howto->name, addend, NULL, NULL,
12452
0
                (bfd_vma) 0);
12453
0
    break;
12454
0
  }
12455
12456
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12457
0
               output_section);
12458
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12459
0
             octets, size);
12460
0
      free (buf);
12461
0
      if (! ok)
12462
0
  return false;
12463
0
    }
12464
12465
  /* The address of a reloc is relative to the section in a
12466
     relocatable file, and is a virtual address in an executable
12467
     file.  */
12468
0
  offset = link_order->offset;
12469
0
  if (! bfd_link_relocatable (info))
12470
0
    offset += output_section->vma;
12471
12472
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12473
0
    {
12474
0
      irel[i].r_offset = offset;
12475
0
      irel[i].r_info = 0;
12476
0
      irel[i].r_addend = 0;
12477
0
    }
12478
0
  if (bed->s->arch_size == 32)
12479
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12480
0
  else
12481
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12482
12483
0
  rel_hdr = reldata->hdr;
12484
0
  erel = rel_hdr->contents;
12485
0
  if (rel_hdr->sh_type == SHT_REL)
12486
0
    {
12487
0
      erel += reldata->count * bed->s->sizeof_rel;
12488
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12489
0
    }
12490
0
  else
12491
0
    {
12492
0
      irel[0].r_addend = addend;
12493
0
      erel += reldata->count * bed->s->sizeof_rela;
12494
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12495
0
    }
12496
12497
0
  ++reldata->count;
12498
12499
0
  return true;
12500
0
}
12501
12502
/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12503
   Returns TRUE upon success, FALSE otherwise.  */
12504
12505
static bool
12506
elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12507
0
{
12508
0
  bool ret = false;
12509
0
  bfd *implib_bfd;
12510
0
  elf_backend_data *bed;
12511
0
  flagword flags;
12512
0
  enum bfd_architecture arch;
12513
0
  unsigned int mach;
12514
0
  asymbol **sympp = NULL;
12515
0
  long symsize;
12516
0
  long symcount;
12517
0
  long src_count;
12518
0
  elf_symbol_type *osymbuf;
12519
0
  size_t amt;
12520
12521
0
  implib_bfd = info->out_implib_bfd;
12522
0
  bed = get_elf_backend_data (abfd);
12523
12524
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12525
0
    return false;
12526
12527
  /* Use flag from executable but make it a relocatable object.  */
12528
0
  flags = bfd_get_file_flags (abfd);
12529
0
  flags &= ~HAS_RELOC;
12530
0
  if (!bfd_set_start_address (implib_bfd, 0)
12531
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12532
0
    return false;
12533
12534
  /* Copy architecture of output file to import library file.  */
12535
0
  arch = bfd_get_arch (abfd);
12536
0
  mach = bfd_get_mach (abfd);
12537
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12538
0
      && (abfd->target_defaulted
12539
0
    || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12540
0
    return false;
12541
12542
  /* Get symbol table size.  */
12543
0
  symsize = bfd_get_symtab_upper_bound (abfd);
12544
0
  if (symsize < 0)
12545
0
    return false;
12546
12547
  /* Read in the symbol table.  */
12548
0
  sympp = (asymbol **) bfd_malloc (symsize);
12549
0
  if (sympp == NULL)
12550
0
    return false;
12551
12552
0
  symcount = bfd_canonicalize_symtab (abfd, sympp);
12553
0
  if (symcount < 0)
12554
0
    goto free_sym_buf;
12555
12556
  /* Allow the BFD backend to copy any private header data it
12557
     understands from the output BFD to the import library BFD.  */
12558
0
  if (! bfd_copy_private_header_data (abfd, implib_bfd))
12559
0
    goto free_sym_buf;
12560
12561
  /* Filter symbols to appear in the import library.  */
12562
0
  if (bed->elf_backend_filter_implib_symbols)
12563
0
    symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12564
0
                   symcount);
12565
0
  else
12566
0
    symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12567
0
  if (symcount == 0)
12568
0
    {
12569
0
      bfd_set_error (bfd_error_no_symbols);
12570
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12571
0
        implib_bfd);
12572
0
      goto free_sym_buf;
12573
0
    }
12574
12575
12576
  /* Make symbols absolute.  */
12577
0
  amt = symcount * sizeof (*osymbuf);
12578
0
  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12579
0
  if (osymbuf == NULL)
12580
0
    goto free_sym_buf;
12581
12582
0
  for (src_count = 0; src_count < symcount; src_count++)
12583
0
    {
12584
0
      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12585
0
        sizeof (*osymbuf));
12586
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12587
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12588
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12589
0
      osymbuf[src_count].internal_elf_sym.st_value =
12590
0
  osymbuf[src_count].symbol.value;
12591
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12592
0
    }
12593
12594
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12595
12596
  /* Allow the BFD backend to copy any private data it understands
12597
     from the output BFD to the import library BFD.  This is done last
12598
     to permit the routine to look at the filtered symbol table.  */
12599
0
  if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12600
0
    goto free_sym_buf;
12601
12602
0
  if (!bfd_close (implib_bfd))
12603
0
    goto free_sym_buf;
12604
12605
0
  ret = true;
12606
12607
0
 free_sym_buf:
12608
0
  free (sympp);
12609
0
  return ret;
12610
0
}
12611
12612
static void
12613
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12614
0
{
12615
0
  asection *o;
12616
12617
0
  if (flinfo->symstrtab != NULL)
12618
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12619
0
  free (flinfo->contents);
12620
0
  free (flinfo->external_relocs);
12621
0
  free (flinfo->internal_relocs);
12622
0
  free (flinfo->external_syms);
12623
0
  free (flinfo->locsym_shndx);
12624
0
  free (flinfo->internal_syms);
12625
0
  free (flinfo->indices);
12626
0
  free (flinfo->sections);
12627
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12628
0
    free (flinfo->symshndxbuf);
12629
0
  for (o = obfd->sections; o != NULL; o = o->next)
12630
0
    {
12631
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12632
0
      free (esdo->rel.hashes);
12633
0
      free (esdo->rela.hashes);
12634
0
    }
12635
0
}
12636
12637
/* Do the final step of an ELF link.  */
12638
12639
bool
12640
_bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12641
0
{
12642
0
  bool dynamic;
12643
0
  bool emit_relocs;
12644
0
  bfd *dynobj;
12645
0
  struct elf_final_link_info flinfo;
12646
0
  asection *o;
12647
0
  struct bfd_link_order *p;
12648
0
  bfd *sub;
12649
0
  bfd_size_type max_contents_size;
12650
0
  bfd_size_type max_external_reloc_size;
12651
0
  bfd_size_type max_internal_reloc_count;
12652
0
  bfd_size_type max_sym_count;
12653
0
  bfd_size_type max_sym_shndx_count;
12654
0
  Elf_Internal_Sym elfsym;
12655
0
  unsigned int i;
12656
0
  Elf_Internal_Shdr *symtab_hdr;
12657
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12658
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12659
0
  struct elf_outext_info eoinfo;
12660
0
  bool merged;
12661
0
  size_t relativecount;
12662
0
  size_t relr_entsize;
12663
0
  asection *reldyn = 0;
12664
0
  bfd_size_type amt;
12665
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12666
0
  bool sections_removed;
12667
12668
0
  if (!is_elf_hash_table (&htab->root))
12669
0
    return false;
12670
12671
0
  if (bfd_link_pic (info))
12672
0
    abfd->flags |= DYNAMIC;
12673
12674
0
  dynamic = htab->dynamic_sections_created;
12675
0
  dynobj = htab->dynobj;
12676
12677
0
  emit_relocs = (bfd_link_relocatable (info)
12678
0
     || info->emitrelocations);
12679
12680
0
  memset (&flinfo, 0, sizeof (flinfo));
12681
0
  flinfo.info = info;
12682
0
  flinfo.output_bfd = abfd;
12683
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12684
0
  if (flinfo.symstrtab == NULL)
12685
0
    return false;
12686
12687
0
  if (! dynamic)
12688
0
    {
12689
0
      flinfo.hash_sec = NULL;
12690
0
      flinfo.symver_sec = NULL;
12691
0
    }
12692
0
  else
12693
0
    {
12694
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12695
      /* Note that dynsym_sec can be NULL (on VMS).  */
12696
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12697
      /* Note that it is OK if symver_sec is NULL.  */
12698
0
    }
12699
12700
0
  if (info->unique_symbol
12701
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12702
0
             local_hash_newfunc,
12703
0
             sizeof (struct local_hash_entry)))
12704
0
    return false;
12705
12706
  /* The object attributes have been merged.  Remove the input
12707
     sections from the link, and set the contents of the output
12708
     section.  */
12709
0
  sections_removed = false;
12710
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12711
0
  const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12712
0
#endif
12713
0
  for (o = abfd->sections; o != NULL; o = o->next)
12714
0
    {
12715
0
      bool remove_section = false;
12716
12717
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12718
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12719
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12720
0
  {
12721
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12722
0
      {
12723
0
        asection *input_section;
12724
12725
0
        if (p->type != bfd_indirect_link_order)
12726
0
    continue;
12727
0
        input_section = p->u.indirect.section;
12728
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12729
     elf_link_input_bfd ignores this section.  */
12730
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12731
0
      }
12732
12733
    /* Skip this section later on.  */
12734
0
    o->map_head.link_order = NULL;
12735
12736
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
12737
    /* Once ELF headers have been written, the size of a section is
12738
       frozen. We need to set the size of the attribute section before
12739
       _bfd_elf_compute_section_file_positions.  */
12740
0
    bfd_set_section_size (o, attr_size);
12741
0
    if (attr_size > 0)
12742
0
      elf_obj_object_attributes (abfd) = o;
12743
0
    else
12744
0
      remove_section = true;
12745
0
  }
12746
0
      else
12747
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12748
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12749
0
  {
12750
    /* Remove empty group section from linker output.  */
12751
0
    remove_section = true;
12752
0
  }
12753
0
      if (remove_section)
12754
0
  {
12755
0
    o->flags |= SEC_EXCLUDE;
12756
0
    bfd_section_list_remove (abfd, o);
12757
0
    abfd->section_count--;
12758
0
    sections_removed = true;
12759
0
  }
12760
0
    }
12761
0
  if (sections_removed)
12762
0
    bfd_fix_excluded_sec_syms (info);
12763
12764
  /* Count up the number of relocations we will output for each output
12765
     section, so that we know the sizes of the reloc sections.  We
12766
     also figure out some maximum sizes.  */
12767
0
#ifdef USE_MMAP
12768
0
  if (bed->use_mmap)
12769
0
    {
12770
      /* Mmap is used only if section size >= the minimum mmap section
12771
   size.  The initial max_contents_size value covers all sections
12772
   smaller than the minimum mmap section size.  It may be increased
12773
   for compressed or linker created sections or sections whose
12774
   rawsize != size.  max_external_reloc_size covers all relocation
12775
   sections smaller than the minimum mmap section size.  */
12776
0
      max_contents_size = _bfd_minimum_mmap_size;
12777
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12778
0
    }
12779
0
  else
12780
0
#endif
12781
0
    {
12782
0
      max_contents_size = 0;
12783
0
      max_external_reloc_size = 0;
12784
0
    }
12785
0
  max_internal_reloc_count = 0;
12786
0
  max_sym_count = 0;
12787
0
  max_sym_shndx_count = 0;
12788
0
  merged = false;
12789
0
  for (o = abfd->sections; o != NULL; o = o->next)
12790
0
    {
12791
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12792
0
      bfd_size_type size_align = 1;
12793
12794
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12795
0
  {
12796
    /* eh_frame editing can extend last FDE to cover padding
12797
       between one section and the next.  */
12798
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12799
0
      * bfd_octets_per_byte (abfd, o));
12800
0
  }
12801
12802
0
      o->reloc_count = 0;
12803
12804
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12805
0
  {
12806
0
    unsigned int reloc_count = 0;
12807
0
    unsigned int additional_reloc_count = 0;
12808
0
    struct bfd_elf_section_data *esdi = NULL;
12809
12810
0
    if (p->type == bfd_section_reloc_link_order
12811
0
        || p->type == bfd_symbol_reloc_link_order)
12812
0
      reloc_count = 1;
12813
0
    else if (p->type == bfd_indirect_link_order)
12814
0
      {
12815
0
        asection *sec;
12816
12817
0
        sec = p->u.indirect.section;
12818
12819
        /* Mark all sections which are to be included in the
12820
     link.  This will normally be every section.  We need
12821
     to do this so that we can identify any sections which
12822
     the linker has decided to not include.  */
12823
0
        sec->linker_mark = true;
12824
12825
0
        if (sec->flags & SEC_MERGE)
12826
0
    merged = true;
12827
12828
0
#ifdef USE_MMAP
12829
        /* Mmap is used only on non-compressed, non-linker created
12830
     sections whose rawsize == size.  */
12831
0
        if (!bed->use_mmap
12832
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12833
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12834
0
      || sec->rawsize != sec->size)
12835
0
#endif
12836
0
    {
12837
0
      bfd_size_type size = (sec->rawsize > sec->size
12838
0
          ? sec->rawsize : sec->size);
12839
0
      size = (size + size_align - 1) & -size_align;
12840
0
      if (max_contents_size < size)
12841
0
        max_contents_size = size;
12842
0
    }
12843
12844
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12845
0
      && (sec->owner->flags & DYNAMIC) == 0)
12846
0
    {
12847
0
      size_t sym_count;
12848
12849
      /* We are interested in just local symbols, not all
12850
         symbols.  */
12851
0
      if (elf_bad_symtab (sec->owner))
12852
0
        sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12853
0
         / bed->s->sizeof_sym);
12854
0
      else
12855
0
        sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12856
12857
0
      if (sym_count > max_sym_count)
12858
0
        max_sym_count = sym_count;
12859
12860
0
      if (sym_count > max_sym_shndx_count
12861
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12862
0
        max_sym_shndx_count = sym_count;
12863
12864
0
      esdi = elf_section_data (sec);
12865
12866
0
      if (esdi->this_hdr.sh_type == SHT_REL
12867
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12868
        /* Some backends use reloc_count in relocation sections
12869
           to count particular types of relocs.  Of course,
12870
           reloc sections themselves can't have relocations.  */
12871
0
        ;
12872
0
      else if (emit_relocs)
12873
0
        {
12874
0
          reloc_count = sec->reloc_count;
12875
0
          if (bed->elf_backend_count_additional_relocs)
12876
0
      {
12877
0
        int c;
12878
0
        c = (*bed->elf_backend_count_additional_relocs) (sec);
12879
0
        additional_reloc_count += c;
12880
0
      }
12881
0
        }
12882
0
      else if (bed->elf_backend_count_relocs)
12883
0
        reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12884
12885
0
      if ((sec->flags & SEC_RELOC) != 0)
12886
0
        {
12887
0
#ifdef USE_MMAP
12888
0
          if (!bed->use_mmap)
12889
0
#endif
12890
0
      {
12891
0
        size_t ext_size = 0;
12892
12893
0
        if (esdi->rel.hdr != NULL)
12894
0
          ext_size = esdi->rel.hdr->sh_size;
12895
0
        if (esdi->rela.hdr != NULL)
12896
0
          ext_size += esdi->rela.hdr->sh_size;
12897
12898
0
        if (ext_size > max_external_reloc_size)
12899
0
          max_external_reloc_size = ext_size;
12900
0
      }
12901
0
          if (sec->reloc_count > max_internal_reloc_count)
12902
0
      max_internal_reloc_count = sec->reloc_count;
12903
0
        }
12904
0
    }
12905
0
      }
12906
12907
0
    if (reloc_count == 0)
12908
0
      continue;
12909
12910
0
    reloc_count += additional_reloc_count;
12911
0
    o->reloc_count += reloc_count;
12912
12913
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12914
0
      {
12915
0
        if (esdi->rel.hdr)
12916
0
    {
12917
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12918
0
      esdo->rel.count += additional_reloc_count;
12919
0
    }
12920
0
        if (esdi->rela.hdr)
12921
0
    {
12922
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12923
0
      esdo->rela.count += additional_reloc_count;
12924
0
    }
12925
0
      }
12926
0
    else
12927
0
      {
12928
0
        if (o->use_rela_p)
12929
0
    esdo->rela.count += reloc_count;
12930
0
        else
12931
0
    esdo->rel.count += reloc_count;
12932
0
      }
12933
0
  }
12934
12935
0
      if (o->reloc_count > 0)
12936
0
  o->flags |= SEC_RELOC;
12937
0
      else
12938
0
  {
12939
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12940
       set it (this is probably a bug) and if it is set
12941
       assign_section_numbers will create a reloc section.  */
12942
0
    o->flags &=~ SEC_RELOC;
12943
0
  }
12944
12945
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12946
   zero.  This is done in elf_fake_sections as well, but forcing
12947
   the VMA to 0 here will ensure that relocs against these
12948
   sections are handled correctly.  */
12949
0
      if ((o->flags & SEC_ALLOC) == 0
12950
0
    && ! o->user_set_vma)
12951
0
  o->vma = 0;
12952
0
    }
12953
12954
0
  if (! bfd_link_relocatable (info) && merged)
12955
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12956
12957
  /* Figure out the file positions for everything but the symbol table
12958
     and the relocs.  We set symcount to force assign_section_numbers
12959
     to create a symbol table.  */
12960
0
  abfd->symcount = info->strip != strip_all || emit_relocs;
12961
0
  BFD_ASSERT (! abfd->output_has_begun);
12962
0
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
12963
0
    goto error_return;
12964
12965
  /* Set sizes, and assign file positions for reloc sections.  */
12966
0
  for (o = abfd->sections; o != NULL; o = o->next)
12967
0
    {
12968
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12969
0
      if ((o->flags & SEC_RELOC) != 0)
12970
0
  {
12971
0
    if (esdo->rel.hdr
12972
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12973
0
      goto error_return;
12974
12975
0
    if (esdo->rela.hdr
12976
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12977
0
      goto error_return;
12978
0
  }
12979
12980
      /* _bfd_elf_compute_section_file_positions makes temporary use
12981
   of target_index.  Reset it.  */
12982
0
      o->target_index = 0;
12983
12984
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12985
   to count upwards while actually outputting the relocations.  */
12986
0
      esdo->rel.count = 0;
12987
0
      esdo->rela.count = 0;
12988
12989
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12990
0
    && !bfd_section_is_ctf (o))
12991
0
  {
12992
    /* Cache the section contents so that they can be compressed
12993
       later.  Use bfd_malloc since it will be freed by
12994
       bfd_compress_section_contents.  */
12995
0
    unsigned char *contents = esdo->this_hdr.contents;
12996
0
    if (contents != NULL)
12997
0
      abort ();
12998
0
    contents
12999
0
      = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
13000
0
    if (contents == NULL)
13001
0
      goto error_return;
13002
0
    esdo->this_hdr.contents = contents;
13003
0
  }
13004
0
    }
13005
13006
  /* We have now assigned file positions for all the sections except .symtab,
13007
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
13008
     the .symtab section at the current file position, and write directly to it.
13009
     We build the .strtab section in memory.  */
13010
0
  abfd->symcount = 0;
13011
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13012
  /* sh_name is set in prep_headers.  */
13013
0
  symtab_hdr->sh_type = SHT_SYMTAB;
13014
  /* sh_flags, sh_addr and sh_size all start off zero.  */
13015
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
13016
  /* sh_link is set in assign_section_numbers.  */
13017
  /* sh_info is set below.  */
13018
  /* sh_offset is set just below.  */
13019
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
13020
13021
0
  if (max_sym_count < 20)
13022
0
    max_sym_count = 20;
13023
0
  htab->strtabsize = max_sym_count;
13024
0
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
13025
0
  htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
13026
0
  if (htab->strtab == NULL)
13027
0
    goto error_return;
13028
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
13029
0
  flinfo.symshndxbuf
13030
0
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
13031
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
13032
13033
0
  if (info->strip != strip_all || emit_relocs)
13034
0
    {
13035
0
      file_ptr off = elf_next_file_pos (abfd);
13036
13037
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13038
13039
      /* Note that at this point elf_next_file_pos (abfd) is
13040
   incorrect.  We do not yet know the size of the .symtab section.
13041
   We correct next_file_pos below, after we do know the size.  */
13042
13043
      /* Start writing out the symbol table.  The first symbol is always a
13044
   dummy symbol.  */
13045
0
      elfsym.st_value = 0;
13046
0
      elfsym.st_size = 0;
13047
0
      elfsym.st_info = 0;
13048
0
      elfsym.st_other = 0;
13049
0
      elfsym.st_shndx = SHN_UNDEF;
13050
0
      elfsym.st_target_internal = 0;
13051
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13052
0
             bfd_und_section_ptr, NULL) != 1)
13053
0
  goto error_return;
13054
13055
      /* Output a symbol for each section if asked or they are used for
13056
   relocs.  These symbols usually have no names.  We store the
13057
   index of each one in the index field of the section, so that
13058
   we can find it again when outputting relocs.  */
13059
13060
0
      if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
13061
0
  {
13062
0
    bool name_local_sections
13063
0
      = (bed->elf_backend_name_local_section_symbols
13064
0
         && bed->elf_backend_name_local_section_symbols (abfd));
13065
0
    const char *name = NULL;
13066
13067
0
    elfsym.st_size = 0;
13068
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13069
0
    elfsym.st_other = 0;
13070
0
    elfsym.st_value = 0;
13071
0
    elfsym.st_target_internal = 0;
13072
0
    for (i = 1; i < elf_numsections (abfd); i++)
13073
0
      {
13074
0
        o = bfd_section_from_elf_index (abfd, i);
13075
0
        if (o != NULL)
13076
0
    {
13077
0
      o->target_index = bfd_get_symcount (abfd);
13078
0
      elfsym.st_shndx = i;
13079
0
      if (!bfd_link_relocatable (info))
13080
0
        elfsym.st_value = o->vma;
13081
0
      if (name_local_sections)
13082
0
        name = o->name;
13083
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13084
0
             NULL) != 1)
13085
0
        goto error_return;
13086
0
    }
13087
0
      }
13088
0
  }
13089
0
    }
13090
13091
  /* On some targets like Irix 5 the symbol split between local and global
13092
     ones recorded in the sh_info field needs to be done between section
13093
     and all other symbols.  */
13094
0
  if (bed->elf_backend_elfsym_local_is_section
13095
0
      && bed->elf_backend_elfsym_local_is_section (abfd))
13096
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13097
13098
  /* Allocate some memory to hold information read in from the input
13099
     files.  */
13100
0
  if (max_contents_size != 0)
13101
0
    {
13102
0
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
13103
0
      if (flinfo.contents == NULL)
13104
0
  goto error_return;
13105
0
    }
13106
13107
0
  if (max_external_reloc_size != 0)
13108
0
    {
13109
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13110
0
      if (flinfo.external_relocs == NULL)
13111
0
  goto error_return;
13112
0
    }
13113
13114
0
  if (max_internal_reloc_count != 0)
13115
0
    {
13116
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13117
0
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13118
0
      if (flinfo.internal_relocs == NULL)
13119
0
  goto error_return;
13120
0
    }
13121
13122
0
  if (max_sym_count != 0)
13123
0
    {
13124
0
      amt = max_sym_count * bed->s->sizeof_sym;
13125
0
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13126
0
      if (flinfo.external_syms == NULL)
13127
0
  goto error_return;
13128
13129
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13130
0
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13131
0
      if (flinfo.internal_syms == NULL)
13132
0
  goto error_return;
13133
13134
0
      amt = max_sym_count * sizeof (long);
13135
0
      flinfo.indices = (long int *) bfd_malloc (amt);
13136
0
      if (flinfo.indices == NULL)
13137
0
  goto error_return;
13138
13139
0
      amt = max_sym_count * sizeof (asection *);
13140
0
      flinfo.sections = (asection **) bfd_malloc (amt);
13141
0
      if (flinfo.sections == NULL)
13142
0
  goto error_return;
13143
0
    }
13144
13145
0
  if (max_sym_shndx_count != 0)
13146
0
    {
13147
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13148
0
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13149
0
      if (flinfo.locsym_shndx == NULL)
13150
0
  goto error_return;
13151
0
    }
13152
13153
0
  if (htab->tls_sec)
13154
0
    {
13155
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13156
0
      asection *sec;
13157
13158
0
      for (sec = htab->tls_sec;
13159
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13160
0
     sec = sec->next)
13161
0
  {
13162
0
    bfd_size_type size = sec->size;
13163
0
    unsigned int opb = bfd_octets_per_byte (abfd, sec);
13164
13165
0
    if (size == 0
13166
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13167
0
      {
13168
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13169
13170
0
        if (ord != NULL)
13171
0
    size = ord->offset * opb + ord->size;
13172
0
      }
13173
0
    end = sec->vma + size / opb;
13174
0
  }
13175
0
      base = htab->tls_sec->vma;
13176
      /* Only align end of TLS section if static TLS doesn't have special
13177
   alignment requirements.  */
13178
0
      if (bed->static_tls_alignment == 1)
13179
0
  end = align_power (end, htab->tls_sec->alignment_power);
13180
0
      htab->tls_size = end - base;
13181
0
    }
13182
13183
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13184
0
    return false;
13185
13186
  /* Finish relative relocations here after regular symbol processing
13187
     is finished if DT_RELR is enabled.  */
13188
0
  if (info->enable_dt_relr
13189
0
      && bed->finish_relative_relocs
13190
0
      && !bed->finish_relative_relocs (info))
13191
0
    info->callbacks->fatal
13192
0
      (_("%P: %pB: failed to finish relative relocations\n"), abfd);
13193
13194
  /* Since ELF permits relocations to be against local symbols, we
13195
     must have the local symbols available when we do the relocations.
13196
     Since we would rather only read the local symbols once, and we
13197
     would rather not keep them in memory, we handle all the
13198
     relocations for a single input file at the same time.
13199
13200
     Unfortunately, there is no way to know the total number of local
13201
     symbols until we have seen all of them, and the local symbol
13202
     indices precede the global symbol indices.  This means that when
13203
     we are generating relocatable output, and we see a reloc against
13204
     a global symbol, we can not know the symbol index until we have
13205
     finished examining all the local symbols to see which ones we are
13206
     going to output.  To deal with this, we keep the relocations in
13207
     memory, and don't output them until the end of the link.  This is
13208
     an unfortunate waste of memory, but I don't see a good way around
13209
     it.  Fortunately, it only happens when performing a relocatable
13210
     link, which is not the common case.  FIXME: If keep_memory is set
13211
     we could write the relocs out and then read them again; I don't
13212
     know how bad the memory loss will be.  */
13213
13214
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13215
0
    sub->output_has_begun = false;
13216
0
  for (o = abfd->sections; o != NULL; o = o->next)
13217
0
    {
13218
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13219
0
  {
13220
0
    if (p->type == bfd_indirect_link_order
13221
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13222
0
      == bfd_target_elf_flavour)
13223
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13224
0
      {
13225
0
        if (! sub->output_has_begun)
13226
0
    {
13227
0
      if (! elf_link_input_bfd (&flinfo, sub))
13228
0
        goto error_return;
13229
0
      sub->output_has_begun = true;
13230
0
    }
13231
0
      }
13232
0
    else if (p->type == bfd_section_reloc_link_order
13233
0
       || p->type == bfd_symbol_reloc_link_order)
13234
0
      {
13235
0
        if (! elf_reloc_link_order (abfd, info, o, p))
13236
0
    goto error_return;
13237
0
      }
13238
0
    else
13239
0
      {
13240
0
        if (! _bfd_default_link_order (abfd, info, o, p))
13241
0
    {
13242
0
      if (p->type == bfd_indirect_link_order
13243
0
          && (bfd_get_flavour (sub)
13244
0
        == bfd_target_elf_flavour)
13245
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13246
0
        != bed->s->elfclass))
13247
0
        {
13248
0
          const char *iclass, *oclass;
13249
13250
0
          switch (bed->s->elfclass)
13251
0
      {
13252
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13253
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13254
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13255
0
      default: abort ();
13256
0
      }
13257
13258
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13259
0
      {
13260
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13261
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13262
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13263
0
      default: abort ();
13264
0
      }
13265
13266
0
          bfd_set_error (bfd_error_wrong_format);
13267
0
          _bfd_error_handler
13268
      /* xgettext:c-format */
13269
0
      (_("%pB: file class %s incompatible with %s"),
13270
0
       sub, iclass, oclass);
13271
0
        }
13272
13273
0
      goto error_return;
13274
0
    }
13275
0
      }
13276
0
  }
13277
0
    }
13278
13279
  /* Free symbol buffer if needed.  */
13280
0
  if (!info->reduce_memory_overheads)
13281
0
    {
13282
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13283
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13284
0
    {
13285
0
      free (elf_tdata (sub)->symbuf);
13286
0
      elf_tdata (sub)->symbuf = NULL;
13287
0
    }
13288
0
    }
13289
13290
  /* Output any global symbols that got converted to local in a
13291
     version script or due to symbol visibility.  We do this in a
13292
     separate step since ELF requires all local symbols to appear
13293
     prior to any global symbols.  FIXME: We should only do this if
13294
     some global symbols were, in fact, converted to become local.
13295
     FIXME: Will this work correctly with the Irix 5 linker?  */
13296
0
  eoinfo.failed = false;
13297
0
  eoinfo.flinfo = &flinfo;
13298
0
  eoinfo.localsyms = true;
13299
0
  eoinfo.file_sym_done = false;
13300
  /* Output non-base symbols first.  */
13301
0
  eoinfo.base_symbol = false;
13302
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13303
0
  if (eoinfo.failed)
13304
0
    goto error_return;
13305
13306
  /* If backend needs to output some local symbols not present in the hash
13307
     table, do it now.  */
13308
0
  if (bed->elf_backend_output_arch_local_syms)
13309
0
    {
13310
0
      if (! ((*bed->elf_backend_output_arch_local_syms)
13311
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13312
0
  goto error_return;
13313
0
    }
13314
13315
  /* That wrote out all the local symbols.  Finish up the symbol table
13316
     with the global symbols. Even if we want to strip everything we
13317
     can, we still need to deal with those global symbols that got
13318
     converted to local in a version script.  */
13319
13320
  /* The sh_info field records the index of the first non local symbol.  */
13321
0
  if (!symtab_hdr->sh_info)
13322
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13323
13324
0
  if (dynamic
13325
0
      && htab->dynsym != NULL
13326
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13327
0
    {
13328
0
      Elf_Internal_Sym sym;
13329
0
      bfd_byte *dynsym = htab->dynsym->contents;
13330
13331
0
      o = htab->dynsym->output_section;
13332
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13333
13334
      /* Write out the section symbols for the output sections.  */
13335
0
      if (bfd_link_pic (info)
13336
0
    || htab->is_relocatable_executable)
13337
0
  {
13338
0
    asection *s;
13339
13340
0
    sym.st_size = 0;
13341
0
    sym.st_name = 0;
13342
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13343
0
    sym.st_other = 0;
13344
0
    sym.st_target_internal = 0;
13345
13346
0
    for (s = abfd->sections; s != NULL; s = s->next)
13347
0
      {
13348
0
        int indx;
13349
0
        bfd_byte *dest;
13350
0
        long dynindx;
13351
13352
0
        dynindx = elf_section_data (s)->dynindx;
13353
0
        if (dynindx <= 0)
13354
0
    continue;
13355
0
        indx = elf_section_data (s)->this_idx;
13356
0
        BFD_ASSERT (indx > 0);
13357
0
        sym.st_shndx = indx;
13358
0
        if (! check_dynsym (abfd, &sym))
13359
0
    goto error_return;
13360
0
        sym.st_value = s->vma;
13361
0
        dest = dynsym + dynindx * bed->s->sizeof_sym;
13362
13363
        /* Inform the linker of the addition of this symbol.  */
13364
13365
0
        if (info->callbacks->ctf_new_dynsym)
13366
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13367
13368
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13369
0
      }
13370
0
  }
13371
13372
      /* Write out the local dynsyms.  */
13373
0
      if (htab->dynlocal)
13374
0
  {
13375
0
    struct elf_link_local_dynamic_entry *e;
13376
0
    for (e = htab->dynlocal; e ; e = e->next)
13377
0
      {
13378
0
        asection *s;
13379
0
        bfd_byte *dest;
13380
13381
        /* Copy the internal symbol and turn off visibility.
13382
     Note that we saved a word of storage and overwrote
13383
     the original st_name with the dynstr_index.  */
13384
0
        sym = e->isym;
13385
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13386
0
        sym.st_shndx = SHN_UNDEF;
13387
13388
0
        s = bfd_section_from_elf_index (e->input_bfd,
13389
0
                e->isym.st_shndx);
13390
0
        if (s != NULL
13391
0
      && s->output_section != NULL
13392
0
      && elf_section_data (s->output_section) != NULL)
13393
0
    {
13394
0
      sym.st_shndx =
13395
0
        elf_section_data (s->output_section)->this_idx;
13396
0
      if (! check_dynsym (abfd, &sym))
13397
0
        goto error_return;
13398
0
      sym.st_value = (s->output_section->vma
13399
0
          + s->output_offset
13400
0
          + e->isym.st_value);
13401
0
    }
13402
13403
        /* Inform the linker of the addition of this symbol.  */
13404
13405
0
        if (info->callbacks->ctf_new_dynsym)
13406
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13407
13408
0
        dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13409
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13410
0
      }
13411
0
  }
13412
0
    }
13413
13414
  /* We get the global symbols from the hash table.  */
13415
0
  eoinfo.failed = false;
13416
0
  eoinfo.localsyms = false;
13417
0
  eoinfo.flinfo = &flinfo;
13418
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13419
0
  if (eoinfo.failed)
13420
0
    goto error_return;
13421
13422
0
  if (htab->has_base_symbols)
13423
0
    {
13424
      /* Output base symbols last in DT_HASH so that they will be picked
13425
   before non-base symbols at run-time.  */
13426
0
      eoinfo.base_symbol = true;
13427
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13428
0
       &eoinfo);
13429
0
      if (eoinfo.failed)
13430
0
  goto error_return;
13431
0
    }
13432
13433
  /* If backend needs to output some symbols not present in the hash
13434
     table, do it now.  */
13435
0
  if (bed->elf_backend_output_arch_syms
13436
0
      && (info->strip != strip_all || emit_relocs))
13437
0
    {
13438
0
      if (! ((*bed->elf_backend_output_arch_syms)
13439
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13440
0
  goto error_return;
13441
0
    }
13442
13443
  /* Finalize the .strtab section.  */
13444
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13445
13446
  /* Swap out the .strtab section. */
13447
0
  if (!elf_link_swap_symbols_out (&flinfo))
13448
0
    goto error_return;
13449
0
  free (htab->strtab);
13450
0
  htab->strtab = NULL;
13451
13452
  /* Now we know the size of the symtab section.  */
13453
0
  if (bfd_get_symcount (abfd) > 0)
13454
0
    {
13455
      /* Finish up and write out the symbol string table (.strtab)
13456
   section.  */
13457
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13458
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13459
13460
0
      if (elf_symtab_shndx_list (abfd))
13461
0
  {
13462
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13463
13464
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13465
0
      {
13466
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13467
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13468
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13469
0
        amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13470
0
        symtab_shndx_hdr->sh_size = amt;
13471
13472
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13473
0
                     off, true, 0);
13474
13475
0
        if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13476
0
      || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13477
0
    goto error_return;
13478
0
      }
13479
0
  }
13480
13481
0
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13482
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13483
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13484
0
      symstrtab_hdr->sh_addralign = 1;
13485
13486
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13487
0
                   off, true, 0);
13488
0
      elf_next_file_pos (abfd) = off;
13489
13490
0
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13491
0
    || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13492
0
  goto error_return;
13493
0
    }
13494
13495
0
  if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13496
0
    {
13497
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13498
0
        info->out_implib_bfd);
13499
0
      goto error_return;
13500
0
    }
13501
13502
  /* Adjust the relocs to have the correct symbol indices.  */
13503
0
  for (o = abfd->sections; o != NULL; o = o->next)
13504
0
    {
13505
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13506
0
      bool sort;
13507
13508
0
      if ((o->flags & SEC_RELOC) == 0)
13509
0
  continue;
13510
13511
0
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13512
0
      if (esdo->rel.hdr != NULL
13513
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13514
0
  goto error_return;
13515
0
      if (esdo->rela.hdr != NULL
13516
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13517
0
  goto error_return;
13518
13519
      /* Set the reloc_count field to 0 to prevent write_relocs from
13520
   trying to swap the relocs out itself.  */
13521
0
      o->reloc_count = 0;
13522
0
    }
13523
13524
0
  relativecount = 0;
13525
0
  if (dynamic && info->combreloc && dynobj != NULL)
13526
0
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13527
13528
0
  relr_entsize = 0;
13529
0
  if (htab->srelrdyn != NULL
13530
0
      && htab->srelrdyn->output_section != NULL
13531
0
      && htab->srelrdyn->size != 0)
13532
0
    {
13533
0
      asection *s = htab->srelrdyn->output_section;
13534
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13535
0
      if (relr_entsize == 0)
13536
0
  {
13537
0
    relr_entsize = bed->s->arch_size / 8;
13538
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13539
0
  }
13540
0
    }
13541
13542
  /* If we are linking against a dynamic object, or generating a
13543
     shared library, finish up the dynamic linking information.  */
13544
0
  if (dynamic)
13545
0
    {
13546
0
      bfd_byte *dyncon, *dynconend;
13547
13548
      /* Fix up .dynamic entries.  */
13549
0
      o = htab->dynamic;
13550
0
      BFD_ASSERT (o != NULL);
13551
13552
0
      dyncon = o->contents;
13553
0
      dynconend = PTR_ADD (o->contents, o->size);
13554
0
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13555
0
  {
13556
0
    Elf_Internal_Dyn dyn;
13557
0
    const char *name;
13558
0
    unsigned int type;
13559
0
    bfd_size_type sh_size;
13560
0
    bfd_vma sh_addr;
13561
13562
0
    bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13563
13564
0
    switch (dyn.d_tag)
13565
0
      {
13566
0
      default:
13567
0
        continue;
13568
0
      case DT_NULL:
13569
0
        if (relativecount != 0)
13570
0
    {
13571
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13572
0
        {
13573
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13574
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13575
0
        }
13576
0
      if (dyn.d_tag != DT_NULL
13577
0
          && dynconend - dyncon >= bed->s->sizeof_dyn)
13578
0
        {
13579
0
          dyn.d_un.d_val = relativecount;
13580
0
          relativecount = 0;
13581
0
          break;
13582
0
        }
13583
0
      relativecount = 0;
13584
0
    }
13585
0
        if (relr_entsize != 0)
13586
0
    {
13587
0
      if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13588
0
        {
13589
0
          asection *s = htab->srelrdyn;
13590
0
          dyn.d_tag = DT_RELR;
13591
0
          dyn.d_un.d_ptr
13592
0
      = s->output_section->vma + s->output_offset;
13593
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13594
0
          dyncon += bed->s->sizeof_dyn;
13595
13596
0
          dyn.d_tag = DT_RELRSZ;
13597
0
          dyn.d_un.d_val = s->size;
13598
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13599
0
          dyncon += bed->s->sizeof_dyn;
13600
13601
0
          dyn.d_tag = DT_RELRENT;
13602
0
          dyn.d_un.d_val = relr_entsize;
13603
0
          relr_entsize = 0;
13604
0
          break;
13605
0
        }
13606
0
      relr_entsize = 0;
13607
0
    }
13608
0
        continue;
13609
13610
0
      case DT_INIT:
13611
0
        name = info->init_function;
13612
0
        goto get_sym;
13613
0
      case DT_FINI:
13614
0
        name = info->fini_function;
13615
0
      get_sym:
13616
0
        {
13617
0
    struct elf_link_hash_entry *h;
13618
13619
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13620
0
    if (h != NULL
13621
0
        && (h->root.type == bfd_link_hash_defined
13622
0
      || h->root.type == bfd_link_hash_defweak))
13623
0
      {
13624
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13625
0
        o = h->root.u.def.section;
13626
0
        if (o->output_section != NULL)
13627
0
          dyn.d_un.d_ptr += (o->output_section->vma
13628
0
           + o->output_offset);
13629
0
        else
13630
0
          {
13631
      /* The symbol is imported from another shared
13632
         library and does not apply to this one.  */
13633
0
      dyn.d_un.d_ptr = 0;
13634
0
          }
13635
0
        break;
13636
0
      }
13637
0
        }
13638
0
        continue;
13639
13640
0
      case DT_PREINIT_ARRAYSZ:
13641
0
        name = ".preinit_array";
13642
0
        goto get_out_size;
13643
0
      case DT_INIT_ARRAYSZ:
13644
0
        name = ".init_array";
13645
0
        goto get_out_size;
13646
0
      case DT_FINI_ARRAYSZ:
13647
0
        name = ".fini_array";
13648
0
      get_out_size:
13649
0
        o = bfd_get_section_by_name (abfd, name);
13650
0
        if (o == NULL)
13651
0
    {
13652
0
      _bfd_error_handler
13653
0
        (_("could not find section %s"), name);
13654
0
      goto error_return;
13655
0
    }
13656
0
        if (o->size == 0)
13657
0
    _bfd_error_handler
13658
0
      (_("warning: %s section has zero size"), name);
13659
0
        dyn.d_un.d_val = o->size;
13660
0
        break;
13661
13662
0
      case DT_PREINIT_ARRAY:
13663
0
        name = ".preinit_array";
13664
0
        goto get_out_vma;
13665
0
      case DT_INIT_ARRAY:
13666
0
        name = ".init_array";
13667
0
        goto get_out_vma;
13668
0
      case DT_FINI_ARRAY:
13669
0
        name = ".fini_array";
13670
0
      get_out_vma:
13671
0
        o = bfd_get_section_by_name (abfd, name);
13672
0
        goto do_vma;
13673
13674
0
      case DT_HASH:
13675
0
        name = ".hash";
13676
0
        goto get_vma;
13677
0
      case DT_GNU_HASH:
13678
0
        name = ".gnu.hash";
13679
0
        goto get_vma;
13680
0
      case DT_STRTAB:
13681
0
        name = ".dynstr";
13682
0
        goto get_vma;
13683
0
      case DT_SYMTAB:
13684
0
        name = ".dynsym";
13685
0
        goto get_vma;
13686
0
      case DT_VERDEF:
13687
0
        name = ".gnu.version_d";
13688
0
        goto get_vma;
13689
0
      case DT_VERNEED:
13690
0
        name = ".gnu.version_r";
13691
0
        goto get_vma;
13692
0
      case DT_VERSYM:
13693
0
        name = ".gnu.version";
13694
0
      get_vma:
13695
0
        o = bfd_get_linker_section (dynobj, name);
13696
0
      do_vma:
13697
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13698
0
    {
13699
0
      _bfd_error_handler
13700
0
        (_("could not find section %s"), name);
13701
0
      goto error_return;
13702
0
    }
13703
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13704
0
    {
13705
0
      _bfd_error_handler
13706
0
        (_("warning: section '%s' is being made into a note"), name);
13707
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13708
0
      goto error_return;
13709
0
    }
13710
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13711
0
        break;
13712
13713
0
      case DT_REL:
13714
0
      case DT_RELA:
13715
0
      case DT_RELSZ:
13716
0
      case DT_RELASZ:
13717
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13718
0
    type = SHT_REL;
13719
0
        else
13720
0
    type = SHT_RELA;
13721
0
        sh_size = 0;
13722
0
        sh_addr = 0;
13723
0
        for (i = 1; i < elf_numsections (abfd); i++)
13724
0
    {
13725
0
      Elf_Internal_Shdr *hdr;
13726
13727
0
      hdr = elf_elfsections (abfd)[i];
13728
0
      if (hdr->sh_type == type
13729
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13730
0
        {
13731
0
          sh_size += hdr->sh_size;
13732
0
          if (sh_addr == 0
13733
0
        || sh_addr > hdr->sh_addr)
13734
0
      sh_addr = hdr->sh_addr;
13735
0
        }
13736
0
    }
13737
13738
0
        if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13739
0
    {
13740
0
      unsigned int opb = bfd_octets_per_byte (abfd, o);
13741
13742
      /* Don't count procedure linkage table relocs in the
13743
         overall reloc count.  */
13744
0
      sh_size -= htab->srelplt->size;
13745
0
      if (sh_size == 0)
13746
        /* If the size is zero, make the address zero too.
13747
           This is to avoid a glibc bug.  If the backend
13748
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13749
           zero, then we'll put DT_RELA at the end of
13750
           DT_JMPREL.  glibc will interpret the end of
13751
           DT_RELA matching the end of DT_JMPREL as the
13752
           case where DT_RELA includes DT_JMPREL, and for
13753
           LD_BIND_NOW will decide that processing DT_RELA
13754
           will process the PLT relocs too.  Net result:
13755
           No PLT relocs applied.  */
13756
0
        sh_addr = 0;
13757
13758
      /* If .rela.plt is the first .rela section, exclude
13759
         it from DT_RELA.  */
13760
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13761
0
               + htab->srelplt->output_offset) * opb)
13762
0
        sh_addr += htab->srelplt->size;
13763
0
    }
13764
13765
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13766
0
    dyn.d_un.d_val = sh_size;
13767
0
        else
13768
0
    dyn.d_un.d_ptr = sh_addr;
13769
0
        break;
13770
0
      }
13771
0
    bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13772
0
  }
13773
0
    }
13774
13775
  /* If we have created any dynamic sections, then output them.  */
13776
0
  if (dynobj != NULL)
13777
0
    {
13778
0
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info,
13779
0
               flinfo.contents))
13780
0
  goto error_return;
13781
13782
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13783
0
      if (bfd_link_textrel_check (info)
13784
0
    && (o = htab->dynamic) != NULL
13785
0
    && o->size != 0)
13786
0
  {
13787
0
    bfd_byte *dyncon, *dynconend;
13788
13789
0
    dyncon = o->contents;
13790
0
    dynconend = o->contents + o->size;
13791
0
    for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13792
0
      {
13793
0
        Elf_Internal_Dyn dyn;
13794
13795
0
        bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13796
13797
0
        if (dyn.d_tag == DT_TEXTREL)
13798
0
    {
13799
0
      if (info->textrel_check == textrel_check_error)
13800
0
        info->callbacks->einfo
13801
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13802
0
      else if (bfd_link_dll (info))
13803
0
        info->callbacks->einfo
13804
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13805
0
      else if (bfd_link_pde (info))
13806
0
        info->callbacks->einfo
13807
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13808
0
      else
13809
0
        info->callbacks->einfo
13810
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13811
0
      break;
13812
0
    }
13813
0
      }
13814
0
  }
13815
13816
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13817
0
  {
13818
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13819
0
        || o->size == 0
13820
0
        || o->output_section == bfd_abs_section_ptr)
13821
0
      continue;
13822
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13823
0
      {
13824
        /* At this point, we are only interested in sections
13825
     created by bfd_elf_link_create_dynamic_sections().  */
13826
0
        continue;
13827
0
      }
13828
0
    if (htab->stab_info.stabstr == o)
13829
0
      continue;
13830
0
    if (htab->eh_info.hdr_sec == o)
13831
0
      continue;
13832
0
    if (strcmp (o->name, ".dynstr") != 0)
13833
0
      {
13834
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13835
0
              * bfd_octets_per_byte (abfd, o));
13836
0
        if (!bfd_set_section_contents (abfd, o->output_section,
13837
0
               o->contents, octets, o->size))
13838
0
    goto error_return;
13839
0
      }
13840
0
    else
13841
0
      {
13842
        /* The contents of the .dynstr section are actually in a
13843
     stringtab.  */
13844
0
        file_ptr off;
13845
13846
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13847
0
        if (bfd_seek (abfd, off, SEEK_SET) != 0
13848
0
      || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13849
0
    goto error_return;
13850
0
      }
13851
0
  }
13852
0
    }
13853
13854
0
  if (!info->resolve_section_groups)
13855
0
    {
13856
0
      bool failed = false;
13857
13858
0
      BFD_ASSERT (bfd_link_relocatable (info));
13859
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13860
0
      if (failed)
13861
0
  goto error_return;
13862
0
    }
13863
13864
  /* If we have optimized stabs strings, output them.  */
13865
0
  if (htab->stab_info.stabstr != NULL)
13866
0
    {
13867
0
      if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13868
0
  goto error_return;
13869
0
    }
13870
13871
0
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13872
0
    goto error_return;
13873
13874
0
  if (! _bfd_elf_write_section_sframe (abfd, info))
13875
0
    goto error_return;
13876
13877
0
  if (! _bfd_elf_write_section_object_attributes (abfd, info))
13878
0
    goto error_ret2;
13879
13880
0
  if (info->callbacks->emit_ctf)
13881
0
      info->callbacks->emit_ctf ();
13882
13883
0
  elf_final_link_free (abfd, &flinfo);
13884
13885
0
  if (info->unique_symbol)
13886
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13887
0
  return true;
13888
13889
0
 error_return:
13890
0
  free (htab->strtab);
13891
0
  htab->strtab = NULL;
13892
0
  elf_final_link_free (abfd, &flinfo);
13893
0
 error_ret2:
13894
0
  if (info->unique_symbol)
13895
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13896
0
  return false;
13897
0
}
13898

13899
/* Initialize COOKIE for input bfd ABFD.  */
13900
13901
static bool
13902
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13903
0
{
13904
0
  Elf_Internal_Shdr *symtab_hdr;
13905
0
  elf_backend_data *bed;
13906
13907
0
  bed = get_elf_backend_data (abfd);
13908
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13909
13910
0
  cookie->abfd = abfd;
13911
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13912
0
  if (elf_bad_symtab (abfd))
13913
0
    {
13914
0
      cookie->locsymcount = cookie->num_sym;
13915
0
      cookie->extsymoff = 0;
13916
0
    }
13917
0
  else
13918
0
    {
13919
0
      cookie->locsymcount = symtab_hdr->sh_info;
13920
0
      cookie->extsymoff = symtab_hdr->sh_info;
13921
0
    }
13922
13923
0
  if (bed->s->arch_size == 32)
13924
0
    cookie->r_sym_shift = 8;
13925
0
  else
13926
0
    cookie->r_sym_shift = 32;
13927
13928
0
  return true;
13929
0
}
13930
13931
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13932
13933
static void
13934
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13935
       bfd *abfd ATTRIBUTE_UNUSED)
13936
0
{
13937
0
}
13938
13939
/* Initialize the relocation information in COOKIE for input section SEC
13940
   of input bfd ABFD.  */
13941
13942
static bool
13943
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13944
      struct bfd_link_info *info, bfd *abfd,
13945
      asection *sec, bool keep_memory)
13946
0
{
13947
0
  if (sec->reloc_count == 0)
13948
0
    {
13949
0
      cookie->rels = NULL;
13950
0
      cookie->relend = NULL;
13951
0
    }
13952
0
  else
13953
0
    {
13954
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13955
0
  (abfd, info, sec, NULL, NULL,
13956
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13957
0
      if (cookie->rels == NULL)
13958
0
  return false;
13959
0
      cookie->rel = cookie->rels;
13960
0
      cookie->relend = cookie->rels + sec->reloc_count;
13961
0
    }
13962
0
  cookie->rel = cookie->rels;
13963
0
  return true;
13964
0
}
13965
13966
/* Free the memory allocated by init_reloc_cookie_rels,
13967
   if appropriate.  */
13968
13969
static void
13970
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13971
      asection *sec)
13972
0
{
13973
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13974
0
    free (cookie->rels);
13975
0
}
13976
13977
/* Initialize the whole of COOKIE for input section SEC.  */
13978
13979
static bool
13980
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13981
             struct bfd_link_info *info,
13982
             asection *sec, bool keep_memory)
13983
0
{
13984
0
  if (!init_reloc_cookie (cookie, sec->owner))
13985
0
    goto error1;
13986
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13987
0
             keep_memory))
13988
0
    goto error2;
13989
0
  return true;
13990
13991
0
 error2:
13992
0
  fini_reloc_cookie (cookie, sec->owner);
13993
0
 error1:
13994
0
  return false;
13995
0
}
13996
13997
/* Free the memory allocated by init_reloc_cookie_for_section,
13998
   if appropriate.  */
13999
14000
static void
14001
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14002
             asection *sec)
14003
0
{
14004
0
  fini_reloc_cookie_rels (cookie, sec);
14005
0
  fini_reloc_cookie (cookie, sec->owner);
14006
0
}
14007

14008
/* Garbage collect unused sections.  */
14009
14010
/* Default gc_mark_hook.  */
14011
14012
asection *
14013
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
14014
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
14015
           struct elf_reloc_cookie *cookie,
14016
           struct elf_link_hash_entry *h,
14017
           unsigned int symndx)
14018
0
{
14019
0
  if (h == NULL)
14020
0
    return _bfd_get_local_sym_section (cookie, symndx);
14021
14022
0
  switch (h->root.type)
14023
0
    {
14024
0
    case bfd_link_hash_defined:
14025
0
    case bfd_link_hash_defweak:
14026
0
      return h->root.u.def.section;
14027
14028
0
    case bfd_link_hash_common:
14029
0
      return h->root.u.c.p->section;
14030
14031
0
    default:
14032
0
      return NULL;
14033
0
    }
14034
0
}
14035
14036
/* Return the debug definition section.  */
14037
14038
static asection *
14039
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14040
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14041
         struct elf_reloc_cookie *cookie,
14042
         struct elf_link_hash_entry *h,
14043
         unsigned int symndx)
14044
0
{
14045
0
  if (h != NULL)
14046
0
    {
14047
      /* Return the global debug definition section.  */
14048
0
      if ((h->root.type == bfd_link_hash_defined
14049
0
     || h->root.type == bfd_link_hash_defweak)
14050
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14051
0
  return h->root.u.def.section;
14052
0
    }
14053
0
  else
14054
0
    {
14055
      /* Return the local debug definition section.  */
14056
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14057
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14058
0
  return isec;
14059
0
    }
14060
14061
0
  return NULL;
14062
0
}
14063
14064
/* COOKIE->rel describes a relocation against section SEC, which is
14065
   a section we've decided to keep.  Return the section that contains
14066
   the relocation symbol, or NULL if no section contains it.  */
14067
14068
asection *
14069
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14070
           elf_gc_mark_hook_fn gc_mark_hook,
14071
           struct elf_reloc_cookie *cookie,
14072
           bool *start_stop)
14073
0
{
14074
0
  unsigned long r_symndx;
14075
0
  struct elf_link_hash_entry *h, *hw;
14076
14077
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14078
0
  if (r_symndx == STN_UNDEF)
14079
0
    return NULL;
14080
14081
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14082
0
  if (h == NULL)
14083
0
    {
14084
      /* A corrupt input file can lead to a situation where the index
14085
   does not reference either a local or an external symbol.  */
14086
0
      if (r_symndx >= cookie->locsymcount)
14087
0
  return NULL;
14088
14089
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14090
0
    }
14091
14092
0
  bool was_marked = h->mark;
14093
14094
0
  h->mark = 1;
14095
  /* Keep all aliases of the symbol too.  If an object symbol
14096
     needs to be copied into .dynbss then all of its aliases
14097
     should be present as dynamic symbols, not just the one used
14098
     on the copy relocation.  */
14099
0
  hw = h;
14100
0
  while (hw->is_weakalias)
14101
0
    {
14102
0
      hw = hw->u.alias;
14103
0
      hw->mark = 1;
14104
0
    }
14105
14106
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14107
0
    {
14108
0
      if (info->start_stop_gc)
14109
0
  return NULL;
14110
14111
      /* To work around a glibc bug, mark XXX input sections
14112
   when there is a reference to __start_XXX or __stop_XXX
14113
   symbols.  */
14114
0
      else if (start_stop != NULL)
14115
0
  {
14116
0
    asection *s = h->u2.start_stop_section;
14117
0
    *start_stop = true;
14118
0
    return s;
14119
0
  }
14120
0
    }
14121
14122
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14123
0
}
14124
14125
/* COOKIE->rel describes a relocation against section SEC, which is
14126
   a section we've decided to keep.  Mark the section that contains
14127
   the relocation symbol.  */
14128
14129
bool
14130
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14131
      asection *sec,
14132
      elf_gc_mark_hook_fn gc_mark_hook,
14133
      struct elf_reloc_cookie *cookie)
14134
0
{
14135
0
  asection *rsec;
14136
0
  bool start_stop = false;
14137
14138
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14139
0
  while (rsec != NULL)
14140
0
    {
14141
0
      if (!rsec->gc_mark)
14142
0
  {
14143
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14144
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14145
0
      rsec->gc_mark = 1;
14146
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14147
0
      return false;
14148
0
  }
14149
0
      if (!start_stop)
14150
0
  break;
14151
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14152
0
    }
14153
0
  return true;
14154
0
}
14155
14156
/* The mark phase of garbage collection.  For a given section, mark
14157
   it and any sections in this section's group, and all the sections
14158
   which define symbols to which it refers.  */
14159
14160
bool
14161
_bfd_elf_gc_mark (struct bfd_link_info *info,
14162
      asection *sec,
14163
      elf_gc_mark_hook_fn gc_mark_hook)
14164
0
{
14165
0
  bool ret;
14166
0
  asection *group_sec, *eh_frame, *sframe;
14167
14168
0
  sec->gc_mark = 1;
14169
14170
  /* Mark all the sections in the group.  */
14171
0
  group_sec = elf_section_data (sec)->next_in_group;
14172
0
  if (group_sec && !group_sec->gc_mark)
14173
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14174
0
      return false;
14175
14176
  /* Look through the section relocs.  */
14177
0
  ret = true;
14178
0
  eh_frame = elf_eh_frame_section (sec->owner);
14179
0
  sframe = elf_sframe_section (sec->owner);
14180
14181
0
  if ((sec->flags & SEC_RELOC) != 0
14182
0
      && sec->reloc_count > 0
14183
0
      && sec != eh_frame
14184
0
      && sec != sframe)
14185
0
    {
14186
0
      struct elf_reloc_cookie cookie;
14187
14188
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14189
0
  ret = false;
14190
0
      else
14191
0
  {
14192
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14193
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14194
0
        {
14195
0
    ret = false;
14196
0
    break;
14197
0
        }
14198
0
    fini_reloc_cookie_for_section (&cookie, sec);
14199
0
  }
14200
0
    }
14201
14202
0
  if (ret && eh_frame && elf_fde_list (sec))
14203
0
    {
14204
0
      struct elf_reloc_cookie cookie;
14205
14206
      /* NB: When --no-keep-memory is used, the symbol table and
14207
   relocation info for eh_frame are freed after they are retrieved
14208
   for each text section in the input object.  If an input object
14209
   has many text sections, the same data is retrieved and freed
14210
   many times which can take a very long time.  Always keep the
14211
   symbol table and relocation info for eh_frame to avoid it.  */
14212
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14213
0
            true))
14214
0
  ret = false;
14215
0
      else
14216
0
  {
14217
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14218
0
              gc_mark_hook, &cookie))
14219
0
      ret = false;
14220
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14221
0
  }
14222
0
    }
14223
14224
0
  eh_frame = elf_section_eh_frame_entry (sec);
14225
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14226
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14227
0
      ret = false;
14228
14229
0
  return ret;
14230
0
}
14231
14232
/* Scan and mark sections in a special or debug section group.  */
14233
14234
static void
14235
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14236
0
{
14237
  /* Point to first section of section group.  */
14238
0
  asection *ssec;
14239
  /* Used to iterate the section group.  */
14240
0
  asection *msec;
14241
14242
0
  bool is_special_grp = true;
14243
0
  bool is_debug_grp = true;
14244
14245
  /* First scan to see if group contains any section other than debug
14246
     and special section.  */
14247
0
  ssec = msec = elf_next_in_group (grp);
14248
0
  while (msec != NULL)
14249
0
    {
14250
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14251
0
  is_debug_grp = false;
14252
14253
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14254
0
  is_special_grp = false;
14255
14256
0
      msec = elf_next_in_group (msec);
14257
0
      if (msec == ssec)
14258
0
  break;
14259
0
    }
14260
14261
  /* If this is a pure debug section group or pure special section group,
14262
     keep all sections in this group.  */
14263
0
  if (is_debug_grp || is_special_grp)
14264
0
    {
14265
0
      msec = ssec;
14266
0
      while (msec != NULL)
14267
0
  {
14268
0
    msec->gc_mark = 1;
14269
0
    msec = elf_next_in_group (msec);
14270
0
    if (msec == ssec)
14271
0
      break;
14272
0
  }
14273
0
    }
14274
0
}
14275
14276
/* Keep debug and special sections.  */
14277
14278
bool
14279
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14280
         elf_gc_mark_hook_fn mark_hook)
14281
0
{
14282
0
  bfd *ibfd;
14283
14284
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14285
0
    {
14286
0
      asection *isec;
14287
0
      bool some_kept;
14288
0
      bool debug_frag_seen;
14289
0
      bool has_kept_debug_info;
14290
14291
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14292
0
  continue;
14293
0
      isec = ibfd->sections;
14294
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14295
0
  continue;
14296
14297
      /* Ensure all linker created sections are kept,
14298
   see if any other section is already marked,
14299
   and note if we have any fragmented debug sections.  */
14300
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14301
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14302
0
  {
14303
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14304
0
      isec->gc_mark = 1;
14305
0
    else if (isec->gc_mark
14306
0
       && (isec->flags & SEC_ALLOC) != 0
14307
0
       && elf_section_type (isec) != SHT_NOTE)
14308
0
      some_kept = true;
14309
0
    else
14310
0
      {
14311
        /* Since all sections, except for backend specific ones,
14312
     have been garbage collected, call mark_hook on this
14313
     section if any of its linked-to sections is marked.  */
14314
0
        asection *linked_to_sec;
14315
0
        for (linked_to_sec = elf_linked_to_section (isec);
14316
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14317
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14318
0
    {
14319
0
      if (linked_to_sec->gc_mark)
14320
0
        {
14321
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14322
0
      return false;
14323
0
          break;
14324
0
        }
14325
0
      linked_to_sec->linker_mark = 1;
14326
0
    }
14327
0
        for (linked_to_sec = elf_linked_to_section (isec);
14328
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14329
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14330
0
    linked_to_sec->linker_mark = 0;
14331
0
      }
14332
14333
0
    if (!debug_frag_seen
14334
0
        && (isec->flags & SEC_DEBUGGING)
14335
0
        && startswith (isec->name, ".debug_line."))
14336
0
      debug_frag_seen = true;
14337
0
    else if (strcmp (bfd_section_name (isec),
14338
0
         "__patchable_function_entries") == 0
14339
0
       && elf_linked_to_section (isec) == NULL)
14340
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14341
0
          "need linked-to section "
14342
0
          "for --gc-sections\n"),
14343
0
              isec->owner, isec);
14344
0
  }
14345
14346
      /* If no non-note alloc section in this file will be kept, then
14347
   we can toss out the debug and special sections.  */
14348
0
      if (!some_kept)
14349
0
  continue;
14350
14351
      /* Keep debug and special sections like .comment when they are
14352
   not part of a group.  Also keep section groups that contain
14353
   just debug sections or special sections.  NB: Sections with
14354
   linked-to section has been handled above.  */
14355
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14356
0
  {
14357
0
    if ((isec->flags & SEC_GROUP) != 0)
14358
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14359
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14360
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14361
0
       && elf_next_in_group (isec) == NULL
14362
0
       && elf_linked_to_section (isec) == NULL)
14363
0
      isec->gc_mark = 1;
14364
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14365
0
      has_kept_debug_info = true;
14366
0
  }
14367
14368
      /* Look for CODE sections which are going to be discarded,
14369
   and find and discard any fragmented debug sections which
14370
   are associated with that code section.  */
14371
0
      if (debug_frag_seen)
14372
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14373
0
    if ((isec->flags & SEC_CODE) != 0
14374
0
        && isec->gc_mark == 0)
14375
0
      {
14376
0
        unsigned int ilen;
14377
0
        asection *dsec;
14378
14379
0
        ilen = strlen (isec->name);
14380
14381
        /* Association is determined by the name of the debug
14382
     section containing the name of the code section as
14383
     a suffix.  For example .debug_line.text.foo is a
14384
     debug section associated with .text.foo.  */
14385
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14386
0
    {
14387
0
      unsigned int dlen;
14388
14389
0
      if (dsec->gc_mark == 0
14390
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14391
0
        continue;
14392
14393
0
      dlen = strlen (dsec->name);
14394
14395
0
      if (dlen > ilen
14396
0
          && strncmp (dsec->name + (dlen - ilen),
14397
0
          isec->name, ilen) == 0)
14398
0
        dsec->gc_mark = 0;
14399
0
    }
14400
0
    }
14401
14402
      /* Mark debug sections referenced by kept debug sections.  */
14403
0
      if (has_kept_debug_info)
14404
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14405
0
    if (isec->gc_mark
14406
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14407
0
      if (!_bfd_elf_gc_mark (info, isec,
14408
0
           elf_gc_mark_debug_section))
14409
0
        return false;
14410
0
    }
14411
0
  return true;
14412
0
}
14413
14414
static bool
14415
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14416
0
{
14417
0
  bfd *sub;
14418
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14419
14420
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14421
0
    {
14422
0
      asection *o;
14423
14424
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14425
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14426
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14427
0
  continue;
14428
0
      o = sub->sections;
14429
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14430
0
  continue;
14431
14432
0
      for (o = sub->sections; o != NULL; o = o->next)
14433
0
  {
14434
    /* When any section in a section group is kept, we keep all
14435
       sections in the section group.  If the first member of
14436
       the section group is excluded, we will also exclude the
14437
       group section.  */
14438
0
    if (o->flags & SEC_GROUP)
14439
0
      {
14440
0
        asection *first = elf_next_in_group (o);
14441
0
        if (first != NULL)
14442
0
    o->gc_mark = first->gc_mark;
14443
0
      }
14444
14445
0
    if (o->gc_mark)
14446
0
      continue;
14447
14448
    /* Skip sweeping sections already excluded.  */
14449
0
    if (o->flags & SEC_EXCLUDE)
14450
0
      continue;
14451
14452
    /* Since this is early in the link process, it is simple
14453
       to remove a section from the output.  */
14454
0
    o->flags |= SEC_EXCLUDE;
14455
14456
0
    if (info->print_gc_sections && o->size != 0)
14457
      /* xgettext:c-format */
14458
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14459
0
        o, sub);
14460
0
  }
14461
0
    }
14462
14463
0
  return true;
14464
0
}
14465
14466
/* Propagate collected vtable information.  This is called through
14467
   elf_link_hash_traverse.  */
14468
14469
static bool
14470
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14471
0
{
14472
  /* Those that are not vtables.  */
14473
0
  if (h->start_stop
14474
0
      || h->u2.vtable == NULL
14475
0
      || h->u2.vtable->parent == NULL)
14476
0
    return true;
14477
14478
  /* Those vtables that do not have parents, we cannot merge.  */
14479
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14480
0
    return true;
14481
14482
  /* If we've already been done, exit.  */
14483
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14484
0
    return true;
14485
14486
  /* Make sure the parent's table is up to date.  */
14487
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14488
14489
0
  if (h->u2.vtable->used == NULL)
14490
0
    {
14491
      /* None of this table's entries were referenced.  Re-use the
14492
   parent's table.  */
14493
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14494
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14495
0
    }
14496
0
  else
14497
0
    {
14498
0
      size_t n;
14499
0
      bool *cu, *pu;
14500
14501
      /* Or the parent's entries into ours.  */
14502
0
      cu = h->u2.vtable->used;
14503
0
      cu[-1] = true;
14504
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14505
0
      if (pu != NULL)
14506
0
  {
14507
0
    elf_backend_data *bed;
14508
0
    unsigned int log_file_align;
14509
14510
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14511
0
    log_file_align = bed->s->log_file_align;
14512
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14513
0
    while (n--)
14514
0
      {
14515
0
        if (*pu)
14516
0
    *cu = true;
14517
0
        pu++;
14518
0
        cu++;
14519
0
      }
14520
0
  }
14521
0
    }
14522
14523
0
  return true;
14524
0
}
14525
14526
struct link_info_ok
14527
{
14528
  struct bfd_link_info *info;
14529
  bool ok;
14530
};
14531
14532
static bool
14533
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14534
            void *ptr)
14535
0
{
14536
0
  asection *sec;
14537
0
  bfd_vma hstart, hend;
14538
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14539
0
  elf_backend_data *bed;
14540
0
  unsigned int log_file_align;
14541
0
  struct link_info_ok *info = (struct link_info_ok *) ptr;
14542
14543
  /* Take care of both those symbols that do not describe vtables as
14544
     well as those that are not loaded.  */
14545
0
  if (h->start_stop
14546
0
      || h->u2.vtable == NULL
14547
0
      || h->u2.vtable->parent == NULL)
14548
0
    return true;
14549
14550
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14551
0
        || h->root.type == bfd_link_hash_defweak);
14552
14553
0
  sec = h->root.u.def.section;
14554
0
  hstart = h->root.u.def.value;
14555
0
  hend = hstart + h->size;
14556
14557
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14558
0
               sec, NULL, NULL, true);
14559
0
  if (!relstart)
14560
0
    return info->ok = false;
14561
0
  bed = get_elf_backend_data (sec->owner);
14562
0
  log_file_align = bed->s->log_file_align;
14563
14564
0
  relend = relstart + sec->reloc_count;
14565
14566
0
  for (rel = relstart; rel < relend; ++rel)
14567
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14568
0
      {
14569
  /* If the entry is in use, do nothing.  */
14570
0
  if (h->u2.vtable->used
14571
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14572
0
    {
14573
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14574
0
      if (h->u2.vtable->used[entry])
14575
0
        continue;
14576
0
    }
14577
  /* Otherwise, kill it.  */
14578
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14579
0
      }
14580
14581
0
  return true;
14582
0
}
14583
14584
/* Mark sections containing dynamically referenced symbols.  When
14585
   building shared libraries, we must assume that any visible symbol is
14586
   referenced.  */
14587
14588
bool
14589
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14590
0
{
14591
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
14592
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14593
14594
0
  if ((h->root.type == bfd_link_hash_defined
14595
0
       || h->root.type == bfd_link_hash_defweak)
14596
0
      && (!h->start_stop
14597
0
    || h->root.ldscript_def
14598
0
    || !info->start_stop_gc)
14599
0
      && ((h->ref_dynamic && !h->forced_local)
14600
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14601
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14602
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14603
0
        && (!bfd_link_executable (info)
14604
0
      || info->gc_keep_exported
14605
0
      || info->export_dynamic
14606
0
      || (h->dynamic
14607
0
          && d != NULL
14608
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14609
0
        && (h->versioned >= versioned
14610
0
      || !bfd_hide_sym_by_version (info->version_info,
14611
0
                 h->root.root.string)))))
14612
0
    h->root.u.def.section->flags |= SEC_KEEP;
14613
14614
0
  return true;
14615
0
}
14616
14617
/* Keep all sections containing symbols undefined on the command-line,
14618
   and the section containing the entry symbol.  */
14619
14620
void
14621
_bfd_elf_gc_keep (struct bfd_link_info *info)
14622
0
{
14623
0
  struct bfd_sym_chain *sym;
14624
14625
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14626
0
    {
14627
0
      struct elf_link_hash_entry *h;
14628
14629
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14630
0
        false, false, false);
14631
14632
0
      if (h != NULL
14633
0
    && (h->root.type == bfd_link_hash_defined
14634
0
        || h->root.type == bfd_link_hash_defweak)
14635
0
    && !bfd_is_const_section (h->root.u.def.section))
14636
0
  h->root.u.def.section->flags |= SEC_KEEP;
14637
0
    }
14638
0
}
14639
14640
bool
14641
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14642
        struct bfd_link_info *info)
14643
0
{
14644
0
  bfd *ibfd = info->input_bfds;
14645
14646
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14647
0
    {
14648
0
      asection *sec;
14649
0
      struct elf_reloc_cookie cookie;
14650
14651
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14652
0
  continue;
14653
0
      sec = ibfd->sections;
14654
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14655
0
  continue;
14656
14657
0
      if (!init_reloc_cookie (&cookie, ibfd))
14658
0
  return false;
14659
14660
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14661
0
  {
14662
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14663
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14664
0
           false))
14665
0
      {
14666
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14667
0
        fini_reloc_cookie_rels (&cookie, sec);
14668
0
      }
14669
0
  }
14670
0
    }
14671
0
  return true;
14672
0
}
14673
14674
/* Do mark and sweep of unused sections.  */
14675
14676
bool
14677
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14678
0
{
14679
0
  bool ok = true;
14680
0
  bfd *sub;
14681
0
  elf_gc_mark_hook_fn gc_mark_hook;
14682
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14683
0
  struct elf_link_hash_table *htab;
14684
0
  struct link_info_ok info_ok;
14685
14686
0
  if (!bed->can_gc_sections
14687
0
      || !is_elf_hash_table (info->hash))
14688
0
    {
14689
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14690
0
      return true;
14691
0
    }
14692
14693
0
  bed->gc_keep (info);
14694
0
  htab = elf_hash_table (info);
14695
14696
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14697
     at the .eh_frame section if we can mark the FDEs individually.  */
14698
0
  for (sub = info->input_bfds;
14699
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14700
0
       sub = sub->link.next)
14701
0
    {
14702
0
      asection *sec;
14703
0
      struct elf_reloc_cookie cookie;
14704
14705
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14706
0
  continue;
14707
0
      sec = sub->sections;
14708
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14709
0
  continue;
14710
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14711
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14712
0
               false))
14713
0
  {
14714
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14715
0
    if (sec->sec_info
14716
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14717
0
      elf_eh_frame_section (sub) = sec;
14718
0
    fini_reloc_cookie_for_section (&cookie, sec);
14719
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14720
0
  }
14721
14722
      /* Handle .sframe section.  */
14723
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14724
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14725
0
               false))
14726
0
  {
14727
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14728
14729
0
    if (sec->sec_info
14730
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14731
0
      elf_sframe_section (sub) = sec;
14732
14733
0
    fini_reloc_cookie_for_section (&cookie, sec);
14734
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14735
0
  }
14736
0
    }
14737
14738
  /* Apply transitive closure to the vtable entry usage info.  */
14739
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14740
0
  if (!ok)
14741
0
    return false;
14742
14743
  /* Kill the vtable relocations that were not used.  */
14744
0
  info_ok.info = info;
14745
0
  info_ok.ok = true;
14746
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14747
0
  if (!info_ok.ok)
14748
0
    return false;
14749
14750
  /* Mark dynamically referenced symbols.  */
14751
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14752
0
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14753
14754
  /* Grovel through relocs to find out who stays ...  */
14755
0
  gc_mark_hook = bed->gc_mark_hook;
14756
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14757
0
    {
14758
0
      asection *o;
14759
14760
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14761
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14762
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14763
0
  continue;
14764
14765
0
      o = sub->sections;
14766
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14767
0
  continue;
14768
14769
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14770
   Also treat note sections as a root, if the section is not part
14771
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14772
   well as FINI_ARRAY sections for ld -r.  */
14773
0
      for (o = sub->sections; o != NULL; o = o->next)
14774
0
  if (!o->gc_mark
14775
0
      && (o->flags & SEC_EXCLUDE) == 0
14776
0
      && ((o->flags & SEC_KEEP) != 0
14777
0
    || (bfd_link_relocatable (info)
14778
0
        && ((elf_section_data (o)->this_hdr.sh_type
14779
0
       == SHT_PREINIT_ARRAY)
14780
0
      || (elf_section_data (o)->this_hdr.sh_type
14781
0
          == SHT_INIT_ARRAY)
14782
0
      || (elf_section_data (o)->this_hdr.sh_type
14783
0
          == SHT_FINI_ARRAY)))
14784
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14785
0
        && elf_next_in_group (o) == NULL
14786
0
        && elf_linked_to_section (o) == NULL)
14787
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14788
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14789
0
    {
14790
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14791
0
        return false;
14792
0
    }
14793
0
    }
14794
14795
  /* Allow the backend to mark additional target specific sections.  */
14796
0
  bed->gc_mark_extra_sections (info, gc_mark_hook);
14797
14798
  /* ... and mark SEC_EXCLUDE for those that go.  */
14799
0
  return elf_gc_sweep (abfd, info);
14800
0
}
14801

14802
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14803
14804
bool
14805
bfd_elf_gc_record_vtinherit (bfd *abfd,
14806
           asection *sec,
14807
           struct elf_link_hash_entry *h,
14808
           bfd_vma offset)
14809
0
{
14810
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14811
0
  struct elf_link_hash_entry **search, *child;
14812
0
  size_t extsymcount;
14813
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14814
14815
  /* The sh_info field of the symtab header tells us where the
14816
     external symbols start.  We don't care about the local symbols at
14817
     this point.  */
14818
0
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14819
0
  if (!elf_bad_symtab (abfd))
14820
0
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14821
14822
0
  sym_hashes = elf_sym_hashes (abfd);
14823
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14824
14825
  /* Hunt down the child symbol, which is in this section at the same
14826
     offset as the relocation.  */
14827
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14828
0
    {
14829
0
      if ((child = *search) != NULL
14830
0
    && (child->root.type == bfd_link_hash_defined
14831
0
        || child->root.type == bfd_link_hash_defweak)
14832
0
    && child->root.u.def.section == sec
14833
0
    && child->root.u.def.value == offset)
14834
0
  goto win;
14835
0
    }
14836
14837
  /* xgettext:c-format */
14838
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14839
0
          abfd, sec, (uint64_t) offset);
14840
0
  bfd_set_error (bfd_error_invalid_operation);
14841
0
  return false;
14842
14843
0
 win:
14844
0
  if (!child->u2.vtable)
14845
0
    {
14846
0
      child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14847
0
        bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14848
0
      if (!child->u2.vtable)
14849
0
  return false;
14850
0
    }
14851
0
  if (!h)
14852
0
    {
14853
      /* This *should* only be the absolute section.  It could potentially
14854
   be that someone has defined a non-global vtable though, which
14855
   would be bad.  It isn't worth paging in the local symbols to be
14856
   sure though; that case should simply be handled by the assembler.  */
14857
14858
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14859
0
    }
14860
0
  else
14861
0
    child->u2.vtable->parent = h;
14862
14863
0
  return true;
14864
0
}
14865
14866
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14867
14868
bool
14869
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14870
         struct elf_link_hash_entry *h,
14871
         bfd_vma addend)
14872
0
{
14873
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14874
0
  unsigned int log_file_align = bed->s->log_file_align;
14875
14876
0
  if (!h || addend > 1u << 28)
14877
0
    {
14878
      /* xgettext:c-format */
14879
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14880
0
        abfd, sec);
14881
0
      bfd_set_error (bfd_error_bad_value);
14882
0
      return false;
14883
0
    }
14884
14885
0
  if (!h->u2.vtable)
14886
0
    {
14887
0
      h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14888
0
          bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14889
0
      if (!h->u2.vtable)
14890
0
  return false;
14891
0
    }
14892
14893
0
  if (addend >= h->u2.vtable->size)
14894
0
    {
14895
0
      size_t size, bytes, file_align;
14896
0
      bool *ptr = h->u2.vtable->used;
14897
14898
      /* While the symbol is undefined, we have to be prepared to handle
14899
   a zero size.  */
14900
0
      file_align = 1 << log_file_align;
14901
0
      if (h->root.type == bfd_link_hash_undefined)
14902
0
  size = addend + file_align;
14903
0
      else
14904
0
  {
14905
0
    size = h->size;
14906
0
    if (addend >= size)
14907
0
      {
14908
        /* Oops!  We've got a reference past the defined end of
14909
     the table.  This is probably a bug -- shall we warn?  */
14910
0
        size = addend + file_align;
14911
0
      }
14912
0
  }
14913
0
      size = (size + file_align - 1) & -file_align;
14914
14915
      /* Allocate one extra entry for use as a "done" flag for the
14916
   consolidation pass.  */
14917
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14918
14919
0
      if (ptr)
14920
0
  {
14921
0
    ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14922
14923
0
    if (ptr != NULL)
14924
0
      {
14925
0
        size_t oldbytes;
14926
14927
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14928
0
        * sizeof (bool));
14929
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14930
0
      }
14931
0
  }
14932
0
      else
14933
0
  ptr = (bool *) bfd_zmalloc (bytes);
14934
14935
0
      if (ptr == NULL)
14936
0
  return false;
14937
14938
      /* And arrange for that done flag to be at index -1.  */
14939
0
      h->u2.vtable->used = ptr + 1;
14940
0
      h->u2.vtable->size = size;
14941
0
    }
14942
14943
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14944
14945
0
  return true;
14946
0
}
14947
14948
/* Map an ELF section header flag to its corresponding string.  */
14949
typedef struct
14950
{
14951
  char *flag_name;
14952
  flagword flag_value;
14953
} elf_flags_to_name_table;
14954
14955
static const elf_flags_to_name_table elf_flags_to_names [] =
14956
{
14957
  { "SHF_WRITE", SHF_WRITE },
14958
  { "SHF_ALLOC", SHF_ALLOC },
14959
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14960
  { "SHF_MERGE", SHF_MERGE },
14961
  { "SHF_STRINGS", SHF_STRINGS },
14962
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14963
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14964
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14965
  { "SHF_GROUP", SHF_GROUP },
14966
  { "SHF_TLS", SHF_TLS },
14967
  { "SHF_MASKOS", SHF_MASKOS },
14968
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14969
};
14970
14971
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14972
bool
14973
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14974
            struct flag_info *flaginfo,
14975
            asection *section)
14976
0
{
14977
0
  const bfd_vma sh_flags = elf_section_flags (section);
14978
14979
0
  if (!flaginfo->flags_initialized)
14980
0
    {
14981
0
      bfd *obfd = info->output_bfd;
14982
0
      elf_backend_data *bed = get_elf_backend_data (obfd);
14983
0
      struct flag_info_list *tf = flaginfo->flag_list;
14984
0
      int with_hex = 0;
14985
0
      int without_hex = 0;
14986
14987
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14988
0
  {
14989
0
    unsigned i;
14990
0
    flagword (*lookup) (char *);
14991
14992
0
    lookup = bed->elf_backend_lookup_section_flags_hook;
14993
0
    if (lookup != NULL)
14994
0
      {
14995
0
        flagword hexval = (*lookup) ((char *) tf->name);
14996
14997
0
        if (hexval != 0)
14998
0
    {
14999
0
      if (tf->with == with_flags)
15000
0
        with_hex |= hexval;
15001
0
      else if (tf->with == without_flags)
15002
0
        without_hex |= hexval;
15003
0
      tf->valid = true;
15004
0
      continue;
15005
0
    }
15006
0
      }
15007
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
15008
0
      {
15009
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
15010
0
    {
15011
0
      if (tf->with == with_flags)
15012
0
        with_hex |= elf_flags_to_names[i].flag_value;
15013
0
      else if (tf->with == without_flags)
15014
0
        without_hex |= elf_flags_to_names[i].flag_value;
15015
0
      tf->valid = true;
15016
0
      break;
15017
0
    }
15018
0
      }
15019
0
    if (!tf->valid)
15020
0
      {
15021
0
        info->callbacks->einfo
15022
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
15023
0
        return false;
15024
0
      }
15025
0
  }
15026
0
      flaginfo->flags_initialized = true;
15027
0
      flaginfo->only_with_flags |= with_hex;
15028
0
      flaginfo->not_with_flags |= without_hex;
15029
0
    }
15030
15031
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
15032
0
    return false;
15033
15034
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
15035
0
    return false;
15036
15037
0
  return true;
15038
0
}
15039
15040
struct alloc_got_off_arg {
15041
  bfd_vma gotoff;
15042
  struct bfd_link_info *info;
15043
};
15044
15045
/* We need a special top-level link routine to convert got reference counts
15046
   to real got offsets.  */
15047
15048
static bool
15049
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15050
0
{
15051
0
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
15052
0
  bfd *obfd = gofarg->info->output_bfd;
15053
0
  elf_backend_data *bed = get_elf_backend_data (obfd);
15054
15055
0
  if (h->got.refcount > 0)
15056
0
    {
15057
0
      h->got.offset = gofarg->gotoff;
15058
0
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
15059
0
    }
15060
0
  else
15061
0
    h->got.offset = (bfd_vma) -1;
15062
15063
0
  return true;
15064
0
}
15065
15066
/* And an accompanying bit to work out final got entry offsets once
15067
   we're done.  Should be called from final_link.  */
15068
15069
bool
15070
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15071
          struct bfd_link_info *info)
15072
0
{
15073
0
  bfd *i;
15074
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15075
0
  bfd_vma gotoff;
15076
0
  struct alloc_got_off_arg gofarg;
15077
15078
0
  BFD_ASSERT (abfd == info->output_bfd);
15079
15080
0
  if (! is_elf_hash_table (info->hash))
15081
0
    return false;
15082
15083
  /* The GOT offset is relative to the .got section, but the GOT header is
15084
     put into the .got.plt section, if the backend uses it.  */
15085
0
  if (bed->want_got_plt)
15086
0
    gotoff = 0;
15087
0
  else
15088
0
    gotoff = bed->got_header_size;
15089
15090
  /* Do the local .got entries first.  */
15091
0
  for (i = info->input_bfds; i; i = i->link.next)
15092
0
    {
15093
0
      bfd_signed_vma *local_got;
15094
0
      size_t j, locsymcount;
15095
0
      Elf_Internal_Shdr *symtab_hdr;
15096
15097
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15098
0
  continue;
15099
15100
0
      local_got = elf_local_got_refcounts (i);
15101
0
      if (!local_got)
15102
0
  continue;
15103
15104
0
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
15105
0
      if (elf_bad_symtab (i))
15106
0
  locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15107
0
      else
15108
0
  locsymcount = symtab_hdr->sh_info;
15109
15110
0
      for (j = 0; j < locsymcount; ++j)
15111
0
  {
15112
0
    if (local_got[j] > 0)
15113
0
      {
15114
0
        local_got[j] = gotoff;
15115
0
        gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15116
0
      }
15117
0
    else
15118
0
      local_got[j] = (bfd_vma) -1;
15119
0
  }
15120
0
    }
15121
15122
  /* Then the global .got entries.  .plt refcounts are handled by
15123
     adjust_dynamic_symbol  */
15124
0
  gofarg.gotoff = gotoff;
15125
0
  gofarg.info = info;
15126
0
  elf_link_hash_traverse (elf_hash_table (info),
15127
0
        elf_gc_allocate_got_offsets,
15128
0
        &gofarg);
15129
0
  return true;
15130
0
}
15131
15132
/* Many folk need no more in the way of final link than this, once
15133
   got entry reference counting is enabled.  */
15134
15135
bool
15136
_bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15137
0
{
15138
0
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15139
0
    return false;
15140
15141
  /* Invoke the regular ELF backend linker to do all the work.  */
15142
0
  return _bfd_elf_final_link (abfd, info);
15143
0
}
15144
15145
bool
15146
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15147
0
{
15148
0
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15149
15150
0
  if (elf_bad_symtab (rcookie->abfd))
15151
0
    rcookie->rel = rcookie->rels;
15152
15153
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15154
0
    {
15155
0
      unsigned long r_symndx;
15156
15157
0
      if (!elf_bad_symtab (rcookie->abfd)
15158
0
    && rcookie->rel->r_offset > offset)
15159
0
  return false;
15160
0
      if (rcookie->rel->r_offset != offset)
15161
0
  continue;
15162
15163
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15164
0
      if (r_symndx == STN_UNDEF)
15165
0
  return true;
15166
15167
0
      struct elf_link_hash_entry *h;
15168
15169
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15170
15171
0
      if (h != NULL)
15172
0
  {
15173
0
    if ((h->root.type == bfd_link_hash_defined
15174
0
         || h->root.type == bfd_link_hash_defweak)
15175
0
        && (h->root.u.def.section->owner != rcookie->abfd
15176
0
      || h->root.u.def.section->kept_section != NULL
15177
0
      || discarded_section (h->root.u.def.section)))
15178
0
      return true;
15179
0
  }
15180
0
      else
15181
0
  {
15182
0
    if (r_symndx >= rcookie->locsymcount)
15183
      /* This can happen with corrupt input.  */
15184
0
      return false;
15185
15186
    /* It's not a relocation against a global symbol,
15187
       but it could be a relocation against a local
15188
       symbol for a discarded section.  */
15189
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15190
0
    if (isec != NULL
15191
0
        && (isec->kept_section != NULL
15192
0
      || discarded_section (isec)))
15193
0
      return true;
15194
0
  }
15195
15196
0
      return false;
15197
0
    }
15198
0
  return false;
15199
0
}
15200
15201
/* Discard unneeded references to discarded sections.
15202
   Returns -1 on error, 1 if any section's size was changed, 0 if
15203
   nothing changed.  This function assumes that the relocations are in
15204
   sorted order, which is true for all known assemblers.  */
15205
15206
int
15207
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15208
0
{
15209
0
  struct elf_reloc_cookie cookie;
15210
0
  asection *o;
15211
0
  bfd *abfd;
15212
0
  int changed = 0;
15213
15214
0
  if (info->traditional_format
15215
0
      || !is_elf_hash_table (info->hash))
15216
0
    return 0;
15217
15218
0
  o = bfd_get_section_by_name (output_bfd, ".stab");
15219
0
  if (o != NULL)
15220
0
    {
15221
0
      asection *i;
15222
15223
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15224
0
  {
15225
0
    if (i->size == 0
15226
0
        || i->reloc_count == 0
15227
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15228
0
      continue;
15229
15230
0
    abfd = i->owner;
15231
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15232
0
      continue;
15233
15234
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15235
0
      return -1;
15236
15237
0
    if (_bfd_discard_section_stabs (abfd, i,
15238
0
            bfd_elf_reloc_symbol_deleted_p,
15239
0
            &cookie))
15240
0
      changed = 1;
15241
15242
0
    fini_reloc_cookie_for_section (&cookie, i);
15243
0
  }
15244
0
    }
15245
15246
0
  o = NULL;
15247
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15248
0
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15249
0
  if (o != NULL)
15250
0
    {
15251
0
      asection *i;
15252
0
      int eh_changed = 0;
15253
0
      unsigned int eh_alignment;  /* Octets.  */
15254
15255
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15256
0
  {
15257
0
    int r;
15258
15259
0
    if (i->size == 0)
15260
0
      continue;
15261
15262
0
    abfd = i->owner;
15263
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15264
0
      continue;
15265
15266
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15267
0
      return -1;
15268
15269
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15270
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15271
0
             bfd_elf_reloc_symbol_deleted_p,
15272
0
             &cookie);
15273
0
    if (r)
15274
0
      {
15275
0
        eh_changed = 1;
15276
0
        if (r >= 2)
15277
0
    changed = 1;
15278
0
      }
15279
15280
0
    fini_reloc_cookie_for_section (&cookie, i);
15281
0
  }
15282
15283
0
      eh_alignment = ((1 << o->alignment_power)
15284
0
          * bfd_octets_per_byte (output_bfd, o));
15285
      /* Skip over zero terminator, and prevent empty sections from
15286
   adding alignment padding at the end.  */
15287
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15288
0
  if (i->size == 0)
15289
0
    i->flags |= SEC_EXCLUDE;
15290
0
  else if (i->size > 4)
15291
0
    break;
15292
      /* The last non-empty eh_frame section doesn't need padding.  */
15293
0
      if (i != NULL)
15294
0
  i = i->map_tail.s;
15295
      /* Any prior sections must pad the last FDE out to the output
15296
   section alignment.  Otherwise we might have zero padding
15297
   between sections, which would be seen as a terminator.  */
15298
0
      for (; i != NULL; i = i->map_tail.s)
15299
0
  if (i->size == 4)
15300
    /* All but the last zero terminator should have been removed.  */
15301
0
    BFD_FAIL ();
15302
0
  else
15303
0
    {
15304
0
      bfd_size_type size
15305
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15306
0
      if (i->size != size)
15307
0
        {
15308
0
    i->size = size;
15309
0
    changed = 1;
15310
0
    eh_changed = 1;
15311
0
        }
15312
0
    }
15313
0
      if (eh_changed)
15314
0
  elf_link_hash_traverse (elf_hash_table (info),
15315
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15316
0
    }
15317
15318
0
  o = bfd_get_section_by_name (output_bfd, ".sframe");
15319
0
  if (o != NULL)
15320
0
    {
15321
0
      asection *i;
15322
15323
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15324
0
  {
15325
0
    if (i->size == 0)
15326
0
      continue;
15327
15328
0
    abfd = i->owner;
15329
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15330
0
      continue;
15331
15332
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15333
0
      return -1;
15334
15335
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15336
0
      {
15337
0
        if (_bfd_elf_discard_section_sframe (i,
15338
0
               bfd_elf_reloc_symbol_deleted_p,
15339
0
               &cookie))
15340
0
    {
15341
0
      if (i->size != i->rawsize)
15342
0
        changed = 1;
15343
0
    }
15344
0
      }
15345
0
    fini_reloc_cookie_for_section (&cookie, i);
15346
0
  }
15347
      /* Update the reference to the output .sframe section.  Used to
15348
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15349
0
      if (!_bfd_elf_set_section_sframe (output_bfd, info))
15350
0
  return -1;
15351
0
    }
15352
15353
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15354
0
    {
15355
0
      elf_backend_data *bed;
15356
0
      asection *s;
15357
15358
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15359
0
  continue;
15360
0
      s = abfd->sections;
15361
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15362
0
  continue;
15363
15364
0
      bed = get_elf_backend_data (abfd);
15365
15366
0
      if (bed->elf_backend_discard_info != NULL)
15367
0
  {
15368
0
    if (!init_reloc_cookie (&cookie, abfd))
15369
0
      return -1;
15370
15371
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15372
0
      changed = 1;
15373
15374
0
    fini_reloc_cookie (&cookie, abfd);
15375
0
  }
15376
0
    }
15377
15378
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15379
0
    _bfd_elf_end_eh_frame_parsing (info);
15380
15381
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15382
0
    changed = 1;
15383
15384
0
  return changed;
15385
0
}
15386
15387
bool
15388
_bfd_elf_section_already_linked (bfd *abfd,
15389
         asection *sec,
15390
         struct bfd_link_info *info)
15391
0
{
15392
0
  flagword flags;
15393
0
  const char *name, *key;
15394
0
  struct bfd_section_already_linked *l;
15395
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15396
15397
0
  if (sec->output_section == bfd_abs_section_ptr)
15398
0
    return false;
15399
15400
0
  flags = sec->flags;
15401
15402
  /* Return if it isn't a linkonce section.  A comdat group section
15403
     also has SEC_LINK_ONCE set.  */
15404
0
  if ((flags & SEC_LINK_ONCE) == 0)
15405
0
    return false;
15406
15407
  /* Don't put group member sections on our list of already linked
15408
     sections.  They are handled as a group via their group section.  */
15409
0
  if (elf_sec_group (sec) != NULL)
15410
0
    return false;
15411
15412
  /* For a SHT_GROUP section, use the group signature as the key.  */
15413
0
  name = sec->name;
15414
0
  if ((flags & SEC_GROUP) != 0
15415
0
      && elf_next_in_group (sec) != NULL
15416
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15417
0
    key = elf_group_name (elf_next_in_group (sec));
15418
0
  else
15419
0
    {
15420
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15421
0
      if (startswith (name, ".gnu.linkonce.")
15422
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15423
0
  key++;
15424
0
      else
15425
  /* Must be a user linkonce section that doesn't follow gcc's
15426
     naming convention.  In this case we won't be matching
15427
     single member groups.  */
15428
0
  key = name;
15429
0
    }
15430
15431
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15432
15433
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15434
0
    {
15435
      /* We may have 2 different types of sections on the list: group
15436
   sections with a signature of <key> (<key> is some string),
15437
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15438
   Match like sections.  LTO plugin sections are an exception.
15439
   They are always named .gnu.linkonce.t.<key> and match either
15440
   type of section.  */
15441
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15442
0
     && ((flags & SEC_GROUP) != 0
15443
0
         || strcmp (name, l->sec->name) == 0))
15444
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15445
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15446
0
  {
15447
    /* The section has already been linked.  See if we should
15448
       issue a warning.  */
15449
0
    if (!_bfd_handle_already_linked (sec, l, info))
15450
0
      return false;
15451
15452
0
    if (flags & SEC_GROUP)
15453
0
      {
15454
0
        asection *first = elf_next_in_group (sec);
15455
0
        asection *s = first;
15456
15457
0
        while (s != NULL)
15458
0
    {
15459
0
      s->output_section = bfd_abs_section_ptr;
15460
      /* Record which group discards it.  */
15461
0
      s->kept_section = l->sec;
15462
0
      s = elf_next_in_group (s);
15463
      /* These lists are circular.  */
15464
0
      if (s == first)
15465
0
        break;
15466
0
    }
15467
0
      }
15468
15469
0
    return true;
15470
0
  }
15471
0
    }
15472
15473
  /* A single member comdat group section may be discarded by a
15474
     linkonce section and vice versa.  */
15475
0
  if ((flags & SEC_GROUP) != 0)
15476
0
    {
15477
0
      asection *first = elf_next_in_group (sec);
15478
15479
0
      if (first != NULL && elf_next_in_group (first) == first)
15480
  /* Check this single member group against linkonce sections.  */
15481
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15482
0
    if ((l->sec->flags & SEC_GROUP) == 0
15483
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15484
0
      {
15485
0
        first->output_section = bfd_abs_section_ptr;
15486
0
        first->kept_section = l->sec;
15487
0
        sec->output_section = bfd_abs_section_ptr;
15488
0
        break;
15489
0
      }
15490
0
    }
15491
0
  else
15492
    /* Check this linkonce section against single member groups.  */
15493
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15494
0
      if (l->sec->flags & SEC_GROUP)
15495
0
  {
15496
0
    asection *first = elf_next_in_group (l->sec);
15497
15498
0
    if (first != NULL
15499
0
        && elf_next_in_group (first) == first
15500
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15501
0
      {
15502
0
        sec->output_section = bfd_abs_section_ptr;
15503
0
        sec->kept_section = first;
15504
0
        break;
15505
0
      }
15506
0
  }
15507
15508
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15509
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15510
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15511
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15512
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15513
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15514
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15515
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15516
     The reverse order cannot happen as there is never a bfd with only the
15517
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15518
     matter as here were are looking only for cross-bfd sections.  */
15519
15520
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15521
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15522
0
      if ((l->sec->flags & SEC_GROUP) == 0
15523
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15524
0
  {
15525
0
    if (abfd != l->sec->owner)
15526
0
      sec->output_section = bfd_abs_section_ptr;
15527
0
    break;
15528
0
  }
15529
15530
  /* This is the first section with this name.  Record it.  */
15531
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15532
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15533
0
  return sec->output_section == bfd_abs_section_ptr;
15534
0
}
15535
15536
bool
15537
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15538
0
{
15539
0
  return sym->st_shndx == SHN_COMMON;
15540
0
}
15541
15542
unsigned int
15543
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15544
0
{
15545
0
  return SHN_COMMON;
15546
0
}
15547
15548
asection *
15549
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15550
0
{
15551
0
  return bfd_com_section_ptr;
15552
0
}
15553
15554
bfd_vma
15555
_bfd_elf_default_got_elt_size (bfd *abfd,
15556
             struct bfd_link_info *info ATTRIBUTE_UNUSED,
15557
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15558
             bfd *ibfd ATTRIBUTE_UNUSED,
15559
             unsigned long symndx ATTRIBUTE_UNUSED)
15560
0
{
15561
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15562
0
  return bed->s->arch_size / 8;
15563
0
}
15564
15565
/* Routines to support the creation of dynamic relocs.  */
15566
15567
/* Returns the name of the dynamic reloc section associated with SEC.  */
15568
15569
static const char *
15570
get_dynamic_reloc_section_name (bfd *       abfd,
15571
        asection *  sec,
15572
        bool is_rela)
15573
0
{
15574
0
  char *name;
15575
0
  const char *old_name = bfd_section_name (sec);
15576
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15577
15578
0
  if (old_name == NULL)
15579
0
    return NULL;
15580
15581
0
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15582
0
  sprintf (name, "%s%s", prefix, old_name);
15583
15584
0
  return name;
15585
0
}
15586
15587
/* Returns the dynamic reloc section associated with SEC.
15588
   If necessary compute the name of the dynamic reloc section based
15589
   on SEC's name (looked up in ABFD's string table) and the setting
15590
   of IS_RELA.  */
15591
15592
asection *
15593
_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15594
            asection *sec,
15595
            bool is_rela)
15596
0
{
15597
0
  asection *reloc_sec = elf_section_data (sec)->sreloc;
15598
15599
0
  if (reloc_sec == NULL)
15600
0
    {
15601
0
      const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15602
15603
0
      if (name != NULL)
15604
0
  {
15605
0
    reloc_sec = bfd_get_linker_section (abfd, name);
15606
15607
0
    if (reloc_sec != NULL)
15608
0
      elf_section_data (sec)->sreloc = reloc_sec;
15609
0
  }
15610
0
    }
15611
15612
0
  return reloc_sec;
15613
0
}
15614
15615
/* Returns the dynamic reloc section associated with SEC.  If the
15616
   section does not exist it is created and attached to the DYNOBJ
15617
   bfd and stored in the SRELOC field of SEC's elf_section_data
15618
   structure.
15619
15620
   ALIGNMENT is the alignment for the newly created section and
15621
   IS_RELA defines whether the name should be .rela.<SEC's name>
15622
   or .rel.<SEC's name>.  The section name is looked up in the
15623
   string table associated with ABFD.  */
15624
15625
asection *
15626
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15627
             bfd *dynobj,
15628
             unsigned int alignment,
15629
             bfd *abfd,
15630
             bool is_rela)
15631
0
{
15632
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15633
15634
0
  if (reloc_sec == NULL)
15635
0
    {
15636
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15637
15638
0
      if (name == NULL)
15639
0
  return NULL;
15640
15641
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15642
15643
0
      if (reloc_sec == NULL)
15644
0
  {
15645
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15646
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15647
0
    if ((sec->flags & SEC_ALLOC) != 0)
15648
0
      flags |= SEC_ALLOC | SEC_LOAD;
15649
15650
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15651
0
    if (reloc_sec != NULL)
15652
0
      {
15653
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15654
     name.  Override as it may be wrong, eg. for a user
15655
     section named "auto" we'll get ".relauto" which is
15656
     seen to be a .rela section.  */
15657
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15658
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15659
0
    reloc_sec = NULL;
15660
0
      }
15661
0
  }
15662
15663
0
      elf_section_data (sec)->sreloc = reloc_sec;
15664
0
    }
15665
15666
0
  return reloc_sec;
15667
0
}
15668
15669
/* Copy the ELF symbol type and other attributes for a linker script
15670
   assignment from HSRC to HDEST.  Generally this should be treated as
15671
   if we found a strong non-dynamic definition for HDEST (except that
15672
   ld ignores multiple definition errors).  */
15673
void
15674
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15675
             struct bfd_link_hash_entry *hdest,
15676
             struct bfd_link_hash_entry *hsrc)
15677
0
{
15678
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15679
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15680
0
  Elf_Internal_Sym isym;
15681
15682
0
  ehdest->type = ehsrc->type;
15683
0
  ehdest->target_internal = ehsrc->target_internal;
15684
15685
0
  isym.st_other = ehsrc->other;
15686
0
  elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15687
0
}
15688
15689
/* Append a RELA relocation REL to section S in BFD.  */
15690
15691
void
15692
_bfd_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15693
0
{
15694
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15695
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15696
0
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15697
0
  bed->s->swap_reloca_out (abfd, rel, loc);
15698
0
}
15699
15700
/* Append a REL relocation REL to section S in BFD.  */
15701
15702
void
15703
_bfd_elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15704
0
{
15705
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15706
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15707
0
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15708
0
  bed->s->swap_reloc_out (abfd, rel, loc);
15709
0
}
15710
15711
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15712
15713
struct bfd_link_hash_entry *
15714
bfd_elf_define_start_stop (struct bfd_link_info *info,
15715
         const char *symbol, asection *sec)
15716
0
{
15717
0
  struct elf_link_hash_entry *h;
15718
15719
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15720
0
          false, false, true);
15721
  /* NB: Common symbols will be turned into definition later.  */
15722
0
  if (h != NULL
15723
0
      && !h->root.ldscript_def
15724
0
      && (h->root.type == bfd_link_hash_undefined
15725
0
    || h->root.type == bfd_link_hash_undefweak
15726
0
    || ((h->ref_regular || h->def_dynamic)
15727
0
        && !h->def_regular
15728
0
        && h->root.type != bfd_link_hash_common)))
15729
0
    {
15730
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15731
0
      h->verinfo.verdef = NULL;
15732
0
      h->root.type = bfd_link_hash_defined;
15733
0
      h->root.u.def.section = sec;
15734
0
      h->root.u.def.value = 0;
15735
0
      h->def_regular = 1;
15736
0
      h->def_dynamic = 0;
15737
0
      h->start_stop = 1;
15738
0
      h->u2.start_stop_section = sec;
15739
0
      if (symbol[0] == '.')
15740
0
  {
15741
    /* .startof. and .sizeof. symbols are local.  */
15742
0
    elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
15743
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
15744
0
  }
15745
0
      else
15746
0
  {
15747
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15748
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15749
0
      | info->start_stop_visibility);
15750
0
    if (was_dynamic)
15751
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15752
0
  }
15753
0
      return &h->root;
15754
0
    }
15755
0
  return NULL;
15756
0
}
15757
15758
/* Find dynamic relocs for H that apply to read-only sections.  */
15759
15760
asection *
15761
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15762
0
{
15763
0
  struct elf_dyn_relocs *p;
15764
15765
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15766
0
    {
15767
0
      asection *s = p->sec->output_section;
15768
15769
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15770
0
  return p->sec;
15771
0
    }
15772
0
  return NULL;
15773
0
}
15774
15775
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15776
   read-only sections.  */
15777
15778
bool
15779
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15780
0
{
15781
0
  asection *sec;
15782
15783
0
  if (h->root.type == bfd_link_hash_indirect)
15784
0
    return true;
15785
15786
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15787
0
  if (sec != NULL)
15788
0
    {
15789
0
      struct bfd_link_info *info = (struct bfd_link_info *) inf;
15790
15791
0
      info->flags |= DF_TEXTREL;
15792
      /* xgettext:c-format */
15793
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15794
0
        "in read-only section `%pA'\n"),
15795
0
            sec->owner, h->root.root.string, sec);
15796
15797
0
      if (bfd_link_textrel_check (info))
15798
  /* xgettext:c-format */
15799
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15800
0
          "in read-only section `%pA'\n"),
15801
0
        sec->owner, h->root.root.string, sec);
15802
15803
      /* Not an error, just cut short the traversal.  */
15804
0
      return false;
15805
0
    }
15806
0
  return true;
15807
0
}
15808
15809
/* Add dynamic tags.  */
15810
15811
bool
15812
_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15813
         bool need_dynamic_reloc)
15814
0
{
15815
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15816
15817
0
  if (htab->dynamic_sections_created)
15818
0
    {
15819
      /* Add some entries to the .dynamic section.  We fill in the
15820
   values later, in finish_dynamic_sections, but we must add
15821
   the entries now so that we get the correct size for the
15822
   .dynamic section.  The DT_DEBUG entry is filled in by the
15823
   dynamic linker and used by the debugger.  */
15824
0
#define add_dynamic_entry(TAG, VAL) \
15825
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15826
15827
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
15828
15829
0
      if (bfd_link_executable (info))
15830
0
  {
15831
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15832
0
      return false;
15833
0
  }
15834
15835
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15836
0
  {
15837
    /* DT_PLTGOT is used by prelink even if there is no PLT
15838
       relocation.  */
15839
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15840
0
      return false;
15841
0
  }
15842
15843
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15844
0
  {
15845
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15846
0
        || !add_dynamic_entry (DT_PLTREL,
15847
0
             (bed->rela_plts_and_copies_p
15848
0
              ? DT_RELA : DT_REL))
15849
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15850
0
      return false;
15851
0
  }
15852
15853
0
      if (htab->tlsdesc_plt
15854
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15855
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15856
0
  return false;
15857
15858
0
      if (need_dynamic_reloc)
15859
0
  {
15860
0
    if (bed->rela_plts_and_copies_p)
15861
0
      {
15862
0
        if (!add_dynamic_entry (DT_RELA, 0)
15863
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15864
0
      || !add_dynamic_entry (DT_RELAENT,
15865
0
           bed->s->sizeof_rela))
15866
0
    return false;
15867
0
      }
15868
0
    else
15869
0
      {
15870
0
        if (!add_dynamic_entry (DT_REL, 0)
15871
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15872
0
      || !add_dynamic_entry (DT_RELENT,
15873
0
           bed->s->sizeof_rel))
15874
0
    return false;
15875
0
      }
15876
15877
    /* If any dynamic relocs apply to a read-only section,
15878
       then we need a DT_TEXTREL entry.  */
15879
0
    if ((info->flags & DF_TEXTREL) == 0)
15880
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15881
0
            info);
15882
15883
0
    if ((info->flags & DF_TEXTREL) != 0)
15884
0
      {
15885
0
        if (htab->ifunc_resolvers)
15886
0
    info->callbacks->einfo
15887
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15888
0
         "may result in a segfault at runtime; recompile with %s\n"),
15889
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15890
15891
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15892
0
    return false;
15893
0
      }
15894
0
  }
15895
0
    }
15896
0
#undef add_dynamic_entry
15897
15898
0
  return true;
15899
0
}