Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/elflink.c
Line
Count
Source (jump to first uncovered line)
1
/* ELF linking support for BFD.
2
   Copyright (C) 1995-2025 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "bfdlink.h"
24
#include "libbfd.h"
25
#define ARCH_SIZE 0
26
#include "elf-bfd.h"
27
#include "safe-ctype.h"
28
#include "libiberty.h"
29
#include "objalloc.h"
30
#if BFD_SUPPORTS_PLUGINS
31
#include "plugin-api.h"
32
#include "plugin.h"
33
#endif
34
35
#include <limits.h>
36
#ifndef CHAR_BIT
37
#define CHAR_BIT 8
38
#endif
39
40
/* This struct is used to pass information to routines called via
41
   elf_link_hash_traverse which must return failure.  */
42
43
struct elf_info_failed
44
{
45
  struct bfd_link_info *info;
46
  bool failed;
47
};
48
49
static bool _bfd_elf_fix_symbol_flags
50
  (struct elf_link_hash_entry *, struct elf_info_failed *);
51
52
/* Return false if linker should avoid caching relocation information
53
   and symbol tables of input files in memory.  */
54
55
static bool
56
_bfd_elf_link_keep_memory (struct bfd_link_info *info)
57
0
{
58
0
#ifdef USE_MMAP
59
  /* Don't cache symbol nor relocation tables if they are mapped in.
60
     NB: Since the --no-keep-memory linker option causes:
61
62
     https://sourceware.org/bugzilla/show_bug.cgi?id=31458
63
64
     this is opt-in by each backend.  */
65
0
  const struct elf_backend_data *bed
66
0
    = get_elf_backend_data (info->output_bfd);
67
0
  if (bed != NULL && bed->use_mmap)
68
0
    return false;
69
0
#endif
70
0
  bfd *abfd;
71
0
  bfd_size_type size;
72
73
0
  if (!info->keep_memory)
74
0
    return false;
75
76
0
  if (info->max_cache_size == (bfd_size_type) -1)
77
0
    return true;
78
79
0
  abfd = info->input_bfds;
80
0
  size = info->cache_size;
81
0
  do
82
0
    {
83
0
      if (size >= info->max_cache_size)
84
0
  {
85
    /* Over the limit.  Reduce the memory usage.  */
86
0
    info->keep_memory = false;
87
0
    return false;
88
0
  }
89
0
      if (!abfd)
90
0
  break;
91
0
      size += abfd->alloc_size;
92
0
      abfd = abfd->link.next;
93
0
    }
94
0
  while (1);
95
96
0
  return true;
97
0
}
98
99
static struct elf_link_hash_entry *
100
get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
101
         unsigned int                   symndx,
102
         unsigned int                   ext_sym_start)
103
0
{
104
0
  if (sym_hashes == NULL
105
      /* Guard against corrupt input.  See PR 32636 for an example.  */
106
0
      || symndx < ext_sym_start)
107
0
    return NULL;
108
109
0
  struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
110
111
  /* The hash might be empty.  See PR 32641 for an example of this.  */
112
0
  if (h == NULL)
113
0
    return NULL;
114
115
0
  while (h->root.type == bfd_link_hash_indirect
116
0
   || h->root.type == bfd_link_hash_warning)
117
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
118
119
0
  return h;
120
0
}
121
122
struct elf_link_hash_entry *
123
_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
124
            unsigned int                   symndx,
125
            Elf_Internal_Shdr *            symtab_hdr)
126
0
{
127
0
  if (symtab_hdr == NULL)
128
0
    return NULL;
129
130
0
  return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info);
131
0
}
132
133
static struct elf_link_hash_entry *
134
get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx)
135
0
{
136
0
  if (cookie == NULL || cookie->sym_hashes == NULL)
137
0
    return NULL;
138
  
139
0
  if (r_symndx >= cookie->locsymcount
140
0
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
141
0
    return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff);
142
143
0
  return NULL;
144
0
}
145
146
asection *
147
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
148
           unsigned long r_symndx,
149
           bool discard)
150
0
{
151
0
  struct elf_link_hash_entry *h;
152
153
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
154
  
155
0
  if (h != NULL)
156
0
    {
157
0
      if ((h->root.type == bfd_link_hash_defined
158
0
     || h->root.type == bfd_link_hash_defweak)
159
0
     && discarded_section (h->root.u.def.section))
160
0
  return h->root.u.def.section;
161
0
      else
162
0
  return NULL;
163
0
    }
164
165
  /* It's not a relocation against a global symbol,
166
     but it could be a relocation against a local
167
     symbol for a discarded section.  */
168
0
  asection *isec;
169
0
  Elf_Internal_Sym *isym;
170
171
  /* Need to: get the symbol; get the section.  */
172
0
  isym = &cookie->locsyms[r_symndx];
173
0
  isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
174
0
  if (isec != NULL
175
0
      && discard ? discarded_section (isec) : 1)
176
0
    return isec;
177
178
0
  return NULL;
179
0
}
180
181
/* Define a symbol in a dynamic linkage section.  */
182
183
struct elf_link_hash_entry *
184
_bfd_elf_define_linkage_sym (bfd *abfd,
185
           struct bfd_link_info *info,
186
           asection *sec,
187
           const char *name)
188
0
{
189
0
  struct elf_link_hash_entry *h;
190
0
  struct bfd_link_hash_entry *bh;
191
0
  const struct elf_backend_data *bed;
192
193
0
  h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
194
0
  if (h != NULL)
195
0
    {
196
      /* Zap symbol defined in an as-needed lib that wasn't linked.
197
   This is a symptom of a larger problem:  Absolute symbols
198
   defined in shared libraries can't be overridden, because we
199
   lose the link to the bfd which is via the symbol section.  */
200
0
      h->root.type = bfd_link_hash_new;
201
0
      bh = &h->root;
202
0
    }
203
0
  else
204
0
    bh = NULL;
205
206
0
  bed = get_elf_backend_data (abfd);
207
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
208
0
           sec, 0, NULL, false, bed->collect,
209
0
           &bh))
210
0
    return NULL;
211
0
  h = (struct elf_link_hash_entry *) bh;
212
0
  BFD_ASSERT (h != NULL);
213
0
  h->def_regular = 1;
214
0
  h->non_elf = 0;
215
0
  h->root.linker_def = 1;
216
0
  h->type = STT_OBJECT;
217
0
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
218
0
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
219
220
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
221
0
  return h;
222
0
}
223
224
bool
225
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
226
0
{
227
0
  flagword flags;
228
0
  asection *s;
229
0
  struct elf_link_hash_entry *h;
230
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
231
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
232
233
  /* This function may be called more than once.  */
234
0
  if (htab->sgot != NULL)
235
0
    return true;
236
237
0
  flags = bed->dynamic_sec_flags;
238
239
0
  s = bfd_make_section_anyway_with_flags (abfd,
240
0
            (bed->rela_plts_and_copies_p
241
0
             ? ".rela.got" : ".rel.got"),
242
0
            flags | SEC_READONLY);
243
0
  if (s == NULL
244
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
245
0
    return false;
246
0
  htab->srelgot = s;
247
248
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
249
0
  if (s == NULL
250
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
251
0
    return false;
252
0
  htab->sgot = s;
253
254
0
  if (bed->want_got_plt)
255
0
    {
256
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
257
0
      if (s == NULL
258
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
259
0
  return false;
260
0
      htab->sgotplt = s;
261
0
    }
262
263
  /* The first bit of the global offset table is the header.  */
264
0
  s->size += bed->got_header_size;
265
266
0
  if (bed->want_got_sym)
267
0
    {
268
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
269
   (or .got.plt) section.  We don't do this in the linker script
270
   because we don't want to define the symbol if we are not creating
271
   a global offset table.  */
272
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
273
0
               "_GLOBAL_OFFSET_TABLE_");
274
0
      elf_hash_table (info)->hgot = h;
275
0
      if (h == NULL)
276
0
  return false;
277
0
    }
278
279
0
  return true;
280
0
}
281

282
/* Create a strtab to hold the dynamic symbol names.  */
283
static bool
284
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
285
0
{
286
0
  struct elf_link_hash_table *hash_table;
287
288
0
  hash_table = elf_hash_table (info);
289
0
  if (hash_table->dynobj == NULL)
290
0
    {
291
      /* We may not set dynobj, an input file holding linker created
292
   dynamic sections to abfd, which may be a dynamic object with
293
   its own dynamic sections.  We need to find a normal input file
294
   to hold linker created sections if possible.  */
295
0
      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
296
0
  {
297
0
    bfd *ibfd;
298
0
    asection *s;
299
0
    for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
300
0
      if ((ibfd->flags
301
0
     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
302
0
    && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
303
0
    && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
304
0
    && !((s = ibfd->sections) != NULL
305
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
306
0
        {
307
0
    abfd = ibfd;
308
0
    break;
309
0
        }
310
0
  }
311
0
      hash_table->dynobj = abfd;
312
0
    }
313
314
0
  if (hash_table->dynstr == NULL)
315
0
    {
316
0
      hash_table->dynstr = _bfd_elf_strtab_init ();
317
0
      if (hash_table->dynstr == NULL)
318
0
  return false;
319
0
    }
320
0
  return true;
321
0
}
322
323
/* Create some sections which will be filled in with dynamic linking
324
   information.  ABFD is an input file which requires dynamic sections
325
   to be created.  The dynamic sections take up virtual memory space
326
   when the final executable is run, so we need to create them before
327
   addresses are assigned to the output sections.  We work out the
328
   actual contents and size of these sections later.  */
329
330
bool
331
_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
332
0
{
333
0
  flagword flags;
334
0
  asection *s;
335
0
  const struct elf_backend_data *bed;
336
0
  struct elf_link_hash_entry *h;
337
338
0
  if (! is_elf_hash_table (info->hash))
339
0
    return false;
340
341
0
  if (elf_hash_table (info)->dynamic_sections_created)
342
0
    return true;
343
344
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
345
0
    return false;
346
347
0
  abfd = elf_hash_table (info)->dynobj;
348
0
  bed = get_elf_backend_data (abfd);
349
350
0
  flags = bed->dynamic_sec_flags;
351
352
  /* A dynamically linked executable has a .interp section, but a
353
     shared library does not.  */
354
0
  if (bfd_link_executable (info) && !info->nointerp)
355
0
    {
356
0
      s = bfd_make_section_anyway_with_flags (abfd, ".interp",
357
0
                flags | SEC_READONLY);
358
0
      if (s == NULL)
359
0
  return false;
360
0
    }
361
362
  /* Create sections to hold version informations.  These are removed
363
     if they are not needed.  */
364
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
365
0
            flags | SEC_READONLY);
366
0
  if (s == NULL
367
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
368
0
    return false;
369
370
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
371
0
            flags | SEC_READONLY);
372
0
  if (s == NULL
373
0
      || !bfd_set_section_alignment (s, 1))
374
0
    return false;
375
376
0
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
377
0
            flags | SEC_READONLY);
378
0
  if (s == NULL
379
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
380
0
    return false;
381
382
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
383
0
            flags | SEC_READONLY);
384
0
  if (s == NULL
385
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
386
0
    return false;
387
0
  elf_hash_table (info)->dynsym = s;
388
389
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
390
0
            flags | SEC_READONLY);
391
0
  if (s == NULL)
392
0
    return false;
393
394
0
  s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
395
0
  if (s == NULL
396
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
397
0
    return false;
398
0
  elf_hash_table (info)->dynamic = s;
399
400
  /* The special symbol _DYNAMIC is always set to the start of the
401
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
402
     only want to define it if we are, in fact, creating a .dynamic
403
     section.  We don't want to define it if there is no .dynamic
404
     section, since on some ELF platforms the start up code examines it
405
     to decide how to initialize the process.  */
406
0
  h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
407
0
  elf_hash_table (info)->hdynamic = h;
408
0
  if (h == NULL)
409
0
    return false;
410
411
0
  if (info->emit_hash)
412
0
    {
413
0
      s = bfd_make_section_anyway_with_flags (abfd, ".hash",
414
0
                flags | SEC_READONLY);
415
0
      if (s == NULL
416
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
417
0
  return false;
418
0
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
419
0
    }
420
421
0
  if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
422
0
    {
423
0
      s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
424
0
                flags | SEC_READONLY);
425
0
      if (s == NULL
426
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
427
0
  return false;
428
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
429
   4 32-bit words followed by variable count of 64-bit words, then
430
   variable count of 32-bit words.  */
431
0
      if (bed->s->arch_size == 64)
432
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
433
0
      else
434
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
435
0
    }
436
437
0
  if (info->enable_dt_relr)
438
0
    {
439
0
      s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
440
0
                flags | SEC_READONLY);
441
0
      if (s == NULL
442
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
443
0
  return false;
444
0
      elf_hash_table (info)->srelrdyn = s;
445
0
    }
446
447
  /* Let the backend create the rest of the sections.  This lets the
448
     backend set the right flags.  The backend will normally create
449
     the .got and .plt sections.  */
450
0
  if (bed->elf_backend_create_dynamic_sections == NULL
451
0
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
452
0
    return false;
453
454
0
  elf_hash_table (info)->dynamic_sections_created = true;
455
456
0
  return true;
457
0
}
458
459
/* Create dynamic sections when linking against a dynamic object.  */
460
461
bool
462
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
463
0
{
464
0
  flagword flags, pltflags;
465
0
  struct elf_link_hash_entry *h;
466
0
  asection *s;
467
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
468
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
469
470
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
471
     .rel[a].bss sections.  */
472
0
  flags = bed->dynamic_sec_flags;
473
474
0
  pltflags = flags;
475
0
  if (bed->plt_not_loaded)
476
    /* We do not clear SEC_ALLOC here because we still want the OS to
477
       allocate space for the section; it's just that there's nothing
478
       to read in from the object file.  */
479
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
480
0
  else
481
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
482
0
  if (bed->plt_readonly)
483
0
    pltflags |= SEC_READONLY;
484
485
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
486
0
  if (s == NULL
487
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
488
0
    return false;
489
0
  htab->splt = s;
490
491
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
492
     .plt section.  */
493
0
  if (bed->want_plt_sym)
494
0
    {
495
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
496
0
               "_PROCEDURE_LINKAGE_TABLE_");
497
0
      elf_hash_table (info)->hplt = h;
498
0
      if (h == NULL)
499
0
  return false;
500
0
    }
501
502
0
  s = bfd_make_section_anyway_with_flags (abfd,
503
0
            (bed->rela_plts_and_copies_p
504
0
             ? ".rela.plt" : ".rel.plt"),
505
0
            flags | SEC_READONLY);
506
0
  if (s == NULL
507
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
508
0
    return false;
509
0
  htab->srelplt = s;
510
511
0
  if (! _bfd_elf_create_got_section (abfd, info))
512
0
    return false;
513
514
0
  if (bed->want_dynbss)
515
0
    {
516
      /* The .dynbss section is a place to put symbols which are defined
517
   by dynamic objects, are referenced by regular objects, and are
518
   not functions.  We must allocate space for them in the process
519
   image and use a R_*_COPY reloc to tell the dynamic linker to
520
   initialize them at run time.  The linker script puts the .dynbss
521
   section into the .bss section of the final image.  */
522
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
523
0
                SEC_ALLOC | SEC_LINKER_CREATED);
524
0
      if (s == NULL)
525
0
  return false;
526
0
      htab->sdynbss = s;
527
528
0
      if (bed->want_dynrelro)
529
0
  {
530
    /* Similarly, but for symbols that were originally in read-only
531
       sections.  This section doesn't really need to have contents,
532
       but make it like other .data.rel.ro sections.  */
533
0
    s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
534
0
              flags);
535
0
    if (s == NULL)
536
0
      return false;
537
0
    htab->sdynrelro = s;
538
0
  }
539
540
      /* The .rel[a].bss section holds copy relocs.  This section is not
541
   normally needed.  We need to create it here, though, so that the
542
   linker will map it to an output section.  We can't just create it
543
   only if we need it, because we will not know whether we need it
544
   until we have seen all the input files, and the first time the
545
   main linker code calls BFD after examining all the input files
546
   (size_dynamic_sections) the input sections have already been
547
   mapped to the output sections.  If the section turns out not to
548
   be needed, we can discard it later.  We will never need this
549
   section when generating a shared object, since they do not use
550
   copy relocs.  */
551
0
      if (bfd_link_executable (info))
552
0
  {
553
0
    s = bfd_make_section_anyway_with_flags (abfd,
554
0
              (bed->rela_plts_and_copies_p
555
0
               ? ".rela.bss" : ".rel.bss"),
556
0
              flags | SEC_READONLY);
557
0
    if (s == NULL
558
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
559
0
      return false;
560
0
    htab->srelbss = s;
561
562
0
    if (bed->want_dynrelro)
563
0
      {
564
0
        s = (bfd_make_section_anyway_with_flags
565
0
       (abfd, (bed->rela_plts_and_copies_p
566
0
         ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
567
0
        flags | SEC_READONLY));
568
0
        if (s == NULL
569
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
570
0
    return false;
571
0
        htab->sreldynrelro = s;
572
0
      }
573
0
  }
574
0
    }
575
576
0
  return true;
577
0
}
578

579
/* Record a new dynamic symbol.  We record the dynamic symbols as we
580
   read the input files, since we need to have a list of all of them
581
   before we can determine the final sizes of the output sections.
582
   Note that we may actually call this function even though we are not
583
   going to output any dynamic symbols; in some cases we know that a
584
   symbol should be in the dynamic symbol table, but only if there is
585
   one.  */
586
587
bool
588
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
589
            struct elf_link_hash_entry *h)
590
0
{
591
0
  if (h->dynindx == -1)
592
0
    {
593
0
      struct elf_strtab_hash *dynstr;
594
0
      char *p;
595
0
      const char *name;
596
0
      size_t indx;
597
598
0
      if (h->root.type == bfd_link_hash_defined
599
0
    || h->root.type == bfd_link_hash_defweak)
600
0
  {
601
    /* An IR symbol should not be made dynamic.  */
602
0
    if (h->root.u.def.section != NULL
603
0
        && h->root.u.def.section->owner != NULL
604
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
605
0
      return true;
606
0
  }
607
608
      /* XXX: The ABI draft says the linker must turn hidden and
609
   internal symbols into STB_LOCAL symbols when producing the
610
   DSO. However, if ld.so honors st_other in the dynamic table,
611
   this would not be necessary.  */
612
0
      switch (ELF_ST_VISIBILITY (h->other))
613
0
  {
614
0
  case STV_INTERNAL:
615
0
  case STV_HIDDEN:
616
0
    if (h->root.type != bfd_link_hash_undefined
617
0
        && h->root.type != bfd_link_hash_undefweak)
618
0
      {
619
0
        h->forced_local = 1;
620
0
        return true;
621
0
      }
622
623
0
  default:
624
0
    break;
625
0
  }
626
627
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
628
0
      ++elf_hash_table (info)->dynsymcount;
629
630
0
      dynstr = elf_hash_table (info)->dynstr;
631
0
      if (dynstr == NULL)
632
0
  {
633
    /* Create a strtab to hold the dynamic symbol names.  */
634
0
    elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
635
0
    if (dynstr == NULL)
636
0
      return false;
637
0
  }
638
639
0
      char *unversioned_name = NULL;
640
641
      /* We don't put any version information in the dynamic string
642
   table.  */
643
0
      name = h->root.root.string;
644
0
      p = strchr (name, ELF_VER_CHR);
645
0
      if (p != NULL)
646
0
  {
647
0
    unversioned_name = bfd_malloc (p - name + 1);
648
0
    memcpy (unversioned_name, name, p - name);
649
0
    unversioned_name[p - name] = 0;
650
0
    name = unversioned_name;
651
0
  }
652
653
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
654
655
0
      if (p != NULL)
656
0
  free (unversioned_name);
657
658
0
      if (indx == (size_t) -1)
659
0
  return false;
660
0
      h->dynstr_index = indx;
661
0
    }
662
663
0
  return true;
664
0
}
665

666
/* Mark a symbol dynamic.  */
667
668
static void
669
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
670
          struct elf_link_hash_entry *h,
671
          Elf_Internal_Sym *sym)
672
0
{
673
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
674
675
  /* It may be called more than once on the same H.  */
676
0
  if(h->dynamic || bfd_link_relocatable (info))
677
0
    return;
678
679
0
  if ((info->dynamic_data
680
0
       && (h->type == STT_OBJECT
681
0
     || h->type == STT_COMMON
682
0
     || (sym != NULL
683
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
684
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
685
0
      || (d != NULL
686
0
    && h->non_elf
687
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
688
0
    {
689
0
      h->dynamic = 1;
690
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
691
   non-IR reference.  */
692
0
      h->root.non_ir_ref_dynamic = 1;
693
0
    }
694
0
}
695
696
/* Record an assignment to a symbol made by a linker script.  We need
697
   this in case some dynamic object refers to this symbol.  */
698
699
bool
700
bfd_elf_record_link_assignment (bfd *output_bfd,
701
        struct bfd_link_info *info,
702
        const char *name,
703
        bool provide,
704
        bool hidden)
705
0
{
706
0
  struct elf_link_hash_entry *h, *hv;
707
0
  struct elf_link_hash_table *htab;
708
0
  const struct elf_backend_data *bed;
709
710
0
  if (!is_elf_hash_table (info->hash))
711
0
    return true;
712
713
0
  htab = elf_hash_table (info);
714
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
715
0
  if (h == NULL)
716
0
    return provide;
717
718
0
  if (h->root.type == bfd_link_hash_warning)
719
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
720
721
0
  if (h->versioned == unknown)
722
0
    {
723
      /* Set versioned if symbol version is unknown.  */
724
0
      char *version = strrchr (name, ELF_VER_CHR);
725
0
      if (version)
726
0
  {
727
0
    if (version > name && version[-1] != ELF_VER_CHR)
728
0
      h->versioned = versioned_hidden;
729
0
    else
730
0
      h->versioned = versioned;
731
0
  }
732
0
    }
733
734
  /* Symbols defined in a linker script but not referenced anywhere
735
     else will have non_elf set.  */
736
0
  if (h->non_elf)
737
0
    {
738
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
739
0
      h->non_elf = 0;
740
0
    }
741
742
0
  switch (h->root.type)
743
0
    {
744
0
    case bfd_link_hash_defined:
745
0
    case bfd_link_hash_defweak:
746
0
    case bfd_link_hash_common:
747
0
      break;
748
0
    case bfd_link_hash_undefweak:
749
0
    case bfd_link_hash_undefined:
750
      /* Since we're defining the symbol, don't let it seem to have not
751
   been defined.  record_dynamic_symbol and size_dynamic_sections
752
   may depend on this.  */
753
0
      h->root.type = bfd_link_hash_new;
754
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
755
0
  bfd_link_repair_undef_list (&htab->root);
756
0
      break;
757
0
    case bfd_link_hash_new:
758
0
      break;
759
0
    case bfd_link_hash_indirect:
760
      /* We had a versioned symbol in a dynamic library.  We make the
761
   the versioned symbol point to this one.  */
762
0
      bed = get_elf_backend_data (output_bfd);
763
0
      hv = h;
764
0
      while (hv->root.type == bfd_link_hash_indirect
765
0
       || hv->root.type == bfd_link_hash_warning)
766
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
767
      /* We don't need to update h->root.u since linker will set them
768
   later.  */
769
0
      h->root.type = bfd_link_hash_undefined;
770
0
      hv->root.type = bfd_link_hash_indirect;
771
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
772
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
773
0
      break;
774
0
    default:
775
0
      BFD_FAIL ();
776
0
      return false;
777
0
    }
778
779
  /* If this symbol is being provided by the linker script, and it is
780
     currently defined by a dynamic object, but not by a regular
781
     object, then mark it as undefined so that the generic linker will
782
     force the correct value.  */
783
0
  if (provide
784
0
      && h->def_dynamic
785
0
      && !h->def_regular)
786
0
    h->root.type = bfd_link_hash_undefined;
787
788
  /* If this symbol is currently defined by a dynamic object, but not
789
     by a regular object, then clear out any version information because
790
     the symbol will not be associated with the dynamic object any
791
     more.  */
792
0
  if (h->def_dynamic && !h->def_regular)
793
0
    h->verinfo.verdef = NULL;
794
795
  /* Make sure this symbol is not garbage collected.  */
796
0
  h->mark = 1;
797
798
0
  h->def_regular = 1;
799
800
0
  if (hidden)
801
0
    {
802
0
      bed = get_elf_backend_data (output_bfd);
803
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
804
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
805
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
806
0
    }
807
808
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
809
     and executables.  */
810
0
  if (!bfd_link_relocatable (info)
811
0
      && h->dynindx != -1
812
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
813
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
814
0
    h->forced_local = 1;
815
816
0
  if ((h->def_dynamic
817
0
       || h->ref_dynamic
818
0
       || bfd_link_dll (info))
819
0
      && !h->forced_local
820
0
      && h->dynindx == -1)
821
0
    {
822
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
823
0
  return false;
824
825
      /* If this is a weak defined symbol, and we know a corresponding
826
   real symbol from the same dynamic object, make sure the real
827
   symbol is also made into a dynamic symbol.  */
828
0
      if (h->is_weakalias)
829
0
  {
830
0
    struct elf_link_hash_entry *def = weakdef (h);
831
832
0
    if (def->dynindx == -1
833
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
834
0
      return false;
835
0
  }
836
0
    }
837
838
0
  return true;
839
0
}
840
841
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
842
   success, and 2 on a failure caused by attempting to record a symbol
843
   in a discarded section, eg. a discarded link-once section symbol.  */
844
845
int
846
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
847
            bfd *input_bfd,
848
            long input_indx)
849
0
{
850
0
  size_t amt;
851
0
  struct elf_link_local_dynamic_entry *entry;
852
0
  struct elf_link_hash_table *eht;
853
0
  struct elf_strtab_hash *dynstr;
854
0
  size_t dynstr_index;
855
0
  char *name;
856
0
  Elf_External_Sym_Shndx eshndx;
857
0
  char esym[sizeof (Elf64_External_Sym)];
858
859
0
  if (! is_elf_hash_table (info->hash))
860
0
    return 0;
861
862
  /* See if the entry exists already.  */
863
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
864
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
865
0
      return 1;
866
867
0
  amt = sizeof (*entry);
868
0
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
869
0
  if (entry == NULL)
870
0
    return 0;
871
872
  /* Go find the symbol, so that we can find it's name.  */
873
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
874
0
           1, input_indx, &entry->isym, esym, &eshndx))
875
0
    {
876
0
      bfd_release (input_bfd, entry);
877
0
      return 0;
878
0
    }
879
880
0
  if (entry->isym.st_shndx != SHN_UNDEF
881
0
      && entry->isym.st_shndx < SHN_LORESERVE)
882
0
    {
883
0
      asection *s;
884
885
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
886
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
887
0
  {
888
    /* We can still bfd_release here as nothing has done another
889
       bfd_alloc.  We can't do this later in this function.  */
890
0
    bfd_release (input_bfd, entry);
891
0
    return 2;
892
0
  }
893
0
    }
894
895
0
  name = (bfd_elf_string_from_elf_section
896
0
    (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
897
0
     entry->isym.st_name));
898
899
0
  dynstr = elf_hash_table (info)->dynstr;
900
0
  if (dynstr == NULL)
901
0
    {
902
      /* Create a strtab to hold the dynamic symbol names.  */
903
0
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
904
0
      if (dynstr == NULL)
905
0
  return 0;
906
0
    }
907
908
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
909
0
  if (dynstr_index == (size_t) -1)
910
0
    return 0;
911
0
  entry->isym.st_name = dynstr_index;
912
913
0
  eht = elf_hash_table (info);
914
915
0
  entry->next = eht->dynlocal;
916
0
  eht->dynlocal = entry;
917
0
  entry->input_bfd = input_bfd;
918
0
  entry->input_indx = input_indx;
919
0
  eht->dynsymcount++;
920
921
  /* Whatever binding the symbol had before, it's now local.  */
922
0
  entry->isym.st_info
923
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
924
925
  /* The dynindx will be set at the end of size_dynamic_sections.  */
926
927
0
  return 1;
928
0
}
929
930
/* Return the dynindex of a local dynamic symbol.  */
931
932
long
933
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
934
            bfd *input_bfd,
935
            long input_indx)
936
0
{
937
0
  struct elf_link_local_dynamic_entry *e;
938
939
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
940
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
941
0
      return e->dynindx;
942
0
  return -1;
943
0
}
944
945
/* This function is used to renumber the dynamic symbols, if some of
946
   them are removed because they are marked as local.  This is called
947
   via elf_link_hash_traverse.  */
948
949
static bool
950
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
951
              void *data)
952
0
{
953
0
  size_t *count = (size_t *) data;
954
955
0
  if (h->forced_local)
956
0
    return true;
957
958
0
  if (h->dynindx != -1)
959
0
    h->dynindx = ++(*count);
960
961
0
  return true;
962
0
}
963
964
965
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
966
   STB_LOCAL binding.  */
967
968
static bool
969
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
970
              void *data)
971
0
{
972
0
  size_t *count = (size_t *) data;
973
974
0
  if (!h->forced_local)
975
0
    return true;
976
977
0
  if (h->dynindx != -1)
978
0
    h->dynindx = ++(*count);
979
980
0
  return true;
981
0
}
982
983
/* Return true if the dynamic symbol for a given section should be
984
   omitted when creating a shared library.  */
985
bool
986
_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
987
              struct bfd_link_info *info,
988
              asection *p)
989
0
{
990
0
  struct elf_link_hash_table *htab;
991
0
  asection *ip;
992
993
0
  switch (elf_section_data (p)->this_hdr.sh_type)
994
0
    {
995
0
    case SHT_PROGBITS:
996
0
    case SHT_NOBITS:
997
      /* If sh_type is yet undecided, assume it could be
998
   SHT_PROGBITS/SHT_NOBITS.  */
999
0
    case SHT_NULL:
1000
0
      htab = elf_hash_table (info);
1001
0
      if (htab->text_index_section != NULL)
1002
0
  return p != htab->text_index_section && p != htab->data_index_section;
1003
1004
0
      return (htab->dynobj != NULL
1005
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1006
0
        && ip->output_section == p);
1007
1008
      /* There shouldn't be section relative relocations
1009
   against any other section.  */
1010
0
    default:
1011
0
      return true;
1012
0
    }
1013
0
}
1014
1015
bool
1016
_bfd_elf_omit_section_dynsym_all
1017
    (bfd *output_bfd ATTRIBUTE_UNUSED,
1018
     struct bfd_link_info *info ATTRIBUTE_UNUSED,
1019
     asection *p ATTRIBUTE_UNUSED)
1020
0
{
1021
0
  return true;
1022
0
}
1023
1024
/* Assign dynsym indices.  In a shared library we generate a section
1025
   symbol for each output section, which come first.  Next come symbols
1026
   which have been forced to local binding.  Then all of the back-end
1027
   allocated local dynamic syms, followed by the rest of the global
1028
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1029
   (This prevents the early call before elf_backend_init_index_section
1030
   and strip_excluded_output_sections setting dynindx for sections
1031
   that are stripped.)  */
1032
1033
static unsigned long
1034
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
1035
        struct bfd_link_info *info,
1036
        unsigned long *section_sym_count)
1037
0
{
1038
0
  unsigned long dynsymcount = 0;
1039
0
  bool do_sec = section_sym_count != NULL;
1040
1041
0
  if (bfd_link_pic (info)
1042
0
      || elf_hash_table (info)->is_relocatable_executable)
1043
0
    {
1044
0
      const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1045
0
      asection *p;
1046
0
      for (p = output_bfd->sections; p ; p = p->next)
1047
0
  if ((p->flags & SEC_EXCLUDE) == 0
1048
0
      && (p->flags & SEC_ALLOC) != 0
1049
0
      && elf_hash_table (info)->dynamic_relocs
1050
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1051
0
    {
1052
0
      ++dynsymcount;
1053
0
      if (do_sec)
1054
0
        elf_section_data (p)->dynindx = dynsymcount;
1055
0
    }
1056
0
  else if (do_sec)
1057
0
    elf_section_data (p)->dynindx = 0;
1058
0
    }
1059
0
  if (do_sec)
1060
0
    *section_sym_count = dynsymcount;
1061
1062
0
  elf_link_hash_traverse (elf_hash_table (info),
1063
0
        elf_link_renumber_local_hash_table_dynsyms,
1064
0
        &dynsymcount);
1065
1066
0
  if (elf_hash_table (info)->dynlocal)
1067
0
    {
1068
0
      struct elf_link_local_dynamic_entry *p;
1069
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1070
0
  p->dynindx = ++dynsymcount;
1071
0
    }
1072
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1073
1074
0
  elf_link_hash_traverse (elf_hash_table (info),
1075
0
        elf_link_renumber_hash_table_dynsyms,
1076
0
        &dynsymcount);
1077
1078
  /* There is an unused NULL entry at the head of the table which we
1079
     must account for in our count even if the table is empty since it
1080
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1081
     .dynamic section.  */
1082
0
  dynsymcount++;
1083
1084
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1085
0
  return dynsymcount;
1086
0
}
1087
1088
/* Merge st_other field.  */
1089
1090
static void
1091
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1092
        unsigned int st_other, asection *sec,
1093
        bool definition, bool dynamic)
1094
0
{
1095
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1096
1097
  /* If st_other has a processor-specific meaning, specific
1098
     code might be needed here.  */
1099
0
  if (bed->elf_backend_merge_symbol_attribute)
1100
0
    (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1101
0
            dynamic);
1102
1103
0
  if (!dynamic)
1104
0
    {
1105
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1106
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1107
1108
      /* Keep the most constraining visibility.  Leave the remainder
1109
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1110
0
      if (symvis - 1 < hvis - 1)
1111
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1112
0
    }
1113
0
  else if (definition
1114
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1115
0
     && (sec->flags & SEC_READONLY) == 0)
1116
0
    h->protected_def = 1;
1117
0
}
1118
1119
/* This function is called when we want to merge a new symbol with an
1120
   existing symbol.  It handles the various cases which arise when we
1121
   find a definition in a dynamic object, or when there is already a
1122
   definition in a dynamic object.  The new symbol is described by
1123
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1124
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1125
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1126
   of an old common symbol.  We set OVERRIDE if the old symbol is
1127
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1128
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1129
   to change.  By OK to change, we mean that we shouldn't warn if the
1130
   type or size does change.  */
1131
1132
static bool
1133
_bfd_elf_merge_symbol (bfd *abfd,
1134
           struct bfd_link_info *info,
1135
           const char *name,
1136
           Elf_Internal_Sym *sym,
1137
           asection **psec,
1138
           bfd_vma *pvalue,
1139
           struct elf_link_hash_entry **sym_hash,
1140
           bfd **poldbfd,
1141
           bool *pold_weak,
1142
           unsigned int *pold_alignment,
1143
           bool *skip,
1144
           bfd **override,
1145
           bool *type_change_ok,
1146
           bool *size_change_ok,
1147
           bool *matched)
1148
0
{
1149
0
  asection *sec, *oldsec;
1150
0
  struct elf_link_hash_entry *h;
1151
0
  struct elf_link_hash_entry *hi;
1152
0
  struct elf_link_hash_entry *flip;
1153
0
  int bind;
1154
0
  bfd *oldbfd;
1155
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1156
0
  bool newweak, oldweak, newfunc, oldfunc;
1157
0
  const struct elf_backend_data *bed;
1158
0
  char *new_version;
1159
0
  bool default_sym = *matched;
1160
0
  struct elf_link_hash_table *htab;
1161
1162
0
  *skip = false;
1163
0
  *override = NULL;
1164
1165
0
  sec = *psec;
1166
0
  bind = ELF_ST_BIND (sym->st_info);
1167
1168
0
  if (! bfd_is_und_section (sec))
1169
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1170
0
  else
1171
0
    h = ((struct elf_link_hash_entry *)
1172
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1173
0
  if (h == NULL)
1174
0
    return false;
1175
0
  *sym_hash = h;
1176
1177
0
  bed = get_elf_backend_data (abfd);
1178
1179
  /* NEW_VERSION is the symbol version of the new symbol.  */
1180
0
  if (h->versioned != unversioned)
1181
0
    {
1182
      /* Symbol version is unknown or versioned.  */
1183
0
      new_version = strrchr (name, ELF_VER_CHR);
1184
0
      if (new_version)
1185
0
  {
1186
0
    if (h->versioned == unknown)
1187
0
      {
1188
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1189
0
    h->versioned = versioned_hidden;
1190
0
        else
1191
0
    h->versioned = versioned;
1192
0
      }
1193
0
    new_version += 1;
1194
0
    if (new_version[0] == '\0')
1195
0
      new_version = NULL;
1196
0
  }
1197
0
      else
1198
0
  h->versioned = unversioned;
1199
0
    }
1200
0
  else
1201
0
    new_version = NULL;
1202
1203
  /* For merging, we only care about real symbols.  But we need to make
1204
     sure that indirect symbol dynamic flags are updated.  */
1205
0
  hi = h;
1206
0
  while (h->root.type == bfd_link_hash_indirect
1207
0
   || h->root.type == bfd_link_hash_warning)
1208
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1209
1210
0
  if (!*matched)
1211
0
    {
1212
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1213
0
  *matched = true;
1214
0
      else
1215
0
  {
1216
    /* OLD_HIDDEN is true if the existing symbol is only visible
1217
       to the symbol with the same symbol version.  NEW_HIDDEN is
1218
       true if the new symbol is only visible to the symbol with
1219
       the same symbol version.  */
1220
0
    bool old_hidden = h->versioned == versioned_hidden;
1221
0
    bool new_hidden = hi->versioned == versioned_hidden;
1222
0
    if (!old_hidden && !new_hidden)
1223
      /* The new symbol matches the existing symbol if both
1224
         aren't hidden.  */
1225
0
      *matched = true;
1226
0
    else
1227
0
      {
1228
        /* OLD_VERSION is the symbol version of the existing
1229
     symbol. */
1230
0
        char *old_version;
1231
1232
0
        if (h->versioned >= versioned)
1233
0
    old_version = strrchr (h->root.root.string,
1234
0
               ELF_VER_CHR) + 1;
1235
0
        else
1236
0
     old_version = NULL;
1237
1238
        /* The new symbol matches the existing symbol if they
1239
     have the same symbol version.  */
1240
0
        *matched = (old_version == new_version
1241
0
        || (old_version != NULL
1242
0
            && new_version != NULL
1243
0
            && strcmp (old_version, new_version) == 0));
1244
0
      }
1245
0
  }
1246
0
    }
1247
1248
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1249
     existing symbol.  */
1250
1251
0
  oldbfd = NULL;
1252
0
  oldsec = NULL;
1253
0
  switch (h->root.type)
1254
0
    {
1255
0
    default:
1256
0
      break;
1257
1258
0
    case bfd_link_hash_undefined:
1259
0
    case bfd_link_hash_undefweak:
1260
0
      oldbfd = h->root.u.undef.abfd;
1261
0
      break;
1262
1263
0
    case bfd_link_hash_defined:
1264
0
    case bfd_link_hash_defweak:
1265
0
      oldbfd = h->root.u.def.section->owner;
1266
0
      oldsec = h->root.u.def.section;
1267
0
      break;
1268
1269
0
    case bfd_link_hash_common:
1270
0
      oldbfd = h->root.u.c.p->section->owner;
1271
0
      oldsec = h->root.u.c.p->section;
1272
0
      if (pold_alignment)
1273
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1274
0
      break;
1275
0
    }
1276
0
  if (poldbfd && *poldbfd == NULL)
1277
0
    *poldbfd = oldbfd;
1278
1279
  /* Differentiate strong and weak symbols.  */
1280
0
  newweak = bind == STB_WEAK;
1281
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1282
0
       || h->root.type == bfd_link_hash_undefweak);
1283
0
  if (pold_weak)
1284
0
    *pold_weak = oldweak;
1285
1286
  /* We have to check it for every instance since the first few may be
1287
     references and not all compilers emit symbol type for undefined
1288
     symbols.  */
1289
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1290
1291
0
  htab = elf_hash_table (info);
1292
1293
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1294
     respectively, is from a dynamic object.  */
1295
1296
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1297
1298
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1299
     syms and defined syms in dynamic libraries respectively.
1300
     ref_dynamic on the other hand can be set for a symbol defined in
1301
     a dynamic library, and def_dynamic may not be set;  When the
1302
     definition in a dynamic lib is overridden by a definition in the
1303
     executable use of the symbol in the dynamic lib becomes a
1304
     reference to the executable symbol.  */
1305
0
  if (newdyn)
1306
0
    {
1307
0
      if (bfd_is_und_section (sec))
1308
0
  {
1309
0
    if (bind != STB_WEAK)
1310
0
      {
1311
0
        h->ref_dynamic_nonweak = 1;
1312
0
        hi->ref_dynamic_nonweak = 1;
1313
0
      }
1314
0
  }
1315
0
      else
1316
0
  {
1317
    /* Update the existing symbol only if they match. */
1318
0
    if (*matched)
1319
0
      h->dynamic_def = 1;
1320
0
    hi->dynamic_def = 1;
1321
0
  }
1322
0
    }
1323
1324
  /* If we just created the symbol, mark it as being an ELF symbol.
1325
     Other than that, there is nothing to do--there is no merge issue
1326
     with a newly defined symbol--so we just return.  */
1327
1328
0
  if (h->root.type == bfd_link_hash_new)
1329
0
    {
1330
0
      h->non_elf = 0;
1331
0
      return true;
1332
0
    }
1333
1334
  /* In cases involving weak versioned symbols, we may wind up trying
1335
     to merge a symbol with itself.  Catch that here, to avoid the
1336
     confusion that results if we try to override a symbol with
1337
     itself.  The additional tests catch cases like
1338
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1339
     dynamic object, which we do want to handle here.  */
1340
0
  if (abfd == oldbfd
1341
0
      && (newweak || oldweak)
1342
0
      && ((abfd->flags & DYNAMIC) == 0
1343
0
    || !h->def_regular))
1344
0
    return true;
1345
1346
0
  olddyn = false;
1347
0
  if (oldbfd != NULL)
1348
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1349
0
  else if (oldsec != NULL)
1350
0
    {
1351
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1352
   indices used by MIPS ELF.  */
1353
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1354
0
    }
1355
1356
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1357
0
  if (!htab->handling_dt_needed
1358
0
      && oldbfd != NULL
1359
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1360
0
    {
1361
0
      if (newdyn != olddyn)
1362
0
  {
1363
    /* Handle a case where plugin_notice won't be called and thus
1364
       won't set the non_ir_ref flags on the first pass over
1365
       symbols.  */
1366
0
    h->root.non_ir_ref_dynamic = true;
1367
0
    hi->root.non_ir_ref_dynamic = true;
1368
0
  }
1369
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1370
0
         && hi->root.type == bfd_link_hash_indirect)
1371
0
  {
1372
    /* Change indirect symbol from IR to undefined.  */
1373
0
    hi->root.type = bfd_link_hash_undefined;
1374
0
    hi->root.u.undef.abfd = oldbfd;
1375
0
  }
1376
0
    }
1377
1378
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1379
     respectively, appear to be a definition rather than reference.  */
1380
1381
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1382
1383
0
  olddef = (h->root.type != bfd_link_hash_undefined
1384
0
      && h->root.type != bfd_link_hash_undefweak
1385
0
      && h->root.type != bfd_link_hash_common);
1386
1387
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1388
     respectively, appear to be a function.  */
1389
1390
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1391
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1392
1393
0
  oldfunc = (h->type != STT_NOTYPE
1394
0
       && bed->is_function_type (h->type));
1395
1396
0
  if (!(newfunc && oldfunc)
1397
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1398
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1399
0
      && h->type != STT_NOTYPE
1400
0
      && (newdef || bfd_is_com_section (sec))
1401
0
      && (olddef || h->root.type == bfd_link_hash_common))
1402
0
    {
1403
      /* If creating a default indirect symbol ("foo" or "foo@") from
1404
   a dynamic versioned definition ("foo@@") skip doing so if
1405
   there is an existing regular definition with a different
1406
   type.  We don't want, for example, a "time" variable in the
1407
   executable overriding a "time" function in a shared library.  */
1408
0
      if (newdyn
1409
0
    && !olddyn)
1410
0
  {
1411
0
    *skip = true;
1412
0
    return true;
1413
0
  }
1414
1415
      /* When adding a symbol from a regular object file after we have
1416
   created indirect symbols, undo the indirection and any
1417
   dynamic state.  */
1418
0
      if (hi != h
1419
0
    && !newdyn
1420
0
    && olddyn)
1421
0
  {
1422
0
    h = hi;
1423
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1424
0
    h->forced_local = 0;
1425
0
    h->ref_dynamic = 0;
1426
0
    h->def_dynamic = 0;
1427
0
    h->dynamic_def = 0;
1428
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1429
0
      {
1430
0
        h->root.type = bfd_link_hash_undefined;
1431
0
        h->root.u.undef.abfd = abfd;
1432
0
      }
1433
0
    else
1434
0
      {
1435
0
        h->root.type = bfd_link_hash_new;
1436
0
        h->root.u.undef.abfd = NULL;
1437
0
      }
1438
0
    return true;
1439
0
  }
1440
0
    }
1441
1442
  /* Check TLS symbols.  We don't check undefined symbols introduced
1443
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1444
     check symbols from plugins because they also have no type.  */
1445
0
  if (oldbfd != NULL
1446
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1447
0
      && (abfd->flags & BFD_PLUGIN) == 0
1448
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1449
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1450
0
    {
1451
0
      bfd *ntbfd, *tbfd;
1452
0
      bool ntdef, tdef;
1453
0
      asection *ntsec, *tsec;
1454
1455
0
      if (h->type == STT_TLS)
1456
0
  {
1457
0
    ntbfd = abfd;
1458
0
    ntsec = sec;
1459
0
    ntdef = newdef;
1460
0
    tbfd = oldbfd;
1461
0
    tsec = oldsec;
1462
0
    tdef = olddef;
1463
0
  }
1464
0
      else
1465
0
  {
1466
0
    ntbfd = oldbfd;
1467
0
    ntsec = oldsec;
1468
0
    ntdef = olddef;
1469
0
    tbfd = abfd;
1470
0
    tsec = sec;
1471
0
    tdef = newdef;
1472
0
  }
1473
1474
0
      if (tdef && ntdef)
1475
0
  _bfd_error_handler
1476
    /* xgettext:c-format */
1477
0
    (_("%s: TLS definition in %pB section %pA "
1478
0
       "mismatches non-TLS definition in %pB section %pA"),
1479
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1480
0
      else if (!tdef && !ntdef)
1481
0
  _bfd_error_handler
1482
    /* xgettext:c-format */
1483
0
    (_("%s: TLS reference in %pB "
1484
0
       "mismatches non-TLS reference in %pB"),
1485
0
     h->root.root.string, tbfd, ntbfd);
1486
0
      else if (tdef)
1487
0
  _bfd_error_handler
1488
    /* xgettext:c-format */
1489
0
    (_("%s: TLS definition in %pB section %pA "
1490
0
       "mismatches non-TLS reference in %pB"),
1491
0
     h->root.root.string, tbfd, tsec, ntbfd);
1492
0
      else
1493
0
  _bfd_error_handler
1494
    /* xgettext:c-format */
1495
0
    (_("%s: TLS reference in %pB "
1496
0
       "mismatches non-TLS definition in %pB section %pA"),
1497
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1498
1499
0
      bfd_set_error (bfd_error_bad_value);
1500
0
      return false;
1501
0
    }
1502
1503
  /* If the old symbol has non-default visibility, we ignore the new
1504
     definition from a dynamic object.  */
1505
0
  if (newdyn
1506
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1507
0
      && !bfd_is_und_section (sec))
1508
0
    {
1509
0
      *skip = true;
1510
      /* Make sure this symbol is dynamic.  */
1511
0
      h->ref_dynamic = 1;
1512
0
      hi->ref_dynamic = 1;
1513
      /* A protected symbol has external availability. Make sure it is
1514
   recorded as dynamic.
1515
1516
   FIXME: Should we check type and size for protected symbol?  */
1517
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1518
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1519
0
      else
1520
0
  return true;
1521
0
    }
1522
0
  else if (!newdyn
1523
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1524
0
     && h->def_dynamic)
1525
0
    {
1526
      /* If the new symbol with non-default visibility comes from a
1527
   relocatable file and the old definition comes from a dynamic
1528
   object, we remove the old definition.  */
1529
0
      if (hi->root.type == bfd_link_hash_indirect)
1530
0
  {
1531
    /* Handle the case where the old dynamic definition is
1532
       default versioned.  We need to copy the symbol info from
1533
       the symbol with default version to the normal one if it
1534
       was referenced before.  */
1535
0
    if (h->ref_regular)
1536
0
      {
1537
0
        hi->root.type = h->root.type;
1538
0
        h->root.type = bfd_link_hash_indirect;
1539
0
        (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1540
1541
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1542
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1543
0
    {
1544
      /* If the new symbol is hidden or internal, completely undo
1545
         any dynamic link state.  */
1546
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1547
0
      h->forced_local = 0;
1548
0
      h->ref_dynamic = 0;
1549
0
    }
1550
0
        else
1551
0
    h->ref_dynamic = 1;
1552
1553
0
        h->def_dynamic = 0;
1554
        /* FIXME: Should we check type and size for protected symbol?  */
1555
0
        h->size = 0;
1556
0
        h->type = 0;
1557
1558
0
        h = hi;
1559
0
      }
1560
0
    else
1561
0
      h = hi;
1562
0
  }
1563
1564
      /* If the old symbol was undefined before, then it will still be
1565
   on the undefs list.  If the new symbol is undefined or
1566
   common, we can't make it bfd_link_hash_new here, because new
1567
   undefined or common symbols will be added to the undefs list
1568
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1569
   added twice to the undefs list.  Also, if the new symbol is
1570
   undefweak then we don't want to lose the strong undef.  */
1571
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1572
0
  {
1573
0
    h->root.type = bfd_link_hash_undefined;
1574
0
    h->root.u.undef.abfd = abfd;
1575
0
  }
1576
0
      else
1577
0
  {
1578
0
    h->root.type = bfd_link_hash_new;
1579
0
    h->root.u.undef.abfd = NULL;
1580
0
  }
1581
1582
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1583
0
  {
1584
    /* If the new symbol is hidden or internal, completely undo
1585
       any dynamic link state.  */
1586
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
1587
0
    h->forced_local = 0;
1588
0
    h->ref_dynamic = 0;
1589
0
  }
1590
0
      else
1591
0
  h->ref_dynamic = 1;
1592
0
      h->def_dynamic = 0;
1593
      /* FIXME: Should we check type and size for protected symbol?  */
1594
0
      h->size = 0;
1595
0
      h->type = 0;
1596
0
      return true;
1597
0
    }
1598
1599
  /* If a new weak symbol definition comes from a regular file and the
1600
     old symbol comes from a dynamic library, we treat the new one as
1601
     strong.  Similarly, an old weak symbol definition from a regular
1602
     file is treated as strong when the new symbol comes from a dynamic
1603
     library.  Further, an old weak symbol from a dynamic library is
1604
     treated as strong if the new symbol is from a dynamic library.
1605
     This reflects the way glibc's ld.so works.
1606
1607
     Also allow a weak symbol to override a linker script symbol
1608
     defined by an early pass over the script.  This is done so the
1609
     linker knows the symbol is defined in an object file, for the
1610
     DEFINED script function.
1611
1612
     Do this before setting *type_change_ok or *size_change_ok so that
1613
     we warn properly when dynamic library symbols are overridden.  */
1614
1615
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1616
0
    newweak = false;
1617
0
  if (olddef && newdyn)
1618
0
    oldweak = false;
1619
1620
  /* Allow changes between different types of function symbol.  */
1621
0
  if (newfunc && oldfunc)
1622
0
    *type_change_ok = true;
1623
1624
  /* It's OK to change the type if either the existing symbol or the
1625
     new symbol is weak.  A type change is also OK if the old symbol
1626
     is undefined and the new symbol is defined.  */
1627
1628
0
  if (oldweak
1629
0
      || newweak
1630
0
      || (newdef
1631
0
    && h->root.type == bfd_link_hash_undefined))
1632
0
    *type_change_ok = true;
1633
1634
  /* It's OK to change the size if either the existing symbol or the
1635
     new symbol is weak, or if the old symbol is undefined.  */
1636
1637
0
  if (*type_change_ok
1638
0
      || h->root.type == bfd_link_hash_undefined)
1639
0
    *size_change_ok = true;
1640
1641
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1642
     symbol, respectively, appears to be a common symbol in a dynamic
1643
     object.  If a symbol appears in an uninitialized section, and is
1644
     not weak, and is not a function, then it may be a common symbol
1645
     which was resolved when the dynamic object was created.  We want
1646
     to treat such symbols specially, because they raise special
1647
     considerations when setting the symbol size: if the symbol
1648
     appears as a common symbol in a regular object, and the size in
1649
     the regular object is larger, we must make sure that we use the
1650
     larger size.  This problematic case can always be avoided in C,
1651
     but it must be handled correctly when using Fortran shared
1652
     libraries.
1653
1654
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1655
     likewise for OLDDYNCOMMON and OLDDEF.
1656
1657
     Note that this test is just a heuristic, and that it is quite
1658
     possible to have an uninitialized symbol in a shared object which
1659
     is really a definition, rather than a common symbol.  This could
1660
     lead to some minor confusion when the symbol really is a common
1661
     symbol in some regular object.  However, I think it will be
1662
     harmless.  */
1663
1664
0
  if (newdyn
1665
0
      && newdef
1666
0
      && !newweak
1667
0
      && (sec->flags & SEC_ALLOC) != 0
1668
0
      && (sec->flags & SEC_LOAD) == 0
1669
0
      && sym->st_size > 0
1670
0
      && !newfunc)
1671
0
    newdyncommon = true;
1672
0
  else
1673
0
    newdyncommon = false;
1674
1675
0
  if (olddyn
1676
0
      && olddef
1677
0
      && h->root.type == bfd_link_hash_defined
1678
0
      && h->def_dynamic
1679
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1680
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1681
0
      && h->size > 0
1682
0
      && !oldfunc)
1683
0
    olddyncommon = true;
1684
0
  else
1685
0
    olddyncommon = false;
1686
1687
  /* We now know everything about the old and new symbols.  We ask the
1688
     backend to check if we can merge them.  */
1689
0
  if (bed->merge_symbol != NULL)
1690
0
    {
1691
0
      if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1692
0
  return false;
1693
0
      sec = *psec;
1694
0
    }
1695
1696
  /* There are multiple definitions of a normal symbol.  Skip the
1697
     default symbol as well as definition from an IR object.  */
1698
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1699
0
      && !default_sym && h->def_regular
1700
0
      && !(oldbfd != NULL
1701
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1702
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1703
0
    {
1704
      /* Handle a multiple definition.  */
1705
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1706
0
                 abfd, sec, *pvalue);
1707
0
      *skip = true;
1708
0
      return true;
1709
0
    }
1710
1711
  /* If both the old and the new symbols look like common symbols in a
1712
     dynamic object, set the size of the symbol to the larger of the
1713
     two.  */
1714
1715
0
  if (olddyncommon
1716
0
      && newdyncommon
1717
0
      && sym->st_size != h->size)
1718
0
    {
1719
      /* Since we think we have two common symbols, issue a multiple
1720
   common warning if desired.  Note that we only warn if the
1721
   size is different.  If the size is the same, we simply let
1722
   the old symbol override the new one as normally happens with
1723
   symbols defined in dynamic objects.  */
1724
1725
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1726
0
             bfd_link_hash_common, sym->st_size);
1727
0
      if (sym->st_size > h->size)
1728
0
  h->size = sym->st_size;
1729
1730
0
      *size_change_ok = true;
1731
0
    }
1732
1733
  /* If we are looking at a dynamic object, and we have found a
1734
     definition, we need to see if the symbol was already defined by
1735
     some other object.  If so, we want to use the existing
1736
     definition, and we do not want to report a multiple symbol
1737
     definition error; we do this by clobbering *PSEC to be
1738
     bfd_und_section_ptr.
1739
1740
     We treat a common symbol as a definition if the symbol in the
1741
     shared library is a function, since common symbols always
1742
     represent variables; this can cause confusion in principle, but
1743
     any such confusion would seem to indicate an erroneous program or
1744
     shared library.  We also permit a common symbol in a regular
1745
     object to override a weak symbol in a shared object.  */
1746
1747
0
  if (newdyn
1748
0
      && newdef
1749
0
      && (olddef
1750
0
    || (h->root.type == bfd_link_hash_common
1751
0
        && (newweak || newfunc))))
1752
0
    {
1753
0
      *override = abfd;
1754
0
      newdef = false;
1755
0
      newdyncommon = false;
1756
1757
0
      *psec = sec = bfd_und_section_ptr;
1758
0
      *size_change_ok = true;
1759
1760
      /* If we get here when the old symbol is a common symbol, then
1761
   we are explicitly letting it override a weak symbol or
1762
   function in a dynamic object, and we don't want to warn about
1763
   a type change.  If the old symbol is a defined symbol, a type
1764
   change warning may still be appropriate.  */
1765
1766
0
      if (h->root.type == bfd_link_hash_common)
1767
0
  *type_change_ok = true;
1768
0
    }
1769
1770
  /* Handle the special case of an old common symbol merging with a
1771
     new symbol which looks like a common symbol in a shared object.
1772
     We change *PSEC and *PVALUE to make the new symbol look like a
1773
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1774
     right thing.  */
1775
1776
0
  if (newdyncommon
1777
0
      && h->root.type == bfd_link_hash_common)
1778
0
    {
1779
0
      *override = oldbfd;
1780
0
      newdef = false;
1781
0
      newdyncommon = false;
1782
0
      *pvalue = sym->st_size;
1783
0
      *psec = sec = bed->common_section (oldsec);
1784
0
      *size_change_ok = true;
1785
0
    }
1786
1787
  /* Skip weak definitions of symbols that are already defined.  */
1788
0
  if (newdef && olddef && newweak)
1789
0
    {
1790
      /* Don't skip new non-IR weak syms.  */
1791
0
      if (!(oldbfd != NULL
1792
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1793
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1794
0
  {
1795
0
    newdef = false;
1796
0
    *skip = true;
1797
0
  }
1798
1799
      /* Merge st_other.  If the symbol already has a dynamic index,
1800
   but visibility says it should not be visible, turn it into a
1801
   local symbol.  */
1802
0
      elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1803
0
      if (h->dynindx != -1)
1804
0
  switch (ELF_ST_VISIBILITY (h->other))
1805
0
    {
1806
0
    case STV_INTERNAL:
1807
0
    case STV_HIDDEN:
1808
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
1809
0
      break;
1810
0
    }
1811
0
    }
1812
1813
  /* If the old symbol is from a dynamic object, and the new symbol is
1814
     a definition which is not from a dynamic object, then the new
1815
     symbol overrides the old symbol.  Symbols from regular files
1816
     always take precedence over symbols from dynamic objects, even if
1817
     they are defined after the dynamic object in the link.
1818
1819
     As above, we again permit a common symbol in a regular object to
1820
     override a definition in a shared object if the shared object
1821
     symbol is a function or is weak.  */
1822
1823
0
  flip = NULL;
1824
0
  if (!newdyn
1825
0
      && (newdef
1826
0
    || (bfd_is_com_section (sec)
1827
0
        && (oldweak || oldfunc)))
1828
0
      && olddyn
1829
0
      && olddef
1830
0
      && h->def_dynamic)
1831
0
    {
1832
      /* Change the hash table entry to undefined, and let
1833
   _bfd_generic_link_add_one_symbol do the right thing with the
1834
   new definition.  */
1835
1836
0
      h->root.type = bfd_link_hash_undefined;
1837
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1838
0
      *size_change_ok = true;
1839
1840
0
      olddef = false;
1841
0
      olddyncommon = false;
1842
1843
      /* We again permit a type change when a common symbol may be
1844
   overriding a function.  */
1845
1846
0
      if (bfd_is_com_section (sec))
1847
0
  {
1848
0
    if (oldfunc)
1849
0
      {
1850
        /* If a common symbol overrides a function, make sure
1851
     that it isn't defined dynamically nor has type
1852
     function.  */
1853
0
        h->def_dynamic = 0;
1854
0
        h->type = STT_NOTYPE;
1855
0
      }
1856
0
    *type_change_ok = true;
1857
0
  }
1858
1859
0
      if (hi->root.type == bfd_link_hash_indirect)
1860
0
  flip = hi;
1861
0
      else
1862
  /* This union may have been set to be non-NULL when this symbol
1863
     was seen in a dynamic object.  We must force the union to be
1864
     NULL, so that it is correct for a regular symbol.  */
1865
0
  h->verinfo.vertree = NULL;
1866
0
    }
1867
1868
  /* Handle the special case of a new common symbol merging with an
1869
     old symbol that looks like it might be a common symbol defined in
1870
     a shared object.  Note that we have already handled the case in
1871
     which a new common symbol should simply override the definition
1872
     in the shared library.  */
1873
1874
0
  if (! newdyn
1875
0
      && bfd_is_com_section (sec)
1876
0
      && olddyncommon)
1877
0
    {
1878
      /* It would be best if we could set the hash table entry to a
1879
   common symbol, but we don't know what to use for the section
1880
   or the alignment.  */
1881
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1882
0
             bfd_link_hash_common, sym->st_size);
1883
1884
      /* If the presumed common symbol in the dynamic object is
1885
   larger, pretend that the new symbol has its size.  */
1886
1887
0
      if (h->size > *pvalue)
1888
0
  *pvalue = h->size;
1889
1890
      /* We need to remember the alignment required by the symbol
1891
   in the dynamic object.  */
1892
0
      BFD_ASSERT (pold_alignment);
1893
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1894
1895
0
      olddef = false;
1896
0
      olddyncommon = false;
1897
1898
0
      h->root.type = bfd_link_hash_undefined;
1899
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1900
1901
0
      *size_change_ok = true;
1902
0
      *type_change_ok = true;
1903
1904
0
      if (hi->root.type == bfd_link_hash_indirect)
1905
0
  flip = hi;
1906
0
      else
1907
0
  h->verinfo.vertree = NULL;
1908
0
    }
1909
1910
0
  if (flip != NULL)
1911
0
    {
1912
      /* Handle the case where we had a versioned symbol in a dynamic
1913
   library and now find a definition in a normal object.  In this
1914
   case, we make the versioned symbol point to the normal one.  */
1915
0
      flip->root.type = h->root.type;
1916
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1917
0
      h->root.type = bfd_link_hash_indirect;
1918
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1919
0
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1920
0
      if (h->def_dynamic)
1921
0
  {
1922
0
    h->def_dynamic = 0;
1923
0
    flip->ref_dynamic = 1;
1924
0
  }
1925
0
    }
1926
1927
0
  return true;
1928
0
}
1929
1930
/* This function is called to create an indirect symbol from the
1931
   default for the symbol with the default version if needed. The
1932
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1933
   set DYNSYM if the new indirect symbol is dynamic.  */
1934
1935
static bool
1936
_bfd_elf_add_default_symbol (bfd *abfd,
1937
           struct bfd_link_info *info,
1938
           struct elf_link_hash_entry *h,
1939
           const char *name,
1940
           Elf_Internal_Sym *sym,
1941
           asection *sec,
1942
           bfd_vma value,
1943
           bfd **poldbfd,
1944
           bool *dynsym)
1945
0
{
1946
0
  bool type_change_ok;
1947
0
  bool size_change_ok;
1948
0
  bool skip;
1949
0
  char *shortname;
1950
0
  struct elf_link_hash_entry *hi;
1951
0
  struct bfd_link_hash_entry *bh;
1952
0
  const struct elf_backend_data *bed;
1953
0
  bool collect;
1954
0
  bool dynamic;
1955
0
  bfd *override;
1956
0
  char *p;
1957
0
  size_t len, shortlen;
1958
0
  asection *tmp_sec;
1959
0
  bool matched;
1960
1961
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1962
0
    return true;
1963
1964
  /* If this symbol has a version, and it is the default version, we
1965
     create an indirect symbol from the default name to the fully
1966
     decorated name.  This will cause external references which do not
1967
     specify a version to be bound to this version of the symbol.  */
1968
0
  p = strchr (name, ELF_VER_CHR);
1969
0
  if (h->versioned == unknown)
1970
0
    {
1971
0
      if (p == NULL)
1972
0
  {
1973
0
    h->versioned = unversioned;
1974
0
    return true;
1975
0
  }
1976
0
      else
1977
0
  {
1978
0
    if (p[1] != ELF_VER_CHR)
1979
0
      {
1980
0
        h->versioned = versioned_hidden;
1981
0
        return true;
1982
0
      }
1983
0
    else
1984
0
      h->versioned = versioned;
1985
0
  }
1986
0
    }
1987
0
  else
1988
0
    {
1989
      /* PR ld/19073: We may see an unversioned definition after the
1990
   default version.  */
1991
0
      if (p == NULL)
1992
0
  return true;
1993
0
    }
1994
1995
0
  bed = get_elf_backend_data (abfd);
1996
0
  collect = bed->collect;
1997
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
1998
1999
0
  shortlen = p - name;
2000
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
2001
0
  if (shortname == NULL)
2002
0
    return false;
2003
0
  memcpy (shortname, name, shortlen);
2004
0
  shortname[shortlen] = '\0';
2005
2006
  /* We are going to create a new symbol.  Merge it with any existing
2007
     symbol with this name.  For the purposes of the merge, act as
2008
     though we were defining the symbol we just defined, although we
2009
     actually going to define an indirect symbol.  */
2010
0
  type_change_ok = false;
2011
0
  size_change_ok = false;
2012
0
  matched = true;
2013
0
  tmp_sec = sec;
2014
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2015
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2016
0
            &type_change_ok, &size_change_ok, &matched))
2017
0
    return false;
2018
2019
0
  if (skip)
2020
0
    goto nondefault;
2021
2022
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2023
0
    {
2024
      /* If the undecorated symbol will have a version added by a
2025
   script different to H, then don't indirect to/from the
2026
   undecorated symbol.  This isn't ideal because we may not yet
2027
   have seen symbol versions, if given by a script on the
2028
   command line rather than via --version-script.  */
2029
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2030
0
  {
2031
0
    bool hide;
2032
2033
0
    hi->verinfo.vertree
2034
0
      = bfd_find_version_for_sym (info->version_info,
2035
0
          hi->root.root.string, &hide);
2036
0
    if (hi->verinfo.vertree != NULL && hide)
2037
0
      {
2038
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
2039
0
        goto nondefault;
2040
0
      }
2041
0
  }
2042
0
      if (hi->verinfo.vertree != NULL
2043
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2044
0
  goto nondefault;
2045
0
    }
2046
2047
0
  if (! override)
2048
0
    {
2049
      /* Add the default symbol if not performing a relocatable link.  */
2050
0
      if (! bfd_link_relocatable (info))
2051
0
  {
2052
0
    bh = &hi->root;
2053
0
    if (bh->type == bfd_link_hash_defined
2054
0
        && bh->u.def.section->owner != NULL
2055
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2056
0
      {
2057
        /* Mark the previous definition from IR object as
2058
     undefined so that the generic linker will override
2059
     it.  */
2060
0
        bh->type = bfd_link_hash_undefined;
2061
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2062
0
      }
2063
0
    if (! (_bfd_generic_link_add_one_symbol
2064
0
     (info, abfd, shortname, BSF_INDIRECT,
2065
0
      bfd_ind_section_ptr,
2066
0
      0, name, false, collect, &bh)))
2067
0
      return false;
2068
0
    hi = (struct elf_link_hash_entry *) bh;
2069
0
  }
2070
0
    }
2071
0
  else
2072
0
    {
2073
      /* In this case the symbol named SHORTNAME is overriding the
2074
   indirect symbol we want to add.  We were planning on making
2075
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2076
   is the name without a version.  NAME is the fully versioned
2077
   name, and it is the default version.
2078
2079
   Overriding means that we already saw a definition for the
2080
   symbol SHORTNAME in a regular object, and it is overriding
2081
   the symbol defined in the dynamic object.
2082
2083
   When this happens, we actually want to change NAME, the
2084
   symbol we just added, to refer to SHORTNAME.  This will cause
2085
   references to NAME in the shared object to become references
2086
   to SHORTNAME in the regular object.  This is what we expect
2087
   when we override a function in a shared object: that the
2088
   references in the shared object will be mapped to the
2089
   definition in the regular object.  */
2090
2091
0
      while (hi->root.type == bfd_link_hash_indirect
2092
0
       || hi->root.type == bfd_link_hash_warning)
2093
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2094
2095
0
      h->root.type = bfd_link_hash_indirect;
2096
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2097
0
      if (h->def_dynamic)
2098
0
  {
2099
0
    h->def_dynamic = 0;
2100
0
    hi->ref_dynamic = 1;
2101
0
    if (hi->ref_regular
2102
0
        || hi->def_regular)
2103
0
      {
2104
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2105
0
    return false;
2106
0
      }
2107
0
  }
2108
2109
      /* Now set HI to H, so that the following code will set the
2110
   other fields correctly.  */
2111
0
      hi = h;
2112
0
    }
2113
2114
  /* Check if HI is a warning symbol.  */
2115
0
  if (hi->root.type == bfd_link_hash_warning)
2116
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2117
2118
  /* If there is a duplicate definition somewhere, then HI may not
2119
     point to an indirect symbol.  We will have reported an error to
2120
     the user in that case.  */
2121
2122
0
  if (hi->root.type == bfd_link_hash_indirect)
2123
0
    {
2124
0
      struct elf_link_hash_entry *ht;
2125
2126
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2127
0
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2128
2129
      /* If we first saw a reference to SHORTNAME with non-default
2130
   visibility, merge that visibility to the @@VER symbol.  */
2131
0
      elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2132
2133
      /* A reference to the SHORTNAME symbol from a dynamic library
2134
   will be satisfied by the versioned symbol at runtime.  In
2135
   effect, we have a reference to the versioned symbol.  */
2136
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2137
0
      hi->dynamic_def |= ht->dynamic_def;
2138
2139
      /* See if the new flags lead us to realize that the symbol must
2140
   be dynamic.  */
2141
0
      if (! *dynsym)
2142
0
  {
2143
0
    if (! dynamic)
2144
0
      {
2145
0
        if (! bfd_link_executable (info)
2146
0
      || hi->def_dynamic
2147
0
      || hi->ref_dynamic)
2148
0
    *dynsym = true;
2149
0
      }
2150
0
    else
2151
0
      {
2152
0
        if (hi->ref_regular)
2153
0
    *dynsym = true;
2154
0
      }
2155
0
  }
2156
0
    }
2157
2158
  /* We also need to define an indirection from the nondefault version
2159
     of the symbol.  */
2160
2161
0
 nondefault:
2162
0
  len = strlen (name);
2163
0
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2164
0
  if (shortname == NULL)
2165
0
    return false;
2166
0
  memcpy (shortname, name, shortlen);
2167
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2168
2169
  /* Once again, merge with any existing symbol.  */
2170
0
  type_change_ok = false;
2171
0
  size_change_ok = false;
2172
0
  tmp_sec = sec;
2173
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2174
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2175
0
            &type_change_ok, &size_change_ok, &matched))
2176
0
    return false;
2177
2178
0
  if (skip)
2179
0
    {
2180
0
      if (!dynamic
2181
0
    && h->root.type == bfd_link_hash_defweak
2182
0
    && hi->root.type == bfd_link_hash_defined)
2183
0
  {
2184
    /* We are handling a weak sym@@ver and attempting to define
2185
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2186
       new weak sym@ver because there is already a strong sym@ver.
2187
       However, sym@ver and sym@@ver are really the same symbol.
2188
       The existing strong sym@ver ought to override sym@@ver.  */
2189
0
    h->root.type = bfd_link_hash_defined;
2190
0
    h->root.u.def.section = hi->root.u.def.section;
2191
0
    h->root.u.def.value = hi->root.u.def.value;
2192
0
    hi->root.type = bfd_link_hash_indirect;
2193
0
    hi->root.u.i.link = &h->root;
2194
0
  }
2195
0
      else
2196
0
  return true;
2197
0
    }
2198
0
  else if (override)
2199
0
    {
2200
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2201
   the type of override we do in the case above unless it is
2202
   overridden by a versioned definition.  */
2203
0
      if (hi->root.type != bfd_link_hash_defined
2204
0
    && hi->root.type != bfd_link_hash_defweak)
2205
0
  _bfd_error_handler
2206
    /* xgettext:c-format */
2207
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2208
0
     abfd, shortname);
2209
0
      return true;
2210
0
    }
2211
0
  else
2212
0
    {
2213
0
      bh = &hi->root;
2214
0
      if (! (_bfd_generic_link_add_one_symbol
2215
0
       (info, abfd, shortname, BSF_INDIRECT,
2216
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2217
0
  return false;
2218
0
      hi = (struct elf_link_hash_entry *) bh;
2219
0
    }
2220
2221
  /* If there is a duplicate definition somewhere, then HI may not
2222
     point to an indirect symbol.  We will have reported an error
2223
     to the user in that case.  */
2224
0
  if (hi->root.type == bfd_link_hash_indirect)
2225
0
    {
2226
0
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2227
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2228
0
      hi->dynamic_def |= h->dynamic_def;
2229
2230
      /* If we first saw a reference to @VER symbol with
2231
   non-default visibility, merge that visibility to the
2232
   @@VER symbol.  */
2233
0
      elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2234
2235
      /* See if the new flags lead us to realize that the symbol
2236
   must be dynamic.  */
2237
0
      if (! *dynsym)
2238
0
  {
2239
0
    if (! dynamic)
2240
0
      {
2241
0
        if (! bfd_link_executable (info)
2242
0
      || hi->ref_dynamic)
2243
0
    *dynsym = true;
2244
0
      }
2245
0
    else
2246
0
      {
2247
0
        if (hi->ref_regular)
2248
0
    *dynsym = true;
2249
0
      }
2250
0
  }
2251
0
    }
2252
2253
0
  return true;
2254
0
}
2255

2256
/* This routine is used to export all defined symbols into the dynamic
2257
   symbol table.  It is called via elf_link_hash_traverse.  */
2258
2259
static bool
2260
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2261
0
{
2262
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2263
2264
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2265
0
  if (h->root.type == bfd_link_hash_indirect)
2266
0
    return true;
2267
2268
  /* Ignore this if we won't export it.  */
2269
0
  if (!eif->info->export_dynamic && !h->dynamic)
2270
0
    return true;
2271
2272
0
  if (h->dynindx == -1
2273
0
      && (h->def_regular || h->ref_regular)
2274
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2275
0
            h->root.root.string))
2276
0
    {
2277
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2278
0
  {
2279
0
    eif->failed = true;
2280
0
    return false;
2281
0
  }
2282
0
    }
2283
2284
0
  return true;
2285
0
}
2286

2287
/* Return the glibc version reference if VERSION_DEP is added to the
2288
   list of glibc version dependencies successfully.  VERSION_DEP will
2289
   be put into the .gnu.version_r section.  GLIBC_MINOR_BASE is the
2290
   pointer to the glibc minor base version.  */
2291
2292
static Elf_Internal_Verneed *
2293
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2294
          Elf_Internal_Verneed *glibc_verref,
2295
          const char *version_dep,
2296
          int *glibc_minor_base)
2297
0
{
2298
0
  Elf_Internal_Verneed *t;
2299
0
  Elf_Internal_Vernaux *a;
2300
0
  size_t amt;
2301
0
  int minor_version = -1;
2302
2303
0
  if (glibc_verref != NULL)
2304
0
    {
2305
0
      t = glibc_verref;
2306
2307
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2308
0
  {
2309
    /* Return if VERSION_DEP dependency has been added.  */
2310
0
    if (a->vna_nodename == version_dep
2311
0
        || strcmp (a->vna_nodename, version_dep) == 0)
2312
0
      return t;
2313
0
  }
2314
0
    }
2315
0
  else
2316
0
    {
2317
0
      for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2318
0
     t != NULL;
2319
0
     t = t->vn_nextref)
2320
0
  {
2321
0
    const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2322
0
    if (soname != NULL && startswith (soname, "libc.so."))
2323
0
      break;
2324
0
  }
2325
2326
      /* Skip the shared library if it isn't libc.so.  */
2327
0
      if (t == NULL)
2328
0
  return t;
2329
2330
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2331
0
  {
2332
    /* Return if VERSION_DEP dependency has been added.  */
2333
0
    if (a->vna_nodename == version_dep
2334
0
        || strcmp (a->vna_nodename, version_dep) == 0)
2335
0
      return t;
2336
2337
    /* Check if libc.so provides GLIBC_2.XX version.  */
2338
0
    if (startswith (a->vna_nodename, "GLIBC_2."))
2339
0
      {
2340
0
        minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2341
0
        if (minor_version < *glibc_minor_base)
2342
0
    *glibc_minor_base = minor_version;
2343
0
      }
2344
0
  }
2345
2346
      /* Skip if it isn't linked against glibc.  */
2347
0
      if (minor_version < 0)
2348
0
  return NULL;
2349
0
    }
2350
2351
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2352
0
  if (startswith (version_dep, "GLIBC_2."))
2353
0
    {
2354
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2355
0
      if (minor_version <= *glibc_minor_base)
2356
0
  return NULL;
2357
0
    }
2358
2359
0
  amt = sizeof *a;
2360
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2361
0
  if (a == NULL)
2362
0
    {
2363
0
      rinfo->failed = true;
2364
0
      return NULL;
2365
0
    }
2366
2367
0
  a->vna_nodename = version_dep;
2368
0
  a->vna_flags = 0;
2369
0
  a->vna_nextptr = t->vn_auxptr;
2370
0
  a->vna_other = rinfo->vers + 1;
2371
0
  ++rinfo->vers;
2372
2373
0
  t->vn_auxptr = a;
2374
2375
0
  return t;
2376
0
}
2377
2378
/* Add VERSION_DEP to the list of version dependencies when linked
2379
   against glibc.  */
2380
2381
void
2382
_bfd_elf_link_add_glibc_version_dependency
2383
  (struct elf_find_verdep_info *rinfo,
2384
   const char *version_dep[])
2385
0
{
2386
0
  Elf_Internal_Verneed *t = NULL;
2387
0
  int glibc_minor_base = INT_MAX;
2388
2389
0
  do
2390
0
    {
2391
0
      t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
2392
0
              &glibc_minor_base);
2393
      /* Return if there is no glibc version reference.  */
2394
0
      if (t == NULL)
2395
0
  return;
2396
0
      version_dep++;
2397
0
    }
2398
0
  while (*version_dep != NULL);
2399
0
}
2400
2401
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2402
   linked against glibc.  */
2403
2404
void
2405
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2406
0
{
2407
0
  if (rinfo->info->enable_dt_relr)
2408
0
    {
2409
0
      const char *version[] =
2410
0
  {
2411
0
    "GLIBC_ABI_DT_RELR",
2412
0
    NULL
2413
0
  };
2414
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2415
0
    }
2416
0
}
2417
2418
/* Look through the symbols which are defined in other shared
2419
   libraries and referenced here.  Update the list of version
2420
   dependencies.  This will be put into the .gnu.version_r section.
2421
   This function is called via elf_link_hash_traverse.  */
2422
2423
static bool
2424
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2425
           void *data)
2426
0
{
2427
0
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2428
0
  Elf_Internal_Verneed *t;
2429
0
  Elf_Internal_Vernaux *a;
2430
0
  size_t amt;
2431
2432
  /* We only care about symbols defined in shared objects with version
2433
     information.  */
2434
0
  if (!h->def_dynamic
2435
0
      || h->def_regular
2436
0
      || h->dynindx == -1
2437
0
      || h->verinfo.verdef == NULL
2438
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2439
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2440
0
    return true;
2441
2442
  /* See if we already know about this version.  */
2443
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2444
0
       t != NULL;
2445
0
       t = t->vn_nextref)
2446
0
    {
2447
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2448
0
  continue;
2449
2450
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2451
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2452
0
    return true;
2453
2454
0
      break;
2455
0
    }
2456
2457
  /* This is a new version.  Add it to tree we are building.  */
2458
2459
0
  if (t == NULL)
2460
0
    {
2461
0
      amt = sizeof *t;
2462
0
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2463
0
      if (t == NULL)
2464
0
  {
2465
0
    rinfo->failed = true;
2466
0
    return false;
2467
0
  }
2468
2469
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2470
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2471
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2472
0
    }
2473
2474
0
  amt = sizeof *a;
2475
0
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2476
0
  if (a == NULL)
2477
0
    {
2478
0
      rinfo->failed = true;
2479
0
      return false;
2480
0
    }
2481
2482
  /* Note that we are copying a string pointer here, and testing it
2483
     above.  If bfd_elf_string_from_elf_section is ever changed to
2484
     discard the string data when low in memory, this will have to be
2485
     fixed.  */
2486
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2487
2488
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2489
0
  a->vna_nextptr = t->vn_auxptr;
2490
2491
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2492
0
  ++rinfo->vers;
2493
2494
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2495
2496
0
  t->vn_auxptr = a;
2497
2498
0
  return true;
2499
0
}
2500
2501
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2502
   hidden.  Set *T_P to NULL if there is no match.  */
2503
2504
static bool
2505
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2506
             struct elf_link_hash_entry *h,
2507
             const char *version_p,
2508
             struct bfd_elf_version_tree **t_p,
2509
             bool *hide)
2510
0
{
2511
0
  struct bfd_elf_version_tree *t;
2512
2513
  /* Look for the version.  If we find it, it is no longer weak.  */
2514
0
  for (t = info->version_info; t != NULL; t = t->next)
2515
0
    {
2516
0
      if (strcmp (t->name, version_p) == 0)
2517
0
  {
2518
0
    size_t len;
2519
0
    char *alc;
2520
0
    struct bfd_elf_version_expr *d;
2521
2522
0
    len = version_p - h->root.root.string;
2523
0
    alc = (char *) bfd_malloc (len);
2524
0
    if (alc == NULL)
2525
0
      return false;
2526
0
    memcpy (alc, h->root.root.string, len - 1);
2527
0
    alc[len - 1] = '\0';
2528
0
    if (alc[len - 2] == ELF_VER_CHR)
2529
0
      alc[len - 2] = '\0';
2530
2531
0
    h->verinfo.vertree = t;
2532
0
    t->used = true;
2533
0
    d = NULL;
2534
2535
0
    if (t->globals.list != NULL)
2536
0
      d = (*t->match) (&t->globals, NULL, alc);
2537
2538
    /* See if there is anything to force this symbol to
2539
       local scope.  */
2540
0
    if (d == NULL && t->locals.list != NULL)
2541
0
      {
2542
0
        d = (*t->match) (&t->locals, NULL, alc);
2543
0
        if (d != NULL
2544
0
      && h->dynindx != -1
2545
0
      && ! info->export_dynamic)
2546
0
    *hide = true;
2547
0
      }
2548
2549
0
    free (alc);
2550
0
    break;
2551
0
  }
2552
0
    }
2553
2554
0
  *t_p = t;
2555
2556
0
  return true;
2557
0
}
2558
2559
/* Return TRUE if the symbol H is hidden by version script.  */
2560
2561
bool
2562
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2563
           struct elf_link_hash_entry *h)
2564
0
{
2565
0
  const char *p;
2566
0
  bool hide = false;
2567
0
  const struct elf_backend_data *bed
2568
0
    = get_elf_backend_data (info->output_bfd);
2569
2570
  /* Version script only hides symbols defined in regular objects.  */
2571
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2572
0
    return true;
2573
2574
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2575
0
  if (p != NULL && h->verinfo.vertree == NULL)
2576
0
    {
2577
0
      struct bfd_elf_version_tree *t;
2578
2579
0
      ++p;
2580
0
      if (*p == ELF_VER_CHR)
2581
0
  ++p;
2582
2583
0
      if (*p != '\0'
2584
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2585
0
    && hide)
2586
0
  {
2587
0
    if (hide)
2588
0
      (*bed->elf_backend_hide_symbol) (info, h, true);
2589
0
    return true;
2590
0
  }
2591
0
    }
2592
2593
  /* If we don't have a version for this symbol, see if we can find
2594
     something.  */
2595
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2596
0
    {
2597
0
      h->verinfo.vertree
2598
0
  = bfd_find_version_for_sym (info->version_info,
2599
0
            h->root.root.string, &hide);
2600
0
      if (h->verinfo.vertree != NULL && hide)
2601
0
  {
2602
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
2603
0
    return true;
2604
0
  }
2605
0
    }
2606
2607
0
  return false;
2608
0
}
2609
2610
/* Figure out appropriate versions for all the symbols.  We may not
2611
   have the version number script until we have read all of the input
2612
   files, so until that point we don't know which symbols should be
2613
   local.  This function is called via elf_link_hash_traverse.  */
2614
2615
static bool
2616
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2617
0
{
2618
0
  struct elf_info_failed *sinfo;
2619
0
  struct bfd_link_info *info;
2620
0
  const struct elf_backend_data *bed;
2621
0
  struct elf_info_failed eif;
2622
0
  char *p;
2623
0
  bool hide;
2624
2625
0
  sinfo = (struct elf_info_failed *) data;
2626
0
  info = sinfo->info;
2627
2628
  /* Fix the symbol flags.  */
2629
0
  eif.failed = false;
2630
0
  eif.info = info;
2631
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2632
0
    {
2633
0
      if (eif.failed)
2634
0
  sinfo->failed = true;
2635
0
      return false;
2636
0
    }
2637
2638
0
  bed = get_elf_backend_data (info->output_bfd);
2639
2640
  /* We only need version numbers for symbols defined in regular
2641
     objects.  */
2642
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2643
0
    {
2644
      /* Hide symbols defined in discarded input sections.  */
2645
0
      if ((h->root.type == bfd_link_hash_defined
2646
0
     || h->root.type == bfd_link_hash_defweak)
2647
0
    && discarded_section (h->root.u.def.section))
2648
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2649
0
      return true;
2650
0
    }
2651
2652
0
  hide = false;
2653
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2654
0
  if (p != NULL && h->verinfo.vertree == NULL)
2655
0
    {
2656
0
      struct bfd_elf_version_tree *t;
2657
2658
0
      ++p;
2659
0
      if (*p == ELF_VER_CHR)
2660
0
  ++p;
2661
2662
      /* If there is no version string, we can just return out.  */
2663
0
      if (*p == '\0')
2664
0
  return true;
2665
2666
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2667
0
  {
2668
0
    sinfo->failed = true;
2669
0
    return false;
2670
0
  }
2671
2672
0
      if (hide)
2673
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2674
2675
      /* If we are building an application, we need to create a
2676
   version node for this version.  */
2677
0
      if (t == NULL && bfd_link_executable (info))
2678
0
  {
2679
0
    struct bfd_elf_version_tree **pp;
2680
0
    int version_index;
2681
2682
    /* If we aren't going to export this symbol, we don't need
2683
       to worry about it.  */
2684
0
    if (h->dynindx == -1)
2685
0
      return true;
2686
2687
0
    t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2688
0
                sizeof *t);
2689
0
    if (t == NULL)
2690
0
      {
2691
0
        sinfo->failed = true;
2692
0
        return false;
2693
0
      }
2694
2695
0
    t->name = p;
2696
0
    t->name_indx = (unsigned int) -1;
2697
0
    t->used = true;
2698
2699
0
    version_index = 1;
2700
    /* Don't count anonymous version tag.  */
2701
0
    if (sinfo->info->version_info != NULL
2702
0
        && sinfo->info->version_info->vernum == 0)
2703
0
      version_index = 0;
2704
0
    for (pp = &sinfo->info->version_info;
2705
0
         *pp != NULL;
2706
0
         pp = &(*pp)->next)
2707
0
      ++version_index;
2708
0
    t->vernum = version_index;
2709
2710
0
    *pp = t;
2711
2712
0
    h->verinfo.vertree = t;
2713
0
  }
2714
0
      else if (t == NULL)
2715
0
  {
2716
    /* We could not find the version for a symbol when
2717
       generating a shared archive.  Return an error.  */
2718
0
    _bfd_error_handler
2719
      /* xgettext:c-format */
2720
0
      (_("%pB: version node not found for symbol %s"),
2721
0
       info->output_bfd, h->root.root.string);
2722
0
    bfd_set_error (bfd_error_bad_value);
2723
0
    sinfo->failed = true;
2724
0
    return false;
2725
0
  }
2726
0
    }
2727
2728
  /* If we don't have a version for this symbol, see if we can find
2729
     something.  */
2730
0
  if (!hide
2731
0
      && h->verinfo.vertree == NULL
2732
0
      && sinfo->info->version_info != NULL)
2733
0
    {
2734
0
      h->verinfo.vertree
2735
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2736
0
            h->root.root.string, &hide);
2737
0
      if (h->verinfo.vertree != NULL && hide)
2738
0
  (*bed->elf_backend_hide_symbol) (info, h, true);
2739
0
    }
2740
2741
0
  return true;
2742
0
}
2743

2744
/* Read and swap the relocs from the section indicated by SHDR.  This
2745
   may be either a REL or a RELA section.  The relocations are
2746
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2747
   which should have already been allocated to contain enough space.
2748
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2749
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2750
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2751
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2752
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2753
2754
   Returns FALSE if something goes wrong.  */
2755
2756
static bool
2757
elf_link_read_relocs_from_section (bfd *abfd,
2758
           const asection *sec,
2759
           Elf_Internal_Shdr *shdr,
2760
           void **external_relocs_addr,
2761
           size_t *external_relocs_size,
2762
           Elf_Internal_Rela *internal_relocs)
2763
0
{
2764
0
  const struct elf_backend_data *bed;
2765
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2766
0
  const bfd_byte *erela;
2767
0
  const bfd_byte *erelaend;
2768
0
  Elf_Internal_Rela *irela;
2769
0
  Elf_Internal_Shdr *symtab_hdr;
2770
0
  size_t nsyms;
2771
0
  void *external_relocs = *external_relocs_addr;
2772
2773
  /* Position ourselves at the start of the section.  */
2774
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2775
0
    return false;
2776
2777
  /* Read the relocations.  */
2778
0
  *external_relocs_size = shdr->sh_size;
2779
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2780
0
         external_relocs_size,
2781
0
         external_relocs_addr, abfd, true))
2782
0
    return false;
2783
2784
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2785
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2786
2787
0
  bed = get_elf_backend_data (abfd);
2788
2789
  /* Convert the external relocations to the internal format.  */
2790
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2791
0
    swap_in = bed->s->swap_reloc_in;
2792
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2793
0
    swap_in = bed->s->swap_reloca_in;
2794
0
  else
2795
0
    {
2796
0
      bfd_set_error (bfd_error_wrong_format);
2797
0
      return false;
2798
0
    }
2799
2800
0
  erela = (const bfd_byte *) external_relocs;
2801
  /* Setting erelaend like this and comparing with <= handles case of
2802
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2803
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2804
0
  irela = internal_relocs;
2805
0
  while (erela <= erelaend)
2806
0
    {
2807
0
      bfd_vma r_symndx;
2808
2809
0
      (*swap_in) (abfd, erela, irela);
2810
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2811
0
      if (bed->s->arch_size == 64)
2812
0
  r_symndx >>= 24;
2813
0
      if (nsyms > 0)
2814
0
  {
2815
0
    if ((size_t) r_symndx >= nsyms)
2816
0
      {
2817
0
        _bfd_error_handler
2818
    /* xgettext:c-format */
2819
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2820
0
       " for offset %#" PRIx64 " in section `%pA'"),
2821
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2822
0
     (uint64_t) irela->r_offset, sec);
2823
0
        bfd_set_error (bfd_error_bad_value);
2824
0
        return false;
2825
0
      }
2826
0
  }
2827
0
      else if (r_symndx != STN_UNDEF)
2828
0
  {
2829
0
    _bfd_error_handler
2830
      /* xgettext:c-format */
2831
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2832
0
         " for offset %#" PRIx64 " in section `%pA'"
2833
0
         " when the object file has no symbol table"),
2834
0
       abfd, (uint64_t) r_symndx,
2835
0
       (uint64_t) irela->r_offset, sec);
2836
0
    bfd_set_error (bfd_error_bad_value);
2837
0
    return false;
2838
0
  }
2839
0
      irela += bed->s->int_rels_per_ext_rel;
2840
0
      erela += shdr->sh_entsize;
2841
0
    }
2842
2843
0
  return true;
2844
0
}
2845
2846
/* Read and swap the relocs for a section O.  They may have been
2847
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2848
   not NULL, they are used as buffers to read into.  They are known to
2849
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2850
   the return value is allocated using either malloc or bfd_alloc,
2851
   according to the KEEP_MEMORY argument.  If O has two relocation
2852
   sections (both REL and RELA relocations), then the REL_HDR
2853
   relocations will appear first in INTERNAL_RELOCS, followed by the
2854
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2855
   update cache_size.  */
2856
2857
Elf_Internal_Rela *
2858
_bfd_elf_link_info_read_relocs (bfd *abfd,
2859
        struct bfd_link_info *info,
2860
        const asection *o,
2861
        void *external_relocs,
2862
        Elf_Internal_Rela *internal_relocs,
2863
        bool keep_memory)
2864
0
{
2865
0
  void *alloc1 = NULL;
2866
0
  size_t alloc1_size;
2867
0
  Elf_Internal_Rela *alloc2 = NULL;
2868
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2869
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2870
0
  Elf_Internal_Rela *internal_rela_relocs;
2871
2872
0
  if (esdo->relocs != NULL)
2873
0
    return esdo->relocs;
2874
2875
0
  if (o->reloc_count == 0)
2876
0
    return NULL;
2877
2878
0
  if (internal_relocs == NULL)
2879
0
    {
2880
0
      bfd_size_type size;
2881
2882
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2883
0
      if (keep_memory && info)
2884
0
  info->cache_size += size;
2885
0
      internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2886
0
      if (internal_relocs == NULL)
2887
0
  return NULL;
2888
0
    }
2889
2890
0
  alloc1 = external_relocs;
2891
0
  internal_rela_relocs = internal_relocs;
2892
0
  if (esdo->rel.hdr)
2893
0
    {
2894
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2895
0
                &alloc1, &alloc1_size,
2896
0
                internal_relocs))
2897
0
  goto error_return;
2898
0
      external_relocs = (((bfd_byte *) external_relocs)
2899
0
       + esdo->rel.hdr->sh_size);
2900
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2901
0
             * bed->s->int_rels_per_ext_rel);
2902
0
    }
2903
2904
0
  if (esdo->rela.hdr
2905
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2906
0
                &alloc1, &alloc1_size,
2907
0
                internal_rela_relocs)))
2908
0
    goto error_return;
2909
2910
  /* Cache the results for next time, if we can.  */
2911
0
  if (keep_memory)
2912
0
    esdo->relocs = internal_relocs;
2913
2914
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2915
2916
  /* Don't free alloc2, since if it was allocated we are passing it
2917
     back (under the name of internal_relocs).  */
2918
2919
0
  return internal_relocs;
2920
2921
0
 error_return:
2922
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2923
0
  free (alloc2);
2924
0
  return NULL;
2925
0
}
2926
2927
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2928
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2929
   struct bfd_link_info.  */
2930
2931
Elf_Internal_Rela *
2932
_bfd_elf_link_read_relocs (bfd *abfd,
2933
         const asection *o,
2934
         void *external_relocs,
2935
         Elf_Internal_Rela *internal_relocs,
2936
         bool keep_memory)
2937
0
{
2938
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2939
0
           internal_relocs, keep_memory);
2940
2941
0
}
2942
2943
/* Compute the size of, and allocate space for, REL_HDR which is the
2944
   section header for a section containing relocations for O.  */
2945
2946
static bool
2947
_bfd_elf_link_size_reloc_section (bfd *abfd,
2948
          struct bfd_elf_section_reloc_data *reldata)
2949
0
{
2950
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2951
2952
  /* That allows us to calculate the size of the section.  */
2953
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2954
2955
  /* The contents field must last into write_object_contents, so we
2956
     allocate it with bfd_alloc rather than malloc.  Also since we
2957
     cannot be sure that the contents will actually be filled in,
2958
     we zero the allocated space.  */
2959
0
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2960
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2961
0
    return false;
2962
2963
0
  if (reldata->hashes == NULL && reldata->count)
2964
0
    {
2965
0
      struct elf_link_hash_entry **p;
2966
2967
0
      p = ((struct elf_link_hash_entry **)
2968
0
     bfd_zmalloc (reldata->count * sizeof (*p)));
2969
0
      if (p == NULL)
2970
0
  return false;
2971
2972
0
      reldata->hashes = p;
2973
0
    }
2974
2975
0
  return true;
2976
0
}
2977
2978
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2979
   originated from the section given by INPUT_REL_HDR) to the
2980
   OUTPUT_BFD.  */
2981
2982
bool
2983
_bfd_elf_link_output_relocs (bfd *output_bfd,
2984
           asection *input_section,
2985
           Elf_Internal_Shdr *input_rel_hdr,
2986
           Elf_Internal_Rela *internal_relocs,
2987
           struct elf_link_hash_entry **rel_hash)
2988
0
{
2989
0
  Elf_Internal_Rela *irela;
2990
0
  Elf_Internal_Rela *irelaend;
2991
0
  bfd_byte *erel;
2992
0
  struct bfd_elf_section_reloc_data *output_reldata;
2993
0
  asection *output_section;
2994
0
  const struct elf_backend_data *bed;
2995
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2996
0
  struct bfd_elf_section_data *esdo;
2997
2998
0
  output_section = input_section->output_section;
2999
3000
0
  bed = get_elf_backend_data (output_bfd);
3001
0
  esdo = elf_section_data (output_section);
3002
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3003
0
    {
3004
0
      output_reldata = &esdo->rel;
3005
0
      swap_out = bed->s->swap_reloc_out;
3006
0
    }
3007
0
  else if (esdo->rela.hdr
3008
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3009
0
    {
3010
0
      output_reldata = &esdo->rela;
3011
0
      swap_out = bed->s->swap_reloca_out;
3012
0
    }
3013
0
  else
3014
0
    {
3015
0
      _bfd_error_handler
3016
  /* xgettext:c-format */
3017
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3018
0
   output_bfd, input_section->owner, input_section);
3019
0
      bfd_set_error (bfd_error_wrong_format);
3020
0
      return false;
3021
0
    }
3022
3023
0
  erel = output_reldata->hdr->contents;
3024
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3025
0
  irela = internal_relocs;
3026
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3027
0
          * bed->s->int_rels_per_ext_rel);
3028
0
  while (irela < irelaend)
3029
0
    {
3030
0
      if (rel_hash && *rel_hash)
3031
0
  (*rel_hash)->has_reloc = 1;
3032
0
      (*swap_out) (output_bfd, irela, erel);
3033
0
      irela += bed->s->int_rels_per_ext_rel;
3034
0
      erel += input_rel_hdr->sh_entsize;
3035
0
      if (rel_hash)
3036
0
  rel_hash++;
3037
0
    }
3038
3039
  /* Bump the counter, so that we know where to add the next set of
3040
     relocations.  */
3041
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3042
3043
0
  return true;
3044
0
}
3045

3046
/* Make weak undefined symbols in PIE dynamic.  */
3047
3048
bool
3049
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3050
         struct elf_link_hash_entry *h)
3051
0
{
3052
0
  if (bfd_link_pie (info)
3053
0
      && h->dynindx == -1
3054
0
      && h->root.type == bfd_link_hash_undefweak)
3055
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3056
3057
0
  return true;
3058
0
}
3059
3060
/* Fix up the flags for a symbol.  This handles various cases which
3061
   can only be fixed after all the input files are seen.  This is
3062
   currently called by both adjust_dynamic_symbol and
3063
   assign_sym_version, which is unnecessary but perhaps more robust in
3064
   the face of future changes.  */
3065
3066
static bool
3067
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3068
         struct elf_info_failed *eif)
3069
0
{
3070
0
  const struct elf_backend_data *bed;
3071
3072
  /* If this symbol was mentioned in a non-ELF file, try to set
3073
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3074
     permit a non-ELF file to correctly refer to a symbol defined in
3075
     an ELF dynamic object.  */
3076
0
  if (h->non_elf)
3077
0
    {
3078
0
      while (h->root.type == bfd_link_hash_indirect)
3079
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3080
3081
0
      if (h->root.type != bfd_link_hash_defined
3082
0
    && h->root.type != bfd_link_hash_defweak)
3083
0
  {
3084
0
    h->ref_regular = 1;
3085
0
    h->ref_regular_nonweak = 1;
3086
0
  }
3087
0
      else
3088
0
  {
3089
0
    if (h->root.u.def.section->owner != NULL
3090
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3091
0
      == bfd_target_elf_flavour))
3092
0
      {
3093
0
        h->ref_regular = 1;
3094
0
        h->ref_regular_nonweak = 1;
3095
0
      }
3096
0
    else
3097
0
      h->def_regular = 1;
3098
0
  }
3099
3100
0
      if (h->dynindx == -1
3101
0
    && (h->def_dynamic
3102
0
        || h->ref_dynamic))
3103
0
  {
3104
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3105
0
      {
3106
0
        eif->failed = true;
3107
0
        return false;
3108
0
      }
3109
0
  }
3110
0
    }
3111
0
  else
3112
0
    {
3113
      /* Unfortunately, NON_ELF is only correct if the symbol
3114
   was first seen in a non-ELF file.  Fortunately, if the symbol
3115
   was first seen in an ELF file, we're probably OK unless the
3116
   symbol was defined in a non-ELF file.  Catch that case here.
3117
   FIXME: We're still in trouble if the symbol was first seen in
3118
   a dynamic object, and then later in a non-ELF regular object.  */
3119
0
      if ((h->root.type == bfd_link_hash_defined
3120
0
     || h->root.type == bfd_link_hash_defweak)
3121
0
    && !h->def_regular
3122
0
    && (h->root.u.def.section->owner != NULL
3123
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3124
0
     != bfd_target_elf_flavour)
3125
0
        : (bfd_is_abs_section (h->root.u.def.section)
3126
0
     && !h->def_dynamic)))
3127
0
  h->def_regular = 1;
3128
0
    }
3129
3130
  /* Backend specific symbol fixup.  */
3131
0
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3132
0
  if (bed->elf_backend_fixup_symbol
3133
0
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3134
0
    return false;
3135
3136
  /* If this is a final link, and the symbol was defined as a common
3137
     symbol in a regular object file, and there was no definition in
3138
     any dynamic object, then the linker will have allocated space for
3139
     the symbol in a common section but the DEF_REGULAR
3140
     flag will not have been set.  */
3141
0
  if (h->root.type == bfd_link_hash_defined
3142
0
      && !h->def_regular
3143
0
      && h->ref_regular
3144
0
      && !h->def_dynamic
3145
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3146
0
    h->def_regular = 1;
3147
3148
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3149
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3150
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3151
3152
  /* If a weak undefined symbol has non-default visibility, we also
3153
     hide it from the dynamic linker.  */
3154
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3155
0
     && h->root.type == bfd_link_hash_undefweak)
3156
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3157
3158
  /* A hidden versioned symbol in executable should be forced local if
3159
     it is is locally defined, not referenced by shared library and not
3160
     exported.  */
3161
0
  else if (bfd_link_executable (eif->info)
3162
0
     && h->versioned == versioned_hidden
3163
0
     && !eif->info->export_dynamic
3164
0
     && !h->dynamic
3165
0
     && !h->ref_dynamic
3166
0
     && h->def_regular)
3167
0
    (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3168
3169
  /* If -Bsymbolic was used (which means to bind references to global
3170
     symbols to the definition within the shared object), and this
3171
     symbol was defined in a regular object, then it actually doesn't
3172
     need a PLT entry.  Likewise, if the symbol has non-default
3173
     visibility.  If the symbol has hidden or internal visibility, we
3174
     will force it local.  */
3175
0
  else if (h->needs_plt
3176
0
     && bfd_link_pic (eif->info)
3177
0
     && is_elf_hash_table (eif->info->hash)
3178
0
     && (SYMBOLIC_BIND (eif->info, h)
3179
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3180
0
     && h->def_regular)
3181
0
    {
3182
0
      bool force_local;
3183
3184
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3185
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3186
0
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3187
0
    }
3188
3189
  /* If this is a weak defined symbol in a dynamic object, and we know
3190
     the real definition in the dynamic object, copy interesting flags
3191
     over to the real definition.  */
3192
0
  if (h->is_weakalias)
3193
0
    {
3194
0
      struct elf_link_hash_entry *def = weakdef (h);
3195
3196
      /* If the real definition is defined by a regular object file,
3197
   don't do anything special.  See the longer description in
3198
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3199
   bfd_link_hash_defined as it was when put on the alias list
3200
   then it must have originally been a versioned symbol (for
3201
   which a non-versioned indirect symbol is created) and later
3202
   a definition for the non-versioned symbol is found.  In that
3203
   case the indirection is flipped with the versioned symbol
3204
   becoming an indirect pointing at the non-versioned symbol.
3205
   Thus, not an alias any more.  */
3206
0
      if (def->def_regular
3207
0
    || def->root.type != bfd_link_hash_defined)
3208
0
  {
3209
0
    h = def;
3210
0
    while ((h = h->u.alias) != def)
3211
0
      h->is_weakalias = 0;
3212
0
  }
3213
0
      else
3214
0
  {
3215
0
    while (h->root.type == bfd_link_hash_indirect)
3216
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3217
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3218
0
          || h->root.type == bfd_link_hash_defweak);
3219
0
    BFD_ASSERT (def->def_dynamic);
3220
0
    (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3221
0
  }
3222
0
    }
3223
3224
0
  return true;
3225
0
}
3226
3227
/* Make the backend pick a good value for a dynamic symbol.  This is
3228
   called via elf_link_hash_traverse, and also calls itself
3229
   recursively.  */
3230
3231
static bool
3232
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3233
0
{
3234
0
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
3235
0
  struct elf_link_hash_table *htab;
3236
0
  const struct elf_backend_data *bed;
3237
3238
0
  if (! is_elf_hash_table (eif->info->hash))
3239
0
    return false;
3240
3241
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3242
0
  if (h->root.type == bfd_link_hash_indirect)
3243
0
    return true;
3244
3245
  /* Fix the symbol flags.  */
3246
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3247
0
    return false;
3248
3249
0
  htab = elf_hash_table (eif->info);
3250
0
  bed = get_elf_backend_data (htab->dynobj);
3251
3252
0
  if (h->root.type == bfd_link_hash_undefweak)
3253
0
    {
3254
0
      if (eif->info->dynamic_undefined_weak == 0)
3255
0
  (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3256
0
      else if (eif->info->dynamic_undefined_weak > 0
3257
0
         && h->ref_regular
3258
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3259
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3260
0
              h->root.root.string))
3261
0
  {
3262
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3263
0
      {
3264
0
        eif->failed = true;
3265
0
        return false;
3266
0
      }
3267
0
  }
3268
0
    }
3269
3270
  /* If this symbol does not require a PLT entry, and it is not
3271
     defined by a dynamic object, or is not referenced by a regular
3272
     object, ignore it.  We do have to handle a weak defined symbol,
3273
     even if no regular object refers to it, if we decided to add it
3274
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3275
     about symbols which are defined by one dynamic object and
3276
     referenced by another one?  */
3277
0
  if (!h->needs_plt
3278
0
      && h->type != STT_GNU_IFUNC
3279
0
      && (h->def_regular
3280
0
    || !h->def_dynamic
3281
0
    || (!h->ref_regular
3282
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3283
0
    {
3284
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3285
0
      return true;
3286
0
    }
3287
3288
  /* If we've already adjusted this symbol, don't do it again.  This
3289
     can happen via a recursive call.  */
3290
0
  if (h->dynamic_adjusted)
3291
0
    return true;
3292
3293
  /* Don't look at this symbol again.  Note that we must set this
3294
     after checking the above conditions, because we may look at a
3295
     symbol once, decide not to do anything, and then get called
3296
     recursively later after REF_REGULAR is set below.  */
3297
0
  h->dynamic_adjusted = 1;
3298
3299
  /* If this is a weak definition, and we know a real definition, and
3300
     the real symbol is not itself defined by a regular object file,
3301
     then get a good value for the real definition.  We handle the
3302
     real symbol first, for the convenience of the backend routine.
3303
3304
     Note that there is a confusing case here.  If the real definition
3305
     is defined by a regular object file, we don't get the real symbol
3306
     from the dynamic object, but we do get the weak symbol.  If the
3307
     processor backend uses a COPY reloc, then if some routine in the
3308
     dynamic object changes the real symbol, we will not see that
3309
     change in the corresponding weak symbol.  This is the way other
3310
     ELF linkers work as well, and seems to be a result of the shared
3311
     library model.
3312
3313
     I will clarify this issue.  Most SVR4 shared libraries define the
3314
     variable _timezone and define timezone as a weak synonym.  The
3315
     tzset call changes _timezone.  If you write
3316
       extern int timezone;
3317
       int _timezone = 5;
3318
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3319
     you might expect that, since timezone is a synonym for _timezone,
3320
     the same number will print both times.  However, if the processor
3321
     backend uses a COPY reloc, then actually timezone will be copied
3322
     into your process image, and, since you define _timezone
3323
     yourself, _timezone will not.  Thus timezone and _timezone will
3324
     wind up at different memory locations.  The tzset call will set
3325
     _timezone, leaving timezone unchanged.  */
3326
3327
0
  if (h->is_weakalias)
3328
0
    {
3329
0
      struct elf_link_hash_entry *def = weakdef (h);
3330
3331
      /* If we get to this point, there is an implicit reference to
3332
   the alias by a regular object file via the weak symbol H.  */
3333
0
      def->ref_regular = 1;
3334
3335
      /* Ensure that the backend adjust_dynamic_symbol function sees
3336
   the strong alias before H by recursively calling ourselves.  */
3337
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3338
0
  return false;
3339
0
    }
3340
3341
  /* If a symbol has no type and no size and does not require a PLT
3342
     entry, then we are probably about to do the wrong thing here: we
3343
     are probably going to create a COPY reloc for an empty object.
3344
     This case can arise when a shared object is built with assembly
3345
     code, and the assembly code fails to set the symbol type.  */
3346
0
  if (h->size == 0
3347
0
      && h->type == STT_NOTYPE
3348
0
      && !h->needs_plt)
3349
0
    _bfd_error_handler
3350
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3351
0
       h->root.root.string);
3352
3353
0
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3354
0
    {
3355
0
      eif->failed = true;
3356
0
      return false;
3357
0
    }
3358
3359
0
  return true;
3360
0
}
3361
3362
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3363
   DYNBSS.  */
3364
3365
bool
3366
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3367
            struct elf_link_hash_entry *h,
3368
            asection *dynbss)
3369
0
{
3370
0
  unsigned int power_of_two;
3371
0
  bfd_vma mask;
3372
0
  asection *sec = h->root.u.def.section;
3373
3374
  /* The section alignment of the definition is the maximum alignment
3375
     requirement of symbols defined in the section.  Since we don't
3376
     know the symbol alignment requirement, we start with the
3377
     maximum alignment and check low bits of the symbol address
3378
     for the minimum alignment.  */
3379
0
  power_of_two = bfd_section_alignment (sec);
3380
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3381
0
  while ((h->root.u.def.value & mask) != 0)
3382
0
    {
3383
0
       mask >>= 1;
3384
0
       --power_of_two;
3385
0
    }
3386
3387
  /* Adjust the section alignment if needed.  */
3388
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3389
0
    return false;
3390
3391
  /* We make sure that the symbol will be aligned properly.  */
3392
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3393
3394
  /* Define the symbol as being at this point in DYNBSS.  */
3395
0
  h->root.u.def.section = dynbss;
3396
0
  h->root.u.def.value = dynbss->size;
3397
3398
  /* Increment the size of DYNBSS to make room for the symbol.  */
3399
0
  dynbss->size += h->size;
3400
3401
  /* No error if extern_protected_data is true.  */
3402
0
  if (h->protected_def
3403
0
      && (!info->extern_protected_data
3404
0
    || (info->extern_protected_data < 0
3405
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3406
0
    info->callbacks->einfo
3407
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3408
0
       h->root.root.string);
3409
3410
0
  return true;
3411
0
}
3412
3413
/* Adjust all external symbols pointing into SEC_MERGE sections
3414
   to reflect the object merging within the sections.  */
3415
3416
static bool
3417
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3418
0
{
3419
0
  asection *sec;
3420
3421
0
  if ((h->root.type == bfd_link_hash_defined
3422
0
       || h->root.type == bfd_link_hash_defweak)
3423
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3424
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3425
0
    {
3426
0
      bfd *output_bfd = (bfd *) data;
3427
3428
0
      h->root.u.def.value =
3429
0
  _bfd_merged_section_offset (output_bfd,
3430
0
            &h->root.u.def.section,
3431
0
            elf_section_data (sec)->sec_info,
3432
0
            h->root.u.def.value);
3433
0
    }
3434
3435
0
  return true;
3436
0
}
3437
3438
/* Returns false if the symbol referred to by H should be considered
3439
   to resolve local to the current module, and true if it should be
3440
   considered to bind dynamically.  */
3441
3442
bool
3443
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3444
         struct bfd_link_info *info,
3445
         bool not_local_protected)
3446
0
{
3447
0
  bool binding_stays_local_p;
3448
0
  const struct elf_backend_data *bed;
3449
0
  struct elf_link_hash_table *hash_table;
3450
3451
0
  if (h == NULL)
3452
0
    return false;
3453
3454
0
  while (h->root.type == bfd_link_hash_indirect
3455
0
   || h->root.type == bfd_link_hash_warning)
3456
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3457
3458
  /* If it was forced local, then clearly it's not dynamic.  */
3459
0
  if (h->dynindx == -1)
3460
0
    return false;
3461
0
  if (h->forced_local)
3462
0
    return false;
3463
3464
  /* Identify the cases where name binding rules say that a
3465
     visible symbol resolves locally.  */
3466
0
  binding_stays_local_p = (bfd_link_executable (info)
3467
0
         || SYMBOLIC_BIND (info, h));
3468
3469
0
  switch (ELF_ST_VISIBILITY (h->other))
3470
0
    {
3471
0
    case STV_INTERNAL:
3472
0
    case STV_HIDDEN:
3473
0
      return false;
3474
3475
0
    case STV_PROTECTED:
3476
0
      hash_table = elf_hash_table (info);
3477
0
      if (!is_elf_hash_table (&hash_table->root))
3478
0
  return false;
3479
3480
0
      bed = get_elf_backend_data (hash_table->dynobj);
3481
3482
      /* Proper resolution for function pointer equality may require
3483
   that these symbols perhaps be resolved dynamically, even though
3484
   we should be resolving them to the current module.  */
3485
0
      if (!not_local_protected || !bed->is_function_type (h->type))
3486
0
  binding_stays_local_p = true;
3487
0
      break;
3488
3489
0
    default:
3490
0
      break;
3491
0
    }
3492
3493
  /* If it isn't defined locally, then clearly it's dynamic.  */
3494
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3495
0
    return true;
3496
3497
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3498
     us that it remains local.  */
3499
0
  return !binding_stays_local_p;
3500
0
}
3501
3502
/* Return true if the symbol referred to by H should be considered
3503
   to resolve local to the current module, and false otherwise.  Differs
3504
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3505
   undefined symbols.  The two functions are virtually identical except
3506
   for the place where dynindx == -1 is tested.  If that test is true,
3507
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3508
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3509
   defined symbols.
3510
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3511
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3512
   treatment of undefined weak symbols.  For those that do not make
3513
   undefined weak symbols dynamic, both functions may return false.  */
3514
3515
bool
3516
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3517
            struct bfd_link_info *info,
3518
            bool local_protected)
3519
0
{
3520
0
  const struct elf_backend_data *bed;
3521
0
  struct elf_link_hash_table *hash_table;
3522
3523
  /* If it's a local sym, of course we resolve locally.  */
3524
0
  if (h == NULL)
3525
0
    return true;
3526
3527
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3528
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3529
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3530
0
    return true;
3531
3532
  /* Forced local symbols resolve locally.  */
3533
0
  if (h->forced_local)
3534
0
    return true;
3535
3536
  /* Common symbols that become definitions don't get the DEF_REGULAR
3537
     flag set, so test it first, and don't bail out.  */
3538
0
  if (ELF_COMMON_DEF_P (h))
3539
0
    /* Do nothing.  */;
3540
  /* If we don't have a definition in a regular file, then we can't
3541
     resolve locally.  The sym is either undefined or dynamic.  */
3542
0
  else if (!h->def_regular)
3543
0
    return false;
3544
3545
  /* Non-dynamic symbols resolve locally.  */
3546
0
  if (h->dynindx == -1)
3547
0
    return true;
3548
3549
  /* At this point, we know the symbol is defined and dynamic.  In an
3550
     executable it must resolve locally, likewise when building symbolic
3551
     shared libraries.  */
3552
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3553
0
    return true;
3554
3555
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3556
     with default visibility might not resolve locally.  */
3557
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3558
0
    return false;
3559
3560
0
  hash_table = elf_hash_table (info);
3561
0
  if (!is_elf_hash_table (&hash_table->root))
3562
0
    return true;
3563
3564
  /* STV_PROTECTED symbols with indirect external access are local. */
3565
0
  if (info->indirect_extern_access > 0)
3566
0
    return true;
3567
3568
0
  bed = get_elf_backend_data (hash_table->dynobj);
3569
3570
  /* If extern_protected_data is false, STV_PROTECTED non-function
3571
     symbols are local.  */
3572
0
  if ((!info->extern_protected_data
3573
0
       || (info->extern_protected_data < 0
3574
0
     && !bed->extern_protected_data))
3575
0
      && !bed->is_function_type (h->type))
3576
0
    return true;
3577
3578
  /* Function pointer equality tests may require that STV_PROTECTED
3579
     symbols be treated as dynamic symbols.  If the address of a
3580
     function not defined in an executable is set to that function's
3581
     plt entry in the executable, then the address of the function in
3582
     a shared library must also be the plt entry in the executable.  */
3583
0
  return local_protected;
3584
0
}
3585
3586
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3587
   aligned.  Returns the first TLS output section.  */
3588
3589
struct bfd_section *
3590
_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3591
0
{
3592
0
  struct bfd_section *sec, *tls;
3593
0
  unsigned int align = 0;
3594
3595
0
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
3596
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3597
0
      break;
3598
0
  tls = sec;
3599
3600
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3601
0
    if (sec->alignment_power > align)
3602
0
      align = sec->alignment_power;
3603
3604
0
  elf_hash_table (info)->tls_sec = tls;
3605
3606
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3607
     alignment, so that the tls segment starts aligned.  */
3608
0
  if (tls != NULL)
3609
0
    (void) bfd_link_align_section (tls, align);
3610
3611
0
  return tls;
3612
0
}
3613
3614
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3615
static bool
3616
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3617
          Elf_Internal_Sym *sym)
3618
0
{
3619
0
  const struct elf_backend_data *bed;
3620
3621
  /* Local symbols do not count, but target specific ones might.  */
3622
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3623
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3624
0
    return false;
3625
3626
0
  bed = get_elf_backend_data (abfd);
3627
  /* Function symbols do not count.  */
3628
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3629
0
    return false;
3630
3631
  /* If the section is undefined, then so is the symbol.  */
3632
0
  if (sym->st_shndx == SHN_UNDEF)
3633
0
    return false;
3634
3635
  /* If the symbol is defined in the common section, then
3636
     it is a common definition and so does not count.  */
3637
0
  if (bed->common_definition (sym))
3638
0
    return false;
3639
3640
  /* If the symbol is in a target specific section then we
3641
     must rely upon the backend to tell us what it is.  */
3642
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3643
    /* FIXME - this function is not coded yet:
3644
3645
       return _bfd_is_global_symbol_definition (abfd, sym);
3646
3647
       Instead for now assume that the definition is not global,
3648
       Even if this is wrong, at least the linker will behave
3649
       in the same way that it used to do.  */
3650
0
    return false;
3651
3652
0
  return true;
3653
0
}
3654
3655
/* Search the symbol table of the archive element of the archive ABFD
3656
   whose archive map contains a mention of SYMDEF, and determine if
3657
   the symbol is defined in this element.  */
3658
static bool
3659
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3660
0
{
3661
0
  Elf_Internal_Shdr * hdr;
3662
0
  size_t symcount;
3663
0
  size_t extsymcount;
3664
0
  size_t extsymoff;
3665
0
  Elf_Internal_Sym *isymbuf;
3666
0
  Elf_Internal_Sym *isym;
3667
0
  Elf_Internal_Sym *isymend;
3668
0
  bool result;
3669
3670
0
  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3671
0
  if (abfd == NULL)
3672
0
    return false;
3673
3674
0
  if (! bfd_check_format (abfd, bfd_object))
3675
0
    return false;
3676
3677
  /* Select the appropriate symbol table.  If we don't know if the
3678
     object file is an IR object, give linker LTO plugin a chance to
3679
     get the correct symbol table.  */
3680
0
  if (abfd->plugin_format == bfd_plugin_yes
3681
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3682
0
#if BFD_SUPPORTS_PLUGINS
3683
0
      || (abfd->plugin_format == bfd_plugin_unknown
3684
0
    && bfd_link_plugin_object_p (abfd))
3685
0
#endif
3686
0
      )
3687
0
    {
3688
      /* Use the IR symbol table if the object has been claimed by
3689
   plugin.  */
3690
0
      abfd = abfd->plugin_dummy_bfd;
3691
0
      hdr = &elf_tdata (abfd)->symtab_hdr;
3692
0
    }
3693
0
  else
3694
0
    {
3695
0
      if (elf_use_dt_symtab_p (abfd))
3696
0
  {
3697
0
    bfd_set_error (bfd_error_wrong_format);
3698
0
    return false;
3699
0
  }
3700
3701
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3702
0
  hdr = &elf_tdata (abfd)->symtab_hdr;
3703
0
      else
3704
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3705
0
    }
3706
3707
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3708
3709
  /* The sh_info field of the symtab header tells us where the
3710
     external symbols start.  We don't care about the local symbols.  */
3711
0
  if (elf_bad_symtab (abfd))
3712
0
    {
3713
0
      extsymcount = symcount;
3714
0
      extsymoff = 0;
3715
0
    }
3716
0
  else
3717
0
    {
3718
0
      extsymcount = symcount - hdr->sh_info;
3719
0
      extsymoff = hdr->sh_info;
3720
0
    }
3721
3722
0
  if (extsymcount == 0)
3723
0
    return false;
3724
3725
  /* Read in the symbol table.  */
3726
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3727
0
          NULL, NULL, NULL);
3728
0
  if (isymbuf == NULL)
3729
0
    return false;
3730
3731
  /* Scan the symbol table looking for SYMDEF.  */
3732
0
  result = false;
3733
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3734
0
    {
3735
0
      const char *name;
3736
3737
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3738
0
                isym->st_name);
3739
0
      if (name == NULL)
3740
0
  break;
3741
3742
0
      if (strcmp (name, symdef->name) == 0)
3743
0
  {
3744
0
    result = is_global_data_symbol_definition (abfd, isym);
3745
0
    break;
3746
0
  }
3747
0
    }
3748
3749
0
  free (isymbuf);
3750
3751
0
  return result;
3752
0
}
3753

3754
/* Add an entry to the .dynamic table.  */
3755
3756
bool
3757
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3758
          bfd_vma tag,
3759
          bfd_vma val)
3760
0
{
3761
0
  struct elf_link_hash_table *hash_table;
3762
0
  const struct elf_backend_data *bed;
3763
0
  asection *s;
3764
0
  bfd_size_type newsize;
3765
0
  bfd_byte *newcontents;
3766
0
  Elf_Internal_Dyn dyn;
3767
3768
0
  hash_table = elf_hash_table (info);
3769
0
  if (! is_elf_hash_table (&hash_table->root))
3770
0
    return false;
3771
3772
0
  if (tag == DT_RELA || tag == DT_REL)
3773
0
    hash_table->dynamic_relocs = true;
3774
3775
0
  bed = get_elf_backend_data (hash_table->dynobj);
3776
0
  s = hash_table->dynamic;
3777
0
  BFD_ASSERT (s != NULL);
3778
3779
0
  newsize = s->size + bed->s->sizeof_dyn;
3780
0
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3781
0
  if (newcontents == NULL)
3782
0
    return false;
3783
3784
0
  dyn.d_tag = tag;
3785
0
  dyn.d_un.d_val = val;
3786
0
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3787
3788
0
  s->size = newsize;
3789
0
  s->contents = newcontents;
3790
3791
0
  return true;
3792
0
}
3793
3794
/* Strip zero-sized dynamic sections.  */
3795
3796
bool
3797
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3798
0
{
3799
0
  struct elf_link_hash_table *hash_table;
3800
0
  const struct elf_backend_data *bed;
3801
0
  asection *s, *sdynamic, **pp;
3802
0
  asection *rela_dyn, *rel_dyn;
3803
0
  Elf_Internal_Dyn dyn;
3804
0
  bfd_byte *extdyn, *next;
3805
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3806
0
  bool strip_zero_sized;
3807
0
  bool strip_zero_sized_plt;
3808
3809
0
  if (bfd_link_relocatable (info))
3810
0
    return true;
3811
3812
0
  hash_table = elf_hash_table (info);
3813
0
  if (!is_elf_hash_table (&hash_table->root))
3814
0
    return false;
3815
3816
0
  if (!hash_table->dynobj)
3817
0
    return true;
3818
3819
0
  sdynamic= hash_table->dynamic;
3820
0
  if (!sdynamic)
3821
0
    return true;
3822
3823
0
  bed = get_elf_backend_data (hash_table->dynobj);
3824
0
  swap_dyn_in = bed->s->swap_dyn_in;
3825
3826
0
  strip_zero_sized = false;
3827
0
  strip_zero_sized_plt = false;
3828
3829
  /* Strip zero-sized dynamic sections.  */
3830
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3831
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3832
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3833
0
    if (s->size == 0
3834
0
  && (s == rela_dyn
3835
0
      || s == rel_dyn
3836
0
      || s == hash_table->srelplt->output_section
3837
0
      || s == hash_table->splt->output_section))
3838
0
      {
3839
0
  *pp = s->next;
3840
0
  info->output_bfd->section_count--;
3841
0
  strip_zero_sized = true;
3842
0
  if (s == rela_dyn)
3843
0
    s = rela_dyn;
3844
0
  if (s == rel_dyn)
3845
0
    s = rel_dyn;
3846
0
  else if (s == hash_table->splt->output_section)
3847
0
    {
3848
0
      s = hash_table->splt;
3849
0
      strip_zero_sized_plt = true;
3850
0
    }
3851
0
  else
3852
0
    s = hash_table->srelplt;
3853
0
  s->flags |= SEC_EXCLUDE;
3854
0
  s->output_section = bfd_abs_section_ptr;
3855
0
      }
3856
0
    else
3857
0
      pp = &s->next;
3858
3859
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3860
0
    for (extdyn = sdynamic->contents;
3861
0
   extdyn < sdynamic->contents + sdynamic->size;
3862
0
   extdyn = next)
3863
0
      {
3864
0
  next = extdyn + bed->s->sizeof_dyn;
3865
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3866
0
  switch (dyn.d_tag)
3867
0
    {
3868
0
    default:
3869
0
      break;
3870
0
    case DT_JMPREL:
3871
0
    case DT_PLTRELSZ:
3872
0
    case DT_PLTREL:
3873
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3874
         the procedure linkage table (the .plt section) has been
3875
         removed.  */
3876
0
      memmove (extdyn, next,
3877
0
         sdynamic->size - (next - sdynamic->contents));
3878
0
      next = extdyn;
3879
0
    }
3880
0
      }
3881
3882
0
  if (strip_zero_sized)
3883
0
    {
3884
      /* Regenerate program headers.  */
3885
0
      elf_seg_map (info->output_bfd) = NULL;
3886
0
      return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3887
0
            NULL);
3888
0
    }
3889
3890
0
  return true;
3891
0
}
3892
3893
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3894
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3895
3896
int
3897
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3898
0
{
3899
0
  struct elf_link_hash_table *hash_table;
3900
0
  size_t strindex;
3901
0
  const char *soname;
3902
3903
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3904
0
    return -1;
3905
3906
0
  hash_table = elf_hash_table (info);
3907
0
  soname = elf_dt_name (abfd);
3908
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3909
0
  if (strindex == (size_t) -1)
3910
0
    return -1;
3911
3912
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3913
0
    {
3914
0
      asection *sdyn;
3915
0
      const struct elf_backend_data *bed;
3916
0
      bfd_byte *extdyn;
3917
3918
0
      bed = get_elf_backend_data (hash_table->dynobj);
3919
0
      sdyn = hash_table->dynamic;
3920
0
      if (sdyn != NULL && sdyn->size != 0)
3921
0
  for (extdyn = sdyn->contents;
3922
0
       extdyn < sdyn->contents + sdyn->size;
3923
0
       extdyn += bed->s->sizeof_dyn)
3924
0
    {
3925
0
      Elf_Internal_Dyn dyn;
3926
3927
0
      bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3928
0
      if (dyn.d_tag == DT_NEEDED
3929
0
    && dyn.d_un.d_val == strindex)
3930
0
        {
3931
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3932
0
    return 1;
3933
0
        }
3934
0
    }
3935
0
    }
3936
3937
0
  if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3938
0
    return -1;
3939
3940
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3941
0
    return -1;
3942
3943
0
  return 0;
3944
0
}
3945
3946
/* Return true if SONAME is on the needed list between NEEDED and STOP
3947
   (or the end of list if STOP is NULL), and needed by a library that
3948
   will be loaded.  */
3949
3950
static bool
3951
on_needed_list (const char *soname,
3952
    struct bfd_link_needed_list *needed,
3953
    struct bfd_link_needed_list *stop)
3954
0
{
3955
0
  struct bfd_link_needed_list *look;
3956
0
  for (look = needed; look != stop; look = look->next)
3957
0
    if (strcmp (soname, look->name) == 0
3958
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3959
      /* If needed by a library that itself is not directly
3960
         needed, recursively check whether that library is
3961
         indirectly needed.  Since we add DT_NEEDED entries to
3962
         the end of the list, library dependencies appear after
3963
         the library.  Therefore search prior to the current
3964
         LOOK, preventing possible infinite recursion.  */
3965
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
3966
0
      return true;
3967
3968
0
  return false;
3969
0
}
3970
3971
/* Sort symbol by value, section, size, and type.  */
3972
static int
3973
elf_sort_symbol (const void *arg1, const void *arg2)
3974
0
{
3975
0
  const struct elf_link_hash_entry *h1;
3976
0
  const struct elf_link_hash_entry *h2;
3977
0
  bfd_signed_vma vdiff;
3978
0
  int sdiff;
3979
0
  const char *n1;
3980
0
  const char *n2;
3981
3982
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
3983
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
3984
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
3985
0
  if (vdiff != 0)
3986
0
    return vdiff > 0 ? 1 : -1;
3987
3988
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3989
0
  if (sdiff != 0)
3990
0
    return sdiff;
3991
3992
  /* Sort so that sized symbols are selected over zero size symbols.  */
3993
0
  vdiff = h1->size - h2->size;
3994
0
  if (vdiff != 0)
3995
0
    return vdiff > 0 ? 1 : -1;
3996
3997
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
3998
0
  if (h1->type != h2->type)
3999
0
    return h1->type - h2->type;
4000
4001
  /* If symbols are properly sized and typed, and multiple strong
4002
     aliases are not defined in a shared library by the user we
4003
     shouldn't get here.  Unfortunately linker script symbols like
4004
     __bss_start sometimes match a user symbol defined at the start of
4005
     .bss without proper size and type.  We'd like to preference the
4006
     user symbol over reserved system symbols.  Sort on leading
4007
     underscores.  */
4008
0
  n1 = h1->root.root.string;
4009
0
  n2 = h2->root.root.string;
4010
0
  while (*n1 == *n2)
4011
0
    {
4012
0
      if (*n1 == 0)
4013
0
  break;
4014
0
      ++n1;
4015
0
      ++n2;
4016
0
    }
4017
0
  if (*n1 == '_')
4018
0
    return -1;
4019
0
  if (*n2 == '_')
4020
0
    return 1;
4021
4022
  /* Final sort on name selects user symbols like '_u' over reserved
4023
     system symbols like '_Z' and also will avoid qsort instability.  */
4024
0
  return *n1 - *n2;
4025
0
}
4026
4027
/* This function is used to adjust offsets into .dynstr for
4028
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4029
4030
static bool
4031
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4032
0
{
4033
0
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
4034
4035
0
  if (h->dynindx != -1)
4036
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4037
0
  return true;
4038
0
}
4039
4040
/* Assign string offsets in .dynstr, update all structures referencing
4041
   them.  */
4042
4043
static bool
4044
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4045
0
{
4046
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4047
0
  struct elf_link_local_dynamic_entry *entry;
4048
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4049
0
  bfd *dynobj = hash_table->dynobj;
4050
0
  asection *sdyn;
4051
0
  bfd_size_type size;
4052
0
  const struct elf_backend_data *bed;
4053
0
  bfd_byte *extdyn;
4054
4055
0
  _bfd_elf_strtab_finalize (dynstr);
4056
0
  size = _bfd_elf_strtab_size (dynstr);
4057
4058
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4059
4060
0
  if (info->callbacks->examine_strtab)
4061
0
    info->callbacks->examine_strtab (dynstr);
4062
4063
0
  bed = get_elf_backend_data (dynobj);
4064
0
  sdyn = hash_table->dynamic;
4065
0
  BFD_ASSERT (sdyn != NULL);
4066
4067
  /* Update all .dynamic entries referencing .dynstr strings.  */
4068
0
  for (extdyn = sdyn->contents;
4069
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4070
0
       extdyn += bed->s->sizeof_dyn)
4071
0
    {
4072
0
      Elf_Internal_Dyn dyn;
4073
4074
0
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4075
0
      switch (dyn.d_tag)
4076
0
  {
4077
0
  case DT_STRSZ:
4078
0
    dyn.d_un.d_val = size;
4079
0
    break;
4080
0
  case DT_NEEDED:
4081
0
  case DT_SONAME:
4082
0
  case DT_RPATH:
4083
0
  case DT_RUNPATH:
4084
0
  case DT_FILTER:
4085
0
  case DT_AUXILIARY:
4086
0
  case DT_AUDIT:
4087
0
  case DT_DEPAUDIT:
4088
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4089
0
    break;
4090
0
  default:
4091
0
    continue;
4092
0
  }
4093
0
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4094
0
    }
4095
4096
  /* Now update local dynamic symbols.  */
4097
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4098
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4099
0
              entry->isym.st_name);
4100
4101
  /* And the rest of dynamic symbols.  */
4102
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4103
4104
  /* Adjust version definitions.  */
4105
0
  if (elf_tdata (output_bfd)->cverdefs)
4106
0
    {
4107
0
      asection *s;
4108
0
      bfd_byte *p;
4109
0
      size_t i;
4110
0
      Elf_Internal_Verdef def;
4111
0
      Elf_Internal_Verdaux defaux;
4112
4113
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4114
0
      p = s->contents;
4115
0
      do
4116
0
  {
4117
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4118
0
           &def);
4119
0
    p += sizeof (Elf_External_Verdef);
4120
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4121
0
      continue;
4122
0
    for (i = 0; i < def.vd_cnt; ++i)
4123
0
      {
4124
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4125
0
          (Elf_External_Verdaux *) p, &defaux);
4126
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4127
0
              defaux.vda_name);
4128
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4129
0
           &defaux, (Elf_External_Verdaux *) p);
4130
0
        p += sizeof (Elf_External_Verdaux);
4131
0
      }
4132
0
  }
4133
0
      while (def.vd_next);
4134
0
    }
4135
4136
  /* Adjust version references.  */
4137
0
  if (elf_tdata (output_bfd)->verref)
4138
0
    {
4139
0
      asection *s;
4140
0
      bfd_byte *p;
4141
0
      size_t i;
4142
0
      Elf_Internal_Verneed need;
4143
0
      Elf_Internal_Vernaux needaux;
4144
4145
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4146
0
      p = s->contents;
4147
0
      do
4148
0
  {
4149
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4150
0
            &need);
4151
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4152
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4153
0
             (Elf_External_Verneed *) p);
4154
0
    p += sizeof (Elf_External_Verneed);
4155
0
    for (i = 0; i < need.vn_cnt; ++i)
4156
0
      {
4157
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4158
0
          (Elf_External_Vernaux *) p, &needaux);
4159
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4160
0
               needaux.vna_name);
4161
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4162
0
           &needaux,
4163
0
           (Elf_External_Vernaux *) p);
4164
0
        p += sizeof (Elf_External_Vernaux);
4165
0
      }
4166
0
  }
4167
0
      while (need.vn_next);
4168
0
    }
4169
4170
0
  return true;
4171
0
}
4172

4173
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4174
   The default is to only match when the INPUT and OUTPUT are exactly
4175
   the same target.  */
4176
4177
bool
4178
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4179
            const bfd_target *output)
4180
0
{
4181
0
  return input == output;
4182
0
}
4183
4184
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4185
   This version is used when different targets for the same architecture
4186
   are virtually identical.  */
4187
4188
bool
4189
_bfd_elf_relocs_compatible (const bfd_target *input,
4190
          const bfd_target *output)
4191
0
{
4192
0
  const struct elf_backend_data *obed, *ibed;
4193
4194
0
  if (input == output)
4195
0
    return true;
4196
4197
0
  ibed = xvec_get_elf_backend_data (input);
4198
0
  obed = xvec_get_elf_backend_data (output);
4199
4200
0
  if (ibed->arch != obed->arch)
4201
0
    return false;
4202
4203
  /* If both backends are using this function, deem them compatible.  */
4204
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4205
0
}
4206
4207
/* Make a special call to the linker "notice" function to tell it that
4208
   we are about to handle an as-needed lib, or have finished
4209
   processing the lib.  */
4210
4211
bool
4212
_bfd_elf_notice_as_needed (bfd *ibfd,
4213
         struct bfd_link_info *info,
4214
         enum notice_asneeded_action act)
4215
0
{
4216
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4217
0
}
4218
4219
/* Call ACTION on each relocation in an ELF object file.  */
4220
4221
bool
4222
_bfd_elf_link_iterate_on_relocs
4223
  (bfd *abfd, struct bfd_link_info *info,
4224
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4225
       const Elf_Internal_Rela *))
4226
0
{
4227
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4228
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4229
4230
  /* If this object is the same format as the output object, and it is
4231
     not a shared library, then let the backend look through the
4232
     relocs.
4233
4234
     This is required to build global offset table entries and to
4235
     arrange for dynamic relocs.  It is not required for the
4236
     particular common case of linking non PIC code, even when linking
4237
     against shared libraries, but unfortunately there is no way of
4238
     knowing whether an object file has been compiled PIC or not.
4239
     Looking through the relocs is not particularly time consuming.
4240
     The problem is that we must either (1) keep the relocs in memory,
4241
     which causes the linker to require additional runtime memory or
4242
     (2) read the relocs twice from the input file, which wastes time.
4243
     This would be a good case for using mmap.
4244
4245
     I have no idea how to handle linking PIC code into a file of a
4246
     different format.  It probably can't be done.  */
4247
0
  if ((abfd->flags & DYNAMIC) == 0
4248
0
      && is_elf_hash_table (&htab->root)
4249
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4250
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4251
0
    {
4252
0
      asection *o;
4253
4254
0
      for (o = abfd->sections; o != NULL; o = o->next)
4255
0
  {
4256
0
    Elf_Internal_Rela *internal_relocs;
4257
0
    bool ok;
4258
4259
    /* Don't check relocations in excluded sections.  Don't do
4260
       anything special with non-loaded, non-alloced sections.
4261
       In particular, any relocs in such sections should not
4262
       affect GOT and PLT reference counting (ie.  we don't
4263
       allow them to create GOT or PLT entries), there's no
4264
       possibility or desire to optimize TLS relocs, and
4265
       there's not much point in propagating relocs to shared
4266
       libs that the dynamic linker won't relocate.  */
4267
0
    if ((o->flags & SEC_ALLOC) == 0
4268
0
        || (o->flags & SEC_RELOC) == 0
4269
0
        || (o->flags & SEC_EXCLUDE) != 0
4270
0
        || o->reloc_count == 0
4271
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4272
0
      && (o->flags & SEC_DEBUGGING) != 0)
4273
0
        || bfd_is_abs_section (o->output_section))
4274
0
      continue;
4275
4276
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4277
0
      (abfd, info, o, NULL, NULL,
4278
0
       _bfd_elf_link_keep_memory (info));
4279
0
    if (internal_relocs == NULL)
4280
0
      return false;
4281
4282
0
    ok = action (abfd, info, o, internal_relocs);
4283
4284
0
    if (elf_section_data (o)->relocs != internal_relocs)
4285
0
      free (internal_relocs);
4286
4287
0
    if (! ok)
4288
0
      return false;
4289
0
  }
4290
0
    }
4291
4292
0
  return true;
4293
0
}
4294
4295
/* Check relocations in an ELF object file.  This is called after
4296
   all input files have been opened.  */
4297
4298
bool
4299
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4300
0
{
4301
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4302
0
  if (bed->check_relocs != NULL)
4303
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4304
0
              bed->check_relocs);
4305
0
  return true;
4306
0
}
4307
4308
/* An entry in the first definition hash table.  */
4309
4310
struct elf_link_first_hash_entry
4311
{
4312
  struct bfd_hash_entry root;
4313
  /* The object of the first definition.  */
4314
  bfd *abfd;
4315
};
4316
4317
/* The function to create a new entry in the first definition hash
4318
   table.  */
4319
4320
static struct bfd_hash_entry *
4321
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4322
           struct bfd_hash_table *table,
4323
           const char *string)
4324
0
{
4325
0
  struct elf_link_first_hash_entry *ret =
4326
0
    (struct elf_link_first_hash_entry *) entry;
4327
4328
  /* Allocate the structure if it has not already been allocated by a
4329
     subclass.  */
4330
0
  if (ret == NULL)
4331
0
    ret = (struct elf_link_first_hash_entry *)
4332
0
  bfd_hash_allocate (table,
4333
0
         sizeof (struct elf_link_first_hash_entry));
4334
0
  if (ret == NULL)
4335
0
    return NULL;
4336
4337
  /* Call the allocation method of the superclass.  */
4338
0
  ret = ((struct elf_link_first_hash_entry *)
4339
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4340
0
         string));
4341
0
  if (ret != NULL)
4342
0
    ret->abfd = NULL;
4343
4344
0
  return (struct bfd_hash_entry *) ret;
4345
0
}
4346
4347
/* Add the symbol NAME from ABFD to first hash.  */
4348
4349
static void
4350
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4351
          const char *name, bool copy)
4352
0
{
4353
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4354
  /* Skip if there is no first hash.  */
4355
0
  if (htab->first_hash == NULL)
4356
0
    return;
4357
4358
0
  struct elf_link_first_hash_entry *e
4359
0
    = ((struct elf_link_first_hash_entry *)
4360
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4361
0
  if (e == NULL)
4362
0
    info->callbacks->fatal
4363
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4364
4365
0
  if (e->abfd == NULL)
4366
    /* Store ABFD in abfd.  */
4367
0
    e->abfd = abfd;
4368
0
}
4369
4370
/* Add symbols from an ELF object file to the linker hash table.  */
4371
4372
static bool
4373
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4374
0
{
4375
0
  Elf_Internal_Ehdr *ehdr;
4376
0
  Elf_Internal_Shdr *hdr;
4377
0
  size_t symcount;
4378
0
  size_t extsymcount;
4379
0
  size_t extsymoff;
4380
0
  struct elf_link_hash_entry **sym_hash;
4381
0
  bool dynamic;
4382
0
  Elf_External_Versym *extversym = NULL;
4383
0
  Elf_External_Versym *extversym_end = NULL;
4384
0
  Elf_External_Versym *ever;
4385
0
  struct elf_link_hash_entry *weaks;
4386
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4387
0
  size_t nondeflt_vers_cnt = 0;
4388
0
  Elf_Internal_Sym *isymbuf = NULL;
4389
0
  Elf_Internal_Sym *isym;
4390
0
  Elf_Internal_Sym *isymend;
4391
0
  const struct elf_backend_data *bed;
4392
0
  bool add_needed;
4393
0
  struct elf_link_hash_table *htab;
4394
0
  void *alloc_mark = NULL;
4395
0
  struct bfd_hash_entry **old_table = NULL;
4396
0
  unsigned int old_size = 0;
4397
0
  unsigned int old_count = 0;
4398
0
  void *old_tab = NULL;
4399
0
  void *old_ent;
4400
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4401
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4402
0
  void *old_strtab = NULL;
4403
0
  size_t tabsize = 0;
4404
0
  asection *s;
4405
0
  bool just_syms;
4406
4407
0
  htab = elf_hash_table (info);
4408
0
  bed = get_elf_backend_data (abfd);
4409
4410
0
  if (elf_use_dt_symtab_p (abfd))
4411
0
    {
4412
0
      bfd_set_error (bfd_error_wrong_format);
4413
0
      return false;
4414
0
    }
4415
4416
0
  if ((abfd->flags & DYNAMIC) == 0)
4417
0
    {
4418
0
      dynamic = false;
4419
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4420
0
    && is_elf_hash_table (&htab->root)
4421
0
    && htab->first_hash == NULL)
4422
0
  {
4423
    /* Initialize first_hash for an IR input.  */
4424
0
    htab->first_hash = (struct bfd_hash_table *)
4425
0
      bfd_malloc (sizeof (struct bfd_hash_table));
4426
0
    if (htab->first_hash == NULL
4427
0
        || !bfd_hash_table_init
4428
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4429
0
        sizeof (struct elf_link_first_hash_entry)))
4430
0
      info->callbacks->fatal
4431
0
        (_("%P: first_hash failed to create: %E\n"));
4432
0
  }
4433
0
    }
4434
0
  else
4435
0
    {
4436
0
      dynamic = true;
4437
4438
      /* You can't use -r against a dynamic object.  Also, there's no
4439
   hope of using a dynamic object which does not exactly match
4440
   the format of the output file.  */
4441
0
      if (bfd_link_relocatable (info)
4442
0
    || !is_elf_hash_table (&htab->root)
4443
0
    || info->output_bfd->xvec != abfd->xvec)
4444
0
  {
4445
0
    if (bfd_link_relocatable (info))
4446
0
      bfd_set_error (bfd_error_invalid_operation);
4447
0
    else
4448
0
      bfd_set_error (bfd_error_wrong_format);
4449
0
    goto error_return;
4450
0
  }
4451
0
    }
4452
4453
0
  ehdr = elf_elfheader (abfd);
4454
0
  if (info->warn_alternate_em
4455
0
      && bed->elf_machine_code != ehdr->e_machine
4456
0
      && ((bed->elf_machine_alt1 != 0
4457
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4458
0
    || (bed->elf_machine_alt2 != 0
4459
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4460
0
    _bfd_error_handler
4461
      /* xgettext:c-format */
4462
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4463
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4464
4465
  /* As a GNU extension, any input sections which are named
4466
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4467
     symbol.  This differs from .gnu.warning sections, which generate
4468
     warnings when they are included in an output file.  */
4469
  /* PR 12761: Also generate this warning when building shared libraries.  */
4470
0
  for (s = abfd->sections; s != NULL; s = s->next)
4471
0
    {
4472
0
      const char *name;
4473
4474
0
      name = bfd_section_name (s);
4475
0
      if (startswith (name, ".gnu.warning."))
4476
0
  {
4477
0
    char *msg;
4478
0
    bfd_size_type sz;
4479
4480
0
    name += sizeof ".gnu.warning." - 1;
4481
4482
    /* If this is a shared object, then look up the symbol
4483
       in the hash table.  If it is there, and it is already
4484
       been defined, then we will not be using the entry
4485
       from this shared object, so we don't need to warn.
4486
       FIXME: If we see the definition in a regular object
4487
       later on, we will warn, but we shouldn't.  The only
4488
       fix is to keep track of what warnings we are supposed
4489
       to emit, and then handle them all at the end of the
4490
       link.  */
4491
0
    if (dynamic)
4492
0
      {
4493
0
        struct elf_link_hash_entry *h;
4494
4495
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4496
4497
        /* FIXME: What about bfd_link_hash_common?  */
4498
0
        if (h != NULL
4499
0
      && (h->root.type == bfd_link_hash_defined
4500
0
          || h->root.type == bfd_link_hash_defweak))
4501
0
    continue;
4502
0
      }
4503
4504
0
    sz = s->size;
4505
0
    msg = (char *) bfd_alloc (abfd, sz + 1);
4506
0
    if (msg == NULL)
4507
0
      goto error_return;
4508
4509
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4510
0
      goto error_return;
4511
4512
0
    msg[sz] = '\0';
4513
4514
0
    if (! (_bfd_generic_link_add_one_symbol
4515
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4516
0
      false, bed->collect, NULL)))
4517
0
      goto error_return;
4518
4519
0
    if (bfd_link_executable (info))
4520
0
      {
4521
        /* Clobber the section size so that the warning does
4522
     not get copied into the output file.  */
4523
0
        s->size = 0;
4524
4525
        /* Also set SEC_EXCLUDE, so that symbols defined in
4526
     the warning section don't get copied to the output.  */
4527
0
        s->flags |= SEC_EXCLUDE;
4528
0
      }
4529
0
  }
4530
0
    }
4531
4532
0
  just_syms = ((s = abfd->sections) != NULL
4533
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4534
4535
0
  add_needed = true;
4536
0
  if (! dynamic)
4537
0
    {
4538
      /* If we are creating a shared library, create all the dynamic
4539
   sections immediately.  We need to attach them to something,
4540
   so we attach them to this BFD, provided it is the right
4541
   format and is not from ld --just-symbols.  Always create the
4542
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4543
   are no input BFD's of the same format as the output, we can't
4544
   make a shared library.  */
4545
0
      if (!just_syms
4546
0
    && (bfd_link_pic (info)
4547
0
        || (!bfd_link_relocatable (info)
4548
0
      && info->nointerp
4549
0
      && (info->export_dynamic || info->dynamic)))
4550
0
    && is_elf_hash_table (&htab->root)
4551
0
    && info->output_bfd->xvec == abfd->xvec
4552
0
    && !htab->dynamic_sections_created)
4553
0
  {
4554
0
    if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4555
0
      goto error_return;
4556
0
  }
4557
0
    }
4558
0
  else if (!is_elf_hash_table (&htab->root))
4559
0
    goto error_return;
4560
0
  else
4561
0
    {
4562
0
      const char *soname = NULL;
4563
0
      char *audit = NULL;
4564
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4565
0
      const Elf_Internal_Phdr *phdr;
4566
0
      struct elf_link_loaded_list *loaded_lib;
4567
4568
      /* ld --just-symbols and dynamic objects don't mix very well.
4569
   ld shouldn't allow it.  */
4570
0
      if (just_syms)
4571
0
  abort ();
4572
4573
      /* If this dynamic lib was specified on the command line with
4574
   --as-needed in effect, then we don't want to add a DT_NEEDED
4575
   tag unless the lib is actually used.  Similary for libs brought
4576
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4577
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4578
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4579
   all.  */
4580
0
      add_needed = (elf_dyn_lib_class (abfd)
4581
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4582
0
           | DYN_NO_NEEDED)) == 0;
4583
4584
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4585
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4586
0
  {
4587
0
    bfd_byte *dynbuf;
4588
0
    bfd_byte *extdyn;
4589
0
    unsigned int elfsec;
4590
0
    unsigned long shlink;
4591
4592
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4593
0
      {
4594
0
      error_free_dyn:
4595
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4596
0
        goto error_return;
4597
0
      }
4598
4599
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4600
0
    if (elfsec == SHN_BAD)
4601
0
      goto error_free_dyn;
4602
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4603
4604
0
    for (extdyn = dynbuf;
4605
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4606
0
         extdyn += bed->s->sizeof_dyn)
4607
0
      {
4608
0
        Elf_Internal_Dyn dyn;
4609
4610
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4611
0
        if (dyn.d_tag == DT_SONAME)
4612
0
    {
4613
0
      unsigned int tagv = dyn.d_un.d_val;
4614
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4615
0
      if (soname == NULL)
4616
0
        goto error_free_dyn;
4617
0
    }
4618
0
        if (dyn.d_tag == DT_NEEDED)
4619
0
    {
4620
0
      struct bfd_link_needed_list *n, **pn;
4621
0
      char *fnm, *anm;
4622
0
      unsigned int tagv = dyn.d_un.d_val;
4623
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4624
4625
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4626
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4627
0
      if (n == NULL || fnm == NULL)
4628
0
        goto error_free_dyn;
4629
0
      amt = strlen (fnm) + 1;
4630
0
      anm = (char *) bfd_alloc (abfd, amt);
4631
0
      if (anm == NULL)
4632
0
        goto error_free_dyn;
4633
0
      memcpy (anm, fnm, amt);
4634
0
      n->name = anm;
4635
0
      n->by = abfd;
4636
0
      n->next = NULL;
4637
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4638
0
        ;
4639
0
      *pn = n;
4640
0
    }
4641
0
        if (dyn.d_tag == DT_RUNPATH)
4642
0
    {
4643
0
      struct bfd_link_needed_list *n, **pn;
4644
0
      char *fnm, *anm;
4645
0
      unsigned int tagv = dyn.d_un.d_val;
4646
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4647
4648
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4649
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4650
0
      if (n == NULL || fnm == NULL)
4651
0
        goto error_free_dyn;
4652
0
      amt = strlen (fnm) + 1;
4653
0
      anm = (char *) bfd_alloc (abfd, amt);
4654
0
      if (anm == NULL)
4655
0
        goto error_free_dyn;
4656
0
      memcpy (anm, fnm, amt);
4657
0
      n->name = anm;
4658
0
      n->by = abfd;
4659
0
      n->next = NULL;
4660
0
      for (pn = & runpath;
4661
0
           *pn != NULL;
4662
0
           pn = &(*pn)->next)
4663
0
        ;
4664
0
      *pn = n;
4665
0
    }
4666
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4667
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4668
0
    {
4669
0
      struct bfd_link_needed_list *n, **pn;
4670
0
      char *fnm, *anm;
4671
0
      unsigned int tagv = dyn.d_un.d_val;
4672
0
      size_t amt = sizeof (struct bfd_link_needed_list);
4673
4674
0
      n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4675
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4676
0
      if (n == NULL || fnm == NULL)
4677
0
        goto error_free_dyn;
4678
0
      amt = strlen (fnm) + 1;
4679
0
      anm = (char *) bfd_alloc (abfd, amt);
4680
0
      if (anm == NULL)
4681
0
        goto error_free_dyn;
4682
0
      memcpy (anm, fnm, amt);
4683
0
      n->name = anm;
4684
0
      n->by = abfd;
4685
0
      n->next = NULL;
4686
0
      for (pn = & rpath;
4687
0
           *pn != NULL;
4688
0
           pn = &(*pn)->next)
4689
0
        ;
4690
0
      *pn = n;
4691
0
    }
4692
0
        if (dyn.d_tag == DT_AUDIT)
4693
0
    {
4694
0
      unsigned int tagv = dyn.d_un.d_val;
4695
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4696
0
    }
4697
0
        if (dyn.d_tag == DT_FLAGS_1)
4698
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4699
0
      }
4700
4701
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4702
0
  }
4703
4704
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4705
   frees all more recently bfd_alloc'd blocks as well.  */
4706
0
      if (runpath)
4707
0
  rpath = runpath;
4708
4709
0
      if (rpath)
4710
0
  {
4711
0
    struct bfd_link_needed_list **pn;
4712
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4713
0
      ;
4714
0
    *pn = rpath;
4715
0
  }
4716
4717
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4718
   all sections contained fully therein.  This makes relro
4719
   shared library sections appear as they will at run-time.  */
4720
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4721
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4722
0
  if (phdr->p_type == PT_GNU_RELRO)
4723
0
    {
4724
0
      for (s = abfd->sections; s != NULL; s = s->next)
4725
0
        {
4726
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4727
4728
0
    if ((s->flags & SEC_ALLOC) != 0
4729
0
        && s->vma * opb >= phdr->p_vaddr
4730
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4731
0
      s->flags |= SEC_READONLY;
4732
0
        }
4733
0
      break;
4734
0
    }
4735
4736
      /* We do not want to include any of the sections in a dynamic
4737
   object in the output file.  We hack by simply clobbering the
4738
   list of sections in the BFD.  This could be handled more
4739
   cleanly by, say, a new section flag; the existing
4740
   SEC_NEVER_LOAD flag is not the one we want, because that one
4741
   still implies that the section takes up space in the output
4742
   file.  */
4743
0
      bfd_section_list_clear (abfd);
4744
4745
      /* Find the name to use in a DT_NEEDED entry that refers to this
4746
   object.  If the object has a DT_SONAME entry, we use it.
4747
   Otherwise, if the generic linker stuck something in
4748
   elf_dt_name, we use that.  Otherwise, we just use the file
4749
   name.  */
4750
0
      if (soname == NULL || *soname == '\0')
4751
0
  {
4752
0
    soname = elf_dt_name (abfd);
4753
0
    if (soname == NULL || *soname == '\0')
4754
0
      soname = bfd_get_filename (abfd);
4755
0
  }
4756
4757
      /* Save the SONAME because sometimes the linker emulation code
4758
   will need to know it.  */
4759
0
      elf_dt_name (abfd) = soname;
4760
4761
      /* If we have already included this dynamic object in the
4762
   link, just ignore it.  There is no reason to include a
4763
   particular dynamic object more than once.  */
4764
0
      for (loaded_lib = htab->dyn_loaded;
4765
0
     loaded_lib != NULL;
4766
0
     loaded_lib = loaded_lib->next)
4767
0
  {
4768
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4769
0
      return true;
4770
0
  }
4771
4772
      /* Create dynamic sections for backends that require that be done
4773
   before setup_gnu_properties.  */
4774
0
      if (add_needed
4775
0
    && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4776
0
  return false;
4777
4778
      /* Save the DT_AUDIT entry for the linker emulation code. */
4779
0
      elf_dt_audit (abfd) = audit;
4780
0
    }
4781
4782
  /* If this is a dynamic object, we always link against the .dynsym
4783
     symbol table, not the .symtab symbol table.  The dynamic linker
4784
     will only see the .dynsym symbol table, so there is no reason to
4785
     look at .symtab for a dynamic object.  */
4786
4787
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4788
0
    hdr = &elf_tdata (abfd)->symtab_hdr;
4789
0
  else
4790
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4791
4792
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4793
4794
  /* The sh_info field of the symtab header tells us where the
4795
     external symbols start.  We don't care about the local symbols at
4796
     this point.  */
4797
0
  if (elf_bad_symtab (abfd))
4798
0
    {
4799
0
      extsymcount = symcount;
4800
0
      extsymoff = 0;
4801
0
    }
4802
0
  else
4803
0
    {
4804
0
      extsymcount = symcount - hdr->sh_info;
4805
0
      extsymoff = hdr->sh_info;
4806
0
    }
4807
4808
0
  sym_hash = elf_sym_hashes (abfd);
4809
0
  if (extsymcount != 0)
4810
0
    {
4811
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4812
0
              NULL, NULL, NULL);
4813
0
      if (isymbuf == NULL)
4814
0
  goto error_return;
4815
4816
0
      if (sym_hash == NULL)
4817
0
  {
4818
    /* We store a pointer to the hash table entry for each
4819
       external symbol.  */
4820
0
    size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4821
0
    sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4822
0
    if (sym_hash == NULL)
4823
0
      goto error_free_sym;
4824
0
    elf_sym_hashes (abfd) = sym_hash;
4825
0
  }
4826
0
    }
4827
4828
0
  if (dynamic)
4829
0
    {
4830
      /* Read in any version definitions.  */
4831
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4832
0
            info->default_imported_symver))
4833
0
  goto error_free_sym;
4834
4835
      /* Read in the symbol versions, but don't bother to convert them
4836
   to internal format.  */
4837
0
      if (elf_dynversym (abfd) != 0)
4838
0
  {
4839
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4840
0
    bfd_size_type amt = versymhdr->sh_size;
4841
4842
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4843
0
      goto error_free_sym;
4844
0
    extversym = (Elf_External_Versym *)
4845
0
      _bfd_malloc_and_read (abfd, amt, amt);
4846
0
    if (extversym == NULL)
4847
0
      goto error_free_sym;
4848
0
    extversym_end = extversym + amt / sizeof (*extversym);
4849
0
  }
4850
0
    }
4851
4852
  /* If we are loading an as-needed shared lib, save the symbol table
4853
     state before we start adding symbols.  If the lib turns out
4854
     to be unneeded, restore the state.  */
4855
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4856
0
    {
4857
0
      unsigned int i;
4858
0
      size_t entsize;
4859
4860
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4861
0
  {
4862
0
    struct bfd_hash_entry *p;
4863
0
    struct elf_link_hash_entry *h;
4864
4865
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4866
0
      {
4867
0
        h = (struct elf_link_hash_entry *) p;
4868
0
        entsize += htab->root.table.entsize;
4869
0
        if (h->root.type == bfd_link_hash_warning)
4870
0
    {
4871
0
      entsize += htab->root.table.entsize;
4872
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4873
0
    }
4874
0
        if (h->root.type == bfd_link_hash_common)
4875
0
    entsize += sizeof (*h->root.u.c.p);
4876
0
      }
4877
0
  }
4878
4879
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4880
0
      old_tab = bfd_malloc (tabsize + entsize);
4881
0
      if (old_tab == NULL)
4882
0
  goto error_free_vers;
4883
4884
      /* Remember the current objalloc pointer, so that all mem for
4885
   symbols added can later be reclaimed.  */
4886
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4887
0
      if (alloc_mark == NULL)
4888
0
  goto error_free_vers;
4889
4890
      /* Make a special call to the linker "notice" function to
4891
   tell it that we are about to handle an as-needed lib.  */
4892
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4893
0
  goto error_free_vers;
4894
4895
      /* Clone the symbol table.  Remember some pointers into the
4896
   symbol table, and dynamic symbol count.  */
4897
0
      old_ent = (char *) old_tab + tabsize;
4898
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4899
0
      old_undefs = htab->root.undefs;
4900
0
      old_undefs_tail = htab->root.undefs_tail;
4901
0
      old_table = htab->root.table.table;
4902
0
      old_size = htab->root.table.size;
4903
0
      old_count = htab->root.table.count;
4904
0
      old_strtab = NULL;
4905
0
      if (htab->dynstr != NULL)
4906
0
  {
4907
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4908
0
    if (old_strtab == NULL)
4909
0
      goto error_free_vers;
4910
0
  }
4911
4912
0
      for (i = 0; i < htab->root.table.size; i++)
4913
0
  {
4914
0
    struct bfd_hash_entry *p;
4915
0
    struct elf_link_hash_entry *h;
4916
4917
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4918
0
      {
4919
0
        h = (struct elf_link_hash_entry *) p;
4920
0
        memcpy (old_ent, h, htab->root.table.entsize);
4921
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4922
0
        if (h->root.type == bfd_link_hash_warning)
4923
0
    {
4924
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4925
0
      memcpy (old_ent, h, htab->root.table.entsize);
4926
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4927
0
    }
4928
0
        if (h->root.type == bfd_link_hash_common)
4929
0
    {
4930
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4931
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4932
0
    }
4933
0
      }
4934
0
  }
4935
0
    }
4936
4937
0
  weaks = NULL;
4938
0
  if (extversym == NULL)
4939
0
    ever = NULL;
4940
0
  else if (extversym + extsymoff < extversym_end)
4941
0
    ever = extversym + extsymoff;
4942
0
  else
4943
0
    {
4944
      /* xgettext:c-format */
4945
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4946
0
        abfd, (long) extsymoff,
4947
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4948
0
      bfd_set_error (bfd_error_bad_value);
4949
0
      goto error_free_vers;
4950
0
    }
4951
4952
0
  if (!bfd_link_relocatable (info)
4953
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4954
0
    {
4955
0
      _bfd_error_handler
4956
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4957
0
    }
4958
4959
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4960
0
       isym < isymend;
4961
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4962
0
    {
4963
0
      int bind;
4964
0
      bfd_vma value;
4965
0
      asection *sec, *new_sec;
4966
0
      flagword flags;
4967
0
      const char *name;
4968
0
      const char *defvername;
4969
0
      bool must_copy_name = false;
4970
0
      struct elf_link_hash_entry *h;
4971
0
      struct elf_link_hash_entry *hi;
4972
0
      bool definition;
4973
0
      bool size_change_ok;
4974
0
      bool type_change_ok;
4975
0
      bool new_weak;
4976
0
      bool old_weak;
4977
0
      bfd *override;
4978
0
      bool common;
4979
0
      bool discarded;
4980
0
      unsigned int old_alignment;
4981
0
      unsigned int shindex;
4982
0
      bfd *old_bfd;
4983
0
      bool matched;
4984
4985
0
      override = NULL;
4986
4987
0
      flags = BSF_NO_FLAGS;
4988
0
      sec = NULL;
4989
0
      value = isym->st_value;
4990
0
      common = bed->common_definition (isym);
4991
0
      if (common && info->inhibit_common_definition)
4992
0
  {
4993
    /* Treat common symbol as undefined for --no-define-common.  */
4994
0
    isym->st_shndx = SHN_UNDEF;
4995
0
    common = false;
4996
0
  }
4997
0
      discarded = false;
4998
4999
0
      bind = ELF_ST_BIND (isym->st_info);
5000
0
      switch (bind)
5001
0
  {
5002
0
  case STB_LOCAL:
5003
    /* This should be impossible, since ELF requires that all
5004
       global symbols follow all local symbols, and that sh_info
5005
       point to the first global symbol.  Unfortunately, Irix 5
5006
       screws this up.  */
5007
0
    if (elf_bad_symtab (abfd))
5008
0
      continue;
5009
5010
    /* If we aren't prepared to handle locals within the globals
5011
       then we'll likely segfault on a NULL symbol hash if the
5012
       symbol is ever referenced in relocations.  */
5013
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5014
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5015
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5016
0
        " (>= sh_info of %lu)"),
5017
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5018
0
            (long) extsymoff);
5019
5020
    /* Dynamic object relocations are not processed by ld, so
5021
       ld won't run into the problem mentioned above.  */
5022
0
    if (dynamic)
5023
0
      continue;
5024
0
    bfd_set_error (bfd_error_bad_value);
5025
0
    goto error_free_vers;
5026
5027
0
  case STB_GLOBAL:
5028
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5029
0
      flags = BSF_GLOBAL;
5030
0
    break;
5031
5032
0
  case STB_WEAK:
5033
0
    flags = BSF_WEAK;
5034
0
    break;
5035
5036
0
  case STB_GNU_UNIQUE:
5037
0
    flags = BSF_GNU_UNIQUE;
5038
0
    break;
5039
5040
0
  default:
5041
    /* Leave it up to the processor backend.  */
5042
0
    break;
5043
0
  }
5044
5045
0
      if (isym->st_shndx == SHN_UNDEF)
5046
0
  sec = bfd_und_section_ptr;
5047
0
      else if (isym->st_shndx == SHN_ABS)
5048
0
  sec = bfd_abs_section_ptr;
5049
0
      else if (isym->st_shndx == SHN_COMMON)
5050
0
  {
5051
0
    sec = bfd_com_section_ptr;
5052
    /* What ELF calls the size we call the value.  What ELF
5053
       calls the value we call the alignment.  */
5054
0
    value = isym->st_size;
5055
0
  }
5056
0
      else
5057
0
  {
5058
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5059
0
    if (sec == NULL)
5060
0
      sec = bfd_abs_section_ptr;
5061
0
    else if (discarded_section (sec))
5062
0
      {
5063
        /* Symbols from discarded section are undefined.  We keep
5064
     its visibility.  */
5065
0
        sec = bfd_und_section_ptr;
5066
0
        discarded = true;
5067
0
        isym->st_shndx = SHN_UNDEF;
5068
0
      }
5069
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5070
0
      value -= sec->vma;
5071
0
  }
5072
5073
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5074
0
                isym->st_name);
5075
0
      if (name == NULL)
5076
0
  goto error_free_vers;
5077
5078
0
      if (isym->st_shndx == SHN_COMMON
5079
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5080
0
  {
5081
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5082
5083
0
    if (xc == NULL)
5084
0
      {
5085
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5086
0
         | SEC_EXCLUDE);
5087
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5088
0
        if (xc == NULL)
5089
0
    goto error_free_vers;
5090
0
      }
5091
0
    sec = xc;
5092
0
  }
5093
0
      else if (isym->st_shndx == SHN_COMMON
5094
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5095
0
         && !bfd_link_relocatable (info))
5096
0
  {
5097
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5098
5099
0
    if (tcomm == NULL)
5100
0
      {
5101
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5102
0
         | SEC_LINKER_CREATED);
5103
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5104
0
        if (tcomm == NULL)
5105
0
    goto error_free_vers;
5106
0
      }
5107
0
    sec = tcomm;
5108
0
  }
5109
0
      else if (bed->elf_add_symbol_hook)
5110
0
  {
5111
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5112
0
               &sec, &value))
5113
0
      goto error_free_vers;
5114
5115
    /* The hook function sets the name to NULL if this symbol
5116
       should be skipped for some reason.  */
5117
0
    if (name == NULL)
5118
0
      continue;
5119
0
  }
5120
5121
      /* Sanity check that all possibilities were handled.  */
5122
0
      if (sec == NULL)
5123
0
  abort ();
5124
5125
      /* Silently discard TLS symbols from --just-syms.  There's
5126
   no way to combine a static TLS block with a new TLS block
5127
   for this executable.  */
5128
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5129
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5130
0
  continue;
5131
5132
0
      if (bfd_is_und_section (sec)
5133
0
    || bfd_is_com_section (sec))
5134
0
  definition = false;
5135
0
      else
5136
0
  definition = true;
5137
5138
0
      size_change_ok = false;
5139
0
      type_change_ok = bed->type_change_ok;
5140
0
      old_weak = false;
5141
0
      matched = false;
5142
0
      old_alignment = 0;
5143
0
      old_bfd = NULL;
5144
0
      new_sec = sec;
5145
0
      defvername = NULL;
5146
5147
0
      if (is_elf_hash_table (&htab->root))
5148
0
  {
5149
0
    Elf_Internal_Versym iver;
5150
0
    unsigned int vernum = 0;
5151
0
    bool skip;
5152
5153
0
    if (ever == NULL)
5154
0
      {
5155
0
        if (info->default_imported_symver)
5156
    /* Use the default symbol version created earlier.  */
5157
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5158
0
        else
5159
0
    iver.vs_vers = 0;
5160
0
      }
5161
0
    else if (ever >= extversym_end)
5162
0
      {
5163
        /* xgettext:c-format */
5164
0
        _bfd_error_handler (_("%pB: not enough version information"),
5165
0
          abfd);
5166
0
        bfd_set_error (bfd_error_bad_value);
5167
0
        goto error_free_vers;
5168
0
      }
5169
0
    else
5170
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5171
5172
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5173
5174
    /* If this is a hidden symbol, or if it is not version
5175
       1, we append the version name to the symbol name.
5176
       However, we do not modify a non-hidden absolute symbol
5177
       if it is not a function, because it might be the version
5178
       symbol itself.  FIXME: What if it isn't?  */
5179
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5180
0
        || (vernum > 1
5181
0
      && (!bfd_is_abs_section (sec)
5182
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5183
0
      {
5184
0
        const char *verstr;
5185
0
        size_t namelen, verlen, newlen;
5186
0
        char *newname, *p;
5187
5188
0
        if (isym->st_shndx != SHN_UNDEF)
5189
0
    {
5190
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5191
0
        verstr = NULL;
5192
0
      else if (vernum > 1)
5193
0
        verstr =
5194
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5195
0
      else
5196
0
        verstr = "";
5197
5198
0
      if (verstr == NULL)
5199
0
        {
5200
0
          _bfd_error_handler
5201
      /* xgettext:c-format */
5202
0
      (_("%pB: %s: invalid version %u (max %d)"),
5203
0
       abfd, name, vernum,
5204
0
       elf_tdata (abfd)->cverdefs);
5205
0
          bfd_set_error (bfd_error_bad_value);
5206
0
          goto error_free_vers;
5207
0
        }
5208
0
    }
5209
0
        else
5210
0
    {
5211
      /* We cannot simply test for the number of
5212
         entries in the VERNEED section since the
5213
         numbers for the needed versions do not start
5214
         at 0.  */
5215
0
      Elf_Internal_Verneed *t;
5216
5217
0
      verstr = NULL;
5218
0
      for (t = elf_tdata (abfd)->verref;
5219
0
           t != NULL;
5220
0
           t = t->vn_nextref)
5221
0
        {
5222
0
          Elf_Internal_Vernaux *a;
5223
5224
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5225
0
      {
5226
0
        if (a->vna_other == vernum)
5227
0
          {
5228
0
            verstr = a->vna_nodename;
5229
0
            break;
5230
0
          }
5231
0
      }
5232
0
          if (a != NULL)
5233
0
      break;
5234
0
        }
5235
0
      if (verstr == NULL)
5236
0
        {
5237
0
          _bfd_error_handler
5238
      /* xgettext:c-format */
5239
0
      (_("%pB: %s: invalid needed version %d"),
5240
0
       abfd, name, vernum);
5241
0
          bfd_set_error (bfd_error_bad_value);
5242
0
          goto error_free_vers;
5243
0
        }
5244
0
    }
5245
5246
0
        namelen = strlen (name);
5247
0
        verlen = strlen (verstr);
5248
0
        newlen = namelen + verlen + 2;
5249
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5250
0
      && isym->st_shndx != SHN_UNDEF)
5251
0
    ++newlen;
5252
5253
0
        newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5254
0
        if (newname == NULL)
5255
0
    goto error_free_vers;
5256
0
        memcpy (newname, name, namelen);
5257
0
        p = newname + namelen;
5258
0
        *p++ = ELF_VER_CHR;
5259
        /* If this is a defined non-hidden version symbol,
5260
     we add another @ to the name.  This indicates the
5261
     default version of the symbol.  */
5262
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5263
0
      && isym->st_shndx != SHN_UNDEF)
5264
0
    *p++ = ELF_VER_CHR, defvername = name;
5265
0
        memcpy (p, verstr, verlen + 1);
5266
5267
0
        name = newname;
5268
        /* Since bfd_hash_alloc is used for "name", the string
5269
     must be copied if added to first_hash.  The string
5270
     memory can be freed when an --as-needed library is
5271
     not needed.  */
5272
0
        must_copy_name = true;
5273
0
      }
5274
5275
    /* If this symbol has default visibility and the user has
5276
       requested we not re-export it, then mark it as hidden.  */
5277
0
    if (!bfd_is_und_section (sec)
5278
0
        && !dynamic
5279
0
        && abfd->no_export
5280
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5281
0
      isym->st_other = (STV_HIDDEN
5282
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5283
5284
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5285
0
              sym_hash, &old_bfd, &old_weak,
5286
0
              &old_alignment, &skip, &override,
5287
0
              &type_change_ok, &size_change_ok,
5288
0
              &matched))
5289
0
      goto error_free_vers;
5290
5291
0
    if (skip)
5292
0
      continue;
5293
5294
0
    h = *sym_hash;
5295
0
    while (h->root.type == bfd_link_hash_indirect
5296
0
     || h->root.type == bfd_link_hash_warning)
5297
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5298
5299
    /* Override a definition only if the new symbol matches the
5300
       existing one.  */
5301
0
    if (override && matched)
5302
0
      {
5303
0
        definition = false;
5304
0
        if (htab->first_hash != NULL
5305
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5306
0
      && h->root.non_ir_ref_regular)
5307
0
    {
5308
      /* When reloading --as-needed shared objects for new
5309
         symbols added from IR inputs, if this shared object
5310
         has the first definition, use it.  */
5311
0
      struct elf_link_first_hash_entry *e
5312
0
        = ((struct elf_link_first_hash_entry *)
5313
0
           bfd_hash_lookup (htab->first_hash, name, false,
5314
0
          false));
5315
0
      if (e != NULL && e->abfd == abfd)
5316
0
        definition = true;
5317
0
    }
5318
0
      }
5319
5320
0
    if (h->versioned != unversioned
5321
0
        && elf_tdata (abfd)->verdef != NULL
5322
0
        && vernum > 1
5323
0
        && definition)
5324
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5325
0
  }
5326
5327
0
      if (! (_bfd_generic_link_add_one_symbol
5328
0
       (info, override ? override : abfd, name, flags, sec, value,
5329
0
        NULL, false, bed->collect,
5330
0
        (struct bfd_link_hash_entry **) sym_hash)))
5331
0
  goto error_free_vers;
5332
5333
0
      h = *sym_hash;
5334
      /* We need to make sure that indirect symbol dynamic flags are
5335
   updated.  */
5336
0
      hi = h;
5337
0
      while (h->root.type == bfd_link_hash_indirect
5338
0
       || h->root.type == bfd_link_hash_warning)
5339
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5340
5341
0
      *sym_hash = h;
5342
5343
      /* Setting the index to -3 tells elf_link_output_extsym that
5344
   this symbol is defined in a discarded section.  */
5345
0
      if (discarded && is_elf_hash_table (&htab->root))
5346
0
  h->indx = -3;
5347
5348
0
      new_weak = (flags & BSF_WEAK) != 0;
5349
0
      if (dynamic
5350
0
    && definition
5351
0
    && new_weak
5352
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5353
0
    && is_elf_hash_table (&htab->root)
5354
0
    && h->u.alias == NULL)
5355
0
  {
5356
    /* Keep a list of all weak defined non function symbols from
5357
       a dynamic object, using the alias field.  Later in this
5358
       function we will set the alias field to the correct
5359
       value.  We only put non-function symbols from dynamic
5360
       objects on this list, because that happens to be the only
5361
       time we need to know the normal symbol corresponding to a
5362
       weak symbol, and the information is time consuming to
5363
       figure out.  If the alias field is not already NULL,
5364
       then this symbol was already defined by some previous
5365
       dynamic object, and we will be using that previous
5366
       definition anyhow.  */
5367
5368
0
    h->u.alias = weaks;
5369
0
    weaks = h;
5370
0
  }
5371
5372
      /* Set the alignment of a common symbol.  */
5373
0
      if ((common || bfd_is_com_section (sec))
5374
0
    && h->root.type == bfd_link_hash_common)
5375
0
  {
5376
0
    unsigned int align;
5377
5378
0
    if (common)
5379
0
      align = bfd_log2 (isym->st_value);
5380
0
    else
5381
0
      {
5382
        /* The new symbol is a common symbol in a shared object.
5383
     We need to get the alignment from the section.  */
5384
0
        align = new_sec->alignment_power;
5385
0
      }
5386
0
    if (align > old_alignment)
5387
0
      h->root.u.c.p->alignment_power = align;
5388
0
    else
5389
0
      h->root.u.c.p->alignment_power = old_alignment;
5390
0
  }
5391
5392
0
      if (is_elf_hash_table (&htab->root))
5393
0
  {
5394
    /* Set a flag in the hash table entry indicating the type of
5395
       reference or definition we just found.  A dynamic symbol
5396
       is one which is referenced or defined by both a regular
5397
       object and a shared object.  */
5398
0
    bool dynsym = false;
5399
5400
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5401
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5402
0
      {
5403
        /* Except for this flag to track nonweak references.  */
5404
0
        if (!definition
5405
0
      && bind != STB_WEAK)
5406
0
    h->ref_ir_nonweak = 1;
5407
0
      }
5408
0
    else if (!dynamic)
5409
0
      {
5410
0
        if (! definition)
5411
0
    {
5412
0
      h->ref_regular = 1;
5413
0
      if (bind != STB_WEAK)
5414
0
        h->ref_regular_nonweak = 1;
5415
0
    }
5416
0
        else
5417
0
    {
5418
0
      h->def_regular = 1;
5419
0
      if (h->def_dynamic)
5420
0
        {
5421
0
          h->def_dynamic = 0;
5422
0
          h->ref_dynamic = 1;
5423
0
        }
5424
0
    }
5425
0
      }
5426
0
    else
5427
0
      {
5428
0
        if (! definition)
5429
0
    {
5430
0
      h->ref_dynamic = 1;
5431
0
      hi->ref_dynamic = 1;
5432
0
    }
5433
0
        else
5434
0
    {
5435
0
      h->def_dynamic = 1;
5436
0
      hi->def_dynamic = 1;
5437
0
    }
5438
0
      }
5439
5440
    /* If an indirect symbol has been forced local, don't
5441
       make the real symbol dynamic.  */
5442
0
    if (h != hi && hi->forced_local)
5443
0
      ;
5444
0
    else if (!dynamic)
5445
0
      {
5446
0
        if (bfd_link_dll (info)
5447
0
      || h->def_dynamic
5448
0
      || h->ref_dynamic)
5449
0
    dynsym = true;
5450
0
      }
5451
0
    else
5452
0
      {
5453
0
        if (h->def_regular
5454
0
      || h->ref_regular
5455
0
      || (h->is_weakalias
5456
0
          && weakdef (h)->dynindx != -1))
5457
0
    dynsym = true;
5458
0
      }
5459
5460
    /* Check to see if we need to add an indirect symbol for
5461
       the default name.  */
5462
0
    if ((definition
5463
0
         || (!override && h->root.type == bfd_link_hash_common))
5464
0
        && !(hi != h
5465
0
       && hi->versioned == versioned_hidden))
5466
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5467
0
                sec, value, &old_bfd, &dynsym))
5468
0
        goto error_free_vers;
5469
5470
    /* Check the alignment when a common symbol is involved. This
5471
       can change when a common symbol is overridden by a normal
5472
       definition or a common symbol is ignored due to the old
5473
       normal definition. We need to make sure the maximum
5474
       alignment is maintained.  */
5475
0
    if ((old_alignment || common)
5476
0
        && h->root.type != bfd_link_hash_common)
5477
0
      {
5478
0
        unsigned int common_align;
5479
0
        unsigned int normal_align;
5480
0
        unsigned int symbol_align;
5481
0
        bfd *normal_bfd;
5482
0
        bfd *common_bfd;
5483
5484
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5485
0
        || h->root.type == bfd_link_hash_defweak);
5486
5487
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5488
0
        if (h->root.u.def.section->owner != NULL
5489
0
      && (h->root.u.def.section->owner->flags
5490
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5491
0
    {
5492
0
      normal_align = h->root.u.def.section->alignment_power;
5493
0
      if (normal_align > symbol_align)
5494
0
        normal_align = symbol_align;
5495
0
    }
5496
0
        else
5497
0
    normal_align = symbol_align;
5498
5499
0
        if (old_alignment)
5500
0
    {
5501
0
      common_align = old_alignment;
5502
0
      common_bfd = old_bfd;
5503
0
      normal_bfd = abfd;
5504
0
    }
5505
0
        else
5506
0
    {
5507
0
      common_align = bfd_log2 (isym->st_value);
5508
0
      common_bfd = abfd;
5509
0
      normal_bfd = old_bfd;
5510
0
    }
5511
5512
0
        if (normal_align < common_align)
5513
0
    {
5514
      /* PR binutils/2735 */
5515
0
      if (normal_bfd == NULL)
5516
0
        _bfd_error_handler
5517
          /* xgettext:c-format */
5518
0
          (_("warning: alignment %u of common symbol `%s' in %pB is"
5519
0
       " greater than the alignment (%u) of its section %pA"),
5520
0
           1 << common_align, name, common_bfd,
5521
0
           1 << normal_align, h->root.u.def.section);
5522
0
      else
5523
0
        _bfd_error_handler
5524
          /* xgettext:c-format */
5525
0
          (_("warning: alignment %u of normal symbol `%s' in %pB"
5526
0
       " is smaller than %u used by the common definition in %pB"),
5527
0
           1 << normal_align, name, normal_bfd,
5528
0
           1 << common_align, common_bfd);
5529
5530
      /* PR 30499: make sure that users understand that this warning is serious.  */
5531
0
      _bfd_error_handler
5532
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5533
0
    }
5534
0
      }
5535
5536
    /* Remember the symbol size if it isn't undefined.  */
5537
0
    if (isym->st_size != 0
5538
0
        && isym->st_shndx != SHN_UNDEF
5539
0
        && (definition || h->size == 0))
5540
0
      {
5541
0
        if (h->size != 0
5542
0
      && h->size != isym->st_size
5543
0
      && ! size_change_ok)
5544
0
    {
5545
0
      _bfd_error_handler
5546
        /* xgettext:c-format */
5547
0
        (_("warning: size of symbol `%s' changed"
5548
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5549
0
         name, (uint64_t) h->size, old_bfd,
5550
0
         (uint64_t) isym->st_size, abfd);
5551
5552
      /* PR 30499: make sure that users understand that this warning is serious.  */
5553
0
      _bfd_error_handler
5554
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5555
0
    }
5556
5557
0
        h->size = isym->st_size;
5558
0
      }
5559
5560
    /* If this is a common symbol, then we always want H->SIZE
5561
       to be the size of the common symbol.  The code just above
5562
       won't fix the size if a common symbol becomes larger.  We
5563
       don't warn about a size change here, because that is
5564
       covered by --warn-common.  Allow changes between different
5565
       function types.  */
5566
0
    if (h->root.type == bfd_link_hash_common)
5567
0
      h->size = h->root.u.c.size;
5568
5569
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5570
0
        && ((definition && !new_weak)
5571
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5572
0
      || h->type == STT_NOTYPE))
5573
0
      {
5574
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5575
5576
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5577
     symbol.  */
5578
0
        if (type == STT_GNU_IFUNC
5579
0
      && (abfd->flags & DYNAMIC) != 0)
5580
0
    type = STT_FUNC;
5581
5582
0
        if (h->type != type)
5583
0
    {
5584
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5585
        /* xgettext:c-format */
5586
0
        _bfd_error_handler
5587
0
          (_("warning: type of symbol `%s' changed"
5588
0
       " from %d to %d in %pB"),
5589
0
           name, h->type, type, abfd);
5590
5591
0
      h->type = type;
5592
0
    }
5593
0
      }
5594
5595
    /* Merge st_other field.  */
5596
0
    elf_merge_st_other (abfd, h, isym->st_other, sec,
5597
0
            definition, dynamic);
5598
5599
    /* We don't want to make debug symbol dynamic.  */
5600
0
    if (definition
5601
0
        && (sec->flags & SEC_DEBUGGING)
5602
0
        && !bfd_link_relocatable (info))
5603
0
      dynsym = false;
5604
5605
    /* Nor should we make plugin symbols dynamic.  */
5606
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5607
0
      dynsym = false;
5608
5609
0
    if (definition)
5610
0
      {
5611
0
        h->target_internal = isym->st_target_internal;
5612
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5613
0
      }
5614
5615
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5616
       in IR.  Since all data or text symbols in IR have the
5617
       same type, value and section, we can't tell if a symbol
5618
       is an alias of another symbol by their types, values and
5619
       sections.  */
5620
0
    if (definition
5621
0
        && !dynamic
5622
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5623
0
      {
5624
0
        char *p = strchr (name, ELF_VER_CHR);
5625
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5626
0
    {
5627
      /* Queue non-default versions so that .symver x, x@FOO
5628
         aliases can be checked.  */
5629
0
      if (!nondeflt_vers)
5630
0
        {
5631
0
          size_t amt = ((isymend - isym + 1)
5632
0
            * sizeof (struct elf_link_hash_entry *));
5633
0
          nondeflt_vers
5634
0
      = (struct elf_link_hash_entry **) bfd_malloc (amt);
5635
0
          if (!nondeflt_vers)
5636
0
      goto error_free_vers;
5637
0
        }
5638
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5639
0
    }
5640
0
      }
5641
5642
0
    if (dynsym && h->dynindx == -1)
5643
0
      {
5644
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5645
0
    goto error_free_vers;
5646
0
        if (h->is_weakalias
5647
0
      && weakdef (h)->dynindx == -1)
5648
0
    {
5649
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5650
0
        goto error_free_vers;
5651
0
    }
5652
0
      }
5653
0
    else if (h->dynindx != -1)
5654
      /* If the symbol already has a dynamic index, but
5655
         visibility says it should not be visible, turn it into
5656
         a local symbol.  */
5657
0
      switch (ELF_ST_VISIBILITY (h->other))
5658
0
        {
5659
0
        case STV_INTERNAL:
5660
0
        case STV_HIDDEN:
5661
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
5662
0
    dynsym = false;
5663
0
    break;
5664
0
        }
5665
5666
0
    if (!add_needed
5667
0
        && matched
5668
0
        && definition
5669
0
        && h->root.type != bfd_link_hash_indirect)
5670
0
      {
5671
0
        if ((dynsym
5672
0
       && h->ref_regular_nonweak)
5673
0
      || (old_bfd != NULL
5674
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5675
0
          && h->ref_ir_nonweak
5676
0
          && !info->lto_all_symbols_read)
5677
0
      || (h->ref_dynamic_nonweak
5678
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5679
0
          && !on_needed_list (elf_dt_name (abfd),
5680
0
            htab->needed, NULL)))
5681
0
    {
5682
0
      const char *soname = elf_dt_name (abfd);
5683
5684
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5685
0
            h->root.root.string);
5686
5687
      /* A symbol from a library loaded via DT_NEEDED of some
5688
         other library is referenced by a regular object.
5689
         Add a DT_NEEDED entry for it.  Issue an error if
5690
         --no-add-needed is used and the reference was not
5691
         a weak one.  */
5692
0
      if (old_bfd != NULL
5693
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5694
0
        {
5695
0
          _bfd_error_handler
5696
      /* xgettext:c-format */
5697
0
      (_("%pB: undefined reference to symbol '%s'"),
5698
0
       old_bfd, name);
5699
0
          bfd_set_error (bfd_error_missing_dso);
5700
0
          goto error_free_vers;
5701
0
        }
5702
5703
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5704
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5705
5706
      /* Create dynamic sections for backends that require
5707
         that be done before setup_gnu_properties.  */
5708
0
      if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5709
0
        goto error_free_vers;
5710
0
      add_needed = true;
5711
0
    }
5712
0
        else if (dynamic
5713
0
           && h->root.u.def.section->owner == abfd)
5714
0
    {
5715
      /* Add this symbol to first hash if this shared
5716
         object has the first definition.  */
5717
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5718
      /* And if it was the default symbol version definition,
5719
         also add the short name.  */
5720
0
      if (defvername)
5721
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5722
0
    }
5723
0
      }
5724
0
  }
5725
0
    }
5726
5727
0
  if (info->lto_plugin_active
5728
0
      && !bfd_link_relocatable (info)
5729
0
      && (abfd->flags & BFD_PLUGIN) == 0
5730
0
      && !just_syms
5731
0
      && extsymcount != 0
5732
0
      && is_elf_hash_table (&htab->root))
5733
0
    {
5734
0
      int r_sym_shift;
5735
5736
0
      if (bed->s->arch_size == 32)
5737
0
  r_sym_shift = 8;
5738
0
      else
5739
0
  r_sym_shift = 32;
5740
5741
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5742
   referenced in regular objects so that linker plugin will get
5743
   the correct symbol resolution.  */
5744
5745
0
      sym_hash = elf_sym_hashes (abfd);
5746
0
      for (s = abfd->sections; s != NULL; s = s->next)
5747
0
  {
5748
0
    Elf_Internal_Rela *internal_relocs;
5749
0
    Elf_Internal_Rela *rel, *relend;
5750
5751
    /* Don't check relocations in excluded sections.  */
5752
0
    if ((s->flags & SEC_RELOC) == 0
5753
0
        || s->reloc_count == 0
5754
0
        || (s->flags & SEC_EXCLUDE) != 0
5755
0
        || (s->flags & SEC_DEBUGGING) != 0)
5756
0
      continue;
5757
5758
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5759
0
      (abfd, info, s, NULL, NULL,
5760
0
       _bfd_elf_link_keep_memory (info));
5761
0
    if (internal_relocs == NULL)
5762
0
      goto error_free_vers;
5763
5764
0
    rel = internal_relocs;
5765
0
    relend = rel + s->reloc_count;
5766
0
    for ( ; rel < relend; rel++)
5767
0
      {
5768
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5769
0
        struct elf_link_hash_entry *h;
5770
5771
        /* Skip local symbols.  */
5772
0
        if (r_symndx < extsymoff)
5773
0
    continue;
5774
5775
0
        h = sym_hash[r_symndx - extsymoff];
5776
0
        if (h != NULL)
5777
0
    h->root.non_ir_ref_regular = 1;
5778
0
      }
5779
5780
0
    if (elf_section_data (s)->relocs != internal_relocs)
5781
0
      free (internal_relocs);
5782
0
  }
5783
0
    }
5784
5785
0
  free (extversym);
5786
0
  extversym = NULL;
5787
0
  free (isymbuf);
5788
0
  isymbuf = NULL;
5789
5790
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5791
0
    {
5792
0
      unsigned int i;
5793
5794
      /* Restore the symbol table.  */
5795
0
      old_ent = (char *) old_tab + tabsize;
5796
0
      memset (elf_sym_hashes (abfd), 0,
5797
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5798
0
      htab->root.table.table = old_table;
5799
0
      htab->root.table.size = old_size;
5800
0
      htab->root.table.count = old_count;
5801
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5802
0
      htab->root.undefs = old_undefs;
5803
0
      htab->root.undefs_tail = old_undefs_tail;
5804
0
      if (htab->dynstr != NULL)
5805
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5806
0
      free (old_strtab);
5807
0
      old_strtab = NULL;
5808
0
      for (i = 0; i < htab->root.table.size; i++)
5809
0
  {
5810
0
    struct bfd_hash_entry *p;
5811
0
    struct elf_link_hash_entry *h;
5812
0
    unsigned int non_ir_ref_dynamic;
5813
5814
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5815
0
      {
5816
        /* Preserve non_ir_ref_dynamic so that this symbol
5817
     will be exported when the dynamic lib becomes needed
5818
     in the second pass.  */
5819
0
        h = (struct elf_link_hash_entry *) p;
5820
0
        if (h->root.type == bfd_link_hash_warning)
5821
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5822
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5823
5824
0
        h = (struct elf_link_hash_entry *) p;
5825
0
        memcpy (h, old_ent, htab->root.table.entsize);
5826
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5827
0
        if (h->root.type == bfd_link_hash_warning)
5828
0
    {
5829
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5830
0
      memcpy (h, old_ent, htab->root.table.entsize);
5831
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5832
0
    }
5833
0
        if (h->root.type == bfd_link_hash_common)
5834
0
    {
5835
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5836
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5837
0
    }
5838
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5839
0
      }
5840
0
  }
5841
5842
      /* Make a special call to the linker "notice" function to
5843
   tell it that symbols added for crefs may need to be removed.  */
5844
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5845
0
  goto error_free_vers;
5846
5847
0
      free (old_tab);
5848
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5849
0
         alloc_mark);
5850
0
      free (nondeflt_vers);
5851
0
      return true;
5852
0
    }
5853
5854
0
  free (old_strtab);
5855
0
  old_strtab = NULL;
5856
0
  if (old_tab != NULL)
5857
0
    {
5858
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5859
0
  goto error_free_vers;
5860
0
      free (old_tab);
5861
0
      old_tab = NULL;
5862
0
    }
5863
5864
  /* Now that all the symbols from this input file are created, if
5865
     not performing a relocatable link, handle .symver foo, foo@BAR
5866
     such that any relocs against foo become foo@BAR.  */
5867
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5868
0
    {
5869
0
      size_t cnt, symidx;
5870
5871
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5872
0
  {
5873
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5874
0
    char *shortname, *p;
5875
0
    size_t amt;
5876
5877
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5878
0
    if (p == NULL
5879
0
        || (h->root.type != bfd_link_hash_defined
5880
0
      && h->root.type != bfd_link_hash_defweak))
5881
0
      continue;
5882
5883
0
    amt = p - h->root.root.string;
5884
0
    shortname = (char *) bfd_malloc (amt + 1);
5885
0
    if (!shortname)
5886
0
      goto error_free_vers;
5887
0
    memcpy (shortname, h->root.root.string, amt);
5888
0
    shortname[amt] = '\0';
5889
5890
0
    hi = (struct elf_link_hash_entry *)
5891
0
         bfd_link_hash_lookup (&htab->root, shortname,
5892
0
             false, false, false);
5893
0
    if (hi != NULL
5894
0
        && hi->root.type == h->root.type
5895
0
        && hi->root.u.def.value == h->root.u.def.value
5896
0
        && hi->root.u.def.section == h->root.u.def.section)
5897
0
      {
5898
0
        (*bed->elf_backend_hide_symbol) (info, hi, true);
5899
0
        hi->root.type = bfd_link_hash_indirect;
5900
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5901
0
        (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5902
0
        sym_hash = elf_sym_hashes (abfd);
5903
0
        if (sym_hash)
5904
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5905
0
      if (sym_hash[symidx] == hi)
5906
0
        {
5907
0
          sym_hash[symidx] = h;
5908
0
          break;
5909
0
        }
5910
0
      }
5911
0
    free (shortname);
5912
0
  }
5913
0
    }
5914
0
  free (nondeflt_vers);
5915
0
  nondeflt_vers = NULL;
5916
5917
  /* Now set the alias field correctly for all the weak defined
5918
     symbols we found.  The only way to do this is to search all the
5919
     symbols.  Since we only need the information for non functions in
5920
     dynamic objects, that's the only time we actually put anything on
5921
     the list WEAKS.  We need this information so that if a regular
5922
     object refers to a symbol defined weakly in a dynamic object, the
5923
     real symbol in the dynamic object is also put in the dynamic
5924
     symbols; we also must arrange for both symbols to point to the
5925
     same memory location.  We could handle the general case of symbol
5926
     aliasing, but a general symbol alias can only be generated in
5927
     assembler code, handling it correctly would be very time
5928
     consuming, and other ELF linkers don't handle general aliasing
5929
     either.  */
5930
0
  if (weaks != NULL)
5931
0
    {
5932
0
      struct elf_link_hash_entry **hpp;
5933
0
      struct elf_link_hash_entry **hppend;
5934
0
      struct elf_link_hash_entry **sorted_sym_hash;
5935
0
      struct elf_link_hash_entry *h;
5936
0
      size_t sym_count, amt;
5937
5938
      /* Since we have to search the whole symbol list for each weak
5939
   defined symbol, search time for N weak defined symbols will be
5940
   O(N^2). Binary search will cut it down to O(NlogN).  */
5941
0
      amt = extsymcount * sizeof (*sorted_sym_hash);
5942
0
      sorted_sym_hash = bfd_malloc (amt);
5943
0
      if (sorted_sym_hash == NULL)
5944
0
  goto error_return;
5945
0
      sym_hash = sorted_sym_hash;
5946
0
      hpp = elf_sym_hashes (abfd);
5947
0
      hppend = hpp + extsymcount;
5948
0
      sym_count = 0;
5949
0
      for (; hpp < hppend; hpp++)
5950
0
  {
5951
0
    h = *hpp;
5952
0
    if (h != NULL
5953
0
        && h->root.type == bfd_link_hash_defined
5954
0
        && !bed->is_function_type (h->type))
5955
0
      {
5956
0
        *sym_hash = h;
5957
0
        sym_hash++;
5958
0
        sym_count++;
5959
0
      }
5960
0
  }
5961
5962
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5963
0
       elf_sort_symbol);
5964
5965
0
      while (weaks != NULL)
5966
0
  {
5967
0
    struct elf_link_hash_entry *hlook;
5968
0
    asection *slook;
5969
0
    bfd_vma vlook;
5970
0
    size_t i, j, idx = 0;
5971
5972
0
    hlook = weaks;
5973
0
    weaks = hlook->u.alias;
5974
0
    hlook->u.alias = NULL;
5975
5976
0
    if (hlook->root.type != bfd_link_hash_defined
5977
0
        && hlook->root.type != bfd_link_hash_defweak)
5978
0
      continue;
5979
5980
0
    slook = hlook->root.u.def.section;
5981
0
    vlook = hlook->root.u.def.value;
5982
5983
0
    i = 0;
5984
0
    j = sym_count;
5985
0
    while (i != j)
5986
0
      {
5987
0
        bfd_signed_vma vdiff;
5988
0
        idx = (i + j) / 2;
5989
0
        h = sorted_sym_hash[idx];
5990
0
        vdiff = vlook - h->root.u.def.value;
5991
0
        if (vdiff < 0)
5992
0
    j = idx;
5993
0
        else if (vdiff > 0)
5994
0
    i = idx + 1;
5995
0
        else
5996
0
    {
5997
0
      int sdiff = slook->id - h->root.u.def.section->id;
5998
0
      if (sdiff < 0)
5999
0
        j = idx;
6000
0
      else if (sdiff > 0)
6001
0
        i = idx + 1;
6002
0
      else
6003
0
        break;
6004
0
    }
6005
0
      }
6006
6007
    /* We didn't find a value/section match.  */
6008
0
    if (i == j)
6009
0
      continue;
6010
6011
    /* With multiple aliases, or when the weak symbol is already
6012
       strongly defined, we have multiple matching symbols and
6013
       the binary search above may land on any of them.  Step
6014
       one past the matching symbol(s).  */
6015
0
    while (++idx != j)
6016
0
      {
6017
0
        h = sorted_sym_hash[idx];
6018
0
        if (h->root.u.def.section != slook
6019
0
      || h->root.u.def.value != vlook)
6020
0
    break;
6021
0
      }
6022
6023
    /* Now look back over the aliases.  Since we sorted by size
6024
       as well as value and section, we'll choose the one with
6025
       the largest size.  */
6026
0
    while (idx-- != i)
6027
0
      {
6028
0
        h = sorted_sym_hash[idx];
6029
6030
        /* Stop if value or section doesn't match.  */
6031
0
        if (h->root.u.def.section != slook
6032
0
      || h->root.u.def.value != vlook)
6033
0
    break;
6034
0
        else if (h != hlook)
6035
0
    {
6036
0
      struct elf_link_hash_entry *t;
6037
6038
0
      hlook->u.alias = h;
6039
0
      hlook->is_weakalias = 1;
6040
0
      t = h;
6041
0
      if (t->u.alias != NULL)
6042
0
        while (t->u.alias != h)
6043
0
          t = t->u.alias;
6044
0
      t->u.alias = hlook;
6045
6046
      /* If the weak definition is in the list of dynamic
6047
         symbols, make sure the real definition is put
6048
         there as well.  */
6049
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6050
0
        {
6051
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6052
0
      {
6053
0
      err_free_sym_hash:
6054
0
        free (sorted_sym_hash);
6055
0
        goto error_return;
6056
0
      }
6057
0
        }
6058
6059
      /* If the real definition is in the list of dynamic
6060
         symbols, make sure the weak definition is put
6061
         there as well.  If we don't do this, then the
6062
         dynamic loader might not merge the entries for the
6063
         real definition and the weak definition.  */
6064
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6065
0
        {
6066
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6067
0
      goto err_free_sym_hash;
6068
0
        }
6069
0
      break;
6070
0
    }
6071
0
      }
6072
0
  }
6073
6074
0
      free (sorted_sym_hash);
6075
0
    }
6076
6077
0
  if (bed->check_directives
6078
0
      && !(*bed->check_directives) (abfd, info))
6079
0
    goto error_return;
6080
6081
  /* If this is a non-traditional link, try to optimize the handling
6082
     of the .stab/.stabstr sections.  */
6083
0
  if (! dynamic
6084
0
      && ! info->traditional_format
6085
0
      && is_elf_hash_table (&htab->root)
6086
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6087
0
    {
6088
0
      asection *stabstr;
6089
6090
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6091
0
      if (stabstr != NULL)
6092
0
  {
6093
0
    bfd_size_type string_offset = 0;
6094
0
    asection *stab;
6095
6096
0
    for (stab = abfd->sections; stab; stab = stab->next)
6097
0
      if (startswith (stab->name, ".stab")
6098
0
    && (!stab->name[5] ||
6099
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6100
0
    && (stab->flags & SEC_MERGE) == 0
6101
0
    && !bfd_is_abs_section (stab->output_section))
6102
0
        {
6103
0
    struct bfd_elf_section_data *secdata;
6104
6105
0
    secdata = elf_section_data (stab);
6106
0
    if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6107
0
                 stabstr, &secdata->sec_info,
6108
0
                 &string_offset))
6109
0
      goto error_return;
6110
0
    if (secdata->sec_info)
6111
0
      stab->sec_info_type = SEC_INFO_TYPE_STABS;
6112
0
      }
6113
0
  }
6114
0
    }
6115
6116
0
  if (dynamic && add_needed)
6117
0
    {
6118
      /* Add this bfd to the loaded list.  */
6119
0
      struct elf_link_loaded_list *n;
6120
6121
0
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6122
0
      if (n == NULL)
6123
0
  goto error_return;
6124
0
      n->abfd = abfd;
6125
0
      n->next = htab->dyn_loaded;
6126
0
      htab->dyn_loaded = n;
6127
0
    }
6128
0
  if (dynamic && !add_needed
6129
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6130
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6131
6132
0
  return true;
6133
6134
0
 error_free_vers:
6135
0
  free (old_tab);
6136
0
  free (old_strtab);
6137
0
  free (nondeflt_vers);
6138
0
  free (extversym);
6139
0
 error_free_sym:
6140
0
  free (isymbuf);
6141
0
 error_return:
6142
0
  return false;
6143
0
}
6144
6145
/* Return the linker hash table entry of a symbol that might be
6146
   satisfied by an archive symbol.  Return -1 on error.  */
6147
6148
struct bfd_link_hash_entry *
6149
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6150
        struct bfd_link_info *info,
6151
        const char *name)
6152
0
{
6153
0
  struct bfd_link_hash_entry *h;
6154
0
  char *p, *copy;
6155
0
  size_t len, first;
6156
6157
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6158
0
  if (h != NULL)
6159
0
    return h;
6160
6161
  /* If this is a default version (the name contains @@), look up the
6162
     symbol again with only one `@' as well as without the version.
6163
     The effect is that references to the symbol with and without the
6164
     version will be matched by the default symbol in the archive.  */
6165
6166
0
  p = strchr (name, ELF_VER_CHR);
6167
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6168
0
    {
6169
      /* Add this symbol to first hash if this archive has the first
6170
   definition.  */
6171
0
      if (is_elf_hash_table (info->hash))
6172
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6173
0
      return h;
6174
0
    }
6175
6176
  /* First check with only one `@'.  */
6177
0
  len = strlen (name);
6178
0
  copy = (char *) bfd_alloc (abfd, len);
6179
0
  if (copy == NULL)
6180
0
    return (struct bfd_link_hash_entry *) -1;
6181
6182
0
  first = p - name + 1;
6183
0
  memcpy (copy, name, first);
6184
0
  memcpy (copy + first, name + first + 1, len - first);
6185
6186
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6187
0
  if (h == NULL)
6188
0
    {
6189
      /* We also need to check references to the symbol without the
6190
   version.  */
6191
0
      copy[first - 1] = '\0';
6192
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6193
0
    }
6194
6195
0
  bfd_release (abfd, copy);
6196
0
  return h;
6197
0
}
6198
6199
/* Add symbols from an ELF archive file to the linker hash table.  We
6200
   don't use _bfd_generic_link_add_archive_symbols because we need to
6201
   handle versioned symbols.
6202
6203
   Fortunately, ELF archive handling is simpler than that done by
6204
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6205
   oddities.  In ELF, if we find a symbol in the archive map, and the
6206
   symbol is currently undefined, we know that we must pull in that
6207
   object file.
6208
6209
   Unfortunately, we do have to make multiple passes over the symbol
6210
   table until nothing further is resolved.  */
6211
6212
static bool
6213
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6214
0
{
6215
0
  symindex c;
6216
0
  unsigned char *included = NULL;
6217
0
  carsym *symdefs;
6218
0
  bool loop;
6219
0
  size_t amt;
6220
0
  const struct elf_backend_data *bed;
6221
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6222
0
    (bfd *, struct bfd_link_info *, const char *);
6223
6224
0
  if (! bfd_has_map (abfd))
6225
0
    {
6226
      /* An empty archive is a special case.  */
6227
0
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6228
0
  return true;
6229
0
      bfd_set_error (bfd_error_no_armap);
6230
0
      return false;
6231
0
    }
6232
6233
  /* Keep track of all symbols we know to be already defined, and all
6234
     files we know to be already included.  This is to speed up the
6235
     second and subsequent passes.  */
6236
0
  c = bfd_ardata (abfd)->symdef_count;
6237
0
  if (c == 0)
6238
0
    return true;
6239
0
  amt = c * sizeof (*included);
6240
0
  included = (unsigned char *) bfd_zmalloc (amt);
6241
0
  if (included == NULL)
6242
0
    return false;
6243
6244
0
  symdefs = bfd_ardata (abfd)->symdefs;
6245
0
  bed = get_elf_backend_data (abfd);
6246
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6247
6248
0
  do
6249
0
    {
6250
0
      file_ptr last;
6251
0
      symindex i;
6252
0
      carsym *symdef;
6253
0
      carsym *symdefend;
6254
6255
0
      loop = false;
6256
0
      last = -1;
6257
6258
0
      symdef = symdefs;
6259
0
      symdefend = symdef + c;
6260
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6261
0
  {
6262
0
    struct bfd_link_hash_entry *h;
6263
0
    bfd *element;
6264
0
    struct bfd_link_hash_entry *undefs_tail;
6265
0
    symindex mark;
6266
6267
0
    if (included[i])
6268
0
      continue;
6269
0
    if (symdef->file_offset == last)
6270
0
      {
6271
0
        included[i] = true;
6272
0
        continue;
6273
0
      }
6274
6275
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6276
0
    if (h == (struct bfd_link_hash_entry *) -1)
6277
0
      goto error_return;
6278
6279
0
    if (h == NULL)
6280
0
      continue;
6281
6282
0
    if (h->type == bfd_link_hash_undefined)
6283
0
      {
6284
0
        if (is_elf_hash_table (info->hash))
6285
0
    {
6286
      /* If the archive element has already been loaded then one
6287
         of the symbols defined by that element might have been
6288
         made undefined due to being in a discarded section.  */
6289
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6290
0
        continue;
6291
6292
      /* In the pre-LTO-plugin pass we must not mistakenly
6293
         include this archive member if an earlier shared
6294
         library defined this symbol.  */
6295
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6296
0
      if (htab->first_hash)
6297
0
        {
6298
0
          struct elf_link_first_hash_entry *e
6299
0
        = ((struct elf_link_first_hash_entry *)
6300
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6301
0
                false, false));
6302
0
          if (e
6303
0
        && (e->abfd->flags & DYNAMIC) != 0
6304
0
        && e->abfd != abfd)
6305
0
      continue;
6306
0
        }
6307
0
    }
6308
0
      }
6309
0
    else if (h->type == bfd_link_hash_common)
6310
0
      {
6311
        /* We currently have a common symbol.  The archive map contains
6312
     a reference to this symbol, so we may want to include it.  We
6313
     only want to include it however, if this archive element
6314
     contains a definition of the symbol, not just another common
6315
     declaration of it.
6316
6317
     Unfortunately some archivers (including GNU ar) will put
6318
     declarations of common symbols into their archive maps, as
6319
     well as real definitions, so we cannot just go by the archive
6320
     map alone.  Instead we must read in the element's symbol
6321
     table and check that to see what kind of symbol definition
6322
     this is.  */
6323
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6324
0
    continue;
6325
0
      }
6326
0
    else
6327
0
      {
6328
0
        if (h->type != bfd_link_hash_undefweak)
6329
    /* Symbol must be defined.  Don't check it again.  */
6330
0
    included[i] = true;
6331
6332
0
        if (!is_elf_hash_table (info->hash))
6333
0
    continue;
6334
0
        struct elf_link_hash_entry *eh
6335
0
    = (struct elf_link_hash_entry *) h;
6336
        /* Ignore the archive if the symbol isn't referenced by a
6337
     regular object or isn't defined in a shared object.  */
6338
0
        if (!eh->ref_regular || !eh->def_dynamic)
6339
0
    continue;
6340
        /* Ignore the dynamic definition if symbol is first
6341
     defined in this archive.  */
6342
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6343
0
        if (htab->first_hash == NULL)
6344
0
    continue;
6345
0
        struct elf_link_first_hash_entry *e
6346
0
    = ((struct elf_link_first_hash_entry *)
6347
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6348
0
            false, false));
6349
0
        if (e == NULL || e->abfd != abfd)
6350
0
    continue;
6351
0
      }
6352
6353
    /* We need to include this archive member.  */
6354
0
    element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6355
0
               info);
6356
0
    if (element == NULL)
6357
0
      goto error_return;
6358
6359
0
    if (! bfd_check_format (element, bfd_object))
6360
0
      goto error_return;
6361
6362
0
    undefs_tail = info->hash->undefs_tail;
6363
6364
0
    if (!(*info->callbacks
6365
0
    ->add_archive_element) (info, element, symdef->name, &element))
6366
0
      continue;
6367
0
    if (!bfd_link_add_symbols (element, info))
6368
0
      goto error_return;
6369
6370
    /* If there are any new undefined symbols, we need to make
6371
       another pass through the archive in order to see whether
6372
       they can be defined.  FIXME: This isn't perfect, because
6373
       common symbols wind up on undefs_tail and because an
6374
       undefined symbol which is defined later on in this pass
6375
       does not require another pass.  This isn't a bug, but it
6376
       does make the code less efficient than it could be.  */
6377
0
    if (undefs_tail != info->hash->undefs_tail)
6378
0
      loop = true;
6379
6380
    /* Look backward to mark all symbols from this object file
6381
       which we have already seen in this pass.  */
6382
0
    mark = i;
6383
0
    do
6384
0
      {
6385
0
        included[mark] = true;
6386
0
        if (mark == 0)
6387
0
    break;
6388
0
        --mark;
6389
0
      }
6390
0
    while (symdefs[mark].file_offset == symdef->file_offset);
6391
6392
    /* We mark subsequent symbols from this object file as we go
6393
       on through the loop.  */
6394
0
    last = symdef->file_offset;
6395
0
  }
6396
0
    }
6397
0
  while (loop);
6398
6399
0
  free (included);
6400
0
  return true;
6401
6402
0
 error_return:
6403
0
  free (included);
6404
0
  return false;
6405
0
}
6406
6407
/* Given an ELF BFD, add symbols to the global hash table as
6408
   appropriate.  */
6409
6410
bool
6411
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6412
0
{
6413
0
  switch (bfd_get_format (abfd))
6414
0
    {
6415
0
    case bfd_object:
6416
0
      return elf_link_add_object_symbols (abfd, info);
6417
0
    case bfd_archive:
6418
0
      return elf_link_add_archive_symbols (abfd, info);
6419
0
    default:
6420
0
      bfd_set_error (bfd_error_wrong_format);
6421
0
      return false;
6422
0
    }
6423
0
}
6424

6425
struct hash_codes_info
6426
{
6427
  unsigned long *hashcodes;
6428
  bool error;
6429
};
6430
6431
/* This function will be called though elf_link_hash_traverse to store
6432
   all hash value of the exported symbols in an array.  */
6433
6434
static bool
6435
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6436
0
{
6437
0
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
6438
0
  const char *name;
6439
0
  unsigned long ha;
6440
0
  char *alc = NULL;
6441
6442
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6443
0
  if (h->dynindx == -1)
6444
0
    return true;
6445
6446
0
  name = h->root.root.string;
6447
0
  if (h->versioned >= versioned)
6448
0
    {
6449
0
      char *p = strchr (name, ELF_VER_CHR);
6450
0
      if (p != NULL)
6451
0
  {
6452
0
    alc = (char *) bfd_malloc (p - name + 1);
6453
0
    if (alc == NULL)
6454
0
      {
6455
0
        inf->error = true;
6456
0
        return false;
6457
0
      }
6458
0
    memcpy (alc, name, p - name);
6459
0
    alc[p - name] = '\0';
6460
0
    name = alc;
6461
0
  }
6462
0
    }
6463
6464
  /* Compute the hash value.  */
6465
0
  ha = bfd_elf_hash (name);
6466
6467
  /* Store the found hash value in the array given as the argument.  */
6468
0
  *(inf->hashcodes)++ = ha;
6469
6470
  /* And store it in the struct so that we can put it in the hash table
6471
     later.  */
6472
0
  h->u.elf_hash_value = ha;
6473
6474
0
  free (alc);
6475
0
  return true;
6476
0
}
6477
6478
struct collect_gnu_hash_codes
6479
{
6480
  bfd *output_bfd;
6481
  const struct elf_backend_data *bed;
6482
  unsigned long int nsyms;
6483
  unsigned long int maskbits;
6484
  unsigned long int *hashcodes;
6485
  unsigned long int *hashval;
6486
  unsigned long int *indx;
6487
  unsigned long int *counts;
6488
  bfd_vma *bitmask;
6489
  bfd_byte *contents;
6490
  bfd_size_type xlat;
6491
  long int min_dynindx;
6492
  unsigned long int bucketcount;
6493
  unsigned long int symindx;
6494
  long int local_indx;
6495
  long int shift1, shift2;
6496
  unsigned long int mask;
6497
  bool error;
6498
};
6499
6500
/* This function will be called though elf_link_hash_traverse to store
6501
   all hash value of the exported symbols in an array.  */
6502
6503
static bool
6504
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6505
0
{
6506
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6507
0
  const char *name;
6508
0
  unsigned long ha;
6509
0
  char *alc = NULL;
6510
6511
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6512
0
  if (h->dynindx == -1)
6513
0
    return true;
6514
6515
  /* Ignore also local symbols and undefined symbols.  */
6516
0
  if (! (*s->bed->elf_hash_symbol) (h))
6517
0
    return true;
6518
6519
0
  name = h->root.root.string;
6520
0
  if (h->versioned >= versioned)
6521
0
    {
6522
0
      char *p = strchr (name, ELF_VER_CHR);
6523
0
      if (p != NULL)
6524
0
  {
6525
0
    alc = (char *) bfd_malloc (p - name + 1);
6526
0
    if (alc == NULL)
6527
0
      {
6528
0
        s->error = true;
6529
0
        return false;
6530
0
      }
6531
0
    memcpy (alc, name, p - name);
6532
0
    alc[p - name] = '\0';
6533
0
    name = alc;
6534
0
  }
6535
0
    }
6536
6537
  /* Compute the hash value.  */
6538
0
  ha = bfd_elf_gnu_hash (name);
6539
6540
  /* Store the found hash value in the array for compute_bucket_count,
6541
     and also for .dynsym reordering purposes.  */
6542
0
  s->hashcodes[s->nsyms] = ha;
6543
0
  s->hashval[h->dynindx] = ha;
6544
0
  ++s->nsyms;
6545
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6546
0
    s->min_dynindx = h->dynindx;
6547
6548
0
  free (alc);
6549
0
  return true;
6550
0
}
6551
6552
/* This function will be called though elf_link_hash_traverse to do
6553
   final dynamic symbol renumbering in case of .gnu.hash.
6554
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6555
   to the translation table.  */
6556
6557
static bool
6558
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6559
0
{
6560
0
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6561
0
  unsigned long int bucket;
6562
0
  unsigned long int val;
6563
6564
  /* Ignore indirect symbols.  */
6565
0
  if (h->dynindx == -1)
6566
0
    return true;
6567
6568
  /* Ignore also local symbols and undefined symbols.  */
6569
0
  if (! (*s->bed->elf_hash_symbol) (h))
6570
0
    {
6571
0
      if (h->dynindx >= s->min_dynindx)
6572
0
  {
6573
0
    if (s->bed->record_xhash_symbol != NULL)
6574
0
      {
6575
0
        (*s->bed->record_xhash_symbol) (h, 0);
6576
0
        s->local_indx++;
6577
0
      }
6578
0
    else
6579
0
      h->dynindx = s->local_indx++;
6580
0
  }
6581
0
      return true;
6582
0
    }
6583
6584
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6585
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6586
0
  & ((s->maskbits >> s->shift1) - 1);
6587
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6588
0
  s->bitmask[val]
6589
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6590
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6591
0
  if (s->counts[bucket] == 1)
6592
    /* Last element terminates the chain.  */
6593
0
    val |= 1;
6594
0
  bfd_put_32 (s->output_bfd, val,
6595
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6596
0
  --s->counts[bucket];
6597
0
  if (s->bed->record_xhash_symbol != NULL)
6598
0
    {
6599
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6600
6601
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6602
0
    }
6603
0
  else
6604
0
    h->dynindx = s->indx[bucket]++;
6605
0
  return true;
6606
0
}
6607
6608
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6609
6610
bool
6611
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6612
0
{
6613
0
  return !(h->forced_local
6614
0
     || h->root.type == bfd_link_hash_undefined
6615
0
     || h->root.type == bfd_link_hash_undefweak
6616
0
     || ((h->root.type == bfd_link_hash_defined
6617
0
    || h->root.type == bfd_link_hash_defweak)
6618
0
         && h->root.u.def.section->output_section == NULL));
6619
0
}
6620
6621
/* Array used to determine the number of hash table buckets to use
6622
   based on the number of symbols there are.  If there are fewer than
6623
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6624
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6625
   than 32771 buckets.  */
6626
6627
static const size_t elf_buckets[] =
6628
{
6629
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6630
  16411, 32771, 0
6631
};
6632
6633
/* Compute bucket count for hashing table.  We do not use a static set
6634
   of possible tables sizes anymore.  Instead we determine for all
6635
   possible reasonable sizes of the table the outcome (i.e., the
6636
   number of collisions etc) and choose the best solution.  The
6637
   weighting functions are not too simple to allow the table to grow
6638
   without bounds.  Instead one of the weighting factors is the size.
6639
   Therefore the result is always a good payoff between few collisions
6640
   (= short chain lengths) and table size.  */
6641
static size_t
6642
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6643
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6644
          unsigned long int nsyms,
6645
          int gnu_hash)
6646
0
{
6647
0
  size_t best_size = 0;
6648
0
  unsigned long int i;
6649
6650
0
  if (info->optimize)
6651
0
    {
6652
0
      size_t minsize;
6653
0
      size_t maxsize;
6654
0
      uint64_t best_chlen = ~((uint64_t) 0);
6655
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6656
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6657
0
      const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6658
0
      unsigned long int *counts;
6659
0
      bfd_size_type amt;
6660
0
      unsigned int no_improvement_count = 0;
6661
6662
      /* Possible optimization parameters: if we have NSYMS symbols we say
6663
   that the hashing table must at least have NSYMS/4 and at most
6664
   2*NSYMS buckets.  */
6665
0
      minsize = nsyms / 4;
6666
0
      if (minsize == 0)
6667
0
  minsize = 1;
6668
0
      best_size = maxsize = nsyms * 2;
6669
0
      if (gnu_hash)
6670
0
  {
6671
0
    if (minsize < 2)
6672
0
      minsize = 2;
6673
0
    if ((best_size & 31) == 0)
6674
0
      ++best_size;
6675
0
  }
6676
6677
      /* Create array where we count the collisions in.  We must use bfd_malloc
6678
   since the size could be large.  */
6679
0
      amt = maxsize;
6680
0
      amt *= sizeof (unsigned long int);
6681
0
      counts = (unsigned long int *) bfd_malloc (amt);
6682
0
      if (counts == NULL)
6683
0
  return 0;
6684
6685
      /* Compute the "optimal" size for the hash table.  The criteria is a
6686
   minimal chain length.  The minor criteria is (of course) the size
6687
   of the table.  */
6688
0
      for (i = minsize; i < maxsize; ++i)
6689
0
  {
6690
    /* Walk through the array of hashcodes and count the collisions.  */
6691
0
    uint64_t max;
6692
0
    unsigned long int j;
6693
0
    unsigned long int fact;
6694
6695
0
    if (gnu_hash && (i & 31) == 0)
6696
0
      continue;
6697
6698
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6699
6700
    /* Determine how often each hash bucket is used.  */
6701
0
    for (j = 0; j < nsyms; ++j)
6702
0
      ++counts[hashcodes[j] % i];
6703
6704
    /* For the weight function we need some information about the
6705
       pagesize on the target.  This is information need not be 100%
6706
       accurate.  Since this information is not available (so far) we
6707
       define it here to a reasonable default value.  If it is crucial
6708
       to have a better value some day simply define this value.  */
6709
0
# ifndef BFD_TARGET_PAGESIZE
6710
0
#  define BFD_TARGET_PAGESIZE (4096)
6711
0
# endif
6712
6713
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6714
       and the chains.  */
6715
0
    max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6716
6717
0
# if 1
6718
    /* Variant 1: optimize for short chains.  We add the squares
6719
       of all the chain lengths (which favors many small chain
6720
       over a few long chains).  */
6721
0
    for (j = 0; j < i; ++j)
6722
0
      max += counts[j] * counts[j];
6723
6724
    /* This adds penalties for the overall size of the table.  */
6725
0
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6726
0
    max *= fact * fact;
6727
# else
6728
    /* Variant 2: Optimize a lot more for small table.  Here we
6729
       also add squares of the size but we also add penalties for
6730
       empty slots (the +1 term).  */
6731
    for (j = 0; j < i; ++j)
6732
      max += (1 + counts[j]) * (1 + counts[j]);
6733
6734
    /* The overall size of the table is considered, but not as
6735
       strong as in variant 1, where it is squared.  */
6736
    fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6737
    max *= fact;
6738
# endif
6739
6740
    /* Compare with current best results.  */
6741
0
    if (max < best_chlen)
6742
0
      {
6743
0
        best_chlen = max;
6744
0
        best_size = i;
6745
0
        no_improvement_count = 0;
6746
0
      }
6747
    /* PR 11843: Avoid futile long searches for the best bucket size
6748
       when there are a large number of symbols.  */
6749
0
    else if (++no_improvement_count == 100)
6750
0
      break;
6751
0
  }
6752
6753
0
      free (counts);
6754
0
    }
6755
0
  else
6756
0
    {
6757
0
      for (i = 0; elf_buckets[i] != 0; i++)
6758
0
  {
6759
0
    best_size = elf_buckets[i];
6760
0
    if (nsyms < elf_buckets[i + 1])
6761
0
      break;
6762
0
  }
6763
0
      if (gnu_hash && best_size < 2)
6764
0
  best_size = 2;
6765
0
    }
6766
6767
0
  return best_size;
6768
0
}
6769
6770
/* Size any SHT_GROUP section for ld -r.  */
6771
6772
bool
6773
_bfd_elf_size_group_sections (struct bfd_link_info *info)
6774
0
{
6775
0
  bfd *ibfd;
6776
0
  asection *s;
6777
6778
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6779
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6780
0
  && (s = ibfd->sections) != NULL
6781
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6782
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6783
0
      return false;
6784
0
  return true;
6785
0
}
6786
6787
/* Set a default stack segment size.  The value in INFO wins.  If it
6788
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6789
   undefined it is initialized.  */
6790
6791
bool
6792
bfd_elf_stack_segment_size (bfd *output_bfd,
6793
          struct bfd_link_info *info,
6794
          const char *legacy_symbol,
6795
          bfd_vma default_size)
6796
0
{
6797
0
  struct elf_link_hash_entry *h = NULL;
6798
6799
  /* Look for legacy symbol.  */
6800
0
  if (legacy_symbol)
6801
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6802
0
            false, false, false);
6803
0
  if (h && (h->root.type == bfd_link_hash_defined
6804
0
      || h->root.type == bfd_link_hash_defweak)
6805
0
      && h->def_regular
6806
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6807
0
    {
6808
      /* The symbol has no type if specified on the command line.  */
6809
0
      h->type = STT_OBJECT;
6810
0
      if (info->stacksize)
6811
  /* xgettext:c-format */
6812
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6813
0
          output_bfd, legacy_symbol);
6814
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6815
  /* xgettext:c-format */
6816
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6817
0
          output_bfd, legacy_symbol);
6818
0
      else
6819
0
  info->stacksize = h->root.u.def.value;
6820
0
    }
6821
6822
0
  if (!info->stacksize)
6823
    /* If the user didn't set a size, or explicitly inhibit the
6824
       size, set it now.  */
6825
0
    info->stacksize = default_size;
6826
6827
  /* Provide the legacy symbol, if it is referenced.  */
6828
0
  if (h && (h->root.type == bfd_link_hash_undefined
6829
0
      || h->root.type == bfd_link_hash_undefweak))
6830
0
    {
6831
0
      struct bfd_link_hash_entry *bh = NULL;
6832
6833
0
      if (!(_bfd_generic_link_add_one_symbol
6834
0
      (info, output_bfd, legacy_symbol,
6835
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6836
0
       info->stacksize >= 0 ? info->stacksize : 0,
6837
0
       NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6838
0
  return false;
6839
6840
0
      h = (struct elf_link_hash_entry *) bh;
6841
0
      h->def_regular = 1;
6842
0
      h->type = STT_OBJECT;
6843
0
    }
6844
6845
0
  return true;
6846
0
}
6847
6848
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6849
6850
struct elf_gc_sweep_symbol_info
6851
{
6852
  struct bfd_link_info *info;
6853
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6854
           bool);
6855
};
6856
6857
static bool
6858
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6859
0
{
6860
0
  if (!h->mark
6861
0
      && (((h->root.type == bfd_link_hash_defined
6862
0
      || h->root.type == bfd_link_hash_defweak)
6863
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6864
0
    && h->root.u.def.section->gc_mark))
6865
0
    || h->root.type == bfd_link_hash_undefined
6866
0
    || h->root.type == bfd_link_hash_undefweak))
6867
0
    {
6868
0
      struct elf_gc_sweep_symbol_info *inf;
6869
6870
0
      inf = (struct elf_gc_sweep_symbol_info *) data;
6871
0
      (*inf->hide_symbol) (inf->info, h, true);
6872
0
      h->def_regular = 0;
6873
0
      h->ref_regular = 0;
6874
0
      h->ref_regular_nonweak = 0;
6875
0
    }
6876
6877
0
  return true;
6878
0
}
6879
6880
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6881
   called by the ELF linker emulation before_allocation routine.  We
6882
   must set the sizes of the sections before the linker sets the
6883
   addresses of the various sections.  */
6884
6885
bool
6886
bfd_elf_size_dynamic_sections (bfd *output_bfd,
6887
             const char *soname,
6888
             const char *rpath,
6889
             const char *filter_shlib,
6890
             const char *audit,
6891
             const char *depaudit,
6892
             const char * const *auxiliary_filters,
6893
             struct bfd_link_info *info,
6894
             asection **sinterpptr)
6895
0
{
6896
0
  bfd *dynobj;
6897
0
  const struct elf_backend_data *bed;
6898
6899
0
  *sinterpptr = NULL;
6900
6901
0
  if (!is_elf_hash_table (info->hash))
6902
0
    return true;
6903
6904
  /* Any syms created from now on start with -1 in
6905
     got.refcount/offset and plt.refcount/offset.  */
6906
0
  elf_hash_table (info)->init_got_refcount
6907
0
    = elf_hash_table (info)->init_got_offset;
6908
0
  elf_hash_table (info)->init_plt_refcount
6909
0
    = elf_hash_table (info)->init_plt_offset;
6910
6911
0
  bed = get_elf_backend_data (output_bfd);
6912
6913
  /* The backend may have to create some sections regardless of whether
6914
     we're dynamic or not.  */
6915
0
  if (bed->elf_backend_early_size_sections
6916
0
      && !bed->elf_backend_early_size_sections (output_bfd, info))
6917
0
    return false;
6918
6919
0
  dynobj = elf_hash_table (info)->dynobj;
6920
6921
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6922
0
    {
6923
0
      struct bfd_elf_version_tree *verdefs;
6924
0
      struct elf_info_failed asvinfo;
6925
0
      struct bfd_elf_version_tree *t;
6926
0
      struct bfd_elf_version_expr *d;
6927
0
      asection *s;
6928
0
      size_t soname_indx;
6929
6930
      /* If we are supposed to export all symbols into the dynamic symbol
6931
   table (this is not the normal case), then do so.  */
6932
0
      if (info->export_dynamic
6933
0
    || (bfd_link_executable (info) && info->dynamic))
6934
0
  {
6935
0
    struct elf_info_failed eif;
6936
6937
0
    eif.info = info;
6938
0
    eif.failed = false;
6939
0
    elf_link_hash_traverse (elf_hash_table (info),
6940
0
          _bfd_elf_export_symbol,
6941
0
          &eif);
6942
0
    if (eif.failed)
6943
0
      return false;
6944
0
  }
6945
6946
0
      if (soname != NULL)
6947
0
  {
6948
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6949
0
               soname, true);
6950
0
    if (soname_indx == (size_t) -1
6951
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6952
0
      return false;
6953
0
  }
6954
0
      else
6955
0
  soname_indx = (size_t) -1;
6956
6957
      /* Make all global versions with definition.  */
6958
0
      for (t = info->version_info; t != NULL; t = t->next)
6959
0
  for (d = t->globals.list; d != NULL; d = d->next)
6960
0
    if (!d->symver && d->literal)
6961
0
      {
6962
0
        const char *verstr, *name;
6963
0
        size_t namelen, verlen, newlen;
6964
0
        char *newname, *p, leading_char;
6965
0
        struct elf_link_hash_entry *newh;
6966
6967
0
        leading_char = bfd_get_symbol_leading_char (output_bfd);
6968
0
        name = d->pattern;
6969
0
        namelen = strlen (name) + (leading_char != '\0');
6970
0
        verstr = t->name;
6971
0
        verlen = strlen (verstr);
6972
0
        newlen = namelen + verlen + 3;
6973
6974
0
        newname = (char *) bfd_malloc (newlen);
6975
0
        if (newname == NULL)
6976
0
    return false;
6977
0
        newname[0] = leading_char;
6978
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
6979
6980
        /* Check the hidden versioned definition.  */
6981
0
        p = newname + namelen;
6982
0
        *p++ = ELF_VER_CHR;
6983
0
        memcpy (p, verstr, verlen + 1);
6984
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
6985
0
             newname, false, false,
6986
0
             false);
6987
0
        if (newh == NULL
6988
0
      || (newh->root.type != bfd_link_hash_defined
6989
0
          && newh->root.type != bfd_link_hash_defweak))
6990
0
    {
6991
      /* Check the default versioned definition.  */
6992
0
      *p++ = ELF_VER_CHR;
6993
0
      memcpy (p, verstr, verlen + 1);
6994
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
6995
0
                 newname, false, false,
6996
0
                 false);
6997
0
    }
6998
0
        free (newname);
6999
7000
        /* Mark this version if there is a definition and it is
7001
     not defined in a shared object.  */
7002
0
        if (newh != NULL
7003
0
      && !newh->def_dynamic
7004
0
      && (newh->root.type == bfd_link_hash_defined
7005
0
          || newh->root.type == bfd_link_hash_defweak))
7006
0
    d->symver = 1;
7007
0
      }
7008
7009
      /* Attach all the symbols to their version information.  */
7010
0
      asvinfo.info = info;
7011
0
      asvinfo.failed = false;
7012
7013
0
      elf_link_hash_traverse (elf_hash_table (info),
7014
0
            _bfd_elf_link_assign_sym_version,
7015
0
            &asvinfo);
7016
0
      if (asvinfo.failed)
7017
0
  return false;
7018
7019
0
      if (!info->allow_undefined_version)
7020
0
  {
7021
    /* Check if all global versions have a definition.  */
7022
0
    bool all_defined = true;
7023
0
    for (t = info->version_info; t != NULL; t = t->next)
7024
0
      for (d = t->globals.list; d != NULL; d = d->next)
7025
0
        if (d->literal && !d->symver && !d->script)
7026
0
    {
7027
0
      _bfd_error_handler
7028
0
        (_("%s: undefined version: %s"),
7029
0
         d->pattern, t->name);
7030
0
      all_defined = false;
7031
0
    }
7032
7033
0
    if (!all_defined)
7034
0
      {
7035
0
        bfd_set_error (bfd_error_bad_value);
7036
0
        return false;
7037
0
      }
7038
0
  }
7039
7040
      /* Set up the version definition section.  */
7041
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7042
0
      BFD_ASSERT (s != NULL);
7043
7044
      /* We may have created additional version definitions if we are
7045
   just linking a regular application.  */
7046
0
      verdefs = info->version_info;
7047
7048
      /* Skip anonymous version tag.  */
7049
0
      if (verdefs != NULL && verdefs->vernum == 0)
7050
0
  verdefs = verdefs->next;
7051
7052
0
      if (verdefs == NULL && !info->create_default_symver)
7053
0
  s->flags |= SEC_EXCLUDE;
7054
0
      else
7055
0
  {
7056
0
    unsigned int cdefs;
7057
0
    bfd_size_type size;
7058
0
    bfd_byte *p;
7059
0
    Elf_Internal_Verdef def;
7060
0
    Elf_Internal_Verdaux defaux;
7061
0
    struct bfd_link_hash_entry *bh;
7062
0
    struct elf_link_hash_entry *h;
7063
0
    const char *name;
7064
7065
0
    cdefs = 0;
7066
0
    size = 0;
7067
7068
    /* Make space for the base version.  */
7069
0
    size += sizeof (Elf_External_Verdef);
7070
0
    size += sizeof (Elf_External_Verdaux);
7071
0
    ++cdefs;
7072
7073
    /* Make space for the default version.  */
7074
0
    if (info->create_default_symver)
7075
0
      {
7076
0
        size += sizeof (Elf_External_Verdef);
7077
0
        ++cdefs;
7078
0
      }
7079
7080
0
    for (t = verdefs; t != NULL; t = t->next)
7081
0
      {
7082
0
        struct bfd_elf_version_deps *n;
7083
7084
        /* Don't emit base version twice.  */
7085
0
        if (t->vernum == 0)
7086
0
    continue;
7087
7088
0
        size += sizeof (Elf_External_Verdef);
7089
0
        size += sizeof (Elf_External_Verdaux);
7090
0
        ++cdefs;
7091
7092
0
        for (n = t->deps; n != NULL; n = n->next)
7093
0
    size += sizeof (Elf_External_Verdaux);
7094
0
      }
7095
7096
0
    s->size = size;
7097
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7098
0
    if (s->contents == NULL && s->size != 0)
7099
0
      return false;
7100
0
    s->alloced = 1;
7101
7102
    /* Fill in the version definition section.  */
7103
7104
0
    p = s->contents;
7105
7106
0
    def.vd_version = VER_DEF_CURRENT;
7107
0
    def.vd_flags = VER_FLG_BASE;
7108
0
    def.vd_ndx = 1;
7109
0
    def.vd_cnt = 1;
7110
0
    if (info->create_default_symver)
7111
0
      {
7112
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7113
0
        def.vd_next = sizeof (Elf_External_Verdef);
7114
0
      }
7115
0
    else
7116
0
      {
7117
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7118
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7119
0
           + sizeof (Elf_External_Verdaux));
7120
0
      }
7121
7122
0
    if (soname_indx != (size_t) -1)
7123
0
      {
7124
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7125
0
              soname_indx);
7126
0
        def.vd_hash = bfd_elf_hash (soname);
7127
0
        defaux.vda_name = soname_indx;
7128
0
        name = soname;
7129
0
      }
7130
0
    else
7131
0
      {
7132
0
        size_t indx;
7133
7134
0
        name = lbasename (bfd_get_filename (output_bfd));
7135
0
        def.vd_hash = bfd_elf_hash (name);
7136
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7137
0
            name, false);
7138
0
        if (indx == (size_t) -1)
7139
0
    return false;
7140
0
        defaux.vda_name = indx;
7141
0
      }
7142
0
    defaux.vda_next = 0;
7143
7144
0
    _bfd_elf_swap_verdef_out (output_bfd, &def,
7145
0
            (Elf_External_Verdef *) p);
7146
0
    p += sizeof (Elf_External_Verdef);
7147
0
    if (info->create_default_symver)
7148
0
      {
7149
        /* Add a symbol representing this version.  */
7150
0
        bh = NULL;
7151
0
        if (! (_bfd_generic_link_add_one_symbol
7152
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7153
0
          0, NULL, false,
7154
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7155
0
    return false;
7156
0
        h = (struct elf_link_hash_entry *) bh;
7157
0
        h->non_elf = 0;
7158
0
        h->def_regular = 1;
7159
0
        h->type = STT_OBJECT;
7160
0
        h->verinfo.vertree = NULL;
7161
7162
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7163
0
    return false;
7164
7165
        /* Create a duplicate of the base version with the same
7166
     aux block, but different flags.  */
7167
0
        def.vd_flags = 0;
7168
0
        def.vd_ndx = 2;
7169
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7170
0
        if (verdefs)
7171
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7172
0
             + sizeof (Elf_External_Verdaux));
7173
0
        else
7174
0
    def.vd_next = 0;
7175
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7176
0
          (Elf_External_Verdef *) p);
7177
0
        p += sizeof (Elf_External_Verdef);
7178
0
      }
7179
0
    _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7180
0
             (Elf_External_Verdaux *) p);
7181
0
    p += sizeof (Elf_External_Verdaux);
7182
7183
0
    for (t = verdefs; t != NULL; t = t->next)
7184
0
      {
7185
0
        unsigned int cdeps;
7186
0
        struct bfd_elf_version_deps *n;
7187
7188
        /* Don't emit the base version twice.  */
7189
0
        if (t->vernum == 0)
7190
0
    continue;
7191
7192
0
        cdeps = 0;
7193
0
        for (n = t->deps; n != NULL; n = n->next)
7194
0
    ++cdeps;
7195
7196
        /* Add a symbol representing this version.  */
7197
0
        bh = NULL;
7198
0
        if (! (_bfd_generic_link_add_one_symbol
7199
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7200
0
          0, NULL, false,
7201
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7202
0
    return false;
7203
0
        h = (struct elf_link_hash_entry *) bh;
7204
0
        h->non_elf = 0;
7205
0
        h->def_regular = 1;
7206
0
        h->type = STT_OBJECT;
7207
0
        h->verinfo.vertree = t;
7208
7209
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7210
0
    return false;
7211
7212
0
        def.vd_version = VER_DEF_CURRENT;
7213
0
        def.vd_flags = 0;
7214
0
        if (t->globals.list == NULL
7215
0
      && t->locals.list == NULL
7216
0
      && ! t->used)
7217
0
    def.vd_flags |= VER_FLG_WEAK;
7218
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7219
0
        def.vd_cnt = cdeps + 1;
7220
0
        def.vd_hash = bfd_elf_hash (t->name);
7221
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7222
0
        def.vd_next = 0;
7223
7224
        /* If a basever node is next, it *must* be the last node in
7225
     the chain, otherwise Verdef construction breaks.  */
7226
0
        if (t->next != NULL && t->next->vernum == 0)
7227
0
    BFD_ASSERT (t->next->next == NULL);
7228
7229
0
        if (t->next != NULL && t->next->vernum != 0)
7230
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7231
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7232
7233
0
        _bfd_elf_swap_verdef_out (output_bfd, &def,
7234
0
          (Elf_External_Verdef *) p);
7235
0
        p += sizeof (Elf_External_Verdef);
7236
7237
0
        defaux.vda_name = h->dynstr_index;
7238
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7239
0
              h->dynstr_index);
7240
0
        defaux.vda_next = 0;
7241
0
        if (t->deps != NULL)
7242
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7243
0
        t->name_indx = defaux.vda_name;
7244
7245
0
        _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7246
0
           (Elf_External_Verdaux *) p);
7247
0
        p += sizeof (Elf_External_Verdaux);
7248
7249
0
        for (n = t->deps; n != NULL; n = n->next)
7250
0
    {
7251
0
      if (n->version_needed == NULL)
7252
0
        {
7253
          /* This can happen if there was an error in the
7254
       version script.  */
7255
0
          defaux.vda_name = 0;
7256
0
        }
7257
0
      else
7258
0
        {
7259
0
          defaux.vda_name = n->version_needed->name_indx;
7260
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7261
0
                defaux.vda_name);
7262
0
        }
7263
0
      if (n->next == NULL)
7264
0
        defaux.vda_next = 0;
7265
0
      else
7266
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7267
7268
0
      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7269
0
               (Elf_External_Verdaux *) p);
7270
0
      p += sizeof (Elf_External_Verdaux);
7271
0
    }
7272
0
      }
7273
7274
0
    elf_tdata (output_bfd)->cverdefs = cdefs;
7275
0
  }
7276
0
    }
7277
7278
0
  if (info->gc_sections && bed->can_gc_sections)
7279
0
    {
7280
0
      struct elf_gc_sweep_symbol_info sweep_info;
7281
7282
      /* Remove the symbols that were in the swept sections from the
7283
   dynamic symbol table.  */
7284
0
      sweep_info.info = info;
7285
0
      sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7286
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7287
0
            &sweep_info);
7288
0
    }
7289
7290
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7291
0
    {
7292
0
      asection *s;
7293
0
      struct elf_find_verdep_info sinfo;
7294
7295
      /* Work out the size of the version reference section.  */
7296
7297
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7298
0
      BFD_ASSERT (s != NULL);
7299
7300
0
      sinfo.info = info;
7301
0
      sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7302
0
      if (sinfo.vers == 0)
7303
0
  sinfo.vers = 1;
7304
0
      sinfo.failed = false;
7305
7306
0
      elf_link_hash_traverse (elf_hash_table (info),
7307
0
            _bfd_elf_link_find_version_dependencies,
7308
0
            &sinfo);
7309
0
      if (sinfo.failed)
7310
0
  return false;
7311
7312
0
      bed->elf_backend_add_glibc_version_dependency (&sinfo);
7313
0
      if (sinfo.failed)
7314
0
  return false;
7315
7316
0
      if (elf_tdata (output_bfd)->verref == NULL)
7317
0
  s->flags |= SEC_EXCLUDE;
7318
0
      else
7319
0
  {
7320
0
    Elf_Internal_Verneed *vn;
7321
0
    unsigned int size;
7322
0
    unsigned int crefs;
7323
0
    bfd_byte *p;
7324
7325
    /* Build the version dependency section.  */
7326
0
    size = 0;
7327
0
    crefs = 0;
7328
0
    for (vn = elf_tdata (output_bfd)->verref;
7329
0
         vn != NULL;
7330
0
         vn = vn->vn_nextref)
7331
0
      {
7332
0
        Elf_Internal_Vernaux *a;
7333
7334
0
        size += sizeof (Elf_External_Verneed);
7335
0
        ++crefs;
7336
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7337
0
    size += sizeof (Elf_External_Vernaux);
7338
0
      }
7339
7340
0
    s->size = size;
7341
0
    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7342
0
    if (s->contents == NULL)
7343
0
      return false;
7344
0
    s->alloced = 1;
7345
7346
0
    p = s->contents;
7347
0
    for (vn = elf_tdata (output_bfd)->verref;
7348
0
         vn != NULL;
7349
0
         vn = vn->vn_nextref)
7350
0
      {
7351
0
        unsigned int caux;
7352
0
        Elf_Internal_Vernaux *a;
7353
0
        size_t indx;
7354
7355
0
        caux = 0;
7356
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7357
0
    ++caux;
7358
7359
0
        vn->vn_version = VER_NEED_CURRENT;
7360
0
        vn->vn_cnt = caux;
7361
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7362
0
            elf_dt_name (vn->vn_bfd) != NULL
7363
0
            ? elf_dt_name (vn->vn_bfd)
7364
0
            : lbasename (bfd_get_filename
7365
0
                   (vn->vn_bfd)),
7366
0
            false);
7367
0
        if (indx == (size_t) -1)
7368
0
    return false;
7369
0
        vn->vn_file = indx;
7370
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7371
0
        if (vn->vn_nextref == NULL)
7372
0
    vn->vn_next = 0;
7373
0
        else
7374
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7375
0
             + caux * sizeof (Elf_External_Vernaux));
7376
7377
0
        _bfd_elf_swap_verneed_out (output_bfd, vn,
7378
0
           (Elf_External_Verneed *) p);
7379
0
        p += sizeof (Elf_External_Verneed);
7380
7381
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7382
0
    {
7383
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7384
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7385
0
                a->vna_nodename, false);
7386
0
      if (indx == (size_t) -1)
7387
0
        return false;
7388
0
      a->vna_name = indx;
7389
0
      if (a->vna_nextptr == NULL)
7390
0
        a->vna_next = 0;
7391
0
      else
7392
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7393
7394
0
      _bfd_elf_swap_vernaux_out (output_bfd, a,
7395
0
               (Elf_External_Vernaux *) p);
7396
0
      p += sizeof (Elf_External_Vernaux);
7397
0
    }
7398
0
      }
7399
7400
0
    elf_tdata (output_bfd)->cverrefs = crefs;
7401
0
  }
7402
0
    }
7403
7404
0
  if (bfd_link_relocatable (info)
7405
0
      && !_bfd_elf_size_group_sections (info))
7406
0
    return false;
7407
7408
  /* Determine any GNU_STACK segment requirements, after the backend
7409
     has had a chance to set a default segment size.  */
7410
0
  if (info->execstack)
7411
0
    {
7412
      /* If the user has explicitly requested warnings, then generate one even
7413
   though the choice is the result of another command line option.  */
7414
0
      if (info->warn_execstack == 1)
7415
0
  {
7416
0
    if (info->error_execstack)
7417
0
      {
7418
0
        _bfd_error_handler
7419
0
    (_("\
7420
0
error: creating an executable stack because of -z execstack command line option"));
7421
0
        return false;
7422
0
      }
7423
7424
0
    _bfd_error_handler
7425
0
      (_("\
7426
0
warning: enabling an executable stack because of -z execstack command line option"));
7427
0
  }
7428
7429
0
      elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7430
0
    }
7431
0
  else if (info->noexecstack)
7432
0
    elf_stack_flags (output_bfd) = PF_R | PF_W;
7433
0
  else
7434
0
    {
7435
0
      bfd *inputobj;
7436
0
      asection *notesec = NULL;
7437
0
      bfd *noteobj = NULL;
7438
0
      bfd *emptyobj = NULL;
7439
0
      int exec = 0;
7440
7441
0
      for (inputobj = info->input_bfds;
7442
0
     inputobj;
7443
0
     inputobj = inputobj->link.next)
7444
0
  {
7445
0
    asection *s;
7446
7447
0
    if (inputobj->flags
7448
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7449
0
      continue;
7450
0
    s = inputobj->sections;
7451
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7452
0
      continue;
7453
7454
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7455
0
    if (s)
7456
0
      {
7457
0
        notesec = s;
7458
0
        if (s->flags & SEC_CODE)
7459
0
    {
7460
0
      noteobj = inputobj;
7461
0
      exec = PF_X;
7462
      /* There is no point in scanning the remaining bfds.  */
7463
0
      break;
7464
0
    }
7465
0
      }
7466
0
    else if (bed->default_execstack && info->default_execstack)
7467
0
      {
7468
0
        exec = PF_X;
7469
0
        emptyobj = inputobj;
7470
0
      }
7471
0
  }
7472
7473
0
      if (notesec || info->stacksize > 0)
7474
0
  {
7475
0
    if (exec)
7476
0
      {
7477
0
        if (info->warn_execstack != 0)
7478
0
    {
7479
      /* PR 29072: Because an executable stack is a serious
7480
         security risk, make sure that the user knows that it is
7481
         being enabled despite the fact that it was not requested
7482
         on the command line.  */
7483
0
      if (noteobj)
7484
0
        {
7485
0
          if (info->error_execstack)
7486
0
      {
7487
0
        _bfd_error_handler (_("\
7488
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7489
0
                bfd_get_filename (noteobj));
7490
0
        return false;
7491
0
      }
7492
7493
0
          _bfd_error_handler (_("\
7494
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7495
0
           bfd_get_filename (noteobj));
7496
0
        }
7497
0
      else if (emptyobj)
7498
0
        {
7499
0
          if (info->error_execstack)
7500
0
      {
7501
0
        _bfd_error_handler (_("\
7502
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7503
0
                bfd_get_filename (emptyobj));
7504
0
        return false;
7505
0
      }
7506
7507
0
          _bfd_error_handler (_("\
7508
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7509
0
            bfd_get_filename (emptyobj));
7510
0
          _bfd_error_handler (_("\
7511
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7512
0
        }
7513
0
    }
7514
0
      }
7515
0
    elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7516
0
  }
7517
7518
0
      if (notesec && exec && bfd_link_relocatable (info)
7519
0
    && notesec->output_section != bfd_abs_section_ptr)
7520
0
  notesec->output_section->flags |= SEC_CODE;
7521
0
    }
7522
7523
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7524
0
    {
7525
0
      struct elf_info_failed eif;
7526
0
      struct elf_link_hash_entry *h;
7527
0
      asection *dynstr;
7528
0
      asection *s;
7529
7530
0
      *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7531
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7532
7533
0
      if (info->symbolic)
7534
0
  {
7535
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7536
0
      return false;
7537
0
    info->flags |= DF_SYMBOLIC;
7538
0
  }
7539
7540
0
      if (rpath != NULL)
7541
0
  {
7542
0
    size_t indx;
7543
0
    bfd_vma tag;
7544
7545
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7546
0
              true);
7547
0
    if (indx == (size_t) -1)
7548
0
      return false;
7549
7550
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7551
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7552
0
      return false;
7553
0
  }
7554
7555
0
      if (filter_shlib != NULL)
7556
0
  {
7557
0
    size_t indx;
7558
7559
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7560
0
              filter_shlib, true);
7561
0
    if (indx == (size_t) -1
7562
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7563
0
      return false;
7564
0
  }
7565
7566
0
      if (auxiliary_filters != NULL)
7567
0
  {
7568
0
    const char * const *p;
7569
7570
0
    for (p = auxiliary_filters; *p != NULL; p++)
7571
0
      {
7572
0
        size_t indx;
7573
7574
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7575
0
            *p, true);
7576
0
        if (indx == (size_t) -1
7577
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7578
0
    return false;
7579
0
      }
7580
0
  }
7581
7582
0
      if (audit != NULL)
7583
0
  {
7584
0
    size_t indx;
7585
7586
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7587
0
              true);
7588
0
    if (indx == (size_t) -1
7589
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7590
0
      return false;
7591
0
  }
7592
7593
0
      if (depaudit != NULL)
7594
0
  {
7595
0
    size_t indx;
7596
7597
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7598
0
              true);
7599
0
    if (indx == (size_t) -1
7600
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7601
0
      return false;
7602
0
  }
7603
7604
0
      eif.info = info;
7605
0
      eif.failed = false;
7606
7607
      /* Find all symbols which were defined in a dynamic object and make
7608
   the backend pick a reasonable value for them.  */
7609
0
      elf_link_hash_traverse (elf_hash_table (info),
7610
0
            _bfd_elf_adjust_dynamic_symbol,
7611
0
            &eif);
7612
0
      if (eif.failed)
7613
0
  return false;
7614
7615
      /* Add some entries to the .dynamic section.  We fill in some of the
7616
   values later, in bfd_elf_final_link, but we must add the entries
7617
   now so that we know the final size of the .dynamic section.  */
7618
7619
      /* If there are initialization and/or finalization functions to
7620
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7621
0
      h = (info->init_function
7622
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7623
0
           info->init_function, false,
7624
0
           false, false)
7625
0
     : NULL);
7626
0
      if (h != NULL
7627
0
    && (h->ref_regular
7628
0
        || h->def_regular))
7629
0
  {
7630
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7631
0
      return false;
7632
0
  }
7633
0
      h = (info->fini_function
7634
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7635
0
           info->fini_function, false,
7636
0
           false, false)
7637
0
     : NULL);
7638
0
      if (h != NULL
7639
0
    && (h->ref_regular
7640
0
        || h->def_regular))
7641
0
  {
7642
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7643
0
      return false;
7644
0
  }
7645
7646
0
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7647
0
      if (s != NULL && s->linker_has_input)
7648
0
  {
7649
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7650
0
    if (! bfd_link_executable (info))
7651
0
      {
7652
0
        bfd *sub;
7653
0
        asection *o;
7654
7655
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7656
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7657
0
        && (o = sub->sections) != NULL
7658
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7659
0
      for (o = sub->sections; o != NULL; o = o->next)
7660
0
        if (elf_section_data (o)->this_hdr.sh_type
7661
0
      == SHT_PREINIT_ARRAY)
7662
0
          {
7663
0
      _bfd_error_handler
7664
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7665
0
         sub);
7666
0
      break;
7667
0
          }
7668
7669
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7670
0
        return false;
7671
0
      }
7672
7673
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7674
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7675
0
      return false;
7676
0
  }
7677
0
      s = bfd_get_section_by_name (output_bfd, ".init_array");
7678
0
      if (s != NULL && s->linker_has_input)
7679
0
  {
7680
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7681
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7682
0
      return false;
7683
0
  }
7684
0
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
7685
0
      if (s != NULL && s->linker_has_input)
7686
0
  {
7687
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7688
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7689
0
      return false;
7690
0
  }
7691
7692
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7693
      /* If .dynstr is excluded from the link, we don't want any of
7694
   these tags.  Strictly, we should be checking each section
7695
   individually;  This quick check covers for the case where
7696
   someone does a /DISCARD/ : { *(*) }.  */
7697
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7698
0
  {
7699
0
    bfd_size_type strsize;
7700
7701
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7702
0
    if ((info->emit_hash
7703
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7704
0
        || (info->emit_gnu_hash
7705
0
      && (bed->record_xhash_symbol == NULL
7706
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7707
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7708
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7709
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7710
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7711
0
                bed->s->sizeof_sym)
7712
0
        || (info->gnu_flags_1
7713
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7714
0
              info->gnu_flags_1)))
7715
0
      return false;
7716
0
  }
7717
0
    }
7718
7719
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7720
0
    return false;
7721
7722
  /* The backend must work out the sizes of all the other dynamic
7723
     sections.  */
7724
0
  if (bed->elf_backend_late_size_sections != NULL
7725
0
      && !bed->elf_backend_late_size_sections (output_bfd, info))
7726
0
    return false;
7727
7728
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7729
0
    {
7730
0
      if (elf_tdata (output_bfd)->cverdefs)
7731
0
  {
7732
0
    unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7733
7734
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7735
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7736
0
      return false;
7737
0
  }
7738
7739
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7740
0
  {
7741
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7742
0
      return false;
7743
0
  }
7744
0
      else if (info->flags & DF_BIND_NOW)
7745
0
  {
7746
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7747
0
      return false;
7748
0
  }
7749
7750
0
      if (info->flags_1)
7751
0
  {
7752
0
    if (bfd_link_executable (info))
7753
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7754
0
        | DF_1_NODELETE
7755
0
        | DF_1_NOOPEN);
7756
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7757
0
      return false;
7758
0
  }
7759
7760
0
      if (elf_tdata (output_bfd)->cverrefs)
7761
0
  {
7762
0
    unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7763
7764
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7765
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7766
0
      return false;
7767
0
  }
7768
7769
0
      if ((elf_tdata (output_bfd)->cverrefs == 0
7770
0
     && elf_tdata (output_bfd)->cverdefs == 0)
7771
0
    || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7772
0
  {
7773
0
    asection *s;
7774
7775
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7776
0
    s->flags |= SEC_EXCLUDE;
7777
0
  }
7778
0
    }
7779
0
  return true;
7780
0
}
7781
7782
/* Find the first non-excluded output section.  We'll use its
7783
   section symbol for some emitted relocs.  */
7784
void
7785
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7786
0
{
7787
0
  asection *s;
7788
0
  asection *found = NULL;
7789
7790
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7791
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7792
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7793
0
      {
7794
0
  found = s;
7795
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7796
0
    break;
7797
0
      }
7798
0
  elf_hash_table (info)->text_index_section = found;
7799
0
}
7800
7801
/* Find two non-excluded output sections, one for code, one for data.
7802
   We'll use their section symbols for some emitted relocs.  */
7803
void
7804
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7805
0
{
7806
0
  asection *s;
7807
0
  asection *found = NULL;
7808
7809
  /* Data first, since setting text_index_section changes
7810
     _bfd_elf_omit_section_dynsym_default.  */
7811
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7812
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7813
0
  && !(s->flags & SEC_READONLY)
7814
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7815
0
      {
7816
0
  found = s;
7817
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7818
0
    break;
7819
0
      }
7820
0
  elf_hash_table (info)->data_index_section = found;
7821
7822
0
  for (s = output_bfd->sections; s != NULL; s = s->next)
7823
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7824
0
  && (s->flags & SEC_READONLY)
7825
0
  && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7826
0
      {
7827
0
  found = s;
7828
0
  break;
7829
0
      }
7830
0
  elf_hash_table (info)->text_index_section = found;
7831
0
}
7832
7833
#define GNU_HASH_SECTION_NAME(bed)          \
7834
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7835
7836
bool
7837
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7838
0
{
7839
0
  const struct elf_backend_data *bed;
7840
0
  unsigned long section_sym_count;
7841
0
  bfd_size_type dynsymcount = 0;
7842
7843
0
  if (!is_elf_hash_table (info->hash))
7844
0
    return true;
7845
7846
0
  bed = get_elf_backend_data (output_bfd);
7847
0
  (*bed->elf_backend_init_index_section) (output_bfd, info);
7848
7849
  /* Assign dynsym indices.  In a shared library we generate a section
7850
     symbol for each output section, which come first.  Next come all
7851
     of the back-end allocated local dynamic syms, followed by the rest
7852
     of the global symbols.
7853
7854
     This is usually not needed for static binaries, however backends
7855
     can request to always do it, e.g. the MIPS backend uses dynamic
7856
     symbol counts to lay out GOT, which will be produced in the
7857
     presence of GOT relocations even in static binaries (holding fixed
7858
     data in that case, to satisfy those relocations).  */
7859
7860
0
  if (elf_hash_table (info)->dynamic_sections_created
7861
0
      || bed->always_renumber_dynsyms)
7862
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7863
0
              &section_sym_count);
7864
7865
0
  if (elf_hash_table (info)->dynamic_sections_created)
7866
0
    {
7867
0
      bfd *dynobj;
7868
0
      asection *s;
7869
0
      unsigned int dtagcount;
7870
7871
0
      dynobj = elf_hash_table (info)->dynobj;
7872
7873
      /* Work out the size of the symbol version section.  */
7874
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7875
0
      BFD_ASSERT (s != NULL);
7876
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7877
0
  {
7878
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7879
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7880
0
    if (s->contents == NULL)
7881
0
      return false;
7882
0
    s->alloced = 1;
7883
7884
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7885
0
      return false;
7886
0
  }
7887
7888
      /* Set the size of the .dynsym and .hash sections.  We counted
7889
   the number of dynamic symbols in elf_link_add_object_symbols.
7890
   We will build the contents of .dynsym and .hash when we build
7891
   the final symbol table, because until then we do not know the
7892
   correct value to give the symbols.  We built the .dynstr
7893
   section as we went along in elf_link_add_object_symbols.  */
7894
0
      s = elf_hash_table (info)->dynsym;
7895
0
      BFD_ASSERT (s != NULL);
7896
0
      s->size = dynsymcount * bed->s->sizeof_sym;
7897
7898
0
      s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7899
0
      if (s->contents == NULL)
7900
0
  return false;
7901
0
      s->alloced = 1;
7902
7903
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7904
   section syms, in case we don't output them all.  */
7905
0
      ++section_sym_count;
7906
0
      memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7907
7908
0
      elf_hash_table (info)->bucketcount = 0;
7909
7910
      /* Compute the size of the hashing table.  As a side effect this
7911
   computes the hash values for all the names we export.  */
7912
0
      if (info->emit_hash)
7913
0
  {
7914
0
    unsigned long int *hashcodes;
7915
0
    struct hash_codes_info hashinf;
7916
0
    bfd_size_type amt;
7917
0
    unsigned long int nsyms;
7918
0
    size_t bucketcount;
7919
0
    size_t hash_entry_size;
7920
7921
    /* Compute the hash values for all exported symbols.  At the same
7922
       time store the values in an array so that we could use them for
7923
       optimizations.  */
7924
0
    amt = dynsymcount * sizeof (unsigned long int);
7925
0
    hashcodes = (unsigned long int *) bfd_malloc (amt);
7926
0
    if (hashcodes == NULL)
7927
0
      return false;
7928
0
    hashinf.hashcodes = hashcodes;
7929
0
    hashinf.error = false;
7930
7931
    /* Put all hash values in HASHCODES.  */
7932
0
    elf_link_hash_traverse (elf_hash_table (info),
7933
0
          elf_collect_hash_codes, &hashinf);
7934
0
    if (hashinf.error)
7935
0
      {
7936
0
        free (hashcodes);
7937
0
        return false;
7938
0
      }
7939
7940
0
    nsyms = hashinf.hashcodes - hashcodes;
7941
0
    bucketcount
7942
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7943
0
    free (hashcodes);
7944
7945
0
    if (bucketcount == 0 && nsyms > 0)
7946
0
      return false;
7947
7948
0
    elf_hash_table (info)->bucketcount = bucketcount;
7949
7950
0
    s = bfd_get_linker_section (dynobj, ".hash");
7951
0
    BFD_ASSERT (s != NULL);
7952
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7953
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7954
0
    s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7955
0
    if (s->contents == NULL)
7956
0
      return false;
7957
0
    s->alloced = 1;
7958
7959
0
    bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7960
0
    bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7961
0
       s->contents + hash_entry_size);
7962
0
  }
7963
7964
0
      if (info->emit_gnu_hash)
7965
0
  {
7966
0
    size_t i, cnt;
7967
0
    unsigned char *contents;
7968
0
    struct collect_gnu_hash_codes cinfo;
7969
0
    bfd_size_type amt;
7970
0
    size_t bucketcount;
7971
7972
0
    memset (&cinfo, 0, sizeof (cinfo));
7973
7974
    /* Compute the hash values for all exported symbols.  At the same
7975
       time store the values in an array so that we could use them for
7976
       optimizations.  */
7977
0
    amt = dynsymcount * 2 * sizeof (unsigned long int);
7978
0
    cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7979
0
    if (cinfo.hashcodes == NULL)
7980
0
      return false;
7981
7982
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
7983
0
    cinfo.min_dynindx = -1;
7984
0
    cinfo.output_bfd = output_bfd;
7985
0
    cinfo.bed = bed;
7986
7987
    /* Put all hash values in HASHCODES.  */
7988
0
    elf_link_hash_traverse (elf_hash_table (info),
7989
0
          elf_collect_gnu_hash_codes, &cinfo);
7990
0
    if (cinfo.error)
7991
0
      {
7992
0
        free (cinfo.hashcodes);
7993
0
        return false;
7994
0
      }
7995
7996
0
    bucketcount
7997
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7998
7999
0
    if (bucketcount == 0)
8000
0
      {
8001
0
        free (cinfo.hashcodes);
8002
0
        return false;
8003
0
      }
8004
8005
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
8006
0
    BFD_ASSERT (s != NULL);
8007
8008
0
    if (cinfo.nsyms == 0)
8009
0
      {
8010
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8011
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8012
0
        free (cinfo.hashcodes);
8013
0
        s->size = 5 * 4 + bed->s->arch_size / 8;
8014
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8015
0
        if (contents == NULL)
8016
0
    return false;
8017
0
        s->contents = contents;
8018
0
        s->alloced = 1;
8019
        /* 1 empty bucket.  */
8020
0
        bfd_put_32 (output_bfd, 1, contents);
8021
        /* SYMIDX above the special symbol 0.  */
8022
0
        bfd_put_32 (output_bfd, 1, contents + 4);
8023
        /* Just one word for bitmask.  */
8024
0
        bfd_put_32 (output_bfd, 1, contents + 8);
8025
        /* Only hash fn bloom filter.  */
8026
0
        bfd_put_32 (output_bfd, 0, contents + 12);
8027
        /* No hashes are valid - empty bitmask.  */
8028
0
        bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
8029
        /* No hashes in the only bucket.  */
8030
0
        bfd_put_32 (output_bfd, 0,
8031
0
        contents + 16 + bed->s->arch_size / 8);
8032
0
      }
8033
0
    else
8034
0
      {
8035
0
        unsigned long int maskwords, maskbitslog2, x;
8036
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8037
8038
0
        x = cinfo.nsyms;
8039
0
        maskbitslog2 = 1;
8040
0
        while ((x >>= 1) != 0)
8041
0
    ++maskbitslog2;
8042
0
        if (maskbitslog2 < 3)
8043
0
    maskbitslog2 = 5;
8044
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8045
0
    maskbitslog2 = maskbitslog2 + 3;
8046
0
        else
8047
0
    maskbitslog2 = maskbitslog2 + 2;
8048
0
        if (bed->s->arch_size == 64)
8049
0
    {
8050
0
      if (maskbitslog2 == 5)
8051
0
        maskbitslog2 = 6;
8052
0
      cinfo.shift1 = 6;
8053
0
    }
8054
0
        else
8055
0
    cinfo.shift1 = 5;
8056
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8057
0
        cinfo.shift2 = maskbitslog2;
8058
0
        cinfo.maskbits = 1 << maskbitslog2;
8059
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8060
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8061
0
        amt += maskwords * sizeof (bfd_vma);
8062
0
        cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8063
0
        if (cinfo.bitmask == NULL)
8064
0
    {
8065
0
      free (cinfo.hashcodes);
8066
0
      return false;
8067
0
    }
8068
8069
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8070
0
        cinfo.indx = cinfo.counts + bucketcount;
8071
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8072
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8073
8074
        /* Determine how often each hash bucket is used.  */
8075
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8076
0
        for (i = 0; i < cinfo.nsyms; ++i)
8077
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8078
8079
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8080
0
    if (cinfo.counts[i] != 0)
8081
0
      {
8082
0
        cinfo.indx[i] = cnt;
8083
0
        cnt += cinfo.counts[i];
8084
0
      }
8085
0
        BFD_ASSERT (cnt == dynsymcount);
8086
0
        cinfo.bucketcount = bucketcount;
8087
0
        cinfo.local_indx = cinfo.min_dynindx;
8088
8089
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8090
0
        s->size += cinfo.maskbits / 8;
8091
0
        if (bed->record_xhash_symbol != NULL)
8092
0
    s->size += cinfo.nsyms * 4;
8093
0
        contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8094
0
        if (contents == NULL)
8095
0
    {
8096
0
      free (cinfo.bitmask);
8097
0
      free (cinfo.hashcodes);
8098
0
      return false;
8099
0
    }
8100
8101
0
        s->contents = contents;
8102
0
        s->alloced = 1;
8103
0
        bfd_put_32 (output_bfd, bucketcount, contents);
8104
0
        bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8105
0
        bfd_put_32 (output_bfd, maskwords, contents + 8);
8106
0
        bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8107
0
        contents += 16 + cinfo.maskbits / 8;
8108
8109
0
        for (i = 0; i < bucketcount; ++i)
8110
0
    {
8111
0
      if (cinfo.counts[i] == 0)
8112
0
        bfd_put_32 (output_bfd, 0, contents);
8113
0
      else
8114
0
        bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8115
0
      contents += 4;
8116
0
    }
8117
8118
0
        cinfo.contents = contents;
8119
8120
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8121
        /* Renumber dynamic symbols, if populating .gnu.hash section.
8122
     If using .MIPS.xhash, populate the translation table.  */
8123
0
        elf_link_hash_traverse (elf_hash_table (info),
8124
0
              elf_gnu_hash_process_symidx, &cinfo);
8125
8126
0
        contents = s->contents + 16;
8127
0
        for (i = 0; i < maskwords; ++i)
8128
0
    {
8129
0
      bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8130
0
         contents);
8131
0
      contents += bed->s->arch_size / 8;
8132
0
    }
8133
8134
0
        free (cinfo.bitmask);
8135
0
        free (cinfo.hashcodes);
8136
0
      }
8137
0
  }
8138
8139
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8140
0
      BFD_ASSERT (s != NULL);
8141
8142
0
      elf_finalize_dynstr (output_bfd, info);
8143
8144
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8145
8146
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8147
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8148
0
    return false;
8149
0
    }
8150
8151
0
  return true;
8152
0
}
8153

8154
/* Make sure sec_info_type is cleared if sec_info is cleared too.  */
8155
8156
static void
8157
merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8158
          asection *sec)
8159
0
{
8160
0
  BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8161
0
  sec->sec_info_type = SEC_INFO_TYPE_NONE;
8162
0
}
8163
8164
/* Finish SHF_MERGE section merging.  */
8165
8166
bool
8167
_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8168
0
{
8169
0
  bfd *ibfd;
8170
0
  asection *sec;
8171
8172
0
  if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
8173
0
    abort ();
8174
8175
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8176
0
    if ((ibfd->flags & DYNAMIC) == 0
8177
0
  && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8178
0
  && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8179
0
      == get_elf_backend_data (obfd)->s->elfclass))
8180
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8181
0
  if ((sec->flags & SEC_MERGE) != 0
8182
0
      && !bfd_is_abs_section (sec->output_section))
8183
0
    {
8184
0
      struct bfd_elf_section_data *secdata;
8185
8186
0
      secdata = elf_section_data (sec);
8187
0
      if (! _bfd_add_merge_section (obfd,
8188
0
            &elf_hash_table (info)->merge_info,
8189
0
            sec, &secdata->sec_info))
8190
0
        return false;
8191
0
      else if (secdata->sec_info)
8192
0
        sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8193
0
    }
8194
8195
0
  if (elf_hash_table (info)->merge_info != NULL)
8196
0
    return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8197
0
        merge_sections_remove_hook);
8198
0
  return true;
8199
0
}
8200
8201
/* Create an entry in an ELF linker hash table.  */
8202
8203
struct bfd_hash_entry *
8204
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8205
          struct bfd_hash_table *table,
8206
          const char *string)
8207
0
{
8208
  /* Allocate the structure if it has not already been allocated by a
8209
     subclass.  */
8210
0
  if (entry == NULL)
8211
0
    {
8212
0
      entry = (struct bfd_hash_entry *)
8213
0
  bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8214
0
      if (entry == NULL)
8215
0
  return entry;
8216
0
    }
8217
8218
  /* Call the allocation method of the superclass.  */
8219
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8220
0
  if (entry != NULL)
8221
0
    {
8222
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8223
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8224
8225
      /* Set local fields.  */
8226
0
      ret->indx = -1;
8227
0
      ret->dynindx = -1;
8228
0
      ret->got = htab->init_got_refcount;
8229
0
      ret->plt = htab->init_plt_refcount;
8230
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8231
0
            - offsetof (struct elf_link_hash_entry, size)));
8232
      /* Assume that we have been called by a non-ELF symbol reader.
8233
   This flag is then reset by the code which reads an ELF input
8234
   file.  This ensures that a symbol created by a non-ELF symbol
8235
   reader will have the flag set correctly.  */
8236
0
      ret->non_elf = 1;
8237
0
    }
8238
8239
0
  return entry;
8240
0
}
8241
8242
/* Copy data from an indirect symbol to its direct symbol, hiding the
8243
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8244
8245
void
8246
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8247
          struct elf_link_hash_entry *dir,
8248
          struct elf_link_hash_entry *ind)
8249
0
{
8250
0
  struct elf_link_hash_table *htab;
8251
8252
0
  if (ind->dyn_relocs != NULL)
8253
0
    {
8254
0
      if (dir->dyn_relocs != NULL)
8255
0
  {
8256
0
    struct elf_dyn_relocs **pp;
8257
0
    struct elf_dyn_relocs *p;
8258
8259
    /* Add reloc counts against the indirect sym to the direct sym
8260
       list.  Merge any entries against the same section.  */
8261
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8262
0
      {
8263
0
        struct elf_dyn_relocs *q;
8264
8265
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8266
0
    if (q->sec == p->sec)
8267
0
      {
8268
0
        q->pc_count += p->pc_count;
8269
0
        q->count += p->count;
8270
0
        *pp = p->next;
8271
0
        break;
8272
0
      }
8273
0
        if (q == NULL)
8274
0
    pp = &p->next;
8275
0
      }
8276
0
    *pp = dir->dyn_relocs;
8277
0
  }
8278
8279
0
      dir->dyn_relocs = ind->dyn_relocs;
8280
0
      ind->dyn_relocs = NULL;
8281
0
    }
8282
8283
  /* Copy down any references that we may have already seen to the
8284
     symbol which just became indirect.  */
8285
8286
0
  if (dir->versioned != versioned_hidden)
8287
0
    dir->ref_dynamic |= ind->ref_dynamic;
8288
0
  dir->ref_regular |= ind->ref_regular;
8289
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8290
0
  dir->non_got_ref |= ind->non_got_ref;
8291
0
  dir->needs_plt |= ind->needs_plt;
8292
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8293
8294
0
  if (ind->root.type != bfd_link_hash_indirect)
8295
0
    return;
8296
8297
  /* Copy over the global and procedure linkage table refcount entries.
8298
     These may have been already set up by a check_relocs routine.  */
8299
0
  htab = elf_hash_table (info);
8300
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8301
0
    {
8302
0
      if (dir->got.refcount < 0)
8303
0
  dir->got.refcount = 0;
8304
0
      dir->got.refcount += ind->got.refcount;
8305
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8306
0
    }
8307
8308
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8309
0
    {
8310
0
      if (dir->plt.refcount < 0)
8311
0
  dir->plt.refcount = 0;
8312
0
      dir->plt.refcount += ind->plt.refcount;
8313
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8314
0
    }
8315
8316
0
  if (ind->dynindx != -1)
8317
0
    {
8318
0
      if (dir->dynindx != -1)
8319
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8320
0
      dir->dynindx = ind->dynindx;
8321
0
      dir->dynstr_index = ind->dynstr_index;
8322
0
      ind->dynindx = -1;
8323
0
      ind->dynstr_index = 0;
8324
0
    }
8325
0
}
8326
8327
void
8328
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8329
        struct elf_link_hash_entry *h,
8330
        bool force_local)
8331
0
{
8332
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8333
0
  if (h->type != STT_GNU_IFUNC)
8334
0
    {
8335
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8336
0
      h->needs_plt = 0;
8337
0
    }
8338
0
  if (force_local)
8339
0
    {
8340
0
      h->forced_local = 1;
8341
0
      if (h->dynindx != -1)
8342
0
  {
8343
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8344
0
          h->dynstr_index);
8345
0
    h->dynindx = -1;
8346
0
    h->dynstr_index = 0;
8347
0
  }
8348
0
    }
8349
0
}
8350
8351
/* Hide a symbol. */
8352
8353
void
8354
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8355
         struct bfd_link_info *info,
8356
         struct bfd_link_hash_entry *h)
8357
0
{
8358
0
  if (is_elf_hash_table (info->hash))
8359
0
    {
8360
0
      const struct elf_backend_data *bed
8361
0
  = get_elf_backend_data (output_bfd);
8362
0
      struct elf_link_hash_entry *eh
8363
0
  = (struct elf_link_hash_entry *) h;
8364
0
      bed->elf_backend_hide_symbol (info, eh, true);
8365
0
      eh->def_dynamic = 0;
8366
0
      eh->ref_dynamic = 0;
8367
0
      eh->dynamic_def = 0;
8368
0
    }
8369
0
}
8370
8371
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8372
   caller.  */
8373
8374
bool
8375
_bfd_elf_link_hash_table_init
8376
  (struct elf_link_hash_table *table,
8377
   bfd *abfd,
8378
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8379
              struct bfd_hash_table *,
8380
              const char *),
8381
   unsigned int entsize)
8382
0
{
8383
0
  bool ret;
8384
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8385
0
  int can_refcount = bed->can_refcount;
8386
8387
0
  table->init_got_refcount.refcount = can_refcount - 1;
8388
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8389
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8390
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8391
  /* The first dynamic symbol is a dummy.  */
8392
0
  table->dynsymcount = 1;
8393
8394
0
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8395
8396
0
  table->root.type = bfd_link_elf_hash_table;
8397
0
  table->hash_table_id = bed->target_id;
8398
0
  table->target_os = bed->target_os;
8399
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8400
8401
0
  return ret;
8402
0
}
8403
8404
/* Create an ELF linker hash table.  */
8405
8406
struct bfd_link_hash_table *
8407
_bfd_elf_link_hash_table_create (bfd *abfd)
8408
0
{
8409
0
  struct elf_link_hash_table *ret;
8410
0
  size_t amt = sizeof (struct elf_link_hash_table);
8411
8412
0
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8413
0
  if (ret == NULL)
8414
0
    return NULL;
8415
8416
0
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8417
0
               sizeof (struct elf_link_hash_entry)))
8418
0
    {
8419
0
      free (ret);
8420
0
      return NULL;
8421
0
    }
8422
8423
0
  return &ret->root;
8424
0
}
8425
8426
/* Destroy an ELF linker hash table.  */
8427
8428
void
8429
_bfd_elf_link_hash_table_free (bfd *obfd)
8430
0
{
8431
0
  struct elf_link_hash_table *htab;
8432
8433
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8434
0
  if (htab->dynstr != NULL)
8435
0
    _bfd_elf_strtab_free (htab->dynstr);
8436
0
  _bfd_merge_sections_free (htab->merge_info);
8437
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8438
0
  if (htab->dynamic != NULL)
8439
0
    {
8440
0
      free (htab->dynamic->contents);
8441
0
      htab->dynamic->contents = NULL;
8442
0
    }
8443
0
  if (htab->first_hash != NULL)
8444
0
    {
8445
0
      bfd_hash_table_free (htab->first_hash);
8446
0
      free (htab->first_hash);
8447
0
    }
8448
0
  if (htab->eh_info.frame_hdr_is_compact)
8449
0
    free (htab->eh_info.u.compact.entries);
8450
0
  else
8451
0
    free (htab->eh_info.u.dwarf.array);
8452
0
  _bfd_generic_link_hash_table_free (obfd);
8453
0
}
8454
8455
/* This is a hook for the ELF emulation code in the generic linker to
8456
   tell the backend linker what file name to use for the DT_NEEDED
8457
   entry for a dynamic object.  */
8458
8459
void
8460
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8461
0
{
8462
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8463
0
      && bfd_get_format (abfd) == bfd_object)
8464
0
    elf_dt_name (abfd) = name;
8465
0
}
8466
8467
int
8468
bfd_elf_get_dyn_lib_class (bfd *abfd)
8469
0
{
8470
0
  int lib_class;
8471
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8472
0
      && bfd_get_format (abfd) == bfd_object)
8473
0
    lib_class = elf_dyn_lib_class (abfd);
8474
0
  else
8475
0
    lib_class = 0;
8476
0
  return lib_class;
8477
0
}
8478
8479
void
8480
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8481
0
{
8482
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8483
0
      && bfd_get_format (abfd) == bfd_object)
8484
0
    elf_dyn_lib_class (abfd) = lib_class;
8485
0
}
8486
8487
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8488
   the linker ELF emulation code.  */
8489
8490
struct bfd_link_needed_list *
8491
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8492
       struct bfd_link_info *info)
8493
0
{
8494
0
  if (! is_elf_hash_table (info->hash))
8495
0
    return NULL;
8496
0
  return elf_hash_table (info)->needed;
8497
0
}
8498
8499
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8500
   hook for the linker ELF emulation code.  */
8501
8502
struct bfd_link_needed_list *
8503
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8504
        struct bfd_link_info *info)
8505
0
{
8506
0
  if (! is_elf_hash_table (info->hash))
8507
0
    return NULL;
8508
0
  return elf_hash_table (info)->runpath;
8509
0
}
8510
8511
/* Get the name actually used for a dynamic object for a link.  This
8512
   is the SONAME entry if there is one.  Otherwise, it is the string
8513
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8514
8515
const char *
8516
bfd_elf_get_dt_soname (bfd *abfd)
8517
0
{
8518
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8519
0
      && bfd_get_format (abfd) == bfd_object)
8520
0
    return elf_dt_name (abfd);
8521
0
  return NULL;
8522
0
}
8523
8524
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8525
   the ELF linker emulation code.  */
8526
8527
bool
8528
bfd_elf_get_bfd_needed_list (bfd *abfd,
8529
           struct bfd_link_needed_list **pneeded)
8530
0
{
8531
0
  asection *s;
8532
0
  bfd_byte *dynbuf = NULL;
8533
0
  unsigned int elfsec;
8534
0
  unsigned long shlink;
8535
0
  bfd_byte *extdyn, *extdynend;
8536
0
  size_t extdynsize;
8537
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8538
8539
0
  *pneeded = NULL;
8540
8541
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8542
0
      || bfd_get_format (abfd) != bfd_object)
8543
0
    return true;
8544
8545
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8546
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8547
0
    return true;
8548
8549
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8550
0
    goto error_return;
8551
8552
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8553
0
  if (elfsec == SHN_BAD)
8554
0
    goto error_return;
8555
8556
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8557
8558
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8559
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8560
8561
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8562
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8563
0
       extdyn += extdynsize)
8564
0
    {
8565
0
      Elf_Internal_Dyn dyn;
8566
8567
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8568
8569
0
      if (dyn.d_tag == DT_NULL)
8570
0
  break;
8571
8572
0
      if (dyn.d_tag == DT_NEEDED)
8573
0
  {
8574
0
    const char *string;
8575
0
    struct bfd_link_needed_list *l;
8576
0
    unsigned int tagv = dyn.d_un.d_val;
8577
0
    size_t amt;
8578
8579
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8580
0
    if (string == NULL)
8581
0
      goto error_return;
8582
8583
0
    amt = sizeof *l;
8584
0
    l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8585
0
    if (l == NULL)
8586
0
      goto error_return;
8587
8588
0
    l->by = abfd;
8589
0
    l->name = string;
8590
0
    l->next = *pneeded;
8591
0
    *pneeded = l;
8592
0
  }
8593
0
    }
8594
8595
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8596
8597
0
  return true;
8598
8599
0
 error_return:
8600
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8601
0
  return false;
8602
0
}
8603
8604
struct elf_symbuf_symbol
8605
{
8606
  unsigned long st_name;  /* Symbol name, index in string tbl */
8607
  unsigned char st_info;  /* Type and binding attributes */
8608
  unsigned char st_other; /* Visibilty, and target specific */
8609
};
8610
8611
struct elf_symbuf_head
8612
{
8613
  struct elf_symbuf_symbol *ssym;
8614
  size_t count;
8615
  unsigned int st_shndx;
8616
};
8617
8618
struct elf_symbol
8619
{
8620
  union
8621
    {
8622
      Elf_Internal_Sym *isym;
8623
      struct elf_symbuf_symbol *ssym;
8624
      void *p;
8625
    } u;
8626
  const char *name;
8627
};
8628
8629
/* Sort references to symbols by ascending section number.  */
8630
8631
static int
8632
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8633
0
{
8634
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8635
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8636
8637
0
  if (s1->st_shndx != s2->st_shndx)
8638
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8639
  /* Final sort by the address of the sym in the symbuf ensures
8640
     a stable sort.  */
8641
0
  if (s1 != s2)
8642
0
    return s1 > s2 ? 1 : -1;
8643
0
  return 0;
8644
0
}
8645
8646
static int
8647
elf_sym_name_compare (const void *arg1, const void *arg2)
8648
0
{
8649
0
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8650
0
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8651
0
  int ret = strcmp (s1->name, s2->name);
8652
0
  if (ret != 0)
8653
0
    return ret;
8654
0
  if (s1->u.p != s2->u.p)
8655
0
    return s1->u.p > s2->u.p ? 1 : -1;
8656
0
  return 0;
8657
0
}
8658
8659
static struct elf_symbuf_head *
8660
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8661
0
{
8662
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8663
0
  struct elf_symbuf_symbol *ssym;
8664
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8665
0
  size_t i, shndx_count, total_size, amt;
8666
8667
0
  amt = symcount * sizeof (*indbuf);
8668
0
  indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8669
0
  if (indbuf == NULL)
8670
0
    return NULL;
8671
8672
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8673
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8674
0
      *ind++ = &isymbuf[i];
8675
0
  indbufend = ind;
8676
8677
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8678
0
   elf_sort_elf_symbol);
8679
8680
0
  shndx_count = 0;
8681
0
  if (indbufend > indbuf)
8682
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8683
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8684
0
  shndx_count++;
8685
8686
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8687
0
    + (indbufend - indbuf) * sizeof (*ssym));
8688
0
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8689
0
  if (ssymbuf == NULL)
8690
0
    {
8691
0
      free (indbuf);
8692
0
      return NULL;
8693
0
    }
8694
8695
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8696
0
  ssymbuf->ssym = NULL;
8697
0
  ssymbuf->count = shndx_count;
8698
0
  ssymbuf->st_shndx = 0;
8699
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8700
0
    {
8701
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8702
0
  {
8703
0
    ssymhead++;
8704
0
    ssymhead->ssym = ssym;
8705
0
    ssymhead->count = 0;
8706
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8707
0
  }
8708
0
      ssym->st_name = (*ind)->st_name;
8709
0
      ssym->st_info = (*ind)->st_info;
8710
0
      ssym->st_other = (*ind)->st_other;
8711
0
      ssymhead->count++;
8712
0
    }
8713
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8714
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8715
8716
0
  free (indbuf);
8717
0
  return ssymbuf;
8718
0
}
8719
8720
/* Check if 2 sections define the same set of local and global
8721
   symbols.  */
8722
8723
static bool
8724
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8725
           struct bfd_link_info *info)
8726
0
{
8727
0
  bfd *bfd1, *bfd2;
8728
0
  const struct elf_backend_data *bed1, *bed2;
8729
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8730
0
  size_t symcount1, symcount2;
8731
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8732
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8733
0
  Elf_Internal_Sym *isym, *isymend;
8734
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8735
0
  size_t count1, count2, sec_count1, sec_count2, i;
8736
0
  unsigned int shndx1, shndx2;
8737
0
  bool result;
8738
0
  bool ignore_section_symbol_p;
8739
8740
0
  bfd1 = sec1->owner;
8741
0
  bfd2 = sec2->owner;
8742
8743
  /* Both sections have to be in ELF.  */
8744
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8745
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8746
0
    return false;
8747
8748
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8749
0
    return false;
8750
8751
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8752
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8753
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8754
0
    return false;
8755
8756
0
  bed1 = get_elf_backend_data (bfd1);
8757
0
  bed2 = get_elf_backend_data (bfd2);
8758
0
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8759
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8760
0
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8761
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8762
8763
0
  if (symcount1 == 0 || symcount2 == 0)
8764
0
    return false;
8765
8766
0
  result = false;
8767
0
  isymbuf1 = NULL;
8768
0
  isymbuf2 = NULL;
8769
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8770
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8771
8772
  /* Ignore section symbols only when matching non-debugging sections
8773
     or linkonce section with comdat section.  */
8774
0
  ignore_section_symbol_p
8775
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8776
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8777
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8778
8779
0
  if (ssymbuf1 == NULL)
8780
0
    {
8781
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8782
0
               NULL, NULL, NULL);
8783
0
      if (isymbuf1 == NULL)
8784
0
  goto done;
8785
8786
0
      if (info != NULL && !info->reduce_memory_overheads)
8787
0
  {
8788
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8789
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8790
0
  }
8791
0
    }
8792
8793
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8794
0
    {
8795
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8796
0
               NULL, NULL, NULL);
8797
0
      if (isymbuf2 == NULL)
8798
0
  goto done;
8799
8800
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8801
0
  {
8802
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8803
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8804
0
  }
8805
0
    }
8806
8807
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8808
0
    {
8809
      /* Optimized faster version.  */
8810
0
      size_t lo, hi, mid;
8811
0
      struct elf_symbol *symp;
8812
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8813
8814
0
      lo = 0;
8815
0
      hi = ssymbuf1->count;
8816
0
      ssymbuf1++;
8817
0
      count1 = 0;
8818
0
      sec_count1 = 0;
8819
0
      while (lo < hi)
8820
0
  {
8821
0
    mid = (lo + hi) / 2;
8822
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8823
0
      hi = mid;
8824
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8825
0
      lo = mid + 1;
8826
0
    else
8827
0
      {
8828
0
        count1 = ssymbuf1[mid].count;
8829
0
        ssymbuf1 += mid;
8830
0
        break;
8831
0
      }
8832
0
  }
8833
0
      if (ignore_section_symbol_p)
8834
0
  {
8835
0
    for (i = 0; i < count1; i++)
8836
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8837
0
        sec_count1++;
8838
0
    count1 -= sec_count1;
8839
0
  }
8840
8841
0
      lo = 0;
8842
0
      hi = ssymbuf2->count;
8843
0
      ssymbuf2++;
8844
0
      count2 = 0;
8845
0
      sec_count2 = 0;
8846
0
      while (lo < hi)
8847
0
  {
8848
0
    mid = (lo + hi) / 2;
8849
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8850
0
      hi = mid;
8851
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8852
0
      lo = mid + 1;
8853
0
    else
8854
0
      {
8855
0
        count2 = ssymbuf2[mid].count;
8856
0
        ssymbuf2 += mid;
8857
0
        break;
8858
0
      }
8859
0
  }
8860
0
      if (ignore_section_symbol_p)
8861
0
  {
8862
0
    for (i = 0; i < count2; i++)
8863
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8864
0
        sec_count2++;
8865
0
    count2 -= sec_count2;
8866
0
  }
8867
8868
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8869
0
  goto done;
8870
8871
0
      symtable1
8872
0
  = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8873
0
      symtable2
8874
0
  = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8875
0
      if (symtable1 == NULL || symtable2 == NULL)
8876
0
  goto done;
8877
8878
0
      symp = symtable1;
8879
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8880
0
     ssym < ssymend; ssym++)
8881
0
  if (sec_count1 == 0
8882
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8883
0
    {
8884
0
      symp->u.ssym = ssym;
8885
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8886
0
                hdr1->sh_link,
8887
0
                ssym->st_name);
8888
0
      if (symp->name == NULL)
8889
0
        goto done;
8890
0
      symp++;
8891
0
    }
8892
8893
0
      symp = symtable2;
8894
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8895
0
     ssym < ssymend; ssym++)
8896
0
  if (sec_count2 == 0
8897
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8898
0
    {
8899
0
      symp->u.ssym = ssym;
8900
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8901
0
                hdr2->sh_link,
8902
0
                ssym->st_name);
8903
0
      if (symp->name == NULL)
8904
0
        goto done;
8905
0
      symp++;
8906
0
    }
8907
8908
      /* Sort symbol by name.  */
8909
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8910
0
       elf_sym_name_compare);
8911
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8912
0
       elf_sym_name_compare);
8913
8914
0
      for (i = 0; i < count1; i++)
8915
  /* Two symbols must have the same binding, type and name.  */
8916
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8917
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8918
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8919
0
    goto done;
8920
8921
0
      result = true;
8922
0
      goto done;
8923
0
    }
8924
8925
0
  symtable1 = (struct elf_symbol *)
8926
0
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8927
0
  symtable2 = (struct elf_symbol *)
8928
0
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8929
0
  if (symtable1 == NULL || symtable2 == NULL)
8930
0
    goto done;
8931
8932
  /* Count definitions in the section.  */
8933
0
  count1 = 0;
8934
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8935
0
    if (isym->st_shndx == shndx1
8936
0
  && (!ignore_section_symbol_p
8937
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8938
0
      symtable1[count1++].u.isym = isym;
8939
8940
0
  count2 = 0;
8941
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8942
0
    if (isym->st_shndx == shndx2
8943
0
  && (!ignore_section_symbol_p
8944
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8945
0
      symtable2[count2++].u.isym = isym;
8946
8947
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8948
0
    goto done;
8949
8950
0
  for (i = 0; i < count1; i++)
8951
0
    {
8952
0
      symtable1[i].name
8953
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8954
0
             symtable1[i].u.isym->st_name);
8955
0
      if (symtable1[i].name == NULL)
8956
0
  goto done;
8957
0
    }
8958
8959
0
  for (i = 0; i < count2; i++)
8960
0
    {
8961
0
      symtable2[i].name
8962
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8963
0
             symtable2[i].u.isym->st_name);
8964
0
      if (symtable2[i].name == NULL)
8965
0
  goto done;
8966
0
    }
8967
8968
  /* Sort symbol by name.  */
8969
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8970
0
   elf_sym_name_compare);
8971
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8972
0
   elf_sym_name_compare);
8973
8974
0
  for (i = 0; i < count1; i++)
8975
    /* Two symbols must have the same binding, type and name.  */
8976
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8977
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8978
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8979
0
      goto done;
8980
8981
0
  result = true;
8982
8983
0
 done:
8984
0
  free (symtable1);
8985
0
  free (symtable2);
8986
0
  free (isymbuf1);
8987
0
  free (isymbuf2);
8988
8989
0
  return result;
8990
0
}
8991
8992
/* Return TRUE if 2 section types are compatible.  */
8993
8994
bool
8995
_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8996
         bfd *bbfd, const asection *bsec)
8997
0
{
8998
0
  if (asec == NULL
8999
0
      || bsec == NULL
9000
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9001
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9002
0
    return true;
9003
9004
0
  return elf_section_type (asec) == elf_section_type (bsec);
9005
0
}
9006

9007
/* Final phase of ELF linker.  */
9008
9009
/* A structure we use to avoid passing large numbers of arguments.  */
9010
9011
struct elf_final_link_info
9012
{
9013
  /* General link information.  */
9014
  struct bfd_link_info *info;
9015
  /* Output BFD.  */
9016
  bfd *output_bfd;
9017
  /* Symbol string table.  */
9018
  struct elf_strtab_hash *symstrtab;
9019
  /* .hash section.  */
9020
  asection *hash_sec;
9021
  /* symbol version section (.gnu.version).  */
9022
  asection *symver_sec;
9023
  /* Buffer large enough to hold contents of any section.  */
9024
  bfd_byte *contents;
9025
  /* Buffer large enough to hold external relocs of any section.  */
9026
  void *external_relocs;
9027
  /* Buffer large enough to hold internal relocs of any section.  */
9028
  Elf_Internal_Rela *internal_relocs;
9029
  /* Buffer large enough to hold external local symbols of any input
9030
     BFD.  */
9031
  bfd_byte *external_syms;
9032
  /* And a buffer for symbol section indices.  */
9033
  Elf_External_Sym_Shndx *locsym_shndx;
9034
  /* Buffer large enough to hold internal local symbols of any input
9035
     BFD.  */
9036
  Elf_Internal_Sym *internal_syms;
9037
  /* Array large enough to hold a symbol index for each local symbol
9038
     of any input BFD.  */
9039
  long *indices;
9040
  /* Array large enough to hold a section pointer for each local
9041
     symbol of any input BFD.  */
9042
  asection **sections;
9043
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9044
  Elf_External_Sym_Shndx *symshndxbuf;
9045
  /* Number of STT_FILE syms seen.  */
9046
  size_t filesym_count;
9047
  /* Local symbol hash table.  */
9048
  struct bfd_hash_table local_hash_table;
9049
};
9050
9051
struct local_hash_entry
9052
{
9053
  /* Base hash table entry structure.  */
9054
  struct bfd_hash_entry root;
9055
  /* Size of the local symbol name.  */
9056
  size_t size;
9057
  /* Number of the duplicated local symbol names.  */
9058
  long count;
9059
};
9060
9061
/* Create an entry in the local symbol hash table.  */
9062
9063
static struct bfd_hash_entry *
9064
local_hash_newfunc (struct bfd_hash_entry *entry,
9065
        struct bfd_hash_table *table,
9066
        const char *string)
9067
0
{
9068
9069
  /* Allocate the structure if it has not already been allocated by a
9070
     subclass.  */
9071
0
  if (entry == NULL)
9072
0
    {
9073
0
      entry = bfd_hash_allocate (table,
9074
0
         sizeof (struct local_hash_entry));
9075
0
      if (entry == NULL)
9076
0
        return entry;
9077
0
    }
9078
9079
  /* Call the allocation method of the superclass.  */
9080
0
  entry = bfd_hash_newfunc (entry, table, string);
9081
0
  if (entry != NULL)
9082
0
    {
9083
0
      ((struct local_hash_entry *) entry)->count = 0;
9084
0
      ((struct local_hash_entry *) entry)->size = 0;
9085
0
    }
9086
9087
0
  return entry;
9088
0
}
9089
9090
/* This struct is used to pass information to elf_link_output_extsym.  */
9091
9092
struct elf_outext_info
9093
{
9094
  bool failed;
9095
  bool localsyms;
9096
  bool file_sym_done;
9097
  struct elf_final_link_info *flinfo;
9098
};
9099
9100
9101
/* Support for evaluating a complex relocation.
9102
9103
   Complex relocations are generalized, self-describing relocations.  The
9104
   implementation of them consists of two parts: complex symbols, and the
9105
   relocations themselves.
9106
9107
   The relocations use a reserved elf-wide relocation type code (R_RELC
9108
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9109
   information (start bit, end bit, word width, etc) into the addend.  This
9110
   information is extracted from CGEN-generated operand tables within gas.
9111
9112
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9113
   internal) representing prefix-notation expressions, including but not
9114
   limited to those sorts of expressions normally encoded as addends in the
9115
   addend field.  The symbol mangling format is:
9116
9117
   <node> := <literal>
9118
    |  <unary-operator> ':' <node>
9119
    |  <binary-operator> ':' <node> ':' <node>
9120
    ;
9121
9122
   <literal> := 's' <digits=N> ':' <N character symbol name>
9123
       |  'S' <digits=N> ':' <N character section name>
9124
       |  '#' <hexdigits>
9125
       ;
9126
9127
   <binary-operator> := as in C
9128
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9129
9130
static void
9131
set_symbol_value (bfd *bfd_with_globals,
9132
      Elf_Internal_Sym *isymbuf,
9133
      size_t locsymcount,
9134
      size_t symidx,
9135
      bfd_vma val)
9136
0
{
9137
0
  struct elf_link_hash_entry *h;
9138
0
  size_t extsymoff = locsymcount;
9139
9140
0
  if (symidx < locsymcount)
9141
0
    {
9142
0
      Elf_Internal_Sym *sym;
9143
9144
0
      sym = isymbuf + symidx;
9145
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9146
0
  {
9147
    /* It is a local symbol: move it to the
9148
       "absolute" section and give it a value.  */
9149
0
    sym->st_shndx = SHN_ABS;
9150
0
    sym->st_value = val;
9151
0
    return;
9152
0
  }
9153
0
      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9154
0
      extsymoff = 0;
9155
0
    }
9156
9157
  /* It is a global symbol: set its link type
9158
     to "defined" and give it a value.  */
9159
0
  h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff);
9160
0
  if (h == NULL)
9161
0
    {
9162
      /* FIXMEL What should we do ?  */
9163
0
      return;
9164
0
    }
9165
0
  h->root.type = bfd_link_hash_defined;
9166
0
  h->root.u.def.value = val;
9167
0
  h->root.u.def.section = bfd_abs_section_ptr;
9168
0
}
9169
9170
static bool
9171
resolve_symbol (const char *name,
9172
    bfd *input_bfd,
9173
    struct elf_final_link_info *flinfo,
9174
    bfd_vma *result,
9175
    Elf_Internal_Sym *isymbuf,
9176
    size_t locsymcount)
9177
0
{
9178
0
  Elf_Internal_Sym *sym;
9179
0
  struct bfd_link_hash_entry *global_entry;
9180
0
  const char *candidate = NULL;
9181
0
  Elf_Internal_Shdr *symtab_hdr;
9182
0
  size_t i;
9183
9184
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9185
9186
0
  for (i = 0; i < locsymcount; ++ i)
9187
0
    {
9188
0
      sym = isymbuf + i;
9189
9190
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9191
0
  continue;
9192
9193
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9194
0
               symtab_hdr->sh_link,
9195
0
               sym->st_name);
9196
#ifdef DEBUG
9197
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9198
        name, candidate, (unsigned long) sym->st_value);
9199
#endif
9200
0
      if (candidate && strcmp (candidate, name) == 0)
9201
0
  {
9202
0
    asection *sec = flinfo->sections [i];
9203
9204
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9205
0
    *result += sec->output_offset + sec->output_section->vma;
9206
#ifdef DEBUG
9207
    printf ("Found symbol with value %8.8lx\n",
9208
      (unsigned long) *result);
9209
#endif
9210
0
    return true;
9211
0
  }
9212
0
    }
9213
9214
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9215
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9216
0
               false, false, true);
9217
0
  if (!global_entry)
9218
0
    return false;
9219
9220
0
  if (global_entry->type == bfd_link_hash_defined
9221
0
      || global_entry->type == bfd_link_hash_defweak)
9222
0
    {
9223
0
      *result = (global_entry->u.def.value
9224
0
     + global_entry->u.def.section->output_section->vma
9225
0
     + global_entry->u.def.section->output_offset);
9226
#ifdef DEBUG
9227
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9228
        global_entry->root.string, (unsigned long) *result);
9229
#endif
9230
0
      return true;
9231
0
    }
9232
9233
0
  return false;
9234
0
}
9235
9236
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9237
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9238
   names like "foo.end" which is the end address of section "foo".  */
9239
9240
static bool
9241
resolve_section (const char *name,
9242
     asection *sections,
9243
     bfd_vma *result,
9244
     bfd * abfd)
9245
0
{
9246
0
  asection *curr;
9247
0
  unsigned int len;
9248
9249
0
  for (curr = sections; curr; curr = curr->next)
9250
0
    if (strcmp (curr->name, name) == 0)
9251
0
      {
9252
0
  *result = curr->vma;
9253
0
  return true;
9254
0
      }
9255
9256
  /* Hmm. still haven't found it. try pseudo-section names.  */
9257
  /* FIXME: This could be coded more efficiently...  */
9258
0
  for (curr = sections; curr; curr = curr->next)
9259
0
    {
9260
0
      len = strlen (curr->name);
9261
0
      if (len > strlen (name))
9262
0
  continue;
9263
9264
0
      if (strncmp (curr->name, name, len) == 0)
9265
0
  {
9266
0
    if (startswith (name + len, ".end"))
9267
0
      {
9268
0
        *result = (curr->vma
9269
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9270
0
        return true;
9271
0
      }
9272
9273
    /* Insert more pseudo-section names here, if you like.  */
9274
0
  }
9275
0
    }
9276
9277
0
  return false;
9278
0
}
9279
9280
static void
9281
undefined_reference (const char *reftype, const char *name)
9282
0
{
9283
  /* xgettext:c-format */
9284
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9285
0
          reftype, name);
9286
0
  bfd_set_error (bfd_error_bad_value);
9287
0
}
9288
9289
static bool
9290
eval_symbol (bfd_vma *result,
9291
       const char **symp,
9292
       bfd *input_bfd,
9293
       struct elf_final_link_info *flinfo,
9294
       bfd_vma dot,
9295
       Elf_Internal_Sym *isymbuf,
9296
       size_t locsymcount,
9297
       int signed_p)
9298
0
{
9299
0
  size_t len;
9300
0
  size_t symlen;
9301
0
  bfd_vma a;
9302
0
  bfd_vma b;
9303
0
  char symbuf[4096];
9304
0
  const char *sym = *symp;
9305
0
  const char *symend;
9306
0
  bool symbol_is_section = false;
9307
9308
0
  len = strlen (sym);
9309
0
  symend = sym + len;
9310
9311
0
  if (len < 1 || len > sizeof (symbuf))
9312
0
    {
9313
0
      bfd_set_error (bfd_error_invalid_operation);
9314
0
      return false;
9315
0
    }
9316
9317
0
  switch (* sym)
9318
0
    {
9319
0
    case '.':
9320
0
      *result = dot;
9321
0
      *symp = sym + 1;
9322
0
      return true;
9323
9324
0
    case '#':
9325
0
      ++sym;
9326
0
      *result = strtoul (sym, (char **) symp, 16);
9327
0
      return true;
9328
9329
0
    case 'S':
9330
0
      symbol_is_section = true;
9331
      /* Fall through.  */
9332
0
    case 's':
9333
0
      ++sym;
9334
0
      symlen = strtol (sym, (char **) symp, 10);
9335
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9336
9337
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9338
0
  {
9339
0
    bfd_set_error (bfd_error_invalid_operation);
9340
0
    return false;
9341
0
  }
9342
9343
0
      memcpy (symbuf, sym, symlen);
9344
0
      symbuf[symlen] = '\0';
9345
0
      *symp = sym + symlen;
9346
9347
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9348
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9349
   interpretation here; section means "try section first", not "must be a
9350
   section", and likewise with symbol.  */
9351
9352
0
      if (symbol_is_section)
9353
0
  {
9354
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9355
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9356
0
          isymbuf, locsymcount))
9357
0
      {
9358
0
        undefined_reference ("section", symbuf);
9359
0
        return false;
9360
0
      }
9361
0
  }
9362
0
      else
9363
0
  {
9364
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9365
0
             isymbuf, locsymcount)
9366
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9367
0
           result, input_bfd))
9368
0
      {
9369
0
        undefined_reference ("symbol", symbuf);
9370
0
        return false;
9371
0
      }
9372
0
  }
9373
9374
0
      return true;
9375
9376
      /* All that remains are operators.  */
9377
9378
0
#define UNARY_OP(op)            \
9379
0
  if (startswith (sym, #op))         \
9380
0
    {               \
9381
0
      sym += strlen (#op);          \
9382
0
      if (*sym == ':')           \
9383
0
  ++sym;             \
9384
0
      *symp = sym;            \
9385
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9386
0
      isymbuf, locsymcount, signed_p)) \
9387
0
  return false;           \
9388
0
      if (signed_p)           \
9389
0
  *result = op ((bfd_signed_vma) a);     \
9390
0
      else              \
9391
0
  *result = op a;           \
9392
0
      return true;            \
9393
0
    }
9394
9395
0
#define BINARY_OP_HEAD(op)          \
9396
0
  if (startswith (sym, #op))         \
9397
0
    {               \
9398
0
      sym += strlen (#op);          \
9399
0
      if (*sym == ':')           \
9400
0
  ++sym;             \
9401
0
      *symp = sym;            \
9402
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9403
0
      isymbuf, locsymcount, signed_p)) \
9404
0
  return false;           \
9405
0
      ++*symp;              \
9406
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9407
0
      isymbuf, locsymcount, signed_p)) \
9408
0
  return false;
9409
0
#define BINARY_OP_TAIL(op)          \
9410
0
      if (signed_p)           \
9411
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9412
0
      else              \
9413
0
  *result = a op b;         \
9414
0
      return true;            \
9415
0
    }
9416
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9417
9418
0
    default:
9419
0
      UNARY_OP  (0-);
9420
0
      BINARY_OP_HEAD (<<);
9421
0
      if (b >= sizeof (a) * CHAR_BIT)
9422
0
  {
9423
0
    *result = 0;
9424
0
    return true;
9425
0
  }
9426
0
      signed_p = 0;
9427
0
      BINARY_OP_TAIL (<<);
9428
0
      BINARY_OP_HEAD (>>);
9429
0
      if (b >= sizeof (a) * CHAR_BIT)
9430
0
  {
9431
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9432
0
    return true;
9433
0
  }
9434
0
      BINARY_OP_TAIL (>>);
9435
0
      BINARY_OP (==);
9436
0
      BINARY_OP (!=);
9437
0
      BINARY_OP (<=);
9438
0
      BINARY_OP (>=);
9439
0
      BINARY_OP (&&);
9440
0
      BINARY_OP (||);
9441
0
      UNARY_OP  (~);
9442
0
      UNARY_OP  (!);
9443
0
      BINARY_OP (*);
9444
0
      BINARY_OP_HEAD (/);
9445
0
      if (b == 0)
9446
0
  {
9447
0
    _bfd_error_handler (_("division by zero"));
9448
0
    bfd_set_error (bfd_error_bad_value);
9449
0
    return false;
9450
0
  }
9451
0
      BINARY_OP_TAIL (/);
9452
0
      BINARY_OP_HEAD (%);
9453
0
      if (b == 0)
9454
0
  {
9455
0
    _bfd_error_handler (_("division by zero"));
9456
0
    bfd_set_error (bfd_error_bad_value);
9457
0
    return false;
9458
0
  }
9459
0
      BINARY_OP_TAIL (%);
9460
0
      BINARY_OP (^);
9461
0
      BINARY_OP (|);
9462
0
      BINARY_OP (&);
9463
0
      BINARY_OP (+);
9464
0
      BINARY_OP (-);
9465
0
      BINARY_OP (<);
9466
0
      BINARY_OP (>);
9467
0
#undef UNARY_OP
9468
0
#undef BINARY_OP
9469
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9470
0
      bfd_set_error (bfd_error_invalid_operation);
9471
0
      return false;
9472
0
    }
9473
0
}
9474
9475
static void
9476
put_value (bfd_vma size,
9477
     unsigned long chunksz,
9478
     bfd *input_bfd,
9479
     bfd_vma x,
9480
     bfd_byte *location)
9481
0
{
9482
0
  location += (size - chunksz);
9483
9484
0
  for (; size; size -= chunksz, location -= chunksz)
9485
0
    {
9486
0
      switch (chunksz)
9487
0
  {
9488
0
  case 1:
9489
0
    bfd_put_8 (input_bfd, x, location);
9490
0
    x >>= 8;
9491
0
    break;
9492
0
  case 2:
9493
0
    bfd_put_16 (input_bfd, x, location);
9494
0
    x >>= 16;
9495
0
    break;
9496
0
  case 4:
9497
0
    bfd_put_32 (input_bfd, x, location);
9498
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9499
0
    x >>= 16;
9500
0
    x >>= 16;
9501
0
    break;
9502
0
#ifdef BFD64
9503
0
  case 8:
9504
0
    bfd_put_64 (input_bfd, x, location);
9505
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9506
0
    x >>= 32;
9507
0
    x >>= 32;
9508
0
    break;
9509
0
#endif
9510
0
  default:
9511
0
    abort ();
9512
0
    break;
9513
0
  }
9514
0
    }
9515
0
}
9516
9517
static bfd_vma
9518
get_value (bfd_vma size,
9519
     unsigned long chunksz,
9520
     bfd *input_bfd,
9521
     bfd_byte *location)
9522
0
{
9523
0
  int shift;
9524
0
  bfd_vma x = 0;
9525
9526
  /* Sanity checks.  */
9527
0
  BFD_ASSERT (chunksz <= sizeof (x)
9528
0
        && size >= chunksz
9529
0
        && chunksz != 0
9530
0
        && (size % chunksz) == 0
9531
0
        && input_bfd != NULL
9532
0
        && location != NULL);
9533
9534
0
  if (chunksz == sizeof (x))
9535
0
    {
9536
0
      BFD_ASSERT (size == chunksz);
9537
9538
      /* Make sure that we do not perform an undefined shift operation.
9539
   We know that size == chunksz so there will only be one iteration
9540
   of the loop below.  */
9541
0
      shift = 0;
9542
0
    }
9543
0
  else
9544
0
    shift = 8 * chunksz;
9545
9546
0
  for (; size; size -= chunksz, location += chunksz)
9547
0
    {
9548
0
      switch (chunksz)
9549
0
  {
9550
0
  case 1:
9551
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9552
0
    break;
9553
0
  case 2:
9554
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9555
0
    break;
9556
0
  case 4:
9557
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9558
0
    break;
9559
0
#ifdef BFD64
9560
0
  case 8:
9561
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9562
0
    break;
9563
0
#endif
9564
0
  default:
9565
0
    abort ();
9566
0
  }
9567
0
    }
9568
0
  return x;
9569
0
}
9570
9571
static void
9572
decode_complex_addend (unsigned long *start,   /* in bits */
9573
           unsigned long *oplen,   /* in bits */
9574
           unsigned long *len,     /* in bits */
9575
           unsigned long *wordsz,  /* in bytes */
9576
           unsigned long *chunksz, /* in bytes */
9577
           unsigned long *lsb0_p,
9578
           unsigned long *signed_p,
9579
           unsigned long *trunc_p,
9580
           unsigned long encoded)
9581
0
{
9582
0
  * start     =  encoded  & 0x3F;
9583
0
  * len       = (encoded >>  6) & 0x3F;
9584
0
  * oplen     = (encoded >> 12) & 0x3F;
9585
0
  * wordsz    = (encoded >> 18) & 0xF;
9586
0
  * chunksz   = (encoded >> 22) & 0xF;
9587
0
  * lsb0_p    = (encoded >> 27) & 1;
9588
0
  * signed_p  = (encoded >> 28) & 1;
9589
0
  * trunc_p   = (encoded >> 29) & 1;
9590
0
}
9591
9592
bfd_reloc_status_type
9593
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9594
            asection *input_section,
9595
            bfd_byte *contents,
9596
            Elf_Internal_Rela *rel,
9597
            bfd_vma relocation)
9598
0
{
9599
0
  bfd_vma shift, x, mask;
9600
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9601
0
  bfd_reloc_status_type r;
9602
0
  bfd_size_type octets;
9603
9604
  /*  Perform this reloc, since it is complex.
9605
      (this is not to say that it necessarily refers to a complex
9606
      symbol; merely that it is a self-describing CGEN based reloc.
9607
      i.e. the addend has the complete reloc information (bit start, end,
9608
      word size, etc) encoded within it.).  */
9609
9610
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9611
0
       &chunksz, &lsb0_p, &signed_p,
9612
0
       &trunc_p, rel->r_addend);
9613
9614
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9615
9616
0
  if (lsb0_p)
9617
0
    shift = (start + 1) - len;
9618
0
  else
9619
0
    shift = (8 * wordsz) - (start + len);
9620
9621
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9622
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9623
9624
#ifdef DEBUG
9625
  printf ("Doing complex reloc: "
9626
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9627
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9628
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9629
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9630
    oplen, (unsigned long) x, (unsigned long) mask,
9631
    (unsigned long) relocation);
9632
#endif
9633
9634
0
  r = bfd_reloc_ok;
9635
0
  if (! trunc_p)
9636
    /* Now do an overflow check.  */
9637
0
    r = bfd_check_overflow ((signed_p
9638
0
           ? complain_overflow_signed
9639
0
           : complain_overflow_unsigned),
9640
0
          len, 0, (8 * wordsz),
9641
0
          relocation);
9642
9643
  /* Do the deed.  */
9644
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9645
9646
#ifdef DEBUG
9647
  printf ("           relocation: %8.8lx\n"
9648
    "         shifted mask: %8.8lx\n"
9649
    " shifted/masked reloc: %8.8lx\n"
9650
    "               result: %8.8lx\n",
9651
    (unsigned long) relocation, (unsigned long) (mask << shift),
9652
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9653
#endif
9654
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9655
0
  return r;
9656
0
}
9657
9658
/* Functions to read r_offset from external (target order) reloc
9659
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9660
   know the value is aligned.  */
9661
9662
static bfd_vma
9663
ext32l_r_offset (const void *p)
9664
0
{
9665
0
  union aligned32
9666
0
  {
9667
0
    uint32_t v;
9668
0
    unsigned char c[4];
9669
0
  };
9670
0
  const union aligned32 *a
9671
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9672
9673
0
  uint32_t aval = (  (uint32_t) a->c[0]
9674
0
       | (uint32_t) a->c[1] << 8
9675
0
       | (uint32_t) a->c[2] << 16
9676
0
       | (uint32_t) a->c[3] << 24);
9677
0
  return aval;
9678
0
}
9679
9680
static bfd_vma
9681
ext32b_r_offset (const void *p)
9682
0
{
9683
0
  union aligned32
9684
0
  {
9685
0
    uint32_t v;
9686
0
    unsigned char c[4];
9687
0
  };
9688
0
  const union aligned32 *a
9689
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9690
9691
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9692
0
       | (uint32_t) a->c[1] << 16
9693
0
       | (uint32_t) a->c[2] << 8
9694
0
       | (uint32_t) a->c[3]);
9695
0
  return aval;
9696
0
}
9697
9698
static bfd_vma
9699
ext64l_r_offset (const void *p)
9700
0
{
9701
0
  union aligned64
9702
0
  {
9703
0
    uint64_t v;
9704
0
    unsigned char c[8];
9705
0
  };
9706
0
  const union aligned64 *a
9707
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9708
9709
0
  uint64_t aval = (  (uint64_t) a->c[0]
9710
0
       | (uint64_t) a->c[1] << 8
9711
0
       | (uint64_t) a->c[2] << 16
9712
0
       | (uint64_t) a->c[3] << 24
9713
0
       | (uint64_t) a->c[4] << 32
9714
0
       | (uint64_t) a->c[5] << 40
9715
0
       | (uint64_t) a->c[6] << 48
9716
0
       | (uint64_t) a->c[7] << 56);
9717
0
  return aval;
9718
0
}
9719
9720
static bfd_vma
9721
ext64b_r_offset (const void *p)
9722
0
{
9723
0
  union aligned64
9724
0
  {
9725
0
    uint64_t v;
9726
0
    unsigned char c[8];
9727
0
  };
9728
0
  const union aligned64 *a
9729
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9730
9731
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9732
0
       | (uint64_t) a->c[1] << 48
9733
0
       | (uint64_t) a->c[2] << 40
9734
0
       | (uint64_t) a->c[3] << 32
9735
0
       | (uint64_t) a->c[4] << 24
9736
0
       | (uint64_t) a->c[5] << 16
9737
0
       | (uint64_t) a->c[6] << 8
9738
0
       | (uint64_t) a->c[7]);
9739
0
  return aval;
9740
0
}
9741
9742
/* When performing a relocatable link, the input relocations are
9743
   preserved.  But, if they reference global symbols, the indices
9744
   referenced must be updated.  Update all the relocations found in
9745
   RELDATA.  */
9746
9747
static bool
9748
elf_link_adjust_relocs (bfd *abfd,
9749
      asection *sec,
9750
      struct bfd_elf_section_reloc_data *reldata,
9751
      bool sort,
9752
      struct bfd_link_info *info)
9753
0
{
9754
0
  unsigned int i;
9755
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9756
0
  bfd_byte *erela;
9757
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9758
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9759
0
  bfd_vma r_type_mask;
9760
0
  int r_sym_shift;
9761
0
  unsigned int count = reldata->count;
9762
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9763
9764
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9765
0
    {
9766
0
      swap_in = bed->s->swap_reloc_in;
9767
0
      swap_out = bed->s->swap_reloc_out;
9768
0
    }
9769
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9770
0
    {
9771
0
      swap_in = bed->s->swap_reloca_in;
9772
0
      swap_out = bed->s->swap_reloca_out;
9773
0
    }
9774
0
  else
9775
0
    abort ();
9776
9777
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9778
0
    abort ();
9779
9780
0
  if (bed->s->arch_size == 32)
9781
0
    {
9782
0
      r_type_mask = 0xff;
9783
0
      r_sym_shift = 8;
9784
0
    }
9785
0
  else
9786
0
    {
9787
0
      r_type_mask = 0xffffffff;
9788
0
      r_sym_shift = 32;
9789
0
    }
9790
9791
0
  erela = reldata->hdr->contents;
9792
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9793
0
    {
9794
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9795
0
      unsigned int j;
9796
9797
0
      if (*rel_hash == NULL)
9798
0
  continue;
9799
9800
0
      if ((*rel_hash)->indx == -2
9801
0
    && info->gc_sections
9802
0
    && ! info->gc_keep_exported)
9803
0
  {
9804
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9805
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9806
0
            abfd, sec,
9807
0
            (*rel_hash)->root.root.string);
9808
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9809
0
            abfd, sec);
9810
0
    bfd_set_error (bfd_error_invalid_operation);
9811
0
    return false;
9812
0
  }
9813
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9814
9815
0
      (*swap_in) (abfd, erela, irela);
9816
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9817
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9818
0
         | (irela[j].r_info & r_type_mask));
9819
0
      (*swap_out) (abfd, irela, erela);
9820
0
    }
9821
9822
0
  if (bed->elf_backend_update_relocs)
9823
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9824
9825
0
  if (sort && count != 0)
9826
0
    {
9827
0
      bfd_vma (*ext_r_off) (const void *);
9828
0
      bfd_vma r_off;
9829
0
      size_t elt_size;
9830
0
      bfd_byte *base, *end, *p, *loc;
9831
0
      bfd_byte *buf = NULL;
9832
9833
0
      if (bed->s->arch_size == 32)
9834
0
  {
9835
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9836
0
      ext_r_off = ext32l_r_offset;
9837
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9838
0
      ext_r_off = ext32b_r_offset;
9839
0
    else
9840
0
      abort ();
9841
0
  }
9842
0
      else
9843
0
  {
9844
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9845
0
      ext_r_off = ext64l_r_offset;
9846
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9847
0
      ext_r_off = ext64b_r_offset;
9848
0
    else
9849
0
      abort ();
9850
0
  }
9851
9852
      /*  Must use a stable sort here.  A modified insertion sort,
9853
    since the relocs are mostly sorted already.  */
9854
0
      elt_size = reldata->hdr->sh_entsize;
9855
0
      base = reldata->hdr->contents;
9856
0
      end = base + count * elt_size;
9857
0
      if (elt_size > sizeof (Elf64_External_Rela))
9858
0
  abort ();
9859
9860
      /* Ensure the first element is lowest.  This acts as a sentinel,
9861
   speeding the main loop below.  */
9862
0
      r_off = (*ext_r_off) (base);
9863
0
      for (p = loc = base; (p += elt_size) < end; )
9864
0
  {
9865
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9866
0
    if (r_off > r_off2)
9867
0
      {
9868
0
        r_off = r_off2;
9869
0
        loc = p;
9870
0
      }
9871
0
  }
9872
0
      if (loc != base)
9873
0
  {
9874
    /* Don't just swap *base and *loc as that changes the order
9875
       of the original base[0] and base[1] if they happen to
9876
       have the same r_offset.  */
9877
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9878
0
    memcpy (onebuf, loc, elt_size);
9879
0
    memmove (base + elt_size, base, loc - base);
9880
0
    memcpy (base, onebuf, elt_size);
9881
0
  }
9882
9883
0
      for (p = base + elt_size; (p += elt_size) < end; )
9884
0
  {
9885
    /* base to p is sorted, *p is next to insert.  */
9886
0
    r_off = (*ext_r_off) (p);
9887
    /* Search the sorted region for location to insert.  */
9888
0
    loc = p - elt_size;
9889
0
    while (r_off < (*ext_r_off) (loc))
9890
0
      loc -= elt_size;
9891
0
    loc += elt_size;
9892
0
    if (loc != p)
9893
0
      {
9894
        /* Chances are there is a run of relocs to insert here,
9895
     from one of more input files.  Files are not always
9896
     linked in order due to the way elf_link_input_bfd is
9897
     called.  See pr17666.  */
9898
0
        size_t sortlen = p - loc;
9899
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9900
0
        size_t runlen = elt_size;
9901
0
        bfd_vma r_off_runend = r_off;
9902
0
        bfd_vma r_off_runend_next;
9903
0
        size_t buf_size = 96 * 1024;
9904
0
        while (p + runlen < end
9905
0
         && (sortlen <= buf_size
9906
0
       || runlen + elt_size <= buf_size)
9907
         /* run must not break the ordering of base..loc+1 */
9908
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9909
         /* run must be already sorted */
9910
0
         && r_off_runend_next >= r_off_runend)
9911
0
    {
9912
0
      runlen += elt_size;
9913
0
      r_off_runend = r_off_runend_next;
9914
0
    }
9915
0
        if (buf == NULL)
9916
0
    {
9917
0
      buf = bfd_malloc (buf_size);
9918
0
      if (buf == NULL)
9919
0
        return false;
9920
0
    }
9921
0
        if (runlen < sortlen)
9922
0
    {
9923
0
      memcpy (buf, p, runlen);
9924
0
      memmove (loc + runlen, loc, sortlen);
9925
0
      memcpy (loc, buf, runlen);
9926
0
    }
9927
0
        else
9928
0
    {
9929
0
      memcpy (buf, loc, sortlen);
9930
0
      memmove (loc, p, runlen);
9931
0
      memcpy (loc + runlen, buf, sortlen);
9932
0
    }
9933
0
        p += runlen - elt_size;
9934
0
      }
9935
0
  }
9936
      /* Hashes are no longer valid.  */
9937
0
      free (reldata->hashes);
9938
0
      reldata->hashes = NULL;
9939
0
      free (buf);
9940
0
    }
9941
0
  return true;
9942
0
}
9943
9944
struct elf_link_sort_rela
9945
{
9946
  union {
9947
    bfd_vma offset;
9948
    bfd_vma sym_mask;
9949
  } u;
9950
  enum elf_reloc_type_class type;
9951
  /* We use this as an array of size int_rels_per_ext_rel.  */
9952
  Elf_Internal_Rela rela[1];
9953
};
9954
9955
/* qsort stability here and for cmp2 is only an issue if multiple
9956
   dynamic relocations are emitted at the same address.  But targets
9957
   that apply a series of dynamic relocations each operating on the
9958
   result of the prior relocation can't use -z combreloc as
9959
   implemented anyway.  Such schemes tend to be broken by sorting on
9960
   symbol index.  That leaves dynamic NONE relocs as the only other
9961
   case where ld might emit multiple relocs at the same address, and
9962
   those are only emitted due to target bugs.  */
9963
9964
static int
9965
elf_link_sort_cmp1 (const void *A, const void *B)
9966
0
{
9967
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9968
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9969
0
  int relativea, relativeb;
9970
9971
0
  relativea = a->type == reloc_class_relative;
9972
0
  relativeb = b->type == reloc_class_relative;
9973
9974
0
  if (relativea < relativeb)
9975
0
    return 1;
9976
0
  if (relativea > relativeb)
9977
0
    return -1;
9978
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9979
0
    return -1;
9980
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9981
0
    return 1;
9982
0
  if (a->rela->r_offset < b->rela->r_offset)
9983
0
    return -1;
9984
0
  if (a->rela->r_offset > b->rela->r_offset)
9985
0
    return 1;
9986
0
  return 0;
9987
0
}
9988
9989
static int
9990
elf_link_sort_cmp2 (const void *A, const void *B)
9991
0
{
9992
0
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9993
0
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9994
9995
0
  if (a->type < b->type)
9996
0
    return -1;
9997
0
  if (a->type > b->type)
9998
0
    return 1;
9999
0
  if (a->u.offset < b->u.offset)
10000
0
    return -1;
10001
0
  if (a->u.offset > b->u.offset)
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 size_t
10011
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
10012
0
{
10013
0
  asection *dynamic_relocs;
10014
0
  asection *rela_dyn;
10015
0
  asection *rel_dyn;
10016
0
  bfd_size_type count, size;
10017
0
  size_t i, ret, sort_elt, ext_size;
10018
0
  bfd_byte *sort, *s_non_relative, *p;
10019
0
  struct elf_link_sort_rela *sq;
10020
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10021
0
  int i2e = bed->s->int_rels_per_ext_rel;
10022
0
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
10023
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10024
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10025
0
  struct bfd_link_order *lo;
10026
0
  bfd_vma r_sym_mask;
10027
0
  bool use_rela;
10028
10029
  /* Find a dynamic reloc section.  */
10030
0
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
10031
0
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
10032
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10033
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10034
0
    {
10035
0
      bool use_rela_initialised = false;
10036
10037
      /* This is just here to stop gcc from complaining.
10038
   Its initialization checking code is not perfect.  */
10039
0
      use_rela = true;
10040
10041
      /* Both sections are present.  Examine the sizes
10042
   of the indirect sections to help us choose.  */
10043
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10044
0
  if (lo->type == bfd_indirect_link_order)
10045
0
    {
10046
0
      asection *o = lo->u.indirect.section;
10047
10048
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10049
0
        {
10050
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10051
      /* Section size is divisible by both rel and rela sizes.
10052
         It is of no help to us.  */
10053
0
      ;
10054
0
    else
10055
0
      {
10056
        /* Section size is only divisible by rela.  */
10057
0
        if (use_rela_initialised && !use_rela)
10058
0
          {
10059
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10060
0
                "they are in more than one size"),
10061
0
              abfd);
10062
0
      bfd_set_error (bfd_error_invalid_operation);
10063
0
      return 0;
10064
0
          }
10065
0
        else
10066
0
          {
10067
0
      use_rela = true;
10068
0
      use_rela_initialised = true;
10069
0
          }
10070
0
      }
10071
0
        }
10072
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10073
0
        {
10074
    /* Section size is only divisible by rel.  */
10075
0
    if (use_rela_initialised && use_rela)
10076
0
      {
10077
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10078
0
            "they are in more than one size"),
10079
0
          abfd);
10080
0
        bfd_set_error (bfd_error_invalid_operation);
10081
0
        return 0;
10082
0
      }
10083
0
    else
10084
0
      {
10085
0
        use_rela = false;
10086
0
        use_rela_initialised = true;
10087
0
      }
10088
0
        }
10089
0
      else
10090
0
        {
10091
    /* The section size is not divisible by either -
10092
       something is wrong.  */
10093
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10094
0
              "they are of an unknown size"), abfd);
10095
0
    bfd_set_error (bfd_error_invalid_operation);
10096
0
    return 0;
10097
0
        }
10098
0
    }
10099
10100
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10101
0
  if (lo->type == bfd_indirect_link_order)
10102
0
    {
10103
0
      asection *o = lo->u.indirect.section;
10104
10105
0
      if ((o->size % bed->s->sizeof_rela) == 0)
10106
0
        {
10107
0
    if ((o->size % bed->s->sizeof_rel) == 0)
10108
      /* Section size is divisible by both rel and rela sizes.
10109
         It is of no help to us.  */
10110
0
      ;
10111
0
    else
10112
0
      {
10113
        /* Section size is only divisible by rela.  */
10114
0
        if (use_rela_initialised && !use_rela)
10115
0
          {
10116
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10117
0
                "they are in more than one size"),
10118
0
              abfd);
10119
0
      bfd_set_error (bfd_error_invalid_operation);
10120
0
      return 0;
10121
0
          }
10122
0
        else
10123
0
          {
10124
0
      use_rela = true;
10125
0
      use_rela_initialised = true;
10126
0
          }
10127
0
      }
10128
0
        }
10129
0
      else if ((o->size % bed->s->sizeof_rel) == 0)
10130
0
        {
10131
    /* Section size is only divisible by rel.  */
10132
0
    if (use_rela_initialised && use_rela)
10133
0
      {
10134
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10135
0
            "they are in more than one size"),
10136
0
          abfd);
10137
0
        bfd_set_error (bfd_error_invalid_operation);
10138
0
        return 0;
10139
0
      }
10140
0
    else
10141
0
      {
10142
0
        use_rela = false;
10143
0
        use_rela_initialised = true;
10144
0
      }
10145
0
        }
10146
0
      else
10147
0
        {
10148
    /* The section size is not divisible by either -
10149
       something is wrong.  */
10150
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10151
0
              "they are of an unknown size"), abfd);
10152
0
    bfd_set_error (bfd_error_invalid_operation);
10153
0
    return 0;
10154
0
        }
10155
0
    }
10156
10157
0
      if (! use_rela_initialised)
10158
  /* Make a guess.  */
10159
0
  use_rela = true;
10160
0
    }
10161
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10162
0
    use_rela = true;
10163
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10164
0
    use_rela = false;
10165
0
  else
10166
0
    return 0;
10167
10168
0
  if (use_rela)
10169
0
    {
10170
0
      dynamic_relocs = rela_dyn;
10171
0
      ext_size = bed->s->sizeof_rela;
10172
0
      swap_in = bed->s->swap_reloca_in;
10173
0
      swap_out = bed->s->swap_reloca_out;
10174
0
    }
10175
0
  else
10176
0
    {
10177
0
      dynamic_relocs = rel_dyn;
10178
0
      ext_size = bed->s->sizeof_rel;
10179
0
      swap_in = bed->s->swap_reloc_in;
10180
0
      swap_out = bed->s->swap_reloc_out;
10181
0
    }
10182
10183
0
  size = 0;
10184
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10185
0
    if (lo->type == bfd_indirect_link_order)
10186
0
      size += lo->u.indirect.section->size;
10187
10188
0
  if (size != dynamic_relocs->size)
10189
0
    return 0;
10190
10191
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10192
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10193
10194
0
  count = dynamic_relocs->size / ext_size;
10195
0
  if (count == 0)
10196
0
    return 0;
10197
0
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10198
10199
0
  if (sort == NULL)
10200
0
    {
10201
0
      (*info->callbacks->warning)
10202
0
  (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10203
0
      return 0;
10204
0
    }
10205
10206
0
  if (bed->s->arch_size == 32)
10207
0
    r_sym_mask = ~(bfd_vma) 0xff;
10208
0
  else
10209
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10210
10211
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10212
0
    if (lo->type == bfd_indirect_link_order)
10213
0
      {
10214
0
  bfd_byte *erel, *erelend;
10215
0
  asection *o = lo->u.indirect.section;
10216
10217
0
  if (o->contents == NULL && o->size != 0)
10218
0
    {
10219
      /* This is a reloc section that is being handled as a normal
10220
         section.  See bfd_section_from_shdr.  We can't combine
10221
         relocs in this case.  */
10222
0
      free (sort);
10223
0
      return 0;
10224
0
    }
10225
0
  erel = o->contents;
10226
0
  erelend = o->contents + o->size;
10227
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10228
10229
0
  while (erel < erelend)
10230
0
    {
10231
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10232
10233
0
      (*swap_in) (abfd, erel, s->rela);
10234
0
      s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10235
0
      s->u.sym_mask = r_sym_mask;
10236
0
      p += sort_elt;
10237
0
      erel += ext_size;
10238
0
    }
10239
0
      }
10240
10241
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10242
10243
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10244
0
    {
10245
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10246
0
      if (s->type != reloc_class_relative)
10247
0
  break;
10248
0
    }
10249
0
  ret = i;
10250
0
  s_non_relative = p;
10251
10252
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10253
0
  for (; i < count; i++, p += sort_elt)
10254
0
    {
10255
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10256
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10257
0
  sq = sp;
10258
0
      sp->u.offset = sq->rela->r_offset;
10259
0
    }
10260
10261
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10262
10263
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10264
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10265
0
    {
10266
      /* We have plt relocs in .rela.dyn.  */
10267
0
      sq = (struct elf_link_sort_rela *) sort;
10268
0
      for (i = 0; i < count; i++)
10269
0
  if (sq[count - i - 1].type != reloc_class_plt)
10270
0
    break;
10271
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10272
0
  {
10273
0
    struct bfd_link_order **plo;
10274
    /* Put srelplt link_order last.  This is so the output_offset
10275
       set in the next loop is correct for DT_JMPREL.  */
10276
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10277
0
      if ((*plo)->type == bfd_indirect_link_order
10278
0
    && (*plo)->u.indirect.section == htab->srelplt)
10279
0
        {
10280
0
    lo = *plo;
10281
0
    *plo = lo->next;
10282
0
        }
10283
0
      else
10284
0
        plo = &(*plo)->next;
10285
0
    *plo = lo;
10286
0
    lo->next = NULL;
10287
0
    dynamic_relocs->map_tail.link_order = lo;
10288
0
  }
10289
0
    }
10290
10291
0
  p = sort;
10292
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10293
0
    if (lo->type == bfd_indirect_link_order)
10294
0
      {
10295
0
  bfd_byte *erel, *erelend;
10296
0
  asection *o = lo->u.indirect.section;
10297
10298
0
  erel = o->contents;
10299
0
  erelend = o->contents + o->size;
10300
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10301
0
  while (erel < erelend)
10302
0
    {
10303
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10304
0
      (*swap_out) (abfd, s->rela, erel);
10305
0
      p += sort_elt;
10306
0
      erel += ext_size;
10307
0
    }
10308
0
      }
10309
10310
0
  free (sort);
10311
0
  *psec = dynamic_relocs;
10312
0
  return ret;
10313
0
}
10314
10315
/* Add a symbol to the output symbol string table.  */
10316
10317
static int
10318
elf_link_output_symstrtab (void *finf,
10319
         const char *name,
10320
         Elf_Internal_Sym *elfsym,
10321
         asection *input_sec,
10322
         struct elf_link_hash_entry *h)
10323
0
{
10324
0
  struct elf_final_link_info *flinfo = finf;
10325
0
  int (*output_symbol_hook)
10326
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10327
0
     struct elf_link_hash_entry *);
10328
0
  struct elf_link_hash_table *hash_table;
10329
0
  const struct elf_backend_data *bed;
10330
0
  bfd_size_type strtabsize;
10331
10332
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10333
10334
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10335
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10336
0
  if (output_symbol_hook != NULL)
10337
0
    {
10338
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10339
0
      if (ret != 1)
10340
0
  return ret;
10341
0
    }
10342
10343
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10344
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10345
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10346
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10347
10348
0
  if (name == NULL || *name == '\0')
10349
0
    elfsym->st_name = (unsigned long) -1;
10350
0
  else
10351
0
    {
10352
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10353
   to get the final offset for st_name.  */
10354
0
      char *versioned_name = (char *) name;
10355
0
      if (h != NULL)
10356
0
  {
10357
0
    if (h->versioned == versioned && h->def_dynamic)
10358
0
      {
10359
        /* Keep only one '@' for versioned symbols defined in
10360
           shared objects.  */
10361
0
        char *version = strrchr (name, ELF_VER_CHR);
10362
0
        char *base_end = strchr (name, ELF_VER_CHR);
10363
0
        if (version != base_end)
10364
0
    {
10365
0
      size_t base_len;
10366
0
      size_t len = strlen (name);
10367
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10368
0
      if (versioned_name == NULL)
10369
0
        return 0;
10370
0
      base_len = base_end - name;
10371
0
      memcpy (versioned_name, name, base_len);
10372
0
      memcpy (versioned_name + base_len, version,
10373
0
        len - base_len);
10374
0
    }
10375
0
      }
10376
0
  }
10377
0
      else if (flinfo->info->unique_symbol
10378
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10379
0
  {
10380
0
    struct local_hash_entry *lh;
10381
0
    size_t count_len;
10382
0
    size_t base_len;
10383
0
    char buf[30];
10384
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10385
0
      {
10386
0
      case STT_FILE:
10387
0
      case STT_SECTION:
10388
0
        break;
10389
0
      default:
10390
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10391
0
         (&flinfo->local_hash_table, name, true, false);
10392
0
        if (lh == NULL)
10393
0
    return 0;
10394
        /* Always append ".COUNT" to local symbols to avoid
10395
     potential conflicts with local symbol "XXX.COUNT".  */
10396
0
        sprintf (buf, "%lx", lh->count);
10397
0
        base_len = lh->size;
10398
0
        if (!base_len)
10399
0
    {
10400
0
      base_len = strlen (name);
10401
0
      lh->size = base_len;
10402
0
    }
10403
0
        count_len = strlen (buf);
10404
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10405
0
            base_len + count_len + 2);
10406
0
        if (versioned_name == NULL)
10407
0
    return 0;
10408
0
        memcpy (versioned_name, name, base_len);
10409
0
        versioned_name[base_len] = '.';
10410
0
        memcpy (versioned_name + base_len + 1, buf,
10411
0
          count_len + 1);
10412
0
        lh->count++;
10413
0
        break;
10414
0
      }
10415
0
  }
10416
0
      elfsym->st_name
10417
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10418
0
                 versioned_name, false);
10419
0
      if (elfsym->st_name == (unsigned long) -1)
10420
0
  return 0;
10421
0
    }
10422
10423
0
  hash_table = elf_hash_table (flinfo->info);
10424
0
  strtabsize = hash_table->strtabsize;
10425
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10426
0
    {
10427
0
      strtabsize += strtabsize;
10428
0
      hash_table->strtabsize = strtabsize;
10429
0
      strtabsize *= sizeof (*hash_table->strtab);
10430
0
      hash_table->strtab
10431
0
  = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10432
0
             strtabsize);
10433
0
      if (hash_table->strtab == NULL)
10434
0
  return 0;
10435
0
    }
10436
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10437
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10438
0
    = flinfo->output_bfd->symcount;
10439
0
  flinfo->output_bfd->symcount += 1;
10440
10441
0
  return 1;
10442
0
}
10443
10444
/* Swap symbols out to the symbol table and flush the output symbols to
10445
   the file.  */
10446
10447
static bool
10448
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10449
0
{
10450
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10451
0
  size_t amt;
10452
0
  size_t i;
10453
0
  const struct elf_backend_data *bed;
10454
0
  bfd_byte *symbuf;
10455
0
  Elf_Internal_Shdr *hdr;
10456
0
  file_ptr pos;
10457
0
  bool ret;
10458
10459
0
  if (flinfo->output_bfd->symcount == 0)
10460
0
    return true;
10461
10462
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10463
10464
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10465
10466
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10467
0
  symbuf = (bfd_byte *) bfd_malloc (amt);
10468
0
  if (symbuf == NULL)
10469
0
    return false;
10470
10471
0
  if (flinfo->symshndxbuf)
10472
0
    {
10473
0
      amt = sizeof (Elf_External_Sym_Shndx);
10474
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10475
0
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10476
0
      if (flinfo->symshndxbuf == NULL)
10477
0
  {
10478
0
    free (symbuf);
10479
0
    return false;
10480
0
  }
10481
0
    }
10482
10483
  /* Now swap out the symbols.  */
10484
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10485
0
    {
10486
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10487
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10488
0
  elfsym->sym.st_name = 0;
10489
0
      else
10490
0
  elfsym->sym.st_name
10491
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10492
0
                elfsym->sym.st_name);
10493
10494
      /* Inform the linker of the addition of this symbol.  */
10495
10496
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10497
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10498
0
             &elfsym->sym);
10499
10500
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10501
0
             ((bfd_byte *) symbuf
10502
0
        + (elfsym->dest_index
10503
0
           * bed->s->sizeof_sym)),
10504
0
             NPTR_ADD (flinfo->symshndxbuf,
10505
0
           elfsym->dest_index));
10506
0
    }
10507
10508
0
  hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10509
0
  pos = hdr->sh_offset + hdr->sh_size;
10510
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10511
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10512
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10513
0
    {
10514
0
      hdr->sh_size += amt;
10515
0
      ret = true;
10516
0
    }
10517
0
  else
10518
0
    ret = false;
10519
10520
0
  free (symbuf);
10521
0
  return ret;
10522
0
}
10523
10524
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
10525
10526
static bool
10527
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10528
0
{
10529
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10530
0
      && sym->st_shndx < SHN_LORESERVE)
10531
0
    {
10532
      /* The gABI doesn't support dynamic symbols in output sections
10533
   beyond 64k.  */
10534
0
      _bfd_error_handler
10535
  /* xgettext:c-format */
10536
0
  (_("%pB: too many sections: %d (>= %d)"),
10537
0
   abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10538
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10539
0
      return false;
10540
0
    }
10541
0
  return true;
10542
0
}
10543
10544
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10545
   allowing an unsatisfied unversioned symbol in the DSO to match a
10546
   versioned symbol that would normally require an explicit version.
10547
   We also handle the case that a DSO references a hidden symbol
10548
   which may be satisfied by a versioned symbol in another DSO.  */
10549
10550
static bool
10551
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10552
         const struct elf_backend_data *bed,
10553
         struct elf_link_hash_entry *h)
10554
0
{
10555
0
  bfd *abfd;
10556
0
  struct elf_link_loaded_list *loaded;
10557
10558
0
  if (!is_elf_hash_table (info->hash))
10559
0
    return false;
10560
10561
  /* Check indirect symbol.  */
10562
0
  while (h->root.type == bfd_link_hash_indirect)
10563
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10564
10565
0
  switch (h->root.type)
10566
0
    {
10567
0
    default:
10568
0
      abfd = NULL;
10569
0
      break;
10570
10571
0
    case bfd_link_hash_undefined:
10572
0
    case bfd_link_hash_undefweak:
10573
0
      abfd = h->root.u.undef.abfd;
10574
0
      if (abfd == NULL
10575
0
    || (abfd->flags & DYNAMIC) == 0
10576
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10577
0
  return false;
10578
0
      break;
10579
10580
0
    case bfd_link_hash_defined:
10581
0
    case bfd_link_hash_defweak:
10582
0
      abfd = h->root.u.def.section->owner;
10583
0
      break;
10584
10585
0
    case bfd_link_hash_common:
10586
0
      abfd = h->root.u.c.p->section->owner;
10587
0
      break;
10588
0
    }
10589
0
  BFD_ASSERT (abfd != NULL);
10590
10591
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10592
0
       loaded != NULL;
10593
0
       loaded = loaded->next)
10594
0
    {
10595
0
      bfd *input;
10596
0
      Elf_Internal_Shdr *hdr;
10597
0
      size_t symcount;
10598
0
      size_t extsymcount;
10599
0
      size_t extsymoff;
10600
0
      Elf_Internal_Shdr *versymhdr;
10601
0
      Elf_Internal_Sym *isym;
10602
0
      Elf_Internal_Sym *isymend;
10603
0
      Elf_Internal_Sym *isymbuf;
10604
0
      Elf_External_Versym *ever;
10605
0
      Elf_External_Versym *extversym;
10606
10607
0
      input = loaded->abfd;
10608
10609
      /* We check each DSO for a possible hidden versioned definition.  */
10610
0
      if (input == abfd
10611
0
    || elf_dynversym (input) == 0)
10612
0
  continue;
10613
10614
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10615
10616
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10617
0
      if (elf_bad_symtab (input))
10618
0
  {
10619
0
    extsymcount = symcount;
10620
0
    extsymoff = 0;
10621
0
  }
10622
0
      else
10623
0
  {
10624
0
    extsymcount = symcount - hdr->sh_info;
10625
0
    extsymoff = hdr->sh_info;
10626
0
  }
10627
10628
0
      if (extsymcount == 0)
10629
0
  continue;
10630
10631
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10632
0
              NULL, NULL, NULL);
10633
0
      if (isymbuf == NULL)
10634
0
  return false;
10635
10636
      /* Read in any version definitions.  */
10637
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10638
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10639
0
    || (extversym = (Elf_External_Versym *)
10640
0
        _bfd_malloc_and_read (input, versymhdr->sh_size,
10641
0
            versymhdr->sh_size)) == NULL)
10642
0
  {
10643
0
    free (isymbuf);
10644
0
    return false;
10645
0
  }
10646
10647
0
      ever = extversym + extsymoff;
10648
0
      isymend = isymbuf + extsymcount;
10649
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10650
0
  {
10651
0
    const char *name;
10652
0
    Elf_Internal_Versym iver;
10653
0
    unsigned short version_index;
10654
10655
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10656
0
        || isym->st_shndx == SHN_UNDEF)
10657
0
      continue;
10658
10659
0
    name = bfd_elf_string_from_elf_section (input,
10660
0
              hdr->sh_link,
10661
0
              isym->st_name);
10662
0
    if (strcmp (name, h->root.root.string) != 0)
10663
0
      continue;
10664
10665
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10666
10667
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10668
0
        && !(h->def_regular
10669
0
       && h->forced_local))
10670
0
      {
10671
        /* If we have a non-hidden versioned sym, then it should
10672
     have provided a definition for the undefined sym unless
10673
     it is defined in a non-shared object and forced local.
10674
         */
10675
0
        abort ();
10676
0
      }
10677
10678
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10679
0
    if (version_index == 1 || version_index == 2)
10680
0
      {
10681
        /* This is the base or first version.  We can use it.  */
10682
0
        free (extversym);
10683
0
        free (isymbuf);
10684
0
        return true;
10685
0
      }
10686
0
  }
10687
10688
0
      free (extversym);
10689
0
      free (isymbuf);
10690
0
    }
10691
10692
0
  return false;
10693
0
}
10694
10695
/* Convert ELF common symbol TYPE.  */
10696
10697
static int
10698
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10699
0
{
10700
  /* Commom symbol can only appear in relocatable link.  */
10701
0
  if (!bfd_link_relocatable (info))
10702
0
    abort ();
10703
0
  switch (info->elf_stt_common)
10704
0
    {
10705
0
    case unchanged:
10706
0
      break;
10707
0
    case elf_stt_common:
10708
0
      type = STT_COMMON;
10709
0
      break;
10710
0
    case no_elf_stt_common:
10711
0
      type = STT_OBJECT;
10712
0
      break;
10713
0
    }
10714
0
  return type;
10715
0
}
10716
10717
/* Add an external symbol to the symbol table.  This is called from
10718
   the hash table traversal routine.  When generating a shared object,
10719
   we go through the symbol table twice.  The first time we output
10720
   anything that might have been forced to local scope in a version
10721
   script.  The second time we output the symbols that are still
10722
   global symbols.  */
10723
10724
static bool
10725
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10726
0
{
10727
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10728
0
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10729
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10730
0
  bool strip;
10731
0
  Elf_Internal_Sym sym;
10732
0
  asection *input_sec;
10733
0
  const struct elf_backend_data *bed;
10734
0
  long indx;
10735
0
  int ret;
10736
0
  unsigned int type;
10737
10738
0
  if (h->root.type == bfd_link_hash_warning)
10739
0
    {
10740
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10741
0
      if (h->root.type == bfd_link_hash_new)
10742
0
  return true;
10743
0
    }
10744
10745
  /* Decide whether to output this symbol in this pass.  */
10746
0
  if (eoinfo->localsyms)
10747
0
    {
10748
0
      if (!h->forced_local)
10749
0
  return true;
10750
0
    }
10751
0
  else
10752
0
    {
10753
0
      if (h->forced_local)
10754
0
  return true;
10755
0
    }
10756
10757
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10758
10759
0
  if (h->root.type == bfd_link_hash_undefined)
10760
0
    {
10761
      /* If we have an undefined symbol reference here then it must have
10762
   come from a shared library that is being linked in.  (Undefined
10763
   references in regular files have already been handled unless
10764
   they are in unreferenced sections which are removed by garbage
10765
   collection).  */
10766
0
      bool ignore_undef = false;
10767
10768
      /* Some symbols may be special in that the fact that they're
10769
   undefined can be safely ignored - let backend determine that.  */
10770
0
      if (bed->elf_backend_ignore_undef_symbol)
10771
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10772
10773
      /* If we are reporting errors for this situation then do so now.  */
10774
0
      if (!ignore_undef
10775
0
    && h->ref_dynamic_nonweak
10776
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10777
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10778
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10779
0
  {
10780
0
    flinfo->info->callbacks->undefined_symbol
10781
0
      (flinfo->info, h->root.root.string,
10782
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10783
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10784
0
       && !flinfo->info->warn_unresolved_syms);
10785
0
  }
10786
10787
      /* Strip a global symbol defined in a discarded section.  */
10788
0
      if (h->indx == -3)
10789
0
  return true;
10790
0
    }
10791
10792
  /* We should also warn if a forced local symbol is referenced from
10793
     shared libraries.  */
10794
0
  if (bfd_link_executable (flinfo->info)
10795
0
      && h->forced_local
10796
0
      && h->ref_dynamic
10797
0
      && h->def_regular
10798
0
      && !h->dynamic_def
10799
0
      && h->ref_dynamic_nonweak
10800
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10801
0
    {
10802
0
      bfd *def_bfd;
10803
0
      const char *msg;
10804
0
      struct elf_link_hash_entry *hi = h;
10805
10806
      /* Check indirect symbol.  */
10807
0
      while (hi->root.type == bfd_link_hash_indirect)
10808
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10809
10810
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10811
  /* xgettext:c-format */
10812
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10813
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10814
  /* xgettext:c-format */
10815
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10816
0
      else
10817
  /* xgettext:c-format */
10818
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10819
0
      def_bfd = flinfo->output_bfd;
10820
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10821
0
  def_bfd = hi->root.u.def.section->owner;
10822
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10823
0
        h->root.root.string, def_bfd);
10824
0
      bfd_set_error (bfd_error_bad_value);
10825
0
      eoinfo->failed = true;
10826
0
      return false;
10827
0
    }
10828
10829
  /* We don't want to output symbols that have never been mentioned by
10830
     a regular file, or that we have been told to strip.  However, if
10831
     h->indx is set to -2, the symbol is used by a reloc and we must
10832
     output it.  */
10833
0
  strip = false;
10834
0
  if (h->indx == -2)
10835
0
    ;
10836
0
  else if ((h->def_dynamic
10837
0
      || h->ref_dynamic
10838
0
      || h->root.type == bfd_link_hash_new)
10839
0
     && !h->def_regular
10840
0
     && !h->ref_regular)
10841
0
    strip = true;
10842
0
  else if (flinfo->info->strip == strip_all)
10843
0
    strip = true;
10844
0
  else if (flinfo->info->strip == strip_some
10845
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10846
0
             h->root.root.string, false, false) == NULL)
10847
0
    strip = true;
10848
0
  else if ((h->root.type == bfd_link_hash_defined
10849
0
      || h->root.type == bfd_link_hash_defweak)
10850
0
     && ((flinfo->info->strip_discarded
10851
0
    && discarded_section (h->root.u.def.section))
10852
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10853
0
       && h->root.u.def.section->owner != NULL
10854
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10855
0
    strip = true;
10856
0
  else if ((h->root.type == bfd_link_hash_undefined
10857
0
      || h->root.type == bfd_link_hash_undefweak)
10858
0
     && h->root.u.undef.abfd != NULL
10859
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10860
0
    strip = true;
10861
10862
  /* Remember if this symbol should be stripped.  */
10863
0
  bool should_strip = strip;
10864
10865
  /* Strip undefined weak symbols link if they don't have relocation.  */
10866
0
  if (!strip)
10867
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10868
10869
0
  type = h->type;
10870
10871
  /* If we're stripping it, and it's not a dynamic symbol, there's
10872
     nothing else to do.   However, if it is a forced local symbol or
10873
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10874
     function a chance to make it dynamic.  */
10875
0
  if (strip
10876
0
      && h->dynindx == -1
10877
0
      && type != STT_GNU_IFUNC
10878
0
      && !h->forced_local)
10879
0
    return true;
10880
10881
0
  sym.st_value = 0;
10882
0
  sym.st_size = h->size;
10883
0
  sym.st_other = h->other;
10884
0
  switch (h->root.type)
10885
0
    {
10886
0
    default:
10887
0
    case bfd_link_hash_new:
10888
0
    case bfd_link_hash_warning:
10889
0
      abort ();
10890
0
      return false;
10891
10892
0
    case bfd_link_hash_undefined:
10893
0
    case bfd_link_hash_undefweak:
10894
0
      input_sec = bfd_und_section_ptr;
10895
0
      sym.st_shndx = SHN_UNDEF;
10896
0
      break;
10897
10898
0
    case bfd_link_hash_defined:
10899
0
    case bfd_link_hash_defweak:
10900
0
      {
10901
0
  input_sec = h->root.u.def.section;
10902
0
  if (input_sec->output_section != NULL)
10903
0
    {
10904
0
      sym.st_shndx =
10905
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10906
0
             input_sec->output_section);
10907
0
      if (sym.st_shndx == SHN_BAD)
10908
0
        {
10909
0
    _bfd_error_handler
10910
      /* xgettext:c-format */
10911
0
      (_("%pB: could not find output section %pA for input section %pA"),
10912
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10913
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10914
0
    eoinfo->failed = true;
10915
0
    return false;
10916
0
        }
10917
10918
      /* ELF symbols in relocatable files are section relative,
10919
         but in nonrelocatable files they are virtual
10920
         addresses.  */
10921
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10922
0
      if (!bfd_link_relocatable (flinfo->info))
10923
0
        {
10924
0
    sym.st_value += input_sec->output_section->vma;
10925
0
    if (h->type == STT_TLS)
10926
0
      {
10927
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10928
0
        if (tls_sec != NULL)
10929
0
          sym.st_value -= tls_sec->vma;
10930
0
      }
10931
0
        }
10932
0
    }
10933
0
  else
10934
0
    {
10935
0
      BFD_ASSERT (input_sec->owner == NULL
10936
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10937
0
      sym.st_shndx = SHN_UNDEF;
10938
0
      input_sec = bfd_und_section_ptr;
10939
0
    }
10940
0
      }
10941
0
      break;
10942
10943
0
    case bfd_link_hash_common:
10944
0
      input_sec = h->root.u.c.p->section;
10945
0
      sym.st_shndx = bed->common_section_index (input_sec);
10946
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10947
0
      break;
10948
10949
0
    case bfd_link_hash_indirect:
10950
      /* These symbols are created by symbol versioning.  They point
10951
   to the decorated version of the name.  For example, if the
10952
   symbol foo@@GNU_1.2 is the default, which should be used when
10953
   foo is used with no version, then we add an indirect symbol
10954
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10955
   since the indirected symbol is already in the hash table.  */
10956
0
      return true;
10957
0
    }
10958
10959
0
  if (type == STT_COMMON || type == STT_OBJECT)
10960
0
    switch (h->root.type)
10961
0
      {
10962
0
      case bfd_link_hash_common:
10963
0
  type = elf_link_convert_common_type (flinfo->info, type);
10964
0
  break;
10965
0
      case bfd_link_hash_defined:
10966
0
      case bfd_link_hash_defweak:
10967
0
  if (bed->common_definition (&sym))
10968
0
    type = elf_link_convert_common_type (flinfo->info, type);
10969
0
  else
10970
0
    type = STT_OBJECT;
10971
0
  break;
10972
0
      case bfd_link_hash_undefined:
10973
0
      case bfd_link_hash_undefweak:
10974
0
  break;
10975
0
      default:
10976
0
  abort ();
10977
0
      }
10978
10979
0
  if (h->forced_local)
10980
0
    {
10981
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10982
      /* Turn off visibility on local symbol.  */
10983
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10984
0
    }
10985
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
10986
0
  else if (h->unique_global && h->def_regular)
10987
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10988
0
  else if (h->root.type == bfd_link_hash_undefweak
10989
0
     || h->root.type == bfd_link_hash_defweak)
10990
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10991
0
  else
10992
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10993
0
  sym.st_target_internal = h->target_internal;
10994
10995
  /* Give the processor backend a chance to tweak the symbol value,
10996
     and also to finish up anything that needs to be done for this
10997
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
10998
     forced local syms when non-shared is due to a historical quirk.
10999
     STT_GNU_IFUNC symbol must go through PLT.  */
11000
0
  if ((h->type == STT_GNU_IFUNC
11001
0
       && h->def_regular
11002
0
       && !bfd_link_relocatable (flinfo->info))
11003
0
      || ((h->dynindx != -1
11004
0
     || h->forced_local)
11005
0
    && ((bfd_link_pic (flinfo->info)
11006
0
         && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11007
0
       || h->root.type != bfd_link_hash_undefweak))
11008
0
        || !h->forced_local)
11009
0
    && elf_hash_table (flinfo->info)->dynamic_sections_created))
11010
0
    {
11011
0
      if (! ((*bed->elf_backend_finish_dynamic_symbol)
11012
0
       (flinfo->output_bfd, flinfo->info, h, &sym)))
11013
0
  {
11014
0
    eoinfo->failed = true;
11015
0
    return false;
11016
0
  }
11017
      /* If a symbol is in the dynamic symbol table and isn't a
11018
   should-strip symbol, also keep it in the symbol table.  */
11019
0
      if (!should_strip)
11020
0
  strip = false;
11021
0
    }
11022
11023
  /* If we are marking the symbol as undefined, and there are no
11024
     non-weak references to this symbol from a regular object, then
11025
     mark the symbol as weak undefined; if there are non-weak
11026
     references, mark the symbol as strong.  We can't do this earlier,
11027
     because it might not be marked as undefined until the
11028
     finish_dynamic_symbol routine gets through with it.  */
11029
0
  if (sym.st_shndx == SHN_UNDEF
11030
0
      && h->ref_regular
11031
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11032
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11033
0
    {
11034
0
      int bindtype;
11035
0
      type = ELF_ST_TYPE (sym.st_info);
11036
11037
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11038
0
      if (type == STT_GNU_IFUNC)
11039
0
  type = STT_FUNC;
11040
11041
0
      if (h->ref_regular_nonweak)
11042
0
  bindtype = STB_GLOBAL;
11043
0
      else
11044
0
  bindtype = STB_WEAK;
11045
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11046
0
    }
11047
11048
  /* If this is a symbol defined in a dynamic library, don't use the
11049
     symbol size from the dynamic library.  Relinking an executable
11050
     against a new library may introduce gratuitous changes in the
11051
     executable's symbols if we keep the size.  */
11052
0
  if (sym.st_shndx == SHN_UNDEF
11053
0
      && !h->def_regular
11054
0
      && h->def_dynamic)
11055
0
    sym.st_size = 0;
11056
11057
  /* If a non-weak symbol with non-default visibility is not defined
11058
     locally, it is a fatal error.  */
11059
0
  if (!bfd_link_relocatable (flinfo->info)
11060
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11061
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11062
0
      && h->root.type == bfd_link_hash_undefined
11063
0
      && !h->def_regular)
11064
0
    {
11065
0
      const char *msg;
11066
11067
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11068
  /* xgettext:c-format */
11069
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11070
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11071
  /* xgettext:c-format */
11072
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11073
0
      else
11074
  /* xgettext:c-format */
11075
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11076
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11077
0
      bfd_set_error (bfd_error_bad_value);
11078
0
      eoinfo->failed = true;
11079
0
      return false;
11080
0
    }
11081
11082
  /* If this symbol should be put in the .dynsym section, then put it
11083
     there now.  We already know the symbol index.  We also fill in
11084
     the entry in the .hash section.  */
11085
0
  if (h->dynindx != -1
11086
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11087
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11088
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11089
0
    {
11090
0
      bfd_byte *esym;
11091
11092
      /* Since there is no version information in the dynamic string,
11093
   if there is no version info in symbol version section, we will
11094
   have a run-time problem if not linking executable, referenced
11095
   by shared library, or not bound locally.  */
11096
0
      if (h->verinfo.verdef == NULL
11097
0
    && (!bfd_link_executable (flinfo->info)
11098
0
        || h->ref_dynamic
11099
0
        || !h->def_regular))
11100
0
  {
11101
0
    char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11102
11103
0
    if (p && p [1] != '\0')
11104
0
      {
11105
0
        _bfd_error_handler
11106
    /* xgettext:c-format */
11107
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11108
0
     flinfo->output_bfd, h->root.root.string);
11109
0
        eoinfo->failed = true;
11110
0
        return false;
11111
0
      }
11112
0
  }
11113
11114
0
      sym.st_name = h->dynstr_index;
11115
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11116
0
        + h->dynindx * bed->s->sizeof_sym);
11117
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11118
0
  {
11119
0
    eoinfo->failed = true;
11120
0
    return false;
11121
0
  }
11122
11123
      /* Inform the linker of the addition of this symbol.  */
11124
11125
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11126
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11127
11128
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11129
11130
0
      if (flinfo->hash_sec != NULL)
11131
0
  {
11132
0
    size_t hash_entry_size;
11133
0
    bfd_byte *bucketpos;
11134
0
    bfd_vma chain;
11135
0
    size_t bucketcount;
11136
0
    size_t bucket;
11137
11138
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11139
0
    bucket = h->u.elf_hash_value % bucketcount;
11140
11141
0
    hash_entry_size
11142
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11143
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11144
0
           + (bucket + 2) * hash_entry_size);
11145
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11146
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11147
0
       bucketpos);
11148
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11149
0
       ((bfd_byte *) flinfo->hash_sec->contents
11150
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11151
0
  }
11152
11153
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11154
0
  {
11155
0
    Elf_Internal_Versym iversym;
11156
0
    Elf_External_Versym *eversym;
11157
11158
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11159
0
      {
11160
0
        if (h->verinfo.verdef == NULL
11161
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11162
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11163
0
    iversym.vs_vers = 1;
11164
0
        else
11165
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11166
0
      }
11167
0
    else
11168
0
      {
11169
0
        if (h->verinfo.vertree == NULL)
11170
0
    iversym.vs_vers = 1;
11171
0
        else
11172
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11173
0
        if (flinfo->info->create_default_symver)
11174
0
    iversym.vs_vers++;
11175
0
      }
11176
11177
    /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11178
       defined locally.  */
11179
0
    if (h->versioned == versioned_hidden && h->def_regular)
11180
0
      iversym.vs_vers |= VERSYM_HIDDEN;
11181
11182
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11183
0
    eversym += h->dynindx;
11184
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11185
0
  }
11186
0
    }
11187
11188
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11189
     strip it from .symtab too.  Obviously we can't do this for
11190
     relocatable output or when needed for --emit-relocs.  */
11191
0
  else if (input_sec == bfd_und_section_ptr
11192
0
     && h->indx != -2
11193
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11194
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11195
0
     && !bfd_link_relocatable (flinfo->info))
11196
0
    return true;
11197
11198
  /* Also strip others that we couldn't earlier due to dynamic symbol
11199
     processing.  */
11200
0
  if (strip)
11201
0
    return true;
11202
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11203
0
    return true;
11204
11205
  /* Output a FILE symbol so that following locals are not associated
11206
     with the wrong input file.  We need one for forced local symbols
11207
     if we've seen more than one FILE symbol or when we have exactly
11208
     one FILE symbol but global symbols are present in a file other
11209
     than the one with the FILE symbol.  We also need one if linker
11210
     defined symbols are present.  In practice these conditions are
11211
     always met, so just emit the FILE symbol unconditionally.  */
11212
0
  if (eoinfo->localsyms
11213
0
      && !eoinfo->file_sym_done
11214
0
      && eoinfo->flinfo->filesym_count != 0)
11215
0
    {
11216
0
      Elf_Internal_Sym fsym;
11217
11218
0
      memset (&fsym, 0, sizeof (fsym));
11219
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11220
0
      fsym.st_shndx = SHN_ABS;
11221
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11222
0
              bfd_und_section_ptr, NULL))
11223
0
  return false;
11224
11225
0
      eoinfo->file_sym_done = true;
11226
0
    }
11227
11228
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11229
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11230
0
           input_sec, h);
11231
0
  if (ret == 0)
11232
0
    {
11233
0
      eoinfo->failed = true;
11234
0
      return false;
11235
0
    }
11236
0
  else if (ret == 1)
11237
0
    h->indx = indx;
11238
0
  else if (h->indx == -2)
11239
0
    abort();
11240
11241
0
  return true;
11242
0
}
11243
11244
/* Return TRUE if special handling is done for relocs in SEC against
11245
   symbols defined in discarded sections.  */
11246
11247
static bool
11248
elf_section_ignore_discarded_relocs (asection *sec)
11249
0
{
11250
0
  const struct elf_backend_data *bed;
11251
11252
0
  switch (sec->sec_info_type)
11253
0
    {
11254
0
    case SEC_INFO_TYPE_STABS:
11255
0
    case SEC_INFO_TYPE_EH_FRAME:
11256
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11257
0
    case SEC_INFO_TYPE_SFRAME:
11258
0
      return true;
11259
0
    default:
11260
0
      break;
11261
0
    }
11262
11263
0
  bed = get_elf_backend_data (sec->owner);
11264
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11265
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11266
0
    return true;
11267
11268
0
  return false;
11269
0
}
11270
11271
/* Return a mask saying how ld should treat relocations in SEC against
11272
   symbols defined in discarded sections.  If this function returns
11273
   COMPLAIN set, ld will issue a warning message.  If this function
11274
   returns PRETEND set, and the discarded section was link-once and the
11275
   same size as the kept link-once section, ld will pretend that the
11276
   symbol was actually defined in the kept section.  Otherwise ld will
11277
   zero the reloc (at least that is the intent, but some cooperation by
11278
   the target dependent code is needed, particularly for REL targets).  */
11279
11280
unsigned int
11281
_bfd_elf_default_action_discarded (asection *sec)
11282
0
{
11283
0
  const struct elf_backend_data *bed;
11284
0
  bed = get_elf_backend_data (sec->owner);
11285
11286
0
  if (sec->flags & SEC_DEBUGGING)
11287
0
    return PRETEND;
11288
11289
0
  if (strcmp (".eh_frame", sec->name) == 0)
11290
0
    return 0;
11291
11292
0
  if (bed->elf_backend_can_make_multiple_eh_frame
11293
0
      && strncmp (sec->name, ".eh_frame.", 10) == 0)
11294
0
    return 0;
11295
11296
0
  if (strcmp (".sframe", sec->name) == 0)
11297
0
    return 0;
11298
11299
0
  if (strcmp (".gcc_except_table", sec->name) == 0)
11300
0
    return 0;
11301
11302
0
  return COMPLAIN | PRETEND;
11303
0
}
11304
11305
/* Find a match between a section and a member of a section group.  */
11306
11307
static asection *
11308
match_group_member (asection *sec, asection *group,
11309
        struct bfd_link_info *info)
11310
0
{
11311
0
  asection *first = elf_next_in_group (group);
11312
0
  asection *s = first;
11313
11314
0
  while (s != NULL)
11315
0
    {
11316
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11317
0
  return s;
11318
11319
0
      s = elf_next_in_group (s);
11320
0
      if (s == first)
11321
0
  break;
11322
0
    }
11323
11324
0
  return NULL;
11325
0
}
11326
11327
/* Check if the kept section of a discarded section SEC can be used
11328
   to replace it.  Return the replacement if it is OK.  Otherwise return
11329
   NULL.  */
11330
11331
asection *
11332
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11333
0
{
11334
0
  asection *kept;
11335
11336
0
  kept = sec->kept_section;
11337
0
  if (kept != NULL)
11338
0
    {
11339
0
      if ((kept->flags & SEC_GROUP) != 0)
11340
0
  kept = match_group_member (sec, kept, info);
11341
0
      if (kept != NULL)
11342
0
  {
11343
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11344
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11345
0
      kept = NULL;
11346
0
    else
11347
0
      {
11348
        /* Get the real kept section.  */
11349
0
        asection *next;
11350
0
        for (next = kept->kept_section;
11351
0
       next != NULL;
11352
0
       next = next->kept_section)
11353
0
    kept = next;
11354
0
      }
11355
0
  }
11356
0
      sec->kept_section = kept;
11357
0
    }
11358
0
  return kept;
11359
0
}
11360
11361
/* Link an input file into the linker output file.  This function
11362
   handles all the sections and relocations of the input file at once.
11363
   This is so that we only have to read the local symbols once, and
11364
   don't have to keep them in memory.  */
11365
11366
static bool
11367
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11368
0
{
11369
0
  int (*relocate_section)
11370
0
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11371
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11372
0
  bfd *output_bfd;
11373
0
  Elf_Internal_Shdr *symtab_hdr;
11374
0
  size_t locsymcount;
11375
0
  size_t extsymoff;
11376
0
  Elf_Internal_Sym *isymbuf;
11377
0
  Elf_Internal_Sym *isym;
11378
0
  Elf_Internal_Sym *isymend;
11379
0
  long *pindex;
11380
0
  asection **ppsection;
11381
0
  asection *o;
11382
0
  const struct elf_backend_data *bed;
11383
0
  struct elf_link_hash_entry **sym_hashes;
11384
0
  bfd_size_type address_size;
11385
0
  bfd_vma r_type_mask;
11386
0
  int r_sym_shift;
11387
0
  bool have_file_sym = false;
11388
11389
0
  output_bfd = flinfo->output_bfd;
11390
0
  bed = get_elf_backend_data (output_bfd);
11391
0
  relocate_section = bed->elf_backend_relocate_section;
11392
11393
  /* If this is a dynamic object, we don't want to do anything here:
11394
     we don't want the local symbols, and we don't want the section
11395
     contents.  */
11396
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11397
0
    return true;
11398
11399
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11400
0
  if (elf_bad_symtab (input_bfd))
11401
0
    {
11402
0
      locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11403
0
      extsymoff = 0;
11404
0
    }
11405
0
  else
11406
0
    {
11407
0
      locsymcount = symtab_hdr->sh_info;
11408
0
      extsymoff = symtab_hdr->sh_info;
11409
0
    }
11410
11411
  /* Enable GNU OSABI features in the output BFD that are used in the input
11412
     BFD.  */
11413
0
  if (bed->elf_osabi == ELFOSABI_NONE
11414
0
      || bed->elf_osabi == ELFOSABI_GNU
11415
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11416
0
    elf_tdata (output_bfd)->has_gnu_osabi
11417
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11418
0
    & (bfd_link_relocatable (flinfo->info)
11419
0
       ? -1 : ~elf_gnu_osabi_retain));
11420
11421
  /* Read the local symbols.  */
11422
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11423
0
  if (isymbuf == NULL && locsymcount != 0)
11424
0
    {
11425
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11426
0
              flinfo->internal_syms,
11427
0
              flinfo->external_syms,
11428
0
              flinfo->locsym_shndx);
11429
0
      if (isymbuf == NULL)
11430
0
  return false;
11431
0
    }
11432
11433
  /* Find local symbol sections and adjust values of symbols in
11434
     SEC_MERGE sections.  Write out those local symbols we know are
11435
     going into the output file.  */
11436
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11437
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11438
0
       isym < isymend;
11439
0
       isym++, pindex++, ppsection++)
11440
0
    {
11441
0
      asection *isec;
11442
0
      const char *name;
11443
0
      Elf_Internal_Sym osym;
11444
0
      long indx;
11445
0
      int ret;
11446
11447
0
      *pindex = -1;
11448
11449
0
      if (elf_bad_symtab (input_bfd))
11450
0
  {
11451
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11452
0
      {
11453
0
        *ppsection = NULL;
11454
0
        continue;
11455
0
      }
11456
0
  }
11457
11458
0
      if (isym->st_shndx == SHN_UNDEF)
11459
0
  isec = bfd_und_section_ptr;
11460
0
      else if (isym->st_shndx == SHN_ABS)
11461
0
  isec = bfd_abs_section_ptr;
11462
0
      else if (isym->st_shndx == SHN_COMMON)
11463
0
  isec = bfd_com_section_ptr;
11464
0
      else
11465
0
  {
11466
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11467
0
    if (isec == NULL)
11468
0
      {
11469
        /* Don't attempt to output symbols with st_shnx in the
11470
     reserved range other than SHN_ABS and SHN_COMMON.  */
11471
0
        isec = bfd_und_section_ptr;
11472
0
      }
11473
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11474
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11475
0
      isym->st_value =
11476
0
        _bfd_merged_section_offset (output_bfd, &isec,
11477
0
            elf_section_data (isec)->sec_info,
11478
0
            isym->st_value);
11479
0
  }
11480
11481
0
      *ppsection = isec;
11482
11483
      /* Don't output the first, undefined, symbol.  In fact, don't
11484
   output any undefined local symbol.  */
11485
0
      if (isec == bfd_und_section_ptr)
11486
0
  continue;
11487
11488
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11489
0
  {
11490
    /* We never output section symbols.  Instead, we use the
11491
       section symbol of the corresponding section in the output
11492
       file.  */
11493
0
    continue;
11494
0
  }
11495
11496
      /* If we are stripping all symbols, we don't want to output this
11497
   one.  */
11498
0
      if (flinfo->info->strip == strip_all)
11499
0
  continue;
11500
11501
      /* If we are discarding all local symbols, we don't want to
11502
   output this one.  If we are generating a relocatable output
11503
   file, then some of the local symbols may be required by
11504
   relocs; we output them below as we discover that they are
11505
   needed.  */
11506
0
      if (flinfo->info->discard == discard_all)
11507
0
  continue;
11508
11509
      /* If this symbol is defined in a section which we are
11510
   discarding, we don't need to keep it.  */
11511
0
      if (isym->st_shndx < SHN_LORESERVE
11512
0
    && (isec->output_section == NULL
11513
0
        || bfd_section_removed_from_list (output_bfd,
11514
0
            isec->output_section)))
11515
0
  continue;
11516
11517
      /* Get the name of the symbol.  */
11518
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11519
0
                isym->st_name);
11520
0
      if (name == NULL)
11521
0
  return false;
11522
11523
      /* See if we are discarding symbols with this name.  */
11524
0
      if ((flinfo->info->strip == strip_some
11525
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11526
0
         == NULL))
11527
0
    || (((flinfo->info->discard == discard_sec_merge
11528
0
    && (isec->flags & SEC_MERGE)
11529
0
    && !bfd_link_relocatable (flinfo->info))
11530
0
         || flinfo->info->discard == discard_l)
11531
0
        && bfd_is_local_label_name (input_bfd, name)))
11532
0
  continue;
11533
11534
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11535
0
  {
11536
0
    if (input_bfd->lto_output)
11537
      /* -flto puts a temp file name here.  This means builds
11538
         are not reproducible.  Discard the symbol.  */
11539
0
      continue;
11540
0
    have_file_sym = true;
11541
0
    flinfo->filesym_count += 1;
11542
0
  }
11543
0
      if (!have_file_sym)
11544
0
  {
11545
    /* In the absence of debug info, bfd_find_nearest_line uses
11546
       FILE symbols to determine the source file for local
11547
       function symbols.  Provide a FILE symbol here if input
11548
       files lack such, so that their symbols won't be
11549
       associated with a previous input file.  It's not the
11550
       source file, but the best we can do.  */
11551
0
    const char *filename;
11552
0
    have_file_sym = true;
11553
0
    flinfo->filesym_count += 1;
11554
0
    memset (&osym, 0, sizeof (osym));
11555
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11556
0
    osym.st_shndx = SHN_ABS;
11557
0
    if (input_bfd->lto_output)
11558
0
      filename = NULL;
11559
0
    else
11560
0
      filename = lbasename (bfd_get_filename (input_bfd));
11561
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11562
0
            bfd_abs_section_ptr, NULL))
11563
0
      return false;
11564
0
  }
11565
11566
0
      osym = *isym;
11567
11568
      /* Adjust the section index for the output file.  */
11569
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11570
0
               isec->output_section);
11571
0
      if (osym.st_shndx == SHN_BAD)
11572
0
  return false;
11573
11574
      /* ELF symbols in relocatable files are section relative, but
11575
   in executable files they are virtual addresses.  Note that
11576
   this code assumes that all ELF sections have an associated
11577
   BFD section with a reasonable value for output_offset; below
11578
   we assume that they also have a reasonable value for
11579
   output_section.  Any special sections must be set up to meet
11580
   these requirements.  */
11581
0
      osym.st_value += isec->output_offset;
11582
0
      if (!bfd_link_relocatable (flinfo->info))
11583
0
  {
11584
0
    osym.st_value += isec->output_section->vma;
11585
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11586
0
      {
11587
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11588
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11589
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11590
0
        else
11591
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11592
0
              STT_NOTYPE);
11593
0
      }
11594
0
  }
11595
11596
0
      indx = bfd_get_symcount (output_bfd);
11597
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11598
0
      if (ret == 0)
11599
0
  return false;
11600
0
      else if (ret == 1)
11601
0
  *pindex = indx;
11602
0
    }
11603
11604
0
  if (bed->s->arch_size == 32)
11605
0
    {
11606
0
      r_type_mask = 0xff;
11607
0
      r_sym_shift = 8;
11608
0
      address_size = 4;
11609
0
    }
11610
0
  else
11611
0
    {
11612
0
      r_type_mask = 0xffffffff;
11613
0
      r_sym_shift = 32;
11614
0
      address_size = 8;
11615
0
    }
11616
11617
  /* Relocate the contents of each section.  */
11618
0
  sym_hashes = elf_sym_hashes (input_bfd);
11619
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11620
0
    {
11621
0
      bfd_byte *contents;
11622
11623
0
      if (! o->linker_mark)
11624
0
  {
11625
    /* This section was omitted from the link.  */
11626
0
    continue;
11627
0
  }
11628
11629
0
      if (!flinfo->info->resolve_section_groups
11630
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11631
0
  {
11632
    /* Deal with the group signature symbol.  */
11633
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11634
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11635
0
    asection *osec = o->output_section;
11636
11637
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11638
0
    if (symndx >= locsymcount
11639
0
        || (elf_bad_symtab (input_bfd)
11640
0
      && flinfo->sections[symndx] == NULL))
11641
0
      {
11642
0
        struct elf_link_hash_entry *h;
11643
11644
0
        h = get_link_hash_entry (sym_hashes, symndx, extsymoff);
11645
0
        if (h == NULL)
11646
0
    {
11647
0
      _bfd_error_handler
11648
        /* xgettext:c-format */
11649
0
        (_("error: %pB: unable to create group section symbol"),
11650
0
         input_bfd);
11651
0
      bfd_set_error (bfd_error_bad_value);
11652
0
      return false;
11653
0
    }       
11654
11655
        /* Arrange for symbol to be output.  */
11656
0
        h->indx = -2;
11657
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11658
0
      }
11659
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11660
0
      {
11661
        /* We'll use the output section target_index.  */
11662
0
        asection *sec = flinfo->sections[symndx]->output_section;
11663
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11664
0
      }
11665
0
    else
11666
0
      {
11667
0
        if (flinfo->indices[symndx] == -1)
11668
0
    {
11669
      /* Otherwise output the local symbol now.  */
11670
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11671
0
      asection *sec = flinfo->sections[symndx]->output_section;
11672
0
      const char *name;
11673
0
      long indx;
11674
0
      int ret;
11675
11676
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11677
0
                symtab_hdr->sh_link,
11678
0
                sym.st_name);
11679
0
      if (name == NULL)
11680
0
        return false;
11681
11682
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11683
0
                    sec);
11684
0
      if (sym.st_shndx == SHN_BAD)
11685
0
        return false;
11686
11687
0
      sym.st_value += o->output_offset;
11688
11689
0
      indx = bfd_get_symcount (output_bfd);
11690
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11691
0
               NULL);
11692
0
      if (ret == 0)
11693
0
        return false;
11694
0
      else if (ret == 1)
11695
0
        flinfo->indices[symndx] = indx;
11696
0
      else
11697
0
        abort ();
11698
0
    }
11699
0
        elf_section_data (osec)->this_hdr.sh_info
11700
0
    = flinfo->indices[symndx];
11701
0
      }
11702
0
  }
11703
11704
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11705
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11706
0
  continue;
11707
11708
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11709
0
  {
11710
    /* Section was created by _bfd_elf_link_create_dynamic_sections
11711
       or somesuch.  */
11712
0
    continue;
11713
0
  }
11714
11715
      /* Get the contents of the section.  They have been cached by a
11716
   relaxation routine.  Note that o is a section in an input
11717
   file, so the contents field will not have been set by any of
11718
   the routines which work on output files.  */
11719
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11720
0
  {
11721
0
    contents = elf_section_data (o)->this_hdr.contents;
11722
0
    if (bed->caches_rawsize
11723
0
        && o->rawsize != 0
11724
0
        && o->rawsize < o->size)
11725
0
      {
11726
0
        memcpy (flinfo->contents, contents, o->rawsize);
11727
0
        contents = flinfo->contents;
11728
0
      }
11729
0
  }
11730
0
      else if (!(o->flags & SEC_RELOC)
11731
0
         && !bed->elf_backend_write_section
11732
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11733
  /* A MERGE section that has no relocations doesn't need the
11734
     contents anymore, they have been recorded earlier.  Except
11735
     if the backend has special provisions for writing sections.  */
11736
0
  contents = NULL;
11737
0
      else
11738
0
  {
11739
0
    contents = flinfo->contents;
11740
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11741
0
                 &contents))
11742
0
      return false;
11743
0
  }
11744
11745
0
      if ((o->flags & SEC_RELOC) != 0)
11746
0
  {
11747
0
    Elf_Internal_Rela *internal_relocs;
11748
0
    Elf_Internal_Rela *rel, *relend;
11749
0
    int action_discarded;
11750
0
    int ret;
11751
11752
    /* Get the swapped relocs.  */
11753
0
    internal_relocs
11754
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11755
0
                flinfo->external_relocs,
11756
0
                flinfo->internal_relocs,
11757
0
                false);
11758
0
    if (internal_relocs == NULL
11759
0
        && o->reloc_count > 0)
11760
0
      return false;
11761
11762
0
    action_discarded = -1;
11763
0
    if (!elf_section_ignore_discarded_relocs (o))
11764
0
      action_discarded = (*bed->action_discarded) (o);
11765
11766
    /* Run through the relocs evaluating complex reloc symbols and
11767
       looking for relocs against symbols from discarded sections
11768
       or section symbols from removed link-once sections.
11769
       Complain about relocs against discarded sections.  Zero
11770
       relocs against removed link-once sections.  */
11771
11772
0
    rel = internal_relocs;
11773
0
    relend = rel + o->reloc_count;
11774
0
    for ( ; rel < relend; rel++)
11775
0
      {
11776
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11777
0
        unsigned int s_type;
11778
0
        asection **ps, *sec;
11779
0
        struct elf_link_hash_entry *h = NULL;
11780
0
        const char *sym_name;
11781
11782
0
        if (r_symndx == STN_UNDEF)
11783
0
    continue;
11784
11785
0
        if (r_symndx >= locsymcount
11786
0
      || (elf_bad_symtab (input_bfd)
11787
0
          && flinfo->sections[r_symndx] == NULL))
11788
0
    {
11789
0
      h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff);
11790
11791
      /* Badly formatted input files can contain relocs that
11792
         reference non-existant symbols.  Check here so that
11793
         we do not seg fault.  */
11794
0
      if (h == NULL)
11795
0
        {
11796
0
          _bfd_error_handler
11797
      /* xgettext:c-format */
11798
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11799
0
         "that references a non-existent global symbol"),
11800
0
       input_bfd, (uint64_t) rel->r_info, o);
11801
0
          bfd_set_error (bfd_error_bad_value);
11802
0
          return false;
11803
0
        }
11804
11805
0
      s_type = h->type;
11806
11807
      /* If a plugin symbol is referenced from a non-IR file,
11808
         mark the symbol as undefined.  Note that the
11809
         linker may attach linker created dynamic sections
11810
         to the plugin bfd.  Symbols defined in linker
11811
         created sections are not plugin symbols.  */
11812
0
      if ((h->root.non_ir_ref_regular
11813
0
           || h->root.non_ir_ref_dynamic)
11814
0
          && (h->root.type == bfd_link_hash_defined
11815
0
        || h->root.type == bfd_link_hash_defweak)
11816
0
          && (h->root.u.def.section->flags
11817
0
        & SEC_LINKER_CREATED) == 0
11818
0
          && h->root.u.def.section->owner != NULL
11819
0
          && (h->root.u.def.section->owner->flags
11820
0
        & BFD_PLUGIN) != 0)
11821
0
        {
11822
0
          h->root.type = bfd_link_hash_undefined;
11823
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11824
0
        }
11825
11826
0
      ps = NULL;
11827
0
      if (h->root.type == bfd_link_hash_defined
11828
0
          || h->root.type == bfd_link_hash_defweak)
11829
0
        ps = &h->root.u.def.section;
11830
11831
0
      sym_name = h->root.root.string;
11832
0
    }
11833
0
        else
11834
0
    {
11835
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11836
11837
0
      s_type = ELF_ST_TYPE (sym->st_info);
11838
0
      ps = &flinfo->sections[r_symndx];
11839
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11840
0
                 sym, *ps);
11841
0
    }
11842
11843
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11844
0
      && !bfd_link_relocatable (flinfo->info))
11845
0
    {
11846
0
      bfd_vma val;
11847
0
      bfd_vma dot = (rel->r_offset
11848
0
         + o->output_offset + o->output_section->vma);
11849
#ifdef DEBUG
11850
      printf ("Encountered a complex symbol!");
11851
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11852
        bfd_get_filename (input_bfd), o->name,
11853
        (long) (rel - internal_relocs));
11854
      printf (" symbol: idx  %8.8lx, name %s\n",
11855
        r_symndx, sym_name);
11856
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11857
        (unsigned long) rel->r_info,
11858
        (unsigned long) rel->r_offset);
11859
#endif
11860
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11861
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11862
0
        return false;
11863
11864
      /* Symbol evaluated OK.  Update to absolute value.  */
11865
0
      set_symbol_value (input_bfd, isymbuf, locsymcount,
11866
0
            r_symndx, val);
11867
0
      continue;
11868
0
    }
11869
11870
0
        if (action_discarded != -1 && ps != NULL)
11871
0
    {
11872
      /* Complain if the definition comes from a
11873
         discarded section.  */
11874
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11875
0
        {
11876
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11877
0
          if (action_discarded & COMPLAIN)
11878
0
      (*flinfo->info->callbacks->einfo)
11879
        /* xgettext:c-format */
11880
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11881
0
           "defined in discarded section `%pA' of %pB\n"),
11882
0
         sym_name, o, input_bfd, sec, sec->owner);
11883
11884
          /* Try to do the best we can to support buggy old
11885
       versions of gcc.  Pretend that the symbol is
11886
       really defined in the kept linkonce section.
11887
       FIXME: This is quite broken.  Modifying the
11888
       symbol here means we will be changing all later
11889
       uses of the symbol, not just in this section.  */
11890
0
          if (action_discarded & PRETEND)
11891
0
      {
11892
0
        asection *kept;
11893
11894
0
        kept = _bfd_elf_check_kept_section (sec,
11895
0
                    flinfo->info);
11896
0
        if (kept != NULL)
11897
0
          {
11898
0
            *ps = kept;
11899
0
            continue;
11900
0
          }
11901
0
      }
11902
0
        }
11903
0
    }
11904
0
      }
11905
11906
    /* Relocate the section by invoking a back end routine.
11907
11908
       The back end routine is responsible for adjusting the
11909
       section contents as necessary, and (if using Rela relocs
11910
       and generating a relocatable output file) adjusting the
11911
       reloc addend as necessary.
11912
11913
       The back end routine does not have to worry about setting
11914
       the reloc address or the reloc symbol index.
11915
11916
       The back end routine is given a pointer to the swapped in
11917
       internal symbols, and can access the hash table entries
11918
       for the external symbols via elf_sym_hashes (input_bfd).
11919
11920
       When generating relocatable output, the back end routine
11921
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11922
       output symbol is going to be a section symbol
11923
       corresponding to the output section, which will require
11924
       the addend to be adjusted.  */
11925
11926
0
    ret = (*relocate_section) (output_bfd, flinfo->info,
11927
0
             input_bfd, o, contents,
11928
0
             internal_relocs,
11929
0
             isymbuf,
11930
0
             flinfo->sections);
11931
0
    if (!ret)
11932
0
      return false;
11933
11934
0
    if (ret == 2
11935
0
        || bfd_link_relocatable (flinfo->info)
11936
0
        || flinfo->info->emitrelocations)
11937
0
      {
11938
0
        Elf_Internal_Rela *irela;
11939
0
        Elf_Internal_Rela *irelaend, *irelamid;
11940
0
        bfd_vma last_offset;
11941
0
        struct elf_link_hash_entry **rel_hash;
11942
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11943
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11944
0
        unsigned int next_erel;
11945
0
        bool rela_normal;
11946
0
        struct bfd_elf_section_data *esdi, *esdo;
11947
11948
0
        esdi = elf_section_data (o);
11949
0
        esdo = elf_section_data (o->output_section);
11950
0
        rela_normal = false;
11951
11952
        /* Adjust the reloc addresses and symbol indices.  */
11953
11954
0
        irela = internal_relocs;
11955
0
        irelaend = irela + o->reloc_count;
11956
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11957
        /* We start processing the REL relocs, if any.  When we reach
11958
     IRELAMID in the loop, we switch to the RELA relocs.  */
11959
0
        irelamid = irela;
11960
0
        if (esdi->rel.hdr != NULL)
11961
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11962
0
           * bed->s->int_rels_per_ext_rel);
11963
0
        rel_hash_list = rel_hash;
11964
0
        rela_hash_list = NULL;
11965
0
        last_offset = o->output_offset;
11966
0
        if (!bfd_link_relocatable (flinfo->info))
11967
0
    last_offset += o->output_section->vma;
11968
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11969
0
    {
11970
0
      unsigned long r_symndx;
11971
0
      asection *sec;
11972
0
      Elf_Internal_Sym sym;
11973
11974
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
11975
0
        {
11976
0
          rel_hash++;
11977
0
          next_erel = 0;
11978
0
        }
11979
11980
0
      if (irela == irelamid)
11981
0
        {
11982
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11983
0
          rela_hash_list = rel_hash;
11984
0
          rela_normal = bed->rela_normal;
11985
0
        }
11986
11987
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
11988
0
                   flinfo->info, o,
11989
0
                   irela->r_offset);
11990
0
      if (irela->r_offset >= (bfd_vma) -2)
11991
0
        {
11992
          /* This is a reloc for a deleted entry or somesuch.
11993
       Turn it into an R_*_NONE reloc, at the same
11994
       offset as the last reloc.  elf_eh_frame.c and
11995
       bfd_elf_discard_info rely on reloc offsets
11996
       being ordered.  */
11997
0
          irela->r_offset = last_offset;
11998
0
          irela->r_info = 0;
11999
0
          irela->r_addend = 0;
12000
0
          continue;
12001
0
        }
12002
12003
0
      irela->r_offset += o->output_offset;
12004
12005
      /* Relocs in an executable have to be virtual addresses.  */
12006
0
      if (!bfd_link_relocatable (flinfo->info))
12007
0
        irela->r_offset += o->output_section->vma;
12008
12009
0
      last_offset = irela->r_offset;
12010
12011
0
      r_symndx = irela->r_info >> r_sym_shift;
12012
0
      if (r_symndx == STN_UNDEF)
12013
0
        continue;
12014
12015
0
      if (r_symndx >= locsymcount
12016
0
          || (elf_bad_symtab (input_bfd)
12017
0
        && flinfo->sections[r_symndx] == NULL))
12018
0
        {
12019
0
          struct elf_link_hash_entry *rh;
12020
12021
          /* This is a reloc against a global symbol.  We
12022
       have not yet output all the local symbols, so
12023
       we do not know the symbol index of any global
12024
       symbol.  We set the rel_hash entry for this
12025
       reloc to point to the global hash table entry
12026
       for this symbol.  The symbol index is then
12027
       set at the end of bfd_elf_final_link.  */
12028
0
          rh = get_link_hash_entry (elf_sym_hashes (input_bfd),
12029
0
            r_symndx, extsymoff);
12030
0
          if (rh == NULL)
12031
0
      {
12032
        /* FIXME: Generate an error ?  */
12033
0
        continue;
12034
0
      }
12035
12036
          /* Setting the index to -2 tells elf_link_output_extsym
12037
       that this symbol is used by a reloc.  */
12038
0
          BFD_ASSERT (rh->indx < 0);
12039
0
          rh->indx = -2;
12040
0
          *rel_hash = rh;
12041
12042
0
          continue;
12043
0
        }
12044
12045
      /* This is a reloc against a local symbol.  */
12046
12047
0
      *rel_hash = NULL;
12048
0
      sym = isymbuf[r_symndx];
12049
0
      sec = flinfo->sections[r_symndx];
12050
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12051
0
        {
12052
          /* I suppose the backend ought to fill in the
12053
       section of any STT_SECTION symbol against a
12054
       processor specific section.  */
12055
0
          r_symndx = STN_UNDEF;
12056
0
          if (bfd_is_abs_section (sec))
12057
0
      ;
12058
0
          else if (sec == NULL || sec->owner == NULL)
12059
0
      {
12060
0
        bfd_set_error (bfd_error_bad_value);
12061
0
        return false;
12062
0
      }
12063
0
          else
12064
0
      {
12065
0
        asection *osec = sec->output_section;
12066
12067
        /* If we have discarded a section, the output
12068
           section will be the absolute section.  In
12069
           case of discarded SEC_MERGE sections, use
12070
           the kept section.  relocate_section should
12071
           have already handled discarded linkonce
12072
           sections.  */
12073
0
        if (bfd_is_abs_section (osec)
12074
0
            && sec->kept_section != NULL
12075
0
            && sec->kept_section->output_section != NULL)
12076
0
          {
12077
0
            osec = sec->kept_section->output_section;
12078
0
            irela->r_addend -= osec->vma;
12079
0
          }
12080
12081
0
        if (!bfd_is_abs_section (osec))
12082
0
          {
12083
0
            r_symndx = osec->target_index;
12084
0
            if (r_symndx == STN_UNDEF)
12085
0
        {
12086
0
          irela->r_addend += osec->vma;
12087
0
          osec = _bfd_nearby_section (output_bfd, osec,
12088
0
                    osec->vma);
12089
0
          irela->r_addend -= osec->vma;
12090
0
          r_symndx = osec->target_index;
12091
0
        }
12092
0
          }
12093
0
      }
12094
12095
          /* Adjust the addend according to where the
12096
       section winds up in the output section.  */
12097
0
          if (rela_normal)
12098
0
      irela->r_addend += sec->output_offset;
12099
0
        }
12100
0
      else
12101
0
        {
12102
0
          if (flinfo->indices[r_symndx] == -1)
12103
0
      {
12104
0
        unsigned long shlink;
12105
0
        const char *name;
12106
0
        asection *osec;
12107
0
        long indx;
12108
12109
0
        if (flinfo->info->strip == strip_all)
12110
0
          {
12111
            /* You can't do ld -r -s.  */
12112
0
            bfd_set_error (bfd_error_invalid_operation);
12113
0
            return false;
12114
0
          }
12115
12116
        /* This symbol was skipped earlier, but
12117
           since it is needed by a reloc, we
12118
           must output it now.  */
12119
0
        shlink = symtab_hdr->sh_link;
12120
0
        name = (bfd_elf_string_from_elf_section
12121
0
          (input_bfd, shlink, sym.st_name));
12122
0
        if (name == NULL)
12123
0
          return false;
12124
12125
0
        osec = sec->output_section;
12126
0
        sym.st_shndx =
12127
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12128
0
                     osec);
12129
0
        if (sym.st_shndx == SHN_BAD)
12130
0
          return false;
12131
12132
0
        sym.st_value += sec->output_offset;
12133
0
        if (!bfd_link_relocatable (flinfo->info))
12134
0
          {
12135
0
            sym.st_value += osec->vma;
12136
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12137
0
        {
12138
0
          struct elf_link_hash_table *htab
12139
0
            = elf_hash_table (flinfo->info);
12140
12141
          /* STT_TLS symbols are relative to PT_TLS
12142
             segment base.  */
12143
0
          if (htab->tls_sec != NULL)
12144
0
            sym.st_value -= htab->tls_sec->vma;
12145
0
          else
12146
0
            sym.st_info
12147
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12148
0
                 STT_NOTYPE);
12149
0
        }
12150
0
          }
12151
12152
0
        indx = bfd_get_symcount (output_bfd);
12153
0
        ret = elf_link_output_symstrtab (flinfo, name,
12154
0
                 &sym, sec,
12155
0
                 NULL);
12156
0
        if (ret == 0)
12157
0
          return false;
12158
0
        else if (ret == 1)
12159
0
          flinfo->indices[r_symndx] = indx;
12160
0
        else
12161
0
          abort ();
12162
0
      }
12163
12164
0
          r_symndx = flinfo->indices[r_symndx];
12165
0
        }
12166
12167
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12168
0
           | (irela->r_info & r_type_mask));
12169
0
    }
12170
12171
        /* Swap out the relocs.  */
12172
0
        input_rel_hdr = esdi->rel.hdr;
12173
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12174
0
    {
12175
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12176
0
                 input_rel_hdr,
12177
0
                 internal_relocs,
12178
0
                 rel_hash_list))
12179
0
        return false;
12180
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12181
0
              * bed->s->int_rels_per_ext_rel);
12182
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12183
0
    }
12184
12185
0
        input_rela_hdr = esdi->rela.hdr;
12186
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12187
0
    {
12188
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12189
0
                 input_rela_hdr,
12190
0
                 internal_relocs,
12191
0
                 rela_hash_list))
12192
0
        return false;
12193
0
    }
12194
0
      }
12195
0
  }
12196
12197
      /* Write out the modified section contents.  */
12198
0
      if (bed->elf_backend_write_section
12199
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12200
0
            contents))
12201
0
  {
12202
    /* Section written out.  */
12203
0
  }
12204
0
      else switch (o->sec_info_type)
12205
0
  {
12206
0
  case SEC_INFO_TYPE_STABS:
12207
0
    if (! (_bfd_write_section_stabs
12208
0
     (output_bfd,
12209
0
      &elf_hash_table (flinfo->info)->stab_info,
12210
0
      o, &elf_section_data (o)->sec_info, contents)))
12211
0
      return false;
12212
0
    break;
12213
0
  case SEC_INFO_TYPE_MERGE:
12214
0
    if (! _bfd_write_merged_section (output_bfd, o,
12215
0
             elf_section_data (o)->sec_info))
12216
0
      return false;
12217
0
    break;
12218
0
  case SEC_INFO_TYPE_EH_FRAME:
12219
0
    {
12220
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12221
0
               o, contents))
12222
0
        return false;
12223
0
    }
12224
0
    break;
12225
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12226
0
    {
12227
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12228
0
               flinfo->info,
12229
0
               o, contents))
12230
0
        return false;
12231
0
    }
12232
0
    break;
12233
0
  case SEC_INFO_TYPE_SFRAME:
12234
0
      {
12235
        /* Merge SFrame section into the SFrame encoder context of the
12236
     output_bfd's section.  The final .sframe output section will
12237
     be written out later.  */
12238
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12239
0
              o, contents))
12240
0
    return false;
12241
0
      }
12242
0
      break;
12243
0
  default:
12244
0
    {
12245
0
      if (! (o->flags & SEC_EXCLUDE))
12246
0
        {
12247
0
    file_ptr offset = (file_ptr) o->output_offset;
12248
0
    bfd_size_type todo = o->size;
12249
12250
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12251
12252
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12253
0
        && o->size > address_size)
12254
0
      {
12255
        /* Reverse-copy input section to output.  */
12256
12257
0
        if ((o->size & (address_size - 1)) != 0
12258
0
      || (o->reloc_count != 0
12259
0
          && (o->size * bed->s->int_rels_per_ext_rel
12260
0
        != o->reloc_count * address_size)))
12261
0
          {
12262
0
      _bfd_error_handler
12263
        /* xgettext:c-format */
12264
0
        (_("error: %pB: size of section %pA is not "
12265
0
           "multiple of address size"),
12266
0
         input_bfd, o);
12267
0
      bfd_set_error (bfd_error_bad_value);
12268
0
      return false;
12269
0
          }
12270
12271
0
        do
12272
0
          {
12273
0
      todo -= address_size;
12274
0
      if (! bfd_set_section_contents (output_bfd,
12275
0
              o->output_section,
12276
0
              contents + todo,
12277
0
              offset,
12278
0
              address_size))
12279
0
        return false;
12280
0
      if (todo == 0)
12281
0
        break;
12282
0
      offset += address_size;
12283
0
          }
12284
0
        while (1);
12285
0
      }
12286
0
    else if (! bfd_set_section_contents (output_bfd,
12287
0
                 o->output_section,
12288
0
                 contents,
12289
0
                 offset, todo))
12290
0
      return false;
12291
0
        }
12292
0
    }
12293
0
    break;
12294
0
  }
12295
12296
      /* Munmap the section contents for each input section.  */
12297
0
      _bfd_elf_link_munmap_section_contents (o);
12298
0
    }
12299
12300
0
  return true;
12301
0
}
12302
12303
/* Generate a reloc when linking an ELF file.  This is a reloc
12304
   requested by the linker, and does not come from any input file.  This
12305
   is used to build constructor and destructor tables when linking
12306
   with -Ur.  */
12307
12308
static bool
12309
elf_reloc_link_order (bfd *output_bfd,
12310
          struct bfd_link_info *info,
12311
          asection *output_section,
12312
          struct bfd_link_order *link_order)
12313
0
{
12314
0
  reloc_howto_type *howto;
12315
0
  long indx;
12316
0
  bfd_vma offset;
12317
0
  bfd_vma addend;
12318
0
  struct bfd_elf_section_reloc_data *reldata;
12319
0
  struct elf_link_hash_entry **rel_hash_ptr;
12320
0
  Elf_Internal_Shdr *rel_hdr;
12321
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12322
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12323
0
  bfd_byte *erel;
12324
0
  unsigned int i;
12325
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12326
12327
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12328
0
  if (howto == NULL)
12329
0
    {
12330
0
      bfd_set_error (bfd_error_bad_value);
12331
0
      return false;
12332
0
    }
12333
12334
0
  addend = link_order->u.reloc.p->addend;
12335
12336
0
  if (esdo->rel.hdr)
12337
0
    reldata = &esdo->rel;
12338
0
  else if (esdo->rela.hdr)
12339
0
    reldata = &esdo->rela;
12340
0
  else
12341
0
    {
12342
0
      reldata = NULL;
12343
0
      BFD_ASSERT (0);
12344
0
    }
12345
12346
  /* Figure out the symbol index.  */
12347
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12348
0
  if (link_order->type == bfd_section_reloc_link_order)
12349
0
    {
12350
0
      indx = link_order->u.reloc.p->u.section->target_index;
12351
0
      BFD_ASSERT (indx != 0);
12352
0
      *rel_hash_ptr = NULL;
12353
0
    }
12354
0
  else
12355
0
    {
12356
0
      struct elf_link_hash_entry *h;
12357
12358
      /* Treat a reloc against a defined symbol as though it were
12359
   actually against the section.  */
12360
0
      h = ((struct elf_link_hash_entry *)
12361
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12362
0
           link_order->u.reloc.p->u.name,
12363
0
           false, false, true));
12364
0
      if (h != NULL
12365
0
    && (h->root.type == bfd_link_hash_defined
12366
0
        || h->root.type == bfd_link_hash_defweak))
12367
0
  {
12368
0
    asection *section;
12369
12370
0
    section = h->root.u.def.section;
12371
0
    indx = section->output_section->target_index;
12372
0
    *rel_hash_ptr = NULL;
12373
    /* It seems that we ought to add the symbol value to the
12374
       addend here, but in practice it has already been added
12375
       because it was passed to constructor_callback.  */
12376
0
    addend += section->output_section->vma + section->output_offset;
12377
0
  }
12378
0
      else if (h != NULL)
12379
0
  {
12380
    /* Setting the index to -2 tells elf_link_output_extsym that
12381
       this symbol is used by a reloc.  */
12382
0
    h->indx = -2;
12383
0
    *rel_hash_ptr = h;
12384
0
    indx = 0;
12385
0
  }
12386
0
      else
12387
0
  {
12388
0
    (*info->callbacks->unattached_reloc)
12389
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12390
0
    indx = 0;
12391
0
  }
12392
0
    }
12393
12394
  /* If this is an inplace reloc, we must write the addend into the
12395
     object file.  */
12396
0
  if (howto->partial_inplace && addend != 0)
12397
0
    {
12398
0
      bfd_size_type size;
12399
0
      bfd_reloc_status_type rstat;
12400
0
      bfd_byte *buf;
12401
0
      bool ok;
12402
0
      const char *sym_name;
12403
0
      bfd_size_type octets;
12404
12405
0
      size = (bfd_size_type) bfd_get_reloc_size (howto);
12406
0
      buf = (bfd_byte *) bfd_zmalloc (size);
12407
0
      if (buf == NULL && size != 0)
12408
0
  return false;
12409
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12410
0
      switch (rstat)
12411
0
  {
12412
0
  case bfd_reloc_ok:
12413
0
    break;
12414
12415
0
  default:
12416
0
  case bfd_reloc_outofrange:
12417
0
    abort ();
12418
12419
0
  case bfd_reloc_overflow:
12420
0
    if (link_order->type == bfd_section_reloc_link_order)
12421
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12422
0
    else
12423
0
      sym_name = link_order->u.reloc.p->u.name;
12424
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12425
0
                howto->name, addend, NULL, NULL,
12426
0
                (bfd_vma) 0);
12427
0
    break;
12428
0
  }
12429
12430
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12431
0
               output_section);
12432
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12433
0
             octets, size);
12434
0
      free (buf);
12435
0
      if (! ok)
12436
0
  return false;
12437
0
    }
12438
12439
  /* The address of a reloc is relative to the section in a
12440
     relocatable file, and is a virtual address in an executable
12441
     file.  */
12442
0
  offset = link_order->offset;
12443
0
  if (! bfd_link_relocatable (info))
12444
0
    offset += output_section->vma;
12445
12446
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12447
0
    {
12448
0
      irel[i].r_offset = offset;
12449
0
      irel[i].r_info = 0;
12450
0
      irel[i].r_addend = 0;
12451
0
    }
12452
0
  if (bed->s->arch_size == 32)
12453
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12454
0
  else
12455
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12456
12457
0
  rel_hdr = reldata->hdr;
12458
0
  erel = rel_hdr->contents;
12459
0
  if (rel_hdr->sh_type == SHT_REL)
12460
0
    {
12461
0
      erel += reldata->count * bed->s->sizeof_rel;
12462
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12463
0
    }
12464
0
  else
12465
0
    {
12466
0
      irel[0].r_addend = addend;
12467
0
      erel += reldata->count * bed->s->sizeof_rela;
12468
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12469
0
    }
12470
12471
0
  ++reldata->count;
12472
12473
0
  return true;
12474
0
}
12475
12476
/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12477
   Returns TRUE upon success, FALSE otherwise.  */
12478
12479
static bool
12480
elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12481
0
{
12482
0
  bool ret = false;
12483
0
  bfd *implib_bfd;
12484
0
  const struct elf_backend_data *bed;
12485
0
  flagword flags;
12486
0
  enum bfd_architecture arch;
12487
0
  unsigned int mach;
12488
0
  asymbol **sympp = NULL;
12489
0
  long symsize;
12490
0
  long symcount;
12491
0
  long src_count;
12492
0
  elf_symbol_type *osymbuf;
12493
0
  size_t amt;
12494
12495
0
  implib_bfd = info->out_implib_bfd;
12496
0
  bed = get_elf_backend_data (abfd);
12497
12498
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12499
0
    return false;
12500
12501
  /* Use flag from executable but make it a relocatable object.  */
12502
0
  flags = bfd_get_file_flags (abfd);
12503
0
  flags &= ~HAS_RELOC;
12504
0
  if (!bfd_set_start_address (implib_bfd, 0)
12505
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12506
0
    return false;
12507
12508
  /* Copy architecture of output file to import library file.  */
12509
0
  arch = bfd_get_arch (abfd);
12510
0
  mach = bfd_get_mach (abfd);
12511
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12512
0
      && (abfd->target_defaulted
12513
0
    || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12514
0
    return false;
12515
12516
  /* Get symbol table size.  */
12517
0
  symsize = bfd_get_symtab_upper_bound (abfd);
12518
0
  if (symsize < 0)
12519
0
    return false;
12520
12521
  /* Read in the symbol table.  */
12522
0
  sympp = (asymbol **) bfd_malloc (symsize);
12523
0
  if (sympp == NULL)
12524
0
    return false;
12525
12526
0
  symcount = bfd_canonicalize_symtab (abfd, sympp);
12527
0
  if (symcount < 0)
12528
0
    goto free_sym_buf;
12529
12530
  /* Allow the BFD backend to copy any private header data it
12531
     understands from the output BFD to the import library BFD.  */
12532
0
  if (! bfd_copy_private_header_data (abfd, implib_bfd))
12533
0
    goto free_sym_buf;
12534
12535
  /* Filter symbols to appear in the import library.  */
12536
0
  if (bed->elf_backend_filter_implib_symbols)
12537
0
    symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12538
0
                   symcount);
12539
0
  else
12540
0
    symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12541
0
  if (symcount == 0)
12542
0
    {
12543
0
      bfd_set_error (bfd_error_no_symbols);
12544
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12545
0
        implib_bfd);
12546
0
      goto free_sym_buf;
12547
0
    }
12548
12549
12550
  /* Make symbols absolute.  */
12551
0
  amt = symcount * sizeof (*osymbuf);
12552
0
  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12553
0
  if (osymbuf == NULL)
12554
0
    goto free_sym_buf;
12555
12556
0
  for (src_count = 0; src_count < symcount; src_count++)
12557
0
    {
12558
0
      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12559
0
        sizeof (*osymbuf));
12560
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12561
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12562
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12563
0
      osymbuf[src_count].internal_elf_sym.st_value =
12564
0
  osymbuf[src_count].symbol.value;
12565
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12566
0
    }
12567
12568
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12569
12570
  /* Allow the BFD backend to copy any private data it understands
12571
     from the output BFD to the import library BFD.  This is done last
12572
     to permit the routine to look at the filtered symbol table.  */
12573
0
  if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12574
0
    goto free_sym_buf;
12575
12576
0
  if (!bfd_close (implib_bfd))
12577
0
    goto free_sym_buf;
12578
12579
0
  ret = true;
12580
12581
0
 free_sym_buf:
12582
0
  free (sympp);
12583
0
  return ret;
12584
0
}
12585
12586
static void
12587
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12588
0
{
12589
0
  asection *o;
12590
12591
0
  if (flinfo->symstrtab != NULL)
12592
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12593
0
  free (flinfo->contents);
12594
0
  free (flinfo->external_relocs);
12595
0
  free (flinfo->internal_relocs);
12596
0
  free (flinfo->external_syms);
12597
0
  free (flinfo->locsym_shndx);
12598
0
  free (flinfo->internal_syms);
12599
0
  free (flinfo->indices);
12600
0
  free (flinfo->sections);
12601
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12602
0
    free (flinfo->symshndxbuf);
12603
0
  for (o = obfd->sections; o != NULL; o = o->next)
12604
0
    {
12605
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12606
0
      free (esdo->rel.hashes);
12607
0
      free (esdo->rela.hashes);
12608
0
    }
12609
0
}
12610
12611
/* Do the final step of an ELF link.  */
12612
12613
bool
12614
bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12615
0
{
12616
0
  bool dynamic;
12617
0
  bool emit_relocs;
12618
0
  bfd *dynobj;
12619
0
  struct elf_final_link_info flinfo;
12620
0
  asection *o;
12621
0
  struct bfd_link_order *p;
12622
0
  bfd *sub;
12623
0
  bfd_size_type max_contents_size;
12624
0
  bfd_size_type max_external_reloc_size;
12625
0
  bfd_size_type max_internal_reloc_count;
12626
0
  bfd_size_type max_sym_count;
12627
0
  bfd_size_type max_sym_shndx_count;
12628
0
  Elf_Internal_Sym elfsym;
12629
0
  unsigned int i;
12630
0
  Elf_Internal_Shdr *symtab_hdr;
12631
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12632
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12633
0
  struct elf_outext_info eoinfo;
12634
0
  bool merged;
12635
0
  size_t relativecount;
12636
0
  size_t relr_entsize;
12637
0
  asection *reldyn = 0;
12638
0
  bfd_size_type amt;
12639
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12640
0
  bool sections_removed;
12641
12642
0
  if (!is_elf_hash_table (&htab->root))
12643
0
    return false;
12644
12645
0
  if (bfd_link_pic (info))
12646
0
    abfd->flags |= DYNAMIC;
12647
12648
0
  dynamic = htab->dynamic_sections_created;
12649
0
  dynobj = htab->dynobj;
12650
12651
0
  emit_relocs = (bfd_link_relocatable (info)
12652
0
     || info->emitrelocations);
12653
12654
0
  memset (&flinfo, 0, sizeof (flinfo));
12655
0
  flinfo.info = info;
12656
0
  flinfo.output_bfd = abfd;
12657
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12658
0
  if (flinfo.symstrtab == NULL)
12659
0
    return false;
12660
12661
0
  if (! dynamic)
12662
0
    {
12663
0
      flinfo.hash_sec = NULL;
12664
0
      flinfo.symver_sec = NULL;
12665
0
    }
12666
0
  else
12667
0
    {
12668
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12669
      /* Note that dynsym_sec can be NULL (on VMS).  */
12670
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12671
      /* Note that it is OK if symver_sec is NULL.  */
12672
0
    }
12673
12674
0
  if (info->unique_symbol
12675
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12676
0
             local_hash_newfunc,
12677
0
             sizeof (struct local_hash_entry)))
12678
0
    return false;
12679
12680
  /* The object attributes have been merged.  Remove the input
12681
     sections from the link, and set the contents of the output
12682
     section.  */
12683
0
  sections_removed = false;
12684
0
  const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12685
0
  for (o = abfd->sections; o != NULL; o = o->next)
12686
0
    {
12687
0
      bool remove_section = false;
12688
12689
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12690
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12691
0
  {
12692
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12693
0
      {
12694
0
        asection *input_section;
12695
12696
0
        if (p->type != bfd_indirect_link_order)
12697
0
    continue;
12698
0
        input_section = p->u.indirect.section;
12699
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12700
     elf_link_input_bfd ignores this section.  */
12701
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12702
0
      }
12703
12704
    /* Skip this section later on.  */
12705
0
    o->map_head.link_order = NULL;
12706
12707
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
12708
    /* Once ELF headers have been written, the size of a section is
12709
       frozen. We need to set the size of the attribute section before
12710
       _bfd_elf_compute_section_file_positions.  */
12711
0
    bfd_set_section_size (o, attr_size);
12712
0
    if (attr_size > 0)
12713
0
      elf_obj_build_attributes (abfd) = o;
12714
0
    else
12715
0
      remove_section = true;
12716
0
  }
12717
0
      else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12718
0
  {
12719
    /* Remove empty group section from linker output.  */
12720
0
    remove_section = true;
12721
0
  }
12722
0
      if (remove_section)
12723
0
  {
12724
0
    o->flags |= SEC_EXCLUDE;
12725
0
    bfd_section_list_remove (abfd, o);
12726
0
    abfd->section_count--;
12727
0
    sections_removed = true;
12728
0
  }
12729
0
    }
12730
0
  if (sections_removed)
12731
0
    _bfd_fix_excluded_sec_syms (abfd, info);
12732
12733
  /* Count up the number of relocations we will output for each output
12734
     section, so that we know the sizes of the reloc sections.  We
12735
     also figure out some maximum sizes.  */
12736
0
#ifdef USE_MMAP
12737
0
  if (bed->use_mmap)
12738
0
    {
12739
      /* Mmap is used only if section size >= the minimum mmap section
12740
   size.  The initial max_contents_size value covers all sections
12741
   smaller than the minimum mmap section size.  It may be increased
12742
   for compressed or linker created sections or sections whose
12743
   rawsize != size.  max_external_reloc_size covers all relocation
12744
   sections smaller than the minimum mmap section size.  */
12745
0
      max_contents_size = _bfd_minimum_mmap_size;
12746
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12747
0
    }
12748
0
  else
12749
0
#endif
12750
0
    {
12751
0
      max_contents_size = 0;
12752
0
      max_external_reloc_size = 0;
12753
0
    }
12754
0
  max_internal_reloc_count = 0;
12755
0
  max_sym_count = 0;
12756
0
  max_sym_shndx_count = 0;
12757
0
  merged = false;
12758
0
  for (o = abfd->sections; o != NULL; o = o->next)
12759
0
    {
12760
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12761
0
      o->reloc_count = 0;
12762
12763
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12764
0
  {
12765
0
    unsigned int reloc_count = 0;
12766
0
    unsigned int additional_reloc_count = 0;
12767
0
    struct bfd_elf_section_data *esdi = NULL;
12768
12769
0
    if (p->type == bfd_section_reloc_link_order
12770
0
        || p->type == bfd_symbol_reloc_link_order)
12771
0
      reloc_count = 1;
12772
0
    else if (p->type == bfd_indirect_link_order)
12773
0
      {
12774
0
        asection *sec;
12775
12776
0
        sec = p->u.indirect.section;
12777
12778
        /* Mark all sections which are to be included in the
12779
     link.  This will normally be every section.  We need
12780
     to do this so that we can identify any sections which
12781
     the linker has decided to not include.  */
12782
0
        sec->linker_mark = true;
12783
12784
0
        if (sec->flags & SEC_MERGE)
12785
0
    merged = true;
12786
12787
0
#ifdef USE_MMAP
12788
        /* Mmap is used only on non-compressed, non-linker created
12789
     sections whose rawsize == size.  */
12790
0
        if (!bed->use_mmap
12791
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12792
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12793
0
      || sec->rawsize != sec->size)
12794
0
#endif
12795
0
    {
12796
0
      if (sec->rawsize > max_contents_size)
12797
0
        max_contents_size = sec->rawsize;
12798
0
      if (sec->size > max_contents_size)
12799
0
        max_contents_size = sec->size;
12800
0
    }
12801
12802
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12803
0
      && (sec->owner->flags & DYNAMIC) == 0)
12804
0
    {
12805
0
      size_t sym_count;
12806
12807
      /* We are interested in just local symbols, not all
12808
         symbols.  */
12809
0
      if (elf_bad_symtab (sec->owner))
12810
0
        sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12811
0
         / bed->s->sizeof_sym);
12812
0
      else
12813
0
        sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12814
12815
0
      if (sym_count > max_sym_count)
12816
0
        max_sym_count = sym_count;
12817
12818
0
      if (sym_count > max_sym_shndx_count
12819
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12820
0
        max_sym_shndx_count = sym_count;
12821
12822
0
      esdi = elf_section_data (sec);
12823
12824
0
      if (esdi->this_hdr.sh_type == SHT_REL
12825
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12826
        /* Some backends use reloc_count in relocation sections
12827
           to count particular types of relocs.  Of course,
12828
           reloc sections themselves can't have relocations.  */
12829
0
        ;
12830
0
      else if (emit_relocs)
12831
0
        {
12832
0
          reloc_count = sec->reloc_count;
12833
0
          if (bed->elf_backend_count_additional_relocs)
12834
0
      {
12835
0
        int c;
12836
0
        c = (*bed->elf_backend_count_additional_relocs) (sec);
12837
0
        additional_reloc_count += c;
12838
0
      }
12839
0
        }
12840
0
      else if (bed->elf_backend_count_relocs)
12841
0
        reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12842
12843
0
      if ((sec->flags & SEC_RELOC) != 0)
12844
0
        {
12845
0
#ifdef USE_MMAP
12846
0
          if (!bed->use_mmap)
12847
0
#endif
12848
0
      {
12849
0
        size_t ext_size = 0;
12850
12851
0
        if (esdi->rel.hdr != NULL)
12852
0
          ext_size = esdi->rel.hdr->sh_size;
12853
0
        if (esdi->rela.hdr != NULL)
12854
0
          ext_size += esdi->rela.hdr->sh_size;
12855
12856
0
        if (ext_size > max_external_reloc_size)
12857
0
          max_external_reloc_size = ext_size;
12858
0
      }
12859
0
          if (sec->reloc_count > max_internal_reloc_count)
12860
0
      max_internal_reloc_count = sec->reloc_count;
12861
0
        }
12862
0
    }
12863
0
      }
12864
12865
0
    if (reloc_count == 0)
12866
0
      continue;
12867
12868
0
    reloc_count += additional_reloc_count;
12869
0
    o->reloc_count += reloc_count;
12870
12871
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12872
0
      {
12873
0
        if (esdi->rel.hdr)
12874
0
    {
12875
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12876
0
      esdo->rel.count += additional_reloc_count;
12877
0
    }
12878
0
        if (esdi->rela.hdr)
12879
0
    {
12880
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12881
0
      esdo->rela.count += additional_reloc_count;
12882
0
    }
12883
0
      }
12884
0
    else
12885
0
      {
12886
0
        if (o->use_rela_p)
12887
0
    esdo->rela.count += reloc_count;
12888
0
        else
12889
0
    esdo->rel.count += reloc_count;
12890
0
      }
12891
0
  }
12892
12893
0
      if (o->reloc_count > 0)
12894
0
  o->flags |= SEC_RELOC;
12895
0
      else
12896
0
  {
12897
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12898
       set it (this is probably a bug) and if it is set
12899
       assign_section_numbers will create a reloc section.  */
12900
0
    o->flags &=~ SEC_RELOC;
12901
0
  }
12902
12903
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12904
   zero.  This is done in elf_fake_sections as well, but forcing
12905
   the VMA to 0 here will ensure that relocs against these
12906
   sections are handled correctly.  */
12907
0
      if ((o->flags & SEC_ALLOC) == 0
12908
0
    && ! o->user_set_vma)
12909
0
  o->vma = 0;
12910
0
    }
12911
12912
0
  if (! bfd_link_relocatable (info) && merged)
12913
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12914
12915
  /* Figure out the file positions for everything but the symbol table
12916
     and the relocs.  We set symcount to force assign_section_numbers
12917
     to create a symbol table.  */
12918
0
  abfd->symcount = info->strip != strip_all || emit_relocs;
12919
0
  BFD_ASSERT (! abfd->output_has_begun);
12920
0
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
12921
0
    goto error_return;
12922
12923
  /* Set sizes, and assign file positions for reloc sections.  */
12924
0
  for (o = abfd->sections; o != NULL; o = o->next)
12925
0
    {
12926
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12927
0
      if ((o->flags & SEC_RELOC) != 0)
12928
0
  {
12929
0
    if (esdo->rel.hdr
12930
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12931
0
      goto error_return;
12932
12933
0
    if (esdo->rela.hdr
12934
0
        && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12935
0
      goto error_return;
12936
0
  }
12937
12938
      /* _bfd_elf_compute_section_file_positions makes temporary use
12939
   of target_index.  Reset it.  */
12940
0
      o->target_index = 0;
12941
12942
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12943
   to count upwards while actually outputting the relocations.  */
12944
0
      esdo->rel.count = 0;
12945
0
      esdo->rela.count = 0;
12946
12947
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12948
0
    && !bfd_section_is_ctf (o))
12949
0
  {
12950
    /* Cache the section contents so that they can be compressed
12951
       later.  Use bfd_malloc since it will be freed by
12952
       bfd_compress_section_contents.  */
12953
0
    unsigned char *contents = esdo->this_hdr.contents;
12954
0
    if (contents != NULL)
12955
0
      abort ();
12956
0
    contents
12957
0
      = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12958
0
    if (contents == NULL)
12959
0
      goto error_return;
12960
0
    esdo->this_hdr.contents = contents;
12961
0
  }
12962
0
    }
12963
12964
  /* We have now assigned file positions for all the sections except .symtab,
12965
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
12966
     the .symtab section at the current file position, and write directly to it.
12967
     We build the .strtab section in memory.  */
12968
0
  abfd->symcount = 0;
12969
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12970
  /* sh_name is set in prep_headers.  */
12971
0
  symtab_hdr->sh_type = SHT_SYMTAB;
12972
  /* sh_flags, sh_addr and sh_size all start off zero.  */
12973
0
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12974
  /* sh_link is set in assign_section_numbers.  */
12975
  /* sh_info is set below.  */
12976
  /* sh_offset is set just below.  */
12977
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12978
12979
0
  if (max_sym_count < 20)
12980
0
    max_sym_count = 20;
12981
0
  htab->strtabsize = max_sym_count;
12982
0
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
12983
0
  htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12984
0
  if (htab->strtab == NULL)
12985
0
    goto error_return;
12986
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
12987
0
  flinfo.symshndxbuf
12988
0
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12989
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
12990
12991
0
  if (info->strip != strip_all || emit_relocs)
12992
0
    {
12993
0
      file_ptr off = elf_next_file_pos (abfd);
12994
12995
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
12996
12997
      /* Note that at this point elf_next_file_pos (abfd) is
12998
   incorrect.  We do not yet know the size of the .symtab section.
12999
   We correct next_file_pos below, after we do know the size.  */
13000
13001
      /* Start writing out the symbol table.  The first symbol is always a
13002
   dummy symbol.  */
13003
0
      elfsym.st_value = 0;
13004
0
      elfsym.st_size = 0;
13005
0
      elfsym.st_info = 0;
13006
0
      elfsym.st_other = 0;
13007
0
      elfsym.st_shndx = SHN_UNDEF;
13008
0
      elfsym.st_target_internal = 0;
13009
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13010
0
             bfd_und_section_ptr, NULL) != 1)
13011
0
  goto error_return;
13012
13013
      /* Output a symbol for each section if asked or they are used for
13014
   relocs.  These symbols usually have no names.  We store the
13015
   index of each one in the index field of the section, so that
13016
   we can find it again when outputting relocs.  */
13017
13018
0
      if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
13019
0
  {
13020
0
    bool name_local_sections
13021
0
      = (bed->elf_backend_name_local_section_symbols
13022
0
         && bed->elf_backend_name_local_section_symbols (abfd));
13023
0
    const char *name = NULL;
13024
13025
0
    elfsym.st_size = 0;
13026
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13027
0
    elfsym.st_other = 0;
13028
0
    elfsym.st_value = 0;
13029
0
    elfsym.st_target_internal = 0;
13030
0
    for (i = 1; i < elf_numsections (abfd); i++)
13031
0
      {
13032
0
        o = bfd_section_from_elf_index (abfd, i);
13033
0
        if (o != NULL)
13034
0
    {
13035
0
      o->target_index = bfd_get_symcount (abfd);
13036
0
      elfsym.st_shndx = i;
13037
0
      if (!bfd_link_relocatable (info))
13038
0
        elfsym.st_value = o->vma;
13039
0
      if (name_local_sections)
13040
0
        name = o->name;
13041
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13042
0
             NULL) != 1)
13043
0
        goto error_return;
13044
0
    }
13045
0
      }
13046
0
  }
13047
0
    }
13048
13049
  /* On some targets like Irix 5 the symbol split between local and global
13050
     ones recorded in the sh_info field needs to be done between section
13051
     and all other symbols.  */
13052
0
  if (bed->elf_backend_elfsym_local_is_section
13053
0
      && bed->elf_backend_elfsym_local_is_section (abfd))
13054
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13055
13056
  /* Allocate some memory to hold information read in from the input
13057
     files.  */
13058
0
  if (max_contents_size != 0)
13059
0
    {
13060
0
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
13061
0
      if (flinfo.contents == NULL)
13062
0
  goto error_return;
13063
0
    }
13064
13065
0
  if (max_external_reloc_size != 0)
13066
0
    {
13067
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13068
0
      if (flinfo.external_relocs == NULL)
13069
0
  goto error_return;
13070
0
    }
13071
13072
0
  if (max_internal_reloc_count != 0)
13073
0
    {
13074
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13075
0
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13076
0
      if (flinfo.internal_relocs == NULL)
13077
0
  goto error_return;
13078
0
    }
13079
13080
0
  if (max_sym_count != 0)
13081
0
    {
13082
0
      amt = max_sym_count * bed->s->sizeof_sym;
13083
0
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13084
0
      if (flinfo.external_syms == NULL)
13085
0
  goto error_return;
13086
13087
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13088
0
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13089
0
      if (flinfo.internal_syms == NULL)
13090
0
  goto error_return;
13091
13092
0
      amt = max_sym_count * sizeof (long);
13093
0
      flinfo.indices = (long int *) bfd_malloc (amt);
13094
0
      if (flinfo.indices == NULL)
13095
0
  goto error_return;
13096
13097
0
      amt = max_sym_count * sizeof (asection *);
13098
0
      flinfo.sections = (asection **) bfd_malloc (amt);
13099
0
      if (flinfo.sections == NULL)
13100
0
  goto error_return;
13101
0
    }
13102
13103
0
  if (max_sym_shndx_count != 0)
13104
0
    {
13105
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13106
0
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13107
0
      if (flinfo.locsym_shndx == NULL)
13108
0
  goto error_return;
13109
0
    }
13110
13111
0
  if (htab->tls_sec)
13112
0
    {
13113
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13114
0
      asection *sec;
13115
13116
0
      for (sec = htab->tls_sec;
13117
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13118
0
     sec = sec->next)
13119
0
  {
13120
0
    bfd_size_type size = sec->size;
13121
0
    unsigned int opb = bfd_octets_per_byte (abfd, sec);
13122
13123
0
    if (size == 0
13124
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13125
0
      {
13126
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13127
13128
0
        if (ord != NULL)
13129
0
    size = ord->offset * opb + ord->size;
13130
0
      }
13131
0
    end = sec->vma + size / opb;
13132
0
  }
13133
0
      base = htab->tls_sec->vma;
13134
      /* Only align end of TLS section if static TLS doesn't have special
13135
   alignment requirements.  */
13136
0
      if (bed->static_tls_alignment == 1)
13137
0
  end = align_power (end, htab->tls_sec->alignment_power);
13138
0
      htab->tls_size = end - base;
13139
0
    }
13140
13141
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13142
0
    return false;
13143
13144
  /* Finish relative relocations here after regular symbol processing
13145
     is finished if DT_RELR is enabled.  */
13146
0
  if (info->enable_dt_relr
13147
0
      && bed->finish_relative_relocs
13148
0
      && !bed->finish_relative_relocs (info))
13149
0
    info->callbacks->fatal
13150
0
      (_("%P: %pB: failed to finish relative relocations\n"), abfd);
13151
13152
  /* Since ELF permits relocations to be against local symbols, we
13153
     must have the local symbols available when we do the relocations.
13154
     Since we would rather only read the local symbols once, and we
13155
     would rather not keep them in memory, we handle all the
13156
     relocations for a single input file at the same time.
13157
13158
     Unfortunately, there is no way to know the total number of local
13159
     symbols until we have seen all of them, and the local symbol
13160
     indices precede the global symbol indices.  This means that when
13161
     we are generating relocatable output, and we see a reloc against
13162
     a global symbol, we can not know the symbol index until we have
13163
     finished examining all the local symbols to see which ones we are
13164
     going to output.  To deal with this, we keep the relocations in
13165
     memory, and don't output them until the end of the link.  This is
13166
     an unfortunate waste of memory, but I don't see a good way around
13167
     it.  Fortunately, it only happens when performing a relocatable
13168
     link, which is not the common case.  FIXME: If keep_memory is set
13169
     we could write the relocs out and then read them again; I don't
13170
     know how bad the memory loss will be.  */
13171
13172
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13173
0
    sub->output_has_begun = false;
13174
0
  for (o = abfd->sections; o != NULL; o = o->next)
13175
0
    {
13176
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13177
0
  {
13178
0
    if (p->type == bfd_indirect_link_order
13179
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13180
0
      == bfd_target_elf_flavour)
13181
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13182
0
      {
13183
0
        if (! sub->output_has_begun)
13184
0
    {
13185
0
      if (! elf_link_input_bfd (&flinfo, sub))
13186
0
        goto error_return;
13187
0
      sub->output_has_begun = true;
13188
0
    }
13189
0
      }
13190
0
    else if (p->type == bfd_section_reloc_link_order
13191
0
       || p->type == bfd_symbol_reloc_link_order)
13192
0
      {
13193
0
        if (! elf_reloc_link_order (abfd, info, o, p))
13194
0
    goto error_return;
13195
0
      }
13196
0
    else
13197
0
      {
13198
0
        if (! _bfd_default_link_order (abfd, info, o, p))
13199
0
    {
13200
0
      if (p->type == bfd_indirect_link_order
13201
0
          && (bfd_get_flavour (sub)
13202
0
        == bfd_target_elf_flavour)
13203
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13204
0
        != bed->s->elfclass))
13205
0
        {
13206
0
          const char *iclass, *oclass;
13207
13208
0
          switch (bed->s->elfclass)
13209
0
      {
13210
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13211
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13212
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13213
0
      default: abort ();
13214
0
      }
13215
13216
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13217
0
      {
13218
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13219
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13220
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13221
0
      default: abort ();
13222
0
      }
13223
13224
0
          bfd_set_error (bfd_error_wrong_format);
13225
0
          _bfd_error_handler
13226
      /* xgettext:c-format */
13227
0
      (_("%pB: file class %s incompatible with %s"),
13228
0
       sub, iclass, oclass);
13229
0
        }
13230
13231
0
      goto error_return;
13232
0
    }
13233
0
      }
13234
0
  }
13235
0
    }
13236
13237
  /* Free symbol buffer if needed.  */
13238
0
  if (!info->reduce_memory_overheads)
13239
0
    {
13240
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13241
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13242
0
    {
13243
0
      free (elf_tdata (sub)->symbuf);
13244
0
      elf_tdata (sub)->symbuf = NULL;
13245
0
    }
13246
0
    }
13247
13248
  /* Output any global symbols that got converted to local in a
13249
     version script or due to symbol visibility.  We do this in a
13250
     separate step since ELF requires all local symbols to appear
13251
     prior to any global symbols.  FIXME: We should only do this if
13252
     some global symbols were, in fact, converted to become local.
13253
     FIXME: Will this work correctly with the Irix 5 linker?  */
13254
0
  eoinfo.failed = false;
13255
0
  eoinfo.flinfo = &flinfo;
13256
0
  eoinfo.localsyms = true;
13257
0
  eoinfo.file_sym_done = false;
13258
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13259
0
  if (eoinfo.failed)
13260
0
    goto error_return;
13261
13262
  /* If backend needs to output some local symbols not present in the hash
13263
     table, do it now.  */
13264
0
  if (bed->elf_backend_output_arch_local_syms)
13265
0
    {
13266
0
      if (! ((*bed->elf_backend_output_arch_local_syms)
13267
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13268
0
  goto error_return;
13269
0
    }
13270
13271
  /* That wrote out all the local symbols.  Finish up the symbol table
13272
     with the global symbols. Even if we want to strip everything we
13273
     can, we still need to deal with those global symbols that got
13274
     converted to local in a version script.  */
13275
13276
  /* The sh_info field records the index of the first non local symbol.  */
13277
0
  if (!symtab_hdr->sh_info)
13278
0
    symtab_hdr->sh_info = bfd_get_symcount (abfd);
13279
13280
0
  if (dynamic
13281
0
      && htab->dynsym != NULL
13282
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13283
0
    {
13284
0
      Elf_Internal_Sym sym;
13285
0
      bfd_byte *dynsym = htab->dynsym->contents;
13286
13287
0
      o = htab->dynsym->output_section;
13288
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13289
13290
      /* Write out the section symbols for the output sections.  */
13291
0
      if (bfd_link_pic (info)
13292
0
    || htab->is_relocatable_executable)
13293
0
  {
13294
0
    asection *s;
13295
13296
0
    sym.st_size = 0;
13297
0
    sym.st_name = 0;
13298
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13299
0
    sym.st_other = 0;
13300
0
    sym.st_target_internal = 0;
13301
13302
0
    for (s = abfd->sections; s != NULL; s = s->next)
13303
0
      {
13304
0
        int indx;
13305
0
        bfd_byte *dest;
13306
0
        long dynindx;
13307
13308
0
        dynindx = elf_section_data (s)->dynindx;
13309
0
        if (dynindx <= 0)
13310
0
    continue;
13311
0
        indx = elf_section_data (s)->this_idx;
13312
0
        BFD_ASSERT (indx > 0);
13313
0
        sym.st_shndx = indx;
13314
0
        if (! check_dynsym (abfd, &sym))
13315
0
    goto error_return;
13316
0
        sym.st_value = s->vma;
13317
0
        dest = dynsym + dynindx * bed->s->sizeof_sym;
13318
13319
        /* Inform the linker of the addition of this symbol.  */
13320
13321
0
        if (info->callbacks->ctf_new_dynsym)
13322
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13323
13324
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13325
0
      }
13326
0
  }
13327
13328
      /* Write out the local dynsyms.  */
13329
0
      if (htab->dynlocal)
13330
0
  {
13331
0
    struct elf_link_local_dynamic_entry *e;
13332
0
    for (e = htab->dynlocal; e ; e = e->next)
13333
0
      {
13334
0
        asection *s;
13335
0
        bfd_byte *dest;
13336
13337
        /* Copy the internal symbol and turn off visibility.
13338
     Note that we saved a word of storage and overwrote
13339
     the original st_name with the dynstr_index.  */
13340
0
        sym = e->isym;
13341
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13342
0
        sym.st_shndx = SHN_UNDEF;
13343
13344
0
        s = bfd_section_from_elf_index (e->input_bfd,
13345
0
                e->isym.st_shndx);
13346
0
        if (s != NULL
13347
0
      && s->output_section != NULL
13348
0
      && elf_section_data (s->output_section) != NULL)
13349
0
    {
13350
0
      sym.st_shndx =
13351
0
        elf_section_data (s->output_section)->this_idx;
13352
0
      if (! check_dynsym (abfd, &sym))
13353
0
        goto error_return;
13354
0
      sym.st_value = (s->output_section->vma
13355
0
          + s->output_offset
13356
0
          + e->isym.st_value);
13357
0
    }
13358
13359
        /* Inform the linker of the addition of this symbol.  */
13360
13361
0
        if (info->callbacks->ctf_new_dynsym)
13362
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13363
13364
0
        dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13365
0
        bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13366
0
      }
13367
0
  }
13368
0
    }
13369
13370
  /* We get the global symbols from the hash table.  */
13371
0
  eoinfo.failed = false;
13372
0
  eoinfo.localsyms = false;
13373
0
  eoinfo.flinfo = &flinfo;
13374
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13375
0
  if (eoinfo.failed)
13376
0
    goto error_return;
13377
13378
  /* If backend needs to output some symbols not present in the hash
13379
     table, do it now.  */
13380
0
  if (bed->elf_backend_output_arch_syms
13381
0
      && (info->strip != strip_all || emit_relocs))
13382
0
    {
13383
0
      if (! ((*bed->elf_backend_output_arch_syms)
13384
0
       (abfd, info, &flinfo, elf_link_output_symstrtab)))
13385
0
  goto error_return;
13386
0
    }
13387
13388
  /* Finalize the .strtab section.  */
13389
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13390
13391
  /* Swap out the .strtab section. */
13392
0
  if (!elf_link_swap_symbols_out (&flinfo))
13393
0
    goto error_return;
13394
0
  free (htab->strtab);
13395
0
  htab->strtab = NULL;
13396
13397
  /* Now we know the size of the symtab section.  */
13398
0
  if (bfd_get_symcount (abfd) > 0)
13399
0
    {
13400
      /* Finish up and write out the symbol string table (.strtab)
13401
   section.  */
13402
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13403
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13404
13405
0
      if (elf_symtab_shndx_list (abfd))
13406
0
  {
13407
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13408
13409
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13410
0
      {
13411
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13412
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13413
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13414
0
        amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13415
0
        symtab_shndx_hdr->sh_size = amt;
13416
13417
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13418
0
                     off, true, 0);
13419
13420
0
        if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13421
0
      || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13422
0
    goto error_return;
13423
0
      }
13424
0
  }
13425
13426
0
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13427
      /* sh_name was set in prep_headers.  */
13428
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13429
0
      symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13430
0
      symstrtab_hdr->sh_addr = 0;
13431
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13432
0
      symstrtab_hdr->sh_entsize = 0;
13433
0
      symstrtab_hdr->sh_link = 0;
13434
0
      symstrtab_hdr->sh_info = 0;
13435
      /* sh_offset is set just below.  */
13436
0
      symstrtab_hdr->sh_addralign = 1;
13437
13438
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13439
0
                   off, true, 0);
13440
0
      elf_next_file_pos (abfd) = off;
13441
13442
0
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13443
0
    || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13444
0
  goto error_return;
13445
0
    }
13446
13447
0
  if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13448
0
    {
13449
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13450
0
        info->out_implib_bfd);
13451
0
      goto error_return;
13452
0
    }
13453
13454
  /* Adjust the relocs to have the correct symbol indices.  */
13455
0
  for (o = abfd->sections; o != NULL; o = o->next)
13456
0
    {
13457
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13458
0
      bool sort;
13459
13460
0
      if ((o->flags & SEC_RELOC) == 0)
13461
0
  continue;
13462
13463
0
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13464
0
      if (esdo->rel.hdr != NULL
13465
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13466
0
  goto error_return;
13467
0
      if (esdo->rela.hdr != NULL
13468
0
    && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13469
0
  goto error_return;
13470
13471
      /* Set the reloc_count field to 0 to prevent write_relocs from
13472
   trying to swap the relocs out itself.  */
13473
0
      o->reloc_count = 0;
13474
0
    }
13475
13476
0
  relativecount = 0;
13477
0
  if (dynamic && info->combreloc && dynobj != NULL)
13478
0
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13479
13480
0
  relr_entsize = 0;
13481
0
  if (htab->srelrdyn != NULL
13482
0
      && htab->srelrdyn->output_section != NULL
13483
0
      && htab->srelrdyn->size != 0)
13484
0
    {
13485
0
      asection *s = htab->srelrdyn->output_section;
13486
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13487
0
      if (relr_entsize == 0)
13488
0
  {
13489
0
    relr_entsize = bed->s->arch_size / 8;
13490
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13491
0
  }
13492
0
    }
13493
13494
  /* If we are linking against a dynamic object, or generating a
13495
     shared library, finish up the dynamic linking information.  */
13496
0
  if (dynamic)
13497
0
    {
13498
0
      bfd_byte *dyncon, *dynconend;
13499
13500
      /* Fix up .dynamic entries.  */
13501
0
      o = htab->dynamic;
13502
0
      BFD_ASSERT (o != NULL);
13503
13504
0
      dyncon = o->contents;
13505
0
      dynconend = PTR_ADD (o->contents, o->size);
13506
0
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13507
0
  {
13508
0
    Elf_Internal_Dyn dyn;
13509
0
    const char *name;
13510
0
    unsigned int type;
13511
0
    bfd_size_type sh_size;
13512
0
    bfd_vma sh_addr;
13513
13514
0
    bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13515
13516
0
    switch (dyn.d_tag)
13517
0
      {
13518
0
      default:
13519
0
        continue;
13520
0
      case DT_NULL:
13521
0
        if (relativecount != 0)
13522
0
    {
13523
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13524
0
        {
13525
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13526
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13527
0
        }
13528
0
      if (dyn.d_tag != DT_NULL
13529
0
          && dynconend - dyncon >= bed->s->sizeof_dyn)
13530
0
        {
13531
0
          dyn.d_un.d_val = relativecount;
13532
0
          relativecount = 0;
13533
0
          break;
13534
0
        }
13535
0
      relativecount = 0;
13536
0
    }
13537
0
        if (relr_entsize != 0)
13538
0
    {
13539
0
      if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13540
0
        {
13541
0
          asection *s = htab->srelrdyn;
13542
0
          dyn.d_tag = DT_RELR;
13543
0
          dyn.d_un.d_ptr
13544
0
      = s->output_section->vma + s->output_offset;
13545
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13546
0
          dyncon += bed->s->sizeof_dyn;
13547
13548
0
          dyn.d_tag = DT_RELRSZ;
13549
0
          dyn.d_un.d_val = s->size;
13550
0
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13551
0
          dyncon += bed->s->sizeof_dyn;
13552
13553
0
          dyn.d_tag = DT_RELRENT;
13554
0
          dyn.d_un.d_val = relr_entsize;
13555
0
          relr_entsize = 0;
13556
0
          break;
13557
0
        }
13558
0
      relr_entsize = 0;
13559
0
    }
13560
0
        continue;
13561
13562
0
      case DT_INIT:
13563
0
        name = info->init_function;
13564
0
        goto get_sym;
13565
0
      case DT_FINI:
13566
0
        name = info->fini_function;
13567
0
      get_sym:
13568
0
        {
13569
0
    struct elf_link_hash_entry *h;
13570
13571
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13572
0
    if (h != NULL
13573
0
        && (h->root.type == bfd_link_hash_defined
13574
0
      || h->root.type == bfd_link_hash_defweak))
13575
0
      {
13576
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13577
0
        o = h->root.u.def.section;
13578
0
        if (o->output_section != NULL)
13579
0
          dyn.d_un.d_ptr += (o->output_section->vma
13580
0
           + o->output_offset);
13581
0
        else
13582
0
          {
13583
      /* The symbol is imported from another shared
13584
         library and does not apply to this one.  */
13585
0
      dyn.d_un.d_ptr = 0;
13586
0
          }
13587
0
        break;
13588
0
      }
13589
0
        }
13590
0
        continue;
13591
13592
0
      case DT_PREINIT_ARRAYSZ:
13593
0
        name = ".preinit_array";
13594
0
        goto get_out_size;
13595
0
      case DT_INIT_ARRAYSZ:
13596
0
        name = ".init_array";
13597
0
        goto get_out_size;
13598
0
      case DT_FINI_ARRAYSZ:
13599
0
        name = ".fini_array";
13600
0
      get_out_size:
13601
0
        o = bfd_get_section_by_name (abfd, name);
13602
0
        if (o == NULL)
13603
0
    {
13604
0
      _bfd_error_handler
13605
0
        (_("could not find section %s"), name);
13606
0
      goto error_return;
13607
0
    }
13608
0
        if (o->size == 0)
13609
0
    _bfd_error_handler
13610
0
      (_("warning: %s section has zero size"), name);
13611
0
        dyn.d_un.d_val = o->size;
13612
0
        break;
13613
13614
0
      case DT_PREINIT_ARRAY:
13615
0
        name = ".preinit_array";
13616
0
        goto get_out_vma;
13617
0
      case DT_INIT_ARRAY:
13618
0
        name = ".init_array";
13619
0
        goto get_out_vma;
13620
0
      case DT_FINI_ARRAY:
13621
0
        name = ".fini_array";
13622
0
      get_out_vma:
13623
0
        o = bfd_get_section_by_name (abfd, name);
13624
0
        goto do_vma;
13625
13626
0
      case DT_HASH:
13627
0
        name = ".hash";
13628
0
        goto get_vma;
13629
0
      case DT_GNU_HASH:
13630
0
        name = ".gnu.hash";
13631
0
        goto get_vma;
13632
0
      case DT_STRTAB:
13633
0
        name = ".dynstr";
13634
0
        goto get_vma;
13635
0
      case DT_SYMTAB:
13636
0
        name = ".dynsym";
13637
0
        goto get_vma;
13638
0
      case DT_VERDEF:
13639
0
        name = ".gnu.version_d";
13640
0
        goto get_vma;
13641
0
      case DT_VERNEED:
13642
0
        name = ".gnu.version_r";
13643
0
        goto get_vma;
13644
0
      case DT_VERSYM:
13645
0
        name = ".gnu.version";
13646
0
      get_vma:
13647
0
        o = bfd_get_linker_section (dynobj, name);
13648
0
      do_vma:
13649
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13650
0
    {
13651
0
      _bfd_error_handler
13652
0
        (_("could not find section %s"), name);
13653
0
      goto error_return;
13654
0
    }
13655
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13656
0
    {
13657
0
      _bfd_error_handler
13658
0
        (_("warning: section '%s' is being made into a note"), name);
13659
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13660
0
      goto error_return;
13661
0
    }
13662
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13663
0
        break;
13664
13665
0
      case DT_REL:
13666
0
      case DT_RELA:
13667
0
      case DT_RELSZ:
13668
0
      case DT_RELASZ:
13669
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13670
0
    type = SHT_REL;
13671
0
        else
13672
0
    type = SHT_RELA;
13673
0
        sh_size = 0;
13674
0
        sh_addr = 0;
13675
0
        for (i = 1; i < elf_numsections (abfd); i++)
13676
0
    {
13677
0
      Elf_Internal_Shdr *hdr;
13678
13679
0
      hdr = elf_elfsections (abfd)[i];
13680
0
      if (hdr->sh_type == type
13681
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13682
0
        {
13683
0
          sh_size += hdr->sh_size;
13684
0
          if (sh_addr == 0
13685
0
        || sh_addr > hdr->sh_addr)
13686
0
      sh_addr = hdr->sh_addr;
13687
0
        }
13688
0
    }
13689
13690
0
        if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13691
0
    {
13692
0
      unsigned int opb = bfd_octets_per_byte (abfd, o);
13693
13694
      /* Don't count procedure linkage table relocs in the
13695
         overall reloc count.  */
13696
0
      sh_size -= htab->srelplt->size;
13697
0
      if (sh_size == 0)
13698
        /* If the size is zero, make the address zero too.
13699
           This is to avoid a glibc bug.  If the backend
13700
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13701
           zero, then we'll put DT_RELA at the end of
13702
           DT_JMPREL.  glibc will interpret the end of
13703
           DT_RELA matching the end of DT_JMPREL as the
13704
           case where DT_RELA includes DT_JMPREL, and for
13705
           LD_BIND_NOW will decide that processing DT_RELA
13706
           will process the PLT relocs too.  Net result:
13707
           No PLT relocs applied.  */
13708
0
        sh_addr = 0;
13709
13710
      /* If .rela.plt is the first .rela section, exclude
13711
         it from DT_RELA.  */
13712
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13713
0
               + htab->srelplt->output_offset) * opb)
13714
0
        sh_addr += htab->srelplt->size;
13715
0
    }
13716
13717
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13718
0
    dyn.d_un.d_val = sh_size;
13719
0
        else
13720
0
    dyn.d_un.d_ptr = sh_addr;
13721
0
        break;
13722
0
      }
13723
0
    bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13724
0
  }
13725
0
    }
13726
13727
  /* If we have created any dynamic sections, then output them.  */
13728
0
  if (dynobj != NULL)
13729
0
    {
13730
0
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13731
0
  goto error_return;
13732
13733
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13734
0
      if (bfd_link_textrel_check (info)
13735
0
    && (o = htab->dynamic) != NULL
13736
0
    && o->size != 0)
13737
0
  {
13738
0
    bfd_byte *dyncon, *dynconend;
13739
13740
0
    dyncon = o->contents;
13741
0
    dynconend = o->contents + o->size;
13742
0
    for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13743
0
      {
13744
0
        Elf_Internal_Dyn dyn;
13745
13746
0
        bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13747
13748
0
        if (dyn.d_tag == DT_TEXTREL)
13749
0
    {
13750
0
      if (info->textrel_check == textrel_check_error)
13751
0
        info->callbacks->einfo
13752
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13753
0
      else if (bfd_link_dll (info))
13754
0
        info->callbacks->einfo
13755
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13756
0
      else if (bfd_link_pde (info))
13757
0
        info->callbacks->einfo
13758
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13759
0
      else
13760
0
        info->callbacks->einfo
13761
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13762
0
      break;
13763
0
    }
13764
0
      }
13765
0
  }
13766
13767
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13768
0
  {
13769
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13770
0
        || o->size == 0
13771
0
        || o->output_section == bfd_abs_section_ptr)
13772
0
      continue;
13773
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13774
0
      {
13775
        /* At this point, we are only interested in sections
13776
     created by _bfd_elf_link_create_dynamic_sections.  */
13777
0
        continue;
13778
0
      }
13779
0
    if (htab->stab_info.stabstr == o)
13780
0
      continue;
13781
0
    if (htab->eh_info.hdr_sec == o)
13782
0
      continue;
13783
0
    if (strcmp (o->name, ".dynstr") != 0)
13784
0
      {
13785
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13786
0
              * bfd_octets_per_byte (abfd, o));
13787
0
        if (!bfd_set_section_contents (abfd, o->output_section,
13788
0
               o->contents, octets, o->size))
13789
0
    goto error_return;
13790
0
      }
13791
0
    else
13792
0
      {
13793
        /* The contents of the .dynstr section are actually in a
13794
     stringtab.  */
13795
0
        file_ptr off;
13796
13797
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13798
0
        if (bfd_seek (abfd, off, SEEK_SET) != 0
13799
0
      || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13800
0
    goto error_return;
13801
0
      }
13802
0
  }
13803
0
    }
13804
13805
0
  if (!info->resolve_section_groups)
13806
0
    {
13807
0
      bool failed = false;
13808
13809
0
      BFD_ASSERT (bfd_link_relocatable (info));
13810
0
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13811
0
      if (failed)
13812
0
  goto error_return;
13813
0
    }
13814
13815
  /* If we have optimized stabs strings, output them.  */
13816
0
  if (htab->stab_info.stabstr != NULL)
13817
0
    {
13818
0
      if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13819
0
  goto error_return;
13820
0
    }
13821
13822
0
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13823
0
    goto error_return;
13824
13825
0
  if (! _bfd_elf_write_section_sframe (abfd, info))
13826
0
    goto error_return;
13827
13828
0
  if (! _bfd_elf_write_section_build_attributes (abfd, info))
13829
0
    goto error_ret2;
13830
13831
0
  if (info->callbacks->emit_ctf)
13832
0
      info->callbacks->emit_ctf ();
13833
13834
0
  elf_final_link_free (abfd, &flinfo);
13835
13836
0
  if (info->unique_symbol)
13837
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13838
0
  return true;
13839
13840
0
 error_return:
13841
0
  free (htab->strtab);
13842
0
  htab->strtab = NULL;
13843
0
  elf_final_link_free (abfd, &flinfo);
13844
0
 error_ret2:
13845
0
  if (info->unique_symbol)
13846
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13847
0
  return false;
13848
0
}
13849

13850
/* Initialize COOKIE for input bfd ABFD.  */
13851
13852
static bool
13853
init_reloc_cookie (struct elf_reloc_cookie *cookie,
13854
       struct bfd_link_info *info, bfd *abfd,
13855
       bool keep_memory)
13856
0
{
13857
0
  Elf_Internal_Shdr *symtab_hdr;
13858
0
  const struct elf_backend_data *bed;
13859
13860
0
  bed = get_elf_backend_data (abfd);
13861
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13862
13863
0
  cookie->abfd = abfd;
13864
0
  cookie->sym_hashes = elf_sym_hashes (abfd);
13865
0
  cookie->bad_symtab = elf_bad_symtab (abfd);
13866
0
  if (cookie->bad_symtab)
13867
0
    {
13868
0
      cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13869
0
      cookie->extsymoff = 0;
13870
0
    }
13871
0
  else
13872
0
    {
13873
0
      cookie->locsymcount = symtab_hdr->sh_info;
13874
0
      cookie->extsymoff = symtab_hdr->sh_info;
13875
0
    }
13876
13877
0
  if (bed->s->arch_size == 32)
13878
0
    cookie->r_sym_shift = 8;
13879
0
  else
13880
0
    cookie->r_sym_shift = 32;
13881
13882
0
  cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13883
0
  if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13884
0
    {
13885
0
      cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13886
0
                cookie->locsymcount, 0,
13887
0
                NULL, NULL, NULL);
13888
0
      if (cookie->locsyms == NULL)
13889
0
  {
13890
0
    info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13891
0
    return false;
13892
0
  }
13893
0
      if (keep_memory || _bfd_elf_link_keep_memory (info))
13894
0
  {
13895
0
    symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13896
0
    info->cache_size += (cookie->locsymcount
13897
0
             * sizeof (Elf_Internal_Sym));
13898
0
  }
13899
0
    }
13900
0
  return true;
13901
0
}
13902
13903
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13904
13905
static void
13906
fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13907
0
{
13908
0
  Elf_Internal_Shdr *symtab_hdr;
13909
13910
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13911
0
  if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13912
0
    free (cookie->locsyms);
13913
0
}
13914
13915
/* Initialize the relocation information in COOKIE for input section SEC
13916
   of input bfd ABFD.  */
13917
13918
static bool
13919
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13920
      struct bfd_link_info *info, bfd *abfd,
13921
      asection *sec, bool keep_memory)
13922
0
{
13923
0
  if (sec->reloc_count == 0)
13924
0
    {
13925
0
      cookie->rels = NULL;
13926
0
      cookie->relend = NULL;
13927
0
    }
13928
0
  else
13929
0
    {
13930
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13931
0
  (abfd, info, sec, NULL, NULL,
13932
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13933
0
      if (cookie->rels == NULL)
13934
0
  return false;
13935
0
      cookie->rel = cookie->rels;
13936
0
      cookie->relend = cookie->rels + sec->reloc_count;
13937
0
    }
13938
0
  cookie->rel = cookie->rels;
13939
0
  return true;
13940
0
}
13941
13942
/* Free the memory allocated by init_reloc_cookie_rels,
13943
   if appropriate.  */
13944
13945
static void
13946
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13947
      asection *sec)
13948
0
{
13949
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13950
0
    free (cookie->rels);
13951
0
}
13952
13953
/* Initialize the whole of COOKIE for input section SEC.  */
13954
13955
static bool
13956
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13957
             struct bfd_link_info *info,
13958
             asection *sec, bool keep_memory)
13959
0
{
13960
0
  if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13961
0
    goto error1;
13962
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13963
0
             keep_memory))
13964
0
    goto error2;
13965
0
  return true;
13966
13967
0
 error2:
13968
0
  fini_reloc_cookie (cookie, sec->owner);
13969
0
 error1:
13970
0
  return false;
13971
0
}
13972
13973
/* Free the memory allocated by init_reloc_cookie_for_section,
13974
   if appropriate.  */
13975
13976
static void
13977
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13978
             asection *sec)
13979
0
{
13980
0
  fini_reloc_cookie_rels (cookie, sec);
13981
0
  fini_reloc_cookie (cookie, sec->owner);
13982
0
}
13983

13984
/* Garbage collect unused sections.  */
13985
13986
/* Default gc_mark_hook.  */
13987
13988
asection *
13989
_bfd_elf_gc_mark_hook (asection *sec,
13990
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
13991
           Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13992
           struct elf_link_hash_entry *h,
13993
           Elf_Internal_Sym *sym)
13994
0
{
13995
0
  if (h == NULL)
13996
0
    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13997
13998
0
  switch (h->root.type)
13999
0
    {
14000
0
    case bfd_link_hash_defined:
14001
0
    case bfd_link_hash_defweak:
14002
0
      return h->root.u.def.section;
14003
14004
0
    case bfd_link_hash_common:
14005
0
      return h->root.u.c.p->section;
14006
14007
0
    default:
14008
0
      return NULL;
14009
0
    }
14010
0
}
14011
14012
/* Return the debug definition section.  */
14013
14014
static asection *
14015
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14016
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14017
         Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
14018
         struct elf_link_hash_entry *h,
14019
         Elf_Internal_Sym *sym)
14020
0
{
14021
0
  if (h != NULL)
14022
0
    {
14023
      /* Return the global debug definition section.  */
14024
0
      if ((h->root.type == bfd_link_hash_defined
14025
0
     || h->root.type == bfd_link_hash_defweak)
14026
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14027
0
  return h->root.u.def.section;
14028
0
    }
14029
0
  else
14030
0
    {
14031
      /* Return the local debug definition section.  */
14032
0
      asection *isec = bfd_section_from_elf_index (sec->owner,
14033
0
               sym->st_shndx);
14034
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14035
0
  return isec;
14036
0
    }
14037
14038
0
  return NULL;
14039
0
}
14040
14041
/* COOKIE->rel describes a relocation against section SEC, which is
14042
   a section we've decided to keep.  Return the section that contains
14043
   the relocation symbol, or NULL if no section contains it.  */
14044
14045
asection *
14046
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14047
           elf_gc_mark_hook_fn gc_mark_hook,
14048
           struct elf_reloc_cookie *cookie,
14049
           bool *start_stop)
14050
0
{
14051
0
  unsigned long r_symndx;
14052
0
  struct elf_link_hash_entry *h, *hw;
14053
14054
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14055
0
  if (r_symndx == STN_UNDEF)
14056
0
    return NULL;
14057
14058
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14059
0
  if (h == NULL)
14060
0
    {
14061
      /* A corrupt input file can lead to a situation where the index
14062
   does not reference either a local or an external symbol.  */
14063
0
      if (r_symndx >= cookie->locsymcount)
14064
0
  return NULL;
14065
14066
0
      return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14067
0
            &cookie->locsyms[r_symndx]);
14068
0
    }
14069
14070
0
  bool was_marked = h->mark;
14071
14072
0
  h->mark = 1;
14073
  /* Keep all aliases of the symbol too.  If an object symbol
14074
     needs to be copied into .dynbss then all of its aliases
14075
     should be present as dynamic symbols, not just the one used
14076
     on the copy relocation.  */
14077
0
  hw = h;
14078
0
  while (hw->is_weakalias)
14079
0
    {
14080
0
      hw = hw->u.alias;
14081
0
      hw->mark = 1;
14082
0
    }
14083
14084
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14085
0
    {
14086
0
      if (info->start_stop_gc)
14087
0
  return NULL;
14088
14089
      /* To work around a glibc bug, mark XXX input sections
14090
   when there is a reference to __start_XXX or __stop_XXX
14091
   symbols.  */
14092
0
      else if (start_stop != NULL)
14093
0
  {
14094
0
    asection *s = h->u2.start_stop_section;
14095
0
    *start_stop = true;
14096
0
    return s;
14097
0
  }
14098
0
    }
14099
14100
0
  return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14101
0
}
14102
14103
/* COOKIE->rel describes a relocation against section SEC, which is
14104
   a section we've decided to keep.  Mark the section that contains
14105
   the relocation symbol.  */
14106
14107
bool
14108
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14109
      asection *sec,
14110
      elf_gc_mark_hook_fn gc_mark_hook,
14111
      struct elf_reloc_cookie *cookie)
14112
0
{
14113
0
  asection *rsec;
14114
0
  bool start_stop = false;
14115
14116
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14117
0
  while (rsec != NULL)
14118
0
    {
14119
0
      if (!rsec->gc_mark)
14120
0
  {
14121
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14122
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14123
0
      rsec->gc_mark = 1;
14124
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14125
0
      return false;
14126
0
  }
14127
0
      if (!start_stop)
14128
0
  break;
14129
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14130
0
    }
14131
0
  return true;
14132
0
}
14133
14134
/* The mark phase of garbage collection.  For a given section, mark
14135
   it and any sections in this section's group, and all the sections
14136
   which define symbols to which it refers.  */
14137
14138
bool
14139
_bfd_elf_gc_mark (struct bfd_link_info *info,
14140
      asection *sec,
14141
      elf_gc_mark_hook_fn gc_mark_hook)
14142
0
{
14143
0
  bool ret;
14144
0
  asection *group_sec, *eh_frame;
14145
14146
0
  sec->gc_mark = 1;
14147
14148
  /* Mark all the sections in the group.  */
14149
0
  group_sec = elf_section_data (sec)->next_in_group;
14150
0
  if (group_sec && !group_sec->gc_mark)
14151
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14152
0
      return false;
14153
14154
  /* Look through the section relocs.  */
14155
0
  ret = true;
14156
0
  eh_frame = elf_eh_frame_section (sec->owner);
14157
0
  if ((sec->flags & SEC_RELOC) != 0
14158
0
      && sec->reloc_count > 0
14159
0
      && sec != eh_frame)
14160
0
    {
14161
0
      struct elf_reloc_cookie cookie;
14162
14163
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14164
0
  ret = false;
14165
0
      else
14166
0
  {
14167
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14168
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14169
0
        {
14170
0
    ret = false;
14171
0
    break;
14172
0
        }
14173
0
    fini_reloc_cookie_for_section (&cookie, sec);
14174
0
  }
14175
0
    }
14176
14177
0
  if (ret && eh_frame && elf_fde_list (sec))
14178
0
    {
14179
0
      struct elf_reloc_cookie cookie;
14180
14181
      /* NB: When --no-keep-memory is used, the symbol table and
14182
   relocation info for eh_frame are freed after they are retrieved
14183
   for each text section in the input object.  If an input object
14184
   has many text sections, the same data is retrieved and freed
14185
   many times which can take a very long time.  Always keep the
14186
   symbol table and relocation info for eh_frame to avoid it.  */
14187
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14188
0
            true))
14189
0
  ret = false;
14190
0
      else
14191
0
  {
14192
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14193
0
              gc_mark_hook, &cookie))
14194
0
      ret = false;
14195
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14196
0
  }
14197
0
    }
14198
14199
0
  eh_frame = elf_section_eh_frame_entry (sec);
14200
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14201
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14202
0
      ret = false;
14203
14204
0
  return ret;
14205
0
}
14206
14207
/* Scan and mark sections in a special or debug section group.  */
14208
14209
static void
14210
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14211
0
{
14212
  /* Point to first section of section group.  */
14213
0
  asection *ssec;
14214
  /* Used to iterate the section group.  */
14215
0
  asection *msec;
14216
14217
0
  bool is_special_grp = true;
14218
0
  bool is_debug_grp = true;
14219
14220
  /* First scan to see if group contains any section other than debug
14221
     and special section.  */
14222
0
  ssec = msec = elf_next_in_group (grp);
14223
0
  do
14224
0
    {
14225
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14226
0
  is_debug_grp = false;
14227
14228
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14229
0
  is_special_grp = false;
14230
14231
0
      msec = elf_next_in_group (msec);
14232
0
    }
14233
0
  while (msec != ssec);
14234
14235
  /* If this is a pure debug section group or pure special section group,
14236
     keep all sections in this group.  */
14237
0
  if (is_debug_grp || is_special_grp)
14238
0
    {
14239
0
      do
14240
0
  {
14241
0
    msec->gc_mark = 1;
14242
0
    msec = elf_next_in_group (msec);
14243
0
  }
14244
0
      while (msec != ssec);
14245
0
    }
14246
0
}
14247
14248
/* Keep debug and special sections.  */
14249
14250
bool
14251
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14252
         elf_gc_mark_hook_fn mark_hook)
14253
0
{
14254
0
  bfd *ibfd;
14255
14256
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14257
0
    {
14258
0
      asection *isec;
14259
0
      bool some_kept;
14260
0
      bool debug_frag_seen;
14261
0
      bool has_kept_debug_info;
14262
14263
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14264
0
  continue;
14265
0
      isec = ibfd->sections;
14266
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14267
0
  continue;
14268
14269
      /* Ensure all linker created sections are kept,
14270
   see if any other section is already marked,
14271
   and note if we have any fragmented debug sections.  */
14272
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14273
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14274
0
  {
14275
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14276
0
      isec->gc_mark = 1;
14277
0
    else if (isec->gc_mark
14278
0
       && (isec->flags & SEC_ALLOC) != 0
14279
0
       && elf_section_type (isec) != SHT_NOTE)
14280
0
      some_kept = true;
14281
0
    else
14282
0
      {
14283
        /* Since all sections, except for backend specific ones,
14284
     have been garbage collected, call mark_hook on this
14285
     section if any of its linked-to sections is marked.  */
14286
0
        asection *linked_to_sec;
14287
0
        for (linked_to_sec = elf_linked_to_section (isec);
14288
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14289
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14290
0
    {
14291
0
      if (linked_to_sec->gc_mark)
14292
0
        {
14293
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14294
0
      return false;
14295
0
          break;
14296
0
        }
14297
0
      linked_to_sec->linker_mark = 1;
14298
0
    }
14299
0
        for (linked_to_sec = elf_linked_to_section (isec);
14300
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14301
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14302
0
    linked_to_sec->linker_mark = 0;
14303
0
      }
14304
14305
0
    if (!debug_frag_seen
14306
0
        && (isec->flags & SEC_DEBUGGING)
14307
0
        && startswith (isec->name, ".debug_line."))
14308
0
      debug_frag_seen = true;
14309
0
    else if (strcmp (bfd_section_name (isec),
14310
0
         "__patchable_function_entries") == 0
14311
0
       && elf_linked_to_section (isec) == NULL)
14312
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14313
0
          "need linked-to section "
14314
0
          "for --gc-sections\n"),
14315
0
              isec->owner, isec);
14316
0
  }
14317
14318
      /* If no non-note alloc section in this file will be kept, then
14319
   we can toss out the debug and special sections.  */
14320
0
      if (!some_kept)
14321
0
  continue;
14322
14323
      /* Keep debug and special sections like .comment when they are
14324
   not part of a group.  Also keep section groups that contain
14325
   just debug sections or special sections.  NB: Sections with
14326
   linked-to section has been handled above.  */
14327
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14328
0
  {
14329
0
    if ((isec->flags & SEC_GROUP) != 0)
14330
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14331
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14332
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14333
0
       && elf_next_in_group (isec) == NULL
14334
0
       && elf_linked_to_section (isec) == NULL)
14335
0
      isec->gc_mark = 1;
14336
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14337
0
      has_kept_debug_info = true;
14338
0
  }
14339
14340
      /* Look for CODE sections which are going to be discarded,
14341
   and find and discard any fragmented debug sections which
14342
   are associated with that code section.  */
14343
0
      if (debug_frag_seen)
14344
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14345
0
    if ((isec->flags & SEC_CODE) != 0
14346
0
        && isec->gc_mark == 0)
14347
0
      {
14348
0
        unsigned int ilen;
14349
0
        asection *dsec;
14350
14351
0
        ilen = strlen (isec->name);
14352
14353
        /* Association is determined by the name of the debug
14354
     section containing the name of the code section as
14355
     a suffix.  For example .debug_line.text.foo is a
14356
     debug section associated with .text.foo.  */
14357
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14358
0
    {
14359
0
      unsigned int dlen;
14360
14361
0
      if (dsec->gc_mark == 0
14362
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14363
0
        continue;
14364
14365
0
      dlen = strlen (dsec->name);
14366
14367
0
      if (dlen > ilen
14368
0
          && strncmp (dsec->name + (dlen - ilen),
14369
0
          isec->name, ilen) == 0)
14370
0
        dsec->gc_mark = 0;
14371
0
    }
14372
0
    }
14373
14374
      /* Mark debug sections referenced by kept debug sections.  */
14375
0
      if (has_kept_debug_info)
14376
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14377
0
    if (isec->gc_mark
14378
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14379
0
      if (!_bfd_elf_gc_mark (info, isec,
14380
0
           elf_gc_mark_debug_section))
14381
0
        return false;
14382
0
    }
14383
0
  return true;
14384
0
}
14385
14386
static bool
14387
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14388
0
{
14389
0
  bfd *sub;
14390
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14391
14392
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14393
0
    {
14394
0
      asection *o;
14395
14396
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14397
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14398
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14399
0
  continue;
14400
0
      o = sub->sections;
14401
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14402
0
  continue;
14403
14404
0
      for (o = sub->sections; o != NULL; o = o->next)
14405
0
  {
14406
    /* When any section in a section group is kept, we keep all
14407
       sections in the section group.  If the first member of
14408
       the section group is excluded, we will also exclude the
14409
       group section.  */
14410
0
    if (o->flags & SEC_GROUP)
14411
0
      {
14412
0
        asection *first = elf_next_in_group (o);
14413
0
        if (first != NULL)
14414
0
    o->gc_mark = first->gc_mark;
14415
0
      }
14416
14417
0
    if (o->gc_mark)
14418
0
      continue;
14419
14420
    /* Skip sweeping sections already excluded.  */
14421
0
    if (o->flags & SEC_EXCLUDE)
14422
0
      continue;
14423
14424
    /* Since this is early in the link process, it is simple
14425
       to remove a section from the output.  */
14426
0
    o->flags |= SEC_EXCLUDE;
14427
14428
0
    if (info->print_gc_sections && o->size != 0)
14429
      /* xgettext:c-format */
14430
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14431
0
        o, sub);
14432
0
  }
14433
0
    }
14434
14435
0
  return true;
14436
0
}
14437
14438
/* Propagate collected vtable information.  This is called through
14439
   elf_link_hash_traverse.  */
14440
14441
static bool
14442
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14443
0
{
14444
  /* Those that are not vtables.  */
14445
0
  if (h->start_stop
14446
0
      || h->u2.vtable == NULL
14447
0
      || h->u2.vtable->parent == NULL)
14448
0
    return true;
14449
14450
  /* Those vtables that do not have parents, we cannot merge.  */
14451
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14452
0
    return true;
14453
14454
  /* If we've already been done, exit.  */
14455
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14456
0
    return true;
14457
14458
  /* Make sure the parent's table is up to date.  */
14459
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14460
14461
0
  if (h->u2.vtable->used == NULL)
14462
0
    {
14463
      /* None of this table's entries were referenced.  Re-use the
14464
   parent's table.  */
14465
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14466
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14467
0
    }
14468
0
  else
14469
0
    {
14470
0
      size_t n;
14471
0
      bool *cu, *pu;
14472
14473
      /* Or the parent's entries into ours.  */
14474
0
      cu = h->u2.vtable->used;
14475
0
      cu[-1] = true;
14476
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14477
0
      if (pu != NULL)
14478
0
  {
14479
0
    const struct elf_backend_data *bed;
14480
0
    unsigned int log_file_align;
14481
14482
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14483
0
    log_file_align = bed->s->log_file_align;
14484
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14485
0
    while (n--)
14486
0
      {
14487
0
        if (*pu)
14488
0
    *cu = true;
14489
0
        pu++;
14490
0
        cu++;
14491
0
      }
14492
0
  }
14493
0
    }
14494
14495
0
  return true;
14496
0
}
14497
14498
struct link_info_ok
14499
{
14500
  struct bfd_link_info *info;
14501
  bool ok;
14502
};
14503
14504
static bool
14505
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14506
            void *ptr)
14507
0
{
14508
0
  asection *sec;
14509
0
  bfd_vma hstart, hend;
14510
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14511
0
  const struct elf_backend_data *bed;
14512
0
  unsigned int log_file_align;
14513
0
  struct link_info_ok *info = (struct link_info_ok *) ptr;
14514
14515
  /* Take care of both those symbols that do not describe vtables as
14516
     well as those that are not loaded.  */
14517
0
  if (h->start_stop
14518
0
      || h->u2.vtable == NULL
14519
0
      || h->u2.vtable->parent == NULL)
14520
0
    return true;
14521
14522
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14523
0
        || h->root.type == bfd_link_hash_defweak);
14524
14525
0
  sec = h->root.u.def.section;
14526
0
  hstart = h->root.u.def.value;
14527
0
  hend = hstart + h->size;
14528
14529
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14530
0
               sec, NULL, NULL, true);
14531
0
  if (!relstart)
14532
0
    return info->ok = false;
14533
0
  bed = get_elf_backend_data (sec->owner);
14534
0
  log_file_align = bed->s->log_file_align;
14535
14536
0
  relend = relstart + sec->reloc_count;
14537
14538
0
  for (rel = relstart; rel < relend; ++rel)
14539
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14540
0
      {
14541
  /* If the entry is in use, do nothing.  */
14542
0
  if (h->u2.vtable->used
14543
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14544
0
    {
14545
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14546
0
      if (h->u2.vtable->used[entry])
14547
0
        continue;
14548
0
    }
14549
  /* Otherwise, kill it.  */
14550
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14551
0
      }
14552
14553
0
  return true;
14554
0
}
14555
14556
/* Mark sections containing dynamically referenced symbols.  When
14557
   building shared libraries, we must assume that any visible symbol is
14558
   referenced.  */
14559
14560
bool
14561
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14562
0
{
14563
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
14564
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14565
14566
0
  if ((h->root.type == bfd_link_hash_defined
14567
0
       || h->root.type == bfd_link_hash_defweak)
14568
0
      && (!h->start_stop
14569
0
    || h->root.ldscript_def
14570
0
    || !info->start_stop_gc)
14571
0
      && ((h->ref_dynamic && !h->forced_local)
14572
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14573
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14574
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14575
0
        && (!bfd_link_executable (info)
14576
0
      || info->gc_keep_exported
14577
0
      || info->export_dynamic
14578
0
      || (h->dynamic
14579
0
          && d != NULL
14580
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14581
0
        && (h->versioned >= versioned
14582
0
      || !bfd_hide_sym_by_version (info->version_info,
14583
0
                 h->root.root.string)))))
14584
0
    h->root.u.def.section->flags |= SEC_KEEP;
14585
14586
0
  return true;
14587
0
}
14588
14589
/* Keep all sections containing symbols undefined on the command-line,
14590
   and the section containing the entry symbol.  */
14591
14592
void
14593
_bfd_elf_gc_keep (struct bfd_link_info *info)
14594
0
{
14595
0
  struct bfd_sym_chain *sym;
14596
14597
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14598
0
    {
14599
0
      struct elf_link_hash_entry *h;
14600
14601
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14602
0
        false, false, false);
14603
14604
0
      if (h != NULL
14605
0
    && (h->root.type == bfd_link_hash_defined
14606
0
        || h->root.type == bfd_link_hash_defweak)
14607
0
    && !bfd_is_const_section (h->root.u.def.section))
14608
0
  h->root.u.def.section->flags |= SEC_KEEP;
14609
0
    }
14610
0
}
14611
14612
bool
14613
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14614
        struct bfd_link_info *info)
14615
0
{
14616
0
  bfd *ibfd = info->input_bfds;
14617
14618
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14619
0
    {
14620
0
      asection *sec;
14621
0
      struct elf_reloc_cookie cookie;
14622
14623
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14624
0
  continue;
14625
0
      sec = ibfd->sections;
14626
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14627
0
  continue;
14628
14629
0
      if (!init_reloc_cookie (&cookie, info, ibfd, false))
14630
0
  return false;
14631
14632
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14633
0
  {
14634
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14635
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14636
0
           false))
14637
0
      {
14638
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14639
0
        fini_reloc_cookie_rels (&cookie, sec);
14640
0
      }
14641
0
  }
14642
0
    }
14643
0
  return true;
14644
0
}
14645
14646
/* Do mark and sweep of unused sections.  */
14647
14648
bool
14649
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14650
0
{
14651
0
  bool ok = true;
14652
0
  bfd *sub;
14653
0
  elf_gc_mark_hook_fn gc_mark_hook;
14654
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14655
0
  struct elf_link_hash_table *htab;
14656
0
  struct link_info_ok info_ok;
14657
14658
0
  if (!bed->can_gc_sections
14659
0
      || !is_elf_hash_table (info->hash))
14660
0
    {
14661
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14662
0
      return true;
14663
0
    }
14664
14665
0
  bed->gc_keep (info);
14666
0
  htab = elf_hash_table (info);
14667
14668
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14669
     at the .eh_frame section if we can mark the FDEs individually.  */
14670
0
  for (sub = info->input_bfds;
14671
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14672
0
       sub = sub->link.next)
14673
0
    {
14674
0
      asection *sec;
14675
0
      struct elf_reloc_cookie cookie;
14676
14677
0
      sec = sub->sections;
14678
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14679
0
  continue;
14680
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14681
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14682
0
               false))
14683
0
  {
14684
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14685
0
    if (elf_section_data (sec)->sec_info
14686
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14687
0
      elf_eh_frame_section (sub) = sec;
14688
0
    fini_reloc_cookie_for_section (&cookie, sec);
14689
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14690
0
  }
14691
0
    }
14692
14693
  /* Apply transitive closure to the vtable entry usage info.  */
14694
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14695
0
  if (!ok)
14696
0
    return false;
14697
14698
  /* Kill the vtable relocations that were not used.  */
14699
0
  info_ok.info = info;
14700
0
  info_ok.ok = true;
14701
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14702
0
  if (!info_ok.ok)
14703
0
    return false;
14704
14705
  /* Mark dynamically referenced symbols.  */
14706
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14707
0
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14708
14709
  /* Grovel through relocs to find out who stays ...  */
14710
0
  gc_mark_hook = bed->gc_mark_hook;
14711
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14712
0
    {
14713
0
      asection *o;
14714
14715
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14716
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14717
0
    || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14718
0
  continue;
14719
14720
0
      o = sub->sections;
14721
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14722
0
  continue;
14723
14724
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14725
   Also treat note sections as a root, if the section is not part
14726
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14727
   well as FINI_ARRAY sections for ld -r.  */
14728
0
      for (o = sub->sections; o != NULL; o = o->next)
14729
0
  if (!o->gc_mark
14730
0
      && (o->flags & SEC_EXCLUDE) == 0
14731
0
      && ((o->flags & SEC_KEEP) != 0
14732
0
    || (bfd_link_relocatable (info)
14733
0
        && ((elf_section_data (o)->this_hdr.sh_type
14734
0
       == SHT_PREINIT_ARRAY)
14735
0
      || (elf_section_data (o)->this_hdr.sh_type
14736
0
          == SHT_INIT_ARRAY)
14737
0
      || (elf_section_data (o)->this_hdr.sh_type
14738
0
          == SHT_FINI_ARRAY)))
14739
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14740
0
        && elf_next_in_group (o) == NULL
14741
0
        && elf_linked_to_section (o) == NULL)
14742
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14743
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14744
0
    {
14745
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14746
0
        return false;
14747
0
    }
14748
0
    }
14749
14750
  /* Allow the backend to mark additional target specific sections.  */
14751
0
  bed->gc_mark_extra_sections (info, gc_mark_hook);
14752
14753
  /* ... and mark SEC_EXCLUDE for those that go.  */
14754
0
  return elf_gc_sweep (abfd, info);
14755
0
}
14756

14757
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14758
14759
bool
14760
bfd_elf_gc_record_vtinherit (bfd *abfd,
14761
           asection *sec,
14762
           struct elf_link_hash_entry *h,
14763
           bfd_vma offset)
14764
0
{
14765
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14766
0
  struct elf_link_hash_entry **search, *child;
14767
0
  size_t extsymcount;
14768
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14769
14770
  /* The sh_info field of the symtab header tells us where the
14771
     external symbols start.  We don't care about the local symbols at
14772
     this point.  */
14773
0
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14774
0
  if (!elf_bad_symtab (abfd))
14775
0
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14776
14777
0
  sym_hashes = elf_sym_hashes (abfd);
14778
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14779
14780
  /* Hunt down the child symbol, which is in this section at the same
14781
     offset as the relocation.  */
14782
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14783
0
    {
14784
0
      if ((child = *search) != NULL
14785
0
    && (child->root.type == bfd_link_hash_defined
14786
0
        || child->root.type == bfd_link_hash_defweak)
14787
0
    && child->root.u.def.section == sec
14788
0
    && child->root.u.def.value == offset)
14789
0
  goto win;
14790
0
    }
14791
14792
  /* xgettext:c-format */
14793
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14794
0
          abfd, sec, (uint64_t) offset);
14795
0
  bfd_set_error (bfd_error_invalid_operation);
14796
0
  return false;
14797
14798
0
 win:
14799
0
  if (!child->u2.vtable)
14800
0
    {
14801
0
      child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14802
0
        bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14803
0
      if (!child->u2.vtable)
14804
0
  return false;
14805
0
    }
14806
0
  if (!h)
14807
0
    {
14808
      /* This *should* only be the absolute section.  It could potentially
14809
   be that someone has defined a non-global vtable though, which
14810
   would be bad.  It isn't worth paging in the local symbols to be
14811
   sure though; that case should simply be handled by the assembler.  */
14812
14813
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14814
0
    }
14815
0
  else
14816
0
    child->u2.vtable->parent = h;
14817
14818
0
  return true;
14819
0
}
14820
14821
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14822
14823
bool
14824
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14825
         struct elf_link_hash_entry *h,
14826
         bfd_vma addend)
14827
0
{
14828
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14829
0
  unsigned int log_file_align = bed->s->log_file_align;
14830
14831
0
  if (!h)
14832
0
    {
14833
      /* xgettext:c-format */
14834
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14835
0
        abfd, sec);
14836
0
      bfd_set_error (bfd_error_bad_value);
14837
0
      return false;
14838
0
    }
14839
14840
0
  if (!h->u2.vtable)
14841
0
    {
14842
0
      h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14843
0
          bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14844
0
      if (!h->u2.vtable)
14845
0
  return false;
14846
0
    }
14847
14848
0
  if (addend >= h->u2.vtable->size)
14849
0
    {
14850
0
      size_t size, bytes, file_align;
14851
0
      bool *ptr = h->u2.vtable->used;
14852
14853
      /* While the symbol is undefined, we have to be prepared to handle
14854
   a zero size.  */
14855
0
      file_align = 1 << log_file_align;
14856
0
      if (h->root.type == bfd_link_hash_undefined)
14857
0
  size = addend + file_align;
14858
0
      else
14859
0
  {
14860
0
    size = h->size;
14861
0
    if (addend >= size)
14862
0
      {
14863
        /* Oops!  We've got a reference past the defined end of
14864
     the table.  This is probably a bug -- shall we warn?  */
14865
0
        size = addend + file_align;
14866
0
      }
14867
0
  }
14868
0
      size = (size + file_align - 1) & -file_align;
14869
14870
      /* Allocate one extra entry for use as a "done" flag for the
14871
   consolidation pass.  */
14872
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14873
14874
0
      if (ptr)
14875
0
  {
14876
0
    ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14877
14878
0
    if (ptr != NULL)
14879
0
      {
14880
0
        size_t oldbytes;
14881
14882
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14883
0
        * sizeof (bool));
14884
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14885
0
      }
14886
0
  }
14887
0
      else
14888
0
  ptr = (bool *) bfd_zmalloc (bytes);
14889
14890
0
      if (ptr == NULL)
14891
0
  return false;
14892
14893
      /* And arrange for that done flag to be at index -1.  */
14894
0
      h->u2.vtable->used = ptr + 1;
14895
0
      h->u2.vtable->size = size;
14896
0
    }
14897
14898
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14899
14900
0
  return true;
14901
0
}
14902
14903
/* Map an ELF section header flag to its corresponding string.  */
14904
typedef struct
14905
{
14906
  char *flag_name;
14907
  flagword flag_value;
14908
} elf_flags_to_name_table;
14909
14910
static const elf_flags_to_name_table elf_flags_to_names [] =
14911
{
14912
  { "SHF_WRITE", SHF_WRITE },
14913
  { "SHF_ALLOC", SHF_ALLOC },
14914
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14915
  { "SHF_MERGE", SHF_MERGE },
14916
  { "SHF_STRINGS", SHF_STRINGS },
14917
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14918
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14919
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14920
  { "SHF_GROUP", SHF_GROUP },
14921
  { "SHF_TLS", SHF_TLS },
14922
  { "SHF_MASKOS", SHF_MASKOS },
14923
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14924
};
14925
14926
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14927
bool
14928
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14929
            struct flag_info *flaginfo,
14930
            asection *section)
14931
0
{
14932
0
  const bfd_vma sh_flags = elf_section_flags (section);
14933
14934
0
  if (!flaginfo->flags_initialized)
14935
0
    {
14936
0
      bfd *obfd = info->output_bfd;
14937
0
      const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14938
0
      struct flag_info_list *tf = flaginfo->flag_list;
14939
0
      int with_hex = 0;
14940
0
      int without_hex = 0;
14941
14942
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14943
0
  {
14944
0
    unsigned i;
14945
0
    flagword (*lookup) (char *);
14946
14947
0
    lookup = bed->elf_backend_lookup_section_flags_hook;
14948
0
    if (lookup != NULL)
14949
0
      {
14950
0
        flagword hexval = (*lookup) ((char *) tf->name);
14951
14952
0
        if (hexval != 0)
14953
0
    {
14954
0
      if (tf->with == with_flags)
14955
0
        with_hex |= hexval;
14956
0
      else if (tf->with == without_flags)
14957
0
        without_hex |= hexval;
14958
0
      tf->valid = true;
14959
0
      continue;
14960
0
    }
14961
0
      }
14962
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14963
0
      {
14964
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14965
0
    {
14966
0
      if (tf->with == with_flags)
14967
0
        with_hex |= elf_flags_to_names[i].flag_value;
14968
0
      else if (tf->with == without_flags)
14969
0
        without_hex |= elf_flags_to_names[i].flag_value;
14970
0
      tf->valid = true;
14971
0
      break;
14972
0
    }
14973
0
      }
14974
0
    if (!tf->valid)
14975
0
      {
14976
0
        info->callbacks->einfo
14977
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14978
0
        return false;
14979
0
      }
14980
0
  }
14981
0
      flaginfo->flags_initialized = true;
14982
0
      flaginfo->only_with_flags |= with_hex;
14983
0
      flaginfo->not_with_flags |= without_hex;
14984
0
    }
14985
14986
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14987
0
    return false;
14988
14989
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
14990
0
    return false;
14991
14992
0
  return true;
14993
0
}
14994
14995
struct alloc_got_off_arg {
14996
  bfd_vma gotoff;
14997
  struct bfd_link_info *info;
14998
};
14999
15000
/* We need a special top-level link routine to convert got reference counts
15001
   to real got offsets.  */
15002
15003
static bool
15004
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15005
0
{
15006
0
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
15007
0
  bfd *obfd = gofarg->info->output_bfd;
15008
0
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
15009
15010
0
  if (h->got.refcount > 0)
15011
0
    {
15012
0
      h->got.offset = gofarg->gotoff;
15013
0
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
15014
0
    }
15015
0
  else
15016
0
    h->got.offset = (bfd_vma) -1;
15017
15018
0
  return true;
15019
0
}
15020
15021
/* And an accompanying bit to work out final got entry offsets once
15022
   we're done.  Should be called from final_link.  */
15023
15024
bool
15025
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15026
          struct bfd_link_info *info)
15027
0
{
15028
0
  bfd *i;
15029
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15030
0
  bfd_vma gotoff;
15031
0
  struct alloc_got_off_arg gofarg;
15032
15033
0
  BFD_ASSERT (abfd == info->output_bfd);
15034
15035
0
  if (! is_elf_hash_table (info->hash))
15036
0
    return false;
15037
15038
  /* The GOT offset is relative to the .got section, but the GOT header is
15039
     put into the .got.plt section, if the backend uses it.  */
15040
0
  if (bed->want_got_plt)
15041
0
    gotoff = 0;
15042
0
  else
15043
0
    gotoff = bed->got_header_size;
15044
15045
  /* Do the local .got entries first.  */
15046
0
  for (i = info->input_bfds; i; i = i->link.next)
15047
0
    {
15048
0
      bfd_signed_vma *local_got;
15049
0
      size_t j, locsymcount;
15050
0
      Elf_Internal_Shdr *symtab_hdr;
15051
15052
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15053
0
  continue;
15054
15055
0
      local_got = elf_local_got_refcounts (i);
15056
0
      if (!local_got)
15057
0
  continue;
15058
15059
0
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
15060
0
      if (elf_bad_symtab (i))
15061
0
  locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15062
0
      else
15063
0
  locsymcount = symtab_hdr->sh_info;
15064
15065
0
      for (j = 0; j < locsymcount; ++j)
15066
0
  {
15067
0
    if (local_got[j] > 0)
15068
0
      {
15069
0
        local_got[j] = gotoff;
15070
0
        gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15071
0
      }
15072
0
    else
15073
0
      local_got[j] = (bfd_vma) -1;
15074
0
  }
15075
0
    }
15076
15077
  /* Then the global .got entries.  .plt refcounts are handled by
15078
     adjust_dynamic_symbol  */
15079
0
  gofarg.gotoff = gotoff;
15080
0
  gofarg.info = info;
15081
0
  elf_link_hash_traverse (elf_hash_table (info),
15082
0
        elf_gc_allocate_got_offsets,
15083
0
        &gofarg);
15084
0
  return true;
15085
0
}
15086
15087
/* Many folk need no more in the way of final link than this, once
15088
   got entry reference counting is enabled.  */
15089
15090
bool
15091
bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15092
0
{
15093
0
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15094
0
    return false;
15095
15096
  /* Invoke the regular ELF backend linker to do all the work.  */
15097
0
  return bfd_elf_final_link (abfd, info);
15098
0
}
15099
15100
bool
15101
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15102
0
{
15103
0
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15104
15105
0
  if (rcookie->bad_symtab)
15106
0
    rcookie->rel = rcookie->rels;
15107
15108
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15109
0
    {
15110
0
      unsigned long r_symndx;
15111
15112
0
      if (! rcookie->bad_symtab)
15113
0
  if (rcookie->rel->r_offset > offset)
15114
0
    return false;
15115
0
      if (rcookie->rel->r_offset != offset)
15116
0
  continue;
15117
15118
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15119
0
      if (r_symndx == STN_UNDEF)
15120
0
  return true;
15121
15122
0
      struct elf_link_hash_entry *h;
15123
15124
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15125
      
15126
0
      if (h != NULL)
15127
0
  {
15128
0
    if ((h->root.type == bfd_link_hash_defined
15129
0
         || h->root.type == bfd_link_hash_defweak)
15130
0
        && (h->root.u.def.section->owner != rcookie->abfd
15131
0
      || h->root.u.def.section->kept_section != NULL
15132
0
      || discarded_section (h->root.u.def.section)))
15133
0
      return true;
15134
0
  }
15135
0
      else
15136
0
  {
15137
0
    if (r_symndx >= rcookie->locsymcount)
15138
      /* This can happen with corrupt input.  */
15139
0
      return false;
15140
15141
    /* It's not a relocation against a global symbol,
15142
       but it could be a relocation against a local
15143
       symbol for a discarded section.  */
15144
0
    asection *isec;
15145
0
    Elf_Internal_Sym *isym;
15146
15147
    /* Need to: get the symbol; get the section.  */
15148
0
    isym = &rcookie->locsyms[r_symndx];
15149
0
    isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15150
0
    if (isec != NULL
15151
0
        && (isec->kept_section != NULL
15152
0
      || discarded_section (isec)))
15153
0
      return true;
15154
0
  }
15155
15156
0
      return false;
15157
0
    }
15158
0
  return false;
15159
0
}
15160
15161
/* Discard unneeded references to discarded sections.
15162
   Returns -1 on error, 1 if any section's size was changed, 0 if
15163
   nothing changed.  This function assumes that the relocations are in
15164
   sorted order, which is true for all known assemblers.  */
15165
15166
int
15167
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15168
0
{
15169
0
  struct elf_reloc_cookie cookie;
15170
0
  asection *o;
15171
0
  bfd *abfd;
15172
0
  int changed = 0;
15173
15174
0
  if (info->traditional_format
15175
0
      || !is_elf_hash_table (info->hash))
15176
0
    return 0;
15177
15178
0
  o = bfd_get_section_by_name (output_bfd, ".stab");
15179
0
  if (o != NULL)
15180
0
    {
15181
0
      asection *i;
15182
15183
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15184
0
  {
15185
0
    if (i->size == 0
15186
0
        || i->reloc_count == 0
15187
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15188
0
      continue;
15189
15190
0
    abfd = i->owner;
15191
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15192
0
      continue;
15193
15194
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15195
0
      return -1;
15196
15197
0
    if (_bfd_discard_section_stabs (abfd, i,
15198
0
            elf_section_data (i)->sec_info,
15199
0
            bfd_elf_reloc_symbol_deleted_p,
15200
0
            &cookie))
15201
0
      changed = 1;
15202
15203
0
    fini_reloc_cookie_for_section (&cookie, i);
15204
0
  }
15205
0
    }
15206
15207
0
  o = NULL;
15208
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15209
0
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15210
0
  if (o != NULL)
15211
0
    {
15212
0
      asection *i;
15213
0
      int eh_changed = 0;
15214
0
      unsigned int eh_alignment;  /* Octets.  */
15215
15216
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15217
0
  {
15218
0
    if (i->size == 0)
15219
0
      continue;
15220
15221
0
    abfd = i->owner;
15222
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15223
0
      continue;
15224
15225
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15226
0
      return -1;
15227
15228
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15229
0
    if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15230
0
             bfd_elf_reloc_symbol_deleted_p,
15231
0
             &cookie))
15232
0
      {
15233
0
        eh_changed = 1;
15234
0
        if (i->size != i->rawsize)
15235
0
    changed = 1;
15236
0
      }
15237
15238
0
    fini_reloc_cookie_for_section (&cookie, i);
15239
0
  }
15240
15241
0
      eh_alignment = ((1 << o->alignment_power)
15242
0
          * bfd_octets_per_byte (output_bfd, o));
15243
      /* Skip over zero terminator, and prevent empty sections from
15244
   adding alignment padding at the end.  */
15245
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15246
0
  if (i->size == 0)
15247
0
    i->flags |= SEC_EXCLUDE;
15248
0
  else if (i->size > 4)
15249
0
    break;
15250
      /* The last non-empty eh_frame section doesn't need padding.  */
15251
0
      if (i != NULL)
15252
0
  i = i->map_tail.s;
15253
      /* Any prior sections must pad the last FDE out to the output
15254
   section alignment.  Otherwise we might have zero padding
15255
   between sections, which would be seen as a terminator.  */
15256
0
      for (; i != NULL; i = i->map_tail.s)
15257
0
  if (i->size == 4)
15258
    /* All but the last zero terminator should have been removed.  */
15259
0
    BFD_FAIL ();
15260
0
  else
15261
0
    {
15262
0
      bfd_size_type size
15263
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15264
0
      if (i->size != size)
15265
0
        {
15266
0
    i->size = size;
15267
0
    changed = 1;
15268
0
    eh_changed = 1;
15269
0
        }
15270
0
    }
15271
0
      if (eh_changed)
15272
0
  elf_link_hash_traverse (elf_hash_table (info),
15273
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15274
0
    }
15275
15276
0
  o = bfd_get_section_by_name (output_bfd, ".sframe");
15277
0
  if (o != NULL)
15278
0
    {
15279
0
      asection *i;
15280
15281
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15282
0
  {
15283
0
    if (i->size == 0)
15284
0
      continue;
15285
15286
0
    abfd = i->owner;
15287
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15288
0
      continue;
15289
15290
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15291
0
      return -1;
15292
15293
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15294
0
      {
15295
0
        if (_bfd_elf_discard_section_sframe (i,
15296
0
               bfd_elf_reloc_symbol_deleted_p,
15297
0
               &cookie))
15298
0
    {
15299
0
      if (i->size != i->rawsize)
15300
0
        changed = 1;
15301
0
    }
15302
0
      }
15303
0
    fini_reloc_cookie_for_section (&cookie, i);
15304
0
  }
15305
      /* Update the reference to the output .sframe section.  Used to
15306
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15307
0
      if (!_bfd_elf_set_section_sframe (output_bfd, info))
15308
0
  return -1;
15309
0
    }
15310
15311
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15312
0
    {
15313
0
      const struct elf_backend_data *bed;
15314
0
      asection *s;
15315
15316
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15317
0
  continue;
15318
0
      s = abfd->sections;
15319
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15320
0
  continue;
15321
15322
0
      bed = get_elf_backend_data (abfd);
15323
15324
0
      if (bed->elf_backend_discard_info != NULL)
15325
0
  {
15326
0
    if (!init_reloc_cookie (&cookie, info, abfd, false))
15327
0
      return -1;
15328
15329
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15330
0
      changed = 1;
15331
15332
0
    fini_reloc_cookie (&cookie, abfd);
15333
0
  }
15334
0
    }
15335
15336
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15337
0
    _bfd_elf_end_eh_frame_parsing (info);
15338
15339
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15340
0
    changed = 1;
15341
15342
0
  return changed;
15343
0
}
15344
15345
bool
15346
_bfd_elf_section_already_linked (bfd *abfd,
15347
         asection *sec,
15348
         struct bfd_link_info *info)
15349
0
{
15350
0
  flagword flags;
15351
0
  const char *name, *key;
15352
0
  struct bfd_section_already_linked *l;
15353
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15354
15355
0
  if (sec->output_section == bfd_abs_section_ptr)
15356
0
    return false;
15357
15358
0
  flags = sec->flags;
15359
15360
  /* Return if it isn't a linkonce section.  A comdat group section
15361
     also has SEC_LINK_ONCE set.  */
15362
0
  if ((flags & SEC_LINK_ONCE) == 0)
15363
0
    return false;
15364
15365
  /* Don't put group member sections on our list of already linked
15366
     sections.  They are handled as a group via their group section.  */
15367
0
  if (elf_sec_group (sec) != NULL)
15368
0
    return false;
15369
15370
  /* For a SHT_GROUP section, use the group signature as the key.  */
15371
0
  name = sec->name;
15372
0
  if ((flags & SEC_GROUP) != 0
15373
0
      && elf_next_in_group (sec) != NULL
15374
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15375
0
    key = elf_group_name (elf_next_in_group (sec));
15376
0
  else
15377
0
    {
15378
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15379
0
      if (startswith (name, ".gnu.linkonce.")
15380
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15381
0
  key++;
15382
0
      else
15383
  /* Must be a user linkonce section that doesn't follow gcc's
15384
     naming convention.  In this case we won't be matching
15385
     single member groups.  */
15386
0
  key = name;
15387
0
    }
15388
15389
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15390
15391
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15392
0
    {
15393
      /* We may have 2 different types of sections on the list: group
15394
   sections with a signature of <key> (<key> is some string),
15395
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15396
   Match like sections.  LTO plugin sections are an exception.
15397
   They are always named .gnu.linkonce.t.<key> and match either
15398
   type of section.  */
15399
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15400
0
     && ((flags & SEC_GROUP) != 0
15401
0
         || strcmp (name, l->sec->name) == 0))
15402
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15403
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15404
0
  {
15405
    /* The section has already been linked.  See if we should
15406
       issue a warning.  */
15407
0
    if (!_bfd_handle_already_linked (sec, l, info))
15408
0
      return false;
15409
15410
0
    if (flags & SEC_GROUP)
15411
0
      {
15412
0
        asection *first = elf_next_in_group (sec);
15413
0
        asection *s = first;
15414
15415
0
        while (s != NULL)
15416
0
    {
15417
0
      s->output_section = bfd_abs_section_ptr;
15418
      /* Record which group discards it.  */
15419
0
      s->kept_section = l->sec;
15420
0
      s = elf_next_in_group (s);
15421
      /* These lists are circular.  */
15422
0
      if (s == first)
15423
0
        break;
15424
0
    }
15425
0
      }
15426
15427
0
    return true;
15428
0
  }
15429
0
    }
15430
15431
  /* A single member comdat group section may be discarded by a
15432
     linkonce section and vice versa.  */
15433
0
  if ((flags & SEC_GROUP) != 0)
15434
0
    {
15435
0
      asection *first = elf_next_in_group (sec);
15436
15437
0
      if (first != NULL && elf_next_in_group (first) == first)
15438
  /* Check this single member group against linkonce sections.  */
15439
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15440
0
    if ((l->sec->flags & SEC_GROUP) == 0
15441
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15442
0
      {
15443
0
        first->output_section = bfd_abs_section_ptr;
15444
0
        first->kept_section = l->sec;
15445
0
        sec->output_section = bfd_abs_section_ptr;
15446
0
        break;
15447
0
      }
15448
0
    }
15449
0
  else
15450
    /* Check this linkonce section against single member groups.  */
15451
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15452
0
      if (l->sec->flags & SEC_GROUP)
15453
0
  {
15454
0
    asection *first = elf_next_in_group (l->sec);
15455
15456
0
    if (first != NULL
15457
0
        && elf_next_in_group (first) == first
15458
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15459
0
      {
15460
0
        sec->output_section = bfd_abs_section_ptr;
15461
0
        sec->kept_section = first;
15462
0
        break;
15463
0
      }
15464
0
  }
15465
15466
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15467
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15468
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15469
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15470
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15471
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15472
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15473
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15474
     The reverse order cannot happen as there is never a bfd with only the
15475
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15476
     matter as here were are looking only for cross-bfd sections.  */
15477
15478
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15479
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15480
0
      if ((l->sec->flags & SEC_GROUP) == 0
15481
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15482
0
  {
15483
0
    if (abfd != l->sec->owner)
15484
0
      sec->output_section = bfd_abs_section_ptr;
15485
0
    break;
15486
0
  }
15487
15488
  /* This is the first section with this name.  Record it.  */
15489
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15490
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15491
0
  return sec->output_section == bfd_abs_section_ptr;
15492
0
}
15493
15494
bool
15495
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15496
0
{
15497
0
  return sym->st_shndx == SHN_COMMON;
15498
0
}
15499
15500
unsigned int
15501
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15502
0
{
15503
0
  return SHN_COMMON;
15504
0
}
15505
15506
asection *
15507
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15508
0
{
15509
0
  return bfd_com_section_ptr;
15510
0
}
15511
15512
bfd_vma
15513
_bfd_elf_default_got_elt_size (bfd *abfd,
15514
             struct bfd_link_info *info ATTRIBUTE_UNUSED,
15515
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15516
             bfd *ibfd ATTRIBUTE_UNUSED,
15517
             unsigned long symndx ATTRIBUTE_UNUSED)
15518
0
{
15519
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15520
0
  return bed->s->arch_size / 8;
15521
0
}
15522
15523
/* Routines to support the creation of dynamic relocs.  */
15524
15525
/* Returns the name of the dynamic reloc section associated with SEC.  */
15526
15527
static const char *
15528
get_dynamic_reloc_section_name (bfd *       abfd,
15529
        asection *  sec,
15530
        bool is_rela)
15531
0
{
15532
0
  char *name;
15533
0
  const char *old_name = bfd_section_name (sec);
15534
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15535
15536
0
  if (old_name == NULL)
15537
0
    return NULL;
15538
15539
0
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15540
0
  sprintf (name, "%s%s", prefix, old_name);
15541
15542
0
  return name;
15543
0
}
15544
15545
/* Returns the dynamic reloc section associated with SEC.
15546
   If necessary compute the name of the dynamic reloc section based
15547
   on SEC's name (looked up in ABFD's string table) and the setting
15548
   of IS_RELA.  */
15549
15550
asection *
15551
_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15552
            asection *sec,
15553
            bool is_rela)
15554
0
{
15555
0
  asection *reloc_sec = elf_section_data (sec)->sreloc;
15556
15557
0
  if (reloc_sec == NULL)
15558
0
    {
15559
0
      const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15560
15561
0
      if (name != NULL)
15562
0
  {
15563
0
    reloc_sec = bfd_get_linker_section (abfd, name);
15564
15565
0
    if (reloc_sec != NULL)
15566
0
      elf_section_data (sec)->sreloc = reloc_sec;
15567
0
  }
15568
0
    }
15569
15570
0
  return reloc_sec;
15571
0
}
15572
15573
/* Returns the dynamic reloc section associated with SEC.  If the
15574
   section does not exist it is created and attached to the DYNOBJ
15575
   bfd and stored in the SRELOC field of SEC's elf_section_data
15576
   structure.
15577
15578
   ALIGNMENT is the alignment for the newly created section and
15579
   IS_RELA defines whether the name should be .rela.<SEC's name>
15580
   or .rel.<SEC's name>.  The section name is looked up in the
15581
   string table associated with ABFD.  */
15582
15583
asection *
15584
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15585
             bfd *dynobj,
15586
             unsigned int alignment,
15587
             bfd *abfd,
15588
             bool is_rela)
15589
0
{
15590
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15591
15592
0
  if (reloc_sec == NULL)
15593
0
    {
15594
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15595
15596
0
      if (name == NULL)
15597
0
  return NULL;
15598
15599
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15600
15601
0
      if (reloc_sec == NULL)
15602
0
  {
15603
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15604
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15605
0
    if ((sec->flags & SEC_ALLOC) != 0)
15606
0
      flags |= SEC_ALLOC | SEC_LOAD;
15607
15608
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15609
0
    if (reloc_sec != NULL)
15610
0
      {
15611
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15612
     name.  Override as it may be wrong, eg. for a user
15613
     section named "auto" we'll get ".relauto" which is
15614
     seen to be a .rela section.  */
15615
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15616
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15617
0
    reloc_sec = NULL;
15618
0
      }
15619
0
  }
15620
15621
0
      elf_section_data (sec)->sreloc = reloc_sec;
15622
0
    }
15623
15624
0
  return reloc_sec;
15625
0
}
15626
15627
/* Copy the ELF symbol type and other attributes for a linker script
15628
   assignment from HSRC to HDEST.  Generally this should be treated as
15629
   if we found a strong non-dynamic definition for HDEST (except that
15630
   ld ignores multiple definition errors).  */
15631
void
15632
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15633
             struct bfd_link_hash_entry *hdest,
15634
             struct bfd_link_hash_entry *hsrc)
15635
0
{
15636
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15637
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15638
0
  Elf_Internal_Sym isym;
15639
15640
0
  ehdest->type = ehsrc->type;
15641
0
  ehdest->target_internal = ehsrc->target_internal;
15642
15643
0
  isym.st_other = ehsrc->other;
15644
0
  elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15645
0
}
15646
15647
/* Append a RELA relocation REL to section S in BFD.  */
15648
15649
void
15650
elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15651
0
{
15652
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15653
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15654
0
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15655
0
  bed->s->swap_reloca_out (abfd, rel, loc);
15656
0
}
15657
15658
/* Append a REL relocation REL to section S in BFD.  */
15659
15660
void
15661
elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15662
0
{
15663
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15664
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15665
0
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15666
0
  bed->s->swap_reloc_out (abfd, rel, loc);
15667
0
}
15668
15669
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15670
15671
struct bfd_link_hash_entry *
15672
bfd_elf_define_start_stop (struct bfd_link_info *info,
15673
         const char *symbol, asection *sec)
15674
0
{
15675
0
  struct elf_link_hash_entry *h;
15676
15677
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15678
0
          false, false, true);
15679
  /* NB: Common symbols will be turned into definition later.  */
15680
0
  if (h != NULL
15681
0
      && !h->root.ldscript_def
15682
0
      && (h->root.type == bfd_link_hash_undefined
15683
0
    || h->root.type == bfd_link_hash_undefweak
15684
0
    || ((h->ref_regular || h->def_dynamic)
15685
0
        && !h->def_regular
15686
0
        && h->root.type != bfd_link_hash_common)))
15687
0
    {
15688
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15689
0
      h->verinfo.verdef = NULL;
15690
0
      h->root.type = bfd_link_hash_defined;
15691
0
      h->root.u.def.section = sec;
15692
0
      h->root.u.def.value = 0;
15693
0
      h->def_regular = 1;
15694
0
      h->def_dynamic = 0;
15695
0
      h->start_stop = 1;
15696
0
      h->u2.start_stop_section = sec;
15697
0
      if (symbol[0] == '.')
15698
0
  {
15699
    /* .startof. and .sizeof. symbols are local.  */
15700
0
    const struct elf_backend_data *bed;
15701
0
    bed = get_elf_backend_data (info->output_bfd);
15702
0
    (*bed->elf_backend_hide_symbol) (info, h, true);
15703
0
  }
15704
0
      else
15705
0
  {
15706
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15707
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15708
0
      | info->start_stop_visibility);
15709
0
    if (was_dynamic)
15710
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15711
0
  }
15712
0
      return &h->root;
15713
0
    }
15714
0
  return NULL;
15715
0
}
15716
15717
/* Find dynamic relocs for H that apply to read-only sections.  */
15718
15719
asection *
15720
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15721
0
{
15722
0
  struct elf_dyn_relocs *p;
15723
15724
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15725
0
    {
15726
0
      asection *s = p->sec->output_section;
15727
15728
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15729
0
  return p->sec;
15730
0
    }
15731
0
  return NULL;
15732
0
}
15733
15734
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15735
   read-only sections.  */
15736
15737
bool
15738
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15739
0
{
15740
0
  asection *sec;
15741
15742
0
  if (h->root.type == bfd_link_hash_indirect)
15743
0
    return true;
15744
15745
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15746
0
  if (sec != NULL)
15747
0
    {
15748
0
      struct bfd_link_info *info = (struct bfd_link_info *) inf;
15749
15750
0
      info->flags |= DF_TEXTREL;
15751
      /* xgettext:c-format */
15752
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15753
0
        "in read-only section `%pA'\n"),
15754
0
            sec->owner, h->root.root.string, sec);
15755
15756
0
      if (bfd_link_textrel_check (info))
15757
  /* xgettext:c-format */
15758
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15759
0
          "in read-only section `%pA'\n"),
15760
0
        sec->owner, h->root.root.string, sec);
15761
15762
      /* Not an error, just cut short the traversal.  */
15763
0
      return false;
15764
0
    }
15765
0
  return true;
15766
0
}
15767
15768
/* Add dynamic tags.  */
15769
15770
bool
15771
_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15772
         bool need_dynamic_reloc)
15773
0
{
15774
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15775
15776
0
  if (htab->dynamic_sections_created)
15777
0
    {
15778
      /* Add some entries to the .dynamic section.  We fill in the
15779
   values later, in finish_dynamic_sections, but we must add
15780
   the entries now so that we get the correct size for the
15781
   .dynamic section.  The DT_DEBUG entry is filled in by the
15782
   dynamic linker and used by the debugger.  */
15783
0
#define add_dynamic_entry(TAG, VAL) \
15784
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15785
15786
0
      const struct elf_backend_data *bed
15787
0
  = get_elf_backend_data (output_bfd);
15788
15789
0
      if (bfd_link_executable (info))
15790
0
  {
15791
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15792
0
      return false;
15793
0
  }
15794
15795
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15796
0
  {
15797
    /* DT_PLTGOT is used by prelink even if there is no PLT
15798
       relocation.  */
15799
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15800
0
      return false;
15801
0
  }
15802
15803
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15804
0
  {
15805
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15806
0
        || !add_dynamic_entry (DT_PLTREL,
15807
0
             (bed->rela_plts_and_copies_p
15808
0
              ? DT_RELA : DT_REL))
15809
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15810
0
      return false;
15811
0
  }
15812
15813
0
      if (htab->tlsdesc_plt
15814
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15815
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15816
0
  return false;
15817
15818
0
      if (need_dynamic_reloc)
15819
0
  {
15820
0
    if (bed->rela_plts_and_copies_p)
15821
0
      {
15822
0
        if (!add_dynamic_entry (DT_RELA, 0)
15823
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15824
0
      || !add_dynamic_entry (DT_RELAENT,
15825
0
           bed->s->sizeof_rela))
15826
0
    return false;
15827
0
      }
15828
0
    else
15829
0
      {
15830
0
        if (!add_dynamic_entry (DT_REL, 0)
15831
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15832
0
      || !add_dynamic_entry (DT_RELENT,
15833
0
           bed->s->sizeof_rel))
15834
0
    return false;
15835
0
      }
15836
15837
    /* If any dynamic relocs apply to a read-only section,
15838
       then we need a DT_TEXTREL entry.  */
15839
0
    if ((info->flags & DF_TEXTREL) == 0)
15840
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15841
0
            info);
15842
15843
0
    if ((info->flags & DF_TEXTREL) != 0)
15844
0
      {
15845
0
        if (htab->ifunc_resolvers)
15846
0
    info->callbacks->einfo
15847
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15848
0
         "may result in a segfault at runtime; recompile with %s\n"),
15849
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15850
15851
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15852
0
    return false;
15853
0
      }
15854
0
  }
15855
0
    }
15856
0
#undef add_dynamic_entry
15857
15858
0
  return true;
15859
0
}