Coverage Report

Created: 2026-09-14 08:07

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
/* Return true if IBFD is compatible with the output.  Used for example
287
   to decide whether an object file can hold linker generated dynamic
288
   sections, and whether object file relocations can be examined by
289
   check_relocs (for plt/got/dyn reloc generation).  */
290
291
static bool
292
compatible_format (struct bfd_link_info *info, bfd *ibfd)
293
0
{
294
0
  return (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
295
0
    && is_elf_hash_table (info->hash)
296
0
    && elf_object_id (ibfd) == elf_hash_table_id (elf_hash_table (info))
297
0
    && !(ibfd->sections != NULL
298
0
         && ibfd->sections->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
299
0
    && (get_elf_backend_data (ibfd)
300
0
        ->relocs_compatible (ibfd->xvec, info->output_bfd->xvec)));
301
0
}
302
303
/* Find a suitable object for attaching dynamic sections.  */
304
305
bfd *
306
_bfd_elf_link_dynobj_internal (struct bfd_link_info *info, bool report)
307
0
{
308
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
309
310
0
  if (htab->dynobj == NULL)
311
0
    {
312
      /* We need to find an input file of the same format as the
313
   output to hold linker created sections.  */
314
0
      bfd *ibfd;
315
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
316
0
  if (compatible_format (info, ibfd))
317
0
    break;
318
0
      if (ibfd == NULL && report)
319
0
  _bfd_error_handler (_("no %s input object found"),
320
0
          bfd_get_target (info->output_bfd));
321
0
      htab->dynobj = ibfd;
322
0
    }
323
324
0
  return htab->dynobj;
325
0
}
326
327
/* Return the strtab for dynamic symbol names.  Create it if
328
   necessary.  */
329
330
static struct elf_strtab_hash *
331
_bfd_elf_link_dynstr (struct bfd_link_info *info)
332
0
{
333
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
334
335
0
  if (htab->dynstr == NULL)
336
0
    htab->dynstr = _bfd_elf_strtab_init ();
337
0
  return htab->dynstr;
338
0
}
339
340
/* Create some sections which will be filled in with dynamic linking
341
   information.  The dynamic sections take up virtual memory space
342
   when the final executable is run, so we need to create them before
343
   addresses are assigned to the output sections.  We work out the
344
   actual contents and size of these sections later.  */
345
346
bool
347
bfd_elf_link_create_dynamic_sections (struct bfd_link_info *info)
348
0
{
349
0
  flagword flags;
350
0
  asection *s;
351
0
  bfd *dynobj;
352
0
  elf_backend_data *obed;
353
0
  struct elf_link_hash_entry *h;
354
355
0
  if (! is_elf_hash_table (info->hash))
356
0
    return false;
357
358
0
  if (elf_hash_table (info)->dynamic_sections_created)
359
0
    return true;
360
361
0
  if (!_bfd_elf_link_dynstr (info))
362
0
    return false;
363
364
0
  dynobj = _bfd_elf_link_dynobj (info);
365
0
  if (dynobj == NULL)
366
0
    return false;
367
0
  obed = get_elf_backend_data (dynobj);
368
369
0
  flags = obed->dynamic_sec_flags;
370
371
  /* A dynamically linked executable has a .interp section, but a
372
     shared library does not.  */
373
0
  if (bfd_link_executable (info) && !info->nointerp)
374
0
    {
375
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".interp",
376
0
                flags | SEC_READONLY);
377
0
      if (s == NULL)
378
0
  return false;
379
0
      elf_hash_table (info)->interp = s;
380
0
    }
381
382
  /* Create sections to hold version informations.  These are removed
383
     if they are not needed.  */
384
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_d",
385
0
            flags | SEC_READONLY);
386
0
  if (s == NULL
387
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
388
0
    return false;
389
390
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version",
391
0
            flags | SEC_READONLY);
392
0
  if (s == NULL
393
0
      || !bfd_set_section_alignment (s, 1))
394
0
    return false;
395
396
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_r",
397
0
            flags | SEC_READONLY);
398
0
  if (s == NULL
399
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
400
0
    return false;
401
402
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynsym",
403
0
            flags | SEC_READONLY);
404
0
  if (s == NULL
405
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
406
0
    return false;
407
0
  elf_hash_table (info)->dynsym = s;
408
409
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynstr",
410
0
            flags | SEC_READONLY);
411
0
  if (s == NULL)
412
0
    return false;
413
414
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynamic", flags);
415
0
  if (s == NULL
416
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
417
0
    return false;
418
0
  elf_hash_table (info)->dynamic = s;
419
420
  /* The special symbol _DYNAMIC is always set to the start of the
421
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
422
     only want to define it if we are, in fact, creating a .dynamic
423
     section.  We don't want to define it if there is no .dynamic
424
     section, since on some ELF platforms the start up code examines it
425
     to decide how to initialize the process.  */
426
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, s, "_DYNAMIC");
427
0
  elf_hash_table (info)->hdynamic = h;
428
0
  if (h == NULL)
429
0
    return false;
430
431
0
  if (info->emit_hash)
432
0
    {
433
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".hash",
434
0
                flags | SEC_READONLY);
435
0
      if (s == NULL
436
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
437
0
  return false;
438
0
      elf_section_data (s)->this_hdr.sh_entsize = obed->s->sizeof_hash_entry;
439
0
    }
440
441
0
  if (info->emit_gnu_hash && obed->record_xhash_symbol == NULL)
442
0
    {
443
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.hash",
444
0
                flags | SEC_READONLY);
445
0
      if (s == NULL
446
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
447
0
  return false;
448
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
449
   4 32-bit words followed by variable count of 64-bit words, then
450
   variable count of 32-bit words.  */
451
0
      if (obed->s->arch_size == 64)
452
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
453
0
      else
454
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
455
0
    }
456
457
0
  if (info->enable_dt_relr)
458
0
    {
459
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".relr.dyn",
460
0
                flags | SEC_READONLY);
461
0
      if (s == NULL
462
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
463
0
  return false;
464
0
      elf_hash_table (info)->srelrdyn = s;
465
0
    }
466
467
  /* Let the backend create the rest of the sections.  This lets the
468
     backend set the right flags.  The backend will normally create
469
     the .got and .plt sections.  */
470
0
  if (obed->elf_backend_create_dynamic_sections == NULL
471
0
      || !obed->elf_backend_create_dynamic_sections (dynobj, info))
472
0
    return false;
473
474
0
  elf_hash_table (info)->dynamic_sections_created = true;
475
476
0
  return true;
477
0
}
478
479
/* Create dynamic sections when linking against a dynamic object.  */
480
481
bool
482
_bfd_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
483
0
{
484
0
  flagword flags, pltflags;
485
0
  struct elf_link_hash_entry *h;
486
0
  asection *s;
487
0
  elf_backend_data *obed = get_elf_backend_data (dynobj);
488
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
489
490
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
491
     .rel[a].bss sections.  */
492
0
  flags = obed->dynamic_sec_flags;
493
494
0
  pltflags = flags;
495
0
  if (obed->plt_not_loaded)
496
    /* We do not clear SEC_ALLOC here because we still want the OS to
497
       allocate space for the section; it's just that there's nothing
498
       to read in from the object file.  */
499
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
500
0
  else
501
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
502
0
  if (obed->plt_readonly)
503
0
    pltflags |= SEC_READONLY;
504
505
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".plt", pltflags);
506
0
  if (s == NULL
507
0
      || !bfd_set_section_alignment (s, obed->plt_alignment))
508
0
    return false;
509
0
  htab->splt = s;
510
511
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
512
     .plt section.  */
513
0
  if (obed->want_plt_sym)
514
0
    {
515
0
      h = _bfd_elf_define_linkage_sym (dynobj, info, s,
516
0
               "_PROCEDURE_LINKAGE_TABLE_");
517
0
      elf_hash_table (info)->hplt = h;
518
0
      if (h == NULL)
519
0
  return false;
520
0
    }
521
522
0
  s = bfd_make_section_anyway_with_flags (dynobj,
523
0
            (obed->rela_plts_and_copies_p
524
0
             ? ".rela.plt" : ".rel.plt"),
525
0
            flags | SEC_READONLY);
526
0
  if (s == NULL
527
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
528
0
    return false;
529
0
  htab->srelplt = s;
530
531
0
  if (! _bfd_elf_create_got_section (dynobj, info))
532
0
    return false;
533
534
0
  if (obed->want_dynbss)
535
0
    {
536
      /* The .dynbss section is a place to put symbols which are defined
537
   by dynamic objects, are referenced by regular objects, and are
538
   not functions.  We must allocate space for them in the process
539
   image and use a R_*_COPY reloc to tell the dynamic linker to
540
   initialize them at run time.  The linker script puts the .dynbss
541
   section into the .bss section of the final image.  */
542
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".dynbss",
543
0
                SEC_ALLOC | SEC_LINKER_CREATED);
544
0
      if (s == NULL)
545
0
  return false;
546
0
      htab->sdynbss = s;
547
548
0
      if (obed->want_dynrelro)
549
0
  {
550
    /* Similarly, but for symbols that were originally in read-only
551
       sections.  This section doesn't really need to have contents,
552
       but make it like other .data.rel.ro sections.  */
553
0
    s = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro",
554
0
              flags);
555
0
    if (s == NULL)
556
0
      return false;
557
0
    htab->sdynrelro = s;
558
0
  }
559
560
      /* The .rel[a].bss section holds copy relocs.  This section is not
561
   normally needed.  We need to create it here, though, so that the
562
   linker will map it to an output section.  We can't just create it
563
   only if we need it, because we will not know whether we need it
564
   until we have seen all the input files, and the first time the
565
   main linker code calls BFD after examining all the input files
566
   (size_dynamic_sections) the input sections have already been
567
   mapped to the output sections.  If the section turns out not to
568
   be needed, we can discard it later.  We will never need this
569
   section when generating a shared object, since they do not use
570
   copy relocs.  */
571
0
      if (bfd_link_executable (info))
572
0
  {
573
0
    s = bfd_make_section_anyway_with_flags (dynobj,
574
0
              (obed->rela_plts_and_copies_p
575
0
               ? ".rela.bss" : ".rel.bss"),
576
0
              flags | SEC_READONLY);
577
0
    if (s == NULL
578
0
        || !bfd_set_section_alignment (s, obed->s->log_file_align))
579
0
      return false;
580
0
    htab->srelbss = s;
581
582
0
    if (obed->want_dynrelro)
583
0
      {
584
0
        s = (bfd_make_section_anyway_with_flags
585
0
       (dynobj, (obed->rela_plts_and_copies_p
586
0
           ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
587
0
        flags | SEC_READONLY));
588
0
        if (s == NULL
589
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
590
0
    return false;
591
0
        htab->sreldynrelro = s;
592
0
      }
593
0
  }
594
0
    }
595
596
0
  return true;
597
0
}
598

599
/* Record a new dynamic symbol.  We record the dynamic symbols as we
600
   read the input files, since we need to have a list of all of them
601
   before we can determine the final sizes of the output sections.
602
   Note that we may actually call this function even though we are not
603
   going to output any dynamic symbols; in some cases we know that a
604
   symbol should be in the dynamic symbol table, but only if there is
605
   one.  */
606
607
bool
608
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
609
            struct elf_link_hash_entry *h)
610
0
{
611
0
  if (h->dynindx == -1)
612
0
    {
613
0
      struct elf_strtab_hash *dynstr;
614
0
      const char *p;
615
0
      const char *name;
616
0
      size_t indx;
617
618
0
      if (h->root.type == bfd_link_hash_defined
619
0
    || h->root.type == bfd_link_hash_defweak)
620
0
  {
621
    /* An IR symbol should not be made dynamic.  */
622
0
    if (h->root.u.def.section != NULL
623
0
        && h->root.u.def.section->owner != NULL
624
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
625
0
      return true;
626
0
  }
627
628
      /* XXX: The ABI draft says the linker must turn hidden and
629
   internal symbols into STB_LOCAL symbols when producing the
630
   DSO. However, if ld.so honors st_other in the dynamic table,
631
   this would not be necessary.  */
632
0
      switch (ELF_ST_VISIBILITY (h->other))
633
0
  {
634
0
  case STV_INTERNAL:
635
0
  case STV_HIDDEN:
636
0
    if (h->root.type != bfd_link_hash_undefined
637
0
        && h->root.type != bfd_link_hash_undefweak)
638
0
      {
639
0
        h->forced_local = 1;
640
0
        return true;
641
0
      }
642
643
0
  default:
644
0
    break;
645
0
  }
646
647
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
648
0
      if (h->forced_local)
649
0
  elf_hash_table (info)->has_local_dynsyms = true;
650
0
      ++elf_hash_table (info)->dynsymcount;
651
652
0
      dynstr = _bfd_elf_link_dynstr (info);
653
0
      if (dynstr == NULL)
654
0
  return false;
655
656
0
      char *unversioned_name = NULL;
657
658
      /* We don't put any version information in the dynamic string
659
   table.  */
660
0
      name = h->root.root.string;
661
0
      p = strchr (name, ELF_VER_CHR);
662
0
      if (p != NULL)
663
0
  {
664
0
    unversioned_name = bfd_malloc (p - name + 1);
665
0
    if (unversioned_name == NULL)
666
0
      return false;
667
0
    memcpy (unversioned_name, name, p - name);
668
0
    unversioned_name[p - name] = 0;
669
0
    name = unversioned_name;
670
0
  }
671
672
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
673
674
0
      if (p != NULL)
675
0
  free (unversioned_name);
676
677
0
      if (indx == (size_t) -1)
678
0
  return false;
679
0
      h->dynstr_index = indx;
680
0
    }
681
682
0
  return true;
683
0
}
684

685
/* Mark a symbol dynamic.  */
686
687
static void
688
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
689
          struct elf_link_hash_entry *h,
690
          Elf_Internal_Sym *sym)
691
0
{
692
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
693
694
  /* It may be called more than once on the same H.  */
695
0
  if(h->dynamic || bfd_link_relocatable (info))
696
0
    return;
697
698
0
  if ((info->dynamic_data
699
0
       && (h->type == STT_OBJECT
700
0
     || h->type == STT_COMMON
701
0
     || (sym != NULL
702
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
703
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
704
0
      || (d != NULL
705
0
    && h->non_elf
706
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
707
0
    {
708
0
      h->dynamic = 1;
709
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
710
   non-IR reference.  */
711
0
      h->root.non_ir_ref_dynamic = 1;
712
0
    }
713
0
}
714
715
/* Record an assignment to a symbol made by a linker script.  We need
716
   this in case some dynamic object refers to this symbol.  */
717
718
bool
719
bfd_elf_record_link_assignment (struct bfd_link_info *info,
720
        const char *name,
721
        bool provide,
722
        bool hidden)
723
0
{
724
0
  struct elf_link_hash_entry *h, *hv;
725
0
  struct elf_link_hash_table *htab;
726
0
  elf_backend_data *obed;
727
728
0
  if (!is_elf_hash_table (info->hash))
729
0
    return true;
730
731
0
  htab = elf_hash_table (info);
732
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
733
0
  if (h == NULL)
734
0
    return provide;
735
736
0
  if (h->root.type == bfd_link_hash_warning)
737
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
738
739
0
  if (h->versioned == unknown)
740
0
    {
741
      /* Set versioned if symbol version is unknown.  */
742
0
      const char *version = strrchr (name, ELF_VER_CHR);
743
0
      if (version)
744
0
  {
745
0
    if (version > name && version[-1] != ELF_VER_CHR)
746
0
      h->versioned = versioned_hidden;
747
0
    else
748
0
      h->versioned = versioned;
749
0
  }
750
0
    }
751
752
  /* Symbols defined in a linker script but not referenced anywhere
753
     else will have non_elf set.  */
754
0
  if (h->non_elf)
755
0
    {
756
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
757
0
      h->non_elf = 0;
758
0
    }
759
760
0
  switch (h->root.type)
761
0
    {
762
0
    case bfd_link_hash_defined:
763
0
    case bfd_link_hash_defweak:
764
0
    case bfd_link_hash_common:
765
0
      break;
766
0
    case bfd_link_hash_undefweak:
767
0
    case bfd_link_hash_undefined:
768
      /* Since we're defining the symbol, don't let it seem to have not
769
   been defined.  record_dynamic_symbol and size_dynamic_sections
770
   may depend on this.  */
771
0
      h->root.type = bfd_link_hash_new;
772
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
773
0
  bfd_link_repair_undef_list (&htab->root);
774
0
      break;
775
0
    case bfd_link_hash_new:
776
0
      break;
777
0
    case bfd_link_hash_indirect:
778
      /* We had a versioned symbol in a dynamic library.  We make the
779
   the versioned symbol point to this one.  */
780
0
      obed = get_elf_backend_data (info->output_bfd);
781
0
      hv = h;
782
0
      while (hv->root.type == bfd_link_hash_indirect
783
0
       || hv->root.type == bfd_link_hash_warning)
784
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
785
      /* We don't need to update h->root.u since linker will set them
786
   later.  */
787
0
      h->root.type = bfd_link_hash_undefined;
788
0
      hv->root.type = bfd_link_hash_indirect;
789
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
790
0
      obed->elf_backend_copy_indirect_symbol (info, h, hv);
791
0
      break;
792
0
    default:
793
0
      BFD_FAIL ();
794
0
      return false;
795
0
    }
796
797
  /* If this symbol is being provided by the linker script, and it is
798
     currently defined by a dynamic object, but not by a regular
799
     object, then mark it as undefined so that the generic linker will
800
     force the correct value.  */
801
0
  if (provide
802
0
      && h->def_dynamic
803
0
      && !h->def_regular)
804
0
    h->root.type = bfd_link_hash_undefined;
805
806
  /* If this symbol is currently defined by a dynamic object, but not
807
     by a regular object, then clear out any version information because
808
     the symbol will not be associated with the dynamic object any
809
     more.  */
810
0
  if (h->def_dynamic && !h->def_regular)
811
0
    h->verinfo.verdef = NULL;
812
813
  /* Make sure this symbol is not garbage collected.  */
814
0
  h->mark = 1;
815
816
0
  h->def_regular = 1;
817
818
0
  if (hidden)
819
0
    {
820
0
      obed = get_elf_backend_data (info->output_bfd);
821
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
822
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
823
0
      obed->elf_backend_hide_symbol (info, h, true);
824
0
    }
825
826
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
827
     and executables.  */
828
0
  if (!bfd_link_relocatable (info)
829
0
      && h->dynindx != -1
830
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
831
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
832
0
    h->forced_local = 1;
833
834
0
  if ((h->def_dynamic
835
0
       || h->ref_dynamic
836
0
       || bfd_link_dll (info))
837
0
      && !h->forced_local
838
0
      && h->dynindx == -1)
839
0
    {
840
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
841
0
  return false;
842
843
      /* If this is a weak defined symbol, and we know a corresponding
844
   real symbol from the same dynamic object, make sure the real
845
   symbol is also made into a dynamic symbol.  */
846
0
      if (h->is_weakalias)
847
0
  {
848
0
    struct elf_link_hash_entry *def = weakdef (h);
849
850
0
    if (def->dynindx == -1
851
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
852
0
      return false;
853
0
  }
854
0
    }
855
856
0
  return true;
857
0
}
858
859
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
860
   success, and 2 on a failure caused by attempting to record a symbol
861
   in a discarded section, eg. a discarded link-once section symbol.  */
862
863
int
864
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
865
            bfd *input_bfd,
866
            long input_indx)
867
0
{
868
0
  struct elf_link_local_dynamic_entry *entry;
869
0
  struct elf_link_hash_table *eht;
870
0
  struct elf_strtab_hash *dynstr;
871
0
  size_t dynstr_index;
872
0
  char *name;
873
0
  Elf_External_Sym_Shndx eshndx;
874
0
  char esym[sizeof (Elf64_External_Sym)];
875
876
0
  if (! is_elf_hash_table (info->hash))
877
0
    return 0;
878
879
  /* See if the entry exists already.  */
880
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
881
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
882
0
      return 1;
883
884
0
  entry = bfd_alloc (input_bfd, sizeof (*entry));
885
0
  if (entry == NULL)
886
0
    return 0;
887
888
  /* Go find the symbol, so that we can find it's name.  */
889
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_symtab_hdr (input_bfd),
890
0
           1, input_indx, &entry->isym, esym, &eshndx))
891
0
    {
892
0
      bfd_release (input_bfd, entry);
893
0
      return 0;
894
0
    }
895
896
0
  if (entry->isym.st_shndx != SHN_UNDEF
897
0
      && entry->isym.st_shndx < SHN_LORESERVE)
898
0
    {
899
0
      asection *s;
900
901
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
902
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
903
0
  {
904
    /* We can still bfd_release here as nothing has done another
905
       bfd_alloc.  We can't do this later in this function.  */
906
0
    bfd_release (input_bfd, entry);
907
0
    return 2;
908
0
  }
909
0
    }
910
911
0
  name = (bfd_elf_string_from_elf_section
912
0
    (input_bfd, elf_symtab_hdr (input_bfd).sh_link,
913
0
     entry->isym.st_name));
914
915
0
  dynstr = _bfd_elf_link_dynstr (info);
916
0
  if (dynstr == NULL)
917
0
    return 0;
918
919
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
920
0
  if (dynstr_index == (size_t) -1)
921
0
    return 0;
922
0
  entry->isym.st_name = dynstr_index;
923
924
0
  eht = elf_hash_table (info);
925
926
0
  entry->next = eht->dynlocal;
927
0
  eht->dynlocal = entry;
928
0
  entry->input_bfd = input_bfd;
929
0
  entry->input_indx = input_indx;
930
0
  eht->dynsymcount++;
931
932
  /* Whatever binding the symbol had before, it's now local.  */
933
0
  entry->isym.st_info
934
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
935
936
  /* The dynindx will be set at the end of size_dynamic_sections.  */
937
938
0
  return 1;
939
0
}
940
941
/* Return the dynindex of a local dynamic symbol.  */
942
943
long
944
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
945
            bfd *input_bfd,
946
            long input_indx)
947
0
{
948
0
  struct elf_link_local_dynamic_entry *e;
949
950
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
951
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
952
0
      return e->dynindx;
953
0
  return -1;
954
0
}
955
956
/* This function is used to renumber the dynamic symbols, if some of
957
   them are removed because they are marked as local.  This is called
958
   via elf_link_hash_traverse.  */
959
960
static bool
961
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
962
              void *data)
963
0
{
964
0
  size_t *count = data;
965
966
0
  if (h->forced_local)
967
0
    return true;
968
969
0
  if (h->dynindx != -1)
970
0
    h->dynindx = ++(*count);
971
972
0
  return true;
973
0
}
974
975
976
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
977
   STB_LOCAL binding.  */
978
979
static bool
980
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
981
              void *data)
982
0
{
983
0
  size_t *count = data;
984
985
0
  if (!h->forced_local)
986
0
    return true;
987
988
0
  if (h->dynindx != -1)
989
0
    h->dynindx = ++(*count);
990
991
0
  return true;
992
0
}
993
994
/* Return true if the dynamic symbol for a given section should be
995
   omitted when creating a shared library.  */
996
bool
997
_bfd_elf_omit_section_dynsym_default (struct bfd_link_info *info,
998
              asection *p)
999
0
{
1000
0
  struct elf_link_hash_table *htab;
1001
0
  asection *ip;
1002
1003
0
  switch (elf_section_data (p)->this_hdr.sh_type)
1004
0
    {
1005
0
    case SHT_PROGBITS:
1006
0
    case SHT_NOBITS:
1007
      /* If sh_type is yet undecided, assume it could be
1008
   SHT_PROGBITS/SHT_NOBITS.  */
1009
0
    case SHT_NULL:
1010
0
      htab = elf_hash_table (info);
1011
0
      if (htab->text_index_section != NULL)
1012
0
  return p != htab->text_index_section && p != htab->data_index_section;
1013
1014
0
      return (htab->dynobj != NULL
1015
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1016
0
        && ip->output_section == p);
1017
1018
      /* There shouldn't be section relative relocations
1019
   against any other section.  */
1020
0
    default:
1021
0
      return true;
1022
0
    }
1023
0
}
1024
1025
bool
1026
_bfd_elf_omit_section_dynsym_all (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1027
          asection *p ATTRIBUTE_UNUSED)
1028
0
{
1029
0
  return true;
1030
0
}
1031
1032
/* Assign dynsym indices.  In a shared library we generate a section
1033
   symbol for each output section, which come first.  Next come symbols
1034
   which have been forced to local binding.  Then all of the back-end
1035
   allocated local dynamic syms, followed by the rest of the global
1036
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1037
   (This prevents the early call before elf_backend_init_index_section
1038
   and strip_excluded_output_sections setting dynindx for sections
1039
   that are stripped.)  */
1040
1041
static unsigned long
1042
_bfd_elf_link_renumber_dynsyms (struct bfd_link_info *info,
1043
        unsigned long *section_sym_count)
1044
0
{
1045
0
  unsigned long dynsymcount = 0;
1046
0
  bool do_sec = section_sym_count != NULL;
1047
1048
0
  if (bfd_link_pic (info)
1049
0
      || elf_hash_table (info)->is_relocatable_executable)
1050
0
    {
1051
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
1052
0
      asection *p;
1053
0
      for (p = info->output_bfd->sections; p ; p = p->next)
1054
0
  if ((p->flags & SEC_EXCLUDE) == 0
1055
0
      && (p->flags & SEC_ALLOC) != 0
1056
0
      && elf_hash_table (info)->dynamic_relocs
1057
0
      && !obed->elf_backend_omit_section_dynsym (info, p))
1058
0
    {
1059
0
      ++dynsymcount;
1060
0
      if (do_sec)
1061
0
        elf_section_data (p)->dynindx = dynsymcount;
1062
0
    }
1063
0
  else if (do_sec)
1064
0
    elf_section_data (p)->dynindx = 0;
1065
0
    }
1066
0
  if (do_sec)
1067
0
    *section_sym_count = dynsymcount;
1068
1069
0
  if (elf_hash_table (info)->has_local_dynsyms)
1070
0
    elf_link_hash_traverse (elf_hash_table (info),
1071
0
          elf_link_renumber_local_hash_table_dynsyms,
1072
0
          &dynsymcount);
1073
1074
0
  if (elf_hash_table (info)->dynlocal)
1075
0
    {
1076
0
      struct elf_link_local_dynamic_entry *p;
1077
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1078
0
  p->dynindx = ++dynsymcount;
1079
0
    }
1080
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1081
1082
0
  elf_link_hash_traverse (elf_hash_table (info),
1083
0
        elf_link_renumber_hash_table_dynsyms,
1084
0
        &dynsymcount);
1085
1086
  /* There is an unused NULL entry at the head of the table which we
1087
     must account for in our count even if the table is empty since it
1088
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1089
     .dynamic section.  */
1090
0
  dynsymcount++;
1091
1092
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1093
0
  return dynsymcount;
1094
0
}
1095
1096
/* Merge st_other field.  Both the input bfd and output bfd are given
1097
   with IBFD reflecting where the ST_OTHER bits came from.  If they
1098
   were from a symbol already in the linker hash table then the proper
1099
   IBFD is the output bfd since symbols in the linker hash table
1100
   interpret their st_other bits according to OBFD.  */
1101
1102
static void
1103
elf_merge_st_other (bfd *obfd, bfd *ibfd,
1104
        struct elf_link_hash_entry *h,
1105
        unsigned int st_other, asection *sec,
1106
        bool definition, bool dynamic)
1107
0
{
1108
0
  if (ibfd->xvec == obfd->xvec)
1109
0
    {
1110
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
1111
1112
      /* If st_other has a processor-specific meaning, specific
1113
   code might be needed here.  */
1114
0
      if (obed->elf_backend_merge_symbol_attribute)
1115
0
  obed->elf_backend_merge_symbol_attribute (h, st_other,
1116
0
              definition, dynamic);
1117
0
    }
1118
1119
0
  if (!dynamic)
1120
0
    {
1121
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1122
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1123
1124
      /* Keep the most constraining visibility.  Leave the remainder
1125
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1126
0
      if (symvis - 1 < hvis - 1)
1127
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1128
0
    }
1129
0
  else if (definition
1130
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1131
0
     && (sec->flags & SEC_READONLY) == 0)
1132
0
    h->protected_def = 1;
1133
0
}
1134
1135
/* This function is called when we want to merge a new symbol with an
1136
   existing symbol.  It handles the various cases which arise when we
1137
   find a definition in a dynamic object, or when there is already a
1138
   definition in a dynamic object.  The new symbol is described by
1139
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1140
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1141
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1142
   of an old common symbol.  We set OVERRIDE if the old symbol is
1143
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1144
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1145
   to change.  By OK to change, we mean that we shouldn't warn if the
1146
   type or size does change.  */
1147
1148
static bool
1149
_bfd_elf_merge_symbol (bfd *abfd,
1150
           struct bfd_link_info *info,
1151
           const char *name,
1152
           Elf_Internal_Sym *sym,
1153
           asection **psec,
1154
           bfd_vma *pvalue,
1155
           struct elf_link_hash_entry **sym_hash,
1156
           bfd **poldbfd,
1157
           bool *pold_weak,
1158
           unsigned int *pold_alignment,
1159
           bool *skip,
1160
           bfd **override,
1161
           bool *type_change_ok,
1162
           bool *size_change_ok,
1163
           bool *matched)
1164
0
{
1165
0
  asection *sec, *oldsec;
1166
0
  struct elf_link_hash_entry *h;
1167
0
  struct elf_link_hash_entry *hi;
1168
0
  struct elf_link_hash_entry *flip;
1169
0
  int bind;
1170
0
  bfd *oldbfd;
1171
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1172
0
  bool newweak, oldweak, newfunc, oldfunc;
1173
0
  elf_backend_data *bed, *obed;
1174
0
  const char *new_version;
1175
0
  bool default_sym = *matched;
1176
0
  struct elf_link_hash_table *htab;
1177
1178
0
  *skip = false;
1179
0
  *override = NULL;
1180
1181
0
  sec = *psec;
1182
0
  bind = ELF_ST_BIND (sym->st_info);
1183
1184
0
  if (! bfd_is_und_section (sec))
1185
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1186
0
  else
1187
0
    h = ((struct elf_link_hash_entry *)
1188
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1189
0
  if (h == NULL)
1190
0
    return false;
1191
0
  *sym_hash = h;
1192
1193
0
  bed = get_elf_backend_data (abfd);
1194
0
  obed = get_elf_backend_data (info->output_bfd);
1195
1196
0
  htab = elf_hash_table (info);
1197
1198
  /* NEW_VERSION is the symbol version of the new symbol.  */
1199
0
  if (h->versioned != unversioned)
1200
0
    {
1201
      /* Symbol version is unknown or versioned.  */
1202
0
      new_version = strrchr (name, ELF_VER_CHR);
1203
0
      if (new_version)
1204
0
  {
1205
0
    if (h->versioned == unknown)
1206
0
      {
1207
        /* The base symbol has an empty version.  */
1208
0
        if (new_version[1] == '\0')
1209
0
    {
1210
0
      htab->has_base_symbols = true;
1211
0
      h->base_symbol = 1;
1212
0
    }
1213
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1214
0
    h->versioned = versioned_hidden;
1215
0
        else
1216
0
    h->versioned = versioned;
1217
0
      }
1218
0
    new_version += 1;
1219
0
    if (new_version[0] == '\0')
1220
0
      new_version = NULL;
1221
0
  }
1222
0
      else
1223
0
  h->versioned = unversioned;
1224
0
    }
1225
0
  else
1226
0
    new_version = NULL;
1227
1228
  /* For merging, we only care about real symbols.  But we need to make
1229
     sure that indirect symbol dynamic flags are updated.  */
1230
0
  hi = h;
1231
0
  while (h->root.type == bfd_link_hash_indirect
1232
0
   || h->root.type == bfd_link_hash_warning)
1233
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1234
1235
0
  if (!*matched)
1236
0
    {
1237
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1238
0
  *matched = true;
1239
0
      else
1240
0
  {
1241
    /* OLD_HIDDEN is true if the existing symbol is only visible
1242
       to the symbol with the same symbol version.  NEW_HIDDEN is
1243
       true if the new symbol is only visible to the symbol with
1244
       the same symbol version.  */
1245
0
    bool old_hidden = h->versioned == versioned_hidden;
1246
0
    bool new_hidden = hi->versioned == versioned_hidden;
1247
0
    if (!old_hidden && !new_hidden)
1248
      /* The new symbol matches the existing symbol if both
1249
         aren't hidden.  */
1250
0
      *matched = true;
1251
0
    else
1252
0
      {
1253
        /* OLD_VERSION is the symbol version of the existing
1254
     symbol. */
1255
0
        const char *old_version;
1256
1257
0
        if (h->versioned >= versioned)
1258
0
    old_version = strrchr (h->root.root.string,
1259
0
               ELF_VER_CHR) + 1;
1260
0
        else
1261
0
     old_version = NULL;
1262
1263
        /* The new symbol matches the existing symbol if they
1264
     have the same symbol version.  */
1265
0
        *matched = (old_version == new_version
1266
0
        || (old_version != NULL
1267
0
            && new_version != NULL
1268
0
            && strcmp (old_version, new_version) == 0));
1269
0
      }
1270
0
  }
1271
0
    }
1272
1273
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1274
     existing symbol.  */
1275
1276
0
  oldbfd = NULL;
1277
0
  oldsec = NULL;
1278
0
  switch (h->root.type)
1279
0
    {
1280
0
    default:
1281
0
      break;
1282
1283
0
    case bfd_link_hash_undefined:
1284
0
    case bfd_link_hash_undefweak:
1285
0
      oldbfd = h->root.u.undef.abfd;
1286
0
      break;
1287
1288
0
    case bfd_link_hash_defined:
1289
0
    case bfd_link_hash_defweak:
1290
0
      oldbfd = h->root.u.def.section->owner;
1291
0
      oldsec = h->root.u.def.section;
1292
0
      break;
1293
1294
0
    case bfd_link_hash_common:
1295
0
      oldbfd = h->root.u.c.p->section->owner;
1296
0
      oldsec = h->root.u.c.p->section;
1297
0
      if (pold_alignment)
1298
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1299
0
      break;
1300
0
    }
1301
0
  if (poldbfd && *poldbfd == NULL)
1302
0
    *poldbfd = oldbfd;
1303
1304
  /* Differentiate strong and weak symbols.  */
1305
0
  newweak = bind == STB_WEAK;
1306
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1307
0
       || h->root.type == bfd_link_hash_undefweak);
1308
0
  if (pold_weak)
1309
0
    *pold_weak = oldweak;
1310
1311
  /* We have to check it for every instance since the first few may be
1312
     references and not all compilers emit symbol type for undefined
1313
     symbols.  */
1314
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1315
1316
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1317
     respectively, is from a dynamic object.  */
1318
1319
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1320
1321
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1322
     syms and defined syms in dynamic libraries respectively.
1323
     ref_dynamic on the other hand can be set for a symbol defined in
1324
     a dynamic library, and def_dynamic may not be set;  When the
1325
     definition in a dynamic lib is overridden by a definition in the
1326
     executable use of the symbol in the dynamic lib becomes a
1327
     reference to the executable symbol.  */
1328
0
  if (newdyn)
1329
0
    {
1330
0
      if (bfd_is_und_section (sec))
1331
0
  {
1332
0
    if (bind != STB_WEAK)
1333
0
      {
1334
0
        h->ref_dynamic_nonweak = 1;
1335
0
        hi->ref_dynamic_nonweak = 1;
1336
0
      }
1337
0
  }
1338
0
      else
1339
0
  {
1340
    /* Update the existing symbol only if they match. */
1341
0
    if (*matched)
1342
0
      h->dynamic_def = 1;
1343
0
    hi->dynamic_def = 1;
1344
0
  }
1345
0
    }
1346
1347
  /* If we just created the symbol, mark it as being an ELF symbol.
1348
     Other than that, there is nothing to do--there is no merge issue
1349
     with a newly defined symbol--so we just return.  */
1350
1351
0
  if (h->root.type == bfd_link_hash_new)
1352
0
    {
1353
0
      h->non_elf = 0;
1354
0
      return true;
1355
0
    }
1356
1357
  /* In cases involving weak versioned symbols, we may wind up trying
1358
     to merge a symbol with itself.  Catch that here, to avoid the
1359
     confusion that results if we try to override a symbol with
1360
     itself.  The additional tests catch cases like
1361
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1362
     dynamic object, which we do want to handle here.  */
1363
0
  if (abfd == oldbfd
1364
0
      && (newweak || oldweak)
1365
0
      && ((abfd->flags & DYNAMIC) == 0
1366
0
    || !h->def_regular))
1367
0
    return true;
1368
1369
0
  olddyn = false;
1370
0
  if (oldbfd != NULL)
1371
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1372
0
  else if (oldsec != NULL)
1373
0
    {
1374
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1375
   indices used by MIPS ELF.  */
1376
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1377
0
    }
1378
1379
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1380
0
  if (!htab->handling_dt_needed
1381
0
      && oldbfd != NULL
1382
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1383
0
    {
1384
0
      if (newdyn != olddyn)
1385
0
  {
1386
    /* Handle a case where plugin_notice won't be called and thus
1387
       won't set the non_ir_ref flags on the first pass over
1388
       symbols.  */
1389
0
    h->root.non_ir_ref_dynamic = true;
1390
0
    hi->root.non_ir_ref_dynamic = true;
1391
0
  }
1392
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1393
0
         && hi->root.type == bfd_link_hash_indirect)
1394
0
  {
1395
    /* Change indirect symbol from IR to undefined.  */
1396
0
    hi->root.type = bfd_link_hash_undefined;
1397
0
    hi->root.u.undef.abfd = oldbfd;
1398
0
  }
1399
0
    }
1400
1401
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1402
     respectively, appear to be a definition rather than reference.  */
1403
1404
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1405
1406
0
  olddef = (h->root.type != bfd_link_hash_undefined
1407
0
      && h->root.type != bfd_link_hash_undefweak
1408
0
      && h->root.type != bfd_link_hash_common);
1409
1410
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1411
     respectively, appear to be a function.  */
1412
1413
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1414
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1415
1416
0
  oldfunc = (h->type != STT_NOTYPE
1417
0
       && bed->is_function_type (h->type));
1418
1419
0
  if (!(newfunc && oldfunc)
1420
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1421
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1422
0
      && h->type != STT_NOTYPE
1423
0
      && (newdef || bfd_is_com_section (sec))
1424
0
      && (olddef || h->root.type == bfd_link_hash_common))
1425
0
    {
1426
      /* If creating a default indirect symbol ("foo" or "foo@") from
1427
   a dynamic versioned definition ("foo@@") skip doing so if
1428
   there is an existing regular definition with a different
1429
   type.  We don't want, for example, a "time" variable in the
1430
   executable overriding a "time" function in a shared library.  */
1431
0
      if (newdyn
1432
0
    && !olddyn)
1433
0
  {
1434
0
    *skip = true;
1435
0
    return true;
1436
0
  }
1437
1438
      /* When adding a symbol from a regular object file after we have
1439
   created indirect symbols, undo the indirection and any
1440
   dynamic state.  */
1441
0
      if (hi != h
1442
0
    && !newdyn
1443
0
    && olddyn)
1444
0
  {
1445
0
    h = hi;
1446
0
    obed->elf_backend_hide_symbol (info, h, true);
1447
0
    h->forced_local = 0;
1448
0
    h->ref_dynamic = 0;
1449
0
    h->def_dynamic = 0;
1450
0
    h->dynamic_def = 0;
1451
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1452
0
      {
1453
0
        h->root.type = bfd_link_hash_undefined;
1454
0
        h->root.u.undef.abfd = abfd;
1455
0
      }
1456
0
    else
1457
0
      {
1458
0
        h->root.type = bfd_link_hash_new;
1459
0
        h->root.u.undef.abfd = NULL;
1460
0
      }
1461
0
    return true;
1462
0
  }
1463
0
    }
1464
1465
  /* Check TLS symbols.  We don't check undefined symbols introduced
1466
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1467
     check symbols from plugins because they also have no type.  */
1468
0
  if (oldbfd != NULL
1469
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1470
0
      && (abfd->flags & BFD_PLUGIN) == 0
1471
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1472
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1473
0
    {
1474
0
      bfd *ntbfd, *tbfd;
1475
0
      bool ntdef, tdef;
1476
0
      asection *ntsec, *tsec;
1477
1478
0
      if (h->type == STT_TLS)
1479
0
  {
1480
0
    ntbfd = abfd;
1481
0
    ntsec = sec;
1482
0
    ntdef = newdef;
1483
0
    tbfd = oldbfd;
1484
0
    tsec = oldsec;
1485
0
    tdef = olddef;
1486
0
  }
1487
0
      else
1488
0
  {
1489
0
    ntbfd = oldbfd;
1490
0
    ntsec = oldsec;
1491
0
    ntdef = olddef;
1492
0
    tbfd = abfd;
1493
0
    tsec = sec;
1494
0
    tdef = newdef;
1495
0
  }
1496
1497
0
      if (tdef && ntdef)
1498
0
  _bfd_error_handler
1499
    /* xgettext:c-format */
1500
0
    (_("%s: TLS definition in %pB section %pA "
1501
0
       "mismatches non-TLS definition in %pB section %pA"),
1502
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1503
0
      else if (!tdef && !ntdef)
1504
0
  _bfd_error_handler
1505
    /* xgettext:c-format */
1506
0
    (_("%s: TLS reference in %pB "
1507
0
       "mismatches non-TLS reference in %pB"),
1508
0
     h->root.root.string, tbfd, ntbfd);
1509
0
      else if (tdef)
1510
0
  _bfd_error_handler
1511
    /* xgettext:c-format */
1512
0
    (_("%s: TLS definition in %pB section %pA "
1513
0
       "mismatches non-TLS reference in %pB"),
1514
0
     h->root.root.string, tbfd, tsec, ntbfd);
1515
0
      else
1516
0
  _bfd_error_handler
1517
    /* xgettext:c-format */
1518
0
    (_("%s: TLS reference in %pB "
1519
0
       "mismatches non-TLS definition in %pB section %pA"),
1520
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1521
1522
0
      bfd_set_error (bfd_error_bad_value);
1523
0
      return false;
1524
0
    }
1525
1526
  /* If the old symbol has non-default visibility, we ignore the new
1527
     definition from a dynamic object.  */
1528
0
  if (newdyn
1529
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1530
0
      && !bfd_is_und_section (sec))
1531
0
    {
1532
0
      *skip = true;
1533
      /* Make sure this symbol is dynamic.  */
1534
0
      h->ref_dynamic = 1;
1535
0
      hi->ref_dynamic = 1;
1536
      /* A protected symbol has external availability. Make sure it is
1537
   recorded as dynamic.
1538
1539
   FIXME: Should we check type and size for protected symbol?  */
1540
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1541
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1542
0
      else
1543
0
  return true;
1544
0
    }
1545
0
  else if (!newdyn
1546
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1547
0
     && h->def_dynamic)
1548
0
    {
1549
      /* If the new symbol with non-default visibility comes from a
1550
   relocatable file and the old definition comes from a dynamic
1551
   object, we remove the old definition.  */
1552
0
      if (hi->root.type == bfd_link_hash_indirect)
1553
0
  {
1554
    /* Handle the case where the old dynamic definition is
1555
       default versioned.  We need to copy the symbol info from
1556
       the symbol with default version to the normal one if it
1557
       was referenced before.  */
1558
0
    if (h->ref_regular)
1559
0
      {
1560
0
        hi->root.type = h->root.type;
1561
0
        h->root.type = bfd_link_hash_indirect;
1562
0
        obed->elf_backend_copy_indirect_symbol (info, hi, h);
1563
1564
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1565
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1566
0
    {
1567
      /* If the new symbol is hidden or internal, completely undo
1568
         any dynamic link state.  */
1569
0
      obed->elf_backend_hide_symbol (info, h, true);
1570
0
      h->forced_local = 0;
1571
0
      h->ref_dynamic = 0;
1572
0
    }
1573
0
        else
1574
0
    h->ref_dynamic = 1;
1575
1576
0
        h->def_dynamic = 0;
1577
        /* FIXME: Should we check type and size for protected symbol?  */
1578
0
        h->size = 0;
1579
0
        h->type = 0;
1580
1581
0
        h = hi;
1582
0
      }
1583
0
    else
1584
0
      h = hi;
1585
0
  }
1586
1587
      /* If the old symbol was undefined before, then it will still be
1588
   on the undefs list.  If the new symbol is undefined or
1589
   common, we can't make it bfd_link_hash_new here, because new
1590
   undefined or common symbols will be added to the undefs list
1591
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1592
   added twice to the undefs list.  Also, if the new symbol is
1593
   undefweak then we don't want to lose the strong undef.  */
1594
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1595
0
  {
1596
0
    h->root.type = bfd_link_hash_undefined;
1597
0
    h->root.u.undef.abfd = abfd;
1598
0
  }
1599
0
      else
1600
0
  {
1601
0
    h->root.type = bfd_link_hash_new;
1602
0
    h->root.u.undef.abfd = NULL;
1603
0
  }
1604
1605
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1606
0
  {
1607
    /* If the new symbol is hidden or internal, completely undo
1608
       any dynamic link state.  */
1609
0
    obed->elf_backend_hide_symbol (info, h, true);
1610
0
    h->forced_local = 0;
1611
0
    h->ref_dynamic = 0;
1612
0
  }
1613
0
      else
1614
0
  h->ref_dynamic = 1;
1615
0
      h->def_dynamic = 0;
1616
      /* FIXME: Should we check type and size for protected symbol?  */
1617
0
      h->size = 0;
1618
0
      h->type = 0;
1619
0
      return true;
1620
0
    }
1621
1622
  /* If a new weak symbol definition comes from a regular file and the
1623
     old symbol comes from a dynamic library, we treat the new one as
1624
     strong.  Similarly, an old weak symbol definition from a regular
1625
     file is treated as strong when the new symbol comes from a dynamic
1626
     library.  Further, an old weak symbol from a dynamic library is
1627
     treated as strong if the new symbol is from a dynamic library.
1628
     This reflects the way glibc's ld.so works.
1629
1630
     Also allow a weak symbol to override a linker script symbol
1631
     defined by an early pass over the script.  This is done so the
1632
     linker knows the symbol is defined in an object file, for the
1633
     DEFINED script function.
1634
1635
     Do this before setting *type_change_ok or *size_change_ok so that
1636
     we warn properly when dynamic library symbols are overridden.  */
1637
1638
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1639
0
    newweak = false;
1640
0
  if (olddef && newdyn)
1641
0
    oldweak = false;
1642
1643
  /* Allow changes between different types of function symbol.  */
1644
0
  if (newfunc && oldfunc)
1645
0
    *type_change_ok = true;
1646
1647
  /* It's OK to change the type if either the existing symbol or the
1648
     new symbol is weak.  A type change is also OK if the old symbol
1649
     is undefined and the new symbol is defined.  */
1650
1651
0
  if (oldweak
1652
0
      || newweak
1653
0
      || (newdef
1654
0
    && h->root.type == bfd_link_hash_undefined))
1655
0
    *type_change_ok = true;
1656
1657
  /* It's OK to change the size if either the existing symbol or the
1658
     new symbol is weak, or if the old symbol is undefined.  */
1659
1660
0
  if (*type_change_ok
1661
0
      || h->root.type == bfd_link_hash_undefined)
1662
0
    *size_change_ok = true;
1663
1664
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1665
     symbol, respectively, appears to be a common symbol in a dynamic
1666
     object.  If a symbol appears in an uninitialized section, and is
1667
     not weak, and is not a function, then it may be a common symbol
1668
     which was resolved when the dynamic object was created.  We want
1669
     to treat such symbols specially, because they raise special
1670
     considerations when setting the symbol size: if the symbol
1671
     appears as a common symbol in a regular object, and the size in
1672
     the regular object is larger, we must make sure that we use the
1673
     larger size.  This problematic case can always be avoided in C,
1674
     but it must be handled correctly when using Fortran shared
1675
     libraries.
1676
1677
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1678
     likewise for OLDDYNCOMMON and OLDDEF.
1679
1680
     Note that this test is just a heuristic, and that it is quite
1681
     possible to have an uninitialized symbol in a shared object which
1682
     is really a definition, rather than a common symbol.  This could
1683
     lead to some minor confusion when the symbol really is a common
1684
     symbol in some regular object.  However, I think it will be
1685
     harmless.  */
1686
1687
0
  if (newdyn
1688
0
      && newdef
1689
0
      && !newweak
1690
0
      && (sec->flags & SEC_ALLOC) != 0
1691
0
      && (sec->flags & SEC_LOAD) == 0
1692
0
      && sym->st_size > 0
1693
0
      && !newfunc)
1694
0
    newdyncommon = true;
1695
0
  else
1696
0
    newdyncommon = false;
1697
1698
0
  if (olddyn
1699
0
      && olddef
1700
0
      && h->root.type == bfd_link_hash_defined
1701
0
      && h->def_dynamic
1702
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1703
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1704
0
      && h->size > 0
1705
0
      && !oldfunc)
1706
0
    olddyncommon = true;
1707
0
  else
1708
0
    olddyncommon = false;
1709
1710
  /* We now know everything about the old and new symbols.  We ask the
1711
     backend to check if we can merge them.  */
1712
0
  if (obed->merge_symbol != NULL)
1713
0
    {
1714
0
      if (!obed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1715
0
  return false;
1716
0
      sec = *psec;
1717
0
    }
1718
1719
  /* There are multiple definitions of a normal symbol.  Skip the
1720
     default symbol as well as definition from an IR object.  */
1721
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1722
0
      && !default_sym && h->def_regular
1723
0
      && !(oldbfd != NULL
1724
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1725
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1726
0
    {
1727
      /* Handle a multiple definition.  */
1728
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1729
0
                 abfd, sec, *pvalue);
1730
0
      *skip = true;
1731
0
      return true;
1732
0
    }
1733
1734
  /* If both the old and the new symbols look like common symbols in a
1735
     dynamic object, set the size of the symbol to the larger of the
1736
     two.  */
1737
1738
0
  if (olddyncommon
1739
0
      && newdyncommon
1740
0
      && sym->st_size != h->size)
1741
0
    {
1742
      /* Since we think we have two common symbols, issue a multiple
1743
   common warning if desired.  Note that we only warn if the
1744
   size is different.  If the size is the same, we simply let
1745
   the old symbol override the new one as normally happens with
1746
   symbols defined in dynamic objects.  */
1747
1748
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1749
0
             bfd_link_hash_common, sym->st_size);
1750
0
      if (sym->st_size > h->size)
1751
0
  h->size = sym->st_size;
1752
1753
0
      *size_change_ok = true;
1754
0
    }
1755
1756
  /* If we are looking at a dynamic object, and we have found a
1757
     definition, we need to see if the symbol was already defined by
1758
     some other object.  If so, we want to use the existing
1759
     definition, and we do not want to report a multiple symbol
1760
     definition error; we do this by clobbering *PSEC to be
1761
     bfd_und_section_ptr.
1762
1763
     We treat a common symbol as a definition if the symbol in the
1764
     shared library is a function, since common symbols always
1765
     represent variables; this can cause confusion in principle, but
1766
     any such confusion would seem to indicate an erroneous program or
1767
     shared library.  We also permit a common symbol in a regular
1768
     object to override a weak symbol in a shared object.  */
1769
1770
0
  if (newdyn
1771
0
      && newdef
1772
0
      && (olddef
1773
0
    || (h->root.type == bfd_link_hash_common
1774
0
        && (newweak || newfunc))))
1775
0
    {
1776
0
      *override = abfd;
1777
0
      newdef = false;
1778
0
      newdyncommon = false;
1779
1780
0
      *psec = sec = bfd_und_section_ptr;
1781
0
      *size_change_ok = true;
1782
1783
      /* If we get here when the old symbol is a common symbol, then
1784
   we are explicitly letting it override a weak symbol or
1785
   function in a dynamic object, and we don't want to warn about
1786
   a type change.  If the old symbol is a defined symbol, a type
1787
   change warning may still be appropriate.  */
1788
1789
0
      if (h->root.type == bfd_link_hash_common)
1790
0
  *type_change_ok = true;
1791
0
    }
1792
1793
  /* Handle the special case of an old common symbol merging with a
1794
     new symbol which looks like a common symbol in a shared object.
1795
     We change *PSEC and *PVALUE to make the new symbol look like a
1796
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1797
     right thing.  */
1798
1799
0
  if (newdyncommon
1800
0
      && h->root.type == bfd_link_hash_common)
1801
0
    {
1802
0
      *override = oldbfd;
1803
0
      newdef = false;
1804
0
      newdyncommon = false;
1805
0
      *pvalue = sym->st_size;
1806
0
      *psec = sec = bed->common_section (oldsec);
1807
0
      *size_change_ok = true;
1808
0
    }
1809
1810
  /* Skip weak definitions of symbols that are already defined.  */
1811
0
  if (newdef && olddef && newweak)
1812
0
    {
1813
      /* Don't skip new non-IR weak syms.  */
1814
0
      if (!(oldbfd != NULL
1815
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1816
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1817
0
  {
1818
0
    newdef = false;
1819
0
    *skip = true;
1820
0
  }
1821
1822
      /* Merge st_other.  If the symbol already has a dynamic index,
1823
   but visibility says it should not be visible, turn it into a
1824
   local symbol.  */
1825
0
      elf_merge_st_other (info->output_bfd, abfd, h, sym->st_other,
1826
0
        sec, newdef, newdyn);
1827
0
      if (h->dynindx != -1)
1828
0
  switch (ELF_ST_VISIBILITY (h->other))
1829
0
    {
1830
0
    case STV_INTERNAL:
1831
0
    case STV_HIDDEN:
1832
0
      obed->elf_backend_hide_symbol (info, h, true);
1833
0
      break;
1834
0
    }
1835
0
    }
1836
1837
  /* If the old symbol is from a dynamic object, and the new symbol is
1838
     a definition which is not from a dynamic object, then the new
1839
     symbol overrides the old symbol.  Symbols from regular files
1840
     always take precedence over symbols from dynamic objects, even if
1841
     they are defined after the dynamic object in the link.
1842
1843
     As above, we again permit a common symbol in a regular object to
1844
     override a definition in a shared object if the shared object
1845
     symbol is a function or is weak.  */
1846
1847
0
  flip = NULL;
1848
0
  if (!newdyn
1849
0
      && (newdef
1850
0
    || (bfd_is_com_section (sec)
1851
0
        && (oldweak || oldfunc)))
1852
0
      && olddyn
1853
0
      && olddef
1854
0
      && h->def_dynamic)
1855
0
    {
1856
      /* Change the hash table entry to undefined, and let
1857
   _bfd_generic_link_add_one_symbol do the right thing with the
1858
   new definition.  */
1859
1860
0
      h->root.type = bfd_link_hash_undefined;
1861
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1862
0
      *size_change_ok = true;
1863
1864
0
      olddef = false;
1865
0
      olddyncommon = false;
1866
1867
      /* We again permit a type change when a common symbol may be
1868
   overriding a function.  */
1869
1870
0
      if (bfd_is_com_section (sec))
1871
0
  {
1872
0
    if (oldfunc)
1873
0
      {
1874
        /* If a common symbol overrides a function, make sure
1875
     that it isn't defined dynamically nor has type
1876
     function.  */
1877
0
        h->def_dynamic = 0;
1878
0
        h->type = STT_NOTYPE;
1879
0
      }
1880
0
    *type_change_ok = true;
1881
0
  }
1882
1883
0
      if (hi->root.type == bfd_link_hash_indirect)
1884
0
  flip = hi;
1885
0
      else
1886
  /* This union may have been set to be non-NULL when this symbol
1887
     was seen in a dynamic object.  We must force the union to be
1888
     NULL, so that it is correct for a regular symbol.  */
1889
0
  h->verinfo.vertree = NULL;
1890
0
    }
1891
1892
  /* Handle the special case of a new common symbol merging with an
1893
     old symbol that looks like it might be a common symbol defined in
1894
     a shared object.  Note that we have already handled the case in
1895
     which a new common symbol should simply override the definition
1896
     in the shared library.  */
1897
1898
0
  if (! newdyn
1899
0
      && bfd_is_com_section (sec)
1900
0
      && olddyncommon)
1901
0
    {
1902
      /* It would be best if we could set the hash table entry to a
1903
   common symbol, but we don't know what to use for the section
1904
   or the alignment.  */
1905
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1906
0
             bfd_link_hash_common, sym->st_size);
1907
1908
      /* If the presumed common symbol in the dynamic object is
1909
   larger, pretend that the new symbol has its size.  */
1910
1911
0
      if (h->size > *pvalue)
1912
0
  *pvalue = h->size;
1913
1914
      /* We need to remember the alignment required by the symbol
1915
   in the dynamic object.  */
1916
0
      BFD_ASSERT (pold_alignment);
1917
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1918
1919
0
      olddef = false;
1920
0
      olddyncommon = false;
1921
1922
0
      h->root.type = bfd_link_hash_undefined;
1923
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1924
1925
0
      *size_change_ok = true;
1926
0
      *type_change_ok = true;
1927
1928
0
      if (hi->root.type == bfd_link_hash_indirect)
1929
0
  flip = hi;
1930
0
      else
1931
0
  h->verinfo.vertree = NULL;
1932
0
    }
1933
1934
0
  if (flip != NULL)
1935
0
    {
1936
      /* Handle the case where we had a versioned symbol in a dynamic
1937
   library and now find a definition in a normal object.  In this
1938
   case, we make the versioned symbol point to the normal one.  */
1939
0
      flip->root.type = h->root.type;
1940
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1941
0
      h->root.type = bfd_link_hash_indirect;
1942
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1943
0
      obed->elf_backend_copy_indirect_symbol (info, flip, h);
1944
0
      if (h->def_dynamic)
1945
0
  {
1946
0
    h->def_dynamic = 0;
1947
0
    flip->ref_dynamic = 1;
1948
0
  }
1949
0
    }
1950
1951
0
  return true;
1952
0
}
1953
1954
/* This function is called to create an indirect symbol from the
1955
   default for the symbol with the default version if needed. The
1956
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1957
   set DYNSYM if the new indirect symbol is dynamic.  */
1958
1959
static bool
1960
_bfd_elf_add_default_symbol (bfd *abfd,
1961
           struct bfd_link_info *info,
1962
           struct elf_link_hash_entry *h,
1963
           const char *name,
1964
           Elf_Internal_Sym *sym,
1965
           asection *sec,
1966
           bfd_vma value,
1967
           bfd **poldbfd,
1968
           bool *dynsym)
1969
0
{
1970
0
  bool type_change_ok;
1971
0
  bool size_change_ok;
1972
0
  bool skip;
1973
0
  char *shortname;
1974
0
  struct elf_link_hash_entry *hi;
1975
0
  struct bfd_link_hash_entry *bh;
1976
0
  elf_backend_data *obed;
1977
0
  bool collect;
1978
0
  bool dynamic;
1979
0
  bfd *override;
1980
0
  const char *p;
1981
0
  size_t len, shortlen;
1982
0
  asection *tmp_sec;
1983
0
  bool matched;
1984
1985
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1986
0
    return true;
1987
1988
  /* If this symbol has a version, and it is the default version, we
1989
     create an indirect symbol from the default name to the fully
1990
     decorated name.  This will cause external references which do not
1991
     specify a version to be bound to this version of the symbol.  */
1992
0
  p = strchr (name, ELF_VER_CHR);
1993
0
  if (h->versioned == unknown)
1994
0
    {
1995
0
      if (p == NULL)
1996
0
  {
1997
0
    h->versioned = unversioned;
1998
0
    return true;
1999
0
  }
2000
0
      else
2001
0
  {
2002
0
    if (p[1] != ELF_VER_CHR)
2003
0
      {
2004
0
        h->versioned = versioned_hidden;
2005
0
        return true;
2006
0
      }
2007
0
    else
2008
0
      h->versioned = versioned;
2009
0
  }
2010
0
    }
2011
0
  else
2012
0
    {
2013
      /* PR ld/19073: We may see an unversioned definition after the
2014
   default version.  */
2015
0
      if (p == NULL)
2016
0
  return true;
2017
0
    }
2018
2019
0
  obed = get_elf_backend_data (info->output_bfd);
2020
0
  collect = obed->collect;
2021
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
2022
2023
0
  shortlen = p - name;
2024
0
  shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
2025
0
  if (shortname == NULL)
2026
0
    return false;
2027
0
  memcpy (shortname, name, shortlen);
2028
0
  shortname[shortlen] = '\0';
2029
2030
  /* We are going to create a new symbol.  Merge it with any existing
2031
     symbol with this name.  For the purposes of the merge, act as
2032
     though we were defining the symbol we just defined, although we
2033
     actually going to define an indirect symbol.  */
2034
0
  type_change_ok = false;
2035
0
  size_change_ok = false;
2036
0
  matched = true;
2037
0
  tmp_sec = sec;
2038
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2039
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2040
0
            &type_change_ok, &size_change_ok, &matched))
2041
0
    return false;
2042
2043
0
  if (skip)
2044
0
    goto nondefault;
2045
2046
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2047
0
    {
2048
      /* If the undecorated symbol will have a version added by a
2049
   script different to H, then don't indirect to/from the
2050
   undecorated symbol.  This isn't ideal because we may not yet
2051
   have seen symbol versions, if given by a script on the
2052
   command line rather than via --version-script.  */
2053
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2054
0
  {
2055
0
    bool hide;
2056
2057
0
    hi->verinfo.vertree
2058
0
      = bfd_find_version_for_sym (info->version_info,
2059
0
          hi->root.root.string, &hide);
2060
0
    if (hi->verinfo.vertree != NULL && hide)
2061
0
      {
2062
0
        obed->elf_backend_hide_symbol (info, hi, true);
2063
0
        goto nondefault;
2064
0
      }
2065
0
  }
2066
0
      if (hi->verinfo.vertree != NULL
2067
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2068
0
  goto nondefault;
2069
0
    }
2070
2071
0
  if (! override)
2072
0
    {
2073
      /* Add the default symbol if not performing a relocatable link.  */
2074
0
      if (! bfd_link_relocatable (info))
2075
0
  {
2076
0
    bh = &hi->root;
2077
0
    if (bh->type == bfd_link_hash_defined
2078
0
        && bh->u.def.section->owner != NULL
2079
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2080
0
      {
2081
        /* Mark the previous definition from IR object as
2082
     undefined so that the generic linker will override
2083
     it.  */
2084
0
        bh->type = bfd_link_hash_undefined;
2085
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2086
0
      }
2087
0
    if (! (_bfd_generic_link_add_one_symbol
2088
0
     (info, abfd, shortname, BSF_INDIRECT,
2089
0
      bfd_ind_section_ptr,
2090
0
      0, name, false, collect, &bh)))
2091
0
      return false;
2092
0
    hi = (struct elf_link_hash_entry *) bh;
2093
0
  }
2094
0
    }
2095
0
  else
2096
0
    {
2097
      /* In this case the symbol named SHORTNAME is overriding the
2098
   indirect symbol we want to add.  We were planning on making
2099
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2100
   is the name without a version.  NAME is the fully versioned
2101
   name, and it is the default version.
2102
2103
   Overriding means that we already saw a definition for the
2104
   symbol SHORTNAME in a regular object, and it is overriding
2105
   the symbol defined in the dynamic object.
2106
2107
   When this happens, we actually want to change NAME, the
2108
   symbol we just added, to refer to SHORTNAME.  This will cause
2109
   references to NAME in the shared object to become references
2110
   to SHORTNAME in the regular object.  This is what we expect
2111
   when we override a function in a shared object: that the
2112
   references in the shared object will be mapped to the
2113
   definition in the regular object.  */
2114
2115
0
      while (hi->root.type == bfd_link_hash_indirect
2116
0
       || hi->root.type == bfd_link_hash_warning)
2117
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2118
2119
0
      h->root.type = bfd_link_hash_indirect;
2120
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2121
0
      if (h->def_dynamic)
2122
0
  {
2123
0
    h->def_dynamic = 0;
2124
0
    hi->ref_dynamic = 1;
2125
0
    if (hi->ref_regular
2126
0
        || hi->def_regular)
2127
0
      {
2128
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2129
0
    return false;
2130
0
      }
2131
0
  }
2132
2133
      /* Now set HI to H, so that the following code will set the
2134
   other fields correctly.  */
2135
0
      hi = h;
2136
0
    }
2137
2138
  /* Check if HI is a warning symbol.  */
2139
0
  if (hi->root.type == bfd_link_hash_warning)
2140
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2141
2142
  /* If there is a duplicate definition somewhere, then HI may not
2143
     point to an indirect symbol.  We will have reported an error to
2144
     the user in that case.  */
2145
2146
0
  if (hi->root.type == bfd_link_hash_indirect)
2147
0
    {
2148
0
      struct elf_link_hash_entry *ht;
2149
2150
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2151
0
      obed->elf_backend_copy_indirect_symbol (info, ht, hi);
2152
2153
      /* If we first saw a reference to SHORTNAME with non-default
2154
   visibility, merge that visibility to the @@VER symbol.  */
2155
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, ht, hi->other,
2156
0
        sec, true, dynamic);
2157
2158
      /* A reference to the SHORTNAME symbol from a dynamic library
2159
   will be satisfied by the versioned symbol at runtime.  In
2160
   effect, we have a reference to the versioned symbol.  */
2161
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2162
0
      hi->dynamic_def |= ht->dynamic_def;
2163
2164
      /* See if the new flags lead us to realize that the symbol must
2165
   be dynamic.  */
2166
0
      if (! *dynsym)
2167
0
  {
2168
0
    if (! dynamic)
2169
0
      {
2170
0
        if (! bfd_link_executable (info)
2171
0
      || hi->def_dynamic
2172
0
      || hi->ref_dynamic)
2173
0
    *dynsym = true;
2174
0
      }
2175
0
    else
2176
0
      {
2177
0
        if (hi->ref_regular)
2178
0
    *dynsym = true;
2179
0
      }
2180
0
  }
2181
0
    }
2182
2183
  /* We also need to define an indirection from the nondefault version
2184
     of the symbol.  */
2185
2186
0
 nondefault:
2187
0
  len = strlen (name);
2188
0
  shortname = bfd_hash_allocate (&info->hash->table, len);
2189
0
  if (shortname == NULL)
2190
0
    return false;
2191
0
  memcpy (shortname, name, shortlen);
2192
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2193
2194
  /* Once again, merge with any existing symbol.  */
2195
0
  type_change_ok = false;
2196
0
  size_change_ok = false;
2197
0
  tmp_sec = sec;
2198
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2199
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2200
0
            &type_change_ok, &size_change_ok, &matched))
2201
0
    return false;
2202
2203
0
  if (skip)
2204
0
    {
2205
0
      if (!dynamic
2206
0
    && h->root.type == bfd_link_hash_defweak
2207
0
    && hi->root.type == bfd_link_hash_defined)
2208
0
  {
2209
    /* We are handling a weak sym@@ver and attempting to define
2210
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2211
       new weak sym@ver because there is already a strong sym@ver.
2212
       However, sym@ver and sym@@ver are really the same symbol.
2213
       The existing strong sym@ver ought to override sym@@ver.  */
2214
0
    h->root.type = bfd_link_hash_defined;
2215
0
    h->root.u.def.section = hi->root.u.def.section;
2216
0
    h->root.u.def.value = hi->root.u.def.value;
2217
0
    hi->root.type = bfd_link_hash_indirect;
2218
0
    hi->root.u.i.link = &h->root;
2219
0
  }
2220
0
      else
2221
0
  return true;
2222
0
    }
2223
0
  else if (override)
2224
0
    {
2225
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2226
   the type of override we do in the case above unless it is
2227
   overridden by a versioned definition.  */
2228
0
      if (hi->root.type != bfd_link_hash_defined
2229
0
    && hi->root.type != bfd_link_hash_defweak)
2230
0
  _bfd_error_handler
2231
    /* xgettext:c-format */
2232
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2233
0
     abfd, shortname);
2234
0
      return true;
2235
0
    }
2236
0
  else
2237
0
    {
2238
0
      bh = &hi->root;
2239
0
      if (! (_bfd_generic_link_add_one_symbol
2240
0
       (info, abfd, shortname, BSF_INDIRECT,
2241
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2242
0
  return false;
2243
0
      hi = (struct elf_link_hash_entry *) bh;
2244
0
    }
2245
2246
  /* If there is a duplicate definition somewhere, then HI may not
2247
     point to an indirect symbol.  We will have reported an error
2248
     to the user in that case.  */
2249
0
  if (hi->root.type == bfd_link_hash_indirect)
2250
0
    {
2251
0
      obed->elf_backend_copy_indirect_symbol (info, h, hi);
2252
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2253
0
      hi->dynamic_def |= h->dynamic_def;
2254
2255
      /* If we first saw a reference to @VER symbol with
2256
   non-default visibility, merge that visibility to the
2257
   @@VER symbol.  */
2258
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, h, hi->other,
2259
0
        sec, true, dynamic);
2260
2261
      /* See if the new flags lead us to realize that the symbol
2262
   must be dynamic.  */
2263
0
      if (! *dynsym)
2264
0
  {
2265
0
    if (! dynamic)
2266
0
      {
2267
0
        if (! bfd_link_executable (info)
2268
0
      || hi->ref_dynamic)
2269
0
    *dynsym = true;
2270
0
      }
2271
0
    else
2272
0
      {
2273
0
        if (hi->ref_regular)
2274
0
    *dynsym = true;
2275
0
      }
2276
0
  }
2277
0
    }
2278
2279
0
  return true;
2280
0
}
2281

2282
/* This routine is used to export all defined symbols into the dynamic
2283
   symbol table.  It is called via elf_link_hash_traverse.  */
2284
2285
static bool
2286
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2287
0
{
2288
0
  struct elf_info_failed *eif = data;
2289
2290
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2291
0
  if (h->root.type == bfd_link_hash_indirect)
2292
0
    return true;
2293
2294
  /* Ignore this if we won't export it.  */
2295
0
  if (!eif->info->export_dynamic && !h->dynamic)
2296
0
    return true;
2297
2298
0
  if (h->dynindx == -1
2299
0
      && (h->def_regular || h->ref_regular)
2300
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2301
0
            h->root.root.string))
2302
0
    {
2303
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2304
0
  {
2305
0
    eif->failed = true;
2306
0
    return false;
2307
0
  }
2308
0
    }
2309
2310
0
  return true;
2311
0
}
2312

2313
/* Return true if linked against glibc.  Otherwise return false.  If
2314
   linked against glibc, add VERSION_DEP to the list of glibc version
2315
   dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
2316
   true, add VERSION_DEP to the version dependency list only if libc.so
2317
   defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
2318
   minor base version.  */
2319
2320
static bool
2321
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2322
          const char *version_dep,
2323
          int *glibc_minor_base,
2324
          bool *auto_version)
2325
0
{
2326
0
  Elf_Internal_Verneed *t;
2327
0
  Elf_Internal_Vernaux *a;
2328
0
  int minor_version = -1;
2329
0
  bool added = false;
2330
0
  bool glibc = false;
2331
2332
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2333
0
       t != NULL;
2334
0
       t = t->vn_nextref)
2335
0
    {
2336
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2337
0
      if (soname != NULL && startswith (soname, "libc.so."))
2338
0
  break;
2339
0
    }
2340
2341
  /* Skip the shared library if it isn't libc.so.  */
2342
0
  if (t == NULL)
2343
0
    goto update_auto_version_and_return;
2344
2345
0
  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2346
0
    {
2347
      /* Return if VERSION_DEP dependency has been added.  */
2348
0
      if (a->vna_nodename == version_dep
2349
0
    || strcmp (a->vna_nodename, version_dep) == 0)
2350
0
  {
2351
0
    glibc = true;
2352
0
    goto update_auto_version_and_return;
2353
0
  }
2354
2355
      /* Check if libc.so provides GLIBC_2.XX version.  */
2356
0
      if (startswith (a->vna_nodename, "GLIBC_2."))
2357
0
  {
2358
0
    minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2359
0
    if (minor_version < *glibc_minor_base)
2360
0
      *glibc_minor_base = minor_version;
2361
0
  }
2362
0
    }
2363
2364
  /* Skip if it isn't linked against glibc.  */
2365
0
  if (minor_version < 0)
2366
0
    goto update_auto_version_and_return;
2367
2368
0
  glibc = true;
2369
2370
0
  if (auto_version && *auto_version)
2371
0
    {
2372
      /* Add VERSION_DEP to the version dependency list only if
2373
   libc.so defines VERSION_DEP.  */
2374
2375
0
      bool defined = false;
2376
0
      Elf_Internal_Verdef *d;
2377
2378
0
      for (d = elf_tdata (t->vn_bfd)->verdef;
2379
0
     d != NULL;
2380
0
     d = d->vd_nextdef)
2381
0
  if (strcmp (d->vd_nodename, version_dep) == 0)
2382
0
    {
2383
0
      defined = true;
2384
0
      break;
2385
0
    }
2386
2387
      /* Set *AUTO_VERSION to false and return true to indicate that
2388
   libc.so doesn't define VERSION_DEP.  */
2389
0
      if (!defined)
2390
0
  goto update_auto_version_and_return;
2391
0
    }
2392
2393
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2394
0
  if (startswith (version_dep, "GLIBC_2."))
2395
0
    {
2396
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2397
0
      if (minor_version <= *glibc_minor_base)
2398
0
  goto update_auto_version_and_return;
2399
0
    }
2400
2401
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2402
0
  if (a == NULL)
2403
0
    {
2404
0
      rinfo->failed = true;
2405
0
      glibc = false;
2406
0
      goto update_auto_version_and_return;
2407
0
    }
2408
2409
0
  a->vna_nodename = version_dep;
2410
0
  a->vna_flags = 0;
2411
0
  a->vna_nextptr = t->vn_auxptr;
2412
0
  a->vna_other = rinfo->vers + 1;
2413
0
  ++rinfo->vers;
2414
2415
0
  t->vn_auxptr = a;
2416
2417
0
  added = true;
2418
2419
0
 update_auto_version_and_return:
2420
0
  if (auto_version)
2421
0
    *auto_version = added;
2422
2423
0
  return glibc;
2424
0
}
2425
2426
/* Add VERSION_DEP to the list of version dependencies when linked
2427
   against glibc.  */
2428
2429
bool
2430
_bfd_elf_link_add_glibc_version_dependency
2431
  (struct elf_find_verdep_info *rinfo,
2432
   const char *const version_dep[],
2433
   bool *auto_version)
2434
0
{
2435
0
  int glibc_minor_base = INT_MAX;
2436
2437
0
  do
2438
0
    {
2439
      /* Return if not linked against glibc.  */
2440
0
      if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
2441
0
               &glibc_minor_base, auto_version))
2442
0
  return false;
2443
0
      version_dep++;
2444
0
      auto_version++;
2445
0
    }
2446
0
  while (*version_dep != NULL);
2447
2448
0
  return true;
2449
0
}
2450
2451
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2452
   linked against glibc.  */
2453
2454
void
2455
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2456
0
{
2457
0
  if (rinfo->info->enable_dt_relr)
2458
0
    {
2459
0
      static const char *const version[] =
2460
0
  {
2461
0
    "GLIBC_ABI_DT_RELR",
2462
0
    NULL
2463
0
  };
2464
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
2465
0
    }
2466
0
}
2467
2468
/* Look through the symbols which are defined in other shared
2469
   libraries and referenced here.  Update the list of version
2470
   dependencies.  This will be put into the .gnu.version_r section.
2471
   This function is called via elf_link_hash_traverse.  */
2472
2473
static bool
2474
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2475
           void *data)
2476
0
{
2477
0
  struct elf_find_verdep_info *rinfo = data;
2478
0
  Elf_Internal_Verneed *t;
2479
0
  Elf_Internal_Vernaux *a;
2480
2481
  /* We only care about symbols defined in shared objects with version
2482
     information.  */
2483
0
  if (!h->def_dynamic
2484
0
      || h->def_regular
2485
0
      || h->dynindx == -1
2486
0
      || h->verinfo.verdef == NULL
2487
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2488
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2489
0
    return true;
2490
2491
  /* See if we already know about this version.  */
2492
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2493
0
       t != NULL;
2494
0
       t = t->vn_nextref)
2495
0
    {
2496
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2497
0
  continue;
2498
2499
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2500
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2501
0
    return true;
2502
2503
0
      break;
2504
0
    }
2505
2506
  /* This is a new version.  Add it to tree we are building.  */
2507
2508
0
  if (t == NULL)
2509
0
    {
2510
0
      t = bfd_zalloc (rinfo->info->output_bfd, sizeof (*t));
2511
0
      if (t == NULL)
2512
0
  {
2513
0
    rinfo->failed = true;
2514
0
    return false;
2515
0
  }
2516
2517
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2518
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2519
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2520
0
    }
2521
2522
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2523
0
  if (a == NULL)
2524
0
    {
2525
0
      rinfo->failed = true;
2526
0
      return false;
2527
0
    }
2528
2529
  /* Note that we are copying a string pointer here, and testing it
2530
     above.  If bfd_elf_string_from_elf_section is ever changed to
2531
     discard the string data when low in memory, this will have to be
2532
     fixed.  */
2533
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2534
2535
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2536
0
  a->vna_nextptr = t->vn_auxptr;
2537
2538
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2539
0
  ++rinfo->vers;
2540
2541
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2542
2543
0
  t->vn_auxptr = a;
2544
2545
0
  return true;
2546
0
}
2547
2548
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2549
   hidden.  Set *T_P to NULL if there is no match.  */
2550
2551
static bool
2552
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2553
             struct elf_link_hash_entry *h,
2554
             const char *version_p,
2555
             struct bfd_elf_version_tree **t_p,
2556
             bool *hide)
2557
0
{
2558
0
  struct bfd_elf_version_tree *t;
2559
2560
  /* Look for the version.  If we find it, it is no longer weak.  */
2561
0
  for (t = info->version_info; t != NULL; t = t->next)
2562
0
    {
2563
0
      if (strcmp (t->name, version_p) == 0)
2564
0
  {
2565
0
    size_t len;
2566
0
    char *alc;
2567
0
    struct bfd_elf_version_expr *d;
2568
2569
0
    len = version_p - h->root.root.string;
2570
0
    alc = bfd_malloc (len);
2571
0
    if (alc == NULL)
2572
0
      return false;
2573
0
    memcpy (alc, h->root.root.string, len - 1);
2574
0
    alc[len - 1] = '\0';
2575
0
    if (alc[len - 2] == ELF_VER_CHR)
2576
0
      alc[len - 2] = '\0';
2577
2578
0
    h->verinfo.vertree = t;
2579
0
    t->used = true;
2580
0
    d = NULL;
2581
2582
0
    if (t->globals.list != NULL)
2583
0
      d = (*t->match) (&t->globals, NULL, alc);
2584
2585
    /* See if there is anything to force this symbol to
2586
       local scope.  */
2587
0
    if (d == NULL && t->locals.list != NULL)
2588
0
      {
2589
0
        d = (*t->match) (&t->locals, NULL, alc);
2590
0
        if (d != NULL
2591
0
      && h->dynindx != -1
2592
0
      && ! info->export_dynamic)
2593
0
    *hide = true;
2594
0
      }
2595
2596
0
    free (alc);
2597
0
    break;
2598
0
  }
2599
0
    }
2600
2601
0
  *t_p = t;
2602
2603
0
  return true;
2604
0
}
2605
2606
/* Return TRUE if the symbol H is hidden by version script.  */
2607
2608
bool
2609
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2610
           struct elf_link_hash_entry *h)
2611
0
{
2612
0
  const char *p;
2613
0
  bool hide = false;
2614
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
2615
2616
  /* Version script only hides symbols defined in regular objects.  */
2617
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2618
0
    return true;
2619
2620
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2621
0
  if (p != NULL && h->verinfo.vertree == NULL)
2622
0
    {
2623
0
      struct bfd_elf_version_tree *t;
2624
2625
0
      ++p;
2626
0
      if (*p == ELF_VER_CHR)
2627
0
  ++p;
2628
2629
0
      if (*p != '\0'
2630
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2631
0
    && hide)
2632
0
  {
2633
0
    if (hide)
2634
0
      obed->elf_backend_hide_symbol (info, h, true);
2635
0
    return true;
2636
0
  }
2637
0
    }
2638
2639
  /* If we don't have a version for this symbol, see if we can find
2640
     something.  */
2641
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2642
0
    {
2643
0
      h->verinfo.vertree
2644
0
  = bfd_find_version_for_sym (info->version_info,
2645
0
            h->root.root.string, &hide);
2646
0
      if (h->verinfo.vertree != NULL && hide)
2647
0
  {
2648
0
    obed->elf_backend_hide_symbol (info, h, true);
2649
0
    return true;
2650
0
  }
2651
0
    }
2652
2653
0
  return false;
2654
0
}
2655
2656
/* Figure out appropriate versions for all the symbols.  We may not
2657
   have the version number script until we have read all of the input
2658
   files, so until that point we don't know which symbols should be
2659
   local.  This function is called via elf_link_hash_traverse.  */
2660
2661
static bool
2662
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2663
0
{
2664
0
  struct elf_info_failed *sinfo = data;
2665
0
  struct bfd_link_info *info = sinfo->info;
2666
0
  elf_backend_data *obed;
2667
0
  struct elf_info_failed eif;
2668
0
  const char *p;
2669
0
  bool hide;
2670
2671
  /* Fix the symbol flags.  */
2672
0
  eif.failed = false;
2673
0
  eif.info = info;
2674
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2675
0
    {
2676
0
      if (eif.failed)
2677
0
  sinfo->failed = true;
2678
0
      return false;
2679
0
    }
2680
2681
0
  obed = get_elf_backend_data (info->output_bfd);
2682
2683
  /* We only need version numbers for symbols defined in regular
2684
     objects.  */
2685
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2686
0
    {
2687
      /* Hide symbols defined in discarded input sections.  */
2688
0
      if ((h->root.type == bfd_link_hash_defined
2689
0
     || h->root.type == bfd_link_hash_defweak)
2690
0
    && discarded_section (h->root.u.def.section))
2691
0
  obed->elf_backend_hide_symbol (info, h, true);
2692
0
      return true;
2693
0
    }
2694
2695
0
  hide = false;
2696
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2697
0
  if (p != NULL && h->verinfo.vertree == NULL)
2698
0
    {
2699
0
      struct bfd_elf_version_tree *t;
2700
0
      bool default_version = false;
2701
2702
0
      ++p;
2703
0
      if (*p == ELF_VER_CHR)
2704
0
  {
2705
0
    default_version = true;
2706
0
    ++p;
2707
0
  }
2708
2709
      /* If there is no version string, we can just return out.  */
2710
0
      if (*p == '\0')
2711
0
  return true;
2712
2713
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2714
0
  {
2715
0
    sinfo->failed = true;
2716
0
    return false;
2717
0
  }
2718
2719
0
      if (hide)
2720
0
  obed->elf_backend_hide_symbol (info, h, true);
2721
0
      else if (default_version)
2722
0
  {
2723
    /* Get the unversioned symbol for the default version.  */
2724
0
    struct elf_link_hash_entry *h_u;
2725
0
    size_t size = p - h->root.root.string - 1;
2726
0
    char *unversioned_name = bfd_malloc (size);
2727
0
    if (unversioned_name == NULL)
2728
0
      {
2729
0
        sinfo->failed = true;
2730
0
        return false;
2731
0
      }
2732
0
    memcpy (unversioned_name, h->root.root.string, size - 1);
2733
0
    unversioned_name[size - 1] = 0;
2734
0
    h_u = elf_link_hash_lookup (elf_hash_table (info),
2735
0
              unversioned_name, false,
2736
0
              false, false);
2737
2738
    /* There must be an unversioned symbol. */
2739
0
    if (h_u == NULL)
2740
0
      abort ();
2741
2742
0
    while (h_u->root.type == bfd_link_hash_indirect
2743
0
     || h_u->root.type == bfd_link_hash_warning)
2744
0
      h_u = (struct elf_link_hash_entry *) h_u->root.u.i.link;
2745
2746
    /* Verify that there is only one default version.  */
2747
0
    if (h_u->versioned != versioned_hidden
2748
0
        && h_u->verinfo.vertree != h->verinfo.vertree)
2749
0
      {
2750
        /* xgettext:c-format */
2751
0
        info->callbacks->einfo
2752
0
    (_("%X%P: %pB: multiple default versions of `%s': "
2753
0
       "`%s' in %pB and `%s' in %pB.\n"),
2754
0
     info->output_bfd, unversioned_name,
2755
0
     h_u->verinfo.vertree->name,
2756
0
     h_u->root.u.def.section->owner,
2757
0
     h->verinfo.vertree->name,
2758
0
     h->root.u.def.section->owner);
2759
0
        bfd_set_error (bfd_error_bad_value);
2760
0
        sinfo->failed = true;
2761
0
        free (unversioned_name);
2762
0
        return false;
2763
0
      }
2764
2765
0
    free (unversioned_name);
2766
0
  }
2767
2768
      /* If we are building an application, we need to create a
2769
   version node for this version.  */
2770
0
      if (t == NULL && bfd_link_executable (info))
2771
0
  {
2772
0
    struct bfd_elf_version_tree **pp;
2773
0
    int version_index;
2774
2775
    /* If we aren't going to export this symbol, we don't need
2776
       to worry about it.  */
2777
0
    if (h->dynindx == -1)
2778
0
      return true;
2779
2780
0
    t = bfd_zalloc (info->output_bfd, sizeof (*t));
2781
0
    if (t == NULL)
2782
0
      {
2783
0
        sinfo->failed = true;
2784
0
        return false;
2785
0
      }
2786
2787
0
    t->name = p;
2788
0
    t->name_indx = (unsigned int) -1;
2789
0
    t->used = true;
2790
2791
0
    version_index = 1;
2792
    /* Don't count anonymous version tag.  */
2793
0
    if (sinfo->info->version_info != NULL
2794
0
        && sinfo->info->version_info->vernum == 0)
2795
0
      version_index = 0;
2796
0
    for (pp = &sinfo->info->version_info;
2797
0
         *pp != NULL;
2798
0
         pp = &(*pp)->next)
2799
0
      ++version_index;
2800
0
    t->vernum = version_index;
2801
2802
0
    *pp = t;
2803
2804
0
    h->verinfo.vertree = t;
2805
0
  }
2806
0
      else if (t == NULL)
2807
0
  {
2808
    /* We could not find the version for a symbol when
2809
       generating a shared archive.  Return an error.  */
2810
0
    _bfd_error_handler
2811
      /* xgettext:c-format */
2812
0
      (_("%pB: version node not found for symbol %s"),
2813
0
       info->output_bfd, h->root.root.string);
2814
0
    bfd_set_error (bfd_error_bad_value);
2815
0
    sinfo->failed = true;
2816
0
    return false;
2817
0
  }
2818
0
    }
2819
2820
  /* If we don't have a version for this symbol, see if we can find
2821
     something.  */
2822
0
  if (!hide
2823
0
      && h->verinfo.vertree == NULL
2824
0
      && sinfo->info->version_info != NULL)
2825
0
    {
2826
0
      h->verinfo.vertree
2827
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2828
0
            h->root.root.string, &hide);
2829
0
      if (h->verinfo.vertree != NULL && hide)
2830
0
  obed->elf_backend_hide_symbol (info, h, true);
2831
0
    }
2832
2833
0
  return true;
2834
0
}
2835

2836
/* Read and swap the relocs from the section indicated by SHDR.  This
2837
   may be either a REL or a RELA section.  The relocations are
2838
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2839
   which should have already been allocated to contain enough space.
2840
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2841
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2842
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2843
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2844
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2845
2846
   Returns FALSE if something goes wrong.  */
2847
2848
static bool
2849
elf_link_read_relocs_from_section (bfd *abfd,
2850
           const asection *sec,
2851
           Elf_Internal_Shdr *shdr,
2852
           void **external_relocs_addr,
2853
           size_t *external_relocs_size,
2854
           Elf_Internal_Rela *internal_relocs)
2855
0
{
2856
0
  elf_backend_data *bed;
2857
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2858
0
  const bfd_byte *erela;
2859
0
  const bfd_byte *erelaend;
2860
0
  Elf_Internal_Rela *irela;
2861
0
  Elf_Internal_Shdr *symtab_hdr;
2862
0
  size_t nsyms;
2863
0
  void *external_relocs = *external_relocs_addr;
2864
2865
  /* Position ourselves at the start of the section.  */
2866
0
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2867
0
    return false;
2868
2869
  /* Read the relocations.  */
2870
0
  *external_relocs_size = shdr->sh_size;
2871
0
  if (!_bfd_mmap_read_temporary (&external_relocs,
2872
0
         external_relocs_size,
2873
0
         external_relocs_addr, abfd, true))
2874
0
    return false;
2875
2876
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2877
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2878
2879
0
  bed = get_elf_backend_data (abfd);
2880
2881
  /* Convert the external relocations to the internal format.  */
2882
0
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2883
0
    swap_in = bed->s->swap_reloc_in;
2884
0
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2885
0
    swap_in = bed->s->swap_reloca_in;
2886
0
  else
2887
0
    {
2888
0
      bfd_set_error (bfd_error_wrong_format);
2889
0
      return false;
2890
0
    }
2891
2892
0
  erela = (const bfd_byte *) external_relocs;
2893
  /* Setting erelaend like this and comparing with <= handles case of
2894
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2895
0
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2896
0
  irela = internal_relocs;
2897
0
  while (erela <= erelaend)
2898
0
    {
2899
0
      bfd_vma r_symndx;
2900
2901
0
      (*swap_in) (abfd, erela, irela);
2902
0
      r_symndx = ELF32_R_SYM (irela->r_info);
2903
0
      if (bed->s->arch_size == 64)
2904
0
  r_symndx >>= 24;
2905
0
      if (nsyms > 0)
2906
0
  {
2907
0
    if ((size_t) r_symndx >= nsyms)
2908
0
      {
2909
0
        _bfd_error_handler
2910
    /* xgettext:c-format */
2911
0
    (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2912
0
       " for offset %#" PRIx64 " in section `%pA'"),
2913
0
     abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2914
0
     (uint64_t) irela->r_offset, sec);
2915
0
        bfd_set_error (bfd_error_bad_value);
2916
0
        return false;
2917
0
      }
2918
0
  }
2919
0
      else if (r_symndx != STN_UNDEF)
2920
0
  {
2921
0
    _bfd_error_handler
2922
      /* xgettext:c-format */
2923
0
      (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2924
0
         " for offset %#" PRIx64 " in section `%pA'"
2925
0
         " when the object file has no symbol table"),
2926
0
       abfd, (uint64_t) r_symndx,
2927
0
       (uint64_t) irela->r_offset, sec);
2928
0
    bfd_set_error (bfd_error_bad_value);
2929
0
    return false;
2930
0
  }
2931
0
      irela += bed->s->int_rels_per_ext_rel;
2932
0
      erela += shdr->sh_entsize;
2933
0
    }
2934
2935
0
  return true;
2936
0
}
2937
2938
/* Read and swap the relocs for a section O.  They may have been
2939
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2940
   not NULL, they are used as buffers to read into.  They are known to
2941
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2942
   the return value is allocated using malloc.  If O has both a REL
2943
   relocation section and a RELA relocation section, then the REL
2944
   relocations will appear first in INTERNAL_RELOCS, followed by the
2945
   RELA relocations.  If KEEP_MEMORY is true the internal relocs are
2946
   cached, with info->cache_size updated if INFO is non-NULL.  */
2947
2948
Elf_Internal_Rela *
2949
_bfd_elf_link_info_read_relocs (bfd *abfd,
2950
        struct bfd_link_info *info,
2951
        const asection *o,
2952
        void *external_relocs,
2953
        Elf_Internal_Rela *internal_relocs,
2954
        bool keep_memory)
2955
0
{
2956
0
  void *ext_alloc1, *ext_alloc2;
2957
0
  size_t ext_mmap1, ext_mmap2;
2958
0
  Elf_Internal_Rela *int_alloc = NULL;
2959
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2960
0
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2961
0
  Elf_Internal_Rela *internal_rela_relocs;
2962
2963
0
  if (esdo->relocs != NULL)
2964
0
    return esdo->relocs;
2965
2966
0
  if (o->reloc_count == 0)
2967
0
    return NULL;
2968
2969
0
  if (internal_relocs == NULL)
2970
0
    {
2971
0
      bfd_size_type size;
2972
2973
0
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2974
0
      if (keep_memory && info)
2975
0
  info->cache_size += size;
2976
0
      internal_relocs = int_alloc = bfd_malloc (size);
2977
0
      if (internal_relocs == NULL)
2978
0
  return NULL;
2979
0
    }
2980
2981
0
  ext_alloc1 = external_relocs;
2982
0
  ext_mmap1 = 0;
2983
0
  internal_rela_relocs = internal_relocs;
2984
0
  if (esdo->rel.hdr)
2985
0
    {
2986
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2987
0
                &ext_alloc1, &ext_mmap1,
2988
0
                internal_relocs))
2989
0
  {
2990
0
    internal_relocs = NULL;
2991
0
    goto out1;
2992
0
  }
2993
0
      if (external_relocs != NULL)
2994
0
  external_relocs = (((bfd_byte *) external_relocs)
2995
0
         + esdo->rel.hdr->sh_size);
2996
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2997
0
             * bed->s->int_rels_per_ext_rel);
2998
0
    }
2999
3000
0
  ext_alloc2 = external_relocs;
3001
0
  ext_mmap2 = 0;
3002
0
  if (esdo->rela.hdr
3003
0
      && !elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
3004
0
               &ext_alloc2, &ext_mmap2,
3005
0
               internal_rela_relocs))
3006
0
    internal_relocs = NULL;
3007
3008
  /* If the external relocs were mmap'd then we want to munmap them
3009
     regardless of whether or not an external_relocs buffer was
3010
     provided.  If they were not mmap'd then we only want to free
3011
     buffers allocated here.  */
3012
0
  if (ext_mmap2 != 0 || external_relocs == NULL)
3013
0
    _bfd_munmap_temporary (ext_alloc2, ext_mmap2);
3014
0
 out1:
3015
0
  if (ext_mmap1 != 0 || external_relocs == NULL)
3016
0
    _bfd_munmap_temporary (ext_alloc1, ext_mmap1);
3017
3018
  /* Don't free int_alloc, if we are returning it under the name of
3019
     internal_relocs.  */
3020
0
  if (internal_relocs == NULL)
3021
0
    free (int_alloc);
3022
3023
  /* Cache the results for next time, if we can.  */
3024
0
  else if (keep_memory)
3025
0
    esdo->relocs = internal_relocs;
3026
3027
0
  return internal_relocs;
3028
0
}
3029
3030
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
3031
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
3032
   struct bfd_link_info.  */
3033
3034
Elf_Internal_Rela *
3035
_bfd_elf_link_read_relocs (bfd *abfd,
3036
         const asection *o,
3037
         void *external_relocs,
3038
         Elf_Internal_Rela *internal_relocs,
3039
         bool keep_memory)
3040
0
{
3041
0
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
3042
0
           internal_relocs, keep_memory);
3043
3044
0
}
3045
3046
/* Compute the size of, and allocate space for, REL_HDR which is the
3047
   section header for a section containing relocations for O.  */
3048
3049
static bool
3050
_bfd_elf_link_size_reloc_section (bfd *obfd,
3051
          struct bfd_elf_section_reloc_data *reldata)
3052
0
{
3053
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
3054
3055
  /* That allows us to calculate the size of the section.  */
3056
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
3057
3058
  /* The contents field must last into write_object_contents, so we
3059
     allocate it with bfd_alloc rather than malloc.  Also since we
3060
     cannot be sure that the contents will actually be filled in,
3061
     we zero the allocated space.  */
3062
0
  rel_hdr->contents = bfd_zalloc (obfd, rel_hdr->sh_size);
3063
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3064
0
    return false;
3065
3066
0
  if (reldata->hashes == NULL && reldata->count)
3067
0
    {
3068
0
      struct elf_link_hash_entry **p;
3069
3070
0
      p = bfd_zmalloc (reldata->count * sizeof (*p));
3071
0
      if (p == NULL)
3072
0
  return false;
3073
3074
0
      reldata->hashes = p;
3075
0
    }
3076
3077
0
  return true;
3078
0
}
3079
3080
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3081
   originated from the section given by INPUT_REL_HDR) to the
3082
   OUTPUT_BFD.  */
3083
3084
bool
3085
_bfd_elf_link_output_relocs (bfd *output_bfd,
3086
           asection *input_section,
3087
           Elf_Internal_Shdr *input_rel_hdr,
3088
           Elf_Internal_Rela *internal_relocs,
3089
           struct elf_link_hash_entry **rel_hash)
3090
0
{
3091
0
  Elf_Internal_Rela *irela;
3092
0
  Elf_Internal_Rela *irelaend;
3093
0
  bfd_byte *erel;
3094
0
  struct bfd_elf_section_reloc_data *output_reldata;
3095
0
  asection *output_section;
3096
0
  elf_backend_data *obed;
3097
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3098
0
  struct bfd_elf_section_data *esdo;
3099
3100
0
  output_section = input_section->output_section;
3101
3102
0
  obed = get_elf_backend_data (output_bfd);
3103
0
  esdo = elf_section_data (output_section);
3104
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3105
0
    {
3106
0
      output_reldata = &esdo->rel;
3107
0
      swap_out = obed->s->swap_reloc_out;
3108
0
    }
3109
0
  else if (esdo->rela.hdr
3110
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3111
0
    {
3112
0
      output_reldata = &esdo->rela;
3113
0
      swap_out = obed->s->swap_reloca_out;
3114
0
    }
3115
0
  else
3116
0
    {
3117
0
      _bfd_error_handler
3118
  /* xgettext:c-format */
3119
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3120
0
   output_bfd, input_section->owner, input_section);
3121
0
      bfd_set_error (bfd_error_wrong_format);
3122
0
      return false;
3123
0
    }
3124
3125
0
  erel = output_reldata->hdr->contents;
3126
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3127
0
  irela = internal_relocs;
3128
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3129
0
          * obed->s->int_rels_per_ext_rel);
3130
0
  while (irela < irelaend)
3131
0
    {
3132
0
      if (rel_hash && *rel_hash)
3133
0
  (*rel_hash)->has_reloc = 1;
3134
0
      (*swap_out) (output_bfd, irela, erel);
3135
0
      irela += obed->s->int_rels_per_ext_rel;
3136
0
      erel += input_rel_hdr->sh_entsize;
3137
0
      if (rel_hash)
3138
0
  rel_hash++;
3139
0
    }
3140
3141
  /* Bump the counter, so that we know where to add the next set of
3142
     relocations.  */
3143
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3144
3145
0
  return true;
3146
0
}
3147

3148
/* Make weak undefined symbols in PIE dynamic.  */
3149
3150
bool
3151
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3152
         struct elf_link_hash_entry *h)
3153
0
{
3154
0
  if (bfd_link_pie (info)
3155
0
      && h->dynindx == -1
3156
0
      && h->root.type == bfd_link_hash_undefweak)
3157
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3158
3159
0
  return true;
3160
0
}
3161
3162
/* Fix up the flags for a symbol.  This handles various cases which
3163
   can only be fixed after all the input files are seen.  This is
3164
   currently called by both adjust_dynamic_symbol and
3165
   assign_sym_version, which is unnecessary but perhaps more robust in
3166
   the face of future changes.  */
3167
3168
static bool
3169
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3170
         struct elf_info_failed *eif)
3171
0
{
3172
0
  elf_backend_data *obed;
3173
3174
  /* If this symbol was mentioned in a non-ELF file, try to set
3175
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3176
     permit a non-ELF file to correctly refer to a symbol defined in
3177
     an ELF dynamic object.  */
3178
0
  if (h->non_elf)
3179
0
    {
3180
0
      while (h->root.type == bfd_link_hash_indirect)
3181
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3182
3183
0
      if (h->root.type != bfd_link_hash_defined
3184
0
    && h->root.type != bfd_link_hash_defweak)
3185
0
  {
3186
0
    h->ref_regular = 1;
3187
0
    h->ref_regular_nonweak = 1;
3188
0
  }
3189
0
      else
3190
0
  {
3191
0
    if (h->root.u.def.section->owner != NULL
3192
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3193
0
      == bfd_target_elf_flavour))
3194
0
      {
3195
0
        h->ref_regular = 1;
3196
0
        h->ref_regular_nonweak = 1;
3197
0
      }
3198
0
    else
3199
0
      h->def_regular = 1;
3200
0
  }
3201
3202
0
      if (h->dynindx == -1
3203
0
    && (h->def_dynamic
3204
0
        || h->ref_dynamic))
3205
0
  {
3206
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3207
0
      {
3208
0
        eif->failed = true;
3209
0
        return false;
3210
0
      }
3211
0
  }
3212
0
    }
3213
0
  else
3214
0
    {
3215
      /* Unfortunately, NON_ELF is only correct if the symbol
3216
   was first seen in a non-ELF file.  Fortunately, if the symbol
3217
   was first seen in an ELF file, we're probably OK unless the
3218
   symbol was defined in a non-ELF file.  Catch that case here.
3219
   FIXME: We're still in trouble if the symbol was first seen in
3220
   a dynamic object, and then later in a non-ELF regular object.  */
3221
0
      if ((h->root.type == bfd_link_hash_defined
3222
0
     || h->root.type == bfd_link_hash_defweak)
3223
0
    && !h->def_regular
3224
0
    && (h->root.u.def.section->owner != NULL
3225
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3226
0
     != bfd_target_elf_flavour)
3227
0
        : (bfd_is_abs_section (h->root.u.def.section)
3228
0
     && !h->def_dynamic)))
3229
0
  h->def_regular = 1;
3230
0
    }
3231
3232
  /* Backend specific symbol fixup.  */
3233
0
  obed = get_elf_backend_data (eif->info->output_bfd);
3234
0
  if (obed->elf_backend_fixup_symbol
3235
0
      && !obed->elf_backend_fixup_symbol (eif->info, h))
3236
0
    return false;
3237
3238
  /* If this is a final link, and the symbol was defined as a common
3239
     symbol in a regular object file, and there was no definition in
3240
     any dynamic object, then the linker will have allocated space for
3241
     the symbol in a common section but the DEF_REGULAR
3242
     flag will not have been set.  */
3243
0
  if (h->root.type == bfd_link_hash_defined
3244
0
      && !h->def_regular
3245
0
      && h->ref_regular
3246
0
      && !h->def_dynamic
3247
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3248
0
    h->def_regular = 1;
3249
3250
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3251
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3252
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3253
3254
  /* If a weak undefined symbol has non-default visibility, we also
3255
     hide it from the dynamic linker.  */
3256
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3257
0
     && h->root.type == bfd_link_hash_undefweak)
3258
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3259
3260
  /* A hidden versioned symbol in executable should be forced local if
3261
     it is is locally defined, not referenced by shared library and not
3262
     exported.  */
3263
0
  else if (bfd_link_executable (eif->info)
3264
0
     && h->versioned == versioned_hidden
3265
0
     && !eif->info->export_dynamic
3266
0
     && !h->dynamic
3267
0
     && !h->ref_dynamic
3268
0
     && h->def_regular)
3269
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3270
3271
  /* If -Bsymbolic was used (which means to bind references to global
3272
     symbols to the definition within the shared object), and this
3273
     symbol was defined in a regular object, then it actually doesn't
3274
     need a PLT entry.  Likewise, if the symbol has non-default
3275
     visibility.  If the symbol has hidden or internal visibility, we
3276
     will force it local.  */
3277
0
  else if (h->needs_plt
3278
0
     && bfd_link_pic (eif->info)
3279
0
     && is_elf_hash_table (eif->info->hash)
3280
0
     && (SYMBOLIC_BIND (eif->info, h)
3281
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3282
0
     && h->def_regular)
3283
0
    {
3284
0
      bool force_local;
3285
3286
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3287
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3288
0
      obed->elf_backend_hide_symbol (eif->info, h, force_local);
3289
0
    }
3290
3291
  /* If this is a weak defined symbol in a dynamic object, and we know
3292
     the real definition in the dynamic object, copy interesting flags
3293
     over to the real definition.  */
3294
0
  if (h->is_weakalias)
3295
0
    {
3296
0
      struct elf_link_hash_entry *def = weakdef (h);
3297
3298
      /* If the real definition is defined by a regular object file,
3299
   don't do anything special.  See the longer description in
3300
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3301
   bfd_link_hash_defined as it was when put on the alias list
3302
   then it must have originally been a versioned symbol (for
3303
   which a non-versioned indirect symbol is created) and later
3304
   a definition for the non-versioned symbol is found.  In that
3305
   case the indirection is flipped with the versioned symbol
3306
   becoming an indirect pointing at the non-versioned symbol.
3307
   Thus, not an alias any more.  */
3308
0
      if (def->def_regular
3309
0
    || def->root.type != bfd_link_hash_defined)
3310
0
  {
3311
0
    h = def;
3312
0
    while ((h = h->u.alias) != def)
3313
0
      h->is_weakalias = 0;
3314
0
  }
3315
0
      else
3316
0
  {
3317
0
    while (h->root.type == bfd_link_hash_indirect)
3318
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3319
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3320
0
          || h->root.type == bfd_link_hash_defweak);
3321
0
    BFD_ASSERT (def->def_dynamic);
3322
0
    obed->elf_backend_copy_indirect_symbol (eif->info, def, h);
3323
0
  }
3324
0
    }
3325
3326
0
  return true;
3327
0
}
3328
3329
/* Make the backend pick a good value for a dynamic symbol.  This is
3330
   called via elf_link_hash_traverse, and also calls itself
3331
   recursively.  */
3332
3333
static bool
3334
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3335
0
{
3336
0
  struct elf_info_failed *eif = data;
3337
0
  struct elf_link_hash_table *htab;
3338
0
  elf_backend_data *obed;
3339
3340
0
  if (! is_elf_hash_table (eif->info->hash))
3341
0
    return false;
3342
3343
0
  htab = elf_hash_table (eif->info);
3344
0
  if (h->forced_local && h->dynindx != -1)
3345
0
    htab->has_local_dynsyms = true;
3346
3347
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3348
0
  if (h->root.type == bfd_link_hash_indirect)
3349
0
    return true;
3350
3351
  /* Fix the symbol flags.  */
3352
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3353
0
    return false;
3354
3355
0
  obed = get_elf_backend_data (htab->dynobj);
3356
3357
0
  if (h->root.type == bfd_link_hash_undefweak)
3358
0
    {
3359
0
      if (eif->info->dynamic_undefined_weak == 0)
3360
0
  obed->elf_backend_hide_symbol (eif->info, h, true);
3361
0
      else if (eif->info->dynamic_undefined_weak > 0
3362
0
         && h->ref_regular
3363
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3364
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3365
0
              h->root.root.string))
3366
0
  {
3367
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3368
0
      {
3369
0
        eif->failed = true;
3370
0
        return false;
3371
0
      }
3372
0
  }
3373
0
    }
3374
3375
  /* If this symbol does not require a PLT entry, and it is not
3376
     defined by a dynamic object, or is not referenced by a regular
3377
     object, ignore it.  We do have to handle a weak defined symbol,
3378
     even if no regular object refers to it, if we decided to add it
3379
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3380
     about symbols which are defined by one dynamic object and
3381
     referenced by another one?  */
3382
0
  if (!h->needs_plt
3383
0
      && h->type != STT_GNU_IFUNC
3384
0
      && (h->def_regular
3385
0
    || !h->def_dynamic
3386
0
    || (!h->ref_regular
3387
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3388
0
    {
3389
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3390
0
      return true;
3391
0
    }
3392
3393
  /* If we've already adjusted this symbol, don't do it again.  This
3394
     can happen via a recursive call.  */
3395
0
  if (h->dynamic_adjusted)
3396
0
    return true;
3397
3398
  /* Don't look at this symbol again.  Note that we must set this
3399
     after checking the above conditions, because we may look at a
3400
     symbol once, decide not to do anything, and then get called
3401
     recursively later after REF_REGULAR is set below.  */
3402
0
  h->dynamic_adjusted = 1;
3403
3404
  /* If this is a weak definition, and we know a real definition, and
3405
     the real symbol is not itself defined by a regular object file,
3406
     then get a good value for the real definition.  We handle the
3407
     real symbol first, for the convenience of the backend routine.
3408
3409
     Note that there is a confusing case here.  If the real definition
3410
     is defined by a regular object file, we don't get the real symbol
3411
     from the dynamic object, but we do get the weak symbol.  If the
3412
     processor backend uses a COPY reloc, then if some routine in the
3413
     dynamic object changes the real symbol, we will not see that
3414
     change in the corresponding weak symbol.  This is the way other
3415
     ELF linkers work as well, and seems to be a result of the shared
3416
     library model.
3417
3418
     I will clarify this issue.  Most SVR4 shared libraries define the
3419
     variable _timezone and define timezone as a weak synonym.  The
3420
     tzset call changes _timezone.  If you write
3421
       extern int timezone;
3422
       int _timezone = 5;
3423
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3424
     you might expect that, since timezone is a synonym for _timezone,
3425
     the same number will print both times.  However, if the processor
3426
     backend uses a COPY reloc, then actually timezone will be copied
3427
     into your process image, and, since you define _timezone
3428
     yourself, _timezone will not.  Thus timezone and _timezone will
3429
     wind up at different memory locations.  The tzset call will set
3430
     _timezone, leaving timezone unchanged.  */
3431
3432
0
  if (h->is_weakalias)
3433
0
    {
3434
0
      struct elf_link_hash_entry *def = weakdef (h);
3435
3436
      /* If we get to this point, there is an implicit reference to
3437
   the alias by a regular object file via the weak symbol H.  */
3438
0
      def->ref_regular = 1;
3439
3440
      /* Ensure that the backend adjust_dynamic_symbol function sees
3441
   the strong alias before H by recursively calling ourselves.  */
3442
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3443
0
  return false;
3444
0
    }
3445
3446
  /* If a symbol has no type and no size and does not require a PLT
3447
     entry, then we are probably about to do the wrong thing here: we
3448
     are probably going to create a COPY reloc for an empty object.
3449
     This case can arise when a shared object is built with assembly
3450
     code, and the assembly code fails to set the symbol type.  */
3451
0
  if (h->size == 0
3452
0
      && h->type == STT_NOTYPE
3453
0
      && !h->needs_plt)
3454
0
    _bfd_error_handler
3455
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3456
0
       h->root.root.string);
3457
3458
0
  if (!obed->elf_backend_adjust_dynamic_symbol (eif->info, h))
3459
0
    {
3460
0
      eif->failed = true;
3461
0
      return false;
3462
0
    }
3463
3464
0
  return true;
3465
0
}
3466
3467
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3468
   DYNBSS.  */
3469
3470
bool
3471
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3472
            struct elf_link_hash_entry *h,
3473
            asection *dynbss)
3474
0
{
3475
0
  unsigned int power_of_two;
3476
0
  bfd_vma mask;
3477
0
  asection *sec = h->root.u.def.section;
3478
3479
  /* The section alignment of the definition is the maximum alignment
3480
     requirement of symbols defined in the section.  Since we don't
3481
     know the symbol alignment requirement, we start with the
3482
     maximum alignment and check low bits of the symbol address
3483
     for the minimum alignment.  */
3484
0
  power_of_two = bfd_section_alignment (sec);
3485
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3486
0
  while ((h->root.u.def.value & mask) != 0)
3487
0
    {
3488
0
       mask >>= 1;
3489
0
       --power_of_two;
3490
0
    }
3491
3492
  /* Adjust the section alignment if needed.  */
3493
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3494
0
    return false;
3495
3496
  /* We make sure that the symbol will be aligned properly.  */
3497
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3498
3499
  /* Define the symbol as being at this point in DYNBSS.  */
3500
0
  h->root.u.def.section = dynbss;
3501
0
  h->root.u.def.value = dynbss->size;
3502
3503
  /* Increment the size of DYNBSS to make room for the symbol.  */
3504
0
  dynbss->size += h->size;
3505
3506
  /* No error if extern_protected_data is true.  */
3507
0
  if (h->protected_def
3508
0
      && (!info->extern_protected_data
3509
0
    || (info->extern_protected_data < 0
3510
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3511
0
    info->callbacks->einfo
3512
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3513
0
       h->root.root.string);
3514
3515
0
  return true;
3516
0
}
3517
3518
/* Adjust all external symbols pointing into SEC_MERGE sections
3519
   to reflect the object merging within the sections.  */
3520
3521
static bool
3522
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3523
0
{
3524
0
  asection *sec;
3525
3526
0
  if ((h->root.type == bfd_link_hash_defined
3527
0
       || h->root.type == bfd_link_hash_defweak)
3528
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3529
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3530
0
    {
3531
0
      bfd *output_bfd = data;
3532
3533
0
      h->root.u.def.value =
3534
0
  _bfd_merged_section_offset (output_bfd,
3535
0
            &h->root.u.def.section,
3536
0
            h->root.u.def.value);
3537
0
    }
3538
3539
0
  return true;
3540
0
}
3541
3542
/* Returns false if the symbol referred to by H should be considered
3543
   to resolve local to the current module, and true if it should be
3544
   considered to bind dynamically.  */
3545
3546
bool
3547
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3548
         struct bfd_link_info *info,
3549
         bool not_local_protected)
3550
0
{
3551
0
  bool binding_stays_local_p;
3552
0
  elf_backend_data *obed;
3553
0
  struct elf_link_hash_table *hash_table;
3554
3555
0
  if (h == NULL)
3556
0
    return false;
3557
3558
0
  while (h->root.type == bfd_link_hash_indirect
3559
0
   || h->root.type == bfd_link_hash_warning)
3560
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3561
3562
  /* If it was forced local, then clearly it's not dynamic.  */
3563
0
  if (h->dynindx == -1)
3564
0
    return false;
3565
0
  if (h->forced_local)
3566
0
    return false;
3567
3568
  /* Identify the cases where name binding rules say that a
3569
     visible symbol resolves locally.  */
3570
0
  binding_stays_local_p = (bfd_link_executable (info)
3571
0
         || SYMBOLIC_BIND (info, h));
3572
3573
0
  switch (ELF_ST_VISIBILITY (h->other))
3574
0
    {
3575
0
    case STV_INTERNAL:
3576
0
    case STV_HIDDEN:
3577
0
      return false;
3578
3579
0
    case STV_PROTECTED:
3580
0
      hash_table = elf_hash_table (info);
3581
0
      if (!is_elf_hash_table (&hash_table->root))
3582
0
  return false;
3583
3584
0
      obed = get_elf_backend_data (hash_table->dynobj);
3585
3586
      /* Proper resolution for function pointer equality may require
3587
   that these symbols perhaps be resolved dynamically, even though
3588
   we should be resolving them to the current module.  */
3589
0
      if (!not_local_protected || !obed->is_function_type (h->type))
3590
0
  binding_stays_local_p = true;
3591
0
      break;
3592
3593
0
    default:
3594
0
      break;
3595
0
    }
3596
3597
  /* If it isn't defined locally, then clearly it's dynamic.  */
3598
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3599
0
    return true;
3600
3601
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3602
     us that it remains local.  */
3603
0
  return !binding_stays_local_p;
3604
0
}
3605
3606
/* Return true if the symbol referred to by H should be considered
3607
   to resolve local to the current module, and false otherwise.  Differs
3608
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3609
   undefined symbols.  The two functions are virtually identical except
3610
   for the place where dynindx == -1 is tested.  If that test is true,
3611
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3612
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3613
   defined symbols.
3614
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3615
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3616
   treatment of undefined weak symbols.  For those that do not make
3617
   undefined weak symbols dynamic, both functions may return false.  */
3618
3619
bool
3620
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3621
            struct bfd_link_info *info,
3622
            bool local_protected)
3623
0
{
3624
0
  elf_backend_data *obed;
3625
0
  struct elf_link_hash_table *hash_table;
3626
3627
  /* If it's a local sym, of course we resolve locally.  */
3628
0
  if (h == NULL)
3629
0
    return true;
3630
3631
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3632
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3633
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3634
0
    return true;
3635
3636
  /* Forced local symbols resolve locally.  */
3637
0
  if (h->forced_local)
3638
0
    return true;
3639
3640
  /* Common symbols that become definitions don't get the DEF_REGULAR
3641
     flag set, so test it first, and don't bail out.  */
3642
0
  if (ELF_COMMON_DEF_P (h))
3643
0
    /* Do nothing.  */;
3644
  /* If we don't have a definition in a regular file, then we can't
3645
     resolve locally.  The sym is either undefined or dynamic.  */
3646
0
  else if (!h->def_regular)
3647
0
    return false;
3648
3649
  /* Non-dynamic symbols resolve locally.  */
3650
0
  if (h->dynindx == -1)
3651
0
    return true;
3652
3653
  /* At this point, we know the symbol is defined and dynamic.  In an
3654
     executable it must resolve locally, likewise when building symbolic
3655
     shared libraries.  */
3656
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3657
0
    return true;
3658
3659
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3660
     with default visibility might not resolve locally.  */
3661
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3662
0
    return false;
3663
3664
0
  hash_table = elf_hash_table (info);
3665
0
  if (!is_elf_hash_table (&hash_table->root))
3666
0
    return true;
3667
3668
  /* STV_PROTECTED symbols with indirect external access are local. */
3669
0
  if (info->indirect_extern_access > 0)
3670
0
    return true;
3671
3672
0
  obed = get_elf_backend_data (hash_table->dynobj);
3673
3674
  /* If extern_protected_data is false, STV_PROTECTED non-function
3675
     symbols are local.  */
3676
0
  if ((!info->extern_protected_data
3677
0
       || (info->extern_protected_data < 0
3678
0
     && !obed->extern_protected_data))
3679
0
      && !obed->is_function_type (h->type))
3680
0
    return true;
3681
3682
  /* Function pointer equality tests may require that STV_PROTECTED
3683
     symbols be treated as dynamic symbols.  If the address of a
3684
     function not defined in an executable is set to that function's
3685
     plt entry in the executable, then the address of the function in
3686
     a shared library must also be the plt entry in the executable.  */
3687
0
  return local_protected;
3688
0
}
3689
3690
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3691
   aligned.  Returns the first TLS output section.  */
3692
3693
struct bfd_section *
3694
bfd_elf_tls_setup (struct bfd_link_info *info)
3695
0
{
3696
0
  struct bfd_section *sec, *tls;
3697
0
  unsigned int align = 0;
3698
3699
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
3700
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3701
0
      break;
3702
0
  tls = sec;
3703
3704
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3705
0
    if (sec->alignment_power > align)
3706
0
      align = sec->alignment_power;
3707
3708
0
  elf_hash_table (info)->tls_sec = tls;
3709
3710
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3711
     alignment, so that the tls segment starts aligned.  */
3712
0
  if (tls != NULL)
3713
0
    (void) bfd_link_align_section (tls, align);
3714
3715
0
  return tls;
3716
0
}
3717
3718
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3719
static bool
3720
is_global_data_symbol_definition (bfd *abfd,
3721
          Elf_Internal_Sym *sym)
3722
0
{
3723
0
  elf_backend_data *bed;
3724
3725
  /* Local symbols do not count, but target specific ones might.  */
3726
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3727
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3728
0
    return false;
3729
3730
0
  bed = get_elf_backend_data (abfd);
3731
  /* Function symbols do not count.  */
3732
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3733
0
    return false;
3734
3735
  /* If the section is undefined, then so is the symbol.  */
3736
0
  if (sym->st_shndx == SHN_UNDEF)
3737
0
    return false;
3738
3739
  /* If the symbol is defined in the common section, then
3740
     it is a common definition and so does not count.  */
3741
0
  if (bed->common_definition (sym))
3742
0
    return false;
3743
3744
  /* If the symbol is in a target specific section then we
3745
     must rely upon the backend to tell us what it is.  */
3746
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3747
    /* FIXME - this function is not coded yet:
3748
3749
       return _bfd_is_global_symbol_definition (abfd, sym);
3750
3751
       Instead for now assume that the definition is not global,
3752
       Even if this is wrong, at least the linker will behave
3753
       in the same way that it used to do.  */
3754
0
    return false;
3755
3756
0
  return true;
3757
0
}
3758
3759
/* Search the symbol table of the archive element of the archive ABFD
3760
   whose archive map contains a mention of SYMDEF, and determine if
3761
   the symbol is defined in this element.  */
3762
static bool
3763
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3764
0
{
3765
0
  Elf_Internal_Shdr * hdr;
3766
0
  size_t symcount;
3767
0
  size_t extsymcount;
3768
0
  size_t extsymoff;
3769
0
  Elf_Internal_Sym *isymbuf;
3770
0
  Elf_Internal_Sym *isym;
3771
0
  Elf_Internal_Sym *isymend;
3772
0
  bool result;
3773
3774
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3775
0
  if (abfd == NULL)
3776
0
    return false;
3777
3778
0
  if (! bfd_check_format (abfd, bfd_object))
3779
0
    return false;
3780
3781
  /* Select the appropriate symbol table.  If we don't know if the
3782
     object file is an IR object, give linker LTO plugin a chance to
3783
     get the correct symbol table.  */
3784
0
  if (abfd->plugin_format == bfd_plugin_yes
3785
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3786
0
      || (abfd->plugin_format == bfd_plugin_unknown
3787
0
    && bfd_link_plugin_object_p (abfd)))
3788
0
    {
3789
      /* Use the IR symbol table if the object has been claimed by
3790
   plugin.  */
3791
0
      abfd = abfd->plugin_dummy_bfd;
3792
0
      hdr = &elf_symtab_hdr (abfd);
3793
0
    }
3794
0
  else
3795
0
    {
3796
0
      if (elf_use_dt_symtab_p (abfd))
3797
0
  {
3798
0
    bfd_set_error (bfd_error_wrong_format);
3799
0
    return false;
3800
0
  }
3801
3802
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3803
0
  hdr = &elf_symtab_hdr (abfd);
3804
0
      else
3805
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3806
0
    }
3807
3808
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3809
3810
  /* The sh_info field of the symtab header tells us where the
3811
     external symbols start.  We don't care about the local symbols.  */
3812
0
  if (elf_bad_symtab (abfd))
3813
0
    {
3814
0
      extsymcount = symcount;
3815
0
      extsymoff = 0;
3816
0
    }
3817
0
  else
3818
0
    {
3819
0
      extsymcount = symcount - hdr->sh_info;
3820
0
      extsymoff = hdr->sh_info;
3821
0
    }
3822
3823
0
  if (extsymcount == 0)
3824
0
    return false;
3825
3826
  /* Read in the symbol table.  */
3827
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3828
0
          NULL, NULL, NULL);
3829
0
  if (isymbuf == NULL)
3830
0
    return false;
3831
3832
  /* Scan the symbol table looking for SYMDEF.  */
3833
0
  result = false;
3834
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3835
0
    {
3836
0
      const char *name;
3837
3838
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3839
0
                isym->st_name);
3840
0
      if (name == NULL)
3841
0
  break;
3842
3843
0
      if (strcmp (name, symdef->name) == 0)
3844
0
  {
3845
0
    result = is_global_data_symbol_definition (abfd, isym);
3846
0
    break;
3847
0
  }
3848
0
    }
3849
3850
0
  free (isymbuf);
3851
3852
0
  return result;
3853
0
}
3854

3855
/* Add an entry to the .dynamic table.  */
3856
3857
bool
3858
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3859
          bfd_vma tag,
3860
          bfd_vma val)
3861
0
{
3862
0
  struct elf_link_hash_table *hash_table;
3863
0
  elf_backend_data *obed;
3864
0
  asection *s;
3865
0
  bfd_size_type newsize;
3866
0
  bfd_byte *newcontents;
3867
0
  Elf_Internal_Dyn dyn;
3868
3869
0
  hash_table = elf_hash_table (info);
3870
0
  if (! is_elf_hash_table (&hash_table->root))
3871
0
    return false;
3872
3873
0
  if (tag == DT_RELA || tag == DT_REL)
3874
0
    hash_table->dynamic_relocs = true;
3875
3876
0
  obed = get_elf_backend_data (hash_table->dynobj);
3877
0
  s = hash_table->dynamic;
3878
0
  BFD_ASSERT (s != NULL);
3879
3880
0
  newsize = s->size + obed->s->sizeof_dyn;
3881
0
  newcontents = bfd_realloc (s->contents, newsize);
3882
0
  if (newcontents == NULL)
3883
0
    return false;
3884
3885
0
  dyn.d_tag = tag;
3886
0
  dyn.d_un.d_val = val;
3887
0
  obed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3888
3889
0
  s->size = newsize;
3890
0
  s->contents = newcontents;
3891
3892
0
  return true;
3893
0
}
3894
3895
/* Strip zero-sized dynamic sections.  */
3896
3897
bool
3898
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3899
0
{
3900
0
  struct elf_link_hash_table *hash_table;
3901
0
  elf_backend_data *obed;
3902
0
  asection *s, *sdynamic, **pp;
3903
0
  asection *rela_dyn, *rel_dyn;
3904
0
  Elf_Internal_Dyn dyn;
3905
0
  bfd_byte *extdyn, *next;
3906
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3907
0
  bool strip_zero_sized;
3908
0
  bool strip_zero_sized_plt;
3909
3910
0
  if (bfd_link_relocatable (info))
3911
0
    return true;
3912
3913
0
  hash_table = elf_hash_table (info);
3914
0
  if (!is_elf_hash_table (&hash_table->root))
3915
0
    return false;
3916
3917
0
  if (!hash_table->dynobj)
3918
0
    return true;
3919
3920
0
  sdynamic= hash_table->dynamic;
3921
0
  if (!sdynamic)
3922
0
    return true;
3923
3924
0
  obed = get_elf_backend_data (hash_table->dynobj);
3925
0
  swap_dyn_in = obed->s->swap_dyn_in;
3926
3927
0
  strip_zero_sized = false;
3928
0
  strip_zero_sized_plt = false;
3929
3930
  /* Strip zero-sized dynamic sections.  */
3931
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3932
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3933
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3934
0
    if (s->size == 0
3935
0
  && (s == rela_dyn
3936
0
      || s == rel_dyn
3937
0
      || s == hash_table->srelplt->output_section
3938
0
      || s == hash_table->splt->output_section))
3939
0
      {
3940
0
  *pp = s->next;
3941
0
  info->output_bfd->section_count--;
3942
0
  strip_zero_sized = true;
3943
0
  if (s == rela_dyn)
3944
0
    s = rela_dyn;
3945
0
  if (s == rel_dyn)
3946
0
    s = rel_dyn;
3947
0
  else if (s == hash_table->splt->output_section)
3948
0
    {
3949
0
      s = hash_table->splt;
3950
0
      strip_zero_sized_plt = true;
3951
0
    }
3952
0
  else
3953
0
    s = hash_table->srelplt;
3954
0
  s->flags |= SEC_EXCLUDE;
3955
0
  s->output_section = bfd_abs_section_ptr;
3956
0
      }
3957
0
    else
3958
0
      pp = &s->next;
3959
3960
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3961
0
    for (extdyn = sdynamic->contents;
3962
0
   extdyn < sdynamic->contents + sdynamic->size;
3963
0
   extdyn = next)
3964
0
      {
3965
0
  next = extdyn + obed->s->sizeof_dyn;
3966
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3967
0
  switch (dyn.d_tag)
3968
0
    {
3969
0
    default:
3970
0
      break;
3971
0
    case DT_JMPREL:
3972
0
    case DT_PLTRELSZ:
3973
0
    case DT_PLTREL:
3974
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3975
         the procedure linkage table (the .plt section) has been
3976
         removed.  */
3977
0
      memmove (extdyn, next,
3978
0
         sdynamic->size - (next - sdynamic->contents));
3979
0
      next = extdyn;
3980
0
    }
3981
0
      }
3982
3983
0
  if (strip_zero_sized)
3984
0
    {
3985
      /* Regenerate program headers.  */
3986
0
      elf_seg_map (info->output_bfd) = NULL;
3987
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3988
0
    }
3989
3990
0
  return true;
3991
0
}
3992
3993
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3994
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3995
3996
int
3997
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3998
0
{
3999
0
  struct elf_strtab_hash *dynstr;
4000
0
  size_t strindex;
4001
0
  const char *soname;
4002
4003
0
  dynstr = _bfd_elf_link_dynstr (info);
4004
0
  if (dynstr == NULL)
4005
0
    return -1;
4006
4007
0
  soname = elf_dt_name (abfd);
4008
0
  strindex = _bfd_elf_strtab_add (dynstr, soname, false);
4009
0
  if (strindex == (size_t) -1)
4010
0
    return -1;
4011
4012
0
  if (!bfd_elf_link_create_dynamic_sections (info))
4013
0
    return -1;
4014
4015
0
  if (_bfd_elf_strtab_refcount (dynstr, strindex) != 1)
4016
0
    {
4017
0
      struct elf_link_hash_table *hash_table = elf_hash_table (info);
4018
0
      elf_backend_data *obed = get_elf_backend_data (hash_table->dynobj);
4019
0
      asection *sdyn = hash_table->dynamic;
4020
0
      bfd_byte *extdyn;
4021
4022
0
      if (sdyn != NULL && sdyn->size != 0)
4023
0
  for (extdyn = sdyn->contents;
4024
0
       extdyn < sdyn->contents + sdyn->size;
4025
0
       extdyn += obed->s->sizeof_dyn)
4026
0
    {
4027
0
      Elf_Internal_Dyn dyn;
4028
4029
0
      obed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
4030
0
      if (dyn.d_tag == DT_NEEDED
4031
0
    && dyn.d_un.d_val == strindex)
4032
0
        {
4033
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
4034
0
    return 1;
4035
0
        }
4036
0
    }
4037
0
    }
4038
4039
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
4040
0
    return -1;
4041
4042
0
  return 0;
4043
0
}
4044
4045
/* Return true if SONAME is on the needed list between NEEDED and STOP
4046
   (or the end of list if STOP is NULL), and needed by a library that
4047
   will be loaded.  */
4048
4049
static bool
4050
on_needed_list (const char *soname,
4051
    struct bfd_link_needed_list *needed,
4052
    struct bfd_link_needed_list *stop)
4053
0
{
4054
0
  struct bfd_link_needed_list *look;
4055
0
  for (look = needed; look != stop; look = look->next)
4056
0
    if (strcmp (soname, look->name) == 0
4057
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
4058
      /* If needed by a library that itself is not directly
4059
         needed, recursively check whether that library is
4060
         indirectly needed.  Since we add DT_NEEDED entries to
4061
         the end of the list, library dependencies appear after
4062
         the library.  Therefore search prior to the current
4063
         LOOK, preventing possible infinite recursion.  */
4064
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4065
0
      return true;
4066
4067
0
  return false;
4068
0
}
4069
4070
/* Sort symbol by value, section, size, and type.  */
4071
static int
4072
elf_sort_symbol (const void *arg1, const void *arg2)
4073
0
{
4074
0
  const struct elf_link_hash_entry *h1;
4075
0
  const struct elf_link_hash_entry *h2;
4076
0
  bfd_signed_vma vdiff;
4077
0
  int sdiff;
4078
0
  const char *n1;
4079
0
  const char *n2;
4080
4081
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4082
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4083
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4084
0
  if (vdiff != 0)
4085
0
    return vdiff > 0 ? 1 : -1;
4086
4087
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4088
0
  if (sdiff != 0)
4089
0
    return sdiff;
4090
4091
  /* Sort so that sized symbols are selected over zero size symbols.  */
4092
0
  vdiff = h1->size - h2->size;
4093
0
  if (vdiff != 0)
4094
0
    return vdiff > 0 ? 1 : -1;
4095
4096
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4097
0
  if (h1->type != h2->type)
4098
0
    return h1->type - h2->type;
4099
4100
  /* If symbols are properly sized and typed, and multiple strong
4101
     aliases are not defined in a shared library by the user we
4102
     shouldn't get here.  Unfortunately linker script symbols like
4103
     __bss_start sometimes match a user symbol defined at the start of
4104
     .bss without proper size and type.  We'd like to preference the
4105
     user symbol over reserved system symbols.  Sort on leading
4106
     underscores.  */
4107
0
  n1 = h1->root.root.string;
4108
0
  n2 = h2->root.root.string;
4109
0
  while (*n1 == *n2)
4110
0
    {
4111
0
      if (*n1 == 0)
4112
0
  break;
4113
0
      ++n1;
4114
0
      ++n2;
4115
0
    }
4116
0
  if (*n1 == '_')
4117
0
    return -1;
4118
0
  if (*n2 == '_')
4119
0
    return 1;
4120
4121
  /* Final sort on name selects user symbols like '_u' over reserved
4122
     system symbols like '_Z' and also will avoid qsort instability.  */
4123
0
  return *n1 - *n2;
4124
0
}
4125
4126
/* This function is used to adjust offsets into .dynstr for
4127
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4128
4129
static bool
4130
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4131
0
{
4132
0
  struct elf_strtab_hash *dynstr = data;
4133
4134
0
  if (h->dynindx != -1)
4135
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4136
0
  return true;
4137
0
}
4138
4139
/* Assign string offsets in .dynstr, update all structures referencing
4140
   them.  */
4141
4142
static bool
4143
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4144
0
{
4145
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4146
0
  struct elf_link_local_dynamic_entry *entry;
4147
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4148
0
  bfd *dynobj = hash_table->dynobj;
4149
0
  asection *sdyn;
4150
0
  bfd_size_type size;
4151
0
  elf_backend_data *obed;
4152
0
  bfd_byte *extdyn;
4153
4154
0
  _bfd_elf_strtab_finalize (dynstr);
4155
0
  size = _bfd_elf_strtab_size (dynstr);
4156
4157
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4158
4159
0
  if (info->callbacks->examine_strtab)
4160
0
    info->callbacks->examine_strtab (dynstr);
4161
4162
0
  obed = get_elf_backend_data (dynobj);
4163
0
  sdyn = hash_table->dynamic;
4164
0
  BFD_ASSERT (sdyn != NULL);
4165
4166
  /* Update all .dynamic entries referencing .dynstr strings.  */
4167
0
  for (extdyn = sdyn->contents;
4168
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4169
0
       extdyn += obed->s->sizeof_dyn)
4170
0
    {
4171
0
      Elf_Internal_Dyn dyn;
4172
4173
0
      obed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4174
0
      switch (dyn.d_tag)
4175
0
  {
4176
0
  case DT_STRSZ:
4177
0
    dyn.d_un.d_val = size;
4178
0
    break;
4179
0
  case DT_NEEDED:
4180
0
  case DT_SONAME:
4181
0
  case DT_RPATH:
4182
0
  case DT_RUNPATH:
4183
0
  case DT_FILTER:
4184
0
  case DT_AUXILIARY:
4185
0
  case DT_AUDIT:
4186
0
  case DT_DEPAUDIT:
4187
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4188
0
    break;
4189
0
  default:
4190
0
    continue;
4191
0
  }
4192
0
      obed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4193
0
    }
4194
4195
  /* Now update local dynamic symbols.  */
4196
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4197
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4198
0
              entry->isym.st_name);
4199
4200
  /* And the rest of dynamic symbols.  */
4201
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4202
4203
  /* Adjust version definitions.  */
4204
0
  if (elf_tdata (output_bfd)->cverdefs)
4205
0
    {
4206
0
      asection *s;
4207
0
      bfd_byte *p;
4208
0
      size_t i;
4209
0
      Elf_Internal_Verdef def;
4210
0
      Elf_Internal_Verdaux defaux;
4211
4212
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4213
0
      p = s->contents;
4214
0
      do
4215
0
  {
4216
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4217
0
           &def);
4218
0
    p += sizeof (Elf_External_Verdef);
4219
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4220
0
      continue;
4221
0
    for (i = 0; i < def.vd_cnt; ++i)
4222
0
      {
4223
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4224
0
          (Elf_External_Verdaux *) p, &defaux);
4225
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4226
0
              defaux.vda_name);
4227
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4228
0
           &defaux, (Elf_External_Verdaux *) p);
4229
0
        p += sizeof (Elf_External_Verdaux);
4230
0
      }
4231
0
  }
4232
0
      while (def.vd_next);
4233
0
    }
4234
4235
  /* Adjust version references.  */
4236
0
  if (elf_tdata (output_bfd)->verref)
4237
0
    {
4238
0
      asection *s;
4239
0
      bfd_byte *p;
4240
0
      size_t i;
4241
0
      Elf_Internal_Verneed need;
4242
0
      Elf_Internal_Vernaux needaux;
4243
4244
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4245
0
      p = s->contents;
4246
0
      do
4247
0
  {
4248
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4249
0
            &need);
4250
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4251
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4252
0
             (Elf_External_Verneed *) p);
4253
0
    p += sizeof (Elf_External_Verneed);
4254
0
    for (i = 0; i < need.vn_cnt; ++i)
4255
0
      {
4256
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4257
0
          (Elf_External_Vernaux *) p, &needaux);
4258
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4259
0
               needaux.vna_name);
4260
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4261
0
           &needaux,
4262
0
           (Elf_External_Vernaux *) p);
4263
0
        p += sizeof (Elf_External_Vernaux);
4264
0
      }
4265
0
  }
4266
0
      while (need.vn_next);
4267
0
    }
4268
4269
0
  return true;
4270
0
}
4271

4272
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4273
   The default is to only match when the INPUT and OUTPUT are exactly
4274
   the same target.  */
4275
4276
bool
4277
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4278
            const bfd_target *output)
4279
0
{
4280
0
  return input == output;
4281
0
}
4282
4283
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4284
   This version is used when different targets for the same architecture
4285
   are virtually identical.  */
4286
4287
bool
4288
_bfd_elf_relocs_compatible (const bfd_target *input,
4289
          const bfd_target *output)
4290
0
{
4291
0
  elf_backend_data *obed, *ibed;
4292
4293
0
  if (input == output)
4294
0
    return true;
4295
4296
0
  ibed = xvec_get_elf_backend_data (input);
4297
0
  obed = xvec_get_elf_backend_data (output);
4298
4299
0
  if (ibed->arch != obed->arch)
4300
0
    return false;
4301
4302
  /* If both backends are using this function, deem them compatible.  */
4303
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4304
0
}
4305
4306
/* Make a special call to the linker "notice" function to tell it that
4307
   we are about to handle an as-needed lib, or have finished
4308
   processing the lib.  */
4309
4310
bool
4311
_bfd_elf_notice_as_needed (bfd *ibfd,
4312
         struct bfd_link_info *info,
4313
         enum notice_asneeded_action act)
4314
0
{
4315
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4316
0
}
4317
4318
/* Call ACTION on each relocation in an ELF object file.  */
4319
4320
bool
4321
_bfd_elf_link_iterate_on_relocs
4322
  (bfd *abfd, struct bfd_link_info *info,
4323
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4324
       const Elf_Internal_Rela *))
4325
0
{
4326
  /* If this object is the same format as the output object, and it is
4327
     not a shared library, then let the backend look through the
4328
     relocs.
4329
4330
     This is required to build global offset table entries and to
4331
     arrange for dynamic relocs.  It is not required for the
4332
     particular common case of linking non PIC code, even when linking
4333
     against shared libraries, but unfortunately there is no way of
4334
     knowing whether an object file has been compiled PIC or not.
4335
     Looking through the relocs is not particularly time consuming.
4336
     The problem is that we must either (1) keep the relocs in memory,
4337
     which causes the linker to require additional runtime memory or
4338
     (2) read the relocs twice from the input file, which wastes time.
4339
     This would be a good case for using mmap.
4340
4341
     I have no idea how to handle linking PIC code into a file of a
4342
     different format.  It probably can't be done.  */
4343
0
  if ((abfd->flags & DYNAMIC) == 0
4344
0
      && compatible_format (info, abfd))
4345
0
    {
4346
0
      for (asection *o = abfd->sections; o != NULL; o = o->next)
4347
0
  {
4348
0
    Elf_Internal_Rela *internal_relocs;
4349
0
    bool ok;
4350
4351
    /* Don't check relocations in excluded sections.  Don't do
4352
       anything special with non-loaded, non-alloced sections.
4353
       In particular, any relocs in such sections should not
4354
       affect GOT and PLT reference counting (ie.  we don't
4355
       allow them to create GOT or PLT entries), there's no
4356
       possibility or desire to optimize TLS relocs, and
4357
       there's not much point in propagating relocs to shared
4358
       libs that the dynamic linker won't relocate.  */
4359
0
    if ((o->flags & SEC_ALLOC) == 0
4360
0
        || (o->flags & SEC_RELOC) == 0
4361
0
        || (o->flags & SEC_EXCLUDE) != 0
4362
0
        || o->reloc_count == 0
4363
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4364
0
      && (o->flags & SEC_DEBUGGING) != 0)
4365
0
        || bfd_is_abs_section (o->output_section))
4366
0
      continue;
4367
4368
0
    internal_relocs = _bfd_elf_link_info_read_relocs
4369
0
      (abfd, info, o, NULL, NULL,
4370
0
       _bfd_elf_link_keep_memory (info));
4371
0
    if (internal_relocs == NULL)
4372
0
      return false;
4373
4374
0
    ok = action (abfd, info, o, internal_relocs);
4375
4376
0
    if (elf_section_data (o)->relocs != internal_relocs)
4377
0
      free (internal_relocs);
4378
4379
0
    if (! ok)
4380
0
      return false;
4381
0
  }
4382
0
    }
4383
4384
0
  return true;
4385
0
}
4386
4387
/* Check relocations in an ELF object file.  This is called after
4388
   all input files have been opened.  */
4389
4390
bool
4391
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4392
0
{
4393
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4394
0
  if (bed->check_relocs != NULL)
4395
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4396
0
              bed->check_relocs);
4397
0
  return true;
4398
0
}
4399
4400
/* An entry in the first definition hash table.  */
4401
4402
struct elf_link_first_hash_entry
4403
{
4404
  struct bfd_hash_entry root;
4405
  /* The object of the first definition.  */
4406
  bfd *abfd;
4407
};
4408
4409
/* The function to create a new entry in the first definition hash
4410
   table.  */
4411
4412
static struct bfd_hash_entry *
4413
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4414
           struct bfd_hash_table *table,
4415
           const char *string)
4416
0
{
4417
0
  struct elf_link_first_hash_entry *ret =
4418
0
    (struct elf_link_first_hash_entry *) entry;
4419
4420
  /* Allocate the structure if it has not already been allocated by a
4421
     subclass.  */
4422
0
  if (ret == NULL)
4423
0
    ret = bfd_hash_allocate (table, sizeof (struct elf_link_first_hash_entry));
4424
0
  if (ret == NULL)
4425
0
    return NULL;
4426
4427
  /* Call the allocation method of the superclass.  */
4428
0
  ret = ((struct elf_link_first_hash_entry *)
4429
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4430
0
         string));
4431
0
  if (ret != NULL)
4432
0
    ret->abfd = NULL;
4433
4434
0
  return (struct bfd_hash_entry *) ret;
4435
0
}
4436
4437
/* Add the symbol NAME from ABFD to first hash.  */
4438
4439
static void
4440
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4441
          const char *name, bool copy)
4442
0
{
4443
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4444
  /* Skip if there is no first hash.  */
4445
0
  if (htab->first_hash == NULL)
4446
0
    return;
4447
4448
0
  struct elf_link_first_hash_entry *e
4449
0
    = ((struct elf_link_first_hash_entry *)
4450
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4451
0
  if (e == NULL)
4452
0
    info->callbacks->fatal
4453
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4454
4455
0
  if (e->abfd == NULL)
4456
    /* Store ABFD in abfd.  */
4457
0
    e->abfd = abfd;
4458
0
}
4459
4460
/* Add symbols from an ELF object file to the linker hash table.  */
4461
4462
static bool
4463
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4464
0
{
4465
0
  Elf_Internal_Ehdr *ehdr;
4466
0
  Elf_Internal_Shdr *hdr;
4467
0
  size_t symcount;
4468
0
  size_t extsymcount;
4469
0
  size_t extsymoff;
4470
0
  struct elf_link_hash_entry **sym_hash;
4471
0
  bool dynamic;
4472
0
  Elf_External_Versym *extversym = NULL;
4473
0
  Elf_External_Versym *extversym_end = NULL;
4474
0
  Elf_External_Versym *ever;
4475
0
  struct elf_link_hash_entry *weaks;
4476
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4477
0
  size_t nondeflt_vers_cnt = 0;
4478
0
  Elf_Internal_Sym *isymbuf = NULL;
4479
0
  Elf_Internal_Sym *isym;
4480
0
  Elf_Internal_Sym *isymend;
4481
0
  elf_backend_data *bed, *obed;
4482
0
  bool add_needed;
4483
0
  struct elf_link_hash_table *htab;
4484
0
  void *alloc_mark = NULL;
4485
0
  struct bfd_hash_entry **old_table = NULL;
4486
0
  unsigned int old_size = 0;
4487
0
  unsigned int old_count = 0;
4488
0
  void *old_tab = NULL;
4489
0
  void *old_ent;
4490
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4491
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4492
0
  void *old_strtab = NULL;
4493
0
  size_t tabsize = 0;
4494
0
  asection *s;
4495
0
  bool just_syms;
4496
4497
0
  htab = elf_hash_table (info);
4498
0
  bed = get_elf_backend_data (abfd);
4499
0
  obed = get_elf_backend_data (info->output_bfd);
4500
4501
0
  if (elf_use_dt_symtab_p (abfd))
4502
0
    {
4503
0
      bfd_set_error (bfd_error_wrong_format);
4504
0
      return false;
4505
0
    }
4506
4507
0
  if ((abfd->flags & DYNAMIC) == 0)
4508
0
    {
4509
0
      dynamic = false;
4510
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4511
0
    && is_elf_hash_table (&htab->root)
4512
0
    && htab->first_hash == NULL)
4513
0
  {
4514
    /* Initialize first_hash for an IR input.  */
4515
0
    htab->first_hash = bfd_malloc (sizeof (struct bfd_hash_table));
4516
0
    if (htab->first_hash == NULL
4517
0
        || !bfd_hash_table_init
4518
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4519
0
        sizeof (struct elf_link_first_hash_entry)))
4520
0
      info->callbacks->fatal
4521
0
        (_("%P: first_hash failed to create: %E\n"));
4522
0
  }
4523
0
    }
4524
0
  else
4525
0
    {
4526
0
      dynamic = true;
4527
4528
      /* You can't use -r against a dynamic object.  Also, there's no
4529
   hope of using a dynamic object which does not exactly match
4530
   the format of the output file.  */
4531
0
      if (bfd_link_relocatable (info)
4532
0
    || !is_elf_hash_table (&htab->root)
4533
0
    || info->output_bfd->xvec != abfd->xvec)
4534
0
  {
4535
0
    if (bfd_link_relocatable (info))
4536
0
      bfd_set_error (bfd_error_invalid_operation);
4537
0
    else
4538
0
      bfd_set_error (bfd_error_wrong_format);
4539
0
    goto error_return;
4540
0
  }
4541
0
    }
4542
4543
0
  ehdr = elf_elfheader (abfd);
4544
0
  if (info->warn_alternate_em
4545
0
      && bed->elf_machine_code != ehdr->e_machine
4546
0
      && ((bed->elf_machine_alt1 != 0
4547
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4548
0
    || (bed->elf_machine_alt2 != 0
4549
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4550
0
    _bfd_error_handler
4551
      /* xgettext:c-format */
4552
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4553
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4554
4555
  /* As a GNU extension, any input sections which are named
4556
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4557
     symbol.  This differs from .gnu.warning sections, which generate
4558
     warnings when they are included in an output file.  */
4559
  /* PR 12761: Also generate this warning when building shared libraries.  */
4560
0
  for (s = abfd->sections; s != NULL; s = s->next)
4561
0
    {
4562
0
      const char *name;
4563
4564
0
      name = bfd_section_name (s);
4565
0
      if (startswith (name, ".gnu.warning."))
4566
0
  {
4567
0
    char *msg;
4568
0
    bfd_size_type sz;
4569
4570
0
    name += sizeof ".gnu.warning." - 1;
4571
4572
    /* If this is a shared object, then look up the symbol
4573
       in the hash table.  If it is there, and it is already
4574
       been defined, then we will not be using the entry
4575
       from this shared object, so we don't need to warn.
4576
       FIXME: If we see the definition in a regular object
4577
       later on, we will warn, but we shouldn't.  The only
4578
       fix is to keep track of what warnings we are supposed
4579
       to emit, and then handle them all at the end of the
4580
       link.  */
4581
0
    if (dynamic)
4582
0
      {
4583
0
        struct elf_link_hash_entry *h;
4584
4585
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4586
4587
        /* FIXME: What about bfd_link_hash_common?  */
4588
0
        if (h != NULL
4589
0
      && (h->root.type == bfd_link_hash_defined
4590
0
          || h->root.type == bfd_link_hash_defweak))
4591
0
    continue;
4592
0
      }
4593
4594
0
    sz = s->size;
4595
0
    msg = bfd_alloc (abfd, sz + 1);
4596
0
    if (msg == NULL)
4597
0
      goto error_return;
4598
4599
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4600
0
      goto error_return;
4601
4602
0
    msg[sz] = '\0';
4603
4604
0
    if (! (_bfd_generic_link_add_one_symbol
4605
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4606
0
      false, bed->collect, NULL)))
4607
0
      goto error_return;
4608
4609
0
    if (bfd_link_executable (info))
4610
0
      {
4611
        /* Clobber the section size so that the warning does
4612
     not get copied into the output file.  */
4613
0
        s->size = 0;
4614
4615
        /* Also set SEC_EXCLUDE, so that symbols defined in
4616
     the warning section don't get copied to the output.  */
4617
0
        s->flags |= SEC_EXCLUDE;
4618
0
      }
4619
0
  }
4620
0
    }
4621
4622
0
  just_syms = ((s = abfd->sections) != NULL
4623
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4624
4625
0
  add_needed = true;
4626
0
  if (! dynamic)
4627
0
    {
4628
      /* If we are creating a shared library, create all the dynamic
4629
   sections immediately.  We need to attach them to something,
4630
   so we attach them to this BFD, provided it is the right
4631
   format and is not from ld --just-symbols.  Always create the
4632
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4633
   are no input BFD's of the same format as the output, we can't
4634
   make a shared library.  */
4635
0
      if (!just_syms
4636
0
    && (bfd_link_pic (info)
4637
0
        || (!bfd_link_relocatable (info)
4638
0
      && info->nointerp
4639
0
      && (info->export_dynamic || info->dynamic)))
4640
0
    && compatible_format (info, abfd))
4641
0
  {
4642
0
    if (!bfd_elf_link_create_dynamic_sections (info))
4643
0
      goto error_return;
4644
0
  }
4645
0
    }
4646
0
  else if (!is_elf_hash_table (&htab->root))
4647
0
    goto error_return;
4648
0
  else
4649
0
    {
4650
0
      const char *soname = NULL;
4651
0
      char *audit = NULL;
4652
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4653
0
      const Elf_Internal_Phdr *phdr;
4654
0
      struct elf_link_loaded_list *loaded_lib;
4655
4656
      /* ld --just-symbols and dynamic objects don't mix very well.
4657
   ld shouldn't allow it.  */
4658
0
      if (just_syms)
4659
0
  abort ();
4660
4661
      /* If this dynamic lib was specified on the command line with
4662
   --as-needed in effect, then we don't want to add a DT_NEEDED
4663
   tag unless the lib is actually used.  Similary for libs brought
4664
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4665
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4666
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4667
   all.  */
4668
0
      add_needed = (elf_dyn_lib_class (abfd)
4669
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4670
0
           | DYN_NO_NEEDED)) == 0;
4671
4672
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4673
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4674
0
  {
4675
0
    bfd_byte *dynbuf;
4676
0
    bfd_byte *extdyn;
4677
0
    unsigned int elfsec;
4678
0
    unsigned long shlink;
4679
4680
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4681
0
      {
4682
0
      error_free_dyn:
4683
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4684
0
        goto error_return;
4685
0
      }
4686
4687
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4688
0
    if (elfsec == SHN_BAD)
4689
0
      goto error_free_dyn;
4690
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4691
4692
0
    for (extdyn = dynbuf;
4693
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4694
0
         extdyn += bed->s->sizeof_dyn)
4695
0
      {
4696
0
        Elf_Internal_Dyn dyn;
4697
4698
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4699
0
        if (dyn.d_tag == DT_SONAME)
4700
0
    {
4701
0
      unsigned int tagv = dyn.d_un.d_val;
4702
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4703
0
      if (soname == NULL)
4704
0
        goto error_free_dyn;
4705
0
    }
4706
0
        if (dyn.d_tag == DT_NEEDED)
4707
0
    {
4708
0
      struct bfd_link_needed_list *n, **pn;
4709
0
      char *fnm, *anm;
4710
0
      unsigned int tagv = dyn.d_un.d_val;
4711
0
      size_t amt;
4712
4713
0
      n = bfd_alloc (abfd, sizeof (*n));
4714
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4715
0
      if (n == NULL || fnm == NULL)
4716
0
        goto error_free_dyn;
4717
0
      amt = strlen (fnm) + 1;
4718
0
      anm = bfd_alloc (abfd, amt);
4719
0
      if (anm == NULL)
4720
0
        goto error_free_dyn;
4721
0
      memcpy (anm, fnm, amt);
4722
0
      n->name = anm;
4723
0
      n->by = abfd;
4724
0
      n->next = NULL;
4725
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4726
0
        ;
4727
0
      *pn = n;
4728
0
    }
4729
0
        if (dyn.d_tag == DT_RUNPATH)
4730
0
    {
4731
0
      struct bfd_link_needed_list *n, **pn;
4732
0
      char *fnm, *anm;
4733
0
      unsigned int tagv = dyn.d_un.d_val;
4734
0
      size_t amt;
4735
4736
0
      n = bfd_alloc (abfd, sizeof (*n));
4737
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4738
0
      if (n == NULL || fnm == NULL)
4739
0
        goto error_free_dyn;
4740
0
      amt = strlen (fnm) + 1;
4741
0
      anm = bfd_alloc (abfd, amt);
4742
0
      if (anm == NULL)
4743
0
        goto error_free_dyn;
4744
0
      memcpy (anm, fnm, amt);
4745
0
      n->name = anm;
4746
0
      n->by = abfd;
4747
0
      n->next = NULL;
4748
0
      for (pn = & runpath;
4749
0
           *pn != NULL;
4750
0
           pn = &(*pn)->next)
4751
0
        ;
4752
0
      *pn = n;
4753
0
    }
4754
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4755
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4756
0
    {
4757
0
      struct bfd_link_needed_list *n, **pn;
4758
0
      char *fnm, *anm;
4759
0
      unsigned int tagv = dyn.d_un.d_val;
4760
0
      size_t amt;
4761
4762
0
      n = bfd_alloc (abfd, sizeof (*n));
4763
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4764
0
      if (n == NULL || fnm == NULL)
4765
0
        goto error_free_dyn;
4766
0
      amt = strlen (fnm) + 1;
4767
0
      anm = bfd_alloc (abfd, amt);
4768
0
      if (anm == NULL)
4769
0
        goto error_free_dyn;
4770
0
      memcpy (anm, fnm, amt);
4771
0
      n->name = anm;
4772
0
      n->by = abfd;
4773
0
      n->next = NULL;
4774
0
      for (pn = & rpath;
4775
0
           *pn != NULL;
4776
0
           pn = &(*pn)->next)
4777
0
        ;
4778
0
      *pn = n;
4779
0
    }
4780
0
        if (dyn.d_tag == DT_AUDIT)
4781
0
    {
4782
0
      unsigned int tagv = dyn.d_un.d_val;
4783
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4784
0
    }
4785
0
        if (dyn.d_tag == DT_FLAGS_1)
4786
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4787
0
      }
4788
4789
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4790
0
  }
4791
4792
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4793
   frees all more recently bfd_alloc'd blocks as well.  */
4794
0
      if (runpath)
4795
0
  rpath = runpath;
4796
4797
0
      if (rpath)
4798
0
  {
4799
0
    struct bfd_link_needed_list **pn;
4800
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4801
0
      ;
4802
0
    *pn = rpath;
4803
0
  }
4804
4805
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4806
   all sections contained fully therein.  This makes relro
4807
   shared library sections appear as they will at run-time.  */
4808
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4809
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4810
0
  if (phdr->p_type == PT_GNU_RELRO)
4811
0
    {
4812
0
      for (s = abfd->sections; s != NULL; s = s->next)
4813
0
        {
4814
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4815
4816
0
    if ((s->flags & SEC_ALLOC) != 0
4817
0
        && s->vma * opb >= phdr->p_vaddr
4818
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4819
0
      s->flags |= SEC_READONLY;
4820
0
        }
4821
0
      break;
4822
0
    }
4823
4824
      /* We do not want to include any of the sections in a dynamic
4825
   object in the output file.  We hack by simply clobbering the
4826
   list of sections in the BFD.  This could be handled more
4827
   cleanly by, say, a new section flag; the existing
4828
   SEC_NEVER_LOAD flag is not the one we want, because that one
4829
   still implies that the section takes up space in the output
4830
   file.  */
4831
0
      bfd_section_list_clear (abfd);
4832
4833
      /* Find the name to use in a DT_NEEDED entry that refers to this
4834
   object.  If the object has a DT_SONAME entry, we use it.
4835
   Otherwise, if the generic linker stuck something in
4836
   elf_dt_name, we use that.  Otherwise, we just use the file
4837
   name.  */
4838
0
      if (soname == NULL || *soname == '\0')
4839
0
  {
4840
0
    soname = elf_dt_name (abfd);
4841
0
    if (soname == NULL || *soname == '\0')
4842
0
      soname = bfd_get_filename (abfd);
4843
0
  }
4844
4845
      /* Save the SONAME because sometimes the linker emulation code
4846
   will need to know it.  */
4847
0
      elf_dt_name (abfd) = soname;
4848
4849
      /* If we have already included this dynamic object in the
4850
   link, just ignore it.  There is no reason to include a
4851
   particular dynamic object more than once.  */
4852
0
      for (loaded_lib = htab->dyn_loaded;
4853
0
     loaded_lib != NULL;
4854
0
     loaded_lib = loaded_lib->next)
4855
0
  {
4856
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4857
0
      return true;
4858
0
  }
4859
4860
      /* Create dynamic sections for backends that require that be done
4861
   before setup_gnu_properties.  */
4862
0
      if (add_needed
4863
0
    && !bfd_elf_link_create_dynamic_sections (info))
4864
0
  return false;
4865
4866
      /* Save the DT_AUDIT entry for the linker emulation code. */
4867
0
      elf_dt_audit (abfd) = audit;
4868
0
    }
4869
4870
  /* If this is a dynamic object, we always link against the .dynsym
4871
     symbol table, not the .symtab symbol table.  The dynamic linker
4872
     will only see the .dynsym symbol table, so there is no reason to
4873
     look at .symtab for a dynamic object.  */
4874
4875
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4876
0
    hdr = &elf_symtab_hdr (abfd);
4877
0
  else
4878
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4879
4880
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4881
4882
  /* The sh_info field of the symtab header tells us where the
4883
     external symbols start.  We don't care about the local symbols at
4884
     this point.  */
4885
0
  if (elf_bad_symtab (abfd))
4886
0
    {
4887
0
      extsymcount = symcount;
4888
0
      extsymoff = 0;
4889
0
    }
4890
0
  else
4891
0
    {
4892
0
      extsymcount = symcount - hdr->sh_info;
4893
0
      extsymoff = hdr->sh_info;
4894
0
    }
4895
4896
0
  sym_hash = elf_sym_hashes (abfd);
4897
0
  if (extsymcount != 0)
4898
0
    {
4899
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4900
0
              NULL, NULL, NULL);
4901
0
      if (isymbuf == NULL)
4902
0
  goto error_return;
4903
4904
0
      if (sym_hash == NULL)
4905
0
  {
4906
    /* We store a pointer to the hash table entry for each
4907
       external symbol.  */
4908
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4909
0
    if (sym_hash == NULL)
4910
0
      goto error_free_sym;
4911
0
    elf_sym_hashes (abfd) = sym_hash;
4912
0
  }
4913
0
    }
4914
4915
0
  if (dynamic)
4916
0
    {
4917
      /* Read in any version definitions.  */
4918
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4919
0
            info->default_imported_symver))
4920
0
  goto error_free_sym;
4921
4922
      /* Read in the symbol versions, but don't bother to convert them
4923
   to internal format.  */
4924
0
      if (elf_dynversym (abfd) != 0)
4925
0
  {
4926
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4927
0
    bfd_size_type amt = versymhdr->sh_size;
4928
4929
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4930
0
      goto error_free_sym;
4931
0
    extversym = _bfd_malloc_and_read (abfd, amt, amt);
4932
0
    if (extversym == NULL)
4933
0
      goto error_free_sym;
4934
0
    extversym_end = extversym + amt / sizeof (*extversym);
4935
0
  }
4936
0
    }
4937
4938
  /* If we are loading an as-needed shared lib, save the symbol table
4939
     state before we start adding symbols.  If the lib turns out
4940
     to be unneeded, restore the state.  */
4941
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4942
0
    {
4943
0
      unsigned int i;
4944
0
      size_t entsize;
4945
4946
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4947
0
  {
4948
0
    struct bfd_hash_entry *p;
4949
0
    struct elf_link_hash_entry *h;
4950
4951
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4952
0
      {
4953
0
        h = (struct elf_link_hash_entry *) p;
4954
0
        entsize += htab->root.table.entsize;
4955
0
        if (h->root.type == bfd_link_hash_warning)
4956
0
    {
4957
0
      entsize += htab->root.table.entsize;
4958
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4959
0
    }
4960
0
        if (h->root.type == bfd_link_hash_common)
4961
0
    entsize += sizeof (*h->root.u.c.p);
4962
0
      }
4963
0
  }
4964
4965
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4966
0
      old_tab = bfd_malloc (tabsize + entsize);
4967
0
      if (old_tab == NULL)
4968
0
  goto error_free_vers;
4969
4970
      /* Remember the current objalloc pointer, so that all mem for
4971
   symbols added can later be reclaimed.  */
4972
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4973
0
      if (alloc_mark == NULL)
4974
0
  goto error_free_vers;
4975
4976
      /* Make a special call to the linker "notice" function to
4977
   tell it that we are about to handle an as-needed lib.  */
4978
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4979
0
  goto error_free_vers;
4980
4981
      /* Clone the symbol table.  Remember some pointers into the
4982
   symbol table, and dynamic symbol count.  */
4983
0
      old_ent = (char *) old_tab + tabsize;
4984
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4985
0
      old_undefs = htab->root.undefs;
4986
0
      old_undefs_tail = htab->root.undefs_tail;
4987
0
      old_table = htab->root.table.table;
4988
0
      old_size = htab->root.table.size;
4989
0
      old_count = htab->root.table.count;
4990
0
      old_strtab = NULL;
4991
0
      if (htab->dynstr != NULL)
4992
0
  {
4993
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4994
0
    if (old_strtab == NULL)
4995
0
      goto error_free_vers;
4996
0
  }
4997
4998
0
      for (i = 0; i < htab->root.table.size; i++)
4999
0
  {
5000
0
    struct bfd_hash_entry *p;
5001
0
    struct elf_link_hash_entry *h;
5002
5003
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5004
0
      {
5005
0
        h = (struct elf_link_hash_entry *) p;
5006
0
        memcpy (old_ent, h, htab->root.table.entsize);
5007
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5008
0
        if (h->root.type == bfd_link_hash_warning)
5009
0
    {
5010
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5011
0
      memcpy (old_ent, h, htab->root.table.entsize);
5012
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5013
0
    }
5014
0
        if (h->root.type == bfd_link_hash_common)
5015
0
    {
5016
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
5017
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5018
0
    }
5019
0
      }
5020
0
  }
5021
0
    }
5022
5023
0
  weaks = NULL;
5024
0
  if (extversym == NULL)
5025
0
    ever = NULL;
5026
0
  else if (extversym + extsymoff < extversym_end)
5027
0
    ever = extversym + extsymoff;
5028
0
  else
5029
0
    {
5030
      /* xgettext:c-format */
5031
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
5032
0
        abfd, (long) extsymoff,
5033
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
5034
0
      bfd_set_error (bfd_error_bad_value);
5035
0
      goto error_free_vers;
5036
0
    }
5037
5038
0
  if (!bfd_link_relocatable (info)
5039
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
5040
0
    {
5041
0
      _bfd_error_handler
5042
0
  (_("%pB: plugin needed to handle lto object"), abfd);
5043
0
    }
5044
5045
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
5046
0
       isym < isymend;
5047
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
5048
0
    {
5049
0
      int bind;
5050
0
      bfd_vma value;
5051
0
      asection *sec, *new_sec;
5052
0
      flagword flags;
5053
0
      const char *name;
5054
0
      const char *defvername;
5055
0
      bool must_copy_name = false;
5056
0
      struct elf_link_hash_entry *h;
5057
0
      struct elf_link_hash_entry *hi;
5058
0
      bool definition;
5059
0
      bool size_change_ok;
5060
0
      bool type_change_ok;
5061
0
      bool new_weak;
5062
0
      bool old_weak;
5063
0
      bfd *override;
5064
0
      bool common;
5065
0
      bool discarded;
5066
0
      unsigned int old_alignment;
5067
0
      unsigned int shindex;
5068
0
      bfd *old_bfd;
5069
0
      bool matched;
5070
5071
0
      override = NULL;
5072
5073
0
      flags = BSF_NO_FLAGS;
5074
0
      sec = NULL;
5075
0
      value = isym->st_value;
5076
0
      common = bed->common_definition (isym);
5077
0
      if (common && info->inhibit_common_definition)
5078
0
  {
5079
    /* Treat common symbol as undefined for --no-define-common.  */
5080
0
    isym->st_shndx = SHN_UNDEF;
5081
0
    common = false;
5082
0
  }
5083
0
      discarded = false;
5084
5085
0
      bind = ELF_ST_BIND (isym->st_info);
5086
0
      switch (bind)
5087
0
  {
5088
0
  case STB_LOCAL:
5089
    /* This should be impossible, since ELF requires that all
5090
       global symbols follow all local symbols, and that sh_info
5091
       point to the first global symbol.  Unfortunately, Irix 5
5092
       screws this up.  */
5093
0
    if (elf_bad_symtab (abfd))
5094
0
      continue;
5095
5096
    /* If we aren't prepared to handle locals within the globals
5097
       then we'll likely segfault on a NULL symbol hash if the
5098
       symbol is ever referenced in relocations.  */
5099
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5100
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5101
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5102
0
        " (>= sh_info of %lu)"),
5103
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5104
0
            (long) extsymoff);
5105
5106
    /* Dynamic object relocations are not processed by ld, so
5107
       ld won't run into the problem mentioned above.  */
5108
0
    if (dynamic)
5109
0
      continue;
5110
0
    bfd_set_error (bfd_error_bad_value);
5111
0
    goto error_free_vers;
5112
5113
0
  case STB_GLOBAL:
5114
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5115
0
      flags = BSF_GLOBAL;
5116
0
    break;
5117
5118
0
  case STB_WEAK:
5119
0
    flags = BSF_WEAK;
5120
0
    break;
5121
5122
0
  case STB_GNU_UNIQUE:
5123
0
    flags = BSF_GNU_UNIQUE;
5124
0
    break;
5125
5126
0
  default:
5127
    /* Leave it up to the processor backend.  */
5128
0
    break;
5129
0
  }
5130
5131
0
      if (isym->st_shndx == SHN_UNDEF)
5132
0
  sec = bfd_und_section_ptr;
5133
0
      else if (isym->st_shndx == SHN_ABS)
5134
0
  sec = bfd_abs_section_ptr;
5135
0
      else if (isym->st_shndx == SHN_COMMON)
5136
0
  {
5137
0
    sec = bfd_com_section_ptr;
5138
    /* What ELF calls the size we call the value.  What ELF
5139
       calls the value we call the alignment.  */
5140
0
    value = isym->st_size;
5141
0
  }
5142
0
      else
5143
0
  {
5144
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5145
0
    if (sec == NULL)
5146
0
      sec = bfd_abs_section_ptr;
5147
0
    else if (discarded_section (sec))
5148
0
      {
5149
        /* Symbols from discarded section are undefined.  We keep
5150
     its visibility.  */
5151
0
        sec = bfd_und_section_ptr;
5152
0
        discarded = true;
5153
0
        isym->st_shndx = SHN_UNDEF;
5154
0
      }
5155
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5156
0
      value -= sec->vma;
5157
0
  }
5158
5159
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5160
0
                isym->st_name);
5161
0
      if (name == NULL)
5162
0
  goto error_free_vers;
5163
5164
0
      if (isym->st_shndx == SHN_COMMON
5165
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5166
0
  {
5167
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5168
5169
0
    if (xc == NULL)
5170
0
      {
5171
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5172
0
         | SEC_EXCLUDE);
5173
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5174
0
        if (xc == NULL)
5175
0
    goto error_free_vers;
5176
0
      }
5177
0
    sec = xc;
5178
0
  }
5179
0
      else if (isym->st_shndx == SHN_COMMON
5180
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5181
0
         && !bfd_link_relocatable (info))
5182
0
  {
5183
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5184
5185
0
    if (tcomm == NULL)
5186
0
      {
5187
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5188
0
         | SEC_LINKER_CREATED);
5189
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5190
0
        if (tcomm == NULL)
5191
0
    goto error_free_vers;
5192
0
      }
5193
0
    sec = tcomm;
5194
0
  }
5195
0
      else if (bed->elf_add_symbol_hook)
5196
0
  {
5197
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5198
0
               &sec, &value))
5199
0
      goto error_free_vers;
5200
5201
    /* The hook function sets the name to NULL if this symbol
5202
       should be skipped for some reason.  */
5203
0
    if (name == NULL)
5204
0
      continue;
5205
0
  }
5206
5207
0
      if (name[0] == '\0')
5208
0
  {
5209
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5210
0
    bfd_set_error (bfd_error_bad_value);
5211
0
    goto error_free_vers;
5212
0
  }
5213
5214
      /* Sanity check that all possibilities were handled.  */
5215
0
      if (sec == NULL)
5216
0
  abort ();
5217
5218
      /* Silently discard TLS symbols from --just-syms.  There's
5219
   no way to combine a static TLS block with a new TLS block
5220
   for this executable.  */
5221
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5222
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5223
0
  continue;
5224
5225
0
      if (bfd_is_und_section (sec)
5226
0
    || bfd_is_com_section (sec))
5227
0
  definition = false;
5228
0
      else
5229
0
  definition = true;
5230
5231
0
      size_change_ok = false;
5232
0
      type_change_ok = bed->type_change_ok;
5233
0
      old_weak = false;
5234
0
      matched = false;
5235
0
      old_alignment = 0;
5236
0
      old_bfd = NULL;
5237
0
      new_sec = sec;
5238
0
      defvername = NULL;
5239
5240
0
      if (is_elf_hash_table (&htab->root))
5241
0
  {
5242
0
    Elf_Internal_Versym iver;
5243
0
    unsigned int vernum = 0;
5244
0
    bool skip;
5245
5246
0
    if (ever == NULL)
5247
0
      {
5248
0
        if (info->default_imported_symver)
5249
    /* Use the default symbol version created earlier.  */
5250
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5251
0
        else
5252
0
    iver.vs_vers = 0;
5253
0
      }
5254
0
    else if (ever >= extversym_end)
5255
0
      {
5256
        /* xgettext:c-format */
5257
0
        _bfd_error_handler (_("%pB: not enough version information"),
5258
0
          abfd);
5259
0
        bfd_set_error (bfd_error_bad_value);
5260
0
        goto error_free_vers;
5261
0
      }
5262
0
    else
5263
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5264
5265
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5266
5267
    /* If this is a hidden symbol, or if it is not version
5268
       1, we append the version name to the symbol name.
5269
       However, we do not modify a non-hidden absolute symbol
5270
       if it is not a function, because it might be the version
5271
       symbol itself.  FIXME: What if it isn't?  */
5272
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5273
0
        || (vernum > 1
5274
0
      && (!bfd_is_abs_section (sec)
5275
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5276
0
      {
5277
0
        const char *verstr;
5278
0
        size_t namelen, verlen, newlen;
5279
0
        char *newname, *p;
5280
5281
0
        if (isym->st_shndx != SHN_UNDEF)
5282
0
    {
5283
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5284
0
        verstr = NULL;
5285
0
      else if (vernum > 1)
5286
0
        verstr =
5287
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5288
0
      else
5289
0
        verstr = "";
5290
5291
0
      if (verstr == NULL)
5292
0
        {
5293
0
          _bfd_error_handler
5294
      /* xgettext:c-format */
5295
0
      (_("%pB: %s: invalid version %u (max %d)"),
5296
0
       abfd, name, vernum,
5297
0
       elf_tdata (abfd)->cverdefs);
5298
0
          bfd_set_error (bfd_error_bad_value);
5299
0
          goto error_free_vers;
5300
0
        }
5301
0
    }
5302
0
        else
5303
0
    {
5304
      /* We cannot simply test for the number of
5305
         entries in the VERNEED section since the
5306
         numbers for the needed versions do not start
5307
         at 0.  */
5308
0
      Elf_Internal_Verneed *t;
5309
5310
0
      verstr = NULL;
5311
0
      for (t = elf_tdata (abfd)->verref;
5312
0
           t != NULL;
5313
0
           t = t->vn_nextref)
5314
0
        {
5315
0
          Elf_Internal_Vernaux *a;
5316
5317
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5318
0
      {
5319
0
        if (a->vna_other == vernum)
5320
0
          {
5321
0
            verstr = a->vna_nodename;
5322
0
            break;
5323
0
          }
5324
0
      }
5325
0
          if (a != NULL)
5326
0
      break;
5327
0
        }
5328
0
      if (verstr == NULL)
5329
0
        {
5330
0
          _bfd_error_handler
5331
      /* xgettext:c-format */
5332
0
      (_("%pB: %s: invalid needed version %d"),
5333
0
       abfd, name, vernum);
5334
0
          bfd_set_error (bfd_error_bad_value);
5335
0
          goto error_free_vers;
5336
0
        }
5337
0
    }
5338
5339
0
        namelen = strlen (name);
5340
0
        verlen = strlen (verstr);
5341
0
        newlen = namelen + verlen + 2;
5342
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5343
0
      && isym->st_shndx != SHN_UNDEF)
5344
0
    ++newlen;
5345
5346
0
        newname = bfd_hash_allocate (&htab->root.table, newlen);
5347
0
        if (newname == NULL)
5348
0
    goto error_free_vers;
5349
0
        memcpy (newname, name, namelen);
5350
0
        p = newname + namelen;
5351
0
        *p++ = ELF_VER_CHR;
5352
        /* If this is a defined non-hidden version symbol,
5353
     we add another @ to the name.  This indicates the
5354
     default version of the symbol.  */
5355
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5356
0
      && isym->st_shndx != SHN_UNDEF)
5357
0
    *p++ = ELF_VER_CHR, defvername = name;
5358
0
        memcpy (p, verstr, verlen + 1);
5359
5360
0
        name = newname;
5361
        /* Since bfd_hash_alloc is used for "name", the string
5362
     must be copied if added to first_hash.  The string
5363
     memory can be freed when an --as-needed library is
5364
     not needed.  */
5365
0
        must_copy_name = true;
5366
0
      }
5367
5368
    /* If this symbol has default visibility and the user has
5369
       requested we not re-export it, then mark it as hidden.  */
5370
0
    if (!bfd_is_und_section (sec)
5371
0
        && !dynamic
5372
0
        && abfd->no_export
5373
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5374
0
      isym->st_other = (STV_HIDDEN
5375
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5376
5377
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5378
0
              sym_hash, &old_bfd, &old_weak,
5379
0
              &old_alignment, &skip, &override,
5380
0
              &type_change_ok, &size_change_ok,
5381
0
              &matched))
5382
0
      goto error_free_vers;
5383
5384
0
    if (skip)
5385
0
      continue;
5386
5387
0
    h = *sym_hash;
5388
0
    while (h->root.type == bfd_link_hash_indirect
5389
0
     || h->root.type == bfd_link_hash_warning)
5390
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5391
5392
    /* Override a definition only if the new symbol matches the
5393
       existing one.  */
5394
0
    if (override && matched)
5395
0
      {
5396
0
        definition = false;
5397
0
        if (htab->first_hash != NULL
5398
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5399
0
      && h->root.non_ir_ref_regular)
5400
0
    {
5401
      /* When reloading --as-needed shared objects for new
5402
         symbols added from IR inputs, if this shared object
5403
         has the first definition, use it.  */
5404
0
      struct elf_link_first_hash_entry *e
5405
0
        = ((struct elf_link_first_hash_entry *)
5406
0
           bfd_hash_lookup (htab->first_hash, name, false,
5407
0
          false));
5408
0
      if (e != NULL && e->abfd == abfd)
5409
0
        definition = true;
5410
0
    }
5411
0
      }
5412
5413
0
    if (h->versioned != unversioned
5414
0
        && elf_tdata (abfd)->verdef != NULL
5415
0
        && vernum > 1
5416
0
        && definition)
5417
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5418
0
  }
5419
5420
0
      if (! (_bfd_generic_link_add_one_symbol
5421
0
       (info, override ? override : abfd, name, flags, sec, value,
5422
0
        NULL, false, bed->collect,
5423
0
        (struct bfd_link_hash_entry **) sym_hash)))
5424
0
  goto error_free_vers;
5425
5426
0
      h = *sym_hash;
5427
      /* We need to make sure that indirect symbol dynamic flags are
5428
   updated.  */
5429
0
      hi = h;
5430
0
      while (h->root.type == bfd_link_hash_indirect
5431
0
       || h->root.type == bfd_link_hash_warning)
5432
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5433
5434
0
      *sym_hash = h;
5435
5436
      /* Setting the index to -3 tells elf_link_output_extsym that
5437
   this symbol is defined in a discarded section.  */
5438
0
      if (discarded && is_elf_hash_table (&htab->root))
5439
0
  h->indx = -3;
5440
5441
0
      new_weak = (flags & BSF_WEAK) != 0;
5442
0
      if (dynamic
5443
0
    && definition
5444
0
    && new_weak
5445
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5446
0
    && is_elf_hash_table (&htab->root)
5447
0
    && h->u.alias == NULL)
5448
0
  {
5449
    /* Keep a list of all weak defined non function symbols from
5450
       a dynamic object, using the alias field.  Later in this
5451
       function we will set the alias field to the correct
5452
       value.  We only put non-function symbols from dynamic
5453
       objects on this list, because that happens to be the only
5454
       time we need to know the normal symbol corresponding to a
5455
       weak symbol, and the information is time consuming to
5456
       figure out.  If the alias field is not already NULL,
5457
       then this symbol was already defined by some previous
5458
       dynamic object, and we will be using that previous
5459
       definition anyhow.  */
5460
5461
0
    h->u.alias = weaks;
5462
0
    weaks = h;
5463
0
  }
5464
5465
      /* Set the alignment of a common symbol.  */
5466
0
      if ((common || bfd_is_com_section (sec))
5467
0
    && h->root.type == bfd_link_hash_common)
5468
0
  {
5469
0
    unsigned int align;
5470
5471
0
    if (common)
5472
0
      align = bfd_log2 (isym->st_value);
5473
0
    else
5474
0
      {
5475
        /* The new symbol is a common symbol in a shared object.
5476
     We need to get the alignment from the section.  */
5477
0
        align = new_sec->alignment_power;
5478
0
      }
5479
0
    if (align > old_alignment)
5480
0
      h->root.u.c.p->alignment_power = align;
5481
0
    else
5482
0
      h->root.u.c.p->alignment_power = old_alignment;
5483
0
  }
5484
5485
0
      if (is_elf_hash_table (&htab->root))
5486
0
  {
5487
    /* Set a flag in the hash table entry indicating the type of
5488
       reference or definition we just found.  A dynamic symbol
5489
       is one which is referenced or defined by both a regular
5490
       object and a shared object.  */
5491
0
    bool dynsym = false;
5492
5493
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5494
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5495
0
      {
5496
        /* Except for this flag to track nonweak references.  */
5497
0
        if (!definition
5498
0
      && bind != STB_WEAK)
5499
0
    h->ref_ir_nonweak = 1;
5500
0
      }
5501
0
    else if (!dynamic)
5502
0
      {
5503
0
        if (! definition)
5504
0
    {
5505
0
      h->ref_regular = 1;
5506
0
      if (bind != STB_WEAK)
5507
0
        h->ref_regular_nonweak = 1;
5508
0
    }
5509
0
        else
5510
0
    {
5511
0
      h->def_regular = 1;
5512
0
      if (h->def_dynamic)
5513
0
        {
5514
0
          h->def_dynamic = 0;
5515
0
          h->ref_dynamic = 1;
5516
0
        }
5517
0
    }
5518
0
      }
5519
0
    else
5520
0
      {
5521
0
        if (! definition)
5522
0
    {
5523
0
      h->ref_dynamic = 1;
5524
0
      hi->ref_dynamic = 1;
5525
0
    }
5526
0
        else
5527
0
    {
5528
0
      h->def_dynamic = 1;
5529
0
      hi->def_dynamic = 1;
5530
0
    }
5531
0
      }
5532
5533
    /* If an indirect symbol has been forced local, don't
5534
       make the real symbol dynamic.  */
5535
0
    if (h != hi && hi->forced_local)
5536
0
      ;
5537
0
    else if (!dynamic)
5538
0
      {
5539
0
        if (bfd_link_dll (info)
5540
0
      || h->def_dynamic
5541
0
      || h->ref_dynamic)
5542
0
    dynsym = true;
5543
0
      }
5544
0
    else
5545
0
      {
5546
0
        if (h->def_regular
5547
0
      || h->ref_regular
5548
0
      || (h->is_weakalias
5549
0
          && weakdef (h)->dynindx != -1))
5550
0
    dynsym = true;
5551
0
      }
5552
5553
    /* Check to see if we need to add an indirect symbol for
5554
       the default name.  */
5555
0
    if ((definition
5556
0
         || (!override && h->root.type == bfd_link_hash_common))
5557
0
        && !(hi != h
5558
0
       && hi->versioned == versioned_hidden))
5559
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5560
0
                sec, value, &old_bfd, &dynsym))
5561
0
        goto error_free_vers;
5562
5563
    /* Check the alignment when a common symbol is involved. This
5564
       can change when a common symbol is overridden by a normal
5565
       definition or a common symbol is ignored due to the old
5566
       normal definition. We need to make sure the maximum
5567
       alignment is maintained.  */
5568
0
    if ((old_alignment || common)
5569
0
        && h->root.type != bfd_link_hash_common)
5570
0
      {
5571
0
        unsigned int common_align;
5572
0
        unsigned int normal_align;
5573
0
        unsigned int symbol_align;
5574
0
        bfd *normal_bfd;
5575
0
        bfd *common_bfd;
5576
5577
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5578
0
        || h->root.type == bfd_link_hash_defweak);
5579
5580
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5581
0
        if (h->root.u.def.section->owner != NULL
5582
0
      && (h->root.u.def.section->owner->flags
5583
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5584
0
    {
5585
0
      normal_align = h->root.u.def.section->alignment_power;
5586
0
      if (normal_align > symbol_align)
5587
0
        normal_align = symbol_align;
5588
0
    }
5589
0
        else
5590
0
    normal_align = symbol_align;
5591
5592
0
        if (old_alignment)
5593
0
    {
5594
0
      common_align = old_alignment;
5595
0
      common_bfd = old_bfd;
5596
0
      normal_bfd = abfd;
5597
0
    }
5598
0
        else
5599
0
    {
5600
0
      common_align = bfd_log2 (isym->st_value);
5601
0
      common_bfd = abfd;
5602
0
      normal_bfd = old_bfd;
5603
0
    }
5604
5605
0
        if (normal_align < common_align)
5606
0
    {
5607
      /* PR binutils/2735 */
5608
0
      uint64_t c_align = UINT64_C (1) << common_align;
5609
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5610
0
      if (normal_bfd == NULL)
5611
0
        _bfd_error_handler
5612
          /* xgettext:c-format */
5613
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5614
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5615
0
           c_align, name, common_bfd,
5616
0
           n_align, h->root.u.def.section);
5617
0
      else
5618
0
        _bfd_error_handler
5619
          /* xgettext:c-format */
5620
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5621
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5622
0
           n_align, name, normal_bfd,
5623
0
           c_align, common_bfd);
5624
5625
      /* PR 30499: make sure that users understand that this warning is serious.  */
5626
0
      _bfd_error_handler
5627
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5628
0
    }
5629
0
      }
5630
5631
    /* Remember the symbol size if it isn't undefined.  */
5632
0
    if (isym->st_size != 0
5633
0
        && isym->st_shndx != SHN_UNDEF
5634
0
        && (definition || h->size == 0))
5635
0
      {
5636
0
        if (h->size != 0
5637
0
      && h->size != isym->st_size
5638
0
      && ! size_change_ok)
5639
0
    {
5640
0
      _bfd_error_handler
5641
        /* xgettext:c-format */
5642
0
        (_("warning: size of symbol `%s' changed"
5643
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5644
0
         name, (uint64_t) h->size, old_bfd,
5645
0
         (uint64_t) isym->st_size, abfd);
5646
5647
      /* PR 30499: make sure that users understand that this warning is serious.  */
5648
0
      _bfd_error_handler
5649
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5650
0
    }
5651
5652
0
        h->size = isym->st_size;
5653
0
      }
5654
5655
    /* If this is a common symbol, then we always want H->SIZE
5656
       to be the size of the common symbol.  The code just above
5657
       won't fix the size if a common symbol becomes larger.  We
5658
       don't warn about a size change here, because that is
5659
       covered by --warn-common.  Allow changes between different
5660
       function types.  */
5661
0
    if (h->root.type == bfd_link_hash_common)
5662
0
      h->size = h->root.u.c.size;
5663
5664
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5665
0
        && ((definition && !new_weak)
5666
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5667
0
      || h->type == STT_NOTYPE))
5668
0
      {
5669
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5670
5671
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5672
     symbol.  */
5673
0
        if (type == STT_GNU_IFUNC
5674
0
      && (abfd->flags & DYNAMIC) != 0)
5675
0
    type = STT_FUNC;
5676
5677
0
        if (h->type != type)
5678
0
    {
5679
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5680
        /* xgettext:c-format */
5681
0
        _bfd_error_handler
5682
0
          (_("warning: type of symbol `%s' changed"
5683
0
       " from %d to %d in %pB"),
5684
0
           name, h->type, type, abfd);
5685
5686
0
      h->type = type;
5687
0
    }
5688
0
      }
5689
5690
    /* Merge st_other field.  */
5691
0
    elf_merge_st_other (info->output_bfd, abfd, h, isym->st_other,
5692
0
            sec, definition, dynamic);
5693
5694
    /* We don't want to make debug symbol dynamic.  */
5695
0
    if (definition
5696
0
        && (sec->flags & SEC_DEBUGGING)
5697
0
        && !bfd_link_relocatable (info))
5698
0
      dynsym = false;
5699
5700
    /* Nor should we make plugin symbols dynamic.  */
5701
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5702
0
      dynsym = false;
5703
5704
0
    if (definition)
5705
0
      {
5706
0
        h->target_internal = isym->st_target_internal;
5707
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5708
0
      }
5709
5710
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5711
       in IR.  Since all data or text symbols in IR have the
5712
       same type, value and section, we can't tell if a symbol
5713
       is an alias of another symbol by their types, values and
5714
       sections.  */
5715
0
    if (definition
5716
0
        && !dynamic
5717
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5718
0
      {
5719
0
        const char *p = strchr (name, ELF_VER_CHR);
5720
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5721
0
    {
5722
      /* Queue non-default versions so that .symver x, x@FOO
5723
         aliases can be checked.  */
5724
0
      if (!nondeflt_vers)
5725
0
        {
5726
0
          nondeflt_vers = bfd_malloc ((isymend - isym + 1)
5727
0
              * sizeof (*nondeflt_vers));
5728
0
          if (!nondeflt_vers)
5729
0
      goto error_free_vers;
5730
0
        }
5731
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5732
0
    }
5733
0
      }
5734
5735
0
    if (dynsym && h->dynindx == -1)
5736
0
      {
5737
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5738
0
    goto error_free_vers;
5739
0
        if (h->is_weakalias
5740
0
      && weakdef (h)->dynindx == -1)
5741
0
    {
5742
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5743
0
        goto error_free_vers;
5744
0
    }
5745
0
      }
5746
0
    else if (h->dynindx != -1)
5747
      /* If the symbol already has a dynamic index, but
5748
         visibility says it should not be visible, turn it into
5749
         a local symbol.  */
5750
0
      switch (ELF_ST_VISIBILITY (h->other))
5751
0
        {
5752
0
        case STV_INTERNAL:
5753
0
        case STV_HIDDEN:
5754
0
    obed->elf_backend_hide_symbol (info, h, true);
5755
0
    dynsym = false;
5756
0
    break;
5757
0
        }
5758
5759
0
    if (!add_needed
5760
0
        && matched
5761
0
        && definition
5762
0
        && h->root.type != bfd_link_hash_indirect)
5763
0
      {
5764
0
        if ((dynsym
5765
0
       && h->ref_regular_nonweak)
5766
0
      || (old_bfd != NULL
5767
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5768
0
          && h->ref_ir_nonweak
5769
0
          && !info->lto_all_symbols_read)
5770
0
      || (h->ref_dynamic_nonweak
5771
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5772
0
          && !on_needed_list (elf_dt_name (abfd),
5773
0
            htab->needed, NULL)))
5774
0
    {
5775
0
      const char *soname = elf_dt_name (abfd);
5776
5777
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5778
0
            h->root.root.string);
5779
5780
      /* A symbol from a library loaded via DT_NEEDED of some
5781
         other library is referenced by a regular object.
5782
         Add a DT_NEEDED entry for it.  Issue an error if
5783
         --no-add-needed is used and the reference was not
5784
         a weak one.  */
5785
0
      if (old_bfd != NULL
5786
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5787
0
        {
5788
0
          _bfd_error_handler
5789
      /* xgettext:c-format */
5790
0
      (_("%pB: undefined reference to symbol '%s'"),
5791
0
       old_bfd, name);
5792
0
          bfd_set_error (bfd_error_missing_dso);
5793
0
          goto error_free_vers;
5794
0
        }
5795
5796
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5797
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5798
5799
      /* Create dynamic sections for backends that require
5800
         that be done before setup_gnu_properties.  */
5801
0
      if (!bfd_elf_link_create_dynamic_sections (info))
5802
0
        goto error_free_vers;
5803
0
      add_needed = true;
5804
0
    }
5805
0
        else if (dynamic
5806
0
           && h->root.u.def.section->owner == abfd)
5807
0
    {
5808
      /* Add this symbol to first hash if this shared
5809
         object has the first definition.  */
5810
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5811
      /* And if it was the default symbol version definition,
5812
         also add the short name.  */
5813
0
      if (defvername)
5814
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5815
0
    }
5816
0
      }
5817
0
  }
5818
0
    }
5819
5820
0
  if (info->lto_plugin_active
5821
0
      && !bfd_link_relocatable (info)
5822
0
      && (abfd->flags & BFD_PLUGIN) == 0
5823
0
      && !just_syms
5824
0
      && extsymcount != 0
5825
0
      && is_elf_hash_table (&htab->root))
5826
0
    {
5827
0
      int r_sym_shift;
5828
5829
0
      if (bed->s->arch_size == 32)
5830
0
  r_sym_shift = 8;
5831
0
      else
5832
0
  r_sym_shift = 32;
5833
5834
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5835
   referenced in regular objects so that linker plugin will get
5836
   the correct symbol resolution.  */
5837
5838
0
      sym_hash = elf_sym_hashes (abfd);
5839
0
      for (s = abfd->sections; s != NULL; s = s->next)
5840
0
  {
5841
0
    Elf_Internal_Rela *internal_relocs;
5842
0
    Elf_Internal_Rela *rel, *relend;
5843
5844
    /* Don't check relocations in excluded sections.  */
5845
0
    if ((s->flags & SEC_RELOC) == 0
5846
0
        || s->reloc_count == 0
5847
0
        || (s->flags & SEC_EXCLUDE) != 0
5848
0
        || (s->flags & SEC_DEBUGGING) != 0)
5849
0
      continue;
5850
5851
0
    internal_relocs = _bfd_elf_link_info_read_relocs
5852
0
      (abfd, info, s, NULL, NULL,
5853
0
       _bfd_elf_link_keep_memory (info));
5854
0
    if (internal_relocs == NULL)
5855
0
      goto error_free_vers;
5856
5857
0
    rel = internal_relocs;
5858
0
    relend = rel + s->reloc_count;
5859
0
    for ( ; rel < relend; rel++)
5860
0
      {
5861
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5862
0
        struct elf_link_hash_entry *h;
5863
5864
        /* Skip local symbols.  */
5865
0
        if (r_symndx < extsymoff)
5866
0
    continue;
5867
5868
0
        h = sym_hash[r_symndx - extsymoff];
5869
0
        if (h != NULL)
5870
0
    h->root.non_ir_ref_regular = 1;
5871
0
      }
5872
5873
0
    if (elf_section_data (s)->relocs != internal_relocs)
5874
0
      free (internal_relocs);
5875
0
  }
5876
0
    }
5877
5878
0
  free (extversym);
5879
0
  extversym = NULL;
5880
0
  free (isymbuf);
5881
0
  isymbuf = NULL;
5882
5883
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5884
0
    {
5885
0
      unsigned int i;
5886
5887
      /* Restore the symbol table.  */
5888
0
      old_ent = (char *) old_tab + tabsize;
5889
0
      memset (elf_sym_hashes (abfd), 0,
5890
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5891
0
      htab->root.table.table = old_table;
5892
0
      htab->root.table.size = old_size;
5893
0
      htab->root.table.count = old_count;
5894
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5895
0
      htab->root.undefs = old_undefs;
5896
0
      htab->root.undefs_tail = old_undefs_tail;
5897
0
      if (htab->dynstr != NULL)
5898
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5899
0
      free (old_strtab);
5900
0
      old_strtab = NULL;
5901
0
      for (i = 0; i < htab->root.table.size; i++)
5902
0
  {
5903
0
    struct bfd_hash_entry *p;
5904
0
    struct elf_link_hash_entry *h;
5905
0
    unsigned int non_ir_ref_dynamic;
5906
5907
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5908
0
      {
5909
        /* Preserve non_ir_ref_dynamic so that this symbol
5910
     will be exported when the dynamic lib becomes needed
5911
     in the second pass.  */
5912
0
        h = (struct elf_link_hash_entry *) p;
5913
0
        if (h->root.type == bfd_link_hash_warning)
5914
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5915
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5916
5917
0
        h = (struct elf_link_hash_entry *) p;
5918
0
        memcpy (h, old_ent, htab->root.table.entsize);
5919
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5920
0
        if (h->root.type == bfd_link_hash_warning)
5921
0
    {
5922
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5923
0
      memcpy (h, old_ent, htab->root.table.entsize);
5924
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5925
0
    }
5926
0
        if (h->root.type == bfd_link_hash_common)
5927
0
    {
5928
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5929
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5930
0
    }
5931
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5932
0
      }
5933
0
  }
5934
5935
      /* Make a special call to the linker "notice" function to
5936
   tell it that symbols added for crefs may need to be removed.  */
5937
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5938
0
  goto error_free_vers;
5939
5940
0
      free (old_tab);
5941
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5942
0
         alloc_mark);
5943
0
      free (nondeflt_vers);
5944
0
      return true;
5945
0
    }
5946
5947
0
  free (old_strtab);
5948
0
  old_strtab = NULL;
5949
0
  if (old_tab != NULL)
5950
0
    {
5951
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5952
0
  goto error_free_vers;
5953
0
      free (old_tab);
5954
0
      old_tab = NULL;
5955
0
    }
5956
5957
  /* Now that all the symbols from this input file are created, if
5958
     not performing a relocatable link, handle .symver foo, foo@BAR
5959
     such that any relocs against foo become foo@BAR.  */
5960
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5961
0
    {
5962
0
      size_t cnt, symidx;
5963
5964
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5965
0
  {
5966
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5967
0
    char *shortname;
5968
0
    const char *p;
5969
0
    size_t amt;
5970
5971
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5972
0
    if (p == NULL
5973
0
        || (h->root.type != bfd_link_hash_defined
5974
0
      && h->root.type != bfd_link_hash_defweak))
5975
0
      continue;
5976
5977
0
    amt = p - h->root.root.string;
5978
0
    shortname = bfd_malloc (amt + 1);
5979
0
    if (!shortname)
5980
0
      goto error_free_vers;
5981
0
    memcpy (shortname, h->root.root.string, amt);
5982
0
    shortname[amt] = '\0';
5983
5984
0
    hi = (struct elf_link_hash_entry *)
5985
0
         bfd_link_hash_lookup (&htab->root, shortname,
5986
0
             false, false, false);
5987
0
    if (hi != NULL
5988
0
        && hi->root.type == h->root.type
5989
0
        && hi->root.u.def.value == h->root.u.def.value
5990
0
        && hi->root.u.def.section == h->root.u.def.section)
5991
0
      {
5992
0
        obed->elf_backend_hide_symbol (info, hi, true);
5993
0
        hi->root.type = bfd_link_hash_indirect;
5994
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5995
0
        obed->elf_backend_copy_indirect_symbol (info, h, hi);
5996
0
        sym_hash = elf_sym_hashes (abfd);
5997
0
        if (sym_hash)
5998
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5999
0
      if (sym_hash[symidx] == hi)
6000
0
        {
6001
0
          sym_hash[symidx] = h;
6002
0
          break;
6003
0
        }
6004
0
      }
6005
0
    free (shortname);
6006
0
  }
6007
0
    }
6008
0
  free (nondeflt_vers);
6009
0
  nondeflt_vers = NULL;
6010
6011
  /* Now set the alias field correctly for all the weak defined
6012
     symbols we found.  The only way to do this is to search all the
6013
     symbols.  Since we only need the information for non functions in
6014
     dynamic objects, that's the only time we actually put anything on
6015
     the list WEAKS.  We need this information so that if a regular
6016
     object refers to a symbol defined weakly in a dynamic object, the
6017
     real symbol in the dynamic object is also put in the dynamic
6018
     symbols; we also must arrange for both symbols to point to the
6019
     same memory location.  We could handle the general case of symbol
6020
     aliasing, but a general symbol alias can only be generated in
6021
     assembler code, handling it correctly would be very time
6022
     consuming, and other ELF linkers don't handle general aliasing
6023
     either.  */
6024
0
  if (weaks != NULL)
6025
0
    {
6026
0
      struct elf_link_hash_entry **hpp;
6027
0
      struct elf_link_hash_entry **hppend;
6028
0
      struct elf_link_hash_entry **sorted_sym_hash;
6029
0
      struct elf_link_hash_entry *h;
6030
0
      size_t sym_count;
6031
6032
      /* Since we have to search the whole symbol list for each weak
6033
   defined symbol, search time for N weak defined symbols will be
6034
   O(N^2). Binary search will cut it down to O(NlogN).  */
6035
0
      sorted_sym_hash = bfd_malloc (extsymcount * sizeof (*sorted_sym_hash));
6036
0
      if (sorted_sym_hash == NULL)
6037
0
  goto error_return;
6038
0
      sym_hash = sorted_sym_hash;
6039
0
      hpp = elf_sym_hashes (abfd);
6040
0
      hppend = hpp + extsymcount;
6041
0
      sym_count = 0;
6042
0
      for (; hpp < hppend; hpp++)
6043
0
  {
6044
0
    h = *hpp;
6045
0
    if (h != NULL
6046
0
        && h->root.type == bfd_link_hash_defined
6047
0
        && !bed->is_function_type (h->type))
6048
0
      {
6049
0
        *sym_hash = h;
6050
0
        sym_hash++;
6051
0
        sym_count++;
6052
0
      }
6053
0
  }
6054
6055
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6056
0
       elf_sort_symbol);
6057
6058
0
      while (weaks != NULL)
6059
0
  {
6060
0
    struct elf_link_hash_entry *hlook;
6061
0
    asection *slook;
6062
0
    bfd_vma vlook;
6063
0
    size_t i, j, idx = 0;
6064
6065
0
    hlook = weaks;
6066
0
    weaks = hlook->u.alias;
6067
0
    hlook->u.alias = NULL;
6068
6069
0
    if (hlook->root.type != bfd_link_hash_defined
6070
0
        && hlook->root.type != bfd_link_hash_defweak)
6071
0
      continue;
6072
6073
0
    slook = hlook->root.u.def.section;
6074
0
    vlook = hlook->root.u.def.value;
6075
6076
0
    i = 0;
6077
0
    j = sym_count;
6078
0
    while (i != j)
6079
0
      {
6080
0
        bfd_signed_vma vdiff;
6081
0
        idx = (i + j) / 2;
6082
0
        h = sorted_sym_hash[idx];
6083
0
        vdiff = vlook - h->root.u.def.value;
6084
0
        if (vdiff < 0)
6085
0
    j = idx;
6086
0
        else if (vdiff > 0)
6087
0
    i = idx + 1;
6088
0
        else
6089
0
    {
6090
0
      int sdiff = slook->id - h->root.u.def.section->id;
6091
0
      if (sdiff < 0)
6092
0
        j = idx;
6093
0
      else if (sdiff > 0)
6094
0
        i = idx + 1;
6095
0
      else
6096
0
        break;
6097
0
    }
6098
0
      }
6099
6100
    /* We didn't find a value/section match.  */
6101
0
    if (i == j)
6102
0
      continue;
6103
6104
    /* With multiple aliases, or when the weak symbol is already
6105
       strongly defined, we have multiple matching symbols and
6106
       the binary search above may land on any of them.  Step
6107
       one past the matching symbol(s).  */
6108
0
    while (++idx != j)
6109
0
      {
6110
0
        h = sorted_sym_hash[idx];
6111
0
        if (h->root.u.def.section != slook
6112
0
      || h->root.u.def.value != vlook)
6113
0
    break;
6114
0
      }
6115
6116
    /* Now look back over the aliases.  Since we sorted by size
6117
       as well as value and section, we'll choose the one with
6118
       the largest size.  */
6119
0
    while (idx-- != i)
6120
0
      {
6121
0
        h = sorted_sym_hash[idx];
6122
6123
        /* Stop if value or section doesn't match.  */
6124
0
        if (h->root.u.def.section != slook
6125
0
      || h->root.u.def.value != vlook)
6126
0
    break;
6127
0
        else if (h != hlook)
6128
0
    {
6129
0
      struct elf_link_hash_entry *t;
6130
6131
0
      hlook->u.alias = h;
6132
0
      hlook->is_weakalias = 1;
6133
0
      t = h;
6134
0
      if (t->u.alias != NULL)
6135
0
        while (t->u.alias != h)
6136
0
          t = t->u.alias;
6137
0
      t->u.alias = hlook;
6138
6139
      /* If the weak definition is in the list of dynamic
6140
         symbols, make sure the real definition is put
6141
         there as well.  */
6142
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6143
0
        {
6144
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6145
0
      {
6146
0
      err_free_sym_hash:
6147
0
        free (sorted_sym_hash);
6148
0
        goto error_return;
6149
0
      }
6150
0
        }
6151
6152
      /* If the real definition is in the list of dynamic
6153
         symbols, make sure the weak definition is put
6154
         there as well.  If we don't do this, then the
6155
         dynamic loader might not merge the entries for the
6156
         real definition and the weak definition.  */
6157
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6158
0
        {
6159
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6160
0
      goto err_free_sym_hash;
6161
0
        }
6162
0
      break;
6163
0
    }
6164
0
      }
6165
0
  }
6166
6167
0
      free (sorted_sym_hash);
6168
0
    }
6169
6170
0
  if (bed->check_directives
6171
0
      && !(*bed->check_directives) (abfd, info))
6172
0
    goto error_return;
6173
6174
  /* If this is a non-traditional link, try to optimize the handling
6175
     of the .stab/.stabstr sections.  */
6176
0
  if (! dynamic
6177
0
      && ! info->traditional_format
6178
0
      && is_elf_hash_table (&htab->root)
6179
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6180
0
    {
6181
0
      asection *stabstr;
6182
6183
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6184
0
      if (stabstr != NULL)
6185
0
  {
6186
0
    bfd_size_type string_offset = 0;
6187
0
    asection *stab;
6188
6189
0
    for (stab = abfd->sections; stab; stab = stab->next)
6190
0
      if (startswith (stab->name, ".stab")
6191
0
    && (!stab->name[5] ||
6192
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6193
0
    && (stab->flags & SEC_MERGE) == 0
6194
0
    && !bfd_is_abs_section (stab->output_section)
6195
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6196
0
               stabstr, &string_offset))
6197
0
        goto error_return;
6198
0
  }
6199
0
    }
6200
6201
0
  if (dynamic && add_needed)
6202
0
    {
6203
      /* Add this bfd to the loaded list.  */
6204
0
      struct elf_link_loaded_list *n;
6205
6206
0
      n = bfd_alloc (abfd, sizeof (*n));
6207
0
      if (n == NULL)
6208
0
  goto error_return;
6209
0
      n->abfd = abfd;
6210
0
      n->next = htab->dyn_loaded;
6211
0
      htab->dyn_loaded = n;
6212
0
    }
6213
0
  if (dynamic && !add_needed
6214
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6215
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6216
6217
0
  return true;
6218
6219
0
 error_free_vers:
6220
0
  free (old_tab);
6221
0
  free (old_strtab);
6222
0
  free (nondeflt_vers);
6223
0
  free (extversym);
6224
0
 error_free_sym:
6225
0
  free (isymbuf);
6226
0
 error_return:
6227
0
  return false;
6228
0
}
6229
6230
/* Return the linker hash table entry of a symbol that might be
6231
   satisfied by an archive symbol.  Return -1 on error.  */
6232
6233
struct bfd_link_hash_entry *
6234
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6235
        struct bfd_link_info *info,
6236
        const char *name)
6237
0
{
6238
0
  struct bfd_link_hash_entry *h;
6239
0
  const char *p;
6240
0
  char *copy;
6241
0
  size_t len, first;
6242
6243
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6244
0
  if (h != NULL)
6245
0
    return h;
6246
6247
  /* If this is a default version (the name contains @@), look up the
6248
     symbol again with only one `@' as well as without the version.
6249
     The effect is that references to the symbol with and without the
6250
     version will be matched by the default symbol in the archive.  */
6251
6252
0
  p = strchr (name, ELF_VER_CHR);
6253
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6254
0
    {
6255
      /* Add this symbol to first hash if this archive has the first
6256
   definition.  */
6257
0
      if (is_elf_hash_table (info->hash))
6258
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6259
0
      return h;
6260
0
    }
6261
6262
  /* First check with only one `@'.  */
6263
0
  len = strlen (name);
6264
0
  copy = bfd_alloc (abfd, len);
6265
0
  if (copy == NULL)
6266
0
    return (struct bfd_link_hash_entry *) -1;
6267
6268
0
  first = p - name + 1;
6269
0
  memcpy (copy, name, first);
6270
0
  memcpy (copy + first, name + first + 1, len - first);
6271
6272
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6273
0
  if (h == NULL)
6274
0
    {
6275
      /* We also need to check references to the symbol without the
6276
   version.  */
6277
0
      copy[first - 1] = '\0';
6278
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6279
0
    }
6280
6281
0
  bfd_release (abfd, copy);
6282
0
  return h;
6283
0
}
6284
6285
/* Add symbols from an ELF archive file to the linker hash table.  We
6286
   don't use _bfd_generic_link_add_archive_symbols because we need to
6287
   handle versioned symbols.
6288
6289
   Fortunately, ELF archive handling is simpler than that done by
6290
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6291
   oddities.  In ELF, if we find a symbol in the archive map, and the
6292
   symbol is currently undefined, we know that we must pull in that
6293
   object file.
6294
6295
   Unfortunately, we do have to make multiple passes over the symbol
6296
   table until nothing further is resolved.  */
6297
6298
static bool
6299
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6300
0
{
6301
0
  symindex c;
6302
0
  unsigned char *included = NULL;
6303
0
  carsym *symdefs;
6304
0
  bool loop;
6305
0
  elf_backend_data *bed;
6306
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6307
0
    (bfd *, struct bfd_link_info *, const char *);
6308
6309
0
  if (! bfd_has_map (abfd))
6310
0
    {
6311
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6312
6313
      /* An empty archive is a special case.  */
6314
0
      if (first_one == NULL)
6315
0
  return true;
6316
6317
0
      if (!_bfd_make_armap (abfd, first_one))
6318
0
  return false;
6319
0
    }
6320
6321
  /* Keep track of all symbols we know to be already defined, and all
6322
     files we know to be already included.  This is to speed up the
6323
     second and subsequent passes.  */
6324
0
  c = bfd_ardata (abfd)->symdef_count;
6325
0
  if (c == 0)
6326
0
    return true;
6327
0
  included = bfd_zmalloc (c * sizeof (*included));
6328
0
  if (included == NULL)
6329
0
    return false;
6330
6331
0
  symdefs = bfd_ardata (abfd)->symdefs;
6332
0
  bed = get_elf_backend_data (abfd);
6333
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6334
6335
0
  do
6336
0
    {
6337
0
      ufile_ptr_or_bfd last;
6338
0
      symindex i;
6339
0
      carsym *symdef;
6340
0
      carsym *symdefend;
6341
6342
0
      loop = false;
6343
0
      last = _bfd_elt_nil (abfd);
6344
6345
0
      symdef = symdefs;
6346
0
      symdefend = symdef + c;
6347
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6348
0
  {
6349
0
    struct bfd_link_hash_entry *h;
6350
0
    bfd *element;
6351
0
    struct bfd_link_hash_entry *undefs_tail;
6352
0
    symindex mark;
6353
6354
0
    if (included[i])
6355
0
      continue;
6356
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6357
0
      {
6358
0
        included[i] = true;
6359
0
        continue;
6360
0
      }
6361
6362
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6363
0
    if (h == (struct bfd_link_hash_entry *) -1)
6364
0
      goto error_return;
6365
6366
0
    if (h == NULL)
6367
0
      continue;
6368
6369
0
    if (h->type == bfd_link_hash_undefined)
6370
0
      {
6371
0
        if (is_elf_hash_table (info->hash))
6372
0
    {
6373
      /* If the archive element has already been loaded then one
6374
         of the symbols defined by that element might have been
6375
         made undefined due to being in a discarded section.  */
6376
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6377
0
        continue;
6378
6379
      /* In the pre-LTO-plugin pass we must not mistakenly
6380
         include this archive member if an earlier shared
6381
         library defined this symbol.  */
6382
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6383
0
      if (htab->first_hash)
6384
0
        {
6385
0
          struct elf_link_first_hash_entry *e
6386
0
        = ((struct elf_link_first_hash_entry *)
6387
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6388
0
                false, false));
6389
0
          if (e
6390
0
        && (e->abfd->flags & DYNAMIC) != 0
6391
0
        && e->abfd != abfd)
6392
0
      continue;
6393
0
        }
6394
0
    }
6395
0
      }
6396
0
    else if (h->type == bfd_link_hash_common)
6397
0
      {
6398
        /* We currently have a common symbol.  The archive map contains
6399
     a reference to this symbol, so we may want to include it.  We
6400
     only want to include it however, if this archive element
6401
     contains a definition of the symbol, not just another common
6402
     declaration of it.
6403
6404
     Unfortunately some archivers (including GNU ar) will put
6405
     declarations of common symbols into their archive maps, as
6406
     well as real definitions, so we cannot just go by the archive
6407
     map alone.  Instead we must read in the element's symbol
6408
     table and check that to see what kind of symbol definition
6409
     this is.  */
6410
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6411
0
    continue;
6412
0
      }
6413
0
    else
6414
0
      {
6415
0
        if (h->type != bfd_link_hash_undefweak)
6416
    /* Symbol must be defined.  Don't check it again.  */
6417
0
    included[i] = true;
6418
6419
0
        if (!is_elf_hash_table (info->hash))
6420
0
    continue;
6421
0
        struct elf_link_hash_entry *eh
6422
0
    = (struct elf_link_hash_entry *) h;
6423
        /* Ignore the archive if the symbol isn't referenced by a
6424
     regular object or isn't defined in a shared object.  */
6425
0
        if (!eh->ref_regular || !eh->def_dynamic)
6426
0
    continue;
6427
        /* Ignore the dynamic definition if symbol is first
6428
     defined in this archive.  */
6429
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6430
0
        if (htab->first_hash == NULL)
6431
0
    continue;
6432
0
        struct elf_link_first_hash_entry *e
6433
0
    = ((struct elf_link_first_hash_entry *)
6434
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6435
0
            false, false));
6436
0
        if (e == NULL || e->abfd != abfd)
6437
0
    continue;
6438
0
      }
6439
6440
    /* We need to include this archive member.  */
6441
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6442
0
    if (element == NULL)
6443
0
      goto error_return;
6444
6445
0
    if (! bfd_check_format (element, bfd_object))
6446
0
      goto error_return;
6447
6448
0
    undefs_tail = info->hash->undefs_tail;
6449
6450
0
    if (!(*info->callbacks
6451
0
    ->add_archive_element) (info, element, symdef->name, &element))
6452
0
      continue;
6453
0
    if (!bfd_link_add_symbols (element, info))
6454
0
      goto error_return;
6455
6456
    /* If there are any new undefined symbols, we need to make
6457
       another pass through the archive in order to see whether
6458
       they can be defined.  FIXME: This isn't perfect, because
6459
       common symbols wind up on undefs_tail and because an
6460
       undefined symbol which is defined later on in this pass
6461
       does not require another pass.  This isn't a bug, but it
6462
       does make the code less efficient than it could be.  */
6463
0
    if (undefs_tail != info->hash->undefs_tail)
6464
0
      loop = true;
6465
6466
    /* Look backward to mark all symbols from this object file
6467
       which we have already seen in this pass.  */
6468
0
    mark = i;
6469
0
    do
6470
0
      {
6471
0
        included[mark] = true;
6472
0
        if (mark == 0)
6473
0
    break;
6474
0
        --mark;
6475
0
      }
6476
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6477
6478
    /* We mark subsequent symbols from this object file as we go
6479
       on through the loop.  */
6480
0
    last = symdef->u;
6481
0
  }
6482
0
    }
6483
0
  while (loop);
6484
6485
0
  free (included);
6486
0
  return true;
6487
6488
0
 error_return:
6489
0
  free (included);
6490
0
  return false;
6491
0
}
6492
6493
/* Given an ELF BFD, add symbols to the global hash table as
6494
   appropriate.  */
6495
6496
bool
6497
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6498
0
{
6499
0
  switch (bfd_get_format (abfd))
6500
0
    {
6501
0
    case bfd_object:
6502
0
      return elf_link_add_object_symbols (abfd, info);
6503
0
    case bfd_archive:
6504
0
      return elf_link_add_archive_symbols (abfd, info);
6505
0
    default:
6506
0
      bfd_set_error (bfd_error_wrong_format);
6507
0
      return false;
6508
0
    }
6509
0
}
6510

6511
struct hash_codes_info
6512
{
6513
  unsigned long *hashcodes;
6514
  bool error;
6515
};
6516
6517
/* This function will be called though elf_link_hash_traverse to store
6518
   all hash value of the exported symbols in an array.  */
6519
6520
static bool
6521
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6522
0
{
6523
0
  struct hash_codes_info *inf = data;
6524
0
  const char *name;
6525
0
  unsigned long ha;
6526
0
  char *alc = NULL;
6527
6528
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6529
0
  if (h->dynindx == -1)
6530
0
    return true;
6531
6532
0
  name = h->root.root.string;
6533
0
  if (h->versioned >= versioned)
6534
0
    {
6535
0
      const char *p = strchr (name, ELF_VER_CHR);
6536
0
      if (p != NULL)
6537
0
  {
6538
0
    alc = bfd_malloc (p - name + 1);
6539
0
    if (alc == NULL)
6540
0
      {
6541
0
        inf->error = true;
6542
0
        return false;
6543
0
      }
6544
0
    memcpy (alc, name, p - name);
6545
0
    alc[p - name] = '\0';
6546
0
    name = alc;
6547
0
  }
6548
0
    }
6549
6550
  /* Compute the hash value.  */
6551
0
  ha = bfd_elf_hash (name);
6552
6553
  /* Store the found hash value in the array given as the argument.  */
6554
0
  *(inf->hashcodes)++ = ha;
6555
6556
  /* And store it in the struct so that we can put it in the hash table
6557
     later.  */
6558
0
  h->u.elf_hash_value = ha;
6559
6560
0
  free (alc);
6561
0
  return true;
6562
0
}
6563
6564
struct collect_gnu_hash_codes
6565
{
6566
  bfd *output_bfd;
6567
  elf_backend_data *bed;
6568
  unsigned long int nsyms;
6569
  unsigned long int maskbits;
6570
  unsigned long int *hashcodes;
6571
  unsigned long int *hashval;
6572
  unsigned long int *indx;
6573
  unsigned long int *counts;
6574
  bfd_vma *bitmask;
6575
  bfd_byte *contents;
6576
  bfd_size_type xlat;
6577
  long int min_dynindx;
6578
  unsigned long int bucketcount;
6579
  unsigned long int symindx;
6580
  long int local_indx;
6581
  long int shift1, shift2;
6582
  unsigned long int mask;
6583
  bool error;
6584
  bool base_symbol;
6585
};
6586
6587
/* This function will be called though elf_link_hash_traverse to store
6588
   all hash value of the exported symbols in an array.  */
6589
6590
static bool
6591
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6592
0
{
6593
0
  struct collect_gnu_hash_codes *s = data;
6594
0
  const char *name;
6595
0
  unsigned long ha;
6596
0
  char *alc = NULL;
6597
6598
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6599
0
  if (h->dynindx == -1)
6600
0
    return true;
6601
6602
  /* Ignore also local symbols and undefined symbols.  */
6603
0
  if (! (*s->bed->elf_hash_symbol) (h))
6604
0
    return true;
6605
6606
0
  name = h->root.root.string;
6607
0
  if (h->versioned >= versioned)
6608
0
    {
6609
0
      const char *p = strchr (name, ELF_VER_CHR);
6610
0
      if (p != NULL)
6611
0
  {
6612
0
    alc = bfd_malloc (p - name + 1);
6613
0
    if (alc == NULL)
6614
0
      {
6615
0
        s->error = true;
6616
0
        return false;
6617
0
      }
6618
0
    memcpy (alc, name, p - name);
6619
0
    alc[p - name] = '\0';
6620
0
    name = alc;
6621
0
  }
6622
0
    }
6623
6624
  /* Compute the hash value.  */
6625
0
  ha = bfd_elf_gnu_hash (name);
6626
6627
  /* Store the found hash value in the array for compute_bucket_count,
6628
     and also for .dynsym reordering purposes.  */
6629
0
  s->hashcodes[s->nsyms] = ha;
6630
0
  s->hashval[h->dynindx] = ha;
6631
0
  ++s->nsyms;
6632
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6633
0
    s->min_dynindx = h->dynindx;
6634
6635
0
  free (alc);
6636
0
  return true;
6637
0
}
6638
6639
/* This function will be called though elf_link_hash_traverse to do
6640
   final dynamic symbol renumbering in case of .gnu.hash.
6641
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6642
   to the translation table.  */
6643
6644
static bool
6645
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6646
0
{
6647
0
  struct collect_gnu_hash_codes *s = data;
6648
0
  unsigned long int bucket;
6649
0
  unsigned long int val;
6650
6651
  /* Ignore indirect symbols.  */
6652
0
  if (h->dynindx == -1)
6653
0
    return true;
6654
6655
  /* Skip if base symbol doesn't match.  */
6656
0
  if (s->base_symbol != !!h->base_symbol)
6657
0
    return true;
6658
6659
  /* Ignore also local symbols and undefined symbols.  */
6660
0
  if (! (*s->bed->elf_hash_symbol) (h))
6661
0
    {
6662
0
      if (h->dynindx >= s->min_dynindx)
6663
0
  {
6664
0
    if (s->bed->record_xhash_symbol != NULL)
6665
0
      {
6666
0
        (*s->bed->record_xhash_symbol) (h, 0);
6667
0
        s->local_indx++;
6668
0
      }
6669
0
    else
6670
0
      h->dynindx = s->local_indx++;
6671
0
  }
6672
0
      return true;
6673
0
    }
6674
6675
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6676
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6677
0
  & ((s->maskbits >> s->shift1) - 1);
6678
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6679
0
  s->bitmask[val]
6680
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6681
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6682
0
  if (s->counts[bucket] == 1)
6683
    /* Last element terminates the chain.  */
6684
0
    val |= 1;
6685
0
  bfd_put_32 (s->output_bfd, val,
6686
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6687
0
  --s->counts[bucket];
6688
0
  if (s->bed->record_xhash_symbol != NULL)
6689
0
    {
6690
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6691
6692
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6693
0
    }
6694
0
  else
6695
0
    h->dynindx = s->indx[bucket]++;
6696
0
  return true;
6697
0
}
6698
6699
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6700
6701
bool
6702
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6703
0
{
6704
0
  return !(h->forced_local
6705
0
     || h->root.type == bfd_link_hash_undefined
6706
0
     || h->root.type == bfd_link_hash_undefweak
6707
0
     || ((h->root.type == bfd_link_hash_defined
6708
0
    || h->root.type == bfd_link_hash_defweak)
6709
0
         && h->root.u.def.section->output_section == NULL));
6710
0
}
6711
6712
/* Array used to determine the number of hash table buckets to use
6713
   based on the number of symbols there are.  If there are fewer than
6714
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6715
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6716
   than 32771 buckets.  */
6717
6718
static const size_t elf_buckets[] =
6719
{
6720
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6721
  16411, 32771, 0
6722
};
6723
6724
/* Compute bucket count for hashing table.  We do not use a static set
6725
   of possible tables sizes anymore.  Instead we determine for all
6726
   possible reasonable sizes of the table the outcome (i.e., the
6727
   number of collisions etc) and choose the best solution.  The
6728
   weighting functions are not too simple to allow the table to grow
6729
   without bounds.  Instead one of the weighting factors is the size.
6730
   Therefore the result is always a good payoff between few collisions
6731
   (= short chain lengths) and table size.  */
6732
static size_t
6733
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6734
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6735
          unsigned long int nsyms,
6736
          int gnu_hash)
6737
0
{
6738
0
  size_t best_size = 0;
6739
0
  unsigned long int i;
6740
6741
0
  if (info->optimize)
6742
0
    {
6743
0
      size_t minsize;
6744
0
      size_t maxsize;
6745
0
      uint64_t best_chlen = ~((uint64_t) 0);
6746
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6747
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6748
0
      elf_backend_data *obed = get_elf_backend_data (dynobj);
6749
0
      unsigned long int *counts;
6750
0
      unsigned int no_improvement_count = 0;
6751
6752
      /* Possible optimization parameters: if we have NSYMS symbols we say
6753
   that the hashing table must at least have NSYMS/4 and at most
6754
   2*NSYMS buckets.  */
6755
0
      minsize = nsyms / 4;
6756
0
      if (minsize == 0)
6757
0
  minsize = 1;
6758
0
      best_size = maxsize = nsyms * 2;
6759
0
      if (gnu_hash)
6760
0
  {
6761
0
    if (minsize < 2)
6762
0
      minsize = 2;
6763
0
    if ((best_size & 31) == 0)
6764
0
      ++best_size;
6765
0
  }
6766
6767
      /* Create array where we count the collisions in.  We must use bfd_malloc
6768
   since the size could be large.  */
6769
0
      counts = bfd_malloc (maxsize * sizeof (*counts));
6770
0
      if (counts == NULL)
6771
0
  return 0;
6772
6773
      /* Compute the "optimal" size for the hash table.  The criteria is a
6774
   minimal chain length.  The minor criteria is (of course) the size
6775
   of the table.  */
6776
0
      for (i = minsize; i < maxsize; ++i)
6777
0
  {
6778
    /* Walk through the array of hashcodes and count the collisions.  */
6779
0
    uint64_t max;
6780
0
    unsigned long int j;
6781
0
    unsigned long int fact;
6782
6783
0
    if (gnu_hash && (i & 31) == 0)
6784
0
      continue;
6785
6786
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6787
6788
    /* Determine how often each hash bucket is used.  */
6789
0
    for (j = 0; j < nsyms; ++j)
6790
0
      ++counts[hashcodes[j] % i];
6791
6792
    /* For the weight function we need some information about the
6793
       pagesize on the target.  This is information need not be 100%
6794
       accurate.  Since this information is not available (so far) we
6795
       define it here to a reasonable default value.  If it is crucial
6796
       to have a better value some day simply define this value.  */
6797
0
# ifndef BFD_TARGET_PAGESIZE
6798
0
#  define BFD_TARGET_PAGESIZE (4096)
6799
0
# endif
6800
6801
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6802
       and the chains.  */
6803
0
    max = (2 + dynsymcount) * obed->s->sizeof_hash_entry;
6804
6805
0
# if 1
6806
    /* Variant 1: optimize for short chains.  We add the squares
6807
       of all the chain lengths (which favors many small chain
6808
       over a few long chains).  */
6809
0
    for (j = 0; j < i; ++j)
6810
0
      max += counts[j] * counts[j];
6811
6812
    /* This adds penalties for the overall size of the table.  */
6813
0
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6814
0
    max *= fact * fact;
6815
# else
6816
    /* Variant 2: Optimize a lot more for small table.  Here we
6817
       also add squares of the size but we also add penalties for
6818
       empty slots (the +1 term).  */
6819
    for (j = 0; j < i; ++j)
6820
      max += (1 + counts[j]) * (1 + counts[j]);
6821
6822
    /* The overall size of the table is considered, but not as
6823
       strong as in variant 1, where it is squared.  */
6824
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6825
    max *= fact;
6826
# endif
6827
6828
    /* Compare with current best results.  */
6829
0
    if (max < best_chlen)
6830
0
      {
6831
0
        best_chlen = max;
6832
0
        best_size = i;
6833
0
        no_improvement_count = 0;
6834
0
      }
6835
    /* PR 11843: Avoid futile long searches for the best bucket size
6836
       when there are a large number of symbols.  */
6837
0
    else if (++no_improvement_count == 100)
6838
0
      break;
6839
0
  }
6840
6841
0
      free (counts);
6842
0
    }
6843
0
  else
6844
0
    {
6845
0
      for (i = 0; elf_buckets[i] != 0; i++)
6846
0
  {
6847
0
    best_size = elf_buckets[i];
6848
0
    if (nsyms < elf_buckets[i + 1])
6849
0
      break;
6850
0
  }
6851
0
      if (gnu_hash && best_size < 2)
6852
0
  best_size = 2;
6853
0
    }
6854
6855
0
  return best_size;
6856
0
}
6857
6858
/* Size any SHT_GROUP section for ld -r.  */
6859
6860
bool
6861
bfd_elf_size_group_sections (struct bfd_link_info *info)
6862
0
{
6863
0
  bfd *ibfd;
6864
0
  asection *s;
6865
6866
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6867
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6868
0
  && (s = ibfd->sections) != NULL
6869
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6870
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6871
0
      return false;
6872
0
  return true;
6873
0
}
6874
6875
/* Set a default stack segment size.  The value in INFO wins.  If it
6876
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6877
   undefined it is initialized.  */
6878
6879
bool
6880
bfd_elf_stack_segment_size (struct bfd_link_info *info,
6881
          const char *legacy_symbol,
6882
          bfd_vma default_size)
6883
0
{
6884
0
  struct elf_link_hash_entry *h = NULL;
6885
6886
  /* Look for legacy symbol.  */
6887
0
  if (legacy_symbol)
6888
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6889
0
            false, false, false);
6890
0
  if (h && (h->root.type == bfd_link_hash_defined
6891
0
      || h->root.type == bfd_link_hash_defweak)
6892
0
      && h->def_regular
6893
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6894
0
    {
6895
      /* The symbol has no type if specified on the command line.  */
6896
0
      h->type = STT_OBJECT;
6897
0
      if (info->stacksize)
6898
  /* xgettext:c-format */
6899
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6900
0
          info->output_bfd, legacy_symbol);
6901
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6902
  /* xgettext:c-format */
6903
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6904
0
          info->output_bfd, legacy_symbol);
6905
0
      else
6906
0
  info->stacksize = h->root.u.def.value;
6907
0
    }
6908
6909
0
  if (!info->stacksize)
6910
    /* If the user didn't set a size, or explicitly inhibit the
6911
       size, set it now.  */
6912
0
    info->stacksize = default_size;
6913
6914
  /* Provide the legacy symbol, if it is referenced.  */
6915
0
  if (h && (h->root.type == bfd_link_hash_undefined
6916
0
      || h->root.type == bfd_link_hash_undefweak))
6917
0
    {
6918
0
      struct bfd_link_hash_entry *bh = NULL;
6919
6920
0
      if (!(_bfd_generic_link_add_one_symbol
6921
0
      (info, info->output_bfd, legacy_symbol,
6922
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6923
0
       info->stacksize >= 0 ? info->stacksize : 0,
6924
0
       NULL, false, get_elf_backend_data (info->output_bfd)->collect,
6925
0
       &bh)))
6926
0
  return false;
6927
6928
0
      h = (struct elf_link_hash_entry *) bh;
6929
0
      h->def_regular = 1;
6930
0
      h->type = STT_OBJECT;
6931
0
    }
6932
6933
0
  return true;
6934
0
}
6935
6936
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6937
6938
struct elf_gc_sweep_symbol_info
6939
{
6940
  struct bfd_link_info *info;
6941
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6942
           bool);
6943
};
6944
6945
static bool
6946
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6947
0
{
6948
0
  if (!h->mark
6949
0
      && (((h->root.type == bfd_link_hash_defined
6950
0
      || h->root.type == bfd_link_hash_defweak)
6951
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6952
0
    && h->root.u.def.section->gc_mark))
6953
0
    || h->root.type == bfd_link_hash_undefined
6954
0
    || h->root.type == bfd_link_hash_undefweak))
6955
0
    {
6956
0
      struct elf_gc_sweep_symbol_info *inf = data;
6957
6958
0
      (*inf->hide_symbol) (inf->info, h, true);
6959
0
      h->def_regular = 0;
6960
0
      h->ref_regular = 0;
6961
0
      h->ref_regular_nonweak = 0;
6962
0
    }
6963
6964
0
  return true;
6965
0
}
6966
6967
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6968
   called by the ELF linker emulation before_allocation routine.  We
6969
   must set the sizes of the sections before the linker sets the
6970
   addresses of the various sections.  */
6971
6972
bool
6973
bfd_elf_size_dynamic_sections (struct bfd_link_info *info,
6974
             const char *soname,
6975
             const char *rpath,
6976
             const char *filter_shlib,
6977
             const char *audit,
6978
             const char *depaudit,
6979
             const char * const *auxiliary_filters,
6980
             asection **sinterpptr)
6981
0
{
6982
0
  bfd *dynobj;
6983
0
  elf_backend_data *obed;
6984
6985
0
  *sinterpptr = NULL;
6986
6987
0
  if (!is_elf_hash_table (info->hash))
6988
0
    return true;
6989
6990
  /* Any syms created from now on start with -1 in
6991
     got.refcount/offset and plt.refcount/offset.  */
6992
0
  elf_hash_table (info)->init_got_refcount
6993
0
    = elf_hash_table (info)->init_got_offset;
6994
0
  elf_hash_table (info)->init_plt_refcount
6995
0
    = elf_hash_table (info)->init_plt_offset;
6996
6997
0
  obed = get_elf_backend_data (info->output_bfd);
6998
6999
  /* The backend may have to create some sections regardless of whether
7000
     we're dynamic or not.  */
7001
0
  if (obed->elf_backend_early_size_sections
7002
0
      && !obed->elf_backend_early_size_sections (info))
7003
0
    return false;
7004
7005
0
  dynobj = elf_hash_table (info)->dynobj;
7006
7007
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7008
0
    {
7009
0
      struct bfd_elf_version_tree *verdefs;
7010
0
      struct elf_info_failed asvinfo;
7011
0
      struct bfd_elf_version_tree *t;
7012
0
      struct bfd_elf_version_expr *d;
7013
0
      asection *s;
7014
0
      size_t soname_indx;
7015
7016
      /* If we are supposed to export all symbols into the dynamic symbol
7017
   table (this is not the normal case), then do so.  */
7018
0
      if (info->export_dynamic
7019
0
    || (bfd_link_executable (info) && info->dynamic))
7020
0
  {
7021
0
    struct elf_info_failed eif;
7022
7023
0
    eif.info = info;
7024
0
    eif.failed = false;
7025
0
    elf_link_hash_traverse (elf_hash_table (info),
7026
0
          _bfd_elf_export_symbol,
7027
0
          &eif);
7028
0
    if (eif.failed)
7029
0
      return false;
7030
0
  }
7031
7032
0
      if (soname != NULL)
7033
0
  {
7034
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7035
0
               soname, true);
7036
0
    if (soname_indx == (size_t) -1
7037
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
7038
0
      return false;
7039
0
  }
7040
0
      else
7041
0
  soname_indx = (size_t) -1;
7042
7043
      /* Make all global versions with definition.  */
7044
0
      for (t = info->version_info; t != NULL; t = t->next)
7045
0
  for (d = t->globals.list; d != NULL; d = d->next)
7046
0
    if (!d->symver && d->literal)
7047
0
      {
7048
0
        const char *verstr, *name;
7049
0
        size_t namelen, verlen, newlen;
7050
0
        char *newname, *p, leading_char;
7051
0
        struct elf_link_hash_entry *newh;
7052
7053
0
        leading_char = bfd_get_symbol_leading_char (info->output_bfd);
7054
0
        name = d->pattern;
7055
0
        namelen = strlen (name) + (leading_char != '\0');
7056
0
        verstr = t->name;
7057
0
        verlen = strlen (verstr);
7058
0
        newlen = namelen + verlen + 3;
7059
7060
0
        newname = bfd_malloc (newlen);
7061
0
        if (newname == NULL)
7062
0
    return false;
7063
0
        newname[0] = leading_char;
7064
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7065
7066
        /* Check the hidden versioned definition.  */
7067
0
        p = newname + namelen;
7068
0
        *p++ = ELF_VER_CHR;
7069
0
        memcpy (p, verstr, verlen + 1);
7070
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7071
0
             newname, false, false,
7072
0
             false);
7073
0
        if (newh == NULL
7074
0
      || (newh->root.type != bfd_link_hash_defined
7075
0
          && newh->root.type != bfd_link_hash_defweak))
7076
0
    {
7077
      /* Check the default versioned definition.  */
7078
0
      *p++ = ELF_VER_CHR;
7079
0
      memcpy (p, verstr, verlen + 1);
7080
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7081
0
                 newname, false, false,
7082
0
                 false);
7083
0
    }
7084
0
        free (newname);
7085
7086
        /* Mark this version if there is a definition and it is
7087
     not defined in a shared object.  */
7088
0
        if (newh != NULL
7089
0
      && !newh->def_dynamic
7090
0
      && (newh->root.type == bfd_link_hash_defined
7091
0
          || newh->root.type == bfd_link_hash_defweak))
7092
0
    d->symver = 1;
7093
0
      }
7094
7095
      /* Attach all the symbols to their version information.  */
7096
0
      asvinfo.info = info;
7097
0
      asvinfo.failed = false;
7098
7099
0
      elf_link_hash_traverse (elf_hash_table (info),
7100
0
            _bfd_elf_link_assign_sym_version,
7101
0
            &asvinfo);
7102
0
      if (asvinfo.failed)
7103
0
  return false;
7104
7105
0
      if (!info->allow_undefined_version)
7106
0
  {
7107
    /* Check if all global versions have a definition.  */
7108
0
    bool all_defined = true;
7109
0
    for (t = info->version_info; t != NULL; t = t->next)
7110
0
      for (d = t->globals.list; d != NULL; d = d->next)
7111
0
        if (d->literal && !d->symver && !d->script)
7112
0
    {
7113
0
      _bfd_error_handler
7114
0
        (_("%s: undefined version: %s"),
7115
0
         d->pattern, t->name);
7116
0
      all_defined = false;
7117
0
    }
7118
7119
0
    if (!all_defined)
7120
0
      {
7121
0
        bfd_set_error (bfd_error_bad_value);
7122
0
        return false;
7123
0
      }
7124
0
  }
7125
7126
      /* Set up the version definition section.  */
7127
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7128
0
      BFD_ASSERT (s != NULL);
7129
7130
      /* We may have created additional version definitions if we are
7131
   just linking a regular application.  */
7132
0
      verdefs = info->version_info;
7133
7134
      /* Skip anonymous version tag.  */
7135
0
      if (verdefs != NULL && verdefs->vernum == 0)
7136
0
  verdefs = verdefs->next;
7137
7138
0
      if (verdefs == NULL && !info->create_default_symver)
7139
0
  s->flags |= SEC_EXCLUDE;
7140
0
      else
7141
0
  {
7142
0
    unsigned int cdefs;
7143
0
    bfd_size_type size;
7144
0
    bfd_byte *p;
7145
0
    Elf_Internal_Verdef def;
7146
0
    Elf_Internal_Verdaux defaux;
7147
0
    struct bfd_link_hash_entry *bh;
7148
0
    struct elf_link_hash_entry *h;
7149
0
    const char *name;
7150
7151
0
    cdefs = 0;
7152
0
    size = 0;
7153
7154
    /* Make space for the base version.  */
7155
0
    size += sizeof (Elf_External_Verdef);
7156
0
    size += sizeof (Elf_External_Verdaux);
7157
0
    ++cdefs;
7158
7159
    /* Make space for the default version.  */
7160
0
    if (info->create_default_symver)
7161
0
      {
7162
0
        size += sizeof (Elf_External_Verdef);
7163
0
        ++cdefs;
7164
0
      }
7165
7166
0
    for (t = verdefs; t != NULL; t = t->next)
7167
0
      {
7168
0
        struct bfd_elf_version_deps *n;
7169
7170
        /* Don't emit base version twice.  */
7171
0
        if (t->vernum == 0)
7172
0
    continue;
7173
7174
0
        size += sizeof (Elf_External_Verdef);
7175
0
        size += sizeof (Elf_External_Verdaux);
7176
0
        ++cdefs;
7177
7178
0
        for (n = t->deps; n != NULL; n = n->next)
7179
0
    size += sizeof (Elf_External_Verdaux);
7180
0
      }
7181
7182
0
    s->size = size;
7183
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7184
0
    if (s->contents == NULL && s->size != 0)
7185
0
      return false;
7186
0
    s->alloced = 1;
7187
7188
    /* Fill in the version definition section.  */
7189
7190
0
    p = s->contents;
7191
7192
0
    def.vd_version = VER_DEF_CURRENT;
7193
0
    def.vd_flags = VER_FLG_BASE;
7194
0
    def.vd_ndx = 1;
7195
0
    def.vd_cnt = 1;
7196
0
    if (info->create_default_symver)
7197
0
      {
7198
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7199
0
        def.vd_next = sizeof (Elf_External_Verdef);
7200
0
      }
7201
0
    else
7202
0
      {
7203
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7204
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7205
0
           + sizeof (Elf_External_Verdaux));
7206
0
      }
7207
7208
0
    if (soname_indx != (size_t) -1)
7209
0
      {
7210
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7211
0
              soname_indx);
7212
0
        def.vd_hash = bfd_elf_hash (soname);
7213
0
        defaux.vda_name = soname_indx;
7214
0
        name = soname;
7215
0
      }
7216
0
    else
7217
0
      {
7218
0
        size_t indx;
7219
7220
0
        name = lbasename (bfd_get_filename (info->output_bfd));
7221
0
        def.vd_hash = bfd_elf_hash (name);
7222
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7223
0
            name, false);
7224
0
        if (indx == (size_t) -1)
7225
0
    return false;
7226
0
        defaux.vda_name = indx;
7227
0
      }
7228
0
    defaux.vda_next = 0;
7229
7230
0
    _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7231
0
            (Elf_External_Verdef *) p);
7232
0
    p += sizeof (Elf_External_Verdef);
7233
0
    if (info->create_default_symver)
7234
0
      {
7235
        /* Add a symbol representing this version.  */
7236
0
        bh = NULL;
7237
0
        if (! (_bfd_generic_link_add_one_symbol
7238
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7239
0
          0, NULL, false,
7240
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7241
0
    return false;
7242
0
        h = (struct elf_link_hash_entry *) bh;
7243
0
        h->non_elf = 0;
7244
0
        h->def_regular = 1;
7245
0
        h->type = STT_OBJECT;
7246
0
        h->verinfo.vertree = NULL;
7247
7248
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7249
0
    return false;
7250
7251
        /* Create a duplicate of the base version with the same
7252
     aux block, but different flags.  */
7253
0
        def.vd_flags = 0;
7254
0
        def.vd_ndx = 2;
7255
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7256
0
        if (verdefs)
7257
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7258
0
             + sizeof (Elf_External_Verdaux));
7259
0
        else
7260
0
    def.vd_next = 0;
7261
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7262
0
          (Elf_External_Verdef *) p);
7263
0
        p += sizeof (Elf_External_Verdef);
7264
0
      }
7265
0
    _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7266
0
             (Elf_External_Verdaux *) p);
7267
0
    p += sizeof (Elf_External_Verdaux);
7268
7269
0
    for (t = verdefs; t != NULL; t = t->next)
7270
0
      {
7271
0
        unsigned int cdeps;
7272
0
        struct bfd_elf_version_deps *n;
7273
7274
        /* Don't emit the base version twice.  */
7275
0
        if (t->vernum == 0)
7276
0
    continue;
7277
7278
0
        cdeps = 0;
7279
0
        for (n = t->deps; n != NULL; n = n->next)
7280
0
    ++cdeps;
7281
7282
        /* Add a symbol representing this version.  */
7283
0
        bh = NULL;
7284
0
        if (! (_bfd_generic_link_add_one_symbol
7285
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7286
0
          0, NULL, false,
7287
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7288
0
    return false;
7289
0
        h = (struct elf_link_hash_entry *) bh;
7290
0
        h->non_elf = 0;
7291
0
        h->def_regular = 1;
7292
0
        h->type = STT_OBJECT;
7293
0
        h->verinfo.vertree = t;
7294
7295
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7296
0
    return false;
7297
7298
0
        def.vd_version = VER_DEF_CURRENT;
7299
0
        def.vd_flags = 0;
7300
0
        if (t->globals.list == NULL
7301
0
      && t->locals.list == NULL
7302
0
      && ! t->used)
7303
0
    def.vd_flags |= VER_FLG_WEAK;
7304
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7305
0
        def.vd_cnt = cdeps + 1;
7306
0
        def.vd_hash = bfd_elf_hash (t->name);
7307
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7308
0
        def.vd_next = 0;
7309
7310
        /* If a basever node is next, it *must* be the last node in
7311
     the chain, otherwise Verdef construction breaks.  */
7312
0
        if (t->next != NULL && t->next->vernum == 0)
7313
0
    BFD_ASSERT (t->next->next == NULL);
7314
7315
0
        if (t->next != NULL && t->next->vernum != 0)
7316
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7317
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7318
7319
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7320
0
          (Elf_External_Verdef *) p);
7321
0
        p += sizeof (Elf_External_Verdef);
7322
7323
0
        defaux.vda_name = h->dynstr_index;
7324
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7325
0
              h->dynstr_index);
7326
0
        defaux.vda_next = 0;
7327
0
        if (t->deps != NULL)
7328
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7329
0
        t->name_indx = defaux.vda_name;
7330
7331
0
        _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7332
0
           (Elf_External_Verdaux *) p);
7333
0
        p += sizeof (Elf_External_Verdaux);
7334
7335
0
        for (n = t->deps; n != NULL; n = n->next)
7336
0
    {
7337
0
      if (n->version_needed == NULL)
7338
0
        {
7339
          /* This can happen if there was an error in the
7340
       version script.  */
7341
0
          defaux.vda_name = 0;
7342
0
        }
7343
0
      else
7344
0
        {
7345
0
          defaux.vda_name = n->version_needed->name_indx;
7346
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7347
0
                defaux.vda_name);
7348
0
        }
7349
0
      if (n->next == NULL)
7350
0
        defaux.vda_next = 0;
7351
0
      else
7352
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7353
7354
0
      _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7355
0
               (Elf_External_Verdaux *) p);
7356
0
      p += sizeof (Elf_External_Verdaux);
7357
0
    }
7358
0
      }
7359
7360
0
    elf_tdata (info->output_bfd)->cverdefs = cdefs;
7361
0
  }
7362
0
    }
7363
7364
0
  if (info->gc_sections && obed->can_gc_sections)
7365
0
    {
7366
0
      struct elf_gc_sweep_symbol_info sweep_info;
7367
7368
      /* Remove the symbols that were in the swept sections from the
7369
   dynamic symbol table.  */
7370
0
      sweep_info.info = info;
7371
0
      sweep_info.hide_symbol = obed->elf_backend_hide_symbol;
7372
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7373
0
            &sweep_info);
7374
0
    }
7375
7376
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7377
0
    {
7378
0
      asection *s;
7379
0
      struct elf_find_verdep_info sinfo;
7380
7381
      /* Work out the size of the version reference section.  */
7382
7383
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7384
0
      BFD_ASSERT (s != NULL);
7385
7386
0
      sinfo.info = info;
7387
0
      sinfo.vers = elf_tdata (info->output_bfd)->cverdefs;
7388
0
      if (sinfo.vers == 0)
7389
0
  sinfo.vers = 1;
7390
0
      sinfo.failed = false;
7391
7392
0
      elf_link_hash_traverse (elf_hash_table (info),
7393
0
            _bfd_elf_link_find_version_dependencies,
7394
0
            &sinfo);
7395
0
      if (sinfo.failed)
7396
0
  return false;
7397
7398
0
      obed->elf_backend_add_glibc_version_dependency (&sinfo);
7399
0
      if (sinfo.failed)
7400
0
  return false;
7401
7402
0
      if (elf_tdata (info->output_bfd)->verref == NULL)
7403
0
  s->flags |= SEC_EXCLUDE;
7404
0
      else
7405
0
  {
7406
0
    Elf_Internal_Verneed *vn;
7407
0
    unsigned int size;
7408
0
    unsigned int crefs;
7409
0
    bfd_byte *p;
7410
7411
    /* Build the version dependency section.  */
7412
0
    size = 0;
7413
0
    crefs = 0;
7414
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7415
0
         vn != NULL;
7416
0
         vn = vn->vn_nextref)
7417
0
      {
7418
0
        Elf_Internal_Vernaux *a;
7419
7420
0
        size += sizeof (Elf_External_Verneed);
7421
0
        ++crefs;
7422
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7423
0
    size += sizeof (Elf_External_Vernaux);
7424
0
      }
7425
7426
0
    s->size = size;
7427
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7428
0
    if (s->contents == NULL)
7429
0
      return false;
7430
0
    s->alloced = 1;
7431
7432
0
    p = s->contents;
7433
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7434
0
         vn != NULL;
7435
0
         vn = vn->vn_nextref)
7436
0
      {
7437
0
        unsigned int caux;
7438
0
        Elf_Internal_Vernaux *a;
7439
0
        size_t indx;
7440
7441
0
        caux = 0;
7442
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7443
0
    ++caux;
7444
7445
0
        vn->vn_version = VER_NEED_CURRENT;
7446
0
        vn->vn_cnt = caux;
7447
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7448
0
            elf_dt_name (vn->vn_bfd) != NULL
7449
0
            ? elf_dt_name (vn->vn_bfd)
7450
0
            : lbasename (bfd_get_filename
7451
0
                   (vn->vn_bfd)),
7452
0
            false);
7453
0
        if (indx == (size_t) -1)
7454
0
    return false;
7455
0
        vn->vn_file = indx;
7456
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7457
0
        if (vn->vn_nextref == NULL)
7458
0
    vn->vn_next = 0;
7459
0
        else
7460
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7461
0
             + caux * sizeof (Elf_External_Vernaux));
7462
7463
0
        _bfd_elf_swap_verneed_out (info->output_bfd, vn,
7464
0
           (Elf_External_Verneed *) p);
7465
0
        p += sizeof (Elf_External_Verneed);
7466
7467
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7468
0
    {
7469
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7470
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7471
0
                a->vna_nodename, false);
7472
0
      if (indx == (size_t) -1)
7473
0
        return false;
7474
0
      a->vna_name = indx;
7475
0
      if (a->vna_nextptr == NULL)
7476
0
        a->vna_next = 0;
7477
0
      else
7478
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7479
7480
0
      _bfd_elf_swap_vernaux_out (info->output_bfd, a,
7481
0
               (Elf_External_Vernaux *) p);
7482
0
      p += sizeof (Elf_External_Vernaux);
7483
0
    }
7484
0
      }
7485
7486
0
    elf_tdata (info->output_bfd)->cverrefs = crefs;
7487
0
  }
7488
0
    }
7489
7490
0
  if (bfd_link_relocatable (info)
7491
0
      && !bfd_elf_size_group_sections (info))
7492
0
    return false;
7493
7494
  /* Determine any GNU_STACK segment requirements, after the backend
7495
     has had a chance to set a default segment size.  */
7496
0
  if (info->execstack)
7497
0
    {
7498
      /* If the user has explicitly requested warnings, then generate one even
7499
   though the choice is the result of another command line option.  */
7500
0
      if (info->warn_execstack == 1)
7501
0
  {
7502
0
    if (info->error_execstack)
7503
0
      {
7504
0
        _bfd_error_handler
7505
0
    (_("\
7506
0
error: creating an executable stack because of -z execstack command line option"));
7507
0
        return false;
7508
0
      }
7509
7510
0
    _bfd_error_handler
7511
0
      (_("\
7512
0
warning: enabling an executable stack because of -z execstack command line option"));
7513
0
  }
7514
7515
0
      elf_stack_flags (info->output_bfd) = PF_R | PF_W | PF_X;
7516
0
    }
7517
0
  else if (info->noexecstack)
7518
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W;
7519
0
  else
7520
0
    {
7521
0
      bfd *inputobj;
7522
0
      asection *notesec = NULL;
7523
0
      bfd *noteobj = NULL;
7524
0
      bfd *emptyobj = NULL;
7525
0
      int exec = 0;
7526
7527
0
      for (inputobj = info->input_bfds;
7528
0
     inputobj;
7529
0
     inputobj = inputobj->link.next)
7530
0
  {
7531
0
    asection *s;
7532
7533
0
    if (inputobj->flags
7534
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7535
0
      continue;
7536
0
    s = inputobj->sections;
7537
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7538
0
      continue;
7539
7540
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7541
0
    if (s)
7542
0
      {
7543
0
        notesec = s;
7544
0
        if (s->flags & SEC_CODE)
7545
0
    {
7546
0
      noteobj = inputobj;
7547
0
      exec = PF_X;
7548
      /* There is no point in scanning the remaining bfds.  */
7549
0
      break;
7550
0
    }
7551
0
      }
7552
0
    else if (obed->default_execstack && info->default_execstack)
7553
0
      {
7554
0
        exec = PF_X;
7555
0
        emptyobj = inputobj;
7556
0
      }
7557
0
  }
7558
7559
0
      if (notesec || info->stacksize > 0)
7560
0
  {
7561
0
    if (exec)
7562
0
      {
7563
0
        if (info->warn_execstack != 0)
7564
0
    {
7565
      /* PR 29072: Because an executable stack is a serious
7566
         security risk, make sure that the user knows that it is
7567
         being enabled despite the fact that it was not requested
7568
         on the command line.  */
7569
0
      if (noteobj)
7570
0
        {
7571
0
          if (info->error_execstack)
7572
0
      {
7573
0
        _bfd_error_handler (_("\
7574
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7575
0
                bfd_get_filename (noteobj));
7576
0
        return false;
7577
0
      }
7578
7579
0
          _bfd_error_handler (_("\
7580
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7581
0
           bfd_get_filename (noteobj));
7582
0
        }
7583
0
      else if (emptyobj)
7584
0
        {
7585
0
          if (info->error_execstack)
7586
0
      {
7587
0
        _bfd_error_handler (_("\
7588
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7589
0
                bfd_get_filename (emptyobj));
7590
0
        return false;
7591
0
      }
7592
7593
0
          _bfd_error_handler (_("\
7594
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7595
0
            bfd_get_filename (emptyobj));
7596
0
          _bfd_error_handler (_("\
7597
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7598
0
        }
7599
0
    }
7600
0
      }
7601
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W | exec;
7602
0
  }
7603
7604
0
      if (notesec && exec && bfd_link_relocatable (info)
7605
0
    && notesec->output_section != bfd_abs_section_ptr)
7606
0
  notesec->output_section->flags |= SEC_CODE;
7607
0
    }
7608
7609
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7610
0
    {
7611
0
      struct elf_info_failed eif;
7612
0
      struct elf_link_hash_entry *h;
7613
0
      asection *dynstr;
7614
0
      asection *s;
7615
7616
0
      *sinterpptr = elf_hash_table (info)->interp;
7617
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7618
7619
0
      if (info->symbolic)
7620
0
  {
7621
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7622
0
      return false;
7623
0
    info->flags |= DF_SYMBOLIC;
7624
0
  }
7625
7626
0
      if (rpath != NULL)
7627
0
  {
7628
0
    size_t indx;
7629
0
    bfd_vma tag;
7630
7631
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7632
0
              true);
7633
0
    if (indx == (size_t) -1)
7634
0
      return false;
7635
7636
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7637
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7638
0
      return false;
7639
0
  }
7640
7641
0
      if (filter_shlib != NULL)
7642
0
  {
7643
0
    size_t indx;
7644
7645
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7646
0
              filter_shlib, true);
7647
0
    if (indx == (size_t) -1
7648
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7649
0
      return false;
7650
0
  }
7651
7652
0
      if (auxiliary_filters != NULL)
7653
0
  {
7654
0
    const char * const *p;
7655
7656
0
    for (p = auxiliary_filters; *p != NULL; p++)
7657
0
      {
7658
0
        size_t indx;
7659
7660
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7661
0
            *p, true);
7662
0
        if (indx == (size_t) -1
7663
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7664
0
    return false;
7665
0
      }
7666
0
  }
7667
7668
0
      if (audit != NULL)
7669
0
  {
7670
0
    size_t indx;
7671
7672
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7673
0
              true);
7674
0
    if (indx == (size_t) -1
7675
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7676
0
      return false;
7677
0
  }
7678
7679
0
      if (depaudit != NULL)
7680
0
  {
7681
0
    size_t indx;
7682
7683
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7684
0
              true);
7685
0
    if (indx == (size_t) -1
7686
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7687
0
      return false;
7688
0
  }
7689
7690
0
      eif.info = info;
7691
0
      eif.failed = false;
7692
7693
      /* Find all symbols which were defined in a dynamic object and make
7694
   the backend pick a reasonable value for them.  */
7695
0
      elf_link_hash_traverse (elf_hash_table (info),
7696
0
            _bfd_elf_adjust_dynamic_symbol,
7697
0
            &eif);
7698
0
      if (eif.failed)
7699
0
  return false;
7700
7701
      /* Add some entries to the .dynamic section.  We fill in some of the
7702
   values later, in bfd_elf_final_link, but we must add the entries
7703
   now so that we know the final size of the .dynamic section.  */
7704
7705
      /* If there are initialization and/or finalization functions to
7706
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7707
0
      h = (info->init_function
7708
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7709
0
           info->init_function, false,
7710
0
           false, false)
7711
0
     : NULL);
7712
0
      if (h != NULL
7713
0
    && (h->ref_regular
7714
0
        || h->def_regular))
7715
0
  {
7716
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7717
0
      return false;
7718
0
    obed->elf_backend_hide_symbol (info, h, true);
7719
0
  }
7720
0
      h = (info->fini_function
7721
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7722
0
           info->fini_function, false,
7723
0
           false, false)
7724
0
     : NULL);
7725
0
      if (h != NULL
7726
0
    && (h->ref_regular
7727
0
        || h->def_regular))
7728
0
  {
7729
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7730
0
      return false;
7731
0
    obed->elf_backend_hide_symbol (info, h, true);
7732
0
  }
7733
7734
0
      s = bfd_get_section_by_name (info->output_bfd, ".preinit_array");
7735
0
      if (s != NULL && s->linker_has_input)
7736
0
  {
7737
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7738
0
    if (! bfd_link_executable (info))
7739
0
      {
7740
0
        bfd *sub;
7741
0
        asection *o;
7742
7743
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7744
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7745
0
        && (o = sub->sections) != NULL
7746
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7747
0
      for (o = sub->sections; o != NULL; o = o->next)
7748
0
        if (elf_section_data (o)->this_hdr.sh_type
7749
0
      == SHT_PREINIT_ARRAY)
7750
0
          {
7751
0
      _bfd_error_handler
7752
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7753
0
         sub);
7754
0
      break;
7755
0
          }
7756
7757
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7758
0
        return false;
7759
0
      }
7760
7761
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7762
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7763
0
      return false;
7764
0
  }
7765
0
      s = bfd_get_section_by_name (info->output_bfd, ".init_array");
7766
0
      if (s != NULL && s->linker_has_input)
7767
0
  {
7768
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7769
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7770
0
      return false;
7771
0
  }
7772
0
      s = bfd_get_section_by_name (info->output_bfd, ".fini_array");
7773
0
      if (s != NULL && s->linker_has_input)
7774
0
  {
7775
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7776
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7777
0
      return false;
7778
0
  }
7779
7780
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7781
      /* If .dynstr is excluded from the link, we don't want any of
7782
   these tags.  Strictly, we should be checking each section
7783
   individually;  This quick check covers for the case where
7784
   someone does a /DISCARD/ : { *(*) }.  */
7785
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7786
0
  {
7787
0
    bfd_size_type strsize;
7788
7789
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7790
0
    if ((info->emit_hash
7791
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7792
0
        || (info->emit_gnu_hash
7793
0
      && (obed->record_xhash_symbol == NULL
7794
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7795
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7796
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7797
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7798
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7799
0
                obed->s->sizeof_sym)
7800
0
        || (info->gnu_flags_1
7801
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7802
0
              info->gnu_flags_1)))
7803
0
      return false;
7804
0
  }
7805
0
    }
7806
7807
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7808
0
    return false;
7809
7810
  /* The backend must work out the sizes of all the other dynamic
7811
     sections.  */
7812
0
  if (obed->elf_backend_late_size_sections != NULL
7813
0
      && !obed->elf_backend_late_size_sections (info))
7814
0
    return false;
7815
7816
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7817
0
    {
7818
0
      if (elf_tdata (info->output_bfd)->cverdefs)
7819
0
  {
7820
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverdefs;
7821
7822
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7823
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7824
0
      return false;
7825
0
  }
7826
7827
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7828
0
  {
7829
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7830
0
      return false;
7831
0
  }
7832
0
      else if (info->flags & DF_BIND_NOW)
7833
0
  {
7834
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7835
0
      return false;
7836
0
  }
7837
7838
0
      if (info->flags_1)
7839
0
  {
7840
0
    if (bfd_link_executable (info))
7841
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7842
0
        | DF_1_NODELETE
7843
0
        | DF_1_NOOPEN);
7844
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7845
0
      return false;
7846
0
  }
7847
7848
0
      if (elf_tdata (info->output_bfd)->cverrefs)
7849
0
  {
7850
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverrefs;
7851
7852
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7853
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7854
0
      return false;
7855
0
  }
7856
7857
0
      if ((elf_tdata (info->output_bfd)->cverrefs == 0
7858
0
     && elf_tdata (info->output_bfd)->cverdefs == 0)
7859
0
    || _bfd_elf_link_renumber_dynsyms (info, NULL) <= 1)
7860
0
  {
7861
0
    asection *s;
7862
7863
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7864
0
    s->flags |= SEC_EXCLUDE;
7865
0
  }
7866
0
    }
7867
0
  return true;
7868
0
}
7869
7870
/* Find the first non-excluded output section.  We'll use its
7871
   section symbol for some emitted relocs.  */
7872
void
7873
_bfd_elf_init_1_index_section (struct bfd_link_info *info)
7874
0
{
7875
0
  asection *s;
7876
0
  asection *found = NULL;
7877
7878
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7879
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7880
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7881
0
      {
7882
0
  found = s;
7883
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7884
0
    break;
7885
0
      }
7886
0
  elf_hash_table (info)->text_index_section = found;
7887
0
}
7888
7889
/* Find two non-excluded output sections, one for code, one for data.
7890
   We'll use their section symbols for some emitted relocs.  */
7891
void
7892
_bfd_elf_init_2_index_sections (struct bfd_link_info *info)
7893
0
{
7894
0
  asection *s;
7895
0
  asection *found = NULL;
7896
7897
  /* Data first, since setting text_index_section changes
7898
     _bfd_elf_omit_section_dynsym_default.  */
7899
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7900
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7901
0
  && !(s->flags & SEC_READONLY)
7902
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7903
0
      {
7904
0
  found = s;
7905
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7906
0
    break;
7907
0
      }
7908
0
  elf_hash_table (info)->data_index_section = found;
7909
7910
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7911
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7912
0
  && (s->flags & SEC_READONLY)
7913
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7914
0
      {
7915
0
  found = s;
7916
0
  break;
7917
0
      }
7918
0
  elf_hash_table (info)->text_index_section = found;
7919
0
}
7920
7921
void
7922
_bfd_elf_init_0_index_sections (struct bfd_link_info *info ATTRIBUTE_UNUSED)
7923
0
{
7924
0
}
7925
7926
#define GNU_HASH_SECTION_NAME(bed)          \
7927
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7928
7929
bool
7930
bfd_elf_size_dynsym_hash_dynstr (struct bfd_link_info *info)
7931
0
{
7932
0
  elf_backend_data *obed;
7933
0
  unsigned long section_sym_count;
7934
0
  bfd_size_type dynsymcount = 0;
7935
7936
0
  if (!is_elf_hash_table (info->hash))
7937
0
    return true;
7938
7939
0
  obed = get_elf_backend_data (info->output_bfd);
7940
0
  obed->elf_backend_init_index_section (info);
7941
7942
  /* Assign dynsym indices.  In a shared library we generate a section
7943
     symbol for each output section, which come first.  Next come all
7944
     of the back-end allocated local dynamic syms, followed by the rest
7945
     of the global symbols.
7946
7947
     This is usually not needed for static binaries, however backends
7948
     can request to always do it, e.g. the MIPS backend uses dynamic
7949
     symbol counts to lay out GOT, which will be produced in the
7950
     presence of GOT relocations even in static binaries (holding fixed
7951
     data in that case, to satisfy those relocations).  */
7952
7953
0
  if (elf_hash_table (info)->dynamic_sections_created
7954
0
      || obed->always_renumber_dynsyms)
7955
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (info, &section_sym_count);
7956
7957
0
  if (elf_hash_table (info)->dynamic_sections_created)
7958
0
    {
7959
0
      bfd *dynobj;
7960
0
      asection *s;
7961
0
      unsigned int dtagcount;
7962
7963
0
      dynobj = elf_hash_table (info)->dynobj;
7964
7965
      /* Work out the size of the symbol version section.  */
7966
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7967
0
      BFD_ASSERT (s != NULL);
7968
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7969
0
  {
7970
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7971
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
7972
0
    if (s->contents == NULL)
7973
0
      return false;
7974
0
    s->alloced = 1;
7975
7976
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7977
0
      return false;
7978
0
  }
7979
7980
      /* Set the size of the .dynsym and .hash sections.  We counted
7981
   the number of dynamic symbols in elf_link_add_object_symbols.
7982
   We will build the contents of .dynsym and .hash when we build
7983
   the final symbol table, because until then we do not know the
7984
   correct value to give the symbols.  We built the .dynstr
7985
   section as we went along in elf_link_add_object_symbols.  */
7986
0
      s = elf_hash_table (info)->dynsym;
7987
0
      BFD_ASSERT (s != NULL);
7988
0
      s->size = dynsymcount * obed->s->sizeof_sym;
7989
7990
0
      s->contents = bfd_alloc (info->output_bfd, s->size);
7991
0
      if (s->contents == NULL)
7992
0
  return false;
7993
0
      s->alloced = 1;
7994
7995
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7996
   section syms, in case we don't output them all.  */
7997
0
      ++section_sym_count;
7998
0
      memset (s->contents, 0, section_sym_count * obed->s->sizeof_sym);
7999
8000
0
      elf_hash_table (info)->bucketcount = 0;
8001
8002
      /* Compute the size of the hashing table.  As a side effect this
8003
   computes the hash values for all the names we export.  */
8004
0
      if (info->emit_hash)
8005
0
  {
8006
0
    unsigned long int *hashcodes;
8007
0
    struct hash_codes_info hashinf;
8008
0
    unsigned long int nsyms;
8009
0
    size_t bucketcount;
8010
0
    size_t hash_entry_size;
8011
8012
    /* Compute the hash values for all exported symbols.  At the same
8013
       time store the values in an array so that we could use them for
8014
       optimizations.  */
8015
0
    hashcodes = bfd_malloc (dynsymcount * sizeof (*hashcodes));
8016
0
    if (hashcodes == NULL)
8017
0
      return false;
8018
0
    hashinf.hashcodes = hashcodes;
8019
0
    hashinf.error = false;
8020
8021
    /* Put all hash values in HASHCODES.  */
8022
0
    elf_link_hash_traverse (elf_hash_table (info),
8023
0
          elf_collect_hash_codes, &hashinf);
8024
0
    if (hashinf.error)
8025
0
      {
8026
0
        free (hashcodes);
8027
0
        return false;
8028
0
      }
8029
8030
0
    nsyms = hashinf.hashcodes - hashcodes;
8031
0
    bucketcount
8032
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
8033
0
    free (hashcodes);
8034
8035
0
    if (bucketcount == 0 && nsyms > 0)
8036
0
      return false;
8037
8038
0
    elf_hash_table (info)->bucketcount = bucketcount;
8039
8040
0
    s = bfd_get_linker_section (dynobj, ".hash");
8041
0
    BFD_ASSERT (s != NULL);
8042
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
8043
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
8044
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
8045
0
    if (s->contents == NULL)
8046
0
      return false;
8047
0
    s->alloced = 1;
8048
8049
0
    bfd_put (8 * hash_entry_size, info->output_bfd, bucketcount,
8050
0
       s->contents);
8051
0
    bfd_put (8 * hash_entry_size, info->output_bfd, dynsymcount,
8052
0
       s->contents + hash_entry_size);
8053
0
  }
8054
8055
0
      if (info->emit_gnu_hash)
8056
0
  {
8057
0
    size_t i, cnt;
8058
0
    unsigned char *contents;
8059
0
    struct collect_gnu_hash_codes cinfo;
8060
0
    bfd_size_type amt;
8061
0
    size_t bucketcount;
8062
8063
0
    memset (&cinfo, 0, sizeof (cinfo));
8064
8065
    /* Compute the hash values for all exported symbols.  At the same
8066
       time store the values in an array so that we could use them for
8067
       optimizations.  */
8068
0
    amt = dynsymcount * 2 * sizeof (*cinfo.hashcodes);
8069
0
    cinfo.hashcodes = bfd_malloc (amt);
8070
0
    if (cinfo.hashcodes == NULL)
8071
0
      return false;
8072
8073
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8074
0
    cinfo.min_dynindx = -1;
8075
0
    cinfo.output_bfd = info->output_bfd;
8076
0
    cinfo.bed = obed;
8077
8078
    /* Put all hash values in HASHCODES.  */
8079
0
    elf_link_hash_traverse (elf_hash_table (info),
8080
0
          elf_collect_gnu_hash_codes, &cinfo);
8081
0
    if (cinfo.error)
8082
0
      {
8083
0
        free (cinfo.hashcodes);
8084
0
        return false;
8085
0
      }
8086
8087
0
    bucketcount
8088
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8089
8090
0
    if (bucketcount == 0)
8091
0
      {
8092
0
        free (cinfo.hashcodes);
8093
0
        return false;
8094
0
      }
8095
8096
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (obed));
8097
0
    BFD_ASSERT (s != NULL);
8098
8099
0
    if (cinfo.nsyms == 0)
8100
0
      {
8101
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8102
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8103
0
        free (cinfo.hashcodes);
8104
0
        s->size = 5 * 4 + obed->s->arch_size / 8;
8105
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8106
0
        if (contents == NULL)
8107
0
    return false;
8108
0
        s->contents = contents;
8109
0
        s->alloced = 1;
8110
        /* 1 empty bucket.  */
8111
0
        bfd_put_32 (info->output_bfd, 1, contents);
8112
        /* SYMIDX above the special symbol 0.  */
8113
0
        bfd_put_32 (info->output_bfd, 1, contents + 4);
8114
        /* Just one word for bitmask.  */
8115
0
        bfd_put_32 (info->output_bfd, 1, contents + 8);
8116
        /* Only hash fn bloom filter.  */
8117
0
        bfd_put_32 (info->output_bfd, 0, contents + 12);
8118
        /* No hashes are valid - empty bitmask.  */
8119
0
        bfd_put (obed->s->arch_size, info->output_bfd, 0, contents + 16);
8120
        /* No hashes in the only bucket.  */
8121
0
        bfd_put_32 (info->output_bfd, 0,
8122
0
        contents + 16 + obed->s->arch_size / 8);
8123
0
      }
8124
0
    else
8125
0
      {
8126
0
        unsigned long int maskwords, maskbitslog2, x;
8127
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8128
8129
0
        x = cinfo.nsyms;
8130
0
        maskbitslog2 = 1;
8131
0
        while ((x >>= 1) != 0)
8132
0
    ++maskbitslog2;
8133
0
        if (maskbitslog2 < 3)
8134
0
    maskbitslog2 = 5;
8135
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8136
0
    maskbitslog2 = maskbitslog2 + 3;
8137
0
        else
8138
0
    maskbitslog2 = maskbitslog2 + 2;
8139
0
        if (obed->s->arch_size == 64)
8140
0
    {
8141
0
      if (maskbitslog2 == 5)
8142
0
        maskbitslog2 = 6;
8143
0
      cinfo.shift1 = 6;
8144
0
    }
8145
0
        else
8146
0
    cinfo.shift1 = 5;
8147
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8148
0
        cinfo.shift2 = maskbitslog2;
8149
0
        cinfo.maskbits = 1 << maskbitslog2;
8150
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8151
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8152
0
        amt += maskwords * sizeof (bfd_vma);
8153
0
        cinfo.bitmask = bfd_malloc (amt);
8154
0
        if (cinfo.bitmask == NULL)
8155
0
    {
8156
0
      free (cinfo.hashcodes);
8157
0
      return false;
8158
0
    }
8159
8160
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8161
0
        cinfo.indx = cinfo.counts + bucketcount;
8162
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8163
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8164
8165
        /* Determine how often each hash bucket is used.  */
8166
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8167
0
        for (i = 0; i < cinfo.nsyms; ++i)
8168
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8169
8170
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8171
0
    if (cinfo.counts[i] != 0)
8172
0
      {
8173
0
        cinfo.indx[i] = cnt;
8174
0
        cnt += cinfo.counts[i];
8175
0
      }
8176
0
        BFD_ASSERT (cnt == dynsymcount);
8177
0
        cinfo.bucketcount = bucketcount;
8178
0
        cinfo.local_indx = cinfo.min_dynindx;
8179
8180
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8181
0
        s->size += cinfo.maskbits / 8;
8182
0
        if (obed->record_xhash_symbol != NULL)
8183
0
    s->size += cinfo.nsyms * 4;
8184
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8185
0
        if (contents == NULL)
8186
0
    {
8187
0
      free (cinfo.bitmask);
8188
0
      free (cinfo.hashcodes);
8189
0
      return false;
8190
0
    }
8191
8192
0
        s->contents = contents;
8193
0
        s->alloced = 1;
8194
0
        bfd_put_32 (info->output_bfd, bucketcount, contents);
8195
0
        bfd_put_32 (info->output_bfd, cinfo.symindx, contents + 4);
8196
0
        bfd_put_32 (info->output_bfd, maskwords, contents + 8);
8197
0
        bfd_put_32 (info->output_bfd, cinfo.shift2, contents + 12);
8198
0
        contents += 16 + cinfo.maskbits / 8;
8199
8200
0
        for (i = 0; i < bucketcount; ++i)
8201
0
    {
8202
0
      if (cinfo.counts[i] == 0)
8203
0
        bfd_put_32 (info->output_bfd, 0, contents);
8204
0
      else
8205
0
        bfd_put_32 (info->output_bfd, cinfo.indx[i], contents);
8206
0
      contents += 4;
8207
0
    }
8208
8209
0
        cinfo.contents = contents;
8210
8211
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8212
8213
0
        if (elf_hash_table (info)->has_base_symbols)
8214
0
    {
8215
      /* Output base symbols first in DT_GNU_HASH so that
8216
         they will be picked before non-base symbols at
8217
         run-time.  */
8218
0
      cinfo.base_symbol = true;
8219
8220
      /* Renumber dynamic symbols, if populating .gnu.hash
8221
         section.  If using .MIPS.xhash, populate the
8222
         translation table.  */
8223
0
      elf_link_hash_traverse (elf_hash_table (info),
8224
0
            elf_gnu_hash_process_symidx, &cinfo);
8225
0
    }
8226
8227
        /* Output non-base symbols last.  */
8228
0
        cinfo.base_symbol = false;
8229
0
        elf_link_hash_traverse (elf_hash_table (info),
8230
0
              elf_gnu_hash_process_symidx, &cinfo);
8231
8232
0
        contents = s->contents + 16;
8233
0
        for (i = 0; i < maskwords; ++i)
8234
0
    {
8235
0
      bfd_put (obed->s->arch_size, info->output_bfd,
8236
0
         cinfo.bitmask[i], contents);
8237
0
      contents += obed->s->arch_size / 8;
8238
0
    }
8239
8240
0
        free (cinfo.bitmask);
8241
0
        free (cinfo.hashcodes);
8242
0
      }
8243
0
  }
8244
8245
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8246
0
      BFD_ASSERT (s != NULL);
8247
8248
0
      elf_finalize_dynstr (info->output_bfd, info);
8249
8250
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8251
8252
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8253
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8254
0
    return false;
8255
0
    }
8256
8257
0
  return true;
8258
0
}
8259

8260
/* Create an entry in an ELF linker hash table.  */
8261
8262
struct bfd_hash_entry *
8263
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8264
          struct bfd_hash_table *table,
8265
          const char *string)
8266
0
{
8267
  /* Allocate the structure if it has not already been allocated by a
8268
     subclass.  */
8269
0
  if (entry == NULL)
8270
0
    {
8271
0
      entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8272
0
      if (entry == NULL)
8273
0
  return entry;
8274
0
    }
8275
8276
  /* Call the allocation method of the superclass.  */
8277
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8278
0
  if (entry != NULL)
8279
0
    {
8280
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8281
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8282
8283
      /* Set local fields.  */
8284
0
      ret->indx = -1;
8285
0
      ret->dynindx = -1;
8286
0
      ret->got = htab->init_got_refcount;
8287
0
      ret->plt = htab->init_plt_refcount;
8288
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8289
0
            - offsetof (struct elf_link_hash_entry, size)));
8290
      /* Assume that we have been called by a non-ELF symbol reader.
8291
   This flag is then reset by the code which reads an ELF input
8292
   file.  This ensures that a symbol created by a non-ELF symbol
8293
   reader will have the flag set correctly.  */
8294
0
      ret->non_elf = 1;
8295
0
    }
8296
8297
0
  return entry;
8298
0
}
8299
8300
/* Copy data from an indirect symbol to its direct symbol, hiding the
8301
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8302
8303
void
8304
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8305
          struct elf_link_hash_entry *dir,
8306
          struct elf_link_hash_entry *ind)
8307
0
{
8308
0
  struct elf_link_hash_table *htab;
8309
8310
0
  if (ind->dyn_relocs != NULL)
8311
0
    {
8312
0
      if (dir->dyn_relocs != NULL)
8313
0
  {
8314
0
    struct elf_dyn_relocs **pp;
8315
0
    struct elf_dyn_relocs *p;
8316
8317
    /* Add reloc counts against the indirect sym to the direct sym
8318
       list.  Merge any entries against the same section.  */
8319
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8320
0
      {
8321
0
        struct elf_dyn_relocs *q;
8322
8323
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8324
0
    if (q->sec == p->sec)
8325
0
      {
8326
0
        q->pc_count += p->pc_count;
8327
0
        q->count += p->count;
8328
0
        *pp = p->next;
8329
0
        break;
8330
0
      }
8331
0
        if (q == NULL)
8332
0
    pp = &p->next;
8333
0
      }
8334
0
    *pp = dir->dyn_relocs;
8335
0
  }
8336
8337
0
      dir->dyn_relocs = ind->dyn_relocs;
8338
0
      ind->dyn_relocs = NULL;
8339
0
    }
8340
8341
  /* Copy down any references that we may have already seen to the
8342
     symbol which just became indirect.  */
8343
8344
0
  if (dir->versioned != versioned_hidden)
8345
0
    dir->ref_dynamic |= ind->ref_dynamic;
8346
0
  dir->ref_regular |= ind->ref_regular;
8347
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8348
0
  dir->non_got_ref |= ind->non_got_ref;
8349
0
  dir->needs_plt |= ind->needs_plt;
8350
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8351
8352
0
  if (ind->root.type != bfd_link_hash_indirect)
8353
0
    return;
8354
8355
  /* Copy over the global and procedure linkage table refcount entries.
8356
     These may have been already set up by a check_relocs routine.  */
8357
0
  htab = elf_hash_table (info);
8358
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8359
0
    {
8360
0
      if (dir->got.refcount < 0)
8361
0
  dir->got.refcount = 0;
8362
0
      dir->got.refcount += ind->got.refcount;
8363
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8364
0
    }
8365
8366
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8367
0
    {
8368
0
      if (dir->plt.refcount < 0)
8369
0
  dir->plt.refcount = 0;
8370
0
      dir->plt.refcount += ind->plt.refcount;
8371
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8372
0
    }
8373
8374
0
  if (ind->dynindx != -1)
8375
0
    {
8376
0
      if (dir->dynindx != -1)
8377
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8378
0
      dir->dynindx = ind->dynindx;
8379
0
      dir->dynstr_index = ind->dynstr_index;
8380
0
      ind->dynindx = -1;
8381
0
      ind->dynstr_index = 0;
8382
0
    }
8383
0
}
8384
8385
void
8386
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8387
        struct elf_link_hash_entry *h,
8388
        bool force_local)
8389
0
{
8390
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8391
0
  if (h->type != STT_GNU_IFUNC)
8392
0
    {
8393
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8394
0
      h->needs_plt = 0;
8395
0
    }
8396
0
  if (force_local)
8397
0
    {
8398
0
      h->forced_local = 1;
8399
0
      if (h->dynindx != -1)
8400
0
  {
8401
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8402
0
          h->dynstr_index);
8403
0
    h->dynindx = -1;
8404
0
    h->dynstr_index = 0;
8405
0
  }
8406
0
    }
8407
0
}
8408
8409
/* Hide a symbol. */
8410
8411
void
8412
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8413
         struct bfd_link_info *info,
8414
         struct bfd_link_hash_entry *h)
8415
0
{
8416
0
  if (is_elf_hash_table (info->hash))
8417
0
    {
8418
0
      elf_backend_data *obed = get_elf_backend_data (output_bfd);
8419
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8420
0
      eh->def_dynamic = 0;
8421
0
      eh->ref_dynamic = 0;
8422
0
      eh->dynamic_def = 0;
8423
0
      obed->elf_backend_hide_symbol (info, eh, true);
8424
0
    }
8425
0
}
8426
8427
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8428
   caller.  */
8429
8430
bool
8431
_bfd_elf_link_hash_table_init
8432
  (struct elf_link_hash_table *table,
8433
   bfd *obfd,
8434
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8435
              struct bfd_hash_table *,
8436
              const char *),
8437
   unsigned int entsize)
8438
0
{
8439
0
  bool ret;
8440
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
8441
0
  int can_refcount = obed->can_refcount;
8442
8443
0
  table->init_got_refcount.refcount = can_refcount - 1;
8444
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8445
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8446
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8447
  /* The first dynamic symbol is a dummy.  */
8448
0
  table->dynsymcount = 1;
8449
8450
0
  ret = _bfd_link_hash_table_init (&table->root, obfd, newfunc, entsize);
8451
8452
0
  table->root.type = bfd_link_elf_hash_table;
8453
0
  table->hash_table_id = obed->target_id;
8454
0
  table->target_os = obed->target_os;
8455
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8456
8457
0
  return ret;
8458
0
}
8459
8460
/* Create an ELF linker hash table.  */
8461
8462
struct bfd_link_hash_table *
8463
_bfd_elf_link_hash_table_create (bfd *obfd)
8464
0
{
8465
0
  struct elf_link_hash_table *ret;
8466
8467
0
  ret = bfd_zmalloc (sizeof (*ret));
8468
0
  if (ret == NULL)
8469
0
    return NULL;
8470
8471
0
  if (! _bfd_elf_link_hash_table_init (ret, obfd, _bfd_elf_link_hash_newfunc,
8472
0
               sizeof (struct elf_link_hash_entry)))
8473
0
    {
8474
0
      free (ret);
8475
0
      return NULL;
8476
0
    }
8477
8478
0
  return &ret->root;
8479
0
}
8480
8481
/* Destroy an ELF linker hash table.  */
8482
8483
void
8484
_bfd_elf_link_hash_table_free (bfd *obfd)
8485
0
{
8486
0
  struct elf_link_hash_table *htab;
8487
8488
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8489
0
  if (htab->dynstr != NULL)
8490
0
    _bfd_elf_strtab_free (htab->dynstr);
8491
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8492
0
  if (htab->dynamic != NULL)
8493
0
    {
8494
0
      free (htab->dynamic->contents);
8495
0
      htab->dynamic->contents = NULL;
8496
0
    }
8497
0
  if (htab->first_hash != NULL)
8498
0
    {
8499
0
      bfd_hash_table_free (htab->first_hash);
8500
0
      free (htab->first_hash);
8501
0
    }
8502
0
  if (htab->eh_info.frame_hdr_is_compact)
8503
0
    free (htab->eh_info.u.compact.entries);
8504
0
  else
8505
0
    free (htab->eh_info.u.dwarf.array);
8506
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8507
0
  _bfd_generic_link_hash_table_free (obfd);
8508
0
}
8509
8510
/* This is a hook for the ELF emulation code in the generic linker to
8511
   tell the backend linker what file name to use for the DT_NEEDED
8512
   entry for a dynamic object.  */
8513
8514
void
8515
bfd_elf_set_dt_needed_name (bfd *obfd, const char *name)
8516
0
{
8517
0
  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour
8518
0
      && bfd_get_format (obfd) == bfd_object)
8519
0
    elf_dt_name (obfd) = name;
8520
0
}
8521
8522
int
8523
bfd_elf_get_dyn_lib_class (bfd *abfd)
8524
0
{
8525
0
  int lib_class;
8526
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8527
0
      && bfd_get_format (abfd) == bfd_object)
8528
0
    lib_class = elf_dyn_lib_class (abfd);
8529
0
  else
8530
0
    lib_class = 0;
8531
0
  return lib_class;
8532
0
}
8533
8534
void
8535
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8536
0
{
8537
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8538
0
      && bfd_get_format (abfd) == bfd_object)
8539
0
    elf_dyn_lib_class (abfd) = lib_class;
8540
0
}
8541
8542
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8543
   the linker ELF emulation code.  */
8544
8545
struct bfd_link_needed_list *
8546
bfd_elf_get_needed_list (struct bfd_link_info *info)
8547
0
{
8548
0
  if (! is_elf_hash_table (info->hash))
8549
0
    return NULL;
8550
0
  return elf_hash_table (info)->needed;
8551
0
}
8552
8553
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8554
   hook for the linker ELF emulation code.  */
8555
8556
struct bfd_link_needed_list *
8557
bfd_elf_get_runpath_list (struct bfd_link_info *info)
8558
0
{
8559
0
  if (! is_elf_hash_table (info->hash))
8560
0
    return NULL;
8561
0
  return elf_hash_table (info)->runpath;
8562
0
}
8563
8564
/* Get the name actually used for a dynamic object for a link.  This
8565
   is the SONAME entry if there is one.  Otherwise, it is the string
8566
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8567
8568
const char *
8569
bfd_elf_get_dt_soname (bfd *abfd)
8570
0
{
8571
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8572
0
      && bfd_get_format (abfd) == bfd_object)
8573
0
    return elf_dt_name (abfd);
8574
0
  return NULL;
8575
0
}
8576
8577
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8578
   the ELF linker emulation code.  */
8579
8580
bool
8581
bfd_elf_get_bfd_needed_list (bfd *abfd,
8582
           struct bfd_link_needed_list **pneeded)
8583
0
{
8584
0
  asection *s;
8585
0
  bfd_byte *dynbuf = NULL;
8586
0
  unsigned int elfsec;
8587
0
  unsigned long shlink;
8588
0
  bfd_byte *extdyn, *extdynend;
8589
0
  size_t extdynsize;
8590
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8591
8592
0
  *pneeded = NULL;
8593
8594
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8595
0
      || bfd_get_format (abfd) != bfd_object)
8596
0
    return true;
8597
8598
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8599
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8600
0
    return true;
8601
8602
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8603
0
    goto error_return;
8604
8605
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8606
0
  if (elfsec == SHN_BAD)
8607
0
    goto error_return;
8608
8609
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8610
8611
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8612
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8613
8614
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8615
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8616
0
       extdyn += extdynsize)
8617
0
    {
8618
0
      Elf_Internal_Dyn dyn;
8619
8620
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8621
8622
0
      if (dyn.d_tag == DT_NULL)
8623
0
  break;
8624
8625
0
      if (dyn.d_tag == DT_NEEDED)
8626
0
  {
8627
0
    const char *string;
8628
0
    struct bfd_link_needed_list *l;
8629
0
    unsigned int tagv = dyn.d_un.d_val;
8630
8631
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8632
0
    if (string == NULL)
8633
0
      goto error_return;
8634
8635
0
    l = bfd_alloc (abfd, sizeof (*l));
8636
0
    if (l == NULL)
8637
0
      goto error_return;
8638
8639
0
    l->by = abfd;
8640
0
    l->name = string;
8641
0
    l->next = *pneeded;
8642
0
    *pneeded = l;
8643
0
  }
8644
0
    }
8645
8646
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8647
8648
0
  return true;
8649
8650
0
 error_return:
8651
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8652
0
  return false;
8653
0
}
8654
8655
struct elf_symbuf_symbol
8656
{
8657
  unsigned long st_name;  /* Symbol name, index in string tbl */
8658
  unsigned char st_info;  /* Type and binding attributes */
8659
  unsigned char st_other; /* Visibilty, and target specific */
8660
};
8661
8662
struct elf_symbuf_head
8663
{
8664
  struct elf_symbuf_symbol *ssym;
8665
  size_t count;
8666
  unsigned int st_shndx;
8667
};
8668
8669
struct elf_symbol
8670
{
8671
  union
8672
    {
8673
      Elf_Internal_Sym *isym;
8674
      struct elf_symbuf_symbol *ssym;
8675
      void *p;
8676
    } u;
8677
  const char *name;
8678
};
8679
8680
/* Sort references to symbols by ascending section number.  */
8681
8682
static int
8683
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8684
0
{
8685
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8686
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8687
8688
0
  if (s1->st_shndx != s2->st_shndx)
8689
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8690
  /* Final sort by the address of the sym in the symbuf ensures
8691
     a stable sort.  */
8692
0
  if (s1 != s2)
8693
0
    return s1 > s2 ? 1 : -1;
8694
0
  return 0;
8695
0
}
8696
8697
static int
8698
elf_sym_name_compare (const void *arg1, const void *arg2)
8699
0
{
8700
0
  const struct elf_symbol *s1 = arg1;
8701
0
  const struct elf_symbol *s2 = arg2;
8702
0
  int ret = strcmp (s1->name, s2->name);
8703
0
  if (ret != 0)
8704
0
    return ret;
8705
0
  if (s1->u.p != s2->u.p)
8706
0
    return s1->u.p > s2->u.p ? 1 : -1;
8707
0
  return 0;
8708
0
}
8709
8710
static struct elf_symbuf_head *
8711
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8712
0
{
8713
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8714
0
  struct elf_symbuf_symbol *ssym;
8715
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8716
0
  size_t i, shndx_count, total_size;
8717
8718
0
  indbuf = bfd_malloc (symcount * sizeof (*indbuf));
8719
0
  if (indbuf == NULL)
8720
0
    return NULL;
8721
8722
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8723
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8724
0
      *ind++ = &isymbuf[i];
8725
0
  indbufend = ind;
8726
8727
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8728
0
   elf_sort_elf_symbol);
8729
8730
0
  shndx_count = 0;
8731
0
  if (indbufend > indbuf)
8732
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8733
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8734
0
  shndx_count++;
8735
8736
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8737
0
    + (indbufend - indbuf) * sizeof (*ssym));
8738
0
  ssymbuf = bfd_malloc (total_size);
8739
0
  if (ssymbuf == NULL)
8740
0
    {
8741
0
      free (indbuf);
8742
0
      return NULL;
8743
0
    }
8744
8745
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8746
0
  ssymbuf->ssym = NULL;
8747
0
  ssymbuf->count = shndx_count;
8748
0
  ssymbuf->st_shndx = 0;
8749
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8750
0
    {
8751
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8752
0
  {
8753
0
    ssymhead++;
8754
0
    ssymhead->ssym = ssym;
8755
0
    ssymhead->count = 0;
8756
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8757
0
  }
8758
0
      ssym->st_name = (*ind)->st_name;
8759
0
      ssym->st_info = (*ind)->st_info;
8760
0
      ssym->st_other = (*ind)->st_other;
8761
0
      ssymhead->count++;
8762
0
    }
8763
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8764
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8765
8766
0
  free (indbuf);
8767
0
  return ssymbuf;
8768
0
}
8769
8770
/* Check if 2 sections define the same set of local and global
8771
   symbols.  */
8772
8773
static bool
8774
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8775
           struct bfd_link_info *info)
8776
0
{
8777
0
  bfd *bfd1, *bfd2;
8778
0
  elf_backend_data *bed1, *bed2;
8779
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8780
0
  size_t symcount1, symcount2;
8781
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8782
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8783
0
  Elf_Internal_Sym *isym, *isymend;
8784
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8785
0
  size_t count1, count2, sec_count1, sec_count2, i;
8786
0
  unsigned int shndx1, shndx2;
8787
0
  bool result;
8788
0
  bool ignore_section_symbol_p;
8789
8790
0
  bfd1 = sec1->owner;
8791
0
  bfd2 = sec2->owner;
8792
8793
  /* Both sections have to be in ELF.  */
8794
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8795
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8796
0
    return false;
8797
8798
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8799
0
    return false;
8800
8801
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8802
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8803
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8804
0
    return false;
8805
8806
0
  bed1 = get_elf_backend_data (bfd1);
8807
0
  bed2 = get_elf_backend_data (bfd2);
8808
0
  hdr1 = &elf_symtab_hdr (bfd1);
8809
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8810
0
  hdr2 = &elf_symtab_hdr (bfd2);
8811
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8812
8813
0
  if (symcount1 == 0 || symcount2 == 0)
8814
0
    return false;
8815
8816
0
  result = false;
8817
0
  isymbuf1 = NULL;
8818
0
  isymbuf2 = NULL;
8819
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8820
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8821
8822
  /* Ignore section symbols only when matching non-debugging sections
8823
     or linkonce section with comdat section.  */
8824
0
  ignore_section_symbol_p
8825
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8826
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8827
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8828
8829
0
  if (ssymbuf1 == NULL)
8830
0
    {
8831
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8832
0
               NULL, NULL, NULL);
8833
0
      if (isymbuf1 == NULL)
8834
0
  goto done;
8835
8836
0
      if (info != NULL && !info->reduce_memory_overheads)
8837
0
  {
8838
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8839
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8840
0
  }
8841
0
    }
8842
8843
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8844
0
    {
8845
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8846
0
               NULL, NULL, NULL);
8847
0
      if (isymbuf2 == NULL)
8848
0
  goto done;
8849
8850
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8851
0
  {
8852
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8853
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8854
0
  }
8855
0
    }
8856
8857
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8858
0
    {
8859
      /* Optimized faster version.  */
8860
0
      size_t lo, hi, mid;
8861
0
      struct elf_symbol *symp;
8862
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8863
8864
0
      lo = 0;
8865
0
      hi = ssymbuf1->count;
8866
0
      ssymbuf1++;
8867
0
      count1 = 0;
8868
0
      sec_count1 = 0;
8869
0
      while (lo < hi)
8870
0
  {
8871
0
    mid = (lo + hi) / 2;
8872
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8873
0
      hi = mid;
8874
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8875
0
      lo = mid + 1;
8876
0
    else
8877
0
      {
8878
0
        count1 = ssymbuf1[mid].count;
8879
0
        ssymbuf1 += mid;
8880
0
        break;
8881
0
      }
8882
0
  }
8883
0
      if (ignore_section_symbol_p)
8884
0
  {
8885
0
    for (i = 0; i < count1; i++)
8886
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8887
0
        sec_count1++;
8888
0
    count1 -= sec_count1;
8889
0
  }
8890
8891
0
      lo = 0;
8892
0
      hi = ssymbuf2->count;
8893
0
      ssymbuf2++;
8894
0
      count2 = 0;
8895
0
      sec_count2 = 0;
8896
0
      while (lo < hi)
8897
0
  {
8898
0
    mid = (lo + hi) / 2;
8899
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8900
0
      hi = mid;
8901
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8902
0
      lo = mid + 1;
8903
0
    else
8904
0
      {
8905
0
        count2 = ssymbuf2[mid].count;
8906
0
        ssymbuf2 += mid;
8907
0
        break;
8908
0
      }
8909
0
  }
8910
0
      if (ignore_section_symbol_p)
8911
0
  {
8912
0
    for (i = 0; i < count2; i++)
8913
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8914
0
        sec_count2++;
8915
0
    count2 -= sec_count2;
8916
0
  }
8917
8918
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8919
0
  goto done;
8920
8921
0
      symtable1 = bfd_malloc (count1 * sizeof (*symtable1));
8922
0
      symtable2 = bfd_malloc (count2 * sizeof (*symtable2));
8923
0
      if (symtable1 == NULL || symtable2 == NULL)
8924
0
  goto done;
8925
8926
0
      symp = symtable1;
8927
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8928
0
     ssym < ssymend; ssym++)
8929
0
  if (sec_count1 == 0
8930
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8931
0
    {
8932
0
      symp->u.ssym = ssym;
8933
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8934
0
                hdr1->sh_link,
8935
0
                ssym->st_name);
8936
0
      if (symp->name == NULL)
8937
0
        goto done;
8938
0
      symp++;
8939
0
    }
8940
8941
0
      symp = symtable2;
8942
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8943
0
     ssym < ssymend; ssym++)
8944
0
  if (sec_count2 == 0
8945
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8946
0
    {
8947
0
      symp->u.ssym = ssym;
8948
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8949
0
                hdr2->sh_link,
8950
0
                ssym->st_name);
8951
0
      if (symp->name == NULL)
8952
0
        goto done;
8953
0
      symp++;
8954
0
    }
8955
8956
      /* Sort symbol by name.  */
8957
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8958
0
       elf_sym_name_compare);
8959
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8960
0
       elf_sym_name_compare);
8961
8962
0
      for (i = 0; i < count1; i++)
8963
  /* Two symbols must have the same binding, type and name.  */
8964
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8965
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8966
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8967
0
    goto done;
8968
8969
0
      result = true;
8970
0
      goto done;
8971
0
    }
8972
8973
0
  symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8974
0
  symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8975
0
  if (symtable1 == NULL || symtable2 == NULL)
8976
0
    goto done;
8977
8978
  /* Count definitions in the section.  */
8979
0
  count1 = 0;
8980
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8981
0
    if (isym->st_shndx == shndx1
8982
0
  && (!ignore_section_symbol_p
8983
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8984
0
      symtable1[count1++].u.isym = isym;
8985
8986
0
  count2 = 0;
8987
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8988
0
    if (isym->st_shndx == shndx2
8989
0
  && (!ignore_section_symbol_p
8990
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8991
0
      symtable2[count2++].u.isym = isym;
8992
8993
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8994
0
    goto done;
8995
8996
0
  for (i = 0; i < count1; i++)
8997
0
    {
8998
0
      symtable1[i].name
8999
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
9000
0
             symtable1[i].u.isym->st_name);
9001
0
      if (symtable1[i].name == NULL)
9002
0
  goto done;
9003
0
    }
9004
9005
0
  for (i = 0; i < count2; i++)
9006
0
    {
9007
0
      symtable2[i].name
9008
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
9009
0
             symtable2[i].u.isym->st_name);
9010
0
      if (symtable2[i].name == NULL)
9011
0
  goto done;
9012
0
    }
9013
9014
  /* Sort symbol by name.  */
9015
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
9016
0
   elf_sym_name_compare);
9017
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
9018
0
   elf_sym_name_compare);
9019
9020
0
  for (i = 0; i < count1; i++)
9021
    /* Two symbols must have the same binding, type and name.  */
9022
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
9023
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
9024
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9025
0
      goto done;
9026
9027
0
  result = true;
9028
9029
0
 done:
9030
0
  free (symtable1);
9031
0
  free (symtable2);
9032
0
  free (isymbuf1);
9033
0
  free (isymbuf2);
9034
9035
0
  return result;
9036
0
}
9037
9038
/* Return TRUE if 2 section types are compatible.  */
9039
9040
bool
9041
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9042
        bfd *bbfd, const asection *bsec)
9043
0
{
9044
0
  if (asec == NULL
9045
0
      || bsec == NULL
9046
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9047
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9048
0
    return true;
9049
9050
0
  return elf_section_type (asec) == elf_section_type (bsec);
9051
0
}
9052

9053
/* Final phase of ELF linker.  */
9054
9055
/* A structure we use to avoid passing large numbers of arguments.  */
9056
9057
struct elf_final_link_info
9058
{
9059
  /* General link information.  */
9060
  struct bfd_link_info *info;
9061
  /* Output BFD.  */
9062
  bfd *output_bfd;
9063
  /* Symbol string table.  */
9064
  struct elf_strtab_hash *symstrtab;
9065
  /* .hash section.  */
9066
  asection *hash_sec;
9067
  /* symbol version section (.gnu.version).  */
9068
  asection *symver_sec;
9069
  /* Buffer large enough to hold contents of any section.  */
9070
  bfd_byte *contents;
9071
  /* Buffer large enough to hold external relocs of any section.  */
9072
  void *external_relocs;
9073
  /* Buffer large enough to hold internal relocs of any section.  */
9074
  Elf_Internal_Rela *internal_relocs;
9075
  /* Buffer large enough to hold external local symbols of any input
9076
     BFD.  */
9077
  bfd_byte *external_syms;
9078
  /* And a buffer for symbol section indices.  */
9079
  Elf_External_Sym_Shndx *locsym_shndx;
9080
  /* Buffer large enough to hold internal local symbols of any input
9081
     BFD.  */
9082
  Elf_Internal_Sym *internal_syms;
9083
  /* Array large enough to hold a symbol index for each local symbol
9084
     of any input BFD.  */
9085
  long *indices;
9086
  /* Array large enough to hold a section pointer for each local
9087
     symbol of any input BFD.  */
9088
  asection **sections;
9089
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9090
  Elf_External_Sym_Shndx *symshndxbuf;
9091
  /* Number of STT_FILE syms seen.  */
9092
  size_t filesym_count;
9093
  /* Local symbol hash table.  */
9094
  struct bfd_hash_table local_hash_table;
9095
};
9096
9097
struct local_hash_entry
9098
{
9099
  /* Base hash table entry structure.  */
9100
  struct bfd_hash_entry root;
9101
  /* Size of the local symbol name.  */
9102
  size_t size;
9103
  /* Number of the duplicated local symbol names.  */
9104
  long count;
9105
};
9106
9107
/* Create an entry in the local symbol hash table.  */
9108
9109
static struct bfd_hash_entry *
9110
local_hash_newfunc (struct bfd_hash_entry *entry,
9111
        struct bfd_hash_table *table,
9112
        const char *string)
9113
0
{
9114
9115
  /* Allocate the structure if it has not already been allocated by a
9116
     subclass.  */
9117
0
  if (entry == NULL)
9118
0
    {
9119
0
      entry = bfd_hash_allocate (table, sizeof (struct local_hash_entry));
9120
0
      if (entry == NULL)
9121
0
        return entry;
9122
0
    }
9123
9124
  /* Call the allocation method of the superclass.  */
9125
0
  entry = bfd_hash_newfunc (entry, table, string);
9126
0
  if (entry != NULL)
9127
0
    {
9128
0
      ((struct local_hash_entry *) entry)->count = 0;
9129
0
      ((struct local_hash_entry *) entry)->size = 0;
9130
0
    }
9131
9132
0
  return entry;
9133
0
}
9134
9135
/* This struct is used to pass information to elf_link_output_extsym.  */
9136
9137
struct elf_outext_info
9138
{
9139
  bool failed;
9140
  bool localsyms;
9141
  bool file_sym_done;
9142
  bool base_symbol;
9143
  struct elf_final_link_info *flinfo;
9144
};
9145
9146
9147
/* Support for evaluating a complex relocation.
9148
9149
   Complex relocations are generalized, self-describing relocations.  The
9150
   implementation of them consists of two parts: complex symbols, and the
9151
   relocations themselves.
9152
9153
   The relocations use a reserved elf-wide relocation type code (R_RELC
9154
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9155
   information (start bit, end bit, word width, etc) into the addend.  This
9156
   information is extracted from CGEN-generated operand tables within gas.
9157
9158
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9159
   internal) representing prefix-notation expressions, including but not
9160
   limited to those sorts of expressions normally encoded as addends in the
9161
   addend field.  The symbol mangling format is:
9162
9163
   <node> := <literal>
9164
    |  <unary-operator> ':' <node>
9165
    |  <binary-operator> ':' <node> ':' <node>
9166
    ;
9167
9168
   <literal> := 's' <digits=N> ':' <N character symbol name>
9169
       |  'S' <digits=N> ':' <N character section name>
9170
       |  '#' <hexdigits>
9171
       ;
9172
9173
   <binary-operator> := as in C
9174
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9175
9176
static bool
9177
set_symbol_value (bfd *bfd_with_globals,
9178
      Elf_Internal_Sym *isymbuf,
9179
      size_t locsymcount,
9180
      size_t num_sym,
9181
      size_t symidx,
9182
      bfd_vma val)
9183
0
{
9184
0
  struct elf_link_hash_entry *h;
9185
0
  size_t extsymoff = locsymcount;
9186
9187
0
  if (symidx < locsymcount)
9188
0
    {
9189
0
      Elf_Internal_Sym *sym;
9190
9191
0
      sym = isymbuf + symidx;
9192
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9193
0
  {
9194
    /* It is a local symbol: move it to the
9195
       "absolute" section and give it a value.  */
9196
0
    sym->st_shndx = SHN_ABS;
9197
0
    sym->st_value = val;
9198
0
    return true;
9199
0
  }
9200
0
      if (!elf_bad_symtab (bfd_with_globals))
9201
0
  {
9202
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9203
0
            bfd_with_globals);
9204
0
    bfd_set_error (bfd_error_bad_value);
9205
0
    return false;
9206
0
  }
9207
0
      extsymoff = 0;
9208
0
    }
9209
9210
  /* It is a global symbol: set its link type
9211
     to "defined" and give it a value.  */
9212
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9213
0
            extsymoff, num_sym);
9214
0
  if (h == NULL)
9215
0
    return false;
9216
0
  h->root.type = bfd_link_hash_defined;
9217
0
  h->root.u.def.value = val;
9218
0
  h->root.u.def.section = bfd_abs_section_ptr;
9219
0
  return true;
9220
0
}
9221
9222
static bool
9223
resolve_symbol (const char *name,
9224
    bfd *input_bfd,
9225
    struct elf_final_link_info *flinfo,
9226
    bfd_vma *result,
9227
    Elf_Internal_Sym *isymbuf,
9228
    size_t locsymcount)
9229
0
{
9230
0
  Elf_Internal_Sym *sym;
9231
0
  struct bfd_link_hash_entry *global_entry;
9232
0
  const char *candidate = NULL;
9233
0
  Elf_Internal_Shdr *symtab_hdr;
9234
0
  size_t i;
9235
9236
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9237
9238
0
  for (i = 0; i < locsymcount; ++ i)
9239
0
    {
9240
0
      sym = isymbuf + i;
9241
9242
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9243
0
  continue;
9244
9245
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9246
0
               symtab_hdr->sh_link,
9247
0
               sym->st_name);
9248
#ifdef DEBUG
9249
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9250
        name, candidate, (unsigned long) sym->st_value);
9251
#endif
9252
0
      if (candidate && strcmp (candidate, name) == 0)
9253
0
  {
9254
0
    asection *sec = flinfo->sections [i];
9255
9256
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9257
0
    *result += sec->output_offset + sec->output_section->vma;
9258
#ifdef DEBUG
9259
    printf ("Found symbol with value %8.8lx\n",
9260
      (unsigned long) *result);
9261
#endif
9262
0
    return true;
9263
0
  }
9264
0
    }
9265
9266
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9267
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9268
0
               false, false, true);
9269
0
  if (!global_entry)
9270
0
    return false;
9271
9272
0
  if (global_entry->type == bfd_link_hash_defined
9273
0
      || global_entry->type == bfd_link_hash_defweak)
9274
0
    {
9275
0
      *result = (global_entry->u.def.value
9276
0
     + global_entry->u.def.section->output_section->vma
9277
0
     + global_entry->u.def.section->output_offset);
9278
#ifdef DEBUG
9279
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9280
        global_entry->root.string, (unsigned long) *result);
9281
#endif
9282
0
      return true;
9283
0
    }
9284
9285
0
  return false;
9286
0
}
9287
9288
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9289
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9290
   names like "foo.end" which is the end address of section "foo".  */
9291
9292
static bool
9293
resolve_section (const char *name,
9294
     asection *sections,
9295
     bfd_vma *result,
9296
     bfd * abfd)
9297
0
{
9298
0
  asection *curr;
9299
0
  unsigned int len;
9300
9301
0
  for (curr = sections; curr; curr = curr->next)
9302
0
    if (strcmp (curr->name, name) == 0)
9303
0
      {
9304
0
  *result = curr->vma;
9305
0
  return true;
9306
0
      }
9307
9308
  /* Hmm. still haven't found it. try pseudo-section names.  */
9309
  /* FIXME: This could be coded more efficiently...  */
9310
0
  for (curr = sections; curr; curr = curr->next)
9311
0
    {
9312
0
      len = strlen (curr->name);
9313
0
      if (len > strlen (name))
9314
0
  continue;
9315
9316
0
      if (strncmp (curr->name, name, len) == 0)
9317
0
  {
9318
0
    if (startswith (name + len, ".end"))
9319
0
      {
9320
0
        *result = (curr->vma
9321
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9322
0
        return true;
9323
0
      }
9324
9325
    /* Insert more pseudo-section names here, if you like.  */
9326
0
  }
9327
0
    }
9328
9329
0
  return false;
9330
0
}
9331
9332
static void
9333
undefined_reference (const char *reftype, const char *name)
9334
0
{
9335
  /* xgettext:c-format */
9336
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9337
0
          reftype, name);
9338
0
  bfd_set_error (bfd_error_bad_value);
9339
0
}
9340
9341
static bool
9342
eval_symbol (bfd_vma *result,
9343
       const char **symp,
9344
       bfd *input_bfd,
9345
       struct elf_final_link_info *flinfo,
9346
       bfd_vma dot,
9347
       Elf_Internal_Sym *isymbuf,
9348
       size_t locsymcount,
9349
       int signed_p)
9350
0
{
9351
0
  size_t len;
9352
0
  size_t symlen;
9353
0
  bfd_vma a;
9354
0
  bfd_vma b;
9355
0
  char symbuf[4096];
9356
0
  const char *sym = *symp;
9357
0
  const char *symend;
9358
0
  bool symbol_is_section = false;
9359
9360
0
  len = strlen (sym);
9361
0
  symend = sym + len;
9362
9363
0
  if (len < 1 || len > sizeof (symbuf))
9364
0
    {
9365
0
      bfd_set_error (bfd_error_invalid_operation);
9366
0
      return false;
9367
0
    }
9368
9369
0
  switch (* sym)
9370
0
    {
9371
0
    case '.':
9372
0
      *result = dot;
9373
0
      *symp = sym + 1;
9374
0
      return true;
9375
9376
0
    case '#':
9377
0
      ++sym;
9378
0
      *result = strtoul (sym, (char **) symp, 16);
9379
0
      return true;
9380
9381
0
    case 'S':
9382
0
      symbol_is_section = true;
9383
      /* Fall through.  */
9384
0
    case 's':
9385
0
      ++sym;
9386
0
      symlen = strtol (sym, (char **) symp, 10);
9387
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9388
9389
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9390
0
  {
9391
0
    bfd_set_error (bfd_error_invalid_operation);
9392
0
    return false;
9393
0
  }
9394
9395
0
      memcpy (symbuf, sym, symlen);
9396
0
      symbuf[symlen] = '\0';
9397
0
      *symp = sym + symlen;
9398
9399
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9400
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9401
   interpretation here; section means "try section first", not "must be a
9402
   section", and likewise with symbol.  */
9403
9404
0
      if (symbol_is_section)
9405
0
  {
9406
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9407
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9408
0
          isymbuf, locsymcount))
9409
0
      {
9410
0
        undefined_reference ("section", symbuf);
9411
0
        return false;
9412
0
      }
9413
0
  }
9414
0
      else
9415
0
  {
9416
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9417
0
             isymbuf, locsymcount)
9418
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9419
0
           result, input_bfd))
9420
0
      {
9421
0
        undefined_reference ("symbol", symbuf);
9422
0
        return false;
9423
0
      }
9424
0
  }
9425
9426
0
      return true;
9427
9428
      /* All that remains are operators.  */
9429
9430
0
#define UNARY_OP(op)            \
9431
0
  if (startswith (sym, #op))          \
9432
0
    {               \
9433
0
      sym += strlen (#op);          \
9434
0
      if (*sym == ':')           \
9435
0
  ++sym;             \
9436
0
      *symp = sym;            \
9437
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9438
0
      isymbuf, locsymcount, signed_p)) \
9439
0
  return false;           \
9440
0
      if (signed_p)           \
9441
0
  *result = op ((bfd_signed_vma) a);     \
9442
0
      else              \
9443
0
  *result = op a;           \
9444
0
      return true;            \
9445
0
    }
9446
9447
0
#define BINARY_OP_HEAD(op)          \
9448
0
  if (startswith (sym, #op))          \
9449
0
    {               \
9450
0
      sym += strlen (#op);          \
9451
0
      if (*sym == ':')           \
9452
0
  ++sym;             \
9453
0
      *symp = sym;            \
9454
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9455
0
      isymbuf, locsymcount, signed_p)) \
9456
0
  return false;           \
9457
0
      ++*symp;              \
9458
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9459
0
      isymbuf, locsymcount, signed_p)) \
9460
0
  return false;
9461
0
#define BINARY_OP_TAIL(op)          \
9462
0
      if (signed_p)           \
9463
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9464
0
      else              \
9465
0
  *result = a op b;         \
9466
0
      return true;            \
9467
0
    }
9468
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9469
9470
0
    default:
9471
0
      UNARY_OP  (0-);
9472
0
      BINARY_OP_HEAD (<<);
9473
0
      if (b >= sizeof (a) * CHAR_BIT)
9474
0
  {
9475
0
    *result = 0;
9476
0
    return true;
9477
0
  }
9478
0
      signed_p = 0;
9479
0
      BINARY_OP_TAIL (<<);
9480
0
      BINARY_OP_HEAD (>>);
9481
0
      if (b >= sizeof (a) * CHAR_BIT)
9482
0
  {
9483
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9484
0
    return true;
9485
0
  }
9486
0
      BINARY_OP_TAIL (>>);
9487
0
      BINARY_OP (==);
9488
0
      BINARY_OP (!=);
9489
0
      BINARY_OP (<=);
9490
0
      BINARY_OP (>=);
9491
0
      BINARY_OP (&&);
9492
0
      BINARY_OP (||);
9493
0
      UNARY_OP  (~);
9494
0
      UNARY_OP  (!);
9495
0
      BINARY_OP (*);
9496
0
      BINARY_OP_HEAD (/);
9497
0
      if (b == 0)
9498
0
  {
9499
0
    _bfd_error_handler (_("division by zero"));
9500
0
    bfd_set_error (bfd_error_bad_value);
9501
0
    return false;
9502
0
  }
9503
0
      BINARY_OP_TAIL (/);
9504
0
      BINARY_OP_HEAD (%);
9505
0
      if (b == 0)
9506
0
  {
9507
0
    _bfd_error_handler (_("division by zero"));
9508
0
    bfd_set_error (bfd_error_bad_value);
9509
0
    return false;
9510
0
  }
9511
0
      BINARY_OP_TAIL (%);
9512
0
      BINARY_OP (^);
9513
0
      BINARY_OP (|);
9514
0
      BINARY_OP (&);
9515
0
      BINARY_OP (+);
9516
0
      BINARY_OP (-);
9517
0
      BINARY_OP (<);
9518
0
      BINARY_OP (>);
9519
0
#undef UNARY_OP
9520
0
#undef BINARY_OP
9521
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9522
0
      bfd_set_error (bfd_error_invalid_operation);
9523
0
      return false;
9524
0
    }
9525
0
}
9526
9527
static void
9528
put_value (bfd_vma size,
9529
     unsigned long chunksz,
9530
     bfd *input_bfd,
9531
     bfd_vma x,
9532
     bfd_byte *location)
9533
0
{
9534
0
  location += (size - chunksz);
9535
9536
0
  for (; size; size -= chunksz, location -= chunksz)
9537
0
    {
9538
0
      switch (chunksz)
9539
0
  {
9540
0
  case 1:
9541
0
    bfd_put_8 (input_bfd, x, location);
9542
0
    x >>= 8;
9543
0
    break;
9544
0
  case 2:
9545
0
    bfd_put_16 (input_bfd, x, location);
9546
0
    x >>= 16;
9547
0
    break;
9548
0
  case 4:
9549
0
    bfd_put_32 (input_bfd, x, location);
9550
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9551
0
    x >>= 16;
9552
0
    x >>= 16;
9553
0
    break;
9554
0
#ifdef BFD64
9555
0
  case 8:
9556
0
    bfd_put_64 (input_bfd, x, location);
9557
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9558
0
    x >>= 32;
9559
0
    x >>= 32;
9560
0
    break;
9561
0
#endif
9562
0
  default:
9563
0
    abort ();
9564
0
    break;
9565
0
  }
9566
0
    }
9567
0
}
9568
9569
static bfd_vma
9570
get_value (bfd_vma size,
9571
     unsigned long chunksz,
9572
     bfd *input_bfd,
9573
     bfd_byte *location)
9574
0
{
9575
0
  int shift;
9576
0
  bfd_vma x = 0;
9577
9578
  /* Sanity checks.  */
9579
0
  BFD_ASSERT (chunksz <= sizeof (x)
9580
0
        && size >= chunksz
9581
0
        && chunksz != 0
9582
0
        && (size % chunksz) == 0
9583
0
        && input_bfd != NULL
9584
0
        && location != NULL);
9585
9586
0
  if (chunksz == sizeof (x))
9587
0
    {
9588
0
      BFD_ASSERT (size == chunksz);
9589
9590
      /* Make sure that we do not perform an undefined shift operation.
9591
   We know that size == chunksz so there will only be one iteration
9592
   of the loop below.  */
9593
0
      shift = 0;
9594
0
    }
9595
0
  else
9596
0
    shift = 8 * chunksz;
9597
9598
0
  for (; size; size -= chunksz, location += chunksz)
9599
0
    {
9600
0
      switch (chunksz)
9601
0
  {
9602
0
  case 1:
9603
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9604
0
    break;
9605
0
  case 2:
9606
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9607
0
    break;
9608
0
  case 4:
9609
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9610
0
    break;
9611
0
#ifdef BFD64
9612
0
  case 8:
9613
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9614
0
    break;
9615
0
#endif
9616
0
  default:
9617
0
    abort ();
9618
0
  }
9619
0
    }
9620
0
  return x;
9621
0
}
9622
9623
static void
9624
decode_complex_addend (unsigned long *start,   /* in bits */
9625
           unsigned long *oplen,   /* in bits */
9626
           unsigned long *len,     /* in bits */
9627
           unsigned long *wordsz,  /* in bytes */
9628
           unsigned long *chunksz, /* in bytes */
9629
           unsigned long *lsb0_p,
9630
           unsigned long *signed_p,
9631
           unsigned long *trunc_p,
9632
           unsigned long encoded)
9633
0
{
9634
0
  * start     =  encoded  & 0x3F;
9635
0
  * len       = (encoded >>  6) & 0x3F;
9636
0
  * oplen     = (encoded >> 12) & 0x3F;
9637
0
  * wordsz    = (encoded >> 18) & 0xF;
9638
0
  * chunksz   = (encoded >> 22) & 0xF;
9639
0
  * lsb0_p    = (encoded >> 27) & 1;
9640
0
  * signed_p  = (encoded >> 28) & 1;
9641
0
  * trunc_p   = (encoded >> 29) & 1;
9642
0
}
9643
9644
bfd_reloc_status_type
9645
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9646
            asection *input_section,
9647
            bfd_byte *contents,
9648
            Elf_Internal_Rela *rel,
9649
            bfd_vma relocation)
9650
0
{
9651
0
  bfd_vma shift, x, mask;
9652
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9653
0
  bfd_reloc_status_type r;
9654
0
  bfd_size_type octets;
9655
9656
  /*  Perform this reloc, since it is complex.
9657
      (this is not to say that it necessarily refers to a complex
9658
      symbol; merely that it is a self-describing CGEN based reloc.
9659
      i.e. the addend has the complete reloc information (bit start, end,
9660
      word size, etc) encoded within it.).  */
9661
9662
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9663
0
       &chunksz, &lsb0_p, &signed_p,
9664
0
       &trunc_p, rel->r_addend);
9665
9666
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9667
9668
0
  if (lsb0_p)
9669
0
    shift = (start + 1) - len;
9670
0
  else
9671
0
    shift = (8 * wordsz) - (start + len);
9672
9673
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9674
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9675
9676
#ifdef DEBUG
9677
  printf ("Doing complex reloc: "
9678
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9679
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9680
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9681
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9682
    oplen, (unsigned long) x, (unsigned long) mask,
9683
    (unsigned long) relocation);
9684
#endif
9685
9686
0
  r = bfd_reloc_ok;
9687
0
  if (! trunc_p)
9688
    /* Now do an overflow check.  */
9689
0
    r = bfd_check_overflow ((signed_p
9690
0
           ? complain_overflow_signed
9691
0
           : complain_overflow_unsigned),
9692
0
          len, 0, (8 * wordsz),
9693
0
          relocation);
9694
9695
  /* Do the deed.  */
9696
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9697
9698
#ifdef DEBUG
9699
  printf ("           relocation: %8.8lx\n"
9700
    "         shifted mask: %8.8lx\n"
9701
    " shifted/masked reloc: %8.8lx\n"
9702
    "               result: %8.8lx\n",
9703
    (unsigned long) relocation, (unsigned long) (mask << shift),
9704
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9705
#endif
9706
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9707
0
  return r;
9708
0
}
9709
9710
/* Functions to read r_offset from external (target order) reloc
9711
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9712
   know the value is aligned.  */
9713
9714
static bfd_vma
9715
ext32l_r_offset (const void *p)
9716
0
{
9717
0
  union aligned32
9718
0
  {
9719
0
    uint32_t v;
9720
0
    unsigned char c[4];
9721
0
  };
9722
0
  const union aligned32 *a
9723
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9724
9725
0
  uint32_t aval = (  (uint32_t) a->c[0]
9726
0
       | (uint32_t) a->c[1] << 8
9727
0
       | (uint32_t) a->c[2] << 16
9728
0
       | (uint32_t) a->c[3] << 24);
9729
0
  return aval;
9730
0
}
9731
9732
static bfd_vma
9733
ext32b_r_offset (const void *p)
9734
0
{
9735
0
  union aligned32
9736
0
  {
9737
0
    uint32_t v;
9738
0
    unsigned char c[4];
9739
0
  };
9740
0
  const union aligned32 *a
9741
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9742
9743
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9744
0
       | (uint32_t) a->c[1] << 16
9745
0
       | (uint32_t) a->c[2] << 8
9746
0
       | (uint32_t) a->c[3]);
9747
0
  return aval;
9748
0
}
9749
9750
static bfd_vma
9751
ext64l_r_offset (const void *p)
9752
0
{
9753
0
  union aligned64
9754
0
  {
9755
0
    uint64_t v;
9756
0
    unsigned char c[8];
9757
0
  };
9758
0
  const union aligned64 *a
9759
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9760
9761
0
  uint64_t aval = (  (uint64_t) a->c[0]
9762
0
       | (uint64_t) a->c[1] << 8
9763
0
       | (uint64_t) a->c[2] << 16
9764
0
       | (uint64_t) a->c[3] << 24
9765
0
       | (uint64_t) a->c[4] << 32
9766
0
       | (uint64_t) a->c[5] << 40
9767
0
       | (uint64_t) a->c[6] << 48
9768
0
       | (uint64_t) a->c[7] << 56);
9769
0
  return aval;
9770
0
}
9771
9772
static bfd_vma
9773
ext64b_r_offset (const void *p)
9774
0
{
9775
0
  union aligned64
9776
0
  {
9777
0
    uint64_t v;
9778
0
    unsigned char c[8];
9779
0
  };
9780
0
  const union aligned64 *a
9781
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9782
9783
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9784
0
       | (uint64_t) a->c[1] << 48
9785
0
       | (uint64_t) a->c[2] << 40
9786
0
       | (uint64_t) a->c[3] << 32
9787
0
       | (uint64_t) a->c[4] << 24
9788
0
       | (uint64_t) a->c[5] << 16
9789
0
       | (uint64_t) a->c[6] << 8
9790
0
       | (uint64_t) a->c[7]);
9791
0
  return aval;
9792
0
}
9793
9794
/* When performing a relocatable link, the input relocations are
9795
   preserved.  But, if they reference global symbols, the indices
9796
   referenced must be updated.  Update all the relocations found in
9797
   RELDATA.  */
9798
9799
static bool
9800
elf_link_adjust_relocs (bfd *abfd,
9801
      asection *sec,
9802
      struct bfd_elf_section_reloc_data *reldata,
9803
      bool sort,
9804
      struct bfd_link_info *info)
9805
0
{
9806
0
  unsigned int i;
9807
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9808
0
  bfd_byte *erela;
9809
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9810
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9811
0
  bfd_vma r_type_mask;
9812
0
  int r_sym_shift;
9813
0
  unsigned int count = reldata->count;
9814
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9815
9816
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9817
0
    {
9818
0
      swap_in = bed->s->swap_reloc_in;
9819
0
      swap_out = bed->s->swap_reloc_out;
9820
0
    }
9821
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9822
0
    {
9823
0
      swap_in = bed->s->swap_reloca_in;
9824
0
      swap_out = bed->s->swap_reloca_out;
9825
0
    }
9826
0
  else
9827
0
    abort ();
9828
9829
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9830
0
    abort ();
9831
9832
0
  if (bed->s->arch_size == 32)
9833
0
    {
9834
0
      r_type_mask = 0xff;
9835
0
      r_sym_shift = 8;
9836
0
    }
9837
0
  else
9838
0
    {
9839
0
      r_type_mask = 0xffffffff;
9840
0
      r_sym_shift = 32;
9841
0
    }
9842
9843
0
  erela = reldata->hdr->contents;
9844
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9845
0
    {
9846
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9847
0
      unsigned int j;
9848
9849
0
      if (*rel_hash == NULL)
9850
0
  continue;
9851
9852
0
      if ((*rel_hash)->indx == -2
9853
0
    && info->gc_sections
9854
0
    && ! info->gc_keep_exported)
9855
0
  {
9856
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9857
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9858
0
            abfd, sec,
9859
0
            (*rel_hash)->root.root.string);
9860
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9861
0
            abfd, sec);
9862
0
    bfd_set_error (bfd_error_invalid_operation);
9863
0
    return false;
9864
0
  }
9865
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9866
9867
0
      (*swap_in) (abfd, erela, irela);
9868
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9869
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9870
0
         | (irela[j].r_info & r_type_mask));
9871
0
      (*swap_out) (abfd, irela, erela);
9872
0
    }
9873
9874
0
  if (bed->elf_backend_update_relocs)
9875
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9876
9877
0
  if (sort && count != 0)
9878
0
    {
9879
0
      bfd_vma (*ext_r_off) (const void *);
9880
0
      bfd_vma r_off;
9881
0
      size_t elt_size;
9882
0
      bfd_byte *base, *end, *p, *loc;
9883
0
      bfd_byte *buf = NULL;
9884
9885
0
      if (bed->s->arch_size == 32)
9886
0
  {
9887
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9888
0
      ext_r_off = ext32l_r_offset;
9889
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9890
0
      ext_r_off = ext32b_r_offset;
9891
0
    else
9892
0
      abort ();
9893
0
  }
9894
0
      else
9895
0
  {
9896
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9897
0
      ext_r_off = ext64l_r_offset;
9898
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9899
0
      ext_r_off = ext64b_r_offset;
9900
0
    else
9901
0
      abort ();
9902
0
  }
9903
9904
      /*  Must use a stable sort here.  A modified insertion sort,
9905
    since the relocs are mostly sorted already.  */
9906
0
      elt_size = reldata->hdr->sh_entsize;
9907
0
      base = reldata->hdr->contents;
9908
0
      end = base + count * elt_size;
9909
0
      if (elt_size > sizeof (Elf64_External_Rela))
9910
0
  abort ();
9911
9912
      /* Ensure the first element is lowest.  This acts as a sentinel,
9913
   speeding the main loop below.  */
9914
0
      r_off = (*ext_r_off) (base);
9915
0
      for (p = loc = base; (p += elt_size) < end; )
9916
0
  {
9917
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9918
0
    if (r_off > r_off2)
9919
0
      {
9920
0
        r_off = r_off2;
9921
0
        loc = p;
9922
0
      }
9923
0
  }
9924
0
      if (loc != base)
9925
0
  {
9926
    /* Don't just swap *base and *loc as that changes the order
9927
       of the original base[0] and base[1] if they happen to
9928
       have the same r_offset.  */
9929
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9930
0
    memcpy (onebuf, loc, elt_size);
9931
0
    memmove (base + elt_size, base, loc - base);
9932
0
    memcpy (base, onebuf, elt_size);
9933
0
  }
9934
9935
0
      for (p = base + elt_size; (p += elt_size) < end; )
9936
0
  {
9937
    /* base to p is sorted, *p is next to insert.  */
9938
0
    r_off = (*ext_r_off) (p);
9939
    /* Search the sorted region for location to insert.  */
9940
0
    loc = p - elt_size;
9941
0
    while (r_off < (*ext_r_off) (loc))
9942
0
      loc -= elt_size;
9943
0
    loc += elt_size;
9944
0
    if (loc != p)
9945
0
      {
9946
        /* Chances are there is a run of relocs to insert here,
9947
     from one of more input files.  Files are not always
9948
     linked in order due to the way elf_link_input_bfd is
9949
     called.  See pr17666.  */
9950
0
        size_t sortlen = p - loc;
9951
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9952
0
        size_t runlen = elt_size;
9953
0
        bfd_vma r_off_runend = r_off;
9954
0
        bfd_vma r_off_runend_next;
9955
0
        size_t buf_size = 96 * 1024;
9956
0
        while (p + runlen < end
9957
0
         && (sortlen <= buf_size
9958
0
       || runlen + elt_size <= buf_size)
9959
         /* run must not break the ordering of base..loc+1 */
9960
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9961
         /* run must be already sorted */
9962
0
         && r_off_runend_next >= r_off_runend)
9963
0
    {
9964
0
      runlen += elt_size;
9965
0
      r_off_runend = r_off_runend_next;
9966
0
    }
9967
0
        if (buf == NULL)
9968
0
    {
9969
0
      buf = bfd_malloc (buf_size);
9970
0
      if (buf == NULL)
9971
0
        return false;
9972
0
    }
9973
0
        if (runlen < sortlen)
9974
0
    {
9975
0
      memcpy (buf, p, runlen);
9976
0
      memmove (loc + runlen, loc, sortlen);
9977
0
      memcpy (loc, buf, runlen);
9978
0
    }
9979
0
        else
9980
0
    {
9981
0
      memcpy (buf, loc, sortlen);
9982
0
      memmove (loc, p, runlen);
9983
0
      memcpy (loc + runlen, buf, sortlen);
9984
0
    }
9985
0
        p += runlen - elt_size;
9986
0
      }
9987
0
  }
9988
      /* Hashes are no longer valid.  */
9989
0
      free (reldata->hashes);
9990
0
      reldata->hashes = NULL;
9991
0
      free (buf);
9992
0
    }
9993
0
  return true;
9994
0
}
9995
9996
struct elf_link_sort_rela
9997
{
9998
  union {
9999
    bfd_vma offset;
10000
    bfd_vma sym_mask;
10001
  } u;
10002
  enum elf_reloc_type_class type;
10003
  /* We use this as an array of size int_rels_per_ext_rel.  */
10004
  Elf_Internal_Rela rela[1];
10005
};
10006
10007
/* qsort stability here and for cmp2 is only an issue if multiple
10008
   dynamic relocations are emitted at the same address.  But targets
10009
   that apply a series of dynamic relocations each operating on the
10010
   result of the prior relocation can't use -z combreloc as
10011
   implemented anyway.  Such schemes tend to be broken by sorting on
10012
   symbol index.  That leaves dynamic NONE relocs as the only other
10013
   case where ld might emit multiple relocs at the same address, and
10014
   those are only emitted due to target bugs.  */
10015
10016
static int
10017
elf_link_sort_cmp1 (const void *A, const void *B)
10018
0
{
10019
0
  const struct elf_link_sort_rela *a = A;
10020
0
  const struct elf_link_sort_rela *b = B;
10021
0
  int relativea, relativeb;
10022
10023
0
  relativea = a->type == reloc_class_relative;
10024
0
  relativeb = b->type == reloc_class_relative;
10025
10026
0
  if (relativea < relativeb)
10027
0
    return 1;
10028
0
  if (relativea > relativeb)
10029
0
    return -1;
10030
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
10031
0
    return -1;
10032
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
10033
0
    return 1;
10034
0
  if (a->rela->r_offset < b->rela->r_offset)
10035
0
    return -1;
10036
0
  if (a->rela->r_offset > b->rela->r_offset)
10037
0
    return 1;
10038
0
  return 0;
10039
0
}
10040
10041
static int
10042
elf_link_sort_cmp2 (const void *A, const void *B)
10043
0
{
10044
0
  const struct elf_link_sort_rela *a = A;
10045
0
  const struct elf_link_sort_rela *b = B;
10046
10047
0
  if (a->type < b->type)
10048
0
    return -1;
10049
0
  if (a->type > b->type)
10050
0
    return 1;
10051
0
  if (a->u.offset < b->u.offset)
10052
0
    return -1;
10053
0
  if (a->u.offset > b->u.offset)
10054
0
    return 1;
10055
0
  if (a->rela->r_offset < b->rela->r_offset)
10056
0
    return -1;
10057
0
  if (a->rela->r_offset > b->rela->r_offset)
10058
0
    return 1;
10059
0
  return 0;
10060
0
}
10061
10062
static size_t
10063
elf_link_sort_relocs (bfd *obfd, struct bfd_link_info *info, asection **psec)
10064
0
{
10065
0
  asection *dynamic_relocs;
10066
0
  asection *rela_dyn;
10067
0
  asection *rel_dyn;
10068
0
  bfd_size_type count, size;
10069
0
  size_t i, ret, sort_elt, ext_size;
10070
0
  bfd_byte *sort, *s_non_relative, *p;
10071
0
  struct elf_link_sort_rela *sq;
10072
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
10073
0
  int i2e = obed->s->int_rels_per_ext_rel;
10074
0
  unsigned int opb = bfd_octets_per_byte (obfd, NULL);
10075
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10076
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10077
0
  struct bfd_link_order *lo;
10078
0
  bfd_vma r_sym_mask;
10079
0
  bool use_rela;
10080
10081
  /* Find a dynamic reloc section.  */
10082
0
  rela_dyn = bfd_get_section_by_name (obfd, ".rela.dyn");
10083
0
  rel_dyn  = bfd_get_section_by_name (obfd, ".rel.dyn");
10084
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10085
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10086
0
    {
10087
0
      bool use_rela_initialised = false;
10088
10089
      /* This is just here to stop gcc from complaining.
10090
   Its initialization checking code is not perfect.  */
10091
0
      use_rela = true;
10092
10093
      /* Both sections are present.  Examine the sizes
10094
   of the indirect sections to help us choose.  */
10095
0
      for (lo = rela_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
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10153
0
  if (lo->type == bfd_indirect_link_order)
10154
0
    {
10155
0
      asection *o = lo->u.indirect.section;
10156
10157
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10158
0
        {
10159
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10160
      /* Section size is divisible by both rel and rela sizes.
10161
         It is of no help to us.  */
10162
0
      ;
10163
0
    else
10164
0
      {
10165
        /* Section size is only divisible by rela.  */
10166
0
        if (use_rela_initialised && !use_rela)
10167
0
          {
10168
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10169
0
                "they are in more than one size"),
10170
0
              obfd);
10171
0
      bfd_set_error (bfd_error_invalid_operation);
10172
0
      return 0;
10173
0
          }
10174
0
        else
10175
0
          {
10176
0
      use_rela = true;
10177
0
      use_rela_initialised = true;
10178
0
          }
10179
0
      }
10180
0
        }
10181
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10182
0
        {
10183
    /* Section size is only divisible by rel.  */
10184
0
    if (use_rela_initialised && use_rela)
10185
0
      {
10186
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10187
0
            "they are in more than one size"),
10188
0
          obfd);
10189
0
        bfd_set_error (bfd_error_invalid_operation);
10190
0
        return 0;
10191
0
      }
10192
0
    else
10193
0
      {
10194
0
        use_rela = false;
10195
0
        use_rela_initialised = true;
10196
0
      }
10197
0
        }
10198
0
      else
10199
0
        {
10200
    /* The section size is not divisible by either -
10201
       something is wrong.  */
10202
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10203
0
              "they are of an unknown size"), obfd);
10204
0
    bfd_set_error (bfd_error_invalid_operation);
10205
0
    return 0;
10206
0
        }
10207
0
    }
10208
10209
0
      if (! use_rela_initialised)
10210
  /* Make a guess.  */
10211
0
  use_rela = true;
10212
0
    }
10213
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10214
0
    use_rela = true;
10215
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10216
0
    use_rela = false;
10217
0
  else
10218
0
    return 0;
10219
10220
0
  if (use_rela)
10221
0
    {
10222
0
      dynamic_relocs = rela_dyn;
10223
0
      ext_size = obed->s->sizeof_rela;
10224
0
      swap_in = obed->s->swap_reloca_in;
10225
0
      swap_out = obed->s->swap_reloca_out;
10226
0
    }
10227
0
  else
10228
0
    {
10229
0
      dynamic_relocs = rel_dyn;
10230
0
      ext_size = obed->s->sizeof_rel;
10231
0
      swap_in = obed->s->swap_reloc_in;
10232
0
      swap_out = obed->s->swap_reloc_out;
10233
0
    }
10234
10235
0
  size = 0;
10236
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10237
0
    if (lo->type == bfd_indirect_link_order)
10238
0
      size += lo->u.indirect.section->size;
10239
10240
0
  if (size != dynamic_relocs->size)
10241
0
    return 0;
10242
10243
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10244
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10245
10246
0
  count = dynamic_relocs->size / ext_size;
10247
0
  if (count == 0)
10248
0
    return 0;
10249
0
  sort = bfd_zmalloc (sort_elt * count);
10250
10251
0
  if (sort == NULL)
10252
0
    {
10253
0
      (*info->callbacks->warning)
10254
0
  (info, _("not enough memory to sort relocations"), 0, obfd, 0, 0);
10255
0
      return 0;
10256
0
    }
10257
10258
0
  if (obed->s->arch_size == 32)
10259
0
    r_sym_mask = ~(bfd_vma) 0xff;
10260
0
  else
10261
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10262
10263
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10264
0
    if (lo->type == bfd_indirect_link_order)
10265
0
      {
10266
0
  bfd_byte *erel, *erelend;
10267
0
  asection *o = lo->u.indirect.section;
10268
10269
0
  if (o->contents == NULL && o->size != 0)
10270
0
    {
10271
      /* This is a reloc section that is being handled as a normal
10272
         section.  See bfd_section_from_shdr.  We can't combine
10273
         relocs in this case.  */
10274
0
      free (sort);
10275
0
      return 0;
10276
0
    }
10277
0
  erel = o->contents;
10278
0
  erelend = o->contents + o->size;
10279
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10280
10281
0
  while (erel < erelend)
10282
0
    {
10283
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10284
10285
0
      (*swap_in) (obfd, erel, s->rela);
10286
0
      s->type = obed->elf_backend_reloc_type_class (info, o, s->rela);
10287
0
      s->u.sym_mask = r_sym_mask;
10288
0
      p += sort_elt;
10289
0
      erel += ext_size;
10290
0
    }
10291
0
      }
10292
10293
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10294
10295
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10296
0
    {
10297
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10298
0
      if (s->type != reloc_class_relative)
10299
0
  break;
10300
0
    }
10301
0
  ret = i;
10302
0
  s_non_relative = p;
10303
10304
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10305
0
  for (; i < count; i++, p += sort_elt)
10306
0
    {
10307
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10308
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10309
0
  sq = sp;
10310
0
      sp->u.offset = sq->rela->r_offset;
10311
0
    }
10312
10313
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10314
10315
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10316
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10317
0
    {
10318
      /* We have plt relocs in .rela.dyn.  */
10319
0
      sq = (struct elf_link_sort_rela *) sort;
10320
0
      for (i = 0; i < count; i++)
10321
0
  if (sq[count - i - 1].type != reloc_class_plt)
10322
0
    break;
10323
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10324
0
  {
10325
0
    struct bfd_link_order **plo;
10326
    /* Put srelplt link_order last.  This is so the output_offset
10327
       set in the next loop is correct for DT_JMPREL.  */
10328
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10329
0
      if ((*plo)->type == bfd_indirect_link_order
10330
0
    && (*plo)->u.indirect.section == htab->srelplt)
10331
0
        {
10332
0
    lo = *plo;
10333
0
    *plo = lo->next;
10334
0
        }
10335
0
      else
10336
0
        plo = &(*plo)->next;
10337
0
    *plo = lo;
10338
0
    lo->next = NULL;
10339
0
    dynamic_relocs->map_tail.link_order = lo;
10340
0
  }
10341
0
    }
10342
10343
0
  p = sort;
10344
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10345
0
    if (lo->type == bfd_indirect_link_order)
10346
0
      {
10347
0
  bfd_byte *erel, *erelend;
10348
0
  asection *o = lo->u.indirect.section;
10349
10350
0
  erel = o->contents;
10351
0
  erelend = o->contents + o->size;
10352
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10353
0
  while (erel < erelend)
10354
0
    {
10355
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10356
0
      (*swap_out) (obfd, s->rela, erel);
10357
0
      p += sort_elt;
10358
0
      erel += ext_size;
10359
0
    }
10360
0
      }
10361
10362
0
  free (sort);
10363
0
  *psec = dynamic_relocs;
10364
0
  return ret;
10365
0
}
10366
10367
/* Add a symbol to the output symbol string table.  */
10368
10369
static int
10370
elf_link_output_symstrtab (void *finf,
10371
         const char *name,
10372
         Elf_Internal_Sym *elfsym,
10373
         asection *input_sec,
10374
         struct elf_link_hash_entry *h)
10375
0
{
10376
0
  struct elf_final_link_info *flinfo = finf;
10377
0
  int (*output_symbol_hook)
10378
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10379
0
     struct elf_link_hash_entry *);
10380
0
  struct elf_link_hash_table *hash_table;
10381
0
  elf_backend_data *bed;
10382
0
  bfd_size_type strtabsize;
10383
10384
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10385
10386
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10387
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10388
0
  if (output_symbol_hook != NULL)
10389
0
    {
10390
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10391
0
      if (ret != 1)
10392
0
  return ret;
10393
0
    }
10394
10395
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10396
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10397
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10398
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10399
10400
0
  if (name == NULL || *name == '\0')
10401
0
    elfsym->st_name = (unsigned long) -1;
10402
0
  else
10403
0
    {
10404
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10405
   to get the final offset for st_name.  */
10406
0
      char *versioned_name = (char *) name;
10407
0
      if (h != NULL)
10408
0
  {
10409
0
    if (h->versioned == versioned && h->def_dynamic)
10410
0
      {
10411
        /* Keep only one '@' for versioned symbols defined in
10412
           shared objects.  */
10413
0
        const char *version = strrchr (name, ELF_VER_CHR);
10414
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10415
0
        if (version != base_end)
10416
0
    {
10417
0
      size_t base_len;
10418
0
      size_t len = strlen (name);
10419
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10420
0
      if (versioned_name == NULL)
10421
0
        return 0;
10422
0
      base_len = base_end - name;
10423
0
      memcpy (versioned_name, name, base_len);
10424
0
      memcpy (versioned_name + base_len, version,
10425
0
        len - base_len);
10426
0
    }
10427
0
      }
10428
0
  }
10429
0
      else if (flinfo->info->unique_symbol
10430
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10431
0
  {
10432
0
    struct local_hash_entry *lh;
10433
0
    size_t count_len;
10434
0
    size_t base_len;
10435
0
    char buf[30];
10436
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10437
0
      {
10438
0
      case STT_FILE:
10439
0
      case STT_SECTION:
10440
0
        break;
10441
0
      default:
10442
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10443
0
         (&flinfo->local_hash_table, name, true, false);
10444
0
        if (lh == NULL)
10445
0
    return 0;
10446
        /* Always append ".COUNT" to local symbols to avoid
10447
     potential conflicts with local symbol "XXX.COUNT".  */
10448
0
        sprintf (buf, "%lx", lh->count);
10449
0
        base_len = lh->size;
10450
0
        if (!base_len)
10451
0
    {
10452
0
      base_len = strlen (name);
10453
0
      lh->size = base_len;
10454
0
    }
10455
0
        count_len = strlen (buf);
10456
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10457
0
            base_len + count_len + 2);
10458
0
        if (versioned_name == NULL)
10459
0
    return 0;
10460
0
        memcpy (versioned_name, name, base_len);
10461
0
        versioned_name[base_len] = '.';
10462
0
        memcpy (versioned_name + base_len + 1, buf,
10463
0
          count_len + 1);
10464
0
        lh->count++;
10465
0
        break;
10466
0
      }
10467
0
  }
10468
0
      elfsym->st_name
10469
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10470
0
                 versioned_name, false);
10471
0
      if (elfsym->st_name == (unsigned long) -1)
10472
0
  return 0;
10473
0
    }
10474
10475
0
  hash_table = elf_hash_table (flinfo->info);
10476
0
  strtabsize = hash_table->strtabsize;
10477
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10478
0
    {
10479
0
      strtabsize += strtabsize;
10480
0
      hash_table->strtabsize = strtabsize;
10481
0
      strtabsize *= sizeof (*hash_table->strtab);
10482
0
      hash_table->strtab = bfd_realloc (hash_table->strtab, strtabsize);
10483
0
      if (hash_table->strtab == NULL)
10484
0
  return 0;
10485
0
    }
10486
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10487
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10488
0
    = flinfo->output_bfd->symcount;
10489
0
  flinfo->output_bfd->symcount += 1;
10490
10491
0
  return 1;
10492
0
}
10493
10494
/* Swap symbols out to the symbol table and flush the output symbols to
10495
   the file.  */
10496
10497
static bool
10498
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10499
0
{
10500
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10501
0
  size_t amt;
10502
0
  size_t i;
10503
0
  elf_backend_data *bed;
10504
0
  bfd_byte *symbuf;
10505
0
  Elf_Internal_Shdr *hdr;
10506
0
  file_ptr pos;
10507
0
  bool ret;
10508
10509
0
  if (flinfo->output_bfd->symcount == 0)
10510
0
    return true;
10511
10512
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10513
10514
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10515
10516
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10517
0
  symbuf = bfd_malloc (amt);
10518
0
  if (symbuf == NULL)
10519
0
    return false;
10520
10521
0
  if (flinfo->symshndxbuf)
10522
0
    {
10523
0
      amt = sizeof (Elf_External_Sym_Shndx);
10524
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10525
0
      flinfo->symshndxbuf = bfd_zmalloc (amt);
10526
0
      if (flinfo->symshndxbuf == NULL)
10527
0
  {
10528
0
    free (symbuf);
10529
0
    return false;
10530
0
  }
10531
0
    }
10532
10533
  /* Now swap out the symbols.  */
10534
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10535
0
    {
10536
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10537
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10538
0
  elfsym->sym.st_name = 0;
10539
0
      else
10540
0
  elfsym->sym.st_name
10541
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10542
0
                elfsym->sym.st_name);
10543
10544
      /* Inform the linker of the addition of this symbol.  */
10545
10546
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10547
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10548
0
             &elfsym->sym);
10549
10550
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10551
0
             ((bfd_byte *) symbuf
10552
0
        + (elfsym->dest_index
10553
0
           * bed->s->sizeof_sym)),
10554
0
             NPTR_ADD (flinfo->symshndxbuf,
10555
0
           elfsym->dest_index));
10556
0
    }
10557
10558
0
  hdr = &elf_symtab_hdr (flinfo->output_bfd);
10559
0
  pos = hdr->sh_offset + hdr->sh_size;
10560
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10561
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10562
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10563
0
    {
10564
0
      hdr->sh_size += amt;
10565
0
      ret = true;
10566
0
    }
10567
0
  else
10568
0
    ret = false;
10569
10570
0
  free (symbuf);
10571
0
  return ret;
10572
0
}
10573
10574
/* Return TRUE if the dynamic symbol SYM in OBFD is supported.  */
10575
10576
static bool
10577
check_dynsym (bfd *obfd, Elf_Internal_Sym *sym)
10578
0
{
10579
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10580
0
      && sym->st_shndx < SHN_LORESERVE)
10581
0
    {
10582
      /* The gABI doesn't support dynamic symbols in output sections
10583
   beyond 64k.  */
10584
0
      _bfd_error_handler
10585
  /* xgettext:c-format */
10586
0
  (_("%pB: too many sections: %d (>= %d)"),
10587
0
   obfd, bfd_count_sections (obfd), SHN_LORESERVE & 0xffff);
10588
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10589
0
      return false;
10590
0
    }
10591
0
  return true;
10592
0
}
10593
10594
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10595
   allowing an unsatisfied unversioned symbol in the DSO to match a
10596
   versioned symbol that would normally require an explicit version.
10597
   We also handle the case that a DSO references a hidden symbol
10598
   which may be satisfied by a versioned symbol in another DSO.  */
10599
10600
static bool
10601
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10602
         elf_backend_data *bed,
10603
         struct elf_link_hash_entry *h)
10604
0
{
10605
0
  bfd *abfd;
10606
0
  struct elf_link_loaded_list *loaded;
10607
10608
0
  if (!is_elf_hash_table (info->hash))
10609
0
    return false;
10610
10611
  /* Check indirect symbol.  */
10612
0
  while (h->root.type == bfd_link_hash_indirect)
10613
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10614
10615
0
  switch (h->root.type)
10616
0
    {
10617
0
    default:
10618
0
      abfd = NULL;
10619
0
      break;
10620
10621
0
    case bfd_link_hash_undefined:
10622
0
    case bfd_link_hash_undefweak:
10623
0
      abfd = h->root.u.undef.abfd;
10624
0
      if (abfd == NULL
10625
0
    || (abfd->flags & DYNAMIC) == 0
10626
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10627
0
  return false;
10628
0
      break;
10629
10630
0
    case bfd_link_hash_defined:
10631
0
    case bfd_link_hash_defweak:
10632
0
      abfd = h->root.u.def.section->owner;
10633
0
      break;
10634
10635
0
    case bfd_link_hash_common:
10636
0
      abfd = h->root.u.c.p->section->owner;
10637
0
      break;
10638
0
    }
10639
0
  BFD_ASSERT (abfd != NULL);
10640
10641
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10642
0
       loaded != NULL;
10643
0
       loaded = loaded->next)
10644
0
    {
10645
0
      bfd *input;
10646
0
      Elf_Internal_Shdr *hdr;
10647
0
      size_t symcount;
10648
0
      size_t extsymcount;
10649
0
      size_t extsymoff;
10650
0
      Elf_Internal_Shdr *versymhdr;
10651
0
      Elf_Internal_Sym *isym;
10652
0
      Elf_Internal_Sym *isymend;
10653
0
      Elf_Internal_Sym *isymbuf;
10654
0
      Elf_External_Versym *ever;
10655
0
      Elf_External_Versym *extversym;
10656
10657
0
      input = loaded->abfd;
10658
10659
      /* We check each DSO for a possible hidden versioned definition.  */
10660
0
      if (input == abfd
10661
0
    || elf_dynversym (input) == 0)
10662
0
  continue;
10663
10664
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10665
10666
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10667
0
      if (elf_bad_symtab (input))
10668
0
  {
10669
0
    extsymcount = symcount;
10670
0
    extsymoff = 0;
10671
0
  }
10672
0
      else
10673
0
  {
10674
0
    extsymcount = symcount - hdr->sh_info;
10675
0
    extsymoff = hdr->sh_info;
10676
0
  }
10677
10678
0
      if (extsymcount == 0)
10679
0
  continue;
10680
10681
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10682
0
              NULL, NULL, NULL);
10683
0
      if (isymbuf == NULL)
10684
0
  return false;
10685
10686
      /* Read in any version definitions.  */
10687
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10688
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10689
0
    || (extversym = _bfd_malloc_and_read (input, versymhdr->sh_size,
10690
0
            versymhdr->sh_size)) == NULL)
10691
0
  {
10692
0
    free (isymbuf);
10693
0
    return false;
10694
0
  }
10695
10696
0
      ever = extversym + extsymoff;
10697
0
      isymend = isymbuf + extsymcount;
10698
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10699
0
  {
10700
0
    const char *name;
10701
0
    Elf_Internal_Versym iver;
10702
0
    unsigned short version_index;
10703
10704
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10705
0
        || isym->st_shndx == SHN_UNDEF)
10706
0
      continue;
10707
10708
0
    name = bfd_elf_string_from_elf_section (input,
10709
0
              hdr->sh_link,
10710
0
              isym->st_name);
10711
0
    if (strcmp (name, h->root.root.string) != 0)
10712
0
      continue;
10713
10714
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10715
10716
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10717
0
        && !(h->def_regular
10718
0
       && h->forced_local))
10719
0
      {
10720
        /* If we have a non-hidden versioned sym, then it should
10721
     have provided a definition for the undefined sym unless
10722
     it is defined in a non-shared object and forced local.
10723
         */
10724
0
        abort ();
10725
0
      }
10726
10727
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10728
0
    if (version_index == 1 || version_index == 2)
10729
0
      {
10730
        /* This is the base or first version.  We can use it.  */
10731
0
        free (extversym);
10732
0
        free (isymbuf);
10733
0
        return true;
10734
0
      }
10735
0
  }
10736
10737
0
      free (extversym);
10738
0
      free (isymbuf);
10739
0
    }
10740
10741
0
  return false;
10742
0
}
10743
10744
/* Convert ELF common symbol TYPE.  */
10745
10746
static int
10747
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10748
0
{
10749
  /* Commom symbol can only appear in relocatable link.  */
10750
0
  if (!bfd_link_relocatable (info))
10751
0
    abort ();
10752
0
  switch (info->elf_stt_common)
10753
0
    {
10754
0
    case unchanged:
10755
0
      break;
10756
0
    case elf_stt_common:
10757
0
      type = STT_COMMON;
10758
0
      break;
10759
0
    case no_elf_stt_common:
10760
0
      type = STT_OBJECT;
10761
0
      break;
10762
0
    }
10763
0
  return type;
10764
0
}
10765
10766
/* Add an external symbol to the symbol table.  This is called from
10767
   the hash table traversal routine.  When generating a shared object,
10768
   we go through the symbol table twice.  The first time we output
10769
   anything that might have been forced to local scope in a version
10770
   script.  The second time we output the symbols that are still
10771
   global symbols.  */
10772
10773
static bool
10774
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10775
0
{
10776
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10777
0
  struct elf_outext_info *eoinfo = data;
10778
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10779
0
  bool strip;
10780
0
  Elf_Internal_Sym sym;
10781
0
  asection *input_sec;
10782
0
  elf_backend_data *bed;
10783
0
  long indx;
10784
0
  int ret;
10785
0
  unsigned int type;
10786
10787
  /* Skip if base symbol doesn't match.  */
10788
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10789
0
    return true;
10790
10791
0
  if (h->root.type == bfd_link_hash_warning)
10792
0
    {
10793
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10794
0
      if (h->root.type == bfd_link_hash_new)
10795
0
  return true;
10796
0
    }
10797
10798
  /* Decide whether to output this symbol in this pass.  */
10799
0
  if (eoinfo->localsyms)
10800
0
    {
10801
0
      if (!h->forced_local)
10802
0
  return true;
10803
0
    }
10804
0
  else
10805
0
    {
10806
0
      if (h->forced_local)
10807
0
  return true;
10808
0
    }
10809
10810
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10811
10812
0
  if (h->root.type == bfd_link_hash_undefined)
10813
0
    {
10814
      /* If we have an undefined symbol reference here then it must have
10815
   come from a shared library that is being linked in.  (Undefined
10816
   references in regular files have already been handled unless
10817
   they are in unreferenced sections which are removed by garbage
10818
   collection).  */
10819
0
      bool ignore_undef = false;
10820
10821
      /* Some symbols may be special in that the fact that they're
10822
   undefined can be safely ignored - let backend determine that.  */
10823
0
      if (bed->elf_backend_ignore_undef_symbol)
10824
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10825
10826
      /* If we are reporting errors for this situation then do so now.  */
10827
0
      if (!ignore_undef
10828
0
    && h->ref_dynamic_nonweak
10829
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10830
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10831
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10832
0
  {
10833
0
    flinfo->info->callbacks->undefined_symbol
10834
0
      (flinfo->info, h->root.root.string,
10835
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10836
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10837
0
       && !flinfo->info->warn_unresolved_syms);
10838
0
  }
10839
10840
      /* Strip a global symbol defined in a discarded section.  */
10841
0
      if (h->indx == -3)
10842
0
  return true;
10843
0
    }
10844
10845
  /* We should also warn if a forced local symbol is referenced from
10846
     shared libraries.  */
10847
0
  if (bfd_link_executable (flinfo->info)
10848
0
      && h->forced_local
10849
0
      && h->ref_dynamic
10850
0
      && h->def_regular
10851
0
      && !h->dynamic_def
10852
0
      && h->ref_dynamic_nonweak
10853
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10854
0
    {
10855
0
      bfd *def_bfd;
10856
0
      const char *msg;
10857
0
      struct elf_link_hash_entry *hi = h;
10858
10859
      /* Check indirect symbol.  */
10860
0
      while (hi->root.type == bfd_link_hash_indirect)
10861
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10862
10863
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10864
  /* xgettext:c-format */
10865
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10866
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10867
  /* xgettext:c-format */
10868
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10869
0
      else
10870
  /* xgettext:c-format */
10871
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10872
0
      def_bfd = flinfo->output_bfd;
10873
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10874
0
  def_bfd = hi->root.u.def.section->owner;
10875
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10876
0
        h->root.root.string, def_bfd);
10877
0
      bfd_set_error (bfd_error_bad_value);
10878
0
      eoinfo->failed = true;
10879
0
      return false;
10880
0
    }
10881
10882
  /* We don't want to output symbols that have never been mentioned by
10883
     a regular file, or that we have been told to strip.  However, if
10884
     h->indx is set to -2, the symbol is used by a reloc and we must
10885
     output it.  */
10886
0
  strip = false;
10887
0
  if (h->indx == -2)
10888
0
    ;
10889
0
  else if ((h->def_dynamic
10890
0
      || h->ref_dynamic
10891
0
      || h->root.type == bfd_link_hash_new)
10892
0
     && !h->def_regular
10893
0
     && !h->ref_regular)
10894
0
    strip = true;
10895
0
  else if (flinfo->info->strip == strip_all)
10896
0
    strip = true;
10897
0
  else if (flinfo->info->strip == strip_some
10898
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10899
0
             h->root.root.string, false, false) == NULL)
10900
0
    strip = true;
10901
0
  else if ((h->root.type == bfd_link_hash_defined
10902
0
      || h->root.type == bfd_link_hash_defweak)
10903
0
     && ((flinfo->info->strip_discarded
10904
0
    && discarded_section (h->root.u.def.section))
10905
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10906
0
       && h->root.u.def.section->owner != NULL
10907
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10908
0
    strip = true;
10909
0
  else if ((h->root.type == bfd_link_hash_undefined
10910
0
      || h->root.type == bfd_link_hash_undefweak)
10911
0
     && h->root.u.undef.abfd != NULL
10912
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10913
0
    strip = true;
10914
10915
  /* Remember if this symbol should be stripped.  */
10916
0
  bool should_strip = strip;
10917
10918
  /* Strip undefined weak symbols link if they don't have relocation.  */
10919
0
  if (!strip)
10920
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10921
10922
0
  type = h->type;
10923
10924
  /* If we're stripping it, and it's not a dynamic symbol, there's
10925
     nothing else to do.   However, if it is a forced local symbol or
10926
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10927
     function a chance to make it dynamic.  */
10928
0
  if (strip
10929
0
      && h->dynindx == -1
10930
0
      && type != STT_GNU_IFUNC
10931
0
      && !h->forced_local)
10932
0
    return true;
10933
10934
0
  sym.st_value = 0;
10935
0
  sym.st_size = h->size;
10936
0
  sym.st_other = h->other;
10937
0
  switch (h->root.type)
10938
0
    {
10939
0
    default:
10940
0
    case bfd_link_hash_new:
10941
0
    case bfd_link_hash_warning:
10942
0
      abort ();
10943
0
      return false;
10944
10945
0
    case bfd_link_hash_undefined:
10946
0
    case bfd_link_hash_undefweak:
10947
0
      input_sec = bfd_und_section_ptr;
10948
0
      sym.st_shndx = SHN_UNDEF;
10949
0
      break;
10950
10951
0
    case bfd_link_hash_defined:
10952
0
    case bfd_link_hash_defweak:
10953
0
      {
10954
0
  input_sec = h->root.u.def.section;
10955
0
  if (input_sec->output_section != NULL)
10956
0
    {
10957
0
      sym.st_shndx =
10958
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10959
0
             input_sec->output_section);
10960
0
      if (sym.st_shndx == SHN_BAD)
10961
0
        {
10962
0
    _bfd_error_handler
10963
      /* xgettext:c-format */
10964
0
      (_("%pB: could not find output section %pA for input section %pA"),
10965
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10966
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10967
0
    eoinfo->failed = true;
10968
0
    return false;
10969
0
        }
10970
10971
      /* ELF symbols in relocatable files are section relative,
10972
         but in nonrelocatable files they are virtual
10973
         addresses.  */
10974
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10975
0
      if (!bfd_link_relocatable (flinfo->info))
10976
0
        {
10977
0
    sym.st_value += input_sec->output_section->vma;
10978
0
    if (h->type == STT_TLS)
10979
0
      {
10980
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10981
0
        if (tls_sec != NULL)
10982
0
          sym.st_value -= tls_sec->vma;
10983
0
      }
10984
0
        }
10985
0
    }
10986
0
  else
10987
0
    {
10988
0
      BFD_ASSERT (input_sec->owner == NULL
10989
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10990
0
      sym.st_shndx = SHN_UNDEF;
10991
0
      input_sec = bfd_und_section_ptr;
10992
0
    }
10993
0
      }
10994
0
      break;
10995
10996
0
    case bfd_link_hash_common:
10997
0
      input_sec = h->root.u.c.p->section;
10998
0
      sym.st_shndx = bed->common_section_index (input_sec);
10999
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
11000
0
      break;
11001
11002
0
    case bfd_link_hash_indirect:
11003
      /* These symbols are created by symbol versioning.  They point
11004
   to the decorated version of the name.  For example, if the
11005
   symbol foo@@GNU_1.2 is the default, which should be used when
11006
   foo is used with no version, then we add an indirect symbol
11007
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
11008
   since the indirected symbol is already in the hash table.  */
11009
0
      return true;
11010
0
    }
11011
11012
0
  if (type == STT_COMMON || type == STT_OBJECT)
11013
0
    switch (h->root.type)
11014
0
      {
11015
0
      case bfd_link_hash_common:
11016
0
  type = elf_link_convert_common_type (flinfo->info, type);
11017
0
  break;
11018
0
      case bfd_link_hash_defined:
11019
0
      case bfd_link_hash_defweak:
11020
0
  if (bed->common_definition (&sym))
11021
0
    type = elf_link_convert_common_type (flinfo->info, type);
11022
0
  else
11023
0
    type = STT_OBJECT;
11024
0
  break;
11025
0
      case bfd_link_hash_undefined:
11026
0
      case bfd_link_hash_undefweak:
11027
0
  break;
11028
0
      default:
11029
0
  abort ();
11030
0
      }
11031
11032
0
  if (h->forced_local)
11033
0
    {
11034
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
11035
      /* Turn off visibility on local symbol.  */
11036
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11037
0
    }
11038
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
11039
0
  else if (h->unique_global && h->def_regular)
11040
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
11041
0
  else if (h->root.type == bfd_link_hash_undefweak
11042
0
     || h->root.type == bfd_link_hash_defweak)
11043
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
11044
0
  else
11045
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
11046
0
  sym.st_target_internal = h->target_internal;
11047
11048
  /* Give the processor backend a chance to tweak the symbol value,
11049
     and also to finish up anything that needs to be done for this
11050
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
11051
     forced local syms when non-shared is due to a historical quirk.
11052
     STT_GNU_IFUNC symbol must go through PLT.  */
11053
0
  if (((h->type == STT_GNU_IFUNC
11054
0
  && h->def_regular
11055
0
  && !bfd_link_relocatable (flinfo->info))
11056
0
       || ((h->dynindx != -1
11057
0
      || h->forced_local)
11058
0
     && ((bfd_link_pic (flinfo->info)
11059
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11060
0
        || h->root.type != bfd_link_hash_undefweak))
11061
0
         || !h->forced_local)
11062
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11063
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11064
0
    {
11065
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->info, h, &sym))
11066
0
  {
11067
0
    eoinfo->failed = true;
11068
0
    return false;
11069
0
  }
11070
      /* If a symbol is in the dynamic symbol table and isn't a
11071
   should-strip symbol, also keep it in the symbol table.  */
11072
0
      if (!should_strip)
11073
0
  strip = false;
11074
0
    }
11075
11076
  /* If we are marking the symbol as undefined, and there are no
11077
     non-weak references to this symbol from a regular object, then
11078
     mark the symbol as weak undefined; if there are non-weak
11079
     references, mark the symbol as strong.  We can't do this earlier,
11080
     because it might not be marked as undefined until the
11081
     finish_dynamic_symbol routine gets through with it.  */
11082
0
  if (sym.st_shndx == SHN_UNDEF
11083
0
      && h->ref_regular
11084
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11085
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11086
0
    {
11087
0
      int bindtype;
11088
0
      type = ELF_ST_TYPE (sym.st_info);
11089
11090
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11091
0
      if (type == STT_GNU_IFUNC)
11092
0
  type = STT_FUNC;
11093
11094
0
      if (h->ref_regular_nonweak)
11095
0
  bindtype = STB_GLOBAL;
11096
0
      else
11097
0
  bindtype = STB_WEAK;
11098
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11099
0
    }
11100
11101
  /* If this is a symbol defined in a dynamic library, don't use the
11102
     symbol size from the dynamic library.  Relinking an executable
11103
     against a new library may introduce gratuitous changes in the
11104
     executable's symbols if we keep the size.  */
11105
0
  if (sym.st_shndx == SHN_UNDEF
11106
0
      && !h->def_regular
11107
0
      && h->def_dynamic)
11108
0
    sym.st_size = 0;
11109
11110
  /* If a non-weak symbol with non-default visibility is not defined
11111
     locally, it is a fatal error.  */
11112
0
  if (!bfd_link_relocatable (flinfo->info)
11113
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11114
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11115
0
      && h->root.type == bfd_link_hash_undefined
11116
0
      && !h->def_regular)
11117
0
    {
11118
0
      const char *msg;
11119
11120
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11121
  /* xgettext:c-format */
11122
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11123
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11124
  /* xgettext:c-format */
11125
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11126
0
      else
11127
  /* xgettext:c-format */
11128
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11129
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11130
0
      bfd_set_error (bfd_error_bad_value);
11131
0
      eoinfo->failed = true;
11132
0
      return false;
11133
0
    }
11134
11135
  /* If this symbol should be put in the .dynsym section, then put it
11136
     there now.  We already know the symbol index.  We also fill in
11137
     the entry in the .hash section.  */
11138
0
  if (h->dynindx != -1
11139
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11140
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11141
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11142
0
    {
11143
0
      bfd_byte *esym;
11144
11145
      /* Since there is no version information in the dynamic string,
11146
   if there is no version info in symbol version section, we will
11147
   have a run-time problem if not linking executable, referenced
11148
   by shared library, or not bound locally.  */
11149
0
      if (h->verinfo.verdef == NULL
11150
0
    && (!bfd_link_executable (flinfo->info)
11151
0
        || h->ref_dynamic
11152
0
        || !h->def_regular))
11153
0
  {
11154
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11155
11156
0
    if (p && p [1] != '\0')
11157
0
      {
11158
0
        _bfd_error_handler
11159
    /* xgettext:c-format */
11160
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11161
0
     flinfo->output_bfd, h->root.root.string);
11162
0
        eoinfo->failed = true;
11163
0
        return false;
11164
0
      }
11165
0
  }
11166
11167
0
      sym.st_name = h->dynstr_index;
11168
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11169
0
        + h->dynindx * bed->s->sizeof_sym);
11170
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11171
0
  {
11172
0
    eoinfo->failed = true;
11173
0
    return false;
11174
0
  }
11175
11176
      /* Inform the linker of the addition of this symbol.  */
11177
11178
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11179
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11180
11181
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11182
11183
0
      if (flinfo->hash_sec != NULL)
11184
0
  {
11185
0
    size_t hash_entry_size;
11186
0
    bfd_byte *bucketpos;
11187
0
    bfd_vma chain;
11188
0
    size_t bucketcount;
11189
0
    size_t bucket;
11190
11191
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11192
0
    bucket = h->u.elf_hash_value % bucketcount;
11193
11194
0
    hash_entry_size
11195
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11196
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11197
0
           + (bucket + 2) * hash_entry_size);
11198
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11199
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11200
0
       bucketpos);
11201
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11202
0
       ((bfd_byte *) flinfo->hash_sec->contents
11203
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11204
0
  }
11205
11206
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11207
0
  {
11208
0
    Elf_Internal_Versym iversym;
11209
0
    Elf_External_Versym *eversym;
11210
11211
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11212
0
      {
11213
0
        if (h->verinfo.verdef == NULL
11214
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11215
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11216
0
    {
11217
0
      iversym.vs_vers = 1;
11218
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11219
        /* Referenced symbol without ELF_VER_CHR has no
11220
           version.  */
11221
0
        iversym.vs_vers = 0;
11222
0
    }
11223
0
        else
11224
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11225
0
      }
11226
0
    else
11227
0
      {
11228
0
        if (h->verinfo.vertree == NULL)
11229
0
    iversym.vs_vers = 1;
11230
0
        else
11231
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11232
0
        if (flinfo->info->create_default_symver)
11233
0
    iversym.vs_vers++;
11234
11235
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11236
     symbol is defined locally.  */
11237
0
        if (h->versioned == versioned_hidden && h->def_regular)
11238
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11239
0
      }
11240
11241
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11242
0
    eversym += h->dynindx;
11243
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11244
0
  }
11245
0
    }
11246
11247
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11248
     strip it from .symtab too.  Obviously we can't do this for
11249
     relocatable output or when needed for --emit-relocs.  */
11250
0
  else if (input_sec == bfd_und_section_ptr
11251
0
     && h->indx != -2
11252
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11253
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11254
0
     && !bfd_link_relocatable (flinfo->info))
11255
0
    return true;
11256
11257
  /* Also strip others that we couldn't earlier due to dynamic symbol
11258
     processing.  */
11259
0
  if (strip)
11260
0
    return true;
11261
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11262
0
    return true;
11263
11264
  /* Output a FILE symbol so that following locals are not associated
11265
     with the wrong input file.  We need one for forced local symbols
11266
     if we've seen more than one FILE symbol or when we have exactly
11267
     one FILE symbol but global symbols are present in a file other
11268
     than the one with the FILE symbol.  We also need one if linker
11269
     defined symbols are present.  In practice these conditions are
11270
     always met, so just emit the FILE symbol unconditionally.  */
11271
0
  if (eoinfo->localsyms
11272
0
      && !eoinfo->file_sym_done
11273
0
      && eoinfo->flinfo->filesym_count != 0)
11274
0
    {
11275
0
      Elf_Internal_Sym fsym;
11276
11277
0
      memset (&fsym, 0, sizeof (fsym));
11278
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11279
0
      fsym.st_shndx = SHN_ABS;
11280
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11281
0
              bfd_und_section_ptr, NULL))
11282
0
  return false;
11283
11284
0
      eoinfo->file_sym_done = true;
11285
0
    }
11286
11287
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11288
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11289
0
           input_sec, h);
11290
0
  if (ret == 0)
11291
0
    {
11292
0
      eoinfo->failed = true;
11293
0
      return false;
11294
0
    }
11295
0
  else if (ret == 1)
11296
0
    h->indx = indx;
11297
0
  else if (h->indx == -2)
11298
0
    abort();
11299
11300
0
  return true;
11301
0
}
11302
11303
/* Return TRUE if special handling is done for relocs in SEC against
11304
   symbols defined in discarded sections.  */
11305
11306
static bool
11307
elf_section_ignore_discarded_relocs (asection *sec)
11308
0
{
11309
0
  elf_backend_data *bed;
11310
11311
0
  switch (sec->sec_info_type)
11312
0
    {
11313
0
    case SEC_INFO_TYPE_STABS:
11314
0
    case SEC_INFO_TYPE_EH_FRAME:
11315
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11316
0
    case SEC_INFO_TYPE_SFRAME:
11317
0
      return true;
11318
0
    default:
11319
0
      break;
11320
0
    }
11321
11322
0
  bed = get_elf_backend_data (sec->owner);
11323
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11324
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11325
0
    return true;
11326
11327
0
  return false;
11328
0
}
11329
11330
/* Return a mask saying how ld should treat relocations in SEC against
11331
   symbols defined in discarded sections.  If this function returns
11332
   COMPLAIN set, ld will issue a warning message.  If this function
11333
   returns PRETEND set, and the discarded section was link-once and the
11334
   same size as the kept link-once section, ld will pretend that the
11335
   symbol was actually defined in the kept section.  Otherwise ld will
11336
   zero the reloc (at least that is the intent, but some cooperation by
11337
   the target dependent code is needed, particularly for REL targets).  */
11338
11339
unsigned int
11340
_bfd_elf_default_action_discarded (asection *sec)
11341
0
{
11342
0
  if (sec->flags & SEC_DEBUGGING)
11343
0
    return PRETEND;
11344
11345
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11346
0
    return 0;
11347
11348
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11349
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11350
0
    return 0;
11351
11352
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11353
0
    return 0;
11354
11355
0
  return COMPLAIN | PRETEND;
11356
0
}
11357
11358
/* Find a match between a section and a member of a section group.  */
11359
11360
static asection *
11361
match_group_member (asection *sec, asection *group,
11362
        struct bfd_link_info *info)
11363
0
{
11364
0
  asection *first = elf_next_in_group (group);
11365
0
  asection *s = first;
11366
11367
0
  while (s != NULL)
11368
0
    {
11369
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11370
0
  return s;
11371
11372
0
      s = elf_next_in_group (s);
11373
0
      if (s == first)
11374
0
  break;
11375
0
    }
11376
11377
0
  return NULL;
11378
0
}
11379
11380
/* Check if the kept section of a discarded section SEC can be used
11381
   to replace it.  Return the replacement if it is OK.  Otherwise return
11382
   NULL.  */
11383
11384
asection *
11385
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11386
0
{
11387
0
  asection *kept;
11388
11389
0
  kept = sec->kept_section;
11390
0
  if (kept != NULL)
11391
0
    {
11392
0
      if ((kept->flags & SEC_GROUP) != 0)
11393
0
  kept = match_group_member (sec, kept, info);
11394
0
      if (kept != NULL)
11395
0
  {
11396
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11397
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11398
0
      kept = NULL;
11399
0
    else
11400
0
      {
11401
        /* Get the real kept section.  */
11402
0
        asection *next;
11403
0
        for (next = kept->kept_section;
11404
0
       next != NULL;
11405
0
       next = next->kept_section)
11406
0
    kept = next;
11407
0
      }
11408
0
  }
11409
0
      sec->kept_section = kept;
11410
0
    }
11411
0
  return kept;
11412
0
}
11413
11414
/* Link an input file into the linker output file.  This function
11415
   handles all the sections and relocations of the input file at once.
11416
   This is so that we only have to read the local symbols once, and
11417
   don't have to keep them in memory.  */
11418
11419
static bool
11420
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11421
0
{
11422
0
  int (*relocate_section)
11423
0
    (struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11424
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11425
0
  bfd *output_bfd;
11426
0
  Elf_Internal_Shdr *symtab_hdr;
11427
0
  size_t locsymcount;
11428
0
  size_t extsymoff;
11429
0
  size_t num_sym;
11430
0
  Elf_Internal_Sym *isymbuf;
11431
0
  Elf_Internal_Sym *isym;
11432
0
  Elf_Internal_Sym *isymend;
11433
0
  long *pindex;
11434
0
  asection **ppsection;
11435
0
  asection *o;
11436
0
  elf_backend_data *bed;
11437
0
  struct elf_link_hash_entry **sym_hashes;
11438
0
  bfd_size_type address_size;
11439
0
  bfd_vma r_type_mask;
11440
0
  int r_sym_shift;
11441
0
  bool have_file_sym = false;
11442
11443
0
  output_bfd = flinfo->output_bfd;
11444
0
  bed = get_elf_backend_data (output_bfd);
11445
0
  relocate_section = bed->elf_backend_relocate_section;
11446
11447
  /* If this is a dynamic object, we don't want to do anything here:
11448
     we don't want the local symbols, and we don't want the section
11449
     contents.  */
11450
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11451
0
    return true;
11452
11453
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11454
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11455
0
  if (elf_bad_symtab (input_bfd))
11456
0
    {
11457
0
      locsymcount = num_sym;
11458
0
      extsymoff = 0;
11459
0
    }
11460
0
  else
11461
0
    {
11462
0
      locsymcount = symtab_hdr->sh_info;
11463
0
      extsymoff = symtab_hdr->sh_info;
11464
0
    }
11465
11466
  /* Enable GNU OSABI features in the output BFD that are used in the input
11467
     BFD.  */
11468
0
  if (bed->elf_osabi == ELFOSABI_NONE
11469
0
      || bed->elf_osabi == ELFOSABI_GNU
11470
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11471
0
    elf_tdata (output_bfd)->has_gnu_osabi
11472
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11473
0
    & (bfd_link_relocatable (flinfo->info)
11474
0
       ? -1 : ~elf_gnu_osabi_retain));
11475
11476
  /* Read the local symbols.  */
11477
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11478
0
  if (isymbuf == NULL && locsymcount != 0)
11479
0
    {
11480
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11481
0
              flinfo->internal_syms,
11482
0
              flinfo->external_syms,
11483
0
              flinfo->locsym_shndx);
11484
0
      if (isymbuf == NULL)
11485
0
  return false;
11486
0
    }
11487
11488
  /* Find local symbol sections and adjust values of symbols in
11489
     SEC_MERGE sections.  Write out those local symbols we know are
11490
     going into the output file.  */
11491
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11492
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11493
0
       isym < isymend;
11494
0
       isym++, pindex++, ppsection++)
11495
0
    {
11496
0
      asection *isec;
11497
0
      const char *name;
11498
0
      Elf_Internal_Sym osym;
11499
0
      long indx;
11500
0
      int ret;
11501
11502
0
      *pindex = -1;
11503
11504
0
      if (elf_bad_symtab (input_bfd))
11505
0
  {
11506
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11507
0
      {
11508
0
        *ppsection = NULL;
11509
0
        continue;
11510
0
      }
11511
0
  }
11512
11513
0
      if (isym->st_shndx == SHN_UNDEF)
11514
0
  isec = bfd_und_section_ptr;
11515
0
      else if (isym->st_shndx == SHN_ABS)
11516
0
  isec = bfd_abs_section_ptr;
11517
0
      else if (isym->st_shndx == SHN_COMMON)
11518
0
  isec = bfd_com_section_ptr;
11519
0
      else
11520
0
  {
11521
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11522
0
    if (isec == NULL)
11523
0
      {
11524
        /* Don't attempt to output symbols with st_shnx in the
11525
     reserved range other than SHN_ABS and SHN_COMMON.  */
11526
0
        isec = bfd_und_section_ptr;
11527
0
      }
11528
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11529
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11530
0
      isym->st_value =
11531
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11532
0
  }
11533
11534
0
      *ppsection = isec;
11535
11536
      /* Don't output the first, undefined, symbol.  In fact, don't
11537
   output any undefined local symbol.  */
11538
0
      if (isec == bfd_und_section_ptr)
11539
0
  continue;
11540
11541
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11542
0
  {
11543
    /* We never output section symbols.  Instead, we use the
11544
       section symbol of the corresponding section in the output
11545
       file.  */
11546
0
    continue;
11547
0
  }
11548
11549
      /* If we are stripping all symbols, we don't want to output this
11550
   one.  */
11551
0
      if (flinfo->info->strip == strip_all)
11552
0
  continue;
11553
11554
      /* If we are discarding all local symbols, we don't want to
11555
   output this one.  If we are generating a relocatable output
11556
   file, then some of the local symbols may be required by
11557
   relocs; we output them below as we discover that they are
11558
   needed.  */
11559
0
      if (flinfo->info->discard == discard_all)
11560
0
  continue;
11561
11562
      /* If this symbol is defined in a section which we are
11563
   discarding, we don't need to keep it.  */
11564
0
      if (isym->st_shndx < SHN_LORESERVE
11565
0
    && (isec->output_section == NULL
11566
0
        || bfd_section_removed_from_list (output_bfd,
11567
0
            isec->output_section)))
11568
0
  continue;
11569
11570
      /* Get the name of the symbol.  */
11571
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11572
0
                isym->st_name);
11573
0
      if (name == NULL)
11574
0
  return false;
11575
11576
      /* See if we are discarding symbols with this name.  */
11577
0
      if ((flinfo->info->strip == strip_some
11578
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11579
0
         == NULL))
11580
0
    || (((flinfo->info->discard == discard_sec_merge
11581
0
    && (isec->flags & SEC_MERGE)
11582
0
    && !bfd_link_relocatable (flinfo->info))
11583
0
         || flinfo->info->discard == discard_l)
11584
0
        && bfd_is_local_label_name (input_bfd, name)))
11585
0
  continue;
11586
11587
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11588
0
  {
11589
0
    if (input_bfd->lto_output)
11590
      /* -flto puts a temp file name here.  This means builds
11591
         are not reproducible.  Discard the symbol.  */
11592
0
      continue;
11593
0
    have_file_sym = true;
11594
0
    flinfo->filesym_count += 1;
11595
0
  }
11596
0
      if (!have_file_sym)
11597
0
  {
11598
    /* In the absence of debug info, bfd_find_nearest_line uses
11599
       FILE symbols to determine the source file for local
11600
       function symbols.  Provide a FILE symbol here if input
11601
       files lack such, so that their symbols won't be
11602
       associated with a previous input file.  It's not the
11603
       source file, but the best we can do.  */
11604
0
    const char *filename;
11605
0
    have_file_sym = true;
11606
0
    flinfo->filesym_count += 1;
11607
0
    memset (&osym, 0, sizeof (osym));
11608
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11609
0
    osym.st_shndx = SHN_ABS;
11610
0
    if (input_bfd->lto_output)
11611
0
      filename = NULL;
11612
0
    else
11613
0
      filename = lbasename (bfd_get_filename (input_bfd));
11614
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11615
0
            bfd_abs_section_ptr, NULL))
11616
0
      return false;
11617
0
  }
11618
11619
0
      osym = *isym;
11620
11621
      /* Adjust the section index for the output file.  */
11622
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11623
0
               isec->output_section);
11624
0
      if (osym.st_shndx == SHN_BAD)
11625
0
  return false;
11626
11627
      /* ELF symbols in relocatable files are section relative, but
11628
   in executable files they are virtual addresses.  Note that
11629
   this code assumes that all ELF sections have an associated
11630
   BFD section with a reasonable value for output_offset; below
11631
   we assume that they also have a reasonable value for
11632
   output_section.  Any special sections must be set up to meet
11633
   these requirements.  */
11634
0
      osym.st_value += isec->output_offset;
11635
0
      if (!bfd_link_relocatable (flinfo->info))
11636
0
  {
11637
0
    osym.st_value += isec->output_section->vma;
11638
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11639
0
      {
11640
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11641
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11642
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11643
0
        else
11644
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11645
0
              STT_NOTYPE);
11646
0
      }
11647
0
  }
11648
11649
0
      indx = bfd_get_symcount (output_bfd);
11650
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11651
0
      if (ret == 0)
11652
0
  return false;
11653
0
      else if (ret == 1)
11654
0
  *pindex = indx;
11655
0
    }
11656
11657
0
  if (bed->s->arch_size == 32)
11658
0
    {
11659
0
      r_type_mask = 0xff;
11660
0
      r_sym_shift = 8;
11661
0
      address_size = 4;
11662
0
    }
11663
0
  else
11664
0
    {
11665
0
      r_type_mask = 0xffffffff;
11666
0
      r_sym_shift = 32;
11667
0
      address_size = 8;
11668
0
    }
11669
11670
  /* Relocate the contents of each section.  */
11671
0
  sym_hashes = elf_sym_hashes (input_bfd);
11672
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11673
0
    {
11674
0
      bfd_byte *contents;
11675
11676
0
      if (! o->linker_mark)
11677
0
  {
11678
    /* This section was omitted from the link.  */
11679
0
    continue;
11680
0
  }
11681
11682
0
      if (!flinfo->info->resolve_section_groups
11683
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11684
0
  {
11685
    /* Deal with the group signature symbol.  */
11686
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11687
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11688
0
    asection *osec = o->output_section;
11689
11690
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11691
0
    if (symndx >= locsymcount
11692
0
        || (elf_bad_symtab (input_bfd)
11693
0
      && flinfo->sections[symndx] == NULL))
11694
0
      {
11695
0
        struct elf_link_hash_entry *h;
11696
11697
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11698
0
            extsymoff, num_sym);
11699
0
        if (h == NULL)
11700
0
    {
11701
0
      _bfd_error_handler
11702
        /* xgettext:c-format */
11703
0
        (_("error: %pB: unable to create group section symbol"),
11704
0
         input_bfd);
11705
0
      bfd_set_error (bfd_error_bad_value);
11706
0
      return false;
11707
0
    }
11708
11709
        /* Arrange for symbol to be output.  */
11710
0
        h->indx = -2;
11711
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11712
0
      }
11713
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11714
0
      {
11715
        /* We'll use the output section target_index.  */
11716
0
        asection *sec = flinfo->sections[symndx]->output_section;
11717
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11718
0
      }
11719
0
    else
11720
0
      {
11721
0
        if (flinfo->indices[symndx] == -1)
11722
0
    {
11723
      /* Otherwise output the local symbol now.  */
11724
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11725
0
      asection *sec = flinfo->sections[symndx]->output_section;
11726
0
      const char *name;
11727
0
      long indx;
11728
0
      int ret;
11729
11730
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11731
0
                symtab_hdr->sh_link,
11732
0
                sym.st_name);
11733
0
      if (name == NULL)
11734
0
        return false;
11735
11736
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11737
0
                    sec);
11738
0
      if (sym.st_shndx == SHN_BAD)
11739
0
        return false;
11740
11741
0
      sym.st_value += o->output_offset;
11742
11743
0
      indx = bfd_get_symcount (output_bfd);
11744
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11745
0
               NULL);
11746
0
      if (ret == 0)
11747
0
        return false;
11748
0
      else if (ret == 1)
11749
0
        flinfo->indices[symndx] = indx;
11750
0
      else
11751
0
        abort ();
11752
0
    }
11753
0
        elf_section_data (osec)->this_hdr.sh_info
11754
0
    = flinfo->indices[symndx];
11755
0
      }
11756
0
  }
11757
11758
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11759
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11760
0
  continue;
11761
11762
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11763
0
  {
11764
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11765
       or somesuch.  */
11766
0
    continue;
11767
0
  }
11768
11769
      /* Get the contents of the section.  They have been cached by a
11770
   relaxation routine.  Note that o is a section in an input
11771
   file, so the contents field will not have been set by any of
11772
   the routines which work on output files.  */
11773
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11774
0
  {
11775
0
    contents = elf_section_data (o)->this_hdr.contents;
11776
0
    if (bed->caches_rawsize
11777
0
        && o->rawsize != 0
11778
0
        && o->rawsize < o->size)
11779
0
      {
11780
0
        memcpy (flinfo->contents, contents, o->rawsize);
11781
0
        contents = flinfo->contents;
11782
0
      }
11783
0
  }
11784
0
      else if (!(o->flags & SEC_RELOC)
11785
0
         && !bed->elf_backend_write_section
11786
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11787
  /* A MERGE section that has no relocations doesn't need the
11788
     contents anymore, they have been recorded earlier.  Except
11789
     if the backend has special provisions for writing sections.  */
11790
0
  contents = NULL;
11791
0
      else
11792
0
  {
11793
0
    contents = flinfo->contents;
11794
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11795
0
                 &contents))
11796
0
      return false;
11797
0
  }
11798
11799
0
      if ((o->flags & SEC_RELOC) != 0)
11800
0
  {
11801
0
    Elf_Internal_Rela *internal_relocs;
11802
0
    Elf_Internal_Rela *rel, *relend;
11803
0
    int action_discarded;
11804
0
    int ret;
11805
11806
    /* Get the swapped relocs.  */
11807
0
    internal_relocs
11808
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11809
0
                flinfo->external_relocs,
11810
0
                flinfo->internal_relocs,
11811
0
                false);
11812
0
    if (internal_relocs == NULL
11813
0
        && o->reloc_count > 0)
11814
0
      return false;
11815
11816
0
    action_discarded = -1;
11817
0
    if (!elf_section_ignore_discarded_relocs (o))
11818
0
      action_discarded = (*bed->action_discarded) (o);
11819
11820
    /* Run through the relocs evaluating complex reloc symbols and
11821
       looking for relocs against symbols from discarded sections
11822
       or section symbols from removed link-once sections.
11823
       Complain about relocs against discarded sections.  Zero
11824
       relocs against removed link-once sections.  */
11825
11826
0
    rel = internal_relocs;
11827
0
    relend = rel + o->reloc_count;
11828
0
    for ( ; rel < relend; rel++)
11829
0
      {
11830
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11831
0
        unsigned int s_type;
11832
0
        asection **ps, *sec;
11833
0
        struct elf_link_hash_entry *h = NULL;
11834
0
        const char *sym_name;
11835
11836
0
        if (r_symndx == STN_UNDEF)
11837
0
    continue;
11838
11839
0
        if (r_symndx >= locsymcount
11840
0
      || (elf_bad_symtab (input_bfd)
11841
0
          && flinfo->sections[r_symndx] == NULL))
11842
0
    {
11843
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11844
0
                extsymoff, num_sym);
11845
11846
      /* Badly formatted input files can contain relocs that
11847
         reference non-existant symbols.  Check here so that
11848
         we do not seg fault.  */
11849
0
      if (h == NULL)
11850
0
        {
11851
0
          _bfd_error_handler
11852
      /* xgettext:c-format */
11853
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11854
0
         "that references a non-existent global symbol"),
11855
0
       input_bfd, (uint64_t) rel->r_info, o);
11856
0
          bfd_set_error (bfd_error_bad_value);
11857
0
          return false;
11858
0
        }
11859
11860
0
      s_type = h->type;
11861
11862
      /* If a plugin symbol is referenced from a non-IR file,
11863
         mark the symbol as undefined.  Note that the
11864
         linker may attach linker created dynamic sections
11865
         to the plugin bfd.  Symbols defined in linker
11866
         created sections are not plugin symbols.  */
11867
0
      if ((h->root.non_ir_ref_regular
11868
0
           || h->root.non_ir_ref_dynamic)
11869
0
          && (h->root.type == bfd_link_hash_defined
11870
0
        || h->root.type == bfd_link_hash_defweak)
11871
0
          && (h->root.u.def.section->flags
11872
0
        & SEC_LINKER_CREATED) == 0
11873
0
          && h->root.u.def.section->owner != NULL
11874
0
          && (h->root.u.def.section->owner->flags
11875
0
        & BFD_PLUGIN) != 0)
11876
0
        {
11877
0
          h->root.type = bfd_link_hash_undefined;
11878
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11879
0
        }
11880
11881
0
      ps = NULL;
11882
0
      if (h->root.type == bfd_link_hash_defined
11883
0
          || h->root.type == bfd_link_hash_defweak)
11884
0
        ps = &h->root.u.def.section;
11885
11886
0
      sym_name = h->root.root.string;
11887
0
    }
11888
0
        else
11889
0
    {
11890
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11891
11892
0
      s_type = ELF_ST_TYPE (sym->st_info);
11893
0
      ps = &flinfo->sections[r_symndx];
11894
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11895
0
                 sym, *ps);
11896
0
    }
11897
11898
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11899
0
      && !bfd_link_relocatable (flinfo->info))
11900
0
    {
11901
0
      bfd_vma val;
11902
0
      bfd_vma dot = (rel->r_offset
11903
0
         + o->output_offset + o->output_section->vma);
11904
#ifdef DEBUG
11905
      printf ("Encountered a complex symbol!");
11906
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11907
        bfd_get_filename (input_bfd), o->name,
11908
        (long) (rel - internal_relocs));
11909
      printf (" symbol: idx  %8.8lx, name %s\n",
11910
        r_symndx, sym_name);
11911
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11912
        (unsigned long) rel->r_info,
11913
        (unsigned long) rel->r_offset);
11914
#endif
11915
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11916
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11917
0
        return false;
11918
11919
      /* Symbol evaluated OK.  Update to absolute value.  */
11920
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11921
0
           num_sym, r_symndx, val))
11922
0
        return false;
11923
11924
0
      continue;
11925
0
    }
11926
11927
0
        if (action_discarded != -1 && ps != NULL)
11928
0
    {
11929
      /* Complain if the definition comes from a
11930
         discarded section.  */
11931
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11932
0
        {
11933
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11934
0
          if (action_discarded & COMPLAIN)
11935
0
      (*flinfo->info->callbacks->einfo)
11936
        /* xgettext:c-format */
11937
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11938
0
           "defined in discarded section `%pA' of %pB\n"),
11939
0
         sym_name, o, input_bfd, sec, sec->owner);
11940
11941
          /* Try to do the best we can to support buggy old
11942
       versions of gcc.  Pretend that the symbol is
11943
       really defined in the kept linkonce section.
11944
       FIXME: This is quite broken.  Modifying the
11945
       symbol here means we will be changing all later
11946
       uses of the symbol, not just in this section.  */
11947
0
          if (action_discarded & PRETEND)
11948
0
      {
11949
0
        asection *kept;
11950
11951
0
        kept = _bfd_elf_check_kept_section (sec,
11952
0
                    flinfo->info);
11953
0
        if (kept != NULL)
11954
0
          {
11955
0
            *ps = kept;
11956
0
            continue;
11957
0
          }
11958
0
      }
11959
0
        }
11960
0
    }
11961
0
      }
11962
11963
    /* Relocate the section by invoking a back end routine.
11964
11965
       The back end routine is responsible for adjusting the
11966
       section contents as necessary, and (if using Rela relocs
11967
       and generating a relocatable output file) adjusting the
11968
       reloc addend as necessary.
11969
11970
       The back end routine does not have to worry about setting
11971
       the reloc address or the reloc symbol index.
11972
11973
       The back end routine is given a pointer to the swapped in
11974
       internal symbols, and can access the hash table entries
11975
       for the external symbols via elf_sym_hashes (input_bfd).
11976
11977
       When generating relocatable output, the back end routine
11978
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11979
       output symbol is going to be a section symbol
11980
       corresponding to the output section, which will require
11981
       the addend to be adjusted.  */
11982
11983
0
    ret = relocate_section (flinfo->info,
11984
0
          input_bfd, o, contents,
11985
0
          internal_relocs,
11986
0
          isymbuf,
11987
0
          flinfo->sections);
11988
0
    if (!ret)
11989
0
      return false;
11990
11991
0
    if (ret == 2
11992
0
        || bfd_link_relocatable (flinfo->info)
11993
0
        || flinfo->info->emitrelocations)
11994
0
      {
11995
0
        Elf_Internal_Rela *irela;
11996
0
        Elf_Internal_Rela *irelaend, *irelamid;
11997
0
        bfd_vma last_offset;
11998
0
        struct elf_link_hash_entry **rel_hash;
11999
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
12000
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
12001
0
        unsigned int next_erel;
12002
0
        bool rela_normal;
12003
0
        struct bfd_elf_section_data *esdi, *esdo;
12004
12005
0
        esdi = elf_section_data (o);
12006
0
        esdo = elf_section_data (o->output_section);
12007
0
        rela_normal = false;
12008
12009
        /* Adjust the reloc addresses and symbol indices.  */
12010
12011
0
        irela = internal_relocs;
12012
0
        irelaend = irela + o->reloc_count;
12013
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
12014
        /* We start processing the REL relocs, if any.  When we reach
12015
     IRELAMID in the loop, we switch to the RELA relocs.  */
12016
0
        irelamid = irela;
12017
0
        if (esdi->rel.hdr != NULL)
12018
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
12019
0
           * bed->s->int_rels_per_ext_rel);
12020
0
        rel_hash_list = rel_hash;
12021
0
        rela_hash_list = NULL;
12022
0
        last_offset = o->output_offset;
12023
0
        if (!bfd_link_relocatable (flinfo->info))
12024
0
    last_offset += o->output_section->vma;
12025
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
12026
0
    {
12027
0
      unsigned long r_symndx;
12028
0
      asection *sec;
12029
0
      Elf_Internal_Sym sym;
12030
12031
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
12032
0
        {
12033
0
          rel_hash++;
12034
0
          next_erel = 0;
12035
0
        }
12036
12037
0
      if (irela == irelamid)
12038
0
        {
12039
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
12040
0
          rela_hash_list = rel_hash;
12041
0
          rela_normal = bed->rela_normal;
12042
0
        }
12043
12044
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
12045
0
                   flinfo->info, o,
12046
0
                   irela->r_offset);
12047
0
      if (irela->r_offset >= (bfd_vma) -2)
12048
0
        {
12049
          /* This is a reloc for a deleted entry or somesuch.
12050
       Turn it into an R_*_NONE reloc, at the same
12051
       offset as the last reloc.  elf_eh_frame.c and
12052
       bfd_elf_discard_info rely on reloc offsets
12053
       being ordered.  */
12054
0
          irela->r_offset = last_offset;
12055
0
          irela->r_info = 0;
12056
0
          irela->r_addend = 0;
12057
0
          continue;
12058
0
        }
12059
12060
0
      irela->r_offset += o->output_offset;
12061
12062
      /* Relocs in an executable have to be virtual addresses.  */
12063
0
      if (!bfd_link_relocatable (flinfo->info))
12064
0
        irela->r_offset += o->output_section->vma;
12065
12066
0
      last_offset = irela->r_offset;
12067
12068
0
      r_symndx = irela->r_info >> r_sym_shift;
12069
0
      if (r_symndx == STN_UNDEF)
12070
0
        continue;
12071
12072
0
      if (r_symndx >= locsymcount
12073
0
          || (elf_bad_symtab (input_bfd)
12074
0
        && flinfo->sections[r_symndx] == NULL))
12075
0
        {
12076
0
          struct elf_link_hash_entry *rh;
12077
12078
          /* This is a reloc against a global symbol.  We
12079
       have not yet output all the local symbols, so
12080
       we do not know the symbol index of any global
12081
       symbol.  We set the rel_hash entry for this
12082
       reloc to point to the global hash table entry
12083
       for this symbol.  The symbol index is then
12084
       set at the end of bfd_elf_final_link.  */
12085
0
          rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12086
0
               extsymoff, num_sym);
12087
0
          if (rh == NULL)
12088
0
      {
12089
        /* FIXME: Generate an error ?  */
12090
0
        continue;
12091
0
      }
12092
12093
          /* Setting the index to -2 tells elf_link_output_extsym
12094
       that this symbol is used by a reloc.  */
12095
0
          BFD_ASSERT (rh->indx < 0);
12096
0
          rh->indx = -2;
12097
0
          *rel_hash = rh;
12098
12099
0
          continue;
12100
0
        }
12101
12102
      /* This is a reloc against a local symbol.  */
12103
12104
0
      *rel_hash = NULL;
12105
0
      sym = isymbuf[r_symndx];
12106
0
      sec = flinfo->sections[r_symndx];
12107
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12108
0
        {
12109
          /* I suppose the backend ought to fill in the
12110
       section of any STT_SECTION symbol against a
12111
       processor specific section.  */
12112
0
          r_symndx = STN_UNDEF;
12113
0
          if (bfd_is_abs_section (sec))
12114
0
      ;
12115
0
          else if (sec == NULL || sec->owner == NULL)
12116
0
      {
12117
0
        bfd_set_error (bfd_error_bad_value);
12118
0
        return false;
12119
0
      }
12120
0
          else
12121
0
      {
12122
0
        asection *osec = sec->output_section;
12123
12124
        /* If we have discarded a section, the output
12125
           section will be the absolute section.  In
12126
           case of discarded SEC_MERGE sections, use
12127
           the kept section.  relocate_section should
12128
           have already handled discarded linkonce
12129
           sections.  */
12130
0
        if (bfd_is_abs_section (osec)
12131
0
            && sec->kept_section != NULL
12132
0
            && sec->kept_section->output_section != NULL)
12133
0
          {
12134
0
            osec = sec->kept_section->output_section;
12135
0
            irela->r_addend -= osec->vma;
12136
0
          }
12137
12138
0
        if (!bfd_is_abs_section (osec))
12139
0
          {
12140
0
            r_symndx = osec->target_index;
12141
0
            if (r_symndx == STN_UNDEF)
12142
0
        {
12143
0
          irela->r_addend += osec->vma;
12144
0
          osec = flinfo->info->callbacks->nearby_section
12145
0
            (output_bfd, osec, osec->vma);
12146
0
          irela->r_addend -= osec->vma;
12147
0
          r_symndx = osec->target_index;
12148
0
        }
12149
0
          }
12150
0
      }
12151
12152
          /* Adjust the addend according to where the
12153
       section winds up in the output section.  */
12154
0
          if (rela_normal)
12155
0
      irela->r_addend += sec->output_offset;
12156
0
        }
12157
0
      else
12158
0
        {
12159
0
          if (flinfo->indices[r_symndx] == -1)
12160
0
      {
12161
0
        unsigned long shlink;
12162
0
        const char *name;
12163
0
        asection *osec;
12164
0
        long indx;
12165
12166
0
        if (flinfo->info->strip == strip_all)
12167
0
          {
12168
            /* You can't do ld -r -s.  */
12169
0
            bfd_set_error (bfd_error_invalid_operation);
12170
0
            return false;
12171
0
          }
12172
12173
        /* This symbol was skipped earlier, but
12174
           since it is needed by a reloc, we
12175
           must output it now.  */
12176
0
        shlink = symtab_hdr->sh_link;
12177
0
        name = (bfd_elf_string_from_elf_section
12178
0
          (input_bfd, shlink, sym.st_name));
12179
0
        if (name == NULL)
12180
0
          return false;
12181
12182
0
        osec = sec->output_section;
12183
0
        sym.st_shndx =
12184
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12185
0
                     osec);
12186
0
        if (sym.st_shndx == SHN_BAD)
12187
0
          return false;
12188
12189
0
        sym.st_value += sec->output_offset;
12190
0
        if (!bfd_link_relocatable (flinfo->info))
12191
0
          {
12192
0
            sym.st_value += osec->vma;
12193
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12194
0
        {
12195
0
          struct elf_link_hash_table *htab
12196
0
            = elf_hash_table (flinfo->info);
12197
12198
          /* STT_TLS symbols are relative to PT_TLS
12199
             segment base.  */
12200
0
          if (htab->tls_sec != NULL)
12201
0
            sym.st_value -= htab->tls_sec->vma;
12202
0
          else
12203
0
            sym.st_info
12204
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12205
0
                 STT_NOTYPE);
12206
0
        }
12207
0
          }
12208
12209
0
        indx = bfd_get_symcount (output_bfd);
12210
0
        ret = elf_link_output_symstrtab (flinfo, name,
12211
0
                 &sym, sec,
12212
0
                 NULL);
12213
0
        if (ret == 0)
12214
0
          return false;
12215
0
        else if (ret == 1)
12216
0
          flinfo->indices[r_symndx] = indx;
12217
0
        else
12218
0
          abort ();
12219
0
      }
12220
12221
0
          r_symndx = flinfo->indices[r_symndx];
12222
0
        }
12223
12224
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12225
0
           | (irela->r_info & r_type_mask));
12226
0
    }
12227
12228
        /* Swap out the relocs.  */
12229
0
        input_rel_hdr = esdi->rel.hdr;
12230
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12231
0
    {
12232
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12233
0
                 input_rel_hdr,
12234
0
                 internal_relocs,
12235
0
                 rel_hash_list))
12236
0
        return false;
12237
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12238
0
              * bed->s->int_rels_per_ext_rel);
12239
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12240
0
    }
12241
12242
0
        input_rela_hdr = esdi->rela.hdr;
12243
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12244
0
    {
12245
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12246
0
                 input_rela_hdr,
12247
0
                 internal_relocs,
12248
0
                 rela_hash_list))
12249
0
        return false;
12250
0
    }
12251
0
      }
12252
0
  }
12253
12254
      /* Write out the modified section contents.  */
12255
0
      if (bed->elf_backend_write_section
12256
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12257
0
            contents))
12258
0
  {
12259
    /* Section written out.  */
12260
0
  }
12261
0
      else switch (o->sec_info_type)
12262
0
  {
12263
0
  case SEC_INFO_TYPE_STABS:
12264
0
    if (! (_bfd_write_section_stabs
12265
0
     (output_bfd,
12266
0
      &elf_hash_table (flinfo->info)->stab_info, o, contents)))
12267
0
      return false;
12268
0
    break;
12269
0
  case SEC_INFO_TYPE_MERGE:
12270
0
    if (! _bfd_write_merged_section (output_bfd, o))
12271
0
      return false;
12272
0
    break;
12273
0
  case SEC_INFO_TYPE_EH_FRAME:
12274
0
    {
12275
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12276
0
               o, contents))
12277
0
        return false;
12278
0
    }
12279
0
    break;
12280
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12281
0
    {
12282
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12283
0
               flinfo->info,
12284
0
               o, contents))
12285
0
        return false;
12286
0
    }
12287
0
    break;
12288
0
  case SEC_INFO_TYPE_SFRAME:
12289
0
      {
12290
        /* Merge SFrame section into the SFrame encoder context of the
12291
     output_bfd's section.  The final .sframe output section will
12292
     be written out later.  */
12293
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12294
0
              o, contents))
12295
0
    return false;
12296
0
      }
12297
0
      break;
12298
0
  default:
12299
0
    {
12300
0
      if (! (o->flags & SEC_EXCLUDE))
12301
0
        {
12302
0
    file_ptr offset = (file_ptr) o->output_offset;
12303
0
    bfd_size_type todo = o->size;
12304
12305
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12306
12307
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12308
0
        && o->size > address_size)
12309
0
      {
12310
        /* Reverse-copy input section to output.  */
12311
12312
0
        if ((o->size & (address_size - 1)) != 0
12313
0
      || (o->reloc_count != 0
12314
0
          && (o->size * bed->s->int_rels_per_ext_rel
12315
0
        != o->reloc_count * address_size)))
12316
0
          {
12317
0
      _bfd_error_handler
12318
        /* xgettext:c-format */
12319
0
        (_("error: %pB: size of section %pA is not "
12320
0
           "multiple of address size"),
12321
0
         input_bfd, o);
12322
0
      bfd_set_error (bfd_error_bad_value);
12323
0
      return false;
12324
0
          }
12325
12326
0
        do
12327
0
          {
12328
0
      todo -= address_size;
12329
0
      if (! bfd_set_section_contents (output_bfd,
12330
0
              o->output_section,
12331
0
              contents + todo,
12332
0
              offset,
12333
0
              address_size))
12334
0
        return false;
12335
0
      if (todo == 0)
12336
0
        break;
12337
0
      offset += address_size;
12338
0
          }
12339
0
        while (1);
12340
0
      }
12341
0
    else if (! bfd_set_section_contents (output_bfd,
12342
0
                 o->output_section,
12343
0
                 contents,
12344
0
                 offset, todo))
12345
0
      return false;
12346
0
        }
12347
0
    }
12348
0
    break;
12349
0
  }
12350
12351
      /* Munmap the section contents for each input section.  */
12352
0
      _bfd_elf_link_munmap_section_contents (o);
12353
0
    }
12354
12355
0
  return true;
12356
0
}
12357
12358
/* Generate a reloc when linking an ELF file.  This is a reloc
12359
   requested by the linker, and does not come from any input file.  This
12360
   is used to build constructor and destructor tables when linking
12361
   with -Ur.  */
12362
12363
static bool
12364
elf_reloc_link_order (bfd *output_bfd,
12365
          struct bfd_link_info *info,
12366
          asection *output_section,
12367
          struct bfd_link_order *link_order)
12368
0
{
12369
0
  reloc_howto_type *howto;
12370
0
  long indx;
12371
0
  bfd_vma offset;
12372
0
  bfd_vma addend;
12373
0
  struct bfd_elf_section_reloc_data *reldata;
12374
0
  struct elf_link_hash_entry **rel_hash_ptr;
12375
0
  Elf_Internal_Shdr *rel_hdr;
12376
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12377
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12378
0
  bfd_byte *erel;
12379
0
  unsigned int i;
12380
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12381
12382
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12383
0
  if (howto == NULL)
12384
0
    {
12385
0
      bfd_set_error (bfd_error_bad_value);
12386
0
      return false;
12387
0
    }
12388
12389
0
  addend = link_order->u.reloc.p->addend;
12390
12391
0
  if (esdo->rel.hdr)
12392
0
    reldata = &esdo->rel;
12393
0
  else if (esdo->rela.hdr)
12394
0
    reldata = &esdo->rela;
12395
0
  else
12396
0
    {
12397
0
      reldata = NULL;
12398
0
      BFD_ASSERT (0);
12399
0
    }
12400
12401
  /* Figure out the symbol index.  */
12402
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12403
0
  if (link_order->type == bfd_section_reloc_link_order)
12404
0
    {
12405
0
      indx = link_order->u.reloc.p->u.section->target_index;
12406
0
      BFD_ASSERT (indx != 0);
12407
0
      *rel_hash_ptr = NULL;
12408
0
    }
12409
0
  else
12410
0
    {
12411
0
      struct elf_link_hash_entry *h;
12412
12413
      /* Treat a reloc against a defined symbol as though it were
12414
   actually against the section.  */
12415
0
      h = ((struct elf_link_hash_entry *)
12416
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12417
0
           link_order->u.reloc.p->u.name,
12418
0
           false, false, true));
12419
0
      if (h != NULL
12420
0
    && (h->root.type == bfd_link_hash_defined
12421
0
        || h->root.type == bfd_link_hash_defweak))
12422
0
  {
12423
0
    asection *section;
12424
12425
0
    section = h->root.u.def.section;
12426
0
    indx = section->output_section->target_index;
12427
0
    *rel_hash_ptr = NULL;
12428
    /* It seems that we ought to add the symbol value to the
12429
       addend here, but in practice it has already been added
12430
       because it was passed to constructor_callback.  */
12431
0
    addend += section->output_section->vma + section->output_offset;
12432
0
  }
12433
0
      else if (h != NULL)
12434
0
  {
12435
    /* Setting the index to -2 tells elf_link_output_extsym that
12436
       this symbol is used by a reloc.  */
12437
0
    h->indx = -2;
12438
0
    *rel_hash_ptr = h;
12439
0
    indx = 0;
12440
0
  }
12441
0
      else
12442
0
  {
12443
0
    (*info->callbacks->unattached_reloc)
12444
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12445
0
    indx = 0;
12446
0
  }
12447
0
    }
12448
12449
  /* If this is an inplace reloc, we must write the addend into the
12450
     object file.  */
12451
0
  if (howto->partial_inplace && addend != 0)
12452
0
    {
12453
0
      bfd_size_type size;
12454
0
      bfd_reloc_status_type rstat;
12455
0
      bfd_byte *buf;
12456
0
      bool ok;
12457
0
      const char *sym_name;
12458
0
      bfd_size_type octets;
12459
12460
0
      size = bfd_get_reloc_size (howto);
12461
0
      buf = bfd_zmalloc (size);
12462
0
      if (buf == NULL && size != 0)
12463
0
  return false;
12464
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12465
0
      switch (rstat)
12466
0
  {
12467
0
  case bfd_reloc_ok:
12468
0
    break;
12469
12470
0
  default:
12471
0
  case bfd_reloc_outofrange:
12472
0
    abort ();
12473
12474
0
  case bfd_reloc_overflow:
12475
0
    if (link_order->type == bfd_section_reloc_link_order)
12476
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12477
0
    else
12478
0
      sym_name = link_order->u.reloc.p->u.name;
12479
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12480
0
                howto->name, addend, NULL, NULL,
12481
0
                (bfd_vma) 0);
12482
0
    break;
12483
0
  }
12484
12485
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12486
0
               output_section);
12487
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12488
0
             octets, size);
12489
0
      free (buf);
12490
0
      if (! ok)
12491
0
  return false;
12492
0
    }
12493
12494
  /* The address of a reloc is relative to the section in a
12495
     relocatable file, and is a virtual address in an executable
12496
     file.  */
12497
0
  offset = link_order->offset;
12498
0
  if (! bfd_link_relocatable (info))
12499
0
    offset += output_section->vma;
12500
12501
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12502
0
    {
12503
0
      irel[i].r_offset = offset;
12504
0
      irel[i].r_info = 0;
12505
0
      irel[i].r_addend = 0;
12506
0
    }
12507
0
  if (bed->s->arch_size == 32)
12508
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12509
0
  else
12510
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12511
12512
0
  rel_hdr = reldata->hdr;
12513
0
  erel = rel_hdr->contents;
12514
0
  if (rel_hdr->sh_type == SHT_REL)
12515
0
    {
12516
0
      erel += reldata->count * bed->s->sizeof_rel;
12517
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12518
0
    }
12519
0
  else
12520
0
    {
12521
0
      irel[0].r_addend = addend;
12522
0
      erel += reldata->count * bed->s->sizeof_rela;
12523
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12524
0
    }
12525
12526
0
  ++reldata->count;
12527
12528
0
  return true;
12529
0
}
12530
12531
/* Generate an import library in INFO->implib_bfd from symbols in OBFD.
12532
   Returns TRUE upon success, FALSE otherwise.  */
12533
12534
static bool
12535
elf_output_implib (bfd *obfd, struct bfd_link_info *info)
12536
0
{
12537
0
  bool ret = false;
12538
0
  bfd *implib_bfd;
12539
0
  elf_backend_data *obed;
12540
0
  flagword flags;
12541
0
  enum bfd_architecture arch;
12542
0
  unsigned int mach;
12543
0
  asymbol **sympp = NULL;
12544
0
  long symsize;
12545
0
  long symcount;
12546
0
  long src_count;
12547
0
  elf_symbol_type *osymbuf;
12548
12549
0
  implib_bfd = info->out_implib_bfd;
12550
0
  obed = get_elf_backend_data (obfd);
12551
12552
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12553
0
    return false;
12554
12555
  /* Use flag from executable but make it a relocatable object.  */
12556
0
  flags = bfd_get_file_flags (obfd);
12557
0
  flags &= ~HAS_RELOC;
12558
0
  if (!bfd_set_start_address (implib_bfd, 0)
12559
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12560
0
    return false;
12561
12562
  /* Copy architecture of output file to import library file.  */
12563
0
  arch = bfd_get_arch (obfd);
12564
0
  mach = bfd_get_mach (obfd);
12565
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12566
0
      && (obfd->target_defaulted
12567
0
    || bfd_get_arch (obfd) != bfd_get_arch (implib_bfd)))
12568
0
    return false;
12569
12570
  /* Get symbol table size.  */
12571
0
  symsize = bfd_get_symtab_upper_bound (obfd);
12572
0
  if (symsize < 0)
12573
0
    return false;
12574
12575
  /* Read in the symbol table.  */
12576
0
  sympp = bfd_malloc (symsize);
12577
0
  if (sympp == NULL)
12578
0
    return false;
12579
12580
0
  symcount = bfd_canonicalize_symtab (obfd, sympp);
12581
0
  if (symcount < 0)
12582
0
    goto free_sym_buf;
12583
12584
  /* Allow the BFD backend to copy any private header data it
12585
     understands from the output BFD to the import library BFD.  */
12586
0
  if (! bfd_copy_private_header_data (obfd, implib_bfd))
12587
0
    goto free_sym_buf;
12588
12589
  /* Filter symbols to appear in the import library.  */
12590
0
  symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
12591
0
  if (symcount == 0)
12592
0
    {
12593
0
      bfd_set_error (bfd_error_no_symbols);
12594
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12595
0
        implib_bfd);
12596
0
      goto free_sym_buf;
12597
0
    }
12598
12599
  /* Make symbols absolute.  */
12600
0
  osymbuf = bfd_alloc (implib_bfd, symcount * sizeof (*osymbuf));
12601
0
  if (osymbuf == NULL)
12602
0
    goto free_sym_buf;
12603
12604
0
  for (src_count = 0; src_count < symcount; src_count++)
12605
0
    {
12606
0
      memcpy (&osymbuf[src_count], sympp[src_count], sizeof (*osymbuf));
12607
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12608
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12609
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12610
0
      osymbuf[src_count].internal_elf_sym.st_value =
12611
0
  osymbuf[src_count].symbol.value;
12612
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12613
0
    }
12614
12615
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12616
12617
  /* Allow the BFD backend to copy any private data it understands
12618
     from the output BFD to the import library BFD.  This is done last
12619
     to permit the routine to look at the filtered symbol table.  */
12620
0
  if (! bfd_copy_private_bfd_data (obfd, implib_bfd))
12621
0
    goto free_sym_buf;
12622
12623
0
  if (!bfd_close (implib_bfd))
12624
0
    goto free_sym_buf;
12625
12626
0
  ret = true;
12627
12628
0
 free_sym_buf:
12629
0
  free (sympp);
12630
0
  return ret;
12631
0
}
12632
12633
static void
12634
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12635
0
{
12636
0
  asection *o;
12637
12638
0
  if (flinfo->symstrtab != NULL)
12639
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12640
0
  free (flinfo->contents);
12641
0
  free (flinfo->external_relocs);
12642
0
  free (flinfo->internal_relocs);
12643
0
  free (flinfo->external_syms);
12644
0
  free (flinfo->locsym_shndx);
12645
0
  free (flinfo->internal_syms);
12646
0
  free (flinfo->indices);
12647
0
  free (flinfo->sections);
12648
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12649
0
    free (flinfo->symshndxbuf);
12650
0
  for (o = obfd->sections; o != NULL; o = o->next)
12651
0
    {
12652
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12653
0
      free (esdo->rel.hashes);
12654
0
      free (esdo->rela.hashes);
12655
0
    }
12656
0
}
12657
12658
/* Do the final step of an ELF link.  */
12659
12660
bool
12661
_bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
12662
0
{
12663
0
  bool dynamic;
12664
0
  bool emit_relocs;
12665
0
  bfd *dynobj;
12666
0
  struct elf_final_link_info flinfo;
12667
0
  asection *o;
12668
0
  struct bfd_link_order *p;
12669
0
  bfd *sub;
12670
0
  bfd_size_type max_contents_size;
12671
0
  bfd_size_type max_external_reloc_size;
12672
0
  bfd_size_type max_internal_reloc_count;
12673
0
  bfd_size_type max_sym_count;
12674
0
  bfd_size_type max_sym_shndx_count;
12675
0
  Elf_Internal_Sym elfsym;
12676
0
  unsigned int i;
12677
0
  Elf_Internal_Shdr *symtab_hdr;
12678
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12679
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
12680
0
  struct elf_outext_info eoinfo;
12681
0
  bool merged;
12682
0
  size_t relativecount;
12683
0
  size_t relr_entsize;
12684
0
  asection *reldyn = 0;
12685
0
  bfd_size_type amt;
12686
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12687
0
  bool sections_removed;
12688
12689
0
  if (!is_elf_hash_table (&htab->root))
12690
0
    return false;
12691
12692
0
  if (bfd_link_pic (info))
12693
0
    obfd->flags |= DYNAMIC;
12694
12695
0
  dynamic = htab->dynamic_sections_created;
12696
0
  dynobj = htab->dynobj;
12697
12698
0
  emit_relocs = (bfd_link_relocatable (info)
12699
0
     || info->emitrelocations);
12700
12701
0
  memset (&flinfo, 0, sizeof (flinfo));
12702
0
  flinfo.info = info;
12703
0
  flinfo.output_bfd = obfd;
12704
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12705
0
  if (flinfo.symstrtab == NULL)
12706
0
    return false;
12707
12708
0
  if (! dynamic)
12709
0
    {
12710
0
      flinfo.hash_sec = NULL;
12711
0
      flinfo.symver_sec = NULL;
12712
0
    }
12713
0
  else
12714
0
    {
12715
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12716
      /* Note that dynsym_sec can be NULL (on VMS).  */
12717
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12718
      /* Note that it is OK if symver_sec is NULL.  */
12719
0
    }
12720
12721
0
  if (info->unique_symbol
12722
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12723
0
             local_hash_newfunc,
12724
0
             sizeof (struct local_hash_entry)))
12725
0
    return false;
12726
12727
  /* The object attributes have been merged.  Remove the input
12728
     sections from the link, and set the contents of the output
12729
     section.  */
12730
0
  sections_removed = false;
12731
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12732
0
  const char *obj_attrs_section = get_elf_backend_data (obfd)->obj_attrs_section;
12733
0
#endif
12734
0
  for (o = obfd->sections; o != NULL; o = o->next)
12735
0
    {
12736
0
      bool remove_section = false;
12737
12738
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12739
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12740
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12741
0
  {
12742
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12743
0
      {
12744
0
        asection *input_section;
12745
12746
0
        if (p->type != bfd_indirect_link_order)
12747
0
    continue;
12748
0
        input_section = p->u.indirect.section;
12749
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12750
     elf_link_input_bfd ignores this section.  */
12751
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12752
0
      }
12753
12754
    /* Skip this section later on.  */
12755
0
    o->map_head.link_order = NULL;
12756
12757
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (obfd);
12758
    /* Once ELF headers have been written, the size of a section is
12759
       frozen. We need to set the size of the attribute section before
12760
       _bfd_elf_compute_section_file_positions.  */
12761
0
    bfd_set_section_size (o, attr_size);
12762
0
    if (attr_size > 0)
12763
0
      elf_obj_object_attributes (obfd) = o;
12764
0
    else
12765
0
      remove_section = true;
12766
0
  }
12767
0
      else
12768
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12769
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12770
0
  {
12771
    /* Remove empty group section from linker output.  */
12772
0
    remove_section = true;
12773
0
  }
12774
0
      if (remove_section)
12775
0
  {
12776
0
    o->flags |= SEC_EXCLUDE;
12777
0
    bfd_section_list_remove (obfd, o);
12778
0
    obfd->section_count--;
12779
0
    sections_removed = true;
12780
0
  }
12781
0
    }
12782
0
  if (sections_removed)
12783
0
    bfd_fix_excluded_sec_syms (info);
12784
12785
  /* Count up the number of relocations we will output for each output
12786
     section, so that we know the sizes of the reloc sections.  We
12787
     also figure out some maximum sizes.  */
12788
0
#ifdef USE_MMAP
12789
0
  if (obed->use_mmap)
12790
0
    {
12791
      /* Mmap is used only if section size >= the minimum mmap section
12792
   size.  The initial max_contents_size value covers all sections
12793
   smaller than the minimum mmap section size.  It may be increased
12794
   for compressed or linker created sections or sections whose
12795
   rawsize != size.  max_external_reloc_size covers all relocation
12796
   sections smaller than the minimum mmap section size.  */
12797
0
      max_contents_size = _bfd_minimum_mmap_size;
12798
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12799
0
    }
12800
0
  else
12801
0
#endif
12802
0
    {
12803
0
      max_contents_size = 0;
12804
0
      max_external_reloc_size = 0;
12805
0
    }
12806
0
  max_internal_reloc_count = 0;
12807
0
  max_sym_count = 0;
12808
0
  max_sym_shndx_count = 0;
12809
0
  merged = false;
12810
0
  for (o = obfd->sections; o != NULL; o = o->next)
12811
0
    {
12812
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12813
0
      bfd_size_type size_align = 1;
12814
12815
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12816
0
  {
12817
    /* eh_frame editing can extend last FDE to cover padding
12818
       between one section and the next.  */
12819
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12820
0
      * bfd_octets_per_byte (obfd, o));
12821
0
  }
12822
12823
0
      o->reloc_count = 0;
12824
12825
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12826
0
  {
12827
0
    unsigned int reloc_count = 0;
12828
0
    unsigned int additional_reloc_count = 0;
12829
0
    struct bfd_elf_section_data *esdi = NULL;
12830
12831
0
    if (p->type == bfd_section_reloc_link_order
12832
0
        || p->type == bfd_symbol_reloc_link_order)
12833
0
      reloc_count = 1;
12834
0
    else if (p->type == bfd_indirect_link_order)
12835
0
      {
12836
0
        asection *sec;
12837
12838
0
        sec = p->u.indirect.section;
12839
12840
        /* Mark all sections which are to be included in the
12841
     link.  This will normally be every section.  We need
12842
     to do this so that we can identify any sections which
12843
     the linker has decided to not include.  */
12844
0
        sec->linker_mark = true;
12845
12846
0
        if (sec->flags & SEC_MERGE)
12847
0
    merged = true;
12848
12849
0
#ifdef USE_MMAP
12850
        /* Mmap is used only on non-compressed, non-linker created
12851
     sections whose rawsize == size.  */
12852
0
        if (!obed->use_mmap
12853
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12854
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12855
0
      || sec->rawsize != sec->size)
12856
0
#endif
12857
0
    {
12858
0
      bfd_size_type size = (sec->rawsize > sec->size
12859
0
          ? sec->rawsize : sec->size);
12860
0
      size = (size + size_align - 1) & -size_align;
12861
0
      if (max_contents_size < size)
12862
0
        max_contents_size = size;
12863
0
    }
12864
12865
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12866
0
      && (sec->owner->flags & DYNAMIC) == 0)
12867
0
    {
12868
0
      size_t sym_count;
12869
12870
      /* We are interested in just local symbols, not all
12871
         symbols.  */
12872
0
      if (elf_bad_symtab (sec->owner))
12873
0
        sym_count = (elf_symtab_hdr (sec->owner).sh_size
12874
0
         / obed->s->sizeof_sym);
12875
0
      else
12876
0
        sym_count = elf_symtab_hdr (sec->owner).sh_info;
12877
12878
0
      if (sym_count > max_sym_count)
12879
0
        max_sym_count = sym_count;
12880
12881
0
      if (sym_count > max_sym_shndx_count
12882
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12883
0
        max_sym_shndx_count = sym_count;
12884
12885
0
      esdi = elf_section_data (sec);
12886
12887
0
      if (esdi->this_hdr.sh_type == SHT_REL
12888
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12889
        /* Some backends use reloc_count in relocation sections
12890
           to count particular types of relocs.  Of course,
12891
           reloc sections themselves can't have relocations.  */
12892
0
        ;
12893
0
      else if (emit_relocs)
12894
0
        {
12895
0
          reloc_count = sec->reloc_count;
12896
0
          if (obed->elf_backend_count_additional_relocs)
12897
0
      {
12898
0
        int c;
12899
0
        c = obed->elf_backend_count_additional_relocs (sec);
12900
0
        additional_reloc_count += c;
12901
0
      }
12902
0
        }
12903
0
      else if (obed->elf_backend_count_relocs)
12904
0
        reloc_count = obed->elf_backend_count_relocs (info, sec);
12905
12906
0
      if ((sec->flags & SEC_RELOC) != 0)
12907
0
        {
12908
0
#ifdef USE_MMAP
12909
0
          if (!obed->use_mmap)
12910
0
#endif
12911
0
      {
12912
0
        size_t ext_size = 0;
12913
12914
0
        if (esdi->rel.hdr != NULL)
12915
0
          ext_size = esdi->rel.hdr->sh_size;
12916
0
        if (esdi->rela.hdr != NULL)
12917
0
          ext_size += esdi->rela.hdr->sh_size;
12918
12919
0
        if (ext_size > max_external_reloc_size)
12920
0
          max_external_reloc_size = ext_size;
12921
0
      }
12922
0
          if (sec->reloc_count > max_internal_reloc_count)
12923
0
      max_internal_reloc_count = sec->reloc_count;
12924
0
        }
12925
0
    }
12926
0
      }
12927
12928
0
    if (reloc_count == 0)
12929
0
      continue;
12930
12931
0
    reloc_count += additional_reloc_count;
12932
0
    o->reloc_count += reloc_count;
12933
12934
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12935
0
      {
12936
0
        if (esdi->rel.hdr)
12937
0
    {
12938
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12939
0
      esdo->rel.count += additional_reloc_count;
12940
0
    }
12941
0
        if (esdi->rela.hdr)
12942
0
    {
12943
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12944
0
      esdo->rela.count += additional_reloc_count;
12945
0
    }
12946
0
      }
12947
0
    else
12948
0
      {
12949
0
        if (o->use_rela_p)
12950
0
    esdo->rela.count += reloc_count;
12951
0
        else
12952
0
    esdo->rel.count += reloc_count;
12953
0
      }
12954
0
  }
12955
12956
0
      if (o->reloc_count > 0)
12957
0
  o->flags |= SEC_RELOC;
12958
0
      else
12959
0
  {
12960
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12961
       set it (this is probably a bug) and if it is set
12962
       assign_section_numbers will create a reloc section.  */
12963
0
    o->flags &=~ SEC_RELOC;
12964
0
  }
12965
12966
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12967
   zero.  This is done in elf_fake_sections as well, but forcing
12968
   the VMA to 0 here will ensure that relocs against these
12969
   sections are handled correctly.  */
12970
0
      if ((o->flags & SEC_ALLOC) == 0
12971
0
    && ! o->user_set_vma)
12972
0
  o->vma = 0;
12973
0
    }
12974
12975
0
  if (! bfd_link_relocatable (info) && merged)
12976
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, obfd);
12977
12978
  /* Figure out the file positions for everything but the symbol table
12979
     and the relocs.  We set symcount to force assign_section_numbers
12980
     to create a symbol table.  */
12981
0
  obfd->symcount = info->strip != strip_all || emit_relocs;
12982
0
  BFD_ASSERT (! obfd->output_has_begun);
12983
0
  if (! _bfd_elf_compute_section_file_positions (obfd, info))
12984
0
    goto error_return;
12985
12986
  /* Set sizes, and assign file positions for reloc sections.  */
12987
0
  for (o = obfd->sections; o != NULL; o = o->next)
12988
0
    {
12989
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12990
0
      if ((o->flags & SEC_RELOC) != 0)
12991
0
  {
12992
0
    if (esdo->rel.hdr
12993
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rel)))
12994
0
      goto error_return;
12995
12996
0
    if (esdo->rela.hdr
12997
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rela)))
12998
0
      goto error_return;
12999
0
  }
13000
13001
      /* _bfd_elf_compute_section_file_positions makes temporary use
13002
   of target_index.  Reset it.  */
13003
0
      o->target_index = 0;
13004
13005
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
13006
   to count upwards while actually outputting the relocations.  */
13007
0
      esdo->rel.count = 0;
13008
0
      esdo->rela.count = 0;
13009
13010
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
13011
0
    && !bfd_section_is_ctf (o))
13012
0
  {
13013
    /* Cache the section contents so that they can be compressed
13014
       later.  Use bfd_malloc since it will be freed by
13015
       bfd_compress_section_contents.  */
13016
0
    unsigned char *contents = esdo->this_hdr.contents;
13017
0
    if (contents != NULL)
13018
0
      abort ();
13019
0
    contents = bfd_malloc (esdo->this_hdr.sh_size);
13020
0
    if (contents == NULL)
13021
0
      goto error_return;
13022
0
    esdo->this_hdr.contents = contents;
13023
0
  }
13024
0
    }
13025
13026
  /* We have now assigned file positions for all the sections except .symtab,
13027
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
13028
     the .symtab section at the current file position, and write directly to it.
13029
     We build the .strtab section in memory.  */
13030
0
  obfd->symcount = 0;
13031
0
  symtab_hdr = &elf_symtab_hdr (obfd);
13032
  /* sh_name is set in prep_headers.  */
13033
0
  symtab_hdr->sh_type = SHT_SYMTAB;
13034
  /* sh_flags, sh_addr and sh_size all start off zero.  */
13035
0
  symtab_hdr->sh_entsize = obed->s->sizeof_sym;
13036
  /* sh_link is set in assign_section_numbers.  */
13037
  /* sh_info is set below.  */
13038
  /* sh_offset is set just below.  */
13039
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << obed->s->log_file_align;
13040
13041
0
  if (max_sym_count < 20)
13042
0
    max_sym_count = 20;
13043
0
  htab->strtabsize = max_sym_count;
13044
0
  htab->strtab = bfd_malloc (max_sym_count * sizeof (*htab->strtab));
13045
0
  if (htab->strtab == NULL)
13046
0
    goto error_return;
13047
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
13048
0
  flinfo.symshndxbuf
13049
0
    = (elf_numsections (obfd) > (SHN_LORESERVE & 0xFFFF)
13050
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
13051
13052
0
  if (info->strip != strip_all || emit_relocs)
13053
0
    {
13054
0
      file_ptr off = elf_next_file_pos (obfd);
13055
13056
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13057
13058
      /* Note that at this point elf_next_file_pos (obfd) is
13059
   incorrect.  We do not yet know the size of the .symtab section.
13060
   We correct next_file_pos below, after we do know the size.  */
13061
13062
      /* Start writing out the symbol table.  The first symbol is always a
13063
   dummy symbol.  */
13064
0
      elfsym.st_value = 0;
13065
0
      elfsym.st_size = 0;
13066
0
      elfsym.st_info = 0;
13067
0
      elfsym.st_other = 0;
13068
0
      elfsym.st_shndx = SHN_UNDEF;
13069
0
      elfsym.st_target_internal = 0;
13070
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13071
0
             bfd_und_section_ptr, NULL) != 1)
13072
0
  goto error_return;
13073
13074
      /* Output a symbol for each section if asked or they are used for
13075
   relocs.  These symbols usually have no names.  We store the
13076
   index of each one in the index field of the section, so that
13077
   we can find it again when outputting relocs.  */
13078
13079
0
      if (bfd_keep_unused_section_symbols (obfd) || emit_relocs)
13080
0
  {
13081
0
    bool name_local_sections
13082
0
      = (obed->elf_backend_name_local_section_symbols
13083
0
         && obed->elf_backend_name_local_section_symbols (obfd));
13084
0
    const char *name = NULL;
13085
13086
0
    elfsym.st_size = 0;
13087
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13088
0
    elfsym.st_other = 0;
13089
0
    elfsym.st_value = 0;
13090
0
    elfsym.st_target_internal = 0;
13091
0
    for (i = 1; i < elf_numsections (obfd); i++)
13092
0
      {
13093
0
        o = bfd_section_from_elf_index (obfd, i);
13094
0
        if (o != NULL)
13095
0
    {
13096
0
      o->target_index = bfd_get_symcount (obfd);
13097
0
      elfsym.st_shndx = i;
13098
0
      if (!bfd_link_relocatable (info))
13099
0
        elfsym.st_value = o->vma;
13100
0
      if (name_local_sections)
13101
0
        name = o->name;
13102
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13103
0
             NULL) != 1)
13104
0
        goto error_return;
13105
0
    }
13106
0
      }
13107
0
  }
13108
0
    }
13109
13110
  /* On some targets like Irix 5 the symbol split between local and global
13111
     ones recorded in the sh_info field needs to be done between section
13112
     and all other symbols.  */
13113
0
  if (obed->elf_backend_elfsym_local_is_section
13114
0
      && obed->elf_backend_elfsym_local_is_section (obfd))
13115
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13116
13117
  /* Allocate some memory to hold information read in from the input
13118
     files.  */
13119
0
  if (max_contents_size != 0)
13120
0
    {
13121
0
      flinfo.contents = bfd_malloc (max_contents_size);
13122
0
      if (flinfo.contents == NULL)
13123
0
  goto error_return;
13124
0
    }
13125
13126
0
  if (max_external_reloc_size != 0)
13127
0
    {
13128
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13129
0
      if (flinfo.external_relocs == NULL)
13130
0
  goto error_return;
13131
0
    }
13132
13133
0
  if (max_internal_reloc_count != 0)
13134
0
    {
13135
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13136
0
      flinfo.internal_relocs = bfd_malloc (amt);
13137
0
      if (flinfo.internal_relocs == NULL)
13138
0
  goto error_return;
13139
0
    }
13140
13141
0
  if (max_sym_count != 0)
13142
0
    {
13143
0
      amt = max_sym_count * obed->s->sizeof_sym;
13144
0
      flinfo.external_syms = bfd_malloc (amt);
13145
0
      if (flinfo.external_syms == NULL)
13146
0
  goto error_return;
13147
13148
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13149
0
      flinfo.internal_syms = bfd_malloc (amt);
13150
0
      if (flinfo.internal_syms == NULL)
13151
0
  goto error_return;
13152
13153
0
      amt = max_sym_count * sizeof (long);
13154
0
      flinfo.indices = bfd_malloc (amt);
13155
0
      if (flinfo.indices == NULL)
13156
0
  goto error_return;
13157
13158
0
      amt = max_sym_count * sizeof (asection *);
13159
0
      flinfo.sections = bfd_malloc (amt);
13160
0
      if (flinfo.sections == NULL)
13161
0
  goto error_return;
13162
0
    }
13163
13164
0
  if (max_sym_shndx_count != 0)
13165
0
    {
13166
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13167
0
      flinfo.locsym_shndx = bfd_malloc (amt);
13168
0
      if (flinfo.locsym_shndx == NULL)
13169
0
  goto error_return;
13170
0
    }
13171
13172
0
  if (htab->tls_sec)
13173
0
    {
13174
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13175
0
      asection *sec;
13176
13177
0
      for (sec = htab->tls_sec;
13178
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13179
0
     sec = sec->next)
13180
0
  {
13181
0
    bfd_size_type size = sec->size;
13182
0
    unsigned int opb = bfd_octets_per_byte (obfd, sec);
13183
13184
0
    if (size == 0
13185
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13186
0
      {
13187
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13188
13189
0
        if (ord != NULL)
13190
0
    size = ord->offset * opb + ord->size;
13191
0
      }
13192
0
    end = sec->vma + size / opb;
13193
0
  }
13194
0
      base = htab->tls_sec->vma;
13195
      /* Only align end of TLS section if static TLS doesn't have special
13196
   alignment requirements.  */
13197
0
      if (obed->static_tls_alignment == 1)
13198
0
  end = align_power (end, htab->tls_sec->alignment_power);
13199
0
      htab->tls_size = end - base;
13200
0
    }
13201
13202
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13203
0
    return false;
13204
13205
  /* Finish relative relocations here after regular symbol processing
13206
     is finished if DT_RELR is enabled.  */
13207
0
  if (info->enable_dt_relr
13208
0
      && obed->finish_relative_relocs
13209
0
      && !obed->finish_relative_relocs (info))
13210
0
    info->callbacks->fatal
13211
0
      (_("%P: %pB: failed to finish relative relocations\n"), obfd);
13212
13213
  /* Since ELF permits relocations to be against local symbols, we
13214
     must have the local symbols available when we do the relocations.
13215
     Since we would rather only read the local symbols once, and we
13216
     would rather not keep them in memory, we handle all the
13217
     relocations for a single input file at the same time.
13218
13219
     Unfortunately, there is no way to know the total number of local
13220
     symbols until we have seen all of them, and the local symbol
13221
     indices precede the global symbol indices.  This means that when
13222
     we are generating relocatable output, and we see a reloc against
13223
     a global symbol, we can not know the symbol index until we have
13224
     finished examining all the local symbols to see which ones we are
13225
     going to output.  To deal with this, we keep the relocations in
13226
     memory, and don't output them until the end of the link.  This is
13227
     an unfortunate waste of memory, but I don't see a good way around
13228
     it.  Fortunately, it only happens when performing a relocatable
13229
     link, which is not the common case.  FIXME: If keep_memory is set
13230
     we could write the relocs out and then read them again; I don't
13231
     know how bad the memory loss will be.  */
13232
13233
0
  for (o = obfd->sections; o != NULL; o = o->next)
13234
0
    {
13235
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13236
0
  {
13237
0
    if (p->type == bfd_indirect_link_order
13238
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13239
0
      == bfd_target_elf_flavour)
13240
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == obed->s->elfclass)
13241
0
      {
13242
0
        if (! sub->output_has_begun)
13243
0
    {
13244
0
      sub->output_has_begun = true;
13245
0
      if ((sub->flags & BFD_LINKER_CREATED) == 0
13246
0
          && !elf_link_input_bfd (&flinfo, sub))
13247
0
        goto error_return;
13248
0
    }
13249
0
      }
13250
0
    else if (p->type == bfd_section_reloc_link_order
13251
0
       || p->type == bfd_symbol_reloc_link_order)
13252
0
      {
13253
0
        if (! elf_reloc_link_order (obfd, info, o, p))
13254
0
    goto error_return;
13255
0
      }
13256
0
    else
13257
0
      {
13258
0
        if (! _bfd_default_link_order (obfd, info, o, p))
13259
0
    {
13260
0
      if (p->type == bfd_indirect_link_order
13261
0
          && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13262
0
        == bfd_target_elf_flavour)
13263
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13264
0
        != obed->s->elfclass))
13265
0
        {
13266
0
          const char *iclass, *oclass;
13267
13268
0
          switch (obed->s->elfclass)
13269
0
      {
13270
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13271
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13272
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13273
0
      default: abort ();
13274
0
      }
13275
13276
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13277
0
      {
13278
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13279
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13280
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13281
0
      default: abort ();
13282
0
      }
13283
13284
0
          bfd_set_error (bfd_error_wrong_format);
13285
0
          _bfd_error_handler
13286
      /* xgettext:c-format */
13287
0
      (_("%pB: file class %s incompatible with %s"),
13288
0
       sub, iclass, oclass);
13289
0
        }
13290
13291
0
      goto error_return;
13292
0
    }
13293
0
      }
13294
0
  }
13295
0
    }
13296
  /* Writing of linker created BFDs is left until last, because the
13297
     aarch64 backend wants to copy insns from a relocated section to
13298
     a stub section.  See erratum_843419 code.  */
13299
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13300
0
    if (sub->output_has_begun && (sub->flags & BFD_LINKER_CREATED) != 0)
13301
0
      {
13302
0
  if (!elf_link_input_bfd (&flinfo, sub))
13303
0
    goto error_return;
13304
0
      }
13305
13306
  /* Free symbol buffer if needed.  */
13307
0
  if (!info->reduce_memory_overheads)
13308
0
    {
13309
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13310
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13311
0
    {
13312
0
      free (elf_tdata (sub)->symbuf);
13313
0
      elf_tdata (sub)->symbuf = NULL;
13314
0
    }
13315
0
    }
13316
13317
  /* Output any global symbols that got converted to local in a
13318
     version script or due to symbol visibility.  We do this in a
13319
     separate step since ELF requires all local symbols to appear
13320
     prior to any global symbols.  FIXME: We should only do this if
13321
     some global symbols were, in fact, converted to become local.
13322
     FIXME: Will this work correctly with the Irix 5 linker?  */
13323
0
  eoinfo.failed = false;
13324
0
  eoinfo.flinfo = &flinfo;
13325
0
  eoinfo.localsyms = true;
13326
0
  eoinfo.file_sym_done = false;
13327
  /* Output non-base symbols first.  */
13328
0
  eoinfo.base_symbol = false;
13329
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13330
0
  if (eoinfo.failed)
13331
0
    goto error_return;
13332
13333
  /* If backend needs to output some local symbols not present in the hash
13334
     table, do it now.  */
13335
0
  if (obed->elf_backend_output_arch_local_syms)
13336
0
    {
13337
0
      if (!obed->elf_backend_output_arch_local_syms (info, &flinfo,
13338
0
                 elf_link_output_symstrtab))
13339
0
  goto error_return;
13340
0
    }
13341
13342
  /* That wrote out all the local symbols.  Finish up the symbol table
13343
     with the global symbols. Even if we want to strip everything we
13344
     can, we still need to deal with those global symbols that got
13345
     converted to local in a version script.  */
13346
13347
  /* The sh_info field records the index of the first non local symbol.  */
13348
0
  if (!symtab_hdr->sh_info)
13349
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13350
13351
0
  if (dynamic
13352
0
      && htab->dynsym != NULL
13353
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13354
0
    {
13355
0
      Elf_Internal_Sym sym;
13356
0
      bfd_byte *dynsym = htab->dynsym->contents;
13357
13358
0
      o = htab->dynsym->output_section;
13359
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13360
13361
      /* Write out the section symbols for the output sections.  */
13362
0
      if (bfd_link_pic (info)
13363
0
    || htab->is_relocatable_executable)
13364
0
  {
13365
0
    asection *s;
13366
13367
0
    sym.st_size = 0;
13368
0
    sym.st_name = 0;
13369
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13370
0
    sym.st_other = 0;
13371
0
    sym.st_target_internal = 0;
13372
13373
0
    for (s = obfd->sections; s != NULL; s = s->next)
13374
0
      {
13375
0
        int indx;
13376
0
        bfd_byte *dest;
13377
0
        long dynindx;
13378
13379
0
        dynindx = elf_section_data (s)->dynindx;
13380
0
        if (dynindx <= 0)
13381
0
    continue;
13382
0
        indx = elf_section_data (s)->this_idx;
13383
0
        BFD_ASSERT (indx > 0);
13384
0
        sym.st_shndx = indx;
13385
0
        if (! check_dynsym (obfd, &sym))
13386
0
    goto error_return;
13387
0
        sym.st_value = s->vma;
13388
0
        dest = dynsym + dynindx * obed->s->sizeof_sym;
13389
13390
        /* Inform the linker of the addition of this symbol.  */
13391
13392
0
        if (info->callbacks->ctf_new_dynsym)
13393
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13394
13395
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13396
0
      }
13397
0
  }
13398
13399
      /* Write out the local dynsyms.  */
13400
0
      if (htab->dynlocal)
13401
0
  {
13402
0
    struct elf_link_local_dynamic_entry *e;
13403
0
    for (e = htab->dynlocal; e ; e = e->next)
13404
0
      {
13405
0
        asection *s;
13406
0
        bfd_byte *dest;
13407
13408
        /* Copy the internal symbol and turn off visibility.
13409
     Note that we saved a word of storage and overwrote
13410
     the original st_name with the dynstr_index.  */
13411
0
        sym = e->isym;
13412
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13413
0
        sym.st_shndx = SHN_UNDEF;
13414
13415
0
        s = bfd_section_from_elf_index (e->input_bfd,
13416
0
                e->isym.st_shndx);
13417
0
        if (s != NULL
13418
0
      && s->output_section != NULL
13419
0
      && elf_section_data (s->output_section) != NULL)
13420
0
    {
13421
0
      sym.st_shndx =
13422
0
        elf_section_data (s->output_section)->this_idx;
13423
0
      if (! check_dynsym (obfd, &sym))
13424
0
        goto error_return;
13425
0
      sym.st_value = (s->output_section->vma
13426
0
          + s->output_offset
13427
0
          + e->isym.st_value);
13428
0
    }
13429
13430
        /* Inform the linker of the addition of this symbol.  */
13431
13432
0
        if (info->callbacks->ctf_new_dynsym)
13433
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13434
13435
0
        dest = dynsym + e->dynindx * obed->s->sizeof_sym;
13436
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13437
0
      }
13438
0
  }
13439
0
    }
13440
13441
  /* We get the global symbols from the hash table.  */
13442
0
  eoinfo.failed = false;
13443
0
  eoinfo.localsyms = false;
13444
0
  eoinfo.flinfo = &flinfo;
13445
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13446
0
  if (eoinfo.failed)
13447
0
    goto error_return;
13448
13449
0
  if (htab->has_base_symbols)
13450
0
    {
13451
      /* Output base symbols last in DT_HASH so that they will be picked
13452
   before non-base symbols at run-time.  */
13453
0
      eoinfo.base_symbol = true;
13454
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13455
0
       &eoinfo);
13456
0
      if (eoinfo.failed)
13457
0
  goto error_return;
13458
0
    }
13459
13460
  /* If backend needs to output some symbols not present in the hash
13461
     table, do it now.  */
13462
0
  if (obed->elf_backend_output_arch_syms
13463
0
      && (info->strip != strip_all || emit_relocs))
13464
0
    {
13465
0
      if (!obed->elf_backend_output_arch_syms (info, &flinfo,
13466
0
                 elf_link_output_symstrtab))
13467
0
  goto error_return;
13468
0
    }
13469
13470
  /* Finalize the .strtab section.  */
13471
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13472
13473
  /* Swap out the .strtab section. */
13474
0
  if (!elf_link_swap_symbols_out (&flinfo))
13475
0
    goto error_return;
13476
0
  free (htab->strtab);
13477
0
  htab->strtab = NULL;
13478
13479
  /* Now we know the size of the symtab section.  */
13480
0
  if (bfd_get_symcount (obfd) > 0)
13481
0
    {
13482
      /* Finish up and write out the symbol string table (.strtab)
13483
   section.  */
13484
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13485
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13486
13487
0
      if (elf_symtab_shndx_list (obfd))
13488
0
  {
13489
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (obfd)->hdr;
13490
13491
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13492
0
      {
13493
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13494
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13495
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13496
0
        amt = bfd_get_symcount (obfd) * sizeof (Elf_External_Sym_Shndx);
13497
0
        symtab_shndx_hdr->sh_size = amt;
13498
13499
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13500
0
                     off, true, 0);
13501
13502
0
        if (bfd_seek (obfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13503
0
      || (bfd_write (flinfo.symshndxbuf, amt, obfd) != amt))
13504
0
    goto error_return;
13505
0
      }
13506
0
  }
13507
13508
0
      symstrtab_hdr = &elf_tdata (obfd)->strtab_hdr;
13509
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13510
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13511
0
      symstrtab_hdr->sh_addralign = 1;
13512
13513
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13514
0
                   off, true, 0);
13515
0
      elf_next_file_pos (obfd) = off;
13516
13517
0
      if (bfd_seek (obfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13518
0
    || ! _bfd_elf_strtab_emit (obfd, flinfo.symstrtab))
13519
0
  goto error_return;
13520
0
    }
13521
13522
0
  if (info->out_implib_bfd && !elf_output_implib (obfd, info))
13523
0
    {
13524
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13525
0
        info->out_implib_bfd);
13526
0
      goto error_return;
13527
0
    }
13528
13529
  /* Adjust the relocs to have the correct symbol indices.  */
13530
0
  for (o = obfd->sections; o != NULL; o = o->next)
13531
0
    {
13532
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13533
0
      bool sort;
13534
13535
0
      if ((o->flags & SEC_RELOC) == 0)
13536
0
  continue;
13537
13538
0
      sort = obed->sort_relocs_p == NULL || obed->sort_relocs_p (o);
13539
0
      if (esdo->rel.hdr != NULL
13540
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rel, sort, info))
13541
0
  goto error_return;
13542
0
      if (esdo->rela.hdr != NULL
13543
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rela, sort, info))
13544
0
  goto error_return;
13545
13546
      /* Set the reloc_count field to 0 to prevent write_relocs from
13547
   trying to swap the relocs out itself.  */
13548
0
      o->reloc_count = 0;
13549
0
    }
13550
13551
0
  relativecount = 0;
13552
0
  if (dynamic && info->combreloc && dynobj != NULL)
13553
0
    relativecount = elf_link_sort_relocs (obfd, info, &reldyn);
13554
13555
0
  relr_entsize = 0;
13556
0
  if (htab->srelrdyn != NULL
13557
0
      && htab->srelrdyn->output_section != NULL
13558
0
      && htab->srelrdyn->size != 0)
13559
0
    {
13560
0
      asection *s = htab->srelrdyn->output_section;
13561
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13562
0
      if (relr_entsize == 0)
13563
0
  {
13564
0
    relr_entsize = obed->s->arch_size / 8;
13565
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13566
0
  }
13567
0
    }
13568
13569
  /* If we are linking against a dynamic object, or generating a
13570
     shared library, finish up the dynamic linking information.  */
13571
0
  if (dynamic)
13572
0
    {
13573
0
      bfd_byte *dyncon, *dynconend;
13574
13575
      /* Fix up .dynamic entries.  */
13576
0
      o = htab->dynamic;
13577
0
      BFD_ASSERT (o != NULL);
13578
13579
0
      dyncon = o->contents;
13580
0
      dynconend = PTR_ADD (o->contents, o->size);
13581
0
      for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13582
0
  {
13583
0
    Elf_Internal_Dyn dyn;
13584
0
    const char *name;
13585
0
    unsigned int type;
13586
0
    bfd_size_type sh_size;
13587
0
    bfd_vma sh_addr;
13588
13589
0
    obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13590
13591
0
    switch (dyn.d_tag)
13592
0
      {
13593
0
      default:
13594
0
        continue;
13595
0
      case DT_NULL:
13596
0
        if (relativecount != 0)
13597
0
    {
13598
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13599
0
        {
13600
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13601
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13602
0
        }
13603
0
      if (dyn.d_tag != DT_NULL
13604
0
          && dynconend - dyncon >= obed->s->sizeof_dyn)
13605
0
        {
13606
0
          dyn.d_un.d_val = relativecount;
13607
0
          relativecount = 0;
13608
0
          break;
13609
0
        }
13610
0
      relativecount = 0;
13611
0
    }
13612
0
        if (relr_entsize != 0)
13613
0
    {
13614
0
      if (dynconend - dyncon >= 3 * obed->s->sizeof_dyn)
13615
0
        {
13616
0
          asection *s = htab->srelrdyn;
13617
0
          dyn.d_tag = DT_RELR;
13618
0
          dyn.d_un.d_ptr
13619
0
      = s->output_section->vma + s->output_offset;
13620
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13621
0
          dyncon += obed->s->sizeof_dyn;
13622
13623
0
          dyn.d_tag = DT_RELRSZ;
13624
0
          dyn.d_un.d_val = s->size;
13625
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13626
0
          dyncon += obed->s->sizeof_dyn;
13627
13628
0
          dyn.d_tag = DT_RELRENT;
13629
0
          dyn.d_un.d_val = relr_entsize;
13630
0
          relr_entsize = 0;
13631
0
          break;
13632
0
        }
13633
0
      relr_entsize = 0;
13634
0
    }
13635
0
        continue;
13636
13637
0
      case DT_INIT:
13638
0
        name = info->init_function;
13639
0
        goto get_sym;
13640
0
      case DT_FINI:
13641
0
        name = info->fini_function;
13642
0
      get_sym:
13643
0
        {
13644
0
    struct elf_link_hash_entry *h;
13645
13646
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13647
0
    if (h != NULL
13648
0
        && (h->root.type == bfd_link_hash_defined
13649
0
      || h->root.type == bfd_link_hash_defweak))
13650
0
      {
13651
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13652
0
        o = h->root.u.def.section;
13653
0
        if (o->output_section != NULL)
13654
0
          dyn.d_un.d_ptr += (o->output_section->vma
13655
0
           + o->output_offset);
13656
0
        else
13657
0
          {
13658
      /* The symbol is imported from another shared
13659
         library and does not apply to this one.  */
13660
0
      dyn.d_un.d_ptr = 0;
13661
0
          }
13662
0
        break;
13663
0
      }
13664
0
        }
13665
0
        continue;
13666
13667
0
      case DT_PREINIT_ARRAYSZ:
13668
0
        name = ".preinit_array";
13669
0
        goto get_out_size;
13670
0
      case DT_INIT_ARRAYSZ:
13671
0
        name = ".init_array";
13672
0
        goto get_out_size;
13673
0
      case DT_FINI_ARRAYSZ:
13674
0
        name = ".fini_array";
13675
0
      get_out_size:
13676
0
        o = bfd_get_section_by_name (obfd, name);
13677
0
        if (o == NULL)
13678
0
    {
13679
0
      _bfd_error_handler
13680
0
        (_("could not find section %s"), name);
13681
0
      goto error_return;
13682
0
    }
13683
0
        if (o->size == 0)
13684
0
    _bfd_error_handler
13685
0
      (_("warning: %s section has zero size"), name);
13686
0
        dyn.d_un.d_val = o->size;
13687
0
        break;
13688
13689
0
      case DT_PREINIT_ARRAY:
13690
0
        name = ".preinit_array";
13691
0
        goto get_out_vma;
13692
0
      case DT_INIT_ARRAY:
13693
0
        name = ".init_array";
13694
0
        goto get_out_vma;
13695
0
      case DT_FINI_ARRAY:
13696
0
        name = ".fini_array";
13697
0
      get_out_vma:
13698
0
        o = bfd_get_section_by_name (obfd, name);
13699
0
        goto do_vma;
13700
13701
0
      case DT_HASH:
13702
0
        name = ".hash";
13703
0
        goto get_vma;
13704
0
      case DT_GNU_HASH:
13705
0
        name = ".gnu.hash";
13706
0
        goto get_vma;
13707
0
      case DT_STRTAB:
13708
0
        name = ".dynstr";
13709
0
        goto get_vma;
13710
0
      case DT_SYMTAB:
13711
0
        name = ".dynsym";
13712
0
        goto get_vma;
13713
0
      case DT_VERDEF:
13714
0
        name = ".gnu.version_d";
13715
0
        goto get_vma;
13716
0
      case DT_VERNEED:
13717
0
        name = ".gnu.version_r";
13718
0
        goto get_vma;
13719
0
      case DT_VERSYM:
13720
0
        name = ".gnu.version";
13721
0
      get_vma:
13722
0
        o = bfd_get_linker_section (dynobj, name);
13723
0
      do_vma:
13724
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13725
0
    {
13726
0
      _bfd_error_handler
13727
0
        (_("could not find section %s"), name);
13728
0
      goto error_return;
13729
0
    }
13730
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13731
0
    {
13732
0
      _bfd_error_handler
13733
0
        (_("warning: section '%s' is being made into a note"), name);
13734
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13735
0
      goto error_return;
13736
0
    }
13737
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13738
0
        break;
13739
13740
0
      case DT_REL:
13741
0
      case DT_RELA:
13742
0
      case DT_RELSZ:
13743
0
      case DT_RELASZ:
13744
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13745
0
    type = SHT_REL;
13746
0
        else
13747
0
    type = SHT_RELA;
13748
0
        sh_size = 0;
13749
0
        sh_addr = 0;
13750
0
        for (i = 1; i < elf_numsections (obfd); i++)
13751
0
    {
13752
0
      Elf_Internal_Shdr *hdr;
13753
13754
0
      hdr = elf_elfsections (obfd)[i];
13755
0
      if (hdr->sh_type == type
13756
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13757
0
        {
13758
0
          sh_size += hdr->sh_size;
13759
0
          if (sh_addr == 0
13760
0
        || sh_addr > hdr->sh_addr)
13761
0
      sh_addr = hdr->sh_addr;
13762
0
        }
13763
0
    }
13764
13765
0
        if (obed->dtrel_excludes_plt && htab->srelplt != NULL)
13766
0
    {
13767
0
      unsigned int opb = bfd_octets_per_byte (obfd, o);
13768
13769
      /* Don't count procedure linkage table relocs in the
13770
         overall reloc count.  */
13771
0
      sh_size -= htab->srelplt->size;
13772
0
      if (sh_size == 0)
13773
        /* If the size is zero, make the address zero too.
13774
           This is to avoid a glibc bug.  If the backend
13775
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13776
           zero, then we'll put DT_RELA at the end of
13777
           DT_JMPREL.  glibc will interpret the end of
13778
           DT_RELA matching the end of DT_JMPREL as the
13779
           case where DT_RELA includes DT_JMPREL, and for
13780
           LD_BIND_NOW will decide that processing DT_RELA
13781
           will process the PLT relocs too.  Net result:
13782
           No PLT relocs applied.  */
13783
0
        sh_addr = 0;
13784
13785
      /* If .rela.plt is the first .rela section, exclude
13786
         it from DT_RELA.  */
13787
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13788
0
               + htab->srelplt->output_offset) * opb)
13789
0
        sh_addr += htab->srelplt->size;
13790
0
    }
13791
13792
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13793
0
    dyn.d_un.d_val = sh_size;
13794
0
        else
13795
0
    dyn.d_un.d_ptr = sh_addr;
13796
0
        break;
13797
0
      }
13798
0
    obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13799
0
  }
13800
0
    }
13801
13802
  /* If we have created any dynamic sections, then output them.  */
13803
0
  if (dynobj != NULL)
13804
0
    {
13805
0
      if (!obed->elf_backend_finish_dynamic_sections (info, flinfo.contents))
13806
0
  goto error_return;
13807
13808
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13809
0
      if (bfd_link_textrel_check (info)
13810
0
    && (o = htab->dynamic) != NULL
13811
0
    && o->size != 0)
13812
0
  {
13813
0
    bfd_byte *dyncon, *dynconend;
13814
13815
0
    dyncon = o->contents;
13816
0
    dynconend = o->contents + o->size;
13817
0
    for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13818
0
      {
13819
0
        Elf_Internal_Dyn dyn;
13820
13821
0
        obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13822
13823
0
        if (dyn.d_tag == DT_TEXTREL)
13824
0
    {
13825
0
      if (info->textrel_check == textrel_check_error)
13826
0
        info->callbacks->einfo
13827
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13828
0
      else if (bfd_link_dll (info))
13829
0
        info->callbacks->einfo
13830
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13831
0
      else if (bfd_link_pde (info))
13832
0
        info->callbacks->einfo
13833
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13834
0
      else
13835
0
        info->callbacks->einfo
13836
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13837
0
      break;
13838
0
    }
13839
0
      }
13840
0
  }
13841
13842
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13843
0
  {
13844
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13845
0
        || o->size == 0
13846
0
        || o->output_section == bfd_abs_section_ptr)
13847
0
      continue;
13848
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13849
0
      {
13850
        /* At this point, we are only interested in sections
13851
     created by bfd_elf_link_create_dynamic_sections().  */
13852
0
        continue;
13853
0
      }
13854
0
    if (htab->stab_info.stabstr == o)
13855
0
      continue;
13856
0
    if (htab->eh_info.hdr_sec == o)
13857
0
      continue;
13858
0
    if (strcmp (o->name, ".dynstr") != 0)
13859
0
      {
13860
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13861
0
              * bfd_octets_per_byte (obfd, o));
13862
0
        if (!bfd_set_section_contents (obfd, o->output_section,
13863
0
               o->contents, octets, o->size))
13864
0
    goto error_return;
13865
0
      }
13866
0
    else
13867
0
      {
13868
        /* The contents of the .dynstr section are actually in a
13869
     stringtab.  */
13870
0
        file_ptr off;
13871
13872
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13873
0
        if (bfd_seek (obfd, off, SEEK_SET) != 0
13874
0
      || !_bfd_elf_strtab_emit (obfd, htab->dynstr))
13875
0
    goto error_return;
13876
0
      }
13877
0
  }
13878
0
    }
13879
13880
0
  if (!info->resolve_section_groups)
13881
0
    {
13882
0
      bool failed = false;
13883
13884
0
      BFD_ASSERT (bfd_link_relocatable (info));
13885
0
      bfd_map_over_sections (obfd, bfd_elf_set_group_contents, &failed);
13886
0
      if (failed)
13887
0
  goto error_return;
13888
0
    }
13889
13890
  /* If we have optimized stabs strings, output them.  */
13891
0
  if (htab->stab_info.stabstr != NULL)
13892
0
    {
13893
0
      if (!_bfd_write_stab_strings (obfd, &htab->stab_info))
13894
0
  goto error_return;
13895
0
    }
13896
13897
0
  if (! _bfd_elf_write_section_eh_frame_hdr (obfd, info))
13898
0
    goto error_return;
13899
13900
0
  if (! _bfd_elf_write_section_sframe (obfd, info))
13901
0
    goto error_return;
13902
13903
0
  if (! _bfd_elf_write_section_object_attributes (obfd, info))
13904
0
    goto error_ret2;
13905
13906
0
  if (info->callbacks->emit_ctf)
13907
0
      info->callbacks->emit_ctf ();
13908
13909
0
  elf_final_link_free (obfd, &flinfo);
13910
13911
0
  if (info->unique_symbol)
13912
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13913
0
  return true;
13914
13915
0
 error_return:
13916
0
  free (htab->strtab);
13917
0
  htab->strtab = NULL;
13918
0
  elf_final_link_free (obfd, &flinfo);
13919
0
 error_ret2:
13920
0
  if (info->unique_symbol)
13921
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13922
0
  return false;
13923
0
}
13924

13925
/* Initialize COOKIE for input bfd ABFD.  */
13926
13927
static bool
13928
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13929
0
{
13930
0
  Elf_Internal_Shdr *symtab_hdr;
13931
0
  elf_backend_data *bed;
13932
13933
0
  bed = get_elf_backend_data (abfd);
13934
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13935
13936
0
  cookie->abfd = abfd;
13937
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13938
0
  if (elf_bad_symtab (abfd))
13939
0
    {
13940
0
      cookie->locsymcount = cookie->num_sym;
13941
0
      cookie->extsymoff = 0;
13942
0
    }
13943
0
  else
13944
0
    {
13945
0
      cookie->locsymcount = symtab_hdr->sh_info;
13946
0
      cookie->extsymoff = symtab_hdr->sh_info;
13947
0
    }
13948
13949
0
  if (bed->s->arch_size == 32)
13950
0
    cookie->r_sym_shift = 8;
13951
0
  else
13952
0
    cookie->r_sym_shift = 32;
13953
13954
0
  return true;
13955
0
}
13956
13957
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13958
13959
static void
13960
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13961
       bfd *abfd ATTRIBUTE_UNUSED)
13962
0
{
13963
0
}
13964
13965
/* Initialize the relocation information in COOKIE for input section SEC
13966
   of input bfd ABFD.  */
13967
13968
static bool
13969
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13970
      struct bfd_link_info *info, bfd *abfd,
13971
      asection *sec, bool keep_memory)
13972
0
{
13973
0
  if (sec->reloc_count == 0)
13974
0
    {
13975
0
      cookie->rels = NULL;
13976
0
      cookie->relend = NULL;
13977
0
    }
13978
0
  else
13979
0
    {
13980
0
      cookie->rels = _bfd_elf_link_info_read_relocs
13981
0
  (abfd, info, sec, NULL, NULL,
13982
0
   keep_memory || _bfd_elf_link_keep_memory (info));
13983
0
      if (cookie->rels == NULL)
13984
0
  return false;
13985
0
      cookie->rel = cookie->rels;
13986
0
      cookie->relend = cookie->rels + sec->reloc_count;
13987
0
    }
13988
0
  cookie->rel = cookie->rels;
13989
0
  return true;
13990
0
}
13991
13992
/* Free the memory allocated by init_reloc_cookie_rels,
13993
   if appropriate.  */
13994
13995
static void
13996
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13997
      asection *sec)
13998
0
{
13999
0
  if (elf_section_data (sec)->relocs != cookie->rels)
14000
0
    free (cookie->rels);
14001
0
}
14002
14003
/* Initialize the whole of COOKIE for input section SEC.  */
14004
14005
static bool
14006
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14007
             struct bfd_link_info *info,
14008
             asection *sec, bool keep_memory)
14009
0
{
14010
0
  if (!init_reloc_cookie (cookie, sec->owner))
14011
0
    goto error1;
14012
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
14013
0
             keep_memory))
14014
0
    goto error2;
14015
0
  return true;
14016
14017
0
 error2:
14018
0
  fini_reloc_cookie (cookie, sec->owner);
14019
0
 error1:
14020
0
  return false;
14021
0
}
14022
14023
/* Free the memory allocated by init_reloc_cookie_for_section,
14024
   if appropriate.  */
14025
14026
static void
14027
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14028
             asection *sec)
14029
0
{
14030
0
  fini_reloc_cookie_rels (cookie, sec);
14031
0
  fini_reloc_cookie (cookie, sec->owner);
14032
0
}
14033

14034
/* Garbage collect unused sections.  */
14035
14036
/* Default gc_mark_hook.  */
14037
14038
asection *
14039
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
14040
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
14041
           struct elf_reloc_cookie *cookie,
14042
           struct elf_link_hash_entry *h,
14043
           unsigned int symndx)
14044
0
{
14045
0
  if (h == NULL)
14046
0
    return _bfd_get_local_sym_section (cookie, symndx);
14047
14048
0
  switch (h->root.type)
14049
0
    {
14050
0
    case bfd_link_hash_defined:
14051
0
    case bfd_link_hash_defweak:
14052
0
      return h->root.u.def.section;
14053
14054
0
    case bfd_link_hash_common:
14055
0
      return h->root.u.c.p->section;
14056
14057
0
    default:
14058
0
      return NULL;
14059
0
    }
14060
0
}
14061
14062
/* Return the debug definition section.  */
14063
14064
static asection *
14065
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14066
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14067
         struct elf_reloc_cookie *cookie,
14068
         struct elf_link_hash_entry *h,
14069
         unsigned int symndx)
14070
0
{
14071
0
  if (h != NULL)
14072
0
    {
14073
      /* Return the global debug definition section.  */
14074
0
      if ((h->root.type == bfd_link_hash_defined
14075
0
     || h->root.type == bfd_link_hash_defweak)
14076
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14077
0
  return h->root.u.def.section;
14078
0
    }
14079
0
  else
14080
0
    {
14081
      /* Return the local debug definition section.  */
14082
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14083
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14084
0
  return isec;
14085
0
    }
14086
14087
0
  return NULL;
14088
0
}
14089
14090
/* COOKIE->rel describes a relocation against section SEC, which is
14091
   a section we've decided to keep.  Return the section that contains
14092
   the relocation symbol, or NULL if no section contains it.  */
14093
14094
asection *
14095
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14096
           elf_gc_mark_hook_fn gc_mark_hook,
14097
           struct elf_reloc_cookie *cookie,
14098
           bool *start_stop)
14099
0
{
14100
0
  unsigned long r_symndx;
14101
0
  struct elf_link_hash_entry *h, *hw;
14102
14103
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14104
0
  if (r_symndx == STN_UNDEF)
14105
0
    return NULL;
14106
14107
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14108
0
  if (h == NULL)
14109
0
    {
14110
      /* A corrupt input file can lead to a situation where the index
14111
   does not reference either a local or an external symbol.  */
14112
0
      if (r_symndx >= cookie->locsymcount)
14113
0
  return NULL;
14114
14115
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14116
0
    }
14117
14118
0
  bool was_marked = h->mark;
14119
14120
0
  h->mark = 1;
14121
  /* Keep all aliases of the symbol too.  If an object symbol
14122
     needs to be copied into .dynbss then all of its aliases
14123
     should be present as dynamic symbols, not just the one used
14124
     on the copy relocation.  */
14125
0
  hw = h;
14126
0
  while (hw->is_weakalias)
14127
0
    {
14128
0
      hw = hw->u.alias;
14129
0
      hw->mark = 1;
14130
0
    }
14131
14132
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14133
0
    {
14134
0
      if (info->start_stop_gc)
14135
0
  return NULL;
14136
14137
      /* To work around a glibc bug, mark XXX input sections
14138
   when there is a reference to __start_XXX or __stop_XXX
14139
   symbols.  */
14140
0
      else if (start_stop != NULL)
14141
0
  {
14142
0
    asection *s = h->u2.start_stop_section;
14143
0
    *start_stop = true;
14144
0
    return s;
14145
0
  }
14146
0
    }
14147
14148
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14149
0
}
14150
14151
/* COOKIE->rel describes a relocation against section SEC, which is
14152
   a section we've decided to keep.  Mark the section that contains
14153
   the relocation symbol.  */
14154
14155
bool
14156
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14157
      asection *sec,
14158
      elf_gc_mark_hook_fn gc_mark_hook,
14159
      struct elf_reloc_cookie *cookie)
14160
0
{
14161
0
  asection *rsec;
14162
0
  bool start_stop = false;
14163
14164
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14165
0
  while (rsec != NULL)
14166
0
    {
14167
0
      if (!rsec->gc_mark)
14168
0
  {
14169
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14170
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14171
0
      rsec->gc_mark = 1;
14172
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14173
0
      return false;
14174
0
  }
14175
0
      if (!start_stop)
14176
0
  break;
14177
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14178
0
    }
14179
0
  return true;
14180
0
}
14181
14182
/* The mark phase of garbage collection.  For a given section, mark
14183
   it and any sections in this section's group, and all the sections
14184
   which define symbols to which it refers.  */
14185
14186
bool
14187
_bfd_elf_gc_mark (struct bfd_link_info *info,
14188
      asection *sec,
14189
      elf_gc_mark_hook_fn gc_mark_hook)
14190
0
{
14191
0
  bool ret;
14192
0
  asection *group_sec, *eh_frame, *sframe;
14193
14194
0
  sec->gc_mark = 1;
14195
14196
  /* Mark all the sections in the group.  */
14197
0
  group_sec = elf_section_data (sec)->next_in_group;
14198
0
  if (group_sec && !group_sec->gc_mark)
14199
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14200
0
      return false;
14201
14202
  /* Look through the section relocs.  */
14203
0
  ret = true;
14204
0
  eh_frame = elf_eh_frame_section (sec->owner);
14205
0
  sframe = elf_sframe_section (sec->owner);
14206
14207
0
  if ((sec->flags & SEC_RELOC) != 0
14208
0
      && sec->reloc_count > 0
14209
0
      && sec != eh_frame
14210
0
      && sec != sframe)
14211
0
    {
14212
0
      struct elf_reloc_cookie cookie;
14213
14214
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14215
0
  ret = false;
14216
0
      else
14217
0
  {
14218
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14219
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14220
0
        {
14221
0
    ret = false;
14222
0
    break;
14223
0
        }
14224
0
    fini_reloc_cookie_for_section (&cookie, sec);
14225
0
  }
14226
0
    }
14227
14228
0
  if (ret && eh_frame && elf_fde_list (sec))
14229
0
    {
14230
0
      struct elf_reloc_cookie cookie;
14231
14232
      /* NB: When --no-keep-memory is used, the symbol table and
14233
   relocation info for eh_frame are freed after they are retrieved
14234
   for each text section in the input object.  If an input object
14235
   has many text sections, the same data is retrieved and freed
14236
   many times which can take a very long time.  Always keep the
14237
   symbol table and relocation info for eh_frame to avoid it.  */
14238
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14239
0
            true))
14240
0
  ret = false;
14241
0
      else
14242
0
  {
14243
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14244
0
              gc_mark_hook, &cookie))
14245
0
      ret = false;
14246
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14247
0
  }
14248
0
    }
14249
14250
0
  eh_frame = elf_section_eh_frame_entry (sec);
14251
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14252
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14253
0
      ret = false;
14254
14255
0
  return ret;
14256
0
}
14257
14258
/* Scan and mark sections in a special or debug section group.  */
14259
14260
static void
14261
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14262
0
{
14263
  /* Point to first section of section group.  */
14264
0
  asection *ssec;
14265
  /* Used to iterate the section group.  */
14266
0
  asection *msec;
14267
14268
0
  bool is_special_grp = true;
14269
0
  bool is_debug_grp = true;
14270
14271
  /* First scan to see if group contains any section other than debug
14272
     and special section.  */
14273
0
  ssec = msec = elf_next_in_group (grp);
14274
0
  while (msec != NULL)
14275
0
    {
14276
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14277
0
  is_debug_grp = false;
14278
14279
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14280
0
  is_special_grp = false;
14281
14282
0
      msec = elf_next_in_group (msec);
14283
0
      if (msec == ssec)
14284
0
  break;
14285
0
    }
14286
14287
  /* If this is a pure debug section group or pure special section group,
14288
     keep all sections in this group.  */
14289
0
  if (is_debug_grp || is_special_grp)
14290
0
    {
14291
0
      msec = ssec;
14292
0
      while (msec != NULL)
14293
0
  {
14294
0
    msec->gc_mark = 1;
14295
0
    msec = elf_next_in_group (msec);
14296
0
    if (msec == ssec)
14297
0
      break;
14298
0
  }
14299
0
    }
14300
0
}
14301
14302
/* Keep debug and special sections.  */
14303
14304
bool
14305
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14306
         elf_gc_mark_hook_fn mark_hook)
14307
0
{
14308
0
  bfd *ibfd;
14309
14310
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14311
0
    {
14312
0
      asection *isec;
14313
0
      bool some_kept;
14314
0
      bool debug_frag_seen;
14315
0
      bool has_kept_debug_info;
14316
14317
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14318
0
  continue;
14319
0
      isec = ibfd->sections;
14320
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14321
0
  continue;
14322
14323
      /* Ensure all linker created sections are kept,
14324
   see if any other section is already marked,
14325
   and note if we have any fragmented debug sections.  */
14326
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14327
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14328
0
  {
14329
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14330
0
      isec->gc_mark = 1;
14331
0
    else if (isec->gc_mark
14332
0
       && (isec->flags & SEC_ALLOC) != 0
14333
0
       && elf_section_type (isec) != SHT_NOTE)
14334
0
      some_kept = true;
14335
0
    else
14336
0
      {
14337
        /* Since all sections, except for backend specific ones,
14338
     have been garbage collected, call mark_hook on this
14339
     section if any of its linked-to sections is marked.  */
14340
0
        asection *linked_to_sec;
14341
0
        for (linked_to_sec = elf_linked_to_section (isec);
14342
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14343
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14344
0
    {
14345
0
      if (linked_to_sec->gc_mark)
14346
0
        {
14347
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14348
0
      return false;
14349
0
          break;
14350
0
        }
14351
0
      linked_to_sec->linker_mark = 1;
14352
0
    }
14353
0
        for (linked_to_sec = elf_linked_to_section (isec);
14354
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14355
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14356
0
    linked_to_sec->linker_mark = 0;
14357
0
      }
14358
14359
0
    if (!debug_frag_seen
14360
0
        && (isec->flags & SEC_DEBUGGING)
14361
0
        && startswith (isec->name, ".debug_line."))
14362
0
      debug_frag_seen = true;
14363
0
    else if (strcmp (bfd_section_name (isec),
14364
0
         "__patchable_function_entries") == 0
14365
0
       && elf_linked_to_section (isec) == NULL)
14366
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14367
0
          "need linked-to section "
14368
0
          "for --gc-sections\n"),
14369
0
              isec->owner, isec);
14370
0
  }
14371
14372
      /* If no non-note alloc section in this file will be kept, then
14373
   we can toss out the debug and special sections.  */
14374
0
      if (!some_kept)
14375
0
  continue;
14376
14377
      /* Keep debug and special sections like .comment when they are
14378
   not part of a group.  Also keep section groups that contain
14379
   just debug sections or special sections.  NB: Sections with
14380
   linked-to section has been handled above.  */
14381
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14382
0
  {
14383
0
    if ((isec->flags & SEC_GROUP) != 0)
14384
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14385
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14386
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14387
0
       && elf_next_in_group (isec) == NULL
14388
0
       && elf_linked_to_section (isec) == NULL)
14389
0
      isec->gc_mark = 1;
14390
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14391
0
      has_kept_debug_info = true;
14392
0
  }
14393
14394
      /* Look for CODE sections which are going to be discarded,
14395
   and find and discard any fragmented debug sections which
14396
   are associated with that code section.  */
14397
0
      if (debug_frag_seen)
14398
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14399
0
    if ((isec->flags & SEC_CODE) != 0
14400
0
        && isec->gc_mark == 0)
14401
0
      {
14402
0
        unsigned int ilen;
14403
0
        asection *dsec;
14404
14405
0
        ilen = strlen (isec->name);
14406
14407
        /* Association is determined by the name of the debug
14408
     section containing the name of the code section as
14409
     a suffix.  For example .debug_line.text.foo is a
14410
     debug section associated with .text.foo.  */
14411
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14412
0
    {
14413
0
      unsigned int dlen;
14414
14415
0
      if (dsec->gc_mark == 0
14416
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14417
0
        continue;
14418
14419
0
      dlen = strlen (dsec->name);
14420
14421
0
      if (dlen > ilen
14422
0
          && strncmp (dsec->name + (dlen - ilen),
14423
0
          isec->name, ilen) == 0)
14424
0
        dsec->gc_mark = 0;
14425
0
    }
14426
0
    }
14427
14428
      /* Mark debug sections referenced by kept debug sections.  */
14429
0
      if (has_kept_debug_info)
14430
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14431
0
    if (isec->gc_mark
14432
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14433
0
      if (!_bfd_elf_gc_mark (info, isec,
14434
0
           elf_gc_mark_debug_section))
14435
0
        return false;
14436
0
    }
14437
0
  return true;
14438
0
}
14439
14440
static bool
14441
elf_gc_sweep (struct bfd_link_info *info)
14442
0
{
14443
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14444
0
    {
14445
0
      if (!compatible_format (info, sub))
14446
0
  continue;
14447
14448
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14449
0
  {
14450
    /* When any section in a section group is kept, we keep all
14451
       sections in the section group.  If the first member of
14452
       the section group is excluded, we will also exclude the
14453
       group section.  */
14454
0
    if (o->flags & SEC_GROUP)
14455
0
      {
14456
0
        asection *first = elf_next_in_group (o);
14457
0
        if (first != NULL)
14458
0
    o->gc_mark = first->gc_mark;
14459
0
      }
14460
14461
0
    if (o->gc_mark)
14462
0
      continue;
14463
14464
    /* Skip sweeping sections already excluded.  */
14465
0
    if (o->flags & SEC_EXCLUDE)
14466
0
      continue;
14467
14468
    /* Since this is early in the link process, it is simple
14469
       to remove a section from the output.  */
14470
0
    o->flags |= SEC_EXCLUDE;
14471
14472
0
    if (info->print_gc_sections && o->size != 0)
14473
      /* xgettext:c-format */
14474
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14475
0
        o, sub);
14476
0
  }
14477
0
    }
14478
14479
0
  return true;
14480
0
}
14481
14482
/* Propagate collected vtable information.  This is called through
14483
   elf_link_hash_traverse.  */
14484
14485
static bool
14486
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14487
0
{
14488
  /* Those that are not vtables.  */
14489
0
  if (h->start_stop
14490
0
      || h->u2.vtable == NULL
14491
0
      || h->u2.vtable->parent == NULL)
14492
0
    return true;
14493
14494
  /* Those vtables that do not have parents, we cannot merge.  */
14495
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14496
0
    return true;
14497
14498
  /* If we've already been done, exit.  */
14499
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14500
0
    return true;
14501
14502
  /* Make sure the parent's table is up to date.  */
14503
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14504
14505
0
  if (h->u2.vtable->used == NULL)
14506
0
    {
14507
      /* None of this table's entries were referenced.  Re-use the
14508
   parent's table.  */
14509
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14510
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14511
0
    }
14512
0
  else
14513
0
    {
14514
0
      size_t n;
14515
0
      bool *cu, *pu;
14516
14517
      /* Or the parent's entries into ours.  */
14518
0
      cu = h->u2.vtable->used;
14519
0
      cu[-1] = true;
14520
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14521
0
      if (pu != NULL)
14522
0
  {
14523
0
    elf_backend_data *bed;
14524
0
    unsigned int log_file_align;
14525
14526
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14527
0
    log_file_align = bed->s->log_file_align;
14528
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14529
0
    while (n--)
14530
0
      {
14531
0
        if (*pu)
14532
0
    *cu = true;
14533
0
        pu++;
14534
0
        cu++;
14535
0
      }
14536
0
  }
14537
0
    }
14538
14539
0
  return true;
14540
0
}
14541
14542
struct link_info_ok
14543
{
14544
  struct bfd_link_info *info;
14545
  bool ok;
14546
};
14547
14548
static bool
14549
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14550
            void *ptr)
14551
0
{
14552
0
  asection *sec;
14553
0
  bfd_vma hstart, hend;
14554
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14555
0
  elf_backend_data *bed;
14556
0
  unsigned int log_file_align;
14557
0
  struct link_info_ok *info = ptr;
14558
14559
  /* Take care of both those symbols that do not describe vtables as
14560
     well as those that are not loaded.  */
14561
0
  if (h->start_stop
14562
0
      || h->u2.vtable == NULL
14563
0
      || h->u2.vtable->parent == NULL)
14564
0
    return true;
14565
14566
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14567
0
        || h->root.type == bfd_link_hash_defweak);
14568
14569
0
  sec = h->root.u.def.section;
14570
0
  hstart = h->root.u.def.value;
14571
0
  hend = hstart + h->size;
14572
14573
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14574
0
               sec, NULL, NULL, true);
14575
0
  if (!relstart)
14576
0
    return info->ok = false;
14577
0
  bed = get_elf_backend_data (sec->owner);
14578
0
  log_file_align = bed->s->log_file_align;
14579
14580
0
  relend = relstart + sec->reloc_count;
14581
14582
0
  for (rel = relstart; rel < relend; ++rel)
14583
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14584
0
      {
14585
  /* If the entry is in use, do nothing.  */
14586
0
  if (h->u2.vtable->used
14587
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14588
0
    {
14589
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14590
0
      if (h->u2.vtable->used[entry])
14591
0
        continue;
14592
0
    }
14593
  /* Otherwise, kill it.  */
14594
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14595
0
      }
14596
14597
0
  return true;
14598
0
}
14599
14600
/* Mark sections containing dynamically referenced symbols.  When
14601
   building shared libraries, we must assume that any visible symbol is
14602
   referenced.  */
14603
14604
bool
14605
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14606
0
{
14607
0
  struct bfd_link_info *info = inf;
14608
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14609
14610
0
  if ((h->root.type == bfd_link_hash_defined
14611
0
       || h->root.type == bfd_link_hash_defweak)
14612
0
      && (!h->start_stop
14613
0
    || h->root.ldscript_def
14614
0
    || !info->start_stop_gc)
14615
0
      && ((h->ref_dynamic && !h->forced_local)
14616
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14617
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14618
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14619
0
        && (!bfd_link_executable (info)
14620
0
      || info->gc_keep_exported
14621
0
      || info->export_dynamic
14622
0
      || (h->dynamic
14623
0
          && d != NULL
14624
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14625
0
        && (h->versioned >= versioned
14626
0
      || !bfd_hide_sym_by_version (info->version_info,
14627
0
                 h->root.root.string)))))
14628
0
    h->root.u.def.section->flags |= SEC_KEEP;
14629
14630
0
  return true;
14631
0
}
14632
14633
/* Keep all sections containing symbols undefined on the command-line,
14634
   and the section containing the entry symbol.  */
14635
14636
void
14637
_bfd_elf_gc_keep (struct bfd_link_info *info)
14638
0
{
14639
0
  struct bfd_sym_chain *sym;
14640
14641
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14642
0
    {
14643
0
      struct elf_link_hash_entry *h;
14644
14645
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14646
0
        false, false, false);
14647
14648
0
      if (h != NULL
14649
0
    && (h->root.type == bfd_link_hash_defined
14650
0
        || h->root.type == bfd_link_hash_defweak)
14651
0
    && !bfd_is_const_section (h->root.u.def.section))
14652
0
  h->root.u.def.section->flags |= SEC_KEEP;
14653
0
    }
14654
0
}
14655
14656
bool
14657
bfd_elf_parse_eh_frame_entries (struct bfd_link_info *info)
14658
0
{
14659
0
  bfd *ibfd = info->input_bfds;
14660
14661
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14662
0
    {
14663
0
      asection *sec;
14664
0
      struct elf_reloc_cookie cookie;
14665
14666
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14667
0
  continue;
14668
0
      sec = ibfd->sections;
14669
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14670
0
  continue;
14671
14672
0
      if (!init_reloc_cookie (&cookie, ibfd))
14673
0
  return false;
14674
14675
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14676
0
  {
14677
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14678
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14679
0
           false))
14680
0
      {
14681
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14682
0
        fini_reloc_cookie_rels (&cookie, sec);
14683
0
      }
14684
0
  }
14685
0
    }
14686
0
  return true;
14687
0
}
14688
14689
/* Do mark and sweep of unused sections.  */
14690
14691
bool
14692
bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
14693
0
{
14694
0
  bool ok = true;
14695
0
  elf_gc_mark_hook_fn gc_mark_hook;
14696
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
14697
0
  struct elf_link_hash_table *htab;
14698
0
  struct link_info_ok info_ok;
14699
14700
0
  if (!obed->can_gc_sections
14701
0
      || !is_elf_hash_table (info->hash))
14702
0
    {
14703
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14704
0
      return true;
14705
0
    }
14706
14707
0
  obed->gc_keep (info);
14708
0
  htab = elf_hash_table (info);
14709
14710
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14711
     at the .eh_frame section if we can mark the FDEs individually.  */
14712
0
  for (bfd *sub = info->input_bfds;
14713
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14714
0
       sub = sub->link.next)
14715
0
    {
14716
0
      asection *sec;
14717
0
      struct elf_reloc_cookie cookie;
14718
14719
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14720
0
  continue;
14721
0
      sec = sub->sections;
14722
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14723
0
  continue;
14724
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14725
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14726
0
               false))
14727
0
  {
14728
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14729
0
    if (sec->sec_info
14730
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14731
0
      elf_eh_frame_section (sub) = sec;
14732
0
    fini_reloc_cookie_for_section (&cookie, sec);
14733
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14734
0
  }
14735
14736
      /* Handle .sframe section.  */
14737
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14738
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14739
0
               false))
14740
0
  {
14741
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14742
14743
0
    if (sec->sec_info
14744
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14745
0
      elf_sframe_section (sub) = sec;
14746
14747
0
    fini_reloc_cookie_for_section (&cookie, sec);
14748
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14749
0
  }
14750
0
    }
14751
14752
  /* Apply transitive closure to the vtable entry usage info.  */
14753
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14754
0
  if (!ok)
14755
0
    return false;
14756
14757
  /* Kill the vtable relocations that were not used.  */
14758
0
  info_ok.info = info;
14759
0
  info_ok.ok = true;
14760
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14761
0
  if (!info_ok.ok)
14762
0
    return false;
14763
14764
  /* Mark dynamically referenced symbols.  */
14765
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14766
0
    elf_link_hash_traverse (htab, obed->gc_mark_dynamic_ref, info);
14767
14768
  /* Grovel through relocs to find out who stays ...  */
14769
0
  gc_mark_hook = obed->gc_mark_hook;
14770
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14771
0
    {
14772
0
      if (!compatible_format (info, sub))
14773
0
  continue;
14774
14775
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14776
   Also treat note sections as a root, if the section is not part
14777
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14778
   well as FINI_ARRAY sections for ld -r.  */
14779
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14780
0
  if (!o->gc_mark
14781
0
      && (o->flags & SEC_EXCLUDE) == 0
14782
0
      && ((o->flags & SEC_KEEP) != 0
14783
0
    || (bfd_link_relocatable (info)
14784
0
        && ((elf_section_data (o)->this_hdr.sh_type
14785
0
       == SHT_PREINIT_ARRAY)
14786
0
      || (elf_section_data (o)->this_hdr.sh_type
14787
0
          == SHT_INIT_ARRAY)
14788
0
      || (elf_section_data (o)->this_hdr.sh_type
14789
0
          == SHT_FINI_ARRAY)))
14790
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14791
0
        && elf_next_in_group (o) == NULL
14792
0
        && elf_linked_to_section (o) == NULL)
14793
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14794
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14795
0
    {
14796
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14797
0
        return false;
14798
0
    }
14799
0
    }
14800
14801
  /* Allow the backend to mark additional target specific sections.  */
14802
0
  if (!obed->gc_mark_extra_sections (info, gc_mark_hook))
14803
0
    return false;
14804
14805
  /* ... and mark SEC_EXCLUDE for those that go.  */
14806
0
  return elf_gc_sweep (info);
14807
0
}
14808

14809
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14810
14811
bool
14812
bfd_elf_gc_record_vtinherit (bfd *abfd,
14813
           asection *sec,
14814
           struct elf_link_hash_entry *h,
14815
           bfd_vma offset)
14816
0
{
14817
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14818
0
  struct elf_link_hash_entry **search, *child;
14819
0
  size_t extsymcount;
14820
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14821
14822
  /* The sh_info field of the symtab header tells us where the
14823
     external symbols start.  We don't care about the local symbols at
14824
     this point.  */
14825
0
  extsymcount = elf_symtab_hdr (abfd).sh_size / bed->s->sizeof_sym;
14826
0
  if (!elf_bad_symtab (abfd))
14827
0
    extsymcount -= elf_symtab_hdr (abfd).sh_info;
14828
14829
0
  sym_hashes = elf_sym_hashes (abfd);
14830
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14831
14832
  /* Hunt down the child symbol, which is in this section at the same
14833
     offset as the relocation.  */
14834
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14835
0
    {
14836
0
      if ((child = *search) != NULL
14837
0
    && (child->root.type == bfd_link_hash_defined
14838
0
        || child->root.type == bfd_link_hash_defweak)
14839
0
    && child->root.u.def.section == sec
14840
0
    && child->root.u.def.value == offset)
14841
0
  goto win;
14842
0
    }
14843
14844
  /* xgettext:c-format */
14845
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14846
0
          abfd, sec, (uint64_t) offset);
14847
0
  bfd_set_error (bfd_error_invalid_operation);
14848
0
  return false;
14849
14850
0
 win:
14851
0
  if (!child->u2.vtable)
14852
0
    {
14853
0
      child->u2.vtable = bfd_zalloc (abfd, sizeof (*child->u2.vtable));
14854
0
      if (!child->u2.vtable)
14855
0
  return false;
14856
0
    }
14857
0
  if (!h)
14858
0
    {
14859
      /* This *should* only be the absolute section.  It could potentially
14860
   be that someone has defined a non-global vtable though, which
14861
   would be bad.  It isn't worth paging in the local symbols to be
14862
   sure though; that case should simply be handled by the assembler.  */
14863
14864
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14865
0
    }
14866
0
  else
14867
0
    child->u2.vtable->parent = h;
14868
14869
0
  return true;
14870
0
}
14871
14872
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14873
14874
bool
14875
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14876
         struct elf_link_hash_entry *h,
14877
         bfd_vma addend)
14878
0
{
14879
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14880
0
  unsigned int log_file_align = bed->s->log_file_align;
14881
14882
0
  if (!h || addend > 1u << 28)
14883
0
    {
14884
      /* xgettext:c-format */
14885
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14886
0
        abfd, sec);
14887
0
      bfd_set_error (bfd_error_bad_value);
14888
0
      return false;
14889
0
    }
14890
14891
0
  if (!h->u2.vtable)
14892
0
    {
14893
0
      h->u2.vtable = bfd_zalloc (abfd, sizeof (*h->u2.vtable));
14894
0
      if (!h->u2.vtable)
14895
0
  return false;
14896
0
    }
14897
14898
0
  if (addend >= h->u2.vtable->size)
14899
0
    {
14900
0
      size_t size, bytes, file_align;
14901
0
      bool *ptr = h->u2.vtable->used;
14902
14903
      /* While the symbol is undefined, we have to be prepared to handle
14904
   a zero size.  */
14905
0
      file_align = 1 << log_file_align;
14906
0
      if (h->root.type == bfd_link_hash_undefined)
14907
0
  size = addend + file_align;
14908
0
      else
14909
0
  {
14910
0
    size = h->size;
14911
0
    if (addend >= size)
14912
0
      {
14913
        /* Oops!  We've got a reference past the defined end of
14914
     the table.  This is probably a bug -- shall we warn?  */
14915
0
        size = addend + file_align;
14916
0
      }
14917
0
  }
14918
0
      size = (size + file_align - 1) & -file_align;
14919
14920
      /* Allocate one extra entry for use as a "done" flag for the
14921
   consolidation pass.  */
14922
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14923
14924
0
      if (ptr)
14925
0
  {
14926
0
    ptr = bfd_realloc (ptr - 1, bytes);
14927
14928
0
    if (ptr != NULL)
14929
0
      {
14930
0
        size_t oldbytes;
14931
14932
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14933
0
        * sizeof (bool));
14934
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14935
0
      }
14936
0
  }
14937
0
      else
14938
0
  ptr = bfd_zmalloc (bytes);
14939
14940
0
      if (ptr == NULL)
14941
0
  return false;
14942
14943
      /* And arrange for that done flag to be at index -1.  */
14944
0
      h->u2.vtable->used = ptr + 1;
14945
0
      h->u2.vtable->size = size;
14946
0
    }
14947
14948
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14949
14950
0
  return true;
14951
0
}
14952
14953
/* Map an ELF section header flag to its corresponding string.  */
14954
typedef struct
14955
{
14956
  char *flag_name;
14957
  flagword flag_value;
14958
} elf_flags_to_name_table;
14959
14960
static const elf_flags_to_name_table elf_flags_to_names [] =
14961
{
14962
  { "SHF_WRITE", SHF_WRITE },
14963
  { "SHF_ALLOC", SHF_ALLOC },
14964
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14965
  { "SHF_MERGE", SHF_MERGE },
14966
  { "SHF_STRINGS", SHF_STRINGS },
14967
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14968
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14969
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14970
  { "SHF_GROUP", SHF_GROUP },
14971
  { "SHF_TLS", SHF_TLS },
14972
  { "SHF_MASKOS", SHF_MASKOS },
14973
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14974
};
14975
14976
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14977
bool
14978
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14979
            struct flag_info *flaginfo,
14980
            asection *section)
14981
0
{
14982
0
  const bfd_vma sh_flags = elf_section_flags (section);
14983
14984
0
  if (!flaginfo->flags_initialized)
14985
0
    {
14986
0
      bfd *obfd = info->output_bfd;
14987
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
14988
0
      struct flag_info_list *tf = flaginfo->flag_list;
14989
0
      int with_hex = 0;
14990
0
      int without_hex = 0;
14991
14992
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14993
0
  {
14994
0
    unsigned i;
14995
0
    flagword (*lookup) (char *);
14996
14997
0
    lookup = obed->elf_backend_lookup_section_flags_hook;
14998
0
    if (lookup != NULL)
14999
0
      {
15000
0
        flagword hexval = (*lookup) ((char *) tf->name);
15001
15002
0
        if (hexval != 0)
15003
0
    {
15004
0
      if (tf->with == with_flags)
15005
0
        with_hex |= hexval;
15006
0
      else if (tf->with == without_flags)
15007
0
        without_hex |= hexval;
15008
0
      tf->valid = true;
15009
0
      continue;
15010
0
    }
15011
0
      }
15012
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
15013
0
      {
15014
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
15015
0
    {
15016
0
      if (tf->with == with_flags)
15017
0
        with_hex |= elf_flags_to_names[i].flag_value;
15018
0
      else if (tf->with == without_flags)
15019
0
        without_hex |= elf_flags_to_names[i].flag_value;
15020
0
      tf->valid = true;
15021
0
      break;
15022
0
    }
15023
0
      }
15024
0
    if (!tf->valid)
15025
0
      {
15026
0
        info->callbacks->einfo
15027
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
15028
0
        return false;
15029
0
      }
15030
0
  }
15031
0
      flaginfo->flags_initialized = true;
15032
0
      flaginfo->only_with_flags |= with_hex;
15033
0
      flaginfo->not_with_flags |= without_hex;
15034
0
    }
15035
15036
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
15037
0
    return false;
15038
15039
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
15040
0
    return false;
15041
15042
0
  return true;
15043
0
}
15044
15045
struct alloc_got_off_arg {
15046
  bfd_vma gotoff;
15047
  struct bfd_link_info *info;
15048
};
15049
15050
/* We need a special top-level link routine to convert got reference counts
15051
   to real got offsets.  */
15052
15053
static bool
15054
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15055
0
{
15056
0
  struct alloc_got_off_arg *gofarg = arg;
15057
0
  bfd *obfd = gofarg->info->output_bfd;
15058
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15059
15060
0
  if (h->got.refcount > 0)
15061
0
    {
15062
0
      h->got.offset = gofarg->gotoff;
15063
0
      gofarg->gotoff += obed->got_elt_size (gofarg->info, h, NULL, 0);
15064
0
    }
15065
0
  else
15066
0
    h->got.offset = (bfd_vma) -1;
15067
15068
0
  return true;
15069
0
}
15070
15071
/* And an accompanying bit to work out final got entry offsets once
15072
   we're done.  Should be called from final_link.  */
15073
15074
static bool
15075
bfd_elf_gc_common_finalize_got_offsets (bfd *obfd,
15076
          struct bfd_link_info *info)
15077
0
{
15078
0
  bfd *i;
15079
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15080
0
  bfd_vma gotoff;
15081
0
  struct alloc_got_off_arg gofarg;
15082
15083
0
  BFD_ASSERT (obfd == info->output_bfd);
15084
15085
0
  if (! is_elf_hash_table (info->hash))
15086
0
    return false;
15087
15088
  /* The GOT offset is relative to the .got section, but the GOT header is
15089
     put into the .got.plt section, if the backend uses it.  */
15090
0
  if (obed->want_got_plt)
15091
0
    gotoff = 0;
15092
0
  else
15093
0
    gotoff = obed->got_header_size;
15094
15095
  /* Do the local .got entries first.  */
15096
0
  for (i = info->input_bfds; i; i = i->link.next)
15097
0
    {
15098
0
      bfd_signed_vma *local_got;
15099
0
      size_t j, locsymcount;
15100
0
      Elf_Internal_Shdr *symtab_hdr;
15101
15102
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15103
0
  continue;
15104
15105
0
      local_got = elf_local_got_refcounts (i);
15106
0
      if (!local_got)
15107
0
  continue;
15108
15109
0
      symtab_hdr = &elf_symtab_hdr (i);
15110
0
      if (elf_bad_symtab (i))
15111
0
  locsymcount = symtab_hdr->sh_size / obed->s->sizeof_sym;
15112
0
      else
15113
0
  locsymcount = symtab_hdr->sh_info;
15114
15115
0
      for (j = 0; j < locsymcount; ++j)
15116
0
  {
15117
0
    if (local_got[j] > 0)
15118
0
      {
15119
0
        local_got[j] = gotoff;
15120
0
        gotoff += obed->got_elt_size (info, NULL, i, j);
15121
0
      }
15122
0
    else
15123
0
      local_got[j] = (bfd_vma) -1;
15124
0
  }
15125
0
    }
15126
15127
  /* Then the global .got entries.  .plt refcounts are handled by
15128
     adjust_dynamic_symbol  */
15129
0
  gofarg.gotoff = gotoff;
15130
0
  gofarg.info = info;
15131
0
  elf_link_hash_traverse (elf_hash_table (info),
15132
0
        elf_gc_allocate_got_offsets,
15133
0
        &gofarg);
15134
0
  return true;
15135
0
}
15136
15137
/* Many folk need no more in the way of final link than this, once
15138
   got entry reference counting is enabled.  */
15139
15140
bool
15141
_bfd_elf_gc_common_final_link (bfd *obfd, struct bfd_link_info *info)
15142
0
{
15143
0
  if (!bfd_elf_gc_common_finalize_got_offsets (obfd, info))
15144
0
    return false;
15145
15146
  /* Invoke the regular ELF backend linker to do all the work.  */
15147
0
  return _bfd_elf_final_link (obfd, info);
15148
0
}
15149
15150
bool
15151
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15152
0
{
15153
0
  struct elf_reloc_cookie *rcookie = cookie;
15154
15155
0
  if (elf_bad_symtab (rcookie->abfd))
15156
0
    rcookie->rel = rcookie->rels;
15157
15158
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15159
0
    {
15160
0
      unsigned long r_symndx;
15161
15162
0
      if (!elf_bad_symtab (rcookie->abfd)
15163
0
    && rcookie->rel->r_offset > offset)
15164
0
  return false;
15165
0
      if (rcookie->rel->r_offset != offset)
15166
0
  continue;
15167
15168
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15169
0
      if (r_symndx == STN_UNDEF)
15170
0
  return true;
15171
15172
0
      struct elf_link_hash_entry *h;
15173
15174
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15175
15176
0
      if (h != NULL)
15177
0
  {
15178
0
    if ((h->root.type == bfd_link_hash_defined
15179
0
         || h->root.type == bfd_link_hash_defweak)
15180
0
        && (h->root.u.def.section->owner != rcookie->abfd
15181
0
      || h->root.u.def.section->kept_section != NULL
15182
0
      || discarded_section (h->root.u.def.section)))
15183
0
      return true;
15184
0
  }
15185
0
      else
15186
0
  {
15187
0
    if (r_symndx >= rcookie->locsymcount)
15188
      /* This can happen with corrupt input.  */
15189
0
      return false;
15190
15191
    /* It's not a relocation against a global symbol,
15192
       but it could be a relocation against a local
15193
       symbol for a discarded section.  */
15194
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15195
0
    if (isec != NULL
15196
0
        && (isec->kept_section != NULL
15197
0
      || discarded_section (isec)))
15198
0
      return true;
15199
0
  }
15200
15201
0
      return false;
15202
0
    }
15203
0
  return false;
15204
0
}
15205
15206
/* Discard unneeded references to discarded sections.
15207
   Returns -1 on error, 1 if any section's size was changed, 0 if
15208
   nothing changed.  This function assumes that the relocations are in
15209
   sorted order, which is true for all known assemblers.  */
15210
15211
int
15212
bfd_elf_discard_info (struct bfd_link_info *info)
15213
0
{
15214
0
  struct elf_reloc_cookie cookie;
15215
0
  asection *o;
15216
0
  bfd *abfd;
15217
0
  int changed = 0;
15218
15219
0
  if (info->traditional_format
15220
0
      || !is_elf_hash_table (info->hash))
15221
0
    return 0;
15222
15223
0
  o = bfd_get_section_by_name (info->output_bfd, ".stab");
15224
0
  if (o != NULL)
15225
0
    {
15226
0
      asection *i;
15227
15228
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15229
0
  {
15230
0
    if (i->size == 0
15231
0
        || i->reloc_count == 0
15232
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15233
0
      continue;
15234
15235
0
    abfd = i->owner;
15236
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15237
0
      continue;
15238
15239
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15240
0
      return -1;
15241
15242
0
    if (_bfd_discard_section_stabs (abfd, i,
15243
0
            bfd_elf_reloc_symbol_deleted_p,
15244
0
            &cookie))
15245
0
      changed = 1;
15246
15247
0
    fini_reloc_cookie_for_section (&cookie, i);
15248
0
  }
15249
0
    }
15250
15251
0
  o = NULL;
15252
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15253
0
    o = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
15254
0
  if (o != NULL)
15255
0
    {
15256
0
      asection *i;
15257
0
      int eh_changed = 0;
15258
0
      unsigned int eh_alignment;  /* Octets.  */
15259
15260
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15261
0
  {
15262
0
    int r;
15263
15264
0
    if (i->size == 0)
15265
0
      continue;
15266
15267
0
    abfd = i->owner;
15268
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15269
0
      continue;
15270
15271
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15272
0
      return -1;
15273
15274
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15275
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15276
0
             bfd_elf_reloc_symbol_deleted_p,
15277
0
             &cookie);
15278
0
    if (r)
15279
0
      {
15280
0
        eh_changed = 1;
15281
0
        if (r >= 2)
15282
0
    changed = 1;
15283
0
      }
15284
15285
0
    fini_reloc_cookie_for_section (&cookie, i);
15286
0
  }
15287
15288
0
      eh_alignment = ((1 << o->alignment_power)
15289
0
          * bfd_octets_per_byte (info->output_bfd, o));
15290
      /* Skip over zero terminator, and prevent empty sections from
15291
   adding alignment padding at the end.  */
15292
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15293
0
  if (i->size == 0)
15294
0
    i->flags |= SEC_EXCLUDE;
15295
0
  else if (i->size > 4)
15296
0
    break;
15297
      /* The last non-empty eh_frame section doesn't need padding.  */
15298
0
      if (i != NULL)
15299
0
  i = i->map_tail.s;
15300
      /* Any prior sections must pad the last FDE out to the output
15301
   section alignment.  Otherwise we might have zero padding
15302
   between sections, which would be seen as a terminator.  */
15303
0
      for (; i != NULL; i = i->map_tail.s)
15304
0
  if (i->size == 4)
15305
    /* All but the last zero terminator should have been removed.  */
15306
0
    BFD_FAIL ();
15307
0
  else
15308
0
    {
15309
0
      bfd_size_type size
15310
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15311
0
      if (i->size != size)
15312
0
        {
15313
0
    i->size = size;
15314
0
    changed = 1;
15315
0
    eh_changed = 1;
15316
0
        }
15317
0
    }
15318
0
      if (eh_changed)
15319
0
  elf_link_hash_traverse (elf_hash_table (info),
15320
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15321
0
    }
15322
15323
0
  o = bfd_get_section_by_name (info->output_bfd, ".sframe");
15324
0
  if (o != NULL)
15325
0
    {
15326
0
      asection *i;
15327
15328
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15329
0
  {
15330
0
    if (i->size == 0)
15331
0
      continue;
15332
15333
0
    abfd = i->owner;
15334
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15335
0
      continue;
15336
15337
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15338
0
      return -1;
15339
15340
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15341
0
      {
15342
0
        if (_bfd_elf_discard_section_sframe (i,
15343
0
               bfd_elf_reloc_symbol_deleted_p,
15344
0
               &cookie))
15345
0
    {
15346
0
      if (i->size != i->rawsize)
15347
0
        changed = 1;
15348
0
    }
15349
0
      }
15350
0
    fini_reloc_cookie_for_section (&cookie, i);
15351
0
  }
15352
      /* Update the reference to the output .sframe section.  Used to
15353
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15354
0
      if (!_bfd_elf_set_section_sframe (info->output_bfd, info))
15355
0
  return -1;
15356
0
    }
15357
15358
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15359
0
    {
15360
0
      elf_backend_data *bed;
15361
0
      asection *s;
15362
15363
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15364
0
  continue;
15365
0
      s = abfd->sections;
15366
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15367
0
  continue;
15368
15369
0
      bed = get_elf_backend_data (abfd);
15370
15371
0
      if (bed->elf_backend_discard_info != NULL)
15372
0
  {
15373
0
    if (!init_reloc_cookie (&cookie, abfd))
15374
0
      return -1;
15375
15376
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15377
0
      changed = 1;
15378
15379
0
    fini_reloc_cookie (&cookie, abfd);
15380
0
  }
15381
0
    }
15382
15383
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15384
0
    _bfd_elf_end_eh_frame_parsing (info);
15385
15386
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15387
0
    changed = 1;
15388
15389
0
  return changed;
15390
0
}
15391
15392
bool
15393
_bfd_elf_section_already_linked (bfd *abfd,
15394
         asection *sec,
15395
         struct bfd_link_info *info)
15396
0
{
15397
0
  flagword flags;
15398
0
  const char *name, *key;
15399
0
  struct bfd_section_already_linked *l;
15400
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15401
15402
0
  if (sec->output_section == bfd_abs_section_ptr)
15403
0
    return false;
15404
15405
0
  flags = sec->flags;
15406
15407
  /* Return if it isn't a linkonce section.  A comdat group section
15408
     also has SEC_LINK_ONCE set.  */
15409
0
  if ((flags & SEC_LINK_ONCE) == 0)
15410
0
    return false;
15411
15412
  /* Don't put group member sections on our list of already linked
15413
     sections.  They are handled as a group via their group section.  */
15414
0
  if (elf_sec_group (sec) != NULL)
15415
0
    return false;
15416
15417
  /* For a SHT_GROUP section, use the group signature as the key.  */
15418
0
  name = sec->name;
15419
0
  if ((flags & SEC_GROUP) != 0
15420
0
      && elf_next_in_group (sec) != NULL
15421
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15422
0
    key = elf_group_name (elf_next_in_group (sec));
15423
0
  else
15424
0
    {
15425
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15426
0
      if (startswith (name, ".gnu.linkonce.")
15427
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15428
0
  key++;
15429
0
      else
15430
  /* Must be a user linkonce section that doesn't follow gcc's
15431
     naming convention.  In this case we won't be matching
15432
     single member groups.  */
15433
0
  key = name;
15434
0
    }
15435
15436
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15437
15438
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15439
0
    {
15440
      /* We may have 2 different types of sections on the list: group
15441
   sections with a signature of <key> (<key> is some string),
15442
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15443
   Match like sections.  LTO plugin sections are an exception.
15444
   They are always named .gnu.linkonce.t.<key> and match either
15445
   type of section.  */
15446
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15447
0
     && ((flags & SEC_GROUP) != 0
15448
0
         || strcmp (name, l->sec->name) == 0))
15449
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15450
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15451
0
  {
15452
    /* The section has already been linked.  See if we should
15453
       issue a warning.  */
15454
0
    if (!_bfd_handle_already_linked (sec, l, info))
15455
0
      return false;
15456
15457
0
    if (flags & SEC_GROUP)
15458
0
      {
15459
0
        asection *first = elf_next_in_group (sec);
15460
0
        asection *s = first;
15461
15462
0
        while (s != NULL)
15463
0
    {
15464
0
      s->output_section = bfd_abs_section_ptr;
15465
      /* Record which group discards it.  */
15466
0
      s->kept_section = l->sec;
15467
0
      s = elf_next_in_group (s);
15468
      /* These lists are circular.  */
15469
0
      if (s == first)
15470
0
        break;
15471
0
    }
15472
0
      }
15473
15474
0
    return true;
15475
0
  }
15476
0
    }
15477
15478
  /* A single member comdat group section may be discarded by a
15479
     linkonce section and vice versa.  */
15480
0
  if ((flags & SEC_GROUP) != 0)
15481
0
    {
15482
0
      asection *first = elf_next_in_group (sec);
15483
15484
0
      if (first != NULL && elf_next_in_group (first) == first)
15485
  /* Check this single member group against linkonce sections.  */
15486
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15487
0
    if ((l->sec->flags & SEC_GROUP) == 0
15488
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15489
0
      {
15490
0
        first->output_section = bfd_abs_section_ptr;
15491
0
        first->kept_section = l->sec;
15492
0
        sec->output_section = bfd_abs_section_ptr;
15493
0
        break;
15494
0
      }
15495
0
    }
15496
0
  else
15497
    /* Check this linkonce section against single member groups.  */
15498
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15499
0
      if (l->sec->flags & SEC_GROUP)
15500
0
  {
15501
0
    asection *first = elf_next_in_group (l->sec);
15502
15503
0
    if (first != NULL
15504
0
        && elf_next_in_group (first) == first
15505
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15506
0
      {
15507
0
        sec->output_section = bfd_abs_section_ptr;
15508
0
        sec->kept_section = first;
15509
0
        break;
15510
0
      }
15511
0
  }
15512
15513
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15514
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15515
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15516
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15517
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15518
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15519
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15520
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15521
     The reverse order cannot happen as there is never a bfd with only the
15522
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15523
     matter as here were are looking only for cross-bfd sections.  */
15524
15525
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15526
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15527
0
      if ((l->sec->flags & SEC_GROUP) == 0
15528
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15529
0
  {
15530
0
    if (abfd != l->sec->owner)
15531
0
      sec->output_section = bfd_abs_section_ptr;
15532
0
    break;
15533
0
  }
15534
15535
  /* This is the first section with this name.  Record it.  */
15536
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15537
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15538
0
  return sec->output_section == bfd_abs_section_ptr;
15539
0
}
15540
15541
bool
15542
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15543
0
{
15544
0
  return sym->st_shndx == SHN_COMMON;
15545
0
}
15546
15547
unsigned int
15548
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15549
0
{
15550
0
  return SHN_COMMON;
15551
0
}
15552
15553
asection *
15554
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15555
0
{
15556
0
  return bfd_com_section_ptr;
15557
0
}
15558
15559
bfd_vma
15560
_bfd_elf_default_got_elt_size (struct bfd_link_info *info,
15561
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15562
             bfd *ibfd ATTRIBUTE_UNUSED,
15563
             unsigned long symndx ATTRIBUTE_UNUSED)
15564
0
{
15565
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15566
0
  return obed->s->arch_size / 8;
15567
0
}
15568
15569
/* Routines to support the creation of dynamic relocs.  */
15570
15571
/* Returns the name of the dynamic reloc section associated with SEC.  */
15572
15573
static char *
15574
get_dynamic_reloc_section_name (bfd *abfd,
15575
        asection *sec,
15576
        bool is_rela)
15577
0
{
15578
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15579
0
  size_t plen = is_rela ? 5 : 4;
15580
0
  const char *old_name = bfd_section_name (sec);
15581
0
  size_t nlen = strlen (old_name);
15582
0
  char *name = bfd_alloc (abfd, plen + nlen + 1);
15583
0
  if (name == NULL)
15584
0
    return NULL;
15585
15586
0
  memcpy (name, prefix, plen);
15587
0
  memcpy (name + plen, old_name, nlen + 1);
15588
0
  return name;
15589
0
}
15590
15591
/* Returns the dynamic reloc section associated with SEC.  If the
15592
   section does not exist it is created and attached to the DYNOBJ
15593
   bfd and stored in the SRELOC field of SEC's elf_section_data
15594
   structure.
15595
15596
   ALIGNMENT is the alignment for the newly created section and
15597
   IS_RELA defines whether the name should be .rela.<SEC's name>
15598
   or .rel.<SEC's name>.  The section name is looked up in the
15599
   string table associated with ABFD.  */
15600
15601
asection *
15602
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15603
             bfd *dynobj,
15604
             unsigned int alignment,
15605
             bfd *abfd,
15606
             bool is_rela)
15607
0
{
15608
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15609
15610
0
  if (reloc_sec == NULL)
15611
0
    {
15612
0
      bool known = (dynobj != NULL
15613
0
        && (get_elf_backend_data (dynobj)
15614
0
      ->relocs_compatible (dynobj->xvec, abfd->xvec)));
15615
0
      BFD_ASSERT (known);
15616
0
      if (!known
15617
0
    || dynobj->xvec->byteorder != abfd->xvec->byteorder)
15618
0
  {
15619
0
    _bfd_error_handler (_("%s dynamic relocs incompatible with%s%s output"),
15620
0
            bfd_get_target (abfd),
15621
0
            dynobj ? " " : "",
15622
0
            dynobj ? bfd_get_target (dynobj) : "");
15623
0
    return NULL;
15624
0
  }
15625
15626
0
      char *name = get_dynamic_reloc_section_name (dynobj, sec, is_rela);
15627
0
      if (name == NULL)
15628
0
  return NULL;
15629
15630
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15631
0
      if (reloc_sec != NULL)
15632
0
  bfd_release (dynobj, name);
15633
0
      else
15634
0
  {
15635
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15636
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15637
0
    if ((sec->flags & SEC_ALLOC) != 0)
15638
0
      flags |= SEC_ALLOC | SEC_LOAD;
15639
15640
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15641
0
    if (reloc_sec != NULL)
15642
0
      {
15643
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15644
     name.  Override as it may be wrong, eg. for a user
15645
     section named "auto" we'll get ".relauto" which is
15646
     seen to be a .rela section.  */
15647
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15648
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15649
0
    reloc_sec = NULL;
15650
0
      }
15651
0
  }
15652
15653
0
      elf_section_data (sec)->sreloc = reloc_sec;
15654
0
    }
15655
15656
0
  return reloc_sec;
15657
0
}
15658
15659
/* Copy the ELF symbol type and other attributes for a linker script
15660
   assignment from HSRC to HDEST.  Generally this should be treated as
15661
   if we found a strong non-dynamic definition for HDEST (except that
15662
   ld ignores multiple definition errors).  */
15663
void
15664
_bfd_elf_copy_link_hash_symbol_type (bfd *obfd,
15665
             struct bfd_link_hash_entry *hdest,
15666
             struct bfd_link_hash_entry *hsrc)
15667
0
{
15668
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15669
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15670
15671
0
  ehdest->type = ehsrc->type;
15672
0
  ehdest->target_internal = ehsrc->target_internal;
15673
15674
0
  elf_merge_st_other (obfd, obfd, ehdest, ehsrc->other, NULL,
15675
0
          true, false);
15676
0
}
15677
15678
/* Append a RELA relocation REL to section S.  */
15679
15680
void
15681
_bfd_elf_append_rela (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15682
0
{
15683
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15684
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rela);
15685
0
  BFD_ASSERT (loc + obed->s->sizeof_rela <= s->contents + s->size);
15686
0
  obed->s->swap_reloca_out (obfd, rel, loc);
15687
0
}
15688
15689
/* Append a REL relocation REL to section S.  */
15690
15691
void
15692
_bfd_elf_append_rel (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15693
0
{
15694
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15695
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rel);
15696
0
  BFD_ASSERT (loc + obed->s->sizeof_rel <= s->contents + s->size);
15697
0
  obed->s->swap_reloc_out (obfd, rel, loc);
15698
0
}
15699
15700
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15701
15702
struct bfd_link_hash_entry *
15703
bfd_elf_define_start_stop (struct bfd_link_info *info,
15704
         const char *symbol, asection *sec)
15705
0
{
15706
0
  struct elf_link_hash_entry *h;
15707
15708
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15709
0
          false, false, true);
15710
  /* NB: Common symbols will be turned into definition later.  */
15711
0
  if (h != NULL
15712
0
      && !h->root.ldscript_def
15713
0
      && (h->root.type == bfd_link_hash_undefined
15714
0
    || h->root.type == bfd_link_hash_undefweak
15715
0
    || ((h->ref_regular || h->def_dynamic)
15716
0
        && !h->def_regular
15717
0
        && h->root.type != bfd_link_hash_common)))
15718
0
    {
15719
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15720
0
      h->verinfo.verdef = NULL;
15721
0
      h->root.type = bfd_link_hash_defined;
15722
0
      h->root.u.def.section = sec;
15723
0
      h->root.u.def.value = 0;
15724
0
      h->def_regular = 1;
15725
0
      h->def_dynamic = 0;
15726
0
      h->start_stop = 1;
15727
0
      h->u2.start_stop_section = sec;
15728
0
      if (symbol[0] == '.')
15729
0
  {
15730
    /* .startof. and .sizeof. symbols are local.  */
15731
0
    elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15732
0
    obed->elf_backend_hide_symbol (info, h, true);
15733
0
  }
15734
0
      else
15735
0
  {
15736
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15737
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15738
0
      | info->start_stop_visibility);
15739
0
    if (was_dynamic)
15740
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15741
0
  }
15742
0
      return &h->root;
15743
0
    }
15744
0
  return NULL;
15745
0
}
15746
15747
/* Find dynamic relocs for H that apply to read-only sections.  */
15748
15749
asection *
15750
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15751
0
{
15752
0
  struct elf_dyn_relocs *p;
15753
15754
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15755
0
    {
15756
0
      asection *s = p->sec->output_section;
15757
15758
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15759
0
  return p->sec;
15760
0
    }
15761
0
  return NULL;
15762
0
}
15763
15764
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15765
   read-only sections.  */
15766
15767
bool
15768
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15769
0
{
15770
0
  asection *sec;
15771
15772
0
  if (h->root.type == bfd_link_hash_indirect)
15773
0
    return true;
15774
15775
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15776
0
  if (sec != NULL)
15777
0
    {
15778
0
      struct bfd_link_info *info = inf;
15779
15780
0
      info->flags |= DF_TEXTREL;
15781
      /* xgettext:c-format */
15782
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15783
0
        "in read-only section `%pA'\n"),
15784
0
            sec->owner, h->root.root.string, sec);
15785
15786
0
      if (bfd_link_textrel_check (info))
15787
  /* xgettext:c-format */
15788
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15789
0
          "in read-only section `%pA'\n"),
15790
0
        sec->owner, h->root.root.string, sec);
15791
15792
      /* Not an error, just cut short the traversal.  */
15793
0
      return false;
15794
0
    }
15795
0
  return true;
15796
0
}
15797
15798
/* Add dynamic tags.  */
15799
15800
bool
15801
_bfd_elf_add_dynamic_tags (struct bfd_link_info *info, bool need_dynamic_reloc)
15802
0
{
15803
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15804
15805
0
  if (htab->dynamic_sections_created)
15806
0
    {
15807
      /* Add some entries to the .dynamic section.  We fill in the
15808
   values later, in finish_dynamic_sections, but we must add
15809
   the entries now so that we get the correct size for the
15810
   .dynamic section.  The DT_DEBUG entry is filled in by the
15811
   dynamic linker and used by the debugger.  */
15812
0
#define add_dynamic_entry(TAG, VAL) \
15813
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15814
15815
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15816
15817
0
      if (bfd_link_executable (info))
15818
0
  {
15819
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15820
0
      return false;
15821
0
  }
15822
15823
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15824
0
  {
15825
    /* DT_PLTGOT is used by prelink even if there is no PLT
15826
       relocation.  */
15827
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15828
0
      return false;
15829
0
  }
15830
15831
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15832
0
  {
15833
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15834
0
        || !add_dynamic_entry (DT_PLTREL,
15835
0
             (obed->rela_plts_and_copies_p
15836
0
              ? DT_RELA : DT_REL))
15837
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15838
0
      return false;
15839
0
  }
15840
15841
0
      if (htab->tlsdesc_plt
15842
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15843
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15844
0
  return false;
15845
15846
0
      if (need_dynamic_reloc)
15847
0
  {
15848
0
    if (obed->rela_plts_and_copies_p)
15849
0
      {
15850
0
        if (!add_dynamic_entry (DT_RELA, 0)
15851
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15852
0
      || !add_dynamic_entry (DT_RELAENT,
15853
0
           obed->s->sizeof_rela))
15854
0
    return false;
15855
0
      }
15856
0
    else
15857
0
      {
15858
0
        if (!add_dynamic_entry (DT_REL, 0)
15859
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15860
0
      || !add_dynamic_entry (DT_RELENT,
15861
0
           obed->s->sizeof_rel))
15862
0
    return false;
15863
0
      }
15864
15865
    /* If any dynamic relocs apply to a read-only section,
15866
       then we need a DT_TEXTREL entry.  */
15867
0
    if ((info->flags & DF_TEXTREL) == 0)
15868
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15869
0
            info);
15870
15871
0
    if ((info->flags & DF_TEXTREL) != 0)
15872
0
      {
15873
0
        if (htab->ifunc_resolvers)
15874
0
    info->callbacks->einfo
15875
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15876
0
         "may result in a segfault at runtime; recompile with %s\n"),
15877
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15878
15879
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15880
0
    return false;
15881
0
      }
15882
0
  }
15883
0
    }
15884
0
#undef add_dynamic_entry
15885
15886
0
  return true;
15887
0
}