Coverage Report

Created: 2026-05-11 07:54

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_symtab_hdr (abfd);
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_symtab_hdr (input_bfd),
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_symtab_hdr (input_bfd).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, *obed;
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
0
  obed = get_elf_backend_data (info->output_bfd);
1187
1188
0
  htab = elf_hash_table (info);
1189
1190
  /* NEW_VERSION is the symbol version of the new symbol.  */
1191
0
  if (h->versioned != unversioned)
1192
0
    {
1193
      /* Symbol version is unknown or versioned.  */
1194
0
      new_version = strrchr (name, ELF_VER_CHR);
1195
0
      if (new_version)
1196
0
  {
1197
0
    if (h->versioned == unknown)
1198
0
      {
1199
        /* The base symbol has an empty version.  */
1200
0
        if (new_version[1] == '\0')
1201
0
    {
1202
0
      htab->has_base_symbols = true;
1203
0
      h->base_symbol = 1;
1204
0
    }
1205
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1206
0
    h->versioned = versioned_hidden;
1207
0
        else
1208
0
    h->versioned = versioned;
1209
0
      }
1210
0
    new_version += 1;
1211
0
    if (new_version[0] == '\0')
1212
0
      new_version = NULL;
1213
0
  }
1214
0
      else
1215
0
  h->versioned = unversioned;
1216
0
    }
1217
0
  else
1218
0
    new_version = NULL;
1219
1220
  /* For merging, we only care about real symbols.  But we need to make
1221
     sure that indirect symbol dynamic flags are updated.  */
1222
0
  hi = h;
1223
0
  while (h->root.type == bfd_link_hash_indirect
1224
0
   || h->root.type == bfd_link_hash_warning)
1225
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1226
1227
0
  if (!*matched)
1228
0
    {
1229
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1230
0
  *matched = true;
1231
0
      else
1232
0
  {
1233
    /* OLD_HIDDEN is true if the existing symbol is only visible
1234
       to the symbol with the same symbol version.  NEW_HIDDEN is
1235
       true if the new symbol is only visible to the symbol with
1236
       the same symbol version.  */
1237
0
    bool old_hidden = h->versioned == versioned_hidden;
1238
0
    bool new_hidden = hi->versioned == versioned_hidden;
1239
0
    if (!old_hidden && !new_hidden)
1240
      /* The new symbol matches the existing symbol if both
1241
         aren't hidden.  */
1242
0
      *matched = true;
1243
0
    else
1244
0
      {
1245
        /* OLD_VERSION is the symbol version of the existing
1246
     symbol. */
1247
0
        const char *old_version;
1248
1249
0
        if (h->versioned >= versioned)
1250
0
    old_version = strrchr (h->root.root.string,
1251
0
               ELF_VER_CHR) + 1;
1252
0
        else
1253
0
     old_version = NULL;
1254
1255
        /* The new symbol matches the existing symbol if they
1256
     have the same symbol version.  */
1257
0
        *matched = (old_version == new_version
1258
0
        || (old_version != NULL
1259
0
            && new_version != NULL
1260
0
            && strcmp (old_version, new_version) == 0));
1261
0
      }
1262
0
  }
1263
0
    }
1264
1265
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1266
     existing symbol.  */
1267
1268
0
  oldbfd = NULL;
1269
0
  oldsec = NULL;
1270
0
  switch (h->root.type)
1271
0
    {
1272
0
    default:
1273
0
      break;
1274
1275
0
    case bfd_link_hash_undefined:
1276
0
    case bfd_link_hash_undefweak:
1277
0
      oldbfd = h->root.u.undef.abfd;
1278
0
      break;
1279
1280
0
    case bfd_link_hash_defined:
1281
0
    case bfd_link_hash_defweak:
1282
0
      oldbfd = h->root.u.def.section->owner;
1283
0
      oldsec = h->root.u.def.section;
1284
0
      break;
1285
1286
0
    case bfd_link_hash_common:
1287
0
      oldbfd = h->root.u.c.p->section->owner;
1288
0
      oldsec = h->root.u.c.p->section;
1289
0
      if (pold_alignment)
1290
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1291
0
      break;
1292
0
    }
1293
0
  if (poldbfd && *poldbfd == NULL)
1294
0
    *poldbfd = oldbfd;
1295
1296
  /* Differentiate strong and weak symbols.  */
1297
0
  newweak = bind == STB_WEAK;
1298
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1299
0
       || h->root.type == bfd_link_hash_undefweak);
1300
0
  if (pold_weak)
1301
0
    *pold_weak = oldweak;
1302
1303
  /* We have to check it for every instance since the first few may be
1304
     references and not all compilers emit symbol type for undefined
1305
     symbols.  */
1306
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1307
1308
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1309
     respectively, is from a dynamic object.  */
1310
1311
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1312
1313
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1314
     syms and defined syms in dynamic libraries respectively.
1315
     ref_dynamic on the other hand can be set for a symbol defined in
1316
     a dynamic library, and def_dynamic may not be set;  When the
1317
     definition in a dynamic lib is overridden by a definition in the
1318
     executable use of the symbol in the dynamic lib becomes a
1319
     reference to the executable symbol.  */
1320
0
  if (newdyn)
1321
0
    {
1322
0
      if (bfd_is_und_section (sec))
1323
0
  {
1324
0
    if (bind != STB_WEAK)
1325
0
      {
1326
0
        h->ref_dynamic_nonweak = 1;
1327
0
        hi->ref_dynamic_nonweak = 1;
1328
0
      }
1329
0
  }
1330
0
      else
1331
0
  {
1332
    /* Update the existing symbol only if they match. */
1333
0
    if (*matched)
1334
0
      h->dynamic_def = 1;
1335
0
    hi->dynamic_def = 1;
1336
0
  }
1337
0
    }
1338
1339
  /* If we just created the symbol, mark it as being an ELF symbol.
1340
     Other than that, there is nothing to do--there is no merge issue
1341
     with a newly defined symbol--so we just return.  */
1342
1343
0
  if (h->root.type == bfd_link_hash_new)
1344
0
    {
1345
0
      h->non_elf = 0;
1346
0
      return true;
1347
0
    }
1348
1349
  /* In cases involving weak versioned symbols, we may wind up trying
1350
     to merge a symbol with itself.  Catch that here, to avoid the
1351
     confusion that results if we try to override a symbol with
1352
     itself.  The additional tests catch cases like
1353
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1354
     dynamic object, which we do want to handle here.  */
1355
0
  if (abfd == oldbfd
1356
0
      && (newweak || oldweak)
1357
0
      && ((abfd->flags & DYNAMIC) == 0
1358
0
    || !h->def_regular))
1359
0
    return true;
1360
1361
0
  olddyn = false;
1362
0
  if (oldbfd != NULL)
1363
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1364
0
  else if (oldsec != NULL)
1365
0
    {
1366
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1367
   indices used by MIPS ELF.  */
1368
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1369
0
    }
1370
1371
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1372
0
  if (!htab->handling_dt_needed
1373
0
      && oldbfd != NULL
1374
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1375
0
    {
1376
0
      if (newdyn != olddyn)
1377
0
  {
1378
    /* Handle a case where plugin_notice won't be called and thus
1379
       won't set the non_ir_ref flags on the first pass over
1380
       symbols.  */
1381
0
    h->root.non_ir_ref_dynamic = true;
1382
0
    hi->root.non_ir_ref_dynamic = true;
1383
0
  }
1384
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1385
0
         && hi->root.type == bfd_link_hash_indirect)
1386
0
  {
1387
    /* Change indirect symbol from IR to undefined.  */
1388
0
    hi->root.type = bfd_link_hash_undefined;
1389
0
    hi->root.u.undef.abfd = oldbfd;
1390
0
  }
1391
0
    }
1392
1393
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1394
     respectively, appear to be a definition rather than reference.  */
1395
1396
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1397
1398
0
  olddef = (h->root.type != bfd_link_hash_undefined
1399
0
      && h->root.type != bfd_link_hash_undefweak
1400
0
      && h->root.type != bfd_link_hash_common);
1401
1402
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1403
     respectively, appear to be a function.  */
1404
1405
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1406
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1407
1408
0
  oldfunc = (h->type != STT_NOTYPE
1409
0
       && bed->is_function_type (h->type));
1410
1411
0
  if (!(newfunc && oldfunc)
1412
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1413
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1414
0
      && h->type != STT_NOTYPE
1415
0
      && (newdef || bfd_is_com_section (sec))
1416
0
      && (olddef || h->root.type == bfd_link_hash_common))
1417
0
    {
1418
      /* If creating a default indirect symbol ("foo" or "foo@") from
1419
   a dynamic versioned definition ("foo@@") skip doing so if
1420
   there is an existing regular definition with a different
1421
   type.  We don't want, for example, a "time" variable in the
1422
   executable overriding a "time" function in a shared library.  */
1423
0
      if (newdyn
1424
0
    && !olddyn)
1425
0
  {
1426
0
    *skip = true;
1427
0
    return true;
1428
0
  }
1429
1430
      /* When adding a symbol from a regular object file after we have
1431
   created indirect symbols, undo the indirection and any
1432
   dynamic state.  */
1433
0
      if (hi != h
1434
0
    && !newdyn
1435
0
    && olddyn)
1436
0
  {
1437
0
    h = hi;
1438
0
    obed->elf_backend_hide_symbol (info, h, true);
1439
0
    h->forced_local = 0;
1440
0
    h->ref_dynamic = 0;
1441
0
    h->def_dynamic = 0;
1442
0
    h->dynamic_def = 0;
1443
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1444
0
      {
1445
0
        h->root.type = bfd_link_hash_undefined;
1446
0
        h->root.u.undef.abfd = abfd;
1447
0
      }
1448
0
    else
1449
0
      {
1450
0
        h->root.type = bfd_link_hash_new;
1451
0
        h->root.u.undef.abfd = NULL;
1452
0
      }
1453
0
    return true;
1454
0
  }
1455
0
    }
1456
1457
  /* Check TLS symbols.  We don't check undefined symbols introduced
1458
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1459
     check symbols from plugins because they also have no type.  */
1460
0
  if (oldbfd != NULL
1461
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1462
0
      && (abfd->flags & BFD_PLUGIN) == 0
1463
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1464
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1465
0
    {
1466
0
      bfd *ntbfd, *tbfd;
1467
0
      bool ntdef, tdef;
1468
0
      asection *ntsec, *tsec;
1469
1470
0
      if (h->type == STT_TLS)
1471
0
  {
1472
0
    ntbfd = abfd;
1473
0
    ntsec = sec;
1474
0
    ntdef = newdef;
1475
0
    tbfd = oldbfd;
1476
0
    tsec = oldsec;
1477
0
    tdef = olddef;
1478
0
  }
1479
0
      else
1480
0
  {
1481
0
    ntbfd = oldbfd;
1482
0
    ntsec = oldsec;
1483
0
    ntdef = olddef;
1484
0
    tbfd = abfd;
1485
0
    tsec = sec;
1486
0
    tdef = newdef;
1487
0
  }
1488
1489
0
      if (tdef && ntdef)
1490
0
  _bfd_error_handler
1491
    /* xgettext:c-format */
1492
0
    (_("%s: TLS definition in %pB section %pA "
1493
0
       "mismatches non-TLS definition in %pB section %pA"),
1494
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1495
0
      else if (!tdef && !ntdef)
1496
0
  _bfd_error_handler
1497
    /* xgettext:c-format */
1498
0
    (_("%s: TLS reference in %pB "
1499
0
       "mismatches non-TLS reference in %pB"),
1500
0
     h->root.root.string, tbfd, ntbfd);
1501
0
      else if (tdef)
1502
0
  _bfd_error_handler
1503
    /* xgettext:c-format */
1504
0
    (_("%s: TLS definition in %pB section %pA "
1505
0
       "mismatches non-TLS reference in %pB"),
1506
0
     h->root.root.string, tbfd, tsec, ntbfd);
1507
0
      else
1508
0
  _bfd_error_handler
1509
    /* xgettext:c-format */
1510
0
    (_("%s: TLS reference in %pB "
1511
0
       "mismatches non-TLS definition in %pB section %pA"),
1512
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1513
1514
0
      bfd_set_error (bfd_error_bad_value);
1515
0
      return false;
1516
0
    }
1517
1518
  /* If the old symbol has non-default visibility, we ignore the new
1519
     definition from a dynamic object.  */
1520
0
  if (newdyn
1521
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1522
0
      && !bfd_is_und_section (sec))
1523
0
    {
1524
0
      *skip = true;
1525
      /* Make sure this symbol is dynamic.  */
1526
0
      h->ref_dynamic = 1;
1527
0
      hi->ref_dynamic = 1;
1528
      /* A protected symbol has external availability. Make sure it is
1529
   recorded as dynamic.
1530
1531
   FIXME: Should we check type and size for protected symbol?  */
1532
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1533
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1534
0
      else
1535
0
  return true;
1536
0
    }
1537
0
  else if (!newdyn
1538
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1539
0
     && h->def_dynamic)
1540
0
    {
1541
      /* If the new symbol with non-default visibility comes from a
1542
   relocatable file and the old definition comes from a dynamic
1543
   object, we remove the old definition.  */
1544
0
      if (hi->root.type == bfd_link_hash_indirect)
1545
0
  {
1546
    /* Handle the case where the old dynamic definition is
1547
       default versioned.  We need to copy the symbol info from
1548
       the symbol with default version to the normal one if it
1549
       was referenced before.  */
1550
0
    if (h->ref_regular)
1551
0
      {
1552
0
        hi->root.type = h->root.type;
1553
0
        h->root.type = bfd_link_hash_indirect;
1554
0
        obed->elf_backend_copy_indirect_symbol (info, hi, h);
1555
1556
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1557
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1558
0
    {
1559
      /* If the new symbol is hidden or internal, completely undo
1560
         any dynamic link state.  */
1561
0
      obed->elf_backend_hide_symbol (info, h, true);
1562
0
      h->forced_local = 0;
1563
0
      h->ref_dynamic = 0;
1564
0
    }
1565
0
        else
1566
0
    h->ref_dynamic = 1;
1567
1568
0
        h->def_dynamic = 0;
1569
        /* FIXME: Should we check type and size for protected symbol?  */
1570
0
        h->size = 0;
1571
0
        h->type = 0;
1572
1573
0
        h = hi;
1574
0
      }
1575
0
    else
1576
0
      h = hi;
1577
0
  }
1578
1579
      /* If the old symbol was undefined before, then it will still be
1580
   on the undefs list.  If the new symbol is undefined or
1581
   common, we can't make it bfd_link_hash_new here, because new
1582
   undefined or common symbols will be added to the undefs list
1583
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1584
   added twice to the undefs list.  Also, if the new symbol is
1585
   undefweak then we don't want to lose the strong undef.  */
1586
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1587
0
  {
1588
0
    h->root.type = bfd_link_hash_undefined;
1589
0
    h->root.u.undef.abfd = abfd;
1590
0
  }
1591
0
      else
1592
0
  {
1593
0
    h->root.type = bfd_link_hash_new;
1594
0
    h->root.u.undef.abfd = NULL;
1595
0
  }
1596
1597
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1598
0
  {
1599
    /* If the new symbol is hidden or internal, completely undo
1600
       any dynamic link state.  */
1601
0
    obed->elf_backend_hide_symbol (info, h, true);
1602
0
    h->forced_local = 0;
1603
0
    h->ref_dynamic = 0;
1604
0
  }
1605
0
      else
1606
0
  h->ref_dynamic = 1;
1607
0
      h->def_dynamic = 0;
1608
      /* FIXME: Should we check type and size for protected symbol?  */
1609
0
      h->size = 0;
1610
0
      h->type = 0;
1611
0
      return true;
1612
0
    }
1613
1614
  /* If a new weak symbol definition comes from a regular file and the
1615
     old symbol comes from a dynamic library, we treat the new one as
1616
     strong.  Similarly, an old weak symbol definition from a regular
1617
     file is treated as strong when the new symbol comes from a dynamic
1618
     library.  Further, an old weak symbol from a dynamic library is
1619
     treated as strong if the new symbol is from a dynamic library.
1620
     This reflects the way glibc's ld.so works.
1621
1622
     Also allow a weak symbol to override a linker script symbol
1623
     defined by an early pass over the script.  This is done so the
1624
     linker knows the symbol is defined in an object file, for the
1625
     DEFINED script function.
1626
1627
     Do this before setting *type_change_ok or *size_change_ok so that
1628
     we warn properly when dynamic library symbols are overridden.  */
1629
1630
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1631
0
    newweak = false;
1632
0
  if (olddef && newdyn)
1633
0
    oldweak = false;
1634
1635
  /* Allow changes between different types of function symbol.  */
1636
0
  if (newfunc && oldfunc)
1637
0
    *type_change_ok = true;
1638
1639
  /* It's OK to change the type if either the existing symbol or the
1640
     new symbol is weak.  A type change is also OK if the old symbol
1641
     is undefined and the new symbol is defined.  */
1642
1643
0
  if (oldweak
1644
0
      || newweak
1645
0
      || (newdef
1646
0
    && h->root.type == bfd_link_hash_undefined))
1647
0
    *type_change_ok = true;
1648
1649
  /* It's OK to change the size if either the existing symbol or the
1650
     new symbol is weak, or if the old symbol is undefined.  */
1651
1652
0
  if (*type_change_ok
1653
0
      || h->root.type == bfd_link_hash_undefined)
1654
0
    *size_change_ok = true;
1655
1656
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1657
     symbol, respectively, appears to be a common symbol in a dynamic
1658
     object.  If a symbol appears in an uninitialized section, and is
1659
     not weak, and is not a function, then it may be a common symbol
1660
     which was resolved when the dynamic object was created.  We want
1661
     to treat such symbols specially, because they raise special
1662
     considerations when setting the symbol size: if the symbol
1663
     appears as a common symbol in a regular object, and the size in
1664
     the regular object is larger, we must make sure that we use the
1665
     larger size.  This problematic case can always be avoided in C,
1666
     but it must be handled correctly when using Fortran shared
1667
     libraries.
1668
1669
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1670
     likewise for OLDDYNCOMMON and OLDDEF.
1671
1672
     Note that this test is just a heuristic, and that it is quite
1673
     possible to have an uninitialized symbol in a shared object which
1674
     is really a definition, rather than a common symbol.  This could
1675
     lead to some minor confusion when the symbol really is a common
1676
     symbol in some regular object.  However, I think it will be
1677
     harmless.  */
1678
1679
0
  if (newdyn
1680
0
      && newdef
1681
0
      && !newweak
1682
0
      && (sec->flags & SEC_ALLOC) != 0
1683
0
      && (sec->flags & SEC_LOAD) == 0
1684
0
      && sym->st_size > 0
1685
0
      && !newfunc)
1686
0
    newdyncommon = true;
1687
0
  else
1688
0
    newdyncommon = false;
1689
1690
0
  if (olddyn
1691
0
      && olddef
1692
0
      && h->root.type == bfd_link_hash_defined
1693
0
      && h->def_dynamic
1694
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1695
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1696
0
      && h->size > 0
1697
0
      && !oldfunc)
1698
0
    olddyncommon = true;
1699
0
  else
1700
0
    olddyncommon = false;
1701
1702
  /* We now know everything about the old and new symbols.  We ask the
1703
     backend to check if we can merge them.  */
1704
0
  if (obed->merge_symbol != NULL)
1705
0
    {
1706
0
      if (!obed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1707
0
  return false;
1708
0
      sec = *psec;
1709
0
    }
1710
1711
  /* There are multiple definitions of a normal symbol.  Skip the
1712
     default symbol as well as definition from an IR object.  */
1713
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1714
0
      && !default_sym && h->def_regular
1715
0
      && !(oldbfd != NULL
1716
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1717
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1718
0
    {
1719
      /* Handle a multiple definition.  */
1720
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1721
0
                 abfd, sec, *pvalue);
1722
0
      *skip = true;
1723
0
      return true;
1724
0
    }
1725
1726
  /* If both the old and the new symbols look like common symbols in a
1727
     dynamic object, set the size of the symbol to the larger of the
1728
     two.  */
1729
1730
0
  if (olddyncommon
1731
0
      && newdyncommon
1732
0
      && sym->st_size != h->size)
1733
0
    {
1734
      /* Since we think we have two common symbols, issue a multiple
1735
   common warning if desired.  Note that we only warn if the
1736
   size is different.  If the size is the same, we simply let
1737
   the old symbol override the new one as normally happens with
1738
   symbols defined in dynamic objects.  */
1739
1740
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1741
0
             bfd_link_hash_common, sym->st_size);
1742
0
      if (sym->st_size > h->size)
1743
0
  h->size = sym->st_size;
1744
1745
0
      *size_change_ok = true;
1746
0
    }
1747
1748
  /* If we are looking at a dynamic object, and we have found a
1749
     definition, we need to see if the symbol was already defined by
1750
     some other object.  If so, we want to use the existing
1751
     definition, and we do not want to report a multiple symbol
1752
     definition error; we do this by clobbering *PSEC to be
1753
     bfd_und_section_ptr.
1754
1755
     We treat a common symbol as a definition if the symbol in the
1756
     shared library is a function, since common symbols always
1757
     represent variables; this can cause confusion in principle, but
1758
     any such confusion would seem to indicate an erroneous program or
1759
     shared library.  We also permit a common symbol in a regular
1760
     object to override a weak symbol in a shared object.  */
1761
1762
0
  if (newdyn
1763
0
      && newdef
1764
0
      && (olddef
1765
0
    || (h->root.type == bfd_link_hash_common
1766
0
        && (newweak || newfunc))))
1767
0
    {
1768
0
      *override = abfd;
1769
0
      newdef = false;
1770
0
      newdyncommon = false;
1771
1772
0
      *psec = sec = bfd_und_section_ptr;
1773
0
      *size_change_ok = true;
1774
1775
      /* If we get here when the old symbol is a common symbol, then
1776
   we are explicitly letting it override a weak symbol or
1777
   function in a dynamic object, and we don't want to warn about
1778
   a type change.  If the old symbol is a defined symbol, a type
1779
   change warning may still be appropriate.  */
1780
1781
0
      if (h->root.type == bfd_link_hash_common)
1782
0
  *type_change_ok = true;
1783
0
    }
1784
1785
  /* Handle the special case of an old common symbol merging with a
1786
     new symbol which looks like a common symbol in a shared object.
1787
     We change *PSEC and *PVALUE to make the new symbol look like a
1788
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1789
     right thing.  */
1790
1791
0
  if (newdyncommon
1792
0
      && h->root.type == bfd_link_hash_common)
1793
0
    {
1794
0
      *override = oldbfd;
1795
0
      newdef = false;
1796
0
      newdyncommon = false;
1797
0
      *pvalue = sym->st_size;
1798
0
      *psec = sec = bed->common_section (oldsec);
1799
0
      *size_change_ok = true;
1800
0
    }
1801
1802
  /* Skip weak definitions of symbols that are already defined.  */
1803
0
  if (newdef && olddef && newweak)
1804
0
    {
1805
      /* Don't skip new non-IR weak syms.  */
1806
0
      if (!(oldbfd != NULL
1807
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1808
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1809
0
  {
1810
0
    newdef = false;
1811
0
    *skip = true;
1812
0
  }
1813
1814
      /* Merge st_other.  If the symbol already has a dynamic index,
1815
   but visibility says it should not be visible, turn it into a
1816
   local symbol.  */
1817
0
      elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1818
0
      if (h->dynindx != -1)
1819
0
  switch (ELF_ST_VISIBILITY (h->other))
1820
0
    {
1821
0
    case STV_INTERNAL:
1822
0
    case STV_HIDDEN:
1823
0
      obed->elf_backend_hide_symbol (info, h, true);
1824
0
      break;
1825
0
    }
1826
0
    }
1827
1828
  /* If the old symbol is from a dynamic object, and the new symbol is
1829
     a definition which is not from a dynamic object, then the new
1830
     symbol overrides the old symbol.  Symbols from regular files
1831
     always take precedence over symbols from dynamic objects, even if
1832
     they are defined after the dynamic object in the link.
1833
1834
     As above, we again permit a common symbol in a regular object to
1835
     override a definition in a shared object if the shared object
1836
     symbol is a function or is weak.  */
1837
1838
0
  flip = NULL;
1839
0
  if (!newdyn
1840
0
      && (newdef
1841
0
    || (bfd_is_com_section (sec)
1842
0
        && (oldweak || oldfunc)))
1843
0
      && olddyn
1844
0
      && olddef
1845
0
      && h->def_dynamic)
1846
0
    {
1847
      /* Change the hash table entry to undefined, and let
1848
   _bfd_generic_link_add_one_symbol do the right thing with the
1849
   new definition.  */
1850
1851
0
      h->root.type = bfd_link_hash_undefined;
1852
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1853
0
      *size_change_ok = true;
1854
1855
0
      olddef = false;
1856
0
      olddyncommon = false;
1857
1858
      /* We again permit a type change when a common symbol may be
1859
   overriding a function.  */
1860
1861
0
      if (bfd_is_com_section (sec))
1862
0
  {
1863
0
    if (oldfunc)
1864
0
      {
1865
        /* If a common symbol overrides a function, make sure
1866
     that it isn't defined dynamically nor has type
1867
     function.  */
1868
0
        h->def_dynamic = 0;
1869
0
        h->type = STT_NOTYPE;
1870
0
      }
1871
0
    *type_change_ok = true;
1872
0
  }
1873
1874
0
      if (hi->root.type == bfd_link_hash_indirect)
1875
0
  flip = hi;
1876
0
      else
1877
  /* This union may have been set to be non-NULL when this symbol
1878
     was seen in a dynamic object.  We must force the union to be
1879
     NULL, so that it is correct for a regular symbol.  */
1880
0
  h->verinfo.vertree = NULL;
1881
0
    }
1882
1883
  /* Handle the special case of a new common symbol merging with an
1884
     old symbol that looks like it might be a common symbol defined in
1885
     a shared object.  Note that we have already handled the case in
1886
     which a new common symbol should simply override the definition
1887
     in the shared library.  */
1888
1889
0
  if (! newdyn
1890
0
      && bfd_is_com_section (sec)
1891
0
      && olddyncommon)
1892
0
    {
1893
      /* It would be best if we could set the hash table entry to a
1894
   common symbol, but we don't know what to use for the section
1895
   or the alignment.  */
1896
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1897
0
             bfd_link_hash_common, sym->st_size);
1898
1899
      /* If the presumed common symbol in the dynamic object is
1900
   larger, pretend that the new symbol has its size.  */
1901
1902
0
      if (h->size > *pvalue)
1903
0
  *pvalue = h->size;
1904
1905
      /* We need to remember the alignment required by the symbol
1906
   in the dynamic object.  */
1907
0
      BFD_ASSERT (pold_alignment);
1908
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1909
1910
0
      olddef = false;
1911
0
      olddyncommon = false;
1912
1913
0
      h->root.type = bfd_link_hash_undefined;
1914
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1915
1916
0
      *size_change_ok = true;
1917
0
      *type_change_ok = true;
1918
1919
0
      if (hi->root.type == bfd_link_hash_indirect)
1920
0
  flip = hi;
1921
0
      else
1922
0
  h->verinfo.vertree = NULL;
1923
0
    }
1924
1925
0
  if (flip != NULL)
1926
0
    {
1927
      /* Handle the case where we had a versioned symbol in a dynamic
1928
   library and now find a definition in a normal object.  In this
1929
   case, we make the versioned symbol point to the normal one.  */
1930
0
      flip->root.type = h->root.type;
1931
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1932
0
      h->root.type = bfd_link_hash_indirect;
1933
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1934
0
      obed->elf_backend_copy_indirect_symbol (info, flip, h);
1935
0
      if (h->def_dynamic)
1936
0
  {
1937
0
    h->def_dynamic = 0;
1938
0
    flip->ref_dynamic = 1;
1939
0
  }
1940
0
    }
1941
1942
0
  return true;
1943
0
}
1944
1945
/* This function is called to create an indirect symbol from the
1946
   default for the symbol with the default version if needed. The
1947
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1948
   set DYNSYM if the new indirect symbol is dynamic.  */
1949
1950
static bool
1951
_bfd_elf_add_default_symbol (bfd *abfd,
1952
           struct bfd_link_info *info,
1953
           struct elf_link_hash_entry *h,
1954
           const char *name,
1955
           Elf_Internal_Sym *sym,
1956
           asection *sec,
1957
           bfd_vma value,
1958
           bfd **poldbfd,
1959
           bool *dynsym)
1960
0
{
1961
0
  bool type_change_ok;
1962
0
  bool size_change_ok;
1963
0
  bool skip;
1964
0
  char *shortname;
1965
0
  struct elf_link_hash_entry *hi;
1966
0
  struct bfd_link_hash_entry *bh;
1967
0
  elf_backend_data *bed;
1968
0
  bool collect;
1969
0
  bool dynamic;
1970
0
  bfd *override;
1971
0
  const char *p;
1972
0
  size_t len, shortlen;
1973
0
  asection *tmp_sec;
1974
0
  bool matched;
1975
1976
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1977
0
    return true;
1978
1979
  /* If this symbol has a version, and it is the default version, we
1980
     create an indirect symbol from the default name to the fully
1981
     decorated name.  This will cause external references which do not
1982
     specify a version to be bound to this version of the symbol.  */
1983
0
  p = strchr (name, ELF_VER_CHR);
1984
0
  if (h->versioned == unknown)
1985
0
    {
1986
0
      if (p == NULL)
1987
0
  {
1988
0
    h->versioned = unversioned;
1989
0
    return true;
1990
0
  }
1991
0
      else
1992
0
  {
1993
0
    if (p[1] != ELF_VER_CHR)
1994
0
      {
1995
0
        h->versioned = versioned_hidden;
1996
0
        return true;
1997
0
      }
1998
0
    else
1999
0
      h->versioned = versioned;
2000
0
  }
2001
0
    }
2002
0
  else
2003
0
    {
2004
      /* PR ld/19073: We may see an unversioned definition after the
2005
   default version.  */
2006
0
      if (p == NULL)
2007
0
  return true;
2008
0
    }
2009
2010
0
  bed = get_elf_backend_data (info->output_bfd);
2011
0
  collect = bed->collect;
2012
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
2013
2014
0
  shortlen = p - name;
2015
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
2016
0
  if (shortname == NULL)
2017
0
    return false;
2018
0
  memcpy (shortname, name, shortlen);
2019
0
  shortname[shortlen] = '\0';
2020
2021
  /* We are going to create a new symbol.  Merge it with any existing
2022
     symbol with this name.  For the purposes of the merge, act as
2023
     though we were defining the symbol we just defined, although we
2024
     actually going to define an indirect symbol.  */
2025
0
  type_change_ok = false;
2026
0
  size_change_ok = false;
2027
0
  matched = true;
2028
0
  tmp_sec = sec;
2029
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2030
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2031
0
            &type_change_ok, &size_change_ok, &matched))
2032
0
    return false;
2033
2034
0
  if (skip)
2035
0
    goto nondefault;
2036
2037
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2038
0
    {
2039
      /* If the undecorated symbol will have a version added by a
2040
   script different to H, then don't indirect to/from the
2041
   undecorated symbol.  This isn't ideal because we may not yet
2042
   have seen symbol versions, if given by a script on the
2043
   command line rather than via --version-script.  */
2044
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2045
0
  {
2046
0
    bool hide;
2047
2048
0
    hi->verinfo.vertree
2049
0
      = bfd_find_version_for_sym (info->version_info,
2050
0
          hi->root.root.string, &hide);
2051
0
    if (hi->verinfo.vertree != NULL && hide)
2052
0
      {
2053
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
2054
0
        goto nondefault;
2055
0
      }
2056
0
  }
2057
0
      if (hi->verinfo.vertree != NULL
2058
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2059
0
  goto nondefault;
2060
0
    }
2061
2062
0
  if (! override)
2063
0
    {
2064
      /* Add the default symbol if not performing a relocatable link.  */
2065
0
      if (! bfd_link_relocatable (info))
2066
0
  {
2067
0
    bh = &hi->root;
2068
0
    if (bh->type == bfd_link_hash_defined
2069
0
        && bh->u.def.section->owner != NULL
2070
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2071
0
      {
2072
        /* Mark the previous definition from IR object as
2073
     undefined so that the generic linker will override
2074
     it.  */
2075
0
        bh->type = bfd_link_hash_undefined;
2076
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2077
0
      }
2078
0
    if (! (_bfd_generic_link_add_one_symbol
2079
0
     (info, abfd, shortname, BSF_INDIRECT,
2080
0
      bfd_ind_section_ptr,
2081
0
      0, name, false, collect, &bh)))
2082
0
      return false;
2083
0
    hi = (struct elf_link_hash_entry *) bh;
2084
0
  }
2085
0
    }
2086
0
  else
2087
0
    {
2088
      /* In this case the symbol named SHORTNAME is overriding the
2089
   indirect symbol we want to add.  We were planning on making
2090
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2091
   is the name without a version.  NAME is the fully versioned
2092
   name, and it is the default version.
2093
2094
   Overriding means that we already saw a definition for the
2095
   symbol SHORTNAME in a regular object, and it is overriding
2096
   the symbol defined in the dynamic object.
2097
2098
   When this happens, we actually want to change NAME, the
2099
   symbol we just added, to refer to SHORTNAME.  This will cause
2100
   references to NAME in the shared object to become references
2101
   to SHORTNAME in the regular object.  This is what we expect
2102
   when we override a function in a shared object: that the
2103
   references in the shared object will be mapped to the
2104
   definition in the regular object.  */
2105
2106
0
      while (hi->root.type == bfd_link_hash_indirect
2107
0
       || hi->root.type == bfd_link_hash_warning)
2108
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2109
2110
0
      h->root.type = bfd_link_hash_indirect;
2111
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2112
0
      if (h->def_dynamic)
2113
0
  {
2114
0
    h->def_dynamic = 0;
2115
0
    hi->ref_dynamic = 1;
2116
0
    if (hi->ref_regular
2117
0
        || hi->def_regular)
2118
0
      {
2119
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2120
0
    return false;
2121
0
      }
2122
0
  }
2123
2124
      /* Now set HI to H, so that the following code will set the
2125
   other fields correctly.  */
2126
0
      hi = h;
2127
0
    }
2128
2129
  /* Check if HI is a warning symbol.  */
2130
0
  if (hi->root.type == bfd_link_hash_warning)
2131
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2132
2133
  /* If there is a duplicate definition somewhere, then HI may not
2134
     point to an indirect symbol.  We will have reported an error to
2135
     the user in that case.  */
2136
2137
0
  if (hi->root.type == bfd_link_hash_indirect)
2138
0
    {
2139
0
      struct elf_link_hash_entry *ht;
2140
2141
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2142
0
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2143
2144
      /* If we first saw a reference to SHORTNAME with non-default
2145
   visibility, merge that visibility to the @@VER symbol.  */
2146
0
      elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2147
2148
      /* A reference to the SHORTNAME symbol from a dynamic library
2149
   will be satisfied by the versioned symbol at runtime.  In
2150
   effect, we have a reference to the versioned symbol.  */
2151
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2152
0
      hi->dynamic_def |= ht->dynamic_def;
2153
2154
      /* See if the new flags lead us to realize that the symbol must
2155
   be dynamic.  */
2156
0
      if (! *dynsym)
2157
0
  {
2158
0
    if (! dynamic)
2159
0
      {
2160
0
        if (! bfd_link_executable (info)
2161
0
      || hi->def_dynamic
2162
0
      || hi->ref_dynamic)
2163
0
    *dynsym = true;
2164
0
      }
2165
0
    else
2166
0
      {
2167
0
        if (hi->ref_regular)
2168
0
    *dynsym = true;
2169
0
      }
2170
0
  }
2171
0
    }
2172
2173
  /* We also need to define an indirection from the nondefault version
2174
     of the symbol.  */
2175
2176
0
 nondefault:
2177
0
  len = strlen (name);
2178
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2179
0
  if (shortname == NULL)
2180
0
    return false;
2181
0
  memcpy (shortname, name, shortlen);
2182
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2183
2184
  /* Once again, merge with any existing symbol.  */
2185
0
  type_change_ok = false;
2186
0
  size_change_ok = false;
2187
0
  tmp_sec = sec;
2188
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2189
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2190
0
            &type_change_ok, &size_change_ok, &matched))
2191
0
    return false;
2192
2193
0
  if (skip)
2194
0
    {
2195
0
      if (!dynamic
2196
0
    && h->root.type == bfd_link_hash_defweak
2197
0
    && hi->root.type == bfd_link_hash_defined)
2198
0
  {
2199
    /* We are handling a weak sym@@ver and attempting to define
2200
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2201
       new weak sym@ver because there is already a strong sym@ver.
2202
       However, sym@ver and sym@@ver are really the same symbol.
2203
       The existing strong sym@ver ought to override sym@@ver.  */
2204
0
    h->root.type = bfd_link_hash_defined;
2205
0
    h->root.u.def.section = hi->root.u.def.section;
2206
0
    h->root.u.def.value = hi->root.u.def.value;
2207
0
    hi->root.type = bfd_link_hash_indirect;
2208
0
    hi->root.u.i.link = &h->root;
2209
0
  }
2210
0
      else
2211
0
  return true;
2212
0
    }
2213
0
  else if (override)
2214
0
    {
2215
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2216
   the type of override we do in the case above unless it is
2217
   overridden by a versioned definition.  */
2218
0
      if (hi->root.type != bfd_link_hash_defined
2219
0
    && hi->root.type != bfd_link_hash_defweak)
2220
0
  _bfd_error_handler
2221
    /* xgettext:c-format */
2222
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2223
0
     abfd, shortname);
2224
0
      return true;
2225
0
    }
2226
0
  else
2227
0
    {
2228
0
      bh = &hi->root;
2229
0
      if (! (_bfd_generic_link_add_one_symbol
2230
0
       (info, abfd, shortname, BSF_INDIRECT,
2231
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2232
0
  return false;
2233
0
      hi = (struct elf_link_hash_entry *) bh;
2234
0
    }
2235
2236
  /* If there is a duplicate definition somewhere, then HI may not
2237
     point to an indirect symbol.  We will have reported an error
2238
     to the user in that case.  */
2239
0
  if (hi->root.type == bfd_link_hash_indirect)
2240
0
    {
2241
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2242
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2243
0
      hi->dynamic_def |= h->dynamic_def;
2244
2245
      /* If we first saw a reference to @VER symbol with
2246
   non-default visibility, merge that visibility to the
2247
   @@VER symbol.  */
2248
0
      elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2249
2250
      /* See if the new flags lead us to realize that the symbol
2251
   must be dynamic.  */
2252
0
      if (! *dynsym)
2253
0
  {
2254
0
    if (! dynamic)
2255
0
      {
2256
0
        if (! bfd_link_executable (info)
2257
0
      || hi->ref_dynamic)
2258
0
    *dynsym = true;
2259
0
      }
2260
0
    else
2261
0
      {
2262
0
        if (hi->ref_regular)
2263
0
    *dynsym = true;
2264
0
      }
2265
0
  }
2266
0
    }
2267
2268
0
  return true;
2269
0
}
2270

2271
/* This routine is used to export all defined symbols into the dynamic
2272
   symbol table.  It is called via elf_link_hash_traverse.  */
2273
2274
static bool
2275
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2276
0
{
2277
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2278
2279
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2280
0
  if (h->root.type == bfd_link_hash_indirect)
2281
0
    return true;
2282
2283
  /* Ignore this if we won't export it.  */
2284
0
  if (!eif->info->export_dynamic && !h->dynamic)
2285
0
    return true;
2286
2287
0
  if (h->dynindx == -1
2288
0
      && (h->def_regular || h->ref_regular)
2289
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2290
0
            h->root.root.string))
2291
0
    {
2292
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2293
0
  {
2294
0
    eif->failed = true;
2295
0
    return false;
2296
0
  }
2297
0
    }
2298
2299
0
  return true;
2300
0
}
2301

2302
/* Return true if linked against glibc.  Otherwise return false.  If
2303
   linked against glibc, add VERSION_DEP to the list of glibc version
2304
   dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
2305
   true, add VERSION_DEP to the version dependency list only if libc.so
2306
   defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
2307
   minor base version.  */
2308
2309
static bool
2310
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2311
          const char *version_dep,
2312
          int *glibc_minor_base,
2313
          bool *auto_version)
2314
0
{
2315
0
  Elf_Internal_Verneed *t;
2316
0
  Elf_Internal_Vernaux *a;
2317
0
  size_t amt;
2318
0
  int minor_version = -1;
2319
0
  bool added = false;
2320
0
  bool glibc = false;
2321
2322
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2323
0
       t != NULL;
2324
0
       t = t->vn_nextref)
2325
0
    {
2326
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2327
0
      if (soname != NULL && startswith (soname, "libc.so."))
2328
0
  break;
2329
0
    }
2330
2331
  /* Skip the shared library if it isn't libc.so.  */
2332
0
  if (t == NULL)
2333
0
    goto update_auto_version_and_return;
2334
2335
0
  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2336
0
    {
2337
      /* Return if VERSION_DEP dependency has been added.  */
2338
0
      if (a->vna_nodename == version_dep
2339
0
    || strcmp (a->vna_nodename, version_dep) == 0)
2340
0
  {
2341
0
    glibc = true;
2342
0
    goto update_auto_version_and_return;
2343
0
  }
2344
2345
      /* Check if libc.so provides GLIBC_2.XX version.  */
2346
0
      if (startswith (a->vna_nodename, "GLIBC_2."))
2347
0
  {
2348
0
    minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2349
0
    if (minor_version < *glibc_minor_base)
2350
0
      *glibc_minor_base = minor_version;
2351
0
  }
2352
0
    }
2353
2354
  /* Skip if it isn't linked against glibc.  */
2355
0
  if (minor_version < 0)
2356
0
    goto update_auto_version_and_return;
2357
2358
0
  glibc = true;
2359
2360
0
  if (auto_version && *auto_version)
2361
0
    {
2362
      /* Add VERSION_DEP to the version dependency list only if
2363
   libc.so defines VERSION_DEP.  */
2364
2365
0
      bool defined = false;
2366
0
      Elf_Internal_Verdef *d;
2367
2368
0
      for (d = elf_tdata (t->vn_bfd)->verdef;
2369
0
     d != NULL;
2370
0
     d = d->vd_nextdef)
2371
0
  if (strcmp (d->vd_nodename, version_dep) == 0)
2372
0
    {
2373
0
      defined = true;
2374
0
      break;
2375
0
    }
2376
2377
      /* Set *AUTO_VERSION to false and return true to indicate that
2378
   libc.so doesn't define VERSION_DEP.  */
2379
0
      if (!defined)
2380
0
  goto update_auto_version_and_return;
2381
0
    }
2382
2383
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2384
0
  if (startswith (version_dep, "GLIBC_2."))
2385
0
    {
2386
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2387
0
      if (minor_version <= *glibc_minor_base)
2388
0
  goto update_auto_version_and_return;
2389
0
    }
2390
2391
0
  amt = sizeof *a;
2392
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2393
0
  if (a == NULL)
2394
0
    {
2395
0
      rinfo->failed = true;
2396
0
      glibc = false;
2397
0
      goto update_auto_version_and_return;
2398
0
    }
2399
2400
0
  a->vna_nodename = version_dep;
2401
0
  a->vna_flags = 0;
2402
0
  a->vna_nextptr = t->vn_auxptr;
2403
0
  a->vna_other = rinfo->vers + 1;
2404
0
  ++rinfo->vers;
2405
2406
0
  t->vn_auxptr = a;
2407
2408
0
  added = true;
2409
2410
0
 update_auto_version_and_return:
2411
0
  if (auto_version)
2412
0
    *auto_version = added;
2413
2414
0
  return glibc;
2415
0
}
2416
2417
/* Add VERSION_DEP to the list of version dependencies when linked
2418
   against glibc.  */
2419
2420
bool
2421
_bfd_elf_link_add_glibc_version_dependency
2422
  (struct elf_find_verdep_info *rinfo,
2423
   const char *const version_dep[],
2424
   bool *auto_version)
2425
0
{
2426
0
  int glibc_minor_base = INT_MAX;
2427
2428
0
  do
2429
0
    {
2430
      /* Return if not linked against glibc.  */
2431
0
      if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
2432
0
               &glibc_minor_base, auto_version))
2433
0
  return false;
2434
0
      version_dep++;
2435
0
      auto_version++;
2436
0
    }
2437
0
  while (*version_dep != NULL);
2438
2439
0
  return true;
2440
0
}
2441
2442
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2443
   linked against glibc.  */
2444
2445
void
2446
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2447
0
{
2448
0
  if (rinfo->info->enable_dt_relr)
2449
0
    {
2450
0
      static const char *const version[] =
2451
0
  {
2452
0
    "GLIBC_ABI_DT_RELR",
2453
0
    NULL
2454
0
  };
2455
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
2456
0
    }
2457
0
}
2458
2459
/* Look through the symbols which are defined in other shared
2460
   libraries and referenced here.  Update the list of version
2461
   dependencies.  This will be put into the .gnu.version_r section.
2462
   This function is called via elf_link_hash_traverse.  */
2463
2464
static bool
2465
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2466
           void *data)
2467
0
{
2468
0
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2469
0
  Elf_Internal_Verneed *t;
2470
0
  Elf_Internal_Vernaux *a;
2471
0
  size_t amt;
2472
2473
  /* We only care about symbols defined in shared objects with version
2474
     information.  */
2475
0
  if (!h->def_dynamic
2476
0
      || h->def_regular
2477
0
      || h->dynindx == -1
2478
0
      || h->verinfo.verdef == NULL
2479
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2480
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2481
0
    return true;
2482
2483
  /* See if we already know about this version.  */
2484
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2485
0
       t != NULL;
2486
0
       t = t->vn_nextref)
2487
0
    {
2488
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2489
0
  continue;
2490
2491
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2492
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2493
0
    return true;
2494
2495
0
      break;
2496
0
    }
2497
2498
  /* This is a new version.  Add it to tree we are building.  */
2499
2500
0
  if (t == NULL)
2501
0
    {
2502
0
      amt = sizeof *t;
2503
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2504
0
      if (t == NULL)
2505
0
  {
2506
0
    rinfo->failed = true;
2507
0
    return false;
2508
0
  }
2509
2510
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2511
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2512
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2513
0
    }
2514
2515
0
  amt = sizeof *a;
2516
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2517
0
  if (a == NULL)
2518
0
    {
2519
0
      rinfo->failed = true;
2520
0
      return false;
2521
0
    }
2522
2523
  /* Note that we are copying a string pointer here, and testing it
2524
     above.  If bfd_elf_string_from_elf_section is ever changed to
2525
     discard the string data when low in memory, this will have to be
2526
     fixed.  */
2527
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2528
2529
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2530
0
  a->vna_nextptr = t->vn_auxptr;
2531
2532
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2533
0
  ++rinfo->vers;
2534
2535
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2536
2537
0
  t->vn_auxptr = a;
2538
2539
0
  return true;
2540
0
}
2541
2542
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2543
   hidden.  Set *T_P to NULL if there is no match.  */
2544
2545
static bool
2546
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2547
             struct elf_link_hash_entry *h,
2548
             const char *version_p,
2549
             struct bfd_elf_version_tree **t_p,
2550
             bool *hide)
2551
0
{
2552
0
  struct bfd_elf_version_tree *t;
2553
2554
  /* Look for the version.  If we find it, it is no longer weak.  */
2555
0
  for (t = info->version_info; t != NULL; t = t->next)
2556
0
    {
2557
0
      if (strcmp (t->name, version_p) == 0)
2558
0
  {
2559
0
    size_t len;
2560
0
    char *alc;
2561
0
    struct bfd_elf_version_expr *d;
2562
2563
0
    len = version_p - h->root.root.string;
2564
0
    alc = (char *) bfd_malloc (len);
2565
0
    if (alc == NULL)
2566
0
      return false;
2567
0
    memcpy (alc, h->root.root.string, len - 1);
2568
0
    alc[len - 1] = '\0';
2569
0
    if (alc[len - 2] == ELF_VER_CHR)
2570
0
      alc[len - 2] = '\0';
2571
2572
0
    h->verinfo.vertree = t;
2573
0
    t->used = true;
2574
0
    d = NULL;
2575
2576
0
    if (t->globals.list != NULL)
2577
0
      d = (*t->match) (&t->globals, NULL, alc);
2578
2579
    /* See if there is anything to force this symbol to
2580
       local scope.  */
2581
0
    if (d == NULL && t->locals.list != NULL)
2582
0
      {
2583
0
        d = (*t->match) (&t->locals, NULL, alc);
2584
0
        if (d != NULL
2585
0
      && h->dynindx != -1
2586
0
      && ! info->export_dynamic)
2587
0
    *hide = true;
2588
0
      }
2589
2590
0
    free (alc);
2591
0
    break;
2592
0
  }
2593
0
    }
2594
2595
0
  *t_p = t;
2596
2597
0
  return true;
2598
0
}
2599
2600
/* Return TRUE if the symbol H is hidden by version script.  */
2601
2602
bool
2603
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2604
           struct elf_link_hash_entry *h)
2605
0
{
2606
0
  const char *p;
2607
0
  bool hide = false;
2608
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2609
2610
  /* Version script only hides symbols defined in regular objects.  */
2611
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2612
0
    return true;
2613
2614
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2615
0
  if (p != NULL && h->verinfo.vertree == NULL)
2616
0
    {
2617
0
      struct bfd_elf_version_tree *t;
2618
2619
0
      ++p;
2620
0
      if (*p == ELF_VER_CHR)
2621
0
  ++p;
2622
2623
0
      if (*p != '\0'
2624
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2625
0
    && hide)
2626
0
  {
2627
0
    if (hide)
2628
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
2629
0
    return true;
2630
0
  }
2631
0
    }
2632
2633
  /* If we don't have a version for this symbol, see if we can find
2634
     something.  */
2635
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2636
0
    {
2637
0
      h->verinfo.vertree
2638
0
  = bfd_find_version_for_sym (info->version_info,
2639
0
            h->root.root.string, &hide);
2640
0
      if (h->verinfo.vertree != NULL && hide)
2641
0
  {
2642
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
2643
0
    return true;
2644
0
  }
2645
0
    }
2646
2647
0
  return false;
2648
0
}
2649
2650
/* Figure out appropriate versions for all the symbols.  We may not
2651
   have the version number script until we have read all of the input
2652
   files, so until that point we don't know which symbols should be
2653
   local.  This function is called via elf_link_hash_traverse.  */
2654
2655
static bool
2656
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2657
0
{
2658
0
  struct elf_info_failed *sinfo;
2659
0
  struct bfd_link_info *info;
2660
0
  elf_backend_data *bed;
2661
0
  struct elf_info_failed eif;
2662
0
  const char *p;
2663
0
  bool hide;
2664
2665
0
  sinfo = (struct elf_info_failed *) data;
2666
0
  info = sinfo->info;
2667
2668
  /* Fix the symbol flags.  */
2669
0
  eif.failed = false;
2670
0
  eif.info = info;
2671
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2672
0
    {
2673
0
      if (eif.failed)
2674
0
  sinfo->failed = true;
2675
0
      return false;
2676
0
    }
2677
2678
0
  bed = get_elf_backend_data (info->output_bfd);
2679
2680
  /* We only need version numbers for symbols defined in regular
2681
     objects.  */
2682
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2683
0
    {
2684
      /* Hide symbols defined in discarded input sections.  */
2685
0
      if ((h->root.type == bfd_link_hash_defined
2686
0
     || h->root.type == bfd_link_hash_defweak)
2687
0
    && discarded_section (h->root.u.def.section))
2688
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2689
0
      return true;
2690
0
    }
2691
2692
0
  hide = false;
2693
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2694
0
  if (p != NULL && h->verinfo.vertree == NULL)
2695
0
    {
2696
0
      struct bfd_elf_version_tree *t;
2697
2698
0
      ++p;
2699
0
      if (*p == ELF_VER_CHR)
2700
0
  ++p;
2701
2702
      /* If there is no version string, we can just return out.  */
2703
0
      if (*p == '\0')
2704
0
  return true;
2705
2706
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2707
0
  {
2708
0
    sinfo->failed = true;
2709
0
    return false;
2710
0
  }
2711
2712
0
      if (hide)
2713
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2714
2715
      /* If we are building an application, we need to create a
2716
   version node for this version.  */
2717
0
      if (t == NULL && bfd_link_executable (info))
2718
0
  {
2719
0
    struct bfd_elf_version_tree **pp;
2720
0
    int version_index;
2721
2722
    /* If we aren't going to export this symbol, we don't need
2723
       to worry about it.  */
2724
0
    if (h->dynindx == -1)
2725
0
      return true;
2726
2727
0
    t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2728
0
                sizeof *t);
2729
0
    if (t == NULL)
2730
0
      {
2731
0
        sinfo->failed = true;
2732
0
        return false;
2733
0
      }
2734
2735
0
    t->name = p;
2736
0
    t->name_indx = (unsigned int) -1;
2737
0
    t->used = true;
2738
2739
0
    version_index = 1;
2740
    /* Don't count anonymous version tag.  */
2741
0
    if (sinfo->info->version_info != NULL
2742
0
        && sinfo->info->version_info->vernum == 0)
2743
0
      version_index = 0;
2744
0
    for (pp = &sinfo->info->version_info;
2745
0
         *pp != NULL;
2746
0
         pp = &(*pp)->next)
2747
0
      ++version_index;
2748
0
    t->vernum = version_index;
2749
2750
0
    *pp = t;
2751
2752
0
    h->verinfo.vertree = t;
2753
0
  }
2754
0
      else if (t == NULL)
2755
0
  {
2756
    /* We could not find the version for a symbol when
2757
       generating a shared archive.  Return an error.  */
2758
0
    _bfd_error_handler
2759
      /* xgettext:c-format */
2760
0
      (_("%pB: version node not found for symbol %s"),
2761
0
       info->output_bfd, h->root.root.string);
2762
0
    bfd_set_error (bfd_error_bad_value);
2763
0
    sinfo->failed = true;
2764
0
    return false;
2765
0
  }
2766
0
    }
2767
2768
  /* If we don't have a version for this symbol, see if we can find
2769
     something.  */
2770
0
  if (!hide
2771
0
      && h->verinfo.vertree == NULL
2772
0
      && sinfo->info->version_info != NULL)
2773
0
    {
2774
0
      h->verinfo.vertree
2775
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2776
0
            h->root.root.string, &hide);
2777
0
      if (h->verinfo.vertree != NULL && hide)
2778
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2779
0
    }
2780
2781
0
  return true;
2782
0
}
2783

2784
/* Read and swap the relocs from the section indicated by SHDR.  This
2785
   may be either a REL or a RELA section.  The relocations are
2786
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2787
   which should have already been allocated to contain enough space.
2788
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2789
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2790
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2791
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2792
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2793
2794
   Returns FALSE if something goes wrong.  */
2795
2796
static bool
2797
elf_link_read_relocs_from_section (bfd *abfd,
2798
           const asection *sec,
2799
           Elf_Internal_Shdr *shdr,
2800
           void **external_relocs_addr,
2801
           size_t *external_relocs_size,
2802
           Elf_Internal_Rela *internal_relocs)
2803
0
{
2804
0
  elf_backend_data *bed;
2805
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2806
0
  const bfd_byte *erela;
2807
0
  const bfd_byte *erelaend;
2808
0
  Elf_Internal_Rela *irela;
2809
0
  Elf_Internal_Shdr *symtab_hdr;
2810
0
  size_t nsyms;
2811
0
  void *external_relocs = *external_relocs_addr;
2812
2813
  /* Position ourselves at the start of the section.  */
2814
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2815
0
    return false;
2816
2817
  /* Read the relocations.  */
2818
0
  *external_relocs_size = shdr->sh_size;
2819
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2820
0
         external_relocs_size,
2821
0
         external_relocs_addr, abfd, true))
2822
0
    return false;
2823
2824
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2825
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2826
2827
0
  bed = get_elf_backend_data (abfd);
2828
2829
  /* Convert the external relocations to the internal format.  */
2830
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2831
0
    swap_in = bed->s->swap_reloc_in;
2832
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2833
0
    swap_in = bed->s->swap_reloca_in;
2834
0
  else
2835
0
    {
2836
0
      bfd_set_error (bfd_error_wrong_format);
2837
0
      return false;
2838
0
    }
2839
2840
0
  erela = (const bfd_byte *) external_relocs;
2841
  /* Setting erelaend like this and comparing with <= handles case of
2842
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2843
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2844
0
  irela = internal_relocs;
2845
0
  while (erela <= erelaend)
2846
0
    {
2847
0
      bfd_vma r_symndx;
2848
2849
0
      (*swap_in) (abfd, erela, irela);
2850
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2851
0
      if (bed->s->arch_size == 64)
2852
0
  r_symndx >>= 24;
2853
0
      if (nsyms > 0)
2854
0
  {
2855
0
    if ((size_t) r_symndx >= nsyms)
2856
0
      {
2857
0
        _bfd_error_handler
2858
    /* xgettext:c-format */
2859
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2860
0
       " for offset %#" PRIx64 " in section `%pA'"),
2861
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2862
0
     (uint64_t) irela->r_offset, sec);
2863
0
        bfd_set_error (bfd_error_bad_value);
2864
0
        return false;
2865
0
      }
2866
0
  }
2867
0
      else if (r_symndx != STN_UNDEF)
2868
0
  {
2869
0
    _bfd_error_handler
2870
      /* xgettext:c-format */
2871
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2872
0
         " for offset %#" PRIx64 " in section `%pA'"
2873
0
         " when the object file has no symbol table"),
2874
0
       abfd, (uint64_t) r_symndx,
2875
0
       (uint64_t) irela->r_offset, sec);
2876
0
    bfd_set_error (bfd_error_bad_value);
2877
0
    return false;
2878
0
  }
2879
0
      irela += bed->s->int_rels_per_ext_rel;
2880
0
      erela += shdr->sh_entsize;
2881
0
    }
2882
2883
0
  return true;
2884
0
}
2885
2886
/* Read and swap the relocs for a section O.  They may have been
2887
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2888
   not NULL, they are used as buffers to read into.  They are known to
2889
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2890
   the return value is allocated using either malloc or bfd_alloc,
2891
   according to the KEEP_MEMORY argument.  If O has two relocation
2892
   sections (both REL and RELA relocations), then the REL_HDR
2893
   relocations will appear first in INTERNAL_RELOCS, followed by the
2894
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2895
   update cache_size.  */
2896
2897
Elf_Internal_Rela *
2898
_bfd_elf_link_info_read_relocs (bfd *abfd,
2899
        struct bfd_link_info *info,
2900
        const asection *o,
2901
        void *external_relocs,
2902
        Elf_Internal_Rela *internal_relocs,
2903
        bool keep_memory)
2904
0
{
2905
0
  void *alloc1 = NULL;
2906
0
  size_t alloc1_size;
2907
0
  Elf_Internal_Rela *alloc2 = NULL;
2908
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2909
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2910
0
  Elf_Internal_Rela *internal_rela_relocs;
2911
2912
0
  if (esdo->relocs != NULL)
2913
0
    return esdo->relocs;
2914
2915
0
  if (o->reloc_count == 0)
2916
0
    return NULL;
2917
2918
0
  if (internal_relocs == NULL)
2919
0
    {
2920
0
      bfd_size_type size;
2921
2922
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2923
0
      if (keep_memory && info)
2924
0
  info->cache_size += size;
2925
0
      internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2926
0
      if (internal_relocs == NULL)
2927
0
  return NULL;
2928
0
    }
2929
2930
0
  alloc1 = external_relocs;
2931
0
  internal_rela_relocs = internal_relocs;
2932
0
  if (esdo->rel.hdr)
2933
0
    {
2934
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2935
0
                &alloc1, &alloc1_size,
2936
0
                internal_relocs))
2937
0
  goto error_return;
2938
0
      external_relocs = (((bfd_byte *) external_relocs)
2939
0
       + esdo->rel.hdr->sh_size);
2940
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2941
0
             * bed->s->int_rels_per_ext_rel);
2942
0
    }
2943
2944
0
  if (esdo->rela.hdr
2945
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2946
0
                &alloc1, &alloc1_size,
2947
0
                internal_rela_relocs)))
2948
0
    goto error_return;
2949
2950
  /* Cache the results for next time, if we can.  */
2951
0
  if (keep_memory)
2952
0
    esdo->relocs = internal_relocs;
2953
2954
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2955
2956
  /* Don't free alloc2, since if it was allocated we are passing it
2957
     back (under the name of internal_relocs).  */
2958
2959
0
  return internal_relocs;
2960
2961
0
 error_return:
2962
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2963
0
  free (alloc2);
2964
0
  return NULL;
2965
0
}
2966
2967
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2968
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2969
   struct bfd_link_info.  */
2970
2971
Elf_Internal_Rela *
2972
_bfd_elf_link_read_relocs (bfd *abfd,
2973
         const asection *o,
2974
         void *external_relocs,
2975
         Elf_Internal_Rela *internal_relocs,
2976
         bool keep_memory)
2977
0
{
2978
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2979
0
           internal_relocs, keep_memory);
2980
2981
0
}
2982
2983
/* Compute the size of, and allocate space for, REL_HDR which is the
2984
   section header for a section containing relocations for O.  */
2985
2986
static bool
2987
_bfd_elf_link_size_reloc_section (bfd *abfd,
2988
          struct bfd_elf_section_reloc_data *reldata)
2989
0
{
2990
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2991
2992
  /* That allows us to calculate the size of the section.  */
2993
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2994
2995
  /* The contents field must last into write_object_contents, so we
2996
     allocate it with bfd_alloc rather than malloc.  Also since we
2997
     cannot be sure that the contents will actually be filled in,
2998
     we zero the allocated space.  */
2999
0
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
3000
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3001
0
    return false;
3002
3003
0
  if (reldata->hashes == NULL && reldata->count)
3004
0
    {
3005
0
      struct elf_link_hash_entry **p;
3006
3007
0
      p = ((struct elf_link_hash_entry **)
3008
0
     bfd_zmalloc (reldata->count * sizeof (*p)));
3009
0
      if (p == NULL)
3010
0
  return false;
3011
3012
0
      reldata->hashes = p;
3013
0
    }
3014
3015
0
  return true;
3016
0
}
3017
3018
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3019
   originated from the section given by INPUT_REL_HDR) to the
3020
   OUTPUT_BFD.  */
3021
3022
bool
3023
_bfd_elf_link_output_relocs (bfd *output_bfd,
3024
           asection *input_section,
3025
           Elf_Internal_Shdr *input_rel_hdr,
3026
           Elf_Internal_Rela *internal_relocs,
3027
           struct elf_link_hash_entry **rel_hash)
3028
0
{
3029
0
  Elf_Internal_Rela *irela;
3030
0
  Elf_Internal_Rela *irelaend;
3031
0
  bfd_byte *erel;
3032
0
  struct bfd_elf_section_reloc_data *output_reldata;
3033
0
  asection *output_section;
3034
0
  elf_backend_data *bed;
3035
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3036
0
  struct bfd_elf_section_data *esdo;
3037
3038
0
  output_section = input_section->output_section;
3039
3040
0
  bed = get_elf_backend_data (output_bfd);
3041
0
  esdo = elf_section_data (output_section);
3042
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3043
0
    {
3044
0
      output_reldata = &esdo->rel;
3045
0
      swap_out = bed->s->swap_reloc_out;
3046
0
    }
3047
0
  else if (esdo->rela.hdr
3048
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3049
0
    {
3050
0
      output_reldata = &esdo->rela;
3051
0
      swap_out = bed->s->swap_reloca_out;
3052
0
    }
3053
0
  else
3054
0
    {
3055
0
      _bfd_error_handler
3056
  /* xgettext:c-format */
3057
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3058
0
   output_bfd, input_section->owner, input_section);
3059
0
      bfd_set_error (bfd_error_wrong_format);
3060
0
      return false;
3061
0
    }
3062
3063
0
  erel = output_reldata->hdr->contents;
3064
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3065
0
  irela = internal_relocs;
3066
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3067
0
          * bed->s->int_rels_per_ext_rel);
3068
0
  while (irela < irelaend)
3069
0
    {
3070
0
      if (rel_hash && *rel_hash)
3071
0
  (*rel_hash)->has_reloc = 1;
3072
0
      (*swap_out) (output_bfd, irela, erel);
3073
0
      irela += bed->s->int_rels_per_ext_rel;
3074
0
      erel += input_rel_hdr->sh_entsize;
3075
0
      if (rel_hash)
3076
0
  rel_hash++;
3077
0
    }
3078
3079
  /* Bump the counter, so that we know where to add the next set of
3080
     relocations.  */
3081
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3082
3083
0
  return true;
3084
0
}
3085

3086
/* Make weak undefined symbols in PIE dynamic.  */
3087
3088
bool
3089
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3090
         struct elf_link_hash_entry *h)
3091
0
{
3092
0
  if (bfd_link_pie (info)
3093
0
      && h->dynindx == -1
3094
0
      && h->root.type == bfd_link_hash_undefweak)
3095
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3096
3097
0
  return true;
3098
0
}
3099
3100
/* Fix up the flags for a symbol.  This handles various cases which
3101
   can only be fixed after all the input files are seen.  This is
3102
   currently called by both adjust_dynamic_symbol and
3103
   assign_sym_version, which is unnecessary but perhaps more robust in
3104
   the face of future changes.  */
3105
3106
static bool
3107
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3108
         struct elf_info_failed *eif)
3109
0
{
3110
0
  elf_backend_data *bed;
3111
3112
  /* If this symbol was mentioned in a non-ELF file, try to set
3113
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3114
     permit a non-ELF file to correctly refer to a symbol defined in
3115
     an ELF dynamic object.  */
3116
0
  if (h->non_elf)
3117
0
    {
3118
0
      while (h->root.type == bfd_link_hash_indirect)
3119
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3120
3121
0
      if (h->root.type != bfd_link_hash_defined
3122
0
    && h->root.type != bfd_link_hash_defweak)
3123
0
  {
3124
0
    h->ref_regular = 1;
3125
0
    h->ref_regular_nonweak = 1;
3126
0
  }
3127
0
      else
3128
0
  {
3129
0
    if (h->root.u.def.section->owner != NULL
3130
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3131
0
      == bfd_target_elf_flavour))
3132
0
      {
3133
0
        h->ref_regular = 1;
3134
0
        h->ref_regular_nonweak = 1;
3135
0
      }
3136
0
    else
3137
0
      h->def_regular = 1;
3138
0
  }
3139
3140
0
      if (h->dynindx == -1
3141
0
    && (h->def_dynamic
3142
0
        || h->ref_dynamic))
3143
0
  {
3144
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3145
0
      {
3146
0
        eif->failed = true;
3147
0
        return false;
3148
0
      }
3149
0
  }
3150
0
    }
3151
0
  else
3152
0
    {
3153
      /* Unfortunately, NON_ELF is only correct if the symbol
3154
   was first seen in a non-ELF file.  Fortunately, if the symbol
3155
   was first seen in an ELF file, we're probably OK unless the
3156
   symbol was defined in a non-ELF file.  Catch that case here.
3157
   FIXME: We're still in trouble if the symbol was first seen in
3158
   a dynamic object, and then later in a non-ELF regular object.  */
3159
0
      if ((h->root.type == bfd_link_hash_defined
3160
0
     || h->root.type == bfd_link_hash_defweak)
3161
0
    && !h->def_regular
3162
0
    && (h->root.u.def.section->owner != NULL
3163
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3164
0
     != bfd_target_elf_flavour)
3165
0
        : (bfd_is_abs_section (h->root.u.def.section)
3166
0
     && !h->def_dynamic)))
3167
0
  h->def_regular = 1;
3168
0
    }
3169
3170
  /* Backend specific symbol fixup.  */
3171
0
  bed = get_elf_backend_data (eif->info->output_bfd);
3172
0
  if (bed->elf_backend_fixup_symbol
3173
0
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3174
0
    return false;
3175
3176
  /* If this is a final link, and the symbol was defined as a common
3177
     symbol in a regular object file, and there was no definition in
3178
     any dynamic object, then the linker will have allocated space for
3179
     the symbol in a common section but the DEF_REGULAR
3180
     flag will not have been set.  */
3181
0
  if (h->root.type == bfd_link_hash_defined
3182
0
      && !h->def_regular
3183
0
      && h->ref_regular
3184
0
      && !h->def_dynamic
3185
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3186
0
    h->def_regular = 1;
3187
3188
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3189
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3190
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3191
3192
  /* If a weak undefined symbol has non-default visibility, we also
3193
     hide it from the dynamic linker.  */
3194
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3195
0
     && h->root.type == bfd_link_hash_undefweak)
3196
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3197
3198
  /* A hidden versioned symbol in executable should be forced local if
3199
     it is is locally defined, not referenced by shared library and not
3200
     exported.  */
3201
0
  else if (bfd_link_executable (eif->info)
3202
0
     && h->versioned == versioned_hidden
3203
0
     && !eif->info->export_dynamic
3204
0
     && !h->dynamic
3205
0
     && !h->ref_dynamic
3206
0
     && h->def_regular)
3207
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3208
3209
  /* If -Bsymbolic was used (which means to bind references to global
3210
     symbols to the definition within the shared object), and this
3211
     symbol was defined in a regular object, then it actually doesn't
3212
     need a PLT entry.  Likewise, if the symbol has non-default
3213
     visibility.  If the symbol has hidden or internal visibility, we
3214
     will force it local.  */
3215
0
  else if (h->needs_plt
3216
0
     && bfd_link_pic (eif->info)
3217
0
     && is_elf_hash_table (eif->info->hash)
3218
0
     && (SYMBOLIC_BIND (eif->info, h)
3219
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3220
0
     && h->def_regular)
3221
0
    {
3222
0
      bool force_local;
3223
3224
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3225
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3226
0
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3227
0
    }
3228
3229
  /* If this is a weak defined symbol in a dynamic object, and we know
3230
     the real definition in the dynamic object, copy interesting flags
3231
     over to the real definition.  */
3232
0
  if (h->is_weakalias)
3233
0
    {
3234
0
      struct elf_link_hash_entry *def = weakdef (h);
3235
3236
      /* If the real definition is defined by a regular object file,
3237
   don't do anything special.  See the longer description in
3238
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3239
   bfd_link_hash_defined as it was when put on the alias list
3240
   then it must have originally been a versioned symbol (for
3241
   which a non-versioned indirect symbol is created) and later
3242
   a definition for the non-versioned symbol is found.  In that
3243
   case the indirection is flipped with the versioned symbol
3244
   becoming an indirect pointing at the non-versioned symbol.
3245
   Thus, not an alias any more.  */
3246
0
      if (def->def_regular
3247
0
    || def->root.type != bfd_link_hash_defined)
3248
0
  {
3249
0
    h = def;
3250
0
    while ((h = h->u.alias) != def)
3251
0
      h->is_weakalias = 0;
3252
0
  }
3253
0
      else
3254
0
  {
3255
0
    while (h->root.type == bfd_link_hash_indirect)
3256
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3257
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3258
0
          || h->root.type == bfd_link_hash_defweak);
3259
0
    BFD_ASSERT (def->def_dynamic);
3260
0
    (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3261
0
  }
3262
0
    }
3263
3264
0
  return true;
3265
0
}
3266
3267
/* Make the backend pick a good value for a dynamic symbol.  This is
3268
   called via elf_link_hash_traverse, and also calls itself
3269
   recursively.  */
3270
3271
static bool
3272
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3273
0
{
3274
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
3275
0
  struct elf_link_hash_table *htab;
3276
0
  elf_backend_data *bed;
3277
3278
0
  if (! is_elf_hash_table (eif->info->hash))
3279
0
    return false;
3280
3281
0
  htab = elf_hash_table (eif->info);
3282
0
  if (h->forced_local && h->dynindx != -1)
3283
0
    htab->has_local_dynsyms = true;
3284
3285
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3286
0
  if (h->root.type == bfd_link_hash_indirect)
3287
0
    return true;
3288
3289
  /* Fix the symbol flags.  */
3290
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3291
0
    return false;
3292
3293
0
  bed = get_elf_backend_data (htab->dynobj);
3294
3295
0
  if (h->root.type == bfd_link_hash_undefweak)
3296
0
    {
3297
0
      if (eif->info->dynamic_undefined_weak == 0)
3298
0
  (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3299
0
      else if (eif->info->dynamic_undefined_weak > 0
3300
0
         && h->ref_regular
3301
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3302
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3303
0
              h->root.root.string))
3304
0
  {
3305
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3306
0
      {
3307
0
        eif->failed = true;
3308
0
        return false;
3309
0
      }
3310
0
  }
3311
0
    }
3312
3313
  /* If this symbol does not require a PLT entry, and it is not
3314
     defined by a dynamic object, or is not referenced by a regular
3315
     object, ignore it.  We do have to handle a weak defined symbol,
3316
     even if no regular object refers to it, if we decided to add it
3317
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3318
     about symbols which are defined by one dynamic object and
3319
     referenced by another one?  */
3320
0
  if (!h->needs_plt
3321
0
      && h->type != STT_GNU_IFUNC
3322
0
      && (h->def_regular
3323
0
    || !h->def_dynamic
3324
0
    || (!h->ref_regular
3325
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3326
0
    {
3327
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3328
0
      return true;
3329
0
    }
3330
3331
  /* If we've already adjusted this symbol, don't do it again.  This
3332
     can happen via a recursive call.  */
3333
0
  if (h->dynamic_adjusted)
3334
0
    return true;
3335
3336
  /* Don't look at this symbol again.  Note that we must set this
3337
     after checking the above conditions, because we may look at a
3338
     symbol once, decide not to do anything, and then get called
3339
     recursively later after REF_REGULAR is set below.  */
3340
0
  h->dynamic_adjusted = 1;
3341
3342
  /* If this is a weak definition, and we know a real definition, and
3343
     the real symbol is not itself defined by a regular object file,
3344
     then get a good value for the real definition.  We handle the
3345
     real symbol first, for the convenience of the backend routine.
3346
3347
     Note that there is a confusing case here.  If the real definition
3348
     is defined by a regular object file, we don't get the real symbol
3349
     from the dynamic object, but we do get the weak symbol.  If the
3350
     processor backend uses a COPY reloc, then if some routine in the
3351
     dynamic object changes the real symbol, we will not see that
3352
     change in the corresponding weak symbol.  This is the way other
3353
     ELF linkers work as well, and seems to be a result of the shared
3354
     library model.
3355
3356
     I will clarify this issue.  Most SVR4 shared libraries define the
3357
     variable _timezone and define timezone as a weak synonym.  The
3358
     tzset call changes _timezone.  If you write
3359
       extern int timezone;
3360
       int _timezone = 5;
3361
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3362
     you might expect that, since timezone is a synonym for _timezone,
3363
     the same number will print both times.  However, if the processor
3364
     backend uses a COPY reloc, then actually timezone will be copied
3365
     into your process image, and, since you define _timezone
3366
     yourself, _timezone will not.  Thus timezone and _timezone will
3367
     wind up at different memory locations.  The tzset call will set
3368
     _timezone, leaving timezone unchanged.  */
3369
3370
0
  if (h->is_weakalias)
3371
0
    {
3372
0
      struct elf_link_hash_entry *def = weakdef (h);
3373
3374
      /* If we get to this point, there is an implicit reference to
3375
   the alias by a regular object file via the weak symbol H.  */
3376
0
      def->ref_regular = 1;
3377
3378
      /* Ensure that the backend adjust_dynamic_symbol function sees
3379
   the strong alias before H by recursively calling ourselves.  */
3380
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3381
0
  return false;
3382
0
    }
3383
3384
  /* If a symbol has no type and no size and does not require a PLT
3385
     entry, then we are probably about to do the wrong thing here: we
3386
     are probably going to create a COPY reloc for an empty object.
3387
     This case can arise when a shared object is built with assembly
3388
     code, and the assembly code fails to set the symbol type.  */
3389
0
  if (h->size == 0
3390
0
      && h->type == STT_NOTYPE
3391
0
      && !h->needs_plt)
3392
0
    _bfd_error_handler
3393
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3394
0
       h->root.root.string);
3395
3396
0
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3397
0
    {
3398
0
      eif->failed = true;
3399
0
      return false;
3400
0
    }
3401
3402
0
  return true;
3403
0
}
3404
3405
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3406
   DYNBSS.  */
3407
3408
bool
3409
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3410
            struct elf_link_hash_entry *h,
3411
            asection *dynbss)
3412
0
{
3413
0
  unsigned int power_of_two;
3414
0
  bfd_vma mask;
3415
0
  asection *sec = h->root.u.def.section;
3416
3417
  /* The section alignment of the definition is the maximum alignment
3418
     requirement of symbols defined in the section.  Since we don't
3419
     know the symbol alignment requirement, we start with the
3420
     maximum alignment and check low bits of the symbol address
3421
     for the minimum alignment.  */
3422
0
  power_of_two = bfd_section_alignment (sec);
3423
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3424
0
  while ((h->root.u.def.value & mask) != 0)
3425
0
    {
3426
0
       mask >>= 1;
3427
0
       --power_of_two;
3428
0
    }
3429
3430
  /* Adjust the section alignment if needed.  */
3431
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3432
0
    return false;
3433
3434
  /* We make sure that the symbol will be aligned properly.  */
3435
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3436
3437
  /* Define the symbol as being at this point in DYNBSS.  */
3438
0
  h->root.u.def.section = dynbss;
3439
0
  h->root.u.def.value = dynbss->size;
3440
3441
  /* Increment the size of DYNBSS to make room for the symbol.  */
3442
0
  dynbss->size += h->size;
3443
3444
  /* No error if extern_protected_data is true.  */
3445
0
  if (h->protected_def
3446
0
      && (!info->extern_protected_data
3447
0
    || (info->extern_protected_data < 0
3448
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3449
0
    info->callbacks->einfo
3450
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3451
0
       h->root.root.string);
3452
3453
0
  return true;
3454
0
}
3455
3456
/* Adjust all external symbols pointing into SEC_MERGE sections
3457
   to reflect the object merging within the sections.  */
3458
3459
static bool
3460
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3461
0
{
3462
0
  asection *sec;
3463
3464
0
  if ((h->root.type == bfd_link_hash_defined
3465
0
       || h->root.type == bfd_link_hash_defweak)
3466
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3467
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3468
0
    {
3469
0
      bfd *output_bfd = (bfd *) data;
3470
3471
0
      h->root.u.def.value =
3472
0
  _bfd_merged_section_offset (output_bfd,
3473
0
            &h->root.u.def.section,
3474
0
            h->root.u.def.value);
3475
0
    }
3476
3477
0
  return true;
3478
0
}
3479
3480
/* Returns false if the symbol referred to by H should be considered
3481
   to resolve local to the current module, and true if it should be
3482
   considered to bind dynamically.  */
3483
3484
bool
3485
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3486
         struct bfd_link_info *info,
3487
         bool not_local_protected)
3488
0
{
3489
0
  bool binding_stays_local_p;
3490
0
  elf_backend_data *bed;
3491
0
  struct elf_link_hash_table *hash_table;
3492
3493
0
  if (h == NULL)
3494
0
    return false;
3495
3496
0
  while (h->root.type == bfd_link_hash_indirect
3497
0
   || h->root.type == bfd_link_hash_warning)
3498
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3499
3500
  /* If it was forced local, then clearly it's not dynamic.  */
3501
0
  if (h->dynindx == -1)
3502
0
    return false;
3503
0
  if (h->forced_local)
3504
0
    return false;
3505
3506
  /* Identify the cases where name binding rules say that a
3507
     visible symbol resolves locally.  */
3508
0
  binding_stays_local_p = (bfd_link_executable (info)
3509
0
         || SYMBOLIC_BIND (info, h));
3510
3511
0
  switch (ELF_ST_VISIBILITY (h->other))
3512
0
    {
3513
0
    case STV_INTERNAL:
3514
0
    case STV_HIDDEN:
3515
0
      return false;
3516
3517
0
    case STV_PROTECTED:
3518
0
      hash_table = elf_hash_table (info);
3519
0
      if (!is_elf_hash_table (&hash_table->root))
3520
0
  return false;
3521
3522
0
      bed = get_elf_backend_data (hash_table->dynobj);
3523
3524
      /* Proper resolution for function pointer equality may require
3525
   that these symbols perhaps be resolved dynamically, even though
3526
   we should be resolving them to the current module.  */
3527
0
      if (!not_local_protected || !bed->is_function_type (h->type))
3528
0
  binding_stays_local_p = true;
3529
0
      break;
3530
3531
0
    default:
3532
0
      break;
3533
0
    }
3534
3535
  /* If it isn't defined locally, then clearly it's dynamic.  */
3536
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3537
0
    return true;
3538
3539
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3540
     us that it remains local.  */
3541
0
  return !binding_stays_local_p;
3542
0
}
3543
3544
/* Return true if the symbol referred to by H should be considered
3545
   to resolve local to the current module, and false otherwise.  Differs
3546
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3547
   undefined symbols.  The two functions are virtually identical except
3548
   for the place where dynindx == -1 is tested.  If that test is true,
3549
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3550
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3551
   defined symbols.
3552
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3553
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3554
   treatment of undefined weak symbols.  For those that do not make
3555
   undefined weak symbols dynamic, both functions may return false.  */
3556
3557
bool
3558
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3559
            struct bfd_link_info *info,
3560
            bool local_protected)
3561
0
{
3562
0
  elf_backend_data *bed;
3563
0
  struct elf_link_hash_table *hash_table;
3564
3565
  /* If it's a local sym, of course we resolve locally.  */
3566
0
  if (h == NULL)
3567
0
    return true;
3568
3569
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3570
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3571
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3572
0
    return true;
3573
3574
  /* Forced local symbols resolve locally.  */
3575
0
  if (h->forced_local)
3576
0
    return true;
3577
3578
  /* Common symbols that become definitions don't get the DEF_REGULAR
3579
     flag set, so test it first, and don't bail out.  */
3580
0
  if (ELF_COMMON_DEF_P (h))
3581
0
    /* Do nothing.  */;
3582
  /* If we don't have a definition in a regular file, then we can't
3583
     resolve locally.  The sym is either undefined or dynamic.  */
3584
0
  else if (!h->def_regular)
3585
0
    return false;
3586
3587
  /* Non-dynamic symbols resolve locally.  */
3588
0
  if (h->dynindx == -1)
3589
0
    return true;
3590
3591
  /* At this point, we know the symbol is defined and dynamic.  In an
3592
     executable it must resolve locally, likewise when building symbolic
3593
     shared libraries.  */
3594
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3595
0
    return true;
3596
3597
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3598
     with default visibility might not resolve locally.  */
3599
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3600
0
    return false;
3601
3602
0
  hash_table = elf_hash_table (info);
3603
0
  if (!is_elf_hash_table (&hash_table->root))
3604
0
    return true;
3605
3606
  /* STV_PROTECTED symbols with indirect external access are local. */
3607
0
  if (info->indirect_extern_access > 0)
3608
0
    return true;
3609
3610
0
  bed = get_elf_backend_data (hash_table->dynobj);
3611
3612
  /* If extern_protected_data is false, STV_PROTECTED non-function
3613
     symbols are local.  */
3614
0
  if ((!info->extern_protected_data
3615
0
       || (info->extern_protected_data < 0
3616
0
     && !bed->extern_protected_data))
3617
0
      && !bed->is_function_type (h->type))
3618
0
    return true;
3619
3620
  /* Function pointer equality tests may require that STV_PROTECTED
3621
     symbols be treated as dynamic symbols.  If the address of a
3622
     function not defined in an executable is set to that function's
3623
     plt entry in the executable, then the address of the function in
3624
     a shared library must also be the plt entry in the executable.  */
3625
0
  return local_protected;
3626
0
}
3627
3628
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3629
   aligned.  Returns the first TLS output section.  */
3630
3631
struct bfd_section *
3632
bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3633
0
{
3634
0
  struct bfd_section *sec, *tls;
3635
0
  unsigned int align = 0;
3636
3637
0
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
3638
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3639
0
      break;
3640
0
  tls = sec;
3641
3642
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3643
0
    if (sec->alignment_power > align)
3644
0
      align = sec->alignment_power;
3645
3646
0
  elf_hash_table (info)->tls_sec = tls;
3647
3648
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3649
     alignment, so that the tls segment starts aligned.  */
3650
0
  if (tls != NULL)
3651
0
    (void) bfd_link_align_section (tls, align);
3652
3653
0
  return tls;
3654
0
}
3655
3656
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3657
static bool
3658
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3659
          Elf_Internal_Sym *sym)
3660
0
{
3661
0
  elf_backend_data *bed;
3662
3663
  /* Local symbols do not count, but target specific ones might.  */
3664
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3665
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3666
0
    return false;
3667
3668
0
  bed = get_elf_backend_data (abfd);
3669
  /* Function symbols do not count.  */
3670
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3671
0
    return false;
3672
3673
  /* If the section is undefined, then so is the symbol.  */
3674
0
  if (sym->st_shndx == SHN_UNDEF)
3675
0
    return false;
3676
3677
  /* If the symbol is defined in the common section, then
3678
     it is a common definition and so does not count.  */
3679
0
  if (bed->common_definition (sym))
3680
0
    return false;
3681
3682
  /* If the symbol is in a target specific section then we
3683
     must rely upon the backend to tell us what it is.  */
3684
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3685
    /* FIXME - this function is not coded yet:
3686
3687
       return _bfd_is_global_symbol_definition (abfd, sym);
3688
3689
       Instead for now assume that the definition is not global,
3690
       Even if this is wrong, at least the linker will behave
3691
       in the same way that it used to do.  */
3692
0
    return false;
3693
3694
0
  return true;
3695
0
}
3696
3697
/* Search the symbol table of the archive element of the archive ABFD
3698
   whose archive map contains a mention of SYMDEF, and determine if
3699
   the symbol is defined in this element.  */
3700
static bool
3701
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3702
0
{
3703
0
  Elf_Internal_Shdr * hdr;
3704
0
  size_t symcount;
3705
0
  size_t extsymcount;
3706
0
  size_t extsymoff;
3707
0
  Elf_Internal_Sym *isymbuf;
3708
0
  Elf_Internal_Sym *isym;
3709
0
  Elf_Internal_Sym *isymend;
3710
0
  bool result;
3711
3712
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3713
0
  if (abfd == NULL)
3714
0
    return false;
3715
3716
0
  if (! bfd_check_format (abfd, bfd_object))
3717
0
    return false;
3718
3719
  /* Select the appropriate symbol table.  If we don't know if the
3720
     object file is an IR object, give linker LTO plugin a chance to
3721
     get the correct symbol table.  */
3722
0
  if (abfd->plugin_format == bfd_plugin_yes
3723
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3724
0
      || (abfd->plugin_format == bfd_plugin_unknown
3725
0
    && bfd_link_plugin_object_p (abfd)))
3726
0
    {
3727
      /* Use the IR symbol table if the object has been claimed by
3728
   plugin.  */
3729
0
      abfd = abfd->plugin_dummy_bfd;
3730
0
      hdr = &elf_symtab_hdr (abfd);
3731
0
    }
3732
0
  else
3733
0
    {
3734
0
      if (elf_use_dt_symtab_p (abfd))
3735
0
  {
3736
0
    bfd_set_error (bfd_error_wrong_format);
3737
0
    return false;
3738
0
  }
3739
3740
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3741
0
  hdr = &elf_symtab_hdr (abfd);
3742
0
      else
3743
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3744
0
    }
3745
3746
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3747
3748
  /* The sh_info field of the symtab header tells us where the
3749
     external symbols start.  We don't care about the local symbols.  */
3750
0
  if (elf_bad_symtab (abfd))
3751
0
    {
3752
0
      extsymcount = symcount;
3753
0
      extsymoff = 0;
3754
0
    }
3755
0
  else
3756
0
    {
3757
0
      extsymcount = symcount - hdr->sh_info;
3758
0
      extsymoff = hdr->sh_info;
3759
0
    }
3760
3761
0
  if (extsymcount == 0)
3762
0
    return false;
3763
3764
  /* Read in the symbol table.  */
3765
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3766
0
          NULL, NULL, NULL);
3767
0
  if (isymbuf == NULL)
3768
0
    return false;
3769
3770
  /* Scan the symbol table looking for SYMDEF.  */
3771
0
  result = false;
3772
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3773
0
    {
3774
0
      const char *name;
3775
3776
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3777
0
                isym->st_name);
3778
0
      if (name == NULL)
3779
0
  break;
3780
3781
0
      if (strcmp (name, symdef->name) == 0)
3782
0
  {
3783
0
    result = is_global_data_symbol_definition (abfd, isym);
3784
0
    break;
3785
0
  }
3786
0
    }
3787
3788
0
  free (isymbuf);
3789
3790
0
  return result;
3791
0
}
3792

3793
/* Add an entry to the .dynamic table.  */
3794
3795
bool
3796
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3797
          bfd_vma tag,
3798
          bfd_vma val)
3799
0
{
3800
0
  struct elf_link_hash_table *hash_table;
3801
0
  elf_backend_data *bed;
3802
0
  asection *s;
3803
0
  bfd_size_type newsize;
3804
0
  bfd_byte *newcontents;
3805
0
  Elf_Internal_Dyn dyn;
3806
3807
0
  hash_table = elf_hash_table (info);
3808
0
  if (! is_elf_hash_table (&hash_table->root))
3809
0
    return false;
3810
3811
0
  if (tag == DT_RELA || tag == DT_REL)
3812
0
    hash_table->dynamic_relocs = true;
3813
3814
0
  bed = get_elf_backend_data (hash_table->dynobj);
3815
0
  s = hash_table->dynamic;
3816
0
  BFD_ASSERT (s != NULL);
3817
3818
0
  newsize = s->size + bed->s->sizeof_dyn;
3819
0
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3820
0
  if (newcontents == NULL)
3821
0
    return false;
3822
3823
0
  dyn.d_tag = tag;
3824
0
  dyn.d_un.d_val = val;
3825
0
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3826
3827
0
  s->size = newsize;
3828
0
  s->contents = newcontents;
3829
3830
0
  return true;
3831
0
}
3832
3833
/* Strip zero-sized dynamic sections.  */
3834
3835
bool
3836
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3837
0
{
3838
0
  struct elf_link_hash_table *hash_table;
3839
0
  elf_backend_data *bed;
3840
0
  asection *s, *sdynamic, **pp;
3841
0
  asection *rela_dyn, *rel_dyn;
3842
0
  Elf_Internal_Dyn dyn;
3843
0
  bfd_byte *extdyn, *next;
3844
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3845
0
  bool strip_zero_sized;
3846
0
  bool strip_zero_sized_plt;
3847
3848
0
  if (bfd_link_relocatable (info))
3849
0
    return true;
3850
3851
0
  hash_table = elf_hash_table (info);
3852
0
  if (!is_elf_hash_table (&hash_table->root))
3853
0
    return false;
3854
3855
0
  if (!hash_table->dynobj)
3856
0
    return true;
3857
3858
0
  sdynamic= hash_table->dynamic;
3859
0
  if (!sdynamic)
3860
0
    return true;
3861
3862
0
  bed = get_elf_backend_data (hash_table->dynobj);
3863
0
  swap_dyn_in = bed->s->swap_dyn_in;
3864
3865
0
  strip_zero_sized = false;
3866
0
  strip_zero_sized_plt = false;
3867
3868
  /* Strip zero-sized dynamic sections.  */
3869
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3870
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3871
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3872
0
    if (s->size == 0
3873
0
  && (s == rela_dyn
3874
0
      || s == rel_dyn
3875
0
      || s == hash_table->srelplt->output_section
3876
0
      || s == hash_table->splt->output_section))
3877
0
      {
3878
0
  *pp = s->next;
3879
0
  info->output_bfd->section_count--;
3880
0
  strip_zero_sized = true;
3881
0
  if (s == rela_dyn)
3882
0
    s = rela_dyn;
3883
0
  if (s == rel_dyn)
3884
0
    s = rel_dyn;
3885
0
  else if (s == hash_table->splt->output_section)
3886
0
    {
3887
0
      s = hash_table->splt;
3888
0
      strip_zero_sized_plt = true;
3889
0
    }
3890
0
  else
3891
0
    s = hash_table->srelplt;
3892
0
  s->flags |= SEC_EXCLUDE;
3893
0
  s->output_section = bfd_abs_section_ptr;
3894
0
      }
3895
0
    else
3896
0
      pp = &s->next;
3897
3898
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3899
0
    for (extdyn = sdynamic->contents;
3900
0
   extdyn < sdynamic->contents + sdynamic->size;
3901
0
   extdyn = next)
3902
0
      {
3903
0
  next = extdyn + bed->s->sizeof_dyn;
3904
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3905
0
  switch (dyn.d_tag)
3906
0
    {
3907
0
    default:
3908
0
      break;
3909
0
    case DT_JMPREL:
3910
0
    case DT_PLTRELSZ:
3911
0
    case DT_PLTREL:
3912
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3913
         the procedure linkage table (the .plt section) has been
3914
         removed.  */
3915
0
      memmove (extdyn, next,
3916
0
         sdynamic->size - (next - sdynamic->contents));
3917
0
      next = extdyn;
3918
0
    }
3919
0
      }
3920
3921
0
  if (strip_zero_sized)
3922
0
    {
3923
      /* Regenerate program headers.  */
3924
0
      elf_seg_map (info->output_bfd) = NULL;
3925
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3926
0
    }
3927
3928
0
  return true;
3929
0
}
3930
3931
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3932
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3933
3934
int
3935
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3936
0
{
3937
0
  struct elf_link_hash_table *hash_table;
3938
0
  size_t strindex;
3939
0
  const char *soname;
3940
3941
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3942
0
    return -1;
3943
3944
0
  hash_table = elf_hash_table (info);
3945
0
  soname = elf_dt_name (abfd);
3946
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3947
0
  if (strindex == (size_t) -1)
3948
0
    return -1;
3949
3950
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3951
0
    {
3952
0
      asection *sdyn;
3953
0
      elf_backend_data *bed;
3954
0
      bfd_byte *extdyn;
3955
3956
0
      bed = get_elf_backend_data (hash_table->dynobj);
3957
0
      sdyn = hash_table->dynamic;
3958
0
      if (sdyn != NULL && sdyn->size != 0)
3959
0
  for (extdyn = sdyn->contents;
3960
0
       extdyn < sdyn->contents + sdyn->size;
3961
0
       extdyn += bed->s->sizeof_dyn)
3962
0
    {
3963
0
      Elf_Internal_Dyn dyn;
3964
3965
0
      bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3966
0
      if (dyn.d_tag == DT_NEEDED
3967
0
    && dyn.d_un.d_val == strindex)
3968
0
        {
3969
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3970
0
    return 1;
3971
0
        }
3972
0
    }
3973
0
    }
3974
3975
0
  if (!bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3976
0
    return -1;
3977
3978
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3979
0
    return -1;
3980
3981
0
  return 0;
3982
0
}
3983
3984
/* Return true if SONAME is on the needed list between NEEDED and STOP
3985
   (or the end of list if STOP is NULL), and needed by a library that
3986
   will be loaded.  */
3987
3988
static bool
3989
on_needed_list (const char *soname,
3990
    struct bfd_link_needed_list *needed,
3991
    struct bfd_link_needed_list *stop)
3992
0
{
3993
0
  struct bfd_link_needed_list *look;
3994
0
  for (look = needed; look != stop; look = look->next)
3995
0
    if (strcmp (soname, look->name) == 0
3996
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3997
      /* If needed by a library that itself is not directly
3998
         needed, recursively check whether that library is
3999
         indirectly needed.  Since we add DT_NEEDED entries to
4000
         the end of the list, library dependencies appear after
4001
         the library.  Therefore search prior to the current
4002
         LOOK, preventing possible infinite recursion.  */
4003
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4004
0
      return true;
4005
4006
0
  return false;
4007
0
}
4008
4009
/* Sort symbol by value, section, size, and type.  */
4010
static int
4011
elf_sort_symbol (const void *arg1, const void *arg2)
4012
0
{
4013
0
  const struct elf_link_hash_entry *h1;
4014
0
  const struct elf_link_hash_entry *h2;
4015
0
  bfd_signed_vma vdiff;
4016
0
  int sdiff;
4017
0
  const char *n1;
4018
0
  const char *n2;
4019
4020
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4021
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4022
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4023
0
  if (vdiff != 0)
4024
0
    return vdiff > 0 ? 1 : -1;
4025
4026
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4027
0
  if (sdiff != 0)
4028
0
    return sdiff;
4029
4030
  /* Sort so that sized symbols are selected over zero size symbols.  */
4031
0
  vdiff = h1->size - h2->size;
4032
0
  if (vdiff != 0)
4033
0
    return vdiff > 0 ? 1 : -1;
4034
4035
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4036
0
  if (h1->type != h2->type)
4037
0
    return h1->type - h2->type;
4038
4039
  /* If symbols are properly sized and typed, and multiple strong
4040
     aliases are not defined in a shared library by the user we
4041
     shouldn't get here.  Unfortunately linker script symbols like
4042
     __bss_start sometimes match a user symbol defined at the start of
4043
     .bss without proper size and type.  We'd like to preference the
4044
     user symbol over reserved system symbols.  Sort on leading
4045
     underscores.  */
4046
0
  n1 = h1->root.root.string;
4047
0
  n2 = h2->root.root.string;
4048
0
  while (*n1 == *n2)
4049
0
    {
4050
0
      if (*n1 == 0)
4051
0
  break;
4052
0
      ++n1;
4053
0
      ++n2;
4054
0
    }
4055
0
  if (*n1 == '_')
4056
0
    return -1;
4057
0
  if (*n2 == '_')
4058
0
    return 1;
4059
4060
  /* Final sort on name selects user symbols like '_u' over reserved
4061
     system symbols like '_Z' and also will avoid qsort instability.  */
4062
0
  return *n1 - *n2;
4063
0
}
4064
4065
/* This function is used to adjust offsets into .dynstr for
4066
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4067
4068
static bool
4069
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4070
0
{
4071
0
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
4072
4073
0
  if (h->dynindx != -1)
4074
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4075
0
  return true;
4076
0
}
4077
4078
/* Assign string offsets in .dynstr, update all structures referencing
4079
   them.  */
4080
4081
static bool
4082
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4083
0
{
4084
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4085
0
  struct elf_link_local_dynamic_entry *entry;
4086
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4087
0
  bfd *dynobj = hash_table->dynobj;
4088
0
  asection *sdyn;
4089
0
  bfd_size_type size;
4090
0
  elf_backend_data *bed;
4091
0
  bfd_byte *extdyn;
4092
4093
0
  _bfd_elf_strtab_finalize (dynstr);
4094
0
  size = _bfd_elf_strtab_size (dynstr);
4095
4096
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4097
4098
0
  if (info->callbacks->examine_strtab)
4099
0
    info->callbacks->examine_strtab (dynstr);
4100
4101
0
  bed = get_elf_backend_data (dynobj);
4102
0
  sdyn = hash_table->dynamic;
4103
0
  BFD_ASSERT (sdyn != NULL);
4104
4105
  /* Update all .dynamic entries referencing .dynstr strings.  */
4106
0
  for (extdyn = sdyn->contents;
4107
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4108
0
       extdyn += bed->s->sizeof_dyn)
4109
0
    {
4110
0
      Elf_Internal_Dyn dyn;
4111
4112
0
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4113
0
      switch (dyn.d_tag)
4114
0
  {
4115
0
  case DT_STRSZ:
4116
0
    dyn.d_un.d_val = size;
4117
0
    break;
4118
0
  case DT_NEEDED:
4119
0
  case DT_SONAME:
4120
0
  case DT_RPATH:
4121
0
  case DT_RUNPATH:
4122
0
  case DT_FILTER:
4123
0
  case DT_AUXILIARY:
4124
0
  case DT_AUDIT:
4125
0
  case DT_DEPAUDIT:
4126
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4127
0
    break;
4128
0
  default:
4129
0
    continue;
4130
0
  }
4131
0
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4132
0
    }
4133
4134
  /* Now update local dynamic symbols.  */
4135
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4136
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4137
0
              entry->isym.st_name);
4138
4139
  /* And the rest of dynamic symbols.  */
4140
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4141
4142
  /* Adjust version definitions.  */
4143
0
  if (elf_tdata (output_bfd)->cverdefs)
4144
0
    {
4145
0
      asection *s;
4146
0
      bfd_byte *p;
4147
0
      size_t i;
4148
0
      Elf_Internal_Verdef def;
4149
0
      Elf_Internal_Verdaux defaux;
4150
4151
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4152
0
      p = s->contents;
4153
0
      do
4154
0
  {
4155
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4156
0
           &def);
4157
0
    p += sizeof (Elf_External_Verdef);
4158
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4159
0
      continue;
4160
0
    for (i = 0; i < def.vd_cnt; ++i)
4161
0
      {
4162
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4163
0
          (Elf_External_Verdaux *) p, &defaux);
4164
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4165
0
              defaux.vda_name);
4166
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4167
0
           &defaux, (Elf_External_Verdaux *) p);
4168
0
        p += sizeof (Elf_External_Verdaux);
4169
0
      }
4170
0
  }
4171
0
      while (def.vd_next);
4172
0
    }
4173
4174
  /* Adjust version references.  */
4175
0
  if (elf_tdata (output_bfd)->verref)
4176
0
    {
4177
0
      asection *s;
4178
0
      bfd_byte *p;
4179
0
      size_t i;
4180
0
      Elf_Internal_Verneed need;
4181
0
      Elf_Internal_Vernaux needaux;
4182
4183
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4184
0
      p = s->contents;
4185
0
      do
4186
0
  {
4187
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4188
0
            &need);
4189
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4190
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4191
0
             (Elf_External_Verneed *) p);
4192
0
    p += sizeof (Elf_External_Verneed);
4193
0
    for (i = 0; i < need.vn_cnt; ++i)
4194
0
      {
4195
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4196
0
          (Elf_External_Vernaux *) p, &needaux);
4197
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4198
0
               needaux.vna_name);
4199
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4200
0
           &needaux,
4201
0
           (Elf_External_Vernaux *) p);
4202
0
        p += sizeof (Elf_External_Vernaux);
4203
0
      }
4204
0
  }
4205
0
      while (need.vn_next);
4206
0
    }
4207
4208
0
  return true;
4209
0
}
4210

4211
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4212
   The default is to only match when the INPUT and OUTPUT are exactly
4213
   the same target.  */
4214
4215
bool
4216
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4217
            const bfd_target *output)
4218
0
{
4219
0
  return input == output;
4220
0
}
4221
4222
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4223
   This version is used when different targets for the same architecture
4224
   are virtually identical.  */
4225
4226
bool
4227
_bfd_elf_relocs_compatible (const bfd_target *input,
4228
          const bfd_target *output)
4229
0
{
4230
0
  elf_backend_data *obed, *ibed;
4231
4232
0
  if (input == output)
4233
0
    return true;
4234
4235
0
  ibed = xvec_get_elf_backend_data (input);
4236
0
  obed = xvec_get_elf_backend_data (output);
4237
4238
0
  if (ibed->arch != obed->arch)
4239
0
    return false;
4240
4241
  /* If both backends are using this function, deem them compatible.  */
4242
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4243
0
}
4244
4245
/* Make a special call to the linker "notice" function to tell it that
4246
   we are about to handle an as-needed lib, or have finished
4247
   processing the lib.  */
4248
4249
bool
4250
_bfd_elf_notice_as_needed (bfd *ibfd,
4251
         struct bfd_link_info *info,
4252
         enum notice_asneeded_action act)
4253
0
{
4254
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4255
0
}
4256
4257
/* Call ACTION on each relocation in an ELF object file.  */
4258
4259
bool
4260
_bfd_elf_link_iterate_on_relocs
4261
  (bfd *abfd, struct bfd_link_info *info,
4262
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4263
       const Elf_Internal_Rela *))
4264
0
{
4265
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4266
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4267
4268
  /* If this object is the same format as the output object, and it is
4269
     not a shared library, then let the backend look through the
4270
     relocs.
4271
4272
     This is required to build global offset table entries and to
4273
     arrange for dynamic relocs.  It is not required for the
4274
     particular common case of linking non PIC code, even when linking
4275
     against shared libraries, but unfortunately there is no way of
4276
     knowing whether an object file has been compiled PIC or not.
4277
     Looking through the relocs is not particularly time consuming.
4278
     The problem is that we must either (1) keep the relocs in memory,
4279
     which causes the linker to require additional runtime memory or
4280
     (2) read the relocs twice from the input file, which wastes time.
4281
     This would be a good case for using mmap.
4282
4283
     I have no idea how to handle linking PIC code into a file of a
4284
     different format.  It probably can't be done.  */
4285
0
  if ((abfd->flags & DYNAMIC) == 0
4286
0
      && is_elf_hash_table (&htab->root)
4287
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4288
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4289
0
    {
4290
0
      asection *o;
4291
4292
0
      for (o = abfd->sections; o != NULL; o = o->next)
4293
0
  {
4294
0
    Elf_Internal_Rela *internal_relocs;
4295
0
    bool ok;
4296
4297
    /* Don't check relocations in excluded sections.  Don't do
4298
       anything special with non-loaded, non-alloced sections.
4299
       In particular, any relocs in such sections should not
4300
       affect GOT and PLT reference counting (ie.  we don't
4301
       allow them to create GOT or PLT entries), there's no
4302
       possibility or desire to optimize TLS relocs, and
4303
       there's not much point in propagating relocs to shared
4304
       libs that the dynamic linker won't relocate.  */
4305
0
    if ((o->flags & SEC_ALLOC) == 0
4306
0
        || (o->flags & SEC_RELOC) == 0
4307
0
        || (o->flags & SEC_EXCLUDE) != 0
4308
0
        || o->reloc_count == 0
4309
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4310
0
      && (o->flags & SEC_DEBUGGING) != 0)
4311
0
        || bfd_is_abs_section (o->output_section))
4312
0
      continue;
4313
4314
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4315
0
      (abfd, info, o, NULL, NULL,
4316
0
       _bfd_elf_link_keep_memory (info));
4317
0
    if (internal_relocs == NULL)
4318
0
      return false;
4319
4320
0
    ok = action (abfd, info, o, internal_relocs);
4321
4322
0
    if (elf_section_data (o)->relocs != internal_relocs)
4323
0
      free (internal_relocs);
4324
4325
0
    if (! ok)
4326
0
      return false;
4327
0
  }
4328
0
    }
4329
4330
0
  return true;
4331
0
}
4332
4333
/* Check relocations in an ELF object file.  This is called after
4334
   all input files have been opened.  */
4335
4336
bool
4337
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4338
0
{
4339
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4340
0
  if (bed->check_relocs != NULL)
4341
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4342
0
              bed->check_relocs);
4343
0
  return true;
4344
0
}
4345
4346
/* An entry in the first definition hash table.  */
4347
4348
struct elf_link_first_hash_entry
4349
{
4350
  struct bfd_hash_entry root;
4351
  /* The object of the first definition.  */
4352
  bfd *abfd;
4353
};
4354
4355
/* The function to create a new entry in the first definition hash
4356
   table.  */
4357
4358
static struct bfd_hash_entry *
4359
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4360
           struct bfd_hash_table *table,
4361
           const char *string)
4362
0
{
4363
0
  struct elf_link_first_hash_entry *ret =
4364
0
    (struct elf_link_first_hash_entry *) entry;
4365
4366
  /* Allocate the structure if it has not already been allocated by a
4367
     subclass.  */
4368
0
  if (ret == NULL)
4369
0
    ret = (struct elf_link_first_hash_entry *)
4370
0
  bfd_hash_allocate (table,
4371
0
         sizeof (struct elf_link_first_hash_entry));
4372
0
  if (ret == NULL)
4373
0
    return NULL;
4374
4375
  /* Call the allocation method of the superclass.  */
4376
0
  ret = ((struct elf_link_first_hash_entry *)
4377
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4378
0
         string));
4379
0
  if (ret != NULL)
4380
0
    ret->abfd = NULL;
4381
4382
0
  return (struct bfd_hash_entry *) ret;
4383
0
}
4384
4385
/* Add the symbol NAME from ABFD to first hash.  */
4386
4387
static void
4388
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4389
          const char *name, bool copy)
4390
0
{
4391
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4392
  /* Skip if there is no first hash.  */
4393
0
  if (htab->first_hash == NULL)
4394
0
    return;
4395
4396
0
  struct elf_link_first_hash_entry *e
4397
0
    = ((struct elf_link_first_hash_entry *)
4398
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4399
0
  if (e == NULL)
4400
0
    info->callbacks->fatal
4401
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4402
4403
0
  if (e->abfd == NULL)
4404
    /* Store ABFD in abfd.  */
4405
0
    e->abfd = abfd;
4406
0
}
4407
4408
/* Add symbols from an ELF object file to the linker hash table.  */
4409
4410
static bool
4411
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4412
0
{
4413
0
  Elf_Internal_Ehdr *ehdr;
4414
0
  Elf_Internal_Shdr *hdr;
4415
0
  size_t symcount;
4416
0
  size_t extsymcount;
4417
0
  size_t extsymoff;
4418
0
  struct elf_link_hash_entry **sym_hash;
4419
0
  bool dynamic;
4420
0
  Elf_External_Versym *extversym = NULL;
4421
0
  Elf_External_Versym *extversym_end = NULL;
4422
0
  Elf_External_Versym *ever;
4423
0
  struct elf_link_hash_entry *weaks;
4424
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4425
0
  size_t nondeflt_vers_cnt = 0;
4426
0
  Elf_Internal_Sym *isymbuf = NULL;
4427
0
  Elf_Internal_Sym *isym;
4428
0
  Elf_Internal_Sym *isymend;
4429
0
  elf_backend_data *bed, *obed;
4430
0
  bool add_needed;
4431
0
  struct elf_link_hash_table *htab;
4432
0
  void *alloc_mark = NULL;
4433
0
  struct bfd_hash_entry **old_table = NULL;
4434
0
  unsigned int old_size = 0;
4435
0
  unsigned int old_count = 0;
4436
0
  void *old_tab = NULL;
4437
0
  void *old_ent;
4438
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4439
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4440
0
  void *old_strtab = NULL;
4441
0
  size_t tabsize = 0;
4442
0
  asection *s;
4443
0
  bool just_syms;
4444
4445
0
  htab = elf_hash_table (info);
4446
0
  bed = get_elf_backend_data (abfd);
4447
0
  obed = get_elf_backend_data (info->output_bfd);
4448
4449
0
  if (elf_use_dt_symtab_p (abfd))
4450
0
    {
4451
0
      bfd_set_error (bfd_error_wrong_format);
4452
0
      return false;
4453
0
    }
4454
4455
0
  if ((abfd->flags & DYNAMIC) == 0)
4456
0
    {
4457
0
      dynamic = false;
4458
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4459
0
    && is_elf_hash_table (&htab->root)
4460
0
    && htab->first_hash == NULL)
4461
0
  {
4462
    /* Initialize first_hash for an IR input.  */
4463
0
    htab->first_hash = (struct bfd_hash_table *)
4464
0
      bfd_malloc (sizeof (struct bfd_hash_table));
4465
0
    if (htab->first_hash == NULL
4466
0
        || !bfd_hash_table_init
4467
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4468
0
        sizeof (struct elf_link_first_hash_entry)))
4469
0
      info->callbacks->fatal
4470
0
        (_("%P: first_hash failed to create: %E\n"));
4471
0
  }
4472
0
    }
4473
0
  else
4474
0
    {
4475
0
      dynamic = true;
4476
4477
      /* You can't use -r against a dynamic object.  Also, there's no
4478
   hope of using a dynamic object which does not exactly match
4479
   the format of the output file.  */
4480
0
      if (bfd_link_relocatable (info)
4481
0
    || !is_elf_hash_table (&htab->root)
4482
0
    || info->output_bfd->xvec != abfd->xvec)
4483
0
  {
4484
0
    if (bfd_link_relocatable (info))
4485
0
      bfd_set_error (bfd_error_invalid_operation);
4486
0
    else
4487
0
      bfd_set_error (bfd_error_wrong_format);
4488
0
    goto error_return;
4489
0
  }
4490
0
    }
4491
4492
0
  ehdr = elf_elfheader (abfd);
4493
0
  if (info->warn_alternate_em
4494
0
      && bed->elf_machine_code != ehdr->e_machine
4495
0
      && ((bed->elf_machine_alt1 != 0
4496
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4497
0
    || (bed->elf_machine_alt2 != 0
4498
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4499
0
    _bfd_error_handler
4500
      /* xgettext:c-format */
4501
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4502
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4503
4504
  /* As a GNU extension, any input sections which are named
4505
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4506
     symbol.  This differs from .gnu.warning sections, which generate
4507
     warnings when they are included in an output file.  */
4508
  /* PR 12761: Also generate this warning when building shared libraries.  */
4509
0
  for (s = abfd->sections; s != NULL; s = s->next)
4510
0
    {
4511
0
      const char *name;
4512
4513
0
      name = bfd_section_name (s);
4514
0
      if (startswith (name, ".gnu.warning."))
4515
0
  {
4516
0
    char *msg;
4517
0
    bfd_size_type sz;
4518
4519
0
    name += sizeof ".gnu.warning." - 1;
4520
4521
    /* If this is a shared object, then look up the symbol
4522
       in the hash table.  If it is there, and it is already
4523
       been defined, then we will not be using the entry
4524
       from this shared object, so we don't need to warn.
4525
       FIXME: If we see the definition in a regular object
4526
       later on, we will warn, but we shouldn't.  The only
4527
       fix is to keep track of what warnings we are supposed
4528
       to emit, and then handle them all at the end of the
4529
       link.  */
4530
0
    if (dynamic)
4531
0
      {
4532
0
        struct elf_link_hash_entry *h;
4533
4534
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4535
4536
        /* FIXME: What about bfd_link_hash_common?  */
4537
0
        if (h != NULL
4538
0
      && (h->root.type == bfd_link_hash_defined
4539
0
          || h->root.type == bfd_link_hash_defweak))
4540
0
    continue;
4541
0
      }
4542
4543
0
    sz = s->size;
4544
0
    msg = (char *) bfd_alloc (abfd, sz + 1);
4545
0
    if (msg == NULL)
4546
0
      goto error_return;
4547
4548
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4549
0
      goto error_return;
4550
4551
0
    msg[sz] = '\0';
4552
4553
0
    if (! (_bfd_generic_link_add_one_symbol
4554
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4555
0
      false, bed->collect, NULL)))
4556
0
      goto error_return;
4557
4558
0
    if (bfd_link_executable (info))
4559
0
      {
4560
        /* Clobber the section size so that the warning does
4561
     not get copied into the output file.  */
4562
0
        s->size = 0;
4563
4564
        /* Also set SEC_EXCLUDE, so that symbols defined in
4565
     the warning section don't get copied to the output.  */
4566
0
        s->flags |= SEC_EXCLUDE;
4567
0
      }
4568
0
  }
4569
0
    }
4570
4571
0
  just_syms = ((s = abfd->sections) != NULL
4572
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4573
4574
0
  add_needed = true;
4575
0
  if (! dynamic)
4576
0
    {
4577
      /* If we are creating a shared library, create all the dynamic
4578
   sections immediately.  We need to attach them to something,
4579
   so we attach them to this BFD, provided it is the right
4580
   format and is not from ld --just-symbols.  Always create the
4581
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4582
   are no input BFD's of the same format as the output, we can't
4583
   make a shared library.  */
4584
0
      if (!just_syms
4585
0
    && (bfd_link_pic (info)
4586
0
        || (!bfd_link_relocatable (info)
4587
0
      && info->nointerp
4588
0
      && (info->export_dynamic || info->dynamic)))
4589
0
    && is_elf_hash_table (&htab->root)
4590
0
    && info->output_bfd->xvec == abfd->xvec
4591
0
    && !htab->dynamic_sections_created)
4592
0
  {
4593
0
    if (!bfd_elf_link_create_dynamic_sections (abfd, info))
4594
0
      goto error_return;
4595
0
  }
4596
0
    }
4597
0
  else if (!is_elf_hash_table (&htab->root))
4598
0
    goto error_return;
4599
0
  else
4600
0
    {
4601
0
      const char *soname = NULL;
4602
0
      char *audit = NULL;
4603
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4604
0
      const Elf_Internal_Phdr *phdr;
4605
0
      struct elf_link_loaded_list *loaded_lib;
4606
4607
      /* ld --just-symbols and dynamic objects don't mix very well.
4608
   ld shouldn't allow it.  */
4609
0
      if (just_syms)
4610
0
  abort ();
4611
4612
      /* If this dynamic lib was specified on the command line with
4613
   --as-needed in effect, then we don't want to add a DT_NEEDED
4614
   tag unless the lib is actually used.  Similary for libs brought
4615
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4616
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4617
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4618
   all.  */
4619
0
      add_needed = (elf_dyn_lib_class (abfd)
4620
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4621
0
           | DYN_NO_NEEDED)) == 0;
4622
4623
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4624
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4625
0
  {
4626
0
    bfd_byte *dynbuf;
4627
0
    bfd_byte *extdyn;
4628
0
    unsigned int elfsec;
4629
0
    unsigned long shlink;
4630
4631
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4632
0
      {
4633
0
      error_free_dyn:
4634
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4635
0
        goto error_return;
4636
0
      }
4637
4638
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4639
0
    if (elfsec == SHN_BAD)
4640
0
      goto error_free_dyn;
4641
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4642
4643
0
    for (extdyn = dynbuf;
4644
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4645
0
         extdyn += bed->s->sizeof_dyn)
4646
0
      {
4647
0
        Elf_Internal_Dyn dyn;
4648
4649
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4650
0
        if (dyn.d_tag == DT_SONAME)
4651
0
    {
4652
0
      unsigned int tagv = dyn.d_un.d_val;
4653
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4654
0
      if (soname == NULL)
4655
0
        goto error_free_dyn;
4656
0
    }
4657
0
        if (dyn.d_tag == DT_NEEDED)
4658
0
    {
4659
0
      struct bfd_link_needed_list *n, **pn;
4660
0
      char *fnm, *anm;
4661
0
      unsigned int tagv = dyn.d_un.d_val;
4662
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4663
4664
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4665
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4666
0
      if (n == NULL || fnm == NULL)
4667
0
        goto error_free_dyn;
4668
0
      amt = strlen (fnm) + 1;
4669
0
      anm = (char *) bfd_alloc (abfd, amt);
4670
0
      if (anm == NULL)
4671
0
        goto error_free_dyn;
4672
0
      memcpy (anm, fnm, amt);
4673
0
      n->name = anm;
4674
0
      n->by = abfd;
4675
0
      n->next = NULL;
4676
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4677
0
        ;
4678
0
      *pn = n;
4679
0
    }
4680
0
        if (dyn.d_tag == DT_RUNPATH)
4681
0
    {
4682
0
      struct bfd_link_needed_list *n, **pn;
4683
0
      char *fnm, *anm;
4684
0
      unsigned int tagv = dyn.d_un.d_val;
4685
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4686
4687
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4688
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4689
0
      if (n == NULL || fnm == NULL)
4690
0
        goto error_free_dyn;
4691
0
      amt = strlen (fnm) + 1;
4692
0
      anm = (char *) bfd_alloc (abfd, amt);
4693
0
      if (anm == NULL)
4694
0
        goto error_free_dyn;
4695
0
      memcpy (anm, fnm, amt);
4696
0
      n->name = anm;
4697
0
      n->by = abfd;
4698
0
      n->next = NULL;
4699
0
      for (pn = & runpath;
4700
0
           *pn != NULL;
4701
0
           pn = &(*pn)->next)
4702
0
        ;
4703
0
      *pn = n;
4704
0
    }
4705
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4706
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4707
0
    {
4708
0
      struct bfd_link_needed_list *n, **pn;
4709
0
      char *fnm, *anm;
4710
0
      unsigned int tagv = dyn.d_un.d_val;
4711
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4712
4713
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4714
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4715
0
      if (n == NULL || fnm == NULL)
4716
0
        goto error_free_dyn;
4717
0
      amt = strlen (fnm) + 1;
4718
0
      anm = (char *) bfd_alloc (abfd, amt);
4719
0
      if (anm == NULL)
4720
0
        goto error_free_dyn;
4721
0
      memcpy (anm, fnm, amt);
4722
0
      n->name = anm;
4723
0
      n->by = abfd;
4724
0
      n->next = NULL;
4725
0
      for (pn = & rpath;
4726
0
           *pn != NULL;
4727
0
           pn = &(*pn)->next)
4728
0
        ;
4729
0
      *pn = n;
4730
0
    }
4731
0
        if (dyn.d_tag == DT_AUDIT)
4732
0
    {
4733
0
      unsigned int tagv = dyn.d_un.d_val;
4734
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4735
0
    }
4736
0
        if (dyn.d_tag == DT_FLAGS_1)
4737
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4738
0
      }
4739
4740
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4741
0
  }
4742
4743
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4744
   frees all more recently bfd_alloc'd blocks as well.  */
4745
0
      if (runpath)
4746
0
  rpath = runpath;
4747
4748
0
      if (rpath)
4749
0
  {
4750
0
    struct bfd_link_needed_list **pn;
4751
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4752
0
      ;
4753
0
    *pn = rpath;
4754
0
  }
4755
4756
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4757
   all sections contained fully therein.  This makes relro
4758
   shared library sections appear as they will at run-time.  */
4759
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4760
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4761
0
  if (phdr->p_type == PT_GNU_RELRO)
4762
0
    {
4763
0
      for (s = abfd->sections; s != NULL; s = s->next)
4764
0
        {
4765
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4766
4767
0
    if ((s->flags & SEC_ALLOC) != 0
4768
0
        && s->vma * opb >= phdr->p_vaddr
4769
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4770
0
      s->flags |= SEC_READONLY;
4771
0
        }
4772
0
      break;
4773
0
    }
4774
4775
      /* We do not want to include any of the sections in a dynamic
4776
   object in the output file.  We hack by simply clobbering the
4777
   list of sections in the BFD.  This could be handled more
4778
   cleanly by, say, a new section flag; the existing
4779
   SEC_NEVER_LOAD flag is not the one we want, because that one
4780
   still implies that the section takes up space in the output
4781
   file.  */
4782
0
      bfd_section_list_clear (abfd);
4783
4784
      /* Find the name to use in a DT_NEEDED entry that refers to this
4785
   object.  If the object has a DT_SONAME entry, we use it.
4786
   Otherwise, if the generic linker stuck something in
4787
   elf_dt_name, we use that.  Otherwise, we just use the file
4788
   name.  */
4789
0
      if (soname == NULL || *soname == '\0')
4790
0
  {
4791
0
    soname = elf_dt_name (abfd);
4792
0
    if (soname == NULL || *soname == '\0')
4793
0
      soname = bfd_get_filename (abfd);
4794
0
  }
4795
4796
      /* Save the SONAME because sometimes the linker emulation code
4797
   will need to know it.  */
4798
0
      elf_dt_name (abfd) = soname;
4799
4800
      /* If we have already included this dynamic object in the
4801
   link, just ignore it.  There is no reason to include a
4802
   particular dynamic object more than once.  */
4803
0
      for (loaded_lib = htab->dyn_loaded;
4804
0
     loaded_lib != NULL;
4805
0
     loaded_lib = loaded_lib->next)
4806
0
  {
4807
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4808
0
      return true;
4809
0
  }
4810
4811
      /* Create dynamic sections for backends that require that be done
4812
   before setup_gnu_properties.  */
4813
0
      if (add_needed
4814
0
    && !bfd_elf_link_create_dynamic_sections (abfd, info))
4815
0
  return false;
4816
4817
      /* Save the DT_AUDIT entry for the linker emulation code. */
4818
0
      elf_dt_audit (abfd) = audit;
4819
0
    }
4820
4821
  /* If this is a dynamic object, we always link against the .dynsym
4822
     symbol table, not the .symtab symbol table.  The dynamic linker
4823
     will only see the .dynsym symbol table, so there is no reason to
4824
     look at .symtab for a dynamic object.  */
4825
4826
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4827
0
    hdr = &elf_symtab_hdr (abfd);
4828
0
  else
4829
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4830
4831
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4832
4833
  /* The sh_info field of the symtab header tells us where the
4834
     external symbols start.  We don't care about the local symbols at
4835
     this point.  */
4836
0
  if (elf_bad_symtab (abfd))
4837
0
    {
4838
0
      extsymcount = symcount;
4839
0
      extsymoff = 0;
4840
0
    }
4841
0
  else
4842
0
    {
4843
0
      extsymcount = symcount - hdr->sh_info;
4844
0
      extsymoff = hdr->sh_info;
4845
0
    }
4846
4847
0
  sym_hash = elf_sym_hashes (abfd);
4848
0
  if (extsymcount != 0)
4849
0
    {
4850
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4851
0
              NULL, NULL, NULL);
4852
0
      if (isymbuf == NULL)
4853
0
  goto error_return;
4854
4855
0
      if (sym_hash == NULL)
4856
0
  {
4857
    /* We store a pointer to the hash table entry for each
4858
       external symbol.  */
4859
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4860
0
    if (sym_hash == NULL)
4861
0
      goto error_free_sym;
4862
0
    elf_sym_hashes (abfd) = sym_hash;
4863
0
  }
4864
0
    }
4865
4866
0
  if (dynamic)
4867
0
    {
4868
      /* Read in any version definitions.  */
4869
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4870
0
            info->default_imported_symver))
4871
0
  goto error_free_sym;
4872
4873
      /* Read in the symbol versions, but don't bother to convert them
4874
   to internal format.  */
4875
0
      if (elf_dynversym (abfd) != 0)
4876
0
  {
4877
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4878
0
    bfd_size_type amt = versymhdr->sh_size;
4879
4880
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4881
0
      goto error_free_sym;
4882
0
    extversym = (Elf_External_Versym *)
4883
0
      _bfd_malloc_and_read (abfd, amt, amt);
4884
0
    if (extversym == NULL)
4885
0
      goto error_free_sym;
4886
0
    extversym_end = extversym + amt / sizeof (*extversym);
4887
0
  }
4888
0
    }
4889
4890
  /* If we are loading an as-needed shared lib, save the symbol table
4891
     state before we start adding symbols.  If the lib turns out
4892
     to be unneeded, restore the state.  */
4893
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4894
0
    {
4895
0
      unsigned int i;
4896
0
      size_t entsize;
4897
4898
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4899
0
  {
4900
0
    struct bfd_hash_entry *p;
4901
0
    struct elf_link_hash_entry *h;
4902
4903
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4904
0
      {
4905
0
        h = (struct elf_link_hash_entry *) p;
4906
0
        entsize += htab->root.table.entsize;
4907
0
        if (h->root.type == bfd_link_hash_warning)
4908
0
    {
4909
0
      entsize += htab->root.table.entsize;
4910
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4911
0
    }
4912
0
        if (h->root.type == bfd_link_hash_common)
4913
0
    entsize += sizeof (*h->root.u.c.p);
4914
0
      }
4915
0
  }
4916
4917
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4918
0
      old_tab = bfd_malloc (tabsize + entsize);
4919
0
      if (old_tab == NULL)
4920
0
  goto error_free_vers;
4921
4922
      /* Remember the current objalloc pointer, so that all mem for
4923
   symbols added can later be reclaimed.  */
4924
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4925
0
      if (alloc_mark == NULL)
4926
0
  goto error_free_vers;
4927
4928
      /* Make a special call to the linker "notice" function to
4929
   tell it that we are about to handle an as-needed lib.  */
4930
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4931
0
  goto error_free_vers;
4932
4933
      /* Clone the symbol table.  Remember some pointers into the
4934
   symbol table, and dynamic symbol count.  */
4935
0
      old_ent = (char *) old_tab + tabsize;
4936
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4937
0
      old_undefs = htab->root.undefs;
4938
0
      old_undefs_tail = htab->root.undefs_tail;
4939
0
      old_table = htab->root.table.table;
4940
0
      old_size = htab->root.table.size;
4941
0
      old_count = htab->root.table.count;
4942
0
      old_strtab = NULL;
4943
0
      if (htab->dynstr != NULL)
4944
0
  {
4945
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4946
0
    if (old_strtab == NULL)
4947
0
      goto error_free_vers;
4948
0
  }
4949
4950
0
      for (i = 0; i < htab->root.table.size; i++)
4951
0
  {
4952
0
    struct bfd_hash_entry *p;
4953
0
    struct elf_link_hash_entry *h;
4954
4955
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4956
0
      {
4957
0
        h = (struct elf_link_hash_entry *) p;
4958
0
        memcpy (old_ent, h, htab->root.table.entsize);
4959
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4960
0
        if (h->root.type == bfd_link_hash_warning)
4961
0
    {
4962
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4963
0
      memcpy (old_ent, h, htab->root.table.entsize);
4964
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4965
0
    }
4966
0
        if (h->root.type == bfd_link_hash_common)
4967
0
    {
4968
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4969
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4970
0
    }
4971
0
      }
4972
0
  }
4973
0
    }
4974
4975
0
  weaks = NULL;
4976
0
  if (extversym == NULL)
4977
0
    ever = NULL;
4978
0
  else if (extversym + extsymoff < extversym_end)
4979
0
    ever = extversym + extsymoff;
4980
0
  else
4981
0
    {
4982
      /* xgettext:c-format */
4983
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4984
0
        abfd, (long) extsymoff,
4985
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4986
0
      bfd_set_error (bfd_error_bad_value);
4987
0
      goto error_free_vers;
4988
0
    }
4989
4990
0
  if (!bfd_link_relocatable (info)
4991
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4992
0
    {
4993
0
      _bfd_error_handler
4994
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4995
0
    }
4996
4997
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4998
0
       isym < isymend;
4999
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
5000
0
    {
5001
0
      int bind;
5002
0
      bfd_vma value;
5003
0
      asection *sec, *new_sec;
5004
0
      flagword flags;
5005
0
      const char *name;
5006
0
      const char *defvername;
5007
0
      bool must_copy_name = false;
5008
0
      struct elf_link_hash_entry *h;
5009
0
      struct elf_link_hash_entry *hi;
5010
0
      bool definition;
5011
0
      bool size_change_ok;
5012
0
      bool type_change_ok;
5013
0
      bool new_weak;
5014
0
      bool old_weak;
5015
0
      bfd *override;
5016
0
      bool common;
5017
0
      bool discarded;
5018
0
      unsigned int old_alignment;
5019
0
      unsigned int shindex;
5020
0
      bfd *old_bfd;
5021
0
      bool matched;
5022
5023
0
      override = NULL;
5024
5025
0
      flags = BSF_NO_FLAGS;
5026
0
      sec = NULL;
5027
0
      value = isym->st_value;
5028
0
      common = bed->common_definition (isym);
5029
0
      if (common && info->inhibit_common_definition)
5030
0
  {
5031
    /* Treat common symbol as undefined for --no-define-common.  */
5032
0
    isym->st_shndx = SHN_UNDEF;
5033
0
    common = false;
5034
0
  }
5035
0
      discarded = false;
5036
5037
0
      bind = ELF_ST_BIND (isym->st_info);
5038
0
      switch (bind)
5039
0
  {
5040
0
  case STB_LOCAL:
5041
    /* This should be impossible, since ELF requires that all
5042
       global symbols follow all local symbols, and that sh_info
5043
       point to the first global symbol.  Unfortunately, Irix 5
5044
       screws this up.  */
5045
0
    if (elf_bad_symtab (abfd))
5046
0
      continue;
5047
5048
    /* If we aren't prepared to handle locals within the globals
5049
       then we'll likely segfault on a NULL symbol hash if the
5050
       symbol is ever referenced in relocations.  */
5051
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5052
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5053
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5054
0
        " (>= sh_info of %lu)"),
5055
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5056
0
            (long) extsymoff);
5057
5058
    /* Dynamic object relocations are not processed by ld, so
5059
       ld won't run into the problem mentioned above.  */
5060
0
    if (dynamic)
5061
0
      continue;
5062
0
    bfd_set_error (bfd_error_bad_value);
5063
0
    goto error_free_vers;
5064
5065
0
  case STB_GLOBAL:
5066
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5067
0
      flags = BSF_GLOBAL;
5068
0
    break;
5069
5070
0
  case STB_WEAK:
5071
0
    flags = BSF_WEAK;
5072
0
    break;
5073
5074
0
  case STB_GNU_UNIQUE:
5075
0
    flags = BSF_GNU_UNIQUE;
5076
0
    break;
5077
5078
0
  default:
5079
    /* Leave it up to the processor backend.  */
5080
0
    break;
5081
0
  }
5082
5083
0
      if (isym->st_shndx == SHN_UNDEF)
5084
0
  sec = bfd_und_section_ptr;
5085
0
      else if (isym->st_shndx == SHN_ABS)
5086
0
  sec = bfd_abs_section_ptr;
5087
0
      else if (isym->st_shndx == SHN_COMMON)
5088
0
  {
5089
0
    sec = bfd_com_section_ptr;
5090
    /* What ELF calls the size we call the value.  What ELF
5091
       calls the value we call the alignment.  */
5092
0
    value = isym->st_size;
5093
0
  }
5094
0
      else
5095
0
  {
5096
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5097
0
    if (sec == NULL)
5098
0
      sec = bfd_abs_section_ptr;
5099
0
    else if (discarded_section (sec))
5100
0
      {
5101
        /* Symbols from discarded section are undefined.  We keep
5102
     its visibility.  */
5103
0
        sec = bfd_und_section_ptr;
5104
0
        discarded = true;
5105
0
        isym->st_shndx = SHN_UNDEF;
5106
0
      }
5107
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5108
0
      value -= sec->vma;
5109
0
  }
5110
5111
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5112
0
                isym->st_name);
5113
0
      if (name == NULL)
5114
0
  goto error_free_vers;
5115
5116
0
      if (isym->st_shndx == SHN_COMMON
5117
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5118
0
  {
5119
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5120
5121
0
    if (xc == NULL)
5122
0
      {
5123
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5124
0
         | SEC_EXCLUDE);
5125
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5126
0
        if (xc == NULL)
5127
0
    goto error_free_vers;
5128
0
      }
5129
0
    sec = xc;
5130
0
  }
5131
0
      else if (isym->st_shndx == SHN_COMMON
5132
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5133
0
         && !bfd_link_relocatable (info))
5134
0
  {
5135
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5136
5137
0
    if (tcomm == NULL)
5138
0
      {
5139
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5140
0
         | SEC_LINKER_CREATED);
5141
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5142
0
        if (tcomm == NULL)
5143
0
    goto error_free_vers;
5144
0
      }
5145
0
    sec = tcomm;
5146
0
  }
5147
0
      else if (bed->elf_add_symbol_hook)
5148
0
  {
5149
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5150
0
               &sec, &value))
5151
0
      goto error_free_vers;
5152
5153
    /* The hook function sets the name to NULL if this symbol
5154
       should be skipped for some reason.  */
5155
0
    if (name == NULL)
5156
0
      continue;
5157
0
  }
5158
5159
0
      if (name[0] == '\0')
5160
0
  {
5161
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5162
0
    bfd_set_error (bfd_error_bad_value);
5163
0
    goto error_free_vers;
5164
0
  }
5165
5166
      /* Sanity check that all possibilities were handled.  */
5167
0
      if (sec == NULL)
5168
0
  abort ();
5169
5170
      /* Silently discard TLS symbols from --just-syms.  There's
5171
   no way to combine a static TLS block with a new TLS block
5172
   for this executable.  */
5173
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5174
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5175
0
  continue;
5176
5177
0
      if (bfd_is_und_section (sec)
5178
0
    || bfd_is_com_section (sec))
5179
0
  definition = false;
5180
0
      else
5181
0
  definition = true;
5182
5183
0
      size_change_ok = false;
5184
0
      type_change_ok = bed->type_change_ok;
5185
0
      old_weak = false;
5186
0
      matched = false;
5187
0
      old_alignment = 0;
5188
0
      old_bfd = NULL;
5189
0
      new_sec = sec;
5190
0
      defvername = NULL;
5191
5192
0
      if (is_elf_hash_table (&htab->root))
5193
0
  {
5194
0
    Elf_Internal_Versym iver;
5195
0
    unsigned int vernum = 0;
5196
0
    bool skip;
5197
5198
0
    if (ever == NULL)
5199
0
      {
5200
0
        if (info->default_imported_symver)
5201
    /* Use the default symbol version created earlier.  */
5202
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5203
0
        else
5204
0
    iver.vs_vers = 0;
5205
0
      }
5206
0
    else if (ever >= extversym_end)
5207
0
      {
5208
        /* xgettext:c-format */
5209
0
        _bfd_error_handler (_("%pB: not enough version information"),
5210
0
          abfd);
5211
0
        bfd_set_error (bfd_error_bad_value);
5212
0
        goto error_free_vers;
5213
0
      }
5214
0
    else
5215
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5216
5217
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5218
5219
    /* If this is a hidden symbol, or if it is not version
5220
       1, we append the version name to the symbol name.
5221
       However, we do not modify a non-hidden absolute symbol
5222
       if it is not a function, because it might be the version
5223
       symbol itself.  FIXME: What if it isn't?  */
5224
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5225
0
        || (vernum > 1
5226
0
      && (!bfd_is_abs_section (sec)
5227
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5228
0
      {
5229
0
        const char *verstr;
5230
0
        size_t namelen, verlen, newlen;
5231
0
        char *newname, *p;
5232
5233
0
        if (isym->st_shndx != SHN_UNDEF)
5234
0
    {
5235
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5236
0
        verstr = NULL;
5237
0
      else if (vernum > 1)
5238
0
        verstr =
5239
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5240
0
      else
5241
0
        verstr = "";
5242
5243
0
      if (verstr == NULL)
5244
0
        {
5245
0
          _bfd_error_handler
5246
      /* xgettext:c-format */
5247
0
      (_("%pB: %s: invalid version %u (max %d)"),
5248
0
       abfd, name, vernum,
5249
0
       elf_tdata (abfd)->cverdefs);
5250
0
          bfd_set_error (bfd_error_bad_value);
5251
0
          goto error_free_vers;
5252
0
        }
5253
0
    }
5254
0
        else
5255
0
    {
5256
      /* We cannot simply test for the number of
5257
         entries in the VERNEED section since the
5258
         numbers for the needed versions do not start
5259
         at 0.  */
5260
0
      Elf_Internal_Verneed *t;
5261
5262
0
      verstr = NULL;
5263
0
      for (t = elf_tdata (abfd)->verref;
5264
0
           t != NULL;
5265
0
           t = t->vn_nextref)
5266
0
        {
5267
0
          Elf_Internal_Vernaux *a;
5268
5269
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5270
0
      {
5271
0
        if (a->vna_other == vernum)
5272
0
          {
5273
0
            verstr = a->vna_nodename;
5274
0
            break;
5275
0
          }
5276
0
      }
5277
0
          if (a != NULL)
5278
0
      break;
5279
0
        }
5280
0
      if (verstr == NULL)
5281
0
        {
5282
0
          _bfd_error_handler
5283
      /* xgettext:c-format */
5284
0
      (_("%pB: %s: invalid needed version %d"),
5285
0
       abfd, name, vernum);
5286
0
          bfd_set_error (bfd_error_bad_value);
5287
0
          goto error_free_vers;
5288
0
        }
5289
0
    }
5290
5291
0
        namelen = strlen (name);
5292
0
        verlen = strlen (verstr);
5293
0
        newlen = namelen + verlen + 2;
5294
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5295
0
      && isym->st_shndx != SHN_UNDEF)
5296
0
    ++newlen;
5297
5298
0
        newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5299
0
        if (newname == NULL)
5300
0
    goto error_free_vers;
5301
0
        memcpy (newname, name, namelen);
5302
0
        p = newname + namelen;
5303
0
        *p++ = ELF_VER_CHR;
5304
        /* If this is a defined non-hidden version symbol,
5305
     we add another @ to the name.  This indicates the
5306
     default version of the symbol.  */
5307
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5308
0
      && isym->st_shndx != SHN_UNDEF)
5309
0
    *p++ = ELF_VER_CHR, defvername = name;
5310
0
        memcpy (p, verstr, verlen + 1);
5311
5312
0
        name = newname;
5313
        /* Since bfd_hash_alloc is used for "name", the string
5314
     must be copied if added to first_hash.  The string
5315
     memory can be freed when an --as-needed library is
5316
     not needed.  */
5317
0
        must_copy_name = true;
5318
0
      }
5319
5320
    /* If this symbol has default visibility and the user has
5321
       requested we not re-export it, then mark it as hidden.  */
5322
0
    if (!bfd_is_und_section (sec)
5323
0
        && !dynamic
5324
0
        && abfd->no_export
5325
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5326
0
      isym->st_other = (STV_HIDDEN
5327
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5328
5329
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5330
0
              sym_hash, &old_bfd, &old_weak,
5331
0
              &old_alignment, &skip, &override,
5332
0
              &type_change_ok, &size_change_ok,
5333
0
              &matched))
5334
0
      goto error_free_vers;
5335
5336
0
    if (skip)
5337
0
      continue;
5338
5339
0
    h = *sym_hash;
5340
0
    while (h->root.type == bfd_link_hash_indirect
5341
0
     || h->root.type == bfd_link_hash_warning)
5342
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5343
5344
    /* Override a definition only if the new symbol matches the
5345
       existing one.  */
5346
0
    if (override && matched)
5347
0
      {
5348
0
        definition = false;
5349
0
        if (htab->first_hash != NULL
5350
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5351
0
      && h->root.non_ir_ref_regular)
5352
0
    {
5353
      /* When reloading --as-needed shared objects for new
5354
         symbols added from IR inputs, if this shared object
5355
         has the first definition, use it.  */
5356
0
      struct elf_link_first_hash_entry *e
5357
0
        = ((struct elf_link_first_hash_entry *)
5358
0
           bfd_hash_lookup (htab->first_hash, name, false,
5359
0
          false));
5360
0
      if (e != NULL && e->abfd == abfd)
5361
0
        definition = true;
5362
0
    }
5363
0
      }
5364
5365
0
    if (h->versioned != unversioned
5366
0
        && elf_tdata (abfd)->verdef != NULL
5367
0
        && vernum > 1
5368
0
        && definition)
5369
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5370
0
  }
5371
5372
0
      if (! (_bfd_generic_link_add_one_symbol
5373
0
       (info, override ? override : abfd, name, flags, sec, value,
5374
0
        NULL, false, bed->collect,
5375
0
        (struct bfd_link_hash_entry **) sym_hash)))
5376
0
  goto error_free_vers;
5377
5378
0
      h = *sym_hash;
5379
      /* We need to make sure that indirect symbol dynamic flags are
5380
   updated.  */
5381
0
      hi = h;
5382
0
      while (h->root.type == bfd_link_hash_indirect
5383
0
       || h->root.type == bfd_link_hash_warning)
5384
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5385
5386
0
      *sym_hash = h;
5387
5388
      /* Setting the index to -3 tells elf_link_output_extsym that
5389
   this symbol is defined in a discarded section.  */
5390
0
      if (discarded && is_elf_hash_table (&htab->root))
5391
0
  h->indx = -3;
5392
5393
0
      new_weak = (flags & BSF_WEAK) != 0;
5394
0
      if (dynamic
5395
0
    && definition
5396
0
    && new_weak
5397
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5398
0
    && is_elf_hash_table (&htab->root)
5399
0
    && h->u.alias == NULL)
5400
0
  {
5401
    /* Keep a list of all weak defined non function symbols from
5402
       a dynamic object, using the alias field.  Later in this
5403
       function we will set the alias field to the correct
5404
       value.  We only put non-function symbols from dynamic
5405
       objects on this list, because that happens to be the only
5406
       time we need to know the normal symbol corresponding to a
5407
       weak symbol, and the information is time consuming to
5408
       figure out.  If the alias field is not already NULL,
5409
       then this symbol was already defined by some previous
5410
       dynamic object, and we will be using that previous
5411
       definition anyhow.  */
5412
5413
0
    h->u.alias = weaks;
5414
0
    weaks = h;
5415
0
  }
5416
5417
      /* Set the alignment of a common symbol.  */
5418
0
      if ((common || bfd_is_com_section (sec))
5419
0
    && h->root.type == bfd_link_hash_common)
5420
0
  {
5421
0
    unsigned int align;
5422
5423
0
    if (common)
5424
0
      align = bfd_log2 (isym->st_value);
5425
0
    else
5426
0
      {
5427
        /* The new symbol is a common symbol in a shared object.
5428
     We need to get the alignment from the section.  */
5429
0
        align = new_sec->alignment_power;
5430
0
      }
5431
0
    if (align > old_alignment)
5432
0
      h->root.u.c.p->alignment_power = align;
5433
0
    else
5434
0
      h->root.u.c.p->alignment_power = old_alignment;
5435
0
  }
5436
5437
0
      if (is_elf_hash_table (&htab->root))
5438
0
  {
5439
    /* Set a flag in the hash table entry indicating the type of
5440
       reference or definition we just found.  A dynamic symbol
5441
       is one which is referenced or defined by both a regular
5442
       object and a shared object.  */
5443
0
    bool dynsym = false;
5444
5445
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5446
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5447
0
      {
5448
        /* Except for this flag to track nonweak references.  */
5449
0
        if (!definition
5450
0
      && bind != STB_WEAK)
5451
0
    h->ref_ir_nonweak = 1;
5452
0
      }
5453
0
    else if (!dynamic)
5454
0
      {
5455
0
        if (! definition)
5456
0
    {
5457
0
      h->ref_regular = 1;
5458
0
      if (bind != STB_WEAK)
5459
0
        h->ref_regular_nonweak = 1;
5460
0
    }
5461
0
        else
5462
0
    {
5463
0
      h->def_regular = 1;
5464
0
      if (h->def_dynamic)
5465
0
        {
5466
0
          h->def_dynamic = 0;
5467
0
          h->ref_dynamic = 1;
5468
0
        }
5469
0
    }
5470
0
      }
5471
0
    else
5472
0
      {
5473
0
        if (! definition)
5474
0
    {
5475
0
      h->ref_dynamic = 1;
5476
0
      hi->ref_dynamic = 1;
5477
0
    }
5478
0
        else
5479
0
    {
5480
0
      h->def_dynamic = 1;
5481
0
      hi->def_dynamic = 1;
5482
0
    }
5483
0
      }
5484
5485
    /* If an indirect symbol has been forced local, don't
5486
       make the real symbol dynamic.  */
5487
0
    if (h != hi && hi->forced_local)
5488
0
      ;
5489
0
    else if (!dynamic)
5490
0
      {
5491
0
        if (bfd_link_dll (info)
5492
0
      || h->def_dynamic
5493
0
      || h->ref_dynamic)
5494
0
    dynsym = true;
5495
0
      }
5496
0
    else
5497
0
      {
5498
0
        if (h->def_regular
5499
0
      || h->ref_regular
5500
0
      || (h->is_weakalias
5501
0
          && weakdef (h)->dynindx != -1))
5502
0
    dynsym = true;
5503
0
      }
5504
5505
    /* Check to see if we need to add an indirect symbol for
5506
       the default name.  */
5507
0
    if ((definition
5508
0
         || (!override && h->root.type == bfd_link_hash_common))
5509
0
        && !(hi != h
5510
0
       && hi->versioned == versioned_hidden))
5511
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5512
0
                sec, value, &old_bfd, &dynsym))
5513
0
        goto error_free_vers;
5514
5515
    /* Check the alignment when a common symbol is involved. This
5516
       can change when a common symbol is overridden by a normal
5517
       definition or a common symbol is ignored due to the old
5518
       normal definition. We need to make sure the maximum
5519
       alignment is maintained.  */
5520
0
    if ((old_alignment || common)
5521
0
        && h->root.type != bfd_link_hash_common)
5522
0
      {
5523
0
        unsigned int common_align;
5524
0
        unsigned int normal_align;
5525
0
        unsigned int symbol_align;
5526
0
        bfd *normal_bfd;
5527
0
        bfd *common_bfd;
5528
5529
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5530
0
        || h->root.type == bfd_link_hash_defweak);
5531
5532
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5533
0
        if (h->root.u.def.section->owner != NULL
5534
0
      && (h->root.u.def.section->owner->flags
5535
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5536
0
    {
5537
0
      normal_align = h->root.u.def.section->alignment_power;
5538
0
      if (normal_align > symbol_align)
5539
0
        normal_align = symbol_align;
5540
0
    }
5541
0
        else
5542
0
    normal_align = symbol_align;
5543
5544
0
        if (old_alignment)
5545
0
    {
5546
0
      common_align = old_alignment;
5547
0
      common_bfd = old_bfd;
5548
0
      normal_bfd = abfd;
5549
0
    }
5550
0
        else
5551
0
    {
5552
0
      common_align = bfd_log2 (isym->st_value);
5553
0
      common_bfd = abfd;
5554
0
      normal_bfd = old_bfd;
5555
0
    }
5556
5557
0
        if (normal_align < common_align)
5558
0
    {
5559
      /* PR binutils/2735 */
5560
0
      uint64_t c_align = UINT64_C (1) << common_align;
5561
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5562
0
      if (normal_bfd == NULL)
5563
0
        _bfd_error_handler
5564
          /* xgettext:c-format */
5565
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5566
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5567
0
           c_align, name, common_bfd,
5568
0
           n_align, h->root.u.def.section);
5569
0
      else
5570
0
        _bfd_error_handler
5571
          /* xgettext:c-format */
5572
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5573
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5574
0
           n_align, name, normal_bfd,
5575
0
           c_align, common_bfd);
5576
5577
      /* PR 30499: make sure that users understand that this warning is serious.  */
5578
0
      _bfd_error_handler
5579
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5580
0
    }
5581
0
      }
5582
5583
    /* Remember the symbol size if it isn't undefined.  */
5584
0
    if (isym->st_size != 0
5585
0
        && isym->st_shndx != SHN_UNDEF
5586
0
        && (definition || h->size == 0))
5587
0
      {
5588
0
        if (h->size != 0
5589
0
      && h->size != isym->st_size
5590
0
      && ! size_change_ok)
5591
0
    {
5592
0
      _bfd_error_handler
5593
        /* xgettext:c-format */
5594
0
        (_("warning: size of symbol `%s' changed"
5595
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5596
0
         name, (uint64_t) h->size, old_bfd,
5597
0
         (uint64_t) isym->st_size, abfd);
5598
5599
      /* PR 30499: make sure that users understand that this warning is serious.  */
5600
0
      _bfd_error_handler
5601
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5602
0
    }
5603
5604
0
        h->size = isym->st_size;
5605
0
      }
5606
5607
    /* If this is a common symbol, then we always want H->SIZE
5608
       to be the size of the common symbol.  The code just above
5609
       won't fix the size if a common symbol becomes larger.  We
5610
       don't warn about a size change here, because that is
5611
       covered by --warn-common.  Allow changes between different
5612
       function types.  */
5613
0
    if (h->root.type == bfd_link_hash_common)
5614
0
      h->size = h->root.u.c.size;
5615
5616
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5617
0
        && ((definition && !new_weak)
5618
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5619
0
      || h->type == STT_NOTYPE))
5620
0
      {
5621
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5622
5623
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5624
     symbol.  */
5625
0
        if (type == STT_GNU_IFUNC
5626
0
      && (abfd->flags & DYNAMIC) != 0)
5627
0
    type = STT_FUNC;
5628
5629
0
        if (h->type != type)
5630
0
    {
5631
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5632
        /* xgettext:c-format */
5633
0
        _bfd_error_handler
5634
0
          (_("warning: type of symbol `%s' changed"
5635
0
       " from %d to %d in %pB"),
5636
0
           name, h->type, type, abfd);
5637
5638
0
      h->type = type;
5639
0
    }
5640
0
      }
5641
5642
    /* Merge st_other field.  */
5643
0
    elf_merge_st_other (abfd, h, isym->st_other, sec,
5644
0
            definition, dynamic);
5645
5646
    /* We don't want to make debug symbol dynamic.  */
5647
0
    if (definition
5648
0
        && (sec->flags & SEC_DEBUGGING)
5649
0
        && !bfd_link_relocatable (info))
5650
0
      dynsym = false;
5651
5652
    /* Nor should we make plugin symbols dynamic.  */
5653
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5654
0
      dynsym = false;
5655
5656
0
    if (definition)
5657
0
      {
5658
0
        h->target_internal = isym->st_target_internal;
5659
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5660
0
      }
5661
5662
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5663
       in IR.  Since all data or text symbols in IR have the
5664
       same type, value and section, we can't tell if a symbol
5665
       is an alias of another symbol by their types, values and
5666
       sections.  */
5667
0
    if (definition
5668
0
        && !dynamic
5669
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5670
0
      {
5671
0
        const char *p = strchr (name, ELF_VER_CHR);
5672
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5673
0
    {
5674
      /* Queue non-default versions so that .symver x, x@FOO
5675
         aliases can be checked.  */
5676
0
      if (!nondeflt_vers)
5677
0
        {
5678
0
          size_t amt = ((isymend - isym + 1)
5679
0
            * sizeof (struct elf_link_hash_entry *));
5680
0
          nondeflt_vers
5681
0
      = (struct elf_link_hash_entry **) bfd_malloc (amt);
5682
0
          if (!nondeflt_vers)
5683
0
      goto error_free_vers;
5684
0
        }
5685
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5686
0
    }
5687
0
      }
5688
5689
0
    if (dynsym && h->dynindx == -1)
5690
0
      {
5691
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5692
0
    goto error_free_vers;
5693
0
        if (h->is_weakalias
5694
0
      && weakdef (h)->dynindx == -1)
5695
0
    {
5696
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5697
0
        goto error_free_vers;
5698
0
    }
5699
0
      }
5700
0
    else if (h->dynindx != -1)
5701
      /* If the symbol already has a dynamic index, but
5702
         visibility says it should not be visible, turn it into
5703
         a local symbol.  */
5704
0
      switch (ELF_ST_VISIBILITY (h->other))
5705
0
        {
5706
0
        case STV_INTERNAL:
5707
0
        case STV_HIDDEN:
5708
0
    obed->elf_backend_hide_symbol (info, h, true);
5709
0
    dynsym = false;
5710
0
    break;
5711
0
        }
5712
5713
0
    if (!add_needed
5714
0
        && matched
5715
0
        && definition
5716
0
        && h->root.type != bfd_link_hash_indirect)
5717
0
      {
5718
0
        if ((dynsym
5719
0
       && h->ref_regular_nonweak)
5720
0
      || (old_bfd != NULL
5721
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5722
0
          && h->ref_ir_nonweak
5723
0
          && !info->lto_all_symbols_read)
5724
0
      || (h->ref_dynamic_nonweak
5725
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5726
0
          && !on_needed_list (elf_dt_name (abfd),
5727
0
            htab->needed, NULL)))
5728
0
    {
5729
0
      const char *soname = elf_dt_name (abfd);
5730
5731
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5732
0
            h->root.root.string);
5733
5734
      /* A symbol from a library loaded via DT_NEEDED of some
5735
         other library is referenced by a regular object.
5736
         Add a DT_NEEDED entry for it.  Issue an error if
5737
         --no-add-needed is used and the reference was not
5738
         a weak one.  */
5739
0
      if (old_bfd != NULL
5740
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5741
0
        {
5742
0
          _bfd_error_handler
5743
      /* xgettext:c-format */
5744
0
      (_("%pB: undefined reference to symbol '%s'"),
5745
0
       old_bfd, name);
5746
0
          bfd_set_error (bfd_error_missing_dso);
5747
0
          goto error_free_vers;
5748
0
        }
5749
5750
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5751
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5752
5753
      /* Create dynamic sections for backends that require
5754
         that be done before setup_gnu_properties.  */
5755
0
      if (!bfd_elf_link_create_dynamic_sections (abfd, info))
5756
0
        goto error_free_vers;
5757
0
      add_needed = true;
5758
0
    }
5759
0
        else if (dynamic
5760
0
           && h->root.u.def.section->owner == abfd)
5761
0
    {
5762
      /* Add this symbol to first hash if this shared
5763
         object has the first definition.  */
5764
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5765
      /* And if it was the default symbol version definition,
5766
         also add the short name.  */
5767
0
      if (defvername)
5768
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5769
0
    }
5770
0
      }
5771
0
  }
5772
0
    }
5773
5774
0
  if (info->lto_plugin_active
5775
0
      && !bfd_link_relocatable (info)
5776
0
      && (abfd->flags & BFD_PLUGIN) == 0
5777
0
      && !just_syms
5778
0
      && extsymcount != 0
5779
0
      && is_elf_hash_table (&htab->root))
5780
0
    {
5781
0
      int r_sym_shift;
5782
5783
0
      if (bed->s->arch_size == 32)
5784
0
  r_sym_shift = 8;
5785
0
      else
5786
0
  r_sym_shift = 32;
5787
5788
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5789
   referenced in regular objects so that linker plugin will get
5790
   the correct symbol resolution.  */
5791
5792
0
      sym_hash = elf_sym_hashes (abfd);
5793
0
      for (s = abfd->sections; s != NULL; s = s->next)
5794
0
  {
5795
0
    Elf_Internal_Rela *internal_relocs;
5796
0
    Elf_Internal_Rela *rel, *relend;
5797
5798
    /* Don't check relocations in excluded sections.  */
5799
0
    if ((s->flags & SEC_RELOC) == 0
5800
0
        || s->reloc_count == 0
5801
0
        || (s->flags & SEC_EXCLUDE) != 0
5802
0
        || (s->flags & SEC_DEBUGGING) != 0)
5803
0
      continue;
5804
5805
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5806
0
      (abfd, info, s, NULL, NULL,
5807
0
       _bfd_elf_link_keep_memory (info));
5808
0
    if (internal_relocs == NULL)
5809
0
      goto error_free_vers;
5810
5811
0
    rel = internal_relocs;
5812
0
    relend = rel + s->reloc_count;
5813
0
    for ( ; rel < relend; rel++)
5814
0
      {
5815
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5816
0
        struct elf_link_hash_entry *h;
5817
5818
        /* Skip local symbols.  */
5819
0
        if (r_symndx < extsymoff)
5820
0
    continue;
5821
5822
0
        h = sym_hash[r_symndx - extsymoff];
5823
0
        if (h != NULL)
5824
0
    h->root.non_ir_ref_regular = 1;
5825
0
      }
5826
5827
0
    if (elf_section_data (s)->relocs != internal_relocs)
5828
0
      free (internal_relocs);
5829
0
  }
5830
0
    }
5831
5832
0
  free (extversym);
5833
0
  extversym = NULL;
5834
0
  free (isymbuf);
5835
0
  isymbuf = NULL;
5836
5837
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5838
0
    {
5839
0
      unsigned int i;
5840
5841
      /* Restore the symbol table.  */
5842
0
      old_ent = (char *) old_tab + tabsize;
5843
0
      memset (elf_sym_hashes (abfd), 0,
5844
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5845
0
      htab->root.table.table = old_table;
5846
0
      htab->root.table.size = old_size;
5847
0
      htab->root.table.count = old_count;
5848
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5849
0
      htab->root.undefs = old_undefs;
5850
0
      htab->root.undefs_tail = old_undefs_tail;
5851
0
      if (htab->dynstr != NULL)
5852
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5853
0
      free (old_strtab);
5854
0
      old_strtab = NULL;
5855
0
      for (i = 0; i < htab->root.table.size; i++)
5856
0
  {
5857
0
    struct bfd_hash_entry *p;
5858
0
    struct elf_link_hash_entry *h;
5859
0
    unsigned int non_ir_ref_dynamic;
5860
5861
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5862
0
      {
5863
        /* Preserve non_ir_ref_dynamic so that this symbol
5864
     will be exported when the dynamic lib becomes needed
5865
     in the second pass.  */
5866
0
        h = (struct elf_link_hash_entry *) p;
5867
0
        if (h->root.type == bfd_link_hash_warning)
5868
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5869
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5870
5871
0
        h = (struct elf_link_hash_entry *) p;
5872
0
        memcpy (h, old_ent, htab->root.table.entsize);
5873
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5874
0
        if (h->root.type == bfd_link_hash_warning)
5875
0
    {
5876
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5877
0
      memcpy (h, old_ent, htab->root.table.entsize);
5878
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5879
0
    }
5880
0
        if (h->root.type == bfd_link_hash_common)
5881
0
    {
5882
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5883
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5884
0
    }
5885
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5886
0
      }
5887
0
  }
5888
5889
      /* Make a special call to the linker "notice" function to
5890
   tell it that symbols added for crefs may need to be removed.  */
5891
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5892
0
  goto error_free_vers;
5893
5894
0
      free (old_tab);
5895
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5896
0
         alloc_mark);
5897
0
      free (nondeflt_vers);
5898
0
      return true;
5899
0
    }
5900
5901
0
  free (old_strtab);
5902
0
  old_strtab = NULL;
5903
0
  if (old_tab != NULL)
5904
0
    {
5905
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5906
0
  goto error_free_vers;
5907
0
      free (old_tab);
5908
0
      old_tab = NULL;
5909
0
    }
5910
5911
  /* Now that all the symbols from this input file are created, if
5912
     not performing a relocatable link, handle .symver foo, foo@BAR
5913
     such that any relocs against foo become foo@BAR.  */
5914
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5915
0
    {
5916
0
      size_t cnt, symidx;
5917
5918
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5919
0
  {
5920
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5921
0
    char *shortname;
5922
0
    const char *p;
5923
0
    size_t amt;
5924
5925
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5926
0
    if (p == NULL
5927
0
        || (h->root.type != bfd_link_hash_defined
5928
0
      && h->root.type != bfd_link_hash_defweak))
5929
0
      continue;
5930
5931
0
    amt = p - h->root.root.string;
5932
0
    shortname = (char *) bfd_malloc (amt + 1);
5933
0
    if (!shortname)
5934
0
      goto error_free_vers;
5935
0
    memcpy (shortname, h->root.root.string, amt);
5936
0
    shortname[amt] = '\0';
5937
5938
0
    hi = (struct elf_link_hash_entry *)
5939
0
         bfd_link_hash_lookup (&htab->root, shortname,
5940
0
             false, false, false);
5941
0
    if (hi != NULL
5942
0
        && hi->root.type == h->root.type
5943
0
        && hi->root.u.def.value == h->root.u.def.value
5944
0
        && hi->root.u.def.section == h->root.u.def.section)
5945
0
      {
5946
0
        obed->elf_backend_hide_symbol (info, hi, true);
5947
0
        hi->root.type = bfd_link_hash_indirect;
5948
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5949
0
        obed->elf_backend_copy_indirect_symbol (info, h, hi);
5950
0
        sym_hash = elf_sym_hashes (abfd);
5951
0
        if (sym_hash)
5952
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5953
0
      if (sym_hash[symidx] == hi)
5954
0
        {
5955
0
          sym_hash[symidx] = h;
5956
0
          break;
5957
0
        }
5958
0
      }
5959
0
    free (shortname);
5960
0
  }
5961
0
    }
5962
0
  free (nondeflt_vers);
5963
0
  nondeflt_vers = NULL;
5964
5965
  /* Now set the alias field correctly for all the weak defined
5966
     symbols we found.  The only way to do this is to search all the
5967
     symbols.  Since we only need the information for non functions in
5968
     dynamic objects, that's the only time we actually put anything on
5969
     the list WEAKS.  We need this information so that if a regular
5970
     object refers to a symbol defined weakly in a dynamic object, the
5971
     real symbol in the dynamic object is also put in the dynamic
5972
     symbols; we also must arrange for both symbols to point to the
5973
     same memory location.  We could handle the general case of symbol
5974
     aliasing, but a general symbol alias can only be generated in
5975
     assembler code, handling it correctly would be very time
5976
     consuming, and other ELF linkers don't handle general aliasing
5977
     either.  */
5978
0
  if (weaks != NULL)
5979
0
    {
5980
0
      struct elf_link_hash_entry **hpp;
5981
0
      struct elf_link_hash_entry **hppend;
5982
0
      struct elf_link_hash_entry **sorted_sym_hash;
5983
0
      struct elf_link_hash_entry *h;
5984
0
      size_t sym_count, amt;
5985
5986
      /* Since we have to search the whole symbol list for each weak
5987
   defined symbol, search time for N weak defined symbols will be
5988
   O(N^2). Binary search will cut it down to O(NlogN).  */
5989
0
      amt = extsymcount * sizeof (*sorted_sym_hash);
5990
0
      sorted_sym_hash = bfd_malloc (amt);
5991
0
      if (sorted_sym_hash == NULL)
5992
0
  goto error_return;
5993
0
      sym_hash = sorted_sym_hash;
5994
0
      hpp = elf_sym_hashes (abfd);
5995
0
      hppend = hpp + extsymcount;
5996
0
      sym_count = 0;
5997
0
      for (; hpp < hppend; hpp++)
5998
0
  {
5999
0
    h = *hpp;
6000
0
    if (h != NULL
6001
0
        && h->root.type == bfd_link_hash_defined
6002
0
        && !bed->is_function_type (h->type))
6003
0
      {
6004
0
        *sym_hash = h;
6005
0
        sym_hash++;
6006
0
        sym_count++;
6007
0
      }
6008
0
  }
6009
6010
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6011
0
       elf_sort_symbol);
6012
6013
0
      while (weaks != NULL)
6014
0
  {
6015
0
    struct elf_link_hash_entry *hlook;
6016
0
    asection *slook;
6017
0
    bfd_vma vlook;
6018
0
    size_t i, j, idx = 0;
6019
6020
0
    hlook = weaks;
6021
0
    weaks = hlook->u.alias;
6022
0
    hlook->u.alias = NULL;
6023
6024
0
    if (hlook->root.type != bfd_link_hash_defined
6025
0
        && hlook->root.type != bfd_link_hash_defweak)
6026
0
      continue;
6027
6028
0
    slook = hlook->root.u.def.section;
6029
0
    vlook = hlook->root.u.def.value;
6030
6031
0
    i = 0;
6032
0
    j = sym_count;
6033
0
    while (i != j)
6034
0
      {
6035
0
        bfd_signed_vma vdiff;
6036
0
        idx = (i + j) / 2;
6037
0
        h = sorted_sym_hash[idx];
6038
0
        vdiff = vlook - h->root.u.def.value;
6039
0
        if (vdiff < 0)
6040
0
    j = idx;
6041
0
        else if (vdiff > 0)
6042
0
    i = idx + 1;
6043
0
        else
6044
0
    {
6045
0
      int sdiff = slook->id - h->root.u.def.section->id;
6046
0
      if (sdiff < 0)
6047
0
        j = idx;
6048
0
      else if (sdiff > 0)
6049
0
        i = idx + 1;
6050
0
      else
6051
0
        break;
6052
0
    }
6053
0
      }
6054
6055
    /* We didn't find a value/section match.  */
6056
0
    if (i == j)
6057
0
      continue;
6058
6059
    /* With multiple aliases, or when the weak symbol is already
6060
       strongly defined, we have multiple matching symbols and
6061
       the binary search above may land on any of them.  Step
6062
       one past the matching symbol(s).  */
6063
0
    while (++idx != j)
6064
0
      {
6065
0
        h = sorted_sym_hash[idx];
6066
0
        if (h->root.u.def.section != slook
6067
0
      || h->root.u.def.value != vlook)
6068
0
    break;
6069
0
      }
6070
6071
    /* Now look back over the aliases.  Since we sorted by size
6072
       as well as value and section, we'll choose the one with
6073
       the largest size.  */
6074
0
    while (idx-- != i)
6075
0
      {
6076
0
        h = sorted_sym_hash[idx];
6077
6078
        /* Stop if value or section doesn't match.  */
6079
0
        if (h->root.u.def.section != slook
6080
0
      || h->root.u.def.value != vlook)
6081
0
    break;
6082
0
        else if (h != hlook)
6083
0
    {
6084
0
      struct elf_link_hash_entry *t;
6085
6086
0
      hlook->u.alias = h;
6087
0
      hlook->is_weakalias = 1;
6088
0
      t = h;
6089
0
      if (t->u.alias != NULL)
6090
0
        while (t->u.alias != h)
6091
0
          t = t->u.alias;
6092
0
      t->u.alias = hlook;
6093
6094
      /* If the weak definition is in the list of dynamic
6095
         symbols, make sure the real definition is put
6096
         there as well.  */
6097
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6098
0
        {
6099
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6100
0
      {
6101
0
      err_free_sym_hash:
6102
0
        free (sorted_sym_hash);
6103
0
        goto error_return;
6104
0
      }
6105
0
        }
6106
6107
      /* If the real definition is in the list of dynamic
6108
         symbols, make sure the weak definition is put
6109
         there as well.  If we don't do this, then the
6110
         dynamic loader might not merge the entries for the
6111
         real definition and the weak definition.  */
6112
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6113
0
        {
6114
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6115
0
      goto err_free_sym_hash;
6116
0
        }
6117
0
      break;
6118
0
    }
6119
0
      }
6120
0
  }
6121
6122
0
      free (sorted_sym_hash);
6123
0
    }
6124
6125
0
  if (bed->check_directives
6126
0
      && !(*bed->check_directives) (abfd, info))
6127
0
    goto error_return;
6128
6129
  /* If this is a non-traditional link, try to optimize the handling
6130
     of the .stab/.stabstr sections.  */
6131
0
  if (! dynamic
6132
0
      && ! info->traditional_format
6133
0
      && is_elf_hash_table (&htab->root)
6134
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6135
0
    {
6136
0
      asection *stabstr;
6137
6138
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6139
0
      if (stabstr != NULL)
6140
0
  {
6141
0
    bfd_size_type string_offset = 0;
6142
0
    asection *stab;
6143
6144
0
    for (stab = abfd->sections; stab; stab = stab->next)
6145
0
      if (startswith (stab->name, ".stab")
6146
0
    && (!stab->name[5] ||
6147
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6148
0
    && (stab->flags & SEC_MERGE) == 0
6149
0
    && !bfd_is_abs_section (stab->output_section)
6150
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6151
0
               stabstr, &string_offset))
6152
0
        goto error_return;
6153
0
  }
6154
0
    }
6155
6156
0
  if (dynamic && add_needed)
6157
0
    {
6158
      /* Add this bfd to the loaded list.  */
6159
0
      struct elf_link_loaded_list *n;
6160
6161
0
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6162
0
      if (n == NULL)
6163
0
  goto error_return;
6164
0
      n->abfd = abfd;
6165
0
      n->next = htab->dyn_loaded;
6166
0
      htab->dyn_loaded = n;
6167
0
    }
6168
0
  if (dynamic && !add_needed
6169
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6170
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6171
6172
0
  return true;
6173
6174
0
 error_free_vers:
6175
0
  free (old_tab);
6176
0
  free (old_strtab);
6177
0
  free (nondeflt_vers);
6178
0
  free (extversym);
6179
0
 error_free_sym:
6180
0
  free (isymbuf);
6181
0
 error_return:
6182
0
  return false;
6183
0
}
6184
6185
/* Return the linker hash table entry of a symbol that might be
6186
   satisfied by an archive symbol.  Return -1 on error.  */
6187
6188
struct bfd_link_hash_entry *
6189
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6190
        struct bfd_link_info *info,
6191
        const char *name)
6192
0
{
6193
0
  struct bfd_link_hash_entry *h;
6194
0
  const char *p;
6195
0
  char *copy;
6196
0
  size_t len, first;
6197
6198
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6199
0
  if (h != NULL)
6200
0
    return h;
6201
6202
  /* If this is a default version (the name contains @@), look up the
6203
     symbol again with only one `@' as well as without the version.
6204
     The effect is that references to the symbol with and without the
6205
     version will be matched by the default symbol in the archive.  */
6206
6207
0
  p = strchr (name, ELF_VER_CHR);
6208
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6209
0
    {
6210
      /* Add this symbol to first hash if this archive has the first
6211
   definition.  */
6212
0
      if (is_elf_hash_table (info->hash))
6213
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6214
0
      return h;
6215
0
    }
6216
6217
  /* First check with only one `@'.  */
6218
0
  len = strlen (name);
6219
0
  copy = (char *) bfd_alloc (abfd, len);
6220
0
  if (copy == NULL)
6221
0
    return (struct bfd_link_hash_entry *) -1;
6222
6223
0
  first = p - name + 1;
6224
0
  memcpy (copy, name, first);
6225
0
  memcpy (copy + first, name + first + 1, len - first);
6226
6227
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6228
0
  if (h == NULL)
6229
0
    {
6230
      /* We also need to check references to the symbol without the
6231
   version.  */
6232
0
      copy[first - 1] = '\0';
6233
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6234
0
    }
6235
6236
0
  bfd_release (abfd, copy);
6237
0
  return h;
6238
0
}
6239
6240
/* Add symbols from an ELF archive file to the linker hash table.  We
6241
   don't use _bfd_generic_link_add_archive_symbols because we need to
6242
   handle versioned symbols.
6243
6244
   Fortunately, ELF archive handling is simpler than that done by
6245
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6246
   oddities.  In ELF, if we find a symbol in the archive map, and the
6247
   symbol is currently undefined, we know that we must pull in that
6248
   object file.
6249
6250
   Unfortunately, we do have to make multiple passes over the symbol
6251
   table until nothing further is resolved.  */
6252
6253
static bool
6254
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6255
0
{
6256
0
  symindex c;
6257
0
  unsigned char *included = NULL;
6258
0
  carsym *symdefs;
6259
0
  bool loop;
6260
0
  size_t amt;
6261
0
  elf_backend_data *bed;
6262
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6263
0
    (bfd *, struct bfd_link_info *, const char *);
6264
6265
0
  if (! bfd_has_map (abfd))
6266
0
    {
6267
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6268
6269
      /* An empty archive is a special case.  */
6270
0
      if (first_one == NULL)
6271
0
  return true;
6272
6273
0
      if (!_bfd_make_armap (abfd, first_one))
6274
0
  return false;
6275
0
    }
6276
6277
  /* Keep track of all symbols we know to be already defined, and all
6278
     files we know to be already included.  This is to speed up the
6279
     second and subsequent passes.  */
6280
0
  c = bfd_ardata (abfd)->symdef_count;
6281
0
  if (c == 0)
6282
0
    return true;
6283
0
  amt = c * sizeof (*included);
6284
0
  included = (unsigned char *) bfd_zmalloc (amt);
6285
0
  if (included == NULL)
6286
0
    return false;
6287
6288
0
  symdefs = bfd_ardata (abfd)->symdefs;
6289
0
  bed = get_elf_backend_data (abfd);
6290
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6291
6292
0
  do
6293
0
    {
6294
0
      ufile_ptr_or_bfd last;
6295
0
      symindex i;
6296
0
      carsym *symdef;
6297
0
      carsym *symdefend;
6298
6299
0
      loop = false;
6300
0
      last = _bfd_elt_nil (abfd);
6301
6302
0
      symdef = symdefs;
6303
0
      symdefend = symdef + c;
6304
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6305
0
  {
6306
0
    struct bfd_link_hash_entry *h;
6307
0
    bfd *element;
6308
0
    struct bfd_link_hash_entry *undefs_tail;
6309
0
    symindex mark;
6310
6311
0
    if (included[i])
6312
0
      continue;
6313
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6314
0
      {
6315
0
        included[i] = true;
6316
0
        continue;
6317
0
      }
6318
6319
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6320
0
    if (h == (struct bfd_link_hash_entry *) -1)
6321
0
      goto error_return;
6322
6323
0
    if (h == NULL)
6324
0
      continue;
6325
6326
0
    if (h->type == bfd_link_hash_undefined)
6327
0
      {
6328
0
        if (is_elf_hash_table (info->hash))
6329
0
    {
6330
      /* If the archive element has already been loaded then one
6331
         of the symbols defined by that element might have been
6332
         made undefined due to being in a discarded section.  */
6333
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6334
0
        continue;
6335
6336
      /* In the pre-LTO-plugin pass we must not mistakenly
6337
         include this archive member if an earlier shared
6338
         library defined this symbol.  */
6339
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6340
0
      if (htab->first_hash)
6341
0
        {
6342
0
          struct elf_link_first_hash_entry *e
6343
0
        = ((struct elf_link_first_hash_entry *)
6344
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6345
0
                false, false));
6346
0
          if (e
6347
0
        && (e->abfd->flags & DYNAMIC) != 0
6348
0
        && e->abfd != abfd)
6349
0
      continue;
6350
0
        }
6351
0
    }
6352
0
      }
6353
0
    else if (h->type == bfd_link_hash_common)
6354
0
      {
6355
        /* We currently have a common symbol.  The archive map contains
6356
     a reference to this symbol, so we may want to include it.  We
6357
     only want to include it however, if this archive element
6358
     contains a definition of the symbol, not just another common
6359
     declaration of it.
6360
6361
     Unfortunately some archivers (including GNU ar) will put
6362
     declarations of common symbols into their archive maps, as
6363
     well as real definitions, so we cannot just go by the archive
6364
     map alone.  Instead we must read in the element's symbol
6365
     table and check that to see what kind of symbol definition
6366
     this is.  */
6367
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6368
0
    continue;
6369
0
      }
6370
0
    else
6371
0
      {
6372
0
        if (h->type != bfd_link_hash_undefweak)
6373
    /* Symbol must be defined.  Don't check it again.  */
6374
0
    included[i] = true;
6375
6376
0
        if (!is_elf_hash_table (info->hash))
6377
0
    continue;
6378
0
        struct elf_link_hash_entry *eh
6379
0
    = (struct elf_link_hash_entry *) h;
6380
        /* Ignore the archive if the symbol isn't referenced by a
6381
     regular object or isn't defined in a shared object.  */
6382
0
        if (!eh->ref_regular || !eh->def_dynamic)
6383
0
    continue;
6384
        /* Ignore the dynamic definition if symbol is first
6385
     defined in this archive.  */
6386
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6387
0
        if (htab->first_hash == NULL)
6388
0
    continue;
6389
0
        struct elf_link_first_hash_entry *e
6390
0
    = ((struct elf_link_first_hash_entry *)
6391
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6392
0
            false, false));
6393
0
        if (e == NULL || e->abfd != abfd)
6394
0
    continue;
6395
0
      }
6396
6397
    /* We need to include this archive member.  */
6398
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6399
0
    if (element == NULL)
6400
0
      goto error_return;
6401
6402
0
    if (! bfd_check_format (element, bfd_object))
6403
0
      goto error_return;
6404
6405
0
    undefs_tail = info->hash->undefs_tail;
6406
6407
0
    if (!(*info->callbacks
6408
0
    ->add_archive_element) (info, element, symdef->name, &element))
6409
0
      continue;
6410
0
    if (!bfd_link_add_symbols (element, info))
6411
0
      goto error_return;
6412
6413
    /* If there are any new undefined symbols, we need to make
6414
       another pass through the archive in order to see whether
6415
       they can be defined.  FIXME: This isn't perfect, because
6416
       common symbols wind up on undefs_tail and because an
6417
       undefined symbol which is defined later on in this pass
6418
       does not require another pass.  This isn't a bug, but it
6419
       does make the code less efficient than it could be.  */
6420
0
    if (undefs_tail != info->hash->undefs_tail)
6421
0
      loop = true;
6422
6423
    /* Look backward to mark all symbols from this object file
6424
       which we have already seen in this pass.  */
6425
0
    mark = i;
6426
0
    do
6427
0
      {
6428
0
        included[mark] = true;
6429
0
        if (mark == 0)
6430
0
    break;
6431
0
        --mark;
6432
0
      }
6433
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6434
6435
    /* We mark subsequent symbols from this object file as we go
6436
       on through the loop.  */
6437
0
    last = symdef->u;
6438
0
  }
6439
0
    }
6440
0
  while (loop);
6441
6442
0
  free (included);
6443
0
  return true;
6444
6445
0
 error_return:
6446
0
  free (included);
6447
0
  return false;
6448
0
}
6449
6450
/* Given an ELF BFD, add symbols to the global hash table as
6451
   appropriate.  */
6452
6453
bool
6454
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6455
0
{
6456
0
  switch (bfd_get_format (abfd))
6457
0
    {
6458
0
    case bfd_object:
6459
0
      return elf_link_add_object_symbols (abfd, info);
6460
0
    case bfd_archive:
6461
0
      return elf_link_add_archive_symbols (abfd, info);
6462
0
    default:
6463
0
      bfd_set_error (bfd_error_wrong_format);
6464
0
      return false;
6465
0
    }
6466
0
}
6467

6468
struct hash_codes_info
6469
{
6470
  unsigned long *hashcodes;
6471
  bool error;
6472
};
6473
6474
/* This function will be called though elf_link_hash_traverse to store
6475
   all hash value of the exported symbols in an array.  */
6476
6477
static bool
6478
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6479
0
{
6480
0
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
6481
0
  const char *name;
6482
0
  unsigned long ha;
6483
0
  char *alc = NULL;
6484
6485
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6486
0
  if (h->dynindx == -1)
6487
0
    return true;
6488
6489
0
  name = h->root.root.string;
6490
0
  if (h->versioned >= versioned)
6491
0
    {
6492
0
      const char *p = strchr (name, ELF_VER_CHR);
6493
0
      if (p != NULL)
6494
0
  {
6495
0
    alc = (char *) bfd_malloc (p - name + 1);
6496
0
    if (alc == NULL)
6497
0
      {
6498
0
        inf->error = true;
6499
0
        return false;
6500
0
      }
6501
0
    memcpy (alc, name, p - name);
6502
0
    alc[p - name] = '\0';
6503
0
    name = alc;
6504
0
  }
6505
0
    }
6506
6507
  /* Compute the hash value.  */
6508
0
  ha = bfd_elf_hash (name);
6509
6510
  /* Store the found hash value in the array given as the argument.  */
6511
0
  *(inf->hashcodes)++ = ha;
6512
6513
  /* And store it in the struct so that we can put it in the hash table
6514
     later.  */
6515
0
  h->u.elf_hash_value = ha;
6516
6517
0
  free (alc);
6518
0
  return true;
6519
0
}
6520
6521
struct collect_gnu_hash_codes
6522
{
6523
  bfd *output_bfd;
6524
  elf_backend_data *bed;
6525
  unsigned long int nsyms;
6526
  unsigned long int maskbits;
6527
  unsigned long int *hashcodes;
6528
  unsigned long int *hashval;
6529
  unsigned long int *indx;
6530
  unsigned long int *counts;
6531
  bfd_vma *bitmask;
6532
  bfd_byte *contents;
6533
  bfd_size_type xlat;
6534
  long int min_dynindx;
6535
  unsigned long int bucketcount;
6536
  unsigned long int symindx;
6537
  long int local_indx;
6538
  long int shift1, shift2;
6539
  unsigned long int mask;
6540
  bool error;
6541
  bool base_symbol;
6542
};
6543
6544
/* This function will be called though elf_link_hash_traverse to store
6545
   all hash value of the exported symbols in an array.  */
6546
6547
static bool
6548
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6549
0
{
6550
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6551
0
  const char *name;
6552
0
  unsigned long ha;
6553
0
  char *alc = NULL;
6554
6555
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6556
0
  if (h->dynindx == -1)
6557
0
    return true;
6558
6559
  /* Ignore also local symbols and undefined symbols.  */
6560
0
  if (! (*s->bed->elf_hash_symbol) (h))
6561
0
    return true;
6562
6563
0
  name = h->root.root.string;
6564
0
  if (h->versioned >= versioned)
6565
0
    {
6566
0
      const char *p = strchr (name, ELF_VER_CHR);
6567
0
      if (p != NULL)
6568
0
  {
6569
0
    alc = (char *) bfd_malloc (p - name + 1);
6570
0
    if (alc == NULL)
6571
0
      {
6572
0
        s->error = true;
6573
0
        return false;
6574
0
      }
6575
0
    memcpy (alc, name, p - name);
6576
0
    alc[p - name] = '\0';
6577
0
    name = alc;
6578
0
  }
6579
0
    }
6580
6581
  /* Compute the hash value.  */
6582
0
  ha = bfd_elf_gnu_hash (name);
6583
6584
  /* Store the found hash value in the array for compute_bucket_count,
6585
     and also for .dynsym reordering purposes.  */
6586
0
  s->hashcodes[s->nsyms] = ha;
6587
0
  s->hashval[h->dynindx] = ha;
6588
0
  ++s->nsyms;
6589
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6590
0
    s->min_dynindx = h->dynindx;
6591
6592
0
  free (alc);
6593
0
  return true;
6594
0
}
6595
6596
/* This function will be called though elf_link_hash_traverse to do
6597
   final dynamic symbol renumbering in case of .gnu.hash.
6598
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6599
   to the translation table.  */
6600
6601
static bool
6602
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6603
0
{
6604
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6605
0
  unsigned long int bucket;
6606
0
  unsigned long int val;
6607
6608
  /* Ignore indirect symbols.  */
6609
0
  if (h->dynindx == -1)
6610
0
    return true;
6611
6612
  /* Skip if base symbol doesn't match.  */
6613
0
  if (s->base_symbol != !!h->base_symbol)
6614
0
    return true;
6615
6616
  /* Ignore also local symbols and undefined symbols.  */
6617
0
  if (! (*s->bed->elf_hash_symbol) (h))
6618
0
    {
6619
0
      if (h->dynindx >= s->min_dynindx)
6620
0
  {
6621
0
    if (s->bed->record_xhash_symbol != NULL)
6622
0
      {
6623
0
        (*s->bed->record_xhash_symbol) (h, 0);
6624
0
        s->local_indx++;
6625
0
      }
6626
0
    else
6627
0
      h->dynindx = s->local_indx++;
6628
0
  }
6629
0
      return true;
6630
0
    }
6631
6632
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6633
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6634
0
  & ((s->maskbits >> s->shift1) - 1);
6635
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6636
0
  s->bitmask[val]
6637
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6638
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6639
0
  if (s->counts[bucket] == 1)
6640
    /* Last element terminates the chain.  */
6641
0
    val |= 1;
6642
0
  bfd_put_32 (s->output_bfd, val,
6643
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6644
0
  --s->counts[bucket];
6645
0
  if (s->bed->record_xhash_symbol != NULL)
6646
0
    {
6647
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6648
6649
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6650
0
    }
6651
0
  else
6652
0
    h->dynindx = s->indx[bucket]++;
6653
0
  return true;
6654
0
}
6655
6656
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6657
6658
bool
6659
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6660
0
{
6661
0
  return !(h->forced_local
6662
0
     || h->root.type == bfd_link_hash_undefined
6663
0
     || h->root.type == bfd_link_hash_undefweak
6664
0
     || ((h->root.type == bfd_link_hash_defined
6665
0
    || h->root.type == bfd_link_hash_defweak)
6666
0
         && h->root.u.def.section->output_section == NULL));
6667
0
}
6668
6669
/* Array used to determine the number of hash table buckets to use
6670
   based on the number of symbols there are.  If there are fewer than
6671
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6672
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6673
   than 32771 buckets.  */
6674
6675
static const size_t elf_buckets[] =
6676
{
6677
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6678
  16411, 32771, 0
6679
};
6680
6681
/* Compute bucket count for hashing table.  We do not use a static set
6682
   of possible tables sizes anymore.  Instead we determine for all
6683
   possible reasonable sizes of the table the outcome (i.e., the
6684
   number of collisions etc) and choose the best solution.  The
6685
   weighting functions are not too simple to allow the table to grow
6686
   without bounds.  Instead one of the weighting factors is the size.
6687
   Therefore the result is always a good payoff between few collisions
6688
   (= short chain lengths) and table size.  */
6689
static size_t
6690
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6691
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6692
          unsigned long int nsyms,
6693
          int gnu_hash)
6694
0
{
6695
0
  size_t best_size = 0;
6696
0
  unsigned long int i;
6697
6698
0
  if (info->optimize)
6699
0
    {
6700
0
      size_t minsize;
6701
0
      size_t maxsize;
6702
0
      uint64_t best_chlen = ~((uint64_t) 0);
6703
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6704
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6705
0
      elf_backend_data *bed = get_elf_backend_data (dynobj);
6706
0
      unsigned long int *counts;
6707
0
      bfd_size_type amt;
6708
0
      unsigned int no_improvement_count = 0;
6709
6710
      /* Possible optimization parameters: if we have NSYMS symbols we say
6711
   that the hashing table must at least have NSYMS/4 and at most
6712
   2*NSYMS buckets.  */
6713
0
      minsize = nsyms / 4;
6714
0
      if (minsize == 0)
6715
0
  minsize = 1;
6716
0
      best_size = maxsize = nsyms * 2;
6717
0
      if (gnu_hash)
6718
0
  {
6719
0
    if (minsize < 2)
6720
0
      minsize = 2;
6721
0
    if ((best_size & 31) == 0)
6722
0
      ++best_size;
6723
0
  }
6724
6725
      /* Create array where we count the collisions in.  We must use bfd_malloc
6726
   since the size could be large.  */
6727
0
      amt = maxsize;
6728
0
      amt *= sizeof (unsigned long int);
6729
0
      counts = (unsigned long int *) bfd_malloc (amt);
6730
0
      if (counts == NULL)
6731
0
  return 0;
6732
6733
      /* Compute the "optimal" size for the hash table.  The criteria is a
6734
   minimal chain length.  The minor criteria is (of course) the size
6735
   of the table.  */
6736
0
      for (i = minsize; i < maxsize; ++i)
6737
0
  {
6738
    /* Walk through the array of hashcodes and count the collisions.  */
6739
0
    uint64_t max;
6740
0
    unsigned long int j;
6741
0
    unsigned long int fact;
6742
6743
0
    if (gnu_hash && (i & 31) == 0)
6744
0
      continue;
6745
6746
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6747
6748
    /* Determine how often each hash bucket is used.  */
6749
0
    for (j = 0; j < nsyms; ++j)
6750
0
      ++counts[hashcodes[j] % i];
6751
6752
    /* For the weight function we need some information about the
6753
       pagesize on the target.  This is information need not be 100%
6754
       accurate.  Since this information is not available (so far) we
6755
       define it here to a reasonable default value.  If it is crucial
6756
       to have a better value some day simply define this value.  */
6757
0
# ifndef BFD_TARGET_PAGESIZE
6758
0
#  define BFD_TARGET_PAGESIZE (4096)
6759
0
# endif
6760
6761
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6762
       and the chains.  */
6763
0
    max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6764
6765
0
# if 1
6766
    /* Variant 1: optimize for short chains.  We add the squares
6767
       of all the chain lengths (which favors many small chain
6768
       over a few long chains).  */
6769
0
    for (j = 0; j < i; ++j)
6770
0
      max += counts[j] * counts[j];
6771
6772
    /* This adds penalties for the overall size of the table.  */
6773
0
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6774
0
    max *= fact * fact;
6775
# else
6776
    /* Variant 2: Optimize a lot more for small table.  Here we
6777
       also add squares of the size but we also add penalties for
6778
       empty slots (the +1 term).  */
6779
    for (j = 0; j < i; ++j)
6780
      max += (1 + counts[j]) * (1 + counts[j]);
6781
6782
    /* The overall size of the table is considered, but not as
6783
       strong as in variant 1, where it is squared.  */
6784
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6785
    max *= fact;
6786
# endif
6787
6788
    /* Compare with current best results.  */
6789
0
    if (max < best_chlen)
6790
0
      {
6791
0
        best_chlen = max;
6792
0
        best_size = i;
6793
0
        no_improvement_count = 0;
6794
0
      }
6795
    /* PR 11843: Avoid futile long searches for the best bucket size
6796
       when there are a large number of symbols.  */
6797
0
    else if (++no_improvement_count == 100)
6798
0
      break;
6799
0
  }
6800
6801
0
      free (counts);
6802
0
    }
6803
0
  else
6804
0
    {
6805
0
      for (i = 0; elf_buckets[i] != 0; i++)
6806
0
  {
6807
0
    best_size = elf_buckets[i];
6808
0
    if (nsyms < elf_buckets[i + 1])
6809
0
      break;
6810
0
  }
6811
0
      if (gnu_hash && best_size < 2)
6812
0
  best_size = 2;
6813
0
    }
6814
6815
0
  return best_size;
6816
0
}
6817
6818
/* Size any SHT_GROUP section for ld -r.  */
6819
6820
bool
6821
bfd_elf_size_group_sections (struct bfd_link_info *info)
6822
0
{
6823
0
  bfd *ibfd;
6824
0
  asection *s;
6825
6826
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6827
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6828
0
  && (s = ibfd->sections) != NULL
6829
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6830
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6831
0
      return false;
6832
0
  return true;
6833
0
}
6834
6835
/* Set a default stack segment size.  The value in INFO wins.  If it
6836
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6837
   undefined it is initialized.  */
6838
6839
bool
6840
bfd_elf_stack_segment_size (bfd *output_bfd,
6841
          struct bfd_link_info *info,
6842
          const char *legacy_symbol,
6843
          bfd_vma default_size)
6844
0
{
6845
0
  struct elf_link_hash_entry *h = NULL;
6846
6847
  /* Look for legacy symbol.  */
6848
0
  if (legacy_symbol)
6849
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6850
0
            false, false, false);
6851
0
  if (h && (h->root.type == bfd_link_hash_defined
6852
0
      || h->root.type == bfd_link_hash_defweak)
6853
0
      && h->def_regular
6854
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6855
0
    {
6856
      /* The symbol has no type if specified on the command line.  */
6857
0
      h->type = STT_OBJECT;
6858
0
      if (info->stacksize)
6859
  /* xgettext:c-format */
6860
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6861
0
          output_bfd, legacy_symbol);
6862
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6863
  /* xgettext:c-format */
6864
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6865
0
          output_bfd, legacy_symbol);
6866
0
      else
6867
0
  info->stacksize = h->root.u.def.value;
6868
0
    }
6869
6870
0
  if (!info->stacksize)
6871
    /* If the user didn't set a size, or explicitly inhibit the
6872
       size, set it now.  */
6873
0
    info->stacksize = default_size;
6874
6875
  /* Provide the legacy symbol, if it is referenced.  */
6876
0
  if (h && (h->root.type == bfd_link_hash_undefined
6877
0
      || h->root.type == bfd_link_hash_undefweak))
6878
0
    {
6879
0
      struct bfd_link_hash_entry *bh = NULL;
6880
6881
0
      if (!(_bfd_generic_link_add_one_symbol
6882
0
      (info, output_bfd, legacy_symbol,
6883
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6884
0
       info->stacksize >= 0 ? info->stacksize : 0,
6885
0
       NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6886
0
  return false;
6887
6888
0
      h = (struct elf_link_hash_entry *) bh;
6889
0
      h->def_regular = 1;
6890
0
      h->type = STT_OBJECT;
6891
0
    }
6892
6893
0
  return true;
6894
0
}
6895
6896
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6897
6898
struct elf_gc_sweep_symbol_info
6899
{
6900
  struct bfd_link_info *info;
6901
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6902
           bool);
6903
};
6904
6905
static bool
6906
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6907
0
{
6908
0
  if (!h->mark
6909
0
      && (((h->root.type == bfd_link_hash_defined
6910
0
      || h->root.type == bfd_link_hash_defweak)
6911
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6912
0
    && h->root.u.def.section->gc_mark))
6913
0
    || h->root.type == bfd_link_hash_undefined
6914
0
    || h->root.type == bfd_link_hash_undefweak))
6915
0
    {
6916
0
      struct elf_gc_sweep_symbol_info *inf;
6917
6918
0
      inf = (struct elf_gc_sweep_symbol_info *) data;
6919
0
      (*inf->hide_symbol) (inf->info, h, true);
6920
0
      h->def_regular = 0;
6921
0
      h->ref_regular = 0;
6922
0
      h->ref_regular_nonweak = 0;
6923
0
    }
6924
6925
0
  return true;
6926
0
}
6927
6928
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6929
   called by the ELF linker emulation before_allocation routine.  We
6930
   must set the sizes of the sections before the linker sets the
6931
   addresses of the various sections.  */
6932
6933
bool
6934
bfd_elf_size_dynamic_sections (bfd *output_bfd,
6935
             const char *soname,
6936
             const char *rpath,
6937
             const char *filter_shlib,
6938
             const char *audit,
6939
             const char *depaudit,
6940
             const char * const *auxiliary_filters,
6941
             struct bfd_link_info *info,
6942
             asection **sinterpptr)
6943
0
{
6944
0
  bfd *dynobj;
6945
0
  elf_backend_data *bed;
6946
6947
0
  *sinterpptr = NULL;
6948
6949
0
  if (!is_elf_hash_table (info->hash))
6950
0
    return true;
6951
6952
  /* Any syms created from now on start with -1 in
6953
     got.refcount/offset and plt.refcount/offset.  */
6954
0
  elf_hash_table (info)->init_got_refcount
6955
0
    = elf_hash_table (info)->init_got_offset;
6956
0
  elf_hash_table (info)->init_plt_refcount
6957
0
    = elf_hash_table (info)->init_plt_offset;
6958
6959
0
  bed = get_elf_backend_data (output_bfd);
6960
6961
  /* The backend may have to create some sections regardless of whether
6962
     we're dynamic or not.  */
6963
0
  if (bed->elf_backend_early_size_sections
6964
0
      && !bed->elf_backend_early_size_sections (output_bfd, info))
6965
0
    return false;
6966
6967
0
  dynobj = elf_hash_table (info)->dynobj;
6968
6969
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6970
0
    {
6971
0
      struct bfd_elf_version_tree *verdefs;
6972
0
      struct elf_info_failed asvinfo;
6973
0
      struct bfd_elf_version_tree *t;
6974
0
      struct bfd_elf_version_expr *d;
6975
0
      asection *s;
6976
0
      size_t soname_indx;
6977
6978
      /* If we are supposed to export all symbols into the dynamic symbol
6979
   table (this is not the normal case), then do so.  */
6980
0
      if (info->export_dynamic
6981
0
    || (bfd_link_executable (info) && info->dynamic))
6982
0
  {
6983
0
    struct elf_info_failed eif;
6984
6985
0
    eif.info = info;
6986
0
    eif.failed = false;
6987
0
    elf_link_hash_traverse (elf_hash_table (info),
6988
0
          _bfd_elf_export_symbol,
6989
0
          &eif);
6990
0
    if (eif.failed)
6991
0
      return false;
6992
0
  }
6993
6994
0
      if (soname != NULL)
6995
0
  {
6996
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6997
0
               soname, true);
6998
0
    if (soname_indx == (size_t) -1
6999
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
7000
0
      return false;
7001
0
  }
7002
0
      else
7003
0
  soname_indx = (size_t) -1;
7004
7005
      /* Make all global versions with definition.  */
7006
0
      for (t = info->version_info; t != NULL; t = t->next)
7007
0
  for (d = t->globals.list; d != NULL; d = d->next)
7008
0
    if (!d->symver && d->literal)
7009
0
      {
7010
0
        const char *verstr, *name;
7011
0
        size_t namelen, verlen, newlen;
7012
0
        char *newname, *p, leading_char;
7013
0
        struct elf_link_hash_entry *newh;
7014
7015
0
        leading_char = bfd_get_symbol_leading_char (output_bfd);
7016
0
        name = d->pattern;
7017
0
        namelen = strlen (name) + (leading_char != '\0');
7018
0
        verstr = t->name;
7019
0
        verlen = strlen (verstr);
7020
0
        newlen = namelen + verlen + 3;
7021
7022
0
        newname = (char *) bfd_malloc (newlen);
7023
0
        if (newname == NULL)
7024
0
    return false;
7025
0
        newname[0] = leading_char;
7026
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7027
7028
        /* Check the hidden versioned definition.  */
7029
0
        p = newname + namelen;
7030
0
        *p++ = ELF_VER_CHR;
7031
0
        memcpy (p, verstr, verlen + 1);
7032
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7033
0
             newname, false, false,
7034
0
             false);
7035
0
        if (newh == NULL
7036
0
      || (newh->root.type != bfd_link_hash_defined
7037
0
          && newh->root.type != bfd_link_hash_defweak))
7038
0
    {
7039
      /* Check the default versioned definition.  */
7040
0
      *p++ = ELF_VER_CHR;
7041
0
      memcpy (p, verstr, verlen + 1);
7042
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7043
0
                 newname, false, false,
7044
0
                 false);
7045
0
    }
7046
0
        free (newname);
7047
7048
        /* Mark this version if there is a definition and it is
7049
     not defined in a shared object.  */
7050
0
        if (newh != NULL
7051
0
      && !newh->def_dynamic
7052
0
      && (newh->root.type == bfd_link_hash_defined
7053
0
          || newh->root.type == bfd_link_hash_defweak))
7054
0
    d->symver = 1;
7055
0
      }
7056
7057
      /* Attach all the symbols to their version information.  */
7058
0
      asvinfo.info = info;
7059
0
      asvinfo.failed = false;
7060
7061
0
      elf_link_hash_traverse (elf_hash_table (info),
7062
0
            _bfd_elf_link_assign_sym_version,
7063
0
            &asvinfo);
7064
0
      if (asvinfo.failed)
7065
0
  return false;
7066
7067
0
      if (!info->allow_undefined_version)
7068
0
  {
7069
    /* Check if all global versions have a definition.  */
7070
0
    bool all_defined = true;
7071
0
    for (t = info->version_info; t != NULL; t = t->next)
7072
0
      for (d = t->globals.list; d != NULL; d = d->next)
7073
0
        if (d->literal && !d->symver && !d->script)
7074
0
    {
7075
0
      _bfd_error_handler
7076
0
        (_("%s: undefined version: %s"),
7077
0
         d->pattern, t->name);
7078
0
      all_defined = false;
7079
0
    }
7080
7081
0
    if (!all_defined)
7082
0
      {
7083
0
        bfd_set_error (bfd_error_bad_value);
7084
0
        return false;
7085
0
      }
7086
0
  }
7087
7088
      /* Set up the version definition section.  */
7089
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7090
0
      BFD_ASSERT (s != NULL);
7091
7092
      /* We may have created additional version definitions if we are
7093
   just linking a regular application.  */
7094
0
      verdefs = info->version_info;
7095
7096
      /* Skip anonymous version tag.  */
7097
0
      if (verdefs != NULL && verdefs->vernum == 0)
7098
0
  verdefs = verdefs->next;
7099
7100
0
      if (verdefs == NULL && !info->create_default_symver)
7101
0
  s->flags |= SEC_EXCLUDE;
7102
0
      else
7103
0
  {
7104
0
    unsigned int cdefs;
7105
0
    bfd_size_type size;
7106
0
    bfd_byte *p;
7107
0
    Elf_Internal_Verdef def;
7108
0
    Elf_Internal_Verdaux defaux;
7109
0
    struct bfd_link_hash_entry *bh;
7110
0
    struct elf_link_hash_entry *h;
7111
0
    const char *name;
7112
7113
0
    cdefs = 0;
7114
0
    size = 0;
7115
7116
    /* Make space for the base version.  */
7117
0
    size += sizeof (Elf_External_Verdef);
7118
0
    size += sizeof (Elf_External_Verdaux);
7119
0
    ++cdefs;
7120
7121
    /* Make space for the default version.  */
7122
0
    if (info->create_default_symver)
7123
0
      {
7124
0
        size += sizeof (Elf_External_Verdef);
7125
0
        ++cdefs;
7126
0
      }
7127
7128
0
    for (t = verdefs; t != NULL; t = t->next)
7129
0
      {
7130
0
        struct bfd_elf_version_deps *n;
7131
7132
        /* Don't emit base version twice.  */
7133
0
        if (t->vernum == 0)
7134
0
    continue;
7135
7136
0
        size += sizeof (Elf_External_Verdef);
7137
0
        size += sizeof (Elf_External_Verdaux);
7138
0
        ++cdefs;
7139
7140
0
        for (n = t->deps; n != NULL; n = n->next)
7141
0
    size += sizeof (Elf_External_Verdaux);
7142
0
      }
7143
7144
0
    s->size = size;
7145
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7146
0
    if (s->contents == NULL && s->size != 0)
7147
0
      return false;
7148
0
    s->alloced = 1;
7149
7150
    /* Fill in the version definition section.  */
7151
7152
0
    p = s->contents;
7153
7154
0
    def.vd_version = VER_DEF_CURRENT;
7155
0
    def.vd_flags = VER_FLG_BASE;
7156
0
    def.vd_ndx = 1;
7157
0
    def.vd_cnt = 1;
7158
0
    if (info->create_default_symver)
7159
0
      {
7160
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7161
0
        def.vd_next = sizeof (Elf_External_Verdef);
7162
0
      }
7163
0
    else
7164
0
      {
7165
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7166
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7167
0
           + sizeof (Elf_External_Verdaux));
7168
0
      }
7169
7170
0
    if (soname_indx != (size_t) -1)
7171
0
      {
7172
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7173
0
              soname_indx);
7174
0
        def.vd_hash = bfd_elf_hash (soname);
7175
0
        defaux.vda_name = soname_indx;
7176
0
        name = soname;
7177
0
      }
7178
0
    else
7179
0
      {
7180
0
        size_t indx;
7181
7182
0
        name = lbasename (bfd_get_filename (output_bfd));
7183
0
        def.vd_hash = bfd_elf_hash (name);
7184
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7185
0
            name, false);
7186
0
        if (indx == (size_t) -1)
7187
0
    return false;
7188
0
        defaux.vda_name = indx;
7189
0
      }
7190
0
    defaux.vda_next = 0;
7191
7192
0
    _bfd_elf_swap_verdef_out (output_bfd, &def,
7193
0
            (Elf_External_Verdef *) p);
7194
0
    p += sizeof (Elf_External_Verdef);
7195
0
    if (info->create_default_symver)
7196
0
      {
7197
        /* Add a symbol representing this version.  */
7198
0
        bh = NULL;
7199
0
        if (! (_bfd_generic_link_add_one_symbol
7200
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7201
0
          0, NULL, false,
7202
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7203
0
    return false;
7204
0
        h = (struct elf_link_hash_entry *) bh;
7205
0
        h->non_elf = 0;
7206
0
        h->def_regular = 1;
7207
0
        h->type = STT_OBJECT;
7208
0
        h->verinfo.vertree = NULL;
7209
7210
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7211
0
    return false;
7212
7213
        /* Create a duplicate of the base version with the same
7214
     aux block, but different flags.  */
7215
0
        def.vd_flags = 0;
7216
0
        def.vd_ndx = 2;
7217
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7218
0
        if (verdefs)
7219
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7220
0
             + sizeof (Elf_External_Verdaux));
7221
0
        else
7222
0
    def.vd_next = 0;
7223
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7224
0
          (Elf_External_Verdef *) p);
7225
0
        p += sizeof (Elf_External_Verdef);
7226
0
      }
7227
0
    _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7228
0
             (Elf_External_Verdaux *) p);
7229
0
    p += sizeof (Elf_External_Verdaux);
7230
7231
0
    for (t = verdefs; t != NULL; t = t->next)
7232
0
      {
7233
0
        unsigned int cdeps;
7234
0
        struct bfd_elf_version_deps *n;
7235
7236
        /* Don't emit the base version twice.  */
7237
0
        if (t->vernum == 0)
7238
0
    continue;
7239
7240
0
        cdeps = 0;
7241
0
        for (n = t->deps; n != NULL; n = n->next)
7242
0
    ++cdeps;
7243
7244
        /* Add a symbol representing this version.  */
7245
0
        bh = NULL;
7246
0
        if (! (_bfd_generic_link_add_one_symbol
7247
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7248
0
          0, NULL, false,
7249
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7250
0
    return false;
7251
0
        h = (struct elf_link_hash_entry *) bh;
7252
0
        h->non_elf = 0;
7253
0
        h->def_regular = 1;
7254
0
        h->type = STT_OBJECT;
7255
0
        h->verinfo.vertree = t;
7256
7257
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7258
0
    return false;
7259
7260
0
        def.vd_version = VER_DEF_CURRENT;
7261
0
        def.vd_flags = 0;
7262
0
        if (t->globals.list == NULL
7263
0
      && t->locals.list == NULL
7264
0
      && ! t->used)
7265
0
    def.vd_flags |= VER_FLG_WEAK;
7266
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7267
0
        def.vd_cnt = cdeps + 1;
7268
0
        def.vd_hash = bfd_elf_hash (t->name);
7269
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7270
0
        def.vd_next = 0;
7271
7272
        /* If a basever node is next, it *must* be the last node in
7273
     the chain, otherwise Verdef construction breaks.  */
7274
0
        if (t->next != NULL && t->next->vernum == 0)
7275
0
    BFD_ASSERT (t->next->next == NULL);
7276
7277
0
        if (t->next != NULL && t->next->vernum != 0)
7278
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7279
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7280
7281
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7282
0
          (Elf_External_Verdef *) p);
7283
0
        p += sizeof (Elf_External_Verdef);
7284
7285
0
        defaux.vda_name = h->dynstr_index;
7286
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7287
0
              h->dynstr_index);
7288
0
        defaux.vda_next = 0;
7289
0
        if (t->deps != NULL)
7290
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7291
0
        t->name_indx = defaux.vda_name;
7292
7293
0
        _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7294
0
           (Elf_External_Verdaux *) p);
7295
0
        p += sizeof (Elf_External_Verdaux);
7296
7297
0
        for (n = t->deps; n != NULL; n = n->next)
7298
0
    {
7299
0
      if (n->version_needed == NULL)
7300
0
        {
7301
          /* This can happen if there was an error in the
7302
       version script.  */
7303
0
          defaux.vda_name = 0;
7304
0
        }
7305
0
      else
7306
0
        {
7307
0
          defaux.vda_name = n->version_needed->name_indx;
7308
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7309
0
                defaux.vda_name);
7310
0
        }
7311
0
      if (n->next == NULL)
7312
0
        defaux.vda_next = 0;
7313
0
      else
7314
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7315
7316
0
      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7317
0
               (Elf_External_Verdaux *) p);
7318
0
      p += sizeof (Elf_External_Verdaux);
7319
0
    }
7320
0
      }
7321
7322
0
    elf_tdata (output_bfd)->cverdefs = cdefs;
7323
0
  }
7324
0
    }
7325
7326
0
  if (info->gc_sections && bed->can_gc_sections)
7327
0
    {
7328
0
      struct elf_gc_sweep_symbol_info sweep_info;
7329
7330
      /* Remove the symbols that were in the swept sections from the
7331
   dynamic symbol table.  */
7332
0
      sweep_info.info = info;
7333
0
      sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7334
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7335
0
            &sweep_info);
7336
0
    }
7337
7338
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7339
0
    {
7340
0
      asection *s;
7341
0
      struct elf_find_verdep_info sinfo;
7342
7343
      /* Work out the size of the version reference section.  */
7344
7345
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7346
0
      BFD_ASSERT (s != NULL);
7347
7348
0
      sinfo.info = info;
7349
0
      sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7350
0
      if (sinfo.vers == 0)
7351
0
  sinfo.vers = 1;
7352
0
      sinfo.failed = false;
7353
7354
0
      elf_link_hash_traverse (elf_hash_table (info),
7355
0
            _bfd_elf_link_find_version_dependencies,
7356
0
            &sinfo);
7357
0
      if (sinfo.failed)
7358
0
  return false;
7359
7360
0
      bed->elf_backend_add_glibc_version_dependency (&sinfo);
7361
0
      if (sinfo.failed)
7362
0
  return false;
7363
7364
0
      if (elf_tdata (output_bfd)->verref == NULL)
7365
0
  s->flags |= SEC_EXCLUDE;
7366
0
      else
7367
0
  {
7368
0
    Elf_Internal_Verneed *vn;
7369
0
    unsigned int size;
7370
0
    unsigned int crefs;
7371
0
    bfd_byte *p;
7372
7373
    /* Build the version dependency section.  */
7374
0
    size = 0;
7375
0
    crefs = 0;
7376
0
    for (vn = elf_tdata (output_bfd)->verref;
7377
0
         vn != NULL;
7378
0
         vn = vn->vn_nextref)
7379
0
      {
7380
0
        Elf_Internal_Vernaux *a;
7381
7382
0
        size += sizeof (Elf_External_Verneed);
7383
0
        ++crefs;
7384
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7385
0
    size += sizeof (Elf_External_Vernaux);
7386
0
      }
7387
7388
0
    s->size = size;
7389
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7390
0
    if (s->contents == NULL)
7391
0
      return false;
7392
0
    s->alloced = 1;
7393
7394
0
    p = s->contents;
7395
0
    for (vn = elf_tdata (output_bfd)->verref;
7396
0
         vn != NULL;
7397
0
         vn = vn->vn_nextref)
7398
0
      {
7399
0
        unsigned int caux;
7400
0
        Elf_Internal_Vernaux *a;
7401
0
        size_t indx;
7402
7403
0
        caux = 0;
7404
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7405
0
    ++caux;
7406
7407
0
        vn->vn_version = VER_NEED_CURRENT;
7408
0
        vn->vn_cnt = caux;
7409
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7410
0
            elf_dt_name (vn->vn_bfd) != NULL
7411
0
            ? elf_dt_name (vn->vn_bfd)
7412
0
            : lbasename (bfd_get_filename
7413
0
                   (vn->vn_bfd)),
7414
0
            false);
7415
0
        if (indx == (size_t) -1)
7416
0
    return false;
7417
0
        vn->vn_file = indx;
7418
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7419
0
        if (vn->vn_nextref == NULL)
7420
0
    vn->vn_next = 0;
7421
0
        else
7422
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7423
0
             + caux * sizeof (Elf_External_Vernaux));
7424
7425
0
        _bfd_elf_swap_verneed_out (output_bfd, vn,
7426
0
           (Elf_External_Verneed *) p);
7427
0
        p += sizeof (Elf_External_Verneed);
7428
7429
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7430
0
    {
7431
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7432
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7433
0
                a->vna_nodename, false);
7434
0
      if (indx == (size_t) -1)
7435
0
        return false;
7436
0
      a->vna_name = indx;
7437
0
      if (a->vna_nextptr == NULL)
7438
0
        a->vna_next = 0;
7439
0
      else
7440
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7441
7442
0
      _bfd_elf_swap_vernaux_out (output_bfd, a,
7443
0
               (Elf_External_Vernaux *) p);
7444
0
      p += sizeof (Elf_External_Vernaux);
7445
0
    }
7446
0
      }
7447
7448
0
    elf_tdata (output_bfd)->cverrefs = crefs;
7449
0
  }
7450
0
    }
7451
7452
0
  if (bfd_link_relocatable (info)
7453
0
      && !bfd_elf_size_group_sections (info))
7454
0
    return false;
7455
7456
  /* Determine any GNU_STACK segment requirements, after the backend
7457
     has had a chance to set a default segment size.  */
7458
0
  if (info->execstack)
7459
0
    {
7460
      /* If the user has explicitly requested warnings, then generate one even
7461
   though the choice is the result of another command line option.  */
7462
0
      if (info->warn_execstack == 1)
7463
0
  {
7464
0
    if (info->error_execstack)
7465
0
      {
7466
0
        _bfd_error_handler
7467
0
    (_("\
7468
0
error: creating an executable stack because of -z execstack command line option"));
7469
0
        return false;
7470
0
      }
7471
7472
0
    _bfd_error_handler
7473
0
      (_("\
7474
0
warning: enabling an executable stack because of -z execstack command line option"));
7475
0
  }
7476
7477
0
      elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7478
0
    }
7479
0
  else if (info->noexecstack)
7480
0
    elf_stack_flags (output_bfd) = PF_R | PF_W;
7481
0
  else
7482
0
    {
7483
0
      bfd *inputobj;
7484
0
      asection *notesec = NULL;
7485
0
      bfd *noteobj = NULL;
7486
0
      bfd *emptyobj = NULL;
7487
0
      int exec = 0;
7488
7489
0
      for (inputobj = info->input_bfds;
7490
0
     inputobj;
7491
0
     inputobj = inputobj->link.next)
7492
0
  {
7493
0
    asection *s;
7494
7495
0
    if (inputobj->flags
7496
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7497
0
      continue;
7498
0
    s = inputobj->sections;
7499
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7500
0
      continue;
7501
7502
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7503
0
    if (s)
7504
0
      {
7505
0
        notesec = s;
7506
0
        if (s->flags & SEC_CODE)
7507
0
    {
7508
0
      noteobj = inputobj;
7509
0
      exec = PF_X;
7510
      /* There is no point in scanning the remaining bfds.  */
7511
0
      break;
7512
0
    }
7513
0
      }
7514
0
    else if (bed->default_execstack && info->default_execstack)
7515
0
      {
7516
0
        exec = PF_X;
7517
0
        emptyobj = inputobj;
7518
0
      }
7519
0
  }
7520
7521
0
      if (notesec || info->stacksize > 0)
7522
0
  {
7523
0
    if (exec)
7524
0
      {
7525
0
        if (info->warn_execstack != 0)
7526
0
    {
7527
      /* PR 29072: Because an executable stack is a serious
7528
         security risk, make sure that the user knows that it is
7529
         being enabled despite the fact that it was not requested
7530
         on the command line.  */
7531
0
      if (noteobj)
7532
0
        {
7533
0
          if (info->error_execstack)
7534
0
      {
7535
0
        _bfd_error_handler (_("\
7536
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7537
0
                bfd_get_filename (noteobj));
7538
0
        return false;
7539
0
      }
7540
7541
0
          _bfd_error_handler (_("\
7542
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7543
0
           bfd_get_filename (noteobj));
7544
0
        }
7545
0
      else if (emptyobj)
7546
0
        {
7547
0
          if (info->error_execstack)
7548
0
      {
7549
0
        _bfd_error_handler (_("\
7550
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7551
0
                bfd_get_filename (emptyobj));
7552
0
        return false;
7553
0
      }
7554
7555
0
          _bfd_error_handler (_("\
7556
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7557
0
            bfd_get_filename (emptyobj));
7558
0
          _bfd_error_handler (_("\
7559
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7560
0
        }
7561
0
    }
7562
0
      }
7563
0
    elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7564
0
  }
7565
7566
0
      if (notesec && exec && bfd_link_relocatable (info)
7567
0
    && notesec->output_section != bfd_abs_section_ptr)
7568
0
  notesec->output_section->flags |= SEC_CODE;
7569
0
    }
7570
7571
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7572
0
    {
7573
0
      struct elf_info_failed eif;
7574
0
      struct elf_link_hash_entry *h;
7575
0
      asection *dynstr;
7576
0
      asection *s;
7577
7578
0
      *sinterpptr = elf_hash_table (info)->interp;
7579
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7580
7581
0
      if (info->symbolic)
7582
0
  {
7583
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7584
0
      return false;
7585
0
    info->flags |= DF_SYMBOLIC;
7586
0
  }
7587
7588
0
      if (rpath != NULL)
7589
0
  {
7590
0
    size_t indx;
7591
0
    bfd_vma tag;
7592
7593
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7594
0
              true);
7595
0
    if (indx == (size_t) -1)
7596
0
      return false;
7597
7598
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7599
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7600
0
      return false;
7601
0
  }
7602
7603
0
      if (filter_shlib != NULL)
7604
0
  {
7605
0
    size_t indx;
7606
7607
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7608
0
              filter_shlib, true);
7609
0
    if (indx == (size_t) -1
7610
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7611
0
      return false;
7612
0
  }
7613
7614
0
      if (auxiliary_filters != NULL)
7615
0
  {
7616
0
    const char * const *p;
7617
7618
0
    for (p = auxiliary_filters; *p != NULL; p++)
7619
0
      {
7620
0
        size_t indx;
7621
7622
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7623
0
            *p, true);
7624
0
        if (indx == (size_t) -1
7625
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7626
0
    return false;
7627
0
      }
7628
0
  }
7629
7630
0
      if (audit != NULL)
7631
0
  {
7632
0
    size_t indx;
7633
7634
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7635
0
              true);
7636
0
    if (indx == (size_t) -1
7637
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7638
0
      return false;
7639
0
  }
7640
7641
0
      if (depaudit != NULL)
7642
0
  {
7643
0
    size_t indx;
7644
7645
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7646
0
              true);
7647
0
    if (indx == (size_t) -1
7648
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7649
0
      return false;
7650
0
  }
7651
7652
0
      eif.info = info;
7653
0
      eif.failed = false;
7654
7655
      /* Find all symbols which were defined in a dynamic object and make
7656
   the backend pick a reasonable value for them.  */
7657
0
      elf_link_hash_traverse (elf_hash_table (info),
7658
0
            _bfd_elf_adjust_dynamic_symbol,
7659
0
            &eif);
7660
0
      if (eif.failed)
7661
0
  return false;
7662
7663
      /* Add some entries to the .dynamic section.  We fill in some of the
7664
   values later, in bfd_elf_final_link, but we must add the entries
7665
   now so that we know the final size of the .dynamic section.  */
7666
7667
      /* If there are initialization and/or finalization functions to
7668
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7669
0
      h = (info->init_function
7670
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7671
0
           info->init_function, false,
7672
0
           false, false)
7673
0
     : NULL);
7674
0
      if (h != NULL
7675
0
    && (h->ref_regular
7676
0
        || h->def_regular))
7677
0
  {
7678
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7679
0
      return false;
7680
0
  }
7681
0
      h = (info->fini_function
7682
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7683
0
           info->fini_function, false,
7684
0
           false, false)
7685
0
     : NULL);
7686
0
      if (h != NULL
7687
0
    && (h->ref_regular
7688
0
        || h->def_regular))
7689
0
  {
7690
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7691
0
      return false;
7692
0
  }
7693
7694
0
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7695
0
      if (s != NULL && s->linker_has_input)
7696
0
  {
7697
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7698
0
    if (! bfd_link_executable (info))
7699
0
      {
7700
0
        bfd *sub;
7701
0
        asection *o;
7702
7703
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7704
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7705
0
        && (o = sub->sections) != NULL
7706
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7707
0
      for (o = sub->sections; o != NULL; o = o->next)
7708
0
        if (elf_section_data (o)->this_hdr.sh_type
7709
0
      == SHT_PREINIT_ARRAY)
7710
0
          {
7711
0
      _bfd_error_handler
7712
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7713
0
         sub);
7714
0
      break;
7715
0
          }
7716
7717
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7718
0
        return false;
7719
0
      }
7720
7721
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7722
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7723
0
      return false;
7724
0
  }
7725
0
      s = bfd_get_section_by_name (output_bfd, ".init_array");
7726
0
      if (s != NULL && s->linker_has_input)
7727
0
  {
7728
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7729
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7730
0
      return false;
7731
0
  }
7732
0
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
7733
0
      if (s != NULL && s->linker_has_input)
7734
0
  {
7735
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7736
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7737
0
      return false;
7738
0
  }
7739
7740
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7741
      /* If .dynstr is excluded from the link, we don't want any of
7742
   these tags.  Strictly, we should be checking each section
7743
   individually;  This quick check covers for the case where
7744
   someone does a /DISCARD/ : { *(*) }.  */
7745
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7746
0
  {
7747
0
    bfd_size_type strsize;
7748
7749
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7750
0
    if ((info->emit_hash
7751
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7752
0
        || (info->emit_gnu_hash
7753
0
      && (bed->record_xhash_symbol == NULL
7754
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7755
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7756
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7757
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7758
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7759
0
                bed->s->sizeof_sym)
7760
0
        || (info->gnu_flags_1
7761
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7762
0
              info->gnu_flags_1)))
7763
0
      return false;
7764
0
  }
7765
0
    }
7766
7767
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7768
0
    return false;
7769
7770
  /* The backend must work out the sizes of all the other dynamic
7771
     sections.  */
7772
0
  if (bed->elf_backend_late_size_sections != NULL
7773
0
      && !bed->elf_backend_late_size_sections (output_bfd, info))
7774
0
    return false;
7775
7776
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7777
0
    {
7778
0
      if (elf_tdata (output_bfd)->cverdefs)
7779
0
  {
7780
0
    unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7781
7782
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7783
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7784
0
      return false;
7785
0
  }
7786
7787
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7788
0
  {
7789
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7790
0
      return false;
7791
0
  }
7792
0
      else if (info->flags & DF_BIND_NOW)
7793
0
  {
7794
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7795
0
      return false;
7796
0
  }
7797
7798
0
      if (info->flags_1)
7799
0
  {
7800
0
    if (bfd_link_executable (info))
7801
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7802
0
        | DF_1_NODELETE
7803
0
        | DF_1_NOOPEN);
7804
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7805
0
      return false;
7806
0
  }
7807
7808
0
      if (elf_tdata (output_bfd)->cverrefs)
7809
0
  {
7810
0
    unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7811
7812
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7813
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7814
0
      return false;
7815
0
  }
7816
7817
0
      if ((elf_tdata (output_bfd)->cverrefs == 0
7818
0
     && elf_tdata (output_bfd)->cverdefs == 0)
7819
0
    || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7820
0
  {
7821
0
    asection *s;
7822
7823
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7824
0
    s->flags |= SEC_EXCLUDE;
7825
0
  }
7826
0
    }
7827
0
  return true;
7828
0
}
7829
7830
/* Find the first non-excluded output section.  We'll use its
7831
   section symbol for some emitted relocs.  */
7832
void
7833
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7834
0
{
7835
0
  asection *s;
7836
0
  asection *found = NULL;
7837
7838
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7839
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7840
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7841
0
      {
7842
0
  found = s;
7843
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7844
0
    break;
7845
0
      }
7846
0
  elf_hash_table (info)->text_index_section = found;
7847
0
}
7848
7849
/* Find two non-excluded output sections, one for code, one for data.
7850
   We'll use their section symbols for some emitted relocs.  */
7851
void
7852
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7853
0
{
7854
0
  asection *s;
7855
0
  asection *found = NULL;
7856
7857
  /* Data first, since setting text_index_section changes
7858
     _bfd_elf_omit_section_dynsym_default.  */
7859
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7860
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7861
0
  && !(s->flags & SEC_READONLY)
7862
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7863
0
      {
7864
0
  found = s;
7865
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7866
0
    break;
7867
0
      }
7868
0
  elf_hash_table (info)->data_index_section = found;
7869
7870
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7871
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7872
0
  && (s->flags & SEC_READONLY)
7873
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7874
0
      {
7875
0
  found = s;
7876
0
  break;
7877
0
      }
7878
0
  elf_hash_table (info)->text_index_section = found;
7879
0
}
7880
7881
#define GNU_HASH_SECTION_NAME(bed)          \
7882
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7883
7884
bool
7885
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7886
0
{
7887
0
  elf_backend_data *bed;
7888
0
  unsigned long section_sym_count;
7889
0
  bfd_size_type dynsymcount = 0;
7890
7891
0
  if (!is_elf_hash_table (info->hash))
7892
0
    return true;
7893
7894
0
  bed = get_elf_backend_data (output_bfd);
7895
0
  (*bed->elf_backend_init_index_section) (output_bfd, info);
7896
7897
  /* Assign dynsym indices.  In a shared library we generate a section
7898
     symbol for each output section, which come first.  Next come all
7899
     of the back-end allocated local dynamic syms, followed by the rest
7900
     of the global symbols.
7901
7902
     This is usually not needed for static binaries, however backends
7903
     can request to always do it, e.g. the MIPS backend uses dynamic
7904
     symbol counts to lay out GOT, which will be produced in the
7905
     presence of GOT relocations even in static binaries (holding fixed
7906
     data in that case, to satisfy those relocations).  */
7907
7908
0
  if (elf_hash_table (info)->dynamic_sections_created
7909
0
      || bed->always_renumber_dynsyms)
7910
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7911
0
              &section_sym_count);
7912
7913
0
  if (elf_hash_table (info)->dynamic_sections_created)
7914
0
    {
7915
0
      bfd *dynobj;
7916
0
      asection *s;
7917
0
      unsigned int dtagcount;
7918
7919
0
      dynobj = elf_hash_table (info)->dynobj;
7920
7921
      /* Work out the size of the symbol version section.  */
7922
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7923
0
      BFD_ASSERT (s != NULL);
7924
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7925
0
  {
7926
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7927
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7928
0
    if (s->contents == NULL)
7929
0
      return false;
7930
0
    s->alloced = 1;
7931
7932
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7933
0
      return false;
7934
0
  }
7935
7936
      /* Set the size of the .dynsym and .hash sections.  We counted
7937
   the number of dynamic symbols in elf_link_add_object_symbols.
7938
   We will build the contents of .dynsym and .hash when we build
7939
   the final symbol table, because until then we do not know the
7940
   correct value to give the symbols.  We built the .dynstr
7941
   section as we went along in elf_link_add_object_symbols.  */
7942
0
      s = elf_hash_table (info)->dynsym;
7943
0
      BFD_ASSERT (s != NULL);
7944
0
      s->size = dynsymcount * bed->s->sizeof_sym;
7945
7946
0
      s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7947
0
      if (s->contents == NULL)
7948
0
  return false;
7949
0
      s->alloced = 1;
7950
7951
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7952
   section syms, in case we don't output them all.  */
7953
0
      ++section_sym_count;
7954
0
      memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7955
7956
0
      elf_hash_table (info)->bucketcount = 0;
7957
7958
      /* Compute the size of the hashing table.  As a side effect this
7959
   computes the hash values for all the names we export.  */
7960
0
      if (info->emit_hash)
7961
0
  {
7962
0
    unsigned long int *hashcodes;
7963
0
    struct hash_codes_info hashinf;
7964
0
    bfd_size_type amt;
7965
0
    unsigned long int nsyms;
7966
0
    size_t bucketcount;
7967
0
    size_t hash_entry_size;
7968
7969
    /* Compute the hash values for all exported symbols.  At the same
7970
       time store the values in an array so that we could use them for
7971
       optimizations.  */
7972
0
    amt = dynsymcount * sizeof (unsigned long int);
7973
0
    hashcodes = (unsigned long int *) bfd_malloc (amt);
7974
0
    if (hashcodes == NULL)
7975
0
      return false;
7976
0
    hashinf.hashcodes = hashcodes;
7977
0
    hashinf.error = false;
7978
7979
    /* Put all hash values in HASHCODES.  */
7980
0
    elf_link_hash_traverse (elf_hash_table (info),
7981
0
          elf_collect_hash_codes, &hashinf);
7982
0
    if (hashinf.error)
7983
0
      {
7984
0
        free (hashcodes);
7985
0
        return false;
7986
0
      }
7987
7988
0
    nsyms = hashinf.hashcodes - hashcodes;
7989
0
    bucketcount
7990
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7991
0
    free (hashcodes);
7992
7993
0
    if (bucketcount == 0 && nsyms > 0)
7994
0
      return false;
7995
7996
0
    elf_hash_table (info)->bucketcount = bucketcount;
7997
7998
0
    s = bfd_get_linker_section (dynobj, ".hash");
7999
0
    BFD_ASSERT (s != NULL);
8000
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
8001
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
8002
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8003
0
    if (s->contents == NULL)
8004
0
      return false;
8005
0
    s->alloced = 1;
8006
8007
0
    bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
8008
0
    bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
8009
0
       s->contents + hash_entry_size);
8010
0
  }
8011
8012
0
      if (info->emit_gnu_hash)
8013
0
  {
8014
0
    size_t i, cnt;
8015
0
    unsigned char *contents;
8016
0
    struct collect_gnu_hash_codes cinfo;
8017
0
    bfd_size_type amt;
8018
0
    size_t bucketcount;
8019
8020
0
    memset (&cinfo, 0, sizeof (cinfo));
8021
8022
    /* Compute the hash values for all exported symbols.  At the same
8023
       time store the values in an array so that we could use them for
8024
       optimizations.  */
8025
0
    amt = dynsymcount * 2 * sizeof (unsigned long int);
8026
0
    cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
8027
0
    if (cinfo.hashcodes == NULL)
8028
0
      return false;
8029
8030
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8031
0
    cinfo.min_dynindx = -1;
8032
0
    cinfo.output_bfd = output_bfd;
8033
0
    cinfo.bed = bed;
8034
8035
    /* Put all hash values in HASHCODES.  */
8036
0
    elf_link_hash_traverse (elf_hash_table (info),
8037
0
          elf_collect_gnu_hash_codes, &cinfo);
8038
0
    if (cinfo.error)
8039
0
      {
8040
0
        free (cinfo.hashcodes);
8041
0
        return false;
8042
0
      }
8043
8044
0
    bucketcount
8045
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8046
8047
0
    if (bucketcount == 0)
8048
0
      {
8049
0
        free (cinfo.hashcodes);
8050
0
        return false;
8051
0
      }
8052
8053
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
8054
0
    BFD_ASSERT (s != NULL);
8055
8056
0
    if (cinfo.nsyms == 0)
8057
0
      {
8058
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8059
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8060
0
        free (cinfo.hashcodes);
8061
0
        s->size = 5 * 4 + bed->s->arch_size / 8;
8062
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8063
0
        if (contents == NULL)
8064
0
    return false;
8065
0
        s->contents = contents;
8066
0
        s->alloced = 1;
8067
        /* 1 empty bucket.  */
8068
0
        bfd_put_32 (output_bfd, 1, contents);
8069
        /* SYMIDX above the special symbol 0.  */
8070
0
        bfd_put_32 (output_bfd, 1, contents + 4);
8071
        /* Just one word for bitmask.  */
8072
0
        bfd_put_32 (output_bfd, 1, contents + 8);
8073
        /* Only hash fn bloom filter.  */
8074
0
        bfd_put_32 (output_bfd, 0, contents + 12);
8075
        /* No hashes are valid - empty bitmask.  */
8076
0
        bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
8077
        /* No hashes in the only bucket.  */
8078
0
        bfd_put_32 (output_bfd, 0,
8079
0
        contents + 16 + bed->s->arch_size / 8);
8080
0
      }
8081
0
    else
8082
0
      {
8083
0
        unsigned long int maskwords, maskbitslog2, x;
8084
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8085
8086
0
        x = cinfo.nsyms;
8087
0
        maskbitslog2 = 1;
8088
0
        while ((x >>= 1) != 0)
8089
0
    ++maskbitslog2;
8090
0
        if (maskbitslog2 < 3)
8091
0
    maskbitslog2 = 5;
8092
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8093
0
    maskbitslog2 = maskbitslog2 + 3;
8094
0
        else
8095
0
    maskbitslog2 = maskbitslog2 + 2;
8096
0
        if (bed->s->arch_size == 64)
8097
0
    {
8098
0
      if (maskbitslog2 == 5)
8099
0
        maskbitslog2 = 6;
8100
0
      cinfo.shift1 = 6;
8101
0
    }
8102
0
        else
8103
0
    cinfo.shift1 = 5;
8104
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8105
0
        cinfo.shift2 = maskbitslog2;
8106
0
        cinfo.maskbits = 1 << maskbitslog2;
8107
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8108
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8109
0
        amt += maskwords * sizeof (bfd_vma);
8110
0
        cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8111
0
        if (cinfo.bitmask == NULL)
8112
0
    {
8113
0
      free (cinfo.hashcodes);
8114
0
      return false;
8115
0
    }
8116
8117
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8118
0
        cinfo.indx = cinfo.counts + bucketcount;
8119
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8120
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8121
8122
        /* Determine how often each hash bucket is used.  */
8123
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8124
0
        for (i = 0; i < cinfo.nsyms; ++i)
8125
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8126
8127
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8128
0
    if (cinfo.counts[i] != 0)
8129
0
      {
8130
0
        cinfo.indx[i] = cnt;
8131
0
        cnt += cinfo.counts[i];
8132
0
      }
8133
0
        BFD_ASSERT (cnt == dynsymcount);
8134
0
        cinfo.bucketcount = bucketcount;
8135
0
        cinfo.local_indx = cinfo.min_dynindx;
8136
8137
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8138
0
        s->size += cinfo.maskbits / 8;
8139
0
        if (bed->record_xhash_symbol != NULL)
8140
0
    s->size += cinfo.nsyms * 4;
8141
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8142
0
        if (contents == NULL)
8143
0
    {
8144
0
      free (cinfo.bitmask);
8145
0
      free (cinfo.hashcodes);
8146
0
      return false;
8147
0
    }
8148
8149
0
        s->contents = contents;
8150
0
        s->alloced = 1;
8151
0
        bfd_put_32 (output_bfd, bucketcount, contents);
8152
0
        bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8153
0
        bfd_put_32 (output_bfd, maskwords, contents + 8);
8154
0
        bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8155
0
        contents += 16 + cinfo.maskbits / 8;
8156
8157
0
        for (i = 0; i < bucketcount; ++i)
8158
0
    {
8159
0
      if (cinfo.counts[i] == 0)
8160
0
        bfd_put_32 (output_bfd, 0, contents);
8161
0
      else
8162
0
        bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8163
0
      contents += 4;
8164
0
    }
8165
8166
0
        cinfo.contents = contents;
8167
8168
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8169
8170
0
        if (elf_hash_table (info)->has_base_symbols)
8171
0
    {
8172
      /* Output base symbols first in DT_GNU_HASH so that
8173
         they will be picked before non-base symbols at
8174
         run-time.  */
8175
0
      cinfo.base_symbol = true;
8176
8177
      /* Renumber dynamic symbols, if populating .gnu.hash
8178
         section.  If using .MIPS.xhash, populate the
8179
         translation table.  */
8180
0
      elf_link_hash_traverse (elf_hash_table (info),
8181
0
            elf_gnu_hash_process_symidx, &cinfo);
8182
0
    }
8183
8184
        /* Output non-base symbols last.  */
8185
0
        cinfo.base_symbol = false;
8186
0
        elf_link_hash_traverse (elf_hash_table (info),
8187
0
              elf_gnu_hash_process_symidx, &cinfo);
8188
8189
0
        contents = s->contents + 16;
8190
0
        for (i = 0; i < maskwords; ++i)
8191
0
    {
8192
0
      bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8193
0
         contents);
8194
0
      contents += bed->s->arch_size / 8;
8195
0
    }
8196
8197
0
        free (cinfo.bitmask);
8198
0
        free (cinfo.hashcodes);
8199
0
      }
8200
0
  }
8201
8202
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8203
0
      BFD_ASSERT (s != NULL);
8204
8205
0
      elf_finalize_dynstr (output_bfd, info);
8206
8207
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8208
8209
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8210
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8211
0
    return false;
8212
0
    }
8213
8214
0
  return true;
8215
0
}
8216

8217
/* Create an entry in an ELF linker hash table.  */
8218
8219
struct bfd_hash_entry *
8220
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8221
          struct bfd_hash_table *table,
8222
          const char *string)
8223
0
{
8224
  /* Allocate the structure if it has not already been allocated by a
8225
     subclass.  */
8226
0
  if (entry == NULL)
8227
0
    {
8228
0
      entry = (struct bfd_hash_entry *)
8229
0
  bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8230
0
      if (entry == NULL)
8231
0
  return entry;
8232
0
    }
8233
8234
  /* Call the allocation method of the superclass.  */
8235
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8236
0
  if (entry != NULL)
8237
0
    {
8238
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8239
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8240
8241
      /* Set local fields.  */
8242
0
      ret->indx = -1;
8243
0
      ret->dynindx = -1;
8244
0
      ret->got = htab->init_got_refcount;
8245
0
      ret->plt = htab->init_plt_refcount;
8246
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8247
0
            - offsetof (struct elf_link_hash_entry, size)));
8248
      /* Assume that we have been called by a non-ELF symbol reader.
8249
   This flag is then reset by the code which reads an ELF input
8250
   file.  This ensures that a symbol created by a non-ELF symbol
8251
   reader will have the flag set correctly.  */
8252
0
      ret->non_elf = 1;
8253
0
    }
8254
8255
0
  return entry;
8256
0
}
8257
8258
/* Copy data from an indirect symbol to its direct symbol, hiding the
8259
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8260
8261
void
8262
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8263
          struct elf_link_hash_entry *dir,
8264
          struct elf_link_hash_entry *ind)
8265
0
{
8266
0
  struct elf_link_hash_table *htab;
8267
8268
0
  if (ind->dyn_relocs != NULL)
8269
0
    {
8270
0
      if (dir->dyn_relocs != NULL)
8271
0
  {
8272
0
    struct elf_dyn_relocs **pp;
8273
0
    struct elf_dyn_relocs *p;
8274
8275
    /* Add reloc counts against the indirect sym to the direct sym
8276
       list.  Merge any entries against the same section.  */
8277
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8278
0
      {
8279
0
        struct elf_dyn_relocs *q;
8280
8281
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8282
0
    if (q->sec == p->sec)
8283
0
      {
8284
0
        q->pc_count += p->pc_count;
8285
0
        q->count += p->count;
8286
0
        *pp = p->next;
8287
0
        break;
8288
0
      }
8289
0
        if (q == NULL)
8290
0
    pp = &p->next;
8291
0
      }
8292
0
    *pp = dir->dyn_relocs;
8293
0
  }
8294
8295
0
      dir->dyn_relocs = ind->dyn_relocs;
8296
0
      ind->dyn_relocs = NULL;
8297
0
    }
8298
8299
  /* Copy down any references that we may have already seen to the
8300
     symbol which just became indirect.  */
8301
8302
0
  if (dir->versioned != versioned_hidden)
8303
0
    dir->ref_dynamic |= ind->ref_dynamic;
8304
0
  dir->ref_regular |= ind->ref_regular;
8305
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8306
0
  dir->non_got_ref |= ind->non_got_ref;
8307
0
  dir->needs_plt |= ind->needs_plt;
8308
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8309
8310
0
  if (ind->root.type != bfd_link_hash_indirect)
8311
0
    return;
8312
8313
  /* Copy over the global and procedure linkage table refcount entries.
8314
     These may have been already set up by a check_relocs routine.  */
8315
0
  htab = elf_hash_table (info);
8316
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8317
0
    {
8318
0
      if (dir->got.refcount < 0)
8319
0
  dir->got.refcount = 0;
8320
0
      dir->got.refcount += ind->got.refcount;
8321
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8322
0
    }
8323
8324
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8325
0
    {
8326
0
      if (dir->plt.refcount < 0)
8327
0
  dir->plt.refcount = 0;
8328
0
      dir->plt.refcount += ind->plt.refcount;
8329
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8330
0
    }
8331
8332
0
  if (ind->dynindx != -1)
8333
0
    {
8334
0
      if (dir->dynindx != -1)
8335
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8336
0
      dir->dynindx = ind->dynindx;
8337
0
      dir->dynstr_index = ind->dynstr_index;
8338
0
      ind->dynindx = -1;
8339
0
      ind->dynstr_index = 0;
8340
0
    }
8341
0
}
8342
8343
void
8344
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8345
        struct elf_link_hash_entry *h,
8346
        bool force_local)
8347
0
{
8348
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8349
0
  if (h->type != STT_GNU_IFUNC)
8350
0
    {
8351
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8352
0
      h->needs_plt = 0;
8353
0
    }
8354
0
  if (force_local)
8355
0
    {
8356
0
      h->forced_local = 1;
8357
0
      if (h->dynindx != -1)
8358
0
  {
8359
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8360
0
          h->dynstr_index);
8361
0
    h->dynindx = -1;
8362
0
    h->dynstr_index = 0;
8363
0
  }
8364
0
    }
8365
0
}
8366
8367
/* Hide a symbol. */
8368
8369
void
8370
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8371
         struct bfd_link_info *info,
8372
         struct bfd_link_hash_entry *h)
8373
0
{
8374
0
  if (is_elf_hash_table (info->hash))
8375
0
    {
8376
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
8377
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8378
0
      eh->def_dynamic = 0;
8379
0
      eh->ref_dynamic = 0;
8380
0
      eh->dynamic_def = 0;
8381
0
      bed->elf_backend_hide_symbol (info, eh, true);
8382
0
    }
8383
0
}
8384
8385
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8386
   caller.  */
8387
8388
bool
8389
_bfd_elf_link_hash_table_init
8390
  (struct elf_link_hash_table *table,
8391
   bfd *abfd,
8392
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8393
              struct bfd_hash_table *,
8394
              const char *),
8395
   unsigned int entsize)
8396
0
{
8397
0
  bool ret;
8398
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
8399
0
  int can_refcount = bed->can_refcount;
8400
8401
0
  table->init_got_refcount.refcount = can_refcount - 1;
8402
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8403
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8404
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8405
  /* The first dynamic symbol is a dummy.  */
8406
0
  table->dynsymcount = 1;
8407
8408
0
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8409
8410
0
  table->root.type = bfd_link_elf_hash_table;
8411
0
  table->hash_table_id = bed->target_id;
8412
0
  table->target_os = bed->target_os;
8413
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8414
8415
0
  return ret;
8416
0
}
8417
8418
/* Create an ELF linker hash table.  */
8419
8420
struct bfd_link_hash_table *
8421
_bfd_elf_link_hash_table_create (bfd *abfd)
8422
0
{
8423
0
  struct elf_link_hash_table *ret;
8424
0
  size_t amt = sizeof (struct elf_link_hash_table);
8425
8426
0
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8427
0
  if (ret == NULL)
8428
0
    return NULL;
8429
8430
0
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8431
0
               sizeof (struct elf_link_hash_entry)))
8432
0
    {
8433
0
      free (ret);
8434
0
      return NULL;
8435
0
    }
8436
8437
0
  return &ret->root;
8438
0
}
8439
8440
/* Destroy an ELF linker hash table.  */
8441
8442
void
8443
_bfd_elf_link_hash_table_free (bfd *obfd)
8444
0
{
8445
0
  struct elf_link_hash_table *htab;
8446
8447
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8448
0
  if (htab->dynstr != NULL)
8449
0
    _bfd_elf_strtab_free (htab->dynstr);
8450
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8451
0
  if (htab->dynamic != NULL)
8452
0
    {
8453
0
      free (htab->dynamic->contents);
8454
0
      htab->dynamic->contents = NULL;
8455
0
    }
8456
0
  if (htab->first_hash != NULL)
8457
0
    {
8458
0
      bfd_hash_table_free (htab->first_hash);
8459
0
      free (htab->first_hash);
8460
0
    }
8461
0
  if (htab->eh_info.frame_hdr_is_compact)
8462
0
    free (htab->eh_info.u.compact.entries);
8463
0
  else
8464
0
    free (htab->eh_info.u.dwarf.array);
8465
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8466
0
  _bfd_generic_link_hash_table_free (obfd);
8467
0
}
8468
8469
/* This is a hook for the ELF emulation code in the generic linker to
8470
   tell the backend linker what file name to use for the DT_NEEDED
8471
   entry for a dynamic object.  */
8472
8473
void
8474
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8475
0
{
8476
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8477
0
      && bfd_get_format (abfd) == bfd_object)
8478
0
    elf_dt_name (abfd) = name;
8479
0
}
8480
8481
int
8482
bfd_elf_get_dyn_lib_class (bfd *abfd)
8483
0
{
8484
0
  int lib_class;
8485
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8486
0
      && bfd_get_format (abfd) == bfd_object)
8487
0
    lib_class = elf_dyn_lib_class (abfd);
8488
0
  else
8489
0
    lib_class = 0;
8490
0
  return lib_class;
8491
0
}
8492
8493
void
8494
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8495
0
{
8496
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8497
0
      && bfd_get_format (abfd) == bfd_object)
8498
0
    elf_dyn_lib_class (abfd) = lib_class;
8499
0
}
8500
8501
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8502
   the linker ELF emulation code.  */
8503
8504
struct bfd_link_needed_list *
8505
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8506
       struct bfd_link_info *info)
8507
0
{
8508
0
  if (! is_elf_hash_table (info->hash))
8509
0
    return NULL;
8510
0
  return elf_hash_table (info)->needed;
8511
0
}
8512
8513
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8514
   hook for the linker ELF emulation code.  */
8515
8516
struct bfd_link_needed_list *
8517
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8518
        struct bfd_link_info *info)
8519
0
{
8520
0
  if (! is_elf_hash_table (info->hash))
8521
0
    return NULL;
8522
0
  return elf_hash_table (info)->runpath;
8523
0
}
8524
8525
/* Get the name actually used for a dynamic object for a link.  This
8526
   is the SONAME entry if there is one.  Otherwise, it is the string
8527
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8528
8529
const char *
8530
bfd_elf_get_dt_soname (bfd *abfd)
8531
0
{
8532
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8533
0
      && bfd_get_format (abfd) == bfd_object)
8534
0
    return elf_dt_name (abfd);
8535
0
  return NULL;
8536
0
}
8537
8538
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8539
   the ELF linker emulation code.  */
8540
8541
bool
8542
bfd_elf_get_bfd_needed_list (bfd *abfd,
8543
           struct bfd_link_needed_list **pneeded)
8544
0
{
8545
0
  asection *s;
8546
0
  bfd_byte *dynbuf = NULL;
8547
0
  unsigned int elfsec;
8548
0
  unsigned long shlink;
8549
0
  bfd_byte *extdyn, *extdynend;
8550
0
  size_t extdynsize;
8551
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8552
8553
0
  *pneeded = NULL;
8554
8555
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8556
0
      || bfd_get_format (abfd) != bfd_object)
8557
0
    return true;
8558
8559
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8560
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8561
0
    return true;
8562
8563
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8564
0
    goto error_return;
8565
8566
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8567
0
  if (elfsec == SHN_BAD)
8568
0
    goto error_return;
8569
8570
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8571
8572
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8573
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8574
8575
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8576
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8577
0
       extdyn += extdynsize)
8578
0
    {
8579
0
      Elf_Internal_Dyn dyn;
8580
8581
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8582
8583
0
      if (dyn.d_tag == DT_NULL)
8584
0
  break;
8585
8586
0
      if (dyn.d_tag == DT_NEEDED)
8587
0
  {
8588
0
    const char *string;
8589
0
    struct bfd_link_needed_list *l;
8590
0
    unsigned int tagv = dyn.d_un.d_val;
8591
0
    size_t amt;
8592
8593
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8594
0
    if (string == NULL)
8595
0
      goto error_return;
8596
8597
0
    amt = sizeof *l;
8598
0
    l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8599
0
    if (l == NULL)
8600
0
      goto error_return;
8601
8602
0
    l->by = abfd;
8603
0
    l->name = string;
8604
0
    l->next = *pneeded;
8605
0
    *pneeded = l;
8606
0
  }
8607
0
    }
8608
8609
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8610
8611
0
  return true;
8612
8613
0
 error_return:
8614
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8615
0
  return false;
8616
0
}
8617
8618
struct elf_symbuf_symbol
8619
{
8620
  unsigned long st_name;  /* Symbol name, index in string tbl */
8621
  unsigned char st_info;  /* Type and binding attributes */
8622
  unsigned char st_other; /* Visibilty, and target specific */
8623
};
8624
8625
struct elf_symbuf_head
8626
{
8627
  struct elf_symbuf_symbol *ssym;
8628
  size_t count;
8629
  unsigned int st_shndx;
8630
};
8631
8632
struct elf_symbol
8633
{
8634
  union
8635
    {
8636
      Elf_Internal_Sym *isym;
8637
      struct elf_symbuf_symbol *ssym;
8638
      void *p;
8639
    } u;
8640
  const char *name;
8641
};
8642
8643
/* Sort references to symbols by ascending section number.  */
8644
8645
static int
8646
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8647
0
{
8648
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8649
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8650
8651
0
  if (s1->st_shndx != s2->st_shndx)
8652
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8653
  /* Final sort by the address of the sym in the symbuf ensures
8654
     a stable sort.  */
8655
0
  if (s1 != s2)
8656
0
    return s1 > s2 ? 1 : -1;
8657
0
  return 0;
8658
0
}
8659
8660
static int
8661
elf_sym_name_compare (const void *arg1, const void *arg2)
8662
0
{
8663
0
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8664
0
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8665
0
  int ret = strcmp (s1->name, s2->name);
8666
0
  if (ret != 0)
8667
0
    return ret;
8668
0
  if (s1->u.p != s2->u.p)
8669
0
    return s1->u.p > s2->u.p ? 1 : -1;
8670
0
  return 0;
8671
0
}
8672
8673
static struct elf_symbuf_head *
8674
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8675
0
{
8676
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8677
0
  struct elf_symbuf_symbol *ssym;
8678
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8679
0
  size_t i, shndx_count, total_size, amt;
8680
8681
0
  amt = symcount * sizeof (*indbuf);
8682
0
  indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8683
0
  if (indbuf == NULL)
8684
0
    return NULL;
8685
8686
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8687
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8688
0
      *ind++ = &isymbuf[i];
8689
0
  indbufend = ind;
8690
8691
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8692
0
   elf_sort_elf_symbol);
8693
8694
0
  shndx_count = 0;
8695
0
  if (indbufend > indbuf)
8696
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8697
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8698
0
  shndx_count++;
8699
8700
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8701
0
    + (indbufend - indbuf) * sizeof (*ssym));
8702
0
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8703
0
  if (ssymbuf == NULL)
8704
0
    {
8705
0
      free (indbuf);
8706
0
      return NULL;
8707
0
    }
8708
8709
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8710
0
  ssymbuf->ssym = NULL;
8711
0
  ssymbuf->count = shndx_count;
8712
0
  ssymbuf->st_shndx = 0;
8713
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8714
0
    {
8715
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8716
0
  {
8717
0
    ssymhead++;
8718
0
    ssymhead->ssym = ssym;
8719
0
    ssymhead->count = 0;
8720
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8721
0
  }
8722
0
      ssym->st_name = (*ind)->st_name;
8723
0
      ssym->st_info = (*ind)->st_info;
8724
0
      ssym->st_other = (*ind)->st_other;
8725
0
      ssymhead->count++;
8726
0
    }
8727
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8728
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8729
8730
0
  free (indbuf);
8731
0
  return ssymbuf;
8732
0
}
8733
8734
/* Check if 2 sections define the same set of local and global
8735
   symbols.  */
8736
8737
static bool
8738
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8739
           struct bfd_link_info *info)
8740
0
{
8741
0
  bfd *bfd1, *bfd2;
8742
0
  elf_backend_data *bed1, *bed2;
8743
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8744
0
  size_t symcount1, symcount2;
8745
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8746
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8747
0
  Elf_Internal_Sym *isym, *isymend;
8748
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8749
0
  size_t count1, count2, sec_count1, sec_count2, i;
8750
0
  unsigned int shndx1, shndx2;
8751
0
  bool result;
8752
0
  bool ignore_section_symbol_p;
8753
8754
0
  bfd1 = sec1->owner;
8755
0
  bfd2 = sec2->owner;
8756
8757
  /* Both sections have to be in ELF.  */
8758
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8759
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8760
0
    return false;
8761
8762
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8763
0
    return false;
8764
8765
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8766
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8767
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8768
0
    return false;
8769
8770
0
  bed1 = get_elf_backend_data (bfd1);
8771
0
  bed2 = get_elf_backend_data (bfd2);
8772
0
  hdr1 = &elf_symtab_hdr (bfd1);
8773
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8774
0
  hdr2 = &elf_symtab_hdr (bfd2);
8775
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8776
8777
0
  if (symcount1 == 0 || symcount2 == 0)
8778
0
    return false;
8779
8780
0
  result = false;
8781
0
  isymbuf1 = NULL;
8782
0
  isymbuf2 = NULL;
8783
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8784
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8785
8786
  /* Ignore section symbols only when matching non-debugging sections
8787
     or linkonce section with comdat section.  */
8788
0
  ignore_section_symbol_p
8789
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8790
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8791
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8792
8793
0
  if (ssymbuf1 == NULL)
8794
0
    {
8795
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8796
0
               NULL, NULL, NULL);
8797
0
      if (isymbuf1 == NULL)
8798
0
  goto done;
8799
8800
0
      if (info != NULL && !info->reduce_memory_overheads)
8801
0
  {
8802
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8803
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8804
0
  }
8805
0
    }
8806
8807
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8808
0
    {
8809
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8810
0
               NULL, NULL, NULL);
8811
0
      if (isymbuf2 == NULL)
8812
0
  goto done;
8813
8814
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8815
0
  {
8816
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8817
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8818
0
  }
8819
0
    }
8820
8821
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8822
0
    {
8823
      /* Optimized faster version.  */
8824
0
      size_t lo, hi, mid;
8825
0
      struct elf_symbol *symp;
8826
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8827
8828
0
      lo = 0;
8829
0
      hi = ssymbuf1->count;
8830
0
      ssymbuf1++;
8831
0
      count1 = 0;
8832
0
      sec_count1 = 0;
8833
0
      while (lo < hi)
8834
0
  {
8835
0
    mid = (lo + hi) / 2;
8836
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8837
0
      hi = mid;
8838
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8839
0
      lo = mid + 1;
8840
0
    else
8841
0
      {
8842
0
        count1 = ssymbuf1[mid].count;
8843
0
        ssymbuf1 += mid;
8844
0
        break;
8845
0
      }
8846
0
  }
8847
0
      if (ignore_section_symbol_p)
8848
0
  {
8849
0
    for (i = 0; i < count1; i++)
8850
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8851
0
        sec_count1++;
8852
0
    count1 -= sec_count1;
8853
0
  }
8854
8855
0
      lo = 0;
8856
0
      hi = ssymbuf2->count;
8857
0
      ssymbuf2++;
8858
0
      count2 = 0;
8859
0
      sec_count2 = 0;
8860
0
      while (lo < hi)
8861
0
  {
8862
0
    mid = (lo + hi) / 2;
8863
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8864
0
      hi = mid;
8865
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8866
0
      lo = mid + 1;
8867
0
    else
8868
0
      {
8869
0
        count2 = ssymbuf2[mid].count;
8870
0
        ssymbuf2 += mid;
8871
0
        break;
8872
0
      }
8873
0
  }
8874
0
      if (ignore_section_symbol_p)
8875
0
  {
8876
0
    for (i = 0; i < count2; i++)
8877
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8878
0
        sec_count2++;
8879
0
    count2 -= sec_count2;
8880
0
  }
8881
8882
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8883
0
  goto done;
8884
8885
0
      symtable1
8886
0
  = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8887
0
      symtable2
8888
0
  = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8889
0
      if (symtable1 == NULL || symtable2 == NULL)
8890
0
  goto done;
8891
8892
0
      symp = symtable1;
8893
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8894
0
     ssym < ssymend; ssym++)
8895
0
  if (sec_count1 == 0
8896
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8897
0
    {
8898
0
      symp->u.ssym = ssym;
8899
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8900
0
                hdr1->sh_link,
8901
0
                ssym->st_name);
8902
0
      if (symp->name == NULL)
8903
0
        goto done;
8904
0
      symp++;
8905
0
    }
8906
8907
0
      symp = symtable2;
8908
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8909
0
     ssym < ssymend; ssym++)
8910
0
  if (sec_count2 == 0
8911
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8912
0
    {
8913
0
      symp->u.ssym = ssym;
8914
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8915
0
                hdr2->sh_link,
8916
0
                ssym->st_name);
8917
0
      if (symp->name == NULL)
8918
0
        goto done;
8919
0
      symp++;
8920
0
    }
8921
8922
      /* Sort symbol by name.  */
8923
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8924
0
       elf_sym_name_compare);
8925
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8926
0
       elf_sym_name_compare);
8927
8928
0
      for (i = 0; i < count1; i++)
8929
  /* Two symbols must have the same binding, type and name.  */
8930
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8931
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8932
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8933
0
    goto done;
8934
8935
0
      result = true;
8936
0
      goto done;
8937
0
    }
8938
8939
0
  symtable1 = (struct elf_symbol *)
8940
0
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8941
0
  symtable2 = (struct elf_symbol *)
8942
0
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8943
0
  if (symtable1 == NULL || symtable2 == NULL)
8944
0
    goto done;
8945
8946
  /* Count definitions in the section.  */
8947
0
  count1 = 0;
8948
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8949
0
    if (isym->st_shndx == shndx1
8950
0
  && (!ignore_section_symbol_p
8951
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8952
0
      symtable1[count1++].u.isym = isym;
8953
8954
0
  count2 = 0;
8955
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8956
0
    if (isym->st_shndx == shndx2
8957
0
  && (!ignore_section_symbol_p
8958
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8959
0
      symtable2[count2++].u.isym = isym;
8960
8961
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8962
0
    goto done;
8963
8964
0
  for (i = 0; i < count1; i++)
8965
0
    {
8966
0
      symtable1[i].name
8967
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8968
0
             symtable1[i].u.isym->st_name);
8969
0
      if (symtable1[i].name == NULL)
8970
0
  goto done;
8971
0
    }
8972
8973
0
  for (i = 0; i < count2; i++)
8974
0
    {
8975
0
      symtable2[i].name
8976
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8977
0
             symtable2[i].u.isym->st_name);
8978
0
      if (symtable2[i].name == NULL)
8979
0
  goto done;
8980
0
    }
8981
8982
  /* Sort symbol by name.  */
8983
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8984
0
   elf_sym_name_compare);
8985
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8986
0
   elf_sym_name_compare);
8987
8988
0
  for (i = 0; i < count1; i++)
8989
    /* Two symbols must have the same binding, type and name.  */
8990
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8991
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8992
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8993
0
      goto done;
8994
8995
0
  result = true;
8996
8997
0
 done:
8998
0
  free (symtable1);
8999
0
  free (symtable2);
9000
0
  free (isymbuf1);
9001
0
  free (isymbuf2);
9002
9003
0
  return result;
9004
0
}
9005
9006
/* Return TRUE if 2 section types are compatible.  */
9007
9008
bool
9009
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9010
        bfd *bbfd, const asection *bsec)
9011
0
{
9012
0
  if (asec == NULL
9013
0
      || bsec == NULL
9014
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9015
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9016
0
    return true;
9017
9018
0
  return elf_section_type (asec) == elf_section_type (bsec);
9019
0
}
9020

9021
/* Final phase of ELF linker.  */
9022
9023
/* A structure we use to avoid passing large numbers of arguments.  */
9024
9025
struct elf_final_link_info
9026
{
9027
  /* General link information.  */
9028
  struct bfd_link_info *info;
9029
  /* Output BFD.  */
9030
  bfd *output_bfd;
9031
  /* Symbol string table.  */
9032
  struct elf_strtab_hash *symstrtab;
9033
  /* .hash section.  */
9034
  asection *hash_sec;
9035
  /* symbol version section (.gnu.version).  */
9036
  asection *symver_sec;
9037
  /* Buffer large enough to hold contents of any section.  */
9038
  bfd_byte *contents;
9039
  /* Buffer large enough to hold external relocs of any section.  */
9040
  void *external_relocs;
9041
  /* Buffer large enough to hold internal relocs of any section.  */
9042
  Elf_Internal_Rela *internal_relocs;
9043
  /* Buffer large enough to hold external local symbols of any input
9044
     BFD.  */
9045
  bfd_byte *external_syms;
9046
  /* And a buffer for symbol section indices.  */
9047
  Elf_External_Sym_Shndx *locsym_shndx;
9048
  /* Buffer large enough to hold internal local symbols of any input
9049
     BFD.  */
9050
  Elf_Internal_Sym *internal_syms;
9051
  /* Array large enough to hold a symbol index for each local symbol
9052
     of any input BFD.  */
9053
  long *indices;
9054
  /* Array large enough to hold a section pointer for each local
9055
     symbol of any input BFD.  */
9056
  asection **sections;
9057
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9058
  Elf_External_Sym_Shndx *symshndxbuf;
9059
  /* Number of STT_FILE syms seen.  */
9060
  size_t filesym_count;
9061
  /* Local symbol hash table.  */
9062
  struct bfd_hash_table local_hash_table;
9063
};
9064
9065
struct local_hash_entry
9066
{
9067
  /* Base hash table entry structure.  */
9068
  struct bfd_hash_entry root;
9069
  /* Size of the local symbol name.  */
9070
  size_t size;
9071
  /* Number of the duplicated local symbol names.  */
9072
  long count;
9073
};
9074
9075
/* Create an entry in the local symbol hash table.  */
9076
9077
static struct bfd_hash_entry *
9078
local_hash_newfunc (struct bfd_hash_entry *entry,
9079
        struct bfd_hash_table *table,
9080
        const char *string)
9081
0
{
9082
9083
  /* Allocate the structure if it has not already been allocated by a
9084
     subclass.  */
9085
0
  if (entry == NULL)
9086
0
    {
9087
0
      entry = bfd_hash_allocate (table,
9088
0
         sizeof (struct local_hash_entry));
9089
0
      if (entry == NULL)
9090
0
        return entry;
9091
0
    }
9092
9093
  /* Call the allocation method of the superclass.  */
9094
0
  entry = bfd_hash_newfunc (entry, table, string);
9095
0
  if (entry != NULL)
9096
0
    {
9097
0
      ((struct local_hash_entry *) entry)->count = 0;
9098
0
      ((struct local_hash_entry *) entry)->size = 0;
9099
0
    }
9100
9101
0
  return entry;
9102
0
}
9103
9104
/* This struct is used to pass information to elf_link_output_extsym.  */
9105
9106
struct elf_outext_info
9107
{
9108
  bool failed;
9109
  bool localsyms;
9110
  bool file_sym_done;
9111
  bool base_symbol;
9112
  struct elf_final_link_info *flinfo;
9113
};
9114
9115
9116
/* Support for evaluating a complex relocation.
9117
9118
   Complex relocations are generalized, self-describing relocations.  The
9119
   implementation of them consists of two parts: complex symbols, and the
9120
   relocations themselves.
9121
9122
   The relocations use a reserved elf-wide relocation type code (R_RELC
9123
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9124
   information (start bit, end bit, word width, etc) into the addend.  This
9125
   information is extracted from CGEN-generated operand tables within gas.
9126
9127
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9128
   internal) representing prefix-notation expressions, including but not
9129
   limited to those sorts of expressions normally encoded as addends in the
9130
   addend field.  The symbol mangling format is:
9131
9132
   <node> := <literal>
9133
    |  <unary-operator> ':' <node>
9134
    |  <binary-operator> ':' <node> ':' <node>
9135
    ;
9136
9137
   <literal> := 's' <digits=N> ':' <N character symbol name>
9138
       |  'S' <digits=N> ':' <N character section name>
9139
       |  '#' <hexdigits>
9140
       ;
9141
9142
   <binary-operator> := as in C
9143
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9144
9145
static bool
9146
set_symbol_value (bfd *bfd_with_globals,
9147
      Elf_Internal_Sym *isymbuf,
9148
      size_t locsymcount,
9149
      size_t num_sym,
9150
      size_t symidx,
9151
      bfd_vma val)
9152
0
{
9153
0
  struct elf_link_hash_entry *h;
9154
0
  size_t extsymoff = locsymcount;
9155
9156
0
  if (symidx < locsymcount)
9157
0
    {
9158
0
      Elf_Internal_Sym *sym;
9159
9160
0
      sym = isymbuf + symidx;
9161
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9162
0
  {
9163
    /* It is a local symbol: move it to the
9164
       "absolute" section and give it a value.  */
9165
0
    sym->st_shndx = SHN_ABS;
9166
0
    sym->st_value = val;
9167
0
    return true;
9168
0
  }
9169
0
      if (!elf_bad_symtab (bfd_with_globals))
9170
0
  {
9171
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9172
0
            bfd_with_globals);
9173
0
    bfd_set_error (bfd_error_bad_value);
9174
0
    return false;
9175
0
  }
9176
0
      extsymoff = 0;
9177
0
    }
9178
9179
  /* It is a global symbol: set its link type
9180
     to "defined" and give it a value.  */
9181
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9182
0
            extsymoff, num_sym);
9183
0
  if (h == NULL)
9184
0
    return false;
9185
0
  h->root.type = bfd_link_hash_defined;
9186
0
  h->root.u.def.value = val;
9187
0
  h->root.u.def.section = bfd_abs_section_ptr;
9188
0
  return true;
9189
0
}
9190
9191
static bool
9192
resolve_symbol (const char *name,
9193
    bfd *input_bfd,
9194
    struct elf_final_link_info *flinfo,
9195
    bfd_vma *result,
9196
    Elf_Internal_Sym *isymbuf,
9197
    size_t locsymcount)
9198
0
{
9199
0
  Elf_Internal_Sym *sym;
9200
0
  struct bfd_link_hash_entry *global_entry;
9201
0
  const char *candidate = NULL;
9202
0
  Elf_Internal_Shdr *symtab_hdr;
9203
0
  size_t i;
9204
9205
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9206
9207
0
  for (i = 0; i < locsymcount; ++ i)
9208
0
    {
9209
0
      sym = isymbuf + i;
9210
9211
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9212
0
  continue;
9213
9214
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9215
0
               symtab_hdr->sh_link,
9216
0
               sym->st_name);
9217
#ifdef DEBUG
9218
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9219
        name, candidate, (unsigned long) sym->st_value);
9220
#endif
9221
0
      if (candidate && strcmp (candidate, name) == 0)
9222
0
  {
9223
0
    asection *sec = flinfo->sections [i];
9224
9225
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9226
0
    *result += sec->output_offset + sec->output_section->vma;
9227
#ifdef DEBUG
9228
    printf ("Found symbol with value %8.8lx\n",
9229
      (unsigned long) *result);
9230
#endif
9231
0
    return true;
9232
0
  }
9233
0
    }
9234
9235
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9236
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9237
0
               false, false, true);
9238
0
  if (!global_entry)
9239
0
    return false;
9240
9241
0
  if (global_entry->type == bfd_link_hash_defined
9242
0
      || global_entry->type == bfd_link_hash_defweak)
9243
0
    {
9244
0
      *result = (global_entry->u.def.value
9245
0
     + global_entry->u.def.section->output_section->vma
9246
0
     + global_entry->u.def.section->output_offset);
9247
#ifdef DEBUG
9248
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9249
        global_entry->root.string, (unsigned long) *result);
9250
#endif
9251
0
      return true;
9252
0
    }
9253
9254
0
  return false;
9255
0
}
9256
9257
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9258
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9259
   names like "foo.end" which is the end address of section "foo".  */
9260
9261
static bool
9262
resolve_section (const char *name,
9263
     asection *sections,
9264
     bfd_vma *result,
9265
     bfd * abfd)
9266
0
{
9267
0
  asection *curr;
9268
0
  unsigned int len;
9269
9270
0
  for (curr = sections; curr; curr = curr->next)
9271
0
    if (strcmp (curr->name, name) == 0)
9272
0
      {
9273
0
  *result = curr->vma;
9274
0
  return true;
9275
0
      }
9276
9277
  /* Hmm. still haven't found it. try pseudo-section names.  */
9278
  /* FIXME: This could be coded more efficiently...  */
9279
0
  for (curr = sections; curr; curr = curr->next)
9280
0
    {
9281
0
      len = strlen (curr->name);
9282
0
      if (len > strlen (name))
9283
0
  continue;
9284
9285
0
      if (strncmp (curr->name, name, len) == 0)
9286
0
  {
9287
0
    if (startswith (name + len, ".end"))
9288
0
      {
9289
0
        *result = (curr->vma
9290
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9291
0
        return true;
9292
0
      }
9293
9294
    /* Insert more pseudo-section names here, if you like.  */
9295
0
  }
9296
0
    }
9297
9298
0
  return false;
9299
0
}
9300
9301
static void
9302
undefined_reference (const char *reftype, const char *name)
9303
0
{
9304
  /* xgettext:c-format */
9305
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9306
0
          reftype, name);
9307
0
  bfd_set_error (bfd_error_bad_value);
9308
0
}
9309
9310
static bool
9311
eval_symbol (bfd_vma *result,
9312
       const char **symp,
9313
       bfd *input_bfd,
9314
       struct elf_final_link_info *flinfo,
9315
       bfd_vma dot,
9316
       Elf_Internal_Sym *isymbuf,
9317
       size_t locsymcount,
9318
       int signed_p)
9319
0
{
9320
0
  size_t len;
9321
0
  size_t symlen;
9322
0
  bfd_vma a;
9323
0
  bfd_vma b;
9324
0
  char symbuf[4096];
9325
0
  const char *sym = *symp;
9326
0
  const char *symend;
9327
0
  bool symbol_is_section = false;
9328
9329
0
  len = strlen (sym);
9330
0
  symend = sym + len;
9331
9332
0
  if (len < 1 || len > sizeof (symbuf))
9333
0
    {
9334
0
      bfd_set_error (bfd_error_invalid_operation);
9335
0
      return false;
9336
0
    }
9337
9338
0
  switch (* sym)
9339
0
    {
9340
0
    case '.':
9341
0
      *result = dot;
9342
0
      *symp = sym + 1;
9343
0
      return true;
9344
9345
0
    case '#':
9346
0
      ++sym;
9347
0
      *result = strtoul (sym, (char **) symp, 16);
9348
0
      return true;
9349
9350
0
    case 'S':
9351
0
      symbol_is_section = true;
9352
      /* Fall through.  */
9353
0
    case 's':
9354
0
      ++sym;
9355
0
      symlen = strtol (sym, (char **) symp, 10);
9356
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9357
9358
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9359
0
  {
9360
0
    bfd_set_error (bfd_error_invalid_operation);
9361
0
    return false;
9362
0
  }
9363
9364
0
      memcpy (symbuf, sym, symlen);
9365
0
      symbuf[symlen] = '\0';
9366
0
      *symp = sym + symlen;
9367
9368
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9369
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9370
   interpretation here; section means "try section first", not "must be a
9371
   section", and likewise with symbol.  */
9372
9373
0
      if (symbol_is_section)
9374
0
  {
9375
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9376
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9377
0
          isymbuf, locsymcount))
9378
0
      {
9379
0
        undefined_reference ("section", symbuf);
9380
0
        return false;
9381
0
      }
9382
0
  }
9383
0
      else
9384
0
  {
9385
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9386
0
             isymbuf, locsymcount)
9387
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9388
0
           result, input_bfd))
9389
0
      {
9390
0
        undefined_reference ("symbol", symbuf);
9391
0
        return false;
9392
0
      }
9393
0
  }
9394
9395
0
      return true;
9396
9397
      /* All that remains are operators.  */
9398
9399
0
#define UNARY_OP(op)            \
9400
0
  if (startswith (sym, #op))          \
9401
0
    {               \
9402
0
      sym += strlen (#op);          \
9403
0
      if (*sym == ':')           \
9404
0
  ++sym;             \
9405
0
      *symp = sym;            \
9406
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9407
0
      isymbuf, locsymcount, signed_p)) \
9408
0
  return false;           \
9409
0
      if (signed_p)           \
9410
0
  *result = op ((bfd_signed_vma) a);     \
9411
0
      else              \
9412
0
  *result = op a;           \
9413
0
      return true;            \
9414
0
    }
9415
9416
0
#define BINARY_OP_HEAD(op)          \
9417
0
  if (startswith (sym, #op))          \
9418
0
    {               \
9419
0
      sym += strlen (#op);          \
9420
0
      if (*sym == ':')           \
9421
0
  ++sym;             \
9422
0
      *symp = sym;            \
9423
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9424
0
      isymbuf, locsymcount, signed_p)) \
9425
0
  return false;           \
9426
0
      ++*symp;              \
9427
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9428
0
      isymbuf, locsymcount, signed_p)) \
9429
0
  return false;
9430
0
#define BINARY_OP_TAIL(op)          \
9431
0
      if (signed_p)           \
9432
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9433
0
      else              \
9434
0
  *result = a op b;         \
9435
0
      return true;            \
9436
0
    }
9437
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9438
9439
0
    default:
9440
0
      UNARY_OP  (0-);
9441
0
      BINARY_OP_HEAD (<<);
9442
0
      if (b >= sizeof (a) * CHAR_BIT)
9443
0
  {
9444
0
    *result = 0;
9445
0
    return true;
9446
0
  }
9447
0
      signed_p = 0;
9448
0
      BINARY_OP_TAIL (<<);
9449
0
      BINARY_OP_HEAD (>>);
9450
0
      if (b >= sizeof (a) * CHAR_BIT)
9451
0
  {
9452
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9453
0
    return true;
9454
0
  }
9455
0
      BINARY_OP_TAIL (>>);
9456
0
      BINARY_OP (==);
9457
0
      BINARY_OP (!=);
9458
0
      BINARY_OP (<=);
9459
0
      BINARY_OP (>=);
9460
0
      BINARY_OP (&&);
9461
0
      BINARY_OP (||);
9462
0
      UNARY_OP  (~);
9463
0
      UNARY_OP  (!);
9464
0
      BINARY_OP (*);
9465
0
      BINARY_OP_HEAD (/);
9466
0
      if (b == 0)
9467
0
  {
9468
0
    _bfd_error_handler (_("division by zero"));
9469
0
    bfd_set_error (bfd_error_bad_value);
9470
0
    return false;
9471
0
  }
9472
0
      BINARY_OP_TAIL (/);
9473
0
      BINARY_OP_HEAD (%);
9474
0
      if (b == 0)
9475
0
  {
9476
0
    _bfd_error_handler (_("division by zero"));
9477
0
    bfd_set_error (bfd_error_bad_value);
9478
0
    return false;
9479
0
  }
9480
0
      BINARY_OP_TAIL (%);
9481
0
      BINARY_OP (^);
9482
0
      BINARY_OP (|);
9483
0
      BINARY_OP (&);
9484
0
      BINARY_OP (+);
9485
0
      BINARY_OP (-);
9486
0
      BINARY_OP (<);
9487
0
      BINARY_OP (>);
9488
0
#undef UNARY_OP
9489
0
#undef BINARY_OP
9490
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9491
0
      bfd_set_error (bfd_error_invalid_operation);
9492
0
      return false;
9493
0
    }
9494
0
}
9495
9496
static void
9497
put_value (bfd_vma size,
9498
     unsigned long chunksz,
9499
     bfd *input_bfd,
9500
     bfd_vma x,
9501
     bfd_byte *location)
9502
0
{
9503
0
  location += (size - chunksz);
9504
9505
0
  for (; size; size -= chunksz, location -= chunksz)
9506
0
    {
9507
0
      switch (chunksz)
9508
0
  {
9509
0
  case 1:
9510
0
    bfd_put_8 (input_bfd, x, location);
9511
0
    x >>= 8;
9512
0
    break;
9513
0
  case 2:
9514
0
    bfd_put_16 (input_bfd, x, location);
9515
0
    x >>= 16;
9516
0
    break;
9517
0
  case 4:
9518
0
    bfd_put_32 (input_bfd, x, location);
9519
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9520
0
    x >>= 16;
9521
0
    x >>= 16;
9522
0
    break;
9523
0
#ifdef BFD64
9524
0
  case 8:
9525
0
    bfd_put_64 (input_bfd, x, location);
9526
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9527
0
    x >>= 32;
9528
0
    x >>= 32;
9529
0
    break;
9530
0
#endif
9531
0
  default:
9532
0
    abort ();
9533
0
    break;
9534
0
  }
9535
0
    }
9536
0
}
9537
9538
static bfd_vma
9539
get_value (bfd_vma size,
9540
     unsigned long chunksz,
9541
     bfd *input_bfd,
9542
     bfd_byte *location)
9543
0
{
9544
0
  int shift;
9545
0
  bfd_vma x = 0;
9546
9547
  /* Sanity checks.  */
9548
0
  BFD_ASSERT (chunksz <= sizeof (x)
9549
0
        && size >= chunksz
9550
0
        && chunksz != 0
9551
0
        && (size % chunksz) == 0
9552
0
        && input_bfd != NULL
9553
0
        && location != NULL);
9554
9555
0
  if (chunksz == sizeof (x))
9556
0
    {
9557
0
      BFD_ASSERT (size == chunksz);
9558
9559
      /* Make sure that we do not perform an undefined shift operation.
9560
   We know that size == chunksz so there will only be one iteration
9561
   of the loop below.  */
9562
0
      shift = 0;
9563
0
    }
9564
0
  else
9565
0
    shift = 8 * chunksz;
9566
9567
0
  for (; size; size -= chunksz, location += chunksz)
9568
0
    {
9569
0
      switch (chunksz)
9570
0
  {
9571
0
  case 1:
9572
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9573
0
    break;
9574
0
  case 2:
9575
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9576
0
    break;
9577
0
  case 4:
9578
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9579
0
    break;
9580
0
#ifdef BFD64
9581
0
  case 8:
9582
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9583
0
    break;
9584
0
#endif
9585
0
  default:
9586
0
    abort ();
9587
0
  }
9588
0
    }
9589
0
  return x;
9590
0
}
9591
9592
static void
9593
decode_complex_addend (unsigned long *start,   /* in bits */
9594
           unsigned long *oplen,   /* in bits */
9595
           unsigned long *len,     /* in bits */
9596
           unsigned long *wordsz,  /* in bytes */
9597
           unsigned long *chunksz, /* in bytes */
9598
           unsigned long *lsb0_p,
9599
           unsigned long *signed_p,
9600
           unsigned long *trunc_p,
9601
           unsigned long encoded)
9602
0
{
9603
0
  * start     =  encoded  & 0x3F;
9604
0
  * len       = (encoded >>  6) & 0x3F;
9605
0
  * oplen     = (encoded >> 12) & 0x3F;
9606
0
  * wordsz    = (encoded >> 18) & 0xF;
9607
0
  * chunksz   = (encoded >> 22) & 0xF;
9608
0
  * lsb0_p    = (encoded >> 27) & 1;
9609
0
  * signed_p  = (encoded >> 28) & 1;
9610
0
  * trunc_p   = (encoded >> 29) & 1;
9611
0
}
9612
9613
bfd_reloc_status_type
9614
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9615
            asection *input_section,
9616
            bfd_byte *contents,
9617
            Elf_Internal_Rela *rel,
9618
            bfd_vma relocation)
9619
0
{
9620
0
  bfd_vma shift, x, mask;
9621
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9622
0
  bfd_reloc_status_type r;
9623
0
  bfd_size_type octets;
9624
9625
  /*  Perform this reloc, since it is complex.
9626
      (this is not to say that it necessarily refers to a complex
9627
      symbol; merely that it is a self-describing CGEN based reloc.
9628
      i.e. the addend has the complete reloc information (bit start, end,
9629
      word size, etc) encoded within it.).  */
9630
9631
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9632
0
       &chunksz, &lsb0_p, &signed_p,
9633
0
       &trunc_p, rel->r_addend);
9634
9635
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9636
9637
0
  if (lsb0_p)
9638
0
    shift = (start + 1) - len;
9639
0
  else
9640
0
    shift = (8 * wordsz) - (start + len);
9641
9642
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9643
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9644
9645
#ifdef DEBUG
9646
  printf ("Doing complex reloc: "
9647
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9648
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9649
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9650
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9651
    oplen, (unsigned long) x, (unsigned long) mask,
9652
    (unsigned long) relocation);
9653
#endif
9654
9655
0
  r = bfd_reloc_ok;
9656
0
  if (! trunc_p)
9657
    /* Now do an overflow check.  */
9658
0
    r = bfd_check_overflow ((signed_p
9659
0
           ? complain_overflow_signed
9660
0
           : complain_overflow_unsigned),
9661
0
          len, 0, (8 * wordsz),
9662
0
          relocation);
9663
9664
  /* Do the deed.  */
9665
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9666
9667
#ifdef DEBUG
9668
  printf ("           relocation: %8.8lx\n"
9669
    "         shifted mask: %8.8lx\n"
9670
    " shifted/masked reloc: %8.8lx\n"
9671
    "               result: %8.8lx\n",
9672
    (unsigned long) relocation, (unsigned long) (mask << shift),
9673
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9674
#endif
9675
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9676
0
  return r;
9677
0
}
9678
9679
/* Functions to read r_offset from external (target order) reloc
9680
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9681
   know the value is aligned.  */
9682
9683
static bfd_vma
9684
ext32l_r_offset (const void *p)
9685
0
{
9686
0
  union aligned32
9687
0
  {
9688
0
    uint32_t v;
9689
0
    unsigned char c[4];
9690
0
  };
9691
0
  const union aligned32 *a
9692
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9693
9694
0
  uint32_t aval = (  (uint32_t) a->c[0]
9695
0
       | (uint32_t) a->c[1] << 8
9696
0
       | (uint32_t) a->c[2] << 16
9697
0
       | (uint32_t) a->c[3] << 24);
9698
0
  return aval;
9699
0
}
9700
9701
static bfd_vma
9702
ext32b_r_offset (const void *p)
9703
0
{
9704
0
  union aligned32
9705
0
  {
9706
0
    uint32_t v;
9707
0
    unsigned char c[4];
9708
0
  };
9709
0
  const union aligned32 *a
9710
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9711
9712
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9713
0
       | (uint32_t) a->c[1] << 16
9714
0
       | (uint32_t) a->c[2] << 8
9715
0
       | (uint32_t) a->c[3]);
9716
0
  return aval;
9717
0
}
9718
9719
static bfd_vma
9720
ext64l_r_offset (const void *p)
9721
0
{
9722
0
  union aligned64
9723
0
  {
9724
0
    uint64_t v;
9725
0
    unsigned char c[8];
9726
0
  };
9727
0
  const union aligned64 *a
9728
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9729
9730
0
  uint64_t aval = (  (uint64_t) a->c[0]
9731
0
       | (uint64_t) a->c[1] << 8
9732
0
       | (uint64_t) a->c[2] << 16
9733
0
       | (uint64_t) a->c[3] << 24
9734
0
       | (uint64_t) a->c[4] << 32
9735
0
       | (uint64_t) a->c[5] << 40
9736
0
       | (uint64_t) a->c[6] << 48
9737
0
       | (uint64_t) a->c[7] << 56);
9738
0
  return aval;
9739
0
}
9740
9741
static bfd_vma
9742
ext64b_r_offset (const void *p)
9743
0
{
9744
0
  union aligned64
9745
0
  {
9746
0
    uint64_t v;
9747
0
    unsigned char c[8];
9748
0
  };
9749
0
  const union aligned64 *a
9750
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9751
9752
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9753
0
       | (uint64_t) a->c[1] << 48
9754
0
       | (uint64_t) a->c[2] << 40
9755
0
       | (uint64_t) a->c[3] << 32
9756
0
       | (uint64_t) a->c[4] << 24
9757
0
       | (uint64_t) a->c[5] << 16
9758
0
       | (uint64_t) a->c[6] << 8
9759
0
       | (uint64_t) a->c[7]);
9760
0
  return aval;
9761
0
}
9762
9763
/* When performing a relocatable link, the input relocations are
9764
   preserved.  But, if they reference global symbols, the indices
9765
   referenced must be updated.  Update all the relocations found in
9766
   RELDATA.  */
9767
9768
static bool
9769
elf_link_adjust_relocs (bfd *abfd,
9770
      asection *sec,
9771
      struct bfd_elf_section_reloc_data *reldata,
9772
      bool sort,
9773
      struct bfd_link_info *info)
9774
0
{
9775
0
  unsigned int i;
9776
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9777
0
  bfd_byte *erela;
9778
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9779
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9780
0
  bfd_vma r_type_mask;
9781
0
  int r_sym_shift;
9782
0
  unsigned int count = reldata->count;
9783
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9784
9785
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9786
0
    {
9787
0
      swap_in = bed->s->swap_reloc_in;
9788
0
      swap_out = bed->s->swap_reloc_out;
9789
0
    }
9790
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9791
0
    {
9792
0
      swap_in = bed->s->swap_reloca_in;
9793
0
      swap_out = bed->s->swap_reloca_out;
9794
0
    }
9795
0
  else
9796
0
    abort ();
9797
9798
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9799
0
    abort ();
9800
9801
0
  if (bed->s->arch_size == 32)
9802
0
    {
9803
0
      r_type_mask = 0xff;
9804
0
      r_sym_shift = 8;
9805
0
    }
9806
0
  else
9807
0
    {
9808
0
      r_type_mask = 0xffffffff;
9809
0
      r_sym_shift = 32;
9810
0
    }
9811
9812
0
  erela = reldata->hdr->contents;
9813
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9814
0
    {
9815
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9816
0
      unsigned int j;
9817
9818
0
      if (*rel_hash == NULL)
9819
0
  continue;
9820
9821
0
      if ((*rel_hash)->indx == -2
9822
0
    && info->gc_sections
9823
0
    && ! info->gc_keep_exported)
9824
0
  {
9825
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9826
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9827
0
            abfd, sec,
9828
0
            (*rel_hash)->root.root.string);
9829
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9830
0
            abfd, sec);
9831
0
    bfd_set_error (bfd_error_invalid_operation);
9832
0
    return false;
9833
0
  }
9834
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9835
9836
0
      (*swap_in) (abfd, erela, irela);
9837
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9838
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9839
0
         | (irela[j].r_info & r_type_mask));
9840
0
      (*swap_out) (abfd, irela, erela);
9841
0
    }
9842
9843
0
  if (bed->elf_backend_update_relocs)
9844
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9845
9846
0
  if (sort && count != 0)
9847
0
    {
9848
0
      bfd_vma (*ext_r_off) (const void *);
9849
0
      bfd_vma r_off;
9850
0
      size_t elt_size;
9851
0
      bfd_byte *base, *end, *p, *loc;
9852
0
      bfd_byte *buf = NULL;
9853
9854
0
      if (bed->s->arch_size == 32)
9855
0
  {
9856
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9857
0
      ext_r_off = ext32l_r_offset;
9858
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9859
0
      ext_r_off = ext32b_r_offset;
9860
0
    else
9861
0
      abort ();
9862
0
  }
9863
0
      else
9864
0
  {
9865
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9866
0
      ext_r_off = ext64l_r_offset;
9867
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9868
0
      ext_r_off = ext64b_r_offset;
9869
0
    else
9870
0
      abort ();
9871
0
  }
9872
9873
      /*  Must use a stable sort here.  A modified insertion sort,
9874
    since the relocs are mostly sorted already.  */
9875
0
      elt_size = reldata->hdr->sh_entsize;
9876
0
      base = reldata->hdr->contents;
9877
0
      end = base + count * elt_size;
9878
0
      if (elt_size > sizeof (Elf64_External_Rela))
9879
0
  abort ();
9880
9881
      /* Ensure the first element is lowest.  This acts as a sentinel,
9882
   speeding the main loop below.  */
9883
0
      r_off = (*ext_r_off) (base);
9884
0
      for (p = loc = base; (p += elt_size) < end; )
9885
0
  {
9886
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9887
0
    if (r_off > r_off2)
9888
0
      {
9889
0
        r_off = r_off2;
9890
0
        loc = p;
9891
0
      }
9892
0
  }
9893
0
      if (loc != base)
9894
0
  {
9895
    /* Don't just swap *base and *loc as that changes the order
9896
       of the original base[0] and base[1] if they happen to
9897
       have the same r_offset.  */
9898
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9899
0
    memcpy (onebuf, loc, elt_size);
9900
0
    memmove (base + elt_size, base, loc - base);
9901
0
    memcpy (base, onebuf, elt_size);
9902
0
  }
9903
9904
0
      for (p = base + elt_size; (p += elt_size) < end; )
9905
0
  {
9906
    /* base to p is sorted, *p is next to insert.  */
9907
0
    r_off = (*ext_r_off) (p);
9908
    /* Search the sorted region for location to insert.  */
9909
0
    loc = p - elt_size;
9910
0
    while (r_off < (*ext_r_off) (loc))
9911
0
      loc -= elt_size;
9912
0
    loc += elt_size;
9913
0
    if (loc != p)
9914
0
      {
9915
        /* Chances are there is a run of relocs to insert here,
9916
     from one of more input files.  Files are not always
9917
     linked in order due to the way elf_link_input_bfd is
9918
     called.  See pr17666.  */
9919
0
        size_t sortlen = p - loc;
9920
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9921
0
        size_t runlen = elt_size;
9922
0
        bfd_vma r_off_runend = r_off;
9923
0
        bfd_vma r_off_runend_next;
9924
0
        size_t buf_size = 96 * 1024;
9925
0
        while (p + runlen < end
9926
0
         && (sortlen <= buf_size
9927
0
       || runlen + elt_size <= buf_size)
9928
         /* run must not break the ordering of base..loc+1 */
9929
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9930
         /* run must be already sorted */
9931
0
         && r_off_runend_next >= r_off_runend)
9932
0
    {
9933
0
      runlen += elt_size;
9934
0
      r_off_runend = r_off_runend_next;
9935
0
    }
9936
0
        if (buf == NULL)
9937
0
    {
9938
0
      buf = bfd_malloc (buf_size);
9939
0
      if (buf == NULL)
9940
0
        return false;
9941
0
    }
9942
0
        if (runlen < sortlen)
9943
0
    {
9944
0
      memcpy (buf, p, runlen);
9945
0
      memmove (loc + runlen, loc, sortlen);
9946
0
      memcpy (loc, buf, runlen);
9947
0
    }
9948
0
        else
9949
0
    {
9950
0
      memcpy (buf, loc, sortlen);
9951
0
      memmove (loc, p, runlen);
9952
0
      memcpy (loc + runlen, buf, sortlen);
9953
0
    }
9954
0
        p += runlen - elt_size;
9955
0
      }
9956
0
  }
9957
      /* Hashes are no longer valid.  */
9958
0
      free (reldata->hashes);
9959
0
      reldata->hashes = NULL;
9960
0
      free (buf);
9961
0
    }
9962
0
  return true;
9963
0
}
9964
9965
struct elf_link_sort_rela
9966
{
9967
  union {
9968
    bfd_vma offset;
9969
    bfd_vma sym_mask;
9970
  } u;
9971
  enum elf_reloc_type_class type;
9972
  /* We use this as an array of size int_rels_per_ext_rel.  */
9973
  Elf_Internal_Rela rela[1];
9974
};
9975
9976
/* qsort stability here and for cmp2 is only an issue if multiple
9977
   dynamic relocations are emitted at the same address.  But targets
9978
   that apply a series of dynamic relocations each operating on the
9979
   result of the prior relocation can't use -z combreloc as
9980
   implemented anyway.  Such schemes tend to be broken by sorting on
9981
   symbol index.  That leaves dynamic NONE relocs as the only other
9982
   case where ld might emit multiple relocs at the same address, and
9983
   those are only emitted due to target bugs.  */
9984
9985
static int
9986
elf_link_sort_cmp1 (const void *A, const void *B)
9987
0
{
9988
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9989
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9990
0
  int relativea, relativeb;
9991
9992
0
  relativea = a->type == reloc_class_relative;
9993
0
  relativeb = b->type == reloc_class_relative;
9994
9995
0
  if (relativea < relativeb)
9996
0
    return 1;
9997
0
  if (relativea > relativeb)
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_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
10002
0
    return 1;
10003
0
  if (a->rela->r_offset < b->rela->r_offset)
10004
0
    return -1;
10005
0
  if (a->rela->r_offset > b->rela->r_offset)
10006
0
    return 1;
10007
0
  return 0;
10008
0
}
10009
10010
static int
10011
elf_link_sort_cmp2 (const void *A, const void *B)
10012
0
{
10013
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
10014
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
10015
10016
0
  if (a->type < b->type)
10017
0
    return -1;
10018
0
  if (a->type > b->type)
10019
0
    return 1;
10020
0
  if (a->u.offset < b->u.offset)
10021
0
    return -1;
10022
0
  if (a->u.offset > b->u.offset)
10023
0
    return 1;
10024
0
  if (a->rela->r_offset < b->rela->r_offset)
10025
0
    return -1;
10026
0
  if (a->rela->r_offset > b->rela->r_offset)
10027
0
    return 1;
10028
0
  return 0;
10029
0
}
10030
10031
static size_t
10032
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
10033
0
{
10034
0
  asection *dynamic_relocs;
10035
0
  asection *rela_dyn;
10036
0
  asection *rel_dyn;
10037
0
  bfd_size_type count, size;
10038
0
  size_t i, ret, sort_elt, ext_size;
10039
0
  bfd_byte *sort, *s_non_relative, *p;
10040
0
  struct elf_link_sort_rela *sq;
10041
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
10042
0
  int i2e = bed->s->int_rels_per_ext_rel;
10043
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
10044
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10045
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10046
0
  struct bfd_link_order *lo;
10047
0
  bfd_vma r_sym_mask;
10048
0
  bool use_rela;
10049
10050
  /* Find a dynamic reloc section.  */
10051
0
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
10052
0
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
10053
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10054
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10055
0
    {
10056
0
      bool use_rela_initialised = false;
10057
10058
      /* This is just here to stop gcc from complaining.
10059
   Its initialization checking code is not perfect.  */
10060
0
      use_rela = true;
10061
10062
      /* Both sections are present.  Examine the sizes
10063
   of the indirect sections to help us choose.  */
10064
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10065
0
  if (lo->type == bfd_indirect_link_order)
10066
0
    {
10067
0
      asection *o = lo->u.indirect.section;
10068
10069
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10070
0
        {
10071
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10072
      /* Section size is divisible by both rel and rela sizes.
10073
         It is of no help to us.  */
10074
0
      ;
10075
0
    else
10076
0
      {
10077
        /* Section size is only divisible by rela.  */
10078
0
        if (use_rela_initialised && !use_rela)
10079
0
          {
10080
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10081
0
                "they are in more than one size"),
10082
0
              abfd);
10083
0
      bfd_set_error (bfd_error_invalid_operation);
10084
0
      return 0;
10085
0
          }
10086
0
        else
10087
0
          {
10088
0
      use_rela = true;
10089
0
      use_rela_initialised = true;
10090
0
          }
10091
0
      }
10092
0
        }
10093
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10094
0
        {
10095
    /* Section size is only divisible by rel.  */
10096
0
    if (use_rela_initialised && use_rela)
10097
0
      {
10098
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10099
0
            "they are in more than one size"),
10100
0
          abfd);
10101
0
        bfd_set_error (bfd_error_invalid_operation);
10102
0
        return 0;
10103
0
      }
10104
0
    else
10105
0
      {
10106
0
        use_rela = false;
10107
0
        use_rela_initialised = true;
10108
0
      }
10109
0
        }
10110
0
      else
10111
0
        {
10112
    /* The section size is not divisible by either -
10113
       something is wrong.  */
10114
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10115
0
              "they are of an unknown size"), abfd);
10116
0
    bfd_set_error (bfd_error_invalid_operation);
10117
0
    return 0;
10118
0
        }
10119
0
    }
10120
10121
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10122
0
  if (lo->type == bfd_indirect_link_order)
10123
0
    {
10124
0
      asection *o = lo->u.indirect.section;
10125
10126
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10127
0
        {
10128
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10129
      /* Section size is divisible by both rel and rela sizes.
10130
         It is of no help to us.  */
10131
0
      ;
10132
0
    else
10133
0
      {
10134
        /* Section size is only divisible by rela.  */
10135
0
        if (use_rela_initialised && !use_rela)
10136
0
          {
10137
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10138
0
                "they are in more than one size"),
10139
0
              abfd);
10140
0
      bfd_set_error (bfd_error_invalid_operation);
10141
0
      return 0;
10142
0
          }
10143
0
        else
10144
0
          {
10145
0
      use_rela = true;
10146
0
      use_rela_initialised = true;
10147
0
          }
10148
0
      }
10149
0
        }
10150
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10151
0
        {
10152
    /* Section size is only divisible by rel.  */
10153
0
    if (use_rela_initialised && use_rela)
10154
0
      {
10155
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10156
0
            "they are in more than one size"),
10157
0
          abfd);
10158
0
        bfd_set_error (bfd_error_invalid_operation);
10159
0
        return 0;
10160
0
      }
10161
0
    else
10162
0
      {
10163
0
        use_rela = false;
10164
0
        use_rela_initialised = true;
10165
0
      }
10166
0
        }
10167
0
      else
10168
0
        {
10169
    /* The section size is not divisible by either -
10170
       something is wrong.  */
10171
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10172
0
              "they are of an unknown size"), abfd);
10173
0
    bfd_set_error (bfd_error_invalid_operation);
10174
0
    return 0;
10175
0
        }
10176
0
    }
10177
10178
0
      if (! use_rela_initialised)
10179
  /* Make a guess.  */
10180
0
  use_rela = true;
10181
0
    }
10182
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10183
0
    use_rela = true;
10184
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10185
0
    use_rela = false;
10186
0
  else
10187
0
    return 0;
10188
10189
0
  if (use_rela)
10190
0
    {
10191
0
      dynamic_relocs = rela_dyn;
10192
0
      ext_size = bed->s->sizeof_rela;
10193
0
      swap_in = bed->s->swap_reloca_in;
10194
0
      swap_out = bed->s->swap_reloca_out;
10195
0
    }
10196
0
  else
10197
0
    {
10198
0
      dynamic_relocs = rel_dyn;
10199
0
      ext_size = bed->s->sizeof_rel;
10200
0
      swap_in = bed->s->swap_reloc_in;
10201
0
      swap_out = bed->s->swap_reloc_out;
10202
0
    }
10203
10204
0
  size = 0;
10205
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10206
0
    if (lo->type == bfd_indirect_link_order)
10207
0
      size += lo->u.indirect.section->size;
10208
10209
0
  if (size != dynamic_relocs->size)
10210
0
    return 0;
10211
10212
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10213
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10214
10215
0
  count = dynamic_relocs->size / ext_size;
10216
0
  if (count == 0)
10217
0
    return 0;
10218
0
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10219
10220
0
  if (sort == NULL)
10221
0
    {
10222
0
      (*info->callbacks->warning)
10223
0
  (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10224
0
      return 0;
10225
0
    }
10226
10227
0
  if (bed->s->arch_size == 32)
10228
0
    r_sym_mask = ~(bfd_vma) 0xff;
10229
0
  else
10230
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10231
10232
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10233
0
    if (lo->type == bfd_indirect_link_order)
10234
0
      {
10235
0
  bfd_byte *erel, *erelend;
10236
0
  asection *o = lo->u.indirect.section;
10237
10238
0
  if (o->contents == NULL && o->size != 0)
10239
0
    {
10240
      /* This is a reloc section that is being handled as a normal
10241
         section.  See bfd_section_from_shdr.  We can't combine
10242
         relocs in this case.  */
10243
0
      free (sort);
10244
0
      return 0;
10245
0
    }
10246
0
  erel = o->contents;
10247
0
  erelend = o->contents + o->size;
10248
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10249
10250
0
  while (erel < erelend)
10251
0
    {
10252
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10253
10254
0
      (*swap_in) (abfd, erel, s->rela);
10255
0
      s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10256
0
      s->u.sym_mask = r_sym_mask;
10257
0
      p += sort_elt;
10258
0
      erel += ext_size;
10259
0
    }
10260
0
      }
10261
10262
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10263
10264
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10265
0
    {
10266
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10267
0
      if (s->type != reloc_class_relative)
10268
0
  break;
10269
0
    }
10270
0
  ret = i;
10271
0
  s_non_relative = p;
10272
10273
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10274
0
  for (; i < count; i++, p += sort_elt)
10275
0
    {
10276
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10277
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10278
0
  sq = sp;
10279
0
      sp->u.offset = sq->rela->r_offset;
10280
0
    }
10281
10282
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10283
10284
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10285
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10286
0
    {
10287
      /* We have plt relocs in .rela.dyn.  */
10288
0
      sq = (struct elf_link_sort_rela *) sort;
10289
0
      for (i = 0; i < count; i++)
10290
0
  if (sq[count - i - 1].type != reloc_class_plt)
10291
0
    break;
10292
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10293
0
  {
10294
0
    struct bfd_link_order **plo;
10295
    /* Put srelplt link_order last.  This is so the output_offset
10296
       set in the next loop is correct for DT_JMPREL.  */
10297
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10298
0
      if ((*plo)->type == bfd_indirect_link_order
10299
0
    && (*plo)->u.indirect.section == htab->srelplt)
10300
0
        {
10301
0
    lo = *plo;
10302
0
    *plo = lo->next;
10303
0
        }
10304
0
      else
10305
0
        plo = &(*plo)->next;
10306
0
    *plo = lo;
10307
0
    lo->next = NULL;
10308
0
    dynamic_relocs->map_tail.link_order = lo;
10309
0
  }
10310
0
    }
10311
10312
0
  p = sort;
10313
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10314
0
    if (lo->type == bfd_indirect_link_order)
10315
0
      {
10316
0
  bfd_byte *erel, *erelend;
10317
0
  asection *o = lo->u.indirect.section;
10318
10319
0
  erel = o->contents;
10320
0
  erelend = o->contents + o->size;
10321
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10322
0
  while (erel < erelend)
10323
0
    {
10324
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10325
0
      (*swap_out) (abfd, s->rela, erel);
10326
0
      p += sort_elt;
10327
0
      erel += ext_size;
10328
0
    }
10329
0
      }
10330
10331
0
  free (sort);
10332
0
  *psec = dynamic_relocs;
10333
0
  return ret;
10334
0
}
10335
10336
/* Add a symbol to the output symbol string table.  */
10337
10338
static int
10339
elf_link_output_symstrtab (void *finf,
10340
         const char *name,
10341
         Elf_Internal_Sym *elfsym,
10342
         asection *input_sec,
10343
         struct elf_link_hash_entry *h)
10344
0
{
10345
0
  struct elf_final_link_info *flinfo = finf;
10346
0
  int (*output_symbol_hook)
10347
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10348
0
     struct elf_link_hash_entry *);
10349
0
  struct elf_link_hash_table *hash_table;
10350
0
  elf_backend_data *bed;
10351
0
  bfd_size_type strtabsize;
10352
10353
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10354
10355
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10356
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10357
0
  if (output_symbol_hook != NULL)
10358
0
    {
10359
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10360
0
      if (ret != 1)
10361
0
  return ret;
10362
0
    }
10363
10364
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10365
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10366
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10367
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10368
10369
0
  if (name == NULL || *name == '\0')
10370
0
    elfsym->st_name = (unsigned long) -1;
10371
0
  else
10372
0
    {
10373
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10374
   to get the final offset for st_name.  */
10375
0
      char *versioned_name = (char *) name;
10376
0
      if (h != NULL)
10377
0
  {
10378
0
    if (h->versioned == versioned && h->def_dynamic)
10379
0
      {
10380
        /* Keep only one '@' for versioned symbols defined in
10381
           shared objects.  */
10382
0
        const char *version = strrchr (name, ELF_VER_CHR);
10383
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10384
0
        if (version != base_end)
10385
0
    {
10386
0
      size_t base_len;
10387
0
      size_t len = strlen (name);
10388
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10389
0
      if (versioned_name == NULL)
10390
0
        return 0;
10391
0
      base_len = base_end - name;
10392
0
      memcpy (versioned_name, name, base_len);
10393
0
      memcpy (versioned_name + base_len, version,
10394
0
        len - base_len);
10395
0
    }
10396
0
      }
10397
0
  }
10398
0
      else if (flinfo->info->unique_symbol
10399
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10400
0
  {
10401
0
    struct local_hash_entry *lh;
10402
0
    size_t count_len;
10403
0
    size_t base_len;
10404
0
    char buf[30];
10405
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10406
0
      {
10407
0
      case STT_FILE:
10408
0
      case STT_SECTION:
10409
0
        break;
10410
0
      default:
10411
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10412
0
         (&flinfo->local_hash_table, name, true, false);
10413
0
        if (lh == NULL)
10414
0
    return 0;
10415
        /* Always append ".COUNT" to local symbols to avoid
10416
     potential conflicts with local symbol "XXX.COUNT".  */
10417
0
        sprintf (buf, "%lx", lh->count);
10418
0
        base_len = lh->size;
10419
0
        if (!base_len)
10420
0
    {
10421
0
      base_len = strlen (name);
10422
0
      lh->size = base_len;
10423
0
    }
10424
0
        count_len = strlen (buf);
10425
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10426
0
            base_len + count_len + 2);
10427
0
        if (versioned_name == NULL)
10428
0
    return 0;
10429
0
        memcpy (versioned_name, name, base_len);
10430
0
        versioned_name[base_len] = '.';
10431
0
        memcpy (versioned_name + base_len + 1, buf,
10432
0
          count_len + 1);
10433
0
        lh->count++;
10434
0
        break;
10435
0
      }
10436
0
  }
10437
0
      elfsym->st_name
10438
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10439
0
                 versioned_name, false);
10440
0
      if (elfsym->st_name == (unsigned long) -1)
10441
0
  return 0;
10442
0
    }
10443
10444
0
  hash_table = elf_hash_table (flinfo->info);
10445
0
  strtabsize = hash_table->strtabsize;
10446
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10447
0
    {
10448
0
      strtabsize += strtabsize;
10449
0
      hash_table->strtabsize = strtabsize;
10450
0
      strtabsize *= sizeof (*hash_table->strtab);
10451
0
      hash_table->strtab
10452
0
  = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10453
0
             strtabsize);
10454
0
      if (hash_table->strtab == NULL)
10455
0
  return 0;
10456
0
    }
10457
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10458
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10459
0
    = flinfo->output_bfd->symcount;
10460
0
  flinfo->output_bfd->symcount += 1;
10461
10462
0
  return 1;
10463
0
}
10464
10465
/* Swap symbols out to the symbol table and flush the output symbols to
10466
   the file.  */
10467
10468
static bool
10469
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10470
0
{
10471
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10472
0
  size_t amt;
10473
0
  size_t i;
10474
0
  elf_backend_data *bed;
10475
0
  bfd_byte *symbuf;
10476
0
  Elf_Internal_Shdr *hdr;
10477
0
  file_ptr pos;
10478
0
  bool ret;
10479
10480
0
  if (flinfo->output_bfd->symcount == 0)
10481
0
    return true;
10482
10483
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10484
10485
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10486
10487
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10488
0
  symbuf = (bfd_byte *) bfd_malloc (amt);
10489
0
  if (symbuf == NULL)
10490
0
    return false;
10491
10492
0
  if (flinfo->symshndxbuf)
10493
0
    {
10494
0
      amt = sizeof (Elf_External_Sym_Shndx);
10495
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10496
0
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10497
0
      if (flinfo->symshndxbuf == NULL)
10498
0
  {
10499
0
    free (symbuf);
10500
0
    return false;
10501
0
  }
10502
0
    }
10503
10504
  /* Now swap out the symbols.  */
10505
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10506
0
    {
10507
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10508
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10509
0
  elfsym->sym.st_name = 0;
10510
0
      else
10511
0
  elfsym->sym.st_name
10512
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10513
0
                elfsym->sym.st_name);
10514
10515
      /* Inform the linker of the addition of this symbol.  */
10516
10517
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10518
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10519
0
             &elfsym->sym);
10520
10521
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10522
0
             ((bfd_byte *) symbuf
10523
0
        + (elfsym->dest_index
10524
0
           * bed->s->sizeof_sym)),
10525
0
             NPTR_ADD (flinfo->symshndxbuf,
10526
0
           elfsym->dest_index));
10527
0
    }
10528
10529
0
  hdr = &elf_symtab_hdr (flinfo->output_bfd);
10530
0
  pos = hdr->sh_offset + hdr->sh_size;
10531
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10532
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10533
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10534
0
    {
10535
0
      hdr->sh_size += amt;
10536
0
      ret = true;
10537
0
    }
10538
0
  else
10539
0
    ret = false;
10540
10541
0
  free (symbuf);
10542
0
  return ret;
10543
0
}
10544
10545
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
10546
10547
static bool
10548
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10549
0
{
10550
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10551
0
      && sym->st_shndx < SHN_LORESERVE)
10552
0
    {
10553
      /* The gABI doesn't support dynamic symbols in output sections
10554
   beyond 64k.  */
10555
0
      _bfd_error_handler
10556
  /* xgettext:c-format */
10557
0
  (_("%pB: too many sections: %d (>= %d)"),
10558
0
   abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10559
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10560
0
      return false;
10561
0
    }
10562
0
  return true;
10563
0
}
10564
10565
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10566
   allowing an unsatisfied unversioned symbol in the DSO to match a
10567
   versioned symbol that would normally require an explicit version.
10568
   We also handle the case that a DSO references a hidden symbol
10569
   which may be satisfied by a versioned symbol in another DSO.  */
10570
10571
static bool
10572
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10573
         elf_backend_data *bed,
10574
         struct elf_link_hash_entry *h)
10575
0
{
10576
0
  bfd *abfd;
10577
0
  struct elf_link_loaded_list *loaded;
10578
10579
0
  if (!is_elf_hash_table (info->hash))
10580
0
    return false;
10581
10582
  /* Check indirect symbol.  */
10583
0
  while (h->root.type == bfd_link_hash_indirect)
10584
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10585
10586
0
  switch (h->root.type)
10587
0
    {
10588
0
    default:
10589
0
      abfd = NULL;
10590
0
      break;
10591
10592
0
    case bfd_link_hash_undefined:
10593
0
    case bfd_link_hash_undefweak:
10594
0
      abfd = h->root.u.undef.abfd;
10595
0
      if (abfd == NULL
10596
0
    || (abfd->flags & DYNAMIC) == 0
10597
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10598
0
  return false;
10599
0
      break;
10600
10601
0
    case bfd_link_hash_defined:
10602
0
    case bfd_link_hash_defweak:
10603
0
      abfd = h->root.u.def.section->owner;
10604
0
      break;
10605
10606
0
    case bfd_link_hash_common:
10607
0
      abfd = h->root.u.c.p->section->owner;
10608
0
      break;
10609
0
    }
10610
0
  BFD_ASSERT (abfd != NULL);
10611
10612
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10613
0
       loaded != NULL;
10614
0
       loaded = loaded->next)
10615
0
    {
10616
0
      bfd *input;
10617
0
      Elf_Internal_Shdr *hdr;
10618
0
      size_t symcount;
10619
0
      size_t extsymcount;
10620
0
      size_t extsymoff;
10621
0
      Elf_Internal_Shdr *versymhdr;
10622
0
      Elf_Internal_Sym *isym;
10623
0
      Elf_Internal_Sym *isymend;
10624
0
      Elf_Internal_Sym *isymbuf;
10625
0
      Elf_External_Versym *ever;
10626
0
      Elf_External_Versym *extversym;
10627
10628
0
      input = loaded->abfd;
10629
10630
      /* We check each DSO for a possible hidden versioned definition.  */
10631
0
      if (input == abfd
10632
0
    || elf_dynversym (input) == 0)
10633
0
  continue;
10634
10635
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10636
10637
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10638
0
      if (elf_bad_symtab (input))
10639
0
  {
10640
0
    extsymcount = symcount;
10641
0
    extsymoff = 0;
10642
0
  }
10643
0
      else
10644
0
  {
10645
0
    extsymcount = symcount - hdr->sh_info;
10646
0
    extsymoff = hdr->sh_info;
10647
0
  }
10648
10649
0
      if (extsymcount == 0)
10650
0
  continue;
10651
10652
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10653
0
              NULL, NULL, NULL);
10654
0
      if (isymbuf == NULL)
10655
0
  return false;
10656
10657
      /* Read in any version definitions.  */
10658
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10659
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10660
0
    || (extversym = (Elf_External_Versym *)
10661
0
        _bfd_malloc_and_read (input, versymhdr->sh_size,
10662
0
            versymhdr->sh_size)) == NULL)
10663
0
  {
10664
0
    free (isymbuf);
10665
0
    return false;
10666
0
  }
10667
10668
0
      ever = extversym + extsymoff;
10669
0
      isymend = isymbuf + extsymcount;
10670
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10671
0
  {
10672
0
    const char *name;
10673
0
    Elf_Internal_Versym iver;
10674
0
    unsigned short version_index;
10675
10676
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10677
0
        || isym->st_shndx == SHN_UNDEF)
10678
0
      continue;
10679
10680
0
    name = bfd_elf_string_from_elf_section (input,
10681
0
              hdr->sh_link,
10682
0
              isym->st_name);
10683
0
    if (strcmp (name, h->root.root.string) != 0)
10684
0
      continue;
10685
10686
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10687
10688
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10689
0
        && !(h->def_regular
10690
0
       && h->forced_local))
10691
0
      {
10692
        /* If we have a non-hidden versioned sym, then it should
10693
     have provided a definition for the undefined sym unless
10694
     it is defined in a non-shared object and forced local.
10695
         */
10696
0
        abort ();
10697
0
      }
10698
10699
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10700
0
    if (version_index == 1 || version_index == 2)
10701
0
      {
10702
        /* This is the base or first version.  We can use it.  */
10703
0
        free (extversym);
10704
0
        free (isymbuf);
10705
0
        return true;
10706
0
      }
10707
0
  }
10708
10709
0
      free (extversym);
10710
0
      free (isymbuf);
10711
0
    }
10712
10713
0
  return false;
10714
0
}
10715
10716
/* Convert ELF common symbol TYPE.  */
10717
10718
static int
10719
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10720
0
{
10721
  /* Commom symbol can only appear in relocatable link.  */
10722
0
  if (!bfd_link_relocatable (info))
10723
0
    abort ();
10724
0
  switch (info->elf_stt_common)
10725
0
    {
10726
0
    case unchanged:
10727
0
      break;
10728
0
    case elf_stt_common:
10729
0
      type = STT_COMMON;
10730
0
      break;
10731
0
    case no_elf_stt_common:
10732
0
      type = STT_OBJECT;
10733
0
      break;
10734
0
    }
10735
0
  return type;
10736
0
}
10737
10738
/* Add an external symbol to the symbol table.  This is called from
10739
   the hash table traversal routine.  When generating a shared object,
10740
   we go through the symbol table twice.  The first time we output
10741
   anything that might have been forced to local scope in a version
10742
   script.  The second time we output the symbols that are still
10743
   global symbols.  */
10744
10745
static bool
10746
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10747
0
{
10748
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10749
0
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10750
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10751
0
  bool strip;
10752
0
  Elf_Internal_Sym sym;
10753
0
  asection *input_sec;
10754
0
  elf_backend_data *bed;
10755
0
  long indx;
10756
0
  int ret;
10757
0
  unsigned int type;
10758
10759
  /* Skip if base symbol doesn't match.  */
10760
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10761
0
    return true;
10762
10763
0
  if (h->root.type == bfd_link_hash_warning)
10764
0
    {
10765
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10766
0
      if (h->root.type == bfd_link_hash_new)
10767
0
  return true;
10768
0
    }
10769
10770
  /* Decide whether to output this symbol in this pass.  */
10771
0
  if (eoinfo->localsyms)
10772
0
    {
10773
0
      if (!h->forced_local)
10774
0
  return true;
10775
0
    }
10776
0
  else
10777
0
    {
10778
0
      if (h->forced_local)
10779
0
  return true;
10780
0
    }
10781
10782
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10783
10784
0
  if (h->root.type == bfd_link_hash_undefined)
10785
0
    {
10786
      /* If we have an undefined symbol reference here then it must have
10787
   come from a shared library that is being linked in.  (Undefined
10788
   references in regular files have already been handled unless
10789
   they are in unreferenced sections which are removed by garbage
10790
   collection).  */
10791
0
      bool ignore_undef = false;
10792
10793
      /* Some symbols may be special in that the fact that they're
10794
   undefined can be safely ignored - let backend determine that.  */
10795
0
      if (bed->elf_backend_ignore_undef_symbol)
10796
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10797
10798
      /* If we are reporting errors for this situation then do so now.  */
10799
0
      if (!ignore_undef
10800
0
    && h->ref_dynamic_nonweak
10801
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10802
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10803
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10804
0
  {
10805
0
    flinfo->info->callbacks->undefined_symbol
10806
0
      (flinfo->info, h->root.root.string,
10807
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10808
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10809
0
       && !flinfo->info->warn_unresolved_syms);
10810
0
  }
10811
10812
      /* Strip a global symbol defined in a discarded section.  */
10813
0
      if (h->indx == -3)
10814
0
  return true;
10815
0
    }
10816
10817
  /* We should also warn if a forced local symbol is referenced from
10818
     shared libraries.  */
10819
0
  if (bfd_link_executable (flinfo->info)
10820
0
      && h->forced_local
10821
0
      && h->ref_dynamic
10822
0
      && h->def_regular
10823
0
      && !h->dynamic_def
10824
0
      && h->ref_dynamic_nonweak
10825
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10826
0
    {
10827
0
      bfd *def_bfd;
10828
0
      const char *msg;
10829
0
      struct elf_link_hash_entry *hi = h;
10830
10831
      /* Check indirect symbol.  */
10832
0
      while (hi->root.type == bfd_link_hash_indirect)
10833
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10834
10835
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10836
  /* xgettext:c-format */
10837
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10838
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10839
  /* xgettext:c-format */
10840
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10841
0
      else
10842
  /* xgettext:c-format */
10843
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10844
0
      def_bfd = flinfo->output_bfd;
10845
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10846
0
  def_bfd = hi->root.u.def.section->owner;
10847
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10848
0
        h->root.root.string, def_bfd);
10849
0
      bfd_set_error (bfd_error_bad_value);
10850
0
      eoinfo->failed = true;
10851
0
      return false;
10852
0
    }
10853
10854
  /* We don't want to output symbols that have never been mentioned by
10855
     a regular file, or that we have been told to strip.  However, if
10856
     h->indx is set to -2, the symbol is used by a reloc and we must
10857
     output it.  */
10858
0
  strip = false;
10859
0
  if (h->indx == -2)
10860
0
    ;
10861
0
  else if ((h->def_dynamic
10862
0
      || h->ref_dynamic
10863
0
      || h->root.type == bfd_link_hash_new)
10864
0
     && !h->def_regular
10865
0
     && !h->ref_regular)
10866
0
    strip = true;
10867
0
  else if (flinfo->info->strip == strip_all)
10868
0
    strip = true;
10869
0
  else if (flinfo->info->strip == strip_some
10870
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10871
0
             h->root.root.string, false, false) == NULL)
10872
0
    strip = true;
10873
0
  else if ((h->root.type == bfd_link_hash_defined
10874
0
      || h->root.type == bfd_link_hash_defweak)
10875
0
     && ((flinfo->info->strip_discarded
10876
0
    && discarded_section (h->root.u.def.section))
10877
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10878
0
       && h->root.u.def.section->owner != NULL
10879
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10880
0
    strip = true;
10881
0
  else if ((h->root.type == bfd_link_hash_undefined
10882
0
      || h->root.type == bfd_link_hash_undefweak)
10883
0
     && h->root.u.undef.abfd != NULL
10884
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10885
0
    strip = true;
10886
10887
  /* Remember if this symbol should be stripped.  */
10888
0
  bool should_strip = strip;
10889
10890
  /* Strip undefined weak symbols link if they don't have relocation.  */
10891
0
  if (!strip)
10892
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10893
10894
0
  type = h->type;
10895
10896
  /* If we're stripping it, and it's not a dynamic symbol, there's
10897
     nothing else to do.   However, if it is a forced local symbol or
10898
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10899
     function a chance to make it dynamic.  */
10900
0
  if (strip
10901
0
      && h->dynindx == -1
10902
0
      && type != STT_GNU_IFUNC
10903
0
      && !h->forced_local)
10904
0
    return true;
10905
10906
0
  sym.st_value = 0;
10907
0
  sym.st_size = h->size;
10908
0
  sym.st_other = h->other;
10909
0
  switch (h->root.type)
10910
0
    {
10911
0
    default:
10912
0
    case bfd_link_hash_new:
10913
0
    case bfd_link_hash_warning:
10914
0
      abort ();
10915
0
      return false;
10916
10917
0
    case bfd_link_hash_undefined:
10918
0
    case bfd_link_hash_undefweak:
10919
0
      input_sec = bfd_und_section_ptr;
10920
0
      sym.st_shndx = SHN_UNDEF;
10921
0
      break;
10922
10923
0
    case bfd_link_hash_defined:
10924
0
    case bfd_link_hash_defweak:
10925
0
      {
10926
0
  input_sec = h->root.u.def.section;
10927
0
  if (input_sec->output_section != NULL)
10928
0
    {
10929
0
      sym.st_shndx =
10930
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10931
0
             input_sec->output_section);
10932
0
      if (sym.st_shndx == SHN_BAD)
10933
0
        {
10934
0
    _bfd_error_handler
10935
      /* xgettext:c-format */
10936
0
      (_("%pB: could not find output section %pA for input section %pA"),
10937
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10938
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10939
0
    eoinfo->failed = true;
10940
0
    return false;
10941
0
        }
10942
10943
      /* ELF symbols in relocatable files are section relative,
10944
         but in nonrelocatable files they are virtual
10945
         addresses.  */
10946
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10947
0
      if (!bfd_link_relocatable (flinfo->info))
10948
0
        {
10949
0
    sym.st_value += input_sec->output_section->vma;
10950
0
    if (h->type == STT_TLS)
10951
0
      {
10952
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10953
0
        if (tls_sec != NULL)
10954
0
          sym.st_value -= tls_sec->vma;
10955
0
      }
10956
0
        }
10957
0
    }
10958
0
  else
10959
0
    {
10960
0
      BFD_ASSERT (input_sec->owner == NULL
10961
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10962
0
      sym.st_shndx = SHN_UNDEF;
10963
0
      input_sec = bfd_und_section_ptr;
10964
0
    }
10965
0
      }
10966
0
      break;
10967
10968
0
    case bfd_link_hash_common:
10969
0
      input_sec = h->root.u.c.p->section;
10970
0
      sym.st_shndx = bed->common_section_index (input_sec);
10971
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10972
0
      break;
10973
10974
0
    case bfd_link_hash_indirect:
10975
      /* These symbols are created by symbol versioning.  They point
10976
   to the decorated version of the name.  For example, if the
10977
   symbol foo@@GNU_1.2 is the default, which should be used when
10978
   foo is used with no version, then we add an indirect symbol
10979
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10980
   since the indirected symbol is already in the hash table.  */
10981
0
      return true;
10982
0
    }
10983
10984
0
  if (type == STT_COMMON || type == STT_OBJECT)
10985
0
    switch (h->root.type)
10986
0
      {
10987
0
      case bfd_link_hash_common:
10988
0
  type = elf_link_convert_common_type (flinfo->info, type);
10989
0
  break;
10990
0
      case bfd_link_hash_defined:
10991
0
      case bfd_link_hash_defweak:
10992
0
  if (bed->common_definition (&sym))
10993
0
    type = elf_link_convert_common_type (flinfo->info, type);
10994
0
  else
10995
0
    type = STT_OBJECT;
10996
0
  break;
10997
0
      case bfd_link_hash_undefined:
10998
0
      case bfd_link_hash_undefweak:
10999
0
  break;
11000
0
      default:
11001
0
  abort ();
11002
0
      }
11003
11004
0
  if (h->forced_local)
11005
0
    {
11006
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
11007
      /* Turn off visibility on local symbol.  */
11008
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11009
0
    }
11010
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
11011
0
  else if (h->unique_global && h->def_regular)
11012
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
11013
0
  else if (h->root.type == bfd_link_hash_undefweak
11014
0
     || h->root.type == bfd_link_hash_defweak)
11015
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
11016
0
  else
11017
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
11018
0
  sym.st_target_internal = h->target_internal;
11019
11020
  /* Give the processor backend a chance to tweak the symbol value,
11021
     and also to finish up anything that needs to be done for this
11022
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
11023
     forced local syms when non-shared is due to a historical quirk.
11024
     STT_GNU_IFUNC symbol must go through PLT.  */
11025
0
  if (((h->type == STT_GNU_IFUNC
11026
0
  && h->def_regular
11027
0
  && !bfd_link_relocatable (flinfo->info))
11028
0
       || ((h->dynindx != -1
11029
0
      || h->forced_local)
11030
0
     && ((bfd_link_pic (flinfo->info)
11031
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11032
0
        || h->root.type != bfd_link_hash_undefweak))
11033
0
         || !h->forced_local)
11034
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11035
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11036
0
    {
11037
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->output_bfd,
11038
0
               flinfo->info, h, &sym))
11039
0
  {
11040
0
    eoinfo->failed = true;
11041
0
    return false;
11042
0
  }
11043
      /* If a symbol is in the dynamic symbol table and isn't a
11044
   should-strip symbol, also keep it in the symbol table.  */
11045
0
      if (!should_strip)
11046
0
  strip = false;
11047
0
    }
11048
11049
  /* If we are marking the symbol as undefined, and there are no
11050
     non-weak references to this symbol from a regular object, then
11051
     mark the symbol as weak undefined; if there are non-weak
11052
     references, mark the symbol as strong.  We can't do this earlier,
11053
     because it might not be marked as undefined until the
11054
     finish_dynamic_symbol routine gets through with it.  */
11055
0
  if (sym.st_shndx == SHN_UNDEF
11056
0
      && h->ref_regular
11057
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11058
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11059
0
    {
11060
0
      int bindtype;
11061
0
      type = ELF_ST_TYPE (sym.st_info);
11062
11063
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11064
0
      if (type == STT_GNU_IFUNC)
11065
0
  type = STT_FUNC;
11066
11067
0
      if (h->ref_regular_nonweak)
11068
0
  bindtype = STB_GLOBAL;
11069
0
      else
11070
0
  bindtype = STB_WEAK;
11071
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11072
0
    }
11073
11074
  /* If this is a symbol defined in a dynamic library, don't use the
11075
     symbol size from the dynamic library.  Relinking an executable
11076
     against a new library may introduce gratuitous changes in the
11077
     executable's symbols if we keep the size.  */
11078
0
  if (sym.st_shndx == SHN_UNDEF
11079
0
      && !h->def_regular
11080
0
      && h->def_dynamic)
11081
0
    sym.st_size = 0;
11082
11083
  /* If a non-weak symbol with non-default visibility is not defined
11084
     locally, it is a fatal error.  */
11085
0
  if (!bfd_link_relocatable (flinfo->info)
11086
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11087
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11088
0
      && h->root.type == bfd_link_hash_undefined
11089
0
      && !h->def_regular)
11090
0
    {
11091
0
      const char *msg;
11092
11093
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11094
  /* xgettext:c-format */
11095
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11096
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11097
  /* xgettext:c-format */
11098
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11099
0
      else
11100
  /* xgettext:c-format */
11101
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11102
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11103
0
      bfd_set_error (bfd_error_bad_value);
11104
0
      eoinfo->failed = true;
11105
0
      return false;
11106
0
    }
11107
11108
  /* If this symbol should be put in the .dynsym section, then put it
11109
     there now.  We already know the symbol index.  We also fill in
11110
     the entry in the .hash section.  */
11111
0
  if (h->dynindx != -1
11112
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11113
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11114
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11115
0
    {
11116
0
      bfd_byte *esym;
11117
11118
      /* Since there is no version information in the dynamic string,
11119
   if there is no version info in symbol version section, we will
11120
   have a run-time problem if not linking executable, referenced
11121
   by shared library, or not bound locally.  */
11122
0
      if (h->verinfo.verdef == NULL
11123
0
    && (!bfd_link_executable (flinfo->info)
11124
0
        || h->ref_dynamic
11125
0
        || !h->def_regular))
11126
0
  {
11127
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11128
11129
0
    if (p && p [1] != '\0')
11130
0
      {
11131
0
        _bfd_error_handler
11132
    /* xgettext:c-format */
11133
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11134
0
     flinfo->output_bfd, h->root.root.string);
11135
0
        eoinfo->failed = true;
11136
0
        return false;
11137
0
      }
11138
0
  }
11139
11140
0
      sym.st_name = h->dynstr_index;
11141
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11142
0
        + h->dynindx * bed->s->sizeof_sym);
11143
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11144
0
  {
11145
0
    eoinfo->failed = true;
11146
0
    return false;
11147
0
  }
11148
11149
      /* Inform the linker of the addition of this symbol.  */
11150
11151
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11152
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11153
11154
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11155
11156
0
      if (flinfo->hash_sec != NULL)
11157
0
  {
11158
0
    size_t hash_entry_size;
11159
0
    bfd_byte *bucketpos;
11160
0
    bfd_vma chain;
11161
0
    size_t bucketcount;
11162
0
    size_t bucket;
11163
11164
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11165
0
    bucket = h->u.elf_hash_value % bucketcount;
11166
11167
0
    hash_entry_size
11168
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11169
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11170
0
           + (bucket + 2) * hash_entry_size);
11171
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11172
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11173
0
       bucketpos);
11174
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11175
0
       ((bfd_byte *) flinfo->hash_sec->contents
11176
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11177
0
  }
11178
11179
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11180
0
  {
11181
0
    Elf_Internal_Versym iversym;
11182
0
    Elf_External_Versym *eversym;
11183
11184
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11185
0
      {
11186
0
        if (h->verinfo.verdef == NULL
11187
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11188
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11189
0
    {
11190
0
      iversym.vs_vers = 1;
11191
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11192
        /* Referenced symbol without ELF_VER_CHR has no
11193
           version.  */
11194
0
        iversym.vs_vers = 0;
11195
0
    }
11196
0
        else
11197
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11198
0
      }
11199
0
    else
11200
0
      {
11201
0
        if (h->verinfo.vertree == NULL)
11202
0
    iversym.vs_vers = 1;
11203
0
        else
11204
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11205
0
        if (flinfo->info->create_default_symver)
11206
0
    iversym.vs_vers++;
11207
11208
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11209
     symbol is defined locally.  */
11210
0
        if (h->versioned == versioned_hidden && h->def_regular)
11211
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11212
0
      }
11213
11214
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11215
0
    eversym += h->dynindx;
11216
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11217
0
  }
11218
0
    }
11219
11220
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11221
     strip it from .symtab too.  Obviously we can't do this for
11222
     relocatable output or when needed for --emit-relocs.  */
11223
0
  else if (input_sec == bfd_und_section_ptr
11224
0
     && h->indx != -2
11225
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11226
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11227
0
     && !bfd_link_relocatable (flinfo->info))
11228
0
    return true;
11229
11230
  /* Also strip others that we couldn't earlier due to dynamic symbol
11231
     processing.  */
11232
0
  if (strip)
11233
0
    return true;
11234
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11235
0
    return true;
11236
11237
  /* Output a FILE symbol so that following locals are not associated
11238
     with the wrong input file.  We need one for forced local symbols
11239
     if we've seen more than one FILE symbol or when we have exactly
11240
     one FILE symbol but global symbols are present in a file other
11241
     than the one with the FILE symbol.  We also need one if linker
11242
     defined symbols are present.  In practice these conditions are
11243
     always met, so just emit the FILE symbol unconditionally.  */
11244
0
  if (eoinfo->localsyms
11245
0
      && !eoinfo->file_sym_done
11246
0
      && eoinfo->flinfo->filesym_count != 0)
11247
0
    {
11248
0
      Elf_Internal_Sym fsym;
11249
11250
0
      memset (&fsym, 0, sizeof (fsym));
11251
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11252
0
      fsym.st_shndx = SHN_ABS;
11253
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11254
0
              bfd_und_section_ptr, NULL))
11255
0
  return false;
11256
11257
0
      eoinfo->file_sym_done = true;
11258
0
    }
11259
11260
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11261
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11262
0
           input_sec, h);
11263
0
  if (ret == 0)
11264
0
    {
11265
0
      eoinfo->failed = true;
11266
0
      return false;
11267
0
    }
11268
0
  else if (ret == 1)
11269
0
    h->indx = indx;
11270
0
  else if (h->indx == -2)
11271
0
    abort();
11272
11273
0
  return true;
11274
0
}
11275
11276
/* Return TRUE if special handling is done for relocs in SEC against
11277
   symbols defined in discarded sections.  */
11278
11279
static bool
11280
elf_section_ignore_discarded_relocs (asection *sec)
11281
0
{
11282
0
  elf_backend_data *bed;
11283
11284
0
  switch (sec->sec_info_type)
11285
0
    {
11286
0
    case SEC_INFO_TYPE_STABS:
11287
0
    case SEC_INFO_TYPE_EH_FRAME:
11288
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11289
0
    case SEC_INFO_TYPE_SFRAME:
11290
0
      return true;
11291
0
    default:
11292
0
      break;
11293
0
    }
11294
11295
0
  bed = get_elf_backend_data (sec->owner);
11296
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11297
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11298
0
    return true;
11299
11300
0
  return false;
11301
0
}
11302
11303
/* Return a mask saying how ld should treat relocations in SEC against
11304
   symbols defined in discarded sections.  If this function returns
11305
   COMPLAIN set, ld will issue a warning message.  If this function
11306
   returns PRETEND set, and the discarded section was link-once and the
11307
   same size as the kept link-once section, ld will pretend that the
11308
   symbol was actually defined in the kept section.  Otherwise ld will
11309
   zero the reloc (at least that is the intent, but some cooperation by
11310
   the target dependent code is needed, particularly for REL targets).  */
11311
11312
unsigned int
11313
_bfd_elf_default_action_discarded (asection *sec)
11314
0
{
11315
0
  if (sec->flags & SEC_DEBUGGING)
11316
0
    return PRETEND;
11317
11318
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11319
0
    return 0;
11320
11321
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11322
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11323
0
    return 0;
11324
11325
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11326
0
    return 0;
11327
11328
0
  return COMPLAIN | PRETEND;
11329
0
}
11330
11331
/* Find a match between a section and a member of a section group.  */
11332
11333
static asection *
11334
match_group_member (asection *sec, asection *group,
11335
        struct bfd_link_info *info)
11336
0
{
11337
0
  asection *first = elf_next_in_group (group);
11338
0
  asection *s = first;
11339
11340
0
  while (s != NULL)
11341
0
    {
11342
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11343
0
  return s;
11344
11345
0
      s = elf_next_in_group (s);
11346
0
      if (s == first)
11347
0
  break;
11348
0
    }
11349
11350
0
  return NULL;
11351
0
}
11352
11353
/* Check if the kept section of a discarded section SEC can be used
11354
   to replace it.  Return the replacement if it is OK.  Otherwise return
11355
   NULL.  */
11356
11357
asection *
11358
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11359
0
{
11360
0
  asection *kept;
11361
11362
0
  kept = sec->kept_section;
11363
0
  if (kept != NULL)
11364
0
    {
11365
0
      if ((kept->flags & SEC_GROUP) != 0)
11366
0
  kept = match_group_member (sec, kept, info);
11367
0
      if (kept != NULL)
11368
0
  {
11369
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11370
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11371
0
      kept = NULL;
11372
0
    else
11373
0
      {
11374
        /* Get the real kept section.  */
11375
0
        asection *next;
11376
0
        for (next = kept->kept_section;
11377
0
       next != NULL;
11378
0
       next = next->kept_section)
11379
0
    kept = next;
11380
0
      }
11381
0
  }
11382
0
      sec->kept_section = kept;
11383
0
    }
11384
0
  return kept;
11385
0
}
11386
11387
/* Link an input file into the linker output file.  This function
11388
   handles all the sections and relocations of the input file at once.
11389
   This is so that we only have to read the local symbols once, and
11390
   don't have to keep them in memory.  */
11391
11392
static bool
11393
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11394
0
{
11395
0
  int (*relocate_section)
11396
0
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11397
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11398
0
  bfd *output_bfd;
11399
0
  Elf_Internal_Shdr *symtab_hdr;
11400
0
  size_t locsymcount;
11401
0
  size_t extsymoff;
11402
0
  size_t num_sym;
11403
0
  Elf_Internal_Sym *isymbuf;
11404
0
  Elf_Internal_Sym *isym;
11405
0
  Elf_Internal_Sym *isymend;
11406
0
  long *pindex;
11407
0
  asection **ppsection;
11408
0
  asection *o;
11409
0
  elf_backend_data *bed;
11410
0
  struct elf_link_hash_entry **sym_hashes;
11411
0
  bfd_size_type address_size;
11412
0
  bfd_vma r_type_mask;
11413
0
  int r_sym_shift;
11414
0
  bool have_file_sym = false;
11415
11416
0
  output_bfd = flinfo->output_bfd;
11417
0
  bed = get_elf_backend_data (output_bfd);
11418
0
  relocate_section = bed->elf_backend_relocate_section;
11419
11420
  /* If this is a dynamic object, we don't want to do anything here:
11421
     we don't want the local symbols, and we don't want the section
11422
     contents.  */
11423
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11424
0
    return true;
11425
11426
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11427
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11428
0
  if (elf_bad_symtab (input_bfd))
11429
0
    {
11430
0
      locsymcount = num_sym;
11431
0
      extsymoff = 0;
11432
0
    }
11433
0
  else
11434
0
    {
11435
0
      locsymcount = symtab_hdr->sh_info;
11436
0
      extsymoff = symtab_hdr->sh_info;
11437
0
    }
11438
11439
  /* Enable GNU OSABI features in the output BFD that are used in the input
11440
     BFD.  */
11441
0
  if (bed->elf_osabi == ELFOSABI_NONE
11442
0
      || bed->elf_osabi == ELFOSABI_GNU
11443
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11444
0
    elf_tdata (output_bfd)->has_gnu_osabi
11445
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11446
0
    & (bfd_link_relocatable (flinfo->info)
11447
0
       ? -1 : ~elf_gnu_osabi_retain));
11448
11449
  /* Read the local symbols.  */
11450
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11451
0
  if (isymbuf == NULL && locsymcount != 0)
11452
0
    {
11453
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11454
0
              flinfo->internal_syms,
11455
0
              flinfo->external_syms,
11456
0
              flinfo->locsym_shndx);
11457
0
      if (isymbuf == NULL)
11458
0
  return false;
11459
0
    }
11460
11461
  /* Find local symbol sections and adjust values of symbols in
11462
     SEC_MERGE sections.  Write out those local symbols we know are
11463
     going into the output file.  */
11464
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11465
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11466
0
       isym < isymend;
11467
0
       isym++, pindex++, ppsection++)
11468
0
    {
11469
0
      asection *isec;
11470
0
      const char *name;
11471
0
      Elf_Internal_Sym osym;
11472
0
      long indx;
11473
0
      int ret;
11474
11475
0
      *pindex = -1;
11476
11477
0
      if (elf_bad_symtab (input_bfd))
11478
0
  {
11479
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11480
0
      {
11481
0
        *ppsection = NULL;
11482
0
        continue;
11483
0
      }
11484
0
  }
11485
11486
0
      if (isym->st_shndx == SHN_UNDEF)
11487
0
  isec = bfd_und_section_ptr;
11488
0
      else if (isym->st_shndx == SHN_ABS)
11489
0
  isec = bfd_abs_section_ptr;
11490
0
      else if (isym->st_shndx == SHN_COMMON)
11491
0
  isec = bfd_com_section_ptr;
11492
0
      else
11493
0
  {
11494
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11495
0
    if (isec == NULL)
11496
0
      {
11497
        /* Don't attempt to output symbols with st_shnx in the
11498
     reserved range other than SHN_ABS and SHN_COMMON.  */
11499
0
        isec = bfd_und_section_ptr;
11500
0
      }
11501
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11502
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11503
0
      isym->st_value =
11504
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11505
0
  }
11506
11507
0
      *ppsection = isec;
11508
11509
      /* Don't output the first, undefined, symbol.  In fact, don't
11510
   output any undefined local symbol.  */
11511
0
      if (isec == bfd_und_section_ptr)
11512
0
  continue;
11513
11514
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11515
0
  {
11516
    /* We never output section symbols.  Instead, we use the
11517
       section symbol of the corresponding section in the output
11518
       file.  */
11519
0
    continue;
11520
0
  }
11521
11522
      /* If we are stripping all symbols, we don't want to output this
11523
   one.  */
11524
0
      if (flinfo->info->strip == strip_all)
11525
0
  continue;
11526
11527
      /* If we are discarding all local symbols, we don't want to
11528
   output this one.  If we are generating a relocatable output
11529
   file, then some of the local symbols may be required by
11530
   relocs; we output them below as we discover that they are
11531
   needed.  */
11532
0
      if (flinfo->info->discard == discard_all)
11533
0
  continue;
11534
11535
      /* If this symbol is defined in a section which we are
11536
   discarding, we don't need to keep it.  */
11537
0
      if (isym->st_shndx < SHN_LORESERVE
11538
0
    && (isec->output_section == NULL
11539
0
        || bfd_section_removed_from_list (output_bfd,
11540
0
            isec->output_section)))
11541
0
  continue;
11542
11543
      /* Get the name of the symbol.  */
11544
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11545
0
                isym->st_name);
11546
0
      if (name == NULL)
11547
0
  return false;
11548
11549
      /* See if we are discarding symbols with this name.  */
11550
0
      if ((flinfo->info->strip == strip_some
11551
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11552
0
         == NULL))
11553
0
    || (((flinfo->info->discard == discard_sec_merge
11554
0
    && (isec->flags & SEC_MERGE)
11555
0
    && !bfd_link_relocatable (flinfo->info))
11556
0
         || flinfo->info->discard == discard_l)
11557
0
        && bfd_is_local_label_name (input_bfd, name)))
11558
0
  continue;
11559
11560
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11561
0
  {
11562
0
    if (input_bfd->lto_output)
11563
      /* -flto puts a temp file name here.  This means builds
11564
         are not reproducible.  Discard the symbol.  */
11565
0
      continue;
11566
0
    have_file_sym = true;
11567
0
    flinfo->filesym_count += 1;
11568
0
  }
11569
0
      if (!have_file_sym)
11570
0
  {
11571
    /* In the absence of debug info, bfd_find_nearest_line uses
11572
       FILE symbols to determine the source file for local
11573
       function symbols.  Provide a FILE symbol here if input
11574
       files lack such, so that their symbols won't be
11575
       associated with a previous input file.  It's not the
11576
       source file, but the best we can do.  */
11577
0
    const char *filename;
11578
0
    have_file_sym = true;
11579
0
    flinfo->filesym_count += 1;
11580
0
    memset (&osym, 0, sizeof (osym));
11581
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11582
0
    osym.st_shndx = SHN_ABS;
11583
0
    if (input_bfd->lto_output)
11584
0
      filename = NULL;
11585
0
    else
11586
0
      filename = lbasename (bfd_get_filename (input_bfd));
11587
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11588
0
            bfd_abs_section_ptr, NULL))
11589
0
      return false;
11590
0
  }
11591
11592
0
      osym = *isym;
11593
11594
      /* Adjust the section index for the output file.  */
11595
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11596
0
               isec->output_section);
11597
0
      if (osym.st_shndx == SHN_BAD)
11598
0
  return false;
11599
11600
      /* ELF symbols in relocatable files are section relative, but
11601
   in executable files they are virtual addresses.  Note that
11602
   this code assumes that all ELF sections have an associated
11603
   BFD section with a reasonable value for output_offset; below
11604
   we assume that they also have a reasonable value for
11605
   output_section.  Any special sections must be set up to meet
11606
   these requirements.  */
11607
0
      osym.st_value += isec->output_offset;
11608
0
      if (!bfd_link_relocatable (flinfo->info))
11609
0
  {
11610
0
    osym.st_value += isec->output_section->vma;
11611
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11612
0
      {
11613
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11614
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11615
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11616
0
        else
11617
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11618
0
              STT_NOTYPE);
11619
0
      }
11620
0
  }
11621
11622
0
      indx = bfd_get_symcount (output_bfd);
11623
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11624
0
      if (ret == 0)
11625
0
  return false;
11626
0
      else if (ret == 1)
11627
0
  *pindex = indx;
11628
0
    }
11629
11630
0
  if (bed->s->arch_size == 32)
11631
0
    {
11632
0
      r_type_mask = 0xff;
11633
0
      r_sym_shift = 8;
11634
0
      address_size = 4;
11635
0
    }
11636
0
  else
11637
0
    {
11638
0
      r_type_mask = 0xffffffff;
11639
0
      r_sym_shift = 32;
11640
0
      address_size = 8;
11641
0
    }
11642
11643
  /* Relocate the contents of each section.  */
11644
0
  sym_hashes = elf_sym_hashes (input_bfd);
11645
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11646
0
    {
11647
0
      bfd_byte *contents;
11648
11649
0
      if (! o->linker_mark)
11650
0
  {
11651
    /* This section was omitted from the link.  */
11652
0
    continue;
11653
0
  }
11654
11655
0
      if (!flinfo->info->resolve_section_groups
11656
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11657
0
  {
11658
    /* Deal with the group signature symbol.  */
11659
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11660
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11661
0
    asection *osec = o->output_section;
11662
11663
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11664
0
    if (symndx >= locsymcount
11665
0
        || (elf_bad_symtab (input_bfd)
11666
0
      && flinfo->sections[symndx] == NULL))
11667
0
      {
11668
0
        struct elf_link_hash_entry *h;
11669
11670
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11671
0
            extsymoff, num_sym);
11672
0
        if (h == NULL)
11673
0
    {
11674
0
      _bfd_error_handler
11675
        /* xgettext:c-format */
11676
0
        (_("error: %pB: unable to create group section symbol"),
11677
0
         input_bfd);
11678
0
      bfd_set_error (bfd_error_bad_value);
11679
0
      return false;
11680
0
    }
11681
11682
        /* Arrange for symbol to be output.  */
11683
0
        h->indx = -2;
11684
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11685
0
      }
11686
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11687
0
      {
11688
        /* We'll use the output section target_index.  */
11689
0
        asection *sec = flinfo->sections[symndx]->output_section;
11690
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11691
0
      }
11692
0
    else
11693
0
      {
11694
0
        if (flinfo->indices[symndx] == -1)
11695
0
    {
11696
      /* Otherwise output the local symbol now.  */
11697
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11698
0
      asection *sec = flinfo->sections[symndx]->output_section;
11699
0
      const char *name;
11700
0
      long indx;
11701
0
      int ret;
11702
11703
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11704
0
                symtab_hdr->sh_link,
11705
0
                sym.st_name);
11706
0
      if (name == NULL)
11707
0
        return false;
11708
11709
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11710
0
                    sec);
11711
0
      if (sym.st_shndx == SHN_BAD)
11712
0
        return false;
11713
11714
0
      sym.st_value += o->output_offset;
11715
11716
0
      indx = bfd_get_symcount (output_bfd);
11717
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11718
0
               NULL);
11719
0
      if (ret == 0)
11720
0
        return false;
11721
0
      else if (ret == 1)
11722
0
        flinfo->indices[symndx] = indx;
11723
0
      else
11724
0
        abort ();
11725
0
    }
11726
0
        elf_section_data (osec)->this_hdr.sh_info
11727
0
    = flinfo->indices[symndx];
11728
0
      }
11729
0
  }
11730
11731
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11732
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11733
0
  continue;
11734
11735
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11736
0
  {
11737
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11738
       or somesuch.  */
11739
0
    continue;
11740
0
  }
11741
11742
      /* Get the contents of the section.  They have been cached by a
11743
   relaxation routine.  Note that o is a section in an input
11744
   file, so the contents field will not have been set by any of
11745
   the routines which work on output files.  */
11746
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11747
0
  {
11748
0
    contents = elf_section_data (o)->this_hdr.contents;
11749
0
    if (bed->caches_rawsize
11750
0
        && o->rawsize != 0
11751
0
        && o->rawsize < o->size)
11752
0
      {
11753
0
        memcpy (flinfo->contents, contents, o->rawsize);
11754
0
        contents = flinfo->contents;
11755
0
      }
11756
0
  }
11757
0
      else if (!(o->flags & SEC_RELOC)
11758
0
         && !bed->elf_backend_write_section
11759
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11760
  /* A MERGE section that has no relocations doesn't need the
11761
     contents anymore, they have been recorded earlier.  Except
11762
     if the backend has special provisions for writing sections.  */
11763
0
  contents = NULL;
11764
0
      else
11765
0
  {
11766
0
    contents = flinfo->contents;
11767
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11768
0
                 &contents))
11769
0
      return false;
11770
0
  }
11771
11772
0
      if ((o->flags & SEC_RELOC) != 0)
11773
0
  {
11774
0
    Elf_Internal_Rela *internal_relocs;
11775
0
    Elf_Internal_Rela *rel, *relend;
11776
0
    int action_discarded;
11777
0
    int ret;
11778
11779
    /* Get the swapped relocs.  */
11780
0
    internal_relocs
11781
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11782
0
                flinfo->external_relocs,
11783
0
                flinfo->internal_relocs,
11784
0
                false);
11785
0
    if (internal_relocs == NULL
11786
0
        && o->reloc_count > 0)
11787
0
      return false;
11788
11789
0
    action_discarded = -1;
11790
0
    if (!elf_section_ignore_discarded_relocs (o))
11791
0
      action_discarded = (*bed->action_discarded) (o);
11792
11793
    /* Run through the relocs evaluating complex reloc symbols and
11794
       looking for relocs against symbols from discarded sections
11795
       or section symbols from removed link-once sections.
11796
       Complain about relocs against discarded sections.  Zero
11797
       relocs against removed link-once sections.  */
11798
11799
0
    rel = internal_relocs;
11800
0
    relend = rel + o->reloc_count;
11801
0
    for ( ; rel < relend; rel++)
11802
0
      {
11803
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11804
0
        unsigned int s_type;
11805
0
        asection **ps, *sec;
11806
0
        struct elf_link_hash_entry *h = NULL;
11807
0
        const char *sym_name;
11808
11809
0
        if (r_symndx == STN_UNDEF)
11810
0
    continue;
11811
11812
0
        if (r_symndx >= locsymcount
11813
0
      || (elf_bad_symtab (input_bfd)
11814
0
          && flinfo->sections[r_symndx] == NULL))
11815
0
    {
11816
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11817
0
                extsymoff, num_sym);
11818
11819
      /* Badly formatted input files can contain relocs that
11820
         reference non-existant symbols.  Check here so that
11821
         we do not seg fault.  */
11822
0
      if (h == NULL)
11823
0
        {
11824
0
          _bfd_error_handler
11825
      /* xgettext:c-format */
11826
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11827
0
         "that references a non-existent global symbol"),
11828
0
       input_bfd, (uint64_t) rel->r_info, o);
11829
0
          bfd_set_error (bfd_error_bad_value);
11830
0
          return false;
11831
0
        }
11832
11833
0
      s_type = h->type;
11834
11835
      /* If a plugin symbol is referenced from a non-IR file,
11836
         mark the symbol as undefined.  Note that the
11837
         linker may attach linker created dynamic sections
11838
         to the plugin bfd.  Symbols defined in linker
11839
         created sections are not plugin symbols.  */
11840
0
      if ((h->root.non_ir_ref_regular
11841
0
           || h->root.non_ir_ref_dynamic)
11842
0
          && (h->root.type == bfd_link_hash_defined
11843
0
        || h->root.type == bfd_link_hash_defweak)
11844
0
          && (h->root.u.def.section->flags
11845
0
        & SEC_LINKER_CREATED) == 0
11846
0
          && h->root.u.def.section->owner != NULL
11847
0
          && (h->root.u.def.section->owner->flags
11848
0
        & BFD_PLUGIN) != 0)
11849
0
        {
11850
0
          h->root.type = bfd_link_hash_undefined;
11851
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11852
0
        }
11853
11854
0
      ps = NULL;
11855
0
      if (h->root.type == bfd_link_hash_defined
11856
0
          || h->root.type == bfd_link_hash_defweak)
11857
0
        ps = &h->root.u.def.section;
11858
11859
0
      sym_name = h->root.root.string;
11860
0
    }
11861
0
        else
11862
0
    {
11863
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11864
11865
0
      s_type = ELF_ST_TYPE (sym->st_info);
11866
0
      ps = &flinfo->sections[r_symndx];
11867
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11868
0
                 sym, *ps);
11869
0
    }
11870
11871
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11872
0
      && !bfd_link_relocatable (flinfo->info))
11873
0
    {
11874
0
      bfd_vma val;
11875
0
      bfd_vma dot = (rel->r_offset
11876
0
         + o->output_offset + o->output_section->vma);
11877
#ifdef DEBUG
11878
      printf ("Encountered a complex symbol!");
11879
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11880
        bfd_get_filename (input_bfd), o->name,
11881
        (long) (rel - internal_relocs));
11882
      printf (" symbol: idx  %8.8lx, name %s\n",
11883
        r_symndx, sym_name);
11884
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11885
        (unsigned long) rel->r_info,
11886
        (unsigned long) rel->r_offset);
11887
#endif
11888
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11889
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11890
0
        return false;
11891
11892
      /* Symbol evaluated OK.  Update to absolute value.  */
11893
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11894
0
           num_sym, r_symndx, val))
11895
0
        return false;
11896
11897
0
      continue;
11898
0
    }
11899
11900
0
        if (action_discarded != -1 && ps != NULL)
11901
0
    {
11902
      /* Complain if the definition comes from a
11903
         discarded section.  */
11904
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11905
0
        {
11906
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11907
0
          if (action_discarded & COMPLAIN)
11908
0
      (*flinfo->info->callbacks->einfo)
11909
        /* xgettext:c-format */
11910
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11911
0
           "defined in discarded section `%pA' of %pB\n"),
11912
0
         sym_name, o, input_bfd, sec, sec->owner);
11913
11914
          /* Try to do the best we can to support buggy old
11915
       versions of gcc.  Pretend that the symbol is
11916
       really defined in the kept linkonce section.
11917
       FIXME: This is quite broken.  Modifying the
11918
       symbol here means we will be changing all later
11919
       uses of the symbol, not just in this section.  */
11920
0
          if (action_discarded & PRETEND)
11921
0
      {
11922
0
        asection *kept;
11923
11924
0
        kept = _bfd_elf_check_kept_section (sec,
11925
0
                    flinfo->info);
11926
0
        if (kept != NULL)
11927
0
          {
11928
0
            *ps = kept;
11929
0
            continue;
11930
0
          }
11931
0
      }
11932
0
        }
11933
0
    }
11934
0
      }
11935
11936
    /* Relocate the section by invoking a back end routine.
11937
11938
       The back end routine is responsible for adjusting the
11939
       section contents as necessary, and (if using Rela relocs
11940
       and generating a relocatable output file) adjusting the
11941
       reloc addend as necessary.
11942
11943
       The back end routine does not have to worry about setting
11944
       the reloc address or the reloc symbol index.
11945
11946
       The back end routine is given a pointer to the swapped in
11947
       internal symbols, and can access the hash table entries
11948
       for the external symbols via elf_sym_hashes (input_bfd).
11949
11950
       When generating relocatable output, the back end routine
11951
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11952
       output symbol is going to be a section symbol
11953
       corresponding to the output section, which will require
11954
       the addend to be adjusted.  */
11955
11956
0
    ret = (*relocate_section) (output_bfd, flinfo->info,
11957
0
             input_bfd, o, contents,
11958
0
             internal_relocs,
11959
0
             isymbuf,
11960
0
             flinfo->sections);
11961
0
    if (!ret)
11962
0
      return false;
11963
11964
0
    if (ret == 2
11965
0
        || bfd_link_relocatable (flinfo->info)
11966
0
        || flinfo->info->emitrelocations)
11967
0
      {
11968
0
        Elf_Internal_Rela *irela;
11969
0
        Elf_Internal_Rela *irelaend, *irelamid;
11970
0
        bfd_vma last_offset;
11971
0
        struct elf_link_hash_entry **rel_hash;
11972
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11973
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11974
0
        unsigned int next_erel;
11975
0
        bool rela_normal;
11976
0
        struct bfd_elf_section_data *esdi, *esdo;
11977
11978
0
        esdi = elf_section_data (o);
11979
0
        esdo = elf_section_data (o->output_section);
11980
0
        rela_normal = false;
11981
11982
        /* Adjust the reloc addresses and symbol indices.  */
11983
11984
0
        irela = internal_relocs;
11985
0
        irelaend = irela + o->reloc_count;
11986
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11987
        /* We start processing the REL relocs, if any.  When we reach
11988
     IRELAMID in the loop, we switch to the RELA relocs.  */
11989
0
        irelamid = irela;
11990
0
        if (esdi->rel.hdr != NULL)
11991
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11992
0
           * bed->s->int_rels_per_ext_rel);
11993
0
        rel_hash_list = rel_hash;
11994
0
        rela_hash_list = NULL;
11995
0
        last_offset = o->output_offset;
11996
0
        if (!bfd_link_relocatable (flinfo->info))
11997
0
    last_offset += o->output_section->vma;
11998
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11999
0
    {
12000
0
      unsigned long r_symndx;
12001
0
      asection *sec;
12002
0
      Elf_Internal_Sym sym;
12003
12004
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
12005
0
        {
12006
0
          rel_hash++;
12007
0
          next_erel = 0;
12008
0
        }
12009
12010
0
      if (irela == irelamid)
12011
0
        {
12012
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
12013
0
          rela_hash_list = rel_hash;
12014
0
          rela_normal = bed->rela_normal;
12015
0
        }
12016
12017
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
12018
0
                   flinfo->info, o,
12019
0
                   irela->r_offset);
12020
0
      if (irela->r_offset >= (bfd_vma) -2)
12021
0
        {
12022
          /* This is a reloc for a deleted entry or somesuch.
12023
       Turn it into an R_*_NONE reloc, at the same
12024
       offset as the last reloc.  elf_eh_frame.c and
12025
       bfd_elf_discard_info rely on reloc offsets
12026
       being ordered.  */
12027
0
          irela->r_offset = last_offset;
12028
0
          irela->r_info = 0;
12029
0
          irela->r_addend = 0;
12030
0
          continue;
12031
0
        }
12032
12033
0
      irela->r_offset += o->output_offset;
12034
12035
      /* Relocs in an executable have to be virtual addresses.  */
12036
0
      if (!bfd_link_relocatable (flinfo->info))
12037
0
        irela->r_offset += o->output_section->vma;
12038
12039
0
      last_offset = irela->r_offset;
12040
12041
0
      r_symndx = irela->r_info >> r_sym_shift;
12042
0
      if (r_symndx == STN_UNDEF)
12043
0
        continue;
12044
12045
0
      if (r_symndx >= locsymcount
12046
0
          || (elf_bad_symtab (input_bfd)
12047
0
        && flinfo->sections[r_symndx] == NULL))
12048
0
        {
12049
0
          struct elf_link_hash_entry *rh;
12050
12051
          /* This is a reloc against a global symbol.  We
12052
       have not yet output all the local symbols, so
12053
       we do not know the symbol index of any global
12054
       symbol.  We set the rel_hash entry for this
12055
       reloc to point to the global hash table entry
12056
       for this symbol.  The symbol index is then
12057
       set at the end of bfd_elf_final_link.  */
12058
0
          rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12059
0
               extsymoff, num_sym);
12060
0
          if (rh == NULL)
12061
0
      {
12062
        /* FIXME: Generate an error ?  */
12063
0
        continue;
12064
0
      }
12065
12066
          /* Setting the index to -2 tells elf_link_output_extsym
12067
       that this symbol is used by a reloc.  */
12068
0
          BFD_ASSERT (rh->indx < 0);
12069
0
          rh->indx = -2;
12070
0
          *rel_hash = rh;
12071
12072
0
          continue;
12073
0
        }
12074
12075
      /* This is a reloc against a local symbol.  */
12076
12077
0
      *rel_hash = NULL;
12078
0
      sym = isymbuf[r_symndx];
12079
0
      sec = flinfo->sections[r_symndx];
12080
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12081
0
        {
12082
          /* I suppose the backend ought to fill in the
12083
       section of any STT_SECTION symbol against a
12084
       processor specific section.  */
12085
0
          r_symndx = STN_UNDEF;
12086
0
          if (bfd_is_abs_section (sec))
12087
0
      ;
12088
0
          else if (sec == NULL || sec->owner == NULL)
12089
0
      {
12090
0
        bfd_set_error (bfd_error_bad_value);
12091
0
        return false;
12092
0
      }
12093
0
          else
12094
0
      {
12095
0
        asection *osec = sec->output_section;
12096
12097
        /* If we have discarded a section, the output
12098
           section will be the absolute section.  In
12099
           case of discarded SEC_MERGE sections, use
12100
           the kept section.  relocate_section should
12101
           have already handled discarded linkonce
12102
           sections.  */
12103
0
        if (bfd_is_abs_section (osec)
12104
0
            && sec->kept_section != NULL
12105
0
            && sec->kept_section->output_section != NULL)
12106
0
          {
12107
0
            osec = sec->kept_section->output_section;
12108
0
            irela->r_addend -= osec->vma;
12109
0
          }
12110
12111
0
        if (!bfd_is_abs_section (osec))
12112
0
          {
12113
0
            r_symndx = osec->target_index;
12114
0
            if (r_symndx == STN_UNDEF)
12115
0
        {
12116
0
          irela->r_addend += osec->vma;
12117
0
          osec = flinfo->info->callbacks->nearby_section
12118
0
            (output_bfd, osec, osec->vma);
12119
0
          irela->r_addend -= osec->vma;
12120
0
          r_symndx = osec->target_index;
12121
0
        }
12122
0
          }
12123
0
      }
12124
12125
          /* Adjust the addend according to where the
12126
       section winds up in the output section.  */
12127
0
          if (rela_normal)
12128
0
      irela->r_addend += sec->output_offset;
12129
0
        }
12130
0
      else
12131
0
        {
12132
0
          if (flinfo->indices[r_symndx] == -1)
12133
0
      {
12134
0
        unsigned long shlink;
12135
0
        const char *name;
12136
0
        asection *osec;
12137
0
        long indx;
12138
12139
0
        if (flinfo->info->strip == strip_all)
12140
0
          {
12141
            /* You can't do ld -r -s.  */
12142
0
            bfd_set_error (bfd_error_invalid_operation);
12143
0
            return false;
12144
0
          }
12145
12146
        /* This symbol was skipped earlier, but
12147
           since it is needed by a reloc, we
12148
           must output it now.  */
12149
0
        shlink = symtab_hdr->sh_link;
12150
0
        name = (bfd_elf_string_from_elf_section
12151
0
          (input_bfd, shlink, sym.st_name));
12152
0
        if (name == NULL)
12153
0
          return false;
12154
12155
0
        osec = sec->output_section;
12156
0
        sym.st_shndx =
12157
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12158
0
                     osec);
12159
0
        if (sym.st_shndx == SHN_BAD)
12160
0
          return false;
12161
12162
0
        sym.st_value += sec->output_offset;
12163
0
        if (!bfd_link_relocatable (flinfo->info))
12164
0
          {
12165
0
            sym.st_value += osec->vma;
12166
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12167
0
        {
12168
0
          struct elf_link_hash_table *htab
12169
0
            = elf_hash_table (flinfo->info);
12170
12171
          /* STT_TLS symbols are relative to PT_TLS
12172
             segment base.  */
12173
0
          if (htab->tls_sec != NULL)
12174
0
            sym.st_value -= htab->tls_sec->vma;
12175
0
          else
12176
0
            sym.st_info
12177
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12178
0
                 STT_NOTYPE);
12179
0
        }
12180
0
          }
12181
12182
0
        indx = bfd_get_symcount (output_bfd);
12183
0
        ret = elf_link_output_symstrtab (flinfo, name,
12184
0
                 &sym, sec,
12185
0
                 NULL);
12186
0
        if (ret == 0)
12187
0
          return false;
12188
0
        else if (ret == 1)
12189
0
          flinfo->indices[r_symndx] = indx;
12190
0
        else
12191
0
          abort ();
12192
0
      }
12193
12194
0
          r_symndx = flinfo->indices[r_symndx];
12195
0
        }
12196
12197
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12198
0
           | (irela->r_info & r_type_mask));
12199
0
    }
12200
12201
        /* Swap out the relocs.  */
12202
0
        input_rel_hdr = esdi->rel.hdr;
12203
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12204
0
    {
12205
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12206
0
                 input_rel_hdr,
12207
0
                 internal_relocs,
12208
0
                 rel_hash_list))
12209
0
        return false;
12210
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12211
0
              * bed->s->int_rels_per_ext_rel);
12212
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12213
0
    }
12214
12215
0
        input_rela_hdr = esdi->rela.hdr;
12216
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12217
0
    {
12218
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12219
0
                 input_rela_hdr,
12220
0
                 internal_relocs,
12221
0
                 rela_hash_list))
12222
0
        return false;
12223
0
    }
12224
0
      }
12225
0
  }
12226
12227
      /* Write out the modified section contents.  */
12228
0
      if (bed->elf_backend_write_section
12229
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12230
0
            contents))
12231
0
  {
12232
    /* Section written out.  */
12233
0
  }
12234
0
      else switch (o->sec_info_type)
12235
0
  {
12236
0
  case SEC_INFO_TYPE_STABS:
12237
0
    if (! (_bfd_write_section_stabs
12238
0
     (output_bfd,
12239
0
      &elf_hash_table (flinfo->info)->stab_info, o, contents)))
12240
0
      return false;
12241
0
    break;
12242
0
  case SEC_INFO_TYPE_MERGE:
12243
0
    if (! _bfd_write_merged_section (output_bfd, o))
12244
0
      return false;
12245
0
    break;
12246
0
  case SEC_INFO_TYPE_EH_FRAME:
12247
0
    {
12248
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12249
0
               o, contents))
12250
0
        return false;
12251
0
    }
12252
0
    break;
12253
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12254
0
    {
12255
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12256
0
               flinfo->info,
12257
0
               o, contents))
12258
0
        return false;
12259
0
    }
12260
0
    break;
12261
0
  case SEC_INFO_TYPE_SFRAME:
12262
0
      {
12263
        /* Merge SFrame section into the SFrame encoder context of the
12264
     output_bfd's section.  The final .sframe output section will
12265
     be written out later.  */
12266
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12267
0
              o, contents))
12268
0
    return false;
12269
0
      }
12270
0
      break;
12271
0
  default:
12272
0
    {
12273
0
      if (! (o->flags & SEC_EXCLUDE))
12274
0
        {
12275
0
    file_ptr offset = (file_ptr) o->output_offset;
12276
0
    bfd_size_type todo = o->size;
12277
12278
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12279
12280
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12281
0
        && o->size > address_size)
12282
0
      {
12283
        /* Reverse-copy input section to output.  */
12284
12285
0
        if ((o->size & (address_size - 1)) != 0
12286
0
      || (o->reloc_count != 0
12287
0
          && (o->size * bed->s->int_rels_per_ext_rel
12288
0
        != o->reloc_count * address_size)))
12289
0
          {
12290
0
      _bfd_error_handler
12291
        /* xgettext:c-format */
12292
0
        (_("error: %pB: size of section %pA is not "
12293
0
           "multiple of address size"),
12294
0
         input_bfd, o);
12295
0
      bfd_set_error (bfd_error_bad_value);
12296
0
      return false;
12297
0
          }
12298
12299
0
        do
12300
0
          {
12301
0
      todo -= address_size;
12302
0
      if (! bfd_set_section_contents (output_bfd,
12303
0
              o->output_section,
12304
0
              contents + todo,
12305
0
              offset,
12306
0
              address_size))
12307
0
        return false;
12308
0
      if (todo == 0)
12309
0
        break;
12310
0
      offset += address_size;
12311
0
          }
12312
0
        while (1);
12313
0
      }
12314
0
    else if (! bfd_set_section_contents (output_bfd,
12315
0
                 o->output_section,
12316
0
                 contents,
12317
0
                 offset, todo))
12318
0
      return false;
12319
0
        }
12320
0
    }
12321
0
    break;
12322
0
  }
12323
12324
      /* Munmap the section contents for each input section.  */
12325
0
      _bfd_elf_link_munmap_section_contents (o);
12326
0
    }
12327
12328
0
  return true;
12329
0
}
12330
12331
/* Generate a reloc when linking an ELF file.  This is a reloc
12332
   requested by the linker, and does not come from any input file.  This
12333
   is used to build constructor and destructor tables when linking
12334
   with -Ur.  */
12335
12336
static bool
12337
elf_reloc_link_order (bfd *output_bfd,
12338
          struct bfd_link_info *info,
12339
          asection *output_section,
12340
          struct bfd_link_order *link_order)
12341
0
{
12342
0
  reloc_howto_type *howto;
12343
0
  long indx;
12344
0
  bfd_vma offset;
12345
0
  bfd_vma addend;
12346
0
  struct bfd_elf_section_reloc_data *reldata;
12347
0
  struct elf_link_hash_entry **rel_hash_ptr;
12348
0
  Elf_Internal_Shdr *rel_hdr;
12349
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12350
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12351
0
  bfd_byte *erel;
12352
0
  unsigned int i;
12353
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12354
12355
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12356
0
  if (howto == NULL)
12357
0
    {
12358
0
      bfd_set_error (bfd_error_bad_value);
12359
0
      return false;
12360
0
    }
12361
12362
0
  addend = link_order->u.reloc.p->addend;
12363
12364
0
  if (esdo->rel.hdr)
12365
0
    reldata = &esdo->rel;
12366
0
  else if (esdo->rela.hdr)
12367
0
    reldata = &esdo->rela;
12368
0
  else
12369
0
    {
12370
0
      reldata = NULL;
12371
0
      BFD_ASSERT (0);
12372
0
    }
12373
12374
  /* Figure out the symbol index.  */
12375
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12376
0
  if (link_order->type == bfd_section_reloc_link_order)
12377
0
    {
12378
0
      indx = link_order->u.reloc.p->u.section->target_index;
12379
0
      BFD_ASSERT (indx != 0);
12380
0
      *rel_hash_ptr = NULL;
12381
0
    }
12382
0
  else
12383
0
    {
12384
0
      struct elf_link_hash_entry *h;
12385
12386
      /* Treat a reloc against a defined symbol as though it were
12387
   actually against the section.  */
12388
0
      h = ((struct elf_link_hash_entry *)
12389
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12390
0
           link_order->u.reloc.p->u.name,
12391
0
           false, false, true));
12392
0
      if (h != NULL
12393
0
    && (h->root.type == bfd_link_hash_defined
12394
0
        || h->root.type == bfd_link_hash_defweak))
12395
0
  {
12396
0
    asection *section;
12397
12398
0
    section = h->root.u.def.section;
12399
0
    indx = section->output_section->target_index;
12400
0
    *rel_hash_ptr = NULL;
12401
    /* It seems that we ought to add the symbol value to the
12402
       addend here, but in practice it has already been added
12403
       because it was passed to constructor_callback.  */
12404
0
    addend += section->output_section->vma + section->output_offset;
12405
0
  }
12406
0
      else if (h != NULL)
12407
0
  {
12408
    /* Setting the index to -2 tells elf_link_output_extsym that
12409
       this symbol is used by a reloc.  */
12410
0
    h->indx = -2;
12411
0
    *rel_hash_ptr = h;
12412
0
    indx = 0;
12413
0
  }
12414
0
      else
12415
0
  {
12416
0
    (*info->callbacks->unattached_reloc)
12417
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12418
0
    indx = 0;
12419
0
  }
12420
0
    }
12421
12422
  /* If this is an inplace reloc, we must write the addend into the
12423
     object file.  */
12424
0
  if (howto->partial_inplace && addend != 0)
12425
0
    {
12426
0
      bfd_size_type size;
12427
0
      bfd_reloc_status_type rstat;
12428
0
      bfd_byte *buf;
12429
0
      bool ok;
12430
0
      const char *sym_name;
12431
0
      bfd_size_type octets;
12432
12433
0
      size = (bfd_size_type) bfd_get_reloc_size (howto);
12434
0
      buf = (bfd_byte *) bfd_zmalloc (size);
12435
0
      if (buf == NULL && size != 0)
12436
0
  return false;
12437
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12438
0
      switch (rstat)
12439
0
  {
12440
0
  case bfd_reloc_ok:
12441
0
    break;
12442
12443
0
  default:
12444
0
  case bfd_reloc_outofrange:
12445
0
    abort ();
12446
12447
0
  case bfd_reloc_overflow:
12448
0
    if (link_order->type == bfd_section_reloc_link_order)
12449
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12450
0
    else
12451
0
      sym_name = link_order->u.reloc.p->u.name;
12452
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12453
0
                howto->name, addend, NULL, NULL,
12454
0
                (bfd_vma) 0);
12455
0
    break;
12456
0
  }
12457
12458
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12459
0
               output_section);
12460
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12461
0
             octets, size);
12462
0
      free (buf);
12463
0
      if (! ok)
12464
0
  return false;
12465
0
    }
12466
12467
  /* The address of a reloc is relative to the section in a
12468
     relocatable file, and is a virtual address in an executable
12469
     file.  */
12470
0
  offset = link_order->offset;
12471
0
  if (! bfd_link_relocatable (info))
12472
0
    offset += output_section->vma;
12473
12474
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12475
0
    {
12476
0
      irel[i].r_offset = offset;
12477
0
      irel[i].r_info = 0;
12478
0
      irel[i].r_addend = 0;
12479
0
    }
12480
0
  if (bed->s->arch_size == 32)
12481
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12482
0
  else
12483
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12484
12485
0
  rel_hdr = reldata->hdr;
12486
0
  erel = rel_hdr->contents;
12487
0
  if (rel_hdr->sh_type == SHT_REL)
12488
0
    {
12489
0
      erel += reldata->count * bed->s->sizeof_rel;
12490
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12491
0
    }
12492
0
  else
12493
0
    {
12494
0
      irel[0].r_addend = addend;
12495
0
      erel += reldata->count * bed->s->sizeof_rela;
12496
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12497
0
    }
12498
12499
0
  ++reldata->count;
12500
12501
0
  return true;
12502
0
}
12503
12504
/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12505
   Returns TRUE upon success, FALSE otherwise.  */
12506
12507
static bool
12508
elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12509
0
{
12510
0
  bool ret = false;
12511
0
  bfd *implib_bfd;
12512
0
  elf_backend_data *bed;
12513
0
  flagword flags;
12514
0
  enum bfd_architecture arch;
12515
0
  unsigned int mach;
12516
0
  asymbol **sympp = NULL;
12517
0
  long symsize;
12518
0
  long symcount;
12519
0
  long src_count;
12520
0
  elf_symbol_type *osymbuf;
12521
0
  size_t amt;
12522
12523
0
  implib_bfd = info->out_implib_bfd;
12524
0
  bed = get_elf_backend_data (abfd);
12525
12526
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12527
0
    return false;
12528
12529
  /* Use flag from executable but make it a relocatable object.  */
12530
0
  flags = bfd_get_file_flags (abfd);
12531
0
  flags &= ~HAS_RELOC;
12532
0
  if (!bfd_set_start_address (implib_bfd, 0)
12533
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12534
0
    return false;
12535
12536
  /* Copy architecture of output file to import library file.  */
12537
0
  arch = bfd_get_arch (abfd);
12538
0
  mach = bfd_get_mach (abfd);
12539
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12540
0
      && (abfd->target_defaulted
12541
0
    || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12542
0
    return false;
12543
12544
  /* Get symbol table size.  */
12545
0
  symsize = bfd_get_symtab_upper_bound (abfd);
12546
0
  if (symsize < 0)
12547
0
    return false;
12548
12549
  /* Read in the symbol table.  */
12550
0
  sympp = (asymbol **) bfd_malloc (symsize);
12551
0
  if (sympp == NULL)
12552
0
    return false;
12553
12554
0
  symcount = bfd_canonicalize_symtab (abfd, sympp);
12555
0
  if (symcount < 0)
12556
0
    goto free_sym_buf;
12557
12558
  /* Allow the BFD backend to copy any private header data it
12559
     understands from the output BFD to the import library BFD.  */
12560
0
  if (! bfd_copy_private_header_data (abfd, implib_bfd))
12561
0
    goto free_sym_buf;
12562
12563
  /* Filter symbols to appear in the import library.  */
12564
0
  if (bed->elf_backend_filter_implib_symbols)
12565
0
    symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12566
0
                   symcount);
12567
0
  else
12568
0
    symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12569
0
  if (symcount == 0)
12570
0
    {
12571
0
      bfd_set_error (bfd_error_no_symbols);
12572
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12573
0
        implib_bfd);
12574
0
      goto free_sym_buf;
12575
0
    }
12576
12577
12578
  /* Make symbols absolute.  */
12579
0
  amt = symcount * sizeof (*osymbuf);
12580
0
  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12581
0
  if (osymbuf == NULL)
12582
0
    goto free_sym_buf;
12583
12584
0
  for (src_count = 0; src_count < symcount; src_count++)
12585
0
    {
12586
0
      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12587
0
        sizeof (*osymbuf));
12588
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12589
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12590
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12591
0
      osymbuf[src_count].internal_elf_sym.st_value =
12592
0
  osymbuf[src_count].symbol.value;
12593
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12594
0
    }
12595
12596
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12597
12598
  /* Allow the BFD backend to copy any private data it understands
12599
     from the output BFD to the import library BFD.  This is done last
12600
     to permit the routine to look at the filtered symbol table.  */
12601
0
  if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12602
0
    goto free_sym_buf;
12603
12604
0
  if (!bfd_close (implib_bfd))
12605
0
    goto free_sym_buf;
12606
12607
0
  ret = true;
12608
12609
0
 free_sym_buf:
12610
0
  free (sympp);
12611
0
  return ret;
12612
0
}
12613
12614
static void
12615
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12616
0
{
12617
0
  asection *o;
12618
12619
0
  if (flinfo->symstrtab != NULL)
12620
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12621
0
  free (flinfo->contents);
12622
0
  free (flinfo->external_relocs);
12623
0
  free (flinfo->internal_relocs);
12624
0
  free (flinfo->external_syms);
12625
0
  free (flinfo->locsym_shndx);
12626
0
  free (flinfo->internal_syms);
12627
0
  free (flinfo->indices);
12628
0
  free (flinfo->sections);
12629
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12630
0
    free (flinfo->symshndxbuf);
12631
0
  for (o = obfd->sections; o != NULL; o = o->next)
12632
0
    {
12633
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12634
0
      free (esdo->rel.hashes);
12635
0
      free (esdo->rela.hashes);
12636
0
    }
12637
0
}
12638
12639
/* Do the final step of an ELF link.  */
12640
12641
bool
12642
_bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12643
0
{
12644
0
  bool dynamic;
12645
0
  bool emit_relocs;
12646
0
  bfd *dynobj;
12647
0
  struct elf_final_link_info flinfo;
12648
0
  asection *o;
12649
0
  struct bfd_link_order *p;
12650
0
  bfd *sub;
12651
0
  bfd_size_type max_contents_size;
12652
0
  bfd_size_type max_external_reloc_size;
12653
0
  bfd_size_type max_internal_reloc_count;
12654
0
  bfd_size_type max_sym_count;
12655
0
  bfd_size_type max_sym_shndx_count;
12656
0
  Elf_Internal_Sym elfsym;
12657
0
  unsigned int i;
12658
0
  Elf_Internal_Shdr *symtab_hdr;
12659
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12660
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12661
0
  struct elf_outext_info eoinfo;
12662
0
  bool merged;
12663
0
  size_t relativecount;
12664
0
  size_t relr_entsize;
12665
0
  asection *reldyn = 0;
12666
0
  bfd_size_type amt;
12667
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12668
0
  bool sections_removed;
12669
12670
0
  if (!is_elf_hash_table (&htab->root))
12671
0
    return false;
12672
12673
0
  if (bfd_link_pic (info))
12674
0
    abfd->flags |= DYNAMIC;
12675
12676
0
  dynamic = htab->dynamic_sections_created;
12677
0
  dynobj = htab->dynobj;
12678
12679
0
  emit_relocs = (bfd_link_relocatable (info)
12680
0
     || info->emitrelocations);
12681
12682
0
  memset (&flinfo, 0, sizeof (flinfo));
12683
0
  flinfo.info = info;
12684
0
  flinfo.output_bfd = abfd;
12685
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12686
0
  if (flinfo.symstrtab == NULL)
12687
0
    return false;
12688
12689
0
  if (! dynamic)
12690
0
    {
12691
0
      flinfo.hash_sec = NULL;
12692
0
      flinfo.symver_sec = NULL;
12693
0
    }
12694
0
  else
12695
0
    {
12696
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12697
      /* Note that dynsym_sec can be NULL (on VMS).  */
12698
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12699
      /* Note that it is OK if symver_sec is NULL.  */
12700
0
    }
12701
12702
0
  if (info->unique_symbol
12703
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12704
0
             local_hash_newfunc,
12705
0
             sizeof (struct local_hash_entry)))
12706
0
    return false;
12707
12708
  /* The object attributes have been merged.  Remove the input
12709
     sections from the link, and set the contents of the output
12710
     section.  */
12711
0
  sections_removed = false;
12712
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12713
0
  const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12714
0
#endif
12715
0
  for (o = abfd->sections; o != NULL; o = o->next)
12716
0
    {
12717
0
      bool remove_section = false;
12718
12719
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12720
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12721
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12722
0
  {
12723
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12724
0
      {
12725
0
        asection *input_section;
12726
12727
0
        if (p->type != bfd_indirect_link_order)
12728
0
    continue;
12729
0
        input_section = p->u.indirect.section;
12730
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12731
     elf_link_input_bfd ignores this section.  */
12732
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12733
0
      }
12734
12735
    /* Skip this section later on.  */
12736
0
    o->map_head.link_order = NULL;
12737
12738
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
12739
    /* Once ELF headers have been written, the size of a section is
12740
       frozen. We need to set the size of the attribute section before
12741
       _bfd_elf_compute_section_file_positions.  */
12742
0
    bfd_set_section_size (o, attr_size);
12743
0
    if (attr_size > 0)
12744
0
      elf_obj_object_attributes (abfd) = o;
12745
0
    else
12746
0
      remove_section = true;
12747
0
  }
12748
0
      else
12749
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12750
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12751
0
  {
12752
    /* Remove empty group section from linker output.  */
12753
0
    remove_section = true;
12754
0
  }
12755
0
      if (remove_section)
12756
0
  {
12757
0
    o->flags |= SEC_EXCLUDE;
12758
0
    bfd_section_list_remove (abfd, o);
12759
0
    abfd->section_count--;
12760
0
    sections_removed = true;
12761
0
  }
12762
0
    }
12763
0
  if (sections_removed)
12764
0
    bfd_fix_excluded_sec_syms (info);
12765
12766
  /* Count up the number of relocations we will output for each output
12767
     section, so that we know the sizes of the reloc sections.  We
12768
     also figure out some maximum sizes.  */
12769
0
#ifdef USE_MMAP
12770
0
  if (bed->use_mmap)
12771
0
    {
12772
      /* Mmap is used only if section size >= the minimum mmap section
12773
   size.  The initial max_contents_size value covers all sections
12774
   smaller than the minimum mmap section size.  It may be increased
12775
   for compressed or linker created sections or sections whose
12776
   rawsize != size.  max_external_reloc_size covers all relocation
12777
   sections smaller than the minimum mmap section size.  */
12778
0
      max_contents_size = _bfd_minimum_mmap_size;
12779
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12780
0
    }
12781
0
  else
12782
0
#endif
12783
0
    {
12784
0
      max_contents_size = 0;
12785
0
      max_external_reloc_size = 0;
12786
0
    }
12787
0
  max_internal_reloc_count = 0;
12788
0
  max_sym_count = 0;
12789
0
  max_sym_shndx_count = 0;
12790
0
  merged = false;
12791
0
  for (o = abfd->sections; o != NULL; o = o->next)
12792
0
    {
12793
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12794
0
      bfd_size_type size_align = 1;
12795
12796
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12797
0
  {
12798
    /* eh_frame editing can extend last FDE to cover padding
12799
       between one section and the next.  */
12800
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12801
0
      * bfd_octets_per_byte (abfd, o));
12802
0
  }
12803
12804
0
      o->reloc_count = 0;
12805
12806
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12807
0
  {
12808
0
    unsigned int reloc_count = 0;
12809
0
    unsigned int additional_reloc_count = 0;
12810
0
    struct bfd_elf_section_data *esdi = NULL;
12811
12812
0
    if (p->type == bfd_section_reloc_link_order
12813
0
        || p->type == bfd_symbol_reloc_link_order)
12814
0
      reloc_count = 1;
12815
0
    else if (p->type == bfd_indirect_link_order)
12816
0
      {
12817
0
        asection *sec;
12818
12819
0
        sec = p->u.indirect.section;
12820
12821
        /* Mark all sections which are to be included in the
12822
     link.  This will normally be every section.  We need
12823
     to do this so that we can identify any sections which
12824
     the linker has decided to not include.  */
12825
0
        sec->linker_mark = true;
12826
12827
0
        if (sec->flags & SEC_MERGE)
12828
0
    merged = true;
12829
12830
0
#ifdef USE_MMAP
12831
        /* Mmap is used only on non-compressed, non-linker created
12832
     sections whose rawsize == size.  */
12833
0
        if (!bed->use_mmap
12834
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12835
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12836
0
      || sec->rawsize != sec->size)
12837
0
#endif
12838
0
    {
12839
0
      bfd_size_type size = (sec->rawsize > sec->size
12840
0
          ? sec->rawsize : sec->size);
12841
0
      size = (size + size_align - 1) & -size_align;
12842
0
      if (max_contents_size < size)
12843
0
        max_contents_size = size;
12844
0
    }
12845
12846
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12847
0
      && (sec->owner->flags & DYNAMIC) == 0)
12848
0
    {
12849
0
      size_t sym_count;
12850
12851
      /* We are interested in just local symbols, not all
12852
         symbols.  */
12853
0
      if (elf_bad_symtab (sec->owner))
12854
0
        sym_count = (elf_symtab_hdr (sec->owner).sh_size
12855
0
         / bed->s->sizeof_sym);
12856
0
      else
12857
0
        sym_count = elf_symtab_hdr (sec->owner).sh_info;
12858
12859
0
      if (sym_count > max_sym_count)
12860
0
        max_sym_count = sym_count;
12861
12862
0
      if (sym_count > max_sym_shndx_count
12863
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12864
0
        max_sym_shndx_count = sym_count;
12865
12866
0
      esdi = elf_section_data (sec);
12867
12868
0
      if (esdi->this_hdr.sh_type == SHT_REL
12869
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12870
        /* Some backends use reloc_count in relocation sections
12871
           to count particular types of relocs.  Of course,
12872
           reloc sections themselves can't have relocations.  */
12873
0
        ;
12874
0
      else if (emit_relocs)
12875
0
        {
12876
0
          reloc_count = sec->reloc_count;
12877
0
          if (bed->elf_backend_count_additional_relocs)
12878
0
      {
12879
0
        int c;
12880
0
        c = (*bed->elf_backend_count_additional_relocs) (sec);
12881
0
        additional_reloc_count += c;
12882
0
      }
12883
0
        }
12884
0
      else if (bed->elf_backend_count_relocs)
12885
0
        reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12886
12887
0
      if ((sec->flags & SEC_RELOC) != 0)
12888
0
        {
12889
0
#ifdef USE_MMAP
12890
0
          if (!bed->use_mmap)
12891
0
#endif
12892
0
      {
12893
0
        size_t ext_size = 0;
12894
12895
0
        if (esdi->rel.hdr != NULL)
12896
0
          ext_size = esdi->rel.hdr->sh_size;
12897
0
        if (esdi->rela.hdr != NULL)
12898
0
          ext_size += esdi->rela.hdr->sh_size;
12899
12900
0
        if (ext_size > max_external_reloc_size)
12901
0
          max_external_reloc_size = ext_size;
12902
0
      }
12903
0
          if (sec->reloc_count > max_internal_reloc_count)
12904
0
      max_internal_reloc_count = sec->reloc_count;
12905
0
        }
12906
0
    }
12907
0
      }
12908
12909
0
    if (reloc_count == 0)
12910
0
      continue;
12911
12912
0
    reloc_count += additional_reloc_count;
12913
0
    o->reloc_count += reloc_count;
12914
12915
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12916
0
      {
12917
0
        if (esdi->rel.hdr)
12918
0
    {
12919
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12920
0
      esdo->rel.count += additional_reloc_count;
12921
0
    }
12922
0
        if (esdi->rela.hdr)
12923
0
    {
12924
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12925
0
      esdo->rela.count += additional_reloc_count;
12926
0
    }
12927
0
      }
12928
0
    else
12929
0
      {
12930
0
        if (o->use_rela_p)
12931
0
    esdo->rela.count += reloc_count;
12932
0
        else
12933
0
    esdo->rel.count += reloc_count;
12934
0
      }
12935
0
  }
12936
12937
0
      if (o->reloc_count > 0)
12938
0
  o->flags |= SEC_RELOC;
12939
0
      else
12940
0
  {
12941
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12942
       set it (this is probably a bug) and if it is set
12943
       assign_section_numbers will create a reloc section.  */
12944
0
    o->flags &=~ SEC_RELOC;
12945
0
  }
12946
12947
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12948
   zero.  This is done in elf_fake_sections as well, but forcing
12949
   the VMA to 0 here will ensure that relocs against these
12950
   sections are handled correctly.  */
12951
0
      if ((o->flags & SEC_ALLOC) == 0
12952
0
    && ! o->user_set_vma)
12953
0
  o->vma = 0;
12954
0
    }
12955
12956
0
  if (! bfd_link_relocatable (info) && merged)
12957
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12958
12959
  /* Figure out the file positions for everything but the symbol table
12960
     and the relocs.  We set symcount to force assign_section_numbers
12961
     to create a symbol table.  */
12962
0
  abfd->symcount = info->strip != strip_all || emit_relocs;
12963
0
  BFD_ASSERT (! abfd->output_has_begun);
12964
0
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
12965
0
    goto error_return;
12966
12967
  /* Set sizes, and assign file positions for reloc sections.  */
12968
0
  for (o = abfd->sections; o != NULL; o = o->next)
12969
0
    {
12970
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12971
0
      if ((o->flags & SEC_RELOC) != 0)
12972
0
  {
12973
0
    if (esdo->rel.hdr
12974
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12975
0
      goto error_return;
12976
12977
0
    if (esdo->rela.hdr
12978
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12979
0
      goto error_return;
12980
0
  }
12981
12982
      /* _bfd_elf_compute_section_file_positions makes temporary use
12983
   of target_index.  Reset it.  */
12984
0
      o->target_index = 0;
12985
12986
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12987
   to count upwards while actually outputting the relocations.  */
12988
0
      esdo->rel.count = 0;
12989
0
      esdo->rela.count = 0;
12990
12991
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12992
0
    && !bfd_section_is_ctf (o))
12993
0
  {
12994
    /* Cache the section contents so that they can be compressed
12995
       later.  Use bfd_malloc since it will be freed by
12996
       bfd_compress_section_contents.  */
12997
0
    unsigned char *contents = esdo->this_hdr.contents;
12998
0
    if (contents != NULL)
12999
0
      abort ();
13000
0
    contents
13001
0
      = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
13002
0
    if (contents == NULL)
13003
0
      goto error_return;
13004
0
    esdo->this_hdr.contents = contents;
13005
0
  }
13006
0
    }
13007
13008
  /* We have now assigned file positions for all the sections except .symtab,
13009
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
13010
     the .symtab section at the current file position, and write directly to it.
13011
     We build the .strtab section in memory.  */
13012
0
  abfd->symcount = 0;
13013
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13014
  /* sh_name is set in prep_headers.  */
13015
0
  symtab_hdr->sh_type = SHT_SYMTAB;
13016
  /* sh_flags, sh_addr and sh_size all start off zero.  */
13017
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
13018
  /* sh_link is set in assign_section_numbers.  */
13019
  /* sh_info is set below.  */
13020
  /* sh_offset is set just below.  */
13021
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
13022
13023
0
  if (max_sym_count < 20)
13024
0
    max_sym_count = 20;
13025
0
  htab->strtabsize = max_sym_count;
13026
0
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
13027
0
  htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
13028
0
  if (htab->strtab == NULL)
13029
0
    goto error_return;
13030
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
13031
0
  flinfo.symshndxbuf
13032
0
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
13033
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
13034
13035
0
  if (info->strip != strip_all || emit_relocs)
13036
0
    {
13037
0
      file_ptr off = elf_next_file_pos (abfd);
13038
13039
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13040
13041
      /* Note that at this point elf_next_file_pos (abfd) is
13042
   incorrect.  We do not yet know the size of the .symtab section.
13043
   We correct next_file_pos below, after we do know the size.  */
13044
13045
      /* Start writing out the symbol table.  The first symbol is always a
13046
   dummy symbol.  */
13047
0
      elfsym.st_value = 0;
13048
0
      elfsym.st_size = 0;
13049
0
      elfsym.st_info = 0;
13050
0
      elfsym.st_other = 0;
13051
0
      elfsym.st_shndx = SHN_UNDEF;
13052
0
      elfsym.st_target_internal = 0;
13053
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13054
0
             bfd_und_section_ptr, NULL) != 1)
13055
0
  goto error_return;
13056
13057
      /* Output a symbol for each section if asked or they are used for
13058
   relocs.  These symbols usually have no names.  We store the
13059
   index of each one in the index field of the section, so that
13060
   we can find it again when outputting relocs.  */
13061
13062
0
      if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
13063
0
  {
13064
0
    bool name_local_sections
13065
0
      = (bed->elf_backend_name_local_section_symbols
13066
0
         && bed->elf_backend_name_local_section_symbols (abfd));
13067
0
    const char *name = NULL;
13068
13069
0
    elfsym.st_size = 0;
13070
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13071
0
    elfsym.st_other = 0;
13072
0
    elfsym.st_value = 0;
13073
0
    elfsym.st_target_internal = 0;
13074
0
    for (i = 1; i < elf_numsections (abfd); i++)
13075
0
      {
13076
0
        o = bfd_section_from_elf_index (abfd, i);
13077
0
        if (o != NULL)
13078
0
    {
13079
0
      o->target_index = bfd_get_symcount (abfd);
13080
0
      elfsym.st_shndx = i;
13081
0
      if (!bfd_link_relocatable (info))
13082
0
        elfsym.st_value = o->vma;
13083
0
      if (name_local_sections)
13084
0
        name = o->name;
13085
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13086
0
             NULL) != 1)
13087
0
        goto error_return;
13088
0
    }
13089
0
      }
13090
0
  }
13091
0
    }
13092
13093
  /* On some targets like Irix 5 the symbol split between local and global
13094
     ones recorded in the sh_info field needs to be done between section
13095
     and all other symbols.  */
13096
0
  if (bed->elf_backend_elfsym_local_is_section
13097
0
      && bed->elf_backend_elfsym_local_is_section (abfd))
13098
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13099
13100
  /* Allocate some memory to hold information read in from the input
13101
     files.  */
13102
0
  if (max_contents_size != 0)
13103
0
    {
13104
0
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
13105
0
      if (flinfo.contents == NULL)
13106
0
  goto error_return;
13107
0
    }
13108
13109
0
  if (max_external_reloc_size != 0)
13110
0
    {
13111
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13112
0
      if (flinfo.external_relocs == NULL)
13113
0
  goto error_return;
13114
0
    }
13115
13116
0
  if (max_internal_reloc_count != 0)
13117
0
    {
13118
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13119
0
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13120
0
      if (flinfo.internal_relocs == NULL)
13121
0
  goto error_return;
13122
0
    }
13123
13124
0
  if (max_sym_count != 0)
13125
0
    {
13126
0
      amt = max_sym_count * bed->s->sizeof_sym;
13127
0
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13128
0
      if (flinfo.external_syms == NULL)
13129
0
  goto error_return;
13130
13131
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13132
0
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13133
0
      if (flinfo.internal_syms == NULL)
13134
0
  goto error_return;
13135
13136
0
      amt = max_sym_count * sizeof (long);
13137
0
      flinfo.indices = (long int *) bfd_malloc (amt);
13138
0
      if (flinfo.indices == NULL)
13139
0
  goto error_return;
13140
13141
0
      amt = max_sym_count * sizeof (asection *);
13142
0
      flinfo.sections = (asection **) bfd_malloc (amt);
13143
0
      if (flinfo.sections == NULL)
13144
0
  goto error_return;
13145
0
    }
13146
13147
0
  if (max_sym_shndx_count != 0)
13148
0
    {
13149
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13150
0
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13151
0
      if (flinfo.locsym_shndx == NULL)
13152
0
  goto error_return;
13153
0
    }
13154
13155
0
  if (htab->tls_sec)
13156
0
    {
13157
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13158
0
      asection *sec;
13159
13160
0
      for (sec = htab->tls_sec;
13161
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13162
0
     sec = sec->next)
13163
0
  {
13164
0
    bfd_size_type size = sec->size;
13165
0
    unsigned int opb = bfd_octets_per_byte (abfd, sec);
13166
13167
0
    if (size == 0
13168
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13169
0
      {
13170
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13171
13172
0
        if (ord != NULL)
13173
0
    size = ord->offset * opb + ord->size;
13174
0
      }
13175
0
    end = sec->vma + size / opb;
13176
0
  }
13177
0
      base = htab->tls_sec->vma;
13178
      /* Only align end of TLS section if static TLS doesn't have special
13179
   alignment requirements.  */
13180
0
      if (bed->static_tls_alignment == 1)
13181
0
  end = align_power (end, htab->tls_sec->alignment_power);
13182
0
      htab->tls_size = end - base;
13183
0
    }
13184
13185
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13186
0
    return false;
13187
13188
  /* Finish relative relocations here after regular symbol processing
13189
     is finished if DT_RELR is enabled.  */
13190
0
  if (info->enable_dt_relr
13191
0
      && bed->finish_relative_relocs
13192
0
      && !bed->finish_relative_relocs (info))
13193
0
    info->callbacks->fatal
13194
0
      (_("%P: %pB: failed to finish relative relocations\n"), abfd);
13195
13196
  /* Since ELF permits relocations to be against local symbols, we
13197
     must have the local symbols available when we do the relocations.
13198
     Since we would rather only read the local symbols once, and we
13199
     would rather not keep them in memory, we handle all the
13200
     relocations for a single input file at the same time.
13201
13202
     Unfortunately, there is no way to know the total number of local
13203
     symbols until we have seen all of them, and the local symbol
13204
     indices precede the global symbol indices.  This means that when
13205
     we are generating relocatable output, and we see a reloc against
13206
     a global symbol, we can not know the symbol index until we have
13207
     finished examining all the local symbols to see which ones we are
13208
     going to output.  To deal with this, we keep the relocations in
13209
     memory, and don't output them until the end of the link.  This is
13210
     an unfortunate waste of memory, but I don't see a good way around
13211
     it.  Fortunately, it only happens when performing a relocatable
13212
     link, which is not the common case.  FIXME: If keep_memory is set
13213
     we could write the relocs out and then read them again; I don't
13214
     know how bad the memory loss will be.  */
13215
13216
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13217
0
    sub->output_has_begun = false;
13218
0
  for (o = abfd->sections; o != NULL; o = o->next)
13219
0
    {
13220
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13221
0
  {
13222
0
    if (p->type == bfd_indirect_link_order
13223
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13224
0
      == bfd_target_elf_flavour)
13225
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13226
0
      {
13227
0
        if (! sub->output_has_begun)
13228
0
    {
13229
0
      if (! elf_link_input_bfd (&flinfo, sub))
13230
0
        goto error_return;
13231
0
      sub->output_has_begun = true;
13232
0
    }
13233
0
      }
13234
0
    else if (p->type == bfd_section_reloc_link_order
13235
0
       || p->type == bfd_symbol_reloc_link_order)
13236
0
      {
13237
0
        if (! elf_reloc_link_order (abfd, info, o, p))
13238
0
    goto error_return;
13239
0
      }
13240
0
    else
13241
0
      {
13242
0
        if (! _bfd_default_link_order (abfd, info, o, p))
13243
0
    {
13244
0
      if (p->type == bfd_indirect_link_order
13245
0
          && (bfd_get_flavour (sub)
13246
0
        == bfd_target_elf_flavour)
13247
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13248
0
        != bed->s->elfclass))
13249
0
        {
13250
0
          const char *iclass, *oclass;
13251
13252
0
          switch (bed->s->elfclass)
13253
0
      {
13254
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13255
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13256
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13257
0
      default: abort ();
13258
0
      }
13259
13260
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13261
0
      {
13262
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13263
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13264
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13265
0
      default: abort ();
13266
0
      }
13267
13268
0
          bfd_set_error (bfd_error_wrong_format);
13269
0
          _bfd_error_handler
13270
      /* xgettext:c-format */
13271
0
      (_("%pB: file class %s incompatible with %s"),
13272
0
       sub, iclass, oclass);
13273
0
        }
13274
13275
0
      goto error_return;
13276
0
    }
13277
0
      }
13278
0
  }
13279
0
    }
13280
13281
  /* Free symbol buffer if needed.  */
13282
0
  if (!info->reduce_memory_overheads)
13283
0
    {
13284
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13285
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13286
0
    {
13287
0
      free (elf_tdata (sub)->symbuf);
13288
0
      elf_tdata (sub)->symbuf = NULL;
13289
0
    }
13290
0
    }
13291
13292
  /* Output any global symbols that got converted to local in a
13293
     version script or due to symbol visibility.  We do this in a
13294
     separate step since ELF requires all local symbols to appear
13295
     prior to any global symbols.  FIXME: We should only do this if
13296
     some global symbols were, in fact, converted to become local.
13297
     FIXME: Will this work correctly with the Irix 5 linker?  */
13298
0
  eoinfo.failed = false;
13299
0
  eoinfo.flinfo = &flinfo;
13300
0
  eoinfo.localsyms = true;
13301
0
  eoinfo.file_sym_done = false;
13302
  /* Output non-base symbols first.  */
13303
0
  eoinfo.base_symbol = false;
13304
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13305
0
  if (eoinfo.failed)
13306
0
    goto error_return;
13307
13308
  /* If backend needs to output some local symbols not present in the hash
13309
     table, do it now.  */
13310
0
  if (bed->elf_backend_output_arch_local_syms)
13311
0
    {
13312
0
      if (! ((*bed->elf_backend_output_arch_local_syms)
13313
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13314
0
  goto error_return;
13315
0
    }
13316
13317
  /* That wrote out all the local symbols.  Finish up the symbol table
13318
     with the global symbols. Even if we want to strip everything we
13319
     can, we still need to deal with those global symbols that got
13320
     converted to local in a version script.  */
13321
13322
  /* The sh_info field records the index of the first non local symbol.  */
13323
0
  if (!symtab_hdr->sh_info)
13324
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13325
13326
0
  if (dynamic
13327
0
      && htab->dynsym != NULL
13328
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13329
0
    {
13330
0
      Elf_Internal_Sym sym;
13331
0
      bfd_byte *dynsym = htab->dynsym->contents;
13332
13333
0
      o = htab->dynsym->output_section;
13334
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13335
13336
      /* Write out the section symbols for the output sections.  */
13337
0
      if (bfd_link_pic (info)
13338
0
    || htab->is_relocatable_executable)
13339
0
  {
13340
0
    asection *s;
13341
13342
0
    sym.st_size = 0;
13343
0
    sym.st_name = 0;
13344
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13345
0
    sym.st_other = 0;
13346
0
    sym.st_target_internal = 0;
13347
13348
0
    for (s = abfd->sections; s != NULL; s = s->next)
13349
0
      {
13350
0
        int indx;
13351
0
        bfd_byte *dest;
13352
0
        long dynindx;
13353
13354
0
        dynindx = elf_section_data (s)->dynindx;
13355
0
        if (dynindx <= 0)
13356
0
    continue;
13357
0
        indx = elf_section_data (s)->this_idx;
13358
0
        BFD_ASSERT (indx > 0);
13359
0
        sym.st_shndx = indx;
13360
0
        if (! check_dynsym (abfd, &sym))
13361
0
    goto error_return;
13362
0
        sym.st_value = s->vma;
13363
0
        dest = dynsym + dynindx * bed->s->sizeof_sym;
13364
13365
        /* Inform the linker of the addition of this symbol.  */
13366
13367
0
        if (info->callbacks->ctf_new_dynsym)
13368
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13369
13370
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13371
0
      }
13372
0
  }
13373
13374
      /* Write out the local dynsyms.  */
13375
0
      if (htab->dynlocal)
13376
0
  {
13377
0
    struct elf_link_local_dynamic_entry *e;
13378
0
    for (e = htab->dynlocal; e ; e = e->next)
13379
0
      {
13380
0
        asection *s;
13381
0
        bfd_byte *dest;
13382
13383
        /* Copy the internal symbol and turn off visibility.
13384
     Note that we saved a word of storage and overwrote
13385
     the original st_name with the dynstr_index.  */
13386
0
        sym = e->isym;
13387
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13388
0
        sym.st_shndx = SHN_UNDEF;
13389
13390
0
        s = bfd_section_from_elf_index (e->input_bfd,
13391
0
                e->isym.st_shndx);
13392
0
        if (s != NULL
13393
0
      && s->output_section != NULL
13394
0
      && elf_section_data (s->output_section) != NULL)
13395
0
    {
13396
0
      sym.st_shndx =
13397
0
        elf_section_data (s->output_section)->this_idx;
13398
0
      if (! check_dynsym (abfd, &sym))
13399
0
        goto error_return;
13400
0
      sym.st_value = (s->output_section->vma
13401
0
          + s->output_offset
13402
0
          + e->isym.st_value);
13403
0
    }
13404
13405
        /* Inform the linker of the addition of this symbol.  */
13406
13407
0
        if (info->callbacks->ctf_new_dynsym)
13408
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13409
13410
0
        dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13411
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13412
0
      }
13413
0
  }
13414
0
    }
13415
13416
  /* We get the global symbols from the hash table.  */
13417
0
  eoinfo.failed = false;
13418
0
  eoinfo.localsyms = false;
13419
0
  eoinfo.flinfo = &flinfo;
13420
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13421
0
  if (eoinfo.failed)
13422
0
    goto error_return;
13423
13424
0
  if (htab->has_base_symbols)
13425
0
    {
13426
      /* Output base symbols last in DT_HASH so that they will be picked
13427
   before non-base symbols at run-time.  */
13428
0
      eoinfo.base_symbol = true;
13429
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13430
0
       &eoinfo);
13431
0
      if (eoinfo.failed)
13432
0
  goto error_return;
13433
0
    }
13434
13435
  /* If backend needs to output some symbols not present in the hash
13436
     table, do it now.  */
13437
0
  if (bed->elf_backend_output_arch_syms
13438
0
      && (info->strip != strip_all || emit_relocs))
13439
0
    {
13440
0
      if (! ((*bed->elf_backend_output_arch_syms)
13441
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13442
0
  goto error_return;
13443
0
    }
13444
13445
  /* Finalize the .strtab section.  */
13446
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13447
13448
  /* Swap out the .strtab section. */
13449
0
  if (!elf_link_swap_symbols_out (&flinfo))
13450
0
    goto error_return;
13451
0
  free (htab->strtab);
13452
0
  htab->strtab = NULL;
13453
13454
  /* Now we know the size of the symtab section.  */
13455
0
  if (bfd_get_symcount (abfd) > 0)
13456
0
    {
13457
      /* Finish up and write out the symbol string table (.strtab)
13458
   section.  */
13459
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13460
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13461
13462
0
      if (elf_symtab_shndx_list (abfd))
13463
0
  {
13464
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13465
13466
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13467
0
      {
13468
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13469
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13470
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13471
0
        amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13472
0
        symtab_shndx_hdr->sh_size = amt;
13473
13474
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13475
0
                     off, true, 0);
13476
13477
0
        if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13478
0
      || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13479
0
    goto error_return;
13480
0
      }
13481
0
  }
13482
13483
0
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13484
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13485
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13486
0
      symstrtab_hdr->sh_addralign = 1;
13487
13488
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13489
0
                   off, true, 0);
13490
0
      elf_next_file_pos (abfd) = off;
13491
13492
0
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13493
0
    || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13494
0
  goto error_return;
13495
0
    }
13496
13497
0
  if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13498
0
    {
13499
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13500
0
        info->out_implib_bfd);
13501
0
      goto error_return;
13502
0
    }
13503
13504
  /* Adjust the relocs to have the correct symbol indices.  */
13505
0
  for (o = abfd->sections; o != NULL; o = o->next)
13506
0
    {
13507
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13508
0
      bool sort;
13509
13510
0
      if ((o->flags & SEC_RELOC) == 0)
13511
0
  continue;
13512
13513
0
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13514
0
      if (esdo->rel.hdr != NULL
13515
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13516
0
  goto error_return;
13517
0
      if (esdo->rela.hdr != NULL
13518
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13519
0
  goto error_return;
13520
13521
      /* Set the reloc_count field to 0 to prevent write_relocs from
13522
   trying to swap the relocs out itself.  */
13523
0
      o->reloc_count = 0;
13524
0
    }
13525
13526
0
  relativecount = 0;
13527
0
  if (dynamic && info->combreloc && dynobj != NULL)
13528
0
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13529
13530
0
  relr_entsize = 0;
13531
0
  if (htab->srelrdyn != NULL
13532
0
      && htab->srelrdyn->output_section != NULL
13533
0
      && htab->srelrdyn->size != 0)
13534
0
    {
13535
0
      asection *s = htab->srelrdyn->output_section;
13536
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13537
0
      if (relr_entsize == 0)
13538
0
  {
13539
0
    relr_entsize = bed->s->arch_size / 8;
13540
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13541
0
  }
13542
0
    }
13543
13544
  /* If we are linking against a dynamic object, or generating a
13545
     shared library, finish up the dynamic linking information.  */
13546
0
  if (dynamic)
13547
0
    {
13548
0
      bfd_byte *dyncon, *dynconend;
13549
13550
      /* Fix up .dynamic entries.  */
13551
0
      o = htab->dynamic;
13552
0
      BFD_ASSERT (o != NULL);
13553
13554
0
      dyncon = o->contents;
13555
0
      dynconend = PTR_ADD (o->contents, o->size);
13556
0
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13557
0
  {
13558
0
    Elf_Internal_Dyn dyn;
13559
0
    const char *name;
13560
0
    unsigned int type;
13561
0
    bfd_size_type sh_size;
13562
0
    bfd_vma sh_addr;
13563
13564
0
    bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13565
13566
0
    switch (dyn.d_tag)
13567
0
      {
13568
0
      default:
13569
0
        continue;
13570
0
      case DT_NULL:
13571
0
        if (relativecount != 0)
13572
0
    {
13573
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13574
0
        {
13575
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13576
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13577
0
        }
13578
0
      if (dyn.d_tag != DT_NULL
13579
0
          && dynconend - dyncon >= bed->s->sizeof_dyn)
13580
0
        {
13581
0
          dyn.d_un.d_val = relativecount;
13582
0
          relativecount = 0;
13583
0
          break;
13584
0
        }
13585
0
      relativecount = 0;
13586
0
    }
13587
0
        if (relr_entsize != 0)
13588
0
    {
13589
0
      if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13590
0
        {
13591
0
          asection *s = htab->srelrdyn;
13592
0
          dyn.d_tag = DT_RELR;
13593
0
          dyn.d_un.d_ptr
13594
0
      = s->output_section->vma + s->output_offset;
13595
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13596
0
          dyncon += bed->s->sizeof_dyn;
13597
13598
0
          dyn.d_tag = DT_RELRSZ;
13599
0
          dyn.d_un.d_val = s->size;
13600
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13601
0
          dyncon += bed->s->sizeof_dyn;
13602
13603
0
          dyn.d_tag = DT_RELRENT;
13604
0
          dyn.d_un.d_val = relr_entsize;
13605
0
          relr_entsize = 0;
13606
0
          break;
13607
0
        }
13608
0
      relr_entsize = 0;
13609
0
    }
13610
0
        continue;
13611
13612
0
      case DT_INIT:
13613
0
        name = info->init_function;
13614
0
        goto get_sym;
13615
0
      case DT_FINI:
13616
0
        name = info->fini_function;
13617
0
      get_sym:
13618
0
        {
13619
0
    struct elf_link_hash_entry *h;
13620
13621
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13622
0
    if (h != NULL
13623
0
        && (h->root.type == bfd_link_hash_defined
13624
0
      || h->root.type == bfd_link_hash_defweak))
13625
0
      {
13626
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13627
0
        o = h->root.u.def.section;
13628
0
        if (o->output_section != NULL)
13629
0
          dyn.d_un.d_ptr += (o->output_section->vma
13630
0
           + o->output_offset);
13631
0
        else
13632
0
          {
13633
      /* The symbol is imported from another shared
13634
         library and does not apply to this one.  */
13635
0
      dyn.d_un.d_ptr = 0;
13636
0
          }
13637
0
        break;
13638
0
      }
13639
0
        }
13640
0
        continue;
13641
13642
0
      case DT_PREINIT_ARRAYSZ:
13643
0
        name = ".preinit_array";
13644
0
        goto get_out_size;
13645
0
      case DT_INIT_ARRAYSZ:
13646
0
        name = ".init_array";
13647
0
        goto get_out_size;
13648
0
      case DT_FINI_ARRAYSZ:
13649
0
        name = ".fini_array";
13650
0
      get_out_size:
13651
0
        o = bfd_get_section_by_name (abfd, name);
13652
0
        if (o == NULL)
13653
0
    {
13654
0
      _bfd_error_handler
13655
0
        (_("could not find section %s"), name);
13656
0
      goto error_return;
13657
0
    }
13658
0
        if (o->size == 0)
13659
0
    _bfd_error_handler
13660
0
      (_("warning: %s section has zero size"), name);
13661
0
        dyn.d_un.d_val = o->size;
13662
0
        break;
13663
13664
0
      case DT_PREINIT_ARRAY:
13665
0
        name = ".preinit_array";
13666
0
        goto get_out_vma;
13667
0
      case DT_INIT_ARRAY:
13668
0
        name = ".init_array";
13669
0
        goto get_out_vma;
13670
0
      case DT_FINI_ARRAY:
13671
0
        name = ".fini_array";
13672
0
      get_out_vma:
13673
0
        o = bfd_get_section_by_name (abfd, name);
13674
0
        goto do_vma;
13675
13676
0
      case DT_HASH:
13677
0
        name = ".hash";
13678
0
        goto get_vma;
13679
0
      case DT_GNU_HASH:
13680
0
        name = ".gnu.hash";
13681
0
        goto get_vma;
13682
0
      case DT_STRTAB:
13683
0
        name = ".dynstr";
13684
0
        goto get_vma;
13685
0
      case DT_SYMTAB:
13686
0
        name = ".dynsym";
13687
0
        goto get_vma;
13688
0
      case DT_VERDEF:
13689
0
        name = ".gnu.version_d";
13690
0
        goto get_vma;
13691
0
      case DT_VERNEED:
13692
0
        name = ".gnu.version_r";
13693
0
        goto get_vma;
13694
0
      case DT_VERSYM:
13695
0
        name = ".gnu.version";
13696
0
      get_vma:
13697
0
        o = bfd_get_linker_section (dynobj, name);
13698
0
      do_vma:
13699
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13700
0
    {
13701
0
      _bfd_error_handler
13702
0
        (_("could not find section %s"), name);
13703
0
      goto error_return;
13704
0
    }
13705
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13706
0
    {
13707
0
      _bfd_error_handler
13708
0
        (_("warning: section '%s' is being made into a note"), name);
13709
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13710
0
      goto error_return;
13711
0
    }
13712
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13713
0
        break;
13714
13715
0
      case DT_REL:
13716
0
      case DT_RELA:
13717
0
      case DT_RELSZ:
13718
0
      case DT_RELASZ:
13719
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13720
0
    type = SHT_REL;
13721
0
        else
13722
0
    type = SHT_RELA;
13723
0
        sh_size = 0;
13724
0
        sh_addr = 0;
13725
0
        for (i = 1; i < elf_numsections (abfd); i++)
13726
0
    {
13727
0
      Elf_Internal_Shdr *hdr;
13728
13729
0
      hdr = elf_elfsections (abfd)[i];
13730
0
      if (hdr->sh_type == type
13731
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13732
0
        {
13733
0
          sh_size += hdr->sh_size;
13734
0
          if (sh_addr == 0
13735
0
        || sh_addr > hdr->sh_addr)
13736
0
      sh_addr = hdr->sh_addr;
13737
0
        }
13738
0
    }
13739
13740
0
        if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13741
0
    {
13742
0
      unsigned int opb = bfd_octets_per_byte (abfd, o);
13743
13744
      /* Don't count procedure linkage table relocs in the
13745
         overall reloc count.  */
13746
0
      sh_size -= htab->srelplt->size;
13747
0
      if (sh_size == 0)
13748
        /* If the size is zero, make the address zero too.
13749
           This is to avoid a glibc bug.  If the backend
13750
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13751
           zero, then we'll put DT_RELA at the end of
13752
           DT_JMPREL.  glibc will interpret the end of
13753
           DT_RELA matching the end of DT_JMPREL as the
13754
           case where DT_RELA includes DT_JMPREL, and for
13755
           LD_BIND_NOW will decide that processing DT_RELA
13756
           will process the PLT relocs too.  Net result:
13757
           No PLT relocs applied.  */
13758
0
        sh_addr = 0;
13759
13760
      /* If .rela.plt is the first .rela section, exclude
13761
         it from DT_RELA.  */
13762
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13763
0
               + htab->srelplt->output_offset) * opb)
13764
0
        sh_addr += htab->srelplt->size;
13765
0
    }
13766
13767
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13768
0
    dyn.d_un.d_val = sh_size;
13769
0
        else
13770
0
    dyn.d_un.d_ptr = sh_addr;
13771
0
        break;
13772
0
      }
13773
0
    bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13774
0
  }
13775
0
    }
13776
13777
  /* If we have created any dynamic sections, then output them.  */
13778
0
  if (dynobj != NULL)
13779
0
    {
13780
0
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info,
13781
0
               flinfo.contents))
13782
0
  goto error_return;
13783
13784
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13785
0
      if (bfd_link_textrel_check (info)
13786
0
    && (o = htab->dynamic) != NULL
13787
0
    && o->size != 0)
13788
0
  {
13789
0
    bfd_byte *dyncon, *dynconend;
13790
13791
0
    dyncon = o->contents;
13792
0
    dynconend = o->contents + o->size;
13793
0
    for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13794
0
      {
13795
0
        Elf_Internal_Dyn dyn;
13796
13797
0
        bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13798
13799
0
        if (dyn.d_tag == DT_TEXTREL)
13800
0
    {
13801
0
      if (info->textrel_check == textrel_check_error)
13802
0
        info->callbacks->einfo
13803
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13804
0
      else if (bfd_link_dll (info))
13805
0
        info->callbacks->einfo
13806
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13807
0
      else if (bfd_link_pde (info))
13808
0
        info->callbacks->einfo
13809
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13810
0
      else
13811
0
        info->callbacks->einfo
13812
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13813
0
      break;
13814
0
    }
13815
0
      }
13816
0
  }
13817
13818
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13819
0
  {
13820
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13821
0
        || o->size == 0
13822
0
        || o->output_section == bfd_abs_section_ptr)
13823
0
      continue;
13824
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13825
0
      {
13826
        /* At this point, we are only interested in sections
13827
     created by bfd_elf_link_create_dynamic_sections().  */
13828
0
        continue;
13829
0
      }
13830
0
    if (htab->stab_info.stabstr == o)
13831
0
      continue;
13832
0
    if (htab->eh_info.hdr_sec == o)
13833
0
      continue;
13834
0
    if (strcmp (o->name, ".dynstr") != 0)
13835
0
      {
13836
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13837
0
              * bfd_octets_per_byte (abfd, o));
13838
0
        if (!bfd_set_section_contents (abfd, o->output_section,
13839
0
               o->contents, octets, o->size))
13840
0
    goto error_return;
13841
0
      }
13842
0
    else
13843
0
      {
13844
        /* The contents of the .dynstr section are actually in a
13845
     stringtab.  */
13846
0
        file_ptr off;
13847
13848
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13849
0
        if (bfd_seek (abfd, off, SEEK_SET) != 0
13850
0
      || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13851
0
    goto error_return;
13852
0
      }
13853
0
  }
13854
0
    }
13855
13856
0
  if (!info->resolve_section_groups)
13857
0
    {
13858
0
      bool failed = false;
13859
13860
0
      BFD_ASSERT (bfd_link_relocatable (info));
13861
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13862
0
      if (failed)
13863
0
  goto error_return;
13864
0
    }
13865
13866
  /* If we have optimized stabs strings, output them.  */
13867
0
  if (htab->stab_info.stabstr != NULL)
13868
0
    {
13869
0
      if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13870
0
  goto error_return;
13871
0
    }
13872
13873
0
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13874
0
    goto error_return;
13875
13876
0
  if (! _bfd_elf_write_section_sframe (abfd, info))
13877
0
    goto error_return;
13878
13879
0
  if (! _bfd_elf_write_section_object_attributes (abfd, info))
13880
0
    goto error_ret2;
13881
13882
0
  if (info->callbacks->emit_ctf)
13883
0
      info->callbacks->emit_ctf ();
13884
13885
0
  elf_final_link_free (abfd, &flinfo);
13886
13887
0
  if (info->unique_symbol)
13888
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13889
0
  return true;
13890
13891
0
 error_return:
13892
0
  free (htab->strtab);
13893
0
  htab->strtab = NULL;
13894
0
  elf_final_link_free (abfd, &flinfo);
13895
0
 error_ret2:
13896
0
  if (info->unique_symbol)
13897
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13898
0
  return false;
13899
0
}
13900

13901
/* Initialize COOKIE for input bfd ABFD.  */
13902
13903
static bool
13904
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13905
0
{
13906
0
  Elf_Internal_Shdr *symtab_hdr;
13907
0
  elf_backend_data *bed;
13908
13909
0
  bed = get_elf_backend_data (abfd);
13910
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13911
13912
0
  cookie->abfd = abfd;
13913
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13914
0
  if (elf_bad_symtab (abfd))
13915
0
    {
13916
0
      cookie->locsymcount = cookie->num_sym;
13917
0
      cookie->extsymoff = 0;
13918
0
    }
13919
0
  else
13920
0
    {
13921
0
      cookie->locsymcount = symtab_hdr->sh_info;
13922
0
      cookie->extsymoff = symtab_hdr->sh_info;
13923
0
    }
13924
13925
0
  if (bed->s->arch_size == 32)
13926
0
    cookie->r_sym_shift = 8;
13927
0
  else
13928
0
    cookie->r_sym_shift = 32;
13929
13930
0
  return true;
13931
0
}
13932
13933
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13934
13935
static void
13936
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13937
       bfd *abfd ATTRIBUTE_UNUSED)
13938
0
{
13939
0
}
13940
13941
/* Initialize the relocation information in COOKIE for input section SEC
13942
   of input bfd ABFD.  */
13943
13944
static bool
13945
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13946
      struct bfd_link_info *info, bfd *abfd,
13947
      asection *sec, bool keep_memory)
13948
0
{
13949
0
  if (sec->reloc_count == 0)
13950
0
    {
13951
0
      cookie->rels = NULL;
13952
0
      cookie->relend = NULL;
13953
0
    }
13954
0
  else
13955
0
    {
13956
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13957
0
  (abfd, info, sec, NULL, NULL,
13958
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13959
0
      if (cookie->rels == NULL)
13960
0
  return false;
13961
0
      cookie->rel = cookie->rels;
13962
0
      cookie->relend = cookie->rels + sec->reloc_count;
13963
0
    }
13964
0
  cookie->rel = cookie->rels;
13965
0
  return true;
13966
0
}
13967
13968
/* Free the memory allocated by init_reloc_cookie_rels,
13969
   if appropriate.  */
13970
13971
static void
13972
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13973
      asection *sec)
13974
0
{
13975
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13976
0
    free (cookie->rels);
13977
0
}
13978
13979
/* Initialize the whole of COOKIE for input section SEC.  */
13980
13981
static bool
13982
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13983
             struct bfd_link_info *info,
13984
             asection *sec, bool keep_memory)
13985
0
{
13986
0
  if (!init_reloc_cookie (cookie, sec->owner))
13987
0
    goto error1;
13988
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13989
0
             keep_memory))
13990
0
    goto error2;
13991
0
  return true;
13992
13993
0
 error2:
13994
0
  fini_reloc_cookie (cookie, sec->owner);
13995
0
 error1:
13996
0
  return false;
13997
0
}
13998
13999
/* Free the memory allocated by init_reloc_cookie_for_section,
14000
   if appropriate.  */
14001
14002
static void
14003
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14004
             asection *sec)
14005
0
{
14006
0
  fini_reloc_cookie_rels (cookie, sec);
14007
0
  fini_reloc_cookie (cookie, sec->owner);
14008
0
}
14009

14010
/* Garbage collect unused sections.  */
14011
14012
/* Default gc_mark_hook.  */
14013
14014
asection *
14015
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
14016
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
14017
           struct elf_reloc_cookie *cookie,
14018
           struct elf_link_hash_entry *h,
14019
           unsigned int symndx)
14020
0
{
14021
0
  if (h == NULL)
14022
0
    return _bfd_get_local_sym_section (cookie, symndx);
14023
14024
0
  switch (h->root.type)
14025
0
    {
14026
0
    case bfd_link_hash_defined:
14027
0
    case bfd_link_hash_defweak:
14028
0
      return h->root.u.def.section;
14029
14030
0
    case bfd_link_hash_common:
14031
0
      return h->root.u.c.p->section;
14032
14033
0
    default:
14034
0
      return NULL;
14035
0
    }
14036
0
}
14037
14038
/* Return the debug definition section.  */
14039
14040
static asection *
14041
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14042
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14043
         struct elf_reloc_cookie *cookie,
14044
         struct elf_link_hash_entry *h,
14045
         unsigned int symndx)
14046
0
{
14047
0
  if (h != NULL)
14048
0
    {
14049
      /* Return the global debug definition section.  */
14050
0
      if ((h->root.type == bfd_link_hash_defined
14051
0
     || h->root.type == bfd_link_hash_defweak)
14052
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14053
0
  return h->root.u.def.section;
14054
0
    }
14055
0
  else
14056
0
    {
14057
      /* Return the local debug definition section.  */
14058
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14059
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14060
0
  return isec;
14061
0
    }
14062
14063
0
  return NULL;
14064
0
}
14065
14066
/* COOKIE->rel describes a relocation against section SEC, which is
14067
   a section we've decided to keep.  Return the section that contains
14068
   the relocation symbol, or NULL if no section contains it.  */
14069
14070
asection *
14071
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14072
           elf_gc_mark_hook_fn gc_mark_hook,
14073
           struct elf_reloc_cookie *cookie,
14074
           bool *start_stop)
14075
0
{
14076
0
  unsigned long r_symndx;
14077
0
  struct elf_link_hash_entry *h, *hw;
14078
14079
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14080
0
  if (r_symndx == STN_UNDEF)
14081
0
    return NULL;
14082
14083
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14084
0
  if (h == NULL)
14085
0
    {
14086
      /* A corrupt input file can lead to a situation where the index
14087
   does not reference either a local or an external symbol.  */
14088
0
      if (r_symndx >= cookie->locsymcount)
14089
0
  return NULL;
14090
14091
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14092
0
    }
14093
14094
0
  bool was_marked = h->mark;
14095
14096
0
  h->mark = 1;
14097
  /* Keep all aliases of the symbol too.  If an object symbol
14098
     needs to be copied into .dynbss then all of its aliases
14099
     should be present as dynamic symbols, not just the one used
14100
     on the copy relocation.  */
14101
0
  hw = h;
14102
0
  while (hw->is_weakalias)
14103
0
    {
14104
0
      hw = hw->u.alias;
14105
0
      hw->mark = 1;
14106
0
    }
14107
14108
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14109
0
    {
14110
0
      if (info->start_stop_gc)
14111
0
  return NULL;
14112
14113
      /* To work around a glibc bug, mark XXX input sections
14114
   when there is a reference to __start_XXX or __stop_XXX
14115
   symbols.  */
14116
0
      else if (start_stop != NULL)
14117
0
  {
14118
0
    asection *s = h->u2.start_stop_section;
14119
0
    *start_stop = true;
14120
0
    return s;
14121
0
  }
14122
0
    }
14123
14124
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14125
0
}
14126
14127
/* COOKIE->rel describes a relocation against section SEC, which is
14128
   a section we've decided to keep.  Mark the section that contains
14129
   the relocation symbol.  */
14130
14131
bool
14132
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14133
      asection *sec,
14134
      elf_gc_mark_hook_fn gc_mark_hook,
14135
      struct elf_reloc_cookie *cookie)
14136
0
{
14137
0
  asection *rsec;
14138
0
  bool start_stop = false;
14139
14140
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14141
0
  while (rsec != NULL)
14142
0
    {
14143
0
      if (!rsec->gc_mark)
14144
0
  {
14145
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14146
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14147
0
      rsec->gc_mark = 1;
14148
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14149
0
      return false;
14150
0
  }
14151
0
      if (!start_stop)
14152
0
  break;
14153
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14154
0
    }
14155
0
  return true;
14156
0
}
14157
14158
/* The mark phase of garbage collection.  For a given section, mark
14159
   it and any sections in this section's group, and all the sections
14160
   which define symbols to which it refers.  */
14161
14162
bool
14163
_bfd_elf_gc_mark (struct bfd_link_info *info,
14164
      asection *sec,
14165
      elf_gc_mark_hook_fn gc_mark_hook)
14166
0
{
14167
0
  bool ret;
14168
0
  asection *group_sec, *eh_frame, *sframe;
14169
14170
0
  sec->gc_mark = 1;
14171
14172
  /* Mark all the sections in the group.  */
14173
0
  group_sec = elf_section_data (sec)->next_in_group;
14174
0
  if (group_sec && !group_sec->gc_mark)
14175
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14176
0
      return false;
14177
14178
  /* Look through the section relocs.  */
14179
0
  ret = true;
14180
0
  eh_frame = elf_eh_frame_section (sec->owner);
14181
0
  sframe = elf_sframe_section (sec->owner);
14182
14183
0
  if ((sec->flags & SEC_RELOC) != 0
14184
0
      && sec->reloc_count > 0
14185
0
      && sec != eh_frame
14186
0
      && sec != sframe)
14187
0
    {
14188
0
      struct elf_reloc_cookie cookie;
14189
14190
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14191
0
  ret = false;
14192
0
      else
14193
0
  {
14194
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14195
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14196
0
        {
14197
0
    ret = false;
14198
0
    break;
14199
0
        }
14200
0
    fini_reloc_cookie_for_section (&cookie, sec);
14201
0
  }
14202
0
    }
14203
14204
0
  if (ret && eh_frame && elf_fde_list (sec))
14205
0
    {
14206
0
      struct elf_reloc_cookie cookie;
14207
14208
      /* NB: When --no-keep-memory is used, the symbol table and
14209
   relocation info for eh_frame are freed after they are retrieved
14210
   for each text section in the input object.  If an input object
14211
   has many text sections, the same data is retrieved and freed
14212
   many times which can take a very long time.  Always keep the
14213
   symbol table and relocation info for eh_frame to avoid it.  */
14214
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14215
0
            true))
14216
0
  ret = false;
14217
0
      else
14218
0
  {
14219
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14220
0
              gc_mark_hook, &cookie))
14221
0
      ret = false;
14222
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14223
0
  }
14224
0
    }
14225
14226
0
  eh_frame = elf_section_eh_frame_entry (sec);
14227
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14228
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14229
0
      ret = false;
14230
14231
0
  return ret;
14232
0
}
14233
14234
/* Scan and mark sections in a special or debug section group.  */
14235
14236
static void
14237
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14238
0
{
14239
  /* Point to first section of section group.  */
14240
0
  asection *ssec;
14241
  /* Used to iterate the section group.  */
14242
0
  asection *msec;
14243
14244
0
  bool is_special_grp = true;
14245
0
  bool is_debug_grp = true;
14246
14247
  /* First scan to see if group contains any section other than debug
14248
     and special section.  */
14249
0
  ssec = msec = elf_next_in_group (grp);
14250
0
  while (msec != NULL)
14251
0
    {
14252
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14253
0
  is_debug_grp = false;
14254
14255
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14256
0
  is_special_grp = false;
14257
14258
0
      msec = elf_next_in_group (msec);
14259
0
      if (msec == ssec)
14260
0
  break;
14261
0
    }
14262
14263
  /* If this is a pure debug section group or pure special section group,
14264
     keep all sections in this group.  */
14265
0
  if (is_debug_grp || is_special_grp)
14266
0
    {
14267
0
      msec = ssec;
14268
0
      while (msec != NULL)
14269
0
  {
14270
0
    msec->gc_mark = 1;
14271
0
    msec = elf_next_in_group (msec);
14272
0
    if (msec == ssec)
14273
0
      break;
14274
0
  }
14275
0
    }
14276
0
}
14277
14278
/* Keep debug and special sections.  */
14279
14280
bool
14281
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14282
         elf_gc_mark_hook_fn mark_hook)
14283
0
{
14284
0
  bfd *ibfd;
14285
14286
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14287
0
    {
14288
0
      asection *isec;
14289
0
      bool some_kept;
14290
0
      bool debug_frag_seen;
14291
0
      bool has_kept_debug_info;
14292
14293
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14294
0
  continue;
14295
0
      isec = ibfd->sections;
14296
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14297
0
  continue;
14298
14299
      /* Ensure all linker created sections are kept,
14300
   see if any other section is already marked,
14301
   and note if we have any fragmented debug sections.  */
14302
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14303
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14304
0
  {
14305
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14306
0
      isec->gc_mark = 1;
14307
0
    else if (isec->gc_mark
14308
0
       && (isec->flags & SEC_ALLOC) != 0
14309
0
       && elf_section_type (isec) != SHT_NOTE)
14310
0
      some_kept = true;
14311
0
    else
14312
0
      {
14313
        /* Since all sections, except for backend specific ones,
14314
     have been garbage collected, call mark_hook on this
14315
     section if any of its linked-to sections is marked.  */
14316
0
        asection *linked_to_sec;
14317
0
        for (linked_to_sec = elf_linked_to_section (isec);
14318
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14319
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14320
0
    {
14321
0
      if (linked_to_sec->gc_mark)
14322
0
        {
14323
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14324
0
      return false;
14325
0
          break;
14326
0
        }
14327
0
      linked_to_sec->linker_mark = 1;
14328
0
    }
14329
0
        for (linked_to_sec = elf_linked_to_section (isec);
14330
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14331
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14332
0
    linked_to_sec->linker_mark = 0;
14333
0
      }
14334
14335
0
    if (!debug_frag_seen
14336
0
        && (isec->flags & SEC_DEBUGGING)
14337
0
        && startswith (isec->name, ".debug_line."))
14338
0
      debug_frag_seen = true;
14339
0
    else if (strcmp (bfd_section_name (isec),
14340
0
         "__patchable_function_entries") == 0
14341
0
       && elf_linked_to_section (isec) == NULL)
14342
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14343
0
          "need linked-to section "
14344
0
          "for --gc-sections\n"),
14345
0
              isec->owner, isec);
14346
0
  }
14347
14348
      /* If no non-note alloc section in this file will be kept, then
14349
   we can toss out the debug and special sections.  */
14350
0
      if (!some_kept)
14351
0
  continue;
14352
14353
      /* Keep debug and special sections like .comment when they are
14354
   not part of a group.  Also keep section groups that contain
14355
   just debug sections or special sections.  NB: Sections with
14356
   linked-to section has been handled above.  */
14357
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14358
0
  {
14359
0
    if ((isec->flags & SEC_GROUP) != 0)
14360
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14361
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14362
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14363
0
       && elf_next_in_group (isec) == NULL
14364
0
       && elf_linked_to_section (isec) == NULL)
14365
0
      isec->gc_mark = 1;
14366
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14367
0
      has_kept_debug_info = true;
14368
0
  }
14369
14370
      /* Look for CODE sections which are going to be discarded,
14371
   and find and discard any fragmented debug sections which
14372
   are associated with that code section.  */
14373
0
      if (debug_frag_seen)
14374
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14375
0
    if ((isec->flags & SEC_CODE) != 0
14376
0
        && isec->gc_mark == 0)
14377
0
      {
14378
0
        unsigned int ilen;
14379
0
        asection *dsec;
14380
14381
0
        ilen = strlen (isec->name);
14382
14383
        /* Association is determined by the name of the debug
14384
     section containing the name of the code section as
14385
     a suffix.  For example .debug_line.text.foo is a
14386
     debug section associated with .text.foo.  */
14387
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14388
0
    {
14389
0
      unsigned int dlen;
14390
14391
0
      if (dsec->gc_mark == 0
14392
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14393
0
        continue;
14394
14395
0
      dlen = strlen (dsec->name);
14396
14397
0
      if (dlen > ilen
14398
0
          && strncmp (dsec->name + (dlen - ilen),
14399
0
          isec->name, ilen) == 0)
14400
0
        dsec->gc_mark = 0;
14401
0
    }
14402
0
    }
14403
14404
      /* Mark debug sections referenced by kept debug sections.  */
14405
0
      if (has_kept_debug_info)
14406
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14407
0
    if (isec->gc_mark
14408
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14409
0
      if (!_bfd_elf_gc_mark (info, isec,
14410
0
           elf_gc_mark_debug_section))
14411
0
        return false;
14412
0
    }
14413
0
  return true;
14414
0
}
14415
14416
static bool
14417
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14418
0
{
14419
0
  bfd *sub;
14420
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14421
14422
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14423
0
    {
14424
0
      asection *o;
14425
14426
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14427
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14428
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14429
0
  continue;
14430
0
      o = sub->sections;
14431
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14432
0
  continue;
14433
14434
0
      for (o = sub->sections; o != NULL; o = o->next)
14435
0
  {
14436
    /* When any section in a section group is kept, we keep all
14437
       sections in the section group.  If the first member of
14438
       the section group is excluded, we will also exclude the
14439
       group section.  */
14440
0
    if (o->flags & SEC_GROUP)
14441
0
      {
14442
0
        asection *first = elf_next_in_group (o);
14443
0
        if (first != NULL)
14444
0
    o->gc_mark = first->gc_mark;
14445
0
      }
14446
14447
0
    if (o->gc_mark)
14448
0
      continue;
14449
14450
    /* Skip sweeping sections already excluded.  */
14451
0
    if (o->flags & SEC_EXCLUDE)
14452
0
      continue;
14453
14454
    /* Since this is early in the link process, it is simple
14455
       to remove a section from the output.  */
14456
0
    o->flags |= SEC_EXCLUDE;
14457
14458
0
    if (info->print_gc_sections && o->size != 0)
14459
      /* xgettext:c-format */
14460
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14461
0
        o, sub);
14462
0
  }
14463
0
    }
14464
14465
0
  return true;
14466
0
}
14467
14468
/* Propagate collected vtable information.  This is called through
14469
   elf_link_hash_traverse.  */
14470
14471
static bool
14472
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14473
0
{
14474
  /* Those that are not vtables.  */
14475
0
  if (h->start_stop
14476
0
      || h->u2.vtable == NULL
14477
0
      || h->u2.vtable->parent == NULL)
14478
0
    return true;
14479
14480
  /* Those vtables that do not have parents, we cannot merge.  */
14481
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14482
0
    return true;
14483
14484
  /* If we've already been done, exit.  */
14485
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14486
0
    return true;
14487
14488
  /* Make sure the parent's table is up to date.  */
14489
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14490
14491
0
  if (h->u2.vtable->used == NULL)
14492
0
    {
14493
      /* None of this table's entries were referenced.  Re-use the
14494
   parent's table.  */
14495
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14496
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14497
0
    }
14498
0
  else
14499
0
    {
14500
0
      size_t n;
14501
0
      bool *cu, *pu;
14502
14503
      /* Or the parent's entries into ours.  */
14504
0
      cu = h->u2.vtable->used;
14505
0
      cu[-1] = true;
14506
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14507
0
      if (pu != NULL)
14508
0
  {
14509
0
    elf_backend_data *bed;
14510
0
    unsigned int log_file_align;
14511
14512
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14513
0
    log_file_align = bed->s->log_file_align;
14514
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14515
0
    while (n--)
14516
0
      {
14517
0
        if (*pu)
14518
0
    *cu = true;
14519
0
        pu++;
14520
0
        cu++;
14521
0
      }
14522
0
  }
14523
0
    }
14524
14525
0
  return true;
14526
0
}
14527
14528
struct link_info_ok
14529
{
14530
  struct bfd_link_info *info;
14531
  bool ok;
14532
};
14533
14534
static bool
14535
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14536
            void *ptr)
14537
0
{
14538
0
  asection *sec;
14539
0
  bfd_vma hstart, hend;
14540
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14541
0
  elf_backend_data *bed;
14542
0
  unsigned int log_file_align;
14543
0
  struct link_info_ok *info = (struct link_info_ok *) ptr;
14544
14545
  /* Take care of both those symbols that do not describe vtables as
14546
     well as those that are not loaded.  */
14547
0
  if (h->start_stop
14548
0
      || h->u2.vtable == NULL
14549
0
      || h->u2.vtable->parent == NULL)
14550
0
    return true;
14551
14552
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14553
0
        || h->root.type == bfd_link_hash_defweak);
14554
14555
0
  sec = h->root.u.def.section;
14556
0
  hstart = h->root.u.def.value;
14557
0
  hend = hstart + h->size;
14558
14559
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14560
0
               sec, NULL, NULL, true);
14561
0
  if (!relstart)
14562
0
    return info->ok = false;
14563
0
  bed = get_elf_backend_data (sec->owner);
14564
0
  log_file_align = bed->s->log_file_align;
14565
14566
0
  relend = relstart + sec->reloc_count;
14567
14568
0
  for (rel = relstart; rel < relend; ++rel)
14569
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14570
0
      {
14571
  /* If the entry is in use, do nothing.  */
14572
0
  if (h->u2.vtable->used
14573
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14574
0
    {
14575
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14576
0
      if (h->u2.vtable->used[entry])
14577
0
        continue;
14578
0
    }
14579
  /* Otherwise, kill it.  */
14580
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14581
0
      }
14582
14583
0
  return true;
14584
0
}
14585
14586
/* Mark sections containing dynamically referenced symbols.  When
14587
   building shared libraries, we must assume that any visible symbol is
14588
   referenced.  */
14589
14590
bool
14591
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14592
0
{
14593
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
14594
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14595
14596
0
  if ((h->root.type == bfd_link_hash_defined
14597
0
       || h->root.type == bfd_link_hash_defweak)
14598
0
      && (!h->start_stop
14599
0
    || h->root.ldscript_def
14600
0
    || !info->start_stop_gc)
14601
0
      && ((h->ref_dynamic && !h->forced_local)
14602
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14603
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14604
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14605
0
        && (!bfd_link_executable (info)
14606
0
      || info->gc_keep_exported
14607
0
      || info->export_dynamic
14608
0
      || (h->dynamic
14609
0
          && d != NULL
14610
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14611
0
        && (h->versioned >= versioned
14612
0
      || !bfd_hide_sym_by_version (info->version_info,
14613
0
                 h->root.root.string)))))
14614
0
    h->root.u.def.section->flags |= SEC_KEEP;
14615
14616
0
  return true;
14617
0
}
14618
14619
/* Keep all sections containing symbols undefined on the command-line,
14620
   and the section containing the entry symbol.  */
14621
14622
void
14623
_bfd_elf_gc_keep (struct bfd_link_info *info)
14624
0
{
14625
0
  struct bfd_sym_chain *sym;
14626
14627
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14628
0
    {
14629
0
      struct elf_link_hash_entry *h;
14630
14631
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14632
0
        false, false, false);
14633
14634
0
      if (h != NULL
14635
0
    && (h->root.type == bfd_link_hash_defined
14636
0
        || h->root.type == bfd_link_hash_defweak)
14637
0
    && !bfd_is_const_section (h->root.u.def.section))
14638
0
  h->root.u.def.section->flags |= SEC_KEEP;
14639
0
    }
14640
0
}
14641
14642
bool
14643
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14644
        struct bfd_link_info *info)
14645
0
{
14646
0
  bfd *ibfd = info->input_bfds;
14647
14648
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14649
0
    {
14650
0
      asection *sec;
14651
0
      struct elf_reloc_cookie cookie;
14652
14653
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14654
0
  continue;
14655
0
      sec = ibfd->sections;
14656
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14657
0
  continue;
14658
14659
0
      if (!init_reloc_cookie (&cookie, ibfd))
14660
0
  return false;
14661
14662
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14663
0
  {
14664
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14665
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14666
0
           false))
14667
0
      {
14668
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14669
0
        fini_reloc_cookie_rels (&cookie, sec);
14670
0
      }
14671
0
  }
14672
0
    }
14673
0
  return true;
14674
0
}
14675
14676
/* Do mark and sweep of unused sections.  */
14677
14678
bool
14679
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14680
0
{
14681
0
  bool ok = true;
14682
0
  bfd *sub;
14683
0
  elf_gc_mark_hook_fn gc_mark_hook;
14684
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14685
0
  struct elf_link_hash_table *htab;
14686
0
  struct link_info_ok info_ok;
14687
14688
0
  if (!bed->can_gc_sections
14689
0
      || !is_elf_hash_table (info->hash))
14690
0
    {
14691
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14692
0
      return true;
14693
0
    }
14694
14695
0
  bed->gc_keep (info);
14696
0
  htab = elf_hash_table (info);
14697
14698
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14699
     at the .eh_frame section if we can mark the FDEs individually.  */
14700
0
  for (sub = info->input_bfds;
14701
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14702
0
       sub = sub->link.next)
14703
0
    {
14704
0
      asection *sec;
14705
0
      struct elf_reloc_cookie cookie;
14706
14707
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14708
0
  continue;
14709
0
      sec = sub->sections;
14710
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14711
0
  continue;
14712
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14713
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14714
0
               false))
14715
0
  {
14716
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14717
0
    if (sec->sec_info
14718
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14719
0
      elf_eh_frame_section (sub) = sec;
14720
0
    fini_reloc_cookie_for_section (&cookie, sec);
14721
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14722
0
  }
14723
14724
      /* Handle .sframe section.  */
14725
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14726
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14727
0
               false))
14728
0
  {
14729
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14730
14731
0
    if (sec->sec_info
14732
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14733
0
      elf_sframe_section (sub) = sec;
14734
14735
0
    fini_reloc_cookie_for_section (&cookie, sec);
14736
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14737
0
  }
14738
0
    }
14739
14740
  /* Apply transitive closure to the vtable entry usage info.  */
14741
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14742
0
  if (!ok)
14743
0
    return false;
14744
14745
  /* Kill the vtable relocations that were not used.  */
14746
0
  info_ok.info = info;
14747
0
  info_ok.ok = true;
14748
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14749
0
  if (!info_ok.ok)
14750
0
    return false;
14751
14752
  /* Mark dynamically referenced symbols.  */
14753
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14754
0
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14755
14756
  /* Grovel through relocs to find out who stays ...  */
14757
0
  gc_mark_hook = bed->gc_mark_hook;
14758
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14759
0
    {
14760
0
      asection *o;
14761
14762
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14763
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14764
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14765
0
  continue;
14766
14767
0
      o = sub->sections;
14768
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14769
0
  continue;
14770
14771
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14772
   Also treat note sections as a root, if the section is not part
14773
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14774
   well as FINI_ARRAY sections for ld -r.  */
14775
0
      for (o = sub->sections; o != NULL; o = o->next)
14776
0
  if (!o->gc_mark
14777
0
      && (o->flags & SEC_EXCLUDE) == 0
14778
0
      && ((o->flags & SEC_KEEP) != 0
14779
0
    || (bfd_link_relocatable (info)
14780
0
        && ((elf_section_data (o)->this_hdr.sh_type
14781
0
       == SHT_PREINIT_ARRAY)
14782
0
      || (elf_section_data (o)->this_hdr.sh_type
14783
0
          == SHT_INIT_ARRAY)
14784
0
      || (elf_section_data (o)->this_hdr.sh_type
14785
0
          == SHT_FINI_ARRAY)))
14786
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14787
0
        && elf_next_in_group (o) == NULL
14788
0
        && elf_linked_to_section (o) == NULL)
14789
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14790
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14791
0
    {
14792
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14793
0
        return false;
14794
0
    }
14795
0
    }
14796
14797
  /* Allow the backend to mark additional target specific sections.  */
14798
0
  bed->gc_mark_extra_sections (info, gc_mark_hook);
14799
14800
  /* ... and mark SEC_EXCLUDE for those that go.  */
14801
0
  return elf_gc_sweep (abfd, info);
14802
0
}
14803

14804
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14805
14806
bool
14807
bfd_elf_gc_record_vtinherit (bfd *abfd,
14808
           asection *sec,
14809
           struct elf_link_hash_entry *h,
14810
           bfd_vma offset)
14811
0
{
14812
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14813
0
  struct elf_link_hash_entry **search, *child;
14814
0
  size_t extsymcount;
14815
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14816
14817
  /* The sh_info field of the symtab header tells us where the
14818
     external symbols start.  We don't care about the local symbols at
14819
     this point.  */
14820
0
  extsymcount = elf_symtab_hdr (abfd).sh_size / bed->s->sizeof_sym;
14821
0
  if (!elf_bad_symtab (abfd))
14822
0
    extsymcount -= elf_symtab_hdr (abfd).sh_info;
14823
14824
0
  sym_hashes = elf_sym_hashes (abfd);
14825
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14826
14827
  /* Hunt down the child symbol, which is in this section at the same
14828
     offset as the relocation.  */
14829
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14830
0
    {
14831
0
      if ((child = *search) != NULL
14832
0
    && (child->root.type == bfd_link_hash_defined
14833
0
        || child->root.type == bfd_link_hash_defweak)
14834
0
    && child->root.u.def.section == sec
14835
0
    && child->root.u.def.value == offset)
14836
0
  goto win;
14837
0
    }
14838
14839
  /* xgettext:c-format */
14840
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14841
0
          abfd, sec, (uint64_t) offset);
14842
0
  bfd_set_error (bfd_error_invalid_operation);
14843
0
  return false;
14844
14845
0
 win:
14846
0
  if (!child->u2.vtable)
14847
0
    {
14848
0
      child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14849
0
        bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14850
0
      if (!child->u2.vtable)
14851
0
  return false;
14852
0
    }
14853
0
  if (!h)
14854
0
    {
14855
      /* This *should* only be the absolute section.  It could potentially
14856
   be that someone has defined a non-global vtable though, which
14857
   would be bad.  It isn't worth paging in the local symbols to be
14858
   sure though; that case should simply be handled by the assembler.  */
14859
14860
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14861
0
    }
14862
0
  else
14863
0
    child->u2.vtable->parent = h;
14864
14865
0
  return true;
14866
0
}
14867
14868
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14869
14870
bool
14871
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14872
         struct elf_link_hash_entry *h,
14873
         bfd_vma addend)
14874
0
{
14875
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14876
0
  unsigned int log_file_align = bed->s->log_file_align;
14877
14878
0
  if (!h || addend > 1u << 28)
14879
0
    {
14880
      /* xgettext:c-format */
14881
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14882
0
        abfd, sec);
14883
0
      bfd_set_error (bfd_error_bad_value);
14884
0
      return false;
14885
0
    }
14886
14887
0
  if (!h->u2.vtable)
14888
0
    {
14889
0
      h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14890
0
          bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14891
0
      if (!h->u2.vtable)
14892
0
  return false;
14893
0
    }
14894
14895
0
  if (addend >= h->u2.vtable->size)
14896
0
    {
14897
0
      size_t size, bytes, file_align;
14898
0
      bool *ptr = h->u2.vtable->used;
14899
14900
      /* While the symbol is undefined, we have to be prepared to handle
14901
   a zero size.  */
14902
0
      file_align = 1 << log_file_align;
14903
0
      if (h->root.type == bfd_link_hash_undefined)
14904
0
  size = addend + file_align;
14905
0
      else
14906
0
  {
14907
0
    size = h->size;
14908
0
    if (addend >= size)
14909
0
      {
14910
        /* Oops!  We've got a reference past the defined end of
14911
     the table.  This is probably a bug -- shall we warn?  */
14912
0
        size = addend + file_align;
14913
0
      }
14914
0
  }
14915
0
      size = (size + file_align - 1) & -file_align;
14916
14917
      /* Allocate one extra entry for use as a "done" flag for the
14918
   consolidation pass.  */
14919
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14920
14921
0
      if (ptr)
14922
0
  {
14923
0
    ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14924
14925
0
    if (ptr != NULL)
14926
0
      {
14927
0
        size_t oldbytes;
14928
14929
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14930
0
        * sizeof (bool));
14931
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14932
0
      }
14933
0
  }
14934
0
      else
14935
0
  ptr = (bool *) bfd_zmalloc (bytes);
14936
14937
0
      if (ptr == NULL)
14938
0
  return false;
14939
14940
      /* And arrange for that done flag to be at index -1.  */
14941
0
      h->u2.vtable->used = ptr + 1;
14942
0
      h->u2.vtable->size = size;
14943
0
    }
14944
14945
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14946
14947
0
  return true;
14948
0
}
14949
14950
/* Map an ELF section header flag to its corresponding string.  */
14951
typedef struct
14952
{
14953
  char *flag_name;
14954
  flagword flag_value;
14955
} elf_flags_to_name_table;
14956
14957
static const elf_flags_to_name_table elf_flags_to_names [] =
14958
{
14959
  { "SHF_WRITE", SHF_WRITE },
14960
  { "SHF_ALLOC", SHF_ALLOC },
14961
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14962
  { "SHF_MERGE", SHF_MERGE },
14963
  { "SHF_STRINGS", SHF_STRINGS },
14964
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14965
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14966
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14967
  { "SHF_GROUP", SHF_GROUP },
14968
  { "SHF_TLS", SHF_TLS },
14969
  { "SHF_MASKOS", SHF_MASKOS },
14970
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14971
};
14972
14973
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14974
bool
14975
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14976
            struct flag_info *flaginfo,
14977
            asection *section)
14978
0
{
14979
0
  const bfd_vma sh_flags = elf_section_flags (section);
14980
14981
0
  if (!flaginfo->flags_initialized)
14982
0
    {
14983
0
      bfd *obfd = info->output_bfd;
14984
0
      elf_backend_data *bed = get_elf_backend_data (obfd);
14985
0
      struct flag_info_list *tf = flaginfo->flag_list;
14986
0
      int with_hex = 0;
14987
0
      int without_hex = 0;
14988
14989
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14990
0
  {
14991
0
    unsigned i;
14992
0
    flagword (*lookup) (char *);
14993
14994
0
    lookup = bed->elf_backend_lookup_section_flags_hook;
14995
0
    if (lookup != NULL)
14996
0
      {
14997
0
        flagword hexval = (*lookup) ((char *) tf->name);
14998
14999
0
        if (hexval != 0)
15000
0
    {
15001
0
      if (tf->with == with_flags)
15002
0
        with_hex |= hexval;
15003
0
      else if (tf->with == without_flags)
15004
0
        without_hex |= hexval;
15005
0
      tf->valid = true;
15006
0
      continue;
15007
0
    }
15008
0
      }
15009
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
15010
0
      {
15011
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
15012
0
    {
15013
0
      if (tf->with == with_flags)
15014
0
        with_hex |= elf_flags_to_names[i].flag_value;
15015
0
      else if (tf->with == without_flags)
15016
0
        without_hex |= elf_flags_to_names[i].flag_value;
15017
0
      tf->valid = true;
15018
0
      break;
15019
0
    }
15020
0
      }
15021
0
    if (!tf->valid)
15022
0
      {
15023
0
        info->callbacks->einfo
15024
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
15025
0
        return false;
15026
0
      }
15027
0
  }
15028
0
      flaginfo->flags_initialized = true;
15029
0
      flaginfo->only_with_flags |= with_hex;
15030
0
      flaginfo->not_with_flags |= without_hex;
15031
0
    }
15032
15033
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
15034
0
    return false;
15035
15036
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
15037
0
    return false;
15038
15039
0
  return true;
15040
0
}
15041
15042
struct alloc_got_off_arg {
15043
  bfd_vma gotoff;
15044
  struct bfd_link_info *info;
15045
};
15046
15047
/* We need a special top-level link routine to convert got reference counts
15048
   to real got offsets.  */
15049
15050
static bool
15051
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15052
0
{
15053
0
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
15054
0
  bfd *obfd = gofarg->info->output_bfd;
15055
0
  elf_backend_data *bed = get_elf_backend_data (obfd);
15056
15057
0
  if (h->got.refcount > 0)
15058
0
    {
15059
0
      h->got.offset = gofarg->gotoff;
15060
0
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
15061
0
    }
15062
0
  else
15063
0
    h->got.offset = (bfd_vma) -1;
15064
15065
0
  return true;
15066
0
}
15067
15068
/* And an accompanying bit to work out final got entry offsets once
15069
   we're done.  Should be called from final_link.  */
15070
15071
bool
15072
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15073
          struct bfd_link_info *info)
15074
0
{
15075
0
  bfd *i;
15076
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15077
0
  bfd_vma gotoff;
15078
0
  struct alloc_got_off_arg gofarg;
15079
15080
0
  BFD_ASSERT (abfd == info->output_bfd);
15081
15082
0
  if (! is_elf_hash_table (info->hash))
15083
0
    return false;
15084
15085
  /* The GOT offset is relative to the .got section, but the GOT header is
15086
     put into the .got.plt section, if the backend uses it.  */
15087
0
  if (bed->want_got_plt)
15088
0
    gotoff = 0;
15089
0
  else
15090
0
    gotoff = bed->got_header_size;
15091
15092
  /* Do the local .got entries first.  */
15093
0
  for (i = info->input_bfds; i; i = i->link.next)
15094
0
    {
15095
0
      bfd_signed_vma *local_got;
15096
0
      size_t j, locsymcount;
15097
0
      Elf_Internal_Shdr *symtab_hdr;
15098
15099
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15100
0
  continue;
15101
15102
0
      local_got = elf_local_got_refcounts (i);
15103
0
      if (!local_got)
15104
0
  continue;
15105
15106
0
      symtab_hdr = &elf_symtab_hdr (i);
15107
0
      if (elf_bad_symtab (i))
15108
0
  locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15109
0
      else
15110
0
  locsymcount = symtab_hdr->sh_info;
15111
15112
0
      for (j = 0; j < locsymcount; ++j)
15113
0
  {
15114
0
    if (local_got[j] > 0)
15115
0
      {
15116
0
        local_got[j] = gotoff;
15117
0
        gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15118
0
      }
15119
0
    else
15120
0
      local_got[j] = (bfd_vma) -1;
15121
0
  }
15122
0
    }
15123
15124
  /* Then the global .got entries.  .plt refcounts are handled by
15125
     adjust_dynamic_symbol  */
15126
0
  gofarg.gotoff = gotoff;
15127
0
  gofarg.info = info;
15128
0
  elf_link_hash_traverse (elf_hash_table (info),
15129
0
        elf_gc_allocate_got_offsets,
15130
0
        &gofarg);
15131
0
  return true;
15132
0
}
15133
15134
/* Many folk need no more in the way of final link than this, once
15135
   got entry reference counting is enabled.  */
15136
15137
bool
15138
_bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15139
0
{
15140
0
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15141
0
    return false;
15142
15143
  /* Invoke the regular ELF backend linker to do all the work.  */
15144
0
  return _bfd_elf_final_link (abfd, info);
15145
0
}
15146
15147
bool
15148
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15149
0
{
15150
0
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15151
15152
0
  if (elf_bad_symtab (rcookie->abfd))
15153
0
    rcookie->rel = rcookie->rels;
15154
15155
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15156
0
    {
15157
0
      unsigned long r_symndx;
15158
15159
0
      if (!elf_bad_symtab (rcookie->abfd)
15160
0
    && rcookie->rel->r_offset > offset)
15161
0
  return false;
15162
0
      if (rcookie->rel->r_offset != offset)
15163
0
  continue;
15164
15165
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15166
0
      if (r_symndx == STN_UNDEF)
15167
0
  return true;
15168
15169
0
      struct elf_link_hash_entry *h;
15170
15171
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15172
15173
0
      if (h != NULL)
15174
0
  {
15175
0
    if ((h->root.type == bfd_link_hash_defined
15176
0
         || h->root.type == bfd_link_hash_defweak)
15177
0
        && (h->root.u.def.section->owner != rcookie->abfd
15178
0
      || h->root.u.def.section->kept_section != NULL
15179
0
      || discarded_section (h->root.u.def.section)))
15180
0
      return true;
15181
0
  }
15182
0
      else
15183
0
  {
15184
0
    if (r_symndx >= rcookie->locsymcount)
15185
      /* This can happen with corrupt input.  */
15186
0
      return false;
15187
15188
    /* It's not a relocation against a global symbol,
15189
       but it could be a relocation against a local
15190
       symbol for a discarded section.  */
15191
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15192
0
    if (isec != NULL
15193
0
        && (isec->kept_section != NULL
15194
0
      || discarded_section (isec)))
15195
0
      return true;
15196
0
  }
15197
15198
0
      return false;
15199
0
    }
15200
0
  return false;
15201
0
}
15202
15203
/* Discard unneeded references to discarded sections.
15204
   Returns -1 on error, 1 if any section's size was changed, 0 if
15205
   nothing changed.  This function assumes that the relocations are in
15206
   sorted order, which is true for all known assemblers.  */
15207
15208
int
15209
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15210
0
{
15211
0
  struct elf_reloc_cookie cookie;
15212
0
  asection *o;
15213
0
  bfd *abfd;
15214
0
  int changed = 0;
15215
15216
0
  if (info->traditional_format
15217
0
      || !is_elf_hash_table (info->hash))
15218
0
    return 0;
15219
15220
0
  o = bfd_get_section_by_name (output_bfd, ".stab");
15221
0
  if (o != NULL)
15222
0
    {
15223
0
      asection *i;
15224
15225
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15226
0
  {
15227
0
    if (i->size == 0
15228
0
        || i->reloc_count == 0
15229
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15230
0
      continue;
15231
15232
0
    abfd = i->owner;
15233
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15234
0
      continue;
15235
15236
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15237
0
      return -1;
15238
15239
0
    if (_bfd_discard_section_stabs (abfd, i,
15240
0
            bfd_elf_reloc_symbol_deleted_p,
15241
0
            &cookie))
15242
0
      changed = 1;
15243
15244
0
    fini_reloc_cookie_for_section (&cookie, i);
15245
0
  }
15246
0
    }
15247
15248
0
  o = NULL;
15249
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15250
0
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15251
0
  if (o != NULL)
15252
0
    {
15253
0
      asection *i;
15254
0
      int eh_changed = 0;
15255
0
      unsigned int eh_alignment;  /* Octets.  */
15256
15257
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15258
0
  {
15259
0
    int r;
15260
15261
0
    if (i->size == 0)
15262
0
      continue;
15263
15264
0
    abfd = i->owner;
15265
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15266
0
      continue;
15267
15268
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15269
0
      return -1;
15270
15271
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15272
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15273
0
             bfd_elf_reloc_symbol_deleted_p,
15274
0
             &cookie);
15275
0
    if (r)
15276
0
      {
15277
0
        eh_changed = 1;
15278
0
        if (r >= 2)
15279
0
    changed = 1;
15280
0
      }
15281
15282
0
    fini_reloc_cookie_for_section (&cookie, i);
15283
0
  }
15284
15285
0
      eh_alignment = ((1 << o->alignment_power)
15286
0
          * bfd_octets_per_byte (output_bfd, o));
15287
      /* Skip over zero terminator, and prevent empty sections from
15288
   adding alignment padding at the end.  */
15289
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15290
0
  if (i->size == 0)
15291
0
    i->flags |= SEC_EXCLUDE;
15292
0
  else if (i->size > 4)
15293
0
    break;
15294
      /* The last non-empty eh_frame section doesn't need padding.  */
15295
0
      if (i != NULL)
15296
0
  i = i->map_tail.s;
15297
      /* Any prior sections must pad the last FDE out to the output
15298
   section alignment.  Otherwise we might have zero padding
15299
   between sections, which would be seen as a terminator.  */
15300
0
      for (; i != NULL; i = i->map_tail.s)
15301
0
  if (i->size == 4)
15302
    /* All but the last zero terminator should have been removed.  */
15303
0
    BFD_FAIL ();
15304
0
  else
15305
0
    {
15306
0
      bfd_size_type size
15307
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15308
0
      if (i->size != size)
15309
0
        {
15310
0
    i->size = size;
15311
0
    changed = 1;
15312
0
    eh_changed = 1;
15313
0
        }
15314
0
    }
15315
0
      if (eh_changed)
15316
0
  elf_link_hash_traverse (elf_hash_table (info),
15317
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15318
0
    }
15319
15320
0
  o = bfd_get_section_by_name (output_bfd, ".sframe");
15321
0
  if (o != NULL)
15322
0
    {
15323
0
      asection *i;
15324
15325
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15326
0
  {
15327
0
    if (i->size == 0)
15328
0
      continue;
15329
15330
0
    abfd = i->owner;
15331
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15332
0
      continue;
15333
15334
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15335
0
      return -1;
15336
15337
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15338
0
      {
15339
0
        if (_bfd_elf_discard_section_sframe (i,
15340
0
               bfd_elf_reloc_symbol_deleted_p,
15341
0
               &cookie))
15342
0
    {
15343
0
      if (i->size != i->rawsize)
15344
0
        changed = 1;
15345
0
    }
15346
0
      }
15347
0
    fini_reloc_cookie_for_section (&cookie, i);
15348
0
  }
15349
      /* Update the reference to the output .sframe section.  Used to
15350
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15351
0
      if (!_bfd_elf_set_section_sframe (output_bfd, info))
15352
0
  return -1;
15353
0
    }
15354
15355
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15356
0
    {
15357
0
      elf_backend_data *bed;
15358
0
      asection *s;
15359
15360
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15361
0
  continue;
15362
0
      s = abfd->sections;
15363
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15364
0
  continue;
15365
15366
0
      bed = get_elf_backend_data (abfd);
15367
15368
0
      if (bed->elf_backend_discard_info != NULL)
15369
0
  {
15370
0
    if (!init_reloc_cookie (&cookie, abfd))
15371
0
      return -1;
15372
15373
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15374
0
      changed = 1;
15375
15376
0
    fini_reloc_cookie (&cookie, abfd);
15377
0
  }
15378
0
    }
15379
15380
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15381
0
    _bfd_elf_end_eh_frame_parsing (info);
15382
15383
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15384
0
    changed = 1;
15385
15386
0
  return changed;
15387
0
}
15388
15389
bool
15390
_bfd_elf_section_already_linked (bfd *abfd,
15391
         asection *sec,
15392
         struct bfd_link_info *info)
15393
0
{
15394
0
  flagword flags;
15395
0
  const char *name, *key;
15396
0
  struct bfd_section_already_linked *l;
15397
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15398
15399
0
  if (sec->output_section == bfd_abs_section_ptr)
15400
0
    return false;
15401
15402
0
  flags = sec->flags;
15403
15404
  /* Return if it isn't a linkonce section.  A comdat group section
15405
     also has SEC_LINK_ONCE set.  */
15406
0
  if ((flags & SEC_LINK_ONCE) == 0)
15407
0
    return false;
15408
15409
  /* Don't put group member sections on our list of already linked
15410
     sections.  They are handled as a group via their group section.  */
15411
0
  if (elf_sec_group (sec) != NULL)
15412
0
    return false;
15413
15414
  /* For a SHT_GROUP section, use the group signature as the key.  */
15415
0
  name = sec->name;
15416
0
  if ((flags & SEC_GROUP) != 0
15417
0
      && elf_next_in_group (sec) != NULL
15418
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15419
0
    key = elf_group_name (elf_next_in_group (sec));
15420
0
  else
15421
0
    {
15422
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15423
0
      if (startswith (name, ".gnu.linkonce.")
15424
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15425
0
  key++;
15426
0
      else
15427
  /* Must be a user linkonce section that doesn't follow gcc's
15428
     naming convention.  In this case we won't be matching
15429
     single member groups.  */
15430
0
  key = name;
15431
0
    }
15432
15433
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15434
15435
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15436
0
    {
15437
      /* We may have 2 different types of sections on the list: group
15438
   sections with a signature of <key> (<key> is some string),
15439
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15440
   Match like sections.  LTO plugin sections are an exception.
15441
   They are always named .gnu.linkonce.t.<key> and match either
15442
   type of section.  */
15443
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15444
0
     && ((flags & SEC_GROUP) != 0
15445
0
         || strcmp (name, l->sec->name) == 0))
15446
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15447
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15448
0
  {
15449
    /* The section has already been linked.  See if we should
15450
       issue a warning.  */
15451
0
    if (!_bfd_handle_already_linked (sec, l, info))
15452
0
      return false;
15453
15454
0
    if (flags & SEC_GROUP)
15455
0
      {
15456
0
        asection *first = elf_next_in_group (sec);
15457
0
        asection *s = first;
15458
15459
0
        while (s != NULL)
15460
0
    {
15461
0
      s->output_section = bfd_abs_section_ptr;
15462
      /* Record which group discards it.  */
15463
0
      s->kept_section = l->sec;
15464
0
      s = elf_next_in_group (s);
15465
      /* These lists are circular.  */
15466
0
      if (s == first)
15467
0
        break;
15468
0
    }
15469
0
      }
15470
15471
0
    return true;
15472
0
  }
15473
0
    }
15474
15475
  /* A single member comdat group section may be discarded by a
15476
     linkonce section and vice versa.  */
15477
0
  if ((flags & SEC_GROUP) != 0)
15478
0
    {
15479
0
      asection *first = elf_next_in_group (sec);
15480
15481
0
      if (first != NULL && elf_next_in_group (first) == first)
15482
  /* Check this single member group against linkonce sections.  */
15483
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15484
0
    if ((l->sec->flags & SEC_GROUP) == 0
15485
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15486
0
      {
15487
0
        first->output_section = bfd_abs_section_ptr;
15488
0
        first->kept_section = l->sec;
15489
0
        sec->output_section = bfd_abs_section_ptr;
15490
0
        break;
15491
0
      }
15492
0
    }
15493
0
  else
15494
    /* Check this linkonce section against single member groups.  */
15495
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15496
0
      if (l->sec->flags & SEC_GROUP)
15497
0
  {
15498
0
    asection *first = elf_next_in_group (l->sec);
15499
15500
0
    if (first != NULL
15501
0
        && elf_next_in_group (first) == first
15502
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15503
0
      {
15504
0
        sec->output_section = bfd_abs_section_ptr;
15505
0
        sec->kept_section = first;
15506
0
        break;
15507
0
      }
15508
0
  }
15509
15510
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15511
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15512
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15513
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15514
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15515
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15516
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15517
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15518
     The reverse order cannot happen as there is never a bfd with only the
15519
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15520
     matter as here were are looking only for cross-bfd sections.  */
15521
15522
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15523
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15524
0
      if ((l->sec->flags & SEC_GROUP) == 0
15525
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15526
0
  {
15527
0
    if (abfd != l->sec->owner)
15528
0
      sec->output_section = bfd_abs_section_ptr;
15529
0
    break;
15530
0
  }
15531
15532
  /* This is the first section with this name.  Record it.  */
15533
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15534
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15535
0
  return sec->output_section == bfd_abs_section_ptr;
15536
0
}
15537
15538
bool
15539
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15540
0
{
15541
0
  return sym->st_shndx == SHN_COMMON;
15542
0
}
15543
15544
unsigned int
15545
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15546
0
{
15547
0
  return SHN_COMMON;
15548
0
}
15549
15550
asection *
15551
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15552
0
{
15553
0
  return bfd_com_section_ptr;
15554
0
}
15555
15556
bfd_vma
15557
_bfd_elf_default_got_elt_size (bfd *abfd,
15558
             struct bfd_link_info *info ATTRIBUTE_UNUSED,
15559
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15560
             bfd *ibfd ATTRIBUTE_UNUSED,
15561
             unsigned long symndx ATTRIBUTE_UNUSED)
15562
0
{
15563
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15564
0
  return bed->s->arch_size / 8;
15565
0
}
15566
15567
/* Routines to support the creation of dynamic relocs.  */
15568
15569
/* Returns the name of the dynamic reloc section associated with SEC.  */
15570
15571
static const char *
15572
get_dynamic_reloc_section_name (bfd *       abfd,
15573
        asection *  sec,
15574
        bool is_rela)
15575
0
{
15576
0
  char *name;
15577
0
  const char *old_name = bfd_section_name (sec);
15578
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15579
15580
0
  if (old_name == NULL)
15581
0
    return NULL;
15582
15583
0
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15584
0
  sprintf (name, "%s%s", prefix, old_name);
15585
15586
0
  return name;
15587
0
}
15588
15589
/* Returns the dynamic reloc section associated with SEC.  If the
15590
   section does not exist it is created and attached to the DYNOBJ
15591
   bfd and stored in the SRELOC field of SEC's elf_section_data
15592
   structure.
15593
15594
   ALIGNMENT is the alignment for the newly created section and
15595
   IS_RELA defines whether the name should be .rela.<SEC's name>
15596
   or .rel.<SEC's name>.  The section name is looked up in the
15597
   string table associated with ABFD.  */
15598
15599
asection *
15600
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15601
             bfd *dynobj,
15602
             unsigned int alignment,
15603
             bfd *abfd,
15604
             bool is_rela)
15605
0
{
15606
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15607
15608
0
  if (reloc_sec == NULL)
15609
0
    {
15610
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15611
15612
0
      if (name == NULL)
15613
0
  return NULL;
15614
15615
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15616
15617
0
      if (reloc_sec == NULL)
15618
0
  {
15619
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15620
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15621
0
    if ((sec->flags & SEC_ALLOC) != 0)
15622
0
      flags |= SEC_ALLOC | SEC_LOAD;
15623
15624
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15625
0
    if (reloc_sec != NULL)
15626
0
      {
15627
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15628
     name.  Override as it may be wrong, eg. for a user
15629
     section named "auto" we'll get ".relauto" which is
15630
     seen to be a .rela section.  */
15631
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15632
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15633
0
    reloc_sec = NULL;
15634
0
      }
15635
0
  }
15636
15637
0
      elf_section_data (sec)->sreloc = reloc_sec;
15638
0
    }
15639
15640
0
  return reloc_sec;
15641
0
}
15642
15643
/* Copy the ELF symbol type and other attributes for a linker script
15644
   assignment from HSRC to HDEST.  Generally this should be treated as
15645
   if we found a strong non-dynamic definition for HDEST (except that
15646
   ld ignores multiple definition errors).  */
15647
void
15648
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15649
             struct bfd_link_hash_entry *hdest,
15650
             struct bfd_link_hash_entry *hsrc)
15651
0
{
15652
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15653
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15654
0
  Elf_Internal_Sym isym;
15655
15656
0
  ehdest->type = ehsrc->type;
15657
0
  ehdest->target_internal = ehsrc->target_internal;
15658
15659
0
  isym.st_other = ehsrc->other;
15660
0
  elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15661
0
}
15662
15663
/* Append a RELA relocation REL to section S in BFD.  */
15664
15665
void
15666
_bfd_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15667
0
{
15668
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15669
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15670
0
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15671
0
  bed->s->swap_reloca_out (abfd, rel, loc);
15672
0
}
15673
15674
/* Append a REL relocation REL to section S in BFD.  */
15675
15676
void
15677
_bfd_elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15678
0
{
15679
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
15680
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15681
0
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15682
0
  bed->s->swap_reloc_out (abfd, rel, loc);
15683
0
}
15684
15685
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15686
15687
struct bfd_link_hash_entry *
15688
bfd_elf_define_start_stop (struct bfd_link_info *info,
15689
         const char *symbol, asection *sec)
15690
0
{
15691
0
  struct elf_link_hash_entry *h;
15692
15693
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15694
0
          false, false, true);
15695
  /* NB: Common symbols will be turned into definition later.  */
15696
0
  if (h != NULL
15697
0
      && !h->root.ldscript_def
15698
0
      && (h->root.type == bfd_link_hash_undefined
15699
0
    || h->root.type == bfd_link_hash_undefweak
15700
0
    || ((h->ref_regular || h->def_dynamic)
15701
0
        && !h->def_regular
15702
0
        && h->root.type != bfd_link_hash_common)))
15703
0
    {
15704
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15705
0
      h->verinfo.verdef = NULL;
15706
0
      h->root.type = bfd_link_hash_defined;
15707
0
      h->root.u.def.section = sec;
15708
0
      h->root.u.def.value = 0;
15709
0
      h->def_regular = 1;
15710
0
      h->def_dynamic = 0;
15711
0
      h->start_stop = 1;
15712
0
      h->u2.start_stop_section = sec;
15713
0
      if (symbol[0] == '.')
15714
0
  {
15715
    /* .startof. and .sizeof. symbols are local.  */
15716
0
    elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
15717
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
15718
0
  }
15719
0
      else
15720
0
  {
15721
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15722
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15723
0
      | info->start_stop_visibility);
15724
0
    if (was_dynamic)
15725
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15726
0
  }
15727
0
      return &h->root;
15728
0
    }
15729
0
  return NULL;
15730
0
}
15731
15732
/* Find dynamic relocs for H that apply to read-only sections.  */
15733
15734
asection *
15735
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15736
0
{
15737
0
  struct elf_dyn_relocs *p;
15738
15739
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15740
0
    {
15741
0
      asection *s = p->sec->output_section;
15742
15743
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15744
0
  return p->sec;
15745
0
    }
15746
0
  return NULL;
15747
0
}
15748
15749
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15750
   read-only sections.  */
15751
15752
bool
15753
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15754
0
{
15755
0
  asection *sec;
15756
15757
0
  if (h->root.type == bfd_link_hash_indirect)
15758
0
    return true;
15759
15760
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15761
0
  if (sec != NULL)
15762
0
    {
15763
0
      struct bfd_link_info *info = (struct bfd_link_info *) inf;
15764
15765
0
      info->flags |= DF_TEXTREL;
15766
      /* xgettext:c-format */
15767
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15768
0
        "in read-only section `%pA'\n"),
15769
0
            sec->owner, h->root.root.string, sec);
15770
15771
0
      if (bfd_link_textrel_check (info))
15772
  /* xgettext:c-format */
15773
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15774
0
          "in read-only section `%pA'\n"),
15775
0
        sec->owner, h->root.root.string, sec);
15776
15777
      /* Not an error, just cut short the traversal.  */
15778
0
      return false;
15779
0
    }
15780
0
  return true;
15781
0
}
15782
15783
/* Add dynamic tags.  */
15784
15785
bool
15786
_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15787
         bool need_dynamic_reloc)
15788
0
{
15789
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15790
15791
0
  if (htab->dynamic_sections_created)
15792
0
    {
15793
      /* Add some entries to the .dynamic section.  We fill in the
15794
   values later, in finish_dynamic_sections, but we must add
15795
   the entries now so that we get the correct size for the
15796
   .dynamic section.  The DT_DEBUG entry is filled in by the
15797
   dynamic linker and used by the debugger.  */
15798
0
#define add_dynamic_entry(TAG, VAL) \
15799
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15800
15801
0
      elf_backend_data *bed = get_elf_backend_data (output_bfd);
15802
15803
0
      if (bfd_link_executable (info))
15804
0
  {
15805
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15806
0
      return false;
15807
0
  }
15808
15809
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15810
0
  {
15811
    /* DT_PLTGOT is used by prelink even if there is no PLT
15812
       relocation.  */
15813
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15814
0
      return false;
15815
0
  }
15816
15817
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15818
0
  {
15819
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15820
0
        || !add_dynamic_entry (DT_PLTREL,
15821
0
             (bed->rela_plts_and_copies_p
15822
0
              ? DT_RELA : DT_REL))
15823
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15824
0
      return false;
15825
0
  }
15826
15827
0
      if (htab->tlsdesc_plt
15828
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15829
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15830
0
  return false;
15831
15832
0
      if (need_dynamic_reloc)
15833
0
  {
15834
0
    if (bed->rela_plts_and_copies_p)
15835
0
      {
15836
0
        if (!add_dynamic_entry (DT_RELA, 0)
15837
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15838
0
      || !add_dynamic_entry (DT_RELAENT,
15839
0
           bed->s->sizeof_rela))
15840
0
    return false;
15841
0
      }
15842
0
    else
15843
0
      {
15844
0
        if (!add_dynamic_entry (DT_REL, 0)
15845
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15846
0
      || !add_dynamic_entry (DT_RELENT,
15847
0
           bed->s->sizeof_rel))
15848
0
    return false;
15849
0
      }
15850
15851
    /* If any dynamic relocs apply to a read-only section,
15852
       then we need a DT_TEXTREL entry.  */
15853
0
    if ((info->flags & DF_TEXTREL) == 0)
15854
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15855
0
            info);
15856
15857
0
    if ((info->flags & DF_TEXTREL) != 0)
15858
0
      {
15859
0
        if (htab->ifunc_resolvers)
15860
0
    info->callbacks->einfo
15861
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15862
0
         "may result in a segfault at runtime; recompile with %s\n"),
15863
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15864
15865
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15866
0
    return false;
15867
0
      }
15868
0
  }
15869
0
    }
15870
0
#undef add_dynamic_entry
15871
15872
0
  return true;
15873
0
}