Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elflink.c
Line
Count
Source
1
/* ELF linking support for BFD.
2
   Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "bfdlink.h"
24
#include "libbfd.h"
25
#define ARCH_SIZE 0
26
#include "elf-bfd.h"
27
#include "sframe-api.h"
28
#include "safe-ctype.h"
29
#include "libiberty.h"
30
#include "objalloc.h"
31
#include "plugin.h"
32
33
#include <limits.h>
34
#ifndef CHAR_BIT
35
#define CHAR_BIT 8
36
#endif
37
38
/* This struct is used to pass information to routines called via
39
   elf_link_hash_traverse which must return failure.  */
40
41
struct elf_info_failed
42
{
43
  struct bfd_link_info *info;
44
  bool failed;
45
};
46
47
static bool _bfd_elf_fix_symbol_flags
48
  (struct elf_link_hash_entry *, struct elf_info_failed *);
49
50
/* Return false if linker should avoid caching relocation information
51
   and symbol tables of input files in memory.  */
52
53
static bool
54
_bfd_elf_link_keep_memory (struct bfd_link_info *info)
55
0
{
56
0
#ifdef USE_MMAP
57
  /* Don't cache symbol nor relocation tables if they are mapped in.
58
     NB: Since the --no-keep-memory linker option causes:
59
60
     https://sourceware.org/bugzilla/show_bug.cgi?id=31458
61
62
     this is opt-in by each backend.  */
63
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
64
0
  if (obed->use_mmap)
65
0
    return false;
66
0
#endif
67
0
  bfd *abfd;
68
0
  bfd_size_type size;
69
70
0
  if (!info->keep_memory)
71
0
    return false;
72
73
0
  if (info->max_cache_size == (bfd_size_type) -1)
74
0
    return true;
75
76
0
  abfd = info->input_bfds;
77
0
  size = info->cache_size;
78
0
  do
79
0
    {
80
0
      if (size >= info->max_cache_size)
81
0
  {
82
    /* Over the limit.  Reduce the memory usage.  */
83
0
    info->keep_memory = false;
84
0
    return false;
85
0
  }
86
0
      if (!abfd)
87
0
  break;
88
0
      size += abfd->alloc_size;
89
0
      abfd = abfd->link.next;
90
0
    }
91
0
  while (1);
92
93
0
  return true;
94
0
}
95
96
struct elf_link_hash_entry *
97
_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **sym_hashes,
98
            unsigned int symndx,
99
            unsigned int ext_sym_start,
100
            unsigned int num_sym)
101
0
{
102
0
  if (sym_hashes == NULL
103
      /* Guard against corrupt input.  See PR 32636 for an example.  */
104
0
      || symndx < ext_sym_start
105
0
      || symndx >= num_sym)
106
0
    return NULL;
107
108
0
  struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
109
110
  /* The hash might be empty when bad_symtab.  Also see PR32641.  */
111
0
  if (h == NULL)
112
0
    return NULL;
113
114
0
  while (h->root.type == bfd_link_hash_indirect
115
0
   || h->root.type == bfd_link_hash_warning)
116
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
117
118
0
  return h;
119
0
}
120
121
static struct elf_link_hash_entry *
122
get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie,
123
            unsigned int symndx)
124
0
{
125
0
  if (cookie == NULL)
126
0
    return NULL;
127
128
0
  return _bfd_elf_get_link_hash_entry (elf_sym_hashes (cookie->abfd), symndx,
129
0
               cookie->extsymoff, cookie->num_sym);
130
0
}
131
132
asection *
133
_bfd_get_local_sym_section (struct elf_reloc_cookie *cookie,
134
          unsigned int symndx)
135
0
{
136
0
  if (symndx >= cookie->locsymcount)
137
0
    return NULL;
138
139
0
  bfd *abfd = cookie->abfd;
140
0
  if (elf_loc_shndx (abfd) == NULL)
141
0
    {
142
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
143
0
      Elf_Internal_Sym *locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
144
0
              cookie->locsymcount, 0,
145
0
              NULL, NULL, NULL);
146
0
      if (locsyms == NULL)
147
0
  return NULL;
148
0
      unsigned int *loc_shndx
149
0
  = bfd_alloc (abfd, cookie->locsymcount * sizeof (*loc_shndx));
150
0
      if (loc_shndx == NULL)
151
0
  return NULL;
152
0
      elf_loc_shndx (abfd) = loc_shndx;
153
0
      for (unsigned int i = 0; i < cookie->locsymcount; i++)
154
0
  {
155
0
    loc_shndx[i] = locsyms[i].st_shndx;
156
0
    if (ELF_ST_BIND (locsyms[i].st_info) != STB_LOCAL)
157
0
      loc_shndx[i] = SHN_BAD;
158
0
  }
159
0
      free (locsyms);
160
0
    }
161
162
0
  return bfd_section_from_elf_index (abfd, elf_loc_shndx (abfd)[symndx]);
163
0
}
164
165
asection *
166
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
167
           unsigned long r_symndx)
168
0
{
169
0
  struct elf_link_hash_entry *h;
170
171
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
172
173
0
  if (h != NULL)
174
0
    {
175
0
      if (h->root.type == bfd_link_hash_defined
176
0
    || h->root.type == bfd_link_hash_defweak)
177
0
  return h->root.u.def.section;
178
0
      else
179
0
  return NULL;
180
0
    }
181
182
0
  return _bfd_get_local_sym_section (cookie, r_symndx);
183
0
}
184
185
/* Define a symbol in a dynamic linkage section.  */
186
187
struct elf_link_hash_entry *
188
_bfd_elf_define_linkage_sym (bfd *dynobj,
189
           struct bfd_link_info *info,
190
           asection *sec,
191
           const char *name)
192
0
{
193
0
  struct elf_link_hash_entry *h;
194
0
  struct bfd_link_hash_entry *bh;
195
0
  elf_backend_data *obed;
196
197
0
  h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
198
0
  if (h != NULL)
199
0
    {
200
      /* Zap symbol defined in an as-needed lib that wasn't linked.
201
   This is a symptom of a larger problem:  Absolute symbols
202
   defined in shared libraries can't be overridden, because we
203
   lose the link to the bfd which is via the symbol section.  */
204
0
      h->root.type = bfd_link_hash_new;
205
0
      bh = &h->root;
206
0
    }
207
0
  else
208
0
    bh = NULL;
209
210
0
  obed = get_elf_backend_data (dynobj);
211
0
  if (!_bfd_generic_link_add_one_symbol (info, dynobj, name, BSF_GLOBAL,
212
0
           sec, 0, NULL, false, obed->collect,
213
0
           &bh))
214
0
    return NULL;
215
0
  h = (struct elf_link_hash_entry *) bh;
216
0
  BFD_ASSERT (h != NULL);
217
0
  h->def_regular = 1;
218
0
  h->non_elf = 0;
219
0
  h->root.linker_def = 1;
220
0
  h->type = STT_OBJECT;
221
0
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
222
0
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
223
224
0
  obed->elf_backend_hide_symbol (info, h, true);
225
0
  return h;
226
0
}
227
228
bool
229
_bfd_elf_create_got_section (bfd *dynobj, struct bfd_link_info *info)
230
0
{
231
0
  flagword flags;
232
0
  asection *s;
233
0
  struct elf_link_hash_entry *h;
234
0
  elf_backend_data *obed = get_elf_backend_data (dynobj);
235
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
236
237
  /* This function may be called more than once.  */
238
0
  if (htab->sgot != NULL)
239
0
    return true;
240
241
0
  flags = obed->dynamic_sec_flags;
242
243
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".got", flags);
244
0
  if (s == NULL
245
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
246
0
    return false;
247
0
  htab->sgot = s;
248
249
0
  if (obed->want_got_plt)
250
0
    {
251
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".got.plt", flags);
252
0
      if (s == NULL
253
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
254
0
  return false;
255
0
      htab->sgotplt = s;
256
0
    }
257
258
  /* The first bit of the global offset table is the header.  */
259
0
  s->size += obed->got_header_size;
260
261
0
  if (obed->want_got_sym)
262
0
    {
263
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
264
   (or .got.plt) section.  We don't do this in the linker script
265
   because we don't want to define the symbol if we are not creating
266
   a global offset table.  */
267
0
      h = _bfd_elf_define_linkage_sym (dynobj, info, s,
268
0
               "_GLOBAL_OFFSET_TABLE_");
269
0
      elf_hash_table (info)->hgot = h;
270
0
      if (h == NULL)
271
0
  return false;
272
0
    }
273
274
0
  s = bfd_make_section_anyway_with_flags (dynobj,
275
0
            (obed->rela_plts_and_copies_p
276
0
             ? ".rela.got" : ".rel.got"),
277
0
            flags | SEC_READONLY);
278
0
  if (s == NULL
279
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
280
0
    return false;
281
0
  htab->srelgot = s;
282
283
0
  return true;
284
0
}
285

286
/* Create a strtab to hold the dynamic symbol names.  */
287
static bool
288
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
289
0
{
290
0
  struct elf_link_hash_table *hash_table;
291
292
0
  hash_table = elf_hash_table (info);
293
0
  if (hash_table->dynobj == NULL)
294
0
    {
295
      /* We may not set dynobj, an input file holding linker created
296
   dynamic sections to abfd, which may be a dynamic object with
297
   its own dynamic sections.  We need to find a normal input file
298
   to hold linker created sections if possible.  */
299
0
      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
300
0
  {
301
0
    bfd *ibfd;
302
0
    asection *s;
303
0
    for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
304
0
      if ((ibfd->flags
305
0
     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
306
0
    && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
307
0
    && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
308
0
    && !((s = ibfd->sections) != NULL
309
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
310
0
        {
311
0
    abfd = ibfd;
312
0
    break;
313
0
        }
314
0
  }
315
0
      hash_table->dynobj = abfd;
316
0
    }
317
318
0
  if (hash_table->dynstr == NULL)
319
0
    {
320
0
      hash_table->dynstr = _bfd_elf_strtab_init ();
321
0
      if (hash_table->dynstr == NULL)
322
0
  return false;
323
0
    }
324
0
  return true;
325
0
}
326
327
/* Create some sections which will be filled in with dynamic linking
328
   information.  ABFD is an input file which requires dynamic sections
329
   to be created.  The dynamic sections take up virtual memory space
330
   when the final executable is run, so we need to create them before
331
   addresses are assigned to the output sections.  We work out the
332
   actual contents and size of these sections later.  */
333
334
bool
335
bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
336
0
{
337
0
  flagword flags;
338
0
  asection *s;
339
0
  bfd *dynobj;
340
0
  elf_backend_data *obed;
341
0
  struct elf_link_hash_entry *h;
342
343
0
  if (! is_elf_hash_table (info->hash))
344
0
    return false;
345
346
0
  if (elf_hash_table (info)->dynamic_sections_created)
347
0
    return true;
348
349
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
350
0
    return false;
351
352
0
  dynobj = elf_hash_table (info)->dynobj;
353
0
  obed = get_elf_backend_data (dynobj);
354
355
0
  flags = obed->dynamic_sec_flags;
356
357
  /* A dynamically linked executable has a .interp section, but a
358
     shared library does not.  */
359
0
  if (bfd_link_executable (info) && !info->nointerp)
360
0
    {
361
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".interp",
362
0
                flags | SEC_READONLY);
363
0
      if (s == NULL)
364
0
  return false;
365
0
      elf_hash_table (info)->interp = s;
366
0
    }
367
368
  /* Create sections to hold version informations.  These are removed
369
     if they are not needed.  */
370
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_d",
371
0
            flags | SEC_READONLY);
372
0
  if (s == NULL
373
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
374
0
    return false;
375
376
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version",
377
0
            flags | SEC_READONLY);
378
0
  if (s == NULL
379
0
      || !bfd_set_section_alignment (s, 1))
380
0
    return false;
381
382
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_r",
383
0
            flags | SEC_READONLY);
384
0
  if (s == NULL
385
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
386
0
    return false;
387
388
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynsym",
389
0
            flags | SEC_READONLY);
390
0
  if (s == NULL
391
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
392
0
    return false;
393
0
  elf_hash_table (info)->dynsym = s;
394
395
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynstr",
396
0
            flags | SEC_READONLY);
397
0
  if (s == NULL)
398
0
    return false;
399
400
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynamic", flags);
401
0
  if (s == NULL
402
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
403
0
    return false;
404
0
  elf_hash_table (info)->dynamic = s;
405
406
  /* The special symbol _DYNAMIC is always set to the start of the
407
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
408
     only want to define it if we are, in fact, creating a .dynamic
409
     section.  We don't want to define it if there is no .dynamic
410
     section, since on some ELF platforms the start up code examines it
411
     to decide how to initialize the process.  */
412
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, s, "_DYNAMIC");
413
0
  elf_hash_table (info)->hdynamic = h;
414
0
  if (h == NULL)
415
0
    return false;
416
417
0
  if (info->emit_hash)
418
0
    {
419
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".hash",
420
0
                flags | SEC_READONLY);
421
0
      if (s == NULL
422
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
423
0
  return false;
424
0
      elf_section_data (s)->this_hdr.sh_entsize = obed->s->sizeof_hash_entry;
425
0
    }
426
427
0
  if (info->emit_gnu_hash && obed->record_xhash_symbol == NULL)
428
0
    {
429
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.hash",
430
0
                flags | SEC_READONLY);
431
0
      if (s == NULL
432
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
433
0
  return false;
434
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
435
   4 32-bit words followed by variable count of 64-bit words, then
436
   variable count of 32-bit words.  */
437
0
      if (obed->s->arch_size == 64)
438
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
439
0
      else
440
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
441
0
    }
442
443
0
  if (info->enable_dt_relr)
444
0
    {
445
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".relr.dyn",
446
0
                flags | SEC_READONLY);
447
0
      if (s == NULL
448
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
449
0
  return false;
450
0
      elf_hash_table (info)->srelrdyn = s;
451
0
    }
452
453
  /* Let the backend create the rest of the sections.  This lets the
454
     backend set the right flags.  The backend will normally create
455
     the .got and .plt sections.  */
456
0
  if (obed->elf_backend_create_dynamic_sections == NULL
457
0
      || !obed->elf_backend_create_dynamic_sections (dynobj, info))
458
0
    return false;
459
460
0
  elf_hash_table (info)->dynamic_sections_created = true;
461
462
0
  return true;
463
0
}
464
465
/* Create dynamic sections when linking against a dynamic object.  */
466
467
bool
468
_bfd_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
469
0
{
470
0
  flagword flags, pltflags;
471
0
  struct elf_link_hash_entry *h;
472
0
  asection *s;
473
0
  elf_backend_data *obed = get_elf_backend_data (dynobj);
474
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
475
476
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
477
     .rel[a].bss sections.  */
478
0
  flags = obed->dynamic_sec_flags;
479
480
0
  pltflags = flags;
481
0
  if (obed->plt_not_loaded)
482
    /* We do not clear SEC_ALLOC here because we still want the OS to
483
       allocate space for the section; it's just that there's nothing
484
       to read in from the object file.  */
485
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
486
0
  else
487
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
488
0
  if (obed->plt_readonly)
489
0
    pltflags |= SEC_READONLY;
490
491
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".plt", pltflags);
492
0
  if (s == NULL
493
0
      || !bfd_set_section_alignment (s, obed->plt_alignment))
494
0
    return false;
495
0
  htab->splt = s;
496
497
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
498
     .plt section.  */
499
0
  if (obed->want_plt_sym)
500
0
    {
501
0
      h = _bfd_elf_define_linkage_sym (dynobj, info, s,
502
0
               "_PROCEDURE_LINKAGE_TABLE_");
503
0
      elf_hash_table (info)->hplt = h;
504
0
      if (h == NULL)
505
0
  return false;
506
0
    }
507
508
0
  s = bfd_make_section_anyway_with_flags (dynobj,
509
0
            (obed->rela_plts_and_copies_p
510
0
             ? ".rela.plt" : ".rel.plt"),
511
0
            flags | SEC_READONLY);
512
0
  if (s == NULL
513
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
514
0
    return false;
515
0
  htab->srelplt = s;
516
517
0
  if (! _bfd_elf_create_got_section (dynobj, info))
518
0
    return false;
519
520
0
  if (obed->want_dynbss)
521
0
    {
522
      /* The .dynbss section is a place to put symbols which are defined
523
   by dynamic objects, are referenced by regular objects, and are
524
   not functions.  We must allocate space for them in the process
525
   image and use a R_*_COPY reloc to tell the dynamic linker to
526
   initialize them at run time.  The linker script puts the .dynbss
527
   section into the .bss section of the final image.  */
528
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".dynbss",
529
0
                SEC_ALLOC | SEC_LINKER_CREATED);
530
0
      if (s == NULL)
531
0
  return false;
532
0
      htab->sdynbss = s;
533
534
0
      if (obed->want_dynrelro)
535
0
  {
536
    /* Similarly, but for symbols that were originally in read-only
537
       sections.  This section doesn't really need to have contents,
538
       but make it like other .data.rel.ro sections.  */
539
0
    s = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro",
540
0
              flags);
541
0
    if (s == NULL)
542
0
      return false;
543
0
    htab->sdynrelro = s;
544
0
  }
545
546
      /* The .rel[a].bss section holds copy relocs.  This section is not
547
   normally needed.  We need to create it here, though, so that the
548
   linker will map it to an output section.  We can't just create it
549
   only if we need it, because we will not know whether we need it
550
   until we have seen all the input files, and the first time the
551
   main linker code calls BFD after examining all the input files
552
   (size_dynamic_sections) the input sections have already been
553
   mapped to the output sections.  If the section turns out not to
554
   be needed, we can discard it later.  We will never need this
555
   section when generating a shared object, since they do not use
556
   copy relocs.  */
557
0
      if (bfd_link_executable (info))
558
0
  {
559
0
    s = bfd_make_section_anyway_with_flags (dynobj,
560
0
              (obed->rela_plts_and_copies_p
561
0
               ? ".rela.bss" : ".rel.bss"),
562
0
              flags | SEC_READONLY);
563
0
    if (s == NULL
564
0
        || !bfd_set_section_alignment (s, obed->s->log_file_align))
565
0
      return false;
566
0
    htab->srelbss = s;
567
568
0
    if (obed->want_dynrelro)
569
0
      {
570
0
        s = (bfd_make_section_anyway_with_flags
571
0
       (dynobj, (obed->rela_plts_and_copies_p
572
0
           ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
573
0
        flags | SEC_READONLY));
574
0
        if (s == NULL
575
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
576
0
    return false;
577
0
        htab->sreldynrelro = s;
578
0
      }
579
0
  }
580
0
    }
581
582
0
  return true;
583
0
}
584

585
/* Record a new dynamic symbol.  We record the dynamic symbols as we
586
   read the input files, since we need to have a list of all of them
587
   before we can determine the final sizes of the output sections.
588
   Note that we may actually call this function even though we are not
589
   going to output any dynamic symbols; in some cases we know that a
590
   symbol should be in the dynamic symbol table, but only if there is
591
   one.  */
592
593
bool
594
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
595
            struct elf_link_hash_entry *h)
596
0
{
597
0
  if (h->dynindx == -1)
598
0
    {
599
0
      struct elf_strtab_hash *dynstr;
600
0
      const char *p;
601
0
      const char *name;
602
0
      size_t indx;
603
604
0
      if (h->root.type == bfd_link_hash_defined
605
0
    || h->root.type == bfd_link_hash_defweak)
606
0
  {
607
    /* An IR symbol should not be made dynamic.  */
608
0
    if (h->root.u.def.section != NULL
609
0
        && h->root.u.def.section->owner != NULL
610
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
611
0
      return true;
612
0
  }
613
614
      /* XXX: The ABI draft says the linker must turn hidden and
615
   internal symbols into STB_LOCAL symbols when producing the
616
   DSO. However, if ld.so honors st_other in the dynamic table,
617
   this would not be necessary.  */
618
0
      switch (ELF_ST_VISIBILITY (h->other))
619
0
  {
620
0
  case STV_INTERNAL:
621
0
  case STV_HIDDEN:
622
0
    if (h->root.type != bfd_link_hash_undefined
623
0
        && h->root.type != bfd_link_hash_undefweak)
624
0
      {
625
0
        h->forced_local = 1;
626
0
        return true;
627
0
      }
628
629
0
  default:
630
0
    break;
631
0
  }
632
633
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
634
0
      if (h->forced_local)
635
0
  elf_hash_table (info)->has_local_dynsyms = true;
636
0
      ++elf_hash_table (info)->dynsymcount;
637
638
0
      dynstr = elf_hash_table (info)->dynstr;
639
0
      if (dynstr == NULL)
640
0
  {
641
    /* Create a strtab to hold the dynamic symbol names.  */
642
0
    elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
643
0
    if (dynstr == NULL)
644
0
      return false;
645
0
  }
646
647
0
      char *unversioned_name = NULL;
648
649
      /* We don't put any version information in the dynamic string
650
   table.  */
651
0
      name = h->root.root.string;
652
0
      p = strchr (name, ELF_VER_CHR);
653
0
      if (p != NULL)
654
0
  {
655
0
    unversioned_name = bfd_malloc (p - name + 1);
656
0
    if (unversioned_name == NULL)
657
0
      return false;
658
0
    memcpy (unversioned_name, name, p - name);
659
0
    unversioned_name[p - name] = 0;
660
0
    name = unversioned_name;
661
0
  }
662
663
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
664
665
0
      if (p != NULL)
666
0
  free (unversioned_name);
667
668
0
      if (indx == (size_t) -1)
669
0
  return false;
670
0
      h->dynstr_index = indx;
671
0
    }
672
673
0
  return true;
674
0
}
675

676
/* Mark a symbol dynamic.  */
677
678
static void
679
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
680
          struct elf_link_hash_entry *h,
681
          Elf_Internal_Sym *sym)
682
0
{
683
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
684
685
  /* It may be called more than once on the same H.  */
686
0
  if(h->dynamic || bfd_link_relocatable (info))
687
0
    return;
688
689
0
  if ((info->dynamic_data
690
0
       && (h->type == STT_OBJECT
691
0
     || h->type == STT_COMMON
692
0
     || (sym != NULL
693
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
694
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
695
0
      || (d != NULL
696
0
    && h->non_elf
697
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
698
0
    {
699
0
      h->dynamic = 1;
700
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
701
   non-IR reference.  */
702
0
      h->root.non_ir_ref_dynamic = 1;
703
0
    }
704
0
}
705
706
/* Record an assignment to a symbol made by a linker script.  We need
707
   this in case some dynamic object refers to this symbol.  */
708
709
bool
710
bfd_elf_record_link_assignment (struct bfd_link_info *info,
711
        const char *name,
712
        bool provide,
713
        bool hidden)
714
0
{
715
0
  struct elf_link_hash_entry *h, *hv;
716
0
  struct elf_link_hash_table *htab;
717
0
  elf_backend_data *obed;
718
719
0
  if (!is_elf_hash_table (info->hash))
720
0
    return true;
721
722
0
  htab = elf_hash_table (info);
723
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
724
0
  if (h == NULL)
725
0
    return provide;
726
727
0
  if (h->root.type == bfd_link_hash_warning)
728
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
729
730
0
  if (h->versioned == unknown)
731
0
    {
732
      /* Set versioned if symbol version is unknown.  */
733
0
      const char *version = strrchr (name, ELF_VER_CHR);
734
0
      if (version)
735
0
  {
736
0
    if (version > name && version[-1] != ELF_VER_CHR)
737
0
      h->versioned = versioned_hidden;
738
0
    else
739
0
      h->versioned = versioned;
740
0
  }
741
0
    }
742
743
  /* Symbols defined in a linker script but not referenced anywhere
744
     else will have non_elf set.  */
745
0
  if (h->non_elf)
746
0
    {
747
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
748
0
      h->non_elf = 0;
749
0
    }
750
751
0
  switch (h->root.type)
752
0
    {
753
0
    case bfd_link_hash_defined:
754
0
    case bfd_link_hash_defweak:
755
0
    case bfd_link_hash_common:
756
0
      break;
757
0
    case bfd_link_hash_undefweak:
758
0
    case bfd_link_hash_undefined:
759
      /* Since we're defining the symbol, don't let it seem to have not
760
   been defined.  record_dynamic_symbol and size_dynamic_sections
761
   may depend on this.  */
762
0
      h->root.type = bfd_link_hash_new;
763
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
764
0
  bfd_link_repair_undef_list (&htab->root);
765
0
      break;
766
0
    case bfd_link_hash_new:
767
0
      break;
768
0
    case bfd_link_hash_indirect:
769
      /* We had a versioned symbol in a dynamic library.  We make the
770
   the versioned symbol point to this one.  */
771
0
      obed = get_elf_backend_data (info->output_bfd);
772
0
      hv = h;
773
0
      while (hv->root.type == bfd_link_hash_indirect
774
0
       || hv->root.type == bfd_link_hash_warning)
775
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
776
      /* We don't need to update h->root.u since linker will set them
777
   later.  */
778
0
      h->root.type = bfd_link_hash_undefined;
779
0
      hv->root.type = bfd_link_hash_indirect;
780
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
781
0
      obed->elf_backend_copy_indirect_symbol (info, h, hv);
782
0
      break;
783
0
    default:
784
0
      BFD_FAIL ();
785
0
      return false;
786
0
    }
787
788
  /* If this symbol is being provided by the linker script, and it is
789
     currently defined by a dynamic object, but not by a regular
790
     object, then mark it as undefined so that the generic linker will
791
     force the correct value.  */
792
0
  if (provide
793
0
      && h->def_dynamic
794
0
      && !h->def_regular)
795
0
    h->root.type = bfd_link_hash_undefined;
796
797
  /* If this symbol is currently defined by a dynamic object, but not
798
     by a regular object, then clear out any version information because
799
     the symbol will not be associated with the dynamic object any
800
     more.  */
801
0
  if (h->def_dynamic && !h->def_regular)
802
0
    h->verinfo.verdef = NULL;
803
804
  /* Make sure this symbol is not garbage collected.  */
805
0
  h->mark = 1;
806
807
0
  h->def_regular = 1;
808
809
0
  if (hidden)
810
0
    {
811
0
      obed = get_elf_backend_data (info->output_bfd);
812
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
813
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
814
0
      obed->elf_backend_hide_symbol (info, h, true);
815
0
    }
816
817
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
818
     and executables.  */
819
0
  if (!bfd_link_relocatable (info)
820
0
      && h->dynindx != -1
821
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
822
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
823
0
    h->forced_local = 1;
824
825
0
  if ((h->def_dynamic
826
0
       || h->ref_dynamic
827
0
       || bfd_link_dll (info))
828
0
      && !h->forced_local
829
0
      && h->dynindx == -1)
830
0
    {
831
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
832
0
  return false;
833
834
      /* If this is a weak defined symbol, and we know a corresponding
835
   real symbol from the same dynamic object, make sure the real
836
   symbol is also made into a dynamic symbol.  */
837
0
      if (h->is_weakalias)
838
0
  {
839
0
    struct elf_link_hash_entry *def = weakdef (h);
840
841
0
    if (def->dynindx == -1
842
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
843
0
      return false;
844
0
  }
845
0
    }
846
847
0
  return true;
848
0
}
849
850
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
851
   success, and 2 on a failure caused by attempting to record a symbol
852
   in a discarded section, eg. a discarded link-once section symbol.  */
853
854
int
855
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
856
            bfd *input_bfd,
857
            long input_indx)
858
0
{
859
0
  struct elf_link_local_dynamic_entry *entry;
860
0
  struct elf_link_hash_table *eht;
861
0
  struct elf_strtab_hash *dynstr;
862
0
  size_t dynstr_index;
863
0
  char *name;
864
0
  Elf_External_Sym_Shndx eshndx;
865
0
  char esym[sizeof (Elf64_External_Sym)];
866
867
0
  if (! is_elf_hash_table (info->hash))
868
0
    return 0;
869
870
  /* See if the entry exists already.  */
871
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
872
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
873
0
      return 1;
874
875
0
  entry = bfd_alloc (input_bfd, sizeof (*entry));
876
0
  if (entry == NULL)
877
0
    return 0;
878
879
  /* Go find the symbol, so that we can find it's name.  */
880
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_symtab_hdr (input_bfd),
881
0
           1, input_indx, &entry->isym, esym, &eshndx))
882
0
    {
883
0
      bfd_release (input_bfd, entry);
884
0
      return 0;
885
0
    }
886
887
0
  if (entry->isym.st_shndx != SHN_UNDEF
888
0
      && entry->isym.st_shndx < SHN_LORESERVE)
889
0
    {
890
0
      asection *s;
891
892
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
893
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
894
0
  {
895
    /* We can still bfd_release here as nothing has done another
896
       bfd_alloc.  We can't do this later in this function.  */
897
0
    bfd_release (input_bfd, entry);
898
0
    return 2;
899
0
  }
900
0
    }
901
902
0
  name = (bfd_elf_string_from_elf_section
903
0
    (input_bfd, elf_symtab_hdr (input_bfd).sh_link,
904
0
     entry->isym.st_name));
905
906
0
  dynstr = elf_hash_table (info)->dynstr;
907
0
  if (dynstr == NULL)
908
0
    {
909
      /* Create a strtab to hold the dynamic symbol names.  */
910
0
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
911
0
      if (dynstr == NULL)
912
0
  return 0;
913
0
    }
914
915
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
916
0
  if (dynstr_index == (size_t) -1)
917
0
    return 0;
918
0
  entry->isym.st_name = dynstr_index;
919
920
0
  eht = elf_hash_table (info);
921
922
0
  entry->next = eht->dynlocal;
923
0
  eht->dynlocal = entry;
924
0
  entry->input_bfd = input_bfd;
925
0
  entry->input_indx = input_indx;
926
0
  eht->dynsymcount++;
927
928
  /* Whatever binding the symbol had before, it's now local.  */
929
0
  entry->isym.st_info
930
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
931
932
  /* The dynindx will be set at the end of size_dynamic_sections.  */
933
934
0
  return 1;
935
0
}
936
937
/* Return the dynindex of a local dynamic symbol.  */
938
939
long
940
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
941
            bfd *input_bfd,
942
            long input_indx)
943
0
{
944
0
  struct elf_link_local_dynamic_entry *e;
945
946
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
947
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
948
0
      return e->dynindx;
949
0
  return -1;
950
0
}
951
952
/* This function is used to renumber the dynamic symbols, if some of
953
   them are removed because they are marked as local.  This is called
954
   via elf_link_hash_traverse.  */
955
956
static bool
957
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
958
              void *data)
959
0
{
960
0
  size_t *count = data;
961
962
0
  if (h->forced_local)
963
0
    return true;
964
965
0
  if (h->dynindx != -1)
966
0
    h->dynindx = ++(*count);
967
968
0
  return true;
969
0
}
970
971
972
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
973
   STB_LOCAL binding.  */
974
975
static bool
976
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
977
              void *data)
978
0
{
979
0
  size_t *count = data;
980
981
0
  if (!h->forced_local)
982
0
    return true;
983
984
0
  if (h->dynindx != -1)
985
0
    h->dynindx = ++(*count);
986
987
0
  return true;
988
0
}
989
990
/* Return true if the dynamic symbol for a given section should be
991
   omitted when creating a shared library.  */
992
bool
993
_bfd_elf_omit_section_dynsym_default (struct bfd_link_info *info,
994
              asection *p)
995
0
{
996
0
  struct elf_link_hash_table *htab;
997
0
  asection *ip;
998
999
0
  switch (elf_section_data (p)->this_hdr.sh_type)
1000
0
    {
1001
0
    case SHT_PROGBITS:
1002
0
    case SHT_NOBITS:
1003
      /* If sh_type is yet undecided, assume it could be
1004
   SHT_PROGBITS/SHT_NOBITS.  */
1005
0
    case SHT_NULL:
1006
0
      htab = elf_hash_table (info);
1007
0
      if (htab->text_index_section != NULL)
1008
0
  return p != htab->text_index_section && p != htab->data_index_section;
1009
1010
0
      return (htab->dynobj != NULL
1011
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1012
0
        && ip->output_section == p);
1013
1014
      /* There shouldn't be section relative relocations
1015
   against any other section.  */
1016
0
    default:
1017
0
      return true;
1018
0
    }
1019
0
}
1020
1021
bool
1022
_bfd_elf_omit_section_dynsym_all (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1023
          asection *p ATTRIBUTE_UNUSED)
1024
0
{
1025
0
  return true;
1026
0
}
1027
1028
/* Assign dynsym indices.  In a shared library we generate a section
1029
   symbol for each output section, which come first.  Next come symbols
1030
   which have been forced to local binding.  Then all of the back-end
1031
   allocated local dynamic syms, followed by the rest of the global
1032
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1033
   (This prevents the early call before elf_backend_init_index_section
1034
   and strip_excluded_output_sections setting dynindx for sections
1035
   that are stripped.)  */
1036
1037
static unsigned long
1038
_bfd_elf_link_renumber_dynsyms (struct bfd_link_info *info,
1039
        unsigned long *section_sym_count)
1040
0
{
1041
0
  unsigned long dynsymcount = 0;
1042
0
  bool do_sec = section_sym_count != NULL;
1043
1044
0
  if (bfd_link_pic (info)
1045
0
      || elf_hash_table (info)->is_relocatable_executable)
1046
0
    {
1047
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
1048
0
      asection *p;
1049
0
      for (p = info->output_bfd->sections; p ; p = p->next)
1050
0
  if ((p->flags & SEC_EXCLUDE) == 0
1051
0
      && (p->flags & SEC_ALLOC) != 0
1052
0
      && elf_hash_table (info)->dynamic_relocs
1053
0
      && !obed->elf_backend_omit_section_dynsym (info, p))
1054
0
    {
1055
0
      ++dynsymcount;
1056
0
      if (do_sec)
1057
0
        elf_section_data (p)->dynindx = dynsymcount;
1058
0
    }
1059
0
  else if (do_sec)
1060
0
    elf_section_data (p)->dynindx = 0;
1061
0
    }
1062
0
  if (do_sec)
1063
0
    *section_sym_count = dynsymcount;
1064
1065
0
  if (elf_hash_table (info)->has_local_dynsyms)
1066
0
    elf_link_hash_traverse (elf_hash_table (info),
1067
0
          elf_link_renumber_local_hash_table_dynsyms,
1068
0
          &dynsymcount);
1069
1070
0
  if (elf_hash_table (info)->dynlocal)
1071
0
    {
1072
0
      struct elf_link_local_dynamic_entry *p;
1073
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1074
0
  p->dynindx = ++dynsymcount;
1075
0
    }
1076
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1077
1078
0
  elf_link_hash_traverse (elf_hash_table (info),
1079
0
        elf_link_renumber_hash_table_dynsyms,
1080
0
        &dynsymcount);
1081
1082
  /* There is an unused NULL entry at the head of the table which we
1083
     must account for in our count even if the table is empty since it
1084
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1085
     .dynamic section.  */
1086
0
  dynsymcount++;
1087
1088
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1089
0
  return dynsymcount;
1090
0
}
1091
1092
/* Merge st_other field.  Both the input bfd and output bfd are given
1093
   with IBFD reflecting where the ST_OTHER bits came from.  If they
1094
   were from a symbol already in the linker hash table then the proper
1095
   IBFD is the output bfd since symbols in the linker hash table
1096
   interpret their st_other bits according to OBFD.  */
1097
1098
static void
1099
elf_merge_st_other (bfd *obfd, bfd *ibfd,
1100
        struct elf_link_hash_entry *h,
1101
        unsigned int st_other, asection *sec,
1102
        bool definition, bool dynamic)
1103
0
{
1104
0
  if (ibfd->xvec == obfd->xvec)
1105
0
    {
1106
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
1107
1108
      /* If st_other has a processor-specific meaning, specific
1109
   code might be needed here.  */
1110
0
      if (obed->elf_backend_merge_symbol_attribute)
1111
0
  obed->elf_backend_merge_symbol_attribute (h, st_other,
1112
0
              definition, dynamic);
1113
0
    }
1114
1115
0
  if (!dynamic)
1116
0
    {
1117
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1118
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1119
1120
      /* Keep the most constraining visibility.  Leave the remainder
1121
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1122
0
      if (symvis - 1 < hvis - 1)
1123
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1124
0
    }
1125
0
  else if (definition
1126
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1127
0
     && (sec->flags & SEC_READONLY) == 0)
1128
0
    h->protected_def = 1;
1129
0
}
1130
1131
/* This function is called when we want to merge a new symbol with an
1132
   existing symbol.  It handles the various cases which arise when we
1133
   find a definition in a dynamic object, or when there is already a
1134
   definition in a dynamic object.  The new symbol is described by
1135
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1136
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1137
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1138
   of an old common symbol.  We set OVERRIDE if the old symbol is
1139
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1140
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1141
   to change.  By OK to change, we mean that we shouldn't warn if the
1142
   type or size does change.  */
1143
1144
static bool
1145
_bfd_elf_merge_symbol (bfd *abfd,
1146
           struct bfd_link_info *info,
1147
           const char *name,
1148
           Elf_Internal_Sym *sym,
1149
           asection **psec,
1150
           bfd_vma *pvalue,
1151
           struct elf_link_hash_entry **sym_hash,
1152
           bfd **poldbfd,
1153
           bool *pold_weak,
1154
           unsigned int *pold_alignment,
1155
           bool *skip,
1156
           bfd **override,
1157
           bool *type_change_ok,
1158
           bool *size_change_ok,
1159
           bool *matched)
1160
0
{
1161
0
  asection *sec, *oldsec;
1162
0
  struct elf_link_hash_entry *h;
1163
0
  struct elf_link_hash_entry *hi;
1164
0
  struct elf_link_hash_entry *flip;
1165
0
  int bind;
1166
0
  bfd *oldbfd;
1167
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1168
0
  bool newweak, oldweak, newfunc, oldfunc;
1169
0
  elf_backend_data *bed, *obed;
1170
0
  const char *new_version;
1171
0
  bool default_sym = *matched;
1172
0
  struct elf_link_hash_table *htab;
1173
1174
0
  *skip = false;
1175
0
  *override = NULL;
1176
1177
0
  sec = *psec;
1178
0
  bind = ELF_ST_BIND (sym->st_info);
1179
1180
0
  if (! bfd_is_und_section (sec))
1181
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1182
0
  else
1183
0
    h = ((struct elf_link_hash_entry *)
1184
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1185
0
  if (h == NULL)
1186
0
    return false;
1187
0
  *sym_hash = h;
1188
1189
0
  bed = get_elf_backend_data (abfd);
1190
0
  obed = get_elf_backend_data (info->output_bfd);
1191
1192
0
  htab = elf_hash_table (info);
1193
1194
  /* NEW_VERSION is the symbol version of the new symbol.  */
1195
0
  if (h->versioned != unversioned)
1196
0
    {
1197
      /* Symbol version is unknown or versioned.  */
1198
0
      new_version = strrchr (name, ELF_VER_CHR);
1199
0
      if (new_version)
1200
0
  {
1201
0
    if (h->versioned == unknown)
1202
0
      {
1203
        /* The base symbol has an empty version.  */
1204
0
        if (new_version[1] == '\0')
1205
0
    {
1206
0
      htab->has_base_symbols = true;
1207
0
      h->base_symbol = 1;
1208
0
    }
1209
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1210
0
    h->versioned = versioned_hidden;
1211
0
        else
1212
0
    h->versioned = versioned;
1213
0
      }
1214
0
    new_version += 1;
1215
0
    if (new_version[0] == '\0')
1216
0
      new_version = NULL;
1217
0
  }
1218
0
      else
1219
0
  h->versioned = unversioned;
1220
0
    }
1221
0
  else
1222
0
    new_version = NULL;
1223
1224
  /* For merging, we only care about real symbols.  But we need to make
1225
     sure that indirect symbol dynamic flags are updated.  */
1226
0
  hi = h;
1227
0
  while (h->root.type == bfd_link_hash_indirect
1228
0
   || h->root.type == bfd_link_hash_warning)
1229
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1230
1231
0
  if (!*matched)
1232
0
    {
1233
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1234
0
  *matched = true;
1235
0
      else
1236
0
  {
1237
    /* OLD_HIDDEN is true if the existing symbol is only visible
1238
       to the symbol with the same symbol version.  NEW_HIDDEN is
1239
       true if the new symbol is only visible to the symbol with
1240
       the same symbol version.  */
1241
0
    bool old_hidden = h->versioned == versioned_hidden;
1242
0
    bool new_hidden = hi->versioned == versioned_hidden;
1243
0
    if (!old_hidden && !new_hidden)
1244
      /* The new symbol matches the existing symbol if both
1245
         aren't hidden.  */
1246
0
      *matched = true;
1247
0
    else
1248
0
      {
1249
        /* OLD_VERSION is the symbol version of the existing
1250
     symbol. */
1251
0
        const char *old_version;
1252
1253
0
        if (h->versioned >= versioned)
1254
0
    old_version = strrchr (h->root.root.string,
1255
0
               ELF_VER_CHR) + 1;
1256
0
        else
1257
0
     old_version = NULL;
1258
1259
        /* The new symbol matches the existing symbol if they
1260
     have the same symbol version.  */
1261
0
        *matched = (old_version == new_version
1262
0
        || (old_version != NULL
1263
0
            && new_version != NULL
1264
0
            && strcmp (old_version, new_version) == 0));
1265
0
      }
1266
0
  }
1267
0
    }
1268
1269
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1270
     existing symbol.  */
1271
1272
0
  oldbfd = NULL;
1273
0
  oldsec = NULL;
1274
0
  switch (h->root.type)
1275
0
    {
1276
0
    default:
1277
0
      break;
1278
1279
0
    case bfd_link_hash_undefined:
1280
0
    case bfd_link_hash_undefweak:
1281
0
      oldbfd = h->root.u.undef.abfd;
1282
0
      break;
1283
1284
0
    case bfd_link_hash_defined:
1285
0
    case bfd_link_hash_defweak:
1286
0
      oldbfd = h->root.u.def.section->owner;
1287
0
      oldsec = h->root.u.def.section;
1288
0
      break;
1289
1290
0
    case bfd_link_hash_common:
1291
0
      oldbfd = h->root.u.c.p->section->owner;
1292
0
      oldsec = h->root.u.c.p->section;
1293
0
      if (pold_alignment)
1294
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1295
0
      break;
1296
0
    }
1297
0
  if (poldbfd && *poldbfd == NULL)
1298
0
    *poldbfd = oldbfd;
1299
1300
  /* Differentiate strong and weak symbols.  */
1301
0
  newweak = bind == STB_WEAK;
1302
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1303
0
       || h->root.type == bfd_link_hash_undefweak);
1304
0
  if (pold_weak)
1305
0
    *pold_weak = oldweak;
1306
1307
  /* We have to check it for every instance since the first few may be
1308
     references and not all compilers emit symbol type for undefined
1309
     symbols.  */
1310
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1311
1312
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1313
     respectively, is from a dynamic object.  */
1314
1315
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1316
1317
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1318
     syms and defined syms in dynamic libraries respectively.
1319
     ref_dynamic on the other hand can be set for a symbol defined in
1320
     a dynamic library, and def_dynamic may not be set;  When the
1321
     definition in a dynamic lib is overridden by a definition in the
1322
     executable use of the symbol in the dynamic lib becomes a
1323
     reference to the executable symbol.  */
1324
0
  if (newdyn)
1325
0
    {
1326
0
      if (bfd_is_und_section (sec))
1327
0
  {
1328
0
    if (bind != STB_WEAK)
1329
0
      {
1330
0
        h->ref_dynamic_nonweak = 1;
1331
0
        hi->ref_dynamic_nonweak = 1;
1332
0
      }
1333
0
  }
1334
0
      else
1335
0
  {
1336
    /* Update the existing symbol only if they match. */
1337
0
    if (*matched)
1338
0
      h->dynamic_def = 1;
1339
0
    hi->dynamic_def = 1;
1340
0
  }
1341
0
    }
1342
1343
  /* If we just created the symbol, mark it as being an ELF symbol.
1344
     Other than that, there is nothing to do--there is no merge issue
1345
     with a newly defined symbol--so we just return.  */
1346
1347
0
  if (h->root.type == bfd_link_hash_new)
1348
0
    {
1349
0
      h->non_elf = 0;
1350
0
      return true;
1351
0
    }
1352
1353
  /* In cases involving weak versioned symbols, we may wind up trying
1354
     to merge a symbol with itself.  Catch that here, to avoid the
1355
     confusion that results if we try to override a symbol with
1356
     itself.  The additional tests catch cases like
1357
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1358
     dynamic object, which we do want to handle here.  */
1359
0
  if (abfd == oldbfd
1360
0
      && (newweak || oldweak)
1361
0
      && ((abfd->flags & DYNAMIC) == 0
1362
0
    || !h->def_regular))
1363
0
    return true;
1364
1365
0
  olddyn = false;
1366
0
  if (oldbfd != NULL)
1367
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1368
0
  else if (oldsec != NULL)
1369
0
    {
1370
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1371
   indices used by MIPS ELF.  */
1372
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1373
0
    }
1374
1375
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1376
0
  if (!htab->handling_dt_needed
1377
0
      && oldbfd != NULL
1378
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1379
0
    {
1380
0
      if (newdyn != olddyn)
1381
0
  {
1382
    /* Handle a case where plugin_notice won't be called and thus
1383
       won't set the non_ir_ref flags on the first pass over
1384
       symbols.  */
1385
0
    h->root.non_ir_ref_dynamic = true;
1386
0
    hi->root.non_ir_ref_dynamic = true;
1387
0
  }
1388
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1389
0
         && hi->root.type == bfd_link_hash_indirect)
1390
0
  {
1391
    /* Change indirect symbol from IR to undefined.  */
1392
0
    hi->root.type = bfd_link_hash_undefined;
1393
0
    hi->root.u.undef.abfd = oldbfd;
1394
0
  }
1395
0
    }
1396
1397
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1398
     respectively, appear to be a definition rather than reference.  */
1399
1400
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1401
1402
0
  olddef = (h->root.type != bfd_link_hash_undefined
1403
0
      && h->root.type != bfd_link_hash_undefweak
1404
0
      && h->root.type != bfd_link_hash_common);
1405
1406
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1407
     respectively, appear to be a function.  */
1408
1409
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1410
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1411
1412
0
  oldfunc = (h->type != STT_NOTYPE
1413
0
       && bed->is_function_type (h->type));
1414
1415
0
  if (!(newfunc && oldfunc)
1416
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1417
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1418
0
      && h->type != STT_NOTYPE
1419
0
      && (newdef || bfd_is_com_section (sec))
1420
0
      && (olddef || h->root.type == bfd_link_hash_common))
1421
0
    {
1422
      /* If creating a default indirect symbol ("foo" or "foo@") from
1423
   a dynamic versioned definition ("foo@@") skip doing so if
1424
   there is an existing regular definition with a different
1425
   type.  We don't want, for example, a "time" variable in the
1426
   executable overriding a "time" function in a shared library.  */
1427
0
      if (newdyn
1428
0
    && !olddyn)
1429
0
  {
1430
0
    *skip = true;
1431
0
    return true;
1432
0
  }
1433
1434
      /* When adding a symbol from a regular object file after we have
1435
   created indirect symbols, undo the indirection and any
1436
   dynamic state.  */
1437
0
      if (hi != h
1438
0
    && !newdyn
1439
0
    && olddyn)
1440
0
  {
1441
0
    h = hi;
1442
0
    obed->elf_backend_hide_symbol (info, h, true);
1443
0
    h->forced_local = 0;
1444
0
    h->ref_dynamic = 0;
1445
0
    h->def_dynamic = 0;
1446
0
    h->dynamic_def = 0;
1447
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1448
0
      {
1449
0
        h->root.type = bfd_link_hash_undefined;
1450
0
        h->root.u.undef.abfd = abfd;
1451
0
      }
1452
0
    else
1453
0
      {
1454
0
        h->root.type = bfd_link_hash_new;
1455
0
        h->root.u.undef.abfd = NULL;
1456
0
      }
1457
0
    return true;
1458
0
  }
1459
0
    }
1460
1461
  /* Check TLS symbols.  We don't check undefined symbols introduced
1462
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1463
     check symbols from plugins because they also have no type.  */
1464
0
  if (oldbfd != NULL
1465
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1466
0
      && (abfd->flags & BFD_PLUGIN) == 0
1467
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1468
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1469
0
    {
1470
0
      bfd *ntbfd, *tbfd;
1471
0
      bool ntdef, tdef;
1472
0
      asection *ntsec, *tsec;
1473
1474
0
      if (h->type == STT_TLS)
1475
0
  {
1476
0
    ntbfd = abfd;
1477
0
    ntsec = sec;
1478
0
    ntdef = newdef;
1479
0
    tbfd = oldbfd;
1480
0
    tsec = oldsec;
1481
0
    tdef = olddef;
1482
0
  }
1483
0
      else
1484
0
  {
1485
0
    ntbfd = oldbfd;
1486
0
    ntsec = oldsec;
1487
0
    ntdef = olddef;
1488
0
    tbfd = abfd;
1489
0
    tsec = sec;
1490
0
    tdef = newdef;
1491
0
  }
1492
1493
0
      if (tdef && ntdef)
1494
0
  _bfd_error_handler
1495
    /* xgettext:c-format */
1496
0
    (_("%s: TLS definition in %pB section %pA "
1497
0
       "mismatches non-TLS definition in %pB section %pA"),
1498
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1499
0
      else if (!tdef && !ntdef)
1500
0
  _bfd_error_handler
1501
    /* xgettext:c-format */
1502
0
    (_("%s: TLS reference in %pB "
1503
0
       "mismatches non-TLS reference in %pB"),
1504
0
     h->root.root.string, tbfd, ntbfd);
1505
0
      else if (tdef)
1506
0
  _bfd_error_handler
1507
    /* xgettext:c-format */
1508
0
    (_("%s: TLS definition in %pB section %pA "
1509
0
       "mismatches non-TLS reference in %pB"),
1510
0
     h->root.root.string, tbfd, tsec, ntbfd);
1511
0
      else
1512
0
  _bfd_error_handler
1513
    /* xgettext:c-format */
1514
0
    (_("%s: TLS reference in %pB "
1515
0
       "mismatches non-TLS definition in %pB section %pA"),
1516
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1517
1518
0
      bfd_set_error (bfd_error_bad_value);
1519
0
      return false;
1520
0
    }
1521
1522
  /* If the old symbol has non-default visibility, we ignore the new
1523
     definition from a dynamic object.  */
1524
0
  if (newdyn
1525
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1526
0
      && !bfd_is_und_section (sec))
1527
0
    {
1528
0
      *skip = true;
1529
      /* Make sure this symbol is dynamic.  */
1530
0
      h->ref_dynamic = 1;
1531
0
      hi->ref_dynamic = 1;
1532
      /* A protected symbol has external availability. Make sure it is
1533
   recorded as dynamic.
1534
1535
   FIXME: Should we check type and size for protected symbol?  */
1536
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1537
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1538
0
      else
1539
0
  return true;
1540
0
    }
1541
0
  else if (!newdyn
1542
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1543
0
     && h->def_dynamic)
1544
0
    {
1545
      /* If the new symbol with non-default visibility comes from a
1546
   relocatable file and the old definition comes from a dynamic
1547
   object, we remove the old definition.  */
1548
0
      if (hi->root.type == bfd_link_hash_indirect)
1549
0
  {
1550
    /* Handle the case where the old dynamic definition is
1551
       default versioned.  We need to copy the symbol info from
1552
       the symbol with default version to the normal one if it
1553
       was referenced before.  */
1554
0
    if (h->ref_regular)
1555
0
      {
1556
0
        hi->root.type = h->root.type;
1557
0
        h->root.type = bfd_link_hash_indirect;
1558
0
        obed->elf_backend_copy_indirect_symbol (info, hi, h);
1559
1560
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1561
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1562
0
    {
1563
      /* If the new symbol is hidden or internal, completely undo
1564
         any dynamic link state.  */
1565
0
      obed->elf_backend_hide_symbol (info, h, true);
1566
0
      h->forced_local = 0;
1567
0
      h->ref_dynamic = 0;
1568
0
    }
1569
0
        else
1570
0
    h->ref_dynamic = 1;
1571
1572
0
        h->def_dynamic = 0;
1573
        /* FIXME: Should we check type and size for protected symbol?  */
1574
0
        h->size = 0;
1575
0
        h->type = 0;
1576
1577
0
        h = hi;
1578
0
      }
1579
0
    else
1580
0
      h = hi;
1581
0
  }
1582
1583
      /* If the old symbol was undefined before, then it will still be
1584
   on the undefs list.  If the new symbol is undefined or
1585
   common, we can't make it bfd_link_hash_new here, because new
1586
   undefined or common symbols will be added to the undefs list
1587
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1588
   added twice to the undefs list.  Also, if the new symbol is
1589
   undefweak then we don't want to lose the strong undef.  */
1590
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1591
0
  {
1592
0
    h->root.type = bfd_link_hash_undefined;
1593
0
    h->root.u.undef.abfd = abfd;
1594
0
  }
1595
0
      else
1596
0
  {
1597
0
    h->root.type = bfd_link_hash_new;
1598
0
    h->root.u.undef.abfd = NULL;
1599
0
  }
1600
1601
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1602
0
  {
1603
    /* If the new symbol is hidden or internal, completely undo
1604
       any dynamic link state.  */
1605
0
    obed->elf_backend_hide_symbol (info, h, true);
1606
0
    h->forced_local = 0;
1607
0
    h->ref_dynamic = 0;
1608
0
  }
1609
0
      else
1610
0
  h->ref_dynamic = 1;
1611
0
      h->def_dynamic = 0;
1612
      /* FIXME: Should we check type and size for protected symbol?  */
1613
0
      h->size = 0;
1614
0
      h->type = 0;
1615
0
      return true;
1616
0
    }
1617
1618
  /* If a new weak symbol definition comes from a regular file and the
1619
     old symbol comes from a dynamic library, we treat the new one as
1620
     strong.  Similarly, an old weak symbol definition from a regular
1621
     file is treated as strong when the new symbol comes from a dynamic
1622
     library.  Further, an old weak symbol from a dynamic library is
1623
     treated as strong if the new symbol is from a dynamic library.
1624
     This reflects the way glibc's ld.so works.
1625
1626
     Also allow a weak symbol to override a linker script symbol
1627
     defined by an early pass over the script.  This is done so the
1628
     linker knows the symbol is defined in an object file, for the
1629
     DEFINED script function.
1630
1631
     Do this before setting *type_change_ok or *size_change_ok so that
1632
     we warn properly when dynamic library symbols are overridden.  */
1633
1634
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1635
0
    newweak = false;
1636
0
  if (olddef && newdyn)
1637
0
    oldweak = false;
1638
1639
  /* Allow changes between different types of function symbol.  */
1640
0
  if (newfunc && oldfunc)
1641
0
    *type_change_ok = true;
1642
1643
  /* It's OK to change the type if either the existing symbol or the
1644
     new symbol is weak.  A type change is also OK if the old symbol
1645
     is undefined and the new symbol is defined.  */
1646
1647
0
  if (oldweak
1648
0
      || newweak
1649
0
      || (newdef
1650
0
    && h->root.type == bfd_link_hash_undefined))
1651
0
    *type_change_ok = true;
1652
1653
  /* It's OK to change the size if either the existing symbol or the
1654
     new symbol is weak, or if the old symbol is undefined.  */
1655
1656
0
  if (*type_change_ok
1657
0
      || h->root.type == bfd_link_hash_undefined)
1658
0
    *size_change_ok = true;
1659
1660
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1661
     symbol, respectively, appears to be a common symbol in a dynamic
1662
     object.  If a symbol appears in an uninitialized section, and is
1663
     not weak, and is not a function, then it may be a common symbol
1664
     which was resolved when the dynamic object was created.  We want
1665
     to treat such symbols specially, because they raise special
1666
     considerations when setting the symbol size: if the symbol
1667
     appears as a common symbol in a regular object, and the size in
1668
     the regular object is larger, we must make sure that we use the
1669
     larger size.  This problematic case can always be avoided in C,
1670
     but it must be handled correctly when using Fortran shared
1671
     libraries.
1672
1673
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1674
     likewise for OLDDYNCOMMON and OLDDEF.
1675
1676
     Note that this test is just a heuristic, and that it is quite
1677
     possible to have an uninitialized symbol in a shared object which
1678
     is really a definition, rather than a common symbol.  This could
1679
     lead to some minor confusion when the symbol really is a common
1680
     symbol in some regular object.  However, I think it will be
1681
     harmless.  */
1682
1683
0
  if (newdyn
1684
0
      && newdef
1685
0
      && !newweak
1686
0
      && (sec->flags & SEC_ALLOC) != 0
1687
0
      && (sec->flags & SEC_LOAD) == 0
1688
0
      && sym->st_size > 0
1689
0
      && !newfunc)
1690
0
    newdyncommon = true;
1691
0
  else
1692
0
    newdyncommon = false;
1693
1694
0
  if (olddyn
1695
0
      && olddef
1696
0
      && h->root.type == bfd_link_hash_defined
1697
0
      && h->def_dynamic
1698
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1699
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1700
0
      && h->size > 0
1701
0
      && !oldfunc)
1702
0
    olddyncommon = true;
1703
0
  else
1704
0
    olddyncommon = false;
1705
1706
  /* We now know everything about the old and new symbols.  We ask the
1707
     backend to check if we can merge them.  */
1708
0
  if (obed->merge_symbol != NULL)
1709
0
    {
1710
0
      if (!obed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1711
0
  return false;
1712
0
      sec = *psec;
1713
0
    }
1714
1715
  /* There are multiple definitions of a normal symbol.  Skip the
1716
     default symbol as well as definition from an IR object.  */
1717
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1718
0
      && !default_sym && h->def_regular
1719
0
      && !(oldbfd != NULL
1720
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1721
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1722
0
    {
1723
      /* Handle a multiple definition.  */
1724
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1725
0
                 abfd, sec, *pvalue);
1726
0
      *skip = true;
1727
0
      return true;
1728
0
    }
1729
1730
  /* If both the old and the new symbols look like common symbols in a
1731
     dynamic object, set the size of the symbol to the larger of the
1732
     two.  */
1733
1734
0
  if (olddyncommon
1735
0
      && newdyncommon
1736
0
      && sym->st_size != h->size)
1737
0
    {
1738
      /* Since we think we have two common symbols, issue a multiple
1739
   common warning if desired.  Note that we only warn if the
1740
   size is different.  If the size is the same, we simply let
1741
   the old symbol override the new one as normally happens with
1742
   symbols defined in dynamic objects.  */
1743
1744
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1745
0
             bfd_link_hash_common, sym->st_size);
1746
0
      if (sym->st_size > h->size)
1747
0
  h->size = sym->st_size;
1748
1749
0
      *size_change_ok = true;
1750
0
    }
1751
1752
  /* If we are looking at a dynamic object, and we have found a
1753
     definition, we need to see if the symbol was already defined by
1754
     some other object.  If so, we want to use the existing
1755
     definition, and we do not want to report a multiple symbol
1756
     definition error; we do this by clobbering *PSEC to be
1757
     bfd_und_section_ptr.
1758
1759
     We treat a common symbol as a definition if the symbol in the
1760
     shared library is a function, since common symbols always
1761
     represent variables; this can cause confusion in principle, but
1762
     any such confusion would seem to indicate an erroneous program or
1763
     shared library.  We also permit a common symbol in a regular
1764
     object to override a weak symbol in a shared object.  */
1765
1766
0
  if (newdyn
1767
0
      && newdef
1768
0
      && (olddef
1769
0
    || (h->root.type == bfd_link_hash_common
1770
0
        && (newweak || newfunc))))
1771
0
    {
1772
0
      *override = abfd;
1773
0
      newdef = false;
1774
0
      newdyncommon = false;
1775
1776
0
      *psec = sec = bfd_und_section_ptr;
1777
0
      *size_change_ok = true;
1778
1779
      /* If we get here when the old symbol is a common symbol, then
1780
   we are explicitly letting it override a weak symbol or
1781
   function in a dynamic object, and we don't want to warn about
1782
   a type change.  If the old symbol is a defined symbol, a type
1783
   change warning may still be appropriate.  */
1784
1785
0
      if (h->root.type == bfd_link_hash_common)
1786
0
  *type_change_ok = true;
1787
0
    }
1788
1789
  /* Handle the special case of an old common symbol merging with a
1790
     new symbol which looks like a common symbol in a shared object.
1791
     We change *PSEC and *PVALUE to make the new symbol look like a
1792
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1793
     right thing.  */
1794
1795
0
  if (newdyncommon
1796
0
      && h->root.type == bfd_link_hash_common)
1797
0
    {
1798
0
      *override = oldbfd;
1799
0
      newdef = false;
1800
0
      newdyncommon = false;
1801
0
      *pvalue = sym->st_size;
1802
0
      *psec = sec = bed->common_section (oldsec);
1803
0
      *size_change_ok = true;
1804
0
    }
1805
1806
  /* Skip weak definitions of symbols that are already defined.  */
1807
0
  if (newdef && olddef && newweak)
1808
0
    {
1809
      /* Don't skip new non-IR weak syms.  */
1810
0
      if (!(oldbfd != NULL
1811
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1812
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1813
0
  {
1814
0
    newdef = false;
1815
0
    *skip = true;
1816
0
  }
1817
1818
      /* Merge st_other.  If the symbol already has a dynamic index,
1819
   but visibility says it should not be visible, turn it into a
1820
   local symbol.  */
1821
0
      elf_merge_st_other (info->output_bfd, abfd, h, sym->st_other,
1822
0
        sec, newdef, newdyn);
1823
0
      if (h->dynindx != -1)
1824
0
  switch (ELF_ST_VISIBILITY (h->other))
1825
0
    {
1826
0
    case STV_INTERNAL:
1827
0
    case STV_HIDDEN:
1828
0
      obed->elf_backend_hide_symbol (info, h, true);
1829
0
      break;
1830
0
    }
1831
0
    }
1832
1833
  /* If the old symbol is from a dynamic object, and the new symbol is
1834
     a definition which is not from a dynamic object, then the new
1835
     symbol overrides the old symbol.  Symbols from regular files
1836
     always take precedence over symbols from dynamic objects, even if
1837
     they are defined after the dynamic object in the link.
1838
1839
     As above, we again permit a common symbol in a regular object to
1840
     override a definition in a shared object if the shared object
1841
     symbol is a function or is weak.  */
1842
1843
0
  flip = NULL;
1844
0
  if (!newdyn
1845
0
      && (newdef
1846
0
    || (bfd_is_com_section (sec)
1847
0
        && (oldweak || oldfunc)))
1848
0
      && olddyn
1849
0
      && olddef
1850
0
      && h->def_dynamic)
1851
0
    {
1852
      /* Change the hash table entry to undefined, and let
1853
   _bfd_generic_link_add_one_symbol do the right thing with the
1854
   new definition.  */
1855
1856
0
      h->root.type = bfd_link_hash_undefined;
1857
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1858
0
      *size_change_ok = true;
1859
1860
0
      olddef = false;
1861
0
      olddyncommon = false;
1862
1863
      /* We again permit a type change when a common symbol may be
1864
   overriding a function.  */
1865
1866
0
      if (bfd_is_com_section (sec))
1867
0
  {
1868
0
    if (oldfunc)
1869
0
      {
1870
        /* If a common symbol overrides a function, make sure
1871
     that it isn't defined dynamically nor has type
1872
     function.  */
1873
0
        h->def_dynamic = 0;
1874
0
        h->type = STT_NOTYPE;
1875
0
      }
1876
0
    *type_change_ok = true;
1877
0
  }
1878
1879
0
      if (hi->root.type == bfd_link_hash_indirect)
1880
0
  flip = hi;
1881
0
      else
1882
  /* This union may have been set to be non-NULL when this symbol
1883
     was seen in a dynamic object.  We must force the union to be
1884
     NULL, so that it is correct for a regular symbol.  */
1885
0
  h->verinfo.vertree = NULL;
1886
0
    }
1887
1888
  /* Handle the special case of a new common symbol merging with an
1889
     old symbol that looks like it might be a common symbol defined in
1890
     a shared object.  Note that we have already handled the case in
1891
     which a new common symbol should simply override the definition
1892
     in the shared library.  */
1893
1894
0
  if (! newdyn
1895
0
      && bfd_is_com_section (sec)
1896
0
      && olddyncommon)
1897
0
    {
1898
      /* It would be best if we could set the hash table entry to a
1899
   common symbol, but we don't know what to use for the section
1900
   or the alignment.  */
1901
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1902
0
             bfd_link_hash_common, sym->st_size);
1903
1904
      /* If the presumed common symbol in the dynamic object is
1905
   larger, pretend that the new symbol has its size.  */
1906
1907
0
      if (h->size > *pvalue)
1908
0
  *pvalue = h->size;
1909
1910
      /* We need to remember the alignment required by the symbol
1911
   in the dynamic object.  */
1912
0
      BFD_ASSERT (pold_alignment);
1913
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1914
1915
0
      olddef = false;
1916
0
      olddyncommon = false;
1917
1918
0
      h->root.type = bfd_link_hash_undefined;
1919
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1920
1921
0
      *size_change_ok = true;
1922
0
      *type_change_ok = true;
1923
1924
0
      if (hi->root.type == bfd_link_hash_indirect)
1925
0
  flip = hi;
1926
0
      else
1927
0
  h->verinfo.vertree = NULL;
1928
0
    }
1929
1930
0
  if (flip != NULL)
1931
0
    {
1932
      /* Handle the case where we had a versioned symbol in a dynamic
1933
   library and now find a definition in a normal object.  In this
1934
   case, we make the versioned symbol point to the normal one.  */
1935
0
      flip->root.type = h->root.type;
1936
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1937
0
      h->root.type = bfd_link_hash_indirect;
1938
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1939
0
      obed->elf_backend_copy_indirect_symbol (info, flip, h);
1940
0
      if (h->def_dynamic)
1941
0
  {
1942
0
    h->def_dynamic = 0;
1943
0
    flip->ref_dynamic = 1;
1944
0
  }
1945
0
    }
1946
1947
0
  return true;
1948
0
}
1949
1950
/* This function is called to create an indirect symbol from the
1951
   default for the symbol with the default version if needed. The
1952
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1953
   set DYNSYM if the new indirect symbol is dynamic.  */
1954
1955
static bool
1956
_bfd_elf_add_default_symbol (bfd *abfd,
1957
           struct bfd_link_info *info,
1958
           struct elf_link_hash_entry *h,
1959
           const char *name,
1960
           Elf_Internal_Sym *sym,
1961
           asection *sec,
1962
           bfd_vma value,
1963
           bfd **poldbfd,
1964
           bool *dynsym)
1965
0
{
1966
0
  bool type_change_ok;
1967
0
  bool size_change_ok;
1968
0
  bool skip;
1969
0
  char *shortname;
1970
0
  struct elf_link_hash_entry *hi;
1971
0
  struct bfd_link_hash_entry *bh;
1972
0
  elf_backend_data *obed;
1973
0
  bool collect;
1974
0
  bool dynamic;
1975
0
  bfd *override;
1976
0
  const char *p;
1977
0
  size_t len, shortlen;
1978
0
  asection *tmp_sec;
1979
0
  bool matched;
1980
1981
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1982
0
    return true;
1983
1984
  /* If this symbol has a version, and it is the default version, we
1985
     create an indirect symbol from the default name to the fully
1986
     decorated name.  This will cause external references which do not
1987
     specify a version to be bound to this version of the symbol.  */
1988
0
  p = strchr (name, ELF_VER_CHR);
1989
0
  if (h->versioned == unknown)
1990
0
    {
1991
0
      if (p == NULL)
1992
0
  {
1993
0
    h->versioned = unversioned;
1994
0
    return true;
1995
0
  }
1996
0
      else
1997
0
  {
1998
0
    if (p[1] != ELF_VER_CHR)
1999
0
      {
2000
0
        h->versioned = versioned_hidden;
2001
0
        return true;
2002
0
      }
2003
0
    else
2004
0
      h->versioned = versioned;
2005
0
  }
2006
0
    }
2007
0
  else
2008
0
    {
2009
      /* PR ld/19073: We may see an unversioned definition after the
2010
   default version.  */
2011
0
      if (p == NULL)
2012
0
  return true;
2013
0
    }
2014
2015
0
  obed = get_elf_backend_data (info->output_bfd);
2016
0
  collect = obed->collect;
2017
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
2018
2019
0
  shortlen = p - name;
2020
0
  shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
2021
0
  if (shortname == NULL)
2022
0
    return false;
2023
0
  memcpy (shortname, name, shortlen);
2024
0
  shortname[shortlen] = '\0';
2025
2026
  /* We are going to create a new symbol.  Merge it with any existing
2027
     symbol with this name.  For the purposes of the merge, act as
2028
     though we were defining the symbol we just defined, although we
2029
     actually going to define an indirect symbol.  */
2030
0
  type_change_ok = false;
2031
0
  size_change_ok = false;
2032
0
  matched = true;
2033
0
  tmp_sec = sec;
2034
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2035
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2036
0
            &type_change_ok, &size_change_ok, &matched))
2037
0
    return false;
2038
2039
0
  if (skip)
2040
0
    goto nondefault;
2041
2042
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2043
0
    {
2044
      /* If the undecorated symbol will have a version added by a
2045
   script different to H, then don't indirect to/from the
2046
   undecorated symbol.  This isn't ideal because we may not yet
2047
   have seen symbol versions, if given by a script on the
2048
   command line rather than via --version-script.  */
2049
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2050
0
  {
2051
0
    bool hide;
2052
2053
0
    hi->verinfo.vertree
2054
0
      = bfd_find_version_for_sym (info->version_info,
2055
0
          hi->root.root.string, &hide);
2056
0
    if (hi->verinfo.vertree != NULL && hide)
2057
0
      {
2058
0
        obed->elf_backend_hide_symbol (info, hi, true);
2059
0
        goto nondefault;
2060
0
      }
2061
0
  }
2062
0
      if (hi->verinfo.vertree != NULL
2063
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2064
0
  goto nondefault;
2065
0
    }
2066
2067
0
  if (! override)
2068
0
    {
2069
      /* Add the default symbol if not performing a relocatable link.  */
2070
0
      if (! bfd_link_relocatable (info))
2071
0
  {
2072
0
    bh = &hi->root;
2073
0
    if (bh->type == bfd_link_hash_defined
2074
0
        && bh->u.def.section->owner != NULL
2075
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2076
0
      {
2077
        /* Mark the previous definition from IR object as
2078
     undefined so that the generic linker will override
2079
     it.  */
2080
0
        bh->type = bfd_link_hash_undefined;
2081
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2082
0
      }
2083
0
    if (! (_bfd_generic_link_add_one_symbol
2084
0
     (info, abfd, shortname, BSF_INDIRECT,
2085
0
      bfd_ind_section_ptr,
2086
0
      0, name, false, collect, &bh)))
2087
0
      return false;
2088
0
    hi = (struct elf_link_hash_entry *) bh;
2089
0
  }
2090
0
    }
2091
0
  else
2092
0
    {
2093
      /* In this case the symbol named SHORTNAME is overriding the
2094
   indirect symbol we want to add.  We were planning on making
2095
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2096
   is the name without a version.  NAME is the fully versioned
2097
   name, and it is the default version.
2098
2099
   Overriding means that we already saw a definition for the
2100
   symbol SHORTNAME in a regular object, and it is overriding
2101
   the symbol defined in the dynamic object.
2102
2103
   When this happens, we actually want to change NAME, the
2104
   symbol we just added, to refer to SHORTNAME.  This will cause
2105
   references to NAME in the shared object to become references
2106
   to SHORTNAME in the regular object.  This is what we expect
2107
   when we override a function in a shared object: that the
2108
   references in the shared object will be mapped to the
2109
   definition in the regular object.  */
2110
2111
0
      while (hi->root.type == bfd_link_hash_indirect
2112
0
       || hi->root.type == bfd_link_hash_warning)
2113
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2114
2115
0
      h->root.type = bfd_link_hash_indirect;
2116
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2117
0
      if (h->def_dynamic)
2118
0
  {
2119
0
    h->def_dynamic = 0;
2120
0
    hi->ref_dynamic = 1;
2121
0
    if (hi->ref_regular
2122
0
        || hi->def_regular)
2123
0
      {
2124
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2125
0
    return false;
2126
0
      }
2127
0
  }
2128
2129
      /* Now set HI to H, so that the following code will set the
2130
   other fields correctly.  */
2131
0
      hi = h;
2132
0
    }
2133
2134
  /* Check if HI is a warning symbol.  */
2135
0
  if (hi->root.type == bfd_link_hash_warning)
2136
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2137
2138
  /* If there is a duplicate definition somewhere, then HI may not
2139
     point to an indirect symbol.  We will have reported an error to
2140
     the user in that case.  */
2141
2142
0
  if (hi->root.type == bfd_link_hash_indirect)
2143
0
    {
2144
0
      struct elf_link_hash_entry *ht;
2145
2146
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2147
0
      obed->elf_backend_copy_indirect_symbol (info, ht, hi);
2148
2149
      /* If we first saw a reference to SHORTNAME with non-default
2150
   visibility, merge that visibility to the @@VER symbol.  */
2151
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, ht, hi->other,
2152
0
        sec, true, dynamic);
2153
2154
      /* A reference to the SHORTNAME symbol from a dynamic library
2155
   will be satisfied by the versioned symbol at runtime.  In
2156
   effect, we have a reference to the versioned symbol.  */
2157
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2158
0
      hi->dynamic_def |= ht->dynamic_def;
2159
2160
      /* See if the new flags lead us to realize that the symbol must
2161
   be dynamic.  */
2162
0
      if (! *dynsym)
2163
0
  {
2164
0
    if (! dynamic)
2165
0
      {
2166
0
        if (! bfd_link_executable (info)
2167
0
      || hi->def_dynamic
2168
0
      || hi->ref_dynamic)
2169
0
    *dynsym = true;
2170
0
      }
2171
0
    else
2172
0
      {
2173
0
        if (hi->ref_regular)
2174
0
    *dynsym = true;
2175
0
      }
2176
0
  }
2177
0
    }
2178
2179
  /* We also need to define an indirection from the nondefault version
2180
     of the symbol.  */
2181
2182
0
 nondefault:
2183
0
  len = strlen (name);
2184
0
  shortname = bfd_hash_allocate (&info->hash->table, len);
2185
0
  if (shortname == NULL)
2186
0
    return false;
2187
0
  memcpy (shortname, name, shortlen);
2188
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2189
2190
  /* Once again, merge with any existing symbol.  */
2191
0
  type_change_ok = false;
2192
0
  size_change_ok = false;
2193
0
  tmp_sec = sec;
2194
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2195
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2196
0
            &type_change_ok, &size_change_ok, &matched))
2197
0
    return false;
2198
2199
0
  if (skip)
2200
0
    {
2201
0
      if (!dynamic
2202
0
    && h->root.type == bfd_link_hash_defweak
2203
0
    && hi->root.type == bfd_link_hash_defined)
2204
0
  {
2205
    /* We are handling a weak sym@@ver and attempting to define
2206
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2207
       new weak sym@ver because there is already a strong sym@ver.
2208
       However, sym@ver and sym@@ver are really the same symbol.
2209
       The existing strong sym@ver ought to override sym@@ver.  */
2210
0
    h->root.type = bfd_link_hash_defined;
2211
0
    h->root.u.def.section = hi->root.u.def.section;
2212
0
    h->root.u.def.value = hi->root.u.def.value;
2213
0
    hi->root.type = bfd_link_hash_indirect;
2214
0
    hi->root.u.i.link = &h->root;
2215
0
  }
2216
0
      else
2217
0
  return true;
2218
0
    }
2219
0
  else if (override)
2220
0
    {
2221
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2222
   the type of override we do in the case above unless it is
2223
   overridden by a versioned definition.  */
2224
0
      if (hi->root.type != bfd_link_hash_defined
2225
0
    && hi->root.type != bfd_link_hash_defweak)
2226
0
  _bfd_error_handler
2227
    /* xgettext:c-format */
2228
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2229
0
     abfd, shortname);
2230
0
      return true;
2231
0
    }
2232
0
  else
2233
0
    {
2234
0
      bh = &hi->root;
2235
0
      if (! (_bfd_generic_link_add_one_symbol
2236
0
       (info, abfd, shortname, BSF_INDIRECT,
2237
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2238
0
  return false;
2239
0
      hi = (struct elf_link_hash_entry *) bh;
2240
0
    }
2241
2242
  /* If there is a duplicate definition somewhere, then HI may not
2243
     point to an indirect symbol.  We will have reported an error
2244
     to the user in that case.  */
2245
0
  if (hi->root.type == bfd_link_hash_indirect)
2246
0
    {
2247
0
      obed->elf_backend_copy_indirect_symbol (info, h, hi);
2248
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2249
0
      hi->dynamic_def |= h->dynamic_def;
2250
2251
      /* If we first saw a reference to @VER symbol with
2252
   non-default visibility, merge that visibility to the
2253
   @@VER symbol.  */
2254
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, h, hi->other,
2255
0
        sec, true, dynamic);
2256
2257
      /* See if the new flags lead us to realize that the symbol
2258
   must be dynamic.  */
2259
0
      if (! *dynsym)
2260
0
  {
2261
0
    if (! dynamic)
2262
0
      {
2263
0
        if (! bfd_link_executable (info)
2264
0
      || hi->ref_dynamic)
2265
0
    *dynsym = true;
2266
0
      }
2267
0
    else
2268
0
      {
2269
0
        if (hi->ref_regular)
2270
0
    *dynsym = true;
2271
0
      }
2272
0
  }
2273
0
    }
2274
2275
0
  return true;
2276
0
}
2277

2278
/* This routine is used to export all defined symbols into the dynamic
2279
   symbol table.  It is called via elf_link_hash_traverse.  */
2280
2281
static bool
2282
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2283
0
{
2284
0
  struct elf_info_failed *eif = data;
2285
2286
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2287
0
  if (h->root.type == bfd_link_hash_indirect)
2288
0
    return true;
2289
2290
  /* Ignore this if we won't export it.  */
2291
0
  if (!eif->info->export_dynamic && !h->dynamic)
2292
0
    return true;
2293
2294
0
  if (h->dynindx == -1
2295
0
      && (h->def_regular || h->ref_regular)
2296
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2297
0
            h->root.root.string))
2298
0
    {
2299
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2300
0
  {
2301
0
    eif->failed = true;
2302
0
    return false;
2303
0
  }
2304
0
    }
2305
2306
0
  return true;
2307
0
}
2308

2309
/* Return true if linked against glibc.  Otherwise return false.  If
2310
   linked against glibc, add VERSION_DEP to the list of glibc version
2311
   dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
2312
   true, add VERSION_DEP to the version dependency list only if libc.so
2313
   defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
2314
   minor base version.  */
2315
2316
static bool
2317
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2318
          const char *version_dep,
2319
          int *glibc_minor_base,
2320
          bool *auto_version)
2321
0
{
2322
0
  Elf_Internal_Verneed *t;
2323
0
  Elf_Internal_Vernaux *a;
2324
0
  int minor_version = -1;
2325
0
  bool added = false;
2326
0
  bool glibc = false;
2327
2328
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2329
0
       t != NULL;
2330
0
       t = t->vn_nextref)
2331
0
    {
2332
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2333
0
      if (soname != NULL && startswith (soname, "libc.so."))
2334
0
  break;
2335
0
    }
2336
2337
  /* Skip the shared library if it isn't libc.so.  */
2338
0
  if (t == NULL)
2339
0
    goto update_auto_version_and_return;
2340
2341
0
  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2342
0
    {
2343
      /* Return if VERSION_DEP dependency has been added.  */
2344
0
      if (a->vna_nodename == version_dep
2345
0
    || strcmp (a->vna_nodename, version_dep) == 0)
2346
0
  {
2347
0
    glibc = true;
2348
0
    goto update_auto_version_and_return;
2349
0
  }
2350
2351
      /* Check if libc.so provides GLIBC_2.XX version.  */
2352
0
      if (startswith (a->vna_nodename, "GLIBC_2."))
2353
0
  {
2354
0
    minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2355
0
    if (minor_version < *glibc_minor_base)
2356
0
      *glibc_minor_base = minor_version;
2357
0
  }
2358
0
    }
2359
2360
  /* Skip if it isn't linked against glibc.  */
2361
0
  if (minor_version < 0)
2362
0
    goto update_auto_version_and_return;
2363
2364
0
  glibc = true;
2365
2366
0
  if (auto_version && *auto_version)
2367
0
    {
2368
      /* Add VERSION_DEP to the version dependency list only if
2369
   libc.so defines VERSION_DEP.  */
2370
2371
0
      bool defined = false;
2372
0
      Elf_Internal_Verdef *d;
2373
2374
0
      for (d = elf_tdata (t->vn_bfd)->verdef;
2375
0
     d != NULL;
2376
0
     d = d->vd_nextdef)
2377
0
  if (strcmp (d->vd_nodename, version_dep) == 0)
2378
0
    {
2379
0
      defined = true;
2380
0
      break;
2381
0
    }
2382
2383
      /* Set *AUTO_VERSION to false and return true to indicate that
2384
   libc.so doesn't define VERSION_DEP.  */
2385
0
      if (!defined)
2386
0
  goto update_auto_version_and_return;
2387
0
    }
2388
2389
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2390
0
  if (startswith (version_dep, "GLIBC_2."))
2391
0
    {
2392
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2393
0
      if (minor_version <= *glibc_minor_base)
2394
0
  goto update_auto_version_and_return;
2395
0
    }
2396
2397
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2398
0
  if (a == NULL)
2399
0
    {
2400
0
      rinfo->failed = true;
2401
0
      glibc = false;
2402
0
      goto update_auto_version_and_return;
2403
0
    }
2404
2405
0
  a->vna_nodename = version_dep;
2406
0
  a->vna_flags = 0;
2407
0
  a->vna_nextptr = t->vn_auxptr;
2408
0
  a->vna_other = rinfo->vers + 1;
2409
0
  ++rinfo->vers;
2410
2411
0
  t->vn_auxptr = a;
2412
2413
0
  added = true;
2414
2415
0
 update_auto_version_and_return:
2416
0
  if (auto_version)
2417
0
    *auto_version = added;
2418
2419
0
  return glibc;
2420
0
}
2421
2422
/* Add VERSION_DEP to the list of version dependencies when linked
2423
   against glibc.  */
2424
2425
bool
2426
_bfd_elf_link_add_glibc_version_dependency
2427
  (struct elf_find_verdep_info *rinfo,
2428
   const char *const version_dep[],
2429
   bool *auto_version)
2430
0
{
2431
0
  int glibc_minor_base = INT_MAX;
2432
2433
0
  do
2434
0
    {
2435
      /* Return if not linked against glibc.  */
2436
0
      if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
2437
0
               &glibc_minor_base, auto_version))
2438
0
  return false;
2439
0
      version_dep++;
2440
0
      auto_version++;
2441
0
    }
2442
0
  while (*version_dep != NULL);
2443
2444
0
  return true;
2445
0
}
2446
2447
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2448
   linked against glibc.  */
2449
2450
void
2451
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2452
0
{
2453
0
  if (rinfo->info->enable_dt_relr)
2454
0
    {
2455
0
      static const char *const version[] =
2456
0
  {
2457
0
    "GLIBC_ABI_DT_RELR",
2458
0
    NULL
2459
0
  };
2460
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
2461
0
    }
2462
0
}
2463
2464
/* Look through the symbols which are defined in other shared
2465
   libraries and referenced here.  Update the list of version
2466
   dependencies.  This will be put into the .gnu.version_r section.
2467
   This function is called via elf_link_hash_traverse.  */
2468
2469
static bool
2470
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2471
           void *data)
2472
0
{
2473
0
  struct elf_find_verdep_info *rinfo = data;
2474
0
  Elf_Internal_Verneed *t;
2475
0
  Elf_Internal_Vernaux *a;
2476
2477
  /* We only care about symbols defined in shared objects with version
2478
     information.  */
2479
0
  if (!h->def_dynamic
2480
0
      || h->def_regular
2481
0
      || h->dynindx == -1
2482
0
      || h->verinfo.verdef == NULL
2483
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2484
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2485
0
    return true;
2486
2487
  /* See if we already know about this version.  */
2488
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2489
0
       t != NULL;
2490
0
       t = t->vn_nextref)
2491
0
    {
2492
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2493
0
  continue;
2494
2495
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2496
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2497
0
    return true;
2498
2499
0
      break;
2500
0
    }
2501
2502
  /* This is a new version.  Add it to tree we are building.  */
2503
2504
0
  if (t == NULL)
2505
0
    {
2506
0
      t = bfd_zalloc (rinfo->info->output_bfd, sizeof (*t));
2507
0
      if (t == NULL)
2508
0
  {
2509
0
    rinfo->failed = true;
2510
0
    return false;
2511
0
  }
2512
2513
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2514
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2515
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2516
0
    }
2517
2518
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2519
0
  if (a == NULL)
2520
0
    {
2521
0
      rinfo->failed = true;
2522
0
      return false;
2523
0
    }
2524
2525
  /* Note that we are copying a string pointer here, and testing it
2526
     above.  If bfd_elf_string_from_elf_section is ever changed to
2527
     discard the string data when low in memory, this will have to be
2528
     fixed.  */
2529
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2530
2531
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2532
0
  a->vna_nextptr = t->vn_auxptr;
2533
2534
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2535
0
  ++rinfo->vers;
2536
2537
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2538
2539
0
  t->vn_auxptr = a;
2540
2541
0
  return true;
2542
0
}
2543
2544
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2545
   hidden.  Set *T_P to NULL if there is no match.  */
2546
2547
static bool
2548
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2549
             struct elf_link_hash_entry *h,
2550
             const char *version_p,
2551
             struct bfd_elf_version_tree **t_p,
2552
             bool *hide)
2553
0
{
2554
0
  struct bfd_elf_version_tree *t;
2555
2556
  /* Look for the version.  If we find it, it is no longer weak.  */
2557
0
  for (t = info->version_info; t != NULL; t = t->next)
2558
0
    {
2559
0
      if (strcmp (t->name, version_p) == 0)
2560
0
  {
2561
0
    size_t len;
2562
0
    char *alc;
2563
0
    struct bfd_elf_version_expr *d;
2564
2565
0
    len = version_p - h->root.root.string;
2566
0
    alc = bfd_malloc (len);
2567
0
    if (alc == NULL)
2568
0
      return false;
2569
0
    memcpy (alc, h->root.root.string, len - 1);
2570
0
    alc[len - 1] = '\0';
2571
0
    if (alc[len - 2] == ELF_VER_CHR)
2572
0
      alc[len - 2] = '\0';
2573
2574
0
    h->verinfo.vertree = t;
2575
0
    t->used = true;
2576
0
    d = NULL;
2577
2578
0
    if (t->globals.list != NULL)
2579
0
      d = (*t->match) (&t->globals, NULL, alc);
2580
2581
    /* See if there is anything to force this symbol to
2582
       local scope.  */
2583
0
    if (d == NULL && t->locals.list != NULL)
2584
0
      {
2585
0
        d = (*t->match) (&t->locals, NULL, alc);
2586
0
        if (d != NULL
2587
0
      && h->dynindx != -1
2588
0
      && ! info->export_dynamic)
2589
0
    *hide = true;
2590
0
      }
2591
2592
0
    free (alc);
2593
0
    break;
2594
0
  }
2595
0
    }
2596
2597
0
  *t_p = t;
2598
2599
0
  return true;
2600
0
}
2601
2602
/* Return TRUE if the symbol H is hidden by version script.  */
2603
2604
bool
2605
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2606
           struct elf_link_hash_entry *h)
2607
0
{
2608
0
  const char *p;
2609
0
  bool hide = false;
2610
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
2611
2612
  /* Version script only hides symbols defined in regular objects.  */
2613
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2614
0
    return true;
2615
2616
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2617
0
  if (p != NULL && h->verinfo.vertree == NULL)
2618
0
    {
2619
0
      struct bfd_elf_version_tree *t;
2620
2621
0
      ++p;
2622
0
      if (*p == ELF_VER_CHR)
2623
0
  ++p;
2624
2625
0
      if (*p != '\0'
2626
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2627
0
    && hide)
2628
0
  {
2629
0
    if (hide)
2630
0
      obed->elf_backend_hide_symbol (info, h, true);
2631
0
    return true;
2632
0
  }
2633
0
    }
2634
2635
  /* If we don't have a version for this symbol, see if we can find
2636
     something.  */
2637
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2638
0
    {
2639
0
      h->verinfo.vertree
2640
0
  = bfd_find_version_for_sym (info->version_info,
2641
0
            h->root.root.string, &hide);
2642
0
      if (h->verinfo.vertree != NULL && hide)
2643
0
  {
2644
0
    obed->elf_backend_hide_symbol (info, h, true);
2645
0
    return true;
2646
0
  }
2647
0
    }
2648
2649
0
  return false;
2650
0
}
2651
2652
/* Figure out appropriate versions for all the symbols.  We may not
2653
   have the version number script until we have read all of the input
2654
   files, so until that point we don't know which symbols should be
2655
   local.  This function is called via elf_link_hash_traverse.  */
2656
2657
static bool
2658
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2659
0
{
2660
0
  struct elf_info_failed *sinfo = data;
2661
0
  struct bfd_link_info *info = sinfo->info;
2662
0
  elf_backend_data *obed;
2663
0
  struct elf_info_failed eif;
2664
0
  const char *p;
2665
0
  bool hide;
2666
2667
  /* Fix the symbol flags.  */
2668
0
  eif.failed = false;
2669
0
  eif.info = info;
2670
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2671
0
    {
2672
0
      if (eif.failed)
2673
0
  sinfo->failed = true;
2674
0
      return false;
2675
0
    }
2676
2677
0
  obed = get_elf_backend_data (info->output_bfd);
2678
2679
  /* We only need version numbers for symbols defined in regular
2680
     objects.  */
2681
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2682
0
    {
2683
      /* Hide symbols defined in discarded input sections.  */
2684
0
      if ((h->root.type == bfd_link_hash_defined
2685
0
     || h->root.type == bfd_link_hash_defweak)
2686
0
    && discarded_section (h->root.u.def.section))
2687
0
  obed->elf_backend_hide_symbol (info, h, true);
2688
0
      return true;
2689
0
    }
2690
2691
0
  hide = false;
2692
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2693
0
  if (p != NULL && h->verinfo.vertree == NULL)
2694
0
    {
2695
0
      struct bfd_elf_version_tree *t;
2696
2697
0
      ++p;
2698
0
      if (*p == ELF_VER_CHR)
2699
0
  ++p;
2700
2701
      /* If there is no version string, we can just return out.  */
2702
0
      if (*p == '\0')
2703
0
  return true;
2704
2705
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2706
0
  {
2707
0
    sinfo->failed = true;
2708
0
    return false;
2709
0
  }
2710
2711
0
      if (hide)
2712
0
  obed->elf_backend_hide_symbol (info, h, true);
2713
2714
      /* If we are building an application, we need to create a
2715
   version node for this version.  */
2716
0
      if (t == NULL && bfd_link_executable (info))
2717
0
  {
2718
0
    struct bfd_elf_version_tree **pp;
2719
0
    int version_index;
2720
2721
    /* If we aren't going to export this symbol, we don't need
2722
       to worry about it.  */
2723
0
    if (h->dynindx == -1)
2724
0
      return true;
2725
2726
0
    t = bfd_zalloc (info->output_bfd, sizeof (*t));
2727
0
    if (t == NULL)
2728
0
      {
2729
0
        sinfo->failed = true;
2730
0
        return false;
2731
0
      }
2732
2733
0
    t->name = p;
2734
0
    t->name_indx = (unsigned int) -1;
2735
0
    t->used = true;
2736
2737
0
    version_index = 1;
2738
    /* Don't count anonymous version tag.  */
2739
0
    if (sinfo->info->version_info != NULL
2740
0
        && sinfo->info->version_info->vernum == 0)
2741
0
      version_index = 0;
2742
0
    for (pp = &sinfo->info->version_info;
2743
0
         *pp != NULL;
2744
0
         pp = &(*pp)->next)
2745
0
      ++version_index;
2746
0
    t->vernum = version_index;
2747
2748
0
    *pp = t;
2749
2750
0
    h->verinfo.vertree = t;
2751
0
  }
2752
0
      else if (t == NULL)
2753
0
  {
2754
    /* We could not find the version for a symbol when
2755
       generating a shared archive.  Return an error.  */
2756
0
    _bfd_error_handler
2757
      /* xgettext:c-format */
2758
0
      (_("%pB: version node not found for symbol %s"),
2759
0
       info->output_bfd, h->root.root.string);
2760
0
    bfd_set_error (bfd_error_bad_value);
2761
0
    sinfo->failed = true;
2762
0
    return false;
2763
0
  }
2764
0
    }
2765
2766
  /* If we don't have a version for this symbol, see if we can find
2767
     something.  */
2768
0
  if (!hide
2769
0
      && h->verinfo.vertree == NULL
2770
0
      && sinfo->info->version_info != NULL)
2771
0
    {
2772
0
      h->verinfo.vertree
2773
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2774
0
            h->root.root.string, &hide);
2775
0
      if (h->verinfo.vertree != NULL && hide)
2776
0
  obed->elf_backend_hide_symbol (info, h, true);
2777
0
    }
2778
2779
0
  return true;
2780
0
}
2781

2782
/* Read and swap the relocs from the section indicated by SHDR.  This
2783
   may be either a REL or a RELA section.  The relocations are
2784
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2785
   which should have already been allocated to contain enough space.
2786
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2787
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2788
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2789
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2790
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2791
2792
   Returns FALSE if something goes wrong.  */
2793
2794
static bool
2795
elf_link_read_relocs_from_section (bfd *abfd,
2796
           const asection *sec,
2797
           Elf_Internal_Shdr *shdr,
2798
           void **external_relocs_addr,
2799
           size_t *external_relocs_size,
2800
           Elf_Internal_Rela *internal_relocs)
2801
0
{
2802
0
  elf_backend_data *bed;
2803
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2804
0
  const bfd_byte *erela;
2805
0
  const bfd_byte *erelaend;
2806
0
  Elf_Internal_Rela *irela;
2807
0
  Elf_Internal_Shdr *symtab_hdr;
2808
0
  size_t nsyms;
2809
0
  void *external_relocs = *external_relocs_addr;
2810
2811
  /* Position ourselves at the start of the section.  */
2812
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2813
0
    return false;
2814
2815
  /* Read the relocations.  */
2816
0
  *external_relocs_size = shdr->sh_size;
2817
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2818
0
         external_relocs_size,
2819
0
         external_relocs_addr, abfd, true))
2820
0
    return false;
2821
2822
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2823
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2824
2825
0
  bed = get_elf_backend_data (abfd);
2826
2827
  /* Convert the external relocations to the internal format.  */
2828
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2829
0
    swap_in = bed->s->swap_reloc_in;
2830
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2831
0
    swap_in = bed->s->swap_reloca_in;
2832
0
  else
2833
0
    {
2834
0
      bfd_set_error (bfd_error_wrong_format);
2835
0
      return false;
2836
0
    }
2837
2838
0
  erela = (const bfd_byte *) external_relocs;
2839
  /* Setting erelaend like this and comparing with <= handles case of
2840
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2841
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2842
0
  irela = internal_relocs;
2843
0
  while (erela <= erelaend)
2844
0
    {
2845
0
      bfd_vma r_symndx;
2846
2847
0
      (*swap_in) (abfd, erela, irela);
2848
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2849
0
      if (bed->s->arch_size == 64)
2850
0
  r_symndx >>= 24;
2851
0
      if (nsyms > 0)
2852
0
  {
2853
0
    if ((size_t) r_symndx >= nsyms)
2854
0
      {
2855
0
        _bfd_error_handler
2856
    /* xgettext:c-format */
2857
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2858
0
       " for offset %#" PRIx64 " in section `%pA'"),
2859
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2860
0
     (uint64_t) irela->r_offset, sec);
2861
0
        bfd_set_error (bfd_error_bad_value);
2862
0
        return false;
2863
0
      }
2864
0
  }
2865
0
      else if (r_symndx != STN_UNDEF)
2866
0
  {
2867
0
    _bfd_error_handler
2868
      /* xgettext:c-format */
2869
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2870
0
         " for offset %#" PRIx64 " in section `%pA'"
2871
0
         " when the object file has no symbol table"),
2872
0
       abfd, (uint64_t) r_symndx,
2873
0
       (uint64_t) irela->r_offset, sec);
2874
0
    bfd_set_error (bfd_error_bad_value);
2875
0
    return false;
2876
0
  }
2877
0
      irela += bed->s->int_rels_per_ext_rel;
2878
0
      erela += shdr->sh_entsize;
2879
0
    }
2880
2881
0
  return true;
2882
0
}
2883
2884
/* Read and swap the relocs for a section O.  They may have been
2885
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2886
   not NULL, they are used as buffers to read into.  They are known to
2887
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2888
   the return value is allocated using either malloc or bfd_alloc,
2889
   according to the KEEP_MEMORY argument.  If O has two relocation
2890
   sections (both REL and RELA relocations), then the REL_HDR
2891
   relocations will appear first in INTERNAL_RELOCS, followed by the
2892
   RELA_HDR relocations.  If INFO isn't NULL and KEEP_MEMORY is true,
2893
   update cache_size.  */
2894
2895
Elf_Internal_Rela *
2896
_bfd_elf_link_info_read_relocs (bfd *abfd,
2897
        struct bfd_link_info *info,
2898
        const asection *o,
2899
        void *external_relocs,
2900
        Elf_Internal_Rela *internal_relocs,
2901
        bool keep_memory)
2902
0
{
2903
0
  void *alloc1 = NULL;
2904
0
  size_t alloc1_size;
2905
0
  Elf_Internal_Rela *alloc2 = NULL;
2906
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2907
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2908
0
  Elf_Internal_Rela *internal_rela_relocs;
2909
2910
0
  if (esdo->relocs != NULL)
2911
0
    return esdo->relocs;
2912
2913
0
  if (o->reloc_count == 0)
2914
0
    return NULL;
2915
2916
0
  if (internal_relocs == NULL)
2917
0
    {
2918
0
      bfd_size_type size;
2919
2920
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2921
0
      if (keep_memory && info)
2922
0
  info->cache_size += size;
2923
0
      internal_relocs = alloc2 = bfd_malloc (size);
2924
0
      if (internal_relocs == NULL)
2925
0
  return NULL;
2926
0
    }
2927
2928
0
  alloc1 = external_relocs;
2929
0
  internal_rela_relocs = internal_relocs;
2930
0
  if (esdo->rel.hdr)
2931
0
    {
2932
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2933
0
                &alloc1, &alloc1_size,
2934
0
                internal_relocs))
2935
0
  goto error_return;
2936
0
      external_relocs = (((bfd_byte *) external_relocs)
2937
0
       + esdo->rel.hdr->sh_size);
2938
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2939
0
             * bed->s->int_rels_per_ext_rel);
2940
0
    }
2941
2942
0
  if (esdo->rela.hdr
2943
0
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2944
0
                &alloc1, &alloc1_size,
2945
0
                internal_rela_relocs)))
2946
0
    goto error_return;
2947
2948
  /* Cache the results for next time, if we can.  */
2949
0
  if (keep_memory)
2950
0
    esdo->relocs = internal_relocs;
2951
2952
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2953
2954
  /* Don't free alloc2, since if it was allocated we are passing it
2955
     back (under the name of internal_relocs).  */
2956
2957
0
  return internal_relocs;
2958
2959
0
 error_return:
2960
0
  _bfd_munmap_temporary (alloc1, alloc1_size);
2961
0
  free (alloc2);
2962
0
  return NULL;
2963
0
}
2964
2965
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2966
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2967
   struct bfd_link_info.  */
2968
2969
Elf_Internal_Rela *
2970
_bfd_elf_link_read_relocs (bfd *abfd,
2971
         const asection *o,
2972
         void *external_relocs,
2973
         Elf_Internal_Rela *internal_relocs,
2974
         bool keep_memory)
2975
0
{
2976
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2977
0
           internal_relocs, keep_memory);
2978
2979
0
}
2980
2981
/* Compute the size of, and allocate space for, REL_HDR which is the
2982
   section header for a section containing relocations for O.  */
2983
2984
static bool
2985
_bfd_elf_link_size_reloc_section (bfd *obfd,
2986
          struct bfd_elf_section_reloc_data *reldata)
2987
0
{
2988
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2989
2990
  /* That allows us to calculate the size of the section.  */
2991
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2992
2993
  /* The contents field must last into write_object_contents, so we
2994
     allocate it with bfd_alloc rather than malloc.  Also since we
2995
     cannot be sure that the contents will actually be filled in,
2996
     we zero the allocated space.  */
2997
0
  rel_hdr->contents = bfd_zalloc (obfd, rel_hdr->sh_size);
2998
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2999
0
    return false;
3000
3001
0
  if (reldata->hashes == NULL && reldata->count)
3002
0
    {
3003
0
      struct elf_link_hash_entry **p;
3004
3005
0
      p = bfd_zmalloc (reldata->count * sizeof (*p));
3006
0
      if (p == NULL)
3007
0
  return false;
3008
3009
0
      reldata->hashes = p;
3010
0
    }
3011
3012
0
  return true;
3013
0
}
3014
3015
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3016
   originated from the section given by INPUT_REL_HDR) to the
3017
   OUTPUT_BFD.  */
3018
3019
bool
3020
_bfd_elf_link_output_relocs (bfd *output_bfd,
3021
           asection *input_section,
3022
           Elf_Internal_Shdr *input_rel_hdr,
3023
           Elf_Internal_Rela *internal_relocs,
3024
           struct elf_link_hash_entry **rel_hash)
3025
0
{
3026
0
  Elf_Internal_Rela *irela;
3027
0
  Elf_Internal_Rela *irelaend;
3028
0
  bfd_byte *erel;
3029
0
  struct bfd_elf_section_reloc_data *output_reldata;
3030
0
  asection *output_section;
3031
0
  elf_backend_data *obed;
3032
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3033
0
  struct bfd_elf_section_data *esdo;
3034
3035
0
  output_section = input_section->output_section;
3036
3037
0
  obed = get_elf_backend_data (output_bfd);
3038
0
  esdo = elf_section_data (output_section);
3039
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3040
0
    {
3041
0
      output_reldata = &esdo->rel;
3042
0
      swap_out = obed->s->swap_reloc_out;
3043
0
    }
3044
0
  else if (esdo->rela.hdr
3045
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3046
0
    {
3047
0
      output_reldata = &esdo->rela;
3048
0
      swap_out = obed->s->swap_reloca_out;
3049
0
    }
3050
0
  else
3051
0
    {
3052
0
      _bfd_error_handler
3053
  /* xgettext:c-format */
3054
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3055
0
   output_bfd, input_section->owner, input_section);
3056
0
      bfd_set_error (bfd_error_wrong_format);
3057
0
      return false;
3058
0
    }
3059
3060
0
  erel = output_reldata->hdr->contents;
3061
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3062
0
  irela = internal_relocs;
3063
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3064
0
          * obed->s->int_rels_per_ext_rel);
3065
0
  while (irela < irelaend)
3066
0
    {
3067
0
      if (rel_hash && *rel_hash)
3068
0
  (*rel_hash)->has_reloc = 1;
3069
0
      (*swap_out) (output_bfd, irela, erel);
3070
0
      irela += obed->s->int_rels_per_ext_rel;
3071
0
      erel += input_rel_hdr->sh_entsize;
3072
0
      if (rel_hash)
3073
0
  rel_hash++;
3074
0
    }
3075
3076
  /* Bump the counter, so that we know where to add the next set of
3077
     relocations.  */
3078
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3079
3080
0
  return true;
3081
0
}
3082

3083
/* Make weak undefined symbols in PIE dynamic.  */
3084
3085
bool
3086
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3087
         struct elf_link_hash_entry *h)
3088
0
{
3089
0
  if (bfd_link_pie (info)
3090
0
      && h->dynindx == -1
3091
0
      && h->root.type == bfd_link_hash_undefweak)
3092
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3093
3094
0
  return true;
3095
0
}
3096
3097
/* Fix up the flags for a symbol.  This handles various cases which
3098
   can only be fixed after all the input files are seen.  This is
3099
   currently called by both adjust_dynamic_symbol and
3100
   assign_sym_version, which is unnecessary but perhaps more robust in
3101
   the face of future changes.  */
3102
3103
static bool
3104
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3105
         struct elf_info_failed *eif)
3106
0
{
3107
0
  elf_backend_data *obed;
3108
3109
  /* If this symbol was mentioned in a non-ELF file, try to set
3110
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3111
     permit a non-ELF file to correctly refer to a symbol defined in
3112
     an ELF dynamic object.  */
3113
0
  if (h->non_elf)
3114
0
    {
3115
0
      while (h->root.type == bfd_link_hash_indirect)
3116
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3117
3118
0
      if (h->root.type != bfd_link_hash_defined
3119
0
    && h->root.type != bfd_link_hash_defweak)
3120
0
  {
3121
0
    h->ref_regular = 1;
3122
0
    h->ref_regular_nonweak = 1;
3123
0
  }
3124
0
      else
3125
0
  {
3126
0
    if (h->root.u.def.section->owner != NULL
3127
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3128
0
      == bfd_target_elf_flavour))
3129
0
      {
3130
0
        h->ref_regular = 1;
3131
0
        h->ref_regular_nonweak = 1;
3132
0
      }
3133
0
    else
3134
0
      h->def_regular = 1;
3135
0
  }
3136
3137
0
      if (h->dynindx == -1
3138
0
    && (h->def_dynamic
3139
0
        || h->ref_dynamic))
3140
0
  {
3141
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3142
0
      {
3143
0
        eif->failed = true;
3144
0
        return false;
3145
0
      }
3146
0
  }
3147
0
    }
3148
0
  else
3149
0
    {
3150
      /* Unfortunately, NON_ELF is only correct if the symbol
3151
   was first seen in a non-ELF file.  Fortunately, if the symbol
3152
   was first seen in an ELF file, we're probably OK unless the
3153
   symbol was defined in a non-ELF file.  Catch that case here.
3154
   FIXME: We're still in trouble if the symbol was first seen in
3155
   a dynamic object, and then later in a non-ELF regular object.  */
3156
0
      if ((h->root.type == bfd_link_hash_defined
3157
0
     || h->root.type == bfd_link_hash_defweak)
3158
0
    && !h->def_regular
3159
0
    && (h->root.u.def.section->owner != NULL
3160
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3161
0
     != bfd_target_elf_flavour)
3162
0
        : (bfd_is_abs_section (h->root.u.def.section)
3163
0
     && !h->def_dynamic)))
3164
0
  h->def_regular = 1;
3165
0
    }
3166
3167
  /* Backend specific symbol fixup.  */
3168
0
  obed = get_elf_backend_data (eif->info->output_bfd);
3169
0
  if (obed->elf_backend_fixup_symbol
3170
0
      && !obed->elf_backend_fixup_symbol (eif->info, h))
3171
0
    return false;
3172
3173
  /* If this is a final link, and the symbol was defined as a common
3174
     symbol in a regular object file, and there was no definition in
3175
     any dynamic object, then the linker will have allocated space for
3176
     the symbol in a common section but the DEF_REGULAR
3177
     flag will not have been set.  */
3178
0
  if (h->root.type == bfd_link_hash_defined
3179
0
      && !h->def_regular
3180
0
      && h->ref_regular
3181
0
      && !h->def_dynamic
3182
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3183
0
    h->def_regular = 1;
3184
3185
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3186
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3187
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3188
3189
  /* If a weak undefined symbol has non-default visibility, we also
3190
     hide it from the dynamic linker.  */
3191
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3192
0
     && h->root.type == bfd_link_hash_undefweak)
3193
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3194
3195
  /* A hidden versioned symbol in executable should be forced local if
3196
     it is is locally defined, not referenced by shared library and not
3197
     exported.  */
3198
0
  else if (bfd_link_executable (eif->info)
3199
0
     && h->versioned == versioned_hidden
3200
0
     && !eif->info->export_dynamic
3201
0
     && !h->dynamic
3202
0
     && !h->ref_dynamic
3203
0
     && h->def_regular)
3204
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3205
3206
  /* If -Bsymbolic was used (which means to bind references to global
3207
     symbols to the definition within the shared object), and this
3208
     symbol was defined in a regular object, then it actually doesn't
3209
     need a PLT entry.  Likewise, if the symbol has non-default
3210
     visibility.  If the symbol has hidden or internal visibility, we
3211
     will force it local.  */
3212
0
  else if (h->needs_plt
3213
0
     && bfd_link_pic (eif->info)
3214
0
     && is_elf_hash_table (eif->info->hash)
3215
0
     && (SYMBOLIC_BIND (eif->info, h)
3216
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3217
0
     && h->def_regular)
3218
0
    {
3219
0
      bool force_local;
3220
3221
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3222
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3223
0
      obed->elf_backend_hide_symbol (eif->info, h, force_local);
3224
0
    }
3225
3226
  /* If this is a weak defined symbol in a dynamic object, and we know
3227
     the real definition in the dynamic object, copy interesting flags
3228
     over to the real definition.  */
3229
0
  if (h->is_weakalias)
3230
0
    {
3231
0
      struct elf_link_hash_entry *def = weakdef (h);
3232
3233
      /* If the real definition is defined by a regular object file,
3234
   don't do anything special.  See the longer description in
3235
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3236
   bfd_link_hash_defined as it was when put on the alias list
3237
   then it must have originally been a versioned symbol (for
3238
   which a non-versioned indirect symbol is created) and later
3239
   a definition for the non-versioned symbol is found.  In that
3240
   case the indirection is flipped with the versioned symbol
3241
   becoming an indirect pointing at the non-versioned symbol.
3242
   Thus, not an alias any more.  */
3243
0
      if (def->def_regular
3244
0
    || def->root.type != bfd_link_hash_defined)
3245
0
  {
3246
0
    h = def;
3247
0
    while ((h = h->u.alias) != def)
3248
0
      h->is_weakalias = 0;
3249
0
  }
3250
0
      else
3251
0
  {
3252
0
    while (h->root.type == bfd_link_hash_indirect)
3253
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3254
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3255
0
          || h->root.type == bfd_link_hash_defweak);
3256
0
    BFD_ASSERT (def->def_dynamic);
3257
0
    obed->elf_backend_copy_indirect_symbol (eif->info, def, h);
3258
0
  }
3259
0
    }
3260
3261
0
  return true;
3262
0
}
3263
3264
/* Make the backend pick a good value for a dynamic symbol.  This is
3265
   called via elf_link_hash_traverse, and also calls itself
3266
   recursively.  */
3267
3268
static bool
3269
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3270
0
{
3271
0
  struct elf_info_failed *eif = data;
3272
0
  struct elf_link_hash_table *htab;
3273
0
  elf_backend_data *obed;
3274
3275
0
  if (! is_elf_hash_table (eif->info->hash))
3276
0
    return false;
3277
3278
0
  htab = elf_hash_table (eif->info);
3279
0
  if (h->forced_local && h->dynindx != -1)
3280
0
    htab->has_local_dynsyms = true;
3281
3282
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3283
0
  if (h->root.type == bfd_link_hash_indirect)
3284
0
    return true;
3285
3286
  /* Fix the symbol flags.  */
3287
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3288
0
    return false;
3289
3290
0
  obed = get_elf_backend_data (htab->dynobj);
3291
3292
0
  if (h->root.type == bfd_link_hash_undefweak)
3293
0
    {
3294
0
      if (eif->info->dynamic_undefined_weak == 0)
3295
0
  obed->elf_backend_hide_symbol (eif->info, h, true);
3296
0
      else if (eif->info->dynamic_undefined_weak > 0
3297
0
         && h->ref_regular
3298
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3299
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3300
0
              h->root.root.string))
3301
0
  {
3302
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3303
0
      {
3304
0
        eif->failed = true;
3305
0
        return false;
3306
0
      }
3307
0
  }
3308
0
    }
3309
3310
  /* If this symbol does not require a PLT entry, and it is not
3311
     defined by a dynamic object, or is not referenced by a regular
3312
     object, ignore it.  We do have to handle a weak defined symbol,
3313
     even if no regular object refers to it, if we decided to add it
3314
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3315
     about symbols which are defined by one dynamic object and
3316
     referenced by another one?  */
3317
0
  if (!h->needs_plt
3318
0
      && h->type != STT_GNU_IFUNC
3319
0
      && (h->def_regular
3320
0
    || !h->def_dynamic
3321
0
    || (!h->ref_regular
3322
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3323
0
    {
3324
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3325
0
      return true;
3326
0
    }
3327
3328
  /* If we've already adjusted this symbol, don't do it again.  This
3329
     can happen via a recursive call.  */
3330
0
  if (h->dynamic_adjusted)
3331
0
    return true;
3332
3333
  /* Don't look at this symbol again.  Note that we must set this
3334
     after checking the above conditions, because we may look at a
3335
     symbol once, decide not to do anything, and then get called
3336
     recursively later after REF_REGULAR is set below.  */
3337
0
  h->dynamic_adjusted = 1;
3338
3339
  /* If this is a weak definition, and we know a real definition, and
3340
     the real symbol is not itself defined by a regular object file,
3341
     then get a good value for the real definition.  We handle the
3342
     real symbol first, for the convenience of the backend routine.
3343
3344
     Note that there is a confusing case here.  If the real definition
3345
     is defined by a regular object file, we don't get the real symbol
3346
     from the dynamic object, but we do get the weak symbol.  If the
3347
     processor backend uses a COPY reloc, then if some routine in the
3348
     dynamic object changes the real symbol, we will not see that
3349
     change in the corresponding weak symbol.  This is the way other
3350
     ELF linkers work as well, and seems to be a result of the shared
3351
     library model.
3352
3353
     I will clarify this issue.  Most SVR4 shared libraries define the
3354
     variable _timezone and define timezone as a weak synonym.  The
3355
     tzset call changes _timezone.  If you write
3356
       extern int timezone;
3357
       int _timezone = 5;
3358
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3359
     you might expect that, since timezone is a synonym for _timezone,
3360
     the same number will print both times.  However, if the processor
3361
     backend uses a COPY reloc, then actually timezone will be copied
3362
     into your process image, and, since you define _timezone
3363
     yourself, _timezone will not.  Thus timezone and _timezone will
3364
     wind up at different memory locations.  The tzset call will set
3365
     _timezone, leaving timezone unchanged.  */
3366
3367
0
  if (h->is_weakalias)
3368
0
    {
3369
0
      struct elf_link_hash_entry *def = weakdef (h);
3370
3371
      /* If we get to this point, there is an implicit reference to
3372
   the alias by a regular object file via the weak symbol H.  */
3373
0
      def->ref_regular = 1;
3374
3375
      /* Ensure that the backend adjust_dynamic_symbol function sees
3376
   the strong alias before H by recursively calling ourselves.  */
3377
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3378
0
  return false;
3379
0
    }
3380
3381
  /* If a symbol has no type and no size and does not require a PLT
3382
     entry, then we are probably about to do the wrong thing here: we
3383
     are probably going to create a COPY reloc for an empty object.
3384
     This case can arise when a shared object is built with assembly
3385
     code, and the assembly code fails to set the symbol type.  */
3386
0
  if (h->size == 0
3387
0
      && h->type == STT_NOTYPE
3388
0
      && !h->needs_plt)
3389
0
    _bfd_error_handler
3390
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3391
0
       h->root.root.string);
3392
3393
0
  if (!obed->elf_backend_adjust_dynamic_symbol (eif->info, h))
3394
0
    {
3395
0
      eif->failed = true;
3396
0
      return false;
3397
0
    }
3398
3399
0
  return true;
3400
0
}
3401
3402
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3403
   DYNBSS.  */
3404
3405
bool
3406
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3407
            struct elf_link_hash_entry *h,
3408
            asection *dynbss)
3409
0
{
3410
0
  unsigned int power_of_two;
3411
0
  bfd_vma mask;
3412
0
  asection *sec = h->root.u.def.section;
3413
3414
  /* The section alignment of the definition is the maximum alignment
3415
     requirement of symbols defined in the section.  Since we don't
3416
     know the symbol alignment requirement, we start with the
3417
     maximum alignment and check low bits of the symbol address
3418
     for the minimum alignment.  */
3419
0
  power_of_two = bfd_section_alignment (sec);
3420
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3421
0
  while ((h->root.u.def.value & mask) != 0)
3422
0
    {
3423
0
       mask >>= 1;
3424
0
       --power_of_two;
3425
0
    }
3426
3427
  /* Adjust the section alignment if needed.  */
3428
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3429
0
    return false;
3430
3431
  /* We make sure that the symbol will be aligned properly.  */
3432
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3433
3434
  /* Define the symbol as being at this point in DYNBSS.  */
3435
0
  h->root.u.def.section = dynbss;
3436
0
  h->root.u.def.value = dynbss->size;
3437
3438
  /* Increment the size of DYNBSS to make room for the symbol.  */
3439
0
  dynbss->size += h->size;
3440
3441
  /* No error if extern_protected_data is true.  */
3442
0
  if (h->protected_def
3443
0
      && (!info->extern_protected_data
3444
0
    || (info->extern_protected_data < 0
3445
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3446
0
    info->callbacks->einfo
3447
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3448
0
       h->root.root.string);
3449
3450
0
  return true;
3451
0
}
3452
3453
/* Adjust all external symbols pointing into SEC_MERGE sections
3454
   to reflect the object merging within the sections.  */
3455
3456
static bool
3457
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3458
0
{
3459
0
  asection *sec;
3460
3461
0
  if ((h->root.type == bfd_link_hash_defined
3462
0
       || h->root.type == bfd_link_hash_defweak)
3463
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3464
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3465
0
    {
3466
0
      bfd *output_bfd = data;
3467
3468
0
      h->root.u.def.value =
3469
0
  _bfd_merged_section_offset (output_bfd,
3470
0
            &h->root.u.def.section,
3471
0
            h->root.u.def.value);
3472
0
    }
3473
3474
0
  return true;
3475
0
}
3476
3477
/* Returns false if the symbol referred to by H should be considered
3478
   to resolve local to the current module, and true if it should be
3479
   considered to bind dynamically.  */
3480
3481
bool
3482
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3483
         struct bfd_link_info *info,
3484
         bool not_local_protected)
3485
0
{
3486
0
  bool binding_stays_local_p;
3487
0
  elf_backend_data *obed;
3488
0
  struct elf_link_hash_table *hash_table;
3489
3490
0
  if (h == NULL)
3491
0
    return false;
3492
3493
0
  while (h->root.type == bfd_link_hash_indirect
3494
0
   || h->root.type == bfd_link_hash_warning)
3495
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3496
3497
  /* If it was forced local, then clearly it's not dynamic.  */
3498
0
  if (h->dynindx == -1)
3499
0
    return false;
3500
0
  if (h->forced_local)
3501
0
    return false;
3502
3503
  /* Identify the cases where name binding rules say that a
3504
     visible symbol resolves locally.  */
3505
0
  binding_stays_local_p = (bfd_link_executable (info)
3506
0
         || SYMBOLIC_BIND (info, h));
3507
3508
0
  switch (ELF_ST_VISIBILITY (h->other))
3509
0
    {
3510
0
    case STV_INTERNAL:
3511
0
    case STV_HIDDEN:
3512
0
      return false;
3513
3514
0
    case STV_PROTECTED:
3515
0
      hash_table = elf_hash_table (info);
3516
0
      if (!is_elf_hash_table (&hash_table->root))
3517
0
  return false;
3518
3519
0
      obed = get_elf_backend_data (hash_table->dynobj);
3520
3521
      /* Proper resolution for function pointer equality may require
3522
   that these symbols perhaps be resolved dynamically, even though
3523
   we should be resolving them to the current module.  */
3524
0
      if (!not_local_protected || !obed->is_function_type (h->type))
3525
0
  binding_stays_local_p = true;
3526
0
      break;
3527
3528
0
    default:
3529
0
      break;
3530
0
    }
3531
3532
  /* If it isn't defined locally, then clearly it's dynamic.  */
3533
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3534
0
    return true;
3535
3536
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3537
     us that it remains local.  */
3538
0
  return !binding_stays_local_p;
3539
0
}
3540
3541
/* Return true if the symbol referred to by H should be considered
3542
   to resolve local to the current module, and false otherwise.  Differs
3543
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3544
   undefined symbols.  The two functions are virtually identical except
3545
   for the place where dynindx == -1 is tested.  If that test is true,
3546
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3547
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3548
   defined symbols.
3549
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3550
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3551
   treatment of undefined weak symbols.  For those that do not make
3552
   undefined weak symbols dynamic, both functions may return false.  */
3553
3554
bool
3555
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3556
            struct bfd_link_info *info,
3557
            bool local_protected)
3558
0
{
3559
0
  elf_backend_data *obed;
3560
0
  struct elf_link_hash_table *hash_table;
3561
3562
  /* If it's a local sym, of course we resolve locally.  */
3563
0
  if (h == NULL)
3564
0
    return true;
3565
3566
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3567
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3568
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3569
0
    return true;
3570
3571
  /* Forced local symbols resolve locally.  */
3572
0
  if (h->forced_local)
3573
0
    return true;
3574
3575
  /* Common symbols that become definitions don't get the DEF_REGULAR
3576
     flag set, so test it first, and don't bail out.  */
3577
0
  if (ELF_COMMON_DEF_P (h))
3578
0
    /* Do nothing.  */;
3579
  /* If we don't have a definition in a regular file, then we can't
3580
     resolve locally.  The sym is either undefined or dynamic.  */
3581
0
  else if (!h->def_regular)
3582
0
    return false;
3583
3584
  /* Non-dynamic symbols resolve locally.  */
3585
0
  if (h->dynindx == -1)
3586
0
    return true;
3587
3588
  /* At this point, we know the symbol is defined and dynamic.  In an
3589
     executable it must resolve locally, likewise when building symbolic
3590
     shared libraries.  */
3591
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3592
0
    return true;
3593
3594
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3595
     with default visibility might not resolve locally.  */
3596
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3597
0
    return false;
3598
3599
0
  hash_table = elf_hash_table (info);
3600
0
  if (!is_elf_hash_table (&hash_table->root))
3601
0
    return true;
3602
3603
  /* STV_PROTECTED symbols with indirect external access are local. */
3604
0
  if (info->indirect_extern_access > 0)
3605
0
    return true;
3606
3607
0
  obed = get_elf_backend_data (hash_table->dynobj);
3608
3609
  /* If extern_protected_data is false, STV_PROTECTED non-function
3610
     symbols are local.  */
3611
0
  if ((!info->extern_protected_data
3612
0
       || (info->extern_protected_data < 0
3613
0
     && !obed->extern_protected_data))
3614
0
      && !obed->is_function_type (h->type))
3615
0
    return true;
3616
3617
  /* Function pointer equality tests may require that STV_PROTECTED
3618
     symbols be treated as dynamic symbols.  If the address of a
3619
     function not defined in an executable is set to that function's
3620
     plt entry in the executable, then the address of the function in
3621
     a shared library must also be the plt entry in the executable.  */
3622
0
  return local_protected;
3623
0
}
3624
3625
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3626
   aligned.  Returns the first TLS output section.  */
3627
3628
struct bfd_section *
3629
bfd_elf_tls_setup (struct bfd_link_info *info)
3630
0
{
3631
0
  struct bfd_section *sec, *tls;
3632
0
  unsigned int align = 0;
3633
3634
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
3635
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3636
0
      break;
3637
0
  tls = sec;
3638
3639
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3640
0
    if (sec->alignment_power > align)
3641
0
      align = sec->alignment_power;
3642
3643
0
  elf_hash_table (info)->tls_sec = tls;
3644
3645
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3646
     alignment, so that the tls segment starts aligned.  */
3647
0
  if (tls != NULL)
3648
0
    (void) bfd_link_align_section (tls, align);
3649
3650
0
  return tls;
3651
0
}
3652
3653
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3654
static bool
3655
is_global_data_symbol_definition (bfd *abfd,
3656
          Elf_Internal_Sym *sym)
3657
0
{
3658
0
  elf_backend_data *bed;
3659
3660
  /* Local symbols do not count, but target specific ones might.  */
3661
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3662
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3663
0
    return false;
3664
3665
0
  bed = get_elf_backend_data (abfd);
3666
  /* Function symbols do not count.  */
3667
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3668
0
    return false;
3669
3670
  /* If the section is undefined, then so is the symbol.  */
3671
0
  if (sym->st_shndx == SHN_UNDEF)
3672
0
    return false;
3673
3674
  /* If the symbol is defined in the common section, then
3675
     it is a common definition and so does not count.  */
3676
0
  if (bed->common_definition (sym))
3677
0
    return false;
3678
3679
  /* If the symbol is in a target specific section then we
3680
     must rely upon the backend to tell us what it is.  */
3681
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3682
    /* FIXME - this function is not coded yet:
3683
3684
       return _bfd_is_global_symbol_definition (abfd, sym);
3685
3686
       Instead for now assume that the definition is not global,
3687
       Even if this is wrong, at least the linker will behave
3688
       in the same way that it used to do.  */
3689
0
    return false;
3690
3691
0
  return true;
3692
0
}
3693
3694
/* Search the symbol table of the archive element of the archive ABFD
3695
   whose archive map contains a mention of SYMDEF, and determine if
3696
   the symbol is defined in this element.  */
3697
static bool
3698
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3699
0
{
3700
0
  Elf_Internal_Shdr * hdr;
3701
0
  size_t symcount;
3702
0
  size_t extsymcount;
3703
0
  size_t extsymoff;
3704
0
  Elf_Internal_Sym *isymbuf;
3705
0
  Elf_Internal_Sym *isym;
3706
0
  Elf_Internal_Sym *isymend;
3707
0
  bool result;
3708
3709
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3710
0
  if (abfd == NULL)
3711
0
    return false;
3712
3713
0
  if (! bfd_check_format (abfd, bfd_object))
3714
0
    return false;
3715
3716
  /* Select the appropriate symbol table.  If we don't know if the
3717
     object file is an IR object, give linker LTO plugin a chance to
3718
     get the correct symbol table.  */
3719
0
  if (abfd->plugin_format == bfd_plugin_yes
3720
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3721
0
      || (abfd->plugin_format == bfd_plugin_unknown
3722
0
    && bfd_link_plugin_object_p (abfd)))
3723
0
    {
3724
      /* Use the IR symbol table if the object has been claimed by
3725
   plugin.  */
3726
0
      abfd = abfd->plugin_dummy_bfd;
3727
0
      hdr = &elf_symtab_hdr (abfd);
3728
0
    }
3729
0
  else
3730
0
    {
3731
0
      if (elf_use_dt_symtab_p (abfd))
3732
0
  {
3733
0
    bfd_set_error (bfd_error_wrong_format);
3734
0
    return false;
3735
0
  }
3736
3737
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3738
0
  hdr = &elf_symtab_hdr (abfd);
3739
0
      else
3740
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3741
0
    }
3742
3743
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3744
3745
  /* The sh_info field of the symtab header tells us where the
3746
     external symbols start.  We don't care about the local symbols.  */
3747
0
  if (elf_bad_symtab (abfd))
3748
0
    {
3749
0
      extsymcount = symcount;
3750
0
      extsymoff = 0;
3751
0
    }
3752
0
  else
3753
0
    {
3754
0
      extsymcount = symcount - hdr->sh_info;
3755
0
      extsymoff = hdr->sh_info;
3756
0
    }
3757
3758
0
  if (extsymcount == 0)
3759
0
    return false;
3760
3761
  /* Read in the symbol table.  */
3762
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3763
0
          NULL, NULL, NULL);
3764
0
  if (isymbuf == NULL)
3765
0
    return false;
3766
3767
  /* Scan the symbol table looking for SYMDEF.  */
3768
0
  result = false;
3769
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3770
0
    {
3771
0
      const char *name;
3772
3773
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3774
0
                isym->st_name);
3775
0
      if (name == NULL)
3776
0
  break;
3777
3778
0
      if (strcmp (name, symdef->name) == 0)
3779
0
  {
3780
0
    result = is_global_data_symbol_definition (abfd, isym);
3781
0
    break;
3782
0
  }
3783
0
    }
3784
3785
0
  free (isymbuf);
3786
3787
0
  return result;
3788
0
}
3789

3790
/* Add an entry to the .dynamic table.  */
3791
3792
bool
3793
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3794
          bfd_vma tag,
3795
          bfd_vma val)
3796
0
{
3797
0
  struct elf_link_hash_table *hash_table;
3798
0
  elf_backend_data *obed;
3799
0
  asection *s;
3800
0
  bfd_size_type newsize;
3801
0
  bfd_byte *newcontents;
3802
0
  Elf_Internal_Dyn dyn;
3803
3804
0
  hash_table = elf_hash_table (info);
3805
0
  if (! is_elf_hash_table (&hash_table->root))
3806
0
    return false;
3807
3808
0
  if (tag == DT_RELA || tag == DT_REL)
3809
0
    hash_table->dynamic_relocs = true;
3810
3811
0
  obed = get_elf_backend_data (hash_table->dynobj);
3812
0
  s = hash_table->dynamic;
3813
0
  BFD_ASSERT (s != NULL);
3814
3815
0
  newsize = s->size + obed->s->sizeof_dyn;
3816
0
  newcontents = bfd_realloc (s->contents, newsize);
3817
0
  if (newcontents == NULL)
3818
0
    return false;
3819
3820
0
  dyn.d_tag = tag;
3821
0
  dyn.d_un.d_val = val;
3822
0
  obed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3823
3824
0
  s->size = newsize;
3825
0
  s->contents = newcontents;
3826
3827
0
  return true;
3828
0
}
3829
3830
/* Strip zero-sized dynamic sections.  */
3831
3832
bool
3833
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3834
0
{
3835
0
  struct elf_link_hash_table *hash_table;
3836
0
  elf_backend_data *obed;
3837
0
  asection *s, *sdynamic, **pp;
3838
0
  asection *rela_dyn, *rel_dyn;
3839
0
  Elf_Internal_Dyn dyn;
3840
0
  bfd_byte *extdyn, *next;
3841
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3842
0
  bool strip_zero_sized;
3843
0
  bool strip_zero_sized_plt;
3844
3845
0
  if (bfd_link_relocatable (info))
3846
0
    return true;
3847
3848
0
  hash_table = elf_hash_table (info);
3849
0
  if (!is_elf_hash_table (&hash_table->root))
3850
0
    return false;
3851
3852
0
  if (!hash_table->dynobj)
3853
0
    return true;
3854
3855
0
  sdynamic= hash_table->dynamic;
3856
0
  if (!sdynamic)
3857
0
    return true;
3858
3859
0
  obed = get_elf_backend_data (hash_table->dynobj);
3860
0
  swap_dyn_in = obed->s->swap_dyn_in;
3861
3862
0
  strip_zero_sized = false;
3863
0
  strip_zero_sized_plt = false;
3864
3865
  /* Strip zero-sized dynamic sections.  */
3866
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3867
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3868
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3869
0
    if (s->size == 0
3870
0
  && (s == rela_dyn
3871
0
      || s == rel_dyn
3872
0
      || s == hash_table->srelplt->output_section
3873
0
      || s == hash_table->splt->output_section))
3874
0
      {
3875
0
  *pp = s->next;
3876
0
  info->output_bfd->section_count--;
3877
0
  strip_zero_sized = true;
3878
0
  if (s == rela_dyn)
3879
0
    s = rela_dyn;
3880
0
  if (s == rel_dyn)
3881
0
    s = rel_dyn;
3882
0
  else if (s == hash_table->splt->output_section)
3883
0
    {
3884
0
      s = hash_table->splt;
3885
0
      strip_zero_sized_plt = true;
3886
0
    }
3887
0
  else
3888
0
    s = hash_table->srelplt;
3889
0
  s->flags |= SEC_EXCLUDE;
3890
0
  s->output_section = bfd_abs_section_ptr;
3891
0
      }
3892
0
    else
3893
0
      pp = &s->next;
3894
3895
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3896
0
    for (extdyn = sdynamic->contents;
3897
0
   extdyn < sdynamic->contents + sdynamic->size;
3898
0
   extdyn = next)
3899
0
      {
3900
0
  next = extdyn + obed->s->sizeof_dyn;
3901
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3902
0
  switch (dyn.d_tag)
3903
0
    {
3904
0
    default:
3905
0
      break;
3906
0
    case DT_JMPREL:
3907
0
    case DT_PLTRELSZ:
3908
0
    case DT_PLTREL:
3909
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3910
         the procedure linkage table (the .plt section) has been
3911
         removed.  */
3912
0
      memmove (extdyn, next,
3913
0
         sdynamic->size - (next - sdynamic->contents));
3914
0
      next = extdyn;
3915
0
    }
3916
0
      }
3917
3918
0
  if (strip_zero_sized)
3919
0
    {
3920
      /* Regenerate program headers.  */
3921
0
      elf_seg_map (info->output_bfd) = NULL;
3922
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3923
0
    }
3924
3925
0
  return true;
3926
0
}
3927
3928
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3929
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3930
3931
int
3932
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3933
0
{
3934
0
  struct elf_link_hash_table *hash_table;
3935
0
  size_t strindex;
3936
0
  const char *soname;
3937
3938
0
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3939
0
    return -1;
3940
3941
0
  hash_table = elf_hash_table (info);
3942
0
  soname = elf_dt_name (abfd);
3943
0
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3944
0
  if (strindex == (size_t) -1)
3945
0
    return -1;
3946
3947
0
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3948
0
    {
3949
0
      asection *sdyn;
3950
0
      elf_backend_data *obed;
3951
0
      bfd_byte *extdyn;
3952
3953
0
      obed = get_elf_backend_data (hash_table->dynobj);
3954
0
      sdyn = hash_table->dynamic;
3955
0
      if (sdyn != NULL && sdyn->size != 0)
3956
0
  for (extdyn = sdyn->contents;
3957
0
       extdyn < sdyn->contents + sdyn->size;
3958
0
       extdyn += obed->s->sizeof_dyn)
3959
0
    {
3960
0
      Elf_Internal_Dyn dyn;
3961
3962
0
      obed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3963
0
      if (dyn.d_tag == DT_NEEDED
3964
0
    && dyn.d_un.d_val == strindex)
3965
0
        {
3966
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3967
0
    return 1;
3968
0
        }
3969
0
    }
3970
0
    }
3971
3972
0
  if (!bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3973
0
    return -1;
3974
3975
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3976
0
    return -1;
3977
3978
0
  return 0;
3979
0
}
3980
3981
/* Return true if SONAME is on the needed list between NEEDED and STOP
3982
   (or the end of list if STOP is NULL), and needed by a library that
3983
   will be loaded.  */
3984
3985
static bool
3986
on_needed_list (const char *soname,
3987
    struct bfd_link_needed_list *needed,
3988
    struct bfd_link_needed_list *stop)
3989
0
{
3990
0
  struct bfd_link_needed_list *look;
3991
0
  for (look = needed; look != stop; look = look->next)
3992
0
    if (strcmp (soname, look->name) == 0
3993
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3994
      /* If needed by a library that itself is not directly
3995
         needed, recursively check whether that library is
3996
         indirectly needed.  Since we add DT_NEEDED entries to
3997
         the end of the list, library dependencies appear after
3998
         the library.  Therefore search prior to the current
3999
         LOOK, preventing possible infinite recursion.  */
4000
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4001
0
      return true;
4002
4003
0
  return false;
4004
0
}
4005
4006
/* Sort symbol by value, section, size, and type.  */
4007
static int
4008
elf_sort_symbol (const void *arg1, const void *arg2)
4009
0
{
4010
0
  const struct elf_link_hash_entry *h1;
4011
0
  const struct elf_link_hash_entry *h2;
4012
0
  bfd_signed_vma vdiff;
4013
0
  int sdiff;
4014
0
  const char *n1;
4015
0
  const char *n2;
4016
4017
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4018
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4019
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4020
0
  if (vdiff != 0)
4021
0
    return vdiff > 0 ? 1 : -1;
4022
4023
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4024
0
  if (sdiff != 0)
4025
0
    return sdiff;
4026
4027
  /* Sort so that sized symbols are selected over zero size symbols.  */
4028
0
  vdiff = h1->size - h2->size;
4029
0
  if (vdiff != 0)
4030
0
    return vdiff > 0 ? 1 : -1;
4031
4032
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4033
0
  if (h1->type != h2->type)
4034
0
    return h1->type - h2->type;
4035
4036
  /* If symbols are properly sized and typed, and multiple strong
4037
     aliases are not defined in a shared library by the user we
4038
     shouldn't get here.  Unfortunately linker script symbols like
4039
     __bss_start sometimes match a user symbol defined at the start of
4040
     .bss without proper size and type.  We'd like to preference the
4041
     user symbol over reserved system symbols.  Sort on leading
4042
     underscores.  */
4043
0
  n1 = h1->root.root.string;
4044
0
  n2 = h2->root.root.string;
4045
0
  while (*n1 == *n2)
4046
0
    {
4047
0
      if (*n1 == 0)
4048
0
  break;
4049
0
      ++n1;
4050
0
      ++n2;
4051
0
    }
4052
0
  if (*n1 == '_')
4053
0
    return -1;
4054
0
  if (*n2 == '_')
4055
0
    return 1;
4056
4057
  /* Final sort on name selects user symbols like '_u' over reserved
4058
     system symbols like '_Z' and also will avoid qsort instability.  */
4059
0
  return *n1 - *n2;
4060
0
}
4061
4062
/* This function is used to adjust offsets into .dynstr for
4063
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4064
4065
static bool
4066
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4067
0
{
4068
0
  struct elf_strtab_hash *dynstr = data;
4069
4070
0
  if (h->dynindx != -1)
4071
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4072
0
  return true;
4073
0
}
4074
4075
/* Assign string offsets in .dynstr, update all structures referencing
4076
   them.  */
4077
4078
static bool
4079
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4080
0
{
4081
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4082
0
  struct elf_link_local_dynamic_entry *entry;
4083
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4084
0
  bfd *dynobj = hash_table->dynobj;
4085
0
  asection *sdyn;
4086
0
  bfd_size_type size;
4087
0
  elf_backend_data *obed;
4088
0
  bfd_byte *extdyn;
4089
4090
0
  _bfd_elf_strtab_finalize (dynstr);
4091
0
  size = _bfd_elf_strtab_size (dynstr);
4092
4093
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4094
4095
0
  if (info->callbacks->examine_strtab)
4096
0
    info->callbacks->examine_strtab (dynstr);
4097
4098
0
  obed = get_elf_backend_data (dynobj);
4099
0
  sdyn = hash_table->dynamic;
4100
0
  BFD_ASSERT (sdyn != NULL);
4101
4102
  /* Update all .dynamic entries referencing .dynstr strings.  */
4103
0
  for (extdyn = sdyn->contents;
4104
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4105
0
       extdyn += obed->s->sizeof_dyn)
4106
0
    {
4107
0
      Elf_Internal_Dyn dyn;
4108
4109
0
      obed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4110
0
      switch (dyn.d_tag)
4111
0
  {
4112
0
  case DT_STRSZ:
4113
0
    dyn.d_un.d_val = size;
4114
0
    break;
4115
0
  case DT_NEEDED:
4116
0
  case DT_SONAME:
4117
0
  case DT_RPATH:
4118
0
  case DT_RUNPATH:
4119
0
  case DT_FILTER:
4120
0
  case DT_AUXILIARY:
4121
0
  case DT_AUDIT:
4122
0
  case DT_DEPAUDIT:
4123
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4124
0
    break;
4125
0
  default:
4126
0
    continue;
4127
0
  }
4128
0
      obed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4129
0
    }
4130
4131
  /* Now update local dynamic symbols.  */
4132
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4133
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4134
0
              entry->isym.st_name);
4135
4136
  /* And the rest of dynamic symbols.  */
4137
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4138
4139
  /* Adjust version definitions.  */
4140
0
  if (elf_tdata (output_bfd)->cverdefs)
4141
0
    {
4142
0
      asection *s;
4143
0
      bfd_byte *p;
4144
0
      size_t i;
4145
0
      Elf_Internal_Verdef def;
4146
0
      Elf_Internal_Verdaux defaux;
4147
4148
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4149
0
      p = s->contents;
4150
0
      do
4151
0
  {
4152
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4153
0
           &def);
4154
0
    p += sizeof (Elf_External_Verdef);
4155
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4156
0
      continue;
4157
0
    for (i = 0; i < def.vd_cnt; ++i)
4158
0
      {
4159
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4160
0
          (Elf_External_Verdaux *) p, &defaux);
4161
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4162
0
              defaux.vda_name);
4163
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4164
0
           &defaux, (Elf_External_Verdaux *) p);
4165
0
        p += sizeof (Elf_External_Verdaux);
4166
0
      }
4167
0
  }
4168
0
      while (def.vd_next);
4169
0
    }
4170
4171
  /* Adjust version references.  */
4172
0
  if (elf_tdata (output_bfd)->verref)
4173
0
    {
4174
0
      asection *s;
4175
0
      bfd_byte *p;
4176
0
      size_t i;
4177
0
      Elf_Internal_Verneed need;
4178
0
      Elf_Internal_Vernaux needaux;
4179
4180
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4181
0
      p = s->contents;
4182
0
      do
4183
0
  {
4184
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4185
0
            &need);
4186
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4187
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4188
0
             (Elf_External_Verneed *) p);
4189
0
    p += sizeof (Elf_External_Verneed);
4190
0
    for (i = 0; i < need.vn_cnt; ++i)
4191
0
      {
4192
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4193
0
          (Elf_External_Vernaux *) p, &needaux);
4194
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4195
0
               needaux.vna_name);
4196
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4197
0
           &needaux,
4198
0
           (Elf_External_Vernaux *) p);
4199
0
        p += sizeof (Elf_External_Vernaux);
4200
0
      }
4201
0
  }
4202
0
      while (need.vn_next);
4203
0
    }
4204
4205
0
  return true;
4206
0
}
4207

4208
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4209
   The default is to only match when the INPUT and OUTPUT are exactly
4210
   the same target.  */
4211
4212
bool
4213
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4214
            const bfd_target *output)
4215
0
{
4216
0
  return input == output;
4217
0
}
4218
4219
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4220
   This version is used when different targets for the same architecture
4221
   are virtually identical.  */
4222
4223
bool
4224
_bfd_elf_relocs_compatible (const bfd_target *input,
4225
          const bfd_target *output)
4226
0
{
4227
0
  elf_backend_data *obed, *ibed;
4228
4229
0
  if (input == output)
4230
0
    return true;
4231
4232
0
  ibed = xvec_get_elf_backend_data (input);
4233
0
  obed = xvec_get_elf_backend_data (output);
4234
4235
0
  if (ibed->arch != obed->arch)
4236
0
    return false;
4237
4238
  /* If both backends are using this function, deem them compatible.  */
4239
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4240
0
}
4241
4242
/* Make a special call to the linker "notice" function to tell it that
4243
   we are about to handle an as-needed lib, or have finished
4244
   processing the lib.  */
4245
4246
bool
4247
_bfd_elf_notice_as_needed (bfd *ibfd,
4248
         struct bfd_link_info *info,
4249
         enum notice_asneeded_action act)
4250
0
{
4251
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4252
0
}
4253
4254
/* Call ACTION on each relocation in an ELF object file.  */
4255
4256
bool
4257
_bfd_elf_link_iterate_on_relocs
4258
  (bfd *abfd, struct bfd_link_info *info,
4259
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4260
       const Elf_Internal_Rela *))
4261
0
{
4262
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4263
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4264
4265
  /* If this object is the same format as the output object, and it is
4266
     not a shared library, then let the backend look through the
4267
     relocs.
4268
4269
     This is required to build global offset table entries and to
4270
     arrange for dynamic relocs.  It is not required for the
4271
     particular common case of linking non PIC code, even when linking
4272
     against shared libraries, but unfortunately there is no way of
4273
     knowing whether an object file has been compiled PIC or not.
4274
     Looking through the relocs is not particularly time consuming.
4275
     The problem is that we must either (1) keep the relocs in memory,
4276
     which causes the linker to require additional runtime memory or
4277
     (2) read the relocs twice from the input file, which wastes time.
4278
     This would be a good case for using mmap.
4279
4280
     I have no idea how to handle linking PIC code into a file of a
4281
     different format.  It probably can't be done.  */
4282
0
  if ((abfd->flags & DYNAMIC) == 0
4283
0
      && is_elf_hash_table (&htab->root)
4284
0
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4285
0
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4286
0
    {
4287
0
      asection *o;
4288
4289
0
      for (o = abfd->sections; o != NULL; o = o->next)
4290
0
  {
4291
0
    Elf_Internal_Rela *internal_relocs;
4292
0
    bool ok;
4293
4294
    /* Don't check relocations in excluded sections.  Don't do
4295
       anything special with non-loaded, non-alloced sections.
4296
       In particular, any relocs in such sections should not
4297
       affect GOT and PLT reference counting (ie.  we don't
4298
       allow them to create GOT or PLT entries), there's no
4299
       possibility or desire to optimize TLS relocs, and
4300
       there's not much point in propagating relocs to shared
4301
       libs that the dynamic linker won't relocate.  */
4302
0
    if ((o->flags & SEC_ALLOC) == 0
4303
0
        || (o->flags & SEC_RELOC) == 0
4304
0
        || (o->flags & SEC_EXCLUDE) != 0
4305
0
        || o->reloc_count == 0
4306
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4307
0
      && (o->flags & SEC_DEBUGGING) != 0)
4308
0
        || bfd_is_abs_section (o->output_section))
4309
0
      continue;
4310
4311
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4312
0
      (abfd, info, o, NULL, NULL,
4313
0
       _bfd_elf_link_keep_memory (info));
4314
0
    if (internal_relocs == NULL)
4315
0
      return false;
4316
4317
0
    ok = action (abfd, info, o, internal_relocs);
4318
4319
0
    if (elf_section_data (o)->relocs != internal_relocs)
4320
0
      free (internal_relocs);
4321
4322
0
    if (! ok)
4323
0
      return false;
4324
0
  }
4325
0
    }
4326
4327
0
  return true;
4328
0
}
4329
4330
/* Check relocations in an ELF object file.  This is called after
4331
   all input files have been opened.  */
4332
4333
bool
4334
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4335
0
{
4336
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4337
0
  if (bed->check_relocs != NULL)
4338
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4339
0
              bed->check_relocs);
4340
0
  return true;
4341
0
}
4342
4343
/* An entry in the first definition hash table.  */
4344
4345
struct elf_link_first_hash_entry
4346
{
4347
  struct bfd_hash_entry root;
4348
  /* The object of the first definition.  */
4349
  bfd *abfd;
4350
};
4351
4352
/* The function to create a new entry in the first definition hash
4353
   table.  */
4354
4355
static struct bfd_hash_entry *
4356
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4357
           struct bfd_hash_table *table,
4358
           const char *string)
4359
0
{
4360
0
  struct elf_link_first_hash_entry *ret =
4361
0
    (struct elf_link_first_hash_entry *) entry;
4362
4363
  /* Allocate the structure if it has not already been allocated by a
4364
     subclass.  */
4365
0
  if (ret == NULL)
4366
0
    ret = bfd_hash_allocate (table, sizeof (struct elf_link_first_hash_entry));
4367
0
  if (ret == NULL)
4368
0
    return NULL;
4369
4370
  /* Call the allocation method of the superclass.  */
4371
0
  ret = ((struct elf_link_first_hash_entry *)
4372
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4373
0
         string));
4374
0
  if (ret != NULL)
4375
0
    ret->abfd = NULL;
4376
4377
0
  return (struct bfd_hash_entry *) ret;
4378
0
}
4379
4380
/* Add the symbol NAME from ABFD to first hash.  */
4381
4382
static void
4383
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4384
          const char *name, bool copy)
4385
0
{
4386
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4387
  /* Skip if there is no first hash.  */
4388
0
  if (htab->first_hash == NULL)
4389
0
    return;
4390
4391
0
  struct elf_link_first_hash_entry *e
4392
0
    = ((struct elf_link_first_hash_entry *)
4393
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4394
0
  if (e == NULL)
4395
0
    info->callbacks->fatal
4396
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4397
4398
0
  if (e->abfd == NULL)
4399
    /* Store ABFD in abfd.  */
4400
0
    e->abfd = abfd;
4401
0
}
4402
4403
/* Add symbols from an ELF object file to the linker hash table.  */
4404
4405
static bool
4406
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4407
0
{
4408
0
  Elf_Internal_Ehdr *ehdr;
4409
0
  Elf_Internal_Shdr *hdr;
4410
0
  size_t symcount;
4411
0
  size_t extsymcount;
4412
0
  size_t extsymoff;
4413
0
  struct elf_link_hash_entry **sym_hash;
4414
0
  bool dynamic;
4415
0
  Elf_External_Versym *extversym = NULL;
4416
0
  Elf_External_Versym *extversym_end = NULL;
4417
0
  Elf_External_Versym *ever;
4418
0
  struct elf_link_hash_entry *weaks;
4419
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4420
0
  size_t nondeflt_vers_cnt = 0;
4421
0
  Elf_Internal_Sym *isymbuf = NULL;
4422
0
  Elf_Internal_Sym *isym;
4423
0
  Elf_Internal_Sym *isymend;
4424
0
  elf_backend_data *bed, *obed;
4425
0
  bool add_needed;
4426
0
  struct elf_link_hash_table *htab;
4427
0
  void *alloc_mark = NULL;
4428
0
  struct bfd_hash_entry **old_table = NULL;
4429
0
  unsigned int old_size = 0;
4430
0
  unsigned int old_count = 0;
4431
0
  void *old_tab = NULL;
4432
0
  void *old_ent;
4433
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4434
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4435
0
  void *old_strtab = NULL;
4436
0
  size_t tabsize = 0;
4437
0
  asection *s;
4438
0
  bool just_syms;
4439
4440
0
  htab = elf_hash_table (info);
4441
0
  bed = get_elf_backend_data (abfd);
4442
0
  obed = get_elf_backend_data (info->output_bfd);
4443
4444
0
  if (elf_use_dt_symtab_p (abfd))
4445
0
    {
4446
0
      bfd_set_error (bfd_error_wrong_format);
4447
0
      return false;
4448
0
    }
4449
4450
0
  if ((abfd->flags & DYNAMIC) == 0)
4451
0
    {
4452
0
      dynamic = false;
4453
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4454
0
    && is_elf_hash_table (&htab->root)
4455
0
    && htab->first_hash == NULL)
4456
0
  {
4457
    /* Initialize first_hash for an IR input.  */
4458
0
    htab->first_hash = bfd_malloc (sizeof (struct bfd_hash_table));
4459
0
    if (htab->first_hash == NULL
4460
0
        || !bfd_hash_table_init
4461
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4462
0
        sizeof (struct elf_link_first_hash_entry)))
4463
0
      info->callbacks->fatal
4464
0
        (_("%P: first_hash failed to create: %E\n"));
4465
0
  }
4466
0
    }
4467
0
  else
4468
0
    {
4469
0
      dynamic = true;
4470
4471
      /* You can't use -r against a dynamic object.  Also, there's no
4472
   hope of using a dynamic object which does not exactly match
4473
   the format of the output file.  */
4474
0
      if (bfd_link_relocatable (info)
4475
0
    || !is_elf_hash_table (&htab->root)
4476
0
    || info->output_bfd->xvec != abfd->xvec)
4477
0
  {
4478
0
    if (bfd_link_relocatable (info))
4479
0
      bfd_set_error (bfd_error_invalid_operation);
4480
0
    else
4481
0
      bfd_set_error (bfd_error_wrong_format);
4482
0
    goto error_return;
4483
0
  }
4484
0
    }
4485
4486
0
  ehdr = elf_elfheader (abfd);
4487
0
  if (info->warn_alternate_em
4488
0
      && bed->elf_machine_code != ehdr->e_machine
4489
0
      && ((bed->elf_machine_alt1 != 0
4490
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4491
0
    || (bed->elf_machine_alt2 != 0
4492
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4493
0
    _bfd_error_handler
4494
      /* xgettext:c-format */
4495
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4496
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4497
4498
  /* As a GNU extension, any input sections which are named
4499
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4500
     symbol.  This differs from .gnu.warning sections, which generate
4501
     warnings when they are included in an output file.  */
4502
  /* PR 12761: Also generate this warning when building shared libraries.  */
4503
0
  for (s = abfd->sections; s != NULL; s = s->next)
4504
0
    {
4505
0
      const char *name;
4506
4507
0
      name = bfd_section_name (s);
4508
0
      if (startswith (name, ".gnu.warning."))
4509
0
  {
4510
0
    char *msg;
4511
0
    bfd_size_type sz;
4512
4513
0
    name += sizeof ".gnu.warning." - 1;
4514
4515
    /* If this is a shared object, then look up the symbol
4516
       in the hash table.  If it is there, and it is already
4517
       been defined, then we will not be using the entry
4518
       from this shared object, so we don't need to warn.
4519
       FIXME: If we see the definition in a regular object
4520
       later on, we will warn, but we shouldn't.  The only
4521
       fix is to keep track of what warnings we are supposed
4522
       to emit, and then handle them all at the end of the
4523
       link.  */
4524
0
    if (dynamic)
4525
0
      {
4526
0
        struct elf_link_hash_entry *h;
4527
4528
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4529
4530
        /* FIXME: What about bfd_link_hash_common?  */
4531
0
        if (h != NULL
4532
0
      && (h->root.type == bfd_link_hash_defined
4533
0
          || h->root.type == bfd_link_hash_defweak))
4534
0
    continue;
4535
0
      }
4536
4537
0
    sz = s->size;
4538
0
    msg = bfd_alloc (abfd, sz + 1);
4539
0
    if (msg == NULL)
4540
0
      goto error_return;
4541
4542
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4543
0
      goto error_return;
4544
4545
0
    msg[sz] = '\0';
4546
4547
0
    if (! (_bfd_generic_link_add_one_symbol
4548
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4549
0
      false, bed->collect, NULL)))
4550
0
      goto error_return;
4551
4552
0
    if (bfd_link_executable (info))
4553
0
      {
4554
        /* Clobber the section size so that the warning does
4555
     not get copied into the output file.  */
4556
0
        s->size = 0;
4557
4558
        /* Also set SEC_EXCLUDE, so that symbols defined in
4559
     the warning section don't get copied to the output.  */
4560
0
        s->flags |= SEC_EXCLUDE;
4561
0
      }
4562
0
  }
4563
0
    }
4564
4565
0
  just_syms = ((s = abfd->sections) != NULL
4566
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4567
4568
0
  add_needed = true;
4569
0
  if (! dynamic)
4570
0
    {
4571
      /* If we are creating a shared library, create all the dynamic
4572
   sections immediately.  We need to attach them to something,
4573
   so we attach them to this BFD, provided it is the right
4574
   format and is not from ld --just-symbols.  Always create the
4575
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4576
   are no input BFD's of the same format as the output, we can't
4577
   make a shared library.  */
4578
0
      if (!just_syms
4579
0
    && (bfd_link_pic (info)
4580
0
        || (!bfd_link_relocatable (info)
4581
0
      && info->nointerp
4582
0
      && (info->export_dynamic || info->dynamic)))
4583
0
    && is_elf_hash_table (&htab->root)
4584
0
    && info->output_bfd->xvec == abfd->xvec
4585
0
    && !htab->dynamic_sections_created)
4586
0
  {
4587
0
    if (!bfd_elf_link_create_dynamic_sections (abfd, info))
4588
0
      goto error_return;
4589
0
  }
4590
0
    }
4591
0
  else if (!is_elf_hash_table (&htab->root))
4592
0
    goto error_return;
4593
0
  else
4594
0
    {
4595
0
      const char *soname = NULL;
4596
0
      char *audit = NULL;
4597
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4598
0
      const Elf_Internal_Phdr *phdr;
4599
0
      struct elf_link_loaded_list *loaded_lib;
4600
4601
      /* ld --just-symbols and dynamic objects don't mix very well.
4602
   ld shouldn't allow it.  */
4603
0
      if (just_syms)
4604
0
  abort ();
4605
4606
      /* If this dynamic lib was specified on the command line with
4607
   --as-needed in effect, then we don't want to add a DT_NEEDED
4608
   tag unless the lib is actually used.  Similary for libs brought
4609
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4610
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4611
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4612
   all.  */
4613
0
      add_needed = (elf_dyn_lib_class (abfd)
4614
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4615
0
           | DYN_NO_NEEDED)) == 0;
4616
4617
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4618
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4619
0
  {
4620
0
    bfd_byte *dynbuf;
4621
0
    bfd_byte *extdyn;
4622
0
    unsigned int elfsec;
4623
0
    unsigned long shlink;
4624
4625
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4626
0
      {
4627
0
      error_free_dyn:
4628
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4629
0
        goto error_return;
4630
0
      }
4631
4632
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4633
0
    if (elfsec == SHN_BAD)
4634
0
      goto error_free_dyn;
4635
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4636
4637
0
    for (extdyn = dynbuf;
4638
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4639
0
         extdyn += bed->s->sizeof_dyn)
4640
0
      {
4641
0
        Elf_Internal_Dyn dyn;
4642
4643
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4644
0
        if (dyn.d_tag == DT_SONAME)
4645
0
    {
4646
0
      unsigned int tagv = dyn.d_un.d_val;
4647
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4648
0
      if (soname == NULL)
4649
0
        goto error_free_dyn;
4650
0
    }
4651
0
        if (dyn.d_tag == DT_NEEDED)
4652
0
    {
4653
0
      struct bfd_link_needed_list *n, **pn;
4654
0
      char *fnm, *anm;
4655
0
      unsigned int tagv = dyn.d_un.d_val;
4656
0
      size_t amt;
4657
4658
0
      n = bfd_alloc (abfd, sizeof (*n));
4659
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4660
0
      if (n == NULL || fnm == NULL)
4661
0
        goto error_free_dyn;
4662
0
      amt = strlen (fnm) + 1;
4663
0
      anm = bfd_alloc (abfd, amt);
4664
0
      if (anm == NULL)
4665
0
        goto error_free_dyn;
4666
0
      memcpy (anm, fnm, amt);
4667
0
      n->name = anm;
4668
0
      n->by = abfd;
4669
0
      n->next = NULL;
4670
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4671
0
        ;
4672
0
      *pn = n;
4673
0
    }
4674
0
        if (dyn.d_tag == DT_RUNPATH)
4675
0
    {
4676
0
      struct bfd_link_needed_list *n, **pn;
4677
0
      char *fnm, *anm;
4678
0
      unsigned int tagv = dyn.d_un.d_val;
4679
0
      size_t amt;
4680
4681
0
      n = bfd_alloc (abfd, sizeof (*n));
4682
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4683
0
      if (n == NULL || fnm == NULL)
4684
0
        goto error_free_dyn;
4685
0
      amt = strlen (fnm) + 1;
4686
0
      anm = bfd_alloc (abfd, amt);
4687
0
      if (anm == NULL)
4688
0
        goto error_free_dyn;
4689
0
      memcpy (anm, fnm, amt);
4690
0
      n->name = anm;
4691
0
      n->by = abfd;
4692
0
      n->next = NULL;
4693
0
      for (pn = & runpath;
4694
0
           *pn != NULL;
4695
0
           pn = &(*pn)->next)
4696
0
        ;
4697
0
      *pn = n;
4698
0
    }
4699
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4700
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4701
0
    {
4702
0
      struct bfd_link_needed_list *n, **pn;
4703
0
      char *fnm, *anm;
4704
0
      unsigned int tagv = dyn.d_un.d_val;
4705
0
      size_t amt;
4706
4707
0
      n = bfd_alloc (abfd, sizeof (*n));
4708
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4709
0
      if (n == NULL || fnm == NULL)
4710
0
        goto error_free_dyn;
4711
0
      amt = strlen (fnm) + 1;
4712
0
      anm = bfd_alloc (abfd, amt);
4713
0
      if (anm == NULL)
4714
0
        goto error_free_dyn;
4715
0
      memcpy (anm, fnm, amt);
4716
0
      n->name = anm;
4717
0
      n->by = abfd;
4718
0
      n->next = NULL;
4719
0
      for (pn = & rpath;
4720
0
           *pn != NULL;
4721
0
           pn = &(*pn)->next)
4722
0
        ;
4723
0
      *pn = n;
4724
0
    }
4725
0
        if (dyn.d_tag == DT_AUDIT)
4726
0
    {
4727
0
      unsigned int tagv = dyn.d_un.d_val;
4728
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4729
0
    }
4730
0
        if (dyn.d_tag == DT_FLAGS_1)
4731
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4732
0
      }
4733
4734
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4735
0
  }
4736
4737
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4738
   frees all more recently bfd_alloc'd blocks as well.  */
4739
0
      if (runpath)
4740
0
  rpath = runpath;
4741
4742
0
      if (rpath)
4743
0
  {
4744
0
    struct bfd_link_needed_list **pn;
4745
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4746
0
      ;
4747
0
    *pn = rpath;
4748
0
  }
4749
4750
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4751
   all sections contained fully therein.  This makes relro
4752
   shared library sections appear as they will at run-time.  */
4753
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4754
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4755
0
  if (phdr->p_type == PT_GNU_RELRO)
4756
0
    {
4757
0
      for (s = abfd->sections; s != NULL; s = s->next)
4758
0
        {
4759
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4760
4761
0
    if ((s->flags & SEC_ALLOC) != 0
4762
0
        && s->vma * opb >= phdr->p_vaddr
4763
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4764
0
      s->flags |= SEC_READONLY;
4765
0
        }
4766
0
      break;
4767
0
    }
4768
4769
      /* We do not want to include any of the sections in a dynamic
4770
   object in the output file.  We hack by simply clobbering the
4771
   list of sections in the BFD.  This could be handled more
4772
   cleanly by, say, a new section flag; the existing
4773
   SEC_NEVER_LOAD flag is not the one we want, because that one
4774
   still implies that the section takes up space in the output
4775
   file.  */
4776
0
      bfd_section_list_clear (abfd);
4777
4778
      /* Find the name to use in a DT_NEEDED entry that refers to this
4779
   object.  If the object has a DT_SONAME entry, we use it.
4780
   Otherwise, if the generic linker stuck something in
4781
   elf_dt_name, we use that.  Otherwise, we just use the file
4782
   name.  */
4783
0
      if (soname == NULL || *soname == '\0')
4784
0
  {
4785
0
    soname = elf_dt_name (abfd);
4786
0
    if (soname == NULL || *soname == '\0')
4787
0
      soname = bfd_get_filename (abfd);
4788
0
  }
4789
4790
      /* Save the SONAME because sometimes the linker emulation code
4791
   will need to know it.  */
4792
0
      elf_dt_name (abfd) = soname;
4793
4794
      /* If we have already included this dynamic object in the
4795
   link, just ignore it.  There is no reason to include a
4796
   particular dynamic object more than once.  */
4797
0
      for (loaded_lib = htab->dyn_loaded;
4798
0
     loaded_lib != NULL;
4799
0
     loaded_lib = loaded_lib->next)
4800
0
  {
4801
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4802
0
      return true;
4803
0
  }
4804
4805
      /* Create dynamic sections for backends that require that be done
4806
   before setup_gnu_properties.  */
4807
0
      if (add_needed
4808
0
    && !bfd_elf_link_create_dynamic_sections (abfd, info))
4809
0
  return false;
4810
4811
      /* Save the DT_AUDIT entry for the linker emulation code. */
4812
0
      elf_dt_audit (abfd) = audit;
4813
0
    }
4814
4815
  /* If this is a dynamic object, we always link against the .dynsym
4816
     symbol table, not the .symtab symbol table.  The dynamic linker
4817
     will only see the .dynsym symbol table, so there is no reason to
4818
     look at .symtab for a dynamic object.  */
4819
4820
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4821
0
    hdr = &elf_symtab_hdr (abfd);
4822
0
  else
4823
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4824
4825
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4826
4827
  /* The sh_info field of the symtab header tells us where the
4828
     external symbols start.  We don't care about the local symbols at
4829
     this point.  */
4830
0
  if (elf_bad_symtab (abfd))
4831
0
    {
4832
0
      extsymcount = symcount;
4833
0
      extsymoff = 0;
4834
0
    }
4835
0
  else
4836
0
    {
4837
0
      extsymcount = symcount - hdr->sh_info;
4838
0
      extsymoff = hdr->sh_info;
4839
0
    }
4840
4841
0
  sym_hash = elf_sym_hashes (abfd);
4842
0
  if (extsymcount != 0)
4843
0
    {
4844
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4845
0
              NULL, NULL, NULL);
4846
0
      if (isymbuf == NULL)
4847
0
  goto error_return;
4848
4849
0
      if (sym_hash == NULL)
4850
0
  {
4851
    /* We store a pointer to the hash table entry for each
4852
       external symbol.  */
4853
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4854
0
    if (sym_hash == NULL)
4855
0
      goto error_free_sym;
4856
0
    elf_sym_hashes (abfd) = sym_hash;
4857
0
  }
4858
0
    }
4859
4860
0
  if (dynamic)
4861
0
    {
4862
      /* Read in any version definitions.  */
4863
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4864
0
            info->default_imported_symver))
4865
0
  goto error_free_sym;
4866
4867
      /* Read in the symbol versions, but don't bother to convert them
4868
   to internal format.  */
4869
0
      if (elf_dynversym (abfd) != 0)
4870
0
  {
4871
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4872
0
    bfd_size_type amt = versymhdr->sh_size;
4873
4874
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4875
0
      goto error_free_sym;
4876
0
    extversym = _bfd_malloc_and_read (abfd, amt, amt);
4877
0
    if (extversym == NULL)
4878
0
      goto error_free_sym;
4879
0
    extversym_end = extversym + amt / sizeof (*extversym);
4880
0
  }
4881
0
    }
4882
4883
  /* If we are loading an as-needed shared lib, save the symbol table
4884
     state before we start adding symbols.  If the lib turns out
4885
     to be unneeded, restore the state.  */
4886
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4887
0
    {
4888
0
      unsigned int i;
4889
0
      size_t entsize;
4890
4891
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4892
0
  {
4893
0
    struct bfd_hash_entry *p;
4894
0
    struct elf_link_hash_entry *h;
4895
4896
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4897
0
      {
4898
0
        h = (struct elf_link_hash_entry *) p;
4899
0
        entsize += htab->root.table.entsize;
4900
0
        if (h->root.type == bfd_link_hash_warning)
4901
0
    {
4902
0
      entsize += htab->root.table.entsize;
4903
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4904
0
    }
4905
0
        if (h->root.type == bfd_link_hash_common)
4906
0
    entsize += sizeof (*h->root.u.c.p);
4907
0
      }
4908
0
  }
4909
4910
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4911
0
      old_tab = bfd_malloc (tabsize + entsize);
4912
0
      if (old_tab == NULL)
4913
0
  goto error_free_vers;
4914
4915
      /* Remember the current objalloc pointer, so that all mem for
4916
   symbols added can later be reclaimed.  */
4917
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4918
0
      if (alloc_mark == NULL)
4919
0
  goto error_free_vers;
4920
4921
      /* Make a special call to the linker "notice" function to
4922
   tell it that we are about to handle an as-needed lib.  */
4923
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4924
0
  goto error_free_vers;
4925
4926
      /* Clone the symbol table.  Remember some pointers into the
4927
   symbol table, and dynamic symbol count.  */
4928
0
      old_ent = (char *) old_tab + tabsize;
4929
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4930
0
      old_undefs = htab->root.undefs;
4931
0
      old_undefs_tail = htab->root.undefs_tail;
4932
0
      old_table = htab->root.table.table;
4933
0
      old_size = htab->root.table.size;
4934
0
      old_count = htab->root.table.count;
4935
0
      old_strtab = NULL;
4936
0
      if (htab->dynstr != NULL)
4937
0
  {
4938
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4939
0
    if (old_strtab == NULL)
4940
0
      goto error_free_vers;
4941
0
  }
4942
4943
0
      for (i = 0; i < htab->root.table.size; i++)
4944
0
  {
4945
0
    struct bfd_hash_entry *p;
4946
0
    struct elf_link_hash_entry *h;
4947
4948
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4949
0
      {
4950
0
        h = (struct elf_link_hash_entry *) p;
4951
0
        memcpy (old_ent, h, htab->root.table.entsize);
4952
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
4953
0
        if (h->root.type == bfd_link_hash_warning)
4954
0
    {
4955
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4956
0
      memcpy (old_ent, h, htab->root.table.entsize);
4957
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
4958
0
    }
4959
0
        if (h->root.type == bfd_link_hash_common)
4960
0
    {
4961
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4962
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4963
0
    }
4964
0
      }
4965
0
  }
4966
0
    }
4967
4968
0
  weaks = NULL;
4969
0
  if (extversym == NULL)
4970
0
    ever = NULL;
4971
0
  else if (extversym + extsymoff < extversym_end)
4972
0
    ever = extversym + extsymoff;
4973
0
  else
4974
0
    {
4975
      /* xgettext:c-format */
4976
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4977
0
        abfd, (long) extsymoff,
4978
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
4979
0
      bfd_set_error (bfd_error_bad_value);
4980
0
      goto error_free_vers;
4981
0
    }
4982
4983
0
  if (!bfd_link_relocatable (info)
4984
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4985
0
    {
4986
0
      _bfd_error_handler
4987
0
  (_("%pB: plugin needed to handle lto object"), abfd);
4988
0
    }
4989
4990
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4991
0
       isym < isymend;
4992
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4993
0
    {
4994
0
      int bind;
4995
0
      bfd_vma value;
4996
0
      asection *sec, *new_sec;
4997
0
      flagword flags;
4998
0
      const char *name;
4999
0
      const char *defvername;
5000
0
      bool must_copy_name = false;
5001
0
      struct elf_link_hash_entry *h;
5002
0
      struct elf_link_hash_entry *hi;
5003
0
      bool definition;
5004
0
      bool size_change_ok;
5005
0
      bool type_change_ok;
5006
0
      bool new_weak;
5007
0
      bool old_weak;
5008
0
      bfd *override;
5009
0
      bool common;
5010
0
      bool discarded;
5011
0
      unsigned int old_alignment;
5012
0
      unsigned int shindex;
5013
0
      bfd *old_bfd;
5014
0
      bool matched;
5015
5016
0
      override = NULL;
5017
5018
0
      flags = BSF_NO_FLAGS;
5019
0
      sec = NULL;
5020
0
      value = isym->st_value;
5021
0
      common = bed->common_definition (isym);
5022
0
      if (common && info->inhibit_common_definition)
5023
0
  {
5024
    /* Treat common symbol as undefined for --no-define-common.  */
5025
0
    isym->st_shndx = SHN_UNDEF;
5026
0
    common = false;
5027
0
  }
5028
0
      discarded = false;
5029
5030
0
      bind = ELF_ST_BIND (isym->st_info);
5031
0
      switch (bind)
5032
0
  {
5033
0
  case STB_LOCAL:
5034
    /* This should be impossible, since ELF requires that all
5035
       global symbols follow all local symbols, and that sh_info
5036
       point to the first global symbol.  Unfortunately, Irix 5
5037
       screws this up.  */
5038
0
    if (elf_bad_symtab (abfd))
5039
0
      continue;
5040
5041
    /* If we aren't prepared to handle locals within the globals
5042
       then we'll likely segfault on a NULL symbol hash if the
5043
       symbol is ever referenced in relocations.  */
5044
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5045
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5046
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5047
0
        " (>= sh_info of %lu)"),
5048
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5049
0
            (long) extsymoff);
5050
5051
    /* Dynamic object relocations are not processed by ld, so
5052
       ld won't run into the problem mentioned above.  */
5053
0
    if (dynamic)
5054
0
      continue;
5055
0
    bfd_set_error (bfd_error_bad_value);
5056
0
    goto error_free_vers;
5057
5058
0
  case STB_GLOBAL:
5059
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5060
0
      flags = BSF_GLOBAL;
5061
0
    break;
5062
5063
0
  case STB_WEAK:
5064
0
    flags = BSF_WEAK;
5065
0
    break;
5066
5067
0
  case STB_GNU_UNIQUE:
5068
0
    flags = BSF_GNU_UNIQUE;
5069
0
    break;
5070
5071
0
  default:
5072
    /* Leave it up to the processor backend.  */
5073
0
    break;
5074
0
  }
5075
5076
0
      if (isym->st_shndx == SHN_UNDEF)
5077
0
  sec = bfd_und_section_ptr;
5078
0
      else if (isym->st_shndx == SHN_ABS)
5079
0
  sec = bfd_abs_section_ptr;
5080
0
      else if (isym->st_shndx == SHN_COMMON)
5081
0
  {
5082
0
    sec = bfd_com_section_ptr;
5083
    /* What ELF calls the size we call the value.  What ELF
5084
       calls the value we call the alignment.  */
5085
0
    value = isym->st_size;
5086
0
  }
5087
0
      else
5088
0
  {
5089
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5090
0
    if (sec == NULL)
5091
0
      sec = bfd_abs_section_ptr;
5092
0
    else if (discarded_section (sec))
5093
0
      {
5094
        /* Symbols from discarded section are undefined.  We keep
5095
     its visibility.  */
5096
0
        sec = bfd_und_section_ptr;
5097
0
        discarded = true;
5098
0
        isym->st_shndx = SHN_UNDEF;
5099
0
      }
5100
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5101
0
      value -= sec->vma;
5102
0
  }
5103
5104
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5105
0
                isym->st_name);
5106
0
      if (name == NULL)
5107
0
  goto error_free_vers;
5108
5109
0
      if (isym->st_shndx == SHN_COMMON
5110
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5111
0
  {
5112
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5113
5114
0
    if (xc == NULL)
5115
0
      {
5116
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5117
0
         | SEC_EXCLUDE);
5118
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5119
0
        if (xc == NULL)
5120
0
    goto error_free_vers;
5121
0
      }
5122
0
    sec = xc;
5123
0
  }
5124
0
      else if (isym->st_shndx == SHN_COMMON
5125
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5126
0
         && !bfd_link_relocatable (info))
5127
0
  {
5128
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5129
5130
0
    if (tcomm == NULL)
5131
0
      {
5132
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5133
0
         | SEC_LINKER_CREATED);
5134
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5135
0
        if (tcomm == NULL)
5136
0
    goto error_free_vers;
5137
0
      }
5138
0
    sec = tcomm;
5139
0
  }
5140
0
      else if (bed->elf_add_symbol_hook)
5141
0
  {
5142
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5143
0
               &sec, &value))
5144
0
      goto error_free_vers;
5145
5146
    /* The hook function sets the name to NULL if this symbol
5147
       should be skipped for some reason.  */
5148
0
    if (name == NULL)
5149
0
      continue;
5150
0
  }
5151
5152
0
      if (name[0] == '\0')
5153
0
  {
5154
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5155
0
    bfd_set_error (bfd_error_bad_value);
5156
0
    goto error_free_vers;
5157
0
  }
5158
5159
      /* Sanity check that all possibilities were handled.  */
5160
0
      if (sec == NULL)
5161
0
  abort ();
5162
5163
      /* Silently discard TLS symbols from --just-syms.  There's
5164
   no way to combine a static TLS block with a new TLS block
5165
   for this executable.  */
5166
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5167
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5168
0
  continue;
5169
5170
0
      if (bfd_is_und_section (sec)
5171
0
    || bfd_is_com_section (sec))
5172
0
  definition = false;
5173
0
      else
5174
0
  definition = true;
5175
5176
0
      size_change_ok = false;
5177
0
      type_change_ok = bed->type_change_ok;
5178
0
      old_weak = false;
5179
0
      matched = false;
5180
0
      old_alignment = 0;
5181
0
      old_bfd = NULL;
5182
0
      new_sec = sec;
5183
0
      defvername = NULL;
5184
5185
0
      if (is_elf_hash_table (&htab->root))
5186
0
  {
5187
0
    Elf_Internal_Versym iver;
5188
0
    unsigned int vernum = 0;
5189
0
    bool skip;
5190
5191
0
    if (ever == NULL)
5192
0
      {
5193
0
        if (info->default_imported_symver)
5194
    /* Use the default symbol version created earlier.  */
5195
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5196
0
        else
5197
0
    iver.vs_vers = 0;
5198
0
      }
5199
0
    else if (ever >= extversym_end)
5200
0
      {
5201
        /* xgettext:c-format */
5202
0
        _bfd_error_handler (_("%pB: not enough version information"),
5203
0
          abfd);
5204
0
        bfd_set_error (bfd_error_bad_value);
5205
0
        goto error_free_vers;
5206
0
      }
5207
0
    else
5208
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5209
5210
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5211
5212
    /* If this is a hidden symbol, or if it is not version
5213
       1, we append the version name to the symbol name.
5214
       However, we do not modify a non-hidden absolute symbol
5215
       if it is not a function, because it might be the version
5216
       symbol itself.  FIXME: What if it isn't?  */
5217
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5218
0
        || (vernum > 1
5219
0
      && (!bfd_is_abs_section (sec)
5220
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5221
0
      {
5222
0
        const char *verstr;
5223
0
        size_t namelen, verlen, newlen;
5224
0
        char *newname, *p;
5225
5226
0
        if (isym->st_shndx != SHN_UNDEF)
5227
0
    {
5228
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5229
0
        verstr = NULL;
5230
0
      else if (vernum > 1)
5231
0
        verstr =
5232
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5233
0
      else
5234
0
        verstr = "";
5235
5236
0
      if (verstr == NULL)
5237
0
        {
5238
0
          _bfd_error_handler
5239
      /* xgettext:c-format */
5240
0
      (_("%pB: %s: invalid version %u (max %d)"),
5241
0
       abfd, name, vernum,
5242
0
       elf_tdata (abfd)->cverdefs);
5243
0
          bfd_set_error (bfd_error_bad_value);
5244
0
          goto error_free_vers;
5245
0
        }
5246
0
    }
5247
0
        else
5248
0
    {
5249
      /* We cannot simply test for the number of
5250
         entries in the VERNEED section since the
5251
         numbers for the needed versions do not start
5252
         at 0.  */
5253
0
      Elf_Internal_Verneed *t;
5254
5255
0
      verstr = NULL;
5256
0
      for (t = elf_tdata (abfd)->verref;
5257
0
           t != NULL;
5258
0
           t = t->vn_nextref)
5259
0
        {
5260
0
          Elf_Internal_Vernaux *a;
5261
5262
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5263
0
      {
5264
0
        if (a->vna_other == vernum)
5265
0
          {
5266
0
            verstr = a->vna_nodename;
5267
0
            break;
5268
0
          }
5269
0
      }
5270
0
          if (a != NULL)
5271
0
      break;
5272
0
        }
5273
0
      if (verstr == NULL)
5274
0
        {
5275
0
          _bfd_error_handler
5276
      /* xgettext:c-format */
5277
0
      (_("%pB: %s: invalid needed version %d"),
5278
0
       abfd, name, vernum);
5279
0
          bfd_set_error (bfd_error_bad_value);
5280
0
          goto error_free_vers;
5281
0
        }
5282
0
    }
5283
5284
0
        namelen = strlen (name);
5285
0
        verlen = strlen (verstr);
5286
0
        newlen = namelen + verlen + 2;
5287
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5288
0
      && isym->st_shndx != SHN_UNDEF)
5289
0
    ++newlen;
5290
5291
0
        newname = bfd_hash_allocate (&htab->root.table, newlen);
5292
0
        if (newname == NULL)
5293
0
    goto error_free_vers;
5294
0
        memcpy (newname, name, namelen);
5295
0
        p = newname + namelen;
5296
0
        *p++ = ELF_VER_CHR;
5297
        /* If this is a defined non-hidden version symbol,
5298
     we add another @ to the name.  This indicates the
5299
     default version of the symbol.  */
5300
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5301
0
      && isym->st_shndx != SHN_UNDEF)
5302
0
    *p++ = ELF_VER_CHR, defvername = name;
5303
0
        memcpy (p, verstr, verlen + 1);
5304
5305
0
        name = newname;
5306
        /* Since bfd_hash_alloc is used for "name", the string
5307
     must be copied if added to first_hash.  The string
5308
     memory can be freed when an --as-needed library is
5309
     not needed.  */
5310
0
        must_copy_name = true;
5311
0
      }
5312
5313
    /* If this symbol has default visibility and the user has
5314
       requested we not re-export it, then mark it as hidden.  */
5315
0
    if (!bfd_is_und_section (sec)
5316
0
        && !dynamic
5317
0
        && abfd->no_export
5318
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5319
0
      isym->st_other = (STV_HIDDEN
5320
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5321
5322
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5323
0
              sym_hash, &old_bfd, &old_weak,
5324
0
              &old_alignment, &skip, &override,
5325
0
              &type_change_ok, &size_change_ok,
5326
0
              &matched))
5327
0
      goto error_free_vers;
5328
5329
0
    if (skip)
5330
0
      continue;
5331
5332
0
    h = *sym_hash;
5333
0
    while (h->root.type == bfd_link_hash_indirect
5334
0
     || h->root.type == bfd_link_hash_warning)
5335
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5336
5337
    /* Override a definition only if the new symbol matches the
5338
       existing one.  */
5339
0
    if (override && matched)
5340
0
      {
5341
0
        definition = false;
5342
0
        if (htab->first_hash != NULL
5343
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5344
0
      && h->root.non_ir_ref_regular)
5345
0
    {
5346
      /* When reloading --as-needed shared objects for new
5347
         symbols added from IR inputs, if this shared object
5348
         has the first definition, use it.  */
5349
0
      struct elf_link_first_hash_entry *e
5350
0
        = ((struct elf_link_first_hash_entry *)
5351
0
           bfd_hash_lookup (htab->first_hash, name, false,
5352
0
          false));
5353
0
      if (e != NULL && e->abfd == abfd)
5354
0
        definition = true;
5355
0
    }
5356
0
      }
5357
5358
0
    if (h->versioned != unversioned
5359
0
        && elf_tdata (abfd)->verdef != NULL
5360
0
        && vernum > 1
5361
0
        && definition)
5362
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5363
0
  }
5364
5365
0
      if (! (_bfd_generic_link_add_one_symbol
5366
0
       (info, override ? override : abfd, name, flags, sec, value,
5367
0
        NULL, false, bed->collect,
5368
0
        (struct bfd_link_hash_entry **) sym_hash)))
5369
0
  goto error_free_vers;
5370
5371
0
      h = *sym_hash;
5372
      /* We need to make sure that indirect symbol dynamic flags are
5373
   updated.  */
5374
0
      hi = h;
5375
0
      while (h->root.type == bfd_link_hash_indirect
5376
0
       || h->root.type == bfd_link_hash_warning)
5377
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5378
5379
0
      *sym_hash = h;
5380
5381
      /* Setting the index to -3 tells elf_link_output_extsym that
5382
   this symbol is defined in a discarded section.  */
5383
0
      if (discarded && is_elf_hash_table (&htab->root))
5384
0
  h->indx = -3;
5385
5386
0
      new_weak = (flags & BSF_WEAK) != 0;
5387
0
      if (dynamic
5388
0
    && definition
5389
0
    && new_weak
5390
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5391
0
    && is_elf_hash_table (&htab->root)
5392
0
    && h->u.alias == NULL)
5393
0
  {
5394
    /* Keep a list of all weak defined non function symbols from
5395
       a dynamic object, using the alias field.  Later in this
5396
       function we will set the alias field to the correct
5397
       value.  We only put non-function symbols from dynamic
5398
       objects on this list, because that happens to be the only
5399
       time we need to know the normal symbol corresponding to a
5400
       weak symbol, and the information is time consuming to
5401
       figure out.  If the alias field is not already NULL,
5402
       then this symbol was already defined by some previous
5403
       dynamic object, and we will be using that previous
5404
       definition anyhow.  */
5405
5406
0
    h->u.alias = weaks;
5407
0
    weaks = h;
5408
0
  }
5409
5410
      /* Set the alignment of a common symbol.  */
5411
0
      if ((common || bfd_is_com_section (sec))
5412
0
    && h->root.type == bfd_link_hash_common)
5413
0
  {
5414
0
    unsigned int align;
5415
5416
0
    if (common)
5417
0
      align = bfd_log2 (isym->st_value);
5418
0
    else
5419
0
      {
5420
        /* The new symbol is a common symbol in a shared object.
5421
     We need to get the alignment from the section.  */
5422
0
        align = new_sec->alignment_power;
5423
0
      }
5424
0
    if (align > old_alignment)
5425
0
      h->root.u.c.p->alignment_power = align;
5426
0
    else
5427
0
      h->root.u.c.p->alignment_power = old_alignment;
5428
0
  }
5429
5430
0
      if (is_elf_hash_table (&htab->root))
5431
0
  {
5432
    /* Set a flag in the hash table entry indicating the type of
5433
       reference or definition we just found.  A dynamic symbol
5434
       is one which is referenced or defined by both a regular
5435
       object and a shared object.  */
5436
0
    bool dynsym = false;
5437
5438
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5439
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5440
0
      {
5441
        /* Except for this flag to track nonweak references.  */
5442
0
        if (!definition
5443
0
      && bind != STB_WEAK)
5444
0
    h->ref_ir_nonweak = 1;
5445
0
      }
5446
0
    else if (!dynamic)
5447
0
      {
5448
0
        if (! definition)
5449
0
    {
5450
0
      h->ref_regular = 1;
5451
0
      if (bind != STB_WEAK)
5452
0
        h->ref_regular_nonweak = 1;
5453
0
    }
5454
0
        else
5455
0
    {
5456
0
      h->def_regular = 1;
5457
0
      if (h->def_dynamic)
5458
0
        {
5459
0
          h->def_dynamic = 0;
5460
0
          h->ref_dynamic = 1;
5461
0
        }
5462
0
    }
5463
0
      }
5464
0
    else
5465
0
      {
5466
0
        if (! definition)
5467
0
    {
5468
0
      h->ref_dynamic = 1;
5469
0
      hi->ref_dynamic = 1;
5470
0
    }
5471
0
        else
5472
0
    {
5473
0
      h->def_dynamic = 1;
5474
0
      hi->def_dynamic = 1;
5475
0
    }
5476
0
      }
5477
5478
    /* If an indirect symbol has been forced local, don't
5479
       make the real symbol dynamic.  */
5480
0
    if (h != hi && hi->forced_local)
5481
0
      ;
5482
0
    else if (!dynamic)
5483
0
      {
5484
0
        if (bfd_link_dll (info)
5485
0
      || h->def_dynamic
5486
0
      || h->ref_dynamic)
5487
0
    dynsym = true;
5488
0
      }
5489
0
    else
5490
0
      {
5491
0
        if (h->def_regular
5492
0
      || h->ref_regular
5493
0
      || (h->is_weakalias
5494
0
          && weakdef (h)->dynindx != -1))
5495
0
    dynsym = true;
5496
0
      }
5497
5498
    /* Check to see if we need to add an indirect symbol for
5499
       the default name.  */
5500
0
    if ((definition
5501
0
         || (!override && h->root.type == bfd_link_hash_common))
5502
0
        && !(hi != h
5503
0
       && hi->versioned == versioned_hidden))
5504
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5505
0
                sec, value, &old_bfd, &dynsym))
5506
0
        goto error_free_vers;
5507
5508
    /* Check the alignment when a common symbol is involved. This
5509
       can change when a common symbol is overridden by a normal
5510
       definition or a common symbol is ignored due to the old
5511
       normal definition. We need to make sure the maximum
5512
       alignment is maintained.  */
5513
0
    if ((old_alignment || common)
5514
0
        && h->root.type != bfd_link_hash_common)
5515
0
      {
5516
0
        unsigned int common_align;
5517
0
        unsigned int normal_align;
5518
0
        unsigned int symbol_align;
5519
0
        bfd *normal_bfd;
5520
0
        bfd *common_bfd;
5521
5522
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5523
0
        || h->root.type == bfd_link_hash_defweak);
5524
5525
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5526
0
        if (h->root.u.def.section->owner != NULL
5527
0
      && (h->root.u.def.section->owner->flags
5528
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5529
0
    {
5530
0
      normal_align = h->root.u.def.section->alignment_power;
5531
0
      if (normal_align > symbol_align)
5532
0
        normal_align = symbol_align;
5533
0
    }
5534
0
        else
5535
0
    normal_align = symbol_align;
5536
5537
0
        if (old_alignment)
5538
0
    {
5539
0
      common_align = old_alignment;
5540
0
      common_bfd = old_bfd;
5541
0
      normal_bfd = abfd;
5542
0
    }
5543
0
        else
5544
0
    {
5545
0
      common_align = bfd_log2 (isym->st_value);
5546
0
      common_bfd = abfd;
5547
0
      normal_bfd = old_bfd;
5548
0
    }
5549
5550
0
        if (normal_align < common_align)
5551
0
    {
5552
      /* PR binutils/2735 */
5553
0
      uint64_t c_align = UINT64_C (1) << common_align;
5554
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5555
0
      if (normal_bfd == NULL)
5556
0
        _bfd_error_handler
5557
          /* xgettext:c-format */
5558
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5559
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5560
0
           c_align, name, common_bfd,
5561
0
           n_align, h->root.u.def.section);
5562
0
      else
5563
0
        _bfd_error_handler
5564
          /* xgettext:c-format */
5565
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5566
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5567
0
           n_align, name, normal_bfd,
5568
0
           c_align, common_bfd);
5569
5570
      /* PR 30499: make sure that users understand that this warning is serious.  */
5571
0
      _bfd_error_handler
5572
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5573
0
    }
5574
0
      }
5575
5576
    /* Remember the symbol size if it isn't undefined.  */
5577
0
    if (isym->st_size != 0
5578
0
        && isym->st_shndx != SHN_UNDEF
5579
0
        && (definition || h->size == 0))
5580
0
      {
5581
0
        if (h->size != 0
5582
0
      && h->size != isym->st_size
5583
0
      && ! size_change_ok)
5584
0
    {
5585
0
      _bfd_error_handler
5586
        /* xgettext:c-format */
5587
0
        (_("warning: size of symbol `%s' changed"
5588
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5589
0
         name, (uint64_t) h->size, old_bfd,
5590
0
         (uint64_t) isym->st_size, abfd);
5591
5592
      /* PR 30499: make sure that users understand that this warning is serious.  */
5593
0
      _bfd_error_handler
5594
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5595
0
    }
5596
5597
0
        h->size = isym->st_size;
5598
0
      }
5599
5600
    /* If this is a common symbol, then we always want H->SIZE
5601
       to be the size of the common symbol.  The code just above
5602
       won't fix the size if a common symbol becomes larger.  We
5603
       don't warn about a size change here, because that is
5604
       covered by --warn-common.  Allow changes between different
5605
       function types.  */
5606
0
    if (h->root.type == bfd_link_hash_common)
5607
0
      h->size = h->root.u.c.size;
5608
5609
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5610
0
        && ((definition && !new_weak)
5611
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5612
0
      || h->type == STT_NOTYPE))
5613
0
      {
5614
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5615
5616
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5617
     symbol.  */
5618
0
        if (type == STT_GNU_IFUNC
5619
0
      && (abfd->flags & DYNAMIC) != 0)
5620
0
    type = STT_FUNC;
5621
5622
0
        if (h->type != type)
5623
0
    {
5624
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5625
        /* xgettext:c-format */
5626
0
        _bfd_error_handler
5627
0
          (_("warning: type of symbol `%s' changed"
5628
0
       " from %d to %d in %pB"),
5629
0
           name, h->type, type, abfd);
5630
5631
0
      h->type = type;
5632
0
    }
5633
0
      }
5634
5635
    /* Merge st_other field.  */
5636
0
    elf_merge_st_other (info->output_bfd, abfd, h, isym->st_other,
5637
0
            sec, definition, dynamic);
5638
5639
    /* We don't want to make debug symbol dynamic.  */
5640
0
    if (definition
5641
0
        && (sec->flags & SEC_DEBUGGING)
5642
0
        && !bfd_link_relocatable (info))
5643
0
      dynsym = false;
5644
5645
    /* Nor should we make plugin symbols dynamic.  */
5646
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5647
0
      dynsym = false;
5648
5649
0
    if (definition)
5650
0
      {
5651
0
        h->target_internal = isym->st_target_internal;
5652
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5653
0
      }
5654
5655
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5656
       in IR.  Since all data or text symbols in IR have the
5657
       same type, value and section, we can't tell if a symbol
5658
       is an alias of another symbol by their types, values and
5659
       sections.  */
5660
0
    if (definition
5661
0
        && !dynamic
5662
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5663
0
      {
5664
0
        const char *p = strchr (name, ELF_VER_CHR);
5665
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5666
0
    {
5667
      /* Queue non-default versions so that .symver x, x@FOO
5668
         aliases can be checked.  */
5669
0
      if (!nondeflt_vers)
5670
0
        {
5671
0
          nondeflt_vers = bfd_malloc ((isymend - isym + 1)
5672
0
              * sizeof (*nondeflt_vers));
5673
0
          if (!nondeflt_vers)
5674
0
      goto error_free_vers;
5675
0
        }
5676
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5677
0
    }
5678
0
      }
5679
5680
0
    if (dynsym && h->dynindx == -1)
5681
0
      {
5682
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5683
0
    goto error_free_vers;
5684
0
        if (h->is_weakalias
5685
0
      && weakdef (h)->dynindx == -1)
5686
0
    {
5687
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5688
0
        goto error_free_vers;
5689
0
    }
5690
0
      }
5691
0
    else if (h->dynindx != -1)
5692
      /* If the symbol already has a dynamic index, but
5693
         visibility says it should not be visible, turn it into
5694
         a local symbol.  */
5695
0
      switch (ELF_ST_VISIBILITY (h->other))
5696
0
        {
5697
0
        case STV_INTERNAL:
5698
0
        case STV_HIDDEN:
5699
0
    obed->elf_backend_hide_symbol (info, h, true);
5700
0
    dynsym = false;
5701
0
    break;
5702
0
        }
5703
5704
0
    if (!add_needed
5705
0
        && matched
5706
0
        && definition
5707
0
        && h->root.type != bfd_link_hash_indirect)
5708
0
      {
5709
0
        if ((dynsym
5710
0
       && h->ref_regular_nonweak)
5711
0
      || (old_bfd != NULL
5712
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5713
0
          && h->ref_ir_nonweak
5714
0
          && !info->lto_all_symbols_read)
5715
0
      || (h->ref_dynamic_nonweak
5716
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5717
0
          && !on_needed_list (elf_dt_name (abfd),
5718
0
            htab->needed, NULL)))
5719
0
    {
5720
0
      const char *soname = elf_dt_name (abfd);
5721
5722
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5723
0
            h->root.root.string);
5724
5725
      /* A symbol from a library loaded via DT_NEEDED of some
5726
         other library is referenced by a regular object.
5727
         Add a DT_NEEDED entry for it.  Issue an error if
5728
         --no-add-needed is used and the reference was not
5729
         a weak one.  */
5730
0
      if (old_bfd != NULL
5731
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5732
0
        {
5733
0
          _bfd_error_handler
5734
      /* xgettext:c-format */
5735
0
      (_("%pB: undefined reference to symbol '%s'"),
5736
0
       old_bfd, name);
5737
0
          bfd_set_error (bfd_error_missing_dso);
5738
0
          goto error_free_vers;
5739
0
        }
5740
5741
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5742
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5743
5744
      /* Create dynamic sections for backends that require
5745
         that be done before setup_gnu_properties.  */
5746
0
      if (!bfd_elf_link_create_dynamic_sections (abfd, info))
5747
0
        goto error_free_vers;
5748
0
      add_needed = true;
5749
0
    }
5750
0
        else if (dynamic
5751
0
           && h->root.u.def.section->owner == abfd)
5752
0
    {
5753
      /* Add this symbol to first hash if this shared
5754
         object has the first definition.  */
5755
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5756
      /* And if it was the default symbol version definition,
5757
         also add the short name.  */
5758
0
      if (defvername)
5759
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5760
0
    }
5761
0
      }
5762
0
  }
5763
0
    }
5764
5765
0
  if (info->lto_plugin_active
5766
0
      && !bfd_link_relocatable (info)
5767
0
      && (abfd->flags & BFD_PLUGIN) == 0
5768
0
      && !just_syms
5769
0
      && extsymcount != 0
5770
0
      && is_elf_hash_table (&htab->root))
5771
0
    {
5772
0
      int r_sym_shift;
5773
5774
0
      if (bed->s->arch_size == 32)
5775
0
  r_sym_shift = 8;
5776
0
      else
5777
0
  r_sym_shift = 32;
5778
5779
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5780
   referenced in regular objects so that linker plugin will get
5781
   the correct symbol resolution.  */
5782
5783
0
      sym_hash = elf_sym_hashes (abfd);
5784
0
      for (s = abfd->sections; s != NULL; s = s->next)
5785
0
  {
5786
0
    Elf_Internal_Rela *internal_relocs;
5787
0
    Elf_Internal_Rela *rel, *relend;
5788
5789
    /* Don't check relocations in excluded sections.  */
5790
0
    if ((s->flags & SEC_RELOC) == 0
5791
0
        || s->reloc_count == 0
5792
0
        || (s->flags & SEC_EXCLUDE) != 0
5793
0
        || (s->flags & SEC_DEBUGGING) != 0)
5794
0
      continue;
5795
5796
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5797
0
      (abfd, info, s, NULL, NULL,
5798
0
       _bfd_elf_link_keep_memory (info));
5799
0
    if (internal_relocs == NULL)
5800
0
      goto error_free_vers;
5801
5802
0
    rel = internal_relocs;
5803
0
    relend = rel + s->reloc_count;
5804
0
    for ( ; rel < relend; rel++)
5805
0
      {
5806
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5807
0
        struct elf_link_hash_entry *h;
5808
5809
        /* Skip local symbols.  */
5810
0
        if (r_symndx < extsymoff)
5811
0
    continue;
5812
5813
0
        h = sym_hash[r_symndx - extsymoff];
5814
0
        if (h != NULL)
5815
0
    h->root.non_ir_ref_regular = 1;
5816
0
      }
5817
5818
0
    if (elf_section_data (s)->relocs != internal_relocs)
5819
0
      free (internal_relocs);
5820
0
  }
5821
0
    }
5822
5823
0
  free (extversym);
5824
0
  extversym = NULL;
5825
0
  free (isymbuf);
5826
0
  isymbuf = NULL;
5827
5828
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5829
0
    {
5830
0
      unsigned int i;
5831
5832
      /* Restore the symbol table.  */
5833
0
      old_ent = (char *) old_tab + tabsize;
5834
0
      memset (elf_sym_hashes (abfd), 0,
5835
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5836
0
      htab->root.table.table = old_table;
5837
0
      htab->root.table.size = old_size;
5838
0
      htab->root.table.count = old_count;
5839
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5840
0
      htab->root.undefs = old_undefs;
5841
0
      htab->root.undefs_tail = old_undefs_tail;
5842
0
      if (htab->dynstr != NULL)
5843
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5844
0
      free (old_strtab);
5845
0
      old_strtab = NULL;
5846
0
      for (i = 0; i < htab->root.table.size; i++)
5847
0
  {
5848
0
    struct bfd_hash_entry *p;
5849
0
    struct elf_link_hash_entry *h;
5850
0
    unsigned int non_ir_ref_dynamic;
5851
5852
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5853
0
      {
5854
        /* Preserve non_ir_ref_dynamic so that this symbol
5855
     will be exported when the dynamic lib becomes needed
5856
     in the second pass.  */
5857
0
        h = (struct elf_link_hash_entry *) p;
5858
0
        if (h->root.type == bfd_link_hash_warning)
5859
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5860
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5861
5862
0
        h = (struct elf_link_hash_entry *) p;
5863
0
        memcpy (h, old_ent, htab->root.table.entsize);
5864
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5865
0
        if (h->root.type == bfd_link_hash_warning)
5866
0
    {
5867
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5868
0
      memcpy (h, old_ent, htab->root.table.entsize);
5869
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5870
0
    }
5871
0
        if (h->root.type == bfd_link_hash_common)
5872
0
    {
5873
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5874
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5875
0
    }
5876
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5877
0
      }
5878
0
  }
5879
5880
      /* Make a special call to the linker "notice" function to
5881
   tell it that symbols added for crefs may need to be removed.  */
5882
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5883
0
  goto error_free_vers;
5884
5885
0
      free (old_tab);
5886
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5887
0
         alloc_mark);
5888
0
      free (nondeflt_vers);
5889
0
      return true;
5890
0
    }
5891
5892
0
  free (old_strtab);
5893
0
  old_strtab = NULL;
5894
0
  if (old_tab != NULL)
5895
0
    {
5896
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5897
0
  goto error_free_vers;
5898
0
      free (old_tab);
5899
0
      old_tab = NULL;
5900
0
    }
5901
5902
  /* Now that all the symbols from this input file are created, if
5903
     not performing a relocatable link, handle .symver foo, foo@BAR
5904
     such that any relocs against foo become foo@BAR.  */
5905
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5906
0
    {
5907
0
      size_t cnt, symidx;
5908
5909
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5910
0
  {
5911
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5912
0
    char *shortname;
5913
0
    const char *p;
5914
0
    size_t amt;
5915
5916
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5917
0
    if (p == NULL
5918
0
        || (h->root.type != bfd_link_hash_defined
5919
0
      && h->root.type != bfd_link_hash_defweak))
5920
0
      continue;
5921
5922
0
    amt = p - h->root.root.string;
5923
0
    shortname = bfd_malloc (amt + 1);
5924
0
    if (!shortname)
5925
0
      goto error_free_vers;
5926
0
    memcpy (shortname, h->root.root.string, amt);
5927
0
    shortname[amt] = '\0';
5928
5929
0
    hi = (struct elf_link_hash_entry *)
5930
0
         bfd_link_hash_lookup (&htab->root, shortname,
5931
0
             false, false, false);
5932
0
    if (hi != NULL
5933
0
        && hi->root.type == h->root.type
5934
0
        && hi->root.u.def.value == h->root.u.def.value
5935
0
        && hi->root.u.def.section == h->root.u.def.section)
5936
0
      {
5937
0
        obed->elf_backend_hide_symbol (info, hi, true);
5938
0
        hi->root.type = bfd_link_hash_indirect;
5939
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5940
0
        obed->elf_backend_copy_indirect_symbol (info, h, hi);
5941
0
        sym_hash = elf_sym_hashes (abfd);
5942
0
        if (sym_hash)
5943
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5944
0
      if (sym_hash[symidx] == hi)
5945
0
        {
5946
0
          sym_hash[symidx] = h;
5947
0
          break;
5948
0
        }
5949
0
      }
5950
0
    free (shortname);
5951
0
  }
5952
0
    }
5953
0
  free (nondeflt_vers);
5954
0
  nondeflt_vers = NULL;
5955
5956
  /* Now set the alias field correctly for all the weak defined
5957
     symbols we found.  The only way to do this is to search all the
5958
     symbols.  Since we only need the information for non functions in
5959
     dynamic objects, that's the only time we actually put anything on
5960
     the list WEAKS.  We need this information so that if a regular
5961
     object refers to a symbol defined weakly in a dynamic object, the
5962
     real symbol in the dynamic object is also put in the dynamic
5963
     symbols; we also must arrange for both symbols to point to the
5964
     same memory location.  We could handle the general case of symbol
5965
     aliasing, but a general symbol alias can only be generated in
5966
     assembler code, handling it correctly would be very time
5967
     consuming, and other ELF linkers don't handle general aliasing
5968
     either.  */
5969
0
  if (weaks != NULL)
5970
0
    {
5971
0
      struct elf_link_hash_entry **hpp;
5972
0
      struct elf_link_hash_entry **hppend;
5973
0
      struct elf_link_hash_entry **sorted_sym_hash;
5974
0
      struct elf_link_hash_entry *h;
5975
0
      size_t sym_count;
5976
5977
      /* Since we have to search the whole symbol list for each weak
5978
   defined symbol, search time for N weak defined symbols will be
5979
   O(N^2). Binary search will cut it down to O(NlogN).  */
5980
0
      sorted_sym_hash = bfd_malloc (extsymcount * sizeof (*sorted_sym_hash));
5981
0
      if (sorted_sym_hash == NULL)
5982
0
  goto error_return;
5983
0
      sym_hash = sorted_sym_hash;
5984
0
      hpp = elf_sym_hashes (abfd);
5985
0
      hppend = hpp + extsymcount;
5986
0
      sym_count = 0;
5987
0
      for (; hpp < hppend; hpp++)
5988
0
  {
5989
0
    h = *hpp;
5990
0
    if (h != NULL
5991
0
        && h->root.type == bfd_link_hash_defined
5992
0
        && !bed->is_function_type (h->type))
5993
0
      {
5994
0
        *sym_hash = h;
5995
0
        sym_hash++;
5996
0
        sym_count++;
5997
0
      }
5998
0
  }
5999
6000
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6001
0
       elf_sort_symbol);
6002
6003
0
      while (weaks != NULL)
6004
0
  {
6005
0
    struct elf_link_hash_entry *hlook;
6006
0
    asection *slook;
6007
0
    bfd_vma vlook;
6008
0
    size_t i, j, idx = 0;
6009
6010
0
    hlook = weaks;
6011
0
    weaks = hlook->u.alias;
6012
0
    hlook->u.alias = NULL;
6013
6014
0
    if (hlook->root.type != bfd_link_hash_defined
6015
0
        && hlook->root.type != bfd_link_hash_defweak)
6016
0
      continue;
6017
6018
0
    slook = hlook->root.u.def.section;
6019
0
    vlook = hlook->root.u.def.value;
6020
6021
0
    i = 0;
6022
0
    j = sym_count;
6023
0
    while (i != j)
6024
0
      {
6025
0
        bfd_signed_vma vdiff;
6026
0
        idx = (i + j) / 2;
6027
0
        h = sorted_sym_hash[idx];
6028
0
        vdiff = vlook - h->root.u.def.value;
6029
0
        if (vdiff < 0)
6030
0
    j = idx;
6031
0
        else if (vdiff > 0)
6032
0
    i = idx + 1;
6033
0
        else
6034
0
    {
6035
0
      int sdiff = slook->id - h->root.u.def.section->id;
6036
0
      if (sdiff < 0)
6037
0
        j = idx;
6038
0
      else if (sdiff > 0)
6039
0
        i = idx + 1;
6040
0
      else
6041
0
        break;
6042
0
    }
6043
0
      }
6044
6045
    /* We didn't find a value/section match.  */
6046
0
    if (i == j)
6047
0
      continue;
6048
6049
    /* With multiple aliases, or when the weak symbol is already
6050
       strongly defined, we have multiple matching symbols and
6051
       the binary search above may land on any of them.  Step
6052
       one past the matching symbol(s).  */
6053
0
    while (++idx != j)
6054
0
      {
6055
0
        h = sorted_sym_hash[idx];
6056
0
        if (h->root.u.def.section != slook
6057
0
      || h->root.u.def.value != vlook)
6058
0
    break;
6059
0
      }
6060
6061
    /* Now look back over the aliases.  Since we sorted by size
6062
       as well as value and section, we'll choose the one with
6063
       the largest size.  */
6064
0
    while (idx-- != i)
6065
0
      {
6066
0
        h = sorted_sym_hash[idx];
6067
6068
        /* Stop if value or section doesn't match.  */
6069
0
        if (h->root.u.def.section != slook
6070
0
      || h->root.u.def.value != vlook)
6071
0
    break;
6072
0
        else if (h != hlook)
6073
0
    {
6074
0
      struct elf_link_hash_entry *t;
6075
6076
0
      hlook->u.alias = h;
6077
0
      hlook->is_weakalias = 1;
6078
0
      t = h;
6079
0
      if (t->u.alias != NULL)
6080
0
        while (t->u.alias != h)
6081
0
          t = t->u.alias;
6082
0
      t->u.alias = hlook;
6083
6084
      /* If the weak definition is in the list of dynamic
6085
         symbols, make sure the real definition is put
6086
         there as well.  */
6087
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6088
0
        {
6089
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6090
0
      {
6091
0
      err_free_sym_hash:
6092
0
        free (sorted_sym_hash);
6093
0
        goto error_return;
6094
0
      }
6095
0
        }
6096
6097
      /* If the real definition is in the list of dynamic
6098
         symbols, make sure the weak definition is put
6099
         there as well.  If we don't do this, then the
6100
         dynamic loader might not merge the entries for the
6101
         real definition and the weak definition.  */
6102
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6103
0
        {
6104
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6105
0
      goto err_free_sym_hash;
6106
0
        }
6107
0
      break;
6108
0
    }
6109
0
      }
6110
0
  }
6111
6112
0
      free (sorted_sym_hash);
6113
0
    }
6114
6115
0
  if (bed->check_directives
6116
0
      && !(*bed->check_directives) (abfd, info))
6117
0
    goto error_return;
6118
6119
  /* If this is a non-traditional link, try to optimize the handling
6120
     of the .stab/.stabstr sections.  */
6121
0
  if (! dynamic
6122
0
      && ! info->traditional_format
6123
0
      && is_elf_hash_table (&htab->root)
6124
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6125
0
    {
6126
0
      asection *stabstr;
6127
6128
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6129
0
      if (stabstr != NULL)
6130
0
  {
6131
0
    bfd_size_type string_offset = 0;
6132
0
    asection *stab;
6133
6134
0
    for (stab = abfd->sections; stab; stab = stab->next)
6135
0
      if (startswith (stab->name, ".stab")
6136
0
    && (!stab->name[5] ||
6137
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6138
0
    && (stab->flags & SEC_MERGE) == 0
6139
0
    && !bfd_is_abs_section (stab->output_section)
6140
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6141
0
               stabstr, &string_offset))
6142
0
        goto error_return;
6143
0
  }
6144
0
    }
6145
6146
0
  if (dynamic && add_needed)
6147
0
    {
6148
      /* Add this bfd to the loaded list.  */
6149
0
      struct elf_link_loaded_list *n;
6150
6151
0
      n = bfd_alloc (abfd, sizeof (*n));
6152
0
      if (n == NULL)
6153
0
  goto error_return;
6154
0
      n->abfd = abfd;
6155
0
      n->next = htab->dyn_loaded;
6156
0
      htab->dyn_loaded = n;
6157
0
    }
6158
0
  if (dynamic && !add_needed
6159
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6160
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6161
6162
0
  return true;
6163
6164
0
 error_free_vers:
6165
0
  free (old_tab);
6166
0
  free (old_strtab);
6167
0
  free (nondeflt_vers);
6168
0
  free (extversym);
6169
0
 error_free_sym:
6170
0
  free (isymbuf);
6171
0
 error_return:
6172
0
  return false;
6173
0
}
6174
6175
/* Return the linker hash table entry of a symbol that might be
6176
   satisfied by an archive symbol.  Return -1 on error.  */
6177
6178
struct bfd_link_hash_entry *
6179
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6180
        struct bfd_link_info *info,
6181
        const char *name)
6182
0
{
6183
0
  struct bfd_link_hash_entry *h;
6184
0
  const char *p;
6185
0
  char *copy;
6186
0
  size_t len, first;
6187
6188
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6189
0
  if (h != NULL)
6190
0
    return h;
6191
6192
  /* If this is a default version (the name contains @@), look up the
6193
     symbol again with only one `@' as well as without the version.
6194
     The effect is that references to the symbol with and without the
6195
     version will be matched by the default symbol in the archive.  */
6196
6197
0
  p = strchr (name, ELF_VER_CHR);
6198
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6199
0
    {
6200
      /* Add this symbol to first hash if this archive has the first
6201
   definition.  */
6202
0
      if (is_elf_hash_table (info->hash))
6203
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6204
0
      return h;
6205
0
    }
6206
6207
  /* First check with only one `@'.  */
6208
0
  len = strlen (name);
6209
0
  copy = bfd_alloc (abfd, len);
6210
0
  if (copy == NULL)
6211
0
    return (struct bfd_link_hash_entry *) -1;
6212
6213
0
  first = p - name + 1;
6214
0
  memcpy (copy, name, first);
6215
0
  memcpy (copy + first, name + first + 1, len - first);
6216
6217
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6218
0
  if (h == NULL)
6219
0
    {
6220
      /* We also need to check references to the symbol without the
6221
   version.  */
6222
0
      copy[first - 1] = '\0';
6223
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6224
0
    }
6225
6226
0
  bfd_release (abfd, copy);
6227
0
  return h;
6228
0
}
6229
6230
/* Add symbols from an ELF archive file to the linker hash table.  We
6231
   don't use _bfd_generic_link_add_archive_symbols because we need to
6232
   handle versioned symbols.
6233
6234
   Fortunately, ELF archive handling is simpler than that done by
6235
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6236
   oddities.  In ELF, if we find a symbol in the archive map, and the
6237
   symbol is currently undefined, we know that we must pull in that
6238
   object file.
6239
6240
   Unfortunately, we do have to make multiple passes over the symbol
6241
   table until nothing further is resolved.  */
6242
6243
static bool
6244
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6245
0
{
6246
0
  symindex c;
6247
0
  unsigned char *included = NULL;
6248
0
  carsym *symdefs;
6249
0
  bool loop;
6250
0
  elf_backend_data *bed;
6251
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6252
0
    (bfd *, struct bfd_link_info *, const char *);
6253
6254
0
  if (! bfd_has_map (abfd))
6255
0
    {
6256
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6257
6258
      /* An empty archive is a special case.  */
6259
0
      if (first_one == NULL)
6260
0
  return true;
6261
6262
0
      if (!_bfd_make_armap (abfd, first_one))
6263
0
  return false;
6264
0
    }
6265
6266
  /* Keep track of all symbols we know to be already defined, and all
6267
     files we know to be already included.  This is to speed up the
6268
     second and subsequent passes.  */
6269
0
  c = bfd_ardata (abfd)->symdef_count;
6270
0
  if (c == 0)
6271
0
    return true;
6272
0
  included = bfd_zmalloc (c * sizeof (*included));
6273
0
  if (included == NULL)
6274
0
    return false;
6275
6276
0
  symdefs = bfd_ardata (abfd)->symdefs;
6277
0
  bed = get_elf_backend_data (abfd);
6278
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6279
6280
0
  do
6281
0
    {
6282
0
      ufile_ptr_or_bfd last;
6283
0
      symindex i;
6284
0
      carsym *symdef;
6285
0
      carsym *symdefend;
6286
6287
0
      loop = false;
6288
0
      last = _bfd_elt_nil (abfd);
6289
6290
0
      symdef = symdefs;
6291
0
      symdefend = symdef + c;
6292
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6293
0
  {
6294
0
    struct bfd_link_hash_entry *h;
6295
0
    bfd *element;
6296
0
    struct bfd_link_hash_entry *undefs_tail;
6297
0
    symindex mark;
6298
6299
0
    if (included[i])
6300
0
      continue;
6301
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6302
0
      {
6303
0
        included[i] = true;
6304
0
        continue;
6305
0
      }
6306
6307
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6308
0
    if (h == (struct bfd_link_hash_entry *) -1)
6309
0
      goto error_return;
6310
6311
0
    if (h == NULL)
6312
0
      continue;
6313
6314
0
    if (h->type == bfd_link_hash_undefined)
6315
0
      {
6316
0
        if (is_elf_hash_table (info->hash))
6317
0
    {
6318
      /* If the archive element has already been loaded then one
6319
         of the symbols defined by that element might have been
6320
         made undefined due to being in a discarded section.  */
6321
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6322
0
        continue;
6323
6324
      /* In the pre-LTO-plugin pass we must not mistakenly
6325
         include this archive member if an earlier shared
6326
         library defined this symbol.  */
6327
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6328
0
      if (htab->first_hash)
6329
0
        {
6330
0
          struct elf_link_first_hash_entry *e
6331
0
        = ((struct elf_link_first_hash_entry *)
6332
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6333
0
                false, false));
6334
0
          if (e
6335
0
        && (e->abfd->flags & DYNAMIC) != 0
6336
0
        && e->abfd != abfd)
6337
0
      continue;
6338
0
        }
6339
0
    }
6340
0
      }
6341
0
    else if (h->type == bfd_link_hash_common)
6342
0
      {
6343
        /* We currently have a common symbol.  The archive map contains
6344
     a reference to this symbol, so we may want to include it.  We
6345
     only want to include it however, if this archive element
6346
     contains a definition of the symbol, not just another common
6347
     declaration of it.
6348
6349
     Unfortunately some archivers (including GNU ar) will put
6350
     declarations of common symbols into their archive maps, as
6351
     well as real definitions, so we cannot just go by the archive
6352
     map alone.  Instead we must read in the element's symbol
6353
     table and check that to see what kind of symbol definition
6354
     this is.  */
6355
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6356
0
    continue;
6357
0
      }
6358
0
    else
6359
0
      {
6360
0
        if (h->type != bfd_link_hash_undefweak)
6361
    /* Symbol must be defined.  Don't check it again.  */
6362
0
    included[i] = true;
6363
6364
0
        if (!is_elf_hash_table (info->hash))
6365
0
    continue;
6366
0
        struct elf_link_hash_entry *eh
6367
0
    = (struct elf_link_hash_entry *) h;
6368
        /* Ignore the archive if the symbol isn't referenced by a
6369
     regular object or isn't defined in a shared object.  */
6370
0
        if (!eh->ref_regular || !eh->def_dynamic)
6371
0
    continue;
6372
        /* Ignore the dynamic definition if symbol is first
6373
     defined in this archive.  */
6374
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6375
0
        if (htab->first_hash == NULL)
6376
0
    continue;
6377
0
        struct elf_link_first_hash_entry *e
6378
0
    = ((struct elf_link_first_hash_entry *)
6379
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6380
0
            false, false));
6381
0
        if (e == NULL || e->abfd != abfd)
6382
0
    continue;
6383
0
      }
6384
6385
    /* We need to include this archive member.  */
6386
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6387
0
    if (element == NULL)
6388
0
      goto error_return;
6389
6390
0
    if (! bfd_check_format (element, bfd_object))
6391
0
      goto error_return;
6392
6393
0
    undefs_tail = info->hash->undefs_tail;
6394
6395
0
    if (!(*info->callbacks
6396
0
    ->add_archive_element) (info, element, symdef->name, &element))
6397
0
      continue;
6398
0
    if (!bfd_link_add_symbols (element, info))
6399
0
      goto error_return;
6400
6401
    /* If there are any new undefined symbols, we need to make
6402
       another pass through the archive in order to see whether
6403
       they can be defined.  FIXME: This isn't perfect, because
6404
       common symbols wind up on undefs_tail and because an
6405
       undefined symbol which is defined later on in this pass
6406
       does not require another pass.  This isn't a bug, but it
6407
       does make the code less efficient than it could be.  */
6408
0
    if (undefs_tail != info->hash->undefs_tail)
6409
0
      loop = true;
6410
6411
    /* Look backward to mark all symbols from this object file
6412
       which we have already seen in this pass.  */
6413
0
    mark = i;
6414
0
    do
6415
0
      {
6416
0
        included[mark] = true;
6417
0
        if (mark == 0)
6418
0
    break;
6419
0
        --mark;
6420
0
      }
6421
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6422
6423
    /* We mark subsequent symbols from this object file as we go
6424
       on through the loop.  */
6425
0
    last = symdef->u;
6426
0
  }
6427
0
    }
6428
0
  while (loop);
6429
6430
0
  free (included);
6431
0
  return true;
6432
6433
0
 error_return:
6434
0
  free (included);
6435
0
  return false;
6436
0
}
6437
6438
/* Given an ELF BFD, add symbols to the global hash table as
6439
   appropriate.  */
6440
6441
bool
6442
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6443
0
{
6444
0
  switch (bfd_get_format (abfd))
6445
0
    {
6446
0
    case bfd_object:
6447
0
      return elf_link_add_object_symbols (abfd, info);
6448
0
    case bfd_archive:
6449
0
      return elf_link_add_archive_symbols (abfd, info);
6450
0
    default:
6451
0
      bfd_set_error (bfd_error_wrong_format);
6452
0
      return false;
6453
0
    }
6454
0
}
6455

6456
struct hash_codes_info
6457
{
6458
  unsigned long *hashcodes;
6459
  bool error;
6460
};
6461
6462
/* This function will be called though elf_link_hash_traverse to store
6463
   all hash value of the exported symbols in an array.  */
6464
6465
static bool
6466
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6467
0
{
6468
0
  struct hash_codes_info *inf = data;
6469
0
  const char *name;
6470
0
  unsigned long ha;
6471
0
  char *alc = NULL;
6472
6473
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6474
0
  if (h->dynindx == -1)
6475
0
    return true;
6476
6477
0
  name = h->root.root.string;
6478
0
  if (h->versioned >= versioned)
6479
0
    {
6480
0
      const char *p = strchr (name, ELF_VER_CHR);
6481
0
      if (p != NULL)
6482
0
  {
6483
0
    alc = bfd_malloc (p - name + 1);
6484
0
    if (alc == NULL)
6485
0
      {
6486
0
        inf->error = true;
6487
0
        return false;
6488
0
      }
6489
0
    memcpy (alc, name, p - name);
6490
0
    alc[p - name] = '\0';
6491
0
    name = alc;
6492
0
  }
6493
0
    }
6494
6495
  /* Compute the hash value.  */
6496
0
  ha = bfd_elf_hash (name);
6497
6498
  /* Store the found hash value in the array given as the argument.  */
6499
0
  *(inf->hashcodes)++ = ha;
6500
6501
  /* And store it in the struct so that we can put it in the hash table
6502
     later.  */
6503
0
  h->u.elf_hash_value = ha;
6504
6505
0
  free (alc);
6506
0
  return true;
6507
0
}
6508
6509
struct collect_gnu_hash_codes
6510
{
6511
  bfd *output_bfd;
6512
  elf_backend_data *bed;
6513
  unsigned long int nsyms;
6514
  unsigned long int maskbits;
6515
  unsigned long int *hashcodes;
6516
  unsigned long int *hashval;
6517
  unsigned long int *indx;
6518
  unsigned long int *counts;
6519
  bfd_vma *bitmask;
6520
  bfd_byte *contents;
6521
  bfd_size_type xlat;
6522
  long int min_dynindx;
6523
  unsigned long int bucketcount;
6524
  unsigned long int symindx;
6525
  long int local_indx;
6526
  long int shift1, shift2;
6527
  unsigned long int mask;
6528
  bool error;
6529
  bool base_symbol;
6530
};
6531
6532
/* This function will be called though elf_link_hash_traverse to store
6533
   all hash value of the exported symbols in an array.  */
6534
6535
static bool
6536
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6537
0
{
6538
0
  struct collect_gnu_hash_codes *s = data;
6539
0
  const char *name;
6540
0
  unsigned long ha;
6541
0
  char *alc = NULL;
6542
6543
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6544
0
  if (h->dynindx == -1)
6545
0
    return true;
6546
6547
  /* Ignore also local symbols and undefined symbols.  */
6548
0
  if (! (*s->bed->elf_hash_symbol) (h))
6549
0
    return true;
6550
6551
0
  name = h->root.root.string;
6552
0
  if (h->versioned >= versioned)
6553
0
    {
6554
0
      const char *p = strchr (name, ELF_VER_CHR);
6555
0
      if (p != NULL)
6556
0
  {
6557
0
    alc = bfd_malloc (p - name + 1);
6558
0
    if (alc == NULL)
6559
0
      {
6560
0
        s->error = true;
6561
0
        return false;
6562
0
      }
6563
0
    memcpy (alc, name, p - name);
6564
0
    alc[p - name] = '\0';
6565
0
    name = alc;
6566
0
  }
6567
0
    }
6568
6569
  /* Compute the hash value.  */
6570
0
  ha = bfd_elf_gnu_hash (name);
6571
6572
  /* Store the found hash value in the array for compute_bucket_count,
6573
     and also for .dynsym reordering purposes.  */
6574
0
  s->hashcodes[s->nsyms] = ha;
6575
0
  s->hashval[h->dynindx] = ha;
6576
0
  ++s->nsyms;
6577
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6578
0
    s->min_dynindx = h->dynindx;
6579
6580
0
  free (alc);
6581
0
  return true;
6582
0
}
6583
6584
/* This function will be called though elf_link_hash_traverse to do
6585
   final dynamic symbol renumbering in case of .gnu.hash.
6586
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6587
   to the translation table.  */
6588
6589
static bool
6590
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6591
0
{
6592
0
  struct collect_gnu_hash_codes *s = data;
6593
0
  unsigned long int bucket;
6594
0
  unsigned long int val;
6595
6596
  /* Ignore indirect symbols.  */
6597
0
  if (h->dynindx == -1)
6598
0
    return true;
6599
6600
  /* Skip if base symbol doesn't match.  */
6601
0
  if (s->base_symbol != !!h->base_symbol)
6602
0
    return true;
6603
6604
  /* Ignore also local symbols and undefined symbols.  */
6605
0
  if (! (*s->bed->elf_hash_symbol) (h))
6606
0
    {
6607
0
      if (h->dynindx >= s->min_dynindx)
6608
0
  {
6609
0
    if (s->bed->record_xhash_symbol != NULL)
6610
0
      {
6611
0
        (*s->bed->record_xhash_symbol) (h, 0);
6612
0
        s->local_indx++;
6613
0
      }
6614
0
    else
6615
0
      h->dynindx = s->local_indx++;
6616
0
  }
6617
0
      return true;
6618
0
    }
6619
6620
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6621
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6622
0
  & ((s->maskbits >> s->shift1) - 1);
6623
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6624
0
  s->bitmask[val]
6625
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6626
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6627
0
  if (s->counts[bucket] == 1)
6628
    /* Last element terminates the chain.  */
6629
0
    val |= 1;
6630
0
  bfd_put_32 (s->output_bfd, val,
6631
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6632
0
  --s->counts[bucket];
6633
0
  if (s->bed->record_xhash_symbol != NULL)
6634
0
    {
6635
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6636
6637
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6638
0
    }
6639
0
  else
6640
0
    h->dynindx = s->indx[bucket]++;
6641
0
  return true;
6642
0
}
6643
6644
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6645
6646
bool
6647
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6648
0
{
6649
0
  return !(h->forced_local
6650
0
     || h->root.type == bfd_link_hash_undefined
6651
0
     || h->root.type == bfd_link_hash_undefweak
6652
0
     || ((h->root.type == bfd_link_hash_defined
6653
0
    || h->root.type == bfd_link_hash_defweak)
6654
0
         && h->root.u.def.section->output_section == NULL));
6655
0
}
6656
6657
/* Array used to determine the number of hash table buckets to use
6658
   based on the number of symbols there are.  If there are fewer than
6659
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6660
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6661
   than 32771 buckets.  */
6662
6663
static const size_t elf_buckets[] =
6664
{
6665
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6666
  16411, 32771, 0
6667
};
6668
6669
/* Compute bucket count for hashing table.  We do not use a static set
6670
   of possible tables sizes anymore.  Instead we determine for all
6671
   possible reasonable sizes of the table the outcome (i.e., the
6672
   number of collisions etc) and choose the best solution.  The
6673
   weighting functions are not too simple to allow the table to grow
6674
   without bounds.  Instead one of the weighting factors is the size.
6675
   Therefore the result is always a good payoff between few collisions
6676
   (= short chain lengths) and table size.  */
6677
static size_t
6678
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6679
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6680
          unsigned long int nsyms,
6681
          int gnu_hash)
6682
0
{
6683
0
  size_t best_size = 0;
6684
0
  unsigned long int i;
6685
6686
0
  if (info->optimize)
6687
0
    {
6688
0
      size_t minsize;
6689
0
      size_t maxsize;
6690
0
      uint64_t best_chlen = ~((uint64_t) 0);
6691
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6692
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6693
0
      elf_backend_data *obed = get_elf_backend_data (dynobj);
6694
0
      unsigned long int *counts;
6695
0
      unsigned int no_improvement_count = 0;
6696
6697
      /* Possible optimization parameters: if we have NSYMS symbols we say
6698
   that the hashing table must at least have NSYMS/4 and at most
6699
   2*NSYMS buckets.  */
6700
0
      minsize = nsyms / 4;
6701
0
      if (minsize == 0)
6702
0
  minsize = 1;
6703
0
      best_size = maxsize = nsyms * 2;
6704
0
      if (gnu_hash)
6705
0
  {
6706
0
    if (minsize < 2)
6707
0
      minsize = 2;
6708
0
    if ((best_size & 31) == 0)
6709
0
      ++best_size;
6710
0
  }
6711
6712
      /* Create array where we count the collisions in.  We must use bfd_malloc
6713
   since the size could be large.  */
6714
0
      counts = bfd_malloc (maxsize * sizeof (*counts));
6715
0
      if (counts == NULL)
6716
0
  return 0;
6717
6718
      /* Compute the "optimal" size for the hash table.  The criteria is a
6719
   minimal chain length.  The minor criteria is (of course) the size
6720
   of the table.  */
6721
0
      for (i = minsize; i < maxsize; ++i)
6722
0
  {
6723
    /* Walk through the array of hashcodes and count the collisions.  */
6724
0
    uint64_t max;
6725
0
    unsigned long int j;
6726
0
    unsigned long int fact;
6727
6728
0
    if (gnu_hash && (i & 31) == 0)
6729
0
      continue;
6730
6731
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6732
6733
    /* Determine how often each hash bucket is used.  */
6734
0
    for (j = 0; j < nsyms; ++j)
6735
0
      ++counts[hashcodes[j] % i];
6736
6737
    /* For the weight function we need some information about the
6738
       pagesize on the target.  This is information need not be 100%
6739
       accurate.  Since this information is not available (so far) we
6740
       define it here to a reasonable default value.  If it is crucial
6741
       to have a better value some day simply define this value.  */
6742
0
# ifndef BFD_TARGET_PAGESIZE
6743
0
#  define BFD_TARGET_PAGESIZE (4096)
6744
0
# endif
6745
6746
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6747
       and the chains.  */
6748
0
    max = (2 + dynsymcount) * obed->s->sizeof_hash_entry;
6749
6750
0
# if 1
6751
    /* Variant 1: optimize for short chains.  We add the squares
6752
       of all the chain lengths (which favors many small chain
6753
       over a few long chains).  */
6754
0
    for (j = 0; j < i; ++j)
6755
0
      max += counts[j] * counts[j];
6756
6757
    /* This adds penalties for the overall size of the table.  */
6758
0
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6759
0
    max *= fact * fact;
6760
# else
6761
    /* Variant 2: Optimize a lot more for small table.  Here we
6762
       also add squares of the size but we also add penalties for
6763
       empty slots (the +1 term).  */
6764
    for (j = 0; j < i; ++j)
6765
      max += (1 + counts[j]) * (1 + counts[j]);
6766
6767
    /* The overall size of the table is considered, but not as
6768
       strong as in variant 1, where it is squared.  */
6769
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6770
    max *= fact;
6771
# endif
6772
6773
    /* Compare with current best results.  */
6774
0
    if (max < best_chlen)
6775
0
      {
6776
0
        best_chlen = max;
6777
0
        best_size = i;
6778
0
        no_improvement_count = 0;
6779
0
      }
6780
    /* PR 11843: Avoid futile long searches for the best bucket size
6781
       when there are a large number of symbols.  */
6782
0
    else if (++no_improvement_count == 100)
6783
0
      break;
6784
0
  }
6785
6786
0
      free (counts);
6787
0
    }
6788
0
  else
6789
0
    {
6790
0
      for (i = 0; elf_buckets[i] != 0; i++)
6791
0
  {
6792
0
    best_size = elf_buckets[i];
6793
0
    if (nsyms < elf_buckets[i + 1])
6794
0
      break;
6795
0
  }
6796
0
      if (gnu_hash && best_size < 2)
6797
0
  best_size = 2;
6798
0
    }
6799
6800
0
  return best_size;
6801
0
}
6802
6803
/* Size any SHT_GROUP section for ld -r.  */
6804
6805
bool
6806
bfd_elf_size_group_sections (struct bfd_link_info *info)
6807
0
{
6808
0
  bfd *ibfd;
6809
0
  asection *s;
6810
6811
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6812
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6813
0
  && (s = ibfd->sections) != NULL
6814
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6815
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6816
0
      return false;
6817
0
  return true;
6818
0
}
6819
6820
/* Set a default stack segment size.  The value in INFO wins.  If it
6821
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6822
   undefined it is initialized.  */
6823
6824
bool
6825
bfd_elf_stack_segment_size (struct bfd_link_info *info,
6826
          const char *legacy_symbol,
6827
          bfd_vma default_size)
6828
0
{
6829
0
  struct elf_link_hash_entry *h = NULL;
6830
6831
  /* Look for legacy symbol.  */
6832
0
  if (legacy_symbol)
6833
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6834
0
            false, false, false);
6835
0
  if (h && (h->root.type == bfd_link_hash_defined
6836
0
      || h->root.type == bfd_link_hash_defweak)
6837
0
      && h->def_regular
6838
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6839
0
    {
6840
      /* The symbol has no type if specified on the command line.  */
6841
0
      h->type = STT_OBJECT;
6842
0
      if (info->stacksize)
6843
  /* xgettext:c-format */
6844
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6845
0
          info->output_bfd, legacy_symbol);
6846
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6847
  /* xgettext:c-format */
6848
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6849
0
          info->output_bfd, legacy_symbol);
6850
0
      else
6851
0
  info->stacksize = h->root.u.def.value;
6852
0
    }
6853
6854
0
  if (!info->stacksize)
6855
    /* If the user didn't set a size, or explicitly inhibit the
6856
       size, set it now.  */
6857
0
    info->stacksize = default_size;
6858
6859
  /* Provide the legacy symbol, if it is referenced.  */
6860
0
  if (h && (h->root.type == bfd_link_hash_undefined
6861
0
      || h->root.type == bfd_link_hash_undefweak))
6862
0
    {
6863
0
      struct bfd_link_hash_entry *bh = NULL;
6864
6865
0
      if (!(_bfd_generic_link_add_one_symbol
6866
0
      (info, info->output_bfd, legacy_symbol,
6867
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6868
0
       info->stacksize >= 0 ? info->stacksize : 0,
6869
0
       NULL, false, get_elf_backend_data (info->output_bfd)->collect,
6870
0
       &bh)))
6871
0
  return false;
6872
6873
0
      h = (struct elf_link_hash_entry *) bh;
6874
0
      h->def_regular = 1;
6875
0
      h->type = STT_OBJECT;
6876
0
    }
6877
6878
0
  return true;
6879
0
}
6880
6881
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6882
6883
struct elf_gc_sweep_symbol_info
6884
{
6885
  struct bfd_link_info *info;
6886
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6887
           bool);
6888
};
6889
6890
static bool
6891
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6892
0
{
6893
0
  if (!h->mark
6894
0
      && (((h->root.type == bfd_link_hash_defined
6895
0
      || h->root.type == bfd_link_hash_defweak)
6896
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6897
0
    && h->root.u.def.section->gc_mark))
6898
0
    || h->root.type == bfd_link_hash_undefined
6899
0
    || h->root.type == bfd_link_hash_undefweak))
6900
0
    {
6901
0
      struct elf_gc_sweep_symbol_info *inf = data;
6902
6903
0
      (*inf->hide_symbol) (inf->info, h, true);
6904
0
      h->def_regular = 0;
6905
0
      h->ref_regular = 0;
6906
0
      h->ref_regular_nonweak = 0;
6907
0
    }
6908
6909
0
  return true;
6910
0
}
6911
6912
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6913
   called by the ELF linker emulation before_allocation routine.  We
6914
   must set the sizes of the sections before the linker sets the
6915
   addresses of the various sections.  */
6916
6917
bool
6918
bfd_elf_size_dynamic_sections (struct bfd_link_info *info,
6919
             const char *soname,
6920
             const char *rpath,
6921
             const char *filter_shlib,
6922
             const char *audit,
6923
             const char *depaudit,
6924
             const char * const *auxiliary_filters,
6925
             asection **sinterpptr)
6926
0
{
6927
0
  bfd *dynobj;
6928
0
  elf_backend_data *obed;
6929
6930
0
  *sinterpptr = NULL;
6931
6932
0
  if (!is_elf_hash_table (info->hash))
6933
0
    return true;
6934
6935
  /* Any syms created from now on start with -1 in
6936
     got.refcount/offset and plt.refcount/offset.  */
6937
0
  elf_hash_table (info)->init_got_refcount
6938
0
    = elf_hash_table (info)->init_got_offset;
6939
0
  elf_hash_table (info)->init_plt_refcount
6940
0
    = elf_hash_table (info)->init_plt_offset;
6941
6942
0
  obed = get_elf_backend_data (info->output_bfd);
6943
6944
  /* The backend may have to create some sections regardless of whether
6945
     we're dynamic or not.  */
6946
0
  if (obed->elf_backend_early_size_sections
6947
0
      && !obed->elf_backend_early_size_sections (info))
6948
0
    return false;
6949
6950
0
  dynobj = elf_hash_table (info)->dynobj;
6951
6952
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6953
0
    {
6954
0
      struct bfd_elf_version_tree *verdefs;
6955
0
      struct elf_info_failed asvinfo;
6956
0
      struct bfd_elf_version_tree *t;
6957
0
      struct bfd_elf_version_expr *d;
6958
0
      asection *s;
6959
0
      size_t soname_indx;
6960
6961
      /* If we are supposed to export all symbols into the dynamic symbol
6962
   table (this is not the normal case), then do so.  */
6963
0
      if (info->export_dynamic
6964
0
    || (bfd_link_executable (info) && info->dynamic))
6965
0
  {
6966
0
    struct elf_info_failed eif;
6967
6968
0
    eif.info = info;
6969
0
    eif.failed = false;
6970
0
    elf_link_hash_traverse (elf_hash_table (info),
6971
0
          _bfd_elf_export_symbol,
6972
0
          &eif);
6973
0
    if (eif.failed)
6974
0
      return false;
6975
0
  }
6976
6977
0
      if (soname != NULL)
6978
0
  {
6979
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6980
0
               soname, true);
6981
0
    if (soname_indx == (size_t) -1
6982
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6983
0
      return false;
6984
0
  }
6985
0
      else
6986
0
  soname_indx = (size_t) -1;
6987
6988
      /* Make all global versions with definition.  */
6989
0
      for (t = info->version_info; t != NULL; t = t->next)
6990
0
  for (d = t->globals.list; d != NULL; d = d->next)
6991
0
    if (!d->symver && d->literal)
6992
0
      {
6993
0
        const char *verstr, *name;
6994
0
        size_t namelen, verlen, newlen;
6995
0
        char *newname, *p, leading_char;
6996
0
        struct elf_link_hash_entry *newh;
6997
6998
0
        leading_char = bfd_get_symbol_leading_char (info->output_bfd);
6999
0
        name = d->pattern;
7000
0
        namelen = strlen (name) + (leading_char != '\0');
7001
0
        verstr = t->name;
7002
0
        verlen = strlen (verstr);
7003
0
        newlen = namelen + verlen + 3;
7004
7005
0
        newname = bfd_malloc (newlen);
7006
0
        if (newname == NULL)
7007
0
    return false;
7008
0
        newname[0] = leading_char;
7009
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7010
7011
        /* Check the hidden versioned definition.  */
7012
0
        p = newname + namelen;
7013
0
        *p++ = ELF_VER_CHR;
7014
0
        memcpy (p, verstr, verlen + 1);
7015
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7016
0
             newname, false, false,
7017
0
             false);
7018
0
        if (newh == NULL
7019
0
      || (newh->root.type != bfd_link_hash_defined
7020
0
          && newh->root.type != bfd_link_hash_defweak))
7021
0
    {
7022
      /* Check the default versioned definition.  */
7023
0
      *p++ = ELF_VER_CHR;
7024
0
      memcpy (p, verstr, verlen + 1);
7025
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7026
0
                 newname, false, false,
7027
0
                 false);
7028
0
    }
7029
0
        free (newname);
7030
7031
        /* Mark this version if there is a definition and it is
7032
     not defined in a shared object.  */
7033
0
        if (newh != NULL
7034
0
      && !newh->def_dynamic
7035
0
      && (newh->root.type == bfd_link_hash_defined
7036
0
          || newh->root.type == bfd_link_hash_defweak))
7037
0
    d->symver = 1;
7038
0
      }
7039
7040
      /* Attach all the symbols to their version information.  */
7041
0
      asvinfo.info = info;
7042
0
      asvinfo.failed = false;
7043
7044
0
      elf_link_hash_traverse (elf_hash_table (info),
7045
0
            _bfd_elf_link_assign_sym_version,
7046
0
            &asvinfo);
7047
0
      if (asvinfo.failed)
7048
0
  return false;
7049
7050
0
      if (!info->allow_undefined_version)
7051
0
  {
7052
    /* Check if all global versions have a definition.  */
7053
0
    bool all_defined = true;
7054
0
    for (t = info->version_info; t != NULL; t = t->next)
7055
0
      for (d = t->globals.list; d != NULL; d = d->next)
7056
0
        if (d->literal && !d->symver && !d->script)
7057
0
    {
7058
0
      _bfd_error_handler
7059
0
        (_("%s: undefined version: %s"),
7060
0
         d->pattern, t->name);
7061
0
      all_defined = false;
7062
0
    }
7063
7064
0
    if (!all_defined)
7065
0
      {
7066
0
        bfd_set_error (bfd_error_bad_value);
7067
0
        return false;
7068
0
      }
7069
0
  }
7070
7071
      /* Set up the version definition section.  */
7072
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7073
0
      BFD_ASSERT (s != NULL);
7074
7075
      /* We may have created additional version definitions if we are
7076
   just linking a regular application.  */
7077
0
      verdefs = info->version_info;
7078
7079
      /* Skip anonymous version tag.  */
7080
0
      if (verdefs != NULL && verdefs->vernum == 0)
7081
0
  verdefs = verdefs->next;
7082
7083
0
      if (verdefs == NULL && !info->create_default_symver)
7084
0
  s->flags |= SEC_EXCLUDE;
7085
0
      else
7086
0
  {
7087
0
    unsigned int cdefs;
7088
0
    bfd_size_type size;
7089
0
    bfd_byte *p;
7090
0
    Elf_Internal_Verdef def;
7091
0
    Elf_Internal_Verdaux defaux;
7092
0
    struct bfd_link_hash_entry *bh;
7093
0
    struct elf_link_hash_entry *h;
7094
0
    const char *name;
7095
7096
0
    cdefs = 0;
7097
0
    size = 0;
7098
7099
    /* Make space for the base version.  */
7100
0
    size += sizeof (Elf_External_Verdef);
7101
0
    size += sizeof (Elf_External_Verdaux);
7102
0
    ++cdefs;
7103
7104
    /* Make space for the default version.  */
7105
0
    if (info->create_default_symver)
7106
0
      {
7107
0
        size += sizeof (Elf_External_Verdef);
7108
0
        ++cdefs;
7109
0
      }
7110
7111
0
    for (t = verdefs; t != NULL; t = t->next)
7112
0
      {
7113
0
        struct bfd_elf_version_deps *n;
7114
7115
        /* Don't emit base version twice.  */
7116
0
        if (t->vernum == 0)
7117
0
    continue;
7118
7119
0
        size += sizeof (Elf_External_Verdef);
7120
0
        size += sizeof (Elf_External_Verdaux);
7121
0
        ++cdefs;
7122
7123
0
        for (n = t->deps; n != NULL; n = n->next)
7124
0
    size += sizeof (Elf_External_Verdaux);
7125
0
      }
7126
7127
0
    s->size = size;
7128
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7129
0
    if (s->contents == NULL && s->size != 0)
7130
0
      return false;
7131
0
    s->alloced = 1;
7132
7133
    /* Fill in the version definition section.  */
7134
7135
0
    p = s->contents;
7136
7137
0
    def.vd_version = VER_DEF_CURRENT;
7138
0
    def.vd_flags = VER_FLG_BASE;
7139
0
    def.vd_ndx = 1;
7140
0
    def.vd_cnt = 1;
7141
0
    if (info->create_default_symver)
7142
0
      {
7143
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7144
0
        def.vd_next = sizeof (Elf_External_Verdef);
7145
0
      }
7146
0
    else
7147
0
      {
7148
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7149
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7150
0
           + sizeof (Elf_External_Verdaux));
7151
0
      }
7152
7153
0
    if (soname_indx != (size_t) -1)
7154
0
      {
7155
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7156
0
              soname_indx);
7157
0
        def.vd_hash = bfd_elf_hash (soname);
7158
0
        defaux.vda_name = soname_indx;
7159
0
        name = soname;
7160
0
      }
7161
0
    else
7162
0
      {
7163
0
        size_t indx;
7164
7165
0
        name = lbasename (bfd_get_filename (info->output_bfd));
7166
0
        def.vd_hash = bfd_elf_hash (name);
7167
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7168
0
            name, false);
7169
0
        if (indx == (size_t) -1)
7170
0
    return false;
7171
0
        defaux.vda_name = indx;
7172
0
      }
7173
0
    defaux.vda_next = 0;
7174
7175
0
    _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7176
0
            (Elf_External_Verdef *) p);
7177
0
    p += sizeof (Elf_External_Verdef);
7178
0
    if (info->create_default_symver)
7179
0
      {
7180
        /* Add a symbol representing this version.  */
7181
0
        bh = NULL;
7182
0
        if (! (_bfd_generic_link_add_one_symbol
7183
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7184
0
          0, NULL, false,
7185
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7186
0
    return false;
7187
0
        h = (struct elf_link_hash_entry *) bh;
7188
0
        h->non_elf = 0;
7189
0
        h->def_regular = 1;
7190
0
        h->type = STT_OBJECT;
7191
0
        h->verinfo.vertree = NULL;
7192
7193
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7194
0
    return false;
7195
7196
        /* Create a duplicate of the base version with the same
7197
     aux block, but different flags.  */
7198
0
        def.vd_flags = 0;
7199
0
        def.vd_ndx = 2;
7200
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7201
0
        if (verdefs)
7202
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7203
0
             + sizeof (Elf_External_Verdaux));
7204
0
        else
7205
0
    def.vd_next = 0;
7206
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7207
0
          (Elf_External_Verdef *) p);
7208
0
        p += sizeof (Elf_External_Verdef);
7209
0
      }
7210
0
    _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7211
0
             (Elf_External_Verdaux *) p);
7212
0
    p += sizeof (Elf_External_Verdaux);
7213
7214
0
    for (t = verdefs; t != NULL; t = t->next)
7215
0
      {
7216
0
        unsigned int cdeps;
7217
0
        struct bfd_elf_version_deps *n;
7218
7219
        /* Don't emit the base version twice.  */
7220
0
        if (t->vernum == 0)
7221
0
    continue;
7222
7223
0
        cdeps = 0;
7224
0
        for (n = t->deps; n != NULL; n = n->next)
7225
0
    ++cdeps;
7226
7227
        /* Add a symbol representing this version.  */
7228
0
        bh = NULL;
7229
0
        if (! (_bfd_generic_link_add_one_symbol
7230
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7231
0
          0, NULL, false,
7232
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7233
0
    return false;
7234
0
        h = (struct elf_link_hash_entry *) bh;
7235
0
        h->non_elf = 0;
7236
0
        h->def_regular = 1;
7237
0
        h->type = STT_OBJECT;
7238
0
        h->verinfo.vertree = t;
7239
7240
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7241
0
    return false;
7242
7243
0
        def.vd_version = VER_DEF_CURRENT;
7244
0
        def.vd_flags = 0;
7245
0
        if (t->globals.list == NULL
7246
0
      && t->locals.list == NULL
7247
0
      && ! t->used)
7248
0
    def.vd_flags |= VER_FLG_WEAK;
7249
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7250
0
        def.vd_cnt = cdeps + 1;
7251
0
        def.vd_hash = bfd_elf_hash (t->name);
7252
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7253
0
        def.vd_next = 0;
7254
7255
        /* If a basever node is next, it *must* be the last node in
7256
     the chain, otherwise Verdef construction breaks.  */
7257
0
        if (t->next != NULL && t->next->vernum == 0)
7258
0
    BFD_ASSERT (t->next->next == NULL);
7259
7260
0
        if (t->next != NULL && t->next->vernum != 0)
7261
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7262
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7263
7264
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7265
0
          (Elf_External_Verdef *) p);
7266
0
        p += sizeof (Elf_External_Verdef);
7267
7268
0
        defaux.vda_name = h->dynstr_index;
7269
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7270
0
              h->dynstr_index);
7271
0
        defaux.vda_next = 0;
7272
0
        if (t->deps != NULL)
7273
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7274
0
        t->name_indx = defaux.vda_name;
7275
7276
0
        _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7277
0
           (Elf_External_Verdaux *) p);
7278
0
        p += sizeof (Elf_External_Verdaux);
7279
7280
0
        for (n = t->deps; n != NULL; n = n->next)
7281
0
    {
7282
0
      if (n->version_needed == NULL)
7283
0
        {
7284
          /* This can happen if there was an error in the
7285
       version script.  */
7286
0
          defaux.vda_name = 0;
7287
0
        }
7288
0
      else
7289
0
        {
7290
0
          defaux.vda_name = n->version_needed->name_indx;
7291
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7292
0
                defaux.vda_name);
7293
0
        }
7294
0
      if (n->next == NULL)
7295
0
        defaux.vda_next = 0;
7296
0
      else
7297
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7298
7299
0
      _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7300
0
               (Elf_External_Verdaux *) p);
7301
0
      p += sizeof (Elf_External_Verdaux);
7302
0
    }
7303
0
      }
7304
7305
0
    elf_tdata (info->output_bfd)->cverdefs = cdefs;
7306
0
  }
7307
0
    }
7308
7309
0
  if (info->gc_sections && obed->can_gc_sections)
7310
0
    {
7311
0
      struct elf_gc_sweep_symbol_info sweep_info;
7312
7313
      /* Remove the symbols that were in the swept sections from the
7314
   dynamic symbol table.  */
7315
0
      sweep_info.info = info;
7316
0
      sweep_info.hide_symbol = obed->elf_backend_hide_symbol;
7317
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7318
0
            &sweep_info);
7319
0
    }
7320
7321
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7322
0
    {
7323
0
      asection *s;
7324
0
      struct elf_find_verdep_info sinfo;
7325
7326
      /* Work out the size of the version reference section.  */
7327
7328
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7329
0
      BFD_ASSERT (s != NULL);
7330
7331
0
      sinfo.info = info;
7332
0
      sinfo.vers = elf_tdata (info->output_bfd)->cverdefs;
7333
0
      if (sinfo.vers == 0)
7334
0
  sinfo.vers = 1;
7335
0
      sinfo.failed = false;
7336
7337
0
      elf_link_hash_traverse (elf_hash_table (info),
7338
0
            _bfd_elf_link_find_version_dependencies,
7339
0
            &sinfo);
7340
0
      if (sinfo.failed)
7341
0
  return false;
7342
7343
0
      obed->elf_backend_add_glibc_version_dependency (&sinfo);
7344
0
      if (sinfo.failed)
7345
0
  return false;
7346
7347
0
      if (elf_tdata (info->output_bfd)->verref == NULL)
7348
0
  s->flags |= SEC_EXCLUDE;
7349
0
      else
7350
0
  {
7351
0
    Elf_Internal_Verneed *vn;
7352
0
    unsigned int size;
7353
0
    unsigned int crefs;
7354
0
    bfd_byte *p;
7355
7356
    /* Build the version dependency section.  */
7357
0
    size = 0;
7358
0
    crefs = 0;
7359
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7360
0
         vn != NULL;
7361
0
         vn = vn->vn_nextref)
7362
0
      {
7363
0
        Elf_Internal_Vernaux *a;
7364
7365
0
        size += sizeof (Elf_External_Verneed);
7366
0
        ++crefs;
7367
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7368
0
    size += sizeof (Elf_External_Vernaux);
7369
0
      }
7370
7371
0
    s->size = size;
7372
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7373
0
    if (s->contents == NULL)
7374
0
      return false;
7375
0
    s->alloced = 1;
7376
7377
0
    p = s->contents;
7378
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7379
0
         vn != NULL;
7380
0
         vn = vn->vn_nextref)
7381
0
      {
7382
0
        unsigned int caux;
7383
0
        Elf_Internal_Vernaux *a;
7384
0
        size_t indx;
7385
7386
0
        caux = 0;
7387
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7388
0
    ++caux;
7389
7390
0
        vn->vn_version = VER_NEED_CURRENT;
7391
0
        vn->vn_cnt = caux;
7392
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7393
0
            elf_dt_name (vn->vn_bfd) != NULL
7394
0
            ? elf_dt_name (vn->vn_bfd)
7395
0
            : lbasename (bfd_get_filename
7396
0
                   (vn->vn_bfd)),
7397
0
            false);
7398
0
        if (indx == (size_t) -1)
7399
0
    return false;
7400
0
        vn->vn_file = indx;
7401
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7402
0
        if (vn->vn_nextref == NULL)
7403
0
    vn->vn_next = 0;
7404
0
        else
7405
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7406
0
             + caux * sizeof (Elf_External_Vernaux));
7407
7408
0
        _bfd_elf_swap_verneed_out (info->output_bfd, vn,
7409
0
           (Elf_External_Verneed *) p);
7410
0
        p += sizeof (Elf_External_Verneed);
7411
7412
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7413
0
    {
7414
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7415
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7416
0
                a->vna_nodename, false);
7417
0
      if (indx == (size_t) -1)
7418
0
        return false;
7419
0
      a->vna_name = indx;
7420
0
      if (a->vna_nextptr == NULL)
7421
0
        a->vna_next = 0;
7422
0
      else
7423
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7424
7425
0
      _bfd_elf_swap_vernaux_out (info->output_bfd, a,
7426
0
               (Elf_External_Vernaux *) p);
7427
0
      p += sizeof (Elf_External_Vernaux);
7428
0
    }
7429
0
      }
7430
7431
0
    elf_tdata (info->output_bfd)->cverrefs = crefs;
7432
0
  }
7433
0
    }
7434
7435
0
  if (bfd_link_relocatable (info)
7436
0
      && !bfd_elf_size_group_sections (info))
7437
0
    return false;
7438
7439
  /* Determine any GNU_STACK segment requirements, after the backend
7440
     has had a chance to set a default segment size.  */
7441
0
  if (info->execstack)
7442
0
    {
7443
      /* If the user has explicitly requested warnings, then generate one even
7444
   though the choice is the result of another command line option.  */
7445
0
      if (info->warn_execstack == 1)
7446
0
  {
7447
0
    if (info->error_execstack)
7448
0
      {
7449
0
        _bfd_error_handler
7450
0
    (_("\
7451
0
error: creating an executable stack because of -z execstack command line option"));
7452
0
        return false;
7453
0
      }
7454
7455
0
    _bfd_error_handler
7456
0
      (_("\
7457
0
warning: enabling an executable stack because of -z execstack command line option"));
7458
0
  }
7459
7460
0
      elf_stack_flags (info->output_bfd) = PF_R | PF_W | PF_X;
7461
0
    }
7462
0
  else if (info->noexecstack)
7463
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W;
7464
0
  else
7465
0
    {
7466
0
      bfd *inputobj;
7467
0
      asection *notesec = NULL;
7468
0
      bfd *noteobj = NULL;
7469
0
      bfd *emptyobj = NULL;
7470
0
      int exec = 0;
7471
7472
0
      for (inputobj = info->input_bfds;
7473
0
     inputobj;
7474
0
     inputobj = inputobj->link.next)
7475
0
  {
7476
0
    asection *s;
7477
7478
0
    if (inputobj->flags
7479
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7480
0
      continue;
7481
0
    s = inputobj->sections;
7482
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7483
0
      continue;
7484
7485
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7486
0
    if (s)
7487
0
      {
7488
0
        notesec = s;
7489
0
        if (s->flags & SEC_CODE)
7490
0
    {
7491
0
      noteobj = inputobj;
7492
0
      exec = PF_X;
7493
      /* There is no point in scanning the remaining bfds.  */
7494
0
      break;
7495
0
    }
7496
0
      }
7497
0
    else if (obed->default_execstack && info->default_execstack)
7498
0
      {
7499
0
        exec = PF_X;
7500
0
        emptyobj = inputobj;
7501
0
      }
7502
0
  }
7503
7504
0
      if (notesec || info->stacksize > 0)
7505
0
  {
7506
0
    if (exec)
7507
0
      {
7508
0
        if (info->warn_execstack != 0)
7509
0
    {
7510
      /* PR 29072: Because an executable stack is a serious
7511
         security risk, make sure that the user knows that it is
7512
         being enabled despite the fact that it was not requested
7513
         on the command line.  */
7514
0
      if (noteobj)
7515
0
        {
7516
0
          if (info->error_execstack)
7517
0
      {
7518
0
        _bfd_error_handler (_("\
7519
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7520
0
                bfd_get_filename (noteobj));
7521
0
        return false;
7522
0
      }
7523
7524
0
          _bfd_error_handler (_("\
7525
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7526
0
           bfd_get_filename (noteobj));
7527
0
        }
7528
0
      else if (emptyobj)
7529
0
        {
7530
0
          if (info->error_execstack)
7531
0
      {
7532
0
        _bfd_error_handler (_("\
7533
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7534
0
                bfd_get_filename (emptyobj));
7535
0
        return false;
7536
0
      }
7537
7538
0
          _bfd_error_handler (_("\
7539
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7540
0
            bfd_get_filename (emptyobj));
7541
0
          _bfd_error_handler (_("\
7542
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7543
0
        }
7544
0
    }
7545
0
      }
7546
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W | exec;
7547
0
  }
7548
7549
0
      if (notesec && exec && bfd_link_relocatable (info)
7550
0
    && notesec->output_section != bfd_abs_section_ptr)
7551
0
  notesec->output_section->flags |= SEC_CODE;
7552
0
    }
7553
7554
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7555
0
    {
7556
0
      struct elf_info_failed eif;
7557
0
      struct elf_link_hash_entry *h;
7558
0
      asection *dynstr;
7559
0
      asection *s;
7560
7561
0
      *sinterpptr = elf_hash_table (info)->interp;
7562
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7563
7564
0
      if (info->symbolic)
7565
0
  {
7566
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7567
0
      return false;
7568
0
    info->flags |= DF_SYMBOLIC;
7569
0
  }
7570
7571
0
      if (rpath != NULL)
7572
0
  {
7573
0
    size_t indx;
7574
0
    bfd_vma tag;
7575
7576
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7577
0
              true);
7578
0
    if (indx == (size_t) -1)
7579
0
      return false;
7580
7581
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7582
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7583
0
      return false;
7584
0
  }
7585
7586
0
      if (filter_shlib != NULL)
7587
0
  {
7588
0
    size_t indx;
7589
7590
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7591
0
              filter_shlib, true);
7592
0
    if (indx == (size_t) -1
7593
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7594
0
      return false;
7595
0
  }
7596
7597
0
      if (auxiliary_filters != NULL)
7598
0
  {
7599
0
    const char * const *p;
7600
7601
0
    for (p = auxiliary_filters; *p != NULL; p++)
7602
0
      {
7603
0
        size_t indx;
7604
7605
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7606
0
            *p, true);
7607
0
        if (indx == (size_t) -1
7608
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7609
0
    return false;
7610
0
      }
7611
0
  }
7612
7613
0
      if (audit != NULL)
7614
0
  {
7615
0
    size_t indx;
7616
7617
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7618
0
              true);
7619
0
    if (indx == (size_t) -1
7620
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7621
0
      return false;
7622
0
  }
7623
7624
0
      if (depaudit != NULL)
7625
0
  {
7626
0
    size_t indx;
7627
7628
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7629
0
              true);
7630
0
    if (indx == (size_t) -1
7631
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7632
0
      return false;
7633
0
  }
7634
7635
0
      eif.info = info;
7636
0
      eif.failed = false;
7637
7638
      /* Find all symbols which were defined in a dynamic object and make
7639
   the backend pick a reasonable value for them.  */
7640
0
      elf_link_hash_traverse (elf_hash_table (info),
7641
0
            _bfd_elf_adjust_dynamic_symbol,
7642
0
            &eif);
7643
0
      if (eif.failed)
7644
0
  return false;
7645
7646
      /* Add some entries to the .dynamic section.  We fill in some of the
7647
   values later, in bfd_elf_final_link, but we must add the entries
7648
   now so that we know the final size of the .dynamic section.  */
7649
7650
      /* If there are initialization and/or finalization functions to
7651
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7652
0
      h = (info->init_function
7653
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7654
0
           info->init_function, false,
7655
0
           false, false)
7656
0
     : NULL);
7657
0
      if (h != NULL
7658
0
    && (h->ref_regular
7659
0
        || h->def_regular))
7660
0
  {
7661
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7662
0
      return false;
7663
0
  }
7664
0
      h = (info->fini_function
7665
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7666
0
           info->fini_function, false,
7667
0
           false, false)
7668
0
     : NULL);
7669
0
      if (h != NULL
7670
0
    && (h->ref_regular
7671
0
        || h->def_regular))
7672
0
  {
7673
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7674
0
      return false;
7675
0
  }
7676
7677
0
      s = bfd_get_section_by_name (info->output_bfd, ".preinit_array");
7678
0
      if (s != NULL && s->linker_has_input)
7679
0
  {
7680
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7681
0
    if (! bfd_link_executable (info))
7682
0
      {
7683
0
        bfd *sub;
7684
0
        asection *o;
7685
7686
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7687
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7688
0
        && (o = sub->sections) != NULL
7689
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7690
0
      for (o = sub->sections; o != NULL; o = o->next)
7691
0
        if (elf_section_data (o)->this_hdr.sh_type
7692
0
      == SHT_PREINIT_ARRAY)
7693
0
          {
7694
0
      _bfd_error_handler
7695
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7696
0
         sub);
7697
0
      break;
7698
0
          }
7699
7700
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7701
0
        return false;
7702
0
      }
7703
7704
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7705
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7706
0
      return false;
7707
0
  }
7708
0
      s = bfd_get_section_by_name (info->output_bfd, ".init_array");
7709
0
      if (s != NULL && s->linker_has_input)
7710
0
  {
7711
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7712
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7713
0
      return false;
7714
0
  }
7715
0
      s = bfd_get_section_by_name (info->output_bfd, ".fini_array");
7716
0
      if (s != NULL && s->linker_has_input)
7717
0
  {
7718
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7719
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7720
0
      return false;
7721
0
  }
7722
7723
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7724
      /* If .dynstr is excluded from the link, we don't want any of
7725
   these tags.  Strictly, we should be checking each section
7726
   individually;  This quick check covers for the case where
7727
   someone does a /DISCARD/ : { *(*) }.  */
7728
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7729
0
  {
7730
0
    bfd_size_type strsize;
7731
7732
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7733
0
    if ((info->emit_hash
7734
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7735
0
        || (info->emit_gnu_hash
7736
0
      && (obed->record_xhash_symbol == NULL
7737
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7738
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7739
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7740
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7741
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7742
0
                obed->s->sizeof_sym)
7743
0
        || (info->gnu_flags_1
7744
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7745
0
              info->gnu_flags_1)))
7746
0
      return false;
7747
0
  }
7748
0
    }
7749
7750
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7751
0
    return false;
7752
7753
  /* The backend must work out the sizes of all the other dynamic
7754
     sections.  */
7755
0
  if (obed->elf_backend_late_size_sections != NULL
7756
0
      && !obed->elf_backend_late_size_sections (info))
7757
0
    return false;
7758
7759
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7760
0
    {
7761
0
      if (elf_tdata (info->output_bfd)->cverdefs)
7762
0
  {
7763
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverdefs;
7764
7765
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7766
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7767
0
      return false;
7768
0
  }
7769
7770
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7771
0
  {
7772
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7773
0
      return false;
7774
0
  }
7775
0
      else if (info->flags & DF_BIND_NOW)
7776
0
  {
7777
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7778
0
      return false;
7779
0
  }
7780
7781
0
      if (info->flags_1)
7782
0
  {
7783
0
    if (bfd_link_executable (info))
7784
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7785
0
        | DF_1_NODELETE
7786
0
        | DF_1_NOOPEN);
7787
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7788
0
      return false;
7789
0
  }
7790
7791
0
      if (elf_tdata (info->output_bfd)->cverrefs)
7792
0
  {
7793
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverrefs;
7794
7795
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7796
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7797
0
      return false;
7798
0
  }
7799
7800
0
      if ((elf_tdata (info->output_bfd)->cverrefs == 0
7801
0
     && elf_tdata (info->output_bfd)->cverdefs == 0)
7802
0
    || _bfd_elf_link_renumber_dynsyms (info, NULL) <= 1)
7803
0
  {
7804
0
    asection *s;
7805
7806
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7807
0
    s->flags |= SEC_EXCLUDE;
7808
0
  }
7809
0
    }
7810
0
  return true;
7811
0
}
7812
7813
/* Find the first non-excluded output section.  We'll use its
7814
   section symbol for some emitted relocs.  */
7815
void
7816
_bfd_elf_init_1_index_section (struct bfd_link_info *info)
7817
0
{
7818
0
  asection *s;
7819
0
  asection *found = NULL;
7820
7821
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7822
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7823
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7824
0
      {
7825
0
  found = s;
7826
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7827
0
    break;
7828
0
      }
7829
0
  elf_hash_table (info)->text_index_section = found;
7830
0
}
7831
7832
/* Find two non-excluded output sections, one for code, one for data.
7833
   We'll use their section symbols for some emitted relocs.  */
7834
void
7835
_bfd_elf_init_2_index_sections (struct bfd_link_info *info)
7836
0
{
7837
0
  asection *s;
7838
0
  asection *found = NULL;
7839
7840
  /* Data first, since setting text_index_section changes
7841
     _bfd_elf_omit_section_dynsym_default.  */
7842
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7843
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7844
0
  && !(s->flags & SEC_READONLY)
7845
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7846
0
      {
7847
0
  found = s;
7848
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7849
0
    break;
7850
0
      }
7851
0
  elf_hash_table (info)->data_index_section = found;
7852
7853
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7854
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7855
0
  && (s->flags & SEC_READONLY)
7856
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7857
0
      {
7858
0
  found = s;
7859
0
  break;
7860
0
      }
7861
0
  elf_hash_table (info)->text_index_section = found;
7862
0
}
7863
7864
void
7865
_bfd_elf_init_0_index_sections (struct bfd_link_info *info ATTRIBUTE_UNUSED)
7866
0
{
7867
0
}
7868
7869
#define GNU_HASH_SECTION_NAME(bed)          \
7870
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7871
7872
bool
7873
bfd_elf_size_dynsym_hash_dynstr (struct bfd_link_info *info)
7874
0
{
7875
0
  elf_backend_data *obed;
7876
0
  unsigned long section_sym_count;
7877
0
  bfd_size_type dynsymcount = 0;
7878
7879
0
  if (!is_elf_hash_table (info->hash))
7880
0
    return true;
7881
7882
0
  obed = get_elf_backend_data (info->output_bfd);
7883
0
  obed->elf_backend_init_index_section (info);
7884
7885
  /* Assign dynsym indices.  In a shared library we generate a section
7886
     symbol for each output section, which come first.  Next come all
7887
     of the back-end allocated local dynamic syms, followed by the rest
7888
     of the global symbols.
7889
7890
     This is usually not needed for static binaries, however backends
7891
     can request to always do it, e.g. the MIPS backend uses dynamic
7892
     symbol counts to lay out GOT, which will be produced in the
7893
     presence of GOT relocations even in static binaries (holding fixed
7894
     data in that case, to satisfy those relocations).  */
7895
7896
0
  if (elf_hash_table (info)->dynamic_sections_created
7897
0
      || obed->always_renumber_dynsyms)
7898
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (info, &section_sym_count);
7899
7900
0
  if (elf_hash_table (info)->dynamic_sections_created)
7901
0
    {
7902
0
      bfd *dynobj;
7903
0
      asection *s;
7904
0
      unsigned int dtagcount;
7905
7906
0
      dynobj = elf_hash_table (info)->dynobj;
7907
7908
      /* Work out the size of the symbol version section.  */
7909
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7910
0
      BFD_ASSERT (s != NULL);
7911
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7912
0
  {
7913
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7914
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
7915
0
    if (s->contents == NULL)
7916
0
      return false;
7917
0
    s->alloced = 1;
7918
7919
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7920
0
      return false;
7921
0
  }
7922
7923
      /* Set the size of the .dynsym and .hash sections.  We counted
7924
   the number of dynamic symbols in elf_link_add_object_symbols.
7925
   We will build the contents of .dynsym and .hash when we build
7926
   the final symbol table, because until then we do not know the
7927
   correct value to give the symbols.  We built the .dynstr
7928
   section as we went along in elf_link_add_object_symbols.  */
7929
0
      s = elf_hash_table (info)->dynsym;
7930
0
      BFD_ASSERT (s != NULL);
7931
0
      s->size = dynsymcount * obed->s->sizeof_sym;
7932
7933
0
      s->contents = bfd_alloc (info->output_bfd, s->size);
7934
0
      if (s->contents == NULL)
7935
0
  return false;
7936
0
      s->alloced = 1;
7937
7938
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7939
   section syms, in case we don't output them all.  */
7940
0
      ++section_sym_count;
7941
0
      memset (s->contents, 0, section_sym_count * obed->s->sizeof_sym);
7942
7943
0
      elf_hash_table (info)->bucketcount = 0;
7944
7945
      /* Compute the size of the hashing table.  As a side effect this
7946
   computes the hash values for all the names we export.  */
7947
0
      if (info->emit_hash)
7948
0
  {
7949
0
    unsigned long int *hashcodes;
7950
0
    struct hash_codes_info hashinf;
7951
0
    unsigned long int nsyms;
7952
0
    size_t bucketcount;
7953
0
    size_t hash_entry_size;
7954
7955
    /* Compute the hash values for all exported symbols.  At the same
7956
       time store the values in an array so that we could use them for
7957
       optimizations.  */
7958
0
    hashcodes = bfd_malloc (dynsymcount * sizeof (*hashcodes));
7959
0
    if (hashcodes == NULL)
7960
0
      return false;
7961
0
    hashinf.hashcodes = hashcodes;
7962
0
    hashinf.error = false;
7963
7964
    /* Put all hash values in HASHCODES.  */
7965
0
    elf_link_hash_traverse (elf_hash_table (info),
7966
0
          elf_collect_hash_codes, &hashinf);
7967
0
    if (hashinf.error)
7968
0
      {
7969
0
        free (hashcodes);
7970
0
        return false;
7971
0
      }
7972
7973
0
    nsyms = hashinf.hashcodes - hashcodes;
7974
0
    bucketcount
7975
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
7976
0
    free (hashcodes);
7977
7978
0
    if (bucketcount == 0 && nsyms > 0)
7979
0
      return false;
7980
7981
0
    elf_hash_table (info)->bucketcount = bucketcount;
7982
7983
0
    s = bfd_get_linker_section (dynobj, ".hash");
7984
0
    BFD_ASSERT (s != NULL);
7985
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7986
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7987
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
7988
0
    if (s->contents == NULL)
7989
0
      return false;
7990
0
    s->alloced = 1;
7991
7992
0
    bfd_put (8 * hash_entry_size, info->output_bfd, bucketcount,
7993
0
       s->contents);
7994
0
    bfd_put (8 * hash_entry_size, info->output_bfd, dynsymcount,
7995
0
       s->contents + hash_entry_size);
7996
0
  }
7997
7998
0
      if (info->emit_gnu_hash)
7999
0
  {
8000
0
    size_t i, cnt;
8001
0
    unsigned char *contents;
8002
0
    struct collect_gnu_hash_codes cinfo;
8003
0
    bfd_size_type amt;
8004
0
    size_t bucketcount;
8005
8006
0
    memset (&cinfo, 0, sizeof (cinfo));
8007
8008
    /* Compute the hash values for all exported symbols.  At the same
8009
       time store the values in an array so that we could use them for
8010
       optimizations.  */
8011
0
    amt = dynsymcount * 2 * sizeof (*cinfo.hashcodes);
8012
0
    cinfo.hashcodes = bfd_malloc (amt);
8013
0
    if (cinfo.hashcodes == NULL)
8014
0
      return false;
8015
8016
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8017
0
    cinfo.min_dynindx = -1;
8018
0
    cinfo.output_bfd = info->output_bfd;
8019
0
    cinfo.bed = obed;
8020
8021
    /* Put all hash values in HASHCODES.  */
8022
0
    elf_link_hash_traverse (elf_hash_table (info),
8023
0
          elf_collect_gnu_hash_codes, &cinfo);
8024
0
    if (cinfo.error)
8025
0
      {
8026
0
        free (cinfo.hashcodes);
8027
0
        return false;
8028
0
      }
8029
8030
0
    bucketcount
8031
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8032
8033
0
    if (bucketcount == 0)
8034
0
      {
8035
0
        free (cinfo.hashcodes);
8036
0
        return false;
8037
0
      }
8038
8039
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (obed));
8040
0
    BFD_ASSERT (s != NULL);
8041
8042
0
    if (cinfo.nsyms == 0)
8043
0
      {
8044
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8045
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8046
0
        free (cinfo.hashcodes);
8047
0
        s->size = 5 * 4 + obed->s->arch_size / 8;
8048
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8049
0
        if (contents == NULL)
8050
0
    return false;
8051
0
        s->contents = contents;
8052
0
        s->alloced = 1;
8053
        /* 1 empty bucket.  */
8054
0
        bfd_put_32 (info->output_bfd, 1, contents);
8055
        /* SYMIDX above the special symbol 0.  */
8056
0
        bfd_put_32 (info->output_bfd, 1, contents + 4);
8057
        /* Just one word for bitmask.  */
8058
0
        bfd_put_32 (info->output_bfd, 1, contents + 8);
8059
        /* Only hash fn bloom filter.  */
8060
0
        bfd_put_32 (info->output_bfd, 0, contents + 12);
8061
        /* No hashes are valid - empty bitmask.  */
8062
0
        bfd_put (obed->s->arch_size, info->output_bfd, 0, contents + 16);
8063
        /* No hashes in the only bucket.  */
8064
0
        bfd_put_32 (info->output_bfd, 0,
8065
0
        contents + 16 + obed->s->arch_size / 8);
8066
0
      }
8067
0
    else
8068
0
      {
8069
0
        unsigned long int maskwords, maskbitslog2, x;
8070
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8071
8072
0
        x = cinfo.nsyms;
8073
0
        maskbitslog2 = 1;
8074
0
        while ((x >>= 1) != 0)
8075
0
    ++maskbitslog2;
8076
0
        if (maskbitslog2 < 3)
8077
0
    maskbitslog2 = 5;
8078
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8079
0
    maskbitslog2 = maskbitslog2 + 3;
8080
0
        else
8081
0
    maskbitslog2 = maskbitslog2 + 2;
8082
0
        if (obed->s->arch_size == 64)
8083
0
    {
8084
0
      if (maskbitslog2 == 5)
8085
0
        maskbitslog2 = 6;
8086
0
      cinfo.shift1 = 6;
8087
0
    }
8088
0
        else
8089
0
    cinfo.shift1 = 5;
8090
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8091
0
        cinfo.shift2 = maskbitslog2;
8092
0
        cinfo.maskbits = 1 << maskbitslog2;
8093
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8094
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8095
0
        amt += maskwords * sizeof (bfd_vma);
8096
0
        cinfo.bitmask = bfd_malloc (amt);
8097
0
        if (cinfo.bitmask == NULL)
8098
0
    {
8099
0
      free (cinfo.hashcodes);
8100
0
      return false;
8101
0
    }
8102
8103
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8104
0
        cinfo.indx = cinfo.counts + bucketcount;
8105
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8106
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8107
8108
        /* Determine how often each hash bucket is used.  */
8109
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8110
0
        for (i = 0; i < cinfo.nsyms; ++i)
8111
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8112
8113
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8114
0
    if (cinfo.counts[i] != 0)
8115
0
      {
8116
0
        cinfo.indx[i] = cnt;
8117
0
        cnt += cinfo.counts[i];
8118
0
      }
8119
0
        BFD_ASSERT (cnt == dynsymcount);
8120
0
        cinfo.bucketcount = bucketcount;
8121
0
        cinfo.local_indx = cinfo.min_dynindx;
8122
8123
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8124
0
        s->size += cinfo.maskbits / 8;
8125
0
        if (obed->record_xhash_symbol != NULL)
8126
0
    s->size += cinfo.nsyms * 4;
8127
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8128
0
        if (contents == NULL)
8129
0
    {
8130
0
      free (cinfo.bitmask);
8131
0
      free (cinfo.hashcodes);
8132
0
      return false;
8133
0
    }
8134
8135
0
        s->contents = contents;
8136
0
        s->alloced = 1;
8137
0
        bfd_put_32 (info->output_bfd, bucketcount, contents);
8138
0
        bfd_put_32 (info->output_bfd, cinfo.symindx, contents + 4);
8139
0
        bfd_put_32 (info->output_bfd, maskwords, contents + 8);
8140
0
        bfd_put_32 (info->output_bfd, cinfo.shift2, contents + 12);
8141
0
        contents += 16 + cinfo.maskbits / 8;
8142
8143
0
        for (i = 0; i < bucketcount; ++i)
8144
0
    {
8145
0
      if (cinfo.counts[i] == 0)
8146
0
        bfd_put_32 (info->output_bfd, 0, contents);
8147
0
      else
8148
0
        bfd_put_32 (info->output_bfd, cinfo.indx[i], contents);
8149
0
      contents += 4;
8150
0
    }
8151
8152
0
        cinfo.contents = contents;
8153
8154
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8155
8156
0
        if (elf_hash_table (info)->has_base_symbols)
8157
0
    {
8158
      /* Output base symbols first in DT_GNU_HASH so that
8159
         they will be picked before non-base symbols at
8160
         run-time.  */
8161
0
      cinfo.base_symbol = true;
8162
8163
      /* Renumber dynamic symbols, if populating .gnu.hash
8164
         section.  If using .MIPS.xhash, populate the
8165
         translation table.  */
8166
0
      elf_link_hash_traverse (elf_hash_table (info),
8167
0
            elf_gnu_hash_process_symidx, &cinfo);
8168
0
    }
8169
8170
        /* Output non-base symbols last.  */
8171
0
        cinfo.base_symbol = false;
8172
0
        elf_link_hash_traverse (elf_hash_table (info),
8173
0
              elf_gnu_hash_process_symidx, &cinfo);
8174
8175
0
        contents = s->contents + 16;
8176
0
        for (i = 0; i < maskwords; ++i)
8177
0
    {
8178
0
      bfd_put (obed->s->arch_size, info->output_bfd,
8179
0
         cinfo.bitmask[i], contents);
8180
0
      contents += obed->s->arch_size / 8;
8181
0
    }
8182
8183
0
        free (cinfo.bitmask);
8184
0
        free (cinfo.hashcodes);
8185
0
      }
8186
0
  }
8187
8188
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8189
0
      BFD_ASSERT (s != NULL);
8190
8191
0
      elf_finalize_dynstr (info->output_bfd, info);
8192
8193
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8194
8195
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8196
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8197
0
    return false;
8198
0
    }
8199
8200
0
  return true;
8201
0
}
8202

8203
/* Create an entry in an ELF linker hash table.  */
8204
8205
struct bfd_hash_entry *
8206
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8207
          struct bfd_hash_table *table,
8208
          const char *string)
8209
0
{
8210
  /* Allocate the structure if it has not already been allocated by a
8211
     subclass.  */
8212
0
  if (entry == NULL)
8213
0
    {
8214
0
      entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8215
0
      if (entry == NULL)
8216
0
  return entry;
8217
0
    }
8218
8219
  /* Call the allocation method of the superclass.  */
8220
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8221
0
  if (entry != NULL)
8222
0
    {
8223
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8224
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8225
8226
      /* Set local fields.  */
8227
0
      ret->indx = -1;
8228
0
      ret->dynindx = -1;
8229
0
      ret->got = htab->init_got_refcount;
8230
0
      ret->plt = htab->init_plt_refcount;
8231
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8232
0
            - offsetof (struct elf_link_hash_entry, size)));
8233
      /* Assume that we have been called by a non-ELF symbol reader.
8234
   This flag is then reset by the code which reads an ELF input
8235
   file.  This ensures that a symbol created by a non-ELF symbol
8236
   reader will have the flag set correctly.  */
8237
0
      ret->non_elf = 1;
8238
0
    }
8239
8240
0
  return entry;
8241
0
}
8242
8243
/* Copy data from an indirect symbol to its direct symbol, hiding the
8244
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8245
8246
void
8247
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8248
          struct elf_link_hash_entry *dir,
8249
          struct elf_link_hash_entry *ind)
8250
0
{
8251
0
  struct elf_link_hash_table *htab;
8252
8253
0
  if (ind->dyn_relocs != NULL)
8254
0
    {
8255
0
      if (dir->dyn_relocs != NULL)
8256
0
  {
8257
0
    struct elf_dyn_relocs **pp;
8258
0
    struct elf_dyn_relocs *p;
8259
8260
    /* Add reloc counts against the indirect sym to the direct sym
8261
       list.  Merge any entries against the same section.  */
8262
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8263
0
      {
8264
0
        struct elf_dyn_relocs *q;
8265
8266
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8267
0
    if (q->sec == p->sec)
8268
0
      {
8269
0
        q->pc_count += p->pc_count;
8270
0
        q->count += p->count;
8271
0
        *pp = p->next;
8272
0
        break;
8273
0
      }
8274
0
        if (q == NULL)
8275
0
    pp = &p->next;
8276
0
      }
8277
0
    *pp = dir->dyn_relocs;
8278
0
  }
8279
8280
0
      dir->dyn_relocs = ind->dyn_relocs;
8281
0
      ind->dyn_relocs = NULL;
8282
0
    }
8283
8284
  /* Copy down any references that we may have already seen to the
8285
     symbol which just became indirect.  */
8286
8287
0
  if (dir->versioned != versioned_hidden)
8288
0
    dir->ref_dynamic |= ind->ref_dynamic;
8289
0
  dir->ref_regular |= ind->ref_regular;
8290
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8291
0
  dir->non_got_ref |= ind->non_got_ref;
8292
0
  dir->needs_plt |= ind->needs_plt;
8293
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8294
8295
0
  if (ind->root.type != bfd_link_hash_indirect)
8296
0
    return;
8297
8298
  /* Copy over the global and procedure linkage table refcount entries.
8299
     These may have been already set up by a check_relocs routine.  */
8300
0
  htab = elf_hash_table (info);
8301
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8302
0
    {
8303
0
      if (dir->got.refcount < 0)
8304
0
  dir->got.refcount = 0;
8305
0
      dir->got.refcount += ind->got.refcount;
8306
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8307
0
    }
8308
8309
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8310
0
    {
8311
0
      if (dir->plt.refcount < 0)
8312
0
  dir->plt.refcount = 0;
8313
0
      dir->plt.refcount += ind->plt.refcount;
8314
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8315
0
    }
8316
8317
0
  if (ind->dynindx != -1)
8318
0
    {
8319
0
      if (dir->dynindx != -1)
8320
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8321
0
      dir->dynindx = ind->dynindx;
8322
0
      dir->dynstr_index = ind->dynstr_index;
8323
0
      ind->dynindx = -1;
8324
0
      ind->dynstr_index = 0;
8325
0
    }
8326
0
}
8327
8328
void
8329
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8330
        struct elf_link_hash_entry *h,
8331
        bool force_local)
8332
0
{
8333
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8334
0
  if (h->type != STT_GNU_IFUNC)
8335
0
    {
8336
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8337
0
      h->needs_plt = 0;
8338
0
    }
8339
0
  if (force_local)
8340
0
    {
8341
0
      h->forced_local = 1;
8342
0
      if (h->dynindx != -1)
8343
0
  {
8344
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8345
0
          h->dynstr_index);
8346
0
    h->dynindx = -1;
8347
0
    h->dynstr_index = 0;
8348
0
  }
8349
0
    }
8350
0
}
8351
8352
/* Hide a symbol. */
8353
8354
void
8355
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8356
         struct bfd_link_info *info,
8357
         struct bfd_link_hash_entry *h)
8358
0
{
8359
0
  if (is_elf_hash_table (info->hash))
8360
0
    {
8361
0
      elf_backend_data *obed = get_elf_backend_data (output_bfd);
8362
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8363
0
      eh->def_dynamic = 0;
8364
0
      eh->ref_dynamic = 0;
8365
0
      eh->dynamic_def = 0;
8366
0
      obed->elf_backend_hide_symbol (info, eh, true);
8367
0
    }
8368
0
}
8369
8370
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8371
   caller.  */
8372
8373
bool
8374
_bfd_elf_link_hash_table_init
8375
  (struct elf_link_hash_table *table,
8376
   bfd *obfd,
8377
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8378
              struct bfd_hash_table *,
8379
              const char *),
8380
   unsigned int entsize)
8381
0
{
8382
0
  bool ret;
8383
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
8384
0
  int can_refcount = obed->can_refcount;
8385
8386
0
  table->init_got_refcount.refcount = can_refcount - 1;
8387
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8388
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8389
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8390
  /* The first dynamic symbol is a dummy.  */
8391
0
  table->dynsymcount = 1;
8392
8393
0
  ret = _bfd_link_hash_table_init (&table->root, obfd, newfunc, entsize);
8394
8395
0
  table->root.type = bfd_link_elf_hash_table;
8396
0
  table->hash_table_id = obed->target_id;
8397
0
  table->target_os = obed->target_os;
8398
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8399
8400
0
  return ret;
8401
0
}
8402
8403
/* Create an ELF linker hash table.  */
8404
8405
struct bfd_link_hash_table *
8406
_bfd_elf_link_hash_table_create (bfd *obfd)
8407
0
{
8408
0
  struct elf_link_hash_table *ret;
8409
8410
0
  ret = bfd_zmalloc (sizeof (*ret));
8411
0
  if (ret == NULL)
8412
0
    return NULL;
8413
8414
0
  if (! _bfd_elf_link_hash_table_init (ret, obfd, _bfd_elf_link_hash_newfunc,
8415
0
               sizeof (struct elf_link_hash_entry)))
8416
0
    {
8417
0
      free (ret);
8418
0
      return NULL;
8419
0
    }
8420
8421
0
  return &ret->root;
8422
0
}
8423
8424
/* Destroy an ELF linker hash table.  */
8425
8426
void
8427
_bfd_elf_link_hash_table_free (bfd *obfd)
8428
0
{
8429
0
  struct elf_link_hash_table *htab;
8430
8431
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8432
0
  if (htab->dynstr != NULL)
8433
0
    _bfd_elf_strtab_free (htab->dynstr);
8434
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8435
0
  if (htab->dynamic != NULL)
8436
0
    {
8437
0
      free (htab->dynamic->contents);
8438
0
      htab->dynamic->contents = NULL;
8439
0
    }
8440
0
  if (htab->first_hash != NULL)
8441
0
    {
8442
0
      bfd_hash_table_free (htab->first_hash);
8443
0
      free (htab->first_hash);
8444
0
    }
8445
0
  if (htab->eh_info.frame_hdr_is_compact)
8446
0
    free (htab->eh_info.u.compact.entries);
8447
0
  else
8448
0
    free (htab->eh_info.u.dwarf.array);
8449
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8450
0
  _bfd_generic_link_hash_table_free (obfd);
8451
0
}
8452
8453
/* This is a hook for the ELF emulation code in the generic linker to
8454
   tell the backend linker what file name to use for the DT_NEEDED
8455
   entry for a dynamic object.  */
8456
8457
void
8458
bfd_elf_set_dt_needed_name (bfd *obfd, const char *name)
8459
0
{
8460
0
  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour
8461
0
      && bfd_get_format (obfd) == bfd_object)
8462
0
    elf_dt_name (obfd) = name;
8463
0
}
8464
8465
int
8466
bfd_elf_get_dyn_lib_class (bfd *abfd)
8467
0
{
8468
0
  int lib_class;
8469
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8470
0
      && bfd_get_format (abfd) == bfd_object)
8471
0
    lib_class = elf_dyn_lib_class (abfd);
8472
0
  else
8473
0
    lib_class = 0;
8474
0
  return lib_class;
8475
0
}
8476
8477
void
8478
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8479
0
{
8480
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8481
0
      && bfd_get_format (abfd) == bfd_object)
8482
0
    elf_dyn_lib_class (abfd) = lib_class;
8483
0
}
8484
8485
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8486
   the linker ELF emulation code.  */
8487
8488
struct bfd_link_needed_list *
8489
bfd_elf_get_needed_list (struct bfd_link_info *info)
8490
0
{
8491
0
  if (! is_elf_hash_table (info->hash))
8492
0
    return NULL;
8493
0
  return elf_hash_table (info)->needed;
8494
0
}
8495
8496
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8497
   hook for the linker ELF emulation code.  */
8498
8499
struct bfd_link_needed_list *
8500
bfd_elf_get_runpath_list (struct bfd_link_info *info)
8501
0
{
8502
0
  if (! is_elf_hash_table (info->hash))
8503
0
    return NULL;
8504
0
  return elf_hash_table (info)->runpath;
8505
0
}
8506
8507
/* Get the name actually used for a dynamic object for a link.  This
8508
   is the SONAME entry if there is one.  Otherwise, it is the string
8509
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8510
8511
const char *
8512
bfd_elf_get_dt_soname (bfd *abfd)
8513
0
{
8514
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8515
0
      && bfd_get_format (abfd) == bfd_object)
8516
0
    return elf_dt_name (abfd);
8517
0
  return NULL;
8518
0
}
8519
8520
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8521
   the ELF linker emulation code.  */
8522
8523
bool
8524
bfd_elf_get_bfd_needed_list (bfd *abfd,
8525
           struct bfd_link_needed_list **pneeded)
8526
0
{
8527
0
  asection *s;
8528
0
  bfd_byte *dynbuf = NULL;
8529
0
  unsigned int elfsec;
8530
0
  unsigned long shlink;
8531
0
  bfd_byte *extdyn, *extdynend;
8532
0
  size_t extdynsize;
8533
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8534
8535
0
  *pneeded = NULL;
8536
8537
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8538
0
      || bfd_get_format (abfd) != bfd_object)
8539
0
    return true;
8540
8541
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8542
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8543
0
    return true;
8544
8545
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8546
0
    goto error_return;
8547
8548
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8549
0
  if (elfsec == SHN_BAD)
8550
0
    goto error_return;
8551
8552
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8553
8554
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8555
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8556
8557
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8558
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8559
0
       extdyn += extdynsize)
8560
0
    {
8561
0
      Elf_Internal_Dyn dyn;
8562
8563
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8564
8565
0
      if (dyn.d_tag == DT_NULL)
8566
0
  break;
8567
8568
0
      if (dyn.d_tag == DT_NEEDED)
8569
0
  {
8570
0
    const char *string;
8571
0
    struct bfd_link_needed_list *l;
8572
0
    unsigned int tagv = dyn.d_un.d_val;
8573
8574
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8575
0
    if (string == NULL)
8576
0
      goto error_return;
8577
8578
0
    l = bfd_alloc (abfd, sizeof (*l));
8579
0
    if (l == NULL)
8580
0
      goto error_return;
8581
8582
0
    l->by = abfd;
8583
0
    l->name = string;
8584
0
    l->next = *pneeded;
8585
0
    *pneeded = l;
8586
0
  }
8587
0
    }
8588
8589
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8590
8591
0
  return true;
8592
8593
0
 error_return:
8594
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8595
0
  return false;
8596
0
}
8597
8598
struct elf_symbuf_symbol
8599
{
8600
  unsigned long st_name;  /* Symbol name, index in string tbl */
8601
  unsigned char st_info;  /* Type and binding attributes */
8602
  unsigned char st_other; /* Visibilty, and target specific */
8603
};
8604
8605
struct elf_symbuf_head
8606
{
8607
  struct elf_symbuf_symbol *ssym;
8608
  size_t count;
8609
  unsigned int st_shndx;
8610
};
8611
8612
struct elf_symbol
8613
{
8614
  union
8615
    {
8616
      Elf_Internal_Sym *isym;
8617
      struct elf_symbuf_symbol *ssym;
8618
      void *p;
8619
    } u;
8620
  const char *name;
8621
};
8622
8623
/* Sort references to symbols by ascending section number.  */
8624
8625
static int
8626
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8627
0
{
8628
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8629
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8630
8631
0
  if (s1->st_shndx != s2->st_shndx)
8632
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8633
  /* Final sort by the address of the sym in the symbuf ensures
8634
     a stable sort.  */
8635
0
  if (s1 != s2)
8636
0
    return s1 > s2 ? 1 : -1;
8637
0
  return 0;
8638
0
}
8639
8640
static int
8641
elf_sym_name_compare (const void *arg1, const void *arg2)
8642
0
{
8643
0
  const struct elf_symbol *s1 = arg1;
8644
0
  const struct elf_symbol *s2 = arg2;
8645
0
  int ret = strcmp (s1->name, s2->name);
8646
0
  if (ret != 0)
8647
0
    return ret;
8648
0
  if (s1->u.p != s2->u.p)
8649
0
    return s1->u.p > s2->u.p ? 1 : -1;
8650
0
  return 0;
8651
0
}
8652
8653
static struct elf_symbuf_head *
8654
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8655
0
{
8656
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8657
0
  struct elf_symbuf_symbol *ssym;
8658
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8659
0
  size_t i, shndx_count, total_size;
8660
8661
0
  indbuf = bfd_malloc (symcount * sizeof (*indbuf));
8662
0
  if (indbuf == NULL)
8663
0
    return NULL;
8664
8665
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8666
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8667
0
      *ind++ = &isymbuf[i];
8668
0
  indbufend = ind;
8669
8670
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8671
0
   elf_sort_elf_symbol);
8672
8673
0
  shndx_count = 0;
8674
0
  if (indbufend > indbuf)
8675
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8676
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8677
0
  shndx_count++;
8678
8679
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8680
0
    + (indbufend - indbuf) * sizeof (*ssym));
8681
0
  ssymbuf = bfd_malloc (total_size);
8682
0
  if (ssymbuf == NULL)
8683
0
    {
8684
0
      free (indbuf);
8685
0
      return NULL;
8686
0
    }
8687
8688
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8689
0
  ssymbuf->ssym = NULL;
8690
0
  ssymbuf->count = shndx_count;
8691
0
  ssymbuf->st_shndx = 0;
8692
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8693
0
    {
8694
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8695
0
  {
8696
0
    ssymhead++;
8697
0
    ssymhead->ssym = ssym;
8698
0
    ssymhead->count = 0;
8699
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8700
0
  }
8701
0
      ssym->st_name = (*ind)->st_name;
8702
0
      ssym->st_info = (*ind)->st_info;
8703
0
      ssym->st_other = (*ind)->st_other;
8704
0
      ssymhead->count++;
8705
0
    }
8706
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8707
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8708
8709
0
  free (indbuf);
8710
0
  return ssymbuf;
8711
0
}
8712
8713
/* Check if 2 sections define the same set of local and global
8714
   symbols.  */
8715
8716
static bool
8717
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8718
           struct bfd_link_info *info)
8719
0
{
8720
0
  bfd *bfd1, *bfd2;
8721
0
  elf_backend_data *bed1, *bed2;
8722
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8723
0
  size_t symcount1, symcount2;
8724
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8725
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8726
0
  Elf_Internal_Sym *isym, *isymend;
8727
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8728
0
  size_t count1, count2, sec_count1, sec_count2, i;
8729
0
  unsigned int shndx1, shndx2;
8730
0
  bool result;
8731
0
  bool ignore_section_symbol_p;
8732
8733
0
  bfd1 = sec1->owner;
8734
0
  bfd2 = sec2->owner;
8735
8736
  /* Both sections have to be in ELF.  */
8737
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8738
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8739
0
    return false;
8740
8741
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8742
0
    return false;
8743
8744
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8745
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8746
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8747
0
    return false;
8748
8749
0
  bed1 = get_elf_backend_data (bfd1);
8750
0
  bed2 = get_elf_backend_data (bfd2);
8751
0
  hdr1 = &elf_symtab_hdr (bfd1);
8752
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8753
0
  hdr2 = &elf_symtab_hdr (bfd2);
8754
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8755
8756
0
  if (symcount1 == 0 || symcount2 == 0)
8757
0
    return false;
8758
8759
0
  result = false;
8760
0
  isymbuf1 = NULL;
8761
0
  isymbuf2 = NULL;
8762
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8763
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8764
8765
  /* Ignore section symbols only when matching non-debugging sections
8766
     or linkonce section with comdat section.  */
8767
0
  ignore_section_symbol_p
8768
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8769
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8770
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8771
8772
0
  if (ssymbuf1 == NULL)
8773
0
    {
8774
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8775
0
               NULL, NULL, NULL);
8776
0
      if (isymbuf1 == NULL)
8777
0
  goto done;
8778
8779
0
      if (info != NULL && !info->reduce_memory_overheads)
8780
0
  {
8781
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8782
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8783
0
  }
8784
0
    }
8785
8786
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8787
0
    {
8788
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8789
0
               NULL, NULL, NULL);
8790
0
      if (isymbuf2 == NULL)
8791
0
  goto done;
8792
8793
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8794
0
  {
8795
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8796
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8797
0
  }
8798
0
    }
8799
8800
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8801
0
    {
8802
      /* Optimized faster version.  */
8803
0
      size_t lo, hi, mid;
8804
0
      struct elf_symbol *symp;
8805
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8806
8807
0
      lo = 0;
8808
0
      hi = ssymbuf1->count;
8809
0
      ssymbuf1++;
8810
0
      count1 = 0;
8811
0
      sec_count1 = 0;
8812
0
      while (lo < hi)
8813
0
  {
8814
0
    mid = (lo + hi) / 2;
8815
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8816
0
      hi = mid;
8817
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8818
0
      lo = mid + 1;
8819
0
    else
8820
0
      {
8821
0
        count1 = ssymbuf1[mid].count;
8822
0
        ssymbuf1 += mid;
8823
0
        break;
8824
0
      }
8825
0
  }
8826
0
      if (ignore_section_symbol_p)
8827
0
  {
8828
0
    for (i = 0; i < count1; i++)
8829
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8830
0
        sec_count1++;
8831
0
    count1 -= sec_count1;
8832
0
  }
8833
8834
0
      lo = 0;
8835
0
      hi = ssymbuf2->count;
8836
0
      ssymbuf2++;
8837
0
      count2 = 0;
8838
0
      sec_count2 = 0;
8839
0
      while (lo < hi)
8840
0
  {
8841
0
    mid = (lo + hi) / 2;
8842
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8843
0
      hi = mid;
8844
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8845
0
      lo = mid + 1;
8846
0
    else
8847
0
      {
8848
0
        count2 = ssymbuf2[mid].count;
8849
0
        ssymbuf2 += mid;
8850
0
        break;
8851
0
      }
8852
0
  }
8853
0
      if (ignore_section_symbol_p)
8854
0
  {
8855
0
    for (i = 0; i < count2; i++)
8856
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8857
0
        sec_count2++;
8858
0
    count2 -= sec_count2;
8859
0
  }
8860
8861
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8862
0
  goto done;
8863
8864
0
      symtable1 = bfd_malloc (count1 * sizeof (*symtable1));
8865
0
      symtable2 = bfd_malloc (count2 * sizeof (*symtable2));
8866
0
      if (symtable1 == NULL || symtable2 == NULL)
8867
0
  goto done;
8868
8869
0
      symp = symtable1;
8870
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8871
0
     ssym < ssymend; ssym++)
8872
0
  if (sec_count1 == 0
8873
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8874
0
    {
8875
0
      symp->u.ssym = ssym;
8876
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8877
0
                hdr1->sh_link,
8878
0
                ssym->st_name);
8879
0
      if (symp->name == NULL)
8880
0
        goto done;
8881
0
      symp++;
8882
0
    }
8883
8884
0
      symp = symtable2;
8885
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8886
0
     ssym < ssymend; ssym++)
8887
0
  if (sec_count2 == 0
8888
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8889
0
    {
8890
0
      symp->u.ssym = ssym;
8891
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8892
0
                hdr2->sh_link,
8893
0
                ssym->st_name);
8894
0
      if (symp->name == NULL)
8895
0
        goto done;
8896
0
      symp++;
8897
0
    }
8898
8899
      /* Sort symbol by name.  */
8900
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8901
0
       elf_sym_name_compare);
8902
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8903
0
       elf_sym_name_compare);
8904
8905
0
      for (i = 0; i < count1; i++)
8906
  /* Two symbols must have the same binding, type and name.  */
8907
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8908
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8909
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8910
0
    goto done;
8911
8912
0
      result = true;
8913
0
      goto done;
8914
0
    }
8915
8916
0
  symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8917
0
  symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8918
0
  if (symtable1 == NULL || symtable2 == NULL)
8919
0
    goto done;
8920
8921
  /* Count definitions in the section.  */
8922
0
  count1 = 0;
8923
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8924
0
    if (isym->st_shndx == shndx1
8925
0
  && (!ignore_section_symbol_p
8926
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8927
0
      symtable1[count1++].u.isym = isym;
8928
8929
0
  count2 = 0;
8930
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8931
0
    if (isym->st_shndx == shndx2
8932
0
  && (!ignore_section_symbol_p
8933
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8934
0
      symtable2[count2++].u.isym = isym;
8935
8936
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8937
0
    goto done;
8938
8939
0
  for (i = 0; i < count1; i++)
8940
0
    {
8941
0
      symtable1[i].name
8942
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8943
0
             symtable1[i].u.isym->st_name);
8944
0
      if (symtable1[i].name == NULL)
8945
0
  goto done;
8946
0
    }
8947
8948
0
  for (i = 0; i < count2; i++)
8949
0
    {
8950
0
      symtable2[i].name
8951
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8952
0
             symtable2[i].u.isym->st_name);
8953
0
      if (symtable2[i].name == NULL)
8954
0
  goto done;
8955
0
    }
8956
8957
  /* Sort symbol by name.  */
8958
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
8959
0
   elf_sym_name_compare);
8960
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
8961
0
   elf_sym_name_compare);
8962
8963
0
  for (i = 0; i < count1; i++)
8964
    /* Two symbols must have the same binding, type and name.  */
8965
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8966
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8967
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8968
0
      goto done;
8969
8970
0
  result = true;
8971
8972
0
 done:
8973
0
  free (symtable1);
8974
0
  free (symtable2);
8975
0
  free (isymbuf1);
8976
0
  free (isymbuf2);
8977
8978
0
  return result;
8979
0
}
8980
8981
/* Return TRUE if 2 section types are compatible.  */
8982
8983
bool
8984
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8985
        bfd *bbfd, const asection *bsec)
8986
0
{
8987
0
  if (asec == NULL
8988
0
      || bsec == NULL
8989
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
8990
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
8991
0
    return true;
8992
8993
0
  return elf_section_type (asec) == elf_section_type (bsec);
8994
0
}
8995

8996
/* Final phase of ELF linker.  */
8997
8998
/* A structure we use to avoid passing large numbers of arguments.  */
8999
9000
struct elf_final_link_info
9001
{
9002
  /* General link information.  */
9003
  struct bfd_link_info *info;
9004
  /* Output BFD.  */
9005
  bfd *output_bfd;
9006
  /* Symbol string table.  */
9007
  struct elf_strtab_hash *symstrtab;
9008
  /* .hash section.  */
9009
  asection *hash_sec;
9010
  /* symbol version section (.gnu.version).  */
9011
  asection *symver_sec;
9012
  /* Buffer large enough to hold contents of any section.  */
9013
  bfd_byte *contents;
9014
  /* Buffer large enough to hold external relocs of any section.  */
9015
  void *external_relocs;
9016
  /* Buffer large enough to hold internal relocs of any section.  */
9017
  Elf_Internal_Rela *internal_relocs;
9018
  /* Buffer large enough to hold external local symbols of any input
9019
     BFD.  */
9020
  bfd_byte *external_syms;
9021
  /* And a buffer for symbol section indices.  */
9022
  Elf_External_Sym_Shndx *locsym_shndx;
9023
  /* Buffer large enough to hold internal local symbols of any input
9024
     BFD.  */
9025
  Elf_Internal_Sym *internal_syms;
9026
  /* Array large enough to hold a symbol index for each local symbol
9027
     of any input BFD.  */
9028
  long *indices;
9029
  /* Array large enough to hold a section pointer for each local
9030
     symbol of any input BFD.  */
9031
  asection **sections;
9032
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9033
  Elf_External_Sym_Shndx *symshndxbuf;
9034
  /* Number of STT_FILE syms seen.  */
9035
  size_t filesym_count;
9036
  /* Local symbol hash table.  */
9037
  struct bfd_hash_table local_hash_table;
9038
};
9039
9040
struct local_hash_entry
9041
{
9042
  /* Base hash table entry structure.  */
9043
  struct bfd_hash_entry root;
9044
  /* Size of the local symbol name.  */
9045
  size_t size;
9046
  /* Number of the duplicated local symbol names.  */
9047
  long count;
9048
};
9049
9050
/* Create an entry in the local symbol hash table.  */
9051
9052
static struct bfd_hash_entry *
9053
local_hash_newfunc (struct bfd_hash_entry *entry,
9054
        struct bfd_hash_table *table,
9055
        const char *string)
9056
0
{
9057
9058
  /* Allocate the structure if it has not already been allocated by a
9059
     subclass.  */
9060
0
  if (entry == NULL)
9061
0
    {
9062
0
      entry = bfd_hash_allocate (table, sizeof (struct local_hash_entry));
9063
0
      if (entry == NULL)
9064
0
        return entry;
9065
0
    }
9066
9067
  /* Call the allocation method of the superclass.  */
9068
0
  entry = bfd_hash_newfunc (entry, table, string);
9069
0
  if (entry != NULL)
9070
0
    {
9071
0
      ((struct local_hash_entry *) entry)->count = 0;
9072
0
      ((struct local_hash_entry *) entry)->size = 0;
9073
0
    }
9074
9075
0
  return entry;
9076
0
}
9077
9078
/* This struct is used to pass information to elf_link_output_extsym.  */
9079
9080
struct elf_outext_info
9081
{
9082
  bool failed;
9083
  bool localsyms;
9084
  bool file_sym_done;
9085
  bool base_symbol;
9086
  struct elf_final_link_info *flinfo;
9087
};
9088
9089
9090
/* Support for evaluating a complex relocation.
9091
9092
   Complex relocations are generalized, self-describing relocations.  The
9093
   implementation of them consists of two parts: complex symbols, and the
9094
   relocations themselves.
9095
9096
   The relocations use a reserved elf-wide relocation type code (R_RELC
9097
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9098
   information (start bit, end bit, word width, etc) into the addend.  This
9099
   information is extracted from CGEN-generated operand tables within gas.
9100
9101
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9102
   internal) representing prefix-notation expressions, including but not
9103
   limited to those sorts of expressions normally encoded as addends in the
9104
   addend field.  The symbol mangling format is:
9105
9106
   <node> := <literal>
9107
    |  <unary-operator> ':' <node>
9108
    |  <binary-operator> ':' <node> ':' <node>
9109
    ;
9110
9111
   <literal> := 's' <digits=N> ':' <N character symbol name>
9112
       |  'S' <digits=N> ':' <N character section name>
9113
       |  '#' <hexdigits>
9114
       ;
9115
9116
   <binary-operator> := as in C
9117
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9118
9119
static bool
9120
set_symbol_value (bfd *bfd_with_globals,
9121
      Elf_Internal_Sym *isymbuf,
9122
      size_t locsymcount,
9123
      size_t num_sym,
9124
      size_t symidx,
9125
      bfd_vma val)
9126
0
{
9127
0
  struct elf_link_hash_entry *h;
9128
0
  size_t extsymoff = locsymcount;
9129
9130
0
  if (symidx < locsymcount)
9131
0
    {
9132
0
      Elf_Internal_Sym *sym;
9133
9134
0
      sym = isymbuf + symidx;
9135
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9136
0
  {
9137
    /* It is a local symbol: move it to the
9138
       "absolute" section and give it a value.  */
9139
0
    sym->st_shndx = SHN_ABS;
9140
0
    sym->st_value = val;
9141
0
    return true;
9142
0
  }
9143
0
      if (!elf_bad_symtab (bfd_with_globals))
9144
0
  {
9145
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9146
0
            bfd_with_globals);
9147
0
    bfd_set_error (bfd_error_bad_value);
9148
0
    return false;
9149
0
  }
9150
0
      extsymoff = 0;
9151
0
    }
9152
9153
  /* It is a global symbol: set its link type
9154
     to "defined" and give it a value.  */
9155
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9156
0
            extsymoff, num_sym);
9157
0
  if (h == NULL)
9158
0
    return false;
9159
0
  h->root.type = bfd_link_hash_defined;
9160
0
  h->root.u.def.value = val;
9161
0
  h->root.u.def.section = bfd_abs_section_ptr;
9162
0
  return true;
9163
0
}
9164
9165
static bool
9166
resolve_symbol (const char *name,
9167
    bfd *input_bfd,
9168
    struct elf_final_link_info *flinfo,
9169
    bfd_vma *result,
9170
    Elf_Internal_Sym *isymbuf,
9171
    size_t locsymcount)
9172
0
{
9173
0
  Elf_Internal_Sym *sym;
9174
0
  struct bfd_link_hash_entry *global_entry;
9175
0
  const char *candidate = NULL;
9176
0
  Elf_Internal_Shdr *symtab_hdr;
9177
0
  size_t i;
9178
9179
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9180
9181
0
  for (i = 0; i < locsymcount; ++ i)
9182
0
    {
9183
0
      sym = isymbuf + i;
9184
9185
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9186
0
  continue;
9187
9188
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9189
0
               symtab_hdr->sh_link,
9190
0
               sym->st_name);
9191
#ifdef DEBUG
9192
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9193
        name, candidate, (unsigned long) sym->st_value);
9194
#endif
9195
0
      if (candidate && strcmp (candidate, name) == 0)
9196
0
  {
9197
0
    asection *sec = flinfo->sections [i];
9198
9199
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9200
0
    *result += sec->output_offset + sec->output_section->vma;
9201
#ifdef DEBUG
9202
    printf ("Found symbol with value %8.8lx\n",
9203
      (unsigned long) *result);
9204
#endif
9205
0
    return true;
9206
0
  }
9207
0
    }
9208
9209
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9210
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9211
0
               false, false, true);
9212
0
  if (!global_entry)
9213
0
    return false;
9214
9215
0
  if (global_entry->type == bfd_link_hash_defined
9216
0
      || global_entry->type == bfd_link_hash_defweak)
9217
0
    {
9218
0
      *result = (global_entry->u.def.value
9219
0
     + global_entry->u.def.section->output_section->vma
9220
0
     + global_entry->u.def.section->output_offset);
9221
#ifdef DEBUG
9222
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9223
        global_entry->root.string, (unsigned long) *result);
9224
#endif
9225
0
      return true;
9226
0
    }
9227
9228
0
  return false;
9229
0
}
9230
9231
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9232
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9233
   names like "foo.end" which is the end address of section "foo".  */
9234
9235
static bool
9236
resolve_section (const char *name,
9237
     asection *sections,
9238
     bfd_vma *result,
9239
     bfd * abfd)
9240
0
{
9241
0
  asection *curr;
9242
0
  unsigned int len;
9243
9244
0
  for (curr = sections; curr; curr = curr->next)
9245
0
    if (strcmp (curr->name, name) == 0)
9246
0
      {
9247
0
  *result = curr->vma;
9248
0
  return true;
9249
0
      }
9250
9251
  /* Hmm. still haven't found it. try pseudo-section names.  */
9252
  /* FIXME: This could be coded more efficiently...  */
9253
0
  for (curr = sections; curr; curr = curr->next)
9254
0
    {
9255
0
      len = strlen (curr->name);
9256
0
      if (len > strlen (name))
9257
0
  continue;
9258
9259
0
      if (strncmp (curr->name, name, len) == 0)
9260
0
  {
9261
0
    if (startswith (name + len, ".end"))
9262
0
      {
9263
0
        *result = (curr->vma
9264
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9265
0
        return true;
9266
0
      }
9267
9268
    /* Insert more pseudo-section names here, if you like.  */
9269
0
  }
9270
0
    }
9271
9272
0
  return false;
9273
0
}
9274
9275
static void
9276
undefined_reference (const char *reftype, const char *name)
9277
0
{
9278
  /* xgettext:c-format */
9279
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9280
0
          reftype, name);
9281
0
  bfd_set_error (bfd_error_bad_value);
9282
0
}
9283
9284
static bool
9285
eval_symbol (bfd_vma *result,
9286
       const char **symp,
9287
       bfd *input_bfd,
9288
       struct elf_final_link_info *flinfo,
9289
       bfd_vma dot,
9290
       Elf_Internal_Sym *isymbuf,
9291
       size_t locsymcount,
9292
       int signed_p)
9293
0
{
9294
0
  size_t len;
9295
0
  size_t symlen;
9296
0
  bfd_vma a;
9297
0
  bfd_vma b;
9298
0
  char symbuf[4096];
9299
0
  const char *sym = *symp;
9300
0
  const char *symend;
9301
0
  bool symbol_is_section = false;
9302
9303
0
  len = strlen (sym);
9304
0
  symend = sym + len;
9305
9306
0
  if (len < 1 || len > sizeof (symbuf))
9307
0
    {
9308
0
      bfd_set_error (bfd_error_invalid_operation);
9309
0
      return false;
9310
0
    }
9311
9312
0
  switch (* sym)
9313
0
    {
9314
0
    case '.':
9315
0
      *result = dot;
9316
0
      *symp = sym + 1;
9317
0
      return true;
9318
9319
0
    case '#':
9320
0
      ++sym;
9321
0
      *result = strtoul (sym, (char **) symp, 16);
9322
0
      return true;
9323
9324
0
    case 'S':
9325
0
      symbol_is_section = true;
9326
      /* Fall through.  */
9327
0
    case 's':
9328
0
      ++sym;
9329
0
      symlen = strtol (sym, (char **) symp, 10);
9330
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9331
9332
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9333
0
  {
9334
0
    bfd_set_error (bfd_error_invalid_operation);
9335
0
    return false;
9336
0
  }
9337
9338
0
      memcpy (symbuf, sym, symlen);
9339
0
      symbuf[symlen] = '\0';
9340
0
      *symp = sym + symlen;
9341
9342
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9343
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9344
   interpretation here; section means "try section first", not "must be a
9345
   section", and likewise with symbol.  */
9346
9347
0
      if (symbol_is_section)
9348
0
  {
9349
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9350
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9351
0
          isymbuf, locsymcount))
9352
0
      {
9353
0
        undefined_reference ("section", symbuf);
9354
0
        return false;
9355
0
      }
9356
0
  }
9357
0
      else
9358
0
  {
9359
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9360
0
             isymbuf, locsymcount)
9361
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9362
0
           result, input_bfd))
9363
0
      {
9364
0
        undefined_reference ("symbol", symbuf);
9365
0
        return false;
9366
0
      }
9367
0
  }
9368
9369
0
      return true;
9370
9371
      /* All that remains are operators.  */
9372
9373
0
#define UNARY_OP(op)            \
9374
0
  if (startswith (sym, #op))          \
9375
0
    {               \
9376
0
      sym += strlen (#op);          \
9377
0
      if (*sym == ':')           \
9378
0
  ++sym;             \
9379
0
      *symp = sym;            \
9380
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9381
0
      isymbuf, locsymcount, signed_p)) \
9382
0
  return false;           \
9383
0
      if (signed_p)           \
9384
0
  *result = op ((bfd_signed_vma) a);     \
9385
0
      else              \
9386
0
  *result = op a;           \
9387
0
      return true;            \
9388
0
    }
9389
9390
0
#define BINARY_OP_HEAD(op)          \
9391
0
  if (startswith (sym, #op))          \
9392
0
    {               \
9393
0
      sym += strlen (#op);          \
9394
0
      if (*sym == ':')           \
9395
0
  ++sym;             \
9396
0
      *symp = sym;            \
9397
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9398
0
      isymbuf, locsymcount, signed_p)) \
9399
0
  return false;           \
9400
0
      ++*symp;              \
9401
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9402
0
      isymbuf, locsymcount, signed_p)) \
9403
0
  return false;
9404
0
#define BINARY_OP_TAIL(op)          \
9405
0
      if (signed_p)           \
9406
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9407
0
      else              \
9408
0
  *result = a op b;         \
9409
0
      return true;            \
9410
0
    }
9411
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9412
9413
0
    default:
9414
0
      UNARY_OP  (0-);
9415
0
      BINARY_OP_HEAD (<<);
9416
0
      if (b >= sizeof (a) * CHAR_BIT)
9417
0
  {
9418
0
    *result = 0;
9419
0
    return true;
9420
0
  }
9421
0
      signed_p = 0;
9422
0
      BINARY_OP_TAIL (<<);
9423
0
      BINARY_OP_HEAD (>>);
9424
0
      if (b >= sizeof (a) * CHAR_BIT)
9425
0
  {
9426
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9427
0
    return true;
9428
0
  }
9429
0
      BINARY_OP_TAIL (>>);
9430
0
      BINARY_OP (==);
9431
0
      BINARY_OP (!=);
9432
0
      BINARY_OP (<=);
9433
0
      BINARY_OP (>=);
9434
0
      BINARY_OP (&&);
9435
0
      BINARY_OP (||);
9436
0
      UNARY_OP  (~);
9437
0
      UNARY_OP  (!);
9438
0
      BINARY_OP (*);
9439
0
      BINARY_OP_HEAD (/);
9440
0
      if (b == 0)
9441
0
  {
9442
0
    _bfd_error_handler (_("division by zero"));
9443
0
    bfd_set_error (bfd_error_bad_value);
9444
0
    return false;
9445
0
  }
9446
0
      BINARY_OP_TAIL (/);
9447
0
      BINARY_OP_HEAD (%);
9448
0
      if (b == 0)
9449
0
  {
9450
0
    _bfd_error_handler (_("division by zero"));
9451
0
    bfd_set_error (bfd_error_bad_value);
9452
0
    return false;
9453
0
  }
9454
0
      BINARY_OP_TAIL (%);
9455
0
      BINARY_OP (^);
9456
0
      BINARY_OP (|);
9457
0
      BINARY_OP (&);
9458
0
      BINARY_OP (+);
9459
0
      BINARY_OP (-);
9460
0
      BINARY_OP (<);
9461
0
      BINARY_OP (>);
9462
0
#undef UNARY_OP
9463
0
#undef BINARY_OP
9464
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9465
0
      bfd_set_error (bfd_error_invalid_operation);
9466
0
      return false;
9467
0
    }
9468
0
}
9469
9470
static void
9471
put_value (bfd_vma size,
9472
     unsigned long chunksz,
9473
     bfd *input_bfd,
9474
     bfd_vma x,
9475
     bfd_byte *location)
9476
0
{
9477
0
  location += (size - chunksz);
9478
9479
0
  for (; size; size -= chunksz, location -= chunksz)
9480
0
    {
9481
0
      switch (chunksz)
9482
0
  {
9483
0
  case 1:
9484
0
    bfd_put_8 (input_bfd, x, location);
9485
0
    x >>= 8;
9486
0
    break;
9487
0
  case 2:
9488
0
    bfd_put_16 (input_bfd, x, location);
9489
0
    x >>= 16;
9490
0
    break;
9491
0
  case 4:
9492
0
    bfd_put_32 (input_bfd, x, location);
9493
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9494
0
    x >>= 16;
9495
0
    x >>= 16;
9496
0
    break;
9497
0
#ifdef BFD64
9498
0
  case 8:
9499
0
    bfd_put_64 (input_bfd, x, location);
9500
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9501
0
    x >>= 32;
9502
0
    x >>= 32;
9503
0
    break;
9504
0
#endif
9505
0
  default:
9506
0
    abort ();
9507
0
    break;
9508
0
  }
9509
0
    }
9510
0
}
9511
9512
static bfd_vma
9513
get_value (bfd_vma size,
9514
     unsigned long chunksz,
9515
     bfd *input_bfd,
9516
     bfd_byte *location)
9517
0
{
9518
0
  int shift;
9519
0
  bfd_vma x = 0;
9520
9521
  /* Sanity checks.  */
9522
0
  BFD_ASSERT (chunksz <= sizeof (x)
9523
0
        && size >= chunksz
9524
0
        && chunksz != 0
9525
0
        && (size % chunksz) == 0
9526
0
        && input_bfd != NULL
9527
0
        && location != NULL);
9528
9529
0
  if (chunksz == sizeof (x))
9530
0
    {
9531
0
      BFD_ASSERT (size == chunksz);
9532
9533
      /* Make sure that we do not perform an undefined shift operation.
9534
   We know that size == chunksz so there will only be one iteration
9535
   of the loop below.  */
9536
0
      shift = 0;
9537
0
    }
9538
0
  else
9539
0
    shift = 8 * chunksz;
9540
9541
0
  for (; size; size -= chunksz, location += chunksz)
9542
0
    {
9543
0
      switch (chunksz)
9544
0
  {
9545
0
  case 1:
9546
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9547
0
    break;
9548
0
  case 2:
9549
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9550
0
    break;
9551
0
  case 4:
9552
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9553
0
    break;
9554
0
#ifdef BFD64
9555
0
  case 8:
9556
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9557
0
    break;
9558
0
#endif
9559
0
  default:
9560
0
    abort ();
9561
0
  }
9562
0
    }
9563
0
  return x;
9564
0
}
9565
9566
static void
9567
decode_complex_addend (unsigned long *start,   /* in bits */
9568
           unsigned long *oplen,   /* in bits */
9569
           unsigned long *len,     /* in bits */
9570
           unsigned long *wordsz,  /* in bytes */
9571
           unsigned long *chunksz, /* in bytes */
9572
           unsigned long *lsb0_p,
9573
           unsigned long *signed_p,
9574
           unsigned long *trunc_p,
9575
           unsigned long encoded)
9576
0
{
9577
0
  * start     =  encoded  & 0x3F;
9578
0
  * len       = (encoded >>  6) & 0x3F;
9579
0
  * oplen     = (encoded >> 12) & 0x3F;
9580
0
  * wordsz    = (encoded >> 18) & 0xF;
9581
0
  * chunksz   = (encoded >> 22) & 0xF;
9582
0
  * lsb0_p    = (encoded >> 27) & 1;
9583
0
  * signed_p  = (encoded >> 28) & 1;
9584
0
  * trunc_p   = (encoded >> 29) & 1;
9585
0
}
9586
9587
bfd_reloc_status_type
9588
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9589
            asection *input_section,
9590
            bfd_byte *contents,
9591
            Elf_Internal_Rela *rel,
9592
            bfd_vma relocation)
9593
0
{
9594
0
  bfd_vma shift, x, mask;
9595
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9596
0
  bfd_reloc_status_type r;
9597
0
  bfd_size_type octets;
9598
9599
  /*  Perform this reloc, since it is complex.
9600
      (this is not to say that it necessarily refers to a complex
9601
      symbol; merely that it is a self-describing CGEN based reloc.
9602
      i.e. the addend has the complete reloc information (bit start, end,
9603
      word size, etc) encoded within it.).  */
9604
9605
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9606
0
       &chunksz, &lsb0_p, &signed_p,
9607
0
       &trunc_p, rel->r_addend);
9608
9609
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9610
9611
0
  if (lsb0_p)
9612
0
    shift = (start + 1) - len;
9613
0
  else
9614
0
    shift = (8 * wordsz) - (start + len);
9615
9616
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9617
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9618
9619
#ifdef DEBUG
9620
  printf ("Doing complex reloc: "
9621
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9622
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9623
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9624
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9625
    oplen, (unsigned long) x, (unsigned long) mask,
9626
    (unsigned long) relocation);
9627
#endif
9628
9629
0
  r = bfd_reloc_ok;
9630
0
  if (! trunc_p)
9631
    /* Now do an overflow check.  */
9632
0
    r = bfd_check_overflow ((signed_p
9633
0
           ? complain_overflow_signed
9634
0
           : complain_overflow_unsigned),
9635
0
          len, 0, (8 * wordsz),
9636
0
          relocation);
9637
9638
  /* Do the deed.  */
9639
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9640
9641
#ifdef DEBUG
9642
  printf ("           relocation: %8.8lx\n"
9643
    "         shifted mask: %8.8lx\n"
9644
    " shifted/masked reloc: %8.8lx\n"
9645
    "               result: %8.8lx\n",
9646
    (unsigned long) relocation, (unsigned long) (mask << shift),
9647
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9648
#endif
9649
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9650
0
  return r;
9651
0
}
9652
9653
/* Functions to read r_offset from external (target order) reloc
9654
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9655
   know the value is aligned.  */
9656
9657
static bfd_vma
9658
ext32l_r_offset (const void *p)
9659
0
{
9660
0
  union aligned32
9661
0
  {
9662
0
    uint32_t v;
9663
0
    unsigned char c[4];
9664
0
  };
9665
0
  const union aligned32 *a
9666
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9667
9668
0
  uint32_t aval = (  (uint32_t) a->c[0]
9669
0
       | (uint32_t) a->c[1] << 8
9670
0
       | (uint32_t) a->c[2] << 16
9671
0
       | (uint32_t) a->c[3] << 24);
9672
0
  return aval;
9673
0
}
9674
9675
static bfd_vma
9676
ext32b_r_offset (const void *p)
9677
0
{
9678
0
  union aligned32
9679
0
  {
9680
0
    uint32_t v;
9681
0
    unsigned char c[4];
9682
0
  };
9683
0
  const union aligned32 *a
9684
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9685
9686
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9687
0
       | (uint32_t) a->c[1] << 16
9688
0
       | (uint32_t) a->c[2] << 8
9689
0
       | (uint32_t) a->c[3]);
9690
0
  return aval;
9691
0
}
9692
9693
static bfd_vma
9694
ext64l_r_offset (const void *p)
9695
0
{
9696
0
  union aligned64
9697
0
  {
9698
0
    uint64_t v;
9699
0
    unsigned char c[8];
9700
0
  };
9701
0
  const union aligned64 *a
9702
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9703
9704
0
  uint64_t aval = (  (uint64_t) a->c[0]
9705
0
       | (uint64_t) a->c[1] << 8
9706
0
       | (uint64_t) a->c[2] << 16
9707
0
       | (uint64_t) a->c[3] << 24
9708
0
       | (uint64_t) a->c[4] << 32
9709
0
       | (uint64_t) a->c[5] << 40
9710
0
       | (uint64_t) a->c[6] << 48
9711
0
       | (uint64_t) a->c[7] << 56);
9712
0
  return aval;
9713
0
}
9714
9715
static bfd_vma
9716
ext64b_r_offset (const void *p)
9717
0
{
9718
0
  union aligned64
9719
0
  {
9720
0
    uint64_t v;
9721
0
    unsigned char c[8];
9722
0
  };
9723
0
  const union aligned64 *a
9724
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9725
9726
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9727
0
       | (uint64_t) a->c[1] << 48
9728
0
       | (uint64_t) a->c[2] << 40
9729
0
       | (uint64_t) a->c[3] << 32
9730
0
       | (uint64_t) a->c[4] << 24
9731
0
       | (uint64_t) a->c[5] << 16
9732
0
       | (uint64_t) a->c[6] << 8
9733
0
       | (uint64_t) a->c[7]);
9734
0
  return aval;
9735
0
}
9736
9737
/* When performing a relocatable link, the input relocations are
9738
   preserved.  But, if they reference global symbols, the indices
9739
   referenced must be updated.  Update all the relocations found in
9740
   RELDATA.  */
9741
9742
static bool
9743
elf_link_adjust_relocs (bfd *abfd,
9744
      asection *sec,
9745
      struct bfd_elf_section_reloc_data *reldata,
9746
      bool sort,
9747
      struct bfd_link_info *info)
9748
0
{
9749
0
  unsigned int i;
9750
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9751
0
  bfd_byte *erela;
9752
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9753
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9754
0
  bfd_vma r_type_mask;
9755
0
  int r_sym_shift;
9756
0
  unsigned int count = reldata->count;
9757
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9758
9759
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9760
0
    {
9761
0
      swap_in = bed->s->swap_reloc_in;
9762
0
      swap_out = bed->s->swap_reloc_out;
9763
0
    }
9764
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9765
0
    {
9766
0
      swap_in = bed->s->swap_reloca_in;
9767
0
      swap_out = bed->s->swap_reloca_out;
9768
0
    }
9769
0
  else
9770
0
    abort ();
9771
9772
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9773
0
    abort ();
9774
9775
0
  if (bed->s->arch_size == 32)
9776
0
    {
9777
0
      r_type_mask = 0xff;
9778
0
      r_sym_shift = 8;
9779
0
    }
9780
0
  else
9781
0
    {
9782
0
      r_type_mask = 0xffffffff;
9783
0
      r_sym_shift = 32;
9784
0
    }
9785
9786
0
  erela = reldata->hdr->contents;
9787
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9788
0
    {
9789
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9790
0
      unsigned int j;
9791
9792
0
      if (*rel_hash == NULL)
9793
0
  continue;
9794
9795
0
      if ((*rel_hash)->indx == -2
9796
0
    && info->gc_sections
9797
0
    && ! info->gc_keep_exported)
9798
0
  {
9799
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9800
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9801
0
            abfd, sec,
9802
0
            (*rel_hash)->root.root.string);
9803
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9804
0
            abfd, sec);
9805
0
    bfd_set_error (bfd_error_invalid_operation);
9806
0
    return false;
9807
0
  }
9808
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9809
9810
0
      (*swap_in) (abfd, erela, irela);
9811
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9812
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9813
0
         | (irela[j].r_info & r_type_mask));
9814
0
      (*swap_out) (abfd, irela, erela);
9815
0
    }
9816
9817
0
  if (bed->elf_backend_update_relocs)
9818
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9819
9820
0
  if (sort && count != 0)
9821
0
    {
9822
0
      bfd_vma (*ext_r_off) (const void *);
9823
0
      bfd_vma r_off;
9824
0
      size_t elt_size;
9825
0
      bfd_byte *base, *end, *p, *loc;
9826
0
      bfd_byte *buf = NULL;
9827
9828
0
      if (bed->s->arch_size == 32)
9829
0
  {
9830
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9831
0
      ext_r_off = ext32l_r_offset;
9832
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9833
0
      ext_r_off = ext32b_r_offset;
9834
0
    else
9835
0
      abort ();
9836
0
  }
9837
0
      else
9838
0
  {
9839
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9840
0
      ext_r_off = ext64l_r_offset;
9841
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9842
0
      ext_r_off = ext64b_r_offset;
9843
0
    else
9844
0
      abort ();
9845
0
  }
9846
9847
      /*  Must use a stable sort here.  A modified insertion sort,
9848
    since the relocs are mostly sorted already.  */
9849
0
      elt_size = reldata->hdr->sh_entsize;
9850
0
      base = reldata->hdr->contents;
9851
0
      end = base + count * elt_size;
9852
0
      if (elt_size > sizeof (Elf64_External_Rela))
9853
0
  abort ();
9854
9855
      /* Ensure the first element is lowest.  This acts as a sentinel,
9856
   speeding the main loop below.  */
9857
0
      r_off = (*ext_r_off) (base);
9858
0
      for (p = loc = base; (p += elt_size) < end; )
9859
0
  {
9860
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9861
0
    if (r_off > r_off2)
9862
0
      {
9863
0
        r_off = r_off2;
9864
0
        loc = p;
9865
0
      }
9866
0
  }
9867
0
      if (loc != base)
9868
0
  {
9869
    /* Don't just swap *base and *loc as that changes the order
9870
       of the original base[0] and base[1] if they happen to
9871
       have the same r_offset.  */
9872
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9873
0
    memcpy (onebuf, loc, elt_size);
9874
0
    memmove (base + elt_size, base, loc - base);
9875
0
    memcpy (base, onebuf, elt_size);
9876
0
  }
9877
9878
0
      for (p = base + elt_size; (p += elt_size) < end; )
9879
0
  {
9880
    /* base to p is sorted, *p is next to insert.  */
9881
0
    r_off = (*ext_r_off) (p);
9882
    /* Search the sorted region for location to insert.  */
9883
0
    loc = p - elt_size;
9884
0
    while (r_off < (*ext_r_off) (loc))
9885
0
      loc -= elt_size;
9886
0
    loc += elt_size;
9887
0
    if (loc != p)
9888
0
      {
9889
        /* Chances are there is a run of relocs to insert here,
9890
     from one of more input files.  Files are not always
9891
     linked in order due to the way elf_link_input_bfd is
9892
     called.  See pr17666.  */
9893
0
        size_t sortlen = p - loc;
9894
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9895
0
        size_t runlen = elt_size;
9896
0
        bfd_vma r_off_runend = r_off;
9897
0
        bfd_vma r_off_runend_next;
9898
0
        size_t buf_size = 96 * 1024;
9899
0
        while (p + runlen < end
9900
0
         && (sortlen <= buf_size
9901
0
       || runlen + elt_size <= buf_size)
9902
         /* run must not break the ordering of base..loc+1 */
9903
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9904
         /* run must be already sorted */
9905
0
         && r_off_runend_next >= r_off_runend)
9906
0
    {
9907
0
      runlen += elt_size;
9908
0
      r_off_runend = r_off_runend_next;
9909
0
    }
9910
0
        if (buf == NULL)
9911
0
    {
9912
0
      buf = bfd_malloc (buf_size);
9913
0
      if (buf == NULL)
9914
0
        return false;
9915
0
    }
9916
0
        if (runlen < sortlen)
9917
0
    {
9918
0
      memcpy (buf, p, runlen);
9919
0
      memmove (loc + runlen, loc, sortlen);
9920
0
      memcpy (loc, buf, runlen);
9921
0
    }
9922
0
        else
9923
0
    {
9924
0
      memcpy (buf, loc, sortlen);
9925
0
      memmove (loc, p, runlen);
9926
0
      memcpy (loc + runlen, buf, sortlen);
9927
0
    }
9928
0
        p += runlen - elt_size;
9929
0
      }
9930
0
  }
9931
      /* Hashes are no longer valid.  */
9932
0
      free (reldata->hashes);
9933
0
      reldata->hashes = NULL;
9934
0
      free (buf);
9935
0
    }
9936
0
  return true;
9937
0
}
9938
9939
struct elf_link_sort_rela
9940
{
9941
  union {
9942
    bfd_vma offset;
9943
    bfd_vma sym_mask;
9944
  } u;
9945
  enum elf_reloc_type_class type;
9946
  /* We use this as an array of size int_rels_per_ext_rel.  */
9947
  Elf_Internal_Rela rela[1];
9948
};
9949
9950
/* qsort stability here and for cmp2 is only an issue if multiple
9951
   dynamic relocations are emitted at the same address.  But targets
9952
   that apply a series of dynamic relocations each operating on the
9953
   result of the prior relocation can't use -z combreloc as
9954
   implemented anyway.  Such schemes tend to be broken by sorting on
9955
   symbol index.  That leaves dynamic NONE relocs as the only other
9956
   case where ld might emit multiple relocs at the same address, and
9957
   those are only emitted due to target bugs.  */
9958
9959
static int
9960
elf_link_sort_cmp1 (const void *A, const void *B)
9961
0
{
9962
0
  const struct elf_link_sort_rela *a = A;
9963
0
  const struct elf_link_sort_rela *b = B;
9964
0
  int relativea, relativeb;
9965
9966
0
  relativea = a->type == reloc_class_relative;
9967
0
  relativeb = b->type == reloc_class_relative;
9968
9969
0
  if (relativea < relativeb)
9970
0
    return 1;
9971
0
  if (relativea > relativeb)
9972
0
    return -1;
9973
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9974
0
    return -1;
9975
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9976
0
    return 1;
9977
0
  if (a->rela->r_offset < b->rela->r_offset)
9978
0
    return -1;
9979
0
  if (a->rela->r_offset > b->rela->r_offset)
9980
0
    return 1;
9981
0
  return 0;
9982
0
}
9983
9984
static int
9985
elf_link_sort_cmp2 (const void *A, const void *B)
9986
0
{
9987
0
  const struct elf_link_sort_rela *a = A;
9988
0
  const struct elf_link_sort_rela *b = B;
9989
9990
0
  if (a->type < b->type)
9991
0
    return -1;
9992
0
  if (a->type > b->type)
9993
0
    return 1;
9994
0
  if (a->u.offset < b->u.offset)
9995
0
    return -1;
9996
0
  if (a->u.offset > b->u.offset)
9997
0
    return 1;
9998
0
  if (a->rela->r_offset < b->rela->r_offset)
9999
0
    return -1;
10000
0
  if (a->rela->r_offset > b->rela->r_offset)
10001
0
    return 1;
10002
0
  return 0;
10003
0
}
10004
10005
static size_t
10006
elf_link_sort_relocs (bfd *obfd, struct bfd_link_info *info, asection **psec)
10007
0
{
10008
0
  asection *dynamic_relocs;
10009
0
  asection *rela_dyn;
10010
0
  asection *rel_dyn;
10011
0
  bfd_size_type count, size;
10012
0
  size_t i, ret, sort_elt, ext_size;
10013
0
  bfd_byte *sort, *s_non_relative, *p;
10014
0
  struct elf_link_sort_rela *sq;
10015
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
10016
0
  int i2e = obed->s->int_rels_per_ext_rel;
10017
0
  unsigned int opb = bfd_octets_per_byte (obfd, NULL);
10018
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10019
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10020
0
  struct bfd_link_order *lo;
10021
0
  bfd_vma r_sym_mask;
10022
0
  bool use_rela;
10023
10024
  /* Find a dynamic reloc section.  */
10025
0
  rela_dyn = bfd_get_section_by_name (obfd, ".rela.dyn");
10026
0
  rel_dyn  = bfd_get_section_by_name (obfd, ".rel.dyn");
10027
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10028
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10029
0
    {
10030
0
      bool use_rela_initialised = false;
10031
10032
      /* This is just here to stop gcc from complaining.
10033
   Its initialization checking code is not perfect.  */
10034
0
      use_rela = true;
10035
10036
      /* Both sections are present.  Examine the sizes
10037
   of the indirect sections to help us choose.  */
10038
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10039
0
  if (lo->type == bfd_indirect_link_order)
10040
0
    {
10041
0
      asection *o = lo->u.indirect.section;
10042
10043
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10044
0
        {
10045
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10046
      /* Section size is divisible by both rel and rela sizes.
10047
         It is of no help to us.  */
10048
0
      ;
10049
0
    else
10050
0
      {
10051
        /* Section size is only divisible by rela.  */
10052
0
        if (use_rela_initialised && !use_rela)
10053
0
          {
10054
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10055
0
                "they are in more than one size"),
10056
0
              obfd);
10057
0
      bfd_set_error (bfd_error_invalid_operation);
10058
0
      return 0;
10059
0
          }
10060
0
        else
10061
0
          {
10062
0
      use_rela = true;
10063
0
      use_rela_initialised = true;
10064
0
          }
10065
0
      }
10066
0
        }
10067
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10068
0
        {
10069
    /* Section size is only divisible by rel.  */
10070
0
    if (use_rela_initialised && use_rela)
10071
0
      {
10072
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10073
0
            "they are in more than one size"),
10074
0
          obfd);
10075
0
        bfd_set_error (bfd_error_invalid_operation);
10076
0
        return 0;
10077
0
      }
10078
0
    else
10079
0
      {
10080
0
        use_rela = false;
10081
0
        use_rela_initialised = true;
10082
0
      }
10083
0
        }
10084
0
      else
10085
0
        {
10086
    /* The section size is not divisible by either -
10087
       something is wrong.  */
10088
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10089
0
              "they are of an unknown size"), obfd);
10090
0
    bfd_set_error (bfd_error_invalid_operation);
10091
0
    return 0;
10092
0
        }
10093
0
    }
10094
10095
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10096
0
  if (lo->type == bfd_indirect_link_order)
10097
0
    {
10098
0
      asection *o = lo->u.indirect.section;
10099
10100
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10101
0
        {
10102
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10103
      /* Section size is divisible by both rel and rela sizes.
10104
         It is of no help to us.  */
10105
0
      ;
10106
0
    else
10107
0
      {
10108
        /* Section size is only divisible by rela.  */
10109
0
        if (use_rela_initialised && !use_rela)
10110
0
          {
10111
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10112
0
                "they are in more than one size"),
10113
0
              obfd);
10114
0
      bfd_set_error (bfd_error_invalid_operation);
10115
0
      return 0;
10116
0
          }
10117
0
        else
10118
0
          {
10119
0
      use_rela = true;
10120
0
      use_rela_initialised = true;
10121
0
          }
10122
0
      }
10123
0
        }
10124
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10125
0
        {
10126
    /* Section size is only divisible by rel.  */
10127
0
    if (use_rela_initialised && use_rela)
10128
0
      {
10129
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10130
0
            "they are in more than one size"),
10131
0
          obfd);
10132
0
        bfd_set_error (bfd_error_invalid_operation);
10133
0
        return 0;
10134
0
      }
10135
0
    else
10136
0
      {
10137
0
        use_rela = false;
10138
0
        use_rela_initialised = true;
10139
0
      }
10140
0
        }
10141
0
      else
10142
0
        {
10143
    /* The section size is not divisible by either -
10144
       something is wrong.  */
10145
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10146
0
              "they are of an unknown size"), obfd);
10147
0
    bfd_set_error (bfd_error_invalid_operation);
10148
0
    return 0;
10149
0
        }
10150
0
    }
10151
10152
0
      if (! use_rela_initialised)
10153
  /* Make a guess.  */
10154
0
  use_rela = true;
10155
0
    }
10156
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10157
0
    use_rela = true;
10158
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10159
0
    use_rela = false;
10160
0
  else
10161
0
    return 0;
10162
10163
0
  if (use_rela)
10164
0
    {
10165
0
      dynamic_relocs = rela_dyn;
10166
0
      ext_size = obed->s->sizeof_rela;
10167
0
      swap_in = obed->s->swap_reloca_in;
10168
0
      swap_out = obed->s->swap_reloca_out;
10169
0
    }
10170
0
  else
10171
0
    {
10172
0
      dynamic_relocs = rel_dyn;
10173
0
      ext_size = obed->s->sizeof_rel;
10174
0
      swap_in = obed->s->swap_reloc_in;
10175
0
      swap_out = obed->s->swap_reloc_out;
10176
0
    }
10177
10178
0
  size = 0;
10179
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10180
0
    if (lo->type == bfd_indirect_link_order)
10181
0
      size += lo->u.indirect.section->size;
10182
10183
0
  if (size != dynamic_relocs->size)
10184
0
    return 0;
10185
10186
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10187
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10188
10189
0
  count = dynamic_relocs->size / ext_size;
10190
0
  if (count == 0)
10191
0
    return 0;
10192
0
  sort = bfd_zmalloc (sort_elt * count);
10193
10194
0
  if (sort == NULL)
10195
0
    {
10196
0
      (*info->callbacks->warning)
10197
0
  (info, _("not enough memory to sort relocations"), 0, obfd, 0, 0);
10198
0
      return 0;
10199
0
    }
10200
10201
0
  if (obed->s->arch_size == 32)
10202
0
    r_sym_mask = ~(bfd_vma) 0xff;
10203
0
  else
10204
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10205
10206
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10207
0
    if (lo->type == bfd_indirect_link_order)
10208
0
      {
10209
0
  bfd_byte *erel, *erelend;
10210
0
  asection *o = lo->u.indirect.section;
10211
10212
0
  if (o->contents == NULL && o->size != 0)
10213
0
    {
10214
      /* This is a reloc section that is being handled as a normal
10215
         section.  See bfd_section_from_shdr.  We can't combine
10216
         relocs in this case.  */
10217
0
      free (sort);
10218
0
      return 0;
10219
0
    }
10220
0
  erel = o->contents;
10221
0
  erelend = o->contents + o->size;
10222
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10223
10224
0
  while (erel < erelend)
10225
0
    {
10226
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10227
10228
0
      (*swap_in) (obfd, erel, s->rela);
10229
0
      s->type = obed->elf_backend_reloc_type_class (info, o, s->rela);
10230
0
      s->u.sym_mask = r_sym_mask;
10231
0
      p += sort_elt;
10232
0
      erel += ext_size;
10233
0
    }
10234
0
      }
10235
10236
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10237
10238
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10239
0
    {
10240
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10241
0
      if (s->type != reloc_class_relative)
10242
0
  break;
10243
0
    }
10244
0
  ret = i;
10245
0
  s_non_relative = p;
10246
10247
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10248
0
  for (; i < count; i++, p += sort_elt)
10249
0
    {
10250
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10251
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10252
0
  sq = sp;
10253
0
      sp->u.offset = sq->rela->r_offset;
10254
0
    }
10255
10256
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10257
10258
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10259
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10260
0
    {
10261
      /* We have plt relocs in .rela.dyn.  */
10262
0
      sq = (struct elf_link_sort_rela *) sort;
10263
0
      for (i = 0; i < count; i++)
10264
0
  if (sq[count - i - 1].type != reloc_class_plt)
10265
0
    break;
10266
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10267
0
  {
10268
0
    struct bfd_link_order **plo;
10269
    /* Put srelplt link_order last.  This is so the output_offset
10270
       set in the next loop is correct for DT_JMPREL.  */
10271
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10272
0
      if ((*plo)->type == bfd_indirect_link_order
10273
0
    && (*plo)->u.indirect.section == htab->srelplt)
10274
0
        {
10275
0
    lo = *plo;
10276
0
    *plo = lo->next;
10277
0
        }
10278
0
      else
10279
0
        plo = &(*plo)->next;
10280
0
    *plo = lo;
10281
0
    lo->next = NULL;
10282
0
    dynamic_relocs->map_tail.link_order = lo;
10283
0
  }
10284
0
    }
10285
10286
0
  p = sort;
10287
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10288
0
    if (lo->type == bfd_indirect_link_order)
10289
0
      {
10290
0
  bfd_byte *erel, *erelend;
10291
0
  asection *o = lo->u.indirect.section;
10292
10293
0
  erel = o->contents;
10294
0
  erelend = o->contents + o->size;
10295
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10296
0
  while (erel < erelend)
10297
0
    {
10298
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10299
0
      (*swap_out) (obfd, s->rela, erel);
10300
0
      p += sort_elt;
10301
0
      erel += ext_size;
10302
0
    }
10303
0
      }
10304
10305
0
  free (sort);
10306
0
  *psec = dynamic_relocs;
10307
0
  return ret;
10308
0
}
10309
10310
/* Add a symbol to the output symbol string table.  */
10311
10312
static int
10313
elf_link_output_symstrtab (void *finf,
10314
         const char *name,
10315
         Elf_Internal_Sym *elfsym,
10316
         asection *input_sec,
10317
         struct elf_link_hash_entry *h)
10318
0
{
10319
0
  struct elf_final_link_info *flinfo = finf;
10320
0
  int (*output_symbol_hook)
10321
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10322
0
     struct elf_link_hash_entry *);
10323
0
  struct elf_link_hash_table *hash_table;
10324
0
  elf_backend_data *bed;
10325
0
  bfd_size_type strtabsize;
10326
10327
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10328
10329
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10330
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10331
0
  if (output_symbol_hook != NULL)
10332
0
    {
10333
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10334
0
      if (ret != 1)
10335
0
  return ret;
10336
0
    }
10337
10338
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10339
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10340
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10341
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10342
10343
0
  if (name == NULL || *name == '\0')
10344
0
    elfsym->st_name = (unsigned long) -1;
10345
0
  else
10346
0
    {
10347
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10348
   to get the final offset for st_name.  */
10349
0
      char *versioned_name = (char *) name;
10350
0
      if (h != NULL)
10351
0
  {
10352
0
    if (h->versioned == versioned && h->def_dynamic)
10353
0
      {
10354
        /* Keep only one '@' for versioned symbols defined in
10355
           shared objects.  */
10356
0
        const char *version = strrchr (name, ELF_VER_CHR);
10357
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10358
0
        if (version != base_end)
10359
0
    {
10360
0
      size_t base_len;
10361
0
      size_t len = strlen (name);
10362
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10363
0
      if (versioned_name == NULL)
10364
0
        return 0;
10365
0
      base_len = base_end - name;
10366
0
      memcpy (versioned_name, name, base_len);
10367
0
      memcpy (versioned_name + base_len, version,
10368
0
        len - base_len);
10369
0
    }
10370
0
      }
10371
0
  }
10372
0
      else if (flinfo->info->unique_symbol
10373
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10374
0
  {
10375
0
    struct local_hash_entry *lh;
10376
0
    size_t count_len;
10377
0
    size_t base_len;
10378
0
    char buf[30];
10379
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10380
0
      {
10381
0
      case STT_FILE:
10382
0
      case STT_SECTION:
10383
0
        break;
10384
0
      default:
10385
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10386
0
         (&flinfo->local_hash_table, name, true, false);
10387
0
        if (lh == NULL)
10388
0
    return 0;
10389
        /* Always append ".COUNT" to local symbols to avoid
10390
     potential conflicts with local symbol "XXX.COUNT".  */
10391
0
        sprintf (buf, "%lx", lh->count);
10392
0
        base_len = lh->size;
10393
0
        if (!base_len)
10394
0
    {
10395
0
      base_len = strlen (name);
10396
0
      lh->size = base_len;
10397
0
    }
10398
0
        count_len = strlen (buf);
10399
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10400
0
            base_len + count_len + 2);
10401
0
        if (versioned_name == NULL)
10402
0
    return 0;
10403
0
        memcpy (versioned_name, name, base_len);
10404
0
        versioned_name[base_len] = '.';
10405
0
        memcpy (versioned_name + base_len + 1, buf,
10406
0
          count_len + 1);
10407
0
        lh->count++;
10408
0
        break;
10409
0
      }
10410
0
  }
10411
0
      elfsym->st_name
10412
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10413
0
                 versioned_name, false);
10414
0
      if (elfsym->st_name == (unsigned long) -1)
10415
0
  return 0;
10416
0
    }
10417
10418
0
  hash_table = elf_hash_table (flinfo->info);
10419
0
  strtabsize = hash_table->strtabsize;
10420
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10421
0
    {
10422
0
      strtabsize += strtabsize;
10423
0
      hash_table->strtabsize = strtabsize;
10424
0
      strtabsize *= sizeof (*hash_table->strtab);
10425
0
      hash_table->strtab = bfd_realloc (hash_table->strtab, strtabsize);
10426
0
      if (hash_table->strtab == NULL)
10427
0
  return 0;
10428
0
    }
10429
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10430
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10431
0
    = flinfo->output_bfd->symcount;
10432
0
  flinfo->output_bfd->symcount += 1;
10433
10434
0
  return 1;
10435
0
}
10436
10437
/* Swap symbols out to the symbol table and flush the output symbols to
10438
   the file.  */
10439
10440
static bool
10441
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10442
0
{
10443
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10444
0
  size_t amt;
10445
0
  size_t i;
10446
0
  elf_backend_data *bed;
10447
0
  bfd_byte *symbuf;
10448
0
  Elf_Internal_Shdr *hdr;
10449
0
  file_ptr pos;
10450
0
  bool ret;
10451
10452
0
  if (flinfo->output_bfd->symcount == 0)
10453
0
    return true;
10454
10455
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10456
10457
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10458
10459
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10460
0
  symbuf = bfd_malloc (amt);
10461
0
  if (symbuf == NULL)
10462
0
    return false;
10463
10464
0
  if (flinfo->symshndxbuf)
10465
0
    {
10466
0
      amt = sizeof (Elf_External_Sym_Shndx);
10467
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10468
0
      flinfo->symshndxbuf = bfd_zmalloc (amt);
10469
0
      if (flinfo->symshndxbuf == NULL)
10470
0
  {
10471
0
    free (symbuf);
10472
0
    return false;
10473
0
  }
10474
0
    }
10475
10476
  /* Now swap out the symbols.  */
10477
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10478
0
    {
10479
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10480
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10481
0
  elfsym->sym.st_name = 0;
10482
0
      else
10483
0
  elfsym->sym.st_name
10484
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10485
0
                elfsym->sym.st_name);
10486
10487
      /* Inform the linker of the addition of this symbol.  */
10488
10489
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10490
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10491
0
             &elfsym->sym);
10492
10493
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10494
0
             ((bfd_byte *) symbuf
10495
0
        + (elfsym->dest_index
10496
0
           * bed->s->sizeof_sym)),
10497
0
             NPTR_ADD (flinfo->symshndxbuf,
10498
0
           elfsym->dest_index));
10499
0
    }
10500
10501
0
  hdr = &elf_symtab_hdr (flinfo->output_bfd);
10502
0
  pos = hdr->sh_offset + hdr->sh_size;
10503
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10504
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10505
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10506
0
    {
10507
0
      hdr->sh_size += amt;
10508
0
      ret = true;
10509
0
    }
10510
0
  else
10511
0
    ret = false;
10512
10513
0
  free (symbuf);
10514
0
  return ret;
10515
0
}
10516
10517
/* Return TRUE if the dynamic symbol SYM in OBFD is supported.  */
10518
10519
static bool
10520
check_dynsym (bfd *obfd, Elf_Internal_Sym *sym)
10521
0
{
10522
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10523
0
      && sym->st_shndx < SHN_LORESERVE)
10524
0
    {
10525
      /* The gABI doesn't support dynamic symbols in output sections
10526
   beyond 64k.  */
10527
0
      _bfd_error_handler
10528
  /* xgettext:c-format */
10529
0
  (_("%pB: too many sections: %d (>= %d)"),
10530
0
   obfd, bfd_count_sections (obfd), SHN_LORESERVE & 0xffff);
10531
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10532
0
      return false;
10533
0
    }
10534
0
  return true;
10535
0
}
10536
10537
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10538
   allowing an unsatisfied unversioned symbol in the DSO to match a
10539
   versioned symbol that would normally require an explicit version.
10540
   We also handle the case that a DSO references a hidden symbol
10541
   which may be satisfied by a versioned symbol in another DSO.  */
10542
10543
static bool
10544
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10545
         elf_backend_data *bed,
10546
         struct elf_link_hash_entry *h)
10547
0
{
10548
0
  bfd *abfd;
10549
0
  struct elf_link_loaded_list *loaded;
10550
10551
0
  if (!is_elf_hash_table (info->hash))
10552
0
    return false;
10553
10554
  /* Check indirect symbol.  */
10555
0
  while (h->root.type == bfd_link_hash_indirect)
10556
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10557
10558
0
  switch (h->root.type)
10559
0
    {
10560
0
    default:
10561
0
      abfd = NULL;
10562
0
      break;
10563
10564
0
    case bfd_link_hash_undefined:
10565
0
    case bfd_link_hash_undefweak:
10566
0
      abfd = h->root.u.undef.abfd;
10567
0
      if (abfd == NULL
10568
0
    || (abfd->flags & DYNAMIC) == 0
10569
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10570
0
  return false;
10571
0
      break;
10572
10573
0
    case bfd_link_hash_defined:
10574
0
    case bfd_link_hash_defweak:
10575
0
      abfd = h->root.u.def.section->owner;
10576
0
      break;
10577
10578
0
    case bfd_link_hash_common:
10579
0
      abfd = h->root.u.c.p->section->owner;
10580
0
      break;
10581
0
    }
10582
0
  BFD_ASSERT (abfd != NULL);
10583
10584
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10585
0
       loaded != NULL;
10586
0
       loaded = loaded->next)
10587
0
    {
10588
0
      bfd *input;
10589
0
      Elf_Internal_Shdr *hdr;
10590
0
      size_t symcount;
10591
0
      size_t extsymcount;
10592
0
      size_t extsymoff;
10593
0
      Elf_Internal_Shdr *versymhdr;
10594
0
      Elf_Internal_Sym *isym;
10595
0
      Elf_Internal_Sym *isymend;
10596
0
      Elf_Internal_Sym *isymbuf;
10597
0
      Elf_External_Versym *ever;
10598
0
      Elf_External_Versym *extversym;
10599
10600
0
      input = loaded->abfd;
10601
10602
      /* We check each DSO for a possible hidden versioned definition.  */
10603
0
      if (input == abfd
10604
0
    || elf_dynversym (input) == 0)
10605
0
  continue;
10606
10607
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10608
10609
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10610
0
      if (elf_bad_symtab (input))
10611
0
  {
10612
0
    extsymcount = symcount;
10613
0
    extsymoff = 0;
10614
0
  }
10615
0
      else
10616
0
  {
10617
0
    extsymcount = symcount - hdr->sh_info;
10618
0
    extsymoff = hdr->sh_info;
10619
0
  }
10620
10621
0
      if (extsymcount == 0)
10622
0
  continue;
10623
10624
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10625
0
              NULL, NULL, NULL);
10626
0
      if (isymbuf == NULL)
10627
0
  return false;
10628
10629
      /* Read in any version definitions.  */
10630
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10631
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10632
0
    || (extversym = _bfd_malloc_and_read (input, versymhdr->sh_size,
10633
0
            versymhdr->sh_size)) == NULL)
10634
0
  {
10635
0
    free (isymbuf);
10636
0
    return false;
10637
0
  }
10638
10639
0
      ever = extversym + extsymoff;
10640
0
      isymend = isymbuf + extsymcount;
10641
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10642
0
  {
10643
0
    const char *name;
10644
0
    Elf_Internal_Versym iver;
10645
0
    unsigned short version_index;
10646
10647
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10648
0
        || isym->st_shndx == SHN_UNDEF)
10649
0
      continue;
10650
10651
0
    name = bfd_elf_string_from_elf_section (input,
10652
0
              hdr->sh_link,
10653
0
              isym->st_name);
10654
0
    if (strcmp (name, h->root.root.string) != 0)
10655
0
      continue;
10656
10657
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10658
10659
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10660
0
        && !(h->def_regular
10661
0
       && h->forced_local))
10662
0
      {
10663
        /* If we have a non-hidden versioned sym, then it should
10664
     have provided a definition for the undefined sym unless
10665
     it is defined in a non-shared object and forced local.
10666
         */
10667
0
        abort ();
10668
0
      }
10669
10670
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10671
0
    if (version_index == 1 || version_index == 2)
10672
0
      {
10673
        /* This is the base or first version.  We can use it.  */
10674
0
        free (extversym);
10675
0
        free (isymbuf);
10676
0
        return true;
10677
0
      }
10678
0
  }
10679
10680
0
      free (extversym);
10681
0
      free (isymbuf);
10682
0
    }
10683
10684
0
  return false;
10685
0
}
10686
10687
/* Convert ELF common symbol TYPE.  */
10688
10689
static int
10690
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10691
0
{
10692
  /* Commom symbol can only appear in relocatable link.  */
10693
0
  if (!bfd_link_relocatable (info))
10694
0
    abort ();
10695
0
  switch (info->elf_stt_common)
10696
0
    {
10697
0
    case unchanged:
10698
0
      break;
10699
0
    case elf_stt_common:
10700
0
      type = STT_COMMON;
10701
0
      break;
10702
0
    case no_elf_stt_common:
10703
0
      type = STT_OBJECT;
10704
0
      break;
10705
0
    }
10706
0
  return type;
10707
0
}
10708
10709
/* Add an external symbol to the symbol table.  This is called from
10710
   the hash table traversal routine.  When generating a shared object,
10711
   we go through the symbol table twice.  The first time we output
10712
   anything that might have been forced to local scope in a version
10713
   script.  The second time we output the symbols that are still
10714
   global symbols.  */
10715
10716
static bool
10717
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10718
0
{
10719
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10720
0
  struct elf_outext_info *eoinfo = data;
10721
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10722
0
  bool strip;
10723
0
  Elf_Internal_Sym sym;
10724
0
  asection *input_sec;
10725
0
  elf_backend_data *bed;
10726
0
  long indx;
10727
0
  int ret;
10728
0
  unsigned int type;
10729
10730
  /* Skip if base symbol doesn't match.  */
10731
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10732
0
    return true;
10733
10734
0
  if (h->root.type == bfd_link_hash_warning)
10735
0
    {
10736
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10737
0
      if (h->root.type == bfd_link_hash_new)
10738
0
  return true;
10739
0
    }
10740
10741
  /* Decide whether to output this symbol in this pass.  */
10742
0
  if (eoinfo->localsyms)
10743
0
    {
10744
0
      if (!h->forced_local)
10745
0
  return true;
10746
0
    }
10747
0
  else
10748
0
    {
10749
0
      if (h->forced_local)
10750
0
  return true;
10751
0
    }
10752
10753
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10754
10755
0
  if (h->root.type == bfd_link_hash_undefined)
10756
0
    {
10757
      /* If we have an undefined symbol reference here then it must have
10758
   come from a shared library that is being linked in.  (Undefined
10759
   references in regular files have already been handled unless
10760
   they are in unreferenced sections which are removed by garbage
10761
   collection).  */
10762
0
      bool ignore_undef = false;
10763
10764
      /* Some symbols may be special in that the fact that they're
10765
   undefined can be safely ignored - let backend determine that.  */
10766
0
      if (bed->elf_backend_ignore_undef_symbol)
10767
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10768
10769
      /* If we are reporting errors for this situation then do so now.  */
10770
0
      if (!ignore_undef
10771
0
    && h->ref_dynamic_nonweak
10772
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10773
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10774
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10775
0
  {
10776
0
    flinfo->info->callbacks->undefined_symbol
10777
0
      (flinfo->info, h->root.root.string,
10778
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10779
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10780
0
       && !flinfo->info->warn_unresolved_syms);
10781
0
  }
10782
10783
      /* Strip a global symbol defined in a discarded section.  */
10784
0
      if (h->indx == -3)
10785
0
  return true;
10786
0
    }
10787
10788
  /* We should also warn if a forced local symbol is referenced from
10789
     shared libraries.  */
10790
0
  if (bfd_link_executable (flinfo->info)
10791
0
      && h->forced_local
10792
0
      && h->ref_dynamic
10793
0
      && h->def_regular
10794
0
      && !h->dynamic_def
10795
0
      && h->ref_dynamic_nonweak
10796
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10797
0
    {
10798
0
      bfd *def_bfd;
10799
0
      const char *msg;
10800
0
      struct elf_link_hash_entry *hi = h;
10801
10802
      /* Check indirect symbol.  */
10803
0
      while (hi->root.type == bfd_link_hash_indirect)
10804
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10805
10806
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10807
  /* xgettext:c-format */
10808
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10809
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10810
  /* xgettext:c-format */
10811
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10812
0
      else
10813
  /* xgettext:c-format */
10814
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10815
0
      def_bfd = flinfo->output_bfd;
10816
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10817
0
  def_bfd = hi->root.u.def.section->owner;
10818
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10819
0
        h->root.root.string, def_bfd);
10820
0
      bfd_set_error (bfd_error_bad_value);
10821
0
      eoinfo->failed = true;
10822
0
      return false;
10823
0
    }
10824
10825
  /* We don't want to output symbols that have never been mentioned by
10826
     a regular file, or that we have been told to strip.  However, if
10827
     h->indx is set to -2, the symbol is used by a reloc and we must
10828
     output it.  */
10829
0
  strip = false;
10830
0
  if (h->indx == -2)
10831
0
    ;
10832
0
  else if ((h->def_dynamic
10833
0
      || h->ref_dynamic
10834
0
      || h->root.type == bfd_link_hash_new)
10835
0
     && !h->def_regular
10836
0
     && !h->ref_regular)
10837
0
    strip = true;
10838
0
  else if (flinfo->info->strip == strip_all)
10839
0
    strip = true;
10840
0
  else if (flinfo->info->strip == strip_some
10841
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10842
0
             h->root.root.string, false, false) == NULL)
10843
0
    strip = true;
10844
0
  else if ((h->root.type == bfd_link_hash_defined
10845
0
      || h->root.type == bfd_link_hash_defweak)
10846
0
     && ((flinfo->info->strip_discarded
10847
0
    && discarded_section (h->root.u.def.section))
10848
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10849
0
       && h->root.u.def.section->owner != NULL
10850
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10851
0
    strip = true;
10852
0
  else if ((h->root.type == bfd_link_hash_undefined
10853
0
      || h->root.type == bfd_link_hash_undefweak)
10854
0
     && h->root.u.undef.abfd != NULL
10855
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10856
0
    strip = true;
10857
10858
  /* Remember if this symbol should be stripped.  */
10859
0
  bool should_strip = strip;
10860
10861
  /* Strip undefined weak symbols link if they don't have relocation.  */
10862
0
  if (!strip)
10863
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10864
10865
0
  type = h->type;
10866
10867
  /* If we're stripping it, and it's not a dynamic symbol, there's
10868
     nothing else to do.   However, if it is a forced local symbol or
10869
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10870
     function a chance to make it dynamic.  */
10871
0
  if (strip
10872
0
      && h->dynindx == -1
10873
0
      && type != STT_GNU_IFUNC
10874
0
      && !h->forced_local)
10875
0
    return true;
10876
10877
0
  sym.st_value = 0;
10878
0
  sym.st_size = h->size;
10879
0
  sym.st_other = h->other;
10880
0
  switch (h->root.type)
10881
0
    {
10882
0
    default:
10883
0
    case bfd_link_hash_new:
10884
0
    case bfd_link_hash_warning:
10885
0
      abort ();
10886
0
      return false;
10887
10888
0
    case bfd_link_hash_undefined:
10889
0
    case bfd_link_hash_undefweak:
10890
0
      input_sec = bfd_und_section_ptr;
10891
0
      sym.st_shndx = SHN_UNDEF;
10892
0
      break;
10893
10894
0
    case bfd_link_hash_defined:
10895
0
    case bfd_link_hash_defweak:
10896
0
      {
10897
0
  input_sec = h->root.u.def.section;
10898
0
  if (input_sec->output_section != NULL)
10899
0
    {
10900
0
      sym.st_shndx =
10901
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10902
0
             input_sec->output_section);
10903
0
      if (sym.st_shndx == SHN_BAD)
10904
0
        {
10905
0
    _bfd_error_handler
10906
      /* xgettext:c-format */
10907
0
      (_("%pB: could not find output section %pA for input section %pA"),
10908
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10909
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10910
0
    eoinfo->failed = true;
10911
0
    return false;
10912
0
        }
10913
10914
      /* ELF symbols in relocatable files are section relative,
10915
         but in nonrelocatable files they are virtual
10916
         addresses.  */
10917
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10918
0
      if (!bfd_link_relocatable (flinfo->info))
10919
0
        {
10920
0
    sym.st_value += input_sec->output_section->vma;
10921
0
    if (h->type == STT_TLS)
10922
0
      {
10923
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10924
0
        if (tls_sec != NULL)
10925
0
          sym.st_value -= tls_sec->vma;
10926
0
      }
10927
0
        }
10928
0
    }
10929
0
  else
10930
0
    {
10931
0
      BFD_ASSERT (input_sec->owner == NULL
10932
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10933
0
      sym.st_shndx = SHN_UNDEF;
10934
0
      input_sec = bfd_und_section_ptr;
10935
0
    }
10936
0
      }
10937
0
      break;
10938
10939
0
    case bfd_link_hash_common:
10940
0
      input_sec = h->root.u.c.p->section;
10941
0
      sym.st_shndx = bed->common_section_index (input_sec);
10942
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10943
0
      break;
10944
10945
0
    case bfd_link_hash_indirect:
10946
      /* These symbols are created by symbol versioning.  They point
10947
   to the decorated version of the name.  For example, if the
10948
   symbol foo@@GNU_1.2 is the default, which should be used when
10949
   foo is used with no version, then we add an indirect symbol
10950
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
10951
   since the indirected symbol is already in the hash table.  */
10952
0
      return true;
10953
0
    }
10954
10955
0
  if (type == STT_COMMON || type == STT_OBJECT)
10956
0
    switch (h->root.type)
10957
0
      {
10958
0
      case bfd_link_hash_common:
10959
0
  type = elf_link_convert_common_type (flinfo->info, type);
10960
0
  break;
10961
0
      case bfd_link_hash_defined:
10962
0
      case bfd_link_hash_defweak:
10963
0
  if (bed->common_definition (&sym))
10964
0
    type = elf_link_convert_common_type (flinfo->info, type);
10965
0
  else
10966
0
    type = STT_OBJECT;
10967
0
  break;
10968
0
      case bfd_link_hash_undefined:
10969
0
      case bfd_link_hash_undefweak:
10970
0
  break;
10971
0
      default:
10972
0
  abort ();
10973
0
      }
10974
10975
0
  if (h->forced_local)
10976
0
    {
10977
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10978
      /* Turn off visibility on local symbol.  */
10979
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10980
0
    }
10981
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
10982
0
  else if (h->unique_global && h->def_regular)
10983
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10984
0
  else if (h->root.type == bfd_link_hash_undefweak
10985
0
     || h->root.type == bfd_link_hash_defweak)
10986
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10987
0
  else
10988
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10989
0
  sym.st_target_internal = h->target_internal;
10990
10991
  /* Give the processor backend a chance to tweak the symbol value,
10992
     and also to finish up anything that needs to be done for this
10993
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
10994
     forced local syms when non-shared is due to a historical quirk.
10995
     STT_GNU_IFUNC symbol must go through PLT.  */
10996
0
  if (((h->type == STT_GNU_IFUNC
10997
0
  && h->def_regular
10998
0
  && !bfd_link_relocatable (flinfo->info))
10999
0
       || ((h->dynindx != -1
11000
0
      || h->forced_local)
11001
0
     && ((bfd_link_pic (flinfo->info)
11002
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11003
0
        || h->root.type != bfd_link_hash_undefweak))
11004
0
         || !h->forced_local)
11005
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11006
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11007
0
    {
11008
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->info, h, &sym))
11009
0
  {
11010
0
    eoinfo->failed = true;
11011
0
    return false;
11012
0
  }
11013
      /* If a symbol is in the dynamic symbol table and isn't a
11014
   should-strip symbol, also keep it in the symbol table.  */
11015
0
      if (!should_strip)
11016
0
  strip = false;
11017
0
    }
11018
11019
  /* If we are marking the symbol as undefined, and there are no
11020
     non-weak references to this symbol from a regular object, then
11021
     mark the symbol as weak undefined; if there are non-weak
11022
     references, mark the symbol as strong.  We can't do this earlier,
11023
     because it might not be marked as undefined until the
11024
     finish_dynamic_symbol routine gets through with it.  */
11025
0
  if (sym.st_shndx == SHN_UNDEF
11026
0
      && h->ref_regular
11027
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11028
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11029
0
    {
11030
0
      int bindtype;
11031
0
      type = ELF_ST_TYPE (sym.st_info);
11032
11033
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11034
0
      if (type == STT_GNU_IFUNC)
11035
0
  type = STT_FUNC;
11036
11037
0
      if (h->ref_regular_nonweak)
11038
0
  bindtype = STB_GLOBAL;
11039
0
      else
11040
0
  bindtype = STB_WEAK;
11041
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11042
0
    }
11043
11044
  /* If this is a symbol defined in a dynamic library, don't use the
11045
     symbol size from the dynamic library.  Relinking an executable
11046
     against a new library may introduce gratuitous changes in the
11047
     executable's symbols if we keep the size.  */
11048
0
  if (sym.st_shndx == SHN_UNDEF
11049
0
      && !h->def_regular
11050
0
      && h->def_dynamic)
11051
0
    sym.st_size = 0;
11052
11053
  /* If a non-weak symbol with non-default visibility is not defined
11054
     locally, it is a fatal error.  */
11055
0
  if (!bfd_link_relocatable (flinfo->info)
11056
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11057
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11058
0
      && h->root.type == bfd_link_hash_undefined
11059
0
      && !h->def_regular)
11060
0
    {
11061
0
      const char *msg;
11062
11063
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11064
  /* xgettext:c-format */
11065
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11066
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11067
  /* xgettext:c-format */
11068
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11069
0
      else
11070
  /* xgettext:c-format */
11071
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11072
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11073
0
      bfd_set_error (bfd_error_bad_value);
11074
0
      eoinfo->failed = true;
11075
0
      return false;
11076
0
    }
11077
11078
  /* If this symbol should be put in the .dynsym section, then put it
11079
     there now.  We already know the symbol index.  We also fill in
11080
     the entry in the .hash section.  */
11081
0
  if (h->dynindx != -1
11082
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11083
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11084
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11085
0
    {
11086
0
      bfd_byte *esym;
11087
11088
      /* Since there is no version information in the dynamic string,
11089
   if there is no version info in symbol version section, we will
11090
   have a run-time problem if not linking executable, referenced
11091
   by shared library, or not bound locally.  */
11092
0
      if (h->verinfo.verdef == NULL
11093
0
    && (!bfd_link_executable (flinfo->info)
11094
0
        || h->ref_dynamic
11095
0
        || !h->def_regular))
11096
0
  {
11097
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11098
11099
0
    if (p && p [1] != '\0')
11100
0
      {
11101
0
        _bfd_error_handler
11102
    /* xgettext:c-format */
11103
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11104
0
     flinfo->output_bfd, h->root.root.string);
11105
0
        eoinfo->failed = true;
11106
0
        return false;
11107
0
      }
11108
0
  }
11109
11110
0
      sym.st_name = h->dynstr_index;
11111
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11112
0
        + h->dynindx * bed->s->sizeof_sym);
11113
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11114
0
  {
11115
0
    eoinfo->failed = true;
11116
0
    return false;
11117
0
  }
11118
11119
      /* Inform the linker of the addition of this symbol.  */
11120
11121
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11122
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11123
11124
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11125
11126
0
      if (flinfo->hash_sec != NULL)
11127
0
  {
11128
0
    size_t hash_entry_size;
11129
0
    bfd_byte *bucketpos;
11130
0
    bfd_vma chain;
11131
0
    size_t bucketcount;
11132
0
    size_t bucket;
11133
11134
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11135
0
    bucket = h->u.elf_hash_value % bucketcount;
11136
11137
0
    hash_entry_size
11138
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11139
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11140
0
           + (bucket + 2) * hash_entry_size);
11141
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11142
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11143
0
       bucketpos);
11144
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11145
0
       ((bfd_byte *) flinfo->hash_sec->contents
11146
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11147
0
  }
11148
11149
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11150
0
  {
11151
0
    Elf_Internal_Versym iversym;
11152
0
    Elf_External_Versym *eversym;
11153
11154
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11155
0
      {
11156
0
        if (h->verinfo.verdef == NULL
11157
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11158
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11159
0
    {
11160
0
      iversym.vs_vers = 1;
11161
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11162
        /* Referenced symbol without ELF_VER_CHR has no
11163
           version.  */
11164
0
        iversym.vs_vers = 0;
11165
0
    }
11166
0
        else
11167
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11168
0
      }
11169
0
    else
11170
0
      {
11171
0
        if (h->verinfo.vertree == NULL)
11172
0
    iversym.vs_vers = 1;
11173
0
        else
11174
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11175
0
        if (flinfo->info->create_default_symver)
11176
0
    iversym.vs_vers++;
11177
11178
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11179
     symbol is defined locally.  */
11180
0
        if (h->versioned == versioned_hidden && h->def_regular)
11181
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11182
0
      }
11183
11184
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11185
0
    eversym += h->dynindx;
11186
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11187
0
  }
11188
0
    }
11189
11190
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11191
     strip it from .symtab too.  Obviously we can't do this for
11192
     relocatable output or when needed for --emit-relocs.  */
11193
0
  else if (input_sec == bfd_und_section_ptr
11194
0
     && h->indx != -2
11195
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11196
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11197
0
     && !bfd_link_relocatable (flinfo->info))
11198
0
    return true;
11199
11200
  /* Also strip others that we couldn't earlier due to dynamic symbol
11201
     processing.  */
11202
0
  if (strip)
11203
0
    return true;
11204
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11205
0
    return true;
11206
11207
  /* Output a FILE symbol so that following locals are not associated
11208
     with the wrong input file.  We need one for forced local symbols
11209
     if we've seen more than one FILE symbol or when we have exactly
11210
     one FILE symbol but global symbols are present in a file other
11211
     than the one with the FILE symbol.  We also need one if linker
11212
     defined symbols are present.  In practice these conditions are
11213
     always met, so just emit the FILE symbol unconditionally.  */
11214
0
  if (eoinfo->localsyms
11215
0
      && !eoinfo->file_sym_done
11216
0
      && eoinfo->flinfo->filesym_count != 0)
11217
0
    {
11218
0
      Elf_Internal_Sym fsym;
11219
11220
0
      memset (&fsym, 0, sizeof (fsym));
11221
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11222
0
      fsym.st_shndx = SHN_ABS;
11223
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11224
0
              bfd_und_section_ptr, NULL))
11225
0
  return false;
11226
11227
0
      eoinfo->file_sym_done = true;
11228
0
    }
11229
11230
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11231
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11232
0
           input_sec, h);
11233
0
  if (ret == 0)
11234
0
    {
11235
0
      eoinfo->failed = true;
11236
0
      return false;
11237
0
    }
11238
0
  else if (ret == 1)
11239
0
    h->indx = indx;
11240
0
  else if (h->indx == -2)
11241
0
    abort();
11242
11243
0
  return true;
11244
0
}
11245
11246
/* Return TRUE if special handling is done for relocs in SEC against
11247
   symbols defined in discarded sections.  */
11248
11249
static bool
11250
elf_section_ignore_discarded_relocs (asection *sec)
11251
0
{
11252
0
  elf_backend_data *bed;
11253
11254
0
  switch (sec->sec_info_type)
11255
0
    {
11256
0
    case SEC_INFO_TYPE_STABS:
11257
0
    case SEC_INFO_TYPE_EH_FRAME:
11258
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11259
0
    case SEC_INFO_TYPE_SFRAME:
11260
0
      return true;
11261
0
    default:
11262
0
      break;
11263
0
    }
11264
11265
0
  bed = get_elf_backend_data (sec->owner);
11266
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11267
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11268
0
    return true;
11269
11270
0
  return false;
11271
0
}
11272
11273
/* Return a mask saying how ld should treat relocations in SEC against
11274
   symbols defined in discarded sections.  If this function returns
11275
   COMPLAIN set, ld will issue a warning message.  If this function
11276
   returns PRETEND set, and the discarded section was link-once and the
11277
   same size as the kept link-once section, ld will pretend that the
11278
   symbol was actually defined in the kept section.  Otherwise ld will
11279
   zero the reloc (at least that is the intent, but some cooperation by
11280
   the target dependent code is needed, particularly for REL targets).  */
11281
11282
unsigned int
11283
_bfd_elf_default_action_discarded (asection *sec)
11284
0
{
11285
0
  if (sec->flags & SEC_DEBUGGING)
11286
0
    return PRETEND;
11287
11288
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11289
0
    return 0;
11290
11291
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11292
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11293
0
    return 0;
11294
11295
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11296
0
    return 0;
11297
11298
0
  return COMPLAIN | PRETEND;
11299
0
}
11300
11301
/* Find a match between a section and a member of a section group.  */
11302
11303
static asection *
11304
match_group_member (asection *sec, asection *group,
11305
        struct bfd_link_info *info)
11306
0
{
11307
0
  asection *first = elf_next_in_group (group);
11308
0
  asection *s = first;
11309
11310
0
  while (s != NULL)
11311
0
    {
11312
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11313
0
  return s;
11314
11315
0
      s = elf_next_in_group (s);
11316
0
      if (s == first)
11317
0
  break;
11318
0
    }
11319
11320
0
  return NULL;
11321
0
}
11322
11323
/* Check if the kept section of a discarded section SEC can be used
11324
   to replace it.  Return the replacement if it is OK.  Otherwise return
11325
   NULL.  */
11326
11327
asection *
11328
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11329
0
{
11330
0
  asection *kept;
11331
11332
0
  kept = sec->kept_section;
11333
0
  if (kept != NULL)
11334
0
    {
11335
0
      if ((kept->flags & SEC_GROUP) != 0)
11336
0
  kept = match_group_member (sec, kept, info);
11337
0
      if (kept != NULL)
11338
0
  {
11339
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11340
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11341
0
      kept = NULL;
11342
0
    else
11343
0
      {
11344
        /* Get the real kept section.  */
11345
0
        asection *next;
11346
0
        for (next = kept->kept_section;
11347
0
       next != NULL;
11348
0
       next = next->kept_section)
11349
0
    kept = next;
11350
0
      }
11351
0
  }
11352
0
      sec->kept_section = kept;
11353
0
    }
11354
0
  return kept;
11355
0
}
11356
11357
/* Link an input file into the linker output file.  This function
11358
   handles all the sections and relocations of the input file at once.
11359
   This is so that we only have to read the local symbols once, and
11360
   don't have to keep them in memory.  */
11361
11362
static bool
11363
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11364
0
{
11365
0
  int (*relocate_section)
11366
0
    (struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11367
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11368
0
  bfd *output_bfd;
11369
0
  Elf_Internal_Shdr *symtab_hdr;
11370
0
  size_t locsymcount;
11371
0
  size_t extsymoff;
11372
0
  size_t num_sym;
11373
0
  Elf_Internal_Sym *isymbuf;
11374
0
  Elf_Internal_Sym *isym;
11375
0
  Elf_Internal_Sym *isymend;
11376
0
  long *pindex;
11377
0
  asection **ppsection;
11378
0
  asection *o;
11379
0
  elf_backend_data *bed;
11380
0
  struct elf_link_hash_entry **sym_hashes;
11381
0
  bfd_size_type address_size;
11382
0
  bfd_vma r_type_mask;
11383
0
  int r_sym_shift;
11384
0
  bool have_file_sym = false;
11385
11386
0
  output_bfd = flinfo->output_bfd;
11387
0
  bed = get_elf_backend_data (output_bfd);
11388
0
  relocate_section = bed->elf_backend_relocate_section;
11389
11390
  /* If this is a dynamic object, we don't want to do anything here:
11391
     we don't want the local symbols, and we don't want the section
11392
     contents.  */
11393
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11394
0
    return true;
11395
11396
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11397
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11398
0
  if (elf_bad_symtab (input_bfd))
11399
0
    {
11400
0
      locsymcount = num_sym;
11401
0
      extsymoff = 0;
11402
0
    }
11403
0
  else
11404
0
    {
11405
0
      locsymcount = symtab_hdr->sh_info;
11406
0
      extsymoff = symtab_hdr->sh_info;
11407
0
    }
11408
11409
  /* Enable GNU OSABI features in the output BFD that are used in the input
11410
     BFD.  */
11411
0
  if (bed->elf_osabi == ELFOSABI_NONE
11412
0
      || bed->elf_osabi == ELFOSABI_GNU
11413
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11414
0
    elf_tdata (output_bfd)->has_gnu_osabi
11415
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11416
0
    & (bfd_link_relocatable (flinfo->info)
11417
0
       ? -1 : ~elf_gnu_osabi_retain));
11418
11419
  /* Read the local symbols.  */
11420
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11421
0
  if (isymbuf == NULL && locsymcount != 0)
11422
0
    {
11423
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11424
0
              flinfo->internal_syms,
11425
0
              flinfo->external_syms,
11426
0
              flinfo->locsym_shndx);
11427
0
      if (isymbuf == NULL)
11428
0
  return false;
11429
0
    }
11430
11431
  /* Find local symbol sections and adjust values of symbols in
11432
     SEC_MERGE sections.  Write out those local symbols we know are
11433
     going into the output file.  */
11434
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11435
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11436
0
       isym < isymend;
11437
0
       isym++, pindex++, ppsection++)
11438
0
    {
11439
0
      asection *isec;
11440
0
      const char *name;
11441
0
      Elf_Internal_Sym osym;
11442
0
      long indx;
11443
0
      int ret;
11444
11445
0
      *pindex = -1;
11446
11447
0
      if (elf_bad_symtab (input_bfd))
11448
0
  {
11449
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11450
0
      {
11451
0
        *ppsection = NULL;
11452
0
        continue;
11453
0
      }
11454
0
  }
11455
11456
0
      if (isym->st_shndx == SHN_UNDEF)
11457
0
  isec = bfd_und_section_ptr;
11458
0
      else if (isym->st_shndx == SHN_ABS)
11459
0
  isec = bfd_abs_section_ptr;
11460
0
      else if (isym->st_shndx == SHN_COMMON)
11461
0
  isec = bfd_com_section_ptr;
11462
0
      else
11463
0
  {
11464
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11465
0
    if (isec == NULL)
11466
0
      {
11467
        /* Don't attempt to output symbols with st_shnx in the
11468
     reserved range other than SHN_ABS and SHN_COMMON.  */
11469
0
        isec = bfd_und_section_ptr;
11470
0
      }
11471
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11472
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11473
0
      isym->st_value =
11474
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11475
0
  }
11476
11477
0
      *ppsection = isec;
11478
11479
      /* Don't output the first, undefined, symbol.  In fact, don't
11480
   output any undefined local symbol.  */
11481
0
      if (isec == bfd_und_section_ptr)
11482
0
  continue;
11483
11484
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11485
0
  {
11486
    /* We never output section symbols.  Instead, we use the
11487
       section symbol of the corresponding section in the output
11488
       file.  */
11489
0
    continue;
11490
0
  }
11491
11492
      /* If we are stripping all symbols, we don't want to output this
11493
   one.  */
11494
0
      if (flinfo->info->strip == strip_all)
11495
0
  continue;
11496
11497
      /* If we are discarding all local symbols, we don't want to
11498
   output this one.  If we are generating a relocatable output
11499
   file, then some of the local symbols may be required by
11500
   relocs; we output them below as we discover that they are
11501
   needed.  */
11502
0
      if (flinfo->info->discard == discard_all)
11503
0
  continue;
11504
11505
      /* If this symbol is defined in a section which we are
11506
   discarding, we don't need to keep it.  */
11507
0
      if (isym->st_shndx < SHN_LORESERVE
11508
0
    && (isec->output_section == NULL
11509
0
        || bfd_section_removed_from_list (output_bfd,
11510
0
            isec->output_section)))
11511
0
  continue;
11512
11513
      /* Get the name of the symbol.  */
11514
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11515
0
                isym->st_name);
11516
0
      if (name == NULL)
11517
0
  return false;
11518
11519
      /* See if we are discarding symbols with this name.  */
11520
0
      if ((flinfo->info->strip == strip_some
11521
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11522
0
         == NULL))
11523
0
    || (((flinfo->info->discard == discard_sec_merge
11524
0
    && (isec->flags & SEC_MERGE)
11525
0
    && !bfd_link_relocatable (flinfo->info))
11526
0
         || flinfo->info->discard == discard_l)
11527
0
        && bfd_is_local_label_name (input_bfd, name)))
11528
0
  continue;
11529
11530
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11531
0
  {
11532
0
    if (input_bfd->lto_output)
11533
      /* -flto puts a temp file name here.  This means builds
11534
         are not reproducible.  Discard the symbol.  */
11535
0
      continue;
11536
0
    have_file_sym = true;
11537
0
    flinfo->filesym_count += 1;
11538
0
  }
11539
0
      if (!have_file_sym)
11540
0
  {
11541
    /* In the absence of debug info, bfd_find_nearest_line uses
11542
       FILE symbols to determine the source file for local
11543
       function symbols.  Provide a FILE symbol here if input
11544
       files lack such, so that their symbols won't be
11545
       associated with a previous input file.  It's not the
11546
       source file, but the best we can do.  */
11547
0
    const char *filename;
11548
0
    have_file_sym = true;
11549
0
    flinfo->filesym_count += 1;
11550
0
    memset (&osym, 0, sizeof (osym));
11551
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11552
0
    osym.st_shndx = SHN_ABS;
11553
0
    if (input_bfd->lto_output)
11554
0
      filename = NULL;
11555
0
    else
11556
0
      filename = lbasename (bfd_get_filename (input_bfd));
11557
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11558
0
            bfd_abs_section_ptr, NULL))
11559
0
      return false;
11560
0
  }
11561
11562
0
      osym = *isym;
11563
11564
      /* Adjust the section index for the output file.  */
11565
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11566
0
               isec->output_section);
11567
0
      if (osym.st_shndx == SHN_BAD)
11568
0
  return false;
11569
11570
      /* ELF symbols in relocatable files are section relative, but
11571
   in executable files they are virtual addresses.  Note that
11572
   this code assumes that all ELF sections have an associated
11573
   BFD section with a reasonable value for output_offset; below
11574
   we assume that they also have a reasonable value for
11575
   output_section.  Any special sections must be set up to meet
11576
   these requirements.  */
11577
0
      osym.st_value += isec->output_offset;
11578
0
      if (!bfd_link_relocatable (flinfo->info))
11579
0
  {
11580
0
    osym.st_value += isec->output_section->vma;
11581
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11582
0
      {
11583
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11584
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11585
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11586
0
        else
11587
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11588
0
              STT_NOTYPE);
11589
0
      }
11590
0
  }
11591
11592
0
      indx = bfd_get_symcount (output_bfd);
11593
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11594
0
      if (ret == 0)
11595
0
  return false;
11596
0
      else if (ret == 1)
11597
0
  *pindex = indx;
11598
0
    }
11599
11600
0
  if (bed->s->arch_size == 32)
11601
0
    {
11602
0
      r_type_mask = 0xff;
11603
0
      r_sym_shift = 8;
11604
0
      address_size = 4;
11605
0
    }
11606
0
  else
11607
0
    {
11608
0
      r_type_mask = 0xffffffff;
11609
0
      r_sym_shift = 32;
11610
0
      address_size = 8;
11611
0
    }
11612
11613
  /* Relocate the contents of each section.  */
11614
0
  sym_hashes = elf_sym_hashes (input_bfd);
11615
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11616
0
    {
11617
0
      bfd_byte *contents;
11618
11619
0
      if (! o->linker_mark)
11620
0
  {
11621
    /* This section was omitted from the link.  */
11622
0
    continue;
11623
0
  }
11624
11625
0
      if (!flinfo->info->resolve_section_groups
11626
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11627
0
  {
11628
    /* Deal with the group signature symbol.  */
11629
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11630
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11631
0
    asection *osec = o->output_section;
11632
11633
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11634
0
    if (symndx >= locsymcount
11635
0
        || (elf_bad_symtab (input_bfd)
11636
0
      && flinfo->sections[symndx] == NULL))
11637
0
      {
11638
0
        struct elf_link_hash_entry *h;
11639
11640
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11641
0
            extsymoff, num_sym);
11642
0
        if (h == NULL)
11643
0
    {
11644
0
      _bfd_error_handler
11645
        /* xgettext:c-format */
11646
0
        (_("error: %pB: unable to create group section symbol"),
11647
0
         input_bfd);
11648
0
      bfd_set_error (bfd_error_bad_value);
11649
0
      return false;
11650
0
    }
11651
11652
        /* Arrange for symbol to be output.  */
11653
0
        h->indx = -2;
11654
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11655
0
      }
11656
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11657
0
      {
11658
        /* We'll use the output section target_index.  */
11659
0
        asection *sec = flinfo->sections[symndx]->output_section;
11660
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11661
0
      }
11662
0
    else
11663
0
      {
11664
0
        if (flinfo->indices[symndx] == -1)
11665
0
    {
11666
      /* Otherwise output the local symbol now.  */
11667
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11668
0
      asection *sec = flinfo->sections[symndx]->output_section;
11669
0
      const char *name;
11670
0
      long indx;
11671
0
      int ret;
11672
11673
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11674
0
                symtab_hdr->sh_link,
11675
0
                sym.st_name);
11676
0
      if (name == NULL)
11677
0
        return false;
11678
11679
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11680
0
                    sec);
11681
0
      if (sym.st_shndx == SHN_BAD)
11682
0
        return false;
11683
11684
0
      sym.st_value += o->output_offset;
11685
11686
0
      indx = bfd_get_symcount (output_bfd);
11687
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11688
0
               NULL);
11689
0
      if (ret == 0)
11690
0
        return false;
11691
0
      else if (ret == 1)
11692
0
        flinfo->indices[symndx] = indx;
11693
0
      else
11694
0
        abort ();
11695
0
    }
11696
0
        elf_section_data (osec)->this_hdr.sh_info
11697
0
    = flinfo->indices[symndx];
11698
0
      }
11699
0
  }
11700
11701
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11702
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11703
0
  continue;
11704
11705
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11706
0
  {
11707
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11708
       or somesuch.  */
11709
0
    continue;
11710
0
  }
11711
11712
      /* Get the contents of the section.  They have been cached by a
11713
   relaxation routine.  Note that o is a section in an input
11714
   file, so the contents field will not have been set by any of
11715
   the routines which work on output files.  */
11716
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11717
0
  {
11718
0
    contents = elf_section_data (o)->this_hdr.contents;
11719
0
    if (bed->caches_rawsize
11720
0
        && o->rawsize != 0
11721
0
        && o->rawsize < o->size)
11722
0
      {
11723
0
        memcpy (flinfo->contents, contents, o->rawsize);
11724
0
        contents = flinfo->contents;
11725
0
      }
11726
0
  }
11727
0
      else if (!(o->flags & SEC_RELOC)
11728
0
         && !bed->elf_backend_write_section
11729
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11730
  /* A MERGE section that has no relocations doesn't need the
11731
     contents anymore, they have been recorded earlier.  Except
11732
     if the backend has special provisions for writing sections.  */
11733
0
  contents = NULL;
11734
0
      else
11735
0
  {
11736
0
    contents = flinfo->contents;
11737
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11738
0
                 &contents))
11739
0
      return false;
11740
0
  }
11741
11742
0
      if ((o->flags & SEC_RELOC) != 0)
11743
0
  {
11744
0
    Elf_Internal_Rela *internal_relocs;
11745
0
    Elf_Internal_Rela *rel, *relend;
11746
0
    int action_discarded;
11747
0
    int ret;
11748
11749
    /* Get the swapped relocs.  */
11750
0
    internal_relocs
11751
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11752
0
                flinfo->external_relocs,
11753
0
                flinfo->internal_relocs,
11754
0
                false);
11755
0
    if (internal_relocs == NULL
11756
0
        && o->reloc_count > 0)
11757
0
      return false;
11758
11759
0
    action_discarded = -1;
11760
0
    if (!elf_section_ignore_discarded_relocs (o))
11761
0
      action_discarded = (*bed->action_discarded) (o);
11762
11763
    /* Run through the relocs evaluating complex reloc symbols and
11764
       looking for relocs against symbols from discarded sections
11765
       or section symbols from removed link-once sections.
11766
       Complain about relocs against discarded sections.  Zero
11767
       relocs against removed link-once sections.  */
11768
11769
0
    rel = internal_relocs;
11770
0
    relend = rel + o->reloc_count;
11771
0
    for ( ; rel < relend; rel++)
11772
0
      {
11773
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11774
0
        unsigned int s_type;
11775
0
        asection **ps, *sec;
11776
0
        struct elf_link_hash_entry *h = NULL;
11777
0
        const char *sym_name;
11778
11779
0
        if (r_symndx == STN_UNDEF)
11780
0
    continue;
11781
11782
0
        if (r_symndx >= locsymcount
11783
0
      || (elf_bad_symtab (input_bfd)
11784
0
          && flinfo->sections[r_symndx] == NULL))
11785
0
    {
11786
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11787
0
                extsymoff, num_sym);
11788
11789
      /* Badly formatted input files can contain relocs that
11790
         reference non-existant symbols.  Check here so that
11791
         we do not seg fault.  */
11792
0
      if (h == NULL)
11793
0
        {
11794
0
          _bfd_error_handler
11795
      /* xgettext:c-format */
11796
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11797
0
         "that references a non-existent global symbol"),
11798
0
       input_bfd, (uint64_t) rel->r_info, o);
11799
0
          bfd_set_error (bfd_error_bad_value);
11800
0
          return false;
11801
0
        }
11802
11803
0
      s_type = h->type;
11804
11805
      /* If a plugin symbol is referenced from a non-IR file,
11806
         mark the symbol as undefined.  Note that the
11807
         linker may attach linker created dynamic sections
11808
         to the plugin bfd.  Symbols defined in linker
11809
         created sections are not plugin symbols.  */
11810
0
      if ((h->root.non_ir_ref_regular
11811
0
           || h->root.non_ir_ref_dynamic)
11812
0
          && (h->root.type == bfd_link_hash_defined
11813
0
        || h->root.type == bfd_link_hash_defweak)
11814
0
          && (h->root.u.def.section->flags
11815
0
        & SEC_LINKER_CREATED) == 0
11816
0
          && h->root.u.def.section->owner != NULL
11817
0
          && (h->root.u.def.section->owner->flags
11818
0
        & BFD_PLUGIN) != 0)
11819
0
        {
11820
0
          h->root.type = bfd_link_hash_undefined;
11821
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11822
0
        }
11823
11824
0
      ps = NULL;
11825
0
      if (h->root.type == bfd_link_hash_defined
11826
0
          || h->root.type == bfd_link_hash_defweak)
11827
0
        ps = &h->root.u.def.section;
11828
11829
0
      sym_name = h->root.root.string;
11830
0
    }
11831
0
        else
11832
0
    {
11833
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11834
11835
0
      s_type = ELF_ST_TYPE (sym->st_info);
11836
0
      ps = &flinfo->sections[r_symndx];
11837
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11838
0
                 sym, *ps);
11839
0
    }
11840
11841
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11842
0
      && !bfd_link_relocatable (flinfo->info))
11843
0
    {
11844
0
      bfd_vma val;
11845
0
      bfd_vma dot = (rel->r_offset
11846
0
         + o->output_offset + o->output_section->vma);
11847
#ifdef DEBUG
11848
      printf ("Encountered a complex symbol!");
11849
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11850
        bfd_get_filename (input_bfd), o->name,
11851
        (long) (rel - internal_relocs));
11852
      printf (" symbol: idx  %8.8lx, name %s\n",
11853
        r_symndx, sym_name);
11854
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11855
        (unsigned long) rel->r_info,
11856
        (unsigned long) rel->r_offset);
11857
#endif
11858
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11859
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11860
0
        return false;
11861
11862
      /* Symbol evaluated OK.  Update to absolute value.  */
11863
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11864
0
           num_sym, r_symndx, val))
11865
0
        return false;
11866
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 (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 = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12029
0
               extsymoff, num_sym);
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 = flinfo->info->callbacks->nearby_section
12088
0
            (output_bfd, osec, 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, o, contents)))
12210
0
      return false;
12211
0
    break;
12212
0
  case SEC_INFO_TYPE_MERGE:
12213
0
    if (! _bfd_write_merged_section (output_bfd, o))
12214
0
      return false;
12215
0
    break;
12216
0
  case SEC_INFO_TYPE_EH_FRAME:
12217
0
    {
12218
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12219
0
               o, contents))
12220
0
        return false;
12221
0
    }
12222
0
    break;
12223
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12224
0
    {
12225
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12226
0
               flinfo->info,
12227
0
               o, contents))
12228
0
        return false;
12229
0
    }
12230
0
    break;
12231
0
  case SEC_INFO_TYPE_SFRAME:
12232
0
      {
12233
        /* Merge SFrame section into the SFrame encoder context of the
12234
     output_bfd's section.  The final .sframe output section will
12235
     be written out later.  */
12236
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12237
0
              o, contents))
12238
0
    return false;
12239
0
      }
12240
0
      break;
12241
0
  default:
12242
0
    {
12243
0
      if (! (o->flags & SEC_EXCLUDE))
12244
0
        {
12245
0
    file_ptr offset = (file_ptr) o->output_offset;
12246
0
    bfd_size_type todo = o->size;
12247
12248
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12249
12250
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12251
0
        && o->size > address_size)
12252
0
      {
12253
        /* Reverse-copy input section to output.  */
12254
12255
0
        if ((o->size & (address_size - 1)) != 0
12256
0
      || (o->reloc_count != 0
12257
0
          && (o->size * bed->s->int_rels_per_ext_rel
12258
0
        != o->reloc_count * address_size)))
12259
0
          {
12260
0
      _bfd_error_handler
12261
        /* xgettext:c-format */
12262
0
        (_("error: %pB: size of section %pA is not "
12263
0
           "multiple of address size"),
12264
0
         input_bfd, o);
12265
0
      bfd_set_error (bfd_error_bad_value);
12266
0
      return false;
12267
0
          }
12268
12269
0
        do
12270
0
          {
12271
0
      todo -= address_size;
12272
0
      if (! bfd_set_section_contents (output_bfd,
12273
0
              o->output_section,
12274
0
              contents + todo,
12275
0
              offset,
12276
0
              address_size))
12277
0
        return false;
12278
0
      if (todo == 0)
12279
0
        break;
12280
0
      offset += address_size;
12281
0
          }
12282
0
        while (1);
12283
0
      }
12284
0
    else if (! bfd_set_section_contents (output_bfd,
12285
0
                 o->output_section,
12286
0
                 contents,
12287
0
                 offset, todo))
12288
0
      return false;
12289
0
        }
12290
0
    }
12291
0
    break;
12292
0
  }
12293
12294
      /* Munmap the section contents for each input section.  */
12295
0
      _bfd_elf_link_munmap_section_contents (o);
12296
0
    }
12297
12298
0
  return true;
12299
0
}
12300
12301
/* Generate a reloc when linking an ELF file.  This is a reloc
12302
   requested by the linker, and does not come from any input file.  This
12303
   is used to build constructor and destructor tables when linking
12304
   with -Ur.  */
12305
12306
static bool
12307
elf_reloc_link_order (bfd *output_bfd,
12308
          struct bfd_link_info *info,
12309
          asection *output_section,
12310
          struct bfd_link_order *link_order)
12311
0
{
12312
0
  reloc_howto_type *howto;
12313
0
  long indx;
12314
0
  bfd_vma offset;
12315
0
  bfd_vma addend;
12316
0
  struct bfd_elf_section_reloc_data *reldata;
12317
0
  struct elf_link_hash_entry **rel_hash_ptr;
12318
0
  Elf_Internal_Shdr *rel_hdr;
12319
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12320
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12321
0
  bfd_byte *erel;
12322
0
  unsigned int i;
12323
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12324
12325
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12326
0
  if (howto == NULL)
12327
0
    {
12328
0
      bfd_set_error (bfd_error_bad_value);
12329
0
      return false;
12330
0
    }
12331
12332
0
  addend = link_order->u.reloc.p->addend;
12333
12334
0
  if (esdo->rel.hdr)
12335
0
    reldata = &esdo->rel;
12336
0
  else if (esdo->rela.hdr)
12337
0
    reldata = &esdo->rela;
12338
0
  else
12339
0
    {
12340
0
      reldata = NULL;
12341
0
      BFD_ASSERT (0);
12342
0
    }
12343
12344
  /* Figure out the symbol index.  */
12345
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12346
0
  if (link_order->type == bfd_section_reloc_link_order)
12347
0
    {
12348
0
      indx = link_order->u.reloc.p->u.section->target_index;
12349
0
      BFD_ASSERT (indx != 0);
12350
0
      *rel_hash_ptr = NULL;
12351
0
    }
12352
0
  else
12353
0
    {
12354
0
      struct elf_link_hash_entry *h;
12355
12356
      /* Treat a reloc against a defined symbol as though it were
12357
   actually against the section.  */
12358
0
      h = ((struct elf_link_hash_entry *)
12359
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12360
0
           link_order->u.reloc.p->u.name,
12361
0
           false, false, true));
12362
0
      if (h != NULL
12363
0
    && (h->root.type == bfd_link_hash_defined
12364
0
        || h->root.type == bfd_link_hash_defweak))
12365
0
  {
12366
0
    asection *section;
12367
12368
0
    section = h->root.u.def.section;
12369
0
    indx = section->output_section->target_index;
12370
0
    *rel_hash_ptr = NULL;
12371
    /* It seems that we ought to add the symbol value to the
12372
       addend here, but in practice it has already been added
12373
       because it was passed to constructor_callback.  */
12374
0
    addend += section->output_section->vma + section->output_offset;
12375
0
  }
12376
0
      else if (h != NULL)
12377
0
  {
12378
    /* Setting the index to -2 tells elf_link_output_extsym that
12379
       this symbol is used by a reloc.  */
12380
0
    h->indx = -2;
12381
0
    *rel_hash_ptr = h;
12382
0
    indx = 0;
12383
0
  }
12384
0
      else
12385
0
  {
12386
0
    (*info->callbacks->unattached_reloc)
12387
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12388
0
    indx = 0;
12389
0
  }
12390
0
    }
12391
12392
  /* If this is an inplace reloc, we must write the addend into the
12393
     object file.  */
12394
0
  if (howto->partial_inplace && addend != 0)
12395
0
    {
12396
0
      bfd_size_type size;
12397
0
      bfd_reloc_status_type rstat;
12398
0
      bfd_byte *buf;
12399
0
      bool ok;
12400
0
      const char *sym_name;
12401
0
      bfd_size_type octets;
12402
12403
0
      size = bfd_get_reloc_size (howto);
12404
0
      buf = bfd_zmalloc (size);
12405
0
      if (buf == NULL && size != 0)
12406
0
  return false;
12407
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12408
0
      switch (rstat)
12409
0
  {
12410
0
  case bfd_reloc_ok:
12411
0
    break;
12412
12413
0
  default:
12414
0
  case bfd_reloc_outofrange:
12415
0
    abort ();
12416
12417
0
  case bfd_reloc_overflow:
12418
0
    if (link_order->type == bfd_section_reloc_link_order)
12419
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12420
0
    else
12421
0
      sym_name = link_order->u.reloc.p->u.name;
12422
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12423
0
                howto->name, addend, NULL, NULL,
12424
0
                (bfd_vma) 0);
12425
0
    break;
12426
0
  }
12427
12428
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12429
0
               output_section);
12430
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12431
0
             octets, size);
12432
0
      free (buf);
12433
0
      if (! ok)
12434
0
  return false;
12435
0
    }
12436
12437
  /* The address of a reloc is relative to the section in a
12438
     relocatable file, and is a virtual address in an executable
12439
     file.  */
12440
0
  offset = link_order->offset;
12441
0
  if (! bfd_link_relocatable (info))
12442
0
    offset += output_section->vma;
12443
12444
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12445
0
    {
12446
0
      irel[i].r_offset = offset;
12447
0
      irel[i].r_info = 0;
12448
0
      irel[i].r_addend = 0;
12449
0
    }
12450
0
  if (bed->s->arch_size == 32)
12451
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12452
0
  else
12453
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12454
12455
0
  rel_hdr = reldata->hdr;
12456
0
  erel = rel_hdr->contents;
12457
0
  if (rel_hdr->sh_type == SHT_REL)
12458
0
    {
12459
0
      erel += reldata->count * bed->s->sizeof_rel;
12460
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12461
0
    }
12462
0
  else
12463
0
    {
12464
0
      irel[0].r_addend = addend;
12465
0
      erel += reldata->count * bed->s->sizeof_rela;
12466
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12467
0
    }
12468
12469
0
  ++reldata->count;
12470
12471
0
  return true;
12472
0
}
12473
12474
/* Generate an import library in INFO->implib_bfd from symbols in OBFD.
12475
   Returns TRUE upon success, FALSE otherwise.  */
12476
12477
static bool
12478
elf_output_implib (bfd *obfd, struct bfd_link_info *info)
12479
0
{
12480
0
  bool ret = false;
12481
0
  bfd *implib_bfd;
12482
0
  elf_backend_data *obed;
12483
0
  flagword flags;
12484
0
  enum bfd_architecture arch;
12485
0
  unsigned int mach;
12486
0
  asymbol **sympp = NULL;
12487
0
  long symsize;
12488
0
  long symcount;
12489
0
  long src_count;
12490
0
  elf_symbol_type *osymbuf;
12491
12492
0
  implib_bfd = info->out_implib_bfd;
12493
0
  obed = get_elf_backend_data (obfd);
12494
12495
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12496
0
    return false;
12497
12498
  /* Use flag from executable but make it a relocatable object.  */
12499
0
  flags = bfd_get_file_flags (obfd);
12500
0
  flags &= ~HAS_RELOC;
12501
0
  if (!bfd_set_start_address (implib_bfd, 0)
12502
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12503
0
    return false;
12504
12505
  /* Copy architecture of output file to import library file.  */
12506
0
  arch = bfd_get_arch (obfd);
12507
0
  mach = bfd_get_mach (obfd);
12508
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12509
0
      && (obfd->target_defaulted
12510
0
    || bfd_get_arch (obfd) != bfd_get_arch (implib_bfd)))
12511
0
    return false;
12512
12513
  /* Get symbol table size.  */
12514
0
  symsize = bfd_get_symtab_upper_bound (obfd);
12515
0
  if (symsize < 0)
12516
0
    return false;
12517
12518
  /* Read in the symbol table.  */
12519
0
  sympp = bfd_malloc (symsize);
12520
0
  if (sympp == NULL)
12521
0
    return false;
12522
12523
0
  symcount = bfd_canonicalize_symtab (obfd, sympp);
12524
0
  if (symcount < 0)
12525
0
    goto free_sym_buf;
12526
12527
  /* Allow the BFD backend to copy any private header data it
12528
     understands from the output BFD to the import library BFD.  */
12529
0
  if (! bfd_copy_private_header_data (obfd, implib_bfd))
12530
0
    goto free_sym_buf;
12531
12532
  /* Filter symbols to appear in the import library.  */
12533
0
  symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
12534
0
  if (symcount == 0)
12535
0
    {
12536
0
      bfd_set_error (bfd_error_no_symbols);
12537
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12538
0
        implib_bfd);
12539
0
      goto free_sym_buf;
12540
0
    }
12541
12542
  /* Make symbols absolute.  */
12543
0
  osymbuf = bfd_alloc (implib_bfd, symcount * sizeof (*osymbuf));
12544
0
  if (osymbuf == NULL)
12545
0
    goto free_sym_buf;
12546
12547
0
  for (src_count = 0; src_count < symcount; src_count++)
12548
0
    {
12549
0
      memcpy (&osymbuf[src_count], sympp[src_count], sizeof (*osymbuf));
12550
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12551
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12552
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12553
0
      osymbuf[src_count].internal_elf_sym.st_value =
12554
0
  osymbuf[src_count].symbol.value;
12555
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12556
0
    }
12557
12558
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12559
12560
  /* Allow the BFD backend to copy any private data it understands
12561
     from the output BFD to the import library BFD.  This is done last
12562
     to permit the routine to look at the filtered symbol table.  */
12563
0
  if (! bfd_copy_private_bfd_data (obfd, implib_bfd))
12564
0
    goto free_sym_buf;
12565
12566
0
  if (!bfd_close (implib_bfd))
12567
0
    goto free_sym_buf;
12568
12569
0
  ret = true;
12570
12571
0
 free_sym_buf:
12572
0
  free (sympp);
12573
0
  return ret;
12574
0
}
12575
12576
static void
12577
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12578
0
{
12579
0
  asection *o;
12580
12581
0
  if (flinfo->symstrtab != NULL)
12582
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12583
0
  free (flinfo->contents);
12584
0
  free (flinfo->external_relocs);
12585
0
  free (flinfo->internal_relocs);
12586
0
  free (flinfo->external_syms);
12587
0
  free (flinfo->locsym_shndx);
12588
0
  free (flinfo->internal_syms);
12589
0
  free (flinfo->indices);
12590
0
  free (flinfo->sections);
12591
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12592
0
    free (flinfo->symshndxbuf);
12593
0
  for (o = obfd->sections; o != NULL; o = o->next)
12594
0
    {
12595
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12596
0
      free (esdo->rel.hashes);
12597
0
      free (esdo->rela.hashes);
12598
0
    }
12599
0
}
12600
12601
/* Do the final step of an ELF link.  */
12602
12603
bool
12604
_bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
12605
0
{
12606
0
  bool dynamic;
12607
0
  bool emit_relocs;
12608
0
  bfd *dynobj;
12609
0
  struct elf_final_link_info flinfo;
12610
0
  asection *o;
12611
0
  struct bfd_link_order *p;
12612
0
  bfd *sub;
12613
0
  bfd_size_type max_contents_size;
12614
0
  bfd_size_type max_external_reloc_size;
12615
0
  bfd_size_type max_internal_reloc_count;
12616
0
  bfd_size_type max_sym_count;
12617
0
  bfd_size_type max_sym_shndx_count;
12618
0
  Elf_Internal_Sym elfsym;
12619
0
  unsigned int i;
12620
0
  Elf_Internal_Shdr *symtab_hdr;
12621
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12622
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
12623
0
  struct elf_outext_info eoinfo;
12624
0
  bool merged;
12625
0
  size_t relativecount;
12626
0
  size_t relr_entsize;
12627
0
  asection *reldyn = 0;
12628
0
  bfd_size_type amt;
12629
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12630
0
  bool sections_removed;
12631
12632
0
  if (!is_elf_hash_table (&htab->root))
12633
0
    return false;
12634
12635
0
  if (bfd_link_pic (info))
12636
0
    obfd->flags |= DYNAMIC;
12637
12638
0
  dynamic = htab->dynamic_sections_created;
12639
0
  dynobj = htab->dynobj;
12640
12641
0
  emit_relocs = (bfd_link_relocatable (info)
12642
0
     || info->emitrelocations);
12643
12644
0
  memset (&flinfo, 0, sizeof (flinfo));
12645
0
  flinfo.info = info;
12646
0
  flinfo.output_bfd = obfd;
12647
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12648
0
  if (flinfo.symstrtab == NULL)
12649
0
    return false;
12650
12651
0
  if (! dynamic)
12652
0
    {
12653
0
      flinfo.hash_sec = NULL;
12654
0
      flinfo.symver_sec = NULL;
12655
0
    }
12656
0
  else
12657
0
    {
12658
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12659
      /* Note that dynsym_sec can be NULL (on VMS).  */
12660
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12661
      /* Note that it is OK if symver_sec is NULL.  */
12662
0
    }
12663
12664
0
  if (info->unique_symbol
12665
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12666
0
             local_hash_newfunc,
12667
0
             sizeof (struct local_hash_entry)))
12668
0
    return false;
12669
12670
  /* The object attributes have been merged.  Remove the input
12671
     sections from the link, and set the contents of the output
12672
     section.  */
12673
0
  sections_removed = false;
12674
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12675
0
  const char *obj_attrs_section = get_elf_backend_data (obfd)->obj_attrs_section;
12676
0
#endif
12677
0
  for (o = obfd->sections; o != NULL; o = o->next)
12678
0
    {
12679
0
      bool remove_section = false;
12680
12681
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12682
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12683
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12684
0
  {
12685
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12686
0
      {
12687
0
        asection *input_section;
12688
12689
0
        if (p->type != bfd_indirect_link_order)
12690
0
    continue;
12691
0
        input_section = p->u.indirect.section;
12692
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12693
     elf_link_input_bfd ignores this section.  */
12694
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12695
0
      }
12696
12697
    /* Skip this section later on.  */
12698
0
    o->map_head.link_order = NULL;
12699
12700
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (obfd);
12701
    /* Once ELF headers have been written, the size of a section is
12702
       frozen. We need to set the size of the attribute section before
12703
       _bfd_elf_compute_section_file_positions.  */
12704
0
    bfd_set_section_size (o, attr_size);
12705
0
    if (attr_size > 0)
12706
0
      elf_obj_object_attributes (obfd) = o;
12707
0
    else
12708
0
      remove_section = true;
12709
0
  }
12710
0
      else
12711
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12712
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12713
0
  {
12714
    /* Remove empty group section from linker output.  */
12715
0
    remove_section = true;
12716
0
  }
12717
0
      if (remove_section)
12718
0
  {
12719
0
    o->flags |= SEC_EXCLUDE;
12720
0
    bfd_section_list_remove (obfd, o);
12721
0
    obfd->section_count--;
12722
0
    sections_removed = true;
12723
0
  }
12724
0
    }
12725
0
  if (sections_removed)
12726
0
    bfd_fix_excluded_sec_syms (info);
12727
12728
  /* Count up the number of relocations we will output for each output
12729
     section, so that we know the sizes of the reloc sections.  We
12730
     also figure out some maximum sizes.  */
12731
0
#ifdef USE_MMAP
12732
0
  if (obed->use_mmap)
12733
0
    {
12734
      /* Mmap is used only if section size >= the minimum mmap section
12735
   size.  The initial max_contents_size value covers all sections
12736
   smaller than the minimum mmap section size.  It may be increased
12737
   for compressed or linker created sections or sections whose
12738
   rawsize != size.  max_external_reloc_size covers all relocation
12739
   sections smaller than the minimum mmap section size.  */
12740
0
      max_contents_size = _bfd_minimum_mmap_size;
12741
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12742
0
    }
12743
0
  else
12744
0
#endif
12745
0
    {
12746
0
      max_contents_size = 0;
12747
0
      max_external_reloc_size = 0;
12748
0
    }
12749
0
  max_internal_reloc_count = 0;
12750
0
  max_sym_count = 0;
12751
0
  max_sym_shndx_count = 0;
12752
0
  merged = false;
12753
0
  for (o = obfd->sections; o != NULL; o = o->next)
12754
0
    {
12755
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12756
0
      bfd_size_type size_align = 1;
12757
12758
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12759
0
  {
12760
    /* eh_frame editing can extend last FDE to cover padding
12761
       between one section and the next.  */
12762
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12763
0
      * bfd_octets_per_byte (obfd, o));
12764
0
  }
12765
12766
0
      o->reloc_count = 0;
12767
12768
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12769
0
  {
12770
0
    unsigned int reloc_count = 0;
12771
0
    unsigned int additional_reloc_count = 0;
12772
0
    struct bfd_elf_section_data *esdi = NULL;
12773
12774
0
    if (p->type == bfd_section_reloc_link_order
12775
0
        || p->type == bfd_symbol_reloc_link_order)
12776
0
      reloc_count = 1;
12777
0
    else if (p->type == bfd_indirect_link_order)
12778
0
      {
12779
0
        asection *sec;
12780
12781
0
        sec = p->u.indirect.section;
12782
12783
        /* Mark all sections which are to be included in the
12784
     link.  This will normally be every section.  We need
12785
     to do this so that we can identify any sections which
12786
     the linker has decided to not include.  */
12787
0
        sec->linker_mark = true;
12788
12789
0
        if (sec->flags & SEC_MERGE)
12790
0
    merged = true;
12791
12792
0
#ifdef USE_MMAP
12793
        /* Mmap is used only on non-compressed, non-linker created
12794
     sections whose rawsize == size.  */
12795
0
        if (!obed->use_mmap
12796
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12797
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12798
0
      || sec->rawsize != sec->size)
12799
0
#endif
12800
0
    {
12801
0
      bfd_size_type size = (sec->rawsize > sec->size
12802
0
          ? sec->rawsize : sec->size);
12803
0
      size = (size + size_align - 1) & -size_align;
12804
0
      if (max_contents_size < size)
12805
0
        max_contents_size = size;
12806
0
    }
12807
12808
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12809
0
      && (sec->owner->flags & DYNAMIC) == 0)
12810
0
    {
12811
0
      size_t sym_count;
12812
12813
      /* We are interested in just local symbols, not all
12814
         symbols.  */
12815
0
      if (elf_bad_symtab (sec->owner))
12816
0
        sym_count = (elf_symtab_hdr (sec->owner).sh_size
12817
0
         / obed->s->sizeof_sym);
12818
0
      else
12819
0
        sym_count = elf_symtab_hdr (sec->owner).sh_info;
12820
12821
0
      if (sym_count > max_sym_count)
12822
0
        max_sym_count = sym_count;
12823
12824
0
      if (sym_count > max_sym_shndx_count
12825
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12826
0
        max_sym_shndx_count = sym_count;
12827
12828
0
      esdi = elf_section_data (sec);
12829
12830
0
      if (esdi->this_hdr.sh_type == SHT_REL
12831
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12832
        /* Some backends use reloc_count in relocation sections
12833
           to count particular types of relocs.  Of course,
12834
           reloc sections themselves can't have relocations.  */
12835
0
        ;
12836
0
      else if (emit_relocs)
12837
0
        {
12838
0
          reloc_count = sec->reloc_count;
12839
0
          if (obed->elf_backend_count_additional_relocs)
12840
0
      {
12841
0
        int c;
12842
0
        c = obed->elf_backend_count_additional_relocs (sec);
12843
0
        additional_reloc_count += c;
12844
0
      }
12845
0
        }
12846
0
      else if (obed->elf_backend_count_relocs)
12847
0
        reloc_count = obed->elf_backend_count_relocs (info, sec);
12848
12849
0
      if ((sec->flags & SEC_RELOC) != 0)
12850
0
        {
12851
0
#ifdef USE_MMAP
12852
0
          if (!obed->use_mmap)
12853
0
#endif
12854
0
      {
12855
0
        size_t ext_size = 0;
12856
12857
0
        if (esdi->rel.hdr != NULL)
12858
0
          ext_size = esdi->rel.hdr->sh_size;
12859
0
        if (esdi->rela.hdr != NULL)
12860
0
          ext_size += esdi->rela.hdr->sh_size;
12861
12862
0
        if (ext_size > max_external_reloc_size)
12863
0
          max_external_reloc_size = ext_size;
12864
0
      }
12865
0
          if (sec->reloc_count > max_internal_reloc_count)
12866
0
      max_internal_reloc_count = sec->reloc_count;
12867
0
        }
12868
0
    }
12869
0
      }
12870
12871
0
    if (reloc_count == 0)
12872
0
      continue;
12873
12874
0
    reloc_count += additional_reloc_count;
12875
0
    o->reloc_count += reloc_count;
12876
12877
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12878
0
      {
12879
0
        if (esdi->rel.hdr)
12880
0
    {
12881
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12882
0
      esdo->rel.count += additional_reloc_count;
12883
0
    }
12884
0
        if (esdi->rela.hdr)
12885
0
    {
12886
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12887
0
      esdo->rela.count += additional_reloc_count;
12888
0
    }
12889
0
      }
12890
0
    else
12891
0
      {
12892
0
        if (o->use_rela_p)
12893
0
    esdo->rela.count += reloc_count;
12894
0
        else
12895
0
    esdo->rel.count += reloc_count;
12896
0
      }
12897
0
  }
12898
12899
0
      if (o->reloc_count > 0)
12900
0
  o->flags |= SEC_RELOC;
12901
0
      else
12902
0
  {
12903
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12904
       set it (this is probably a bug) and if it is set
12905
       assign_section_numbers will create a reloc section.  */
12906
0
    o->flags &=~ SEC_RELOC;
12907
0
  }
12908
12909
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12910
   zero.  This is done in elf_fake_sections as well, but forcing
12911
   the VMA to 0 here will ensure that relocs against these
12912
   sections are handled correctly.  */
12913
0
      if ((o->flags & SEC_ALLOC) == 0
12914
0
    && ! o->user_set_vma)
12915
0
  o->vma = 0;
12916
0
    }
12917
12918
0
  if (! bfd_link_relocatable (info) && merged)
12919
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, obfd);
12920
12921
  /* Figure out the file positions for everything but the symbol table
12922
     and the relocs.  We set symcount to force assign_section_numbers
12923
     to create a symbol table.  */
12924
0
  obfd->symcount = info->strip != strip_all || emit_relocs;
12925
0
  BFD_ASSERT (! obfd->output_has_begun);
12926
0
  if (! _bfd_elf_compute_section_file_positions (obfd, info))
12927
0
    goto error_return;
12928
12929
  /* Set sizes, and assign file positions for reloc sections.  */
12930
0
  for (o = obfd->sections; o != NULL; o = o->next)
12931
0
    {
12932
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12933
0
      if ((o->flags & SEC_RELOC) != 0)
12934
0
  {
12935
0
    if (esdo->rel.hdr
12936
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rel)))
12937
0
      goto error_return;
12938
12939
0
    if (esdo->rela.hdr
12940
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rela)))
12941
0
      goto error_return;
12942
0
  }
12943
12944
      /* _bfd_elf_compute_section_file_positions makes temporary use
12945
   of target_index.  Reset it.  */
12946
0
      o->target_index = 0;
12947
12948
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12949
   to count upwards while actually outputting the relocations.  */
12950
0
      esdo->rel.count = 0;
12951
0
      esdo->rela.count = 0;
12952
12953
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12954
0
    && !bfd_section_is_ctf (o))
12955
0
  {
12956
    /* Cache the section contents so that they can be compressed
12957
       later.  Use bfd_malloc since it will be freed by
12958
       bfd_compress_section_contents.  */
12959
0
    unsigned char *contents = esdo->this_hdr.contents;
12960
0
    if (contents != NULL)
12961
0
      abort ();
12962
0
    contents = bfd_malloc (esdo->this_hdr.sh_size);
12963
0
    if (contents == NULL)
12964
0
      goto error_return;
12965
0
    esdo->this_hdr.contents = contents;
12966
0
  }
12967
0
    }
12968
12969
  /* We have now assigned file positions for all the sections except .symtab,
12970
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
12971
     the .symtab section at the current file position, and write directly to it.
12972
     We build the .strtab section in memory.  */
12973
0
  obfd->symcount = 0;
12974
0
  symtab_hdr = &elf_symtab_hdr (obfd);
12975
  /* sh_name is set in prep_headers.  */
12976
0
  symtab_hdr->sh_type = SHT_SYMTAB;
12977
  /* sh_flags, sh_addr and sh_size all start off zero.  */
12978
0
  symtab_hdr->sh_entsize = obed->s->sizeof_sym;
12979
  /* sh_link is set in assign_section_numbers.  */
12980
  /* sh_info is set below.  */
12981
  /* sh_offset is set just below.  */
12982
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << obed->s->log_file_align;
12983
12984
0
  if (max_sym_count < 20)
12985
0
    max_sym_count = 20;
12986
0
  htab->strtabsize = max_sym_count;
12987
0
  htab->strtab = bfd_malloc (max_sym_count * sizeof (*htab->strtab));
12988
0
  if (htab->strtab == NULL)
12989
0
    goto error_return;
12990
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
12991
0
  flinfo.symshndxbuf
12992
0
    = (elf_numsections (obfd) > (SHN_LORESERVE & 0xFFFF)
12993
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
12994
12995
0
  if (info->strip != strip_all || emit_relocs)
12996
0
    {
12997
0
      file_ptr off = elf_next_file_pos (obfd);
12998
12999
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13000
13001
      /* Note that at this point elf_next_file_pos (obfd) is
13002
   incorrect.  We do not yet know the size of the .symtab section.
13003
   We correct next_file_pos below, after we do know the size.  */
13004
13005
      /* Start writing out the symbol table.  The first symbol is always a
13006
   dummy symbol.  */
13007
0
      elfsym.st_value = 0;
13008
0
      elfsym.st_size = 0;
13009
0
      elfsym.st_info = 0;
13010
0
      elfsym.st_other = 0;
13011
0
      elfsym.st_shndx = SHN_UNDEF;
13012
0
      elfsym.st_target_internal = 0;
13013
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13014
0
             bfd_und_section_ptr, NULL) != 1)
13015
0
  goto error_return;
13016
13017
      /* Output a symbol for each section if asked or they are used for
13018
   relocs.  These symbols usually have no names.  We store the
13019
   index of each one in the index field of the section, so that
13020
   we can find it again when outputting relocs.  */
13021
13022
0
      if (bfd_keep_unused_section_symbols (obfd) || emit_relocs)
13023
0
  {
13024
0
    bool name_local_sections
13025
0
      = (obed->elf_backend_name_local_section_symbols
13026
0
         && obed->elf_backend_name_local_section_symbols (obfd));
13027
0
    const char *name = NULL;
13028
13029
0
    elfsym.st_size = 0;
13030
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13031
0
    elfsym.st_other = 0;
13032
0
    elfsym.st_value = 0;
13033
0
    elfsym.st_target_internal = 0;
13034
0
    for (i = 1; i < elf_numsections (obfd); i++)
13035
0
      {
13036
0
        o = bfd_section_from_elf_index (obfd, i);
13037
0
        if (o != NULL)
13038
0
    {
13039
0
      o->target_index = bfd_get_symcount (obfd);
13040
0
      elfsym.st_shndx = i;
13041
0
      if (!bfd_link_relocatable (info))
13042
0
        elfsym.st_value = o->vma;
13043
0
      if (name_local_sections)
13044
0
        name = o->name;
13045
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13046
0
             NULL) != 1)
13047
0
        goto error_return;
13048
0
    }
13049
0
      }
13050
0
  }
13051
0
    }
13052
13053
  /* On some targets like Irix 5 the symbol split between local and global
13054
     ones recorded in the sh_info field needs to be done between section
13055
     and all other symbols.  */
13056
0
  if (obed->elf_backend_elfsym_local_is_section
13057
0
      && obed->elf_backend_elfsym_local_is_section (obfd))
13058
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13059
13060
  /* Allocate some memory to hold information read in from the input
13061
     files.  */
13062
0
  if (max_contents_size != 0)
13063
0
    {
13064
0
      flinfo.contents = bfd_malloc (max_contents_size);
13065
0
      if (flinfo.contents == NULL)
13066
0
  goto error_return;
13067
0
    }
13068
13069
0
  if (max_external_reloc_size != 0)
13070
0
    {
13071
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13072
0
      if (flinfo.external_relocs == NULL)
13073
0
  goto error_return;
13074
0
    }
13075
13076
0
  if (max_internal_reloc_count != 0)
13077
0
    {
13078
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13079
0
      flinfo.internal_relocs = bfd_malloc (amt);
13080
0
      if (flinfo.internal_relocs == NULL)
13081
0
  goto error_return;
13082
0
    }
13083
13084
0
  if (max_sym_count != 0)
13085
0
    {
13086
0
      amt = max_sym_count * obed->s->sizeof_sym;
13087
0
      flinfo.external_syms = bfd_malloc (amt);
13088
0
      if (flinfo.external_syms == NULL)
13089
0
  goto error_return;
13090
13091
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13092
0
      flinfo.internal_syms = bfd_malloc (amt);
13093
0
      if (flinfo.internal_syms == NULL)
13094
0
  goto error_return;
13095
13096
0
      amt = max_sym_count * sizeof (long);
13097
0
      flinfo.indices = bfd_malloc (amt);
13098
0
      if (flinfo.indices == NULL)
13099
0
  goto error_return;
13100
13101
0
      amt = max_sym_count * sizeof (asection *);
13102
0
      flinfo.sections = bfd_malloc (amt);
13103
0
      if (flinfo.sections == NULL)
13104
0
  goto error_return;
13105
0
    }
13106
13107
0
  if (max_sym_shndx_count != 0)
13108
0
    {
13109
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13110
0
      flinfo.locsym_shndx = bfd_malloc (amt);
13111
0
      if (flinfo.locsym_shndx == NULL)
13112
0
  goto error_return;
13113
0
    }
13114
13115
0
  if (htab->tls_sec)
13116
0
    {
13117
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13118
0
      asection *sec;
13119
13120
0
      for (sec = htab->tls_sec;
13121
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13122
0
     sec = sec->next)
13123
0
  {
13124
0
    bfd_size_type size = sec->size;
13125
0
    unsigned int opb = bfd_octets_per_byte (obfd, sec);
13126
13127
0
    if (size == 0
13128
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13129
0
      {
13130
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13131
13132
0
        if (ord != NULL)
13133
0
    size = ord->offset * opb + ord->size;
13134
0
      }
13135
0
    end = sec->vma + size / opb;
13136
0
  }
13137
0
      base = htab->tls_sec->vma;
13138
      /* Only align end of TLS section if static TLS doesn't have special
13139
   alignment requirements.  */
13140
0
      if (obed->static_tls_alignment == 1)
13141
0
  end = align_power (end, htab->tls_sec->alignment_power);
13142
0
      htab->tls_size = end - base;
13143
0
    }
13144
13145
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13146
0
    return false;
13147
13148
  /* Finish relative relocations here after regular symbol processing
13149
     is finished if DT_RELR is enabled.  */
13150
0
  if (info->enable_dt_relr
13151
0
      && obed->finish_relative_relocs
13152
0
      && !obed->finish_relative_relocs (info))
13153
0
    info->callbacks->fatal
13154
0
      (_("%P: %pB: failed to finish relative relocations\n"), obfd);
13155
13156
  /* Since ELF permits relocations to be against local symbols, we
13157
     must have the local symbols available when we do the relocations.
13158
     Since we would rather only read the local symbols once, and we
13159
     would rather not keep them in memory, we handle all the
13160
     relocations for a single input file at the same time.
13161
13162
     Unfortunately, there is no way to know the total number of local
13163
     symbols until we have seen all of them, and the local symbol
13164
     indices precede the global symbol indices.  This means that when
13165
     we are generating relocatable output, and we see a reloc against
13166
     a global symbol, we can not know the symbol index until we have
13167
     finished examining all the local symbols to see which ones we are
13168
     going to output.  To deal with this, we keep the relocations in
13169
     memory, and don't output them until the end of the link.  This is
13170
     an unfortunate waste of memory, but I don't see a good way around
13171
     it.  Fortunately, it only happens when performing a relocatable
13172
     link, which is not the common case.  FIXME: If keep_memory is set
13173
     we could write the relocs out and then read them again; I don't
13174
     know how bad the memory loss will be.  */
13175
13176
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13177
0
    sub->output_has_begun = false;
13178
0
  for (o = obfd->sections; o != NULL; o = o->next)
13179
0
    {
13180
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13181
0
  {
13182
0
    if (p->type == bfd_indirect_link_order
13183
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13184
0
      == bfd_target_elf_flavour)
13185
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == obed->s->elfclass)
13186
0
      {
13187
0
        if (! sub->output_has_begun)
13188
0
    {
13189
0
      if (! elf_link_input_bfd (&flinfo, sub))
13190
0
        goto error_return;
13191
0
      sub->output_has_begun = true;
13192
0
    }
13193
0
      }
13194
0
    else if (p->type == bfd_section_reloc_link_order
13195
0
       || p->type == bfd_symbol_reloc_link_order)
13196
0
      {
13197
0
        if (! elf_reloc_link_order (obfd, info, o, p))
13198
0
    goto error_return;
13199
0
      }
13200
0
    else
13201
0
      {
13202
0
        if (! _bfd_default_link_order (obfd, info, o, p))
13203
0
    {
13204
0
      if (p->type == bfd_indirect_link_order
13205
0
          && (bfd_get_flavour (sub)
13206
0
        == bfd_target_elf_flavour)
13207
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13208
0
        != obed->s->elfclass))
13209
0
        {
13210
0
          const char *iclass, *oclass;
13211
13212
0
          switch (obed->s->elfclass)
13213
0
      {
13214
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13215
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13216
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13217
0
      default: abort ();
13218
0
      }
13219
13220
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13221
0
      {
13222
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13223
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13224
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13225
0
      default: abort ();
13226
0
      }
13227
13228
0
          bfd_set_error (bfd_error_wrong_format);
13229
0
          _bfd_error_handler
13230
      /* xgettext:c-format */
13231
0
      (_("%pB: file class %s incompatible with %s"),
13232
0
       sub, iclass, oclass);
13233
0
        }
13234
13235
0
      goto error_return;
13236
0
    }
13237
0
      }
13238
0
  }
13239
0
    }
13240
13241
  /* Free symbol buffer if needed.  */
13242
0
  if (!info->reduce_memory_overheads)
13243
0
    {
13244
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13245
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13246
0
    {
13247
0
      free (elf_tdata (sub)->symbuf);
13248
0
      elf_tdata (sub)->symbuf = NULL;
13249
0
    }
13250
0
    }
13251
13252
  /* Output any global symbols that got converted to local in a
13253
     version script or due to symbol visibility.  We do this in a
13254
     separate step since ELF requires all local symbols to appear
13255
     prior to any global symbols.  FIXME: We should only do this if
13256
     some global symbols were, in fact, converted to become local.
13257
     FIXME: Will this work correctly with the Irix 5 linker?  */
13258
0
  eoinfo.failed = false;
13259
0
  eoinfo.flinfo = &flinfo;
13260
0
  eoinfo.localsyms = true;
13261
0
  eoinfo.file_sym_done = false;
13262
  /* Output non-base symbols first.  */
13263
0
  eoinfo.base_symbol = false;
13264
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13265
0
  if (eoinfo.failed)
13266
0
    goto error_return;
13267
13268
  /* If backend needs to output some local symbols not present in the hash
13269
     table, do it now.  */
13270
0
  if (obed->elf_backend_output_arch_local_syms)
13271
0
    {
13272
0
      if (!obed->elf_backend_output_arch_local_syms (info, &flinfo,
13273
0
                 elf_link_output_symstrtab))
13274
0
  goto error_return;
13275
0
    }
13276
13277
  /* That wrote out all the local symbols.  Finish up the symbol table
13278
     with the global symbols. Even if we want to strip everything we
13279
     can, we still need to deal with those global symbols that got
13280
     converted to local in a version script.  */
13281
13282
  /* The sh_info field records the index of the first non local symbol.  */
13283
0
  if (!symtab_hdr->sh_info)
13284
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13285
13286
0
  if (dynamic
13287
0
      && htab->dynsym != NULL
13288
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13289
0
    {
13290
0
      Elf_Internal_Sym sym;
13291
0
      bfd_byte *dynsym = htab->dynsym->contents;
13292
13293
0
      o = htab->dynsym->output_section;
13294
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13295
13296
      /* Write out the section symbols for the output sections.  */
13297
0
      if (bfd_link_pic (info)
13298
0
    || htab->is_relocatable_executable)
13299
0
  {
13300
0
    asection *s;
13301
13302
0
    sym.st_size = 0;
13303
0
    sym.st_name = 0;
13304
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13305
0
    sym.st_other = 0;
13306
0
    sym.st_target_internal = 0;
13307
13308
0
    for (s = obfd->sections; s != NULL; s = s->next)
13309
0
      {
13310
0
        int indx;
13311
0
        bfd_byte *dest;
13312
0
        long dynindx;
13313
13314
0
        dynindx = elf_section_data (s)->dynindx;
13315
0
        if (dynindx <= 0)
13316
0
    continue;
13317
0
        indx = elf_section_data (s)->this_idx;
13318
0
        BFD_ASSERT (indx > 0);
13319
0
        sym.st_shndx = indx;
13320
0
        if (! check_dynsym (obfd, &sym))
13321
0
    goto error_return;
13322
0
        sym.st_value = s->vma;
13323
0
        dest = dynsym + dynindx * obed->s->sizeof_sym;
13324
13325
        /* Inform the linker of the addition of this symbol.  */
13326
13327
0
        if (info->callbacks->ctf_new_dynsym)
13328
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13329
13330
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13331
0
      }
13332
0
  }
13333
13334
      /* Write out the local dynsyms.  */
13335
0
      if (htab->dynlocal)
13336
0
  {
13337
0
    struct elf_link_local_dynamic_entry *e;
13338
0
    for (e = htab->dynlocal; e ; e = e->next)
13339
0
      {
13340
0
        asection *s;
13341
0
        bfd_byte *dest;
13342
13343
        /* Copy the internal symbol and turn off visibility.
13344
     Note that we saved a word of storage and overwrote
13345
     the original st_name with the dynstr_index.  */
13346
0
        sym = e->isym;
13347
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13348
0
        sym.st_shndx = SHN_UNDEF;
13349
13350
0
        s = bfd_section_from_elf_index (e->input_bfd,
13351
0
                e->isym.st_shndx);
13352
0
        if (s != NULL
13353
0
      && s->output_section != NULL
13354
0
      && elf_section_data (s->output_section) != NULL)
13355
0
    {
13356
0
      sym.st_shndx =
13357
0
        elf_section_data (s->output_section)->this_idx;
13358
0
      if (! check_dynsym (obfd, &sym))
13359
0
        goto error_return;
13360
0
      sym.st_value = (s->output_section->vma
13361
0
          + s->output_offset
13362
0
          + e->isym.st_value);
13363
0
    }
13364
13365
        /* Inform the linker of the addition of this symbol.  */
13366
13367
0
        if (info->callbacks->ctf_new_dynsym)
13368
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13369
13370
0
        dest = dynsym + e->dynindx * obed->s->sizeof_sym;
13371
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13372
0
      }
13373
0
  }
13374
0
    }
13375
13376
  /* We get the global symbols from the hash table.  */
13377
0
  eoinfo.failed = false;
13378
0
  eoinfo.localsyms = false;
13379
0
  eoinfo.flinfo = &flinfo;
13380
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13381
0
  if (eoinfo.failed)
13382
0
    goto error_return;
13383
13384
0
  if (htab->has_base_symbols)
13385
0
    {
13386
      /* Output base symbols last in DT_HASH so that they will be picked
13387
   before non-base symbols at run-time.  */
13388
0
      eoinfo.base_symbol = true;
13389
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13390
0
       &eoinfo);
13391
0
      if (eoinfo.failed)
13392
0
  goto error_return;
13393
0
    }
13394
13395
  /* If backend needs to output some symbols not present in the hash
13396
     table, do it now.  */
13397
0
  if (obed->elf_backend_output_arch_syms
13398
0
      && (info->strip != strip_all || emit_relocs))
13399
0
    {
13400
0
      if (!obed->elf_backend_output_arch_syms (info, &flinfo,
13401
0
                 elf_link_output_symstrtab))
13402
0
  goto error_return;
13403
0
    }
13404
13405
  /* Finalize the .strtab section.  */
13406
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13407
13408
  /* Swap out the .strtab section. */
13409
0
  if (!elf_link_swap_symbols_out (&flinfo))
13410
0
    goto error_return;
13411
0
  free (htab->strtab);
13412
0
  htab->strtab = NULL;
13413
13414
  /* Now we know the size of the symtab section.  */
13415
0
  if (bfd_get_symcount (obfd) > 0)
13416
0
    {
13417
      /* Finish up and write out the symbol string table (.strtab)
13418
   section.  */
13419
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13420
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13421
13422
0
      if (elf_symtab_shndx_list (obfd))
13423
0
  {
13424
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (obfd)->hdr;
13425
13426
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13427
0
      {
13428
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13429
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13430
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13431
0
        amt = bfd_get_symcount (obfd) * sizeof (Elf_External_Sym_Shndx);
13432
0
        symtab_shndx_hdr->sh_size = amt;
13433
13434
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13435
0
                     off, true, 0);
13436
13437
0
        if (bfd_seek (obfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13438
0
      || (bfd_write (flinfo.symshndxbuf, amt, obfd) != amt))
13439
0
    goto error_return;
13440
0
      }
13441
0
  }
13442
13443
0
      symstrtab_hdr = &elf_tdata (obfd)->strtab_hdr;
13444
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13445
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13446
0
      symstrtab_hdr->sh_addralign = 1;
13447
13448
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13449
0
                   off, true, 0);
13450
0
      elf_next_file_pos (obfd) = off;
13451
13452
0
      if (bfd_seek (obfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13453
0
    || ! _bfd_elf_strtab_emit (obfd, flinfo.symstrtab))
13454
0
  goto error_return;
13455
0
    }
13456
13457
0
  if (info->out_implib_bfd && !elf_output_implib (obfd, info))
13458
0
    {
13459
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13460
0
        info->out_implib_bfd);
13461
0
      goto error_return;
13462
0
    }
13463
13464
  /* Adjust the relocs to have the correct symbol indices.  */
13465
0
  for (o = obfd->sections; o != NULL; o = o->next)
13466
0
    {
13467
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13468
0
      bool sort;
13469
13470
0
      if ((o->flags & SEC_RELOC) == 0)
13471
0
  continue;
13472
13473
0
      sort = obed->sort_relocs_p == NULL || obed->sort_relocs_p (o);
13474
0
      if (esdo->rel.hdr != NULL
13475
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rel, sort, info))
13476
0
  goto error_return;
13477
0
      if (esdo->rela.hdr != NULL
13478
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rela, sort, info))
13479
0
  goto error_return;
13480
13481
      /* Set the reloc_count field to 0 to prevent write_relocs from
13482
   trying to swap the relocs out itself.  */
13483
0
      o->reloc_count = 0;
13484
0
    }
13485
13486
0
  relativecount = 0;
13487
0
  if (dynamic && info->combreloc && dynobj != NULL)
13488
0
    relativecount = elf_link_sort_relocs (obfd, info, &reldyn);
13489
13490
0
  relr_entsize = 0;
13491
0
  if (htab->srelrdyn != NULL
13492
0
      && htab->srelrdyn->output_section != NULL
13493
0
      && htab->srelrdyn->size != 0)
13494
0
    {
13495
0
      asection *s = htab->srelrdyn->output_section;
13496
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13497
0
      if (relr_entsize == 0)
13498
0
  {
13499
0
    relr_entsize = obed->s->arch_size / 8;
13500
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13501
0
  }
13502
0
    }
13503
13504
  /* If we are linking against a dynamic object, or generating a
13505
     shared library, finish up the dynamic linking information.  */
13506
0
  if (dynamic)
13507
0
    {
13508
0
      bfd_byte *dyncon, *dynconend;
13509
13510
      /* Fix up .dynamic entries.  */
13511
0
      o = htab->dynamic;
13512
0
      BFD_ASSERT (o != NULL);
13513
13514
0
      dyncon = o->contents;
13515
0
      dynconend = PTR_ADD (o->contents, o->size);
13516
0
      for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13517
0
  {
13518
0
    Elf_Internal_Dyn dyn;
13519
0
    const char *name;
13520
0
    unsigned int type;
13521
0
    bfd_size_type sh_size;
13522
0
    bfd_vma sh_addr;
13523
13524
0
    obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13525
13526
0
    switch (dyn.d_tag)
13527
0
      {
13528
0
      default:
13529
0
        continue;
13530
0
      case DT_NULL:
13531
0
        if (relativecount != 0)
13532
0
    {
13533
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13534
0
        {
13535
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13536
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13537
0
        }
13538
0
      if (dyn.d_tag != DT_NULL
13539
0
          && dynconend - dyncon >= obed->s->sizeof_dyn)
13540
0
        {
13541
0
          dyn.d_un.d_val = relativecount;
13542
0
          relativecount = 0;
13543
0
          break;
13544
0
        }
13545
0
      relativecount = 0;
13546
0
    }
13547
0
        if (relr_entsize != 0)
13548
0
    {
13549
0
      if (dynconend - dyncon >= 3 * obed->s->sizeof_dyn)
13550
0
        {
13551
0
          asection *s = htab->srelrdyn;
13552
0
          dyn.d_tag = DT_RELR;
13553
0
          dyn.d_un.d_ptr
13554
0
      = s->output_section->vma + s->output_offset;
13555
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13556
0
          dyncon += obed->s->sizeof_dyn;
13557
13558
0
          dyn.d_tag = DT_RELRSZ;
13559
0
          dyn.d_un.d_val = s->size;
13560
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13561
0
          dyncon += obed->s->sizeof_dyn;
13562
13563
0
          dyn.d_tag = DT_RELRENT;
13564
0
          dyn.d_un.d_val = relr_entsize;
13565
0
          relr_entsize = 0;
13566
0
          break;
13567
0
        }
13568
0
      relr_entsize = 0;
13569
0
    }
13570
0
        continue;
13571
13572
0
      case DT_INIT:
13573
0
        name = info->init_function;
13574
0
        goto get_sym;
13575
0
      case DT_FINI:
13576
0
        name = info->fini_function;
13577
0
      get_sym:
13578
0
        {
13579
0
    struct elf_link_hash_entry *h;
13580
13581
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13582
0
    if (h != NULL
13583
0
        && (h->root.type == bfd_link_hash_defined
13584
0
      || h->root.type == bfd_link_hash_defweak))
13585
0
      {
13586
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13587
0
        o = h->root.u.def.section;
13588
0
        if (o->output_section != NULL)
13589
0
          dyn.d_un.d_ptr += (o->output_section->vma
13590
0
           + o->output_offset);
13591
0
        else
13592
0
          {
13593
      /* The symbol is imported from another shared
13594
         library and does not apply to this one.  */
13595
0
      dyn.d_un.d_ptr = 0;
13596
0
          }
13597
0
        break;
13598
0
      }
13599
0
        }
13600
0
        continue;
13601
13602
0
      case DT_PREINIT_ARRAYSZ:
13603
0
        name = ".preinit_array";
13604
0
        goto get_out_size;
13605
0
      case DT_INIT_ARRAYSZ:
13606
0
        name = ".init_array";
13607
0
        goto get_out_size;
13608
0
      case DT_FINI_ARRAYSZ:
13609
0
        name = ".fini_array";
13610
0
      get_out_size:
13611
0
        o = bfd_get_section_by_name (obfd, name);
13612
0
        if (o == NULL)
13613
0
    {
13614
0
      _bfd_error_handler
13615
0
        (_("could not find section %s"), name);
13616
0
      goto error_return;
13617
0
    }
13618
0
        if (o->size == 0)
13619
0
    _bfd_error_handler
13620
0
      (_("warning: %s section has zero size"), name);
13621
0
        dyn.d_un.d_val = o->size;
13622
0
        break;
13623
13624
0
      case DT_PREINIT_ARRAY:
13625
0
        name = ".preinit_array";
13626
0
        goto get_out_vma;
13627
0
      case DT_INIT_ARRAY:
13628
0
        name = ".init_array";
13629
0
        goto get_out_vma;
13630
0
      case DT_FINI_ARRAY:
13631
0
        name = ".fini_array";
13632
0
      get_out_vma:
13633
0
        o = bfd_get_section_by_name (obfd, name);
13634
0
        goto do_vma;
13635
13636
0
      case DT_HASH:
13637
0
        name = ".hash";
13638
0
        goto get_vma;
13639
0
      case DT_GNU_HASH:
13640
0
        name = ".gnu.hash";
13641
0
        goto get_vma;
13642
0
      case DT_STRTAB:
13643
0
        name = ".dynstr";
13644
0
        goto get_vma;
13645
0
      case DT_SYMTAB:
13646
0
        name = ".dynsym";
13647
0
        goto get_vma;
13648
0
      case DT_VERDEF:
13649
0
        name = ".gnu.version_d";
13650
0
        goto get_vma;
13651
0
      case DT_VERNEED:
13652
0
        name = ".gnu.version_r";
13653
0
        goto get_vma;
13654
0
      case DT_VERSYM:
13655
0
        name = ".gnu.version";
13656
0
      get_vma:
13657
0
        o = bfd_get_linker_section (dynobj, name);
13658
0
      do_vma:
13659
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13660
0
    {
13661
0
      _bfd_error_handler
13662
0
        (_("could not find section %s"), name);
13663
0
      goto error_return;
13664
0
    }
13665
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13666
0
    {
13667
0
      _bfd_error_handler
13668
0
        (_("warning: section '%s' is being made into a note"), name);
13669
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13670
0
      goto error_return;
13671
0
    }
13672
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13673
0
        break;
13674
13675
0
      case DT_REL:
13676
0
      case DT_RELA:
13677
0
      case DT_RELSZ:
13678
0
      case DT_RELASZ:
13679
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13680
0
    type = SHT_REL;
13681
0
        else
13682
0
    type = SHT_RELA;
13683
0
        sh_size = 0;
13684
0
        sh_addr = 0;
13685
0
        for (i = 1; i < elf_numsections (obfd); i++)
13686
0
    {
13687
0
      Elf_Internal_Shdr *hdr;
13688
13689
0
      hdr = elf_elfsections (obfd)[i];
13690
0
      if (hdr->sh_type == type
13691
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13692
0
        {
13693
0
          sh_size += hdr->sh_size;
13694
0
          if (sh_addr == 0
13695
0
        || sh_addr > hdr->sh_addr)
13696
0
      sh_addr = hdr->sh_addr;
13697
0
        }
13698
0
    }
13699
13700
0
        if (obed->dtrel_excludes_plt && htab->srelplt != NULL)
13701
0
    {
13702
0
      unsigned int opb = bfd_octets_per_byte (obfd, o);
13703
13704
      /* Don't count procedure linkage table relocs in the
13705
         overall reloc count.  */
13706
0
      sh_size -= htab->srelplt->size;
13707
0
      if (sh_size == 0)
13708
        /* If the size is zero, make the address zero too.
13709
           This is to avoid a glibc bug.  If the backend
13710
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13711
           zero, then we'll put DT_RELA at the end of
13712
           DT_JMPREL.  glibc will interpret the end of
13713
           DT_RELA matching the end of DT_JMPREL as the
13714
           case where DT_RELA includes DT_JMPREL, and for
13715
           LD_BIND_NOW will decide that processing DT_RELA
13716
           will process the PLT relocs too.  Net result:
13717
           No PLT relocs applied.  */
13718
0
        sh_addr = 0;
13719
13720
      /* If .rela.plt is the first .rela section, exclude
13721
         it from DT_RELA.  */
13722
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13723
0
               + htab->srelplt->output_offset) * opb)
13724
0
        sh_addr += htab->srelplt->size;
13725
0
    }
13726
13727
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13728
0
    dyn.d_un.d_val = sh_size;
13729
0
        else
13730
0
    dyn.d_un.d_ptr = sh_addr;
13731
0
        break;
13732
0
      }
13733
0
    obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13734
0
  }
13735
0
    }
13736
13737
  /* If we have created any dynamic sections, then output them.  */
13738
0
  if (dynobj != NULL)
13739
0
    {
13740
0
      if (!obed->elf_backend_finish_dynamic_sections (info, flinfo.contents))
13741
0
  goto error_return;
13742
13743
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13744
0
      if (bfd_link_textrel_check (info)
13745
0
    && (o = htab->dynamic) != NULL
13746
0
    && o->size != 0)
13747
0
  {
13748
0
    bfd_byte *dyncon, *dynconend;
13749
13750
0
    dyncon = o->contents;
13751
0
    dynconend = o->contents + o->size;
13752
0
    for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13753
0
      {
13754
0
        Elf_Internal_Dyn dyn;
13755
13756
0
        obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13757
13758
0
        if (dyn.d_tag == DT_TEXTREL)
13759
0
    {
13760
0
      if (info->textrel_check == textrel_check_error)
13761
0
        info->callbacks->einfo
13762
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13763
0
      else if (bfd_link_dll (info))
13764
0
        info->callbacks->einfo
13765
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13766
0
      else if (bfd_link_pde (info))
13767
0
        info->callbacks->einfo
13768
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13769
0
      else
13770
0
        info->callbacks->einfo
13771
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13772
0
      break;
13773
0
    }
13774
0
      }
13775
0
  }
13776
13777
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13778
0
  {
13779
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13780
0
        || o->size == 0
13781
0
        || o->output_section == bfd_abs_section_ptr)
13782
0
      continue;
13783
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13784
0
      {
13785
        /* At this point, we are only interested in sections
13786
     created by bfd_elf_link_create_dynamic_sections().  */
13787
0
        continue;
13788
0
      }
13789
0
    if (htab->stab_info.stabstr == o)
13790
0
      continue;
13791
0
    if (htab->eh_info.hdr_sec == o)
13792
0
      continue;
13793
0
    if (strcmp (o->name, ".dynstr") != 0)
13794
0
      {
13795
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13796
0
              * bfd_octets_per_byte (obfd, o));
13797
0
        if (!bfd_set_section_contents (obfd, o->output_section,
13798
0
               o->contents, octets, o->size))
13799
0
    goto error_return;
13800
0
      }
13801
0
    else
13802
0
      {
13803
        /* The contents of the .dynstr section are actually in a
13804
     stringtab.  */
13805
0
        file_ptr off;
13806
13807
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13808
0
        if (bfd_seek (obfd, off, SEEK_SET) != 0
13809
0
      || !_bfd_elf_strtab_emit (obfd, htab->dynstr))
13810
0
    goto error_return;
13811
0
      }
13812
0
  }
13813
0
    }
13814
13815
0
  if (!info->resolve_section_groups)
13816
0
    {
13817
0
      bool failed = false;
13818
13819
0
      BFD_ASSERT (bfd_link_relocatable (info));
13820
0
      bfd_map_over_sections (obfd, bfd_elf_set_group_contents, &failed);
13821
0
      if (failed)
13822
0
  goto error_return;
13823
0
    }
13824
13825
  /* If we have optimized stabs strings, output them.  */
13826
0
  if (htab->stab_info.stabstr != NULL)
13827
0
    {
13828
0
      if (!_bfd_write_stab_strings (obfd, &htab->stab_info))
13829
0
  goto error_return;
13830
0
    }
13831
13832
0
  if (! _bfd_elf_write_section_eh_frame_hdr (obfd, info))
13833
0
    goto error_return;
13834
13835
0
  if (! _bfd_elf_write_section_sframe (obfd, info))
13836
0
    goto error_return;
13837
13838
0
  if (! _bfd_elf_write_section_object_attributes (obfd, info))
13839
0
    goto error_ret2;
13840
13841
0
  if (info->callbacks->emit_ctf)
13842
0
      info->callbacks->emit_ctf ();
13843
13844
0
  elf_final_link_free (obfd, &flinfo);
13845
13846
0
  if (info->unique_symbol)
13847
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13848
0
  return true;
13849
13850
0
 error_return:
13851
0
  free (htab->strtab);
13852
0
  htab->strtab = NULL;
13853
0
  elf_final_link_free (obfd, &flinfo);
13854
0
 error_ret2:
13855
0
  if (info->unique_symbol)
13856
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13857
0
  return false;
13858
0
}
13859

13860
/* Initialize COOKIE for input bfd ABFD.  */
13861
13862
static bool
13863
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13864
0
{
13865
0
  Elf_Internal_Shdr *symtab_hdr;
13866
0
  elf_backend_data *bed;
13867
13868
0
  bed = get_elf_backend_data (abfd);
13869
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13870
13871
0
  cookie->abfd = abfd;
13872
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13873
0
  if (elf_bad_symtab (abfd))
13874
0
    {
13875
0
      cookie->locsymcount = cookie->num_sym;
13876
0
      cookie->extsymoff = 0;
13877
0
    }
13878
0
  else
13879
0
    {
13880
0
      cookie->locsymcount = symtab_hdr->sh_info;
13881
0
      cookie->extsymoff = symtab_hdr->sh_info;
13882
0
    }
13883
13884
0
  if (bed->s->arch_size == 32)
13885
0
    cookie->r_sym_shift = 8;
13886
0
  else
13887
0
    cookie->r_sym_shift = 32;
13888
13889
0
  return true;
13890
0
}
13891
13892
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13893
13894
static void
13895
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13896
       bfd *abfd ATTRIBUTE_UNUSED)
13897
0
{
13898
0
}
13899
13900
/* Initialize the relocation information in COOKIE for input section SEC
13901
   of input bfd ABFD.  */
13902
13903
static bool
13904
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13905
      struct bfd_link_info *info, bfd *abfd,
13906
      asection *sec, bool keep_memory)
13907
0
{
13908
0
  if (sec->reloc_count == 0)
13909
0
    {
13910
0
      cookie->rels = NULL;
13911
0
      cookie->relend = NULL;
13912
0
    }
13913
0
  else
13914
0
    {
13915
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13916
0
  (abfd, info, sec, NULL, NULL,
13917
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13918
0
      if (cookie->rels == NULL)
13919
0
  return false;
13920
0
      cookie->rel = cookie->rels;
13921
0
      cookie->relend = cookie->rels + sec->reloc_count;
13922
0
    }
13923
0
  cookie->rel = cookie->rels;
13924
0
  return true;
13925
0
}
13926
13927
/* Free the memory allocated by init_reloc_cookie_rels,
13928
   if appropriate.  */
13929
13930
static void
13931
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13932
      asection *sec)
13933
0
{
13934
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13935
0
    free (cookie->rels);
13936
0
}
13937
13938
/* Initialize the whole of COOKIE for input section SEC.  */
13939
13940
static bool
13941
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13942
             struct bfd_link_info *info,
13943
             asection *sec, bool keep_memory)
13944
0
{
13945
0
  if (!init_reloc_cookie (cookie, sec->owner))
13946
0
    goto error1;
13947
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13948
0
             keep_memory))
13949
0
    goto error2;
13950
0
  return true;
13951
13952
0
 error2:
13953
0
  fini_reloc_cookie (cookie, sec->owner);
13954
0
 error1:
13955
0
  return false;
13956
0
}
13957
13958
/* Free the memory allocated by init_reloc_cookie_for_section,
13959
   if appropriate.  */
13960
13961
static void
13962
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13963
             asection *sec)
13964
0
{
13965
0
  fini_reloc_cookie_rels (cookie, sec);
13966
0
  fini_reloc_cookie (cookie, sec->owner);
13967
0
}
13968

13969
/* Garbage collect unused sections.  */
13970
13971
/* Default gc_mark_hook.  */
13972
13973
asection *
13974
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
13975
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
13976
           struct elf_reloc_cookie *cookie,
13977
           struct elf_link_hash_entry *h,
13978
           unsigned int symndx)
13979
0
{
13980
0
  if (h == NULL)
13981
0
    return _bfd_get_local_sym_section (cookie, symndx);
13982
13983
0
  switch (h->root.type)
13984
0
    {
13985
0
    case bfd_link_hash_defined:
13986
0
    case bfd_link_hash_defweak:
13987
0
      return h->root.u.def.section;
13988
13989
0
    case bfd_link_hash_common:
13990
0
      return h->root.u.c.p->section;
13991
13992
0
    default:
13993
0
      return NULL;
13994
0
    }
13995
0
}
13996
13997
/* Return the debug definition section.  */
13998
13999
static asection *
14000
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14001
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14002
         struct elf_reloc_cookie *cookie,
14003
         struct elf_link_hash_entry *h,
14004
         unsigned int symndx)
14005
0
{
14006
0
  if (h != NULL)
14007
0
    {
14008
      /* Return the global debug definition section.  */
14009
0
      if ((h->root.type == bfd_link_hash_defined
14010
0
     || h->root.type == bfd_link_hash_defweak)
14011
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14012
0
  return h->root.u.def.section;
14013
0
    }
14014
0
  else
14015
0
    {
14016
      /* Return the local debug definition section.  */
14017
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14018
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14019
0
  return isec;
14020
0
    }
14021
14022
0
  return NULL;
14023
0
}
14024
14025
/* COOKIE->rel describes a relocation against section SEC, which is
14026
   a section we've decided to keep.  Return the section that contains
14027
   the relocation symbol, or NULL if no section contains it.  */
14028
14029
asection *
14030
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14031
           elf_gc_mark_hook_fn gc_mark_hook,
14032
           struct elf_reloc_cookie *cookie,
14033
           bool *start_stop)
14034
0
{
14035
0
  unsigned long r_symndx;
14036
0
  struct elf_link_hash_entry *h, *hw;
14037
14038
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14039
0
  if (r_symndx == STN_UNDEF)
14040
0
    return NULL;
14041
14042
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14043
0
  if (h == NULL)
14044
0
    {
14045
      /* A corrupt input file can lead to a situation where the index
14046
   does not reference either a local or an external symbol.  */
14047
0
      if (r_symndx >= cookie->locsymcount)
14048
0
  return NULL;
14049
14050
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14051
0
    }
14052
14053
0
  bool was_marked = h->mark;
14054
14055
0
  h->mark = 1;
14056
  /* Keep all aliases of the symbol too.  If an object symbol
14057
     needs to be copied into .dynbss then all of its aliases
14058
     should be present as dynamic symbols, not just the one used
14059
     on the copy relocation.  */
14060
0
  hw = h;
14061
0
  while (hw->is_weakalias)
14062
0
    {
14063
0
      hw = hw->u.alias;
14064
0
      hw->mark = 1;
14065
0
    }
14066
14067
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14068
0
    {
14069
0
      if (info->start_stop_gc)
14070
0
  return NULL;
14071
14072
      /* To work around a glibc bug, mark XXX input sections
14073
   when there is a reference to __start_XXX or __stop_XXX
14074
   symbols.  */
14075
0
      else if (start_stop != NULL)
14076
0
  {
14077
0
    asection *s = h->u2.start_stop_section;
14078
0
    *start_stop = true;
14079
0
    return s;
14080
0
  }
14081
0
    }
14082
14083
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14084
0
}
14085
14086
/* COOKIE->rel describes a relocation against section SEC, which is
14087
   a section we've decided to keep.  Mark the section that contains
14088
   the relocation symbol.  */
14089
14090
bool
14091
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14092
      asection *sec,
14093
      elf_gc_mark_hook_fn gc_mark_hook,
14094
      struct elf_reloc_cookie *cookie)
14095
0
{
14096
0
  asection *rsec;
14097
0
  bool start_stop = false;
14098
14099
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14100
0
  while (rsec != NULL)
14101
0
    {
14102
0
      if (!rsec->gc_mark)
14103
0
  {
14104
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14105
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14106
0
      rsec->gc_mark = 1;
14107
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14108
0
      return false;
14109
0
  }
14110
0
      if (!start_stop)
14111
0
  break;
14112
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14113
0
    }
14114
0
  return true;
14115
0
}
14116
14117
/* The mark phase of garbage collection.  For a given section, mark
14118
   it and any sections in this section's group, and all the sections
14119
   which define symbols to which it refers.  */
14120
14121
bool
14122
_bfd_elf_gc_mark (struct bfd_link_info *info,
14123
      asection *sec,
14124
      elf_gc_mark_hook_fn gc_mark_hook)
14125
0
{
14126
0
  bool ret;
14127
0
  asection *group_sec, *eh_frame, *sframe;
14128
14129
0
  sec->gc_mark = 1;
14130
14131
  /* Mark all the sections in the group.  */
14132
0
  group_sec = elf_section_data (sec)->next_in_group;
14133
0
  if (group_sec && !group_sec->gc_mark)
14134
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14135
0
      return false;
14136
14137
  /* Look through the section relocs.  */
14138
0
  ret = true;
14139
0
  eh_frame = elf_eh_frame_section (sec->owner);
14140
0
  sframe = elf_sframe_section (sec->owner);
14141
14142
0
  if ((sec->flags & SEC_RELOC) != 0
14143
0
      && sec->reloc_count > 0
14144
0
      && sec != eh_frame
14145
0
      && sec != sframe)
14146
0
    {
14147
0
      struct elf_reloc_cookie cookie;
14148
14149
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14150
0
  ret = false;
14151
0
      else
14152
0
  {
14153
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14154
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14155
0
        {
14156
0
    ret = false;
14157
0
    break;
14158
0
        }
14159
0
    fini_reloc_cookie_for_section (&cookie, sec);
14160
0
  }
14161
0
    }
14162
14163
0
  if (ret && eh_frame && elf_fde_list (sec))
14164
0
    {
14165
0
      struct elf_reloc_cookie cookie;
14166
14167
      /* NB: When --no-keep-memory is used, the symbol table and
14168
   relocation info for eh_frame are freed after they are retrieved
14169
   for each text section in the input object.  If an input object
14170
   has many text sections, the same data is retrieved and freed
14171
   many times which can take a very long time.  Always keep the
14172
   symbol table and relocation info for eh_frame to avoid it.  */
14173
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14174
0
            true))
14175
0
  ret = false;
14176
0
      else
14177
0
  {
14178
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14179
0
              gc_mark_hook, &cookie))
14180
0
      ret = false;
14181
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14182
0
  }
14183
0
    }
14184
14185
0
  eh_frame = elf_section_eh_frame_entry (sec);
14186
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14187
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14188
0
      ret = false;
14189
14190
0
  return ret;
14191
0
}
14192
14193
/* Scan and mark sections in a special or debug section group.  */
14194
14195
static void
14196
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14197
0
{
14198
  /* Point to first section of section group.  */
14199
0
  asection *ssec;
14200
  /* Used to iterate the section group.  */
14201
0
  asection *msec;
14202
14203
0
  bool is_special_grp = true;
14204
0
  bool is_debug_grp = true;
14205
14206
  /* First scan to see if group contains any section other than debug
14207
     and special section.  */
14208
0
  ssec = msec = elf_next_in_group (grp);
14209
0
  while (msec != NULL)
14210
0
    {
14211
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14212
0
  is_debug_grp = false;
14213
14214
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14215
0
  is_special_grp = false;
14216
14217
0
      msec = elf_next_in_group (msec);
14218
0
      if (msec == ssec)
14219
0
  break;
14220
0
    }
14221
14222
  /* If this is a pure debug section group or pure special section group,
14223
     keep all sections in this group.  */
14224
0
  if (is_debug_grp || is_special_grp)
14225
0
    {
14226
0
      msec = ssec;
14227
0
      while (msec != NULL)
14228
0
  {
14229
0
    msec->gc_mark = 1;
14230
0
    msec = elf_next_in_group (msec);
14231
0
    if (msec == ssec)
14232
0
      break;
14233
0
  }
14234
0
    }
14235
0
}
14236
14237
/* Keep debug and special sections.  */
14238
14239
bool
14240
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14241
         elf_gc_mark_hook_fn mark_hook)
14242
0
{
14243
0
  bfd *ibfd;
14244
14245
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14246
0
    {
14247
0
      asection *isec;
14248
0
      bool some_kept;
14249
0
      bool debug_frag_seen;
14250
0
      bool has_kept_debug_info;
14251
14252
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14253
0
  continue;
14254
0
      isec = ibfd->sections;
14255
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14256
0
  continue;
14257
14258
      /* Ensure all linker created sections are kept,
14259
   see if any other section is already marked,
14260
   and note if we have any fragmented debug sections.  */
14261
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14262
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14263
0
  {
14264
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14265
0
      isec->gc_mark = 1;
14266
0
    else if (isec->gc_mark
14267
0
       && (isec->flags & SEC_ALLOC) != 0
14268
0
       && elf_section_type (isec) != SHT_NOTE)
14269
0
      some_kept = true;
14270
0
    else
14271
0
      {
14272
        /* Since all sections, except for backend specific ones,
14273
     have been garbage collected, call mark_hook on this
14274
     section if any of its linked-to sections is marked.  */
14275
0
        asection *linked_to_sec;
14276
0
        for (linked_to_sec = elf_linked_to_section (isec);
14277
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14278
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14279
0
    {
14280
0
      if (linked_to_sec->gc_mark)
14281
0
        {
14282
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14283
0
      return false;
14284
0
          break;
14285
0
        }
14286
0
      linked_to_sec->linker_mark = 1;
14287
0
    }
14288
0
        for (linked_to_sec = elf_linked_to_section (isec);
14289
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14290
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14291
0
    linked_to_sec->linker_mark = 0;
14292
0
      }
14293
14294
0
    if (!debug_frag_seen
14295
0
        && (isec->flags & SEC_DEBUGGING)
14296
0
        && startswith (isec->name, ".debug_line."))
14297
0
      debug_frag_seen = true;
14298
0
    else if (strcmp (bfd_section_name (isec),
14299
0
         "__patchable_function_entries") == 0
14300
0
       && elf_linked_to_section (isec) == NULL)
14301
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14302
0
          "need linked-to section "
14303
0
          "for --gc-sections\n"),
14304
0
              isec->owner, isec);
14305
0
  }
14306
14307
      /* If no non-note alloc section in this file will be kept, then
14308
   we can toss out the debug and special sections.  */
14309
0
      if (!some_kept)
14310
0
  continue;
14311
14312
      /* Keep debug and special sections like .comment when they are
14313
   not part of a group.  Also keep section groups that contain
14314
   just debug sections or special sections.  NB: Sections with
14315
   linked-to section has been handled above.  */
14316
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14317
0
  {
14318
0
    if ((isec->flags & SEC_GROUP) != 0)
14319
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14320
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14321
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14322
0
       && elf_next_in_group (isec) == NULL
14323
0
       && elf_linked_to_section (isec) == NULL)
14324
0
      isec->gc_mark = 1;
14325
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14326
0
      has_kept_debug_info = true;
14327
0
  }
14328
14329
      /* Look for CODE sections which are going to be discarded,
14330
   and find and discard any fragmented debug sections which
14331
   are associated with that code section.  */
14332
0
      if (debug_frag_seen)
14333
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14334
0
    if ((isec->flags & SEC_CODE) != 0
14335
0
        && isec->gc_mark == 0)
14336
0
      {
14337
0
        unsigned int ilen;
14338
0
        asection *dsec;
14339
14340
0
        ilen = strlen (isec->name);
14341
14342
        /* Association is determined by the name of the debug
14343
     section containing the name of the code section as
14344
     a suffix.  For example .debug_line.text.foo is a
14345
     debug section associated with .text.foo.  */
14346
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14347
0
    {
14348
0
      unsigned int dlen;
14349
14350
0
      if (dsec->gc_mark == 0
14351
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14352
0
        continue;
14353
14354
0
      dlen = strlen (dsec->name);
14355
14356
0
      if (dlen > ilen
14357
0
          && strncmp (dsec->name + (dlen - ilen),
14358
0
          isec->name, ilen) == 0)
14359
0
        dsec->gc_mark = 0;
14360
0
    }
14361
0
    }
14362
14363
      /* Mark debug sections referenced by kept debug sections.  */
14364
0
      if (has_kept_debug_info)
14365
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14366
0
    if (isec->gc_mark
14367
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14368
0
      if (!_bfd_elf_gc_mark (info, isec,
14369
0
           elf_gc_mark_debug_section))
14370
0
        return false;
14371
0
    }
14372
0
  return true;
14373
0
}
14374
14375
static bool
14376
elf_gc_sweep (bfd *obfd, struct bfd_link_info *info)
14377
0
{
14378
0
  bfd *sub;
14379
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
14380
14381
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14382
0
    {
14383
0
      asection *o;
14384
14385
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14386
0
    || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14387
0
    || !obed->relocs_compatible (sub->xvec, obfd->xvec))
14388
0
  continue;
14389
0
      o = sub->sections;
14390
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14391
0
  continue;
14392
14393
0
      for (o = sub->sections; o != NULL; o = o->next)
14394
0
  {
14395
    /* When any section in a section group is kept, we keep all
14396
       sections in the section group.  If the first member of
14397
       the section group is excluded, we will also exclude the
14398
       group section.  */
14399
0
    if (o->flags & SEC_GROUP)
14400
0
      {
14401
0
        asection *first = elf_next_in_group (o);
14402
0
        if (first != NULL)
14403
0
    o->gc_mark = first->gc_mark;
14404
0
      }
14405
14406
0
    if (o->gc_mark)
14407
0
      continue;
14408
14409
    /* Skip sweeping sections already excluded.  */
14410
0
    if (o->flags & SEC_EXCLUDE)
14411
0
      continue;
14412
14413
    /* Since this is early in the link process, it is simple
14414
       to remove a section from the output.  */
14415
0
    o->flags |= SEC_EXCLUDE;
14416
14417
0
    if (info->print_gc_sections && o->size != 0)
14418
      /* xgettext:c-format */
14419
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14420
0
        o, sub);
14421
0
  }
14422
0
    }
14423
14424
0
  return true;
14425
0
}
14426
14427
/* Propagate collected vtable information.  This is called through
14428
   elf_link_hash_traverse.  */
14429
14430
static bool
14431
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14432
0
{
14433
  /* Those that are not vtables.  */
14434
0
  if (h->start_stop
14435
0
      || h->u2.vtable == NULL
14436
0
      || h->u2.vtable->parent == NULL)
14437
0
    return true;
14438
14439
  /* Those vtables that do not have parents, we cannot merge.  */
14440
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14441
0
    return true;
14442
14443
  /* If we've already been done, exit.  */
14444
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14445
0
    return true;
14446
14447
  /* Make sure the parent's table is up to date.  */
14448
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14449
14450
0
  if (h->u2.vtable->used == NULL)
14451
0
    {
14452
      /* None of this table's entries were referenced.  Re-use the
14453
   parent's table.  */
14454
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14455
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14456
0
    }
14457
0
  else
14458
0
    {
14459
0
      size_t n;
14460
0
      bool *cu, *pu;
14461
14462
      /* Or the parent's entries into ours.  */
14463
0
      cu = h->u2.vtable->used;
14464
0
      cu[-1] = true;
14465
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14466
0
      if (pu != NULL)
14467
0
  {
14468
0
    elf_backend_data *bed;
14469
0
    unsigned int log_file_align;
14470
14471
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14472
0
    log_file_align = bed->s->log_file_align;
14473
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14474
0
    while (n--)
14475
0
      {
14476
0
        if (*pu)
14477
0
    *cu = true;
14478
0
        pu++;
14479
0
        cu++;
14480
0
      }
14481
0
  }
14482
0
    }
14483
14484
0
  return true;
14485
0
}
14486
14487
struct link_info_ok
14488
{
14489
  struct bfd_link_info *info;
14490
  bool ok;
14491
};
14492
14493
static bool
14494
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14495
            void *ptr)
14496
0
{
14497
0
  asection *sec;
14498
0
  bfd_vma hstart, hend;
14499
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14500
0
  elf_backend_data *bed;
14501
0
  unsigned int log_file_align;
14502
0
  struct link_info_ok *info = ptr;
14503
14504
  /* Take care of both those symbols that do not describe vtables as
14505
     well as those that are not loaded.  */
14506
0
  if (h->start_stop
14507
0
      || h->u2.vtable == NULL
14508
0
      || h->u2.vtable->parent == NULL)
14509
0
    return true;
14510
14511
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14512
0
        || h->root.type == bfd_link_hash_defweak);
14513
14514
0
  sec = h->root.u.def.section;
14515
0
  hstart = h->root.u.def.value;
14516
0
  hend = hstart + h->size;
14517
14518
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14519
0
               sec, NULL, NULL, true);
14520
0
  if (!relstart)
14521
0
    return info->ok = false;
14522
0
  bed = get_elf_backend_data (sec->owner);
14523
0
  log_file_align = bed->s->log_file_align;
14524
14525
0
  relend = relstart + sec->reloc_count;
14526
14527
0
  for (rel = relstart; rel < relend; ++rel)
14528
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14529
0
      {
14530
  /* If the entry is in use, do nothing.  */
14531
0
  if (h->u2.vtable->used
14532
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14533
0
    {
14534
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14535
0
      if (h->u2.vtable->used[entry])
14536
0
        continue;
14537
0
    }
14538
  /* Otherwise, kill it.  */
14539
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14540
0
      }
14541
14542
0
  return true;
14543
0
}
14544
14545
/* Mark sections containing dynamically referenced symbols.  When
14546
   building shared libraries, we must assume that any visible symbol is
14547
   referenced.  */
14548
14549
bool
14550
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14551
0
{
14552
0
  struct bfd_link_info *info = inf;
14553
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14554
14555
0
  if ((h->root.type == bfd_link_hash_defined
14556
0
       || h->root.type == bfd_link_hash_defweak)
14557
0
      && (!h->start_stop
14558
0
    || h->root.ldscript_def
14559
0
    || !info->start_stop_gc)
14560
0
      && ((h->ref_dynamic && !h->forced_local)
14561
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14562
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14563
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14564
0
        && (!bfd_link_executable (info)
14565
0
      || info->gc_keep_exported
14566
0
      || info->export_dynamic
14567
0
      || (h->dynamic
14568
0
          && d != NULL
14569
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14570
0
        && (h->versioned >= versioned
14571
0
      || !bfd_hide_sym_by_version (info->version_info,
14572
0
                 h->root.root.string)))))
14573
0
    h->root.u.def.section->flags |= SEC_KEEP;
14574
14575
0
  return true;
14576
0
}
14577
14578
/* Keep all sections containing symbols undefined on the command-line,
14579
   and the section containing the entry symbol.  */
14580
14581
void
14582
_bfd_elf_gc_keep (struct bfd_link_info *info)
14583
0
{
14584
0
  struct bfd_sym_chain *sym;
14585
14586
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14587
0
    {
14588
0
      struct elf_link_hash_entry *h;
14589
14590
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14591
0
        false, false, false);
14592
14593
0
      if (h != NULL
14594
0
    && (h->root.type == bfd_link_hash_defined
14595
0
        || h->root.type == bfd_link_hash_defweak)
14596
0
    && !bfd_is_const_section (h->root.u.def.section))
14597
0
  h->root.u.def.section->flags |= SEC_KEEP;
14598
0
    }
14599
0
}
14600
14601
bool
14602
bfd_elf_parse_eh_frame_entries (struct bfd_link_info *info)
14603
0
{
14604
0
  bfd *ibfd = info->input_bfds;
14605
14606
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14607
0
    {
14608
0
      asection *sec;
14609
0
      struct elf_reloc_cookie cookie;
14610
14611
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14612
0
  continue;
14613
0
      sec = ibfd->sections;
14614
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14615
0
  continue;
14616
14617
0
      if (!init_reloc_cookie (&cookie, ibfd))
14618
0
  return false;
14619
14620
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14621
0
  {
14622
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14623
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14624
0
           false))
14625
0
      {
14626
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14627
0
        fini_reloc_cookie_rels (&cookie, sec);
14628
0
      }
14629
0
  }
14630
0
    }
14631
0
  return true;
14632
0
}
14633
14634
/* Do mark and sweep of unused sections.  */
14635
14636
bool
14637
bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
14638
0
{
14639
0
  bool ok = true;
14640
0
  bfd *sub;
14641
0
  elf_gc_mark_hook_fn gc_mark_hook;
14642
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
14643
0
  struct elf_link_hash_table *htab;
14644
0
  struct link_info_ok info_ok;
14645
14646
0
  if (!obed->can_gc_sections
14647
0
      || !is_elf_hash_table (info->hash))
14648
0
    {
14649
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14650
0
      return true;
14651
0
    }
14652
14653
0
  obed->gc_keep (info);
14654
0
  htab = elf_hash_table (info);
14655
14656
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14657
     at the .eh_frame section if we can mark the FDEs individually.  */
14658
0
  for (sub = info->input_bfds;
14659
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14660
0
       sub = sub->link.next)
14661
0
    {
14662
0
      asection *sec;
14663
0
      struct elf_reloc_cookie cookie;
14664
14665
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14666
0
  continue;
14667
0
      sec = sub->sections;
14668
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14669
0
  continue;
14670
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14671
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14672
0
               false))
14673
0
  {
14674
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14675
0
    if (sec->sec_info
14676
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14677
0
      elf_eh_frame_section (sub) = sec;
14678
0
    fini_reloc_cookie_for_section (&cookie, sec);
14679
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14680
0
  }
14681
14682
      /* Handle .sframe section.  */
14683
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14684
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14685
0
               false))
14686
0
  {
14687
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14688
14689
0
    if (sec->sec_info
14690
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14691
0
      elf_sframe_section (sub) = sec;
14692
14693
0
    fini_reloc_cookie_for_section (&cookie, sec);
14694
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14695
0
  }
14696
0
    }
14697
14698
  /* Apply transitive closure to the vtable entry usage info.  */
14699
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14700
0
  if (!ok)
14701
0
    return false;
14702
14703
  /* Kill the vtable relocations that were not used.  */
14704
0
  info_ok.info = info;
14705
0
  info_ok.ok = true;
14706
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14707
0
  if (!info_ok.ok)
14708
0
    return false;
14709
14710
  /* Mark dynamically referenced symbols.  */
14711
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14712
0
    elf_link_hash_traverse (htab, obed->gc_mark_dynamic_ref, info);
14713
14714
  /* Grovel through relocs to find out who stays ...  */
14715
0
  gc_mark_hook = obed->gc_mark_hook;
14716
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14717
0
    {
14718
0
      asection *o;
14719
14720
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14721
0
    || elf_object_id (sub) != elf_hash_table_id (htab)
14722
0
    || !obed->relocs_compatible (sub->xvec, obfd->xvec))
14723
0
  continue;
14724
14725
0
      o = sub->sections;
14726
0
      if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14727
0
  continue;
14728
14729
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14730
   Also treat note sections as a root, if the section is not part
14731
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14732
   well as FINI_ARRAY sections for ld -r.  */
14733
0
      for (o = sub->sections; o != NULL; o = o->next)
14734
0
  if (!o->gc_mark
14735
0
      && (o->flags & SEC_EXCLUDE) == 0
14736
0
      && ((o->flags & SEC_KEEP) != 0
14737
0
    || (bfd_link_relocatable (info)
14738
0
        && ((elf_section_data (o)->this_hdr.sh_type
14739
0
       == SHT_PREINIT_ARRAY)
14740
0
      || (elf_section_data (o)->this_hdr.sh_type
14741
0
          == SHT_INIT_ARRAY)
14742
0
      || (elf_section_data (o)->this_hdr.sh_type
14743
0
          == SHT_FINI_ARRAY)))
14744
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14745
0
        && elf_next_in_group (o) == NULL
14746
0
        && elf_linked_to_section (o) == NULL)
14747
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14748
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14749
0
    {
14750
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14751
0
        return false;
14752
0
    }
14753
0
    }
14754
14755
  /* Allow the backend to mark additional target specific sections.  */
14756
0
  obed->gc_mark_extra_sections (info, gc_mark_hook);
14757
14758
  /* ... and mark SEC_EXCLUDE for those that go.  */
14759
0
  return elf_gc_sweep (obfd, info);
14760
0
}
14761

14762
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14763
14764
bool
14765
bfd_elf_gc_record_vtinherit (bfd *abfd,
14766
           asection *sec,
14767
           struct elf_link_hash_entry *h,
14768
           bfd_vma offset)
14769
0
{
14770
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14771
0
  struct elf_link_hash_entry **search, *child;
14772
0
  size_t extsymcount;
14773
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14774
14775
  /* The sh_info field of the symtab header tells us where the
14776
     external symbols start.  We don't care about the local symbols at
14777
     this point.  */
14778
0
  extsymcount = elf_symtab_hdr (abfd).sh_size / bed->s->sizeof_sym;
14779
0
  if (!elf_bad_symtab (abfd))
14780
0
    extsymcount -= elf_symtab_hdr (abfd).sh_info;
14781
14782
0
  sym_hashes = elf_sym_hashes (abfd);
14783
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14784
14785
  /* Hunt down the child symbol, which is in this section at the same
14786
     offset as the relocation.  */
14787
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14788
0
    {
14789
0
      if ((child = *search) != NULL
14790
0
    && (child->root.type == bfd_link_hash_defined
14791
0
        || child->root.type == bfd_link_hash_defweak)
14792
0
    && child->root.u.def.section == sec
14793
0
    && child->root.u.def.value == offset)
14794
0
  goto win;
14795
0
    }
14796
14797
  /* xgettext:c-format */
14798
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14799
0
          abfd, sec, (uint64_t) offset);
14800
0
  bfd_set_error (bfd_error_invalid_operation);
14801
0
  return false;
14802
14803
0
 win:
14804
0
  if (!child->u2.vtable)
14805
0
    {
14806
0
      child->u2.vtable = bfd_zalloc (abfd, sizeof (*child->u2.vtable));
14807
0
      if (!child->u2.vtable)
14808
0
  return false;
14809
0
    }
14810
0
  if (!h)
14811
0
    {
14812
      /* This *should* only be the absolute section.  It could potentially
14813
   be that someone has defined a non-global vtable though, which
14814
   would be bad.  It isn't worth paging in the local symbols to be
14815
   sure though; that case should simply be handled by the assembler.  */
14816
14817
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14818
0
    }
14819
0
  else
14820
0
    child->u2.vtable->parent = h;
14821
14822
0
  return true;
14823
0
}
14824
14825
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14826
14827
bool
14828
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14829
         struct elf_link_hash_entry *h,
14830
         bfd_vma addend)
14831
0
{
14832
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14833
0
  unsigned int log_file_align = bed->s->log_file_align;
14834
14835
0
  if (!h || addend > 1u << 28)
14836
0
    {
14837
      /* xgettext:c-format */
14838
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14839
0
        abfd, sec);
14840
0
      bfd_set_error (bfd_error_bad_value);
14841
0
      return false;
14842
0
    }
14843
14844
0
  if (!h->u2.vtable)
14845
0
    {
14846
0
      h->u2.vtable = bfd_zalloc (abfd, sizeof (*h->u2.vtable));
14847
0
      if (!h->u2.vtable)
14848
0
  return false;
14849
0
    }
14850
14851
0
  if (addend >= h->u2.vtable->size)
14852
0
    {
14853
0
      size_t size, bytes, file_align;
14854
0
      bool *ptr = h->u2.vtable->used;
14855
14856
      /* While the symbol is undefined, we have to be prepared to handle
14857
   a zero size.  */
14858
0
      file_align = 1 << log_file_align;
14859
0
      if (h->root.type == bfd_link_hash_undefined)
14860
0
  size = addend + file_align;
14861
0
      else
14862
0
  {
14863
0
    size = h->size;
14864
0
    if (addend >= size)
14865
0
      {
14866
        /* Oops!  We've got a reference past the defined end of
14867
     the table.  This is probably a bug -- shall we warn?  */
14868
0
        size = addend + file_align;
14869
0
      }
14870
0
  }
14871
0
      size = (size + file_align - 1) & -file_align;
14872
14873
      /* Allocate one extra entry for use as a "done" flag for the
14874
   consolidation pass.  */
14875
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14876
14877
0
      if (ptr)
14878
0
  {
14879
0
    ptr = bfd_realloc (ptr - 1, bytes);
14880
14881
0
    if (ptr != NULL)
14882
0
      {
14883
0
        size_t oldbytes;
14884
14885
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14886
0
        * sizeof (bool));
14887
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14888
0
      }
14889
0
  }
14890
0
      else
14891
0
  ptr = bfd_zmalloc (bytes);
14892
14893
0
      if (ptr == NULL)
14894
0
  return false;
14895
14896
      /* And arrange for that done flag to be at index -1.  */
14897
0
      h->u2.vtable->used = ptr + 1;
14898
0
      h->u2.vtable->size = size;
14899
0
    }
14900
14901
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14902
14903
0
  return true;
14904
0
}
14905
14906
/* Map an ELF section header flag to its corresponding string.  */
14907
typedef struct
14908
{
14909
  char *flag_name;
14910
  flagword flag_value;
14911
} elf_flags_to_name_table;
14912
14913
static const elf_flags_to_name_table elf_flags_to_names [] =
14914
{
14915
  { "SHF_WRITE", SHF_WRITE },
14916
  { "SHF_ALLOC", SHF_ALLOC },
14917
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14918
  { "SHF_MERGE", SHF_MERGE },
14919
  { "SHF_STRINGS", SHF_STRINGS },
14920
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14921
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14922
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14923
  { "SHF_GROUP", SHF_GROUP },
14924
  { "SHF_TLS", SHF_TLS },
14925
  { "SHF_MASKOS", SHF_MASKOS },
14926
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14927
};
14928
14929
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14930
bool
14931
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14932
            struct flag_info *flaginfo,
14933
            asection *section)
14934
0
{
14935
0
  const bfd_vma sh_flags = elf_section_flags (section);
14936
14937
0
  if (!flaginfo->flags_initialized)
14938
0
    {
14939
0
      bfd *obfd = info->output_bfd;
14940
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
14941
0
      struct flag_info_list *tf = flaginfo->flag_list;
14942
0
      int with_hex = 0;
14943
0
      int without_hex = 0;
14944
14945
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14946
0
  {
14947
0
    unsigned i;
14948
0
    flagword (*lookup) (char *);
14949
14950
0
    lookup = obed->elf_backend_lookup_section_flags_hook;
14951
0
    if (lookup != NULL)
14952
0
      {
14953
0
        flagword hexval = (*lookup) ((char *) tf->name);
14954
14955
0
        if (hexval != 0)
14956
0
    {
14957
0
      if (tf->with == with_flags)
14958
0
        with_hex |= hexval;
14959
0
      else if (tf->with == without_flags)
14960
0
        without_hex |= hexval;
14961
0
      tf->valid = true;
14962
0
      continue;
14963
0
    }
14964
0
      }
14965
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14966
0
      {
14967
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14968
0
    {
14969
0
      if (tf->with == with_flags)
14970
0
        with_hex |= elf_flags_to_names[i].flag_value;
14971
0
      else if (tf->with == without_flags)
14972
0
        without_hex |= elf_flags_to_names[i].flag_value;
14973
0
      tf->valid = true;
14974
0
      break;
14975
0
    }
14976
0
      }
14977
0
    if (!tf->valid)
14978
0
      {
14979
0
        info->callbacks->einfo
14980
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14981
0
        return false;
14982
0
      }
14983
0
  }
14984
0
      flaginfo->flags_initialized = true;
14985
0
      flaginfo->only_with_flags |= with_hex;
14986
0
      flaginfo->not_with_flags |= without_hex;
14987
0
    }
14988
14989
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14990
0
    return false;
14991
14992
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
14993
0
    return false;
14994
14995
0
  return true;
14996
0
}
14997
14998
struct alloc_got_off_arg {
14999
  bfd_vma gotoff;
15000
  struct bfd_link_info *info;
15001
};
15002
15003
/* We need a special top-level link routine to convert got reference counts
15004
   to real got offsets.  */
15005
15006
static bool
15007
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15008
0
{
15009
0
  struct alloc_got_off_arg *gofarg = arg;
15010
0
  bfd *obfd = gofarg->info->output_bfd;
15011
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15012
15013
0
  if (h->got.refcount > 0)
15014
0
    {
15015
0
      h->got.offset = gofarg->gotoff;
15016
0
      gofarg->gotoff += obed->got_elt_size (gofarg->info, h, NULL, 0);
15017
0
    }
15018
0
  else
15019
0
    h->got.offset = (bfd_vma) -1;
15020
15021
0
  return true;
15022
0
}
15023
15024
/* And an accompanying bit to work out final got entry offsets once
15025
   we're done.  Should be called from final_link.  */
15026
15027
static bool
15028
bfd_elf_gc_common_finalize_got_offsets (bfd *obfd,
15029
          struct bfd_link_info *info)
15030
0
{
15031
0
  bfd *i;
15032
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15033
0
  bfd_vma gotoff;
15034
0
  struct alloc_got_off_arg gofarg;
15035
15036
0
  BFD_ASSERT (obfd == info->output_bfd);
15037
15038
0
  if (! is_elf_hash_table (info->hash))
15039
0
    return false;
15040
15041
  /* The GOT offset is relative to the .got section, but the GOT header is
15042
     put into the .got.plt section, if the backend uses it.  */
15043
0
  if (obed->want_got_plt)
15044
0
    gotoff = 0;
15045
0
  else
15046
0
    gotoff = obed->got_header_size;
15047
15048
  /* Do the local .got entries first.  */
15049
0
  for (i = info->input_bfds; i; i = i->link.next)
15050
0
    {
15051
0
      bfd_signed_vma *local_got;
15052
0
      size_t j, locsymcount;
15053
0
      Elf_Internal_Shdr *symtab_hdr;
15054
15055
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15056
0
  continue;
15057
15058
0
      local_got = elf_local_got_refcounts (i);
15059
0
      if (!local_got)
15060
0
  continue;
15061
15062
0
      symtab_hdr = &elf_symtab_hdr (i);
15063
0
      if (elf_bad_symtab (i))
15064
0
  locsymcount = symtab_hdr->sh_size / obed->s->sizeof_sym;
15065
0
      else
15066
0
  locsymcount = symtab_hdr->sh_info;
15067
15068
0
      for (j = 0; j < locsymcount; ++j)
15069
0
  {
15070
0
    if (local_got[j] > 0)
15071
0
      {
15072
0
        local_got[j] = gotoff;
15073
0
        gotoff += obed->got_elt_size (info, NULL, i, j);
15074
0
      }
15075
0
    else
15076
0
      local_got[j] = (bfd_vma) -1;
15077
0
  }
15078
0
    }
15079
15080
  /* Then the global .got entries.  .plt refcounts are handled by
15081
     adjust_dynamic_symbol  */
15082
0
  gofarg.gotoff = gotoff;
15083
0
  gofarg.info = info;
15084
0
  elf_link_hash_traverse (elf_hash_table (info),
15085
0
        elf_gc_allocate_got_offsets,
15086
0
        &gofarg);
15087
0
  return true;
15088
0
}
15089
15090
/* Many folk need no more in the way of final link than this, once
15091
   got entry reference counting is enabled.  */
15092
15093
bool
15094
_bfd_elf_gc_common_final_link (bfd *obfd, struct bfd_link_info *info)
15095
0
{
15096
0
  if (!bfd_elf_gc_common_finalize_got_offsets (obfd, info))
15097
0
    return false;
15098
15099
  /* Invoke the regular ELF backend linker to do all the work.  */
15100
0
  return _bfd_elf_final_link (obfd, info);
15101
0
}
15102
15103
bool
15104
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15105
0
{
15106
0
  struct elf_reloc_cookie *rcookie = cookie;
15107
15108
0
  if (elf_bad_symtab (rcookie->abfd))
15109
0
    rcookie->rel = rcookie->rels;
15110
15111
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15112
0
    {
15113
0
      unsigned long r_symndx;
15114
15115
0
      if (!elf_bad_symtab (rcookie->abfd)
15116
0
    && rcookie->rel->r_offset > offset)
15117
0
  return false;
15118
0
      if (rcookie->rel->r_offset != offset)
15119
0
  continue;
15120
15121
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15122
0
      if (r_symndx == STN_UNDEF)
15123
0
  return true;
15124
15125
0
      struct elf_link_hash_entry *h;
15126
15127
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15128
15129
0
      if (h != NULL)
15130
0
  {
15131
0
    if ((h->root.type == bfd_link_hash_defined
15132
0
         || h->root.type == bfd_link_hash_defweak)
15133
0
        && (h->root.u.def.section->owner != rcookie->abfd
15134
0
      || h->root.u.def.section->kept_section != NULL
15135
0
      || discarded_section (h->root.u.def.section)))
15136
0
      return true;
15137
0
  }
15138
0
      else
15139
0
  {
15140
0
    if (r_symndx >= rcookie->locsymcount)
15141
      /* This can happen with corrupt input.  */
15142
0
      return false;
15143
15144
    /* It's not a relocation against a global symbol,
15145
       but it could be a relocation against a local
15146
       symbol for a discarded section.  */
15147
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15148
0
    if (isec != NULL
15149
0
        && (isec->kept_section != NULL
15150
0
      || discarded_section (isec)))
15151
0
      return true;
15152
0
  }
15153
15154
0
      return false;
15155
0
    }
15156
0
  return false;
15157
0
}
15158
15159
/* Discard unneeded references to discarded sections.
15160
   Returns -1 on error, 1 if any section's size was changed, 0 if
15161
   nothing changed.  This function assumes that the relocations are in
15162
   sorted order, which is true for all known assemblers.  */
15163
15164
int
15165
bfd_elf_discard_info (struct bfd_link_info *info)
15166
0
{
15167
0
  struct elf_reloc_cookie cookie;
15168
0
  asection *o;
15169
0
  bfd *abfd;
15170
0
  int changed = 0;
15171
15172
0
  if (info->traditional_format
15173
0
      || !is_elf_hash_table (info->hash))
15174
0
    return 0;
15175
15176
0
  o = bfd_get_section_by_name (info->output_bfd, ".stab");
15177
0
  if (o != NULL)
15178
0
    {
15179
0
      asection *i;
15180
15181
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15182
0
  {
15183
0
    if (i->size == 0
15184
0
        || i->reloc_count == 0
15185
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15186
0
      continue;
15187
15188
0
    abfd = i->owner;
15189
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15190
0
      continue;
15191
15192
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15193
0
      return -1;
15194
15195
0
    if (_bfd_discard_section_stabs (abfd, i,
15196
0
            bfd_elf_reloc_symbol_deleted_p,
15197
0
            &cookie))
15198
0
      changed = 1;
15199
15200
0
    fini_reloc_cookie_for_section (&cookie, i);
15201
0
  }
15202
0
    }
15203
15204
0
  o = NULL;
15205
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15206
0
    o = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
15207
0
  if (o != NULL)
15208
0
    {
15209
0
      asection *i;
15210
0
      int eh_changed = 0;
15211
0
      unsigned int eh_alignment;  /* Octets.  */
15212
15213
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15214
0
  {
15215
0
    int r;
15216
15217
0
    if (i->size == 0)
15218
0
      continue;
15219
15220
0
    abfd = i->owner;
15221
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15222
0
      continue;
15223
15224
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15225
0
      return -1;
15226
15227
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15228
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15229
0
             bfd_elf_reloc_symbol_deleted_p,
15230
0
             &cookie);
15231
0
    if (r)
15232
0
      {
15233
0
        eh_changed = 1;
15234
0
        if (r >= 2)
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 (info->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 (info->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 (info->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
      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, abfd))
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 (struct bfd_link_info *info,
15514
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15515
             bfd *ibfd ATTRIBUTE_UNUSED,
15516
             unsigned long symndx ATTRIBUTE_UNUSED)
15517
0
{
15518
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15519
0
  return obed->s->arch_size / 8;
15520
0
}
15521
15522
/* Routines to support the creation of dynamic relocs.  */
15523
15524
/* Returns the name of the dynamic reloc section associated with SEC.  */
15525
15526
static const char *
15527
get_dynamic_reloc_section_name (bfd *       abfd,
15528
        asection *  sec,
15529
        bool is_rela)
15530
0
{
15531
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15532
0
  size_t plen = is_rela ? 5 : 4;
15533
0
  const char *old_name = bfd_section_name (sec);
15534
0
  size_t nlen = strlen (old_name);
15535
0
  char *name = bfd_alloc (abfd, plen + nlen + 1);
15536
0
  if (name == NULL)
15537
0
    return NULL;
15538
15539
0
  memcpy (name, prefix, plen);
15540
0
  memcpy (name + plen, old_name, nlen + 1);
15541
0
  return name;
15542
0
}
15543
15544
/* Returns the dynamic reloc section associated with SEC.  If the
15545
   section does not exist it is created and attached to the DYNOBJ
15546
   bfd and stored in the SRELOC field of SEC's elf_section_data
15547
   structure.
15548
15549
   ALIGNMENT is the alignment for the newly created section and
15550
   IS_RELA defines whether the name should be .rela.<SEC's name>
15551
   or .rel.<SEC's name>.  The section name is looked up in the
15552
   string table associated with ABFD.  */
15553
15554
asection *
15555
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15556
             bfd *dynobj,
15557
             unsigned int alignment,
15558
             bfd *abfd,
15559
             bool is_rela)
15560
0
{
15561
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15562
15563
0
  if (reloc_sec == NULL)
15564
0
    {
15565
0
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15566
15567
0
      if (name == NULL)
15568
0
  return NULL;
15569
15570
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15571
15572
0
      if (reloc_sec == NULL)
15573
0
  {
15574
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15575
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15576
0
    if ((sec->flags & SEC_ALLOC) != 0)
15577
0
      flags |= SEC_ALLOC | SEC_LOAD;
15578
15579
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15580
0
    if (reloc_sec != NULL)
15581
0
      {
15582
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15583
     name.  Override as it may be wrong, eg. for a user
15584
     section named "auto" we'll get ".relauto" which is
15585
     seen to be a .rela section.  */
15586
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15587
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15588
0
    reloc_sec = NULL;
15589
0
      }
15590
0
  }
15591
15592
0
      elf_section_data (sec)->sreloc = reloc_sec;
15593
0
    }
15594
15595
0
  return reloc_sec;
15596
0
}
15597
15598
/* Copy the ELF symbol type and other attributes for a linker script
15599
   assignment from HSRC to HDEST.  Generally this should be treated as
15600
   if we found a strong non-dynamic definition for HDEST (except that
15601
   ld ignores multiple definition errors).  */
15602
void
15603
_bfd_elf_copy_link_hash_symbol_type (bfd *obfd,
15604
             struct bfd_link_hash_entry *hdest,
15605
             struct bfd_link_hash_entry *hsrc)
15606
0
{
15607
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15608
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15609
15610
0
  ehdest->type = ehsrc->type;
15611
0
  ehdest->target_internal = ehsrc->target_internal;
15612
15613
0
  elf_merge_st_other (obfd, obfd, ehdest, ehsrc->other, NULL,
15614
0
          true, false);
15615
0
}
15616
15617
/* Append a RELA relocation REL to section S.  */
15618
15619
void
15620
_bfd_elf_append_rela (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15621
0
{
15622
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15623
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rela);
15624
0
  BFD_ASSERT (loc + obed->s->sizeof_rela <= s->contents + s->size);
15625
0
  obed->s->swap_reloca_out (obfd, rel, loc);
15626
0
}
15627
15628
/* Append a REL relocation REL to section S.  */
15629
15630
void
15631
_bfd_elf_append_rel (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15632
0
{
15633
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15634
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rel);
15635
0
  BFD_ASSERT (loc + obed->s->sizeof_rel <= s->contents + s->size);
15636
0
  obed->s->swap_reloc_out (obfd, rel, loc);
15637
0
}
15638
15639
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15640
15641
struct bfd_link_hash_entry *
15642
bfd_elf_define_start_stop (struct bfd_link_info *info,
15643
         const char *symbol, asection *sec)
15644
0
{
15645
0
  struct elf_link_hash_entry *h;
15646
15647
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15648
0
          false, false, true);
15649
  /* NB: Common symbols will be turned into definition later.  */
15650
0
  if (h != NULL
15651
0
      && !h->root.ldscript_def
15652
0
      && (h->root.type == bfd_link_hash_undefined
15653
0
    || h->root.type == bfd_link_hash_undefweak
15654
0
    || ((h->ref_regular || h->def_dynamic)
15655
0
        && !h->def_regular
15656
0
        && h->root.type != bfd_link_hash_common)))
15657
0
    {
15658
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15659
0
      h->verinfo.verdef = NULL;
15660
0
      h->root.type = bfd_link_hash_defined;
15661
0
      h->root.u.def.section = sec;
15662
0
      h->root.u.def.value = 0;
15663
0
      h->def_regular = 1;
15664
0
      h->def_dynamic = 0;
15665
0
      h->start_stop = 1;
15666
0
      h->u2.start_stop_section = sec;
15667
0
      if (symbol[0] == '.')
15668
0
  {
15669
    /* .startof. and .sizeof. symbols are local.  */
15670
0
    elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15671
0
    obed->elf_backend_hide_symbol (info, h, true);
15672
0
  }
15673
0
      else
15674
0
  {
15675
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15676
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15677
0
      | info->start_stop_visibility);
15678
0
    if (was_dynamic)
15679
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15680
0
  }
15681
0
      return &h->root;
15682
0
    }
15683
0
  return NULL;
15684
0
}
15685
15686
/* Find dynamic relocs for H that apply to read-only sections.  */
15687
15688
asection *
15689
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15690
0
{
15691
0
  struct elf_dyn_relocs *p;
15692
15693
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15694
0
    {
15695
0
      asection *s = p->sec->output_section;
15696
15697
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15698
0
  return p->sec;
15699
0
    }
15700
0
  return NULL;
15701
0
}
15702
15703
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15704
   read-only sections.  */
15705
15706
bool
15707
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15708
0
{
15709
0
  asection *sec;
15710
15711
0
  if (h->root.type == bfd_link_hash_indirect)
15712
0
    return true;
15713
15714
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15715
0
  if (sec != NULL)
15716
0
    {
15717
0
      struct bfd_link_info *info = inf;
15718
15719
0
      info->flags |= DF_TEXTREL;
15720
      /* xgettext:c-format */
15721
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15722
0
        "in read-only section `%pA'\n"),
15723
0
            sec->owner, h->root.root.string, sec);
15724
15725
0
      if (bfd_link_textrel_check (info))
15726
  /* xgettext:c-format */
15727
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15728
0
          "in read-only section `%pA'\n"),
15729
0
        sec->owner, h->root.root.string, sec);
15730
15731
      /* Not an error, just cut short the traversal.  */
15732
0
      return false;
15733
0
    }
15734
0
  return true;
15735
0
}
15736
15737
/* Add dynamic tags.  */
15738
15739
bool
15740
_bfd_elf_add_dynamic_tags (struct bfd_link_info *info, bool need_dynamic_reloc)
15741
0
{
15742
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15743
15744
0
  if (htab->dynamic_sections_created)
15745
0
    {
15746
      /* Add some entries to the .dynamic section.  We fill in the
15747
   values later, in finish_dynamic_sections, but we must add
15748
   the entries now so that we get the correct size for the
15749
   .dynamic section.  The DT_DEBUG entry is filled in by the
15750
   dynamic linker and used by the debugger.  */
15751
0
#define add_dynamic_entry(TAG, VAL) \
15752
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15753
15754
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15755
15756
0
      if (bfd_link_executable (info))
15757
0
  {
15758
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15759
0
      return false;
15760
0
  }
15761
15762
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15763
0
  {
15764
    /* DT_PLTGOT is used by prelink even if there is no PLT
15765
       relocation.  */
15766
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15767
0
      return false;
15768
0
  }
15769
15770
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15771
0
  {
15772
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15773
0
        || !add_dynamic_entry (DT_PLTREL,
15774
0
             (obed->rela_plts_and_copies_p
15775
0
              ? DT_RELA : DT_REL))
15776
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15777
0
      return false;
15778
0
  }
15779
15780
0
      if (htab->tlsdesc_plt
15781
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15782
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15783
0
  return false;
15784
15785
0
      if (need_dynamic_reloc)
15786
0
  {
15787
0
    if (obed->rela_plts_and_copies_p)
15788
0
      {
15789
0
        if (!add_dynamic_entry (DT_RELA, 0)
15790
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15791
0
      || !add_dynamic_entry (DT_RELAENT,
15792
0
           obed->s->sizeof_rela))
15793
0
    return false;
15794
0
      }
15795
0
    else
15796
0
      {
15797
0
        if (!add_dynamic_entry (DT_REL, 0)
15798
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15799
0
      || !add_dynamic_entry (DT_RELENT,
15800
0
           obed->s->sizeof_rel))
15801
0
    return false;
15802
0
      }
15803
15804
    /* If any dynamic relocs apply to a read-only section,
15805
       then we need a DT_TEXTREL entry.  */
15806
0
    if ((info->flags & DF_TEXTREL) == 0)
15807
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15808
0
            info);
15809
15810
0
    if ((info->flags & DF_TEXTREL) != 0)
15811
0
      {
15812
0
        if (htab->ifunc_resolvers)
15813
0
    info->callbacks->einfo
15814
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15815
0
         "may result in a segfault at runtime; recompile with %s\n"),
15816
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15817
15818
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15819
0
    return false;
15820
0
      }
15821
0
  }
15822
0
    }
15823
0
#undef add_dynamic_entry
15824
15825
0
  return true;
15826
0
}