Coverage Report

Created: 2026-11-21 09:00

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
  BFD_ASSERT (htab->dynobj != NULL);
311
0
  if (htab->dynobj == NULL)
312
0
    {
313
      /* We need to find an input file of the same format as the
314
   output to hold linker created sections.  */
315
0
      bfd *ibfd;
316
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
317
0
  if (compatible_format (info, ibfd))
318
0
    break;
319
0
      if (ibfd == NULL && report)
320
0
  _bfd_error_handler (_("no %s input object found"),
321
0
          bfd_get_target (info->output_bfd));
322
0
      htab->dynobj = ibfd;
323
0
    }
324
325
0
  return htab->dynobj;
326
0
}
327
328
/* Return the strtab for dynamic symbol names.  Create it if
329
   necessary.  */
330
331
static struct elf_strtab_hash *
332
_bfd_elf_link_dynstr (struct bfd_link_info *info)
333
0
{
334
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
335
336
0
  if (htab->dynstr == NULL)
337
0
    htab->dynstr = _bfd_elf_strtab_init ();
338
0
  return htab->dynstr;
339
0
}
340
341
/* Create some sections which will be filled in with dynamic linking
342
   information.  The dynamic sections take up virtual memory space
343
   when the final executable is run, so we need to create them before
344
   addresses are assigned to the output sections.  We work out the
345
   actual contents and size of these sections later.  */
346
347
bool
348
bfd_elf_link_create_dynamic_sections (struct bfd_link_info *info)
349
0
{
350
0
  flagword flags;
351
0
  asection *s;
352
0
  bfd *dynobj;
353
0
  elf_backend_data *obed;
354
0
  struct elf_link_hash_entry *h;
355
356
0
  if (! is_elf_hash_table (info->hash))
357
0
    return false;
358
359
0
  if (elf_hash_table (info)->dynamic_sections_created)
360
0
    return true;
361
362
0
  if (!_bfd_elf_link_dynstr (info))
363
0
    return false;
364
365
0
  dynobj = _bfd_elf_link_dynobj (info);
366
0
  if (dynobj == NULL)
367
0
    return false;
368
0
  obed = get_elf_backend_data (dynobj);
369
370
0
  flags = obed->dynamic_sec_flags;
371
372
  /* A dynamically linked executable has a .interp section, but a
373
     shared library does not.  */
374
0
  if (bfd_link_executable (info) && !info->nointerp)
375
0
    {
376
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".interp",
377
0
                flags | SEC_READONLY);
378
0
      if (s == NULL)
379
0
  return false;
380
0
      elf_hash_table (info)->interp = s;
381
0
    }
382
383
  /* Create sections to hold version informations.  These are removed
384
     if they are not needed.  */
385
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_d",
386
0
            flags | SEC_READONLY);
387
0
  if (s == NULL
388
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
389
0
    return false;
390
391
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version",
392
0
            flags | SEC_READONLY);
393
0
  if (s == NULL
394
0
      || !bfd_set_section_alignment (s, 1))
395
0
    return false;
396
397
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.version_r",
398
0
            flags | SEC_READONLY);
399
0
  if (s == NULL
400
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
401
0
    return false;
402
403
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynsym",
404
0
            flags | SEC_READONLY);
405
0
  if (s == NULL
406
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
407
0
    return false;
408
0
  elf_hash_table (info)->dynsym = s;
409
410
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynstr",
411
0
            flags | SEC_READONLY);
412
0
  if (s == NULL)
413
0
    return false;
414
415
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".dynamic", flags);
416
0
  if (s == NULL
417
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
418
0
    return false;
419
0
  elf_hash_table (info)->dynamic = s;
420
421
  /* The special symbol _DYNAMIC is always set to the start of the
422
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
423
     only want to define it if we are, in fact, creating a .dynamic
424
     section.  We don't want to define it if there is no .dynamic
425
     section, since on some ELF platforms the start up code examines it
426
     to decide how to initialize the process.  */
427
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, s, "_DYNAMIC");
428
0
  elf_hash_table (info)->hdynamic = h;
429
0
  if (h == NULL)
430
0
    return false;
431
432
0
  if (info->emit_hash)
433
0
    {
434
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".hash",
435
0
                flags | SEC_READONLY);
436
0
      if (s == NULL
437
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
438
0
  return false;
439
0
      elf_section_data (s)->this_hdr.sh_entsize = obed->s->sizeof_hash_entry;
440
0
    }
441
442
0
  if (info->emit_gnu_hash && obed->record_xhash_symbol == NULL)
443
0
    {
444
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".gnu.hash",
445
0
                flags | SEC_READONLY);
446
0
      if (s == NULL
447
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
448
0
  return false;
449
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
450
   4 32-bit words followed by variable count of 64-bit words, then
451
   variable count of 32-bit words.  */
452
0
      if (obed->s->arch_size == 64)
453
0
  elf_section_data (s)->this_hdr.sh_entsize = 0;
454
0
      else
455
0
  elf_section_data (s)->this_hdr.sh_entsize = 4;
456
0
    }
457
458
0
  if (info->enable_dt_relr)
459
0
    {
460
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".relr.dyn",
461
0
                flags | SEC_READONLY);
462
0
      if (s == NULL
463
0
    || !bfd_set_section_alignment (s, obed->s->log_file_align))
464
0
  return false;
465
0
      elf_hash_table (info)->srelrdyn = s;
466
0
    }
467
468
  /* Let the backend create the rest of the sections.  This lets the
469
     backend set the right flags.  The backend will normally create
470
     the .got and .plt sections.  */
471
0
  if (obed->elf_backend_create_dynamic_sections == NULL
472
0
      || !obed->elf_backend_create_dynamic_sections (dynobj, info))
473
0
    return false;
474
475
0
  elf_hash_table (info)->dynamic_sections_created = true;
476
477
0
  return true;
478
0
}
479
480
/* Create dynamic sections when linking against a dynamic object.  */
481
482
bool
483
_bfd_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
484
0
{
485
0
  flagword flags, pltflags;
486
0
  struct elf_link_hash_entry *h;
487
0
  asection *s;
488
0
  elf_backend_data *obed = get_elf_backend_data (dynobj);
489
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
490
491
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
492
     .rel[a].bss sections.  */
493
0
  flags = obed->dynamic_sec_flags;
494
495
0
  pltflags = flags;
496
0
  if (obed->plt_not_loaded)
497
    /* We do not clear SEC_ALLOC here because we still want the OS to
498
       allocate space for the section; it's just that there's nothing
499
       to read in from the object file.  */
500
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
501
0
  else
502
0
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
503
0
  if (obed->plt_readonly)
504
0
    pltflags |= SEC_READONLY;
505
506
0
  s = bfd_make_section_anyway_with_flags (dynobj, ".plt", pltflags);
507
0
  if (s == NULL
508
0
      || !bfd_set_section_alignment (s, obed->plt_alignment))
509
0
    return false;
510
0
  htab->splt = s;
511
512
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
513
     .plt section.  */
514
0
  if (obed->want_plt_sym)
515
0
    {
516
0
      h = _bfd_elf_define_linkage_sym (dynobj, info, s,
517
0
               "_PROCEDURE_LINKAGE_TABLE_");
518
0
      elf_hash_table (info)->hplt = h;
519
0
      if (h == NULL)
520
0
  return false;
521
0
    }
522
523
0
  s = bfd_make_section_anyway_with_flags (dynobj,
524
0
            (obed->rela_plts_and_copies_p
525
0
             ? ".rela.plt" : ".rel.plt"),
526
0
            flags | SEC_READONLY);
527
0
  if (s == NULL
528
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
529
0
    return false;
530
0
  htab->srelplt = s;
531
532
0
  if (! _bfd_elf_create_got_section (dynobj, info))
533
0
    return false;
534
535
0
  if (obed->want_dynbss)
536
0
    {
537
      /* The .dynbss section is a place to put symbols which are defined
538
   by dynamic objects, are referenced by regular objects, and are
539
   not functions.  We must allocate space for them in the process
540
   image and use a R_*_COPY reloc to tell the dynamic linker to
541
   initialize them at run time.  The linker script puts the .dynbss
542
   section into the .bss section of the final image.  */
543
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".dynbss",
544
0
                SEC_ALLOC | SEC_LINKER_CREATED);
545
0
      if (s == NULL)
546
0
  return false;
547
0
      htab->sdynbss = s;
548
549
0
      if (obed->want_dynrelro)
550
0
  {
551
    /* Similarly, but for symbols that were originally in read-only
552
       sections.  This section doesn't really need to have contents,
553
       but make it like other .data.rel.ro sections.  */
554
0
    s = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro",
555
0
              flags);
556
0
    if (s == NULL)
557
0
      return false;
558
0
    htab->sdynrelro = s;
559
0
  }
560
561
      /* The .rel[a].bss section holds copy relocs.  This section is not
562
   normally needed.  We need to create it here, though, so that the
563
   linker will map it to an output section.  We can't just create it
564
   only if we need it, because we will not know whether we need it
565
   until we have seen all the input files, and the first time the
566
   main linker code calls BFD after examining all the input files
567
   (size_dynamic_sections) the input sections have already been
568
   mapped to the output sections.  If the section turns out not to
569
   be needed, we can discard it later.  We will never need this
570
   section when generating a shared object, since they do not use
571
   copy relocs.  */
572
0
      if (bfd_link_executable (info))
573
0
  {
574
0
    s = bfd_make_section_anyway_with_flags (dynobj,
575
0
              (obed->rela_plts_and_copies_p
576
0
               ? ".rela.bss" : ".rel.bss"),
577
0
              flags | SEC_READONLY);
578
0
    if (s == NULL
579
0
        || !bfd_set_section_alignment (s, obed->s->log_file_align))
580
0
      return false;
581
0
    htab->srelbss = s;
582
583
0
    if (obed->want_dynrelro)
584
0
      {
585
0
        s = (bfd_make_section_anyway_with_flags
586
0
       (dynobj, (obed->rela_plts_and_copies_p
587
0
           ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
588
0
        flags | SEC_READONLY));
589
0
        if (s == NULL
590
0
      || !bfd_set_section_alignment (s, obed->s->log_file_align))
591
0
    return false;
592
0
        htab->sreldynrelro = s;
593
0
      }
594
0
  }
595
0
    }
596
597
0
  return true;
598
0
}
599

600
/* Record a new dynamic symbol.  We record the dynamic symbols as we
601
   read the input files, since we need to have a list of all of them
602
   before we can determine the final sizes of the output sections.
603
   Note that we may actually call this function even though we are not
604
   going to output any dynamic symbols; in some cases we know that a
605
   symbol should be in the dynamic symbol table, but only if there is
606
   one.  */
607
608
bool
609
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
610
            struct elf_link_hash_entry *h)
611
0
{
612
0
  if (h->dynindx == -1)
613
0
    {
614
0
      struct elf_strtab_hash *dynstr;
615
0
      const char *p;
616
0
      const char *name;
617
0
      size_t indx;
618
619
0
      if (h->root.type == bfd_link_hash_defined
620
0
    || h->root.type == bfd_link_hash_defweak)
621
0
  {
622
    /* An IR symbol should not be made dynamic.  */
623
0
    if (h->root.u.def.section != NULL
624
0
        && h->root.u.def.section->owner != NULL
625
0
        && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
626
0
      return true;
627
0
  }
628
629
      /* XXX: The ABI draft says the linker must turn hidden and
630
   internal symbols into STB_LOCAL symbols when producing the
631
   DSO. However, if ld.so honors st_other in the dynamic table,
632
   this would not be necessary.  */
633
0
      switch (ELF_ST_VISIBILITY (h->other))
634
0
  {
635
0
  case STV_INTERNAL:
636
0
  case STV_HIDDEN:
637
0
    if (h->root.type != bfd_link_hash_undefined
638
0
        && h->root.type != bfd_link_hash_undefweak)
639
0
      {
640
0
        h->forced_local = 1;
641
0
        return true;
642
0
      }
643
644
0
  default:
645
0
    break;
646
0
  }
647
648
0
      h->dynindx = elf_hash_table (info)->dynsymcount;
649
0
      if (h->forced_local)
650
0
  elf_hash_table (info)->has_local_dynsyms = true;
651
0
      ++elf_hash_table (info)->dynsymcount;
652
653
0
      dynstr = _bfd_elf_link_dynstr (info);
654
0
      if (dynstr == NULL)
655
0
  return false;
656
657
0
      char *unversioned_name = NULL;
658
659
      /* We don't put any version information in the dynamic string
660
   table.  */
661
0
      name = h->root.root.string;
662
0
      p = strchr (name, ELF_VER_CHR);
663
0
      if (p != NULL)
664
0
  {
665
0
    unversioned_name = bfd_malloc (p - name + 1);
666
0
    if (unversioned_name == NULL)
667
0
      return false;
668
0
    memcpy (unversioned_name, name, p - name);
669
0
    unversioned_name[p - name] = 0;
670
0
    name = unversioned_name;
671
0
  }
672
673
0
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
674
675
0
      if (p != NULL)
676
0
  free (unversioned_name);
677
678
0
      if (indx == (size_t) -1)
679
0
  return false;
680
0
      h->dynstr_index = indx;
681
0
    }
682
683
0
  return true;
684
0
}
685

686
/* Mark a symbol dynamic.  */
687
688
static void
689
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
690
          struct elf_link_hash_entry *h,
691
          Elf_Internal_Sym *sym)
692
0
{
693
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
694
695
  /* It may be called more than once on the same H.  */
696
0
  if(h->dynamic || bfd_link_relocatable (info))
697
0
    return;
698
699
0
  if ((info->dynamic_data
700
0
       && (h->type == STT_OBJECT
701
0
     || h->type == STT_COMMON
702
0
     || (sym != NULL
703
0
         && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
704
0
       || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
705
0
      || (d != NULL
706
0
    && h->non_elf
707
0
    && (*d->match) (&d->head, NULL, h->root.root.string)))
708
0
    {
709
0
      h->dynamic = 1;
710
      /* NB: If a symbol is made dynamic by --dynamic-list, it has
711
   non-IR reference.  */
712
0
      h->root.non_ir_ref_dynamic = 1;
713
0
    }
714
0
}
715
716
/* Record an assignment to a symbol made by a linker script.  We need
717
   this in case some dynamic object refers to this symbol.  */
718
719
bool
720
bfd_elf_record_link_assignment (struct bfd_link_info *info,
721
        const char *name,
722
        bool provide,
723
        bool hidden)
724
0
{
725
0
  struct elf_link_hash_entry *h, *hv;
726
0
  struct elf_link_hash_table *htab;
727
0
  elf_backend_data *obed;
728
729
0
  if (!is_elf_hash_table (info->hash))
730
0
    return true;
731
732
0
  htab = elf_hash_table (info);
733
0
  h = elf_link_hash_lookup (htab, name, !provide, true, false);
734
0
  if (h == NULL)
735
0
    return provide;
736
737
0
  if (h->root.type == bfd_link_hash_warning)
738
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
739
740
0
  if (h->versioned == unknown)
741
0
    {
742
      /* Set versioned if symbol version is unknown.  */
743
0
      const char *version = strrchr (name, ELF_VER_CHR);
744
0
      if (version)
745
0
  {
746
0
    if (version > name && version[-1] != ELF_VER_CHR)
747
0
      h->versioned = versioned_hidden;
748
0
    else
749
0
      h->versioned = versioned;
750
0
  }
751
0
    }
752
753
  /* Symbols defined in a linker script but not referenced anywhere
754
     else will have non_elf set.  */
755
0
  if (h->non_elf)
756
0
    {
757
0
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
758
0
      h->non_elf = 0;
759
0
    }
760
761
0
  switch (h->root.type)
762
0
    {
763
0
    case bfd_link_hash_defined:
764
0
    case bfd_link_hash_defweak:
765
0
    case bfd_link_hash_common:
766
0
      break;
767
0
    case bfd_link_hash_undefweak:
768
0
    case bfd_link_hash_undefined:
769
      /* Since we're defining the symbol, don't let it seem to have not
770
   been defined.  record_dynamic_symbol and size_dynamic_sections
771
   may depend on this.  */
772
0
      h->root.type = bfd_link_hash_new;
773
0
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
774
0
  bfd_link_repair_undef_list (&htab->root);
775
0
      break;
776
0
    case bfd_link_hash_new:
777
0
      break;
778
0
    case bfd_link_hash_indirect:
779
      /* We had a versioned symbol in a dynamic library.  We make the
780
   the versioned symbol point to this one.  */
781
0
      obed = get_elf_backend_data (info->output_bfd);
782
0
      hv = h;
783
0
      while (hv->root.type == bfd_link_hash_indirect
784
0
       || hv->root.type == bfd_link_hash_warning)
785
0
  hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
786
      /* We don't need to update h->root.u since linker will set them
787
   later.  */
788
0
      h->root.type = bfd_link_hash_undefined;
789
0
      hv->root.type = bfd_link_hash_indirect;
790
0
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
791
0
      obed->elf_backend_copy_indirect_symbol (info, h, hv);
792
0
      break;
793
0
    default:
794
0
      BFD_FAIL ();
795
0
      return false;
796
0
    }
797
798
  /* If this symbol is being provided by the linker script, and it is
799
     currently defined by a dynamic object, but not by a regular
800
     object, then mark it as undefined so that the generic linker will
801
     force the correct value.  */
802
0
  if (provide
803
0
      && h->def_dynamic
804
0
      && !h->def_regular)
805
0
    h->root.type = bfd_link_hash_undefined;
806
807
  /* If this symbol is currently defined by a dynamic object, but not
808
     by a regular object, then clear out any version information because
809
     the symbol will not be associated with the dynamic object any
810
     more.  */
811
0
  if (h->def_dynamic && !h->def_regular)
812
0
    h->verinfo.verdef = NULL;
813
814
  /* Make sure this symbol is not garbage collected.  */
815
0
  h->mark = 1;
816
817
0
  h->def_regular = 1;
818
819
0
  if (hidden)
820
0
    {
821
0
      obed = get_elf_backend_data (info->output_bfd);
822
0
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
823
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
824
0
      obed->elf_backend_hide_symbol (info, h, true);
825
0
    }
826
827
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
828
     and executables.  */
829
0
  if (!bfd_link_relocatable (info)
830
0
      && h->dynindx != -1
831
0
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
832
0
    || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
833
0
    h->forced_local = 1;
834
835
0
  if ((h->def_dynamic
836
0
       || h->ref_dynamic
837
0
       || bfd_link_dll (info))
838
0
      && !h->forced_local
839
0
      && h->dynindx == -1)
840
0
    {
841
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
842
0
  return false;
843
844
      /* If this is a weak defined symbol, and we know a corresponding
845
   real symbol from the same dynamic object, make sure the real
846
   symbol is also made into a dynamic symbol.  */
847
0
      if (h->is_weakalias)
848
0
  {
849
0
    struct elf_link_hash_entry *def = weakdef (h);
850
851
0
    if (def->dynindx == -1
852
0
        && !bfd_elf_link_record_dynamic_symbol (info, def))
853
0
      return false;
854
0
  }
855
0
    }
856
857
0
  return true;
858
0
}
859
860
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
861
   success, and 2 on a failure caused by attempting to record a symbol
862
   in a discarded section, eg. a discarded link-once section symbol.  */
863
864
int
865
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
866
            bfd *input_bfd,
867
            long input_indx)
868
0
{
869
0
  struct elf_link_local_dynamic_entry *entry;
870
0
  struct elf_link_hash_table *eht;
871
0
  struct elf_strtab_hash *dynstr;
872
0
  size_t dynstr_index;
873
0
  char *name;
874
0
  Elf_External_Sym_Shndx eshndx;
875
0
  char esym[sizeof (Elf64_External_Sym)];
876
877
0
  if (! is_elf_hash_table (info->hash))
878
0
    return 0;
879
880
  /* See if the entry exists already.  */
881
0
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
882
0
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
883
0
      return 1;
884
885
0
  entry = bfd_alloc (input_bfd, sizeof (*entry));
886
0
  if (entry == NULL)
887
0
    return 0;
888
889
  /* Go find the symbol, so that we can find it's name.  */
890
0
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_symtab_hdr (input_bfd),
891
0
           1, input_indx, &entry->isym, esym, &eshndx))
892
0
    {
893
0
      bfd_release (input_bfd, entry);
894
0
      return 0;
895
0
    }
896
897
0
  if (entry->isym.st_shndx != SHN_UNDEF
898
0
      && entry->isym.st_shndx < SHN_LORESERVE)
899
0
    {
900
0
      asection *s;
901
902
0
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
903
0
      if (s == NULL || bfd_is_abs_section (s->output_section))
904
0
  {
905
    /* We can still bfd_release here as nothing has done another
906
       bfd_alloc.  We can't do this later in this function.  */
907
0
    bfd_release (input_bfd, entry);
908
0
    return 2;
909
0
  }
910
0
    }
911
912
0
  name = (bfd_elf_string_from_elf_section
913
0
    (input_bfd, elf_symtab_hdr (input_bfd).sh_link,
914
0
     entry->isym.st_name));
915
916
0
  dynstr = _bfd_elf_link_dynstr (info);
917
0
  if (dynstr == NULL)
918
0
    return 0;
919
920
0
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
921
0
  if (dynstr_index == (size_t) -1)
922
0
    return 0;
923
0
  entry->isym.st_name = dynstr_index;
924
925
0
  eht = elf_hash_table (info);
926
927
0
  entry->next = eht->dynlocal;
928
0
  eht->dynlocal = entry;
929
0
  entry->input_bfd = input_bfd;
930
0
  entry->input_indx = input_indx;
931
0
  eht->dynsymcount++;
932
933
  /* Whatever binding the symbol had before, it's now local.  */
934
0
  entry->isym.st_info
935
0
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
936
937
  /* The dynindx will be set at the end of size_dynamic_sections.  */
938
939
0
  return 1;
940
0
}
941
942
/* Return the dynindex of a local dynamic symbol.  */
943
944
long
945
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
946
            bfd *input_bfd,
947
            long input_indx)
948
0
{
949
0
  struct elf_link_local_dynamic_entry *e;
950
951
0
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
952
0
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
953
0
      return e->dynindx;
954
0
  return -1;
955
0
}
956
957
/* This function is used to renumber the dynamic symbols, if some of
958
   them are removed because they are marked as local.  This is called
959
   via elf_link_hash_traverse.  */
960
961
static bool
962
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
963
              void *data)
964
0
{
965
0
  size_t *count = data;
966
967
0
  if (h->forced_local)
968
0
    return true;
969
970
0
  if (h->dynindx != -1)
971
0
    h->dynindx = ++(*count);
972
973
0
  return true;
974
0
}
975
976
977
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
978
   STB_LOCAL binding.  */
979
980
static bool
981
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
982
              void *data)
983
0
{
984
0
  size_t *count = data;
985
986
0
  if (!h->forced_local)
987
0
    return true;
988
989
0
  if (h->dynindx != -1)
990
0
    h->dynindx = ++(*count);
991
992
0
  return true;
993
0
}
994
995
/* Return true if the dynamic symbol for a given section should be
996
   omitted when creating a shared library.  */
997
bool
998
_bfd_elf_omit_section_dynsym_default (struct bfd_link_info *info,
999
              asection *p)
1000
0
{
1001
0
  struct elf_link_hash_table *htab;
1002
0
  asection *ip;
1003
1004
0
  switch (elf_section_data (p)->this_hdr.sh_type)
1005
0
    {
1006
0
    case SHT_PROGBITS:
1007
0
    case SHT_NOBITS:
1008
      /* If sh_type is yet undecided, assume it could be
1009
   SHT_PROGBITS/SHT_NOBITS.  */
1010
0
    case SHT_NULL:
1011
0
      htab = elf_hash_table (info);
1012
0
      if (htab->text_index_section != NULL)
1013
0
  return p != htab->text_index_section && p != htab->data_index_section;
1014
1015
0
      return (htab->dynobj != NULL
1016
0
        && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1017
0
        && ip->output_section == p);
1018
1019
      /* There shouldn't be section relative relocations
1020
   against any other section.  */
1021
0
    default:
1022
0
      return true;
1023
0
    }
1024
0
}
1025
1026
bool
1027
_bfd_elf_omit_section_dynsym_all (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1028
          asection *p ATTRIBUTE_UNUSED)
1029
0
{
1030
0
  return true;
1031
0
}
1032
1033
/* Assign dynsym indices.  In a shared library we generate a section
1034
   symbol for each output section, which come first.  Next come symbols
1035
   which have been forced to local binding.  Then all of the back-end
1036
   allocated local dynamic syms, followed by the rest of the global
1037
   symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1038
   (This prevents the early call before elf_backend_init_index_section
1039
   and strip_excluded_output_sections setting dynindx for sections
1040
   that are stripped.)  */
1041
1042
static unsigned long
1043
_bfd_elf_link_renumber_dynsyms (struct bfd_link_info *info,
1044
        unsigned long *section_sym_count)
1045
0
{
1046
0
  unsigned long dynsymcount = 0;
1047
0
  bool do_sec = section_sym_count != NULL;
1048
1049
0
  if (bfd_link_pic (info)
1050
0
      || elf_hash_table (info)->is_relocatable_executable)
1051
0
    {
1052
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
1053
0
      asection *p;
1054
0
      for (p = info->output_bfd->sections; p ; p = p->next)
1055
0
  if ((p->flags & SEC_EXCLUDE) == 0
1056
0
      && (p->flags & SEC_ALLOC) != 0
1057
0
      && elf_hash_table (info)->dynamic_relocs
1058
0
      && !obed->elf_backend_omit_section_dynsym (info, p))
1059
0
    {
1060
0
      ++dynsymcount;
1061
0
      if (do_sec)
1062
0
        elf_section_data (p)->dynindx = dynsymcount;
1063
0
    }
1064
0
  else if (do_sec)
1065
0
    elf_section_data (p)->dynindx = 0;
1066
0
    }
1067
0
  if (do_sec)
1068
0
    *section_sym_count = dynsymcount;
1069
1070
0
  if (elf_hash_table (info)->has_local_dynsyms)
1071
0
    elf_link_hash_traverse (elf_hash_table (info),
1072
0
          elf_link_renumber_local_hash_table_dynsyms,
1073
0
          &dynsymcount);
1074
1075
0
  if (elf_hash_table (info)->dynlocal)
1076
0
    {
1077
0
      struct elf_link_local_dynamic_entry *p;
1078
0
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1079
0
  p->dynindx = ++dynsymcount;
1080
0
    }
1081
0
  elf_hash_table (info)->local_dynsymcount = dynsymcount;
1082
1083
0
  elf_link_hash_traverse (elf_hash_table (info),
1084
0
        elf_link_renumber_hash_table_dynsyms,
1085
0
        &dynsymcount);
1086
1087
  /* There is an unused NULL entry at the head of the table which we
1088
     must account for in our count even if the table is empty since it
1089
     is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1090
     .dynamic section.  */
1091
0
  dynsymcount++;
1092
1093
0
  elf_hash_table (info)->dynsymcount = dynsymcount;
1094
0
  return dynsymcount;
1095
0
}
1096
1097
/* Merge st_other field.  Both the input bfd and output bfd are given
1098
   with IBFD reflecting where the ST_OTHER bits came from.  If they
1099
   were from a symbol already in the linker hash table then the proper
1100
   IBFD is the output bfd since symbols in the linker hash table
1101
   interpret their st_other bits according to OBFD.  */
1102
1103
static void
1104
elf_merge_st_other (bfd *obfd, bfd *ibfd,
1105
        struct elf_link_hash_entry *h,
1106
        unsigned int st_other, asection *sec,
1107
        bool definition, bool dynamic)
1108
0
{
1109
0
  if (ibfd->xvec == obfd->xvec)
1110
0
    {
1111
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
1112
1113
      /* If st_other has a processor-specific meaning, specific
1114
   code might be needed here.  */
1115
0
      if (obed->elf_backend_merge_symbol_attribute)
1116
0
  obed->elf_backend_merge_symbol_attribute (h, st_other,
1117
0
              definition, dynamic);
1118
0
    }
1119
1120
0
  if (!dynamic)
1121
0
    {
1122
0
      unsigned symvis = ELF_ST_VISIBILITY (st_other);
1123
0
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
1124
1125
      /* Keep the most constraining visibility.  Leave the remainder
1126
   of the st_other field to elf_backend_merge_symbol_attribute.  */
1127
0
      if (symvis - 1 < hvis - 1)
1128
0
  h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1129
0
    }
1130
0
  else if (definition
1131
0
     && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1132
0
     && (sec->flags & SEC_READONLY) == 0)
1133
0
    h->protected_def = 1;
1134
0
}
1135
1136
/* This function is called when we want to merge a new symbol with an
1137
   existing symbol.  It handles the various cases which arise when we
1138
   find a definition in a dynamic object, or when there is already a
1139
   definition in a dynamic object.  The new symbol is described by
1140
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1141
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1142
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1143
   of an old common symbol.  We set OVERRIDE if the old symbol is
1144
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1145
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1146
   to change.  By OK to change, we mean that we shouldn't warn if the
1147
   type or size does change.  */
1148
1149
static bool
1150
_bfd_elf_merge_symbol (bfd *abfd,
1151
           struct bfd_link_info *info,
1152
           const char *name,
1153
           Elf_Internal_Sym *sym,
1154
           asection **psec,
1155
           bfd_vma *pvalue,
1156
           struct elf_link_hash_entry **sym_hash,
1157
           bfd **poldbfd,
1158
           bool *pold_weak,
1159
           unsigned int *pold_alignment,
1160
           bool *skip,
1161
           bfd **override,
1162
           bool *type_change_ok,
1163
           bool *size_change_ok,
1164
           bool *matched)
1165
0
{
1166
0
  asection *sec, *oldsec;
1167
0
  struct elf_link_hash_entry *h;
1168
0
  struct elf_link_hash_entry *hi;
1169
0
  struct elf_link_hash_entry *flip;
1170
0
  int bind;
1171
0
  bfd *oldbfd;
1172
0
  bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1173
0
  bool newweak, oldweak, newfunc, oldfunc;
1174
0
  elf_backend_data *bed, *obed;
1175
0
  const char *new_version;
1176
0
  bool default_sym = *matched;
1177
0
  struct elf_link_hash_table *htab;
1178
1179
0
  *skip = false;
1180
0
  *override = NULL;
1181
1182
0
  sec = *psec;
1183
0
  bind = ELF_ST_BIND (sym->st_info);
1184
1185
0
  if (! bfd_is_und_section (sec))
1186
0
    h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1187
0
  else
1188
0
    h = ((struct elf_link_hash_entry *)
1189
0
   bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1190
0
  if (h == NULL)
1191
0
    return false;
1192
0
  *sym_hash = h;
1193
1194
0
  bed = get_elf_backend_data (abfd);
1195
0
  obed = get_elf_backend_data (info->output_bfd);
1196
1197
0
  htab = elf_hash_table (info);
1198
1199
  /* NEW_VERSION is the symbol version of the new symbol.  */
1200
0
  if (h->versioned != unversioned)
1201
0
    {
1202
      /* Symbol version is unknown or versioned.  */
1203
0
      new_version = strrchr (name, ELF_VER_CHR);
1204
0
      if (new_version)
1205
0
  {
1206
0
    if (h->versioned == unknown)
1207
0
      {
1208
        /* The base symbol has an empty version.  */
1209
0
        if (new_version[1] == '\0')
1210
0
    {
1211
0
      htab->has_base_symbols = true;
1212
0
      h->base_symbol = 1;
1213
0
    }
1214
0
        if (new_version > name && new_version[-1] != ELF_VER_CHR)
1215
0
    h->versioned = versioned_hidden;
1216
0
        else
1217
0
    h->versioned = versioned;
1218
0
      }
1219
0
    new_version += 1;
1220
0
    if (new_version[0] == '\0')
1221
0
      new_version = NULL;
1222
0
  }
1223
0
      else
1224
0
  h->versioned = unversioned;
1225
0
    }
1226
0
  else
1227
0
    new_version = NULL;
1228
1229
  /* For merging, we only care about real symbols.  But we need to make
1230
     sure that indirect symbol dynamic flags are updated.  */
1231
0
  hi = h;
1232
0
  while (h->root.type == bfd_link_hash_indirect
1233
0
   || h->root.type == bfd_link_hash_warning)
1234
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1235
1236
0
  if (!*matched)
1237
0
    {
1238
0
      if (hi == h || h->root.type == bfd_link_hash_new)
1239
0
  *matched = true;
1240
0
      else
1241
0
  {
1242
    /* OLD_HIDDEN is true if the existing symbol is only visible
1243
       to the symbol with the same symbol version.  NEW_HIDDEN is
1244
       true if the new symbol is only visible to the symbol with
1245
       the same symbol version.  */
1246
0
    bool old_hidden = h->versioned == versioned_hidden;
1247
0
    bool new_hidden = hi->versioned == versioned_hidden;
1248
0
    if (!old_hidden && !new_hidden)
1249
      /* The new symbol matches the existing symbol if both
1250
         aren't hidden.  */
1251
0
      *matched = true;
1252
0
    else
1253
0
      {
1254
        /* OLD_VERSION is the symbol version of the existing
1255
     symbol. */
1256
0
        const char *old_version;
1257
1258
0
        if (h->versioned >= versioned)
1259
0
    old_version = strrchr (h->root.root.string,
1260
0
               ELF_VER_CHR) + 1;
1261
0
        else
1262
0
     old_version = NULL;
1263
1264
        /* The new symbol matches the existing symbol if they
1265
     have the same symbol version.  */
1266
0
        *matched = (old_version == new_version
1267
0
        || (old_version != NULL
1268
0
            && new_version != NULL
1269
0
            && strcmp (old_version, new_version) == 0));
1270
0
      }
1271
0
  }
1272
0
    }
1273
1274
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1275
     existing symbol.  */
1276
1277
0
  oldbfd = NULL;
1278
0
  oldsec = NULL;
1279
0
  switch (h->root.type)
1280
0
    {
1281
0
    default:
1282
0
      break;
1283
1284
0
    case bfd_link_hash_undefined:
1285
0
    case bfd_link_hash_undefweak:
1286
0
      oldbfd = h->root.u.undef.abfd;
1287
0
      break;
1288
1289
0
    case bfd_link_hash_defined:
1290
0
    case bfd_link_hash_defweak:
1291
0
      oldbfd = h->root.u.def.section->owner;
1292
0
      oldsec = h->root.u.def.section;
1293
0
      break;
1294
1295
0
    case bfd_link_hash_common:
1296
0
      oldbfd = h->root.u.c.p->section->owner;
1297
0
      oldsec = h->root.u.c.p->section;
1298
0
      if (pold_alignment)
1299
0
  *pold_alignment = h->root.u.c.p->alignment_power;
1300
0
      break;
1301
0
    }
1302
0
  if (poldbfd && *poldbfd == NULL)
1303
0
    *poldbfd = oldbfd;
1304
1305
  /* Differentiate strong and weak symbols.  */
1306
0
  newweak = bind == STB_WEAK;
1307
0
  oldweak = (h->root.type == bfd_link_hash_defweak
1308
0
       || h->root.type == bfd_link_hash_undefweak);
1309
0
  if (pold_weak)
1310
0
    *pold_weak = oldweak;
1311
1312
  /* We have to check it for every instance since the first few may be
1313
     references and not all compilers emit symbol type for undefined
1314
     symbols.  */
1315
0
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1316
1317
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1318
     respectively, is from a dynamic object.  */
1319
1320
0
  newdyn = (abfd->flags & DYNAMIC) != 0;
1321
1322
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1323
     syms and defined syms in dynamic libraries respectively.
1324
     ref_dynamic on the other hand can be set for a symbol defined in
1325
     a dynamic library, and def_dynamic may not be set;  When the
1326
     definition in a dynamic lib is overridden by a definition in the
1327
     executable use of the symbol in the dynamic lib becomes a
1328
     reference to the executable symbol.  */
1329
0
  if (newdyn)
1330
0
    {
1331
0
      if (bfd_is_und_section (sec))
1332
0
  {
1333
0
    if (bind != STB_WEAK)
1334
0
      {
1335
0
        h->ref_dynamic_nonweak = 1;
1336
0
        hi->ref_dynamic_nonweak = 1;
1337
0
      }
1338
0
  }
1339
0
      else
1340
0
  {
1341
    /* Update the existing symbol only if they match. */
1342
0
    if (*matched)
1343
0
      h->dynamic_def = 1;
1344
0
    hi->dynamic_def = 1;
1345
0
  }
1346
0
    }
1347
1348
  /* If we just created the symbol, mark it as being an ELF symbol.
1349
     Other than that, there is nothing to do--there is no merge issue
1350
     with a newly defined symbol--so we just return.  */
1351
1352
0
  if (h->root.type == bfd_link_hash_new)
1353
0
    {
1354
0
      h->non_elf = 0;
1355
0
      return true;
1356
0
    }
1357
1358
  /* In cases involving weak versioned symbols, we may wind up trying
1359
     to merge a symbol with itself.  Catch that here, to avoid the
1360
     confusion that results if we try to override a symbol with
1361
     itself.  The additional tests catch cases like
1362
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1363
     dynamic object, which we do want to handle here.  */
1364
0
  if (abfd == oldbfd
1365
0
      && (newweak || oldweak)
1366
0
      && ((abfd->flags & DYNAMIC) == 0
1367
0
    || !h->def_regular))
1368
0
    return true;
1369
1370
0
  olddyn = false;
1371
0
  if (oldbfd != NULL)
1372
0
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1373
0
  else if (oldsec != NULL)
1374
0
    {
1375
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1376
   indices used by MIPS ELF.  */
1377
0
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1378
0
    }
1379
1380
  /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries.  */
1381
0
  if (!htab->handling_dt_needed
1382
0
      && oldbfd != NULL
1383
0
      && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1384
0
    {
1385
0
      if (newdyn != olddyn)
1386
0
  {
1387
    /* Handle a case where plugin_notice won't be called and thus
1388
       won't set the non_ir_ref flags on the first pass over
1389
       symbols.  */
1390
0
    h->root.non_ir_ref_dynamic = true;
1391
0
    hi->root.non_ir_ref_dynamic = true;
1392
0
  }
1393
0
      else if ((oldbfd->flags & BFD_PLUGIN) != 0
1394
0
         && hi->root.type == bfd_link_hash_indirect)
1395
0
  {
1396
    /* Change indirect symbol from IR to undefined.  */
1397
0
    hi->root.type = bfd_link_hash_undefined;
1398
0
    hi->root.u.undef.abfd = oldbfd;
1399
0
  }
1400
0
    }
1401
1402
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1403
     respectively, appear to be a definition rather than reference.  */
1404
1405
0
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1406
1407
0
  olddef = (h->root.type != bfd_link_hash_undefined
1408
0
      && h->root.type != bfd_link_hash_undefweak
1409
0
      && h->root.type != bfd_link_hash_common);
1410
1411
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1412
     respectively, appear to be a function.  */
1413
1414
0
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1415
0
       && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1416
1417
0
  oldfunc = (h->type != STT_NOTYPE
1418
0
       && bed->is_function_type (h->type));
1419
1420
0
  if (!(newfunc && oldfunc)
1421
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1422
0
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1423
0
      && h->type != STT_NOTYPE
1424
0
      && (newdef || bfd_is_com_section (sec))
1425
0
      && (olddef || h->root.type == bfd_link_hash_common))
1426
0
    {
1427
      /* If creating a default indirect symbol ("foo" or "foo@") from
1428
   a dynamic versioned definition ("foo@@") skip doing so if
1429
   there is an existing regular definition with a different
1430
   type.  We don't want, for example, a "time" variable in the
1431
   executable overriding a "time" function in a shared library.  */
1432
0
      if (newdyn
1433
0
    && !olddyn)
1434
0
  {
1435
0
    *skip = true;
1436
0
    return true;
1437
0
  }
1438
1439
      /* When adding a symbol from a regular object file after we have
1440
   created indirect symbols, undo the indirection and any
1441
   dynamic state.  */
1442
0
      if (hi != h
1443
0
    && !newdyn
1444
0
    && olddyn)
1445
0
  {
1446
0
    h = hi;
1447
0
    obed->elf_backend_hide_symbol (info, h, true);
1448
0
    h->forced_local = 0;
1449
0
    h->ref_dynamic = 0;
1450
0
    h->def_dynamic = 0;
1451
0
    h->dynamic_def = 0;
1452
0
    if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1453
0
      {
1454
0
        h->root.type = bfd_link_hash_undefined;
1455
0
        h->root.u.undef.abfd = abfd;
1456
0
      }
1457
0
    else
1458
0
      {
1459
0
        h->root.type = bfd_link_hash_new;
1460
0
        h->root.u.undef.abfd = NULL;
1461
0
      }
1462
0
    return true;
1463
0
  }
1464
0
    }
1465
1466
  /* Check TLS symbols.  We don't check undefined symbols introduced
1467
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1468
     check symbols from plugins because they also have no type.  */
1469
0
  if (oldbfd != NULL
1470
0
      && (oldbfd->flags & BFD_PLUGIN) == 0
1471
0
      && (abfd->flags & BFD_PLUGIN) == 0
1472
0
      && ELF_ST_TYPE (sym->st_info) != h->type
1473
0
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1474
0
    {
1475
0
      bfd *ntbfd, *tbfd;
1476
0
      bool ntdef, tdef;
1477
0
      asection *ntsec, *tsec;
1478
1479
0
      if (h->type == STT_TLS)
1480
0
  {
1481
0
    ntbfd = abfd;
1482
0
    ntsec = sec;
1483
0
    ntdef = newdef;
1484
0
    tbfd = oldbfd;
1485
0
    tsec = oldsec;
1486
0
    tdef = olddef;
1487
0
  }
1488
0
      else
1489
0
  {
1490
0
    ntbfd = oldbfd;
1491
0
    ntsec = oldsec;
1492
0
    ntdef = olddef;
1493
0
    tbfd = abfd;
1494
0
    tsec = sec;
1495
0
    tdef = newdef;
1496
0
  }
1497
1498
0
      if (tdef && ntdef)
1499
0
  _bfd_error_handler
1500
    /* xgettext:c-format */
1501
0
    (_("%s: TLS definition in %pB section %pA "
1502
0
       "mismatches non-TLS definition in %pB section %pA"),
1503
0
     h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1504
0
      else if (!tdef && !ntdef)
1505
0
  _bfd_error_handler
1506
    /* xgettext:c-format */
1507
0
    (_("%s: TLS reference in %pB "
1508
0
       "mismatches non-TLS reference in %pB"),
1509
0
     h->root.root.string, tbfd, ntbfd);
1510
0
      else if (tdef)
1511
0
  _bfd_error_handler
1512
    /* xgettext:c-format */
1513
0
    (_("%s: TLS definition in %pB section %pA "
1514
0
       "mismatches non-TLS reference in %pB"),
1515
0
     h->root.root.string, tbfd, tsec, ntbfd);
1516
0
      else
1517
0
  _bfd_error_handler
1518
    /* xgettext:c-format */
1519
0
    (_("%s: TLS reference in %pB "
1520
0
       "mismatches non-TLS definition in %pB section %pA"),
1521
0
     h->root.root.string, tbfd, ntbfd, ntsec);
1522
1523
0
      bfd_set_error (bfd_error_bad_value);
1524
0
      return false;
1525
0
    }
1526
1527
  /* If the old symbol has non-default visibility, we ignore the new
1528
     definition from a dynamic object.  */
1529
0
  if (newdyn
1530
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1531
0
      && !bfd_is_und_section (sec))
1532
0
    {
1533
0
      *skip = true;
1534
      /* Make sure this symbol is dynamic.  */
1535
0
      h->ref_dynamic = 1;
1536
0
      hi->ref_dynamic = 1;
1537
      /* A protected symbol has external availability. Make sure it is
1538
   recorded as dynamic.
1539
1540
   FIXME: Should we check type and size for protected symbol?  */
1541
0
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1542
0
  return bfd_elf_link_record_dynamic_symbol (info, h);
1543
0
      else
1544
0
  return true;
1545
0
    }
1546
0
  else if (!newdyn
1547
0
     && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1548
0
     && h->def_dynamic)
1549
0
    {
1550
      /* If the new symbol with non-default visibility comes from a
1551
   relocatable file and the old definition comes from a dynamic
1552
   object, we remove the old definition.  */
1553
0
      if (hi->root.type == bfd_link_hash_indirect)
1554
0
  {
1555
    /* Handle the case where the old dynamic definition is
1556
       default versioned.  We need to copy the symbol info from
1557
       the symbol with default version to the normal one if it
1558
       was referenced before.  */
1559
0
    if (h->ref_regular)
1560
0
      {
1561
0
        hi->root.type = h->root.type;
1562
0
        h->root.type = bfd_link_hash_indirect;
1563
0
        obed->elf_backend_copy_indirect_symbol (info, hi, h);
1564
1565
0
        h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1566
0
        if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1567
0
    {
1568
      /* If the new symbol is hidden or internal, completely undo
1569
         any dynamic link state.  */
1570
0
      obed->elf_backend_hide_symbol (info, h, true);
1571
0
      h->forced_local = 0;
1572
0
      h->ref_dynamic = 0;
1573
0
    }
1574
0
        else
1575
0
    h->ref_dynamic = 1;
1576
1577
0
        h->def_dynamic = 0;
1578
        /* FIXME: Should we check type and size for protected symbol?  */
1579
0
        h->size = 0;
1580
0
        h->type = 0;
1581
1582
0
        h = hi;
1583
0
      }
1584
0
    else
1585
0
      h = hi;
1586
0
  }
1587
1588
      /* If the old symbol was undefined before, then it will still be
1589
   on the undefs list.  If the new symbol is undefined or
1590
   common, we can't make it bfd_link_hash_new here, because new
1591
   undefined or common symbols will be added to the undefs list
1592
   by _bfd_generic_link_add_one_symbol.  Symbols may not be
1593
   added twice to the undefs list.  Also, if the new symbol is
1594
   undefweak then we don't want to lose the strong undef.  */
1595
0
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1596
0
  {
1597
0
    h->root.type = bfd_link_hash_undefined;
1598
0
    h->root.u.undef.abfd = abfd;
1599
0
  }
1600
0
      else
1601
0
  {
1602
0
    h->root.type = bfd_link_hash_new;
1603
0
    h->root.u.undef.abfd = NULL;
1604
0
  }
1605
1606
0
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1607
0
  {
1608
    /* If the new symbol is hidden or internal, completely undo
1609
       any dynamic link state.  */
1610
0
    obed->elf_backend_hide_symbol (info, h, true);
1611
0
    h->forced_local = 0;
1612
0
    h->ref_dynamic = 0;
1613
0
  }
1614
0
      else
1615
0
  h->ref_dynamic = 1;
1616
0
      h->def_dynamic = 0;
1617
      /* FIXME: Should we check type and size for protected symbol?  */
1618
0
      h->size = 0;
1619
0
      h->type = 0;
1620
0
      return true;
1621
0
    }
1622
1623
  /* If a new weak symbol definition comes from a regular file and the
1624
     old symbol comes from a dynamic library, we treat the new one as
1625
     strong.  Similarly, an old weak symbol definition from a regular
1626
     file is treated as strong when the new symbol comes from a dynamic
1627
     library.  Further, an old weak symbol from a dynamic library is
1628
     treated as strong if the new symbol is from a dynamic library.
1629
     This reflects the way glibc's ld.so works.
1630
1631
     Also allow a weak symbol to override a linker script symbol
1632
     defined by an early pass over the script.  This is done so the
1633
     linker knows the symbol is defined in an object file, for the
1634
     DEFINED script function.
1635
1636
     Do this before setting *type_change_ok or *size_change_ok so that
1637
     we warn properly when dynamic library symbols are overridden.  */
1638
1639
0
  if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1640
0
    newweak = false;
1641
0
  if (olddef && newdyn)
1642
0
    oldweak = false;
1643
1644
  /* Allow changes between different types of function symbol.  */
1645
0
  if (newfunc && oldfunc)
1646
0
    *type_change_ok = true;
1647
1648
  /* It's OK to change the type if either the existing symbol or the
1649
     new symbol is weak.  A type change is also OK if the old symbol
1650
     is undefined and the new symbol is defined.  */
1651
1652
0
  if (oldweak
1653
0
      || newweak
1654
0
      || (newdef
1655
0
    && h->root.type == bfd_link_hash_undefined))
1656
0
    *type_change_ok = true;
1657
1658
  /* It's OK to change the size if either the existing symbol or the
1659
     new symbol is weak, or if the old symbol is undefined.  */
1660
1661
0
  if (*type_change_ok
1662
0
      || h->root.type == bfd_link_hash_undefined)
1663
0
    *size_change_ok = true;
1664
1665
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1666
     symbol, respectively, appears to be a common symbol in a dynamic
1667
     object.  If a symbol appears in an uninitialized section, and is
1668
     not weak, and is not a function, then it may be a common symbol
1669
     which was resolved when the dynamic object was created.  We want
1670
     to treat such symbols specially, because they raise special
1671
     considerations when setting the symbol size: if the symbol
1672
     appears as a common symbol in a regular object, and the size in
1673
     the regular object is larger, we must make sure that we use the
1674
     larger size.  This problematic case can always be avoided in C,
1675
     but it must be handled correctly when using Fortran shared
1676
     libraries.
1677
1678
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1679
     likewise for OLDDYNCOMMON and OLDDEF.
1680
1681
     Note that this test is just a heuristic, and that it is quite
1682
     possible to have an uninitialized symbol in a shared object which
1683
     is really a definition, rather than a common symbol.  This could
1684
     lead to some minor confusion when the symbol really is a common
1685
     symbol in some regular object.  However, I think it will be
1686
     harmless.  */
1687
1688
0
  if (newdyn
1689
0
      && newdef
1690
0
      && !newweak
1691
0
      && (sec->flags & SEC_ALLOC) != 0
1692
0
      && (sec->flags & SEC_LOAD) == 0
1693
0
      && sym->st_size > 0
1694
0
      && !newfunc)
1695
0
    newdyncommon = true;
1696
0
  else
1697
0
    newdyncommon = false;
1698
1699
0
  if (olddyn
1700
0
      && olddef
1701
0
      && h->root.type == bfd_link_hash_defined
1702
0
      && h->def_dynamic
1703
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1704
0
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1705
0
      && h->size > 0
1706
0
      && !oldfunc)
1707
0
    olddyncommon = true;
1708
0
  else
1709
0
    olddyncommon = false;
1710
1711
  /* We now know everything about the old and new symbols.  We ask the
1712
     backend to check if we can merge them.  */
1713
0
  if (obed->merge_symbol != NULL)
1714
0
    {
1715
0
      if (!obed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1716
0
  return false;
1717
0
      sec = *psec;
1718
0
    }
1719
1720
  /* There are multiple definitions of a normal symbol.  Skip the
1721
     default symbol as well as definition from an IR object.  */
1722
0
  if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1723
0
      && !default_sym && h->def_regular
1724
0
      && !(oldbfd != NULL
1725
0
     && (oldbfd->flags & BFD_PLUGIN) != 0
1726
0
     && (abfd->flags & BFD_PLUGIN) == 0))
1727
0
    {
1728
      /* Handle a multiple definition.  */
1729
0
      (*info->callbacks->multiple_definition) (info, &h->root,
1730
0
                 abfd, sec, *pvalue);
1731
0
      *skip = true;
1732
0
      return true;
1733
0
    }
1734
1735
  /* If both the old and the new symbols look like common symbols in a
1736
     dynamic object, set the size of the symbol to the larger of the
1737
     two.  */
1738
1739
0
  if (olddyncommon
1740
0
      && newdyncommon
1741
0
      && sym->st_size != h->size)
1742
0
    {
1743
      /* Since we think we have two common symbols, issue a multiple
1744
   common warning if desired.  Note that we only warn if the
1745
   size is different.  If the size is the same, we simply let
1746
   the old symbol override the new one as normally happens with
1747
   symbols defined in dynamic objects.  */
1748
1749
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1750
0
             bfd_link_hash_common, sym->st_size);
1751
0
      if (sym->st_size > h->size)
1752
0
  h->size = sym->st_size;
1753
1754
0
      *size_change_ok = true;
1755
0
    }
1756
1757
  /* If we are looking at a dynamic object, and we have found a
1758
     definition, we need to see if the symbol was already defined by
1759
     some other object.  If so, we want to use the existing
1760
     definition, and we do not want to report a multiple symbol
1761
     definition error; we do this by clobbering *PSEC to be
1762
     bfd_und_section_ptr.
1763
1764
     We treat a common symbol as a definition if the symbol in the
1765
     shared library is a function, since common symbols always
1766
     represent variables; this can cause confusion in principle, but
1767
     any such confusion would seem to indicate an erroneous program or
1768
     shared library.  We also permit a common symbol in a regular
1769
     object to override a weak symbol in a shared object.  */
1770
1771
0
  if (newdyn
1772
0
      && newdef
1773
0
      && (olddef
1774
0
    || (h->root.type == bfd_link_hash_common
1775
0
        && (newweak || newfunc))))
1776
0
    {
1777
0
      *override = abfd;
1778
0
      newdef = false;
1779
0
      newdyncommon = false;
1780
1781
0
      *psec = sec = bfd_und_section_ptr;
1782
0
      *size_change_ok = true;
1783
1784
      /* If we get here when the old symbol is a common symbol, then
1785
   we are explicitly letting it override a weak symbol or
1786
   function in a dynamic object, and we don't want to warn about
1787
   a type change.  If the old symbol is a defined symbol, a type
1788
   change warning may still be appropriate.  */
1789
1790
0
      if (h->root.type == bfd_link_hash_common)
1791
0
  *type_change_ok = true;
1792
0
    }
1793
1794
  /* Handle the special case of an old common symbol merging with a
1795
     new symbol which looks like a common symbol in a shared object.
1796
     We change *PSEC and *PVALUE to make the new symbol look like a
1797
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1798
     right thing.  */
1799
1800
0
  if (newdyncommon
1801
0
      && h->root.type == bfd_link_hash_common)
1802
0
    {
1803
0
      *override = oldbfd;
1804
0
      newdef = false;
1805
0
      newdyncommon = false;
1806
0
      *pvalue = sym->st_size;
1807
0
      *psec = sec = bed->common_section (oldsec);
1808
0
      *size_change_ok = true;
1809
0
    }
1810
1811
  /* Skip weak definitions of symbols that are already defined.  */
1812
0
  if (newdef && olddef && newweak)
1813
0
    {
1814
      /* Don't skip new non-IR weak syms.  */
1815
0
      if (!(oldbfd != NULL
1816
0
      && (oldbfd->flags & BFD_PLUGIN) != 0
1817
0
      && (abfd->flags & BFD_PLUGIN) == 0))
1818
0
  {
1819
0
    newdef = false;
1820
0
    *skip = true;
1821
0
  }
1822
1823
      /* Merge st_other.  If the symbol already has a dynamic index,
1824
   but visibility says it should not be visible, turn it into a
1825
   local symbol.  */
1826
0
      elf_merge_st_other (info->output_bfd, abfd, h, sym->st_other,
1827
0
        sec, newdef, newdyn);
1828
0
      if (h->dynindx != -1)
1829
0
  switch (ELF_ST_VISIBILITY (h->other))
1830
0
    {
1831
0
    case STV_INTERNAL:
1832
0
    case STV_HIDDEN:
1833
0
      obed->elf_backend_hide_symbol (info, h, true);
1834
0
      break;
1835
0
    }
1836
0
    }
1837
1838
  /* If the old symbol is from a dynamic object, and the new symbol is
1839
     a definition which is not from a dynamic object, then the new
1840
     symbol overrides the old symbol.  Symbols from regular files
1841
     always take precedence over symbols from dynamic objects, even if
1842
     they are defined after the dynamic object in the link.
1843
1844
     As above, we again permit a common symbol in a regular object to
1845
     override a definition in a shared object if the shared object
1846
     symbol is a function or is weak.  */
1847
1848
0
  flip = NULL;
1849
0
  if (!newdyn
1850
0
      && (newdef
1851
0
    || (bfd_is_com_section (sec)
1852
0
        && (oldweak || oldfunc)))
1853
0
      && olddyn
1854
0
      && olddef
1855
0
      && h->def_dynamic)
1856
0
    {
1857
      /* Change the hash table entry to undefined, and let
1858
   _bfd_generic_link_add_one_symbol do the right thing with the
1859
   new definition.  */
1860
1861
0
      h->root.type = bfd_link_hash_undefined;
1862
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1863
0
      *size_change_ok = true;
1864
1865
0
      olddef = false;
1866
0
      olddyncommon = false;
1867
1868
      /* We again permit a type change when a common symbol may be
1869
   overriding a function.  */
1870
1871
0
      if (bfd_is_com_section (sec))
1872
0
  {
1873
0
    if (oldfunc)
1874
0
      {
1875
        /* If a common symbol overrides a function, make sure
1876
     that it isn't defined dynamically nor has type
1877
     function.  */
1878
0
        h->def_dynamic = 0;
1879
0
        h->type = STT_NOTYPE;
1880
0
      }
1881
0
    *type_change_ok = true;
1882
0
  }
1883
1884
0
      if (hi->root.type == bfd_link_hash_indirect)
1885
0
  flip = hi;
1886
0
      else
1887
  /* This union may have been set to be non-NULL when this symbol
1888
     was seen in a dynamic object.  We must force the union to be
1889
     NULL, so that it is correct for a regular symbol.  */
1890
0
  h->verinfo.vertree = NULL;
1891
0
    }
1892
1893
  /* Handle the special case of a new common symbol merging with an
1894
     old symbol that looks like it might be a common symbol defined in
1895
     a shared object.  Note that we have already handled the case in
1896
     which a new common symbol should simply override the definition
1897
     in the shared library.  */
1898
1899
0
  if (! newdyn
1900
0
      && bfd_is_com_section (sec)
1901
0
      && olddyncommon)
1902
0
    {
1903
      /* It would be best if we could set the hash table entry to a
1904
   common symbol, but we don't know what to use for the section
1905
   or the alignment.  */
1906
0
      (*info->callbacks->multiple_common) (info, &h->root, abfd,
1907
0
             bfd_link_hash_common, sym->st_size);
1908
1909
      /* If the presumed common symbol in the dynamic object is
1910
   larger, pretend that the new symbol has its size.  */
1911
1912
0
      if (h->size > *pvalue)
1913
0
  *pvalue = h->size;
1914
1915
      /* We need to remember the alignment required by the symbol
1916
   in the dynamic object.  */
1917
0
      BFD_ASSERT (pold_alignment);
1918
0
      *pold_alignment = h->root.u.def.section->alignment_power;
1919
1920
0
      olddef = false;
1921
0
      olddyncommon = false;
1922
1923
0
      h->root.type = bfd_link_hash_undefined;
1924
0
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1925
1926
0
      *size_change_ok = true;
1927
0
      *type_change_ok = true;
1928
1929
0
      if (hi->root.type == bfd_link_hash_indirect)
1930
0
  flip = hi;
1931
0
      else
1932
0
  h->verinfo.vertree = NULL;
1933
0
    }
1934
1935
0
  if (flip != NULL)
1936
0
    {
1937
      /* Handle the case where we had a versioned symbol in a dynamic
1938
   library and now find a definition in a normal object.  In this
1939
   case, we make the versioned symbol point to the normal one.  */
1940
0
      flip->root.type = h->root.type;
1941
0
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1942
0
      h->root.type = bfd_link_hash_indirect;
1943
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1944
0
      obed->elf_backend_copy_indirect_symbol (info, flip, h);
1945
0
      if (h->def_dynamic)
1946
0
  {
1947
0
    h->def_dynamic = 0;
1948
0
    flip->ref_dynamic = 1;
1949
0
  }
1950
0
    }
1951
1952
0
  return true;
1953
0
}
1954
1955
/* This function is called to create an indirect symbol from the
1956
   default for the symbol with the default version if needed. The
1957
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1958
   set DYNSYM if the new indirect symbol is dynamic.  */
1959
1960
static bool
1961
_bfd_elf_add_default_symbol (bfd *abfd,
1962
           struct bfd_link_info *info,
1963
           struct elf_link_hash_entry *h,
1964
           const char *name,
1965
           Elf_Internal_Sym *sym,
1966
           asection *sec,
1967
           bfd_vma value,
1968
           bfd **poldbfd,
1969
           bool *dynsym)
1970
0
{
1971
0
  bool type_change_ok;
1972
0
  bool size_change_ok;
1973
0
  bool skip;
1974
0
  char *shortname;
1975
0
  struct elf_link_hash_entry *hi;
1976
0
  struct bfd_link_hash_entry *bh;
1977
0
  elf_backend_data *obed;
1978
0
  bool collect;
1979
0
  bool dynamic;
1980
0
  bfd *override;
1981
0
  const char *p;
1982
0
  size_t len, shortlen;
1983
0
  asection *tmp_sec;
1984
0
  bool matched;
1985
1986
0
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1987
0
    return true;
1988
1989
  /* If this symbol has a version, and it is the default version, we
1990
     create an indirect symbol from the default name to the fully
1991
     decorated name.  This will cause external references which do not
1992
     specify a version to be bound to this version of the symbol.  */
1993
0
  p = strchr (name, ELF_VER_CHR);
1994
0
  if (h->versioned == unknown)
1995
0
    {
1996
0
      if (p == NULL)
1997
0
  {
1998
0
    h->versioned = unversioned;
1999
0
    return true;
2000
0
  }
2001
0
      else
2002
0
  {
2003
0
    if (p[1] != ELF_VER_CHR)
2004
0
      {
2005
0
        h->versioned = versioned_hidden;
2006
0
        return true;
2007
0
      }
2008
0
    else
2009
0
      h->versioned = versioned;
2010
0
  }
2011
0
    }
2012
0
  else
2013
0
    {
2014
      /* PR ld/19073: We may see an unversioned definition after the
2015
   default version.  */
2016
0
      if (p == NULL)
2017
0
  return true;
2018
0
    }
2019
2020
0
  obed = get_elf_backend_data (info->output_bfd);
2021
0
  collect = obed->collect;
2022
0
  dynamic = (abfd->flags & DYNAMIC) != 0;
2023
2024
0
  shortlen = p - name;
2025
0
  shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
2026
0
  if (shortname == NULL)
2027
0
    return false;
2028
0
  memcpy (shortname, name, shortlen);
2029
0
  shortname[shortlen] = '\0';
2030
2031
  /* We are going to create a new symbol.  Merge it with any existing
2032
     symbol with this name.  For the purposes of the merge, act as
2033
     though we were defining the symbol we just defined, although we
2034
     actually going to define an indirect symbol.  */
2035
0
  type_change_ok = false;
2036
0
  size_change_ok = false;
2037
0
  matched = true;
2038
0
  tmp_sec = sec;
2039
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2040
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2041
0
            &type_change_ok, &size_change_ok, &matched))
2042
0
    return false;
2043
2044
0
  if (skip)
2045
0
    goto nondefault;
2046
2047
0
  if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2048
0
    {
2049
      /* If the undecorated symbol will have a version added by a
2050
   script different to H, then don't indirect to/from the
2051
   undecorated symbol.  This isn't ideal because we may not yet
2052
   have seen symbol versions, if given by a script on the
2053
   command line rather than via --version-script.  */
2054
0
      if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2055
0
  {
2056
0
    bool hide;
2057
2058
0
    hi->verinfo.vertree
2059
0
      = bfd_find_version_for_sym (info->version_info,
2060
0
          hi->root.root.string, &hide);
2061
0
    if (hi->verinfo.vertree != NULL && hide)
2062
0
      {
2063
0
        obed->elf_backend_hide_symbol (info, hi, true);
2064
0
        goto nondefault;
2065
0
      }
2066
0
  }
2067
0
      if (hi->verinfo.vertree != NULL
2068
0
    && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2069
0
  goto nondefault;
2070
0
    }
2071
2072
0
  if (! override)
2073
0
    {
2074
      /* Add the default symbol if not performing a relocatable link.  */
2075
0
      if (! bfd_link_relocatable (info))
2076
0
  {
2077
0
    bh = &hi->root;
2078
0
    if (bh->type == bfd_link_hash_defined
2079
0
        && bh->u.def.section->owner != NULL
2080
0
        && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2081
0
      {
2082
        /* Mark the previous definition from IR object as
2083
     undefined so that the generic linker will override
2084
     it.  */
2085
0
        bh->type = bfd_link_hash_undefined;
2086
0
        bh->u.undef.abfd = bh->u.def.section->owner;
2087
0
      }
2088
0
    if (! (_bfd_generic_link_add_one_symbol
2089
0
     (info, abfd, shortname, BSF_INDIRECT,
2090
0
      bfd_ind_section_ptr,
2091
0
      0, name, false, collect, &bh)))
2092
0
      return false;
2093
0
    hi = (struct elf_link_hash_entry *) bh;
2094
0
  }
2095
0
    }
2096
0
  else
2097
0
    {
2098
      /* In this case the symbol named SHORTNAME is overriding the
2099
   indirect symbol we want to add.  We were planning on making
2100
   SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
2101
   is the name without a version.  NAME is the fully versioned
2102
   name, and it is the default version.
2103
2104
   Overriding means that we already saw a definition for the
2105
   symbol SHORTNAME in a regular object, and it is overriding
2106
   the symbol defined in the dynamic object.
2107
2108
   When this happens, we actually want to change NAME, the
2109
   symbol we just added, to refer to SHORTNAME.  This will cause
2110
   references to NAME in the shared object to become references
2111
   to SHORTNAME in the regular object.  This is what we expect
2112
   when we override a function in a shared object: that the
2113
   references in the shared object will be mapped to the
2114
   definition in the regular object.  */
2115
2116
0
      while (hi->root.type == bfd_link_hash_indirect
2117
0
       || hi->root.type == bfd_link_hash_warning)
2118
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2119
2120
0
      h->root.type = bfd_link_hash_indirect;
2121
0
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2122
0
      if (h->def_dynamic)
2123
0
  {
2124
0
    h->def_dynamic = 0;
2125
0
    hi->ref_dynamic = 1;
2126
0
    if (hi->ref_regular
2127
0
        || hi->def_regular)
2128
0
      {
2129
0
        if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2130
0
    return false;
2131
0
      }
2132
0
  }
2133
2134
      /* Now set HI to H, so that the following code will set the
2135
   other fields correctly.  */
2136
0
      hi = h;
2137
0
    }
2138
2139
  /* Check if HI is a warning symbol.  */
2140
0
  if (hi->root.type == bfd_link_hash_warning)
2141
0
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2142
2143
  /* If there is a duplicate definition somewhere, then HI may not
2144
     point to an indirect symbol.  We will have reported an error to
2145
     the user in that case.  */
2146
2147
0
  if (hi->root.type == bfd_link_hash_indirect)
2148
0
    {
2149
0
      struct elf_link_hash_entry *ht;
2150
2151
0
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2152
0
      obed->elf_backend_copy_indirect_symbol (info, ht, hi);
2153
2154
      /* If we first saw a reference to SHORTNAME with non-default
2155
   visibility, merge that visibility to the @@VER symbol.  */
2156
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, ht, hi->other,
2157
0
        sec, true, dynamic);
2158
2159
      /* A reference to the SHORTNAME symbol from a dynamic library
2160
   will be satisfied by the versioned symbol at runtime.  In
2161
   effect, we have a reference to the versioned symbol.  */
2162
0
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2163
0
      hi->dynamic_def |= ht->dynamic_def;
2164
2165
      /* See if the new flags lead us to realize that the symbol must
2166
   be dynamic.  */
2167
0
      if (! *dynsym)
2168
0
  {
2169
0
    if (! dynamic)
2170
0
      {
2171
0
        if (! bfd_link_executable (info)
2172
0
      || hi->def_dynamic
2173
0
      || hi->ref_dynamic)
2174
0
    *dynsym = true;
2175
0
      }
2176
0
    else
2177
0
      {
2178
0
        if (hi->ref_regular)
2179
0
    *dynsym = true;
2180
0
      }
2181
0
  }
2182
0
    }
2183
2184
  /* We also need to define an indirection from the nondefault version
2185
     of the symbol.  */
2186
2187
0
 nondefault:
2188
0
  len = strlen (name);
2189
0
  shortname = bfd_hash_allocate (&info->hash->table, len);
2190
0
  if (shortname == NULL)
2191
0
    return false;
2192
0
  memcpy (shortname, name, shortlen);
2193
0
  memcpy (shortname + shortlen, p + 1, len - shortlen);
2194
2195
  /* Once again, merge with any existing symbol.  */
2196
0
  type_change_ok = false;
2197
0
  size_change_ok = false;
2198
0
  tmp_sec = sec;
2199
0
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2200
0
            &hi, poldbfd, NULL, NULL, &skip, &override,
2201
0
            &type_change_ok, &size_change_ok, &matched))
2202
0
    return false;
2203
2204
0
  if (skip)
2205
0
    {
2206
0
      if (!dynamic
2207
0
    && h->root.type == bfd_link_hash_defweak
2208
0
    && hi->root.type == bfd_link_hash_defined)
2209
0
  {
2210
    /* We are handling a weak sym@@ver and attempting to define
2211
       a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2212
       new weak sym@ver because there is already a strong sym@ver.
2213
       However, sym@ver and sym@@ver are really the same symbol.
2214
       The existing strong sym@ver ought to override sym@@ver.  */
2215
0
    h->root.type = bfd_link_hash_defined;
2216
0
    h->root.u.def.section = hi->root.u.def.section;
2217
0
    h->root.u.def.value = hi->root.u.def.value;
2218
0
    hi->root.type = bfd_link_hash_indirect;
2219
0
    hi->root.u.i.link = &h->root;
2220
0
  }
2221
0
      else
2222
0
  return true;
2223
0
    }
2224
0
  else if (override)
2225
0
    {
2226
      /* Here SHORTNAME is a versioned name, so we don't expect to see
2227
   the type of override we do in the case above unless it is
2228
   overridden by a versioned definition.  */
2229
0
      if (hi->root.type != bfd_link_hash_defined
2230
0
    && hi->root.type != bfd_link_hash_defweak)
2231
0
  _bfd_error_handler
2232
    /* xgettext:c-format */
2233
0
    (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2234
0
     abfd, shortname);
2235
0
      return true;
2236
0
    }
2237
0
  else
2238
0
    {
2239
0
      bh = &hi->root;
2240
0
      if (! (_bfd_generic_link_add_one_symbol
2241
0
       (info, abfd, shortname, BSF_INDIRECT,
2242
0
        bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2243
0
  return false;
2244
0
      hi = (struct elf_link_hash_entry *) bh;
2245
0
    }
2246
2247
  /* If there is a duplicate definition somewhere, then HI may not
2248
     point to an indirect symbol.  We will have reported an error
2249
     to the user in that case.  */
2250
0
  if (hi->root.type == bfd_link_hash_indirect)
2251
0
    {
2252
0
      obed->elf_backend_copy_indirect_symbol (info, h, hi);
2253
0
      h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2254
0
      hi->dynamic_def |= h->dynamic_def;
2255
2256
      /* If we first saw a reference to @VER symbol with
2257
   non-default visibility, merge that visibility to the
2258
   @@VER symbol.  */
2259
0
      elf_merge_st_other (info->output_bfd, info->output_bfd, h, hi->other,
2260
0
        sec, true, dynamic);
2261
2262
      /* See if the new flags lead us to realize that the symbol
2263
   must be dynamic.  */
2264
0
      if (! *dynsym)
2265
0
  {
2266
0
    if (! dynamic)
2267
0
      {
2268
0
        if (! bfd_link_executable (info)
2269
0
      || hi->ref_dynamic)
2270
0
    *dynsym = true;
2271
0
      }
2272
0
    else
2273
0
      {
2274
0
        if (hi->ref_regular)
2275
0
    *dynsym = true;
2276
0
      }
2277
0
  }
2278
0
    }
2279
2280
0
  return true;
2281
0
}
2282

2283
/* This routine is used to export all defined symbols into the dynamic
2284
   symbol table.  It is called via elf_link_hash_traverse.  */
2285
2286
static bool
2287
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2288
0
{
2289
0
  struct elf_info_failed *eif = data;
2290
2291
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2292
0
  if (h->root.type == bfd_link_hash_indirect)
2293
0
    return true;
2294
2295
  /* Ignore this if we won't export it.  */
2296
0
  if (!eif->info->export_dynamic && !h->dynamic)
2297
0
    return true;
2298
2299
0
  if (h->dynindx == -1
2300
0
      && (h->def_regular || h->ref_regular)
2301
0
      && ! bfd_hide_sym_by_version (eif->info->version_info,
2302
0
            h->root.root.string))
2303
0
    {
2304
0
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2305
0
  {
2306
0
    eif->failed = true;
2307
0
    return false;
2308
0
  }
2309
0
    }
2310
2311
0
  return true;
2312
0
}
2313

2314
/* Return true if linked against glibc.  Otherwise return false.  If
2315
   linked against glibc, add VERSION_DEP to the list of glibc version
2316
   dependencies and set *AUTO_VERSION to true.  If *AUTO_VERSION is
2317
   true, add VERSION_DEP to the version dependency list only if libc.so
2318
   defines VERSION_DEP.  GLIBC_MINOR_BASE is the pointer to the glibc
2319
   minor base version.  */
2320
2321
static bool
2322
elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2323
          const char *version_dep,
2324
          int *glibc_minor_base,
2325
          bool *auto_version)
2326
0
{
2327
0
  Elf_Internal_Verneed *t;
2328
0
  Elf_Internal_Vernaux *a;
2329
0
  int minor_version = -1;
2330
0
  bool added = false;
2331
0
  bool glibc = false;
2332
2333
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2334
0
       t != NULL;
2335
0
       t = t->vn_nextref)
2336
0
    {
2337
0
      const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2338
0
      if (soname != NULL && startswith (soname, "libc.so."))
2339
0
  break;
2340
0
    }
2341
2342
  /* Skip the shared library if it isn't libc.so.  */
2343
0
  if (t == NULL)
2344
0
    goto update_auto_version_and_return;
2345
2346
0
  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2347
0
    {
2348
      /* Return if VERSION_DEP dependency has been added.  */
2349
0
      if (a->vna_nodename == version_dep
2350
0
    || strcmp (a->vna_nodename, version_dep) == 0)
2351
0
  {
2352
0
    glibc = true;
2353
0
    goto update_auto_version_and_return;
2354
0
  }
2355
2356
      /* Check if libc.so provides GLIBC_2.XX version.  */
2357
0
      if (startswith (a->vna_nodename, "GLIBC_2."))
2358
0
  {
2359
0
    minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2360
0
    if (minor_version < *glibc_minor_base)
2361
0
      *glibc_minor_base = minor_version;
2362
0
  }
2363
0
    }
2364
2365
  /* Skip if it isn't linked against glibc.  */
2366
0
  if (minor_version < 0)
2367
0
    goto update_auto_version_and_return;
2368
2369
0
  glibc = true;
2370
2371
0
  if (auto_version && *auto_version)
2372
0
    {
2373
      /* Add VERSION_DEP to the version dependency list only if
2374
   libc.so defines VERSION_DEP.  */
2375
2376
0
      bool defined = false;
2377
0
      Elf_Internal_Verdef *d;
2378
2379
0
      for (d = elf_tdata (t->vn_bfd)->verdef;
2380
0
     d != NULL;
2381
0
     d = d->vd_nextdef)
2382
0
  if (strcmp (d->vd_nodename, version_dep) == 0)
2383
0
    {
2384
0
      defined = true;
2385
0
      break;
2386
0
    }
2387
2388
      /* Set *AUTO_VERSION to false and return true to indicate that
2389
   libc.so doesn't define VERSION_DEP.  */
2390
0
      if (!defined)
2391
0
  goto update_auto_version_and_return;
2392
0
    }
2393
2394
  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
2395
0
  if (startswith (version_dep, "GLIBC_2."))
2396
0
    {
2397
0
      minor_version = strtol (version_dep + 8, NULL, 10);
2398
0
      if (minor_version <= *glibc_minor_base)
2399
0
  goto update_auto_version_and_return;
2400
0
    }
2401
2402
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2403
0
  if (a == NULL)
2404
0
    {
2405
0
      rinfo->failed = true;
2406
0
      glibc = false;
2407
0
      goto update_auto_version_and_return;
2408
0
    }
2409
2410
0
  a->vna_nodename = version_dep;
2411
0
  a->vna_flags = 0;
2412
0
  a->vna_nextptr = t->vn_auxptr;
2413
0
  a->vna_other = rinfo->vers + 1;
2414
0
  ++rinfo->vers;
2415
2416
0
  t->vn_auxptr = a;
2417
2418
0
  added = true;
2419
2420
0
 update_auto_version_and_return:
2421
0
  if (auto_version)
2422
0
    *auto_version = added;
2423
2424
0
  return glibc;
2425
0
}
2426
2427
/* Add VERSION_DEP to the list of version dependencies when linked
2428
   against glibc.  */
2429
2430
bool
2431
_bfd_elf_link_add_glibc_version_dependency
2432
  (struct elf_find_verdep_info *rinfo,
2433
   const char *const version_dep[],
2434
   bool *auto_version)
2435
0
{
2436
0
  int glibc_minor_base = INT_MAX;
2437
2438
0
  do
2439
0
    {
2440
      /* Return if not linked against glibc.  */
2441
0
      if (!elf_link_add_glibc_verneed (rinfo, *version_dep,
2442
0
               &glibc_minor_base, auto_version))
2443
0
  return false;
2444
0
      version_dep++;
2445
0
      auto_version++;
2446
0
    }
2447
0
  while (*version_dep != NULL);
2448
2449
0
  return true;
2450
0
}
2451
2452
/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2453
   linked against glibc.  */
2454
2455
void
2456
_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2457
0
{
2458
0
  if (rinfo->info->enable_dt_relr)
2459
0
    {
2460
0
      static const char *const version[] =
2461
0
  {
2462
0
    "GLIBC_ABI_DT_RELR",
2463
0
    NULL
2464
0
  };
2465
0
      _bfd_elf_link_add_glibc_version_dependency (rinfo, version, NULL);
2466
0
    }
2467
0
}
2468
2469
/* Look through the symbols which are defined in other shared
2470
   libraries and referenced here.  Update the list of version
2471
   dependencies.  This will be put into the .gnu.version_r section.
2472
   This function is called via elf_link_hash_traverse.  */
2473
2474
static bool
2475
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2476
           void *data)
2477
0
{
2478
0
  struct elf_find_verdep_info *rinfo = data;
2479
0
  Elf_Internal_Verneed *t;
2480
0
  Elf_Internal_Vernaux *a;
2481
2482
  /* We only care about symbols defined in shared objects with version
2483
     information.  */
2484
0
  if (!h->def_dynamic
2485
0
      || h->def_regular
2486
0
      || h->dynindx == -1
2487
0
      || h->verinfo.verdef == NULL
2488
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2489
0
    & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2490
0
    return true;
2491
2492
  /* See if we already know about this version.  */
2493
0
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2494
0
       t != NULL;
2495
0
       t = t->vn_nextref)
2496
0
    {
2497
0
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2498
0
  continue;
2499
2500
0
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2501
0
  if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2502
0
    return true;
2503
2504
0
      break;
2505
0
    }
2506
2507
  /* This is a new version.  Add it to tree we are building.  */
2508
2509
0
  if (t == NULL)
2510
0
    {
2511
0
      t = bfd_zalloc (rinfo->info->output_bfd, sizeof (*t));
2512
0
      if (t == NULL)
2513
0
  {
2514
0
    rinfo->failed = true;
2515
0
    return false;
2516
0
  }
2517
2518
0
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2519
0
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2520
0
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2521
0
    }
2522
2523
0
  a = bfd_zalloc (rinfo->info->output_bfd, sizeof (*a));
2524
0
  if (a == NULL)
2525
0
    {
2526
0
      rinfo->failed = true;
2527
0
      return false;
2528
0
    }
2529
2530
  /* Note that we are copying a string pointer here, and testing it
2531
     above.  If bfd_elf_string_from_elf_section is ever changed to
2532
     discard the string data when low in memory, this will have to be
2533
     fixed.  */
2534
0
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2535
2536
0
  a->vna_flags = h->verinfo.verdef->vd_flags;
2537
0
  a->vna_nextptr = t->vn_auxptr;
2538
2539
0
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2540
0
  ++rinfo->vers;
2541
2542
0
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2543
2544
0
  t->vn_auxptr = a;
2545
2546
0
  return true;
2547
0
}
2548
2549
/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2550
   hidden.  Set *T_P to NULL if there is no match.  */
2551
2552
static bool
2553
_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2554
             struct elf_link_hash_entry *h,
2555
             const char *version_p,
2556
             struct bfd_elf_version_tree **t_p,
2557
             bool *hide)
2558
0
{
2559
0
  struct bfd_elf_version_tree *t;
2560
2561
  /* Look for the version.  If we find it, it is no longer weak.  */
2562
0
  for (t = info->version_info; t != NULL; t = t->next)
2563
0
    {
2564
0
      if (strcmp (t->name, version_p) == 0)
2565
0
  {
2566
0
    size_t len;
2567
0
    char *alc;
2568
0
    struct bfd_elf_version_expr *d;
2569
2570
0
    len = version_p - h->root.root.string;
2571
0
    alc = bfd_malloc (len);
2572
0
    if (alc == NULL)
2573
0
      return false;
2574
0
    memcpy (alc, h->root.root.string, len - 1);
2575
0
    alc[len - 1] = '\0';
2576
0
    if (alc[len - 2] == ELF_VER_CHR)
2577
0
      alc[len - 2] = '\0';
2578
2579
0
    h->verinfo.vertree = t;
2580
0
    t->used = true;
2581
0
    d = NULL;
2582
2583
0
    if (t->globals.list != NULL)
2584
0
      d = (*t->match) (&t->globals, NULL, alc);
2585
2586
    /* See if there is anything to force this symbol to
2587
       local scope.  */
2588
0
    if (d == NULL && t->locals.list != NULL)
2589
0
      {
2590
0
        d = (*t->match) (&t->locals, NULL, alc);
2591
0
        if (d != NULL
2592
0
      && h->dynindx != -1
2593
0
      && ! info->export_dynamic)
2594
0
    *hide = true;
2595
0
      }
2596
2597
0
    free (alc);
2598
0
    break;
2599
0
  }
2600
0
    }
2601
2602
0
  *t_p = t;
2603
2604
0
  return true;
2605
0
}
2606
2607
/* Return TRUE if the symbol H is hidden by version script.  */
2608
2609
bool
2610
_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2611
           struct elf_link_hash_entry *h)
2612
0
{
2613
0
  const char *p;
2614
0
  bool hide = false;
2615
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
2616
2617
  /* Version script only hides symbols defined in regular objects.  */
2618
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2619
0
    return true;
2620
2621
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2622
0
  if (p != NULL && h->verinfo.vertree == NULL)
2623
0
    {
2624
0
      struct bfd_elf_version_tree *t;
2625
2626
0
      ++p;
2627
0
      if (*p == ELF_VER_CHR)
2628
0
  ++p;
2629
2630
0
      if (*p != '\0'
2631
0
    && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2632
0
    && hide)
2633
0
  {
2634
0
    if (hide)
2635
0
      obed->elf_backend_hide_symbol (info, h, true);
2636
0
    return true;
2637
0
  }
2638
0
    }
2639
2640
  /* If we don't have a version for this symbol, see if we can find
2641
     something.  */
2642
0
  if (h->verinfo.vertree == NULL && info->version_info != NULL)
2643
0
    {
2644
0
      h->verinfo.vertree
2645
0
  = bfd_find_version_for_sym (info->version_info,
2646
0
            h->root.root.string, &hide);
2647
0
      if (h->verinfo.vertree != NULL && hide)
2648
0
  {
2649
0
    obed->elf_backend_hide_symbol (info, h, true);
2650
0
    return true;
2651
0
  }
2652
0
    }
2653
2654
0
  return false;
2655
0
}
2656
2657
/* Figure out appropriate versions for all the symbols.  We may not
2658
   have the version number script until we have read all of the input
2659
   files, so until that point we don't know which symbols should be
2660
   local.  This function is called via elf_link_hash_traverse.  */
2661
2662
static bool
2663
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2664
0
{
2665
0
  struct elf_info_failed *sinfo = data;
2666
0
  struct bfd_link_info *info = sinfo->info;
2667
0
  elf_backend_data *obed;
2668
0
  struct elf_info_failed eif;
2669
0
  const char *p;
2670
0
  bool hide;
2671
2672
  /* Fix the symbol flags.  */
2673
0
  eif.failed = false;
2674
0
  eif.info = info;
2675
0
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2676
0
    {
2677
0
      if (eif.failed)
2678
0
  sinfo->failed = true;
2679
0
      return false;
2680
0
    }
2681
2682
0
  obed = get_elf_backend_data (info->output_bfd);
2683
2684
  /* We only need version numbers for symbols defined in regular
2685
     objects.  */
2686
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2687
0
    {
2688
      /* Hide symbols defined in discarded input sections.  */
2689
0
      if ((h->root.type == bfd_link_hash_defined
2690
0
     || h->root.type == bfd_link_hash_defweak)
2691
0
    && discarded_section (h->root.u.def.section))
2692
0
  obed->elf_backend_hide_symbol (info, h, true);
2693
0
      return true;
2694
0
    }
2695
2696
0
  hide = false;
2697
0
  p = strchr (h->root.root.string, ELF_VER_CHR);
2698
0
  if (p != NULL && h->verinfo.vertree == NULL)
2699
0
    {
2700
0
      struct bfd_elf_version_tree *t;
2701
0
      bool default_version = false;
2702
2703
0
      ++p;
2704
0
      if (*p == ELF_VER_CHR)
2705
0
  {
2706
0
    default_version = true;
2707
0
    ++p;
2708
0
  }
2709
2710
      /* If there is no version string, we can just return out.  */
2711
0
      if (*p == '\0')
2712
0
  return true;
2713
2714
0
      if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2715
0
  {
2716
0
    sinfo->failed = true;
2717
0
    return false;
2718
0
  }
2719
2720
0
      if (hide)
2721
0
  obed->elf_backend_hide_symbol (info, h, true);
2722
0
      else if (default_version)
2723
0
  {
2724
    /* Get the unversioned symbol for the default version.  */
2725
0
    struct elf_link_hash_entry *h_u;
2726
0
    size_t size = p - h->root.root.string - 1;
2727
0
    char *unversioned_name = bfd_malloc (size);
2728
0
    if (unversioned_name == NULL)
2729
0
      {
2730
0
        sinfo->failed = true;
2731
0
        return false;
2732
0
      }
2733
0
    memcpy (unversioned_name, h->root.root.string, size - 1);
2734
0
    unversioned_name[size - 1] = 0;
2735
0
    h_u = elf_link_hash_lookup (elf_hash_table (info),
2736
0
              unversioned_name, false,
2737
0
              false, false);
2738
2739
    /* There must be an unversioned symbol. */
2740
0
    if (h_u == NULL)
2741
0
      abort ();
2742
2743
0
    while (h_u->root.type == bfd_link_hash_indirect
2744
0
     || h_u->root.type == bfd_link_hash_warning)
2745
0
      h_u = (struct elf_link_hash_entry *) h_u->root.u.i.link;
2746
2747
    /* Verify that there is only one default version.  */
2748
0
    if (h_u->versioned != versioned_hidden
2749
0
        && h_u->verinfo.vertree != h->verinfo.vertree)
2750
0
      {
2751
        /* xgettext:c-format */
2752
0
        info->callbacks->einfo
2753
0
    (_("%X%P: %pB: multiple default versions of `%s': "
2754
0
       "`%s' in %pB and `%s' in %pB.\n"),
2755
0
     info->output_bfd, unversioned_name,
2756
0
     h_u->verinfo.vertree->name,
2757
0
     h_u->root.u.def.section->owner,
2758
0
     h->verinfo.vertree->name,
2759
0
     h->root.u.def.section->owner);
2760
0
        bfd_set_error (bfd_error_bad_value);
2761
0
        sinfo->failed = true;
2762
0
        free (unversioned_name);
2763
0
        return false;
2764
0
      }
2765
2766
0
    free (unversioned_name);
2767
0
  }
2768
2769
      /* If we are building an application, we need to create a
2770
   version node for this version.  */
2771
0
      if (t == NULL && bfd_link_executable (info))
2772
0
  {
2773
0
    struct bfd_elf_version_tree **pp;
2774
0
    int version_index;
2775
2776
    /* If we aren't going to export this symbol, we don't need
2777
       to worry about it.  */
2778
0
    if (h->dynindx == -1)
2779
0
      return true;
2780
2781
0
    t = bfd_zalloc (info->output_bfd, sizeof (*t));
2782
0
    if (t == NULL)
2783
0
      {
2784
0
        sinfo->failed = true;
2785
0
        return false;
2786
0
      }
2787
2788
0
    t->name = p;
2789
0
    t->name_indx = (unsigned int) -1;
2790
0
    t->used = true;
2791
2792
0
    version_index = 1;
2793
    /* Don't count anonymous version tag.  */
2794
0
    if (sinfo->info->version_info != NULL
2795
0
        && sinfo->info->version_info->vernum == 0)
2796
0
      version_index = 0;
2797
0
    for (pp = &sinfo->info->version_info;
2798
0
         *pp != NULL;
2799
0
         pp = &(*pp)->next)
2800
0
      ++version_index;
2801
0
    t->vernum = version_index;
2802
2803
0
    *pp = t;
2804
2805
0
    h->verinfo.vertree = t;
2806
0
  }
2807
0
      else if (t == NULL)
2808
0
  {
2809
    /* We could not find the version for a symbol when
2810
       generating a shared archive.  Return an error.  */
2811
0
    _bfd_error_handler
2812
      /* xgettext:c-format */
2813
0
      (_("%pB: version node not found for symbol %s"),
2814
0
       info->output_bfd, h->root.root.string);
2815
0
    bfd_set_error (bfd_error_bad_value);
2816
0
    sinfo->failed = true;
2817
0
    return false;
2818
0
  }
2819
0
    }
2820
2821
  /* If we don't have a version for this symbol, see if we can find
2822
     something.  */
2823
0
  if (!hide
2824
0
      && h->verinfo.vertree == NULL
2825
0
      && sinfo->info->version_info != NULL)
2826
0
    {
2827
0
      h->verinfo.vertree
2828
0
  = bfd_find_version_for_sym (sinfo->info->version_info,
2829
0
            h->root.root.string, &hide);
2830
0
      if (h->verinfo.vertree != NULL && hide)
2831
0
  obed->elf_backend_hide_symbol (info, h, true);
2832
0
    }
2833
2834
0
  return true;
2835
0
}
2836

2837
/* Read and swap the relocs from the section indicated by SHDR.  This
2838
   may be either a REL or a RELA section.  The relocations are
2839
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2840
   which should have already been allocated to contain enough space.
2841
   The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2842
   relocations should be stored.  If *EXTERNAL_RELOCS_ADDR is NULL,
2843
   *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2844
   memory address and size.  Otherwise, *EXTERNAL_RELOCS_ADDR is
2845
   unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2846
2847
   Returns FALSE if something goes wrong.  */
2848
2849
static bool
2850
elf_link_read_relocs_from_section (bfd *abfd,
2851
           const asection *sec,
2852
           Elf_Internal_Shdr *shdr,
2853
           void **external_relocs_addr,
2854
           size_t *external_relocs_size,
2855
           Elf_Internal_Rela *internal_relocs)
2856
835
{
2857
835
  elf_backend_data *bed;
2858
835
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2859
835
  const bfd_byte *erela;
2860
835
  const bfd_byte *erelaend;
2861
835
  Elf_Internal_Rela *irela;
2862
835
  Elf_Internal_Shdr *symtab_hdr;
2863
835
  struct elf_link_hash_entry **sym_hashes;
2864
835
  bool hash_check;
2865
835
  size_t nsyms;
2866
835
  void *external_relocs = *external_relocs_addr;
2867
2868
  /* Position ourselves at the start of the section.  */
2869
835
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2870
288
    return false;
2871
2872
  /* Read the relocations.  */
2873
547
  *external_relocs_size = shdr->sh_size;
2874
547
  if (!_bfd_mmap_read_temporary (&external_relocs,
2875
547
         external_relocs_size,
2876
547
         external_relocs_addr, abfd, true))
2877
265
    return false;
2878
2879
282
  symtab_hdr = &elf_symtab_hdr (abfd);
2880
282
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2881
282
  sym_hashes = elf_sym_hashes (abfd);
2882
282
  hash_check = elf_sym_hashes_valid (abfd);
2883
2884
282
  bed = get_elf_backend_data (abfd);
2885
2886
  /* Convert the external relocations to the internal format.  */
2887
282
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2888
0
    swap_in = bed->s->swap_reloc_in;
2889
282
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2890
282
    swap_in = bed->s->swap_reloca_in;
2891
0
  else
2892
0
    {
2893
0
      bfd_set_error (bfd_error_wrong_format);
2894
0
      return false;
2895
0
    }
2896
2897
282
  erela = (const bfd_byte *) external_relocs;
2898
  /* Setting erelaend like this and comparing with <= handles case of
2899
     a fuzzed object with sh_size not a multiple of sh_entsize.  */
2900
282
  erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2901
282
  irela = internal_relocs;
2902
569
  while (erela <= erelaend)
2903
564
    {
2904
564
      bfd_vma r_symndx;
2905
2906
564
      (*swap_in) (abfd, erela, irela);
2907
564
      r_symndx = ELF32_R_SYM (irela->r_info);
2908
564
      if (bed->s->arch_size == 64)
2909
564
  r_symndx >>= 24;
2910
564
      if (r_symndx >= nsyms
2911
287
    || (hash_check
2912
0
        && r_symndx >= symtab_hdr->sh_info
2913
0
        && (sym_hashes == NULL
2914
0
      || (!elf_bad_symtab (abfd)
2915
0
          && sym_hashes[r_symndx - symtab_hdr->sh_info] == NULL))))
2916
277
  {
2917
277
    _bfd_error_handler
2918
      /* xgettext:c-format */
2919
277
      (_("%pB:(%pA+%#" PRIx64 "): bad reloc symbol index %#" PRIx64),
2920
277
       abfd, sec, (uint64_t) irela->r_offset, (uint64_t) r_symndx);
2921
277
    bfd_set_error (bfd_error_bad_value);
2922
277
    return false;
2923
277
  }
2924
287
      irela += bed->s->int_rels_per_ext_rel;
2925
287
      erela += shdr->sh_entsize;
2926
287
    }
2927
2928
5
  return true;
2929
282
}
2930
2931
/* Read and swap the relocs for a section O.  They may have been
2932
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2933
   not NULL, they are used as buffers to read into.  They are known to
2934
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2935
   the return value is allocated using malloc.  If O has both a REL
2936
   relocation section and a RELA relocation section, then the REL
2937
   relocations will appear first in INTERNAL_RELOCS, followed by the
2938
   RELA relocations.  If KEEP_MEMORY is true the internal relocs are
2939
   cached, with info->cache_size updated if INFO is non-NULL.  */
2940
2941
Elf_Internal_Rela *
2942
_bfd_elf_link_info_read_relocs (bfd *abfd,
2943
        struct bfd_link_info *info,
2944
        const asection *o,
2945
        void *external_relocs,
2946
        Elf_Internal_Rela *internal_relocs,
2947
        bool keep_memory)
2948
1.20k
{
2949
1.20k
  void *ext_alloc1, *ext_alloc2;
2950
1.20k
  size_t ext_mmap1, ext_mmap2;
2951
1.20k
  Elf_Internal_Rela *int_alloc = NULL;
2952
1.20k
  elf_backend_data *bed = get_elf_backend_data (abfd);
2953
1.20k
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2954
1.20k
  Elf_Internal_Rela *internal_rela_relocs;
2955
2956
1.20k
  if (esdo->relocs != NULL)
2957
367
    return esdo->relocs;
2958
2959
835
  if (o->reloc_count == 0)
2960
0
    return NULL;
2961
2962
835
  if (internal_relocs == NULL)
2963
835
    {
2964
835
      bfd_size_type size;
2965
2966
835
      size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2967
835
      if (keep_memory && info)
2968
0
  info->cache_size += size;
2969
835
      internal_relocs = int_alloc = bfd_malloc (size);
2970
835
      if (internal_relocs == NULL)
2971
0
  return NULL;
2972
835
    }
2973
2974
835
  ext_alloc1 = external_relocs;
2975
835
  ext_mmap1 = 0;
2976
835
  internal_rela_relocs = internal_relocs;
2977
835
  if (esdo->rel.hdr)
2978
0
    {
2979
0
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2980
0
                &ext_alloc1, &ext_mmap1,
2981
0
                internal_relocs))
2982
0
  {
2983
0
    internal_relocs = NULL;
2984
0
    goto out1;
2985
0
  }
2986
0
      if (external_relocs != NULL)
2987
0
  external_relocs = (((bfd_byte *) external_relocs)
2988
0
         + esdo->rel.hdr->sh_size);
2989
0
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2990
0
             * bed->s->int_rels_per_ext_rel);
2991
0
    }
2992
2993
835
  ext_alloc2 = external_relocs;
2994
835
  ext_mmap2 = 0;
2995
835
  if (esdo->rela.hdr
2996
835
      && !elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2997
835
               &ext_alloc2, &ext_mmap2,
2998
835
               internal_rela_relocs))
2999
830
    internal_relocs = NULL;
3000
3001
  /* If the external relocs were mmap'd then we want to munmap them
3002
     regardless of whether or not an external_relocs buffer was
3003
     provided.  If they were not mmap'd then we only want to free
3004
     buffers allocated here.  */
3005
835
  if (ext_mmap2 != 0 || external_relocs == NULL)
3006
835
    _bfd_munmap_temporary (ext_alloc2, ext_mmap2);
3007
835
 out1:
3008
835
  if (ext_mmap1 != 0 || external_relocs == NULL)
3009
835
    _bfd_munmap_temporary (ext_alloc1, ext_mmap1);
3010
3011
  /* Don't free int_alloc, if we are returning it under the name of
3012
     internal_relocs.  */
3013
835
  if (internal_relocs == NULL)
3014
830
    free (int_alloc);
3015
3016
  /* Cache the results for next time, if we can.  */
3017
5
  else if (keep_memory)
3018
5
    esdo->relocs = internal_relocs;
3019
3020
835
  return internal_relocs;
3021
835
}
3022
3023
/* This is similar to _bfd_elf_link_info_read_relocs, except for that
3024
   NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
3025
   struct bfd_link_info.  */
3026
3027
Elf_Internal_Rela *
3028
_bfd_elf_link_read_relocs (bfd *abfd,
3029
         const asection *o,
3030
         void *external_relocs,
3031
         Elf_Internal_Rela *internal_relocs,
3032
         bool keep_memory)
3033
1.20k
{
3034
1.20k
  return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
3035
1.20k
           internal_relocs, keep_memory);
3036
3037
1.20k
}
3038
3039
/* Compute the size of, and allocate space for, REL_HDR which is the
3040
   section header for a section containing relocations for O.  */
3041
3042
static bool
3043
_bfd_elf_link_size_reloc_section (bfd *obfd,
3044
          struct bfd_elf_section_reloc_data *reldata)
3045
0
{
3046
0
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
3047
3048
  /* That allows us to calculate the size of the section.  */
3049
0
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
3050
3051
  /* The contents field must last into write_object_contents, so we
3052
     allocate it with bfd_alloc rather than malloc.  Also since we
3053
     cannot be sure that the contents will actually be filled in,
3054
     we zero the allocated space.  */
3055
0
  rel_hdr->contents = bfd_zalloc (obfd, rel_hdr->sh_size);
3056
0
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3057
0
    return false;
3058
3059
0
  if (reldata->hashes == NULL && reldata->count)
3060
0
    {
3061
0
      struct elf_link_hash_entry **p;
3062
3063
0
      p = bfd_zmalloc (reldata->count * sizeof (*p));
3064
0
      if (p == NULL)
3065
0
  return false;
3066
3067
0
      reldata->hashes = p;
3068
0
    }
3069
3070
0
  return true;
3071
0
}
3072
3073
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
3074
   originated from the section given by INPUT_REL_HDR) to the
3075
   OUTPUT_BFD.  */
3076
3077
bool
3078
_bfd_elf_link_output_relocs (bfd *output_bfd,
3079
           asection *input_section,
3080
           Elf_Internal_Shdr *input_rel_hdr,
3081
           Elf_Internal_Rela *internal_relocs,
3082
           struct elf_link_hash_entry **rel_hash)
3083
0
{
3084
0
  Elf_Internal_Rela *irela;
3085
0
  Elf_Internal_Rela *irelaend;
3086
0
  bfd_byte *erel;
3087
0
  struct bfd_elf_section_reloc_data *output_reldata;
3088
0
  asection *output_section;
3089
0
  elf_backend_data *obed;
3090
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
3091
0
  struct bfd_elf_section_data *esdo;
3092
3093
0
  output_section = input_section->output_section;
3094
3095
0
  obed = get_elf_backend_data (output_bfd);
3096
0
  esdo = elf_section_data (output_section);
3097
0
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3098
0
    {
3099
0
      output_reldata = &esdo->rel;
3100
0
      swap_out = obed->s->swap_reloc_out;
3101
0
    }
3102
0
  else if (esdo->rela.hdr
3103
0
     && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3104
0
    {
3105
0
      output_reldata = &esdo->rela;
3106
0
      swap_out = obed->s->swap_reloca_out;
3107
0
    }
3108
0
  else
3109
0
    {
3110
0
      _bfd_error_handler
3111
  /* xgettext:c-format */
3112
0
  (_("%pB: relocation size mismatch in %pB section %pA"),
3113
0
   output_bfd, input_section->owner, input_section);
3114
0
      bfd_set_error (bfd_error_wrong_format);
3115
0
      return false;
3116
0
    }
3117
3118
0
  erel = output_reldata->hdr->contents;
3119
0
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
3120
0
  irela = internal_relocs;
3121
0
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3122
0
          * obed->s->int_rels_per_ext_rel);
3123
0
  while (irela < irelaend)
3124
0
    {
3125
0
      if (rel_hash && *rel_hash)
3126
0
  (*rel_hash)->has_reloc = 1;
3127
0
      (*swap_out) (output_bfd, irela, erel);
3128
0
      irela += obed->s->int_rels_per_ext_rel;
3129
0
      erel += input_rel_hdr->sh_entsize;
3130
0
      if (rel_hash)
3131
0
  rel_hash++;
3132
0
    }
3133
3134
  /* Bump the counter, so that we know where to add the next set of
3135
     relocations.  */
3136
0
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3137
3138
0
  return true;
3139
0
}
3140

3141
/* Make weak undefined symbols in PIE dynamic.  */
3142
3143
bool
3144
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3145
         struct elf_link_hash_entry *h)
3146
0
{
3147
0
  if (bfd_link_pie (info)
3148
0
      && h->dynindx == -1
3149
0
      && h->root.type == bfd_link_hash_undefweak)
3150
0
    return bfd_elf_link_record_dynamic_symbol (info, h);
3151
3152
0
  return true;
3153
0
}
3154
3155
/* Fix up the flags for a symbol.  This handles various cases which
3156
   can only be fixed after all the input files are seen.  This is
3157
   currently called by both adjust_dynamic_symbol and
3158
   assign_sym_version, which is unnecessary but perhaps more robust in
3159
   the face of future changes.  */
3160
3161
static bool
3162
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3163
         struct elf_info_failed *eif)
3164
0
{
3165
0
  elf_backend_data *obed;
3166
3167
  /* If this symbol was mentioned in a non-ELF file, try to set
3168
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
3169
     permit a non-ELF file to correctly refer to a symbol defined in
3170
     an ELF dynamic object.  */
3171
0
  if (h->non_elf)
3172
0
    {
3173
0
      while (h->root.type == bfd_link_hash_indirect)
3174
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3175
3176
0
      if (h->root.type != bfd_link_hash_defined
3177
0
    && h->root.type != bfd_link_hash_defweak)
3178
0
  {
3179
0
    h->ref_regular = 1;
3180
0
    h->ref_regular_nonweak = 1;
3181
0
  }
3182
0
      else
3183
0
  {
3184
0
    if (h->root.u.def.section->owner != NULL
3185
0
        && (bfd_get_flavour (h->root.u.def.section->owner)
3186
0
      == bfd_target_elf_flavour))
3187
0
      {
3188
0
        h->ref_regular = 1;
3189
0
        h->ref_regular_nonweak = 1;
3190
0
      }
3191
0
    else
3192
0
      h->def_regular = 1;
3193
0
  }
3194
3195
0
      if (h->dynindx == -1
3196
0
    && (h->def_dynamic
3197
0
        || h->ref_dynamic))
3198
0
  {
3199
0
    if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3200
0
      {
3201
0
        eif->failed = true;
3202
0
        return false;
3203
0
      }
3204
0
  }
3205
0
    }
3206
0
  else
3207
0
    {
3208
      /* Unfortunately, NON_ELF is only correct if the symbol
3209
   was first seen in a non-ELF file.  Fortunately, if the symbol
3210
   was first seen in an ELF file, we're probably OK unless the
3211
   symbol was defined in a non-ELF file.  Catch that case here.
3212
   FIXME: We're still in trouble if the symbol was first seen in
3213
   a dynamic object, and then later in a non-ELF regular object.  */
3214
0
      if ((h->root.type == bfd_link_hash_defined
3215
0
     || h->root.type == bfd_link_hash_defweak)
3216
0
    && !h->def_regular
3217
0
    && (h->root.u.def.section->owner != NULL
3218
0
        ? (bfd_get_flavour (h->root.u.def.section->owner)
3219
0
     != bfd_target_elf_flavour)
3220
0
        : (bfd_is_abs_section (h->root.u.def.section)
3221
0
     && !h->def_dynamic)))
3222
0
  h->def_regular = 1;
3223
0
    }
3224
3225
  /* Backend specific symbol fixup.  */
3226
0
  obed = get_elf_backend_data (eif->info->output_bfd);
3227
0
  if (obed->elf_backend_fixup_symbol
3228
0
      && !obed->elf_backend_fixup_symbol (eif->info, h))
3229
0
    return false;
3230
3231
  /* If this is a final link, and the symbol was defined as a common
3232
     symbol in a regular object file, and there was no definition in
3233
     any dynamic object, then the linker will have allocated space for
3234
     the symbol in a common section but the DEF_REGULAR
3235
     flag will not have been set.  */
3236
0
  if (h->root.type == bfd_link_hash_defined
3237
0
      && !h->def_regular
3238
0
      && h->ref_regular
3239
0
      && !h->def_dynamic
3240
0
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3241
0
    h->def_regular = 1;
3242
3243
  /* Symbols defined in discarded sections shouldn't be dynamic.  */
3244
0
  if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3245
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3246
3247
  /* If a weak undefined symbol has non-default visibility, we also
3248
     hide it from the dynamic linker.  */
3249
0
  else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3250
0
     && h->root.type == bfd_link_hash_undefweak)
3251
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3252
3253
  /* A hidden versioned symbol in executable should be forced local if
3254
     it is is locally defined, not referenced by shared library and not
3255
     exported.  */
3256
0
  else if (bfd_link_executable (eif->info)
3257
0
     && h->versioned == versioned_hidden
3258
0
     && !eif->info->export_dynamic
3259
0
     && !h->dynamic
3260
0
     && !h->ref_dynamic
3261
0
     && h->def_regular)
3262
0
    obed->elf_backend_hide_symbol (eif->info, h, true);
3263
3264
  /* If -Bsymbolic was used (which means to bind references to global
3265
     symbols to the definition within the shared object), and this
3266
     symbol was defined in a regular object, then it actually doesn't
3267
     need a PLT entry.  Likewise, if the symbol has non-default
3268
     visibility.  If the symbol has hidden or internal visibility, we
3269
     will force it local.  */
3270
0
  else if (h->needs_plt
3271
0
     && bfd_link_pic (eif->info)
3272
0
     && is_elf_hash_table (eif->info->hash)
3273
0
     && (SYMBOLIC_BIND (eif->info, h)
3274
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3275
0
     && h->def_regular)
3276
0
    {
3277
0
      bool force_local;
3278
3279
0
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3280
0
         || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3281
0
      obed->elf_backend_hide_symbol (eif->info, h, force_local);
3282
0
    }
3283
3284
  /* If this is a weak defined symbol in a dynamic object, and we know
3285
     the real definition in the dynamic object, copy interesting flags
3286
     over to the real definition.  */
3287
0
  if (h->is_weakalias)
3288
0
    {
3289
0
      struct elf_link_hash_entry *def = weakdef (h);
3290
3291
      /* If the real definition is defined by a regular object file,
3292
   don't do anything special.  See the longer description in
3293
   _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
3294
   bfd_link_hash_defined as it was when put on the alias list
3295
   then it must have originally been a versioned symbol (for
3296
   which a non-versioned indirect symbol is created) and later
3297
   a definition for the non-versioned symbol is found.  In that
3298
   case the indirection is flipped with the versioned symbol
3299
   becoming an indirect pointing at the non-versioned symbol.
3300
   Thus, not an alias any more.  */
3301
0
      if (def->def_regular
3302
0
    || def->root.type != bfd_link_hash_defined)
3303
0
  {
3304
0
    h = def;
3305
0
    while ((h = h->u.alias) != def)
3306
0
      h->is_weakalias = 0;
3307
0
  }
3308
0
      else
3309
0
  {
3310
0
    while (h->root.type == bfd_link_hash_indirect)
3311
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3312
0
    BFD_ASSERT (h->root.type == bfd_link_hash_defined
3313
0
          || h->root.type == bfd_link_hash_defweak);
3314
0
    BFD_ASSERT (def->def_dynamic);
3315
0
    obed->elf_backend_copy_indirect_symbol (eif->info, def, h);
3316
0
  }
3317
0
    }
3318
3319
0
  return true;
3320
0
}
3321
3322
/* Make the backend pick a good value for a dynamic symbol.  This is
3323
   called via elf_link_hash_traverse, and also calls itself
3324
   recursively.  */
3325
3326
static bool
3327
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3328
0
{
3329
0
  struct elf_info_failed *eif = data;
3330
0
  struct elf_link_hash_table *htab;
3331
0
  elf_backend_data *obed;
3332
3333
0
  if (! is_elf_hash_table (eif->info->hash))
3334
0
    return false;
3335
3336
0
  htab = elf_hash_table (eif->info);
3337
0
  if (h->forced_local && h->dynindx != -1)
3338
0
    htab->has_local_dynsyms = true;
3339
3340
  /* Ignore indirect symbols.  These are added by the versioning code.  */
3341
0
  if (h->root.type == bfd_link_hash_indirect)
3342
0
    return true;
3343
3344
  /* Fix the symbol flags.  */
3345
0
  if (! _bfd_elf_fix_symbol_flags (h, eif))
3346
0
    return false;
3347
3348
0
  obed = get_elf_backend_data (htab->dynobj);
3349
3350
0
  if (h->root.type == bfd_link_hash_undefweak)
3351
0
    {
3352
0
      if (eif->info->dynamic_undefined_weak == 0)
3353
0
  obed->elf_backend_hide_symbol (eif->info, h, true);
3354
0
      else if (eif->info->dynamic_undefined_weak > 0
3355
0
         && h->ref_regular
3356
0
         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3357
0
         && !bfd_hide_sym_by_version (eif->info->version_info,
3358
0
              h->root.root.string))
3359
0
  {
3360
0
    if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3361
0
      {
3362
0
        eif->failed = true;
3363
0
        return false;
3364
0
      }
3365
0
  }
3366
0
    }
3367
3368
  /* If this symbol does not require a PLT entry, and it is not
3369
     defined by a dynamic object, or is not referenced by a regular
3370
     object, ignore it.  We do have to handle a weak defined symbol,
3371
     even if no regular object refers to it, if we decided to add it
3372
     to the dynamic symbol table.  FIXME: Do we normally need to worry
3373
     about symbols which are defined by one dynamic object and
3374
     referenced by another one?  */
3375
0
  if (!h->needs_plt
3376
0
      && h->type != STT_GNU_IFUNC
3377
0
      && (h->def_regular
3378
0
    || !h->def_dynamic
3379
0
    || (!h->ref_regular
3380
0
        && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3381
0
    {
3382
0
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
3383
0
      return true;
3384
0
    }
3385
3386
  /* If we've already adjusted this symbol, don't do it again.  This
3387
     can happen via a recursive call.  */
3388
0
  if (h->dynamic_adjusted)
3389
0
    return true;
3390
3391
  /* Don't look at this symbol again.  Note that we must set this
3392
     after checking the above conditions, because we may look at a
3393
     symbol once, decide not to do anything, and then get called
3394
     recursively later after REF_REGULAR is set below.  */
3395
0
  h->dynamic_adjusted = 1;
3396
3397
  /* If this is a weak definition, and we know a real definition, and
3398
     the real symbol is not itself defined by a regular object file,
3399
     then get a good value for the real definition.  We handle the
3400
     real symbol first, for the convenience of the backend routine.
3401
3402
     Note that there is a confusing case here.  If the real definition
3403
     is defined by a regular object file, we don't get the real symbol
3404
     from the dynamic object, but we do get the weak symbol.  If the
3405
     processor backend uses a COPY reloc, then if some routine in the
3406
     dynamic object changes the real symbol, we will not see that
3407
     change in the corresponding weak symbol.  This is the way other
3408
     ELF linkers work as well, and seems to be a result of the shared
3409
     library model.
3410
3411
     I will clarify this issue.  Most SVR4 shared libraries define the
3412
     variable _timezone and define timezone as a weak synonym.  The
3413
     tzset call changes _timezone.  If you write
3414
       extern int timezone;
3415
       int _timezone = 5;
3416
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3417
     you might expect that, since timezone is a synonym for _timezone,
3418
     the same number will print both times.  However, if the processor
3419
     backend uses a COPY reloc, then actually timezone will be copied
3420
     into your process image, and, since you define _timezone
3421
     yourself, _timezone will not.  Thus timezone and _timezone will
3422
     wind up at different memory locations.  The tzset call will set
3423
     _timezone, leaving timezone unchanged.  */
3424
3425
0
  if (h->is_weakalias)
3426
0
    {
3427
0
      struct elf_link_hash_entry *def = weakdef (h);
3428
3429
      /* If we get to this point, there is an implicit reference to
3430
   the alias by a regular object file via the weak symbol H.  */
3431
0
      def->ref_regular = 1;
3432
3433
      /* Ensure that the backend adjust_dynamic_symbol function sees
3434
   the strong alias before H by recursively calling ourselves.  */
3435
0
      if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3436
0
  return false;
3437
0
    }
3438
3439
  /* If a symbol has no type and no size and does not require a PLT
3440
     entry, then we are probably about to do the wrong thing here: we
3441
     are probably going to create a COPY reloc for an empty object.
3442
     This case can arise when a shared object is built with assembly
3443
     code, and the assembly code fails to set the symbol type.  */
3444
0
  if (h->size == 0
3445
0
      && h->type == STT_NOTYPE
3446
0
      && !h->needs_plt)
3447
0
    _bfd_error_handler
3448
0
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
3449
0
       h->root.root.string);
3450
3451
0
  if (!obed->elf_backend_adjust_dynamic_symbol (eif->info, h))
3452
0
    {
3453
0
      eif->failed = true;
3454
0
      return false;
3455
0
    }
3456
3457
0
  return true;
3458
0
}
3459
3460
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3461
   DYNBSS.  */
3462
3463
bool
3464
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3465
            struct elf_link_hash_entry *h,
3466
            asection *dynbss)
3467
0
{
3468
0
  unsigned int power_of_two;
3469
0
  bfd_vma mask;
3470
0
  asection *sec = h->root.u.def.section;
3471
3472
  /* The section alignment of the definition is the maximum alignment
3473
     requirement of symbols defined in the section.  Since we don't
3474
     know the symbol alignment requirement, we start with the
3475
     maximum alignment and check low bits of the symbol address
3476
     for the minimum alignment.  */
3477
0
  power_of_two = bfd_section_alignment (sec);
3478
0
  mask = ((bfd_vma) 1 << power_of_two) - 1;
3479
0
  while ((h->root.u.def.value & mask) != 0)
3480
0
    {
3481
0
       mask >>= 1;
3482
0
       --power_of_two;
3483
0
    }
3484
3485
  /* Adjust the section alignment if needed.  */
3486
0
  if (!bfd_link_align_section (dynbss, power_of_two))
3487
0
    return false;
3488
3489
  /* We make sure that the symbol will be aligned properly.  */
3490
0
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3491
3492
  /* Define the symbol as being at this point in DYNBSS.  */
3493
0
  h->root.u.def.section = dynbss;
3494
0
  h->root.u.def.value = dynbss->size;
3495
3496
  /* Increment the size of DYNBSS to make room for the symbol.  */
3497
0
  dynbss->size += h->size;
3498
3499
  /* No error if extern_protected_data is true.  */
3500
0
  if (h->protected_def
3501
0
      && (!info->extern_protected_data
3502
0
    || (info->extern_protected_data < 0
3503
0
        && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3504
0
    info->callbacks->einfo
3505
0
      (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3506
0
       h->root.root.string);
3507
3508
0
  return true;
3509
0
}
3510
3511
/* Adjust all external symbols pointing into SEC_MERGE sections
3512
   to reflect the object merging within the sections.  */
3513
3514
static bool
3515
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3516
0
{
3517
0
  asection *sec;
3518
3519
0
  if ((h->root.type == bfd_link_hash_defined
3520
0
       || h->root.type == bfd_link_hash_defweak)
3521
0
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3522
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3523
0
    {
3524
0
      bfd *output_bfd = data;
3525
3526
0
      h->root.u.def.value =
3527
0
  _bfd_merged_section_offset (output_bfd,
3528
0
            &h->root.u.def.section,
3529
0
            h->root.u.def.value);
3530
0
    }
3531
3532
0
  return true;
3533
0
}
3534
3535
/* Returns false if the symbol referred to by H should be considered
3536
   to resolve local to the current module, and true if it should be
3537
   considered to bind dynamically.  */
3538
3539
bool
3540
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3541
         struct bfd_link_info *info,
3542
         bool not_local_protected)
3543
0
{
3544
0
  bool binding_stays_local_p;
3545
0
  elf_backend_data *obed;
3546
0
  struct elf_link_hash_table *hash_table;
3547
3548
0
  if (h == NULL)
3549
0
    return false;
3550
3551
0
  while (h->root.type == bfd_link_hash_indirect
3552
0
   || h->root.type == bfd_link_hash_warning)
3553
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3554
3555
  /* If it was forced local, then clearly it's not dynamic.  */
3556
0
  if (h->dynindx == -1)
3557
0
    return false;
3558
0
  if (h->forced_local)
3559
0
    return false;
3560
3561
  /* Identify the cases where name binding rules say that a
3562
     visible symbol resolves locally.  */
3563
0
  binding_stays_local_p = (bfd_link_executable (info)
3564
0
         || SYMBOLIC_BIND (info, h));
3565
3566
0
  switch (ELF_ST_VISIBILITY (h->other))
3567
0
    {
3568
0
    case STV_INTERNAL:
3569
0
    case STV_HIDDEN:
3570
0
      return false;
3571
3572
0
    case STV_PROTECTED:
3573
0
      hash_table = elf_hash_table (info);
3574
0
      if (!is_elf_hash_table (&hash_table->root))
3575
0
  return false;
3576
3577
0
      obed = get_elf_backend_data (hash_table->dynobj);
3578
3579
      /* Proper resolution for function pointer equality may require
3580
   that these symbols perhaps be resolved dynamically, even though
3581
   we should be resolving them to the current module.  */
3582
0
      if (!not_local_protected || !obed->is_function_type (h->type))
3583
0
  binding_stays_local_p = true;
3584
0
      break;
3585
3586
0
    default:
3587
0
      break;
3588
0
    }
3589
3590
  /* If it isn't defined locally, then clearly it's dynamic.  */
3591
0
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3592
0
    return true;
3593
3594
  /* Otherwise, the symbol is dynamic if binding rules don't tell
3595
     us that it remains local.  */
3596
0
  return !binding_stays_local_p;
3597
0
}
3598
3599
/* Return true if the symbol referred to by H should be considered
3600
   to resolve local to the current module, and false otherwise.  Differs
3601
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3602
   undefined symbols.  The two functions are virtually identical except
3603
   for the place where dynindx == -1 is tested.  If that test is true,
3604
   _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3605
   _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3606
   defined symbols.
3607
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3608
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3609
   treatment of undefined weak symbols.  For those that do not make
3610
   undefined weak symbols dynamic, both functions may return false.  */
3611
3612
bool
3613
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3614
            struct bfd_link_info *info,
3615
            bool local_protected)
3616
0
{
3617
0
  elf_backend_data *obed;
3618
0
  struct elf_link_hash_table *hash_table;
3619
3620
  /* If it's a local sym, of course we resolve locally.  */
3621
0
  if (h == NULL)
3622
0
    return true;
3623
3624
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3625
0
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3626
0
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3627
0
    return true;
3628
3629
  /* Forced local symbols resolve locally.  */
3630
0
  if (h->forced_local)
3631
0
    return true;
3632
3633
  /* Common symbols that become definitions don't get the DEF_REGULAR
3634
     flag set, so test it first, and don't bail out.  */
3635
0
  if (ELF_COMMON_DEF_P (h))
3636
0
    /* Do nothing.  */;
3637
  /* If we don't have a definition in a regular file, then we can't
3638
     resolve locally.  The sym is either undefined or dynamic.  */
3639
0
  else if (!h->def_regular)
3640
0
    return false;
3641
3642
  /* Non-dynamic symbols resolve locally.  */
3643
0
  if (h->dynindx == -1)
3644
0
    return true;
3645
3646
  /* At this point, we know the symbol is defined and dynamic.  In an
3647
     executable it must resolve locally, likewise when building symbolic
3648
     shared libraries.  */
3649
0
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3650
0
    return true;
3651
3652
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
3653
     with default visibility might not resolve locally.  */
3654
0
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3655
0
    return false;
3656
3657
0
  hash_table = elf_hash_table (info);
3658
0
  if (!is_elf_hash_table (&hash_table->root))
3659
0
    return true;
3660
3661
  /* STV_PROTECTED symbols with indirect external access are local. */
3662
0
  if (info->indirect_extern_access > 0)
3663
0
    return true;
3664
3665
0
  obed = get_elf_backend_data (hash_table->dynobj);
3666
3667
  /* If extern_protected_data is false, STV_PROTECTED non-function
3668
     symbols are local.  */
3669
0
  if ((!info->extern_protected_data
3670
0
       || (info->extern_protected_data < 0
3671
0
     && !obed->extern_protected_data))
3672
0
      && !obed->is_function_type (h->type))
3673
0
    return true;
3674
3675
  /* Function pointer equality tests may require that STV_PROTECTED
3676
     symbols be treated as dynamic symbols.  If the address of a
3677
     function not defined in an executable is set to that function's
3678
     plt entry in the executable, then the address of the function in
3679
     a shared library must also be the plt entry in the executable.  */
3680
0
  return local_protected;
3681
0
}
3682
3683
/* Caches some TLS segment info, and ensures that the TLS segment vma is
3684
   aligned.  Returns the first TLS output section.  */
3685
3686
struct bfd_section *
3687
bfd_elf_tls_setup (struct bfd_link_info *info)
3688
0
{
3689
0
  struct bfd_section *sec, *tls;
3690
0
  unsigned int align = 0;
3691
3692
0
  for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
3693
0
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3694
0
      break;
3695
0
  tls = sec;
3696
3697
0
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3698
0
    if (sec->alignment_power > align)
3699
0
      align = sec->alignment_power;
3700
3701
0
  elf_hash_table (info)->tls_sec = tls;
3702
3703
  /* Ensure the alignment of the first section (usually .tdata) is the largest
3704
     alignment, so that the tls segment starts aligned.  */
3705
0
  if (tls != NULL)
3706
0
    (void) bfd_link_align_section (tls, align);
3707
3708
0
  return tls;
3709
0
}
3710
3711
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3712
static bool
3713
is_global_data_symbol_definition (bfd *abfd,
3714
          Elf_Internal_Sym *sym)
3715
0
{
3716
0
  elf_backend_data *bed;
3717
3718
  /* Local symbols do not count, but target specific ones might.  */
3719
0
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3720
0
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3721
0
    return false;
3722
3723
0
  bed = get_elf_backend_data (abfd);
3724
  /* Function symbols do not count.  */
3725
0
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3726
0
    return false;
3727
3728
  /* If the section is undefined, then so is the symbol.  */
3729
0
  if (sym->st_shndx == SHN_UNDEF)
3730
0
    return false;
3731
3732
  /* If the symbol is defined in the common section, then
3733
     it is a common definition and so does not count.  */
3734
0
  if (bed->common_definition (sym))
3735
0
    return false;
3736
3737
  /* If the symbol is in a target specific section then we
3738
     must rely upon the backend to tell us what it is.  */
3739
0
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3740
    /* FIXME - this function is not coded yet:
3741
3742
       return _bfd_is_global_symbol_definition (abfd, sym);
3743
3744
       Instead for now assume that the definition is not global,
3745
       Even if this is wrong, at least the linker will behave
3746
       in the same way that it used to do.  */
3747
0
    return false;
3748
3749
0
  return true;
3750
0
}
3751
3752
/* Search the symbol table of the archive element of the archive ABFD
3753
   whose archive map contains a mention of SYMDEF, and determine if
3754
   the symbol is defined in this element.  */
3755
static bool
3756
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3757
0
{
3758
0
  Elf_Internal_Shdr * hdr;
3759
0
  size_t symcount;
3760
0
  size_t extsymcount;
3761
0
  size_t extsymoff;
3762
0
  Elf_Internal_Sym *isymbuf;
3763
0
  Elf_Internal_Sym *isym;
3764
0
  Elf_Internal_Sym *isymend;
3765
0
  bool result;
3766
3767
0
  abfd = _bfd_get_elt_from_symdef (abfd, symdef, NULL);
3768
0
  if (abfd == NULL)
3769
0
    return false;
3770
3771
0
  if (! bfd_check_format (abfd, bfd_object))
3772
0
    return false;
3773
3774
  /* Select the appropriate symbol table.  If we don't know if the
3775
     object file is an IR object, give linker LTO plugin a chance to
3776
     get the correct symbol table.  */
3777
0
  if (abfd->plugin_format == bfd_plugin_yes
3778
0
      || abfd->plugin_format == bfd_plugin_yes_unused
3779
0
      || (abfd->plugin_format == bfd_plugin_unknown
3780
0
    && bfd_link_plugin_object_p (abfd)))
3781
0
    {
3782
      /* Use the IR symbol table if the object has been claimed by
3783
   plugin.  */
3784
0
      abfd = abfd->plugin_dummy_bfd;
3785
0
      hdr = &elf_symtab_hdr (abfd);
3786
0
    }
3787
0
  else
3788
0
    {
3789
0
      if (elf_use_dt_symtab_p (abfd))
3790
0
  {
3791
0
    bfd_set_error (bfd_error_wrong_format);
3792
0
    return false;
3793
0
  }
3794
3795
0
      if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3796
0
  hdr = &elf_symtab_hdr (abfd);
3797
0
      else
3798
0
  hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3799
0
    }
3800
3801
0
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3802
3803
  /* The sh_info field of the symtab header tells us where the
3804
     external symbols start.  We don't care about the local symbols.  */
3805
0
  if (elf_bad_symtab (abfd))
3806
0
    {
3807
0
      extsymcount = symcount;
3808
0
      extsymoff = 0;
3809
0
    }
3810
0
  else
3811
0
    {
3812
0
      extsymcount = symcount - hdr->sh_info;
3813
0
      extsymoff = hdr->sh_info;
3814
0
    }
3815
3816
0
  if (extsymcount == 0)
3817
0
    return false;
3818
3819
  /* Read in the symbol table.  */
3820
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3821
0
          NULL, NULL, NULL);
3822
0
  if (isymbuf == NULL)
3823
0
    return false;
3824
3825
  /* Scan the symbol table looking for SYMDEF.  */
3826
0
  result = false;
3827
0
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3828
0
    {
3829
0
      const char *name;
3830
3831
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3832
0
                isym->st_name);
3833
0
      if (name == NULL)
3834
0
  break;
3835
3836
0
      if (strcmp (name, symdef->name) == 0)
3837
0
  {
3838
0
    result = is_global_data_symbol_definition (abfd, isym);
3839
0
    break;
3840
0
  }
3841
0
    }
3842
3843
0
  free (isymbuf);
3844
3845
0
  return result;
3846
0
}
3847

3848
/* Add an entry to the .dynamic table.  */
3849
3850
bool
3851
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3852
          bfd_vma tag,
3853
          bfd_vma val)
3854
0
{
3855
0
  struct elf_link_hash_table *hash_table;
3856
0
  elf_backend_data *obed;
3857
0
  asection *s;
3858
0
  bfd_size_type newsize;
3859
0
  bfd_byte *newcontents;
3860
0
  Elf_Internal_Dyn dyn;
3861
3862
0
  hash_table = elf_hash_table (info);
3863
0
  if (! is_elf_hash_table (&hash_table->root))
3864
0
    return false;
3865
3866
0
  if (tag == DT_RELA || tag == DT_REL)
3867
0
    hash_table->dynamic_relocs = true;
3868
3869
0
  obed = get_elf_backend_data (hash_table->dynobj);
3870
0
  s = hash_table->dynamic;
3871
0
  BFD_ASSERT (s != NULL);
3872
3873
0
  newsize = s->size + obed->s->sizeof_dyn;
3874
0
  newcontents = bfd_realloc (s->contents, newsize);
3875
0
  if (newcontents == NULL)
3876
0
    return false;
3877
3878
0
  dyn.d_tag = tag;
3879
0
  dyn.d_un.d_val = val;
3880
0
  obed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3881
3882
0
  s->size = newsize;
3883
0
  s->contents = newcontents;
3884
3885
0
  return true;
3886
0
}
3887
3888
/* Strip zero-sized dynamic sections.  */
3889
3890
bool
3891
_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3892
0
{
3893
0
  struct elf_link_hash_table *hash_table;
3894
0
  elf_backend_data *obed;
3895
0
  asection *s, *sdynamic, **pp;
3896
0
  asection *rela_dyn, *rel_dyn;
3897
0
  Elf_Internal_Dyn dyn;
3898
0
  bfd_byte *extdyn, *next;
3899
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3900
0
  bool strip_zero_sized;
3901
0
  bool strip_zero_sized_plt;
3902
3903
0
  if (bfd_link_relocatable (info))
3904
0
    return true;
3905
3906
0
  hash_table = elf_hash_table (info);
3907
0
  if (!is_elf_hash_table (&hash_table->root))
3908
0
    return false;
3909
3910
0
  if (!hash_table->dynobj)
3911
0
    return true;
3912
3913
0
  sdynamic= hash_table->dynamic;
3914
0
  if (!sdynamic)
3915
0
    return true;
3916
3917
0
  obed = get_elf_backend_data (hash_table->dynobj);
3918
0
  swap_dyn_in = obed->s->swap_dyn_in;
3919
3920
0
  strip_zero_sized = false;
3921
0
  strip_zero_sized_plt = false;
3922
3923
  /* Strip zero-sized dynamic sections.  */
3924
0
  rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3925
0
  rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3926
0
  for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3927
0
    if (s->size == 0
3928
0
  && (s == rela_dyn
3929
0
      || s == rel_dyn
3930
0
      || s == hash_table->srelplt->output_section
3931
0
      || s == hash_table->splt->output_section))
3932
0
      {
3933
0
  *pp = s->next;
3934
0
  info->output_bfd->section_count--;
3935
0
  strip_zero_sized = true;
3936
0
  if (s == rela_dyn)
3937
0
    s = rela_dyn;
3938
0
  if (s == rel_dyn)
3939
0
    s = rel_dyn;
3940
0
  else if (s == hash_table->splt->output_section)
3941
0
    {
3942
0
      s = hash_table->splt;
3943
0
      strip_zero_sized_plt = true;
3944
0
    }
3945
0
  else
3946
0
    s = hash_table->srelplt;
3947
0
  s->flags |= SEC_EXCLUDE;
3948
0
  s->output_section = bfd_abs_section_ptr;
3949
0
      }
3950
0
    else
3951
0
      pp = &s->next;
3952
3953
0
  if (strip_zero_sized_plt && sdynamic->size != 0)
3954
0
    for (extdyn = sdynamic->contents;
3955
0
   extdyn < sdynamic->contents + sdynamic->size;
3956
0
   extdyn = next)
3957
0
      {
3958
0
  next = extdyn + obed->s->sizeof_dyn;
3959
0
  swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3960
0
  switch (dyn.d_tag)
3961
0
    {
3962
0
    default:
3963
0
      break;
3964
0
    case DT_JMPREL:
3965
0
    case DT_PLTRELSZ:
3966
0
    case DT_PLTREL:
3967
      /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3968
         the procedure linkage table (the .plt section) has been
3969
         removed.  */
3970
0
      memmove (extdyn, next,
3971
0
         sdynamic->size - (next - sdynamic->contents));
3972
0
      next = extdyn;
3973
0
    }
3974
0
      }
3975
3976
0
  if (strip_zero_sized)
3977
0
    {
3978
      /* Regenerate program headers.  */
3979
0
      elf_seg_map (info->output_bfd) = NULL;
3980
0
      return bfd_elf_map_sections_to_segments (info->output_bfd, info, NULL);
3981
0
    }
3982
3983
0
  return true;
3984
0
}
3985
3986
/* Add a DT_NEEDED entry for this dynamic object.  Returns -1 on error,
3987
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3988
3989
int
3990
bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3991
0
{
3992
0
  struct elf_strtab_hash *dynstr;
3993
0
  size_t strindex;
3994
0
  const char *soname;
3995
3996
0
  dynstr = _bfd_elf_link_dynstr (info);
3997
0
  if (dynstr == NULL)
3998
0
    return -1;
3999
4000
0
  soname = elf_dt_name (abfd);
4001
0
  strindex = _bfd_elf_strtab_add (dynstr, soname, false);
4002
0
  if (strindex == (size_t) -1)
4003
0
    return -1;
4004
4005
0
  if (!bfd_elf_link_create_dynamic_sections (info))
4006
0
    return -1;
4007
4008
0
  if (_bfd_elf_strtab_refcount (dynstr, strindex) != 1)
4009
0
    {
4010
0
      struct elf_link_hash_table *hash_table = elf_hash_table (info);
4011
0
      elf_backend_data *obed = get_elf_backend_data (hash_table->dynobj);
4012
0
      asection *sdyn = hash_table->dynamic;
4013
0
      bfd_byte *extdyn;
4014
4015
0
      if (sdyn != NULL && sdyn->size != 0)
4016
0
  for (extdyn = sdyn->contents;
4017
0
       extdyn < sdyn->contents + sdyn->size;
4018
0
       extdyn += obed->s->sizeof_dyn)
4019
0
    {
4020
0
      Elf_Internal_Dyn dyn;
4021
4022
0
      obed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
4023
0
      if (dyn.d_tag == DT_NEEDED
4024
0
    && dyn.d_un.d_val == strindex)
4025
0
        {
4026
0
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
4027
0
    return 1;
4028
0
        }
4029
0
    }
4030
0
    }
4031
4032
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
4033
0
    return -1;
4034
4035
0
  return 0;
4036
0
}
4037
4038
/* Return true if SONAME is on the needed list between NEEDED and STOP
4039
   (or the end of list if STOP is NULL), and needed by a library that
4040
   will be loaded.  */
4041
4042
static bool
4043
on_needed_list (const char *soname,
4044
    struct bfd_link_needed_list *needed,
4045
    struct bfd_link_needed_list *stop)
4046
0
{
4047
0
  struct bfd_link_needed_list *look;
4048
0
  for (look = needed; look != stop; look = look->next)
4049
0
    if (strcmp (soname, look->name) == 0
4050
0
  && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
4051
      /* If needed by a library that itself is not directly
4052
         needed, recursively check whether that library is
4053
         indirectly needed.  Since we add DT_NEEDED entries to
4054
         the end of the list, library dependencies appear after
4055
         the library.  Therefore search prior to the current
4056
         LOOK, preventing possible infinite recursion.  */
4057
0
      || on_needed_list (elf_dt_name (look->by), needed, look)))
4058
0
      return true;
4059
4060
0
  return false;
4061
0
}
4062
4063
/* Sort symbol by value, section, size, and type.  */
4064
static int
4065
elf_sort_symbol (const void *arg1, const void *arg2)
4066
0
{
4067
0
  const struct elf_link_hash_entry *h1;
4068
0
  const struct elf_link_hash_entry *h2;
4069
0
  bfd_signed_vma vdiff;
4070
0
  int sdiff;
4071
0
  const char *n1;
4072
0
  const char *n2;
4073
4074
0
  h1 = *(const struct elf_link_hash_entry **) arg1;
4075
0
  h2 = *(const struct elf_link_hash_entry **) arg2;
4076
0
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
4077
0
  if (vdiff != 0)
4078
0
    return vdiff > 0 ? 1 : -1;
4079
4080
0
  sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
4081
0
  if (sdiff != 0)
4082
0
    return sdiff;
4083
4084
  /* Sort so that sized symbols are selected over zero size symbols.  */
4085
0
  vdiff = h1->size - h2->size;
4086
0
  if (vdiff != 0)
4087
0
    return vdiff > 0 ? 1 : -1;
4088
4089
  /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
4090
0
  if (h1->type != h2->type)
4091
0
    return h1->type - h2->type;
4092
4093
  /* If symbols are properly sized and typed, and multiple strong
4094
     aliases are not defined in a shared library by the user we
4095
     shouldn't get here.  Unfortunately linker script symbols like
4096
     __bss_start sometimes match a user symbol defined at the start of
4097
     .bss without proper size and type.  We'd like to preference the
4098
     user symbol over reserved system symbols.  Sort on leading
4099
     underscores.  */
4100
0
  n1 = h1->root.root.string;
4101
0
  n2 = h2->root.root.string;
4102
0
  while (*n1 == *n2)
4103
0
    {
4104
0
      if (*n1 == 0)
4105
0
  break;
4106
0
      ++n1;
4107
0
      ++n2;
4108
0
    }
4109
0
  if (*n1 == '_')
4110
0
    return -1;
4111
0
  if (*n2 == '_')
4112
0
    return 1;
4113
4114
  /* Final sort on name selects user symbols like '_u' over reserved
4115
     system symbols like '_Z' and also will avoid qsort instability.  */
4116
0
  return *n1 - *n2;
4117
0
}
4118
4119
/* This function is used to adjust offsets into .dynstr for
4120
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
4121
4122
static bool
4123
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4124
0
{
4125
0
  struct elf_strtab_hash *dynstr = data;
4126
4127
0
  if (h->dynindx != -1)
4128
0
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4129
0
  return true;
4130
0
}
4131
4132
/* Assign string offsets in .dynstr, update all structures referencing
4133
   them.  */
4134
4135
static bool
4136
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4137
0
{
4138
0
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
4139
0
  struct elf_link_local_dynamic_entry *entry;
4140
0
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
4141
0
  bfd *dynobj = hash_table->dynobj;
4142
0
  asection *sdyn;
4143
0
  bfd_size_type size;
4144
0
  elf_backend_data *obed;
4145
0
  bfd_byte *extdyn;
4146
4147
0
  _bfd_elf_strtab_finalize (dynstr);
4148
0
  size = _bfd_elf_strtab_size (dynstr);
4149
4150
  /* Allow the linker to examine the dynsymtab now it's fully populated.  */
4151
4152
0
  if (info->callbacks->examine_strtab)
4153
0
    info->callbacks->examine_strtab (dynstr);
4154
4155
0
  obed = get_elf_backend_data (dynobj);
4156
0
  sdyn = hash_table->dynamic;
4157
0
  BFD_ASSERT (sdyn != NULL);
4158
4159
  /* Update all .dynamic entries referencing .dynstr strings.  */
4160
0
  for (extdyn = sdyn->contents;
4161
0
       extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4162
0
       extdyn += obed->s->sizeof_dyn)
4163
0
    {
4164
0
      Elf_Internal_Dyn dyn;
4165
4166
0
      obed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4167
0
      switch (dyn.d_tag)
4168
0
  {
4169
0
  case DT_STRSZ:
4170
0
    dyn.d_un.d_val = size;
4171
0
    break;
4172
0
  case DT_NEEDED:
4173
0
  case DT_SONAME:
4174
0
  case DT_RPATH:
4175
0
  case DT_RUNPATH:
4176
0
  case DT_FILTER:
4177
0
  case DT_AUXILIARY:
4178
0
  case DT_AUDIT:
4179
0
  case DT_DEPAUDIT:
4180
0
    dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4181
0
    break;
4182
0
  default:
4183
0
    continue;
4184
0
  }
4185
0
      obed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4186
0
    }
4187
4188
  /* Now update local dynamic symbols.  */
4189
0
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4190
0
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4191
0
              entry->isym.st_name);
4192
4193
  /* And the rest of dynamic symbols.  */
4194
0
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4195
4196
  /* Adjust version definitions.  */
4197
0
  if (elf_tdata (output_bfd)->cverdefs)
4198
0
    {
4199
0
      asection *s;
4200
0
      bfd_byte *p;
4201
0
      size_t i;
4202
0
      Elf_Internal_Verdef def;
4203
0
      Elf_Internal_Verdaux defaux;
4204
4205
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4206
0
      p = s->contents;
4207
0
      do
4208
0
  {
4209
0
    _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4210
0
           &def);
4211
0
    p += sizeof (Elf_External_Verdef);
4212
0
    if (def.vd_aux != sizeof (Elf_External_Verdef))
4213
0
      continue;
4214
0
    for (i = 0; i < def.vd_cnt; ++i)
4215
0
      {
4216
0
        _bfd_elf_swap_verdaux_in (output_bfd,
4217
0
          (Elf_External_Verdaux *) p, &defaux);
4218
0
        defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4219
0
              defaux.vda_name);
4220
0
        _bfd_elf_swap_verdaux_out (output_bfd,
4221
0
           &defaux, (Elf_External_Verdaux *) p);
4222
0
        p += sizeof (Elf_External_Verdaux);
4223
0
      }
4224
0
  }
4225
0
      while (def.vd_next);
4226
0
    }
4227
4228
  /* Adjust version references.  */
4229
0
  if (elf_tdata (output_bfd)->verref)
4230
0
    {
4231
0
      asection *s;
4232
0
      bfd_byte *p;
4233
0
      size_t i;
4234
0
      Elf_Internal_Verneed need;
4235
0
      Elf_Internal_Vernaux needaux;
4236
4237
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4238
0
      p = s->contents;
4239
0
      do
4240
0
  {
4241
0
    _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4242
0
            &need);
4243
0
    need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4244
0
    _bfd_elf_swap_verneed_out (output_bfd, &need,
4245
0
             (Elf_External_Verneed *) p);
4246
0
    p += sizeof (Elf_External_Verneed);
4247
0
    for (i = 0; i < need.vn_cnt; ++i)
4248
0
      {
4249
0
        _bfd_elf_swap_vernaux_in (output_bfd,
4250
0
          (Elf_External_Vernaux *) p, &needaux);
4251
0
        needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4252
0
               needaux.vna_name);
4253
0
        _bfd_elf_swap_vernaux_out (output_bfd,
4254
0
           &needaux,
4255
0
           (Elf_External_Vernaux *) p);
4256
0
        p += sizeof (Elf_External_Vernaux);
4257
0
      }
4258
0
  }
4259
0
      while (need.vn_next);
4260
0
    }
4261
4262
0
  return true;
4263
0
}
4264

4265
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4266
   The default is to only match when the INPUT and OUTPUT are exactly
4267
   the same target.  */
4268
4269
bool
4270
_bfd_elf_default_relocs_compatible (const bfd_target *input,
4271
            const bfd_target *output)
4272
0
{
4273
0
  return input == output;
4274
0
}
4275
4276
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4277
   This version is used when different targets for the same architecture
4278
   are virtually identical.  */
4279
4280
bool
4281
_bfd_elf_relocs_compatible (const bfd_target *input,
4282
          const bfd_target *output)
4283
0
{
4284
0
  elf_backend_data *obed, *ibed;
4285
4286
0
  if (input == output)
4287
0
    return true;
4288
4289
0
  ibed = xvec_get_elf_backend_data (input);
4290
0
  obed = xvec_get_elf_backend_data (output);
4291
4292
0
  if (ibed->arch != obed->arch)
4293
0
    return false;
4294
4295
  /* If both backends are using this function, deem them compatible.  */
4296
0
  return ibed->relocs_compatible == obed->relocs_compatible;
4297
0
}
4298
4299
/* Make a special call to the linker "notice" function to tell it that
4300
   we are about to handle an as-needed lib, or have finished
4301
   processing the lib.  */
4302
4303
bool
4304
_bfd_elf_notice_as_needed (bfd *ibfd,
4305
         struct bfd_link_info *info,
4306
         enum notice_asneeded_action act)
4307
0
{
4308
0
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4309
0
}
4310
4311
/* Call ACTION on each relocation in an ELF object file.  */
4312
4313
bool
4314
_bfd_elf_link_iterate_on_relocs
4315
  (bfd *abfd, struct bfd_link_info *info,
4316
   bool (*action) (bfd *, struct bfd_link_info *, asection *,
4317
       const Elf_Internal_Rela *))
4318
0
{
4319
  /* If this object is the same format as the output object, and it is
4320
     not a shared library, then let the backend look through the
4321
     relocs.
4322
4323
     This is required to build global offset table entries and to
4324
     arrange for dynamic relocs.  It is not required for the
4325
     particular common case of linking non PIC code, even when linking
4326
     against shared libraries, but unfortunately there is no way of
4327
     knowing whether an object file has been compiled PIC or not.
4328
     Looking through the relocs is not particularly time consuming.
4329
     The problem is that we must either (1) keep the relocs in memory,
4330
     which causes the linker to require additional runtime memory or
4331
     (2) read the relocs twice from the input file, which wastes time.
4332
     This would be a good case for using mmap.
4333
4334
     I have no idea how to handle linking PIC code into a file of a
4335
     different format.  It probably can't be done.  */
4336
0
  if ((abfd->flags & DYNAMIC) == 0
4337
0
      && compatible_format (info, abfd))
4338
0
    {
4339
0
      for (asection *o = abfd->sections; o != NULL; o = o->next)
4340
0
  {
4341
0
    Elf_Internal_Rela *internal_relocs;
4342
0
    bool ok;
4343
4344
    /* Don't check relocations in excluded sections.  Don't do
4345
       anything special with non-loaded, non-alloced sections.
4346
       In particular, any relocs in such sections should not
4347
       affect GOT and PLT reference counting (ie.  we don't
4348
       allow them to create GOT or PLT entries), there's no
4349
       possibility or desire to optimize TLS relocs, and
4350
       there's not much point in propagating relocs to shared
4351
       libs that the dynamic linker won't relocate.  */
4352
0
    if ((o->flags & SEC_ALLOC) == 0
4353
0
        || (o->flags & SEC_RELOC) == 0
4354
0
        || (o->flags & SEC_EXCLUDE) != 0
4355
0
        || o->reloc_count == 0
4356
0
        || ((info->strip == strip_all || info->strip == strip_debugger)
4357
0
      && (o->flags & SEC_DEBUGGING) != 0)
4358
0
        || bfd_is_abs_section (o->output_section))
4359
0
      continue;
4360
4361
0
    internal_relocs
4362
0
      = _bfd_elf_link_info_read_relocs (abfd, info, o, NULL, NULL,
4363
0
                _bfd_elf_link_keep_memory (info));
4364
0
    if (internal_relocs == NULL)
4365
0
      return false;
4366
4367
0
    ok = action (abfd, info, o, internal_relocs);
4368
4369
0
    if (elf_section_data (o)->relocs != internal_relocs)
4370
0
      free (internal_relocs);
4371
4372
0
    if (! ok)
4373
0
      return false;
4374
0
  }
4375
0
    }
4376
4377
0
  return true;
4378
0
}
4379
4380
/* Check relocations in an ELF object file.  This is called after
4381
   all input files have been opened.  */
4382
4383
bool
4384
_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4385
0
{
4386
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4387
0
  if (bed->check_relocs != NULL)
4388
0
    return _bfd_elf_link_iterate_on_relocs (abfd, info,
4389
0
              bed->check_relocs);
4390
0
  return true;
4391
0
}
4392
4393
/* An entry in the first definition hash table.  */
4394
4395
struct elf_link_first_hash_entry
4396
{
4397
  struct bfd_hash_entry root;
4398
  /* The object of the first definition.  */
4399
  bfd *abfd;
4400
};
4401
4402
/* The function to create a new entry in the first definition hash
4403
   table.  */
4404
4405
static struct bfd_hash_entry *
4406
elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4407
           struct bfd_hash_table *table,
4408
           const char *string)
4409
0
{
4410
0
  struct elf_link_first_hash_entry *ret =
4411
0
    (struct elf_link_first_hash_entry *) entry;
4412
4413
  /* Allocate the structure if it has not already been allocated by a
4414
     subclass.  */
4415
0
  if (ret == NULL)
4416
0
    ret = bfd_hash_allocate (table, sizeof (struct elf_link_first_hash_entry));
4417
0
  if (ret == NULL)
4418
0
    return NULL;
4419
4420
  /* Call the allocation method of the superclass.  */
4421
0
  ret = ((struct elf_link_first_hash_entry *)
4422
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4423
0
         string));
4424
0
  if (ret != NULL)
4425
0
    ret->abfd = NULL;
4426
4427
0
  return (struct bfd_hash_entry *) ret;
4428
0
}
4429
4430
/* Add the symbol NAME from ABFD to first hash.  */
4431
4432
static void
4433
elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4434
          const char *name, bool copy)
4435
0
{
4436
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
4437
  /* Skip if there is no first hash.  */
4438
0
  if (htab->first_hash == NULL)
4439
0
    return;
4440
4441
0
  struct elf_link_first_hash_entry *e
4442
0
    = ((struct elf_link_first_hash_entry *)
4443
0
       bfd_hash_lookup (htab->first_hash, name, true, copy));
4444
0
  if (e == NULL)
4445
0
    info->callbacks->fatal
4446
0
      (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4447
4448
0
  if (e->abfd == NULL)
4449
    /* Store ABFD in abfd.  */
4450
0
    e->abfd = abfd;
4451
0
}
4452
4453
/* Add symbols from an ELF object file to the linker hash table.  */
4454
4455
static bool
4456
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4457
0
{
4458
0
  Elf_Internal_Ehdr *ehdr;
4459
0
  Elf_Internal_Shdr *hdr;
4460
0
  size_t symcount;
4461
0
  size_t extsymcount;
4462
0
  size_t extsymoff;
4463
0
  struct elf_link_hash_entry **sym_hash;
4464
0
  bool dynamic;
4465
0
  Elf_External_Versym *extversym = NULL;
4466
0
  Elf_External_Versym *extversym_end = NULL;
4467
0
  Elf_External_Versym *ever;
4468
0
  struct elf_link_hash_entry *weaks;
4469
0
  struct elf_link_hash_entry **nondeflt_vers = NULL;
4470
0
  size_t nondeflt_vers_cnt = 0;
4471
0
  Elf_Internal_Sym *isymbuf = NULL;
4472
0
  Elf_Internal_Sym *isym;
4473
0
  Elf_Internal_Sym *isymend;
4474
0
  elf_backend_data *bed, *obed;
4475
0
  bool add_needed;
4476
0
  struct elf_link_hash_table *htab;
4477
0
  void *alloc_mark = NULL;
4478
0
  struct bfd_hash_entry **old_table = NULL;
4479
0
  unsigned int old_size = 0;
4480
0
  unsigned int old_count = 0;
4481
0
  void *old_tab = NULL;
4482
0
  void *old_ent;
4483
0
  struct bfd_link_hash_entry *old_undefs = NULL;
4484
0
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
4485
0
  void *old_strtab = NULL;
4486
0
  size_t tabsize = 0;
4487
0
  asection *s;
4488
0
  bool just_syms;
4489
4490
0
  htab = elf_hash_table (info);
4491
0
  bed = get_elf_backend_data (abfd);
4492
0
  obed = get_elf_backend_data (info->output_bfd);
4493
4494
0
  if (elf_use_dt_symtab_p (abfd))
4495
0
    {
4496
0
      bfd_set_error (bfd_error_wrong_format);
4497
0
      return false;
4498
0
    }
4499
4500
0
  if ((abfd->flags & DYNAMIC) == 0)
4501
0
    {
4502
0
      dynamic = false;
4503
0
      if ((abfd->flags & BFD_PLUGIN) != 0
4504
0
    && is_elf_hash_table (&htab->root)
4505
0
    && htab->first_hash == NULL)
4506
0
  {
4507
    /* Initialize first_hash for an IR input.  */
4508
0
    htab->first_hash = bfd_malloc (sizeof (struct bfd_hash_table));
4509
0
    if (htab->first_hash == NULL
4510
0
        || !bfd_hash_table_init
4511
0
       (htab->first_hash, elf_link_first_hash_newfunc,
4512
0
        sizeof (struct elf_link_first_hash_entry)))
4513
0
      info->callbacks->fatal
4514
0
        (_("%P: first_hash failed to create: %E\n"));
4515
0
  }
4516
0
    }
4517
0
  else
4518
0
    {
4519
0
      dynamic = true;
4520
4521
      /* You can't use -r against a dynamic object.  Also, there's no
4522
   hope of using a dynamic object which does not exactly match
4523
   the format of the output file.  */
4524
0
      if (bfd_link_relocatable (info)
4525
0
    || !is_elf_hash_table (&htab->root)
4526
0
    || info->output_bfd->xvec != abfd->xvec)
4527
0
  {
4528
0
    if (bfd_link_relocatable (info))
4529
0
      bfd_set_error (bfd_error_invalid_operation);
4530
0
    else
4531
0
      bfd_set_error (bfd_error_wrong_format);
4532
0
    goto error_return;
4533
0
  }
4534
0
    }
4535
4536
0
  ehdr = elf_elfheader (abfd);
4537
0
  if (info->warn_alternate_em
4538
0
      && bed->elf_machine_code != ehdr->e_machine
4539
0
      && ((bed->elf_machine_alt1 != 0
4540
0
     && ehdr->e_machine == bed->elf_machine_alt1)
4541
0
    || (bed->elf_machine_alt2 != 0
4542
0
        && ehdr->e_machine == bed->elf_machine_alt2)))
4543
0
    _bfd_error_handler
4544
      /* xgettext:c-format */
4545
0
      (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4546
0
       ehdr->e_machine, abfd, bed->elf_machine_code);
4547
4548
  /* As a GNU extension, any input sections which are named
4549
     .gnu.warning.SYMBOL are treated as warning symbols for the given
4550
     symbol.  This differs from .gnu.warning sections, which generate
4551
     warnings when they are included in an output file.  */
4552
  /* PR 12761: Also generate this warning when building shared libraries.  */
4553
0
  for (s = abfd->sections; s != NULL; s = s->next)
4554
0
    {
4555
0
      const char *name;
4556
4557
0
      name = bfd_section_name (s);
4558
0
      if (startswith (name, ".gnu.warning."))
4559
0
  {
4560
0
    char *msg;
4561
0
    bfd_size_type sz;
4562
4563
0
    name += sizeof ".gnu.warning." - 1;
4564
4565
    /* If this is a shared object, then look up the symbol
4566
       in the hash table.  If it is there, and it is already
4567
       been defined, then we will not be using the entry
4568
       from this shared object, so we don't need to warn.
4569
       FIXME: If we see the definition in a regular object
4570
       later on, we will warn, but we shouldn't.  The only
4571
       fix is to keep track of what warnings we are supposed
4572
       to emit, and then handle them all at the end of the
4573
       link.  */
4574
0
    if (dynamic)
4575
0
      {
4576
0
        struct elf_link_hash_entry *h;
4577
4578
0
        h = elf_link_hash_lookup (htab, name, false, false, true);
4579
4580
        /* FIXME: What about bfd_link_hash_common?  */
4581
0
        if (h != NULL
4582
0
      && (h->root.type == bfd_link_hash_defined
4583
0
          || h->root.type == bfd_link_hash_defweak))
4584
0
    continue;
4585
0
      }
4586
4587
0
    sz = s->size;
4588
0
    msg = bfd_alloc (abfd, sz + 1);
4589
0
    if (msg == NULL)
4590
0
      goto error_return;
4591
4592
0
    if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4593
0
      goto error_return;
4594
4595
0
    msg[sz] = '\0';
4596
4597
0
    if (! (_bfd_generic_link_add_one_symbol
4598
0
     (info, abfd, name, BSF_WARNING, s, 0, msg,
4599
0
      false, bed->collect, NULL)))
4600
0
      goto error_return;
4601
4602
0
    if (bfd_link_executable (info))
4603
0
      {
4604
        /* Clobber the section size so that the warning does
4605
     not get copied into the output file.  */
4606
0
        s->size = 0;
4607
4608
        /* Also set SEC_EXCLUDE, so that symbols defined in
4609
     the warning section don't get copied to the output.  */
4610
0
        s->flags |= SEC_EXCLUDE;
4611
0
      }
4612
0
  }
4613
0
    }
4614
4615
0
  just_syms = ((s = abfd->sections) != NULL
4616
0
         && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4617
4618
0
  add_needed = true;
4619
0
  if (! dynamic)
4620
0
    {
4621
      /* If we are creating a shared library, create all the dynamic
4622
   sections immediately.  We need to attach them to something,
4623
   so we attach them to this BFD, provided it is the right
4624
   format and is not from ld --just-symbols.  Always create the
4625
   dynamic sections for -E/--dynamic-list.  FIXME: If there
4626
   are no input BFD's of the same format as the output, we can't
4627
   make a shared library.  */
4628
0
      if (!just_syms
4629
0
    && (bfd_link_pic (info)
4630
0
        || (!bfd_link_relocatable (info)
4631
0
      && info->nointerp
4632
0
      && (info->export_dynamic || info->dynamic)))
4633
0
    && compatible_format (info, abfd))
4634
0
  {
4635
0
    if (!bfd_elf_link_create_dynamic_sections (info))
4636
0
      goto error_return;
4637
0
  }
4638
0
    }
4639
0
  else if (!is_elf_hash_table (&htab->root))
4640
0
    goto error_return;
4641
0
  else
4642
0
    {
4643
0
      const char *soname = NULL;
4644
0
      char *audit = NULL;
4645
0
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4646
0
      const Elf_Internal_Phdr *phdr;
4647
0
      struct elf_link_loaded_list *loaded_lib;
4648
4649
      /* ld --just-symbols and dynamic objects don't mix very well.
4650
   ld shouldn't allow it.  */
4651
0
      if (just_syms)
4652
0
  abort ();
4653
4654
      /* If this dynamic lib was specified on the command line with
4655
   --as-needed in effect, then we don't want to add a DT_NEEDED
4656
   tag unless the lib is actually used.  Similary for libs brought
4657
   in by another lib's DT_NEEDED.  When --no-add-needed is used
4658
   on a dynamic lib, we don't want to add a DT_NEEDED entry for
4659
   any dynamic library in DT_NEEDED tags in the dynamic lib at
4660
   all.  */
4661
0
      add_needed = (elf_dyn_lib_class (abfd)
4662
0
        & (DYN_AS_NEEDED | DYN_DT_NEEDED
4663
0
           | DYN_NO_NEEDED)) == 0;
4664
4665
0
      s = bfd_get_section_by_name (abfd, ".dynamic");
4666
0
      if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4667
0
  {
4668
0
    bfd_byte *dynbuf;
4669
0
    bfd_byte *extdyn;
4670
0
    unsigned int elfsec;
4671
0
    unsigned long shlink;
4672
4673
0
    if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4674
0
      {
4675
0
      error_free_dyn:
4676
0
        _bfd_elf_munmap_section_contents (s, dynbuf);
4677
0
        goto error_return;
4678
0
      }
4679
4680
0
    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4681
0
    if (elfsec == SHN_BAD)
4682
0
      goto error_free_dyn;
4683
0
    shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4684
4685
0
    for (extdyn = dynbuf;
4686
0
         (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4687
0
         extdyn += bed->s->sizeof_dyn)
4688
0
      {
4689
0
        Elf_Internal_Dyn dyn;
4690
4691
0
        bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4692
0
        if (dyn.d_tag == DT_SONAME)
4693
0
    {
4694
0
      unsigned int tagv = dyn.d_un.d_val;
4695
0
      soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4696
0
      if (soname == NULL)
4697
0
        goto error_free_dyn;
4698
0
    }
4699
0
        if (dyn.d_tag == DT_NEEDED)
4700
0
    {
4701
0
      struct bfd_link_needed_list *n, **pn;
4702
0
      char *fnm, *anm;
4703
0
      unsigned int tagv = dyn.d_un.d_val;
4704
0
      size_t amt;
4705
4706
0
      n = bfd_alloc (abfd, sizeof (*n));
4707
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4708
0
      if (n == NULL || fnm == NULL)
4709
0
        goto error_free_dyn;
4710
0
      amt = strlen (fnm) + 1;
4711
0
      anm = bfd_alloc (abfd, amt);
4712
0
      if (anm == NULL)
4713
0
        goto error_free_dyn;
4714
0
      memcpy (anm, fnm, amt);
4715
0
      n->name = anm;
4716
0
      n->by = abfd;
4717
0
      n->next = NULL;
4718
0
      for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4719
0
        ;
4720
0
      *pn = n;
4721
0
    }
4722
0
        if (dyn.d_tag == DT_RUNPATH)
4723
0
    {
4724
0
      struct bfd_link_needed_list *n, **pn;
4725
0
      char *fnm, *anm;
4726
0
      unsigned int tagv = dyn.d_un.d_val;
4727
0
      size_t amt;
4728
4729
0
      n = bfd_alloc (abfd, sizeof (*n));
4730
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4731
0
      if (n == NULL || fnm == NULL)
4732
0
        goto error_free_dyn;
4733
0
      amt = strlen (fnm) + 1;
4734
0
      anm = bfd_alloc (abfd, amt);
4735
0
      if (anm == NULL)
4736
0
        goto error_free_dyn;
4737
0
      memcpy (anm, fnm, amt);
4738
0
      n->name = anm;
4739
0
      n->by = abfd;
4740
0
      n->next = NULL;
4741
0
      for (pn = & runpath;
4742
0
           *pn != NULL;
4743
0
           pn = &(*pn)->next)
4744
0
        ;
4745
0
      *pn = n;
4746
0
    }
4747
        /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4748
0
        if (!runpath && dyn.d_tag == DT_RPATH)
4749
0
    {
4750
0
      struct bfd_link_needed_list *n, **pn;
4751
0
      char *fnm, *anm;
4752
0
      unsigned int tagv = dyn.d_un.d_val;
4753
0
      size_t amt;
4754
4755
0
      n = bfd_alloc (abfd, sizeof (*n));
4756
0
      fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4757
0
      if (n == NULL || fnm == NULL)
4758
0
        goto error_free_dyn;
4759
0
      amt = strlen (fnm) + 1;
4760
0
      anm = bfd_alloc (abfd, amt);
4761
0
      if (anm == NULL)
4762
0
        goto error_free_dyn;
4763
0
      memcpy (anm, fnm, amt);
4764
0
      n->name = anm;
4765
0
      n->by = abfd;
4766
0
      n->next = NULL;
4767
0
      for (pn = & rpath;
4768
0
           *pn != NULL;
4769
0
           pn = &(*pn)->next)
4770
0
        ;
4771
0
      *pn = n;
4772
0
    }
4773
0
        if (dyn.d_tag == DT_AUDIT)
4774
0
    {
4775
0
      unsigned int tagv = dyn.d_un.d_val;
4776
0
      audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4777
0
    }
4778
0
        if (dyn.d_tag == DT_FLAGS_1)
4779
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4780
0
      }
4781
4782
0
    _bfd_elf_munmap_section_contents (s, dynbuf);
4783
0
  }
4784
4785
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4786
   frees all more recently bfd_alloc'd blocks as well.  */
4787
0
      if (runpath)
4788
0
  rpath = runpath;
4789
4790
0
      if (rpath)
4791
0
  {
4792
0
    struct bfd_link_needed_list **pn;
4793
0
    for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4794
0
      ;
4795
0
    *pn = rpath;
4796
0
  }
4797
4798
      /* If we have a PT_GNU_RELRO program header, mark as read-only
4799
   all sections contained fully therein.  This makes relro
4800
   shared library sections appear as they will at run-time.  */
4801
0
      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4802
0
      while (phdr-- > elf_tdata (abfd)->phdr)
4803
0
  if (phdr->p_type == PT_GNU_RELRO)
4804
0
    {
4805
0
      for (s = abfd->sections; s != NULL; s = s->next)
4806
0
        {
4807
0
    unsigned int opb = bfd_octets_per_byte (abfd, s);
4808
4809
0
    if ((s->flags & SEC_ALLOC) != 0
4810
0
        && s->vma * opb >= phdr->p_vaddr
4811
0
        && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4812
0
      s->flags |= SEC_READONLY;
4813
0
        }
4814
0
      break;
4815
0
    }
4816
4817
      /* We do not want to include any of the sections in a dynamic
4818
   object in the output file.  We hack by simply clobbering the
4819
   list of sections in the BFD.  This could be handled more
4820
   cleanly by, say, a new section flag; the existing
4821
   SEC_NEVER_LOAD flag is not the one we want, because that one
4822
   still implies that the section takes up space in the output
4823
   file.  */
4824
0
      bfd_section_list_clear (abfd);
4825
4826
      /* Find the name to use in a DT_NEEDED entry that refers to this
4827
   object.  If the object has a DT_SONAME entry, we use it.
4828
   Otherwise, if the generic linker stuck something in
4829
   elf_dt_name, we use that.  Otherwise, we just use the file
4830
   name.  */
4831
0
      if (soname == NULL || *soname == '\0')
4832
0
  {
4833
0
    soname = elf_dt_name (abfd);
4834
0
    if (soname == NULL || *soname == '\0')
4835
0
      soname = bfd_get_filename (abfd);
4836
0
  }
4837
4838
      /* Save the SONAME because sometimes the linker emulation code
4839
   will need to know it.  */
4840
0
      elf_dt_name (abfd) = soname;
4841
4842
      /* If we have already included this dynamic object in the
4843
   link, just ignore it.  There is no reason to include a
4844
   particular dynamic object more than once.  */
4845
0
      for (loaded_lib = htab->dyn_loaded;
4846
0
     loaded_lib != NULL;
4847
0
     loaded_lib = loaded_lib->next)
4848
0
  {
4849
0
    if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4850
0
      return true;
4851
0
  }
4852
4853
      /* Create dynamic sections for backends that require that be done
4854
   before setup_gnu_properties.  */
4855
0
      if (add_needed
4856
0
    && !bfd_elf_link_create_dynamic_sections (info))
4857
0
  return false;
4858
4859
      /* Save the DT_AUDIT entry for the linker emulation code. */
4860
0
      elf_dt_audit (abfd) = audit;
4861
0
    }
4862
4863
  /* If this is a dynamic object, we always link against the .dynsym
4864
     symbol table, not the .symtab symbol table.  The dynamic linker
4865
     will only see the .dynsym symbol table, so there is no reason to
4866
     look at .symtab for a dynamic object.  */
4867
4868
0
  if (! dynamic || elf_dynsymtab (abfd) == 0)
4869
0
    hdr = &elf_symtab_hdr (abfd);
4870
0
  else
4871
0
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4872
4873
0
  symcount = hdr->sh_size / bed->s->sizeof_sym;
4874
4875
  /* The sh_info field of the symtab header tells us where the
4876
     external symbols start.  We don't care about the local symbols at
4877
     this point.  */
4878
0
  if (elf_bad_symtab (abfd))
4879
0
    {
4880
0
      extsymcount = symcount;
4881
0
      extsymoff = 0;
4882
0
    }
4883
0
  else
4884
0
    {
4885
0
      extsymcount = symcount - hdr->sh_info;
4886
0
      extsymoff = hdr->sh_info;
4887
0
    }
4888
4889
0
  sym_hash = elf_sym_hashes (abfd);
4890
0
  if (extsymcount != 0)
4891
0
    {
4892
0
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4893
0
              NULL, NULL, NULL);
4894
0
      if (isymbuf == NULL)
4895
0
  goto error_return;
4896
4897
0
      if (sym_hash == NULL)
4898
0
  {
4899
    /* We store a pointer to the hash table entry for each
4900
       external symbol.  */
4901
0
    sym_hash = bfd_zalloc (abfd, extsymcount * sizeof (*sym_hash));
4902
0
    if (sym_hash == NULL)
4903
0
      goto error_free_sym;
4904
0
    elf_sym_hashes (abfd) = sym_hash;
4905
0
  }
4906
0
    }
4907
4908
0
  if (dynamic)
4909
0
    {
4910
      /* Read in any version definitions.  */
4911
0
      if (!_bfd_elf_slurp_version_tables (abfd,
4912
0
            info->default_imported_symver))
4913
0
  goto error_free_sym;
4914
4915
      /* Read in the symbol versions, but don't bother to convert them
4916
   to internal format.  */
4917
0
      if (elf_dynversym (abfd) != 0)
4918
0
  {
4919
0
    Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4920
0
    bfd_size_type amt = versymhdr->sh_size;
4921
4922
0
    if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4923
0
      goto error_free_sym;
4924
0
    extversym = _bfd_malloc_and_read (abfd, amt, amt);
4925
0
    if (extversym == NULL)
4926
0
      goto error_free_sym;
4927
0
    extversym_end = extversym + amt / sizeof (*extversym);
4928
0
  }
4929
0
    }
4930
4931
  /* If we are loading an as-needed shared lib, save the symbol table
4932
     state before we start adding symbols.  If the lib turns out
4933
     to be unneeded, restore the state.  */
4934
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4935
0
    {
4936
0
      unsigned int i;
4937
0
      size_t entsize;
4938
4939
0
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4940
0
  {
4941
0
    struct bfd_hash_entry *p;
4942
0
    struct elf_link_hash_entry *h;
4943
4944
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4945
0
      {
4946
0
        h = (struct elf_link_hash_entry *) p;
4947
0
        entsize += htab->root.table.entsize;
4948
0
        if (h->root.type == bfd_link_hash_warning)
4949
0
    {
4950
0
      entsize += htab->root.table.entsize;
4951
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4952
0
    }
4953
0
        if (h->root.type == bfd_link_hash_common)
4954
0
    entsize += sizeof (*h->root.u.c.p);
4955
0
      }
4956
0
  }
4957
4958
0
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4959
0
      old_tab = bfd_malloc (tabsize + entsize);
4960
0
      if (old_tab == NULL)
4961
0
  goto error_free_vers;
4962
4963
      /* Remember the current objalloc pointer, so that all mem for
4964
   symbols added can later be reclaimed.  */
4965
0
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4966
0
      if (alloc_mark == NULL)
4967
0
  goto error_free_vers;
4968
4969
      /* Make a special call to the linker "notice" function to
4970
   tell it that we are about to handle an as-needed lib.  */
4971
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4972
0
  goto error_free_vers;
4973
4974
      /* Clone the symbol table.  Remember some pointers into the
4975
   symbol table, and dynamic symbol count.  */
4976
0
      old_ent = (char *) old_tab + tabsize;
4977
0
      memcpy (old_tab, htab->root.table.table, tabsize);
4978
0
      old_undefs = htab->root.undefs;
4979
0
      old_undefs_tail = htab->root.undefs_tail;
4980
0
      old_table = htab->root.table.table;
4981
0
      old_size = htab->root.table.size;
4982
0
      old_count = htab->root.table.count;
4983
0
      old_strtab = NULL;
4984
0
      if (htab->dynstr != NULL)
4985
0
  {
4986
0
    old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4987
0
    if (old_strtab == NULL)
4988
0
      goto error_free_vers;
4989
0
  }
4990
4991
0
      for (i = 0; i < htab->root.table.size; i++)
4992
0
  {
4993
0
    struct bfd_hash_entry *p;
4994
0
    struct elf_link_hash_entry *h;
4995
4996
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4997
0
      {
4998
0
        h = (struct elf_link_hash_entry *) p;
4999
0
        memcpy (old_ent, h, htab->root.table.entsize);
5000
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5001
0
        if (h->root.type == bfd_link_hash_warning)
5002
0
    {
5003
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5004
0
      memcpy (old_ent, h, htab->root.table.entsize);
5005
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5006
0
    }
5007
0
        if (h->root.type == bfd_link_hash_common)
5008
0
    {
5009
0
      memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
5010
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5011
0
    }
5012
0
      }
5013
0
  }
5014
0
    }
5015
5016
0
  weaks = NULL;
5017
0
  if (extversym == NULL)
5018
0
    ever = NULL;
5019
0
  else if (extversym + extsymoff < extversym_end)
5020
0
    ever = extversym + extsymoff;
5021
0
  else
5022
0
    {
5023
      /* xgettext:c-format */
5024
0
      _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
5025
0
        abfd, (long) extsymoff,
5026
0
        (long) (extversym_end - extversym) / sizeof (* extversym));
5027
0
      bfd_set_error (bfd_error_bad_value);
5028
0
      goto error_free_vers;
5029
0
    }
5030
5031
0
  if (!bfd_link_relocatable (info)
5032
0
      && bfd_get_lto_type (abfd) == lto_slim_ir_object)
5033
0
    {
5034
0
      _bfd_error_handler
5035
0
  (_("%pB: plugin needed to handle lto object"), abfd);
5036
0
    }
5037
5038
0
  for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
5039
0
       isym < isymend;
5040
0
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
5041
0
    {
5042
0
      int bind;
5043
0
      bfd_vma value;
5044
0
      asection *sec, *new_sec;
5045
0
      flagword flags;
5046
0
      const char *name;
5047
0
      const char *defvername;
5048
0
      bool must_copy_name = false;
5049
0
      struct elf_link_hash_entry *h;
5050
0
      struct elf_link_hash_entry *hi;
5051
0
      bool definition;
5052
0
      bool size_change_ok;
5053
0
      bool type_change_ok;
5054
0
      bool new_weak;
5055
0
      bool old_weak;
5056
0
      bfd *override;
5057
0
      bool common;
5058
0
      bool discarded;
5059
0
      unsigned int old_alignment;
5060
0
      unsigned int shindex;
5061
0
      bfd *old_bfd;
5062
0
      bool matched;
5063
5064
0
      override = NULL;
5065
5066
0
      flags = BSF_NO_FLAGS;
5067
0
      sec = NULL;
5068
0
      value = isym->st_value;
5069
0
      common = bed->common_definition (isym);
5070
0
      if (common && info->inhibit_common_definition)
5071
0
  {
5072
    /* Treat common symbol as undefined for --no-define-common.  */
5073
0
    isym->st_shndx = SHN_UNDEF;
5074
0
    common = false;
5075
0
  }
5076
0
      discarded = false;
5077
5078
0
      bind = ELF_ST_BIND (isym->st_info);
5079
0
      switch (bind)
5080
0
  {
5081
0
  case STB_LOCAL:
5082
    /* This should be impossible, since ELF requires that all
5083
       global symbols follow all local symbols, and that sh_info
5084
       point to the first global symbol.  Unfortunately, Irix 5
5085
       screws this up.  */
5086
0
    if (elf_bad_symtab (abfd))
5087
0
      continue;
5088
5089
    /* If we aren't prepared to handle locals within the globals
5090
       then we'll likely segfault on a NULL symbol hash if the
5091
       symbol is ever referenced in relocations.  */
5092
0
    shindex = elf_elfheader (abfd)->e_shstrndx;
5093
0
    name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5094
0
    _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5095
0
        " (>= sh_info of %lu)"),
5096
0
            abfd, name, (long) (isym - isymbuf + extsymoff),
5097
0
            (long) extsymoff);
5098
5099
    /* Dynamic object relocations are not processed by ld, so
5100
       ld won't run into the problem mentioned above.  */
5101
0
    if (dynamic)
5102
0
      continue;
5103
0
    bfd_set_error (bfd_error_bad_value);
5104
0
    goto error_free_vers;
5105
5106
0
  case STB_GLOBAL:
5107
0
    if (isym->st_shndx != SHN_UNDEF && !common)
5108
0
      flags = BSF_GLOBAL;
5109
0
    break;
5110
5111
0
  case STB_WEAK:
5112
0
    flags = BSF_WEAK;
5113
0
    break;
5114
5115
0
  case STB_GNU_UNIQUE:
5116
0
    flags = BSF_GNU_UNIQUE;
5117
0
    break;
5118
5119
0
  default:
5120
    /* Leave it up to the processor backend.  */
5121
0
    break;
5122
0
  }
5123
5124
0
      if (isym->st_shndx == SHN_UNDEF)
5125
0
  sec = bfd_und_section_ptr;
5126
0
      else if (isym->st_shndx == SHN_ABS)
5127
0
  sec = bfd_abs_section_ptr;
5128
0
      else if (isym->st_shndx == SHN_COMMON)
5129
0
  {
5130
0
    sec = bfd_com_section_ptr;
5131
    /* What ELF calls the size we call the value.  What ELF
5132
       calls the value we call the alignment.  */
5133
0
    value = isym->st_size;
5134
0
  }
5135
0
      else
5136
0
  {
5137
0
    sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5138
0
    if (sec == NULL)
5139
0
      sec = bfd_abs_section_ptr;
5140
0
    else if (discarded_section (sec))
5141
0
      {
5142
        /* Symbols from discarded section are undefined.  We keep
5143
     its visibility.  */
5144
0
        sec = bfd_und_section_ptr;
5145
0
        discarded = true;
5146
0
        isym->st_shndx = SHN_UNDEF;
5147
0
      }
5148
0
    else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5149
0
      value -= sec->vma;
5150
0
  }
5151
5152
0
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5153
0
                isym->st_name);
5154
0
      if (name == NULL)
5155
0
  goto error_free_vers;
5156
5157
0
      if (isym->st_shndx == SHN_COMMON
5158
0
    && (abfd->flags & BFD_PLUGIN) != 0)
5159
0
  {
5160
0
    asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5161
5162
0
    if (xc == NULL)
5163
0
      {
5164
0
        flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5165
0
         | SEC_EXCLUDE);
5166
0
        xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5167
0
        if (xc == NULL)
5168
0
    goto error_free_vers;
5169
0
      }
5170
0
    sec = xc;
5171
0
  }
5172
0
      else if (isym->st_shndx == SHN_COMMON
5173
0
         && ELF_ST_TYPE (isym->st_info) == STT_TLS
5174
0
         && !bfd_link_relocatable (info))
5175
0
  {
5176
0
    asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5177
5178
0
    if (tcomm == NULL)
5179
0
      {
5180
0
        flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5181
0
         | SEC_LINKER_CREATED);
5182
0
        tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5183
0
        if (tcomm == NULL)
5184
0
    goto error_free_vers;
5185
0
      }
5186
0
    sec = tcomm;
5187
0
  }
5188
0
      else if (bed->elf_add_symbol_hook)
5189
0
  {
5190
0
    if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5191
0
               &sec, &value))
5192
0
      goto error_free_vers;
5193
5194
    /* The hook function sets the name to NULL if this symbol
5195
       should be skipped for some reason.  */
5196
0
    if (name == NULL)
5197
0
      continue;
5198
0
  }
5199
5200
0
      if (name[0] == '\0')
5201
0
  {
5202
0
    _bfd_error_handler (_("%pB: corrupt symbol table"), abfd);
5203
0
    bfd_set_error (bfd_error_bad_value);
5204
0
    goto error_free_vers;
5205
0
  }
5206
5207
      /* Sanity check that all possibilities were handled.  */
5208
0
      if (sec == NULL)
5209
0
  abort ();
5210
5211
      /* Silently discard TLS symbols from --just-syms.  There's
5212
   no way to combine a static TLS block with a new TLS block
5213
   for this executable.  */
5214
0
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5215
0
    && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5216
0
  continue;
5217
5218
0
      if (bfd_is_und_section (sec)
5219
0
    || bfd_is_com_section (sec))
5220
0
  definition = false;
5221
0
      else
5222
0
  definition = true;
5223
5224
0
      size_change_ok = false;
5225
0
      type_change_ok = bed->type_change_ok;
5226
0
      old_weak = false;
5227
0
      matched = false;
5228
0
      old_alignment = 0;
5229
0
      old_bfd = NULL;
5230
0
      new_sec = sec;
5231
0
      defvername = NULL;
5232
5233
0
      if (is_elf_hash_table (&htab->root))
5234
0
  {
5235
0
    Elf_Internal_Versym iver;
5236
0
    unsigned int vernum = 0;
5237
0
    bool skip;
5238
5239
0
    if (ever == NULL)
5240
0
      {
5241
0
        if (info->default_imported_symver)
5242
    /* Use the default symbol version created earlier.  */
5243
0
    iver.vs_vers = elf_tdata (abfd)->cverdefs;
5244
0
        else
5245
0
    iver.vs_vers = 0;
5246
0
      }
5247
0
    else if (ever >= extversym_end)
5248
0
      {
5249
        /* xgettext:c-format */
5250
0
        _bfd_error_handler (_("%pB: not enough version information"),
5251
0
          abfd);
5252
0
        bfd_set_error (bfd_error_bad_value);
5253
0
        goto error_free_vers;
5254
0
      }
5255
0
    else
5256
0
      _bfd_elf_swap_versym_in (abfd, ever, &iver);
5257
5258
0
    vernum = iver.vs_vers & VERSYM_VERSION;
5259
5260
    /* If this is a hidden symbol, or if it is not version
5261
       1, we append the version name to the symbol name.
5262
       However, we do not modify a non-hidden absolute symbol
5263
       if it is not a function, because it might be the version
5264
       symbol itself.  FIXME: What if it isn't?  */
5265
0
    if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5266
0
        || (vernum > 1
5267
0
      && (!bfd_is_abs_section (sec)
5268
0
          || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5269
0
      {
5270
0
        const char *verstr;
5271
0
        size_t namelen, verlen, newlen;
5272
0
        char *newname, *p;
5273
5274
0
        if (isym->st_shndx != SHN_UNDEF)
5275
0
    {
5276
0
      if (vernum > elf_tdata (abfd)->cverdefs)
5277
0
        verstr = NULL;
5278
0
      else if (vernum > 1)
5279
0
        verstr =
5280
0
          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5281
0
      else
5282
0
        verstr = "";
5283
5284
0
      if (verstr == NULL)
5285
0
        {
5286
0
          _bfd_error_handler
5287
      /* xgettext:c-format */
5288
0
      (_("%pB: %s: invalid version %u (max %d)"),
5289
0
       abfd, name, vernum,
5290
0
       elf_tdata (abfd)->cverdefs);
5291
0
          bfd_set_error (bfd_error_bad_value);
5292
0
          goto error_free_vers;
5293
0
        }
5294
0
    }
5295
0
        else
5296
0
    {
5297
      /* We cannot simply test for the number of
5298
         entries in the VERNEED section since the
5299
         numbers for the needed versions do not start
5300
         at 0.  */
5301
0
      Elf_Internal_Verneed *t;
5302
5303
0
      verstr = NULL;
5304
0
      for (t = elf_tdata (abfd)->verref;
5305
0
           t != NULL;
5306
0
           t = t->vn_nextref)
5307
0
        {
5308
0
          Elf_Internal_Vernaux *a;
5309
5310
0
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5311
0
      {
5312
0
        if (a->vna_other == vernum)
5313
0
          {
5314
0
            verstr = a->vna_nodename;
5315
0
            break;
5316
0
          }
5317
0
      }
5318
0
          if (a != NULL)
5319
0
      break;
5320
0
        }
5321
0
      if (verstr == NULL)
5322
0
        {
5323
0
          _bfd_error_handler
5324
      /* xgettext:c-format */
5325
0
      (_("%pB: %s: invalid needed version %d"),
5326
0
       abfd, name, vernum);
5327
0
          bfd_set_error (bfd_error_bad_value);
5328
0
          goto error_free_vers;
5329
0
        }
5330
0
    }
5331
5332
0
        namelen = strlen (name);
5333
0
        verlen = strlen (verstr);
5334
0
        newlen = namelen + verlen + 2;
5335
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5336
0
      && isym->st_shndx != SHN_UNDEF)
5337
0
    ++newlen;
5338
5339
0
        newname = bfd_hash_allocate (&htab->root.table, newlen);
5340
0
        if (newname == NULL)
5341
0
    goto error_free_vers;
5342
0
        memcpy (newname, name, namelen);
5343
0
        p = newname + namelen;
5344
0
        *p++ = ELF_VER_CHR;
5345
        /* If this is a defined non-hidden version symbol,
5346
     we add another @ to the name.  This indicates the
5347
     default version of the symbol.  */
5348
0
        if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5349
0
      && isym->st_shndx != SHN_UNDEF)
5350
0
    *p++ = ELF_VER_CHR, defvername = name;
5351
0
        memcpy (p, verstr, verlen + 1);
5352
5353
0
        name = newname;
5354
        /* Since bfd_hash_alloc is used for "name", the string
5355
     must be copied if added to first_hash.  The string
5356
     memory can be freed when an --as-needed library is
5357
     not needed.  */
5358
0
        must_copy_name = true;
5359
0
      }
5360
5361
    /* If this symbol has default visibility and the user has
5362
       requested we not re-export it, then mark it as hidden.  */
5363
0
    if (!bfd_is_und_section (sec)
5364
0
        && !dynamic
5365
0
        && abfd->no_export
5366
0
        && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5367
0
      isym->st_other = (STV_HIDDEN
5368
0
            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5369
5370
0
    if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5371
0
              sym_hash, &old_bfd, &old_weak,
5372
0
              &old_alignment, &skip, &override,
5373
0
              &type_change_ok, &size_change_ok,
5374
0
              &matched))
5375
0
      goto error_free_vers;
5376
5377
0
    if (skip)
5378
0
      continue;
5379
5380
0
    h = *sym_hash;
5381
0
    while (h->root.type == bfd_link_hash_indirect
5382
0
     || h->root.type == bfd_link_hash_warning)
5383
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5384
5385
    /* Override a definition only if the new symbol matches the
5386
       existing one.  */
5387
0
    if (override && matched)
5388
0
      {
5389
0
        definition = false;
5390
0
        if (htab->first_hash != NULL
5391
0
      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5392
0
      && h->root.non_ir_ref_regular)
5393
0
    {
5394
      /* When reloading --as-needed shared objects for new
5395
         symbols added from IR inputs, if this shared object
5396
         has the first definition, use it.  */
5397
0
      struct elf_link_first_hash_entry *e
5398
0
        = ((struct elf_link_first_hash_entry *)
5399
0
           bfd_hash_lookup (htab->first_hash, name, false,
5400
0
          false));
5401
0
      if (e != NULL && e->abfd == abfd)
5402
0
        definition = true;
5403
0
    }
5404
0
      }
5405
5406
0
    if (h->versioned != unversioned
5407
0
        && elf_tdata (abfd)->verdef != NULL
5408
0
        && vernum > 1
5409
0
        && definition)
5410
0
      h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5411
0
  }
5412
5413
0
      if (! (_bfd_generic_link_add_one_symbol
5414
0
       (info, override ? override : abfd, name, flags, sec, value,
5415
0
        NULL, false, bed->collect,
5416
0
        (struct bfd_link_hash_entry **) sym_hash)))
5417
0
  goto error_free_vers;
5418
5419
0
      h = *sym_hash;
5420
      /* We need to make sure that indirect symbol dynamic flags are
5421
   updated.  */
5422
0
      hi = h;
5423
0
      while (h->root.type == bfd_link_hash_indirect
5424
0
       || h->root.type == bfd_link_hash_warning)
5425
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5426
5427
0
      *sym_hash = h;
5428
5429
      /* Setting the index to -3 tells elf_link_output_extsym that
5430
   this symbol is defined in a discarded section.  */
5431
0
      if (discarded && is_elf_hash_table (&htab->root))
5432
0
  h->indx = -3;
5433
5434
0
      new_weak = (flags & BSF_WEAK) != 0;
5435
0
      if (dynamic
5436
0
    && definition
5437
0
    && new_weak
5438
0
    && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5439
0
    && is_elf_hash_table (&htab->root)
5440
0
    && h->u.alias == NULL)
5441
0
  {
5442
    /* Keep a list of all weak defined non function symbols from
5443
       a dynamic object, using the alias field.  Later in this
5444
       function we will set the alias field to the correct
5445
       value.  We only put non-function symbols from dynamic
5446
       objects on this list, because that happens to be the only
5447
       time we need to know the normal symbol corresponding to a
5448
       weak symbol, and the information is time consuming to
5449
       figure out.  If the alias field is not already NULL,
5450
       then this symbol was already defined by some previous
5451
       dynamic object, and we will be using that previous
5452
       definition anyhow.  */
5453
5454
0
    h->u.alias = weaks;
5455
0
    weaks = h;
5456
0
  }
5457
5458
      /* Set the alignment of a common symbol.  */
5459
0
      if ((common || bfd_is_com_section (sec))
5460
0
    && h->root.type == bfd_link_hash_common)
5461
0
  {
5462
0
    unsigned int align;
5463
5464
0
    if (common)
5465
0
      align = bfd_log2 (isym->st_value);
5466
0
    else
5467
0
      {
5468
        /* The new symbol is a common symbol in a shared object.
5469
     We need to get the alignment from the section.  */
5470
0
        align = new_sec->alignment_power;
5471
0
      }
5472
0
    if (align > old_alignment)
5473
0
      h->root.u.c.p->alignment_power = align;
5474
0
    else
5475
0
      h->root.u.c.p->alignment_power = old_alignment;
5476
0
  }
5477
5478
0
      if (is_elf_hash_table (&htab->root))
5479
0
  {
5480
    /* Set a flag in the hash table entry indicating the type of
5481
       reference or definition we just found.  A dynamic symbol
5482
       is one which is referenced or defined by both a regular
5483
       object and a shared object.  */
5484
0
    bool dynsym = false;
5485
5486
    /* Plugin symbols aren't normal.  Don't set def/ref flags.  */
5487
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5488
0
      {
5489
        /* Except for this flag to track nonweak references.  */
5490
0
        if (!definition
5491
0
      && bind != STB_WEAK)
5492
0
    h->ref_ir_nonweak = 1;
5493
0
      }
5494
0
    else if (!dynamic)
5495
0
      {
5496
0
        if (! definition)
5497
0
    {
5498
0
      h->ref_regular = 1;
5499
0
      if (bind != STB_WEAK)
5500
0
        h->ref_regular_nonweak = 1;
5501
0
    }
5502
0
        else
5503
0
    {
5504
0
      h->def_regular = 1;
5505
0
      if (h->def_dynamic)
5506
0
        {
5507
0
          h->def_dynamic = 0;
5508
0
          h->ref_dynamic = 1;
5509
0
        }
5510
0
    }
5511
0
      }
5512
0
    else
5513
0
      {
5514
0
        if (! definition)
5515
0
    {
5516
0
      h->ref_dynamic = 1;
5517
0
      hi->ref_dynamic = 1;
5518
0
    }
5519
0
        else
5520
0
    {
5521
0
      h->def_dynamic = 1;
5522
0
      hi->def_dynamic = 1;
5523
0
    }
5524
0
      }
5525
5526
    /* If an indirect symbol has been forced local, don't
5527
       make the real symbol dynamic.  */
5528
0
    if (h != hi && hi->forced_local)
5529
0
      ;
5530
0
    else if (!dynamic)
5531
0
      {
5532
0
        if (bfd_link_dll (info)
5533
0
      || h->def_dynamic
5534
0
      || h->ref_dynamic)
5535
0
    dynsym = true;
5536
0
      }
5537
0
    else
5538
0
      {
5539
0
        if (h->def_regular
5540
0
      || h->ref_regular
5541
0
      || (h->is_weakalias
5542
0
          && weakdef (h)->dynindx != -1))
5543
0
    dynsym = true;
5544
0
      }
5545
5546
    /* Check to see if we need to add an indirect symbol for
5547
       the default name.  */
5548
0
    if ((definition
5549
0
         || (!override && h->root.type == bfd_link_hash_common))
5550
0
        && !(hi != h
5551
0
       && hi->versioned == versioned_hidden))
5552
0
      if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5553
0
                sec, value, &old_bfd, &dynsym))
5554
0
        goto error_free_vers;
5555
5556
    /* Check the alignment when a common symbol is involved. This
5557
       can change when a common symbol is overridden by a normal
5558
       definition or a common symbol is ignored due to the old
5559
       normal definition. We need to make sure the maximum
5560
       alignment is maintained.  */
5561
0
    if ((old_alignment || common)
5562
0
        && h->root.type != bfd_link_hash_common)
5563
0
      {
5564
0
        unsigned int common_align;
5565
0
        unsigned int normal_align;
5566
0
        unsigned int symbol_align;
5567
0
        bfd *normal_bfd;
5568
0
        bfd *common_bfd;
5569
5570
0
        BFD_ASSERT (h->root.type == bfd_link_hash_defined
5571
0
        || h->root.type == bfd_link_hash_defweak);
5572
5573
0
        symbol_align = ffs (h->root.u.def.value) - 1;
5574
0
        if (h->root.u.def.section->owner != NULL
5575
0
      && (h->root.u.def.section->owner->flags
5576
0
           & (DYNAMIC | BFD_PLUGIN)) == 0)
5577
0
    {
5578
0
      normal_align = h->root.u.def.section->alignment_power;
5579
0
      if (normal_align > symbol_align)
5580
0
        normal_align = symbol_align;
5581
0
    }
5582
0
        else
5583
0
    normal_align = symbol_align;
5584
5585
0
        if (old_alignment)
5586
0
    {
5587
0
      common_align = old_alignment;
5588
0
      common_bfd = old_bfd;
5589
0
      normal_bfd = abfd;
5590
0
    }
5591
0
        else
5592
0
    {
5593
0
      common_align = bfd_log2 (isym->st_value);
5594
0
      common_bfd = abfd;
5595
0
      normal_bfd = old_bfd;
5596
0
    }
5597
5598
0
        if (normal_align < common_align)
5599
0
    {
5600
      /* PR binutils/2735 */
5601
0
      uint64_t c_align = UINT64_C (1) << common_align;
5602
0
      uint64_t n_align = UINT64_C (1) << normal_align;
5603
0
      if (normal_bfd == NULL)
5604
0
        _bfd_error_handler
5605
          /* xgettext:c-format */
5606
0
          (_("warning: alignment %" PRIu64 " of common symbol `%s' in %pB is"
5607
0
       " greater than the alignment (%" PRIu64 ") of its section %pA"),
5608
0
           c_align, name, common_bfd,
5609
0
           n_align, h->root.u.def.section);
5610
0
      else
5611
0
        _bfd_error_handler
5612
          /* xgettext:c-format */
5613
0
          (_("warning: alignment %" PRIu64 " of normal symbol `%s' in %pB"
5614
0
       " is smaller than %" PRIu64 " used by the common definition in %pB"),
5615
0
           n_align, name, normal_bfd,
5616
0
           c_align, common_bfd);
5617
5618
      /* PR 30499: make sure that users understand that this warning is serious.  */
5619
0
      _bfd_error_handler
5620
0
        (_("warning: NOTE: alignment discrepancies can cause real problems.  Investigation is advised."));
5621
0
    }
5622
0
      }
5623
5624
    /* Remember the symbol size if it isn't undefined.  */
5625
0
    if (isym->st_size != 0
5626
0
        && isym->st_shndx != SHN_UNDEF
5627
0
        && (definition || h->size == 0))
5628
0
      {
5629
0
        if (h->size != 0
5630
0
      && h->size != isym->st_size
5631
0
      && ! size_change_ok)
5632
0
    {
5633
0
      _bfd_error_handler
5634
        /* xgettext:c-format */
5635
0
        (_("warning: size of symbol `%s' changed"
5636
0
           " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5637
0
         name, (uint64_t) h->size, old_bfd,
5638
0
         (uint64_t) isym->st_size, abfd);
5639
5640
      /* PR 30499: make sure that users understand that this warning is serious.  */
5641
0
      _bfd_error_handler
5642
0
        (_("warning: NOTE: size discrepancies can cause real problems.  Investigation is advised."));
5643
0
    }
5644
5645
0
        h->size = isym->st_size;
5646
0
      }
5647
5648
    /* If this is a common symbol, then we always want H->SIZE
5649
       to be the size of the common symbol.  The code just above
5650
       won't fix the size if a common symbol becomes larger.  We
5651
       don't warn about a size change here, because that is
5652
       covered by --warn-common.  Allow changes between different
5653
       function types.  */
5654
0
    if (h->root.type == bfd_link_hash_common)
5655
0
      h->size = h->root.u.c.size;
5656
5657
0
    if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5658
0
        && ((definition && !new_weak)
5659
0
      || (old_weak && h->root.type == bfd_link_hash_common)
5660
0
      || h->type == STT_NOTYPE))
5661
0
      {
5662
0
        unsigned int type = ELF_ST_TYPE (isym->st_info);
5663
5664
        /* Turn an IFUNC symbol from a DSO into a normal FUNC
5665
     symbol.  */
5666
0
        if (type == STT_GNU_IFUNC
5667
0
      && (abfd->flags & DYNAMIC) != 0)
5668
0
    type = STT_FUNC;
5669
5670
0
        if (h->type != type)
5671
0
    {
5672
0
      if (h->type != STT_NOTYPE && ! type_change_ok)
5673
        /* xgettext:c-format */
5674
0
        _bfd_error_handler
5675
0
          (_("warning: type of symbol `%s' changed"
5676
0
       " from %d to %d in %pB"),
5677
0
           name, h->type, type, abfd);
5678
5679
0
      h->type = type;
5680
0
    }
5681
0
      }
5682
5683
    /* Merge st_other field.  */
5684
0
    elf_merge_st_other (info->output_bfd, abfd, h, isym->st_other,
5685
0
            sec, definition, dynamic);
5686
5687
    /* We don't want to make debug symbol dynamic.  */
5688
0
    if (definition
5689
0
        && (sec->flags & SEC_DEBUGGING)
5690
0
        && !bfd_link_relocatable (info))
5691
0
      dynsym = false;
5692
5693
    /* Nor should we make plugin symbols dynamic.  */
5694
0
    if ((abfd->flags & BFD_PLUGIN) != 0)
5695
0
      dynsym = false;
5696
5697
0
    if (definition)
5698
0
      {
5699
0
        h->target_internal = isym->st_target_internal;
5700
0
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5701
0
      }
5702
5703
    /* Don't add indirect symbols for .symver x, x@FOO aliases
5704
       in IR.  Since all data or text symbols in IR have the
5705
       same type, value and section, we can't tell if a symbol
5706
       is an alias of another symbol by their types, values and
5707
       sections.  */
5708
0
    if (definition
5709
0
        && !dynamic
5710
0
        && (abfd->flags & BFD_PLUGIN) == 0)
5711
0
      {
5712
0
        const char *p = strchr (name, ELF_VER_CHR);
5713
0
        if (p != NULL && p[1] != ELF_VER_CHR)
5714
0
    {
5715
      /* Queue non-default versions so that .symver x, x@FOO
5716
         aliases can be checked.  */
5717
0
      if (!nondeflt_vers)
5718
0
        {
5719
0
          nondeflt_vers = bfd_malloc ((isymend - isym + 1)
5720
0
              * sizeof (*nondeflt_vers));
5721
0
          if (!nondeflt_vers)
5722
0
      goto error_free_vers;
5723
0
        }
5724
0
      nondeflt_vers[nondeflt_vers_cnt++] = h;
5725
0
    }
5726
0
      }
5727
5728
0
    if (dynsym && h->dynindx == -1)
5729
0
      {
5730
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
5731
0
    goto error_free_vers;
5732
0
        if (h->is_weakalias
5733
0
      && weakdef (h)->dynindx == -1)
5734
0
    {
5735
0
      if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5736
0
        goto error_free_vers;
5737
0
    }
5738
0
      }
5739
0
    else if (h->dynindx != -1)
5740
      /* If the symbol already has a dynamic index, but
5741
         visibility says it should not be visible, turn it into
5742
         a local symbol.  */
5743
0
      switch (ELF_ST_VISIBILITY (h->other))
5744
0
        {
5745
0
        case STV_INTERNAL:
5746
0
        case STV_HIDDEN:
5747
0
    obed->elf_backend_hide_symbol (info, h, true);
5748
0
    dynsym = false;
5749
0
    break;
5750
0
        }
5751
5752
0
    if (!add_needed
5753
0
        && matched
5754
0
        && definition
5755
0
        && h->root.type != bfd_link_hash_indirect)
5756
0
      {
5757
0
        if ((dynsym
5758
0
       && h->ref_regular_nonweak)
5759
0
      || (old_bfd != NULL
5760
0
          && (old_bfd->flags & BFD_PLUGIN) != 0
5761
0
          && h->ref_ir_nonweak
5762
0
          && !info->lto_all_symbols_read)
5763
0
      || (h->ref_dynamic_nonweak
5764
0
          && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5765
0
          && !on_needed_list (elf_dt_name (abfd),
5766
0
            htab->needed, NULL)))
5767
0
    {
5768
0
      const char *soname = elf_dt_name (abfd);
5769
5770
0
      info->callbacks->minfo ("%!", soname, old_bfd,
5771
0
            h->root.root.string);
5772
5773
      /* A symbol from a library loaded via DT_NEEDED of some
5774
         other library is referenced by a regular object.
5775
         Add a DT_NEEDED entry for it.  Issue an error if
5776
         --no-add-needed is used and the reference was not
5777
         a weak one.  */
5778
0
      if (old_bfd != NULL
5779
0
          && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5780
0
        {
5781
0
          _bfd_error_handler
5782
      /* xgettext:c-format */
5783
0
      (_("%pB: undefined reference to symbol '%s'"),
5784
0
       old_bfd, name);
5785
0
          bfd_set_error (bfd_error_missing_dso);
5786
0
          goto error_free_vers;
5787
0
        }
5788
5789
0
      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5790
0
        (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5791
5792
      /* Create dynamic sections for backends that require
5793
         that be done before setup_gnu_properties.  */
5794
0
      if (!bfd_elf_link_create_dynamic_sections (info))
5795
0
        goto error_free_vers;
5796
0
      add_needed = true;
5797
0
    }
5798
0
        else if (dynamic
5799
0
           && h->root.u.def.section->owner == abfd)
5800
0
    {
5801
      /* Add this symbol to first hash if this shared
5802
         object has the first definition.  */
5803
0
      elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5804
      /* And if it was the default symbol version definition,
5805
         also add the short name.  */
5806
0
      if (defvername)
5807
0
        elf_link_add_to_first_hash (abfd, info, defvername, false);
5808
0
    }
5809
0
      }
5810
0
  }
5811
0
    }
5812
5813
0
  if (info->lto_plugin_active
5814
0
      && !bfd_link_relocatable (info)
5815
0
      && (abfd->flags & BFD_PLUGIN) == 0
5816
0
      && !just_syms
5817
0
      && extsymcount != 0
5818
0
      && is_elf_hash_table (&htab->root))
5819
0
    {
5820
0
      int r_sym_shift;
5821
5822
0
      if (bed->s->arch_size == 32)
5823
0
  r_sym_shift = 8;
5824
0
      else
5825
0
  r_sym_shift = 32;
5826
5827
      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5828
   referenced in regular objects so that linker plugin will get
5829
   the correct symbol resolution.  */
5830
5831
0
      sym_hash = elf_sym_hashes (abfd);
5832
0
      for (s = abfd->sections; s != NULL; s = s->next)
5833
0
  {
5834
0
    Elf_Internal_Rela *internal_relocs;
5835
0
    Elf_Internal_Rela *rel, *relend;
5836
5837
    /* Don't check relocations in excluded sections.  */
5838
0
    if ((s->flags & SEC_RELOC) == 0
5839
0
        || s->reloc_count == 0
5840
0
        || (s->flags & SEC_EXCLUDE) != 0
5841
0
        || (s->flags & SEC_DEBUGGING) != 0)
5842
0
      continue;
5843
5844
0
    internal_relocs
5845
0
      = _bfd_elf_link_info_read_relocs (abfd, info, s, NULL, NULL,
5846
0
                _bfd_elf_link_keep_memory (info));
5847
0
    if (internal_relocs == NULL)
5848
0
      goto error_free_vers;
5849
5850
0
    rel = internal_relocs;
5851
0
    relend = rel + s->reloc_count;
5852
0
    for ( ; rel < relend; rel++)
5853
0
      {
5854
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
5855
0
        struct elf_link_hash_entry *h;
5856
5857
        /* Skip local symbols.  */
5858
0
        if (r_symndx < extsymoff)
5859
0
    continue;
5860
5861
0
        h = sym_hash[r_symndx - extsymoff];
5862
0
        if (h != NULL)
5863
0
    h->root.non_ir_ref_regular = 1;
5864
0
      }
5865
5866
0
    if (elf_section_data (s)->relocs != internal_relocs)
5867
0
      free (internal_relocs);
5868
0
  }
5869
0
    }
5870
5871
0
  free (extversym);
5872
0
  extversym = NULL;
5873
0
  free (isymbuf);
5874
0
  isymbuf = NULL;
5875
5876
0
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5877
0
    {
5878
0
      unsigned int i;
5879
5880
      /* Restore the symbol table.  */
5881
0
      old_ent = (char *) old_tab + tabsize;
5882
0
      memset (elf_sym_hashes (abfd), 0,
5883
0
        extsymcount * sizeof (struct elf_link_hash_entry *));
5884
0
      htab->root.table.table = old_table;
5885
0
      htab->root.table.size = old_size;
5886
0
      htab->root.table.count = old_count;
5887
0
      memcpy (htab->root.table.table, old_tab, tabsize);
5888
0
      htab->root.undefs = old_undefs;
5889
0
      htab->root.undefs_tail = old_undefs_tail;
5890
0
      if (htab->dynstr != NULL)
5891
0
  _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5892
0
      free (old_strtab);
5893
0
      old_strtab = NULL;
5894
0
      for (i = 0; i < htab->root.table.size; i++)
5895
0
  {
5896
0
    struct bfd_hash_entry *p;
5897
0
    struct elf_link_hash_entry *h;
5898
0
    unsigned int non_ir_ref_dynamic;
5899
5900
0
    for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5901
0
      {
5902
        /* Preserve non_ir_ref_dynamic so that this symbol
5903
     will be exported when the dynamic lib becomes needed
5904
     in the second pass.  */
5905
0
        h = (struct elf_link_hash_entry *) p;
5906
0
        if (h->root.type == bfd_link_hash_warning)
5907
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5908
0
        non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5909
5910
0
        h = (struct elf_link_hash_entry *) p;
5911
0
        memcpy (h, old_ent, htab->root.table.entsize);
5912
0
        old_ent = (char *) old_ent + htab->root.table.entsize;
5913
0
        if (h->root.type == bfd_link_hash_warning)
5914
0
    {
5915
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5916
0
      memcpy (h, old_ent, htab->root.table.entsize);
5917
0
      old_ent = (char *) old_ent + htab->root.table.entsize;
5918
0
    }
5919
0
        if (h->root.type == bfd_link_hash_common)
5920
0
    {
5921
0
      memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5922
0
      old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5923
0
    }
5924
0
        h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5925
0
      }
5926
0
  }
5927
5928
      /* Make a special call to the linker "notice" function to
5929
   tell it that symbols added for crefs may need to be removed.  */
5930
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5931
0
  goto error_free_vers;
5932
5933
0
      free (old_tab);
5934
0
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5935
0
         alloc_mark);
5936
0
      free (nondeflt_vers);
5937
0
      return true;
5938
0
    }
5939
5940
0
  free (old_strtab);
5941
0
  old_strtab = NULL;
5942
0
  if (old_tab != NULL)
5943
0
    {
5944
0
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5945
0
  goto error_free_vers;
5946
0
      free (old_tab);
5947
0
      old_tab = NULL;
5948
0
    }
5949
5950
  /* Now that all the symbols from this input file are created, if
5951
     not performing a relocatable link, handle .symver foo, foo@BAR
5952
     such that any relocs against foo become foo@BAR.  */
5953
0
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5954
0
    {
5955
0
      size_t cnt, symidx;
5956
5957
0
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5958
0
  {
5959
0
    struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5960
0
    char *shortname;
5961
0
    const char *p;
5962
0
    size_t amt;
5963
5964
0
    p = strchr (h->root.root.string, ELF_VER_CHR);
5965
0
    if (p == NULL
5966
0
        || (h->root.type != bfd_link_hash_defined
5967
0
      && h->root.type != bfd_link_hash_defweak))
5968
0
      continue;
5969
5970
0
    amt = p - h->root.root.string;
5971
0
    shortname = bfd_malloc (amt + 1);
5972
0
    if (!shortname)
5973
0
      goto error_free_vers;
5974
0
    memcpy (shortname, h->root.root.string, amt);
5975
0
    shortname[amt] = '\0';
5976
5977
0
    hi = (struct elf_link_hash_entry *)
5978
0
         bfd_link_hash_lookup (&htab->root, shortname,
5979
0
             false, false, false);
5980
0
    if (hi != NULL
5981
0
        && hi->root.type == h->root.type
5982
0
        && hi->root.u.def.value == h->root.u.def.value
5983
0
        && hi->root.u.def.section == h->root.u.def.section)
5984
0
      {
5985
0
        obed->elf_backend_hide_symbol (info, hi, true);
5986
0
        hi->root.type = bfd_link_hash_indirect;
5987
0
        hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5988
0
        obed->elf_backend_copy_indirect_symbol (info, h, hi);
5989
0
        sym_hash = elf_sym_hashes (abfd);
5990
0
        if (sym_hash)
5991
0
    for (symidx = 0; symidx < extsymcount; ++symidx)
5992
0
      if (sym_hash[symidx] == hi)
5993
0
        {
5994
0
          sym_hash[symidx] = h;
5995
0
          break;
5996
0
        }
5997
0
      }
5998
0
    free (shortname);
5999
0
  }
6000
0
    }
6001
0
  free (nondeflt_vers);
6002
0
  nondeflt_vers = NULL;
6003
6004
  /* Now set the alias field correctly for all the weak defined
6005
     symbols we found.  The only way to do this is to search all the
6006
     symbols.  Since we only need the information for non functions in
6007
     dynamic objects, that's the only time we actually put anything on
6008
     the list WEAKS.  We need this information so that if a regular
6009
     object refers to a symbol defined weakly in a dynamic object, the
6010
     real symbol in the dynamic object is also put in the dynamic
6011
     symbols; we also must arrange for both symbols to point to the
6012
     same memory location.  We could handle the general case of symbol
6013
     aliasing, but a general symbol alias can only be generated in
6014
     assembler code, handling it correctly would be very time
6015
     consuming, and other ELF linkers don't handle general aliasing
6016
     either.  */
6017
0
  if (weaks != NULL)
6018
0
    {
6019
0
      struct elf_link_hash_entry **hpp;
6020
0
      struct elf_link_hash_entry **hppend;
6021
0
      struct elf_link_hash_entry **sorted_sym_hash;
6022
0
      struct elf_link_hash_entry *h;
6023
0
      size_t sym_count;
6024
6025
      /* Since we have to search the whole symbol list for each weak
6026
   defined symbol, search time for N weak defined symbols will be
6027
   O(N^2). Binary search will cut it down to O(NlogN).  */
6028
0
      sorted_sym_hash = bfd_malloc (extsymcount * sizeof (*sorted_sym_hash));
6029
0
      if (sorted_sym_hash == NULL)
6030
0
  goto error_return;
6031
0
      sym_hash = sorted_sym_hash;
6032
0
      hpp = elf_sym_hashes (abfd);
6033
0
      hppend = hpp + extsymcount;
6034
0
      sym_count = 0;
6035
0
      for (; hpp < hppend; hpp++)
6036
0
  {
6037
0
    h = *hpp;
6038
0
    if (h != NULL
6039
0
        && h->root.type == bfd_link_hash_defined
6040
0
        && !bed->is_function_type (h->type))
6041
0
      {
6042
0
        *sym_hash = h;
6043
0
        sym_hash++;
6044
0
        sym_count++;
6045
0
      }
6046
0
  }
6047
6048
0
      qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
6049
0
       elf_sort_symbol);
6050
6051
0
      while (weaks != NULL)
6052
0
  {
6053
0
    struct elf_link_hash_entry *hlook;
6054
0
    asection *slook;
6055
0
    bfd_vma vlook;
6056
0
    size_t i, j, idx = 0;
6057
6058
0
    hlook = weaks;
6059
0
    weaks = hlook->u.alias;
6060
0
    hlook->u.alias = NULL;
6061
6062
0
    if (hlook->root.type != bfd_link_hash_defined
6063
0
        && hlook->root.type != bfd_link_hash_defweak)
6064
0
      continue;
6065
6066
0
    slook = hlook->root.u.def.section;
6067
0
    vlook = hlook->root.u.def.value;
6068
6069
0
    i = 0;
6070
0
    j = sym_count;
6071
0
    while (i != j)
6072
0
      {
6073
0
        bfd_signed_vma vdiff;
6074
0
        idx = (i + j) / 2;
6075
0
        h = sorted_sym_hash[idx];
6076
0
        vdiff = vlook - h->root.u.def.value;
6077
0
        if (vdiff < 0)
6078
0
    j = idx;
6079
0
        else if (vdiff > 0)
6080
0
    i = idx + 1;
6081
0
        else
6082
0
    {
6083
0
      int sdiff = slook->id - h->root.u.def.section->id;
6084
0
      if (sdiff < 0)
6085
0
        j = idx;
6086
0
      else if (sdiff > 0)
6087
0
        i = idx + 1;
6088
0
      else
6089
0
        break;
6090
0
    }
6091
0
      }
6092
6093
    /* We didn't find a value/section match.  */
6094
0
    if (i == j)
6095
0
      continue;
6096
6097
    /* With multiple aliases, or when the weak symbol is already
6098
       strongly defined, we have multiple matching symbols and
6099
       the binary search above may land on any of them.  Step
6100
       one past the matching symbol(s).  */
6101
0
    while (++idx != j)
6102
0
      {
6103
0
        h = sorted_sym_hash[idx];
6104
0
        if (h->root.u.def.section != slook
6105
0
      || h->root.u.def.value != vlook)
6106
0
    break;
6107
0
      }
6108
6109
    /* Now look back over the aliases.  Since we sorted by size
6110
       as well as value and section, we'll choose the one with
6111
       the largest size.  */
6112
0
    while (idx-- != i)
6113
0
      {
6114
0
        h = sorted_sym_hash[idx];
6115
6116
        /* Stop if value or section doesn't match.  */
6117
0
        if (h->root.u.def.section != slook
6118
0
      || h->root.u.def.value != vlook)
6119
0
    break;
6120
0
        else if (h != hlook)
6121
0
    {
6122
0
      struct elf_link_hash_entry *t;
6123
6124
0
      hlook->u.alias = h;
6125
0
      hlook->is_weakalias = 1;
6126
0
      t = h;
6127
0
      if (t->u.alias != NULL)
6128
0
        while (t->u.alias != h)
6129
0
          t = t->u.alias;
6130
0
      t->u.alias = hlook;
6131
6132
      /* If the weak definition is in the list of dynamic
6133
         symbols, make sure the real definition is put
6134
         there as well.  */
6135
0
      if (hlook->dynindx != -1 && h->dynindx == -1)
6136
0
        {
6137
0
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
6138
0
      {
6139
0
      err_free_sym_hash:
6140
0
        free (sorted_sym_hash);
6141
0
        goto error_return;
6142
0
      }
6143
0
        }
6144
6145
      /* If the real definition is in the list of dynamic
6146
         symbols, make sure the weak definition is put
6147
         there as well.  If we don't do this, then the
6148
         dynamic loader might not merge the entries for the
6149
         real definition and the weak definition.  */
6150
0
      if (h->dynindx != -1 && hlook->dynindx == -1)
6151
0
        {
6152
0
          if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6153
0
      goto err_free_sym_hash;
6154
0
        }
6155
0
      break;
6156
0
    }
6157
0
      }
6158
0
  }
6159
6160
0
      free (sorted_sym_hash);
6161
0
    }
6162
6163
0
  if (bed->check_directives
6164
0
      && !(*bed->check_directives) (abfd, info))
6165
0
    goto error_return;
6166
6167
  /* If this is a non-traditional link, try to optimize the handling
6168
     of the .stab/.stabstr sections.  */
6169
0
  if (! dynamic
6170
0
      && ! info->traditional_format
6171
0
      && is_elf_hash_table (&htab->root)
6172
0
      && (info->strip != strip_all && info->strip != strip_debugger))
6173
0
    {
6174
0
      asection *stabstr;
6175
6176
0
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6177
0
      if (stabstr != NULL)
6178
0
  {
6179
0
    bfd_size_type string_offset = 0;
6180
0
    asection *stab;
6181
6182
0
    for (stab = abfd->sections; stab; stab = stab->next)
6183
0
      if (startswith (stab->name, ".stab")
6184
0
    && (!stab->name[5] ||
6185
0
        (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6186
0
    && (stab->flags & SEC_MERGE) == 0
6187
0
    && !bfd_is_abs_section (stab->output_section)
6188
0
    && !_bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6189
0
               stabstr, &string_offset))
6190
0
        goto error_return;
6191
0
  }
6192
0
    }
6193
6194
0
  if (dynamic && add_needed)
6195
0
    {
6196
      /* Add this bfd to the loaded list.  */
6197
0
      struct elf_link_loaded_list *n;
6198
6199
0
      n = bfd_alloc (abfd, sizeof (*n));
6200
0
      if (n == NULL)
6201
0
  goto error_return;
6202
0
      n->abfd = abfd;
6203
0
      n->next = htab->dyn_loaded;
6204
0
      htab->dyn_loaded = n;
6205
0
    }
6206
0
  if (dynamic && !add_needed
6207
0
      && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6208
0
    elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6209
6210
0
  return true;
6211
6212
0
 error_free_vers:
6213
0
  free (old_tab);
6214
0
  free (old_strtab);
6215
0
  free (nondeflt_vers);
6216
0
  free (extversym);
6217
0
 error_free_sym:
6218
0
  free (isymbuf);
6219
0
 error_return:
6220
0
  return false;
6221
0
}
6222
6223
/* Return the linker hash table entry of a symbol that might be
6224
   satisfied by an archive symbol.  Return -1 on error.  */
6225
6226
struct bfd_link_hash_entry *
6227
_bfd_elf_archive_symbol_lookup (bfd *abfd,
6228
        struct bfd_link_info *info,
6229
        const char *name)
6230
0
{
6231
0
  struct bfd_link_hash_entry *h;
6232
0
  const char *p;
6233
0
  char *copy;
6234
0
  size_t len, first;
6235
6236
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6237
0
  if (h != NULL)
6238
0
    return h;
6239
6240
  /* If this is a default version (the name contains @@), look up the
6241
     symbol again with only one `@' as well as without the version.
6242
     The effect is that references to the symbol with and without the
6243
     version will be matched by the default symbol in the archive.  */
6244
6245
0
  p = strchr (name, ELF_VER_CHR);
6246
0
  if (p == NULL || p[1] != ELF_VER_CHR)
6247
0
    {
6248
      /* Add this symbol to first hash if this archive has the first
6249
   definition.  */
6250
0
      if (is_elf_hash_table (info->hash))
6251
0
  elf_link_add_to_first_hash (abfd, info, name, false);
6252
0
      return h;
6253
0
    }
6254
6255
  /* First check with only one `@'.  */
6256
0
  len = strlen (name);
6257
0
  copy = bfd_alloc (abfd, len);
6258
0
  if (copy == NULL)
6259
0
    return (struct bfd_link_hash_entry *) -1;
6260
6261
0
  first = p - name + 1;
6262
0
  memcpy (copy, name, first);
6263
0
  memcpy (copy + first, name + first + 1, len - first);
6264
6265
0
  h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6266
0
  if (h == NULL)
6267
0
    {
6268
      /* We also need to check references to the symbol without the
6269
   version.  */
6270
0
      copy[first - 1] = '\0';
6271
0
      h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6272
0
    }
6273
6274
0
  bfd_release (abfd, copy);
6275
0
  return h;
6276
0
}
6277
6278
/* Add symbols from an ELF archive file to the linker hash table.  We
6279
   don't use _bfd_generic_link_add_archive_symbols because we need to
6280
   handle versioned symbols.
6281
6282
   Fortunately, ELF archive handling is simpler than that done by
6283
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6284
   oddities.  In ELF, if we find a symbol in the archive map, and the
6285
   symbol is currently undefined, we know that we must pull in that
6286
   object file.
6287
6288
   Unfortunately, we do have to make multiple passes over the symbol
6289
   table until nothing further is resolved.  */
6290
6291
static bool
6292
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6293
0
{
6294
0
  symindex c;
6295
0
  unsigned char *included = NULL;
6296
0
  carsym *symdefs;
6297
0
  bool loop;
6298
0
  elf_backend_data *bed;
6299
0
  struct bfd_link_hash_entry * (*archive_symbol_lookup)
6300
0
    (bfd *, struct bfd_link_info *, const char *);
6301
6302
0
  if (! bfd_has_map (abfd))
6303
0
    {
6304
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
6305
6306
      /* An empty archive is a special case.  */
6307
0
      if (first_one == NULL)
6308
0
  return true;
6309
6310
0
      if (!_bfd_make_armap (abfd, first_one))
6311
0
  return false;
6312
0
    }
6313
6314
  /* Keep track of all symbols we know to be already defined, and all
6315
     files we know to be already included.  This is to speed up the
6316
     second and subsequent passes.  */
6317
0
  c = bfd_ardata (abfd)->symdef_count;
6318
0
  if (c == 0)
6319
0
    return true;
6320
0
  included = bfd_zmalloc (c * sizeof (*included));
6321
0
  if (included == NULL)
6322
0
    return false;
6323
6324
0
  symdefs = bfd_ardata (abfd)->symdefs;
6325
0
  bed = get_elf_backend_data (abfd);
6326
0
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6327
6328
0
  do
6329
0
    {
6330
0
      ufile_ptr_or_bfd last;
6331
0
      symindex i;
6332
0
      carsym *symdef;
6333
0
      carsym *symdefend;
6334
6335
0
      loop = false;
6336
0
      last = _bfd_elt_nil (abfd);
6337
6338
0
      symdef = symdefs;
6339
0
      symdefend = symdef + c;
6340
0
      for (i = 0; symdef < symdefend; symdef++, i++)
6341
0
  {
6342
0
    struct bfd_link_hash_entry *h;
6343
0
    bfd *element;
6344
0
    struct bfd_link_hash_entry *undefs_tail;
6345
0
    symindex mark;
6346
6347
0
    if (included[i])
6348
0
      continue;
6349
0
    if (_bfd_elt_eq (abfd, symdef->u, last))
6350
0
      {
6351
0
        included[i] = true;
6352
0
        continue;
6353
0
      }
6354
6355
0
    h = archive_symbol_lookup (abfd, info, symdef->name);
6356
0
    if (h == (struct bfd_link_hash_entry *) -1)
6357
0
      goto error_return;
6358
6359
0
    if (h == NULL)
6360
0
      continue;
6361
6362
0
    if (h->type == bfd_link_hash_undefined)
6363
0
      {
6364
0
        if (is_elf_hash_table (info->hash))
6365
0
    {
6366
      /* If the archive element has already been loaded then one
6367
         of the symbols defined by that element might have been
6368
         made undefined due to being in a discarded section.  */
6369
0
      if (((struct elf_link_hash_entry *) h)->indx == -3)
6370
0
        continue;
6371
6372
      /* In the pre-LTO-plugin pass we must not mistakenly
6373
         include this archive member if an earlier shared
6374
         library defined this symbol.  */
6375
0
      struct elf_link_hash_table *htab = elf_hash_table (info);
6376
0
      if (htab->first_hash)
6377
0
        {
6378
0
          struct elf_link_first_hash_entry *e
6379
0
        = ((struct elf_link_first_hash_entry *)
6380
0
           bfd_hash_lookup (htab->first_hash, symdef->name,
6381
0
                false, false));
6382
0
          if (e
6383
0
        && (e->abfd->flags & DYNAMIC) != 0
6384
0
        && e->abfd != abfd)
6385
0
      continue;
6386
0
        }
6387
0
    }
6388
0
      }
6389
0
    else if (h->type == bfd_link_hash_common)
6390
0
      {
6391
        /* We currently have a common symbol.  The archive map contains
6392
     a reference to this symbol, so we may want to include it.  We
6393
     only want to include it however, if this archive element
6394
     contains a definition of the symbol, not just another common
6395
     declaration of it.
6396
6397
     Unfortunately some archivers (including GNU ar) will put
6398
     declarations of common symbols into their archive maps, as
6399
     well as real definitions, so we cannot just go by the archive
6400
     map alone.  Instead we must read in the element's symbol
6401
     table and check that to see what kind of symbol definition
6402
     this is.  */
6403
0
        if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6404
0
    continue;
6405
0
      }
6406
0
    else
6407
0
      {
6408
0
        if (h->type != bfd_link_hash_undefweak)
6409
    /* Symbol must be defined.  Don't check it again.  */
6410
0
    included[i] = true;
6411
6412
0
        if (!is_elf_hash_table (info->hash))
6413
0
    continue;
6414
0
        struct elf_link_hash_entry *eh
6415
0
    = (struct elf_link_hash_entry *) h;
6416
        /* Ignore the archive if the symbol isn't referenced by a
6417
     regular object or isn't defined in a shared object.  */
6418
0
        if (!eh->ref_regular || !eh->def_dynamic)
6419
0
    continue;
6420
        /* Ignore the dynamic definition if symbol is first
6421
     defined in this archive.  */
6422
0
        struct elf_link_hash_table *htab = elf_hash_table (info);
6423
0
        if (htab->first_hash == NULL)
6424
0
    continue;
6425
0
        struct elf_link_first_hash_entry *e
6426
0
    = ((struct elf_link_first_hash_entry *)
6427
0
       bfd_hash_lookup (htab->first_hash, symdef->name,
6428
0
            false, false));
6429
0
        if (e == NULL || e->abfd != abfd)
6430
0
    continue;
6431
0
      }
6432
6433
    /* We need to include this archive member.  */
6434
0
    element = _bfd_get_elt_from_symdef (abfd, symdef, info);
6435
0
    if (element == NULL)
6436
0
      goto error_return;
6437
6438
0
    if (! bfd_check_format (element, bfd_object))
6439
0
      goto error_return;
6440
6441
0
    undefs_tail = info->hash->undefs_tail;
6442
6443
0
    if (!(*info->callbacks
6444
0
    ->add_archive_element) (info, element, symdef->name, &element))
6445
0
      continue;
6446
0
    if (!bfd_link_add_symbols (element, info))
6447
0
      goto error_return;
6448
6449
    /* If there are any new undefined symbols, we need to make
6450
       another pass through the archive in order to see whether
6451
       they can be defined.  FIXME: This isn't perfect, because
6452
       common symbols wind up on undefs_tail and because an
6453
       undefined symbol which is defined later on in this pass
6454
       does not require another pass.  This isn't a bug, but it
6455
       does make the code less efficient than it could be.  */
6456
0
    if (undefs_tail != info->hash->undefs_tail)
6457
0
      loop = true;
6458
6459
    /* Look backward to mark all symbols from this object file
6460
       which we have already seen in this pass.  */
6461
0
    mark = i;
6462
0
    do
6463
0
      {
6464
0
        included[mark] = true;
6465
0
        if (mark == 0)
6466
0
    break;
6467
0
        --mark;
6468
0
      }
6469
0
    while (_bfd_elt_eq (abfd, symdefs[mark].u, symdef->u));
6470
6471
    /* We mark subsequent symbols from this object file as we go
6472
       on through the loop.  */
6473
0
    last = symdef->u;
6474
0
  }
6475
0
    }
6476
0
  while (loop);
6477
6478
0
  free (included);
6479
0
  return true;
6480
6481
0
 error_return:
6482
0
  free (included);
6483
0
  return false;
6484
0
}
6485
6486
/* Given an ELF BFD, add symbols to the global hash table as
6487
   appropriate.  */
6488
6489
bool
6490
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6491
0
{
6492
0
  switch (bfd_get_format (abfd))
6493
0
    {
6494
0
    case bfd_object:
6495
0
      return elf_link_add_object_symbols (abfd, info);
6496
0
    case bfd_archive:
6497
0
      return elf_link_add_archive_symbols (abfd, info);
6498
0
    default:
6499
0
      bfd_set_error (bfd_error_wrong_format);
6500
0
      return false;
6501
0
    }
6502
0
}
6503

6504
struct hash_codes_info
6505
{
6506
  unsigned long *hashcodes;
6507
  bool error;
6508
};
6509
6510
/* This function will be called though elf_link_hash_traverse to store
6511
   all hash value of the exported symbols in an array.  */
6512
6513
static bool
6514
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6515
0
{
6516
0
  struct hash_codes_info *inf = data;
6517
0
  const char *name;
6518
0
  unsigned long ha;
6519
0
  char *alc = NULL;
6520
6521
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6522
0
  if (h->dynindx == -1)
6523
0
    return true;
6524
6525
0
  name = h->root.root.string;
6526
0
  if (h->versioned >= versioned)
6527
0
    {
6528
0
      const char *p = strchr (name, ELF_VER_CHR);
6529
0
      if (p != NULL)
6530
0
  {
6531
0
    alc = bfd_malloc (p - name + 1);
6532
0
    if (alc == NULL)
6533
0
      {
6534
0
        inf->error = true;
6535
0
        return false;
6536
0
      }
6537
0
    memcpy (alc, name, p - name);
6538
0
    alc[p - name] = '\0';
6539
0
    name = alc;
6540
0
  }
6541
0
    }
6542
6543
  /* Compute the hash value.  */
6544
0
  ha = bfd_elf_hash (name);
6545
6546
  /* Store the found hash value in the array given as the argument.  */
6547
0
  *(inf->hashcodes)++ = ha;
6548
6549
  /* And store it in the struct so that we can put it in the hash table
6550
     later.  */
6551
0
  h->u.elf_hash_value = ha;
6552
6553
0
  free (alc);
6554
0
  return true;
6555
0
}
6556
6557
struct collect_gnu_hash_codes
6558
{
6559
  bfd *output_bfd;
6560
  elf_backend_data *bed;
6561
  unsigned long int nsyms;
6562
  unsigned long int maskbits;
6563
  unsigned long int *hashcodes;
6564
  unsigned long int *hashval;
6565
  unsigned long int *indx;
6566
  unsigned long int *counts;
6567
  bfd_vma *bitmask;
6568
  bfd_byte *contents;
6569
  bfd_size_type xlat;
6570
  long int min_dynindx;
6571
  unsigned long int bucketcount;
6572
  unsigned long int symindx;
6573
  long int local_indx;
6574
  long int shift1, shift2;
6575
  unsigned long int mask;
6576
  bool error;
6577
  bool base_symbol;
6578
};
6579
6580
/* This function will be called though elf_link_hash_traverse to store
6581
   all hash value of the exported symbols in an array.  */
6582
6583
static bool
6584
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6585
0
{
6586
0
  struct collect_gnu_hash_codes *s = data;
6587
0
  const char *name;
6588
0
  unsigned long ha;
6589
0
  char *alc = NULL;
6590
6591
  /* Ignore indirect symbols.  These are added by the versioning code.  */
6592
0
  if (h->dynindx == -1)
6593
0
    return true;
6594
6595
  /* Ignore also local symbols and undefined symbols.  */
6596
0
  if (! (*s->bed->elf_hash_symbol) (h))
6597
0
    return true;
6598
6599
0
  name = h->root.root.string;
6600
0
  if (h->versioned >= versioned)
6601
0
    {
6602
0
      const char *p = strchr (name, ELF_VER_CHR);
6603
0
      if (p != NULL)
6604
0
  {
6605
0
    alc = bfd_malloc (p - name + 1);
6606
0
    if (alc == NULL)
6607
0
      {
6608
0
        s->error = true;
6609
0
        return false;
6610
0
      }
6611
0
    memcpy (alc, name, p - name);
6612
0
    alc[p - name] = '\0';
6613
0
    name = alc;
6614
0
  }
6615
0
    }
6616
6617
  /* Compute the hash value.  */
6618
0
  ha = bfd_elf_gnu_hash (name);
6619
6620
  /* Store the found hash value in the array for compute_bucket_count,
6621
     and also for .dynsym reordering purposes.  */
6622
0
  s->hashcodes[s->nsyms] = ha;
6623
0
  s->hashval[h->dynindx] = ha;
6624
0
  ++s->nsyms;
6625
0
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6626
0
    s->min_dynindx = h->dynindx;
6627
6628
0
  free (alc);
6629
0
  return true;
6630
0
}
6631
6632
/* This function will be called though elf_link_hash_traverse to do
6633
   final dynamic symbol renumbering in case of .gnu.hash.
6634
   If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6635
   to the translation table.  */
6636
6637
static bool
6638
elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6639
0
{
6640
0
  struct collect_gnu_hash_codes *s = data;
6641
0
  unsigned long int bucket;
6642
0
  unsigned long int val;
6643
6644
  /* Ignore indirect symbols.  */
6645
0
  if (h->dynindx == -1)
6646
0
    return true;
6647
6648
  /* Skip if base symbol doesn't match.  */
6649
0
  if (s->base_symbol != !!h->base_symbol)
6650
0
    return true;
6651
6652
  /* Ignore also local symbols and undefined symbols.  */
6653
0
  if (! (*s->bed->elf_hash_symbol) (h))
6654
0
    {
6655
0
      if (h->dynindx >= s->min_dynindx)
6656
0
  {
6657
0
    if (s->bed->record_xhash_symbol != NULL)
6658
0
      {
6659
0
        (*s->bed->record_xhash_symbol) (h, 0);
6660
0
        s->local_indx++;
6661
0
      }
6662
0
    else
6663
0
      h->dynindx = s->local_indx++;
6664
0
  }
6665
0
      return true;
6666
0
    }
6667
6668
0
  bucket = s->hashval[h->dynindx] % s->bucketcount;
6669
0
  val = (s->hashval[h->dynindx] >> s->shift1)
6670
0
  & ((s->maskbits >> s->shift1) - 1);
6671
0
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6672
0
  s->bitmask[val]
6673
0
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6674
0
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6675
0
  if (s->counts[bucket] == 1)
6676
    /* Last element terminates the chain.  */
6677
0
    val |= 1;
6678
0
  bfd_put_32 (s->output_bfd, val,
6679
0
        s->contents + (s->indx[bucket] - s->symindx) * 4);
6680
0
  --s->counts[bucket];
6681
0
  if (s->bed->record_xhash_symbol != NULL)
6682
0
    {
6683
0
      bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6684
6685
0
      (*s->bed->record_xhash_symbol) (h, xlat_loc);
6686
0
    }
6687
0
  else
6688
0
    h->dynindx = s->indx[bucket]++;
6689
0
  return true;
6690
0
}
6691
6692
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6693
6694
bool
6695
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6696
0
{
6697
0
  return !(h->forced_local
6698
0
     || h->root.type == bfd_link_hash_undefined
6699
0
     || h->root.type == bfd_link_hash_undefweak
6700
0
     || ((h->root.type == bfd_link_hash_defined
6701
0
    || h->root.type == bfd_link_hash_defweak)
6702
0
         && h->root.u.def.section->output_section == NULL));
6703
0
}
6704
6705
/* Array used to determine the number of hash table buckets to use
6706
   based on the number of symbols there are.  If there are fewer than
6707
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6708
   fewer than 37 we use 17 buckets, and so forth.  We never use more
6709
   than 32771 buckets.  */
6710
6711
static const size_t elf_buckets[] =
6712
{
6713
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6714
  16411, 32771, 0
6715
};
6716
6717
/* Compute bucket count for hashing table.  We do not use a static set
6718
   of possible tables sizes anymore.  Instead we determine for all
6719
   possible reasonable sizes of the table the outcome (i.e., the
6720
   number of collisions etc) and choose the best solution.  The
6721
   weighting functions are not too simple to allow the table to grow
6722
   without bounds.  Instead one of the weighting factors is the size.
6723
   Therefore the result is always a good payoff between few collisions
6724
   (= short chain lengths) and table size.  */
6725
static size_t
6726
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6727
          unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6728
          unsigned long int nsyms,
6729
          int gnu_hash)
6730
0
{
6731
0
  size_t best_size = 0;
6732
0
  unsigned long int i;
6733
6734
0
  if (info->optimize)
6735
0
    {
6736
0
      size_t minsize;
6737
0
      size_t maxsize;
6738
0
      uint64_t best_chlen = ~((uint64_t) 0);
6739
0
      bfd *dynobj = elf_hash_table (info)->dynobj;
6740
0
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6741
0
      elf_backend_data *obed = get_elf_backend_data (dynobj);
6742
0
      unsigned long int *counts;
6743
0
      unsigned int no_improvement_count = 0;
6744
6745
      /* Possible optimization parameters: if we have NSYMS symbols we say
6746
   that the hashing table must at least have NSYMS/4 and at most
6747
   2*NSYMS buckets.  */
6748
0
      minsize = nsyms / 4;
6749
0
      if (minsize == 0)
6750
0
  minsize = 1;
6751
0
      best_size = maxsize = nsyms * 2;
6752
0
      if (gnu_hash)
6753
0
  {
6754
0
    if (minsize < 2)
6755
0
      minsize = 2;
6756
0
    if ((best_size & 31) == 0)
6757
0
      ++best_size;
6758
0
  }
6759
6760
      /* Create array where we count the collisions in.  We must use bfd_malloc
6761
   since the size could be large.  */
6762
0
      counts = bfd_malloc (maxsize * sizeof (*counts));
6763
0
      if (counts == NULL)
6764
0
  return 0;
6765
6766
      /* Compute the "optimal" size for the hash table.  The criteria is a
6767
   minimal chain length.  The minor criteria is (of course) the size
6768
   of the table.  */
6769
0
      for (i = minsize; i < maxsize; ++i)
6770
0
  {
6771
    /* Walk through the array of hashcodes and count the collisions.  */
6772
0
    uint64_t max;
6773
0
    unsigned long int j;
6774
0
    unsigned long int fact;
6775
6776
0
    if (gnu_hash && (i & 31) == 0)
6777
0
      continue;
6778
6779
0
    memset (counts, '\0', i * sizeof (unsigned long int));
6780
6781
    /* Determine how often each hash bucket is used.  */
6782
0
    for (j = 0; j < nsyms; ++j)
6783
0
      ++counts[hashcodes[j] % i];
6784
6785
    /* For the weight function we need some information about the
6786
       pagesize on the target.  This is information need not be 100%
6787
       accurate.  Since this information is not available (so far) we
6788
       define it here to a reasonable default value.  If it is crucial
6789
       to have a better value some day simply define this value.  */
6790
0
# ifndef BFD_TARGET_PAGESIZE
6791
0
#  define BFD_TARGET_PAGESIZE (4096)
6792
0
# endif
6793
6794
    /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6795
       and the chains.  */
6796
0
    max = (2 + dynsymcount) * obed->s->sizeof_hash_entry;
6797
6798
0
# if 1
6799
    /* Variant 1: optimize for short chains.  We add the squares
6800
       of all the chain lengths (which favors many small chain
6801
       over a few long chains).  */
6802
0
    for (j = 0; j < i; ++j)
6803
0
      max += counts[j] * counts[j];
6804
6805
    /* This adds penalties for the overall size of the table.  */
6806
0
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6807
0
    max *= fact * fact;
6808
# else
6809
    /* Variant 2: Optimize a lot more for small table.  Here we
6810
       also add squares of the size but we also add penalties for
6811
       empty slots (the +1 term).  */
6812
    for (j = 0; j < i; ++j)
6813
      max += (1 + counts[j]) * (1 + counts[j]);
6814
6815
    /* The overall size of the table is considered, but not as
6816
       strong as in variant 1, where it is squared.  */
6817
    fact = i / (BFD_TARGET_PAGESIZE / obed->s->sizeof_hash_entry) + 1;
6818
    max *= fact;
6819
# endif
6820
6821
    /* Compare with current best results.  */
6822
0
    if (max < best_chlen)
6823
0
      {
6824
0
        best_chlen = max;
6825
0
        best_size = i;
6826
0
        no_improvement_count = 0;
6827
0
      }
6828
    /* PR 11843: Avoid futile long searches for the best bucket size
6829
       when there are a large number of symbols.  */
6830
0
    else if (++no_improvement_count == 100)
6831
0
      break;
6832
0
  }
6833
6834
0
      free (counts);
6835
0
    }
6836
0
  else
6837
0
    {
6838
0
      for (i = 0; elf_buckets[i] != 0; i++)
6839
0
  {
6840
0
    best_size = elf_buckets[i];
6841
0
    if (nsyms < elf_buckets[i + 1])
6842
0
      break;
6843
0
  }
6844
0
      if (gnu_hash && best_size < 2)
6845
0
  best_size = 2;
6846
0
    }
6847
6848
0
  return best_size;
6849
0
}
6850
6851
/* Size any SHT_GROUP section for ld -r.  */
6852
6853
bool
6854
bfd_elf_size_group_sections (struct bfd_link_info *info)
6855
0
{
6856
0
  bfd *ibfd;
6857
0
  asection *s;
6858
6859
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6860
0
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6861
0
  && (s = ibfd->sections) != NULL
6862
0
  && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6863
0
  && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6864
0
      return false;
6865
0
  return true;
6866
0
}
6867
6868
/* Set a default stack segment size.  The value in INFO wins.  If it
6869
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6870
   undefined it is initialized.  */
6871
6872
bool
6873
bfd_elf_stack_segment_size (struct bfd_link_info *info,
6874
          const char *legacy_symbol,
6875
          bfd_vma default_size)
6876
0
{
6877
0
  struct elf_link_hash_entry *h = NULL;
6878
6879
  /* Look for legacy symbol.  */
6880
0
  if (legacy_symbol)
6881
0
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6882
0
            false, false, false);
6883
0
  if (h && (h->root.type == bfd_link_hash_defined
6884
0
      || h->root.type == bfd_link_hash_defweak)
6885
0
      && h->def_regular
6886
0
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6887
0
    {
6888
      /* The symbol has no type if specified on the command line.  */
6889
0
      h->type = STT_OBJECT;
6890
0
      if (info->stacksize)
6891
  /* xgettext:c-format */
6892
0
  _bfd_error_handler (_("%pB: stack size specified and %s set"),
6893
0
          info->output_bfd, legacy_symbol);
6894
0
      else if (h->root.u.def.section != bfd_abs_section_ptr)
6895
  /* xgettext:c-format */
6896
0
  _bfd_error_handler (_("%pB: %s not absolute"),
6897
0
          info->output_bfd, legacy_symbol);
6898
0
      else
6899
0
  info->stacksize = h->root.u.def.value;
6900
0
    }
6901
6902
0
  if (!info->stacksize)
6903
    /* If the user didn't set a size, or explicitly inhibit the
6904
       size, set it now.  */
6905
0
    info->stacksize = default_size;
6906
6907
  /* Provide the legacy symbol, if it is referenced.  */
6908
0
  if (h && (h->root.type == bfd_link_hash_undefined
6909
0
      || h->root.type == bfd_link_hash_undefweak))
6910
0
    {
6911
0
      struct bfd_link_hash_entry *bh = NULL;
6912
6913
0
      if (!(_bfd_generic_link_add_one_symbol
6914
0
      (info, info->output_bfd, legacy_symbol,
6915
0
       BSF_GLOBAL, bfd_abs_section_ptr,
6916
0
       info->stacksize >= 0 ? info->stacksize : 0,
6917
0
       NULL, false, get_elf_backend_data (info->output_bfd)->collect,
6918
0
       &bh)))
6919
0
  return false;
6920
6921
0
      h = (struct elf_link_hash_entry *) bh;
6922
0
      h->def_regular = 1;
6923
0
      h->type = STT_OBJECT;
6924
0
    }
6925
6926
0
  return true;
6927
0
}
6928
6929
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6930
6931
struct elf_gc_sweep_symbol_info
6932
{
6933
  struct bfd_link_info *info;
6934
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6935
           bool);
6936
};
6937
6938
static bool
6939
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6940
0
{
6941
0
  if (!h->mark
6942
0
      && (((h->root.type == bfd_link_hash_defined
6943
0
      || h->root.type == bfd_link_hash_defweak)
6944
0
     && !((h->def_regular || ELF_COMMON_DEF_P (h))
6945
0
    && h->root.u.def.section->gc_mark))
6946
0
    || h->root.type == bfd_link_hash_undefined
6947
0
    || h->root.type == bfd_link_hash_undefweak))
6948
0
    {
6949
0
      struct elf_gc_sweep_symbol_info *inf = data;
6950
6951
0
      (*inf->hide_symbol) (inf->info, h, true);
6952
0
      h->def_regular = 0;
6953
0
      h->ref_regular = 0;
6954
0
      h->ref_regular_nonweak = 0;
6955
0
    }
6956
6957
0
  return true;
6958
0
}
6959
6960
/* Set up the sizes and contents of the ELF dynamic sections.  This is
6961
   called by the ELF linker emulation before_allocation routine.  We
6962
   must set the sizes of the sections before the linker sets the
6963
   addresses of the various sections.  */
6964
6965
bool
6966
bfd_elf_size_dynamic_sections (struct bfd_link_info *info,
6967
             const char *soname,
6968
             const char *rpath,
6969
             const char *filter_shlib,
6970
             const char *audit,
6971
             const char *depaudit,
6972
             const char * const *auxiliary_filters,
6973
             asection **sinterpptr)
6974
0
{
6975
0
  bfd *dynobj;
6976
0
  elf_backend_data *obed;
6977
6978
0
  *sinterpptr = NULL;
6979
6980
0
  if (!is_elf_hash_table (info->hash))
6981
0
    return true;
6982
6983
  /* Any syms created from now on start with -1 in
6984
     got.refcount/offset and plt.refcount/offset.  */
6985
0
  elf_hash_table (info)->init_got_refcount
6986
0
    = elf_hash_table (info)->init_got_offset;
6987
0
  elf_hash_table (info)->init_plt_refcount
6988
0
    = elf_hash_table (info)->init_plt_offset;
6989
6990
0
  obed = get_elf_backend_data (info->output_bfd);
6991
6992
  /* The backend may have to create some sections regardless of whether
6993
     we're dynamic or not.  */
6994
0
  if (obed->elf_backend_early_size_sections
6995
0
      && !obed->elf_backend_early_size_sections (info))
6996
0
    return false;
6997
6998
0
  dynobj = elf_hash_table (info)->dynobj;
6999
7000
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7001
0
    {
7002
0
      struct bfd_elf_version_tree *verdefs;
7003
0
      struct elf_info_failed asvinfo;
7004
0
      struct bfd_elf_version_tree *t;
7005
0
      struct bfd_elf_version_expr *d;
7006
0
      asection *s;
7007
0
      size_t soname_indx;
7008
7009
      /* If we are supposed to export all symbols into the dynamic symbol
7010
   table (this is not the normal case), then do so.  */
7011
0
      if (info->export_dynamic
7012
0
    || (bfd_link_executable (info) && info->dynamic))
7013
0
  {
7014
0
    struct elf_info_failed eif;
7015
7016
0
    eif.info = info;
7017
0
    eif.failed = false;
7018
0
    elf_link_hash_traverse (elf_hash_table (info),
7019
0
          _bfd_elf_export_symbol,
7020
0
          &eif);
7021
0
    if (eif.failed)
7022
0
      return false;
7023
0
  }
7024
7025
0
      if (soname != NULL)
7026
0
  {
7027
0
    soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7028
0
               soname, true);
7029
0
    if (soname_indx == (size_t) -1
7030
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
7031
0
      return false;
7032
0
  }
7033
0
      else
7034
0
  soname_indx = (size_t) -1;
7035
7036
      /* Make all global versions with definition.  */
7037
0
      for (t = info->version_info; t != NULL; t = t->next)
7038
0
  for (d = t->globals.list; d != NULL; d = d->next)
7039
0
    if (!d->symver && d->literal)
7040
0
      {
7041
0
        const char *verstr, *name;
7042
0
        size_t namelen, verlen, newlen;
7043
0
        char *newname, *p, leading_char;
7044
0
        struct elf_link_hash_entry *newh;
7045
7046
0
        leading_char = bfd_get_symbol_leading_char (info->output_bfd);
7047
0
        name = d->pattern;
7048
0
        namelen = strlen (name) + (leading_char != '\0');
7049
0
        verstr = t->name;
7050
0
        verlen = strlen (verstr);
7051
0
        newlen = namelen + verlen + 3;
7052
7053
0
        newname = bfd_malloc (newlen);
7054
0
        if (newname == NULL)
7055
0
    return false;
7056
0
        newname[0] = leading_char;
7057
0
        memcpy (newname + (leading_char != '\0'), name, namelen);
7058
7059
        /* Check the hidden versioned definition.  */
7060
0
        p = newname + namelen;
7061
0
        *p++ = ELF_VER_CHR;
7062
0
        memcpy (p, verstr, verlen + 1);
7063
0
        newh = elf_link_hash_lookup (elf_hash_table (info),
7064
0
             newname, false, false,
7065
0
             false);
7066
0
        if (newh == NULL
7067
0
      || (newh->root.type != bfd_link_hash_defined
7068
0
          && newh->root.type != bfd_link_hash_defweak))
7069
0
    {
7070
      /* Check the default versioned definition.  */
7071
0
      *p++ = ELF_VER_CHR;
7072
0
      memcpy (p, verstr, verlen + 1);
7073
0
      newh = elf_link_hash_lookup (elf_hash_table (info),
7074
0
                 newname, false, false,
7075
0
                 false);
7076
0
    }
7077
0
        free (newname);
7078
7079
        /* Mark this version if there is a definition and it is
7080
     not defined in a shared object.  */
7081
0
        if (newh != NULL
7082
0
      && !newh->def_dynamic
7083
0
      && (newh->root.type == bfd_link_hash_defined
7084
0
          || newh->root.type == bfd_link_hash_defweak))
7085
0
    d->symver = 1;
7086
0
      }
7087
7088
      /* Attach all the symbols to their version information.  */
7089
0
      asvinfo.info = info;
7090
0
      asvinfo.failed = false;
7091
7092
0
      elf_link_hash_traverse (elf_hash_table (info),
7093
0
            _bfd_elf_link_assign_sym_version,
7094
0
            &asvinfo);
7095
0
      if (asvinfo.failed)
7096
0
  return false;
7097
7098
0
      if (!info->allow_undefined_version)
7099
0
  {
7100
    /* Check if all global versions have a definition.  */
7101
0
    bool all_defined = true;
7102
0
    for (t = info->version_info; t != NULL; t = t->next)
7103
0
      for (d = t->globals.list; d != NULL; d = d->next)
7104
0
        if (d->literal && !d->symver && !d->script)
7105
0
    {
7106
0
      _bfd_error_handler
7107
0
        (_("%s: undefined version: %s"),
7108
0
         d->pattern, t->name);
7109
0
      all_defined = false;
7110
0
    }
7111
7112
0
    if (!all_defined)
7113
0
      {
7114
0
        bfd_set_error (bfd_error_bad_value);
7115
0
        return false;
7116
0
      }
7117
0
  }
7118
7119
      /* Set up the version definition section.  */
7120
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7121
0
      BFD_ASSERT (s != NULL);
7122
7123
      /* We may have created additional version definitions if we are
7124
   just linking a regular application.  */
7125
0
      verdefs = info->version_info;
7126
7127
      /* Skip anonymous version tag.  */
7128
0
      if (verdefs != NULL && verdefs->vernum == 0)
7129
0
  verdefs = verdefs->next;
7130
7131
0
      if (verdefs == NULL && !info->create_default_symver)
7132
0
  s->flags |= SEC_EXCLUDE;
7133
0
      else
7134
0
  {
7135
0
    unsigned int cdefs;
7136
0
    bfd_size_type size;
7137
0
    bfd_byte *p;
7138
0
    Elf_Internal_Verdef def;
7139
0
    Elf_Internal_Verdaux defaux;
7140
0
    struct bfd_link_hash_entry *bh;
7141
0
    struct elf_link_hash_entry *h;
7142
0
    const char *name;
7143
7144
0
    cdefs = 0;
7145
0
    size = 0;
7146
7147
    /* Make space for the base version.  */
7148
0
    size += sizeof (Elf_External_Verdef);
7149
0
    size += sizeof (Elf_External_Verdaux);
7150
0
    ++cdefs;
7151
7152
    /* Make space for the default version.  */
7153
0
    if (info->create_default_symver)
7154
0
      {
7155
0
        size += sizeof (Elf_External_Verdef);
7156
0
        ++cdefs;
7157
0
      }
7158
7159
0
    for (t = verdefs; t != NULL; t = t->next)
7160
0
      {
7161
0
        struct bfd_elf_version_deps *n;
7162
7163
        /* Don't emit base version twice.  */
7164
0
        if (t->vernum == 0)
7165
0
    continue;
7166
7167
0
        size += sizeof (Elf_External_Verdef);
7168
0
        size += sizeof (Elf_External_Verdaux);
7169
0
        ++cdefs;
7170
7171
0
        for (n = t->deps; n != NULL; n = n->next)
7172
0
    size += sizeof (Elf_External_Verdaux);
7173
0
      }
7174
7175
0
    s->size = size;
7176
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7177
0
    if (s->contents == NULL && s->size != 0)
7178
0
      return false;
7179
0
    s->alloced = 1;
7180
7181
    /* Fill in the version definition section.  */
7182
7183
0
    p = s->contents;
7184
7185
0
    def.vd_version = VER_DEF_CURRENT;
7186
0
    def.vd_flags = VER_FLG_BASE;
7187
0
    def.vd_ndx = 1;
7188
0
    def.vd_cnt = 1;
7189
0
    if (info->create_default_symver)
7190
0
      {
7191
0
        def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7192
0
        def.vd_next = sizeof (Elf_External_Verdef);
7193
0
      }
7194
0
    else
7195
0
      {
7196
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7197
0
        def.vd_next = (sizeof (Elf_External_Verdef)
7198
0
           + sizeof (Elf_External_Verdaux));
7199
0
      }
7200
7201
0
    if (soname_indx != (size_t) -1)
7202
0
      {
7203
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7204
0
              soname_indx);
7205
0
        def.vd_hash = bfd_elf_hash (soname);
7206
0
        defaux.vda_name = soname_indx;
7207
0
        name = soname;
7208
0
      }
7209
0
    else
7210
0
      {
7211
0
        size_t indx;
7212
7213
0
        name = lbasename (bfd_get_filename (info->output_bfd));
7214
0
        def.vd_hash = bfd_elf_hash (name);
7215
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7216
0
            name, false);
7217
0
        if (indx == (size_t) -1)
7218
0
    return false;
7219
0
        defaux.vda_name = indx;
7220
0
      }
7221
0
    defaux.vda_next = 0;
7222
7223
0
    _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7224
0
            (Elf_External_Verdef *) p);
7225
0
    p += sizeof (Elf_External_Verdef);
7226
0
    if (info->create_default_symver)
7227
0
      {
7228
        /* Add a symbol representing this version.  */
7229
0
        bh = NULL;
7230
0
        if (! (_bfd_generic_link_add_one_symbol
7231
0
         (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7232
0
          0, NULL, false,
7233
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7234
0
    return false;
7235
0
        h = (struct elf_link_hash_entry *) bh;
7236
0
        h->non_elf = 0;
7237
0
        h->def_regular = 1;
7238
0
        h->type = STT_OBJECT;
7239
0
        h->verinfo.vertree = NULL;
7240
7241
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7242
0
    return false;
7243
7244
        /* Create a duplicate of the base version with the same
7245
     aux block, but different flags.  */
7246
0
        def.vd_flags = 0;
7247
0
        def.vd_ndx = 2;
7248
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7249
0
        if (verdefs)
7250
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7251
0
             + sizeof (Elf_External_Verdaux));
7252
0
        else
7253
0
    def.vd_next = 0;
7254
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7255
0
          (Elf_External_Verdef *) p);
7256
0
        p += sizeof (Elf_External_Verdef);
7257
0
      }
7258
0
    _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7259
0
             (Elf_External_Verdaux *) p);
7260
0
    p += sizeof (Elf_External_Verdaux);
7261
7262
0
    for (t = verdefs; t != NULL; t = t->next)
7263
0
      {
7264
0
        unsigned int cdeps;
7265
0
        struct bfd_elf_version_deps *n;
7266
7267
        /* Don't emit the base version twice.  */
7268
0
        if (t->vernum == 0)
7269
0
    continue;
7270
7271
0
        cdeps = 0;
7272
0
        for (n = t->deps; n != NULL; n = n->next)
7273
0
    ++cdeps;
7274
7275
        /* Add a symbol representing this version.  */
7276
0
        bh = NULL;
7277
0
        if (! (_bfd_generic_link_add_one_symbol
7278
0
         (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7279
0
          0, NULL, false,
7280
0
          get_elf_backend_data (dynobj)->collect, &bh)))
7281
0
    return false;
7282
0
        h = (struct elf_link_hash_entry *) bh;
7283
0
        h->non_elf = 0;
7284
0
        h->def_regular = 1;
7285
0
        h->type = STT_OBJECT;
7286
0
        h->verinfo.vertree = t;
7287
7288
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
7289
0
    return false;
7290
7291
0
        def.vd_version = VER_DEF_CURRENT;
7292
0
        def.vd_flags = 0;
7293
0
        if (t->globals.list == NULL
7294
0
      && t->locals.list == NULL
7295
0
      && ! t->used)
7296
0
    def.vd_flags |= VER_FLG_WEAK;
7297
0
        def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7298
0
        def.vd_cnt = cdeps + 1;
7299
0
        def.vd_hash = bfd_elf_hash (t->name);
7300
0
        def.vd_aux = sizeof (Elf_External_Verdef);
7301
0
        def.vd_next = 0;
7302
7303
        /* If a basever node is next, it *must* be the last node in
7304
     the chain, otherwise Verdef construction breaks.  */
7305
0
        if (t->next != NULL && t->next->vernum == 0)
7306
0
    BFD_ASSERT (t->next->next == NULL);
7307
7308
0
        if (t->next != NULL && t->next->vernum != 0)
7309
0
    def.vd_next = (sizeof (Elf_External_Verdef)
7310
0
             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7311
7312
0
        _bfd_elf_swap_verdef_out (info->output_bfd, &def,
7313
0
          (Elf_External_Verdef *) p);
7314
0
        p += sizeof (Elf_External_Verdef);
7315
7316
0
        defaux.vda_name = h->dynstr_index;
7317
0
        _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7318
0
              h->dynstr_index);
7319
0
        defaux.vda_next = 0;
7320
0
        if (t->deps != NULL)
7321
0
    defaux.vda_next = sizeof (Elf_External_Verdaux);
7322
0
        t->name_indx = defaux.vda_name;
7323
7324
0
        _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7325
0
           (Elf_External_Verdaux *) p);
7326
0
        p += sizeof (Elf_External_Verdaux);
7327
7328
0
        for (n = t->deps; n != NULL; n = n->next)
7329
0
    {
7330
0
      if (n->version_needed == NULL)
7331
0
        {
7332
          /* This can happen if there was an error in the
7333
       version script.  */
7334
0
          defaux.vda_name = 0;
7335
0
        }
7336
0
      else
7337
0
        {
7338
0
          defaux.vda_name = n->version_needed->name_indx;
7339
0
          _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7340
0
                defaux.vda_name);
7341
0
        }
7342
0
      if (n->next == NULL)
7343
0
        defaux.vda_next = 0;
7344
0
      else
7345
0
        defaux.vda_next = sizeof (Elf_External_Verdaux);
7346
7347
0
      _bfd_elf_swap_verdaux_out (info->output_bfd, &defaux,
7348
0
               (Elf_External_Verdaux *) p);
7349
0
      p += sizeof (Elf_External_Verdaux);
7350
0
    }
7351
0
      }
7352
7353
0
    elf_tdata (info->output_bfd)->cverdefs = cdefs;
7354
0
  }
7355
0
    }
7356
7357
0
  if (info->gc_sections && obed->can_gc_sections)
7358
0
    {
7359
0
      struct elf_gc_sweep_symbol_info sweep_info;
7360
7361
      /* Remove the symbols that were in the swept sections from the
7362
   dynamic symbol table.  */
7363
0
      sweep_info.info = info;
7364
0
      sweep_info.hide_symbol = obed->elf_backend_hide_symbol;
7365
0
      elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7366
0
            &sweep_info);
7367
0
    }
7368
7369
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7370
0
    {
7371
0
      asection *s;
7372
0
      struct elf_find_verdep_info sinfo;
7373
7374
      /* Work out the size of the version reference section.  */
7375
7376
0
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7377
0
      BFD_ASSERT (s != NULL);
7378
7379
0
      sinfo.info = info;
7380
0
      sinfo.vers = elf_tdata (info->output_bfd)->cverdefs;
7381
0
      if (sinfo.vers == 0)
7382
0
  sinfo.vers = 1;
7383
0
      sinfo.failed = false;
7384
7385
0
      elf_link_hash_traverse (elf_hash_table (info),
7386
0
            _bfd_elf_link_find_version_dependencies,
7387
0
            &sinfo);
7388
0
      if (sinfo.failed)
7389
0
  return false;
7390
7391
0
      obed->elf_backend_add_glibc_version_dependency (&sinfo);
7392
0
      if (sinfo.failed)
7393
0
  return false;
7394
7395
0
      if (elf_tdata (info->output_bfd)->verref == NULL)
7396
0
  s->flags |= SEC_EXCLUDE;
7397
0
      else
7398
0
  {
7399
0
    Elf_Internal_Verneed *vn;
7400
0
    unsigned int size;
7401
0
    unsigned int crefs;
7402
0
    bfd_byte *p;
7403
7404
    /* Build the version dependency section.  */
7405
0
    size = 0;
7406
0
    crefs = 0;
7407
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7408
0
         vn != NULL;
7409
0
         vn = vn->vn_nextref)
7410
0
      {
7411
0
        Elf_Internal_Vernaux *a;
7412
7413
0
        size += sizeof (Elf_External_Verneed);
7414
0
        ++crefs;
7415
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7416
0
    size += sizeof (Elf_External_Vernaux);
7417
0
      }
7418
7419
0
    s->size = size;
7420
0
    s->contents = bfd_alloc (info->output_bfd, s->size);
7421
0
    if (s->contents == NULL)
7422
0
      return false;
7423
0
    s->alloced = 1;
7424
7425
0
    p = s->contents;
7426
0
    for (vn = elf_tdata (info->output_bfd)->verref;
7427
0
         vn != NULL;
7428
0
         vn = vn->vn_nextref)
7429
0
      {
7430
0
        unsigned int caux;
7431
0
        Elf_Internal_Vernaux *a;
7432
0
        size_t indx;
7433
7434
0
        caux = 0;
7435
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7436
0
    ++caux;
7437
7438
0
        vn->vn_version = VER_NEED_CURRENT;
7439
0
        vn->vn_cnt = caux;
7440
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7441
0
            elf_dt_name (vn->vn_bfd) != NULL
7442
0
            ? elf_dt_name (vn->vn_bfd)
7443
0
            : lbasename (bfd_get_filename
7444
0
                   (vn->vn_bfd)),
7445
0
            false);
7446
0
        if (indx == (size_t) -1)
7447
0
    return false;
7448
0
        vn->vn_file = indx;
7449
0
        vn->vn_aux = sizeof (Elf_External_Verneed);
7450
0
        if (vn->vn_nextref == NULL)
7451
0
    vn->vn_next = 0;
7452
0
        else
7453
0
    vn->vn_next = (sizeof (Elf_External_Verneed)
7454
0
             + caux * sizeof (Elf_External_Vernaux));
7455
7456
0
        _bfd_elf_swap_verneed_out (info->output_bfd, vn,
7457
0
           (Elf_External_Verneed *) p);
7458
0
        p += sizeof (Elf_External_Verneed);
7459
7460
0
        for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7461
0
    {
7462
0
      a->vna_hash = bfd_elf_hash (a->vna_nodename);
7463
0
      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7464
0
                a->vna_nodename, false);
7465
0
      if (indx == (size_t) -1)
7466
0
        return false;
7467
0
      a->vna_name = indx;
7468
0
      if (a->vna_nextptr == NULL)
7469
0
        a->vna_next = 0;
7470
0
      else
7471
0
        a->vna_next = sizeof (Elf_External_Vernaux);
7472
7473
0
      _bfd_elf_swap_vernaux_out (info->output_bfd, a,
7474
0
               (Elf_External_Vernaux *) p);
7475
0
      p += sizeof (Elf_External_Vernaux);
7476
0
    }
7477
0
      }
7478
7479
0
    elf_tdata (info->output_bfd)->cverrefs = crefs;
7480
0
  }
7481
0
    }
7482
7483
0
  if (bfd_link_relocatable (info)
7484
0
      && !bfd_elf_size_group_sections (info))
7485
0
    return false;
7486
7487
  /* Determine any GNU_STACK segment requirements, after the backend
7488
     has had a chance to set a default segment size.  */
7489
0
  if (info->execstack)
7490
0
    {
7491
      /* If the user has explicitly requested warnings, then generate one even
7492
   though the choice is the result of another command line option.  */
7493
0
      if (info->warn_execstack == 1)
7494
0
  {
7495
0
    if (info->error_execstack)
7496
0
      {
7497
0
        _bfd_error_handler
7498
0
    (_("\
7499
0
error: creating an executable stack because of -z execstack command line option"));
7500
0
        return false;
7501
0
      }
7502
7503
0
    _bfd_error_handler
7504
0
      (_("\
7505
0
warning: enabling an executable stack because of -z execstack command line option"));
7506
0
  }
7507
7508
0
      elf_stack_flags (info->output_bfd) = PF_R | PF_W | PF_X;
7509
0
    }
7510
0
  else if (info->noexecstack)
7511
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W;
7512
0
  else
7513
0
    {
7514
0
      bfd *inputobj;
7515
0
      asection *notesec = NULL;
7516
0
      bfd *noteobj = NULL;
7517
0
      bfd *emptyobj = NULL;
7518
0
      int exec = 0;
7519
7520
0
      for (inputobj = info->input_bfds;
7521
0
     inputobj;
7522
0
     inputobj = inputobj->link.next)
7523
0
  {
7524
0
    asection *s;
7525
7526
0
    if (inputobj->flags
7527
0
        & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7528
0
      continue;
7529
0
    s = inputobj->sections;
7530
0
    if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7531
0
      continue;
7532
7533
0
    s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7534
0
    if (s)
7535
0
      {
7536
0
        notesec = s;
7537
0
        if (s->flags & SEC_CODE)
7538
0
    {
7539
0
      noteobj = inputobj;
7540
0
      exec = PF_X;
7541
      /* There is no point in scanning the remaining bfds.  */
7542
0
      break;
7543
0
    }
7544
0
      }
7545
0
    else if (obed->default_execstack && info->default_execstack)
7546
0
      {
7547
0
        exec = PF_X;
7548
0
        emptyobj = inputobj;
7549
0
      }
7550
0
  }
7551
7552
0
      if (notesec || info->stacksize > 0)
7553
0
  {
7554
0
    if (exec)
7555
0
      {
7556
0
        if (info->warn_execstack != 0)
7557
0
    {
7558
      /* PR 29072: Because an executable stack is a serious
7559
         security risk, make sure that the user knows that it is
7560
         being enabled despite the fact that it was not requested
7561
         on the command line.  */
7562
0
      if (noteobj)
7563
0
        {
7564
0
          if (info->error_execstack)
7565
0
      {
7566
0
        _bfd_error_handler (_("\
7567
0
error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7568
0
                bfd_get_filename (noteobj));
7569
0
        return false;
7570
0
      }
7571
7572
0
          _bfd_error_handler (_("\
7573
0
warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7574
0
           bfd_get_filename (noteobj));
7575
0
        }
7576
0
      else if (emptyobj)
7577
0
        {
7578
0
          if (info->error_execstack)
7579
0
      {
7580
0
        _bfd_error_handler (_("\
7581
0
error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7582
0
                bfd_get_filename (emptyobj));
7583
0
        return false;
7584
0
      }
7585
7586
0
          _bfd_error_handler (_("\
7587
0
warning: %s: missing .note.GNU-stack section implies executable stack"),
7588
0
            bfd_get_filename (emptyobj));
7589
0
          _bfd_error_handler (_("\
7590
0
NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7591
0
        }
7592
0
    }
7593
0
      }
7594
0
    elf_stack_flags (info->output_bfd) = PF_R | PF_W | exec;
7595
0
  }
7596
7597
0
      if (notesec && exec && bfd_link_relocatable (info)
7598
0
    && notesec->output_section != bfd_abs_section_ptr)
7599
0
  notesec->output_section->flags |= SEC_CODE;
7600
0
    }
7601
7602
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7603
0
    {
7604
0
      struct elf_info_failed eif;
7605
0
      struct elf_link_hash_entry *h;
7606
0
      asection *dynstr;
7607
0
      asection *s;
7608
7609
0
      *sinterpptr = elf_hash_table (info)->interp;
7610
0
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7611
7612
0
      if (info->symbolic)
7613
0
  {
7614
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7615
0
      return false;
7616
0
    info->flags |= DF_SYMBOLIC;
7617
0
  }
7618
7619
0
      if (rpath != NULL)
7620
0
  {
7621
0
    size_t indx;
7622
0
    bfd_vma tag;
7623
7624
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7625
0
              true);
7626
0
    if (indx == (size_t) -1)
7627
0
      return false;
7628
7629
0
    tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7630
0
    if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7631
0
      return false;
7632
0
  }
7633
7634
0
      if (filter_shlib != NULL)
7635
0
  {
7636
0
    size_t indx;
7637
7638
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7639
0
              filter_shlib, true);
7640
0
    if (indx == (size_t) -1
7641
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7642
0
      return false;
7643
0
  }
7644
7645
0
      if (auxiliary_filters != NULL)
7646
0
  {
7647
0
    const char * const *p;
7648
7649
0
    for (p = auxiliary_filters; *p != NULL; p++)
7650
0
      {
7651
0
        size_t indx;
7652
7653
0
        indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7654
0
            *p, true);
7655
0
        if (indx == (size_t) -1
7656
0
      || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7657
0
    return false;
7658
0
      }
7659
0
  }
7660
7661
0
      if (audit != NULL)
7662
0
  {
7663
0
    size_t indx;
7664
7665
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7666
0
              true);
7667
0
    if (indx == (size_t) -1
7668
0
        || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7669
0
      return false;
7670
0
  }
7671
7672
0
      if (depaudit != NULL)
7673
0
  {
7674
0
    size_t indx;
7675
7676
0
    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7677
0
              true);
7678
0
    if (indx == (size_t) -1
7679
0
        || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7680
0
      return false;
7681
0
  }
7682
7683
0
      eif.info = info;
7684
0
      eif.failed = false;
7685
7686
      /* Find all symbols which were defined in a dynamic object and make
7687
   the backend pick a reasonable value for them.  */
7688
0
      elf_link_hash_traverse (elf_hash_table (info),
7689
0
            _bfd_elf_adjust_dynamic_symbol,
7690
0
            &eif);
7691
0
      if (eif.failed)
7692
0
  return false;
7693
7694
      /* Add some entries to the .dynamic section.  We fill in some of the
7695
   values later, in bfd_elf_final_link, but we must add the entries
7696
   now so that we know the final size of the .dynamic section.  */
7697
7698
      /* If there are initialization and/or finalization functions to
7699
   call then add the corresponding DT_INIT/DT_FINI entries.  */
7700
0
      h = (info->init_function
7701
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7702
0
           info->init_function, false,
7703
0
           false, false)
7704
0
     : NULL);
7705
0
      if (h != NULL
7706
0
    && (h->ref_regular
7707
0
        || h->def_regular))
7708
0
  {
7709
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7710
0
      return false;
7711
0
  }
7712
0
      h = (info->fini_function
7713
0
     ? elf_link_hash_lookup (elf_hash_table (info),
7714
0
           info->fini_function, false,
7715
0
           false, false)
7716
0
     : NULL);
7717
0
      if (h != NULL
7718
0
    && (h->ref_regular
7719
0
        || h->def_regular))
7720
0
  {
7721
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7722
0
      return false;
7723
0
  }
7724
7725
0
      s = bfd_get_section_by_name (info->output_bfd, ".preinit_array");
7726
0
      if (s != NULL && s->linker_has_input)
7727
0
  {
7728
    /* DT_PREINIT_ARRAY is not allowed in shared library.  */
7729
0
    if (! bfd_link_executable (info))
7730
0
      {
7731
0
        bfd *sub;
7732
0
        asection *o;
7733
7734
0
        for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7735
0
    if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7736
0
        && (o = sub->sections) != NULL
7737
0
        && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7738
0
      for (o = sub->sections; o != NULL; o = o->next)
7739
0
        if (elf_section_data (o)->this_hdr.sh_type
7740
0
      == SHT_PREINIT_ARRAY)
7741
0
          {
7742
0
      _bfd_error_handler
7743
0
        (_("%pB: .preinit_array section is not allowed in DSO"),
7744
0
         sub);
7745
0
      break;
7746
0
          }
7747
7748
0
        bfd_set_error (bfd_error_nonrepresentable_section);
7749
0
        return false;
7750
0
      }
7751
7752
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7753
0
        || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7754
0
      return false;
7755
0
  }
7756
0
      s = bfd_get_section_by_name (info->output_bfd, ".init_array");
7757
0
      if (s != NULL && s->linker_has_input)
7758
0
  {
7759
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7760
0
        || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7761
0
      return false;
7762
0
  }
7763
0
      s = bfd_get_section_by_name (info->output_bfd, ".fini_array");
7764
0
      if (s != NULL && s->linker_has_input)
7765
0
  {
7766
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7767
0
        || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7768
0
      return false;
7769
0
  }
7770
7771
0
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7772
      /* If .dynstr is excluded from the link, we don't want any of
7773
   these tags.  Strictly, we should be checking each section
7774
   individually;  This quick check covers for the case where
7775
   someone does a /DISCARD/ : { *(*) }.  */
7776
0
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7777
0
  {
7778
0
    bfd_size_type strsize;
7779
7780
0
    strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7781
0
    if ((info->emit_hash
7782
0
         && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7783
0
        || (info->emit_gnu_hash
7784
0
      && (obed->record_xhash_symbol == NULL
7785
0
          && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7786
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7787
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7788
0
        || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7789
0
        || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7790
0
                obed->s->sizeof_sym)
7791
0
        || (info->gnu_flags_1
7792
0
      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7793
0
              info->gnu_flags_1)))
7794
0
      return false;
7795
0
  }
7796
0
    }
7797
7798
0
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7799
0
    return false;
7800
7801
  /* The backend must work out the sizes of all the other dynamic
7802
     sections.  */
7803
0
  if (obed->elf_backend_late_size_sections != NULL
7804
0
      && !obed->elf_backend_late_size_sections (info))
7805
0
    return false;
7806
7807
0
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7808
0
    {
7809
0
      if (elf_tdata (info->output_bfd)->cverdefs)
7810
0
  {
7811
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverdefs;
7812
7813
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7814
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7815
0
      return false;
7816
0
  }
7817
7818
0
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7819
0
  {
7820
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7821
0
      return false;
7822
0
  }
7823
0
      else if (info->flags & DF_BIND_NOW)
7824
0
  {
7825
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7826
0
      return false;
7827
0
  }
7828
7829
0
      if (info->flags_1)
7830
0
  {
7831
0
    if (bfd_link_executable (info))
7832
0
      info->flags_1 &= ~ (DF_1_INITFIRST
7833
0
        | DF_1_NODELETE
7834
0
        | DF_1_NOOPEN);
7835
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7836
0
      return false;
7837
0
  }
7838
7839
0
      if (elf_tdata (info->output_bfd)->cverrefs)
7840
0
  {
7841
0
    unsigned int crefs = elf_tdata (info->output_bfd)->cverrefs;
7842
7843
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7844
0
        || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7845
0
      return false;
7846
0
  }
7847
7848
0
      if ((elf_tdata (info->output_bfd)->cverrefs == 0
7849
0
     && elf_tdata (info->output_bfd)->cverdefs == 0)
7850
0
    || _bfd_elf_link_renumber_dynsyms (info, NULL) <= 1)
7851
0
  {
7852
0
    asection *s;
7853
7854
0
    s = bfd_get_linker_section (dynobj, ".gnu.version");
7855
0
    s->flags |= SEC_EXCLUDE;
7856
0
  }
7857
0
    }
7858
0
  return true;
7859
0
}
7860
7861
/* Find the first non-excluded output section.  We'll use its
7862
   section symbol for some emitted relocs.  */
7863
void
7864
_bfd_elf_init_1_index_section (struct bfd_link_info *info)
7865
0
{
7866
0
  asection *s;
7867
0
  asection *found = NULL;
7868
7869
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7870
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7871
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7872
0
      {
7873
0
  found = s;
7874
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7875
0
    break;
7876
0
      }
7877
0
  elf_hash_table (info)->text_index_section = found;
7878
0
}
7879
7880
/* Find two non-excluded output sections, one for code, one for data.
7881
   We'll use their section symbols for some emitted relocs.  */
7882
void
7883
_bfd_elf_init_2_index_sections (struct bfd_link_info *info)
7884
0
{
7885
0
  asection *s;
7886
0
  asection *found = NULL;
7887
7888
  /* Data first, since setting text_index_section changes
7889
     _bfd_elf_omit_section_dynsym_default.  */
7890
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7891
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7892
0
  && !(s->flags & SEC_READONLY)
7893
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7894
0
      {
7895
0
  found = s;
7896
0
  if ((s->flags & SEC_THREAD_LOCAL) == 0)
7897
0
    break;
7898
0
      }
7899
0
  elf_hash_table (info)->data_index_section = found;
7900
7901
0
  for (s = info->output_bfd->sections; s != NULL; s = s->next)
7902
0
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7903
0
  && (s->flags & SEC_READONLY)
7904
0
  && !_bfd_elf_omit_section_dynsym_default (info, s))
7905
0
      {
7906
0
  found = s;
7907
0
  break;
7908
0
      }
7909
0
  elf_hash_table (info)->text_index_section = found;
7910
0
}
7911
7912
void
7913
_bfd_elf_init_0_index_sections (struct bfd_link_info *info ATTRIBUTE_UNUSED)
7914
0
{
7915
0
}
7916
7917
#define GNU_HASH_SECTION_NAME(bed)          \
7918
0
  (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7919
7920
bool
7921
bfd_elf_size_dynsym_hash_dynstr (struct bfd_link_info *info)
7922
0
{
7923
0
  elf_backend_data *obed;
7924
0
  unsigned long section_sym_count;
7925
0
  bfd_size_type dynsymcount = 0;
7926
7927
0
  if (!is_elf_hash_table (info->hash))
7928
0
    return true;
7929
7930
0
  obed = get_elf_backend_data (info->output_bfd);
7931
0
  obed->elf_backend_init_index_section (info);
7932
7933
  /* Assign dynsym indices.  In a shared library we generate a section
7934
     symbol for each output section, which come first.  Next come all
7935
     of the back-end allocated local dynamic syms, followed by the rest
7936
     of the global symbols.
7937
7938
     This is usually not needed for static binaries, however backends
7939
     can request to always do it, e.g. the MIPS backend uses dynamic
7940
     symbol counts to lay out GOT, which will be produced in the
7941
     presence of GOT relocations even in static binaries (holding fixed
7942
     data in that case, to satisfy those relocations).  */
7943
7944
0
  if (elf_hash_table (info)->dynamic_sections_created
7945
0
      || obed->always_renumber_dynsyms)
7946
0
    dynsymcount = _bfd_elf_link_renumber_dynsyms (info, &section_sym_count);
7947
7948
0
  if (elf_hash_table (info)->dynamic_sections_created)
7949
0
    {
7950
0
      bfd *dynobj;
7951
0
      asection *s;
7952
0
      unsigned int dtagcount;
7953
7954
0
      dynobj = elf_hash_table (info)->dynobj;
7955
7956
      /* Work out the size of the symbol version section.  */
7957
0
      s = bfd_get_linker_section (dynobj, ".gnu.version");
7958
0
      BFD_ASSERT (s != NULL);
7959
0
      if ((s->flags & SEC_EXCLUDE) == 0)
7960
0
  {
7961
0
    s->size = dynsymcount * sizeof (Elf_External_Versym);
7962
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
7963
0
    if (s->contents == NULL)
7964
0
      return false;
7965
0
    s->alloced = 1;
7966
7967
0
    if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7968
0
      return false;
7969
0
  }
7970
7971
      /* Set the size of the .dynsym and .hash sections.  We counted
7972
   the number of dynamic symbols in elf_link_add_object_symbols.
7973
   We will build the contents of .dynsym and .hash when we build
7974
   the final symbol table, because until then we do not know the
7975
   correct value to give the symbols.  We built the .dynstr
7976
   section as we went along in elf_link_add_object_symbols.  */
7977
0
      s = elf_hash_table (info)->dynsym;
7978
0
      BFD_ASSERT (s != NULL);
7979
0
      s->size = dynsymcount * obed->s->sizeof_sym;
7980
7981
0
      s->contents = bfd_alloc (info->output_bfd, s->size);
7982
0
      if (s->contents == NULL)
7983
0
  return false;
7984
0
      s->alloced = 1;
7985
7986
      /* The first entry in .dynsym is a dummy symbol.  Clear all the
7987
   section syms, in case we don't output them all.  */
7988
0
      ++section_sym_count;
7989
0
      memset (s->contents, 0, section_sym_count * obed->s->sizeof_sym);
7990
7991
0
      elf_hash_table (info)->bucketcount = 0;
7992
7993
      /* Compute the size of the hashing table.  As a side effect this
7994
   computes the hash values for all the names we export.  */
7995
0
      if (info->emit_hash)
7996
0
  {
7997
0
    unsigned long int *hashcodes;
7998
0
    struct hash_codes_info hashinf;
7999
0
    unsigned long int nsyms;
8000
0
    size_t bucketcount;
8001
0
    size_t hash_entry_size;
8002
8003
    /* Compute the hash values for all exported symbols.  At the same
8004
       time store the values in an array so that we could use them for
8005
       optimizations.  */
8006
0
    hashcodes = bfd_malloc (dynsymcount * sizeof (*hashcodes));
8007
0
    if (hashcodes == NULL)
8008
0
      return false;
8009
0
    hashinf.hashcodes = hashcodes;
8010
0
    hashinf.error = false;
8011
8012
    /* Put all hash values in HASHCODES.  */
8013
0
    elf_link_hash_traverse (elf_hash_table (info),
8014
0
          elf_collect_hash_codes, &hashinf);
8015
0
    if (hashinf.error)
8016
0
      {
8017
0
        free (hashcodes);
8018
0
        return false;
8019
0
      }
8020
8021
0
    nsyms = hashinf.hashcodes - hashcodes;
8022
0
    bucketcount
8023
0
      = compute_bucket_count (info, hashcodes, nsyms, 0);
8024
0
    free (hashcodes);
8025
8026
0
    if (bucketcount == 0 && nsyms > 0)
8027
0
      return false;
8028
8029
0
    elf_hash_table (info)->bucketcount = bucketcount;
8030
8031
0
    s = bfd_get_linker_section (dynobj, ".hash");
8032
0
    BFD_ASSERT (s != NULL);
8033
0
    hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
8034
0
    s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
8035
0
    s->contents = bfd_zalloc (info->output_bfd, s->size);
8036
0
    if (s->contents == NULL)
8037
0
      return false;
8038
0
    s->alloced = 1;
8039
8040
0
    bfd_put (8 * hash_entry_size, info->output_bfd, bucketcount,
8041
0
       s->contents);
8042
0
    bfd_put (8 * hash_entry_size, info->output_bfd, dynsymcount,
8043
0
       s->contents + hash_entry_size);
8044
0
  }
8045
8046
0
      if (info->emit_gnu_hash)
8047
0
  {
8048
0
    size_t i, cnt;
8049
0
    unsigned char *contents;
8050
0
    struct collect_gnu_hash_codes cinfo;
8051
0
    bfd_size_type amt;
8052
0
    size_t bucketcount;
8053
8054
0
    memset (&cinfo, 0, sizeof (cinfo));
8055
8056
    /* Compute the hash values for all exported symbols.  At the same
8057
       time store the values in an array so that we could use them for
8058
       optimizations.  */
8059
0
    amt = dynsymcount * 2 * sizeof (*cinfo.hashcodes);
8060
0
    cinfo.hashcodes = bfd_malloc (amt);
8061
0
    if (cinfo.hashcodes == NULL)
8062
0
      return false;
8063
8064
0
    cinfo.hashval = cinfo.hashcodes + dynsymcount;
8065
0
    cinfo.min_dynindx = -1;
8066
0
    cinfo.output_bfd = info->output_bfd;
8067
0
    cinfo.bed = obed;
8068
8069
    /* Put all hash values in HASHCODES.  */
8070
0
    elf_link_hash_traverse (elf_hash_table (info),
8071
0
          elf_collect_gnu_hash_codes, &cinfo);
8072
0
    if (cinfo.error)
8073
0
      {
8074
0
        free (cinfo.hashcodes);
8075
0
        return false;
8076
0
      }
8077
8078
0
    bucketcount
8079
0
      = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
8080
8081
0
    if (bucketcount == 0)
8082
0
      {
8083
0
        free (cinfo.hashcodes);
8084
0
        return false;
8085
0
      }
8086
8087
0
    s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (obed));
8088
0
    BFD_ASSERT (s != NULL);
8089
8090
0
    if (cinfo.nsyms == 0)
8091
0
      {
8092
        /* Empty .gnu.hash or .MIPS.xhash section is special.  */
8093
0
        BFD_ASSERT (cinfo.min_dynindx == -1);
8094
0
        free (cinfo.hashcodes);
8095
0
        s->size = 5 * 4 + obed->s->arch_size / 8;
8096
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8097
0
        if (contents == NULL)
8098
0
    return false;
8099
0
        s->contents = contents;
8100
0
        s->alloced = 1;
8101
        /* 1 empty bucket.  */
8102
0
        bfd_put_32 (info->output_bfd, 1, contents);
8103
        /* SYMINDX is the number of .dynsym symbols which cannot
8104
     be looked up using .gnu.hash.  That's all of them in
8105
     this case.  */
8106
0
        bfd_put_32 (info->output_bfd, dynsymcount, contents + 4);
8107
        /* Just one word for bitmask.  */
8108
0
        bfd_put_32 (info->output_bfd, 1, contents + 8);
8109
        /* Only hash fn bloom filter.  */
8110
0
        bfd_put_32 (info->output_bfd, 0, contents + 12);
8111
        /* No hashes are valid - empty bitmask.  */
8112
0
        bfd_put (obed->s->arch_size, info->output_bfd, 0, contents + 16);
8113
        /* No hashes in the only bucket.  */
8114
0
        bfd_put_32 (info->output_bfd, 0,
8115
0
        contents + 16 + obed->s->arch_size / 8);
8116
0
      }
8117
0
    else
8118
0
      {
8119
0
        unsigned long int maskwords, maskbitslog2, x;
8120
0
        BFD_ASSERT (cinfo.min_dynindx != -1);
8121
8122
0
        x = cinfo.nsyms;
8123
0
        maskbitslog2 = 1;
8124
0
        while ((x >>= 1) != 0)
8125
0
    ++maskbitslog2;
8126
0
        if (maskbitslog2 < 3)
8127
0
    maskbitslog2 = 5;
8128
0
        else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8129
0
    maskbitslog2 = maskbitslog2 + 3;
8130
0
        else
8131
0
    maskbitslog2 = maskbitslog2 + 2;
8132
0
        if (obed->s->arch_size == 64)
8133
0
    {
8134
0
      if (maskbitslog2 == 5)
8135
0
        maskbitslog2 = 6;
8136
0
      cinfo.shift1 = 6;
8137
0
    }
8138
0
        else
8139
0
    cinfo.shift1 = 5;
8140
0
        cinfo.mask = (1 << cinfo.shift1) - 1;
8141
0
        cinfo.shift2 = maskbitslog2;
8142
0
        cinfo.maskbits = 1 << maskbitslog2;
8143
0
        maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8144
0
        amt = bucketcount * sizeof (unsigned long int) * 2;
8145
0
        amt += maskwords * sizeof (bfd_vma);
8146
0
        cinfo.bitmask = bfd_malloc (amt);
8147
0
        if (cinfo.bitmask == NULL)
8148
0
    {
8149
0
      free (cinfo.hashcodes);
8150
0
      return false;
8151
0
    }
8152
8153
0
        cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8154
0
        cinfo.indx = cinfo.counts + bucketcount;
8155
0
        cinfo.symindx = dynsymcount - cinfo.nsyms;
8156
0
        memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8157
8158
        /* Determine how often each hash bucket is used.  */
8159
0
        memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8160
0
        for (i = 0; i < cinfo.nsyms; ++i)
8161
0
    ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8162
8163
0
        for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8164
0
    if (cinfo.counts[i] != 0)
8165
0
      {
8166
0
        cinfo.indx[i] = cnt;
8167
0
        cnt += cinfo.counts[i];
8168
0
      }
8169
0
        BFD_ASSERT (cnt == dynsymcount);
8170
0
        cinfo.bucketcount = bucketcount;
8171
0
        cinfo.local_indx = cinfo.min_dynindx;
8172
8173
0
        s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8174
0
        s->size += cinfo.maskbits / 8;
8175
0
        if (obed->record_xhash_symbol != NULL)
8176
0
    s->size += cinfo.nsyms * 4;
8177
0
        contents = bfd_zalloc (info->output_bfd, s->size);
8178
0
        if (contents == NULL)
8179
0
    {
8180
0
      free (cinfo.bitmask);
8181
0
      free (cinfo.hashcodes);
8182
0
      return false;
8183
0
    }
8184
8185
0
        s->contents = contents;
8186
0
        s->alloced = 1;
8187
0
        bfd_put_32 (info->output_bfd, bucketcount, contents);
8188
0
        bfd_put_32 (info->output_bfd, cinfo.symindx, contents + 4);
8189
0
        bfd_put_32 (info->output_bfd, maskwords, contents + 8);
8190
0
        bfd_put_32 (info->output_bfd, cinfo.shift2, contents + 12);
8191
0
        contents += 16 + cinfo.maskbits / 8;
8192
8193
0
        for (i = 0; i < bucketcount; ++i)
8194
0
    {
8195
0
      if (cinfo.counts[i] == 0)
8196
0
        bfd_put_32 (info->output_bfd, 0, contents);
8197
0
      else
8198
0
        bfd_put_32 (info->output_bfd, cinfo.indx[i], contents);
8199
0
      contents += 4;
8200
0
    }
8201
8202
0
        cinfo.contents = contents;
8203
8204
0
        cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8205
8206
0
        if (elf_hash_table (info)->has_base_symbols)
8207
0
    {
8208
      /* Output base symbols first in DT_GNU_HASH so that
8209
         they will be picked before non-base symbols at
8210
         run-time.  */
8211
0
      cinfo.base_symbol = true;
8212
8213
      /* Renumber dynamic symbols, if populating .gnu.hash
8214
         section.  If using .MIPS.xhash, populate the
8215
         translation table.  */
8216
0
      elf_link_hash_traverse (elf_hash_table (info),
8217
0
            elf_gnu_hash_process_symidx, &cinfo);
8218
0
    }
8219
8220
        /* Output non-base symbols last.  */
8221
0
        cinfo.base_symbol = false;
8222
0
        elf_link_hash_traverse (elf_hash_table (info),
8223
0
              elf_gnu_hash_process_symidx, &cinfo);
8224
8225
0
        contents = s->contents + 16;
8226
0
        for (i = 0; i < maskwords; ++i)
8227
0
    {
8228
0
      bfd_put (obed->s->arch_size, info->output_bfd,
8229
0
         cinfo.bitmask[i], contents);
8230
0
      contents += obed->s->arch_size / 8;
8231
0
    }
8232
8233
0
        free (cinfo.bitmask);
8234
0
        free (cinfo.hashcodes);
8235
0
      }
8236
0
  }
8237
8238
0
      s = bfd_get_linker_section (dynobj, ".dynstr");
8239
0
      BFD_ASSERT (s != NULL);
8240
8241
0
      elf_finalize_dynstr (info->output_bfd, info);
8242
8243
0
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8244
8245
0
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8246
0
  if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8247
0
    return false;
8248
0
    }
8249
8250
0
  return true;
8251
0
}
8252

8253
/* Create an entry in an ELF linker hash table.  */
8254
8255
struct bfd_hash_entry *
8256
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8257
          struct bfd_hash_table *table,
8258
          const char *string)
8259
0
{
8260
  /* Allocate the structure if it has not already been allocated by a
8261
     subclass.  */
8262
0
  if (entry == NULL)
8263
0
    {
8264
0
      entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8265
0
      if (entry == NULL)
8266
0
  return entry;
8267
0
    }
8268
8269
  /* Call the allocation method of the superclass.  */
8270
0
  entry = _bfd_link_hash_newfunc (entry, table, string);
8271
0
  if (entry != NULL)
8272
0
    {
8273
0
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8274
0
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8275
8276
      /* Set local fields.  */
8277
0
      ret->indx = -1;
8278
0
      ret->dynindx = -1;
8279
0
      ret->got = htab->init_got_refcount;
8280
0
      ret->plt = htab->init_plt_refcount;
8281
0
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8282
0
            - offsetof (struct elf_link_hash_entry, size)));
8283
      /* Assume that we have been called by a non-ELF symbol reader.
8284
   This flag is then reset by the code which reads an ELF input
8285
   file.  This ensures that a symbol created by a non-ELF symbol
8286
   reader will have the flag set correctly.  */
8287
0
      ret->non_elf = 1;
8288
0
    }
8289
8290
0
  return entry;
8291
0
}
8292
8293
/* Copy data from an indirect symbol to its direct symbol, hiding the
8294
   old indirect symbol.  Also used for copying flags to a weakdef.  */
8295
8296
void
8297
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8298
          struct elf_link_hash_entry *dir,
8299
          struct elf_link_hash_entry *ind)
8300
0
{
8301
0
  struct elf_link_hash_table *htab;
8302
8303
0
  if (ind->dyn_relocs != NULL)
8304
0
    {
8305
0
      if (dir->dyn_relocs != NULL)
8306
0
  {
8307
0
    struct elf_dyn_relocs **pp;
8308
0
    struct elf_dyn_relocs *p;
8309
8310
    /* Add reloc counts against the indirect sym to the direct sym
8311
       list.  Merge any entries against the same section.  */
8312
0
    for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8313
0
      {
8314
0
        struct elf_dyn_relocs *q;
8315
8316
0
        for (q = dir->dyn_relocs; q != NULL; q = q->next)
8317
0
    if (q->sec == p->sec)
8318
0
      {
8319
0
        q->pc_count += p->pc_count;
8320
0
        q->count += p->count;
8321
0
        *pp = p->next;
8322
0
        break;
8323
0
      }
8324
0
        if (q == NULL)
8325
0
    pp = &p->next;
8326
0
      }
8327
0
    *pp = dir->dyn_relocs;
8328
0
  }
8329
8330
0
      dir->dyn_relocs = ind->dyn_relocs;
8331
0
      ind->dyn_relocs = NULL;
8332
0
    }
8333
8334
  /* Copy down any references that we may have already seen to the
8335
     symbol which just became indirect.  */
8336
8337
0
  if (dir->versioned != versioned_hidden)
8338
0
    dir->ref_dynamic |= ind->ref_dynamic;
8339
0
  dir->ref_regular |= ind->ref_regular;
8340
0
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8341
0
  dir->non_got_ref |= ind->non_got_ref;
8342
0
  dir->needs_plt |= ind->needs_plt;
8343
0
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
8344
8345
0
  if (ind->root.type != bfd_link_hash_indirect)
8346
0
    return;
8347
8348
  /* Copy over the global and procedure linkage table refcount entries.
8349
     These may have been already set up by a check_relocs routine.  */
8350
0
  htab = elf_hash_table (info);
8351
0
  if (ind->got.refcount > htab->init_got_refcount.refcount)
8352
0
    {
8353
0
      if (dir->got.refcount < 0)
8354
0
  dir->got.refcount = 0;
8355
0
      dir->got.refcount += ind->got.refcount;
8356
0
      ind->got.refcount = htab->init_got_refcount.refcount;
8357
0
    }
8358
8359
0
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8360
0
    {
8361
0
      if (dir->plt.refcount < 0)
8362
0
  dir->plt.refcount = 0;
8363
0
      dir->plt.refcount += ind->plt.refcount;
8364
0
      ind->plt.refcount = htab->init_plt_refcount.refcount;
8365
0
    }
8366
8367
0
  if (ind->dynindx != -1)
8368
0
    {
8369
0
      if (dir->dynindx != -1)
8370
0
  _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8371
0
      dir->dynindx = ind->dynindx;
8372
0
      dir->dynstr_index = ind->dynstr_index;
8373
0
      ind->dynindx = -1;
8374
0
      ind->dynstr_index = 0;
8375
0
    }
8376
0
}
8377
8378
void
8379
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8380
        struct elf_link_hash_entry *h,
8381
        bool force_local)
8382
0
{
8383
  /* STT_GNU_IFUNC symbol must go through PLT.  */
8384
0
  if (h->type != STT_GNU_IFUNC)
8385
0
    {
8386
0
      h->plt = elf_hash_table (info)->init_plt_offset;
8387
0
      h->needs_plt = 0;
8388
0
    }
8389
0
  if (force_local)
8390
0
    {
8391
0
      h->forced_local = 1;
8392
0
      if (h->dynindx != -1)
8393
0
  {
8394
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8395
0
          h->dynstr_index);
8396
0
    h->dynindx = -1;
8397
0
    h->dynstr_index = 0;
8398
0
  }
8399
0
    }
8400
0
}
8401
8402
/* Hide a symbol. */
8403
8404
void
8405
_bfd_elf_link_hide_symbol (bfd *output_bfd,
8406
         struct bfd_link_info *info,
8407
         struct bfd_link_hash_entry *h)
8408
0
{
8409
0
  if (is_elf_hash_table (info->hash))
8410
0
    {
8411
0
      elf_backend_data *obed = get_elf_backend_data (output_bfd);
8412
0
      struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
8413
0
      eh->def_dynamic = 0;
8414
0
      eh->ref_dynamic = 0;
8415
0
      eh->dynamic_def = 0;
8416
0
      obed->elf_backend_hide_symbol (info, eh, true);
8417
0
    }
8418
0
}
8419
8420
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
8421
   caller.  */
8422
8423
bool
8424
_bfd_elf_link_hash_table_init
8425
  (struct elf_link_hash_table *table,
8426
   bfd *obfd,
8427
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8428
              struct bfd_hash_table *,
8429
              const char *),
8430
   unsigned int entsize)
8431
0
{
8432
0
  bool ret;
8433
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
8434
0
  int can_refcount = obed->can_refcount;
8435
8436
0
  table->init_got_refcount.refcount = can_refcount - 1;
8437
0
  table->init_plt_refcount.refcount = can_refcount - 1;
8438
0
  table->init_got_offset.offset = -(bfd_vma) 1;
8439
0
  table->init_plt_offset.offset = -(bfd_vma) 1;
8440
  /* The first dynamic symbol is a dummy.  */
8441
0
  table->dynsymcount = 1;
8442
8443
0
  ret = _bfd_link_hash_table_init (&table->root, obfd, newfunc, entsize);
8444
8445
0
  table->root.type = bfd_link_elf_hash_table;
8446
0
  table->hash_table_id = obed->target_id;
8447
0
  table->target_os = obed->target_os;
8448
0
  table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8449
8450
0
  return ret;
8451
0
}
8452
8453
/* Create an ELF linker hash table.  */
8454
8455
struct bfd_link_hash_table *
8456
_bfd_elf_link_hash_table_create (bfd *obfd)
8457
0
{
8458
0
  struct elf_link_hash_table *ret;
8459
8460
0
  ret = bfd_zmalloc (sizeof (*ret));
8461
0
  if (ret == NULL)
8462
0
    return NULL;
8463
8464
0
  if (! _bfd_elf_link_hash_table_init (ret, obfd, _bfd_elf_link_hash_newfunc,
8465
0
               sizeof (struct elf_link_hash_entry)))
8466
0
    {
8467
0
      free (ret);
8468
0
      return NULL;
8469
0
    }
8470
8471
0
  return &ret->root;
8472
0
}
8473
8474
/* Destroy an ELF linker hash table.  */
8475
8476
void
8477
_bfd_elf_link_hash_table_free (bfd *obfd)
8478
0
{
8479
0
  struct elf_link_hash_table *htab;
8480
8481
0
  htab = (struct elf_link_hash_table *) obfd->link.hash;
8482
0
  if (htab->dynstr != NULL)
8483
0
    _bfd_elf_strtab_free (htab->dynstr);
8484
  /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
8485
0
  if (htab->dynamic != NULL)
8486
0
    {
8487
0
      free (htab->dynamic->contents);
8488
0
      htab->dynamic->contents = NULL;
8489
0
    }
8490
0
  if (htab->first_hash != NULL)
8491
0
    {
8492
0
      bfd_hash_table_free (htab->first_hash);
8493
0
      free (htab->first_hash);
8494
0
    }
8495
0
  if (htab->eh_info.frame_hdr_is_compact)
8496
0
    free (htab->eh_info.u.compact.entries);
8497
0
  else
8498
0
    free (htab->eh_info.u.dwarf.array);
8499
0
  sframe_encoder_free (&htab->sfe_info.sfe_ctx);
8500
0
  _bfd_generic_link_hash_table_free (obfd);
8501
0
}
8502
8503
/* This is a hook for the ELF emulation code in the generic linker to
8504
   tell the backend linker what file name to use for the DT_NEEDED
8505
   entry for a dynamic object.  */
8506
8507
void
8508
bfd_elf_set_dt_needed_name (bfd *obfd, const char *name)
8509
0
{
8510
0
  if (bfd_get_flavour (obfd) == bfd_target_elf_flavour
8511
0
      && bfd_get_format (obfd) == bfd_object)
8512
0
    elf_dt_name (obfd) = name;
8513
0
}
8514
8515
int
8516
bfd_elf_get_dyn_lib_class (bfd *abfd)
8517
0
{
8518
0
  int lib_class;
8519
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8520
0
      && bfd_get_format (abfd) == bfd_object)
8521
0
    lib_class = elf_dyn_lib_class (abfd);
8522
0
  else
8523
0
    lib_class = 0;
8524
0
  return lib_class;
8525
0
}
8526
8527
void
8528
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8529
0
{
8530
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8531
0
      && bfd_get_format (abfd) == bfd_object)
8532
0
    elf_dyn_lib_class (abfd) = lib_class;
8533
0
}
8534
8535
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
8536
   the linker ELF emulation code.  */
8537
8538
struct bfd_link_needed_list *
8539
bfd_elf_get_needed_list (struct bfd_link_info *info)
8540
0
{
8541
0
  if (! is_elf_hash_table (info->hash))
8542
0
    return NULL;
8543
0
  return elf_hash_table (info)->needed;
8544
0
}
8545
8546
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
8547
   hook for the linker ELF emulation code.  */
8548
8549
struct bfd_link_needed_list *
8550
bfd_elf_get_runpath_list (struct bfd_link_info *info)
8551
0
{
8552
0
  if (! is_elf_hash_table (info->hash))
8553
0
    return NULL;
8554
0
  return elf_hash_table (info)->runpath;
8555
0
}
8556
8557
/* Get the name actually used for a dynamic object for a link.  This
8558
   is the SONAME entry if there is one.  Otherwise, it is the string
8559
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
8560
8561
const char *
8562
bfd_elf_get_dt_soname (bfd *abfd)
8563
0
{
8564
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8565
0
      && bfd_get_format (abfd) == bfd_object)
8566
0
    return elf_dt_name (abfd);
8567
0
  return NULL;
8568
0
}
8569
8570
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
8571
   the ELF linker emulation code.  */
8572
8573
bool
8574
bfd_elf_get_bfd_needed_list (bfd *abfd,
8575
           struct bfd_link_needed_list **pneeded)
8576
0
{
8577
0
  asection *s;
8578
0
  bfd_byte *dynbuf = NULL;
8579
0
  unsigned int elfsec;
8580
0
  unsigned long shlink;
8581
0
  bfd_byte *extdyn, *extdynend;
8582
0
  size_t extdynsize;
8583
0
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8584
8585
0
  *pneeded = NULL;
8586
8587
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8588
0
      || bfd_get_format (abfd) != bfd_object)
8589
0
    return true;
8590
8591
0
  s = bfd_get_section_by_name (abfd, ".dynamic");
8592
0
  if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8593
0
    return true;
8594
8595
0
  if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8596
0
    goto error_return;
8597
8598
0
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8599
0
  if (elfsec == SHN_BAD)
8600
0
    goto error_return;
8601
8602
0
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8603
8604
0
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8605
0
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8606
8607
0
  for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8608
0
       (size_t) (extdynend - extdyn) >= extdynsize;
8609
0
       extdyn += extdynsize)
8610
0
    {
8611
0
      Elf_Internal_Dyn dyn;
8612
8613
0
      (*swap_dyn_in) (abfd, extdyn, &dyn);
8614
8615
0
      if (dyn.d_tag == DT_NULL)
8616
0
  break;
8617
8618
0
      if (dyn.d_tag == DT_NEEDED)
8619
0
  {
8620
0
    const char *string;
8621
0
    struct bfd_link_needed_list *l;
8622
0
    unsigned int tagv = dyn.d_un.d_val;
8623
8624
0
    string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8625
0
    if (string == NULL)
8626
0
      goto error_return;
8627
8628
0
    l = bfd_alloc (abfd, sizeof (*l));
8629
0
    if (l == NULL)
8630
0
      goto error_return;
8631
8632
0
    l->by = abfd;
8633
0
    l->name = string;
8634
0
    l->next = *pneeded;
8635
0
    *pneeded = l;
8636
0
  }
8637
0
    }
8638
8639
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8640
8641
0
  return true;
8642
8643
0
 error_return:
8644
0
  _bfd_elf_munmap_section_contents (s, dynbuf);
8645
0
  return false;
8646
0
}
8647
8648
struct elf_symbuf_symbol
8649
{
8650
  unsigned long st_name;  /* Symbol name, index in string tbl */
8651
  unsigned char st_info;  /* Type and binding attributes */
8652
  unsigned char st_other; /* Visibilty, and target specific */
8653
};
8654
8655
struct elf_symbuf_head
8656
{
8657
  struct elf_symbuf_symbol *ssym;
8658
  size_t count;
8659
  unsigned int st_shndx;
8660
};
8661
8662
struct elf_symbol
8663
{
8664
  union
8665
    {
8666
      Elf_Internal_Sym *isym;
8667
      struct elf_symbuf_symbol *ssym;
8668
      void *p;
8669
    } u;
8670
  const char *name;
8671
};
8672
8673
/* Sort references to symbols by ascending section number.  */
8674
8675
static int
8676
elf_sort_elf_symbol (const void *arg1, const void *arg2)
8677
0
{
8678
0
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8679
0
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8680
8681
0
  if (s1->st_shndx != s2->st_shndx)
8682
0
    return s1->st_shndx > s2->st_shndx ? 1 : -1;
8683
  /* Final sort by the address of the sym in the symbuf ensures
8684
     a stable sort.  */
8685
0
  if (s1 != s2)
8686
0
    return s1 > s2 ? 1 : -1;
8687
0
  return 0;
8688
0
}
8689
8690
static int
8691
elf_sym_name_compare (const void *arg1, const void *arg2)
8692
0
{
8693
0
  const struct elf_symbol *s1 = arg1;
8694
0
  const struct elf_symbol *s2 = arg2;
8695
0
  int ret = strcmp (s1->name, s2->name);
8696
0
  if (ret != 0)
8697
0
    return ret;
8698
0
  if (s1->u.p != s2->u.p)
8699
0
    return s1->u.p > s2->u.p ? 1 : -1;
8700
0
  return 0;
8701
0
}
8702
8703
static struct elf_symbuf_head *
8704
elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8705
0
{
8706
0
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
8707
0
  struct elf_symbuf_symbol *ssym;
8708
0
  struct elf_symbuf_head *ssymbuf, *ssymhead;
8709
0
  size_t i, shndx_count, total_size;
8710
8711
0
  indbuf = bfd_malloc (symcount * sizeof (*indbuf));
8712
0
  if (indbuf == NULL)
8713
0
    return NULL;
8714
8715
0
  for (ind = indbuf, i = 0; i < symcount; i++)
8716
0
    if (isymbuf[i].st_shndx != SHN_UNDEF)
8717
0
      *ind++ = &isymbuf[i];
8718
0
  indbufend = ind;
8719
8720
0
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8721
0
   elf_sort_elf_symbol);
8722
8723
0
  shndx_count = 0;
8724
0
  if (indbufend > indbuf)
8725
0
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8726
0
      if (ind[0]->st_shndx != ind[1]->st_shndx)
8727
0
  shndx_count++;
8728
8729
0
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8730
0
    + (indbufend - indbuf) * sizeof (*ssym));
8731
0
  ssymbuf = bfd_malloc (total_size);
8732
0
  if (ssymbuf == NULL)
8733
0
    {
8734
0
      free (indbuf);
8735
0
      return NULL;
8736
0
    }
8737
8738
0
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8739
0
  ssymbuf->ssym = NULL;
8740
0
  ssymbuf->count = shndx_count;
8741
0
  ssymbuf->st_shndx = 0;
8742
0
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8743
0
    {
8744
0
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8745
0
  {
8746
0
    ssymhead++;
8747
0
    ssymhead->ssym = ssym;
8748
0
    ssymhead->count = 0;
8749
0
    ssymhead->st_shndx = (*ind)->st_shndx;
8750
0
  }
8751
0
      ssym->st_name = (*ind)->st_name;
8752
0
      ssym->st_info = (*ind)->st_info;
8753
0
      ssym->st_other = (*ind)->st_other;
8754
0
      ssymhead->count++;
8755
0
    }
8756
0
  BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8757
0
        && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8758
8759
0
  free (indbuf);
8760
0
  return ssymbuf;
8761
0
}
8762
8763
/* Check if 2 sections define the same set of local and global
8764
   symbols.  */
8765
8766
static bool
8767
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8768
           struct bfd_link_info *info)
8769
0
{
8770
0
  bfd *bfd1, *bfd2;
8771
0
  elf_backend_data *bed1, *bed2;
8772
0
  Elf_Internal_Shdr *hdr1, *hdr2;
8773
0
  size_t symcount1, symcount2;
8774
0
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
8775
0
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8776
0
  Elf_Internal_Sym *isym, *isymend;
8777
0
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8778
0
  size_t count1, count2, sec_count1, sec_count2, i;
8779
0
  unsigned int shndx1, shndx2;
8780
0
  bool result;
8781
0
  bool ignore_section_symbol_p;
8782
8783
0
  bfd1 = sec1->owner;
8784
0
  bfd2 = sec2->owner;
8785
8786
  /* Both sections have to be in ELF.  */
8787
0
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8788
0
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8789
0
    return false;
8790
8791
0
  if (elf_section_type (sec1) != elf_section_type (sec2))
8792
0
    return false;
8793
8794
0
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8795
0
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8796
0
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8797
0
    return false;
8798
8799
0
  bed1 = get_elf_backend_data (bfd1);
8800
0
  bed2 = get_elf_backend_data (bfd2);
8801
0
  hdr1 = &elf_symtab_hdr (bfd1);
8802
0
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8803
0
  hdr2 = &elf_symtab_hdr (bfd2);
8804
0
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8805
8806
0
  if (symcount1 == 0 || symcount2 == 0)
8807
0
    return false;
8808
8809
0
  result = false;
8810
0
  isymbuf1 = NULL;
8811
0
  isymbuf2 = NULL;
8812
0
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8813
0
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8814
8815
  /* Ignore section symbols only when matching non-debugging sections
8816
     or linkonce section with comdat section.  */
8817
0
  ignore_section_symbol_p
8818
0
    = ((sec1->flags & SEC_DEBUGGING) == 0
8819
0
       || ((elf_section_flags (sec1) & SHF_GROUP)
8820
0
     != (elf_section_flags (sec2) & SHF_GROUP)));
8821
8822
0
  if (ssymbuf1 == NULL)
8823
0
    {
8824
0
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8825
0
               NULL, NULL, NULL);
8826
0
      if (isymbuf1 == NULL)
8827
0
  goto done;
8828
8829
0
      if (info != NULL && !info->reduce_memory_overheads)
8830
0
  {
8831
0
    ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8832
0
    elf_tdata (bfd1)->symbuf = ssymbuf1;
8833
0
  }
8834
0
    }
8835
8836
0
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8837
0
    {
8838
0
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8839
0
               NULL, NULL, NULL);
8840
0
      if (isymbuf2 == NULL)
8841
0
  goto done;
8842
8843
0
      if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8844
0
  {
8845
0
    ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8846
0
    elf_tdata (bfd2)->symbuf = ssymbuf2;
8847
0
  }
8848
0
    }
8849
8850
0
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8851
0
    {
8852
      /* Optimized faster version.  */
8853
0
      size_t lo, hi, mid;
8854
0
      struct elf_symbol *symp;
8855
0
      struct elf_symbuf_symbol *ssym, *ssymend;
8856
8857
0
      lo = 0;
8858
0
      hi = ssymbuf1->count;
8859
0
      ssymbuf1++;
8860
0
      count1 = 0;
8861
0
      sec_count1 = 0;
8862
0
      while (lo < hi)
8863
0
  {
8864
0
    mid = (lo + hi) / 2;
8865
0
    if (shndx1 < ssymbuf1[mid].st_shndx)
8866
0
      hi = mid;
8867
0
    else if (shndx1 > ssymbuf1[mid].st_shndx)
8868
0
      lo = mid + 1;
8869
0
    else
8870
0
      {
8871
0
        count1 = ssymbuf1[mid].count;
8872
0
        ssymbuf1 += mid;
8873
0
        break;
8874
0
      }
8875
0
  }
8876
0
      if (ignore_section_symbol_p)
8877
0
  {
8878
0
    for (i = 0; i < count1; i++)
8879
0
      if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8880
0
        sec_count1++;
8881
0
    count1 -= sec_count1;
8882
0
  }
8883
8884
0
      lo = 0;
8885
0
      hi = ssymbuf2->count;
8886
0
      ssymbuf2++;
8887
0
      count2 = 0;
8888
0
      sec_count2 = 0;
8889
0
      while (lo < hi)
8890
0
  {
8891
0
    mid = (lo + hi) / 2;
8892
0
    if (shndx2 < ssymbuf2[mid].st_shndx)
8893
0
      hi = mid;
8894
0
    else if (shndx2 > ssymbuf2[mid].st_shndx)
8895
0
      lo = mid + 1;
8896
0
    else
8897
0
      {
8898
0
        count2 = ssymbuf2[mid].count;
8899
0
        ssymbuf2 += mid;
8900
0
        break;
8901
0
      }
8902
0
  }
8903
0
      if (ignore_section_symbol_p)
8904
0
  {
8905
0
    for (i = 0; i < count2; i++)
8906
0
      if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8907
0
        sec_count2++;
8908
0
    count2 -= sec_count2;
8909
0
  }
8910
8911
0
      if (count1 == 0 || count2 == 0 || count1 != count2)
8912
0
  goto done;
8913
8914
0
      symtable1 = bfd_malloc (count1 * sizeof (*symtable1));
8915
0
      symtable2 = bfd_malloc (count2 * sizeof (*symtable2));
8916
0
      if (symtable1 == NULL || symtable2 == NULL)
8917
0
  goto done;
8918
8919
0
      symp = symtable1;
8920
0
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8921
0
     ssym < ssymend; ssym++)
8922
0
  if (sec_count1 == 0
8923
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8924
0
    {
8925
0
      symp->u.ssym = ssym;
8926
0
      symp->name = bfd_elf_string_from_elf_section (bfd1,
8927
0
                hdr1->sh_link,
8928
0
                ssym->st_name);
8929
0
      if (symp->name == NULL)
8930
0
        goto done;
8931
0
      symp++;
8932
0
    }
8933
8934
0
      symp = symtable2;
8935
0
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8936
0
     ssym < ssymend; ssym++)
8937
0
  if (sec_count2 == 0
8938
0
      || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8939
0
    {
8940
0
      symp->u.ssym = ssym;
8941
0
      symp->name = bfd_elf_string_from_elf_section (bfd2,
8942
0
                hdr2->sh_link,
8943
0
                ssym->st_name);
8944
0
      if (symp->name == NULL)
8945
0
        goto done;
8946
0
      symp++;
8947
0
    }
8948
8949
      /* Sort symbol by name.  */
8950
0
      qsort (symtable1, count1, sizeof (struct elf_symbol),
8951
0
       elf_sym_name_compare);
8952
0
      qsort (symtable2, count1, sizeof (struct elf_symbol),
8953
0
       elf_sym_name_compare);
8954
8955
0
      for (i = 0; i < count1; i++)
8956
  /* Two symbols must have the same binding, type and name.  */
8957
0
  if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8958
0
      || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8959
0
      || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8960
0
    goto done;
8961
8962
0
      result = true;
8963
0
      goto done;
8964
0
    }
8965
8966
0
  symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8967
0
  symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8968
0
  if (symtable1 == NULL || symtable2 == NULL)
8969
0
    goto done;
8970
8971
  /* Count definitions in the section.  */
8972
0
  count1 = 0;
8973
0
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8974
0
    if (isym->st_shndx == shndx1
8975
0
  && (!ignore_section_symbol_p
8976
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8977
0
      symtable1[count1++].u.isym = isym;
8978
8979
0
  count2 = 0;
8980
0
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8981
0
    if (isym->st_shndx == shndx2
8982
0
  && (!ignore_section_symbol_p
8983
0
      || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8984
0
      symtable2[count2++].u.isym = isym;
8985
8986
0
  if (count1 == 0 || count2 == 0 || count1 != count2)
8987
0
    goto done;
8988
8989
0
  for (i = 0; i < count1; i++)
8990
0
    {
8991
0
      symtable1[i].name
8992
0
  = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8993
0
             symtable1[i].u.isym->st_name);
8994
0
      if (symtable1[i].name == NULL)
8995
0
  goto done;
8996
0
    }
8997
8998
0
  for (i = 0; i < count2; i++)
8999
0
    {
9000
0
      symtable2[i].name
9001
0
  = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
9002
0
             symtable2[i].u.isym->st_name);
9003
0
      if (symtable2[i].name == NULL)
9004
0
  goto done;
9005
0
    }
9006
9007
  /* Sort symbol by name.  */
9008
0
  qsort (symtable1, count1, sizeof (struct elf_symbol),
9009
0
   elf_sym_name_compare);
9010
0
  qsort (symtable2, count1, sizeof (struct elf_symbol),
9011
0
   elf_sym_name_compare);
9012
9013
0
  for (i = 0; i < count1; i++)
9014
    /* Two symbols must have the same binding, type and name.  */
9015
0
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
9016
0
  || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
9017
0
  || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
9018
0
      goto done;
9019
9020
0
  result = true;
9021
9022
0
 done:
9023
0
  free (symtable1);
9024
0
  free (symtable2);
9025
0
  free (isymbuf1);
9026
0
  free (isymbuf2);
9027
9028
0
  return result;
9029
0
}
9030
9031
/* Return TRUE if 2 section types are compatible.  */
9032
9033
bool
9034
bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
9035
        bfd *bbfd, const asection *bsec)
9036
0
{
9037
0
  if (asec == NULL
9038
0
      || bsec == NULL
9039
0
      || abfd->xvec->flavour != bfd_target_elf_flavour
9040
0
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
9041
0
    return true;
9042
9043
0
  return elf_section_type (asec) == elf_section_type (bsec);
9044
0
}
9045

9046
/* Final phase of ELF linker.  */
9047
9048
/* A structure we use to avoid passing large numbers of arguments.  */
9049
9050
struct elf_final_link_info
9051
{
9052
  /* General link information.  */
9053
  struct bfd_link_info *info;
9054
  /* Output BFD.  */
9055
  bfd *output_bfd;
9056
  /* Symbol string table.  */
9057
  struct elf_strtab_hash *symstrtab;
9058
  /* .hash section.  */
9059
  asection *hash_sec;
9060
  /* symbol version section (.gnu.version).  */
9061
  asection *symver_sec;
9062
  /* Buffer large enough to hold contents of any section.  */
9063
  bfd_byte *contents;
9064
  /* Buffer large enough to hold external relocs of any section.  */
9065
  void *external_relocs;
9066
  /* Buffer large enough to hold internal relocs of any section.  */
9067
  Elf_Internal_Rela *internal_relocs;
9068
  /* Buffer large enough to hold external local symbols of any input
9069
     BFD.  */
9070
  bfd_byte *external_syms;
9071
  /* And a buffer for symbol section indices.  */
9072
  Elf_External_Sym_Shndx *locsym_shndx;
9073
  /* Buffer large enough to hold internal local symbols of any input
9074
     BFD.  */
9075
  Elf_Internal_Sym *internal_syms;
9076
  /* Array large enough to hold a symbol index for each local symbol
9077
     of any input BFD.  */
9078
  long *indices;
9079
  /* Array large enough to hold a section pointer for each local
9080
     symbol of any input BFD.  */
9081
  asection **sections;
9082
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
9083
  Elf_External_Sym_Shndx *symshndxbuf;
9084
  /* Number of STT_FILE syms seen.  */
9085
  size_t filesym_count;
9086
  /* Local symbol hash table.  */
9087
  struct bfd_hash_table local_hash_table;
9088
};
9089
9090
struct local_hash_entry
9091
{
9092
  /* Base hash table entry structure.  */
9093
  struct bfd_hash_entry root;
9094
  /* Size of the local symbol name.  */
9095
  size_t size;
9096
  /* Number of the duplicated local symbol names.  */
9097
  long count;
9098
};
9099
9100
/* Create an entry in the local symbol hash table.  */
9101
9102
static struct bfd_hash_entry *
9103
local_hash_newfunc (struct bfd_hash_entry *entry,
9104
        struct bfd_hash_table *table,
9105
        const char *string)
9106
0
{
9107
9108
  /* Allocate the structure if it has not already been allocated by a
9109
     subclass.  */
9110
0
  if (entry == NULL)
9111
0
    {
9112
0
      entry = bfd_hash_allocate (table, sizeof (struct local_hash_entry));
9113
0
      if (entry == NULL)
9114
0
        return entry;
9115
0
    }
9116
9117
  /* Call the allocation method of the superclass.  */
9118
0
  entry = bfd_hash_newfunc (entry, table, string);
9119
0
  if (entry != NULL)
9120
0
    {
9121
0
      ((struct local_hash_entry *) entry)->count = 0;
9122
0
      ((struct local_hash_entry *) entry)->size = 0;
9123
0
    }
9124
9125
0
  return entry;
9126
0
}
9127
9128
/* This struct is used to pass information to elf_link_output_extsym.  */
9129
9130
struct elf_outext_info
9131
{
9132
  bool failed;
9133
  bool localsyms;
9134
  bool file_sym_done;
9135
  bool base_symbol;
9136
  struct elf_final_link_info *flinfo;
9137
};
9138
9139
9140
/* Support for evaluating a complex relocation.
9141
9142
   Complex relocations are generalized, self-describing relocations.  The
9143
   implementation of them consists of two parts: complex symbols, and the
9144
   relocations themselves.
9145
9146
   The relocations use a reserved elf-wide relocation type code (R_RELC
9147
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
9148
   information (start bit, end bit, word width, etc) into the addend.  This
9149
   information is extracted from CGEN-generated operand tables within gas.
9150
9151
   Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9152
   internal) representing prefix-notation expressions, including but not
9153
   limited to those sorts of expressions normally encoded as addends in the
9154
   addend field.  The symbol mangling format is:
9155
9156
   <node> := <literal>
9157
    |  <unary-operator> ':' <node>
9158
    |  <binary-operator> ':' <node> ':' <node>
9159
    ;
9160
9161
   <literal> := 's' <digits=N> ':' <N character symbol name>
9162
       |  'S' <digits=N> ':' <N character section name>
9163
       |  '#' <hexdigits>
9164
       ;
9165
9166
   <binary-operator> := as in C
9167
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
9168
9169
static bool
9170
set_symbol_value (bfd *bfd_with_globals,
9171
      Elf_Internal_Sym *isymbuf,
9172
      size_t locsymcount,
9173
      size_t num_sym,
9174
      size_t symidx,
9175
      bfd_vma val)
9176
0
{
9177
0
  struct elf_link_hash_entry *h;
9178
0
  size_t extsymoff = locsymcount;
9179
9180
0
  if (symidx < locsymcount)
9181
0
    {
9182
0
      Elf_Internal_Sym *sym;
9183
9184
0
      sym = isymbuf + symidx;
9185
0
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9186
0
  {
9187
    /* It is a local symbol: move it to the
9188
       "absolute" section and give it a value.  */
9189
0
    sym->st_shndx = SHN_ABS;
9190
0
    sym->st_value = val;
9191
0
    return true;
9192
0
  }
9193
0
      if (!elf_bad_symtab (bfd_with_globals))
9194
0
  {
9195
0
    _bfd_error_handler (_("%pB: corrupt symbol table"),
9196
0
            bfd_with_globals);
9197
0
    bfd_set_error (bfd_error_bad_value);
9198
0
    return false;
9199
0
  }
9200
0
      extsymoff = 0;
9201
0
    }
9202
9203
  /* It is a global symbol: set its link type
9204
     to "defined" and give it a value.  */
9205
0
  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
9206
0
            extsymoff, num_sym);
9207
0
  if (h == NULL)
9208
0
    return false;
9209
0
  h->root.type = bfd_link_hash_defined;
9210
0
  h->root.u.def.value = val;
9211
0
  h->root.u.def.section = bfd_abs_section_ptr;
9212
0
  return true;
9213
0
}
9214
9215
static bool
9216
resolve_symbol (const char *name,
9217
    bfd *input_bfd,
9218
    struct elf_final_link_info *flinfo,
9219
    bfd_vma *result,
9220
    Elf_Internal_Sym *isymbuf,
9221
    size_t locsymcount)
9222
0
{
9223
0
  Elf_Internal_Sym *sym;
9224
0
  struct bfd_link_hash_entry *global_entry;
9225
0
  const char *candidate = NULL;
9226
0
  Elf_Internal_Shdr *symtab_hdr;
9227
0
  size_t i;
9228
9229
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9230
9231
0
  for (i = 0; i < locsymcount; ++ i)
9232
0
    {
9233
0
      sym = isymbuf + i;
9234
9235
0
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9236
0
  continue;
9237
9238
0
      candidate = bfd_elf_string_from_elf_section (input_bfd,
9239
0
               symtab_hdr->sh_link,
9240
0
               sym->st_name);
9241
#ifdef DEBUG
9242
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9243
        name, candidate, (unsigned long) sym->st_value);
9244
#endif
9245
0
      if (candidate && strcmp (candidate, name) == 0)
9246
0
  {
9247
0
    asection *sec = flinfo->sections [i];
9248
9249
0
    *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9250
0
    *result += sec->output_offset + sec->output_section->vma;
9251
#ifdef DEBUG
9252
    printf ("Found symbol with value %8.8lx\n",
9253
      (unsigned long) *result);
9254
#endif
9255
0
    return true;
9256
0
  }
9257
0
    }
9258
9259
  /* Hmm, haven't found it yet. perhaps it is a global.  */
9260
0
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9261
0
               false, false, true);
9262
0
  if (!global_entry)
9263
0
    return false;
9264
9265
0
  if (global_entry->type == bfd_link_hash_defined
9266
0
      || global_entry->type == bfd_link_hash_defweak)
9267
0
    {
9268
0
      *result = (global_entry->u.def.value
9269
0
     + global_entry->u.def.section->output_section->vma
9270
0
     + global_entry->u.def.section->output_offset);
9271
#ifdef DEBUG
9272
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9273
        global_entry->root.string, (unsigned long) *result);
9274
#endif
9275
0
      return true;
9276
0
    }
9277
9278
0
  return false;
9279
0
}
9280
9281
/* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
9282
   bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
9283
   names like "foo.end" which is the end address of section "foo".  */
9284
9285
static bool
9286
resolve_section (const char *name,
9287
     asection *sections,
9288
     bfd_vma *result,
9289
     bfd * abfd)
9290
0
{
9291
0
  asection *curr;
9292
0
  unsigned int len;
9293
9294
0
  for (curr = sections; curr; curr = curr->next)
9295
0
    if (strcmp (curr->name, name) == 0)
9296
0
      {
9297
0
  *result = curr->vma;
9298
0
  return true;
9299
0
      }
9300
9301
  /* Hmm. still haven't found it. try pseudo-section names.  */
9302
  /* FIXME: This could be coded more efficiently...  */
9303
0
  for (curr = sections; curr; curr = curr->next)
9304
0
    {
9305
0
      len = strlen (curr->name);
9306
0
      if (len > strlen (name))
9307
0
  continue;
9308
9309
0
      if (strncmp (curr->name, name, len) == 0)
9310
0
  {
9311
0
    if (startswith (name + len, ".end"))
9312
0
      {
9313
0
        *result = (curr->vma
9314
0
       + curr->size / bfd_octets_per_byte (abfd, curr));
9315
0
        return true;
9316
0
      }
9317
9318
    /* Insert more pseudo-section names here, if you like.  */
9319
0
  }
9320
0
    }
9321
9322
0
  return false;
9323
0
}
9324
9325
static void
9326
undefined_reference (const char *reftype, const char *name)
9327
0
{
9328
  /* xgettext:c-format */
9329
0
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9330
0
          reftype, name);
9331
0
  bfd_set_error (bfd_error_bad_value);
9332
0
}
9333
9334
static bool
9335
eval_symbol (bfd_vma *result,
9336
       const char **symp,
9337
       bfd *input_bfd,
9338
       struct elf_final_link_info *flinfo,
9339
       bfd_vma dot,
9340
       Elf_Internal_Sym *isymbuf,
9341
       size_t locsymcount,
9342
       int signed_p)
9343
0
{
9344
0
  size_t len;
9345
0
  size_t symlen;
9346
0
  bfd_vma a;
9347
0
  bfd_vma b;
9348
0
  char symbuf[4096];
9349
0
  const char *sym = *symp;
9350
0
  const char *symend;
9351
0
  bool symbol_is_section = false;
9352
9353
0
  len = strlen (sym);
9354
0
  symend = sym + len;
9355
9356
0
  if (len < 1 || len > sizeof (symbuf))
9357
0
    {
9358
0
      bfd_set_error (bfd_error_invalid_operation);
9359
0
      return false;
9360
0
    }
9361
9362
0
  switch (* sym)
9363
0
    {
9364
0
    case '.':
9365
0
      *result = dot;
9366
0
      *symp = sym + 1;
9367
0
      return true;
9368
9369
0
    case '#':
9370
0
      ++sym;
9371
0
      *result = strtoul (sym, (char **) symp, 16);
9372
0
      return true;
9373
9374
0
    case 'S':
9375
0
      symbol_is_section = true;
9376
      /* Fall through.  */
9377
0
    case 's':
9378
0
      ++sym;
9379
0
      symlen = strtol (sym, (char **) symp, 10);
9380
0
      sym = *symp + 1; /* Skip the trailing ':'.  */
9381
9382
0
      if (symend < sym || symlen + 1 > sizeof (symbuf))
9383
0
  {
9384
0
    bfd_set_error (bfd_error_invalid_operation);
9385
0
    return false;
9386
0
  }
9387
9388
0
      memcpy (symbuf, sym, symlen);
9389
0
      symbuf[symlen] = '\0';
9390
0
      *symp = sym + symlen;
9391
9392
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
9393
   the symbol as a section, or vice-versa. so we're pretty liberal in our
9394
   interpretation here; section means "try section first", not "must be a
9395
   section", and likewise with symbol.  */
9396
9397
0
      if (symbol_is_section)
9398
0
  {
9399
0
    if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9400
0
        && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9401
0
          isymbuf, locsymcount))
9402
0
      {
9403
0
        undefined_reference ("section", symbuf);
9404
0
        return false;
9405
0
      }
9406
0
  }
9407
0
      else
9408
0
  {
9409
0
    if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9410
0
             isymbuf, locsymcount)
9411
0
        && !resolve_section (symbuf, flinfo->output_bfd->sections,
9412
0
           result, input_bfd))
9413
0
      {
9414
0
        undefined_reference ("symbol", symbuf);
9415
0
        return false;
9416
0
      }
9417
0
  }
9418
9419
0
      return true;
9420
9421
      /* All that remains are operators.  */
9422
9423
0
#define UNARY_OP(op)            \
9424
0
  if (startswith (sym, #op))          \
9425
0
    {               \
9426
0
      sym += strlen (#op);          \
9427
0
      if (*sym == ':')           \
9428
0
  ++sym;             \
9429
0
      *symp = sym;            \
9430
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9431
0
      isymbuf, locsymcount, signed_p)) \
9432
0
  return false;           \
9433
0
      if (signed_p)           \
9434
0
  *result = op ((bfd_signed_vma) a);     \
9435
0
      else              \
9436
0
  *result = op a;           \
9437
0
      return true;            \
9438
0
    }
9439
9440
0
#define BINARY_OP_HEAD(op)          \
9441
0
  if (startswith (sym, #op))          \
9442
0
    {               \
9443
0
      sym += strlen (#op);          \
9444
0
      if (*sym == ':')           \
9445
0
  ++sym;             \
9446
0
      *symp = sym;            \
9447
0
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9448
0
      isymbuf, locsymcount, signed_p)) \
9449
0
  return false;           \
9450
0
      ++*symp;              \
9451
0
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9452
0
      isymbuf, locsymcount, signed_p)) \
9453
0
  return false;
9454
0
#define BINARY_OP_TAIL(op)          \
9455
0
      if (signed_p)           \
9456
0
  *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9457
0
      else              \
9458
0
  *result = a op b;         \
9459
0
      return true;            \
9460
0
    }
9461
0
#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9462
9463
0
    default:
9464
0
      UNARY_OP  (0-);
9465
0
      BINARY_OP_HEAD (<<);
9466
0
      if (b >= sizeof (a) * CHAR_BIT)
9467
0
  {
9468
0
    *result = 0;
9469
0
    return true;
9470
0
  }
9471
0
      signed_p = 0;
9472
0
      BINARY_OP_TAIL (<<);
9473
0
      BINARY_OP_HEAD (>>);
9474
0
      if (b >= sizeof (a) * CHAR_BIT)
9475
0
  {
9476
0
    *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9477
0
    return true;
9478
0
  }
9479
0
      BINARY_OP_TAIL (>>);
9480
0
      BINARY_OP (==);
9481
0
      BINARY_OP (!=);
9482
0
      BINARY_OP (<=);
9483
0
      BINARY_OP (>=);
9484
0
      BINARY_OP (&&);
9485
0
      BINARY_OP (||);
9486
0
      UNARY_OP  (~);
9487
0
      UNARY_OP  (!);
9488
0
      BINARY_OP (*);
9489
0
      BINARY_OP_HEAD (/);
9490
0
      if (b == 0)
9491
0
  {
9492
0
    _bfd_error_handler (_("division by zero"));
9493
0
    bfd_set_error (bfd_error_bad_value);
9494
0
    return false;
9495
0
  }
9496
0
      BINARY_OP_TAIL (/);
9497
0
      BINARY_OP_HEAD (%);
9498
0
      if (b == 0)
9499
0
  {
9500
0
    _bfd_error_handler (_("division by zero"));
9501
0
    bfd_set_error (bfd_error_bad_value);
9502
0
    return false;
9503
0
  }
9504
0
      BINARY_OP_TAIL (%);
9505
0
      BINARY_OP (^);
9506
0
      BINARY_OP (|);
9507
0
      BINARY_OP (&);
9508
0
      BINARY_OP (+);
9509
0
      BINARY_OP (-);
9510
0
      BINARY_OP (<);
9511
0
      BINARY_OP (>);
9512
0
#undef UNARY_OP
9513
0
#undef BINARY_OP
9514
0
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9515
0
      bfd_set_error (bfd_error_invalid_operation);
9516
0
      return false;
9517
0
    }
9518
0
}
9519
9520
static void
9521
put_value (bfd_vma size,
9522
     unsigned long chunksz,
9523
     bfd *input_bfd,
9524
     bfd_vma x,
9525
     bfd_byte *location)
9526
0
{
9527
0
  location += (size - chunksz);
9528
9529
0
  for (; size; size -= chunksz, location -= chunksz)
9530
0
    {
9531
0
      switch (chunksz)
9532
0
  {
9533
0
  case 1:
9534
0
    bfd_put_8 (input_bfd, x, location);
9535
0
    x >>= 8;
9536
0
    break;
9537
0
  case 2:
9538
0
    bfd_put_16 (input_bfd, x, location);
9539
0
    x >>= 16;
9540
0
    break;
9541
0
  case 4:
9542
0
    bfd_put_32 (input_bfd, x, location);
9543
    /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
9544
0
    x >>= 16;
9545
0
    x >>= 16;
9546
0
    break;
9547
0
#ifdef BFD64
9548
0
  case 8:
9549
0
    bfd_put_64 (input_bfd, x, location);
9550
    /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
9551
0
    x >>= 32;
9552
0
    x >>= 32;
9553
0
    break;
9554
0
#endif
9555
0
  default:
9556
0
    abort ();
9557
0
    break;
9558
0
  }
9559
0
    }
9560
0
}
9561
9562
static bfd_vma
9563
get_value (bfd_vma size,
9564
     unsigned long chunksz,
9565
     bfd *input_bfd,
9566
     bfd_byte *location)
9567
0
{
9568
0
  int shift;
9569
0
  bfd_vma x = 0;
9570
9571
  /* Sanity checks.  */
9572
0
  BFD_ASSERT (chunksz <= sizeof (x)
9573
0
        && size >= chunksz
9574
0
        && chunksz != 0
9575
0
        && (size % chunksz) == 0
9576
0
        && input_bfd != NULL
9577
0
        && location != NULL);
9578
9579
0
  if (chunksz == sizeof (x))
9580
0
    {
9581
0
      BFD_ASSERT (size == chunksz);
9582
9583
      /* Make sure that we do not perform an undefined shift operation.
9584
   We know that size == chunksz so there will only be one iteration
9585
   of the loop below.  */
9586
0
      shift = 0;
9587
0
    }
9588
0
  else
9589
0
    shift = 8 * chunksz;
9590
9591
0
  for (; size; size -= chunksz, location += chunksz)
9592
0
    {
9593
0
      switch (chunksz)
9594
0
  {
9595
0
  case 1:
9596
0
    x = (x << shift) | bfd_get_8 (input_bfd, location);
9597
0
    break;
9598
0
  case 2:
9599
0
    x = (x << shift) | bfd_get_16 (input_bfd, location);
9600
0
    break;
9601
0
  case 4:
9602
0
    x = (x << shift) | bfd_get_32 (input_bfd, location);
9603
0
    break;
9604
0
#ifdef BFD64
9605
0
  case 8:
9606
0
    x = (x << shift) | bfd_get_64 (input_bfd, location);
9607
0
    break;
9608
0
#endif
9609
0
  default:
9610
0
    abort ();
9611
0
  }
9612
0
    }
9613
0
  return x;
9614
0
}
9615
9616
static void
9617
decode_complex_addend (unsigned long *start,   /* in bits */
9618
           unsigned long *oplen,   /* in bits */
9619
           unsigned long *len,     /* in bits */
9620
           unsigned long *wordsz,  /* in bytes */
9621
           unsigned long *chunksz, /* in bytes */
9622
           unsigned long *lsb0_p,
9623
           unsigned long *signed_p,
9624
           unsigned long *trunc_p,
9625
           unsigned long encoded)
9626
0
{
9627
0
  * start     =  encoded  & 0x3F;
9628
0
  * len       = (encoded >>  6) & 0x3F;
9629
0
  * oplen     = (encoded >> 12) & 0x3F;
9630
0
  * wordsz    = (encoded >> 18) & 0xF;
9631
0
  * chunksz   = (encoded >> 22) & 0xF;
9632
0
  * lsb0_p    = (encoded >> 27) & 1;
9633
0
  * signed_p  = (encoded >> 28) & 1;
9634
0
  * trunc_p   = (encoded >> 29) & 1;
9635
0
}
9636
9637
bfd_reloc_status_type
9638
bfd_elf_perform_complex_relocation (bfd *input_bfd,
9639
            asection *input_section,
9640
            bfd_byte *contents,
9641
            Elf_Internal_Rela *rel,
9642
            bfd_vma relocation)
9643
0
{
9644
0
  bfd_vma shift, x, mask;
9645
0
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9646
0
  bfd_reloc_status_type r;
9647
0
  bfd_size_type octets;
9648
9649
  /*  Perform this reloc, since it is complex.
9650
      (this is not to say that it necessarily refers to a complex
9651
      symbol; merely that it is a self-describing CGEN based reloc.
9652
      i.e. the addend has the complete reloc information (bit start, end,
9653
      word size, etc) encoded within it.).  */
9654
9655
0
  decode_complex_addend (&start, &oplen, &len, &wordsz,
9656
0
       &chunksz, &lsb0_p, &signed_p,
9657
0
       &trunc_p, rel->r_addend);
9658
9659
0
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
9660
9661
0
  if (lsb0_p)
9662
0
    shift = (start + 1) - len;
9663
0
  else
9664
0
    shift = (8 * wordsz) - (start + len);
9665
9666
0
  octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9667
0
  x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9668
9669
#ifdef DEBUG
9670
  printf ("Doing complex reloc: "
9671
    "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9672
    "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9673
    "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9674
    lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9675
    oplen, (unsigned long) x, (unsigned long) mask,
9676
    (unsigned long) relocation);
9677
#endif
9678
9679
0
  r = bfd_reloc_ok;
9680
0
  if (! trunc_p)
9681
    /* Now do an overflow check.  */
9682
0
    r = bfd_check_overflow ((signed_p
9683
0
           ? complain_overflow_signed
9684
0
           : complain_overflow_unsigned),
9685
0
          len, 0, (8 * wordsz),
9686
0
          relocation);
9687
9688
  /* Do the deed.  */
9689
0
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9690
9691
#ifdef DEBUG
9692
  printf ("           relocation: %8.8lx\n"
9693
    "         shifted mask: %8.8lx\n"
9694
    " shifted/masked reloc: %8.8lx\n"
9695
    "               result: %8.8lx\n",
9696
    (unsigned long) relocation, (unsigned long) (mask << shift),
9697
    (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9698
#endif
9699
0
  put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9700
0
  return r;
9701
0
}
9702
9703
/* Functions to read r_offset from external (target order) reloc
9704
   entry.  Faster than bfd_getl32 et al, because we let the compiler
9705
   know the value is aligned.  */
9706
9707
static bfd_vma
9708
ext32l_r_offset (const void *p)
9709
0
{
9710
0
  union aligned32
9711
0
  {
9712
0
    uint32_t v;
9713
0
    unsigned char c[4];
9714
0
  };
9715
0
  const union aligned32 *a
9716
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9717
9718
0
  uint32_t aval = (  (uint32_t) a->c[0]
9719
0
       | (uint32_t) a->c[1] << 8
9720
0
       | (uint32_t) a->c[2] << 16
9721
0
       | (uint32_t) a->c[3] << 24);
9722
0
  return aval;
9723
0
}
9724
9725
static bfd_vma
9726
ext32b_r_offset (const void *p)
9727
0
{
9728
0
  union aligned32
9729
0
  {
9730
0
    uint32_t v;
9731
0
    unsigned char c[4];
9732
0
  };
9733
0
  const union aligned32 *a
9734
0
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9735
9736
0
  uint32_t aval = (  (uint32_t) a->c[0] << 24
9737
0
       | (uint32_t) a->c[1] << 16
9738
0
       | (uint32_t) a->c[2] << 8
9739
0
       | (uint32_t) a->c[3]);
9740
0
  return aval;
9741
0
}
9742
9743
static bfd_vma
9744
ext64l_r_offset (const void *p)
9745
0
{
9746
0
  union aligned64
9747
0
  {
9748
0
    uint64_t v;
9749
0
    unsigned char c[8];
9750
0
  };
9751
0
  const union aligned64 *a
9752
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9753
9754
0
  uint64_t aval = (  (uint64_t) a->c[0]
9755
0
       | (uint64_t) a->c[1] << 8
9756
0
       | (uint64_t) a->c[2] << 16
9757
0
       | (uint64_t) a->c[3] << 24
9758
0
       | (uint64_t) a->c[4] << 32
9759
0
       | (uint64_t) a->c[5] << 40
9760
0
       | (uint64_t) a->c[6] << 48
9761
0
       | (uint64_t) a->c[7] << 56);
9762
0
  return aval;
9763
0
}
9764
9765
static bfd_vma
9766
ext64b_r_offset (const void *p)
9767
0
{
9768
0
  union aligned64
9769
0
  {
9770
0
    uint64_t v;
9771
0
    unsigned char c[8];
9772
0
  };
9773
0
  const union aligned64 *a
9774
0
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9775
9776
0
  uint64_t aval = (  (uint64_t) a->c[0] << 56
9777
0
       | (uint64_t) a->c[1] << 48
9778
0
       | (uint64_t) a->c[2] << 40
9779
0
       | (uint64_t) a->c[3] << 32
9780
0
       | (uint64_t) a->c[4] << 24
9781
0
       | (uint64_t) a->c[5] << 16
9782
0
       | (uint64_t) a->c[6] << 8
9783
0
       | (uint64_t) a->c[7]);
9784
0
  return aval;
9785
0
}
9786
9787
/* When performing a relocatable link, the input relocations are
9788
   preserved.  But, if they reference global symbols, the indices
9789
   referenced must be updated.  Update all the relocations found in
9790
   RELDATA.  */
9791
9792
static bool
9793
elf_link_adjust_relocs (bfd *abfd,
9794
      asection *sec,
9795
      struct bfd_elf_section_reloc_data *reldata,
9796
      bool sort,
9797
      struct bfd_link_info *info)
9798
0
{
9799
0
  unsigned int i;
9800
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9801
0
  bfd_byte *erela;
9802
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9803
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9804
0
  bfd_vma r_type_mask;
9805
0
  int r_sym_shift;
9806
0
  unsigned int count = reldata->count;
9807
0
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
9808
9809
0
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9810
0
    {
9811
0
      swap_in = bed->s->swap_reloc_in;
9812
0
      swap_out = bed->s->swap_reloc_out;
9813
0
    }
9814
0
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9815
0
    {
9816
0
      swap_in = bed->s->swap_reloca_in;
9817
0
      swap_out = bed->s->swap_reloca_out;
9818
0
    }
9819
0
  else
9820
0
    abort ();
9821
9822
0
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9823
0
    abort ();
9824
9825
0
  if (bed->s->arch_size == 32)
9826
0
    {
9827
0
      r_type_mask = 0xff;
9828
0
      r_sym_shift = 8;
9829
0
    }
9830
0
  else
9831
0
    {
9832
0
      r_type_mask = 0xffffffff;
9833
0
      r_sym_shift = 32;
9834
0
    }
9835
9836
0
  erela = reldata->hdr->contents;
9837
0
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9838
0
    {
9839
0
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9840
0
      unsigned int j;
9841
9842
0
      if (*rel_hash == NULL)
9843
0
  continue;
9844
9845
0
      if ((*rel_hash)->indx == -2
9846
0
    && info->gc_sections
9847
0
    && ! info->gc_keep_exported)
9848
0
  {
9849
    /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
9850
0
    _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9851
0
            abfd, sec,
9852
0
            (*rel_hash)->root.root.string);
9853
0
    _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9854
0
            abfd, sec);
9855
0
    bfd_set_error (bfd_error_invalid_operation);
9856
0
    return false;
9857
0
  }
9858
0
      BFD_ASSERT ((*rel_hash)->indx >= 0);
9859
9860
0
      (*swap_in) (abfd, erela, irela);
9861
0
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9862
0
  irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9863
0
         | (irela[j].r_info & r_type_mask));
9864
0
      (*swap_out) (abfd, irela, erela);
9865
0
    }
9866
9867
0
  if (bed->elf_backend_update_relocs)
9868
0
    (*bed->elf_backend_update_relocs) (sec, reldata);
9869
9870
0
  if (sort && count != 0)
9871
0
    {
9872
0
      bfd_vma (*ext_r_off) (const void *);
9873
0
      bfd_vma r_off;
9874
0
      size_t elt_size;
9875
0
      bfd_byte *base, *end, *p, *loc;
9876
0
      bfd_byte *buf = NULL;
9877
9878
0
      if (bed->s->arch_size == 32)
9879
0
  {
9880
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9881
0
      ext_r_off = ext32l_r_offset;
9882
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9883
0
      ext_r_off = ext32b_r_offset;
9884
0
    else
9885
0
      abort ();
9886
0
  }
9887
0
      else
9888
0
  {
9889
0
    if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9890
0
      ext_r_off = ext64l_r_offset;
9891
0
    else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9892
0
      ext_r_off = ext64b_r_offset;
9893
0
    else
9894
0
      abort ();
9895
0
  }
9896
9897
      /*  Must use a stable sort here.  A modified insertion sort,
9898
    since the relocs are mostly sorted already.  */
9899
0
      elt_size = reldata->hdr->sh_entsize;
9900
0
      base = reldata->hdr->contents;
9901
0
      end = base + count * elt_size;
9902
0
      if (elt_size > sizeof (Elf64_External_Rela))
9903
0
  abort ();
9904
9905
      /* Ensure the first element is lowest.  This acts as a sentinel,
9906
   speeding the main loop below.  */
9907
0
      r_off = (*ext_r_off) (base);
9908
0
      for (p = loc = base; (p += elt_size) < end; )
9909
0
  {
9910
0
    bfd_vma r_off2 = (*ext_r_off) (p);
9911
0
    if (r_off > r_off2)
9912
0
      {
9913
0
        r_off = r_off2;
9914
0
        loc = p;
9915
0
      }
9916
0
  }
9917
0
      if (loc != base)
9918
0
  {
9919
    /* Don't just swap *base and *loc as that changes the order
9920
       of the original base[0] and base[1] if they happen to
9921
       have the same r_offset.  */
9922
0
    bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9923
0
    memcpy (onebuf, loc, elt_size);
9924
0
    memmove (base + elt_size, base, loc - base);
9925
0
    memcpy (base, onebuf, elt_size);
9926
0
  }
9927
9928
0
      for (p = base + elt_size; (p += elt_size) < end; )
9929
0
  {
9930
    /* base to p is sorted, *p is next to insert.  */
9931
0
    r_off = (*ext_r_off) (p);
9932
    /* Search the sorted region for location to insert.  */
9933
0
    loc = p - elt_size;
9934
0
    while (r_off < (*ext_r_off) (loc))
9935
0
      loc -= elt_size;
9936
0
    loc += elt_size;
9937
0
    if (loc != p)
9938
0
      {
9939
        /* Chances are there is a run of relocs to insert here,
9940
     from one of more input files.  Files are not always
9941
     linked in order due to the way elf_link_input_bfd is
9942
     called.  See pr17666.  */
9943
0
        size_t sortlen = p - loc;
9944
0
        bfd_vma r_off2 = (*ext_r_off) (loc);
9945
0
        size_t runlen = elt_size;
9946
0
        bfd_vma r_off_runend = r_off;
9947
0
        bfd_vma r_off_runend_next;
9948
0
        size_t buf_size = 96 * 1024;
9949
0
        while (p + runlen < end
9950
0
         && (sortlen <= buf_size
9951
0
       || runlen + elt_size <= buf_size)
9952
         /* run must not break the ordering of base..loc+1 */
9953
0
         && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9954
         /* run must be already sorted */
9955
0
         && r_off_runend_next >= r_off_runend)
9956
0
    {
9957
0
      runlen += elt_size;
9958
0
      r_off_runend = r_off_runend_next;
9959
0
    }
9960
0
        if (buf == NULL)
9961
0
    {
9962
0
      buf = bfd_malloc (buf_size);
9963
0
      if (buf == NULL)
9964
0
        return false;
9965
0
    }
9966
0
        if (runlen < sortlen)
9967
0
    {
9968
0
      memcpy (buf, p, runlen);
9969
0
      memmove (loc + runlen, loc, sortlen);
9970
0
      memcpy (loc, buf, runlen);
9971
0
    }
9972
0
        else
9973
0
    {
9974
0
      memcpy (buf, loc, sortlen);
9975
0
      memmove (loc, p, runlen);
9976
0
      memcpy (loc + runlen, buf, sortlen);
9977
0
    }
9978
0
        p += runlen - elt_size;
9979
0
      }
9980
0
  }
9981
      /* Hashes are no longer valid.  */
9982
0
      free (reldata->hashes);
9983
0
      reldata->hashes = NULL;
9984
0
      free (buf);
9985
0
    }
9986
0
  return true;
9987
0
}
9988
9989
struct elf_link_sort_rela
9990
{
9991
  union {
9992
    bfd_vma offset;
9993
    bfd_vma sym_mask;
9994
  } u;
9995
  enum elf_reloc_type_class type;
9996
  /* We use this as an array of size int_rels_per_ext_rel.  */
9997
  Elf_Internal_Rela rela[1];
9998
};
9999
10000
/* qsort stability here and for cmp2 is only an issue if multiple
10001
   dynamic relocations are emitted at the same address.  But targets
10002
   that apply a series of dynamic relocations each operating on the
10003
   result of the prior relocation can't use -z combreloc as
10004
   implemented anyway.  Such schemes tend to be broken by sorting on
10005
   symbol index.  That leaves dynamic NONE relocs as the only other
10006
   case where ld might emit multiple relocs at the same address, and
10007
   those are only emitted due to target bugs.  */
10008
10009
static int
10010
elf_link_sort_cmp1 (const void *A, const void *B)
10011
0
{
10012
0
  const struct elf_link_sort_rela *a = A;
10013
0
  const struct elf_link_sort_rela *b = B;
10014
0
  int relativea, relativeb;
10015
10016
0
  relativea = a->type == reloc_class_relative;
10017
0
  relativeb = b->type == reloc_class_relative;
10018
10019
0
  if (relativea < relativeb)
10020
0
    return 1;
10021
0
  if (relativea > relativeb)
10022
0
    return -1;
10023
0
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
10024
0
    return -1;
10025
0
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
10026
0
    return 1;
10027
0
  if (a->rela->r_offset < b->rela->r_offset)
10028
0
    return -1;
10029
0
  if (a->rela->r_offset > b->rela->r_offset)
10030
0
    return 1;
10031
0
  return 0;
10032
0
}
10033
10034
static int
10035
elf_link_sort_cmp2 (const void *A, const void *B)
10036
0
{
10037
0
  const struct elf_link_sort_rela *a = A;
10038
0
  const struct elf_link_sort_rela *b = B;
10039
10040
0
  if (a->type < b->type)
10041
0
    return -1;
10042
0
  if (a->type > b->type)
10043
0
    return 1;
10044
0
  if (a->u.offset < b->u.offset)
10045
0
    return -1;
10046
0
  if (a->u.offset > b->u.offset)
10047
0
    return 1;
10048
0
  if (a->rela->r_offset < b->rela->r_offset)
10049
0
    return -1;
10050
0
  if (a->rela->r_offset > b->rela->r_offset)
10051
0
    return 1;
10052
0
  return 0;
10053
0
}
10054
10055
static size_t
10056
elf_link_sort_relocs (bfd *obfd, struct bfd_link_info *info, asection **psec)
10057
0
{
10058
0
  asection *dynamic_relocs;
10059
0
  asection *rela_dyn;
10060
0
  asection *rel_dyn;
10061
0
  bfd_size_type count, size;
10062
0
  size_t i, ret, sort_elt, ext_size;
10063
0
  bfd_byte *sort, *s_non_relative, *p;
10064
0
  struct elf_link_sort_rela *sq;
10065
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
10066
0
  int i2e = obed->s->int_rels_per_ext_rel;
10067
0
  unsigned int opb = bfd_octets_per_byte (obfd, NULL);
10068
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10069
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10070
0
  struct bfd_link_order *lo;
10071
0
  bfd_vma r_sym_mask;
10072
0
  bool use_rela;
10073
10074
  /* Find a dynamic reloc section.  */
10075
0
  rela_dyn = bfd_get_section_by_name (obfd, ".rela.dyn");
10076
0
  rel_dyn  = bfd_get_section_by_name (obfd, ".rel.dyn");
10077
0
  if (rela_dyn != NULL && rela_dyn->size > 0
10078
0
      && rel_dyn != NULL && rel_dyn->size > 0)
10079
0
    {
10080
0
      bool use_rela_initialised = false;
10081
10082
      /* This is just here to stop gcc from complaining.
10083
   Its initialization checking code is not perfect.  */
10084
0
      use_rela = true;
10085
10086
      /* Both sections are present.  Examine the sizes
10087
   of the indirect sections to help us choose.  */
10088
0
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10089
0
  if (lo->type == bfd_indirect_link_order)
10090
0
    {
10091
0
      asection *o = lo->u.indirect.section;
10092
10093
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10094
0
        {
10095
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10096
      /* Section size is divisible by both rel and rela sizes.
10097
         It is of no help to us.  */
10098
0
      ;
10099
0
    else
10100
0
      {
10101
        /* Section size is only divisible by rela.  */
10102
0
        if (use_rela_initialised && !use_rela)
10103
0
          {
10104
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10105
0
                "they are in more than one size"),
10106
0
              obfd);
10107
0
      bfd_set_error (bfd_error_invalid_operation);
10108
0
      return 0;
10109
0
          }
10110
0
        else
10111
0
          {
10112
0
      use_rela = true;
10113
0
      use_rela_initialised = true;
10114
0
          }
10115
0
      }
10116
0
        }
10117
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10118
0
        {
10119
    /* Section size is only divisible by rel.  */
10120
0
    if (use_rela_initialised && use_rela)
10121
0
      {
10122
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10123
0
            "they are in more than one size"),
10124
0
          obfd);
10125
0
        bfd_set_error (bfd_error_invalid_operation);
10126
0
        return 0;
10127
0
      }
10128
0
    else
10129
0
      {
10130
0
        use_rela = false;
10131
0
        use_rela_initialised = true;
10132
0
      }
10133
0
        }
10134
0
      else
10135
0
        {
10136
    /* The section size is not divisible by either -
10137
       something is wrong.  */
10138
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10139
0
              "they are of an unknown size"), obfd);
10140
0
    bfd_set_error (bfd_error_invalid_operation);
10141
0
    return 0;
10142
0
        }
10143
0
    }
10144
10145
0
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10146
0
  if (lo->type == bfd_indirect_link_order)
10147
0
    {
10148
0
      asection *o = lo->u.indirect.section;
10149
10150
0
      if ((o->size % obed->s->sizeof_rela) == 0)
10151
0
        {
10152
0
    if ((o->size % obed->s->sizeof_rel) == 0)
10153
      /* Section size is divisible by both rel and rela sizes.
10154
         It is of no help to us.  */
10155
0
      ;
10156
0
    else
10157
0
      {
10158
        /* Section size is only divisible by rela.  */
10159
0
        if (use_rela_initialised && !use_rela)
10160
0
          {
10161
0
      _bfd_error_handler (_("%pB: unable to sort relocs - "
10162
0
                "they are in more than one size"),
10163
0
              obfd);
10164
0
      bfd_set_error (bfd_error_invalid_operation);
10165
0
      return 0;
10166
0
          }
10167
0
        else
10168
0
          {
10169
0
      use_rela = true;
10170
0
      use_rela_initialised = true;
10171
0
          }
10172
0
      }
10173
0
        }
10174
0
      else if ((o->size % obed->s->sizeof_rel) == 0)
10175
0
        {
10176
    /* Section size is only divisible by rel.  */
10177
0
    if (use_rela_initialised && use_rela)
10178
0
      {
10179
0
        _bfd_error_handler (_("%pB: unable to sort relocs - "
10180
0
            "they are in more than one size"),
10181
0
          obfd);
10182
0
        bfd_set_error (bfd_error_invalid_operation);
10183
0
        return 0;
10184
0
      }
10185
0
    else
10186
0
      {
10187
0
        use_rela = false;
10188
0
        use_rela_initialised = true;
10189
0
      }
10190
0
        }
10191
0
      else
10192
0
        {
10193
    /* The section size is not divisible by either -
10194
       something is wrong.  */
10195
0
    _bfd_error_handler (_("%pB: unable to sort relocs - "
10196
0
              "they are of an unknown size"), obfd);
10197
0
    bfd_set_error (bfd_error_invalid_operation);
10198
0
    return 0;
10199
0
        }
10200
0
    }
10201
10202
0
      if (! use_rela_initialised)
10203
  /* Make a guess.  */
10204
0
  use_rela = true;
10205
0
    }
10206
0
  else if (rela_dyn != NULL && rela_dyn->size > 0)
10207
0
    use_rela = true;
10208
0
  else if (rel_dyn != NULL && rel_dyn->size > 0)
10209
0
    use_rela = false;
10210
0
  else
10211
0
    return 0;
10212
10213
0
  if (use_rela)
10214
0
    {
10215
0
      dynamic_relocs = rela_dyn;
10216
0
      ext_size = obed->s->sizeof_rela;
10217
0
      swap_in = obed->s->swap_reloca_in;
10218
0
      swap_out = obed->s->swap_reloca_out;
10219
0
    }
10220
0
  else
10221
0
    {
10222
0
      dynamic_relocs = rel_dyn;
10223
0
      ext_size = obed->s->sizeof_rel;
10224
0
      swap_in = obed->s->swap_reloc_in;
10225
0
      swap_out = obed->s->swap_reloc_out;
10226
0
    }
10227
10228
0
  size = 0;
10229
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10230
0
    if (lo->type == bfd_indirect_link_order)
10231
0
      size += lo->u.indirect.section->size;
10232
10233
0
  if (size != dynamic_relocs->size)
10234
0
    return 0;
10235
10236
0
  sort_elt = (sizeof (struct elf_link_sort_rela)
10237
0
        + (i2e - 1) * sizeof (Elf_Internal_Rela));
10238
10239
0
  count = dynamic_relocs->size / ext_size;
10240
0
  if (count == 0)
10241
0
    return 0;
10242
0
  sort = bfd_zmalloc (sort_elt * count);
10243
10244
0
  if (sort == NULL)
10245
0
    {
10246
0
      (*info->callbacks->warning)
10247
0
  (info, _("not enough memory to sort relocations"), 0, obfd, 0, 0);
10248
0
      return 0;
10249
0
    }
10250
10251
0
  if (obed->s->arch_size == 32)
10252
0
    r_sym_mask = ~(bfd_vma) 0xff;
10253
0
  else
10254
0
    r_sym_mask = ~(bfd_vma) 0xffffffff;
10255
10256
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10257
0
    if (lo->type == bfd_indirect_link_order)
10258
0
      {
10259
0
  bfd_byte *erel, *erelend;
10260
0
  asection *o = lo->u.indirect.section;
10261
10262
0
  if (o->contents == NULL && o->size != 0)
10263
0
    {
10264
      /* This is a reloc section that is being handled as a normal
10265
         section.  See bfd_section_from_shdr.  We can't combine
10266
         relocs in this case.  */
10267
0
      free (sort);
10268
0
      return 0;
10269
0
    }
10270
0
  erel = o->contents;
10271
0
  erelend = o->contents + o->size;
10272
0
  p = sort + o->output_offset * opb / ext_size * sort_elt;
10273
10274
0
  while (erel < erelend)
10275
0
    {
10276
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10277
10278
0
      (*swap_in) (obfd, erel, s->rela);
10279
0
      s->type = obed->elf_backend_reloc_type_class (info, o, s->rela);
10280
0
      s->u.sym_mask = r_sym_mask;
10281
0
      p += sort_elt;
10282
0
      erel += ext_size;
10283
0
    }
10284
0
      }
10285
10286
0
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10287
10288
0
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
10289
0
    {
10290
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10291
0
      if (s->type != reloc_class_relative)
10292
0
  break;
10293
0
    }
10294
0
  ret = i;
10295
0
  s_non_relative = p;
10296
10297
0
  sq = (struct elf_link_sort_rela *) s_non_relative;
10298
0
  for (; i < count; i++, p += sort_elt)
10299
0
    {
10300
0
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10301
0
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10302
0
  sq = sp;
10303
0
      sp->u.offset = sq->rela->r_offset;
10304
0
    }
10305
10306
0
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10307
10308
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
10309
0
  if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10310
0
    {
10311
      /* We have plt relocs in .rela.dyn.  */
10312
0
      sq = (struct elf_link_sort_rela *) sort;
10313
0
      for (i = 0; i < count; i++)
10314
0
  if (sq[count - i - 1].type != reloc_class_plt)
10315
0
    break;
10316
0
      if (i != 0 && htab->srelplt->size == i * ext_size)
10317
0
  {
10318
0
    struct bfd_link_order **plo;
10319
    /* Put srelplt link_order last.  This is so the output_offset
10320
       set in the next loop is correct for DT_JMPREL.  */
10321
0
    for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10322
0
      if ((*plo)->type == bfd_indirect_link_order
10323
0
    && (*plo)->u.indirect.section == htab->srelplt)
10324
0
        {
10325
0
    lo = *plo;
10326
0
    *plo = lo->next;
10327
0
        }
10328
0
      else
10329
0
        plo = &(*plo)->next;
10330
0
    *plo = lo;
10331
0
    lo->next = NULL;
10332
0
    dynamic_relocs->map_tail.link_order = lo;
10333
0
  }
10334
0
    }
10335
10336
0
  p = sort;
10337
0
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10338
0
    if (lo->type == bfd_indirect_link_order)
10339
0
      {
10340
0
  bfd_byte *erel, *erelend;
10341
0
  asection *o = lo->u.indirect.section;
10342
10343
0
  erel = o->contents;
10344
0
  erelend = o->contents + o->size;
10345
0
  o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10346
0
  while (erel < erelend)
10347
0
    {
10348
0
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10349
0
      (*swap_out) (obfd, s->rela, erel);
10350
0
      p += sort_elt;
10351
0
      erel += ext_size;
10352
0
    }
10353
0
      }
10354
10355
0
  free (sort);
10356
0
  *psec = dynamic_relocs;
10357
0
  return ret;
10358
0
}
10359
10360
/* Add a symbol to the output symbol string table.  */
10361
10362
static int
10363
elf_link_output_symstrtab (void *finf,
10364
         const char *name,
10365
         Elf_Internal_Sym *elfsym,
10366
         asection *input_sec,
10367
         struct elf_link_hash_entry *h)
10368
0
{
10369
0
  struct elf_final_link_info *flinfo = finf;
10370
0
  int (*output_symbol_hook)
10371
0
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10372
0
     struct elf_link_hash_entry *);
10373
0
  struct elf_link_hash_table *hash_table;
10374
0
  elf_backend_data *bed;
10375
0
  bfd_size_type strtabsize;
10376
10377
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10378
10379
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10380
0
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10381
0
  if (output_symbol_hook != NULL)
10382
0
    {
10383
0
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10384
0
      if (ret != 1)
10385
0
  return ret;
10386
0
    }
10387
10388
0
  if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10389
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10390
0
  if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10391
0
    elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10392
10393
0
  if (name == NULL || *name == '\0')
10394
0
    elfsym->st_name = (unsigned long) -1;
10395
0
  else
10396
0
    {
10397
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10398
   to get the final offset for st_name.  */
10399
0
      char *versioned_name = (char *) name;
10400
0
      if (h != NULL)
10401
0
  {
10402
0
    if (h->versioned == versioned && h->def_dynamic)
10403
0
      {
10404
        /* Keep only one '@' for versioned symbols defined in
10405
           shared objects.  */
10406
0
        const char *version = strrchr (name, ELF_VER_CHR);
10407
0
        const char *base_end = strchr (name, ELF_VER_CHR);
10408
0
        if (version != base_end)
10409
0
    {
10410
0
      size_t base_len;
10411
0
      size_t len = strlen (name);
10412
0
      versioned_name = bfd_alloc (flinfo->output_bfd, len);
10413
0
      if (versioned_name == NULL)
10414
0
        return 0;
10415
0
      base_len = base_end - name;
10416
0
      memcpy (versioned_name, name, base_len);
10417
0
      memcpy (versioned_name + base_len, version,
10418
0
        len - base_len);
10419
0
    }
10420
0
      }
10421
0
  }
10422
0
      else if (flinfo->info->unique_symbol
10423
0
         && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10424
0
  {
10425
0
    struct local_hash_entry *lh;
10426
0
    size_t count_len;
10427
0
    size_t base_len;
10428
0
    char buf[30];
10429
0
    switch (ELF_ST_TYPE (elfsym->st_info))
10430
0
      {
10431
0
      case STT_FILE:
10432
0
      case STT_SECTION:
10433
0
        break;
10434
0
      default:
10435
0
        lh = (struct local_hash_entry *) bfd_hash_lookup
10436
0
         (&flinfo->local_hash_table, name, true, false);
10437
0
        if (lh == NULL)
10438
0
    return 0;
10439
        /* Always append ".COUNT" to local symbols to avoid
10440
     potential conflicts with local symbol "XXX.COUNT".  */
10441
0
        sprintf (buf, "%lx", lh->count);
10442
0
        base_len = lh->size;
10443
0
        if (!base_len)
10444
0
    {
10445
0
      base_len = strlen (name);
10446
0
      lh->size = base_len;
10447
0
    }
10448
0
        count_len = strlen (buf);
10449
0
        versioned_name = bfd_alloc (flinfo->output_bfd,
10450
0
            base_len + count_len + 2);
10451
0
        if (versioned_name == NULL)
10452
0
    return 0;
10453
0
        memcpy (versioned_name, name, base_len);
10454
0
        versioned_name[base_len] = '.';
10455
0
        memcpy (versioned_name + base_len + 1, buf,
10456
0
          count_len + 1);
10457
0
        lh->count++;
10458
0
        break;
10459
0
      }
10460
0
  }
10461
0
      elfsym->st_name
10462
0
  = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10463
0
                 versioned_name, false);
10464
0
      if (elfsym->st_name == (unsigned long) -1)
10465
0
  return 0;
10466
0
    }
10467
10468
0
  hash_table = elf_hash_table (flinfo->info);
10469
0
  strtabsize = hash_table->strtabsize;
10470
0
  if (strtabsize <= flinfo->output_bfd->symcount)
10471
0
    {
10472
0
      strtabsize += strtabsize;
10473
0
      hash_table->strtabsize = strtabsize;
10474
0
      strtabsize *= sizeof (*hash_table->strtab);
10475
0
      hash_table->strtab = bfd_realloc (hash_table->strtab, strtabsize);
10476
0
      if (hash_table->strtab == NULL)
10477
0
  return 0;
10478
0
    }
10479
0
  hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10480
0
  hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10481
0
    = flinfo->output_bfd->symcount;
10482
0
  flinfo->output_bfd->symcount += 1;
10483
10484
0
  return 1;
10485
0
}
10486
10487
/* Swap symbols out to the symbol table and flush the output symbols to
10488
   the file.  */
10489
10490
static bool
10491
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10492
0
{
10493
0
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10494
0
  size_t amt;
10495
0
  size_t i;
10496
0
  elf_backend_data *bed;
10497
0
  bfd_byte *symbuf;
10498
0
  Elf_Internal_Shdr *hdr;
10499
0
  file_ptr pos;
10500
0
  bool ret;
10501
10502
0
  if (flinfo->output_bfd->symcount == 0)
10503
0
    return true;
10504
10505
0
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10506
10507
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10508
10509
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10510
0
  symbuf = bfd_malloc (amt);
10511
0
  if (symbuf == NULL)
10512
0
    return false;
10513
10514
0
  if (flinfo->symshndxbuf)
10515
0
    {
10516
0
      amt = sizeof (Elf_External_Sym_Shndx);
10517
0
      amt *= bfd_get_symcount (flinfo->output_bfd);
10518
0
      flinfo->symshndxbuf = bfd_zmalloc (amt);
10519
0
      if (flinfo->symshndxbuf == NULL)
10520
0
  {
10521
0
    free (symbuf);
10522
0
    return false;
10523
0
  }
10524
0
    }
10525
10526
  /* Now swap out the symbols.  */
10527
0
  for (i = 0; i < flinfo->output_bfd->symcount; i++)
10528
0
    {
10529
0
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10530
0
      if (elfsym->sym.st_name == (unsigned long) -1)
10531
0
  elfsym->sym.st_name = 0;
10532
0
      else
10533
0
  elfsym->sym.st_name
10534
0
    = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10535
0
                elfsym->sym.st_name);
10536
10537
      /* Inform the linker of the addition of this symbol.  */
10538
10539
0
      if (flinfo->info->callbacks->ctf_new_symbol)
10540
0
  flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10541
0
             &elfsym->sym);
10542
10543
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10544
0
             ((bfd_byte *) symbuf
10545
0
        + (elfsym->dest_index
10546
0
           * bed->s->sizeof_sym)),
10547
0
             NPTR_ADD (flinfo->symshndxbuf,
10548
0
           elfsym->dest_index));
10549
0
    }
10550
10551
0
  hdr = &elf_symtab_hdr (flinfo->output_bfd);
10552
0
  pos = hdr->sh_offset + hdr->sh_size;
10553
0
  amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10554
0
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10555
0
      && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10556
0
    {
10557
0
      hdr->sh_size += amt;
10558
0
      ret = true;
10559
0
    }
10560
0
  else
10561
0
    ret = false;
10562
10563
0
  free (symbuf);
10564
0
  return ret;
10565
0
}
10566
10567
/* Return TRUE if the dynamic symbol SYM in OBFD is supported.  */
10568
10569
static bool
10570
check_dynsym (bfd *obfd, Elf_Internal_Sym *sym)
10571
0
{
10572
0
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10573
0
      && sym->st_shndx < SHN_LORESERVE)
10574
0
    {
10575
      /* The gABI doesn't support dynamic symbols in output sections
10576
   beyond 64k.  */
10577
0
      _bfd_error_handler
10578
  /* xgettext:c-format */
10579
0
  (_("%pB: too many sections: %d (>= %d)"),
10580
0
   obfd, bfd_count_sections (obfd), SHN_LORESERVE & 0xffff);
10581
0
      bfd_set_error (bfd_error_nonrepresentable_section);
10582
0
      return false;
10583
0
    }
10584
0
  return true;
10585
0
}
10586
10587
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10588
   allowing an unsatisfied unversioned symbol in the DSO to match a
10589
   versioned symbol that would normally require an explicit version.
10590
   We also handle the case that a DSO references a hidden symbol
10591
   which may be satisfied by a versioned symbol in another DSO.  */
10592
10593
static bool
10594
elf_link_check_versioned_symbol (struct bfd_link_info *info,
10595
         elf_backend_data *bed,
10596
         struct elf_link_hash_entry *h)
10597
0
{
10598
0
  bfd *abfd;
10599
0
  struct elf_link_loaded_list *loaded;
10600
10601
0
  if (!is_elf_hash_table (info->hash))
10602
0
    return false;
10603
10604
  /* Check indirect symbol.  */
10605
0
  while (h->root.type == bfd_link_hash_indirect)
10606
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10607
10608
0
  switch (h->root.type)
10609
0
    {
10610
0
    default:
10611
0
      abfd = NULL;
10612
0
      break;
10613
10614
0
    case bfd_link_hash_undefined:
10615
0
    case bfd_link_hash_undefweak:
10616
0
      abfd = h->root.u.undef.abfd;
10617
0
      if (abfd == NULL
10618
0
    || (abfd->flags & DYNAMIC) == 0
10619
0
    || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10620
0
  return false;
10621
0
      break;
10622
10623
0
    case bfd_link_hash_defined:
10624
0
    case bfd_link_hash_defweak:
10625
0
      abfd = h->root.u.def.section->owner;
10626
0
      break;
10627
10628
0
    case bfd_link_hash_common:
10629
0
      abfd = h->root.u.c.p->section->owner;
10630
0
      break;
10631
0
    }
10632
0
  BFD_ASSERT (abfd != NULL);
10633
10634
0
  for (loaded = elf_hash_table (info)->dyn_loaded;
10635
0
       loaded != NULL;
10636
0
       loaded = loaded->next)
10637
0
    {
10638
0
      bfd *input;
10639
0
      Elf_Internal_Shdr *hdr;
10640
0
      size_t symcount;
10641
0
      size_t extsymcount;
10642
0
      size_t extsymoff;
10643
0
      Elf_Internal_Shdr *versymhdr;
10644
0
      Elf_Internal_Sym *isym;
10645
0
      Elf_Internal_Sym *isymend;
10646
0
      Elf_Internal_Sym *isymbuf;
10647
0
      Elf_External_Versym *ever;
10648
0
      Elf_External_Versym *extversym;
10649
10650
0
      input = loaded->abfd;
10651
10652
      /* We check each DSO for a possible hidden versioned definition.  */
10653
0
      if (input == abfd
10654
0
    || elf_dynversym (input) == 0)
10655
0
  continue;
10656
10657
0
      hdr = &elf_tdata (input)->dynsymtab_hdr;
10658
10659
0
      symcount = hdr->sh_size / bed->s->sizeof_sym;
10660
0
      if (elf_bad_symtab (input))
10661
0
  {
10662
0
    extsymcount = symcount;
10663
0
    extsymoff = 0;
10664
0
  }
10665
0
      else
10666
0
  {
10667
0
    extsymcount = symcount - hdr->sh_info;
10668
0
    extsymoff = hdr->sh_info;
10669
0
  }
10670
10671
0
      if (extsymcount == 0)
10672
0
  continue;
10673
10674
0
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10675
0
              NULL, NULL, NULL);
10676
0
      if (isymbuf == NULL)
10677
0
  return false;
10678
10679
      /* Read in any version definitions.  */
10680
0
      versymhdr = &elf_tdata (input)->dynversym_hdr;
10681
0
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10682
0
    || (extversym = _bfd_malloc_and_read (input, versymhdr->sh_size,
10683
0
            versymhdr->sh_size)) == NULL)
10684
0
  {
10685
0
    free (isymbuf);
10686
0
    return false;
10687
0
  }
10688
10689
0
      ever = extversym + extsymoff;
10690
0
      isymend = isymbuf + extsymcount;
10691
0
      for (isym = isymbuf; isym < isymend; isym++, ever++)
10692
0
  {
10693
0
    const char *name;
10694
0
    Elf_Internal_Versym iver;
10695
0
    unsigned short version_index;
10696
10697
0
    if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10698
0
        || isym->st_shndx == SHN_UNDEF)
10699
0
      continue;
10700
10701
0
    name = bfd_elf_string_from_elf_section (input,
10702
0
              hdr->sh_link,
10703
0
              isym->st_name);
10704
0
    if (strcmp (name, h->root.root.string) != 0)
10705
0
      continue;
10706
10707
0
    _bfd_elf_swap_versym_in (input, ever, &iver);
10708
10709
0
    if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10710
0
        && !(h->def_regular
10711
0
       && h->forced_local))
10712
0
      {
10713
        /* If we have a non-hidden versioned sym, then it should
10714
     have provided a definition for the undefined sym unless
10715
     it is defined in a non-shared object and forced local.
10716
         */
10717
0
        abort ();
10718
0
      }
10719
10720
0
    version_index = iver.vs_vers & VERSYM_VERSION;
10721
0
    if (version_index == 1 || version_index == 2)
10722
0
      {
10723
        /* This is the base or first version.  We can use it.  */
10724
0
        free (extversym);
10725
0
        free (isymbuf);
10726
0
        return true;
10727
0
      }
10728
0
  }
10729
10730
0
      free (extversym);
10731
0
      free (isymbuf);
10732
0
    }
10733
10734
0
  return false;
10735
0
}
10736
10737
/* Convert ELF common symbol TYPE.  */
10738
10739
static int
10740
elf_link_convert_common_type (struct bfd_link_info *info, int type)
10741
0
{
10742
  /* Commom symbol can only appear in relocatable link.  */
10743
0
  if (!bfd_link_relocatable (info))
10744
0
    abort ();
10745
0
  switch (info->elf_stt_common)
10746
0
    {
10747
0
    case unchanged:
10748
0
      break;
10749
0
    case elf_stt_common:
10750
0
      type = STT_COMMON;
10751
0
      break;
10752
0
    case no_elf_stt_common:
10753
0
      type = STT_OBJECT;
10754
0
      break;
10755
0
    }
10756
0
  return type;
10757
0
}
10758
10759
/* Add an external symbol to the symbol table.  This is called from
10760
   the hash table traversal routine.  When generating a shared object,
10761
   we go through the symbol table twice.  The first time we output
10762
   anything that might have been forced to local scope in a version
10763
   script.  The second time we output the symbols that are still
10764
   global symbols.  */
10765
10766
static bool
10767
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10768
0
{
10769
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10770
0
  struct elf_outext_info *eoinfo = data;
10771
0
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
10772
0
  bool strip;
10773
0
  Elf_Internal_Sym sym;
10774
0
  asection *input_sec;
10775
0
  elf_backend_data *bed;
10776
0
  long indx;
10777
0
  int ret;
10778
0
  unsigned int type;
10779
10780
  /* Skip if base symbol doesn't match.  */
10781
0
  if (eoinfo->base_symbol != !!h->base_symbol)
10782
0
    return true;
10783
10784
0
  if (h->root.type == bfd_link_hash_warning)
10785
0
    {
10786
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10787
0
      if (h->root.type == bfd_link_hash_new)
10788
0
  return true;
10789
0
    }
10790
10791
  /* Decide whether to output this symbol in this pass.  */
10792
0
  if (eoinfo->localsyms)
10793
0
    {
10794
0
      if (!h->forced_local)
10795
0
  return true;
10796
0
    }
10797
0
  else
10798
0
    {
10799
0
      if (h->forced_local)
10800
0
  return true;
10801
0
    }
10802
10803
0
  bed = get_elf_backend_data (flinfo->output_bfd);
10804
10805
0
  if (h->root.type == bfd_link_hash_undefined)
10806
0
    {
10807
      /* If we have an undefined symbol reference here then it must have
10808
   come from a shared library that is being linked in.  (Undefined
10809
   references in regular files have already been handled unless
10810
   they are in unreferenced sections which are removed by garbage
10811
   collection).  */
10812
0
      bool ignore_undef = false;
10813
10814
      /* Some symbols may be special in that the fact that they're
10815
   undefined can be safely ignored - let backend determine that.  */
10816
0
      if (bed->elf_backend_ignore_undef_symbol)
10817
0
  ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10818
10819
      /* If we are reporting errors for this situation then do so now.  */
10820
0
      if (!ignore_undef
10821
0
    && h->ref_dynamic_nonweak
10822
0
    && (!h->ref_regular || flinfo->info->gc_sections)
10823
0
    && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10824
0
    && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10825
0
  {
10826
0
    flinfo->info->callbacks->undefined_symbol
10827
0
      (flinfo->info, h->root.root.string,
10828
0
       h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10829
0
       flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10830
0
       && !flinfo->info->warn_unresolved_syms);
10831
0
  }
10832
10833
      /* Strip a global symbol defined in a discarded section.  */
10834
0
      if (h->indx == -3)
10835
0
  return true;
10836
0
    }
10837
10838
  /* We should also warn if a forced local symbol is referenced from
10839
     shared libraries.  */
10840
0
  if (bfd_link_executable (flinfo->info)
10841
0
      && h->forced_local
10842
0
      && h->ref_dynamic
10843
0
      && h->def_regular
10844
0
      && !h->dynamic_def
10845
0
      && h->ref_dynamic_nonweak
10846
0
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10847
0
    {
10848
0
      bfd *def_bfd;
10849
0
      const char *msg;
10850
0
      struct elf_link_hash_entry *hi = h;
10851
10852
      /* Check indirect symbol.  */
10853
0
      while (hi->root.type == bfd_link_hash_indirect)
10854
0
  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10855
10856
0
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10857
  /* xgettext:c-format */
10858
0
  msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10859
0
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10860
  /* xgettext:c-format */
10861
0
  msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10862
0
      else
10863
  /* xgettext:c-format */
10864
0
  msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10865
0
      def_bfd = flinfo->output_bfd;
10866
0
      if (hi->root.u.def.section != bfd_abs_section_ptr)
10867
0
  def_bfd = hi->root.u.def.section->owner;
10868
0
      _bfd_error_handler (msg, flinfo->output_bfd,
10869
0
        h->root.root.string, def_bfd);
10870
0
      bfd_set_error (bfd_error_bad_value);
10871
0
      eoinfo->failed = true;
10872
0
      return false;
10873
0
    }
10874
10875
  /* We don't want to output symbols that have never been mentioned by
10876
     a regular file, or that we have been told to strip.  However, if
10877
     h->indx is set to -2, the symbol is used by a reloc and we must
10878
     output it.  */
10879
0
  strip = false;
10880
0
  if (h->indx == -2)
10881
0
    ;
10882
0
  else if ((h->def_dynamic
10883
0
      || h->ref_dynamic
10884
0
      || h->root.type == bfd_link_hash_new)
10885
0
     && !h->def_regular
10886
0
     && !h->ref_regular)
10887
0
    strip = true;
10888
0
  else if (flinfo->info->strip == strip_all)
10889
0
    strip = true;
10890
0
  else if (flinfo->info->strip == strip_some
10891
0
     && bfd_hash_lookup (flinfo->info->keep_hash,
10892
0
             h->root.root.string, false, false) == NULL)
10893
0
    strip = true;
10894
0
  else if ((h->root.type == bfd_link_hash_defined
10895
0
      || h->root.type == bfd_link_hash_defweak)
10896
0
     && ((flinfo->info->strip_discarded
10897
0
    && discarded_section (h->root.u.def.section))
10898
0
         || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10899
0
       && h->root.u.def.section->owner != NULL
10900
0
       && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10901
0
    strip = true;
10902
0
  else if ((h->root.type == bfd_link_hash_undefined
10903
0
      || h->root.type == bfd_link_hash_undefweak)
10904
0
     && h->root.u.undef.abfd != NULL
10905
0
     && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10906
0
    strip = true;
10907
10908
  /* Remember if this symbol should be stripped.  */
10909
0
  bool should_strip = strip;
10910
10911
  /* Strip undefined weak symbols link if they don't have relocation.  */
10912
0
  if (!strip)
10913
0
    strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10914
10915
0
  type = h->type;
10916
10917
  /* If we're stripping it, and it's not a dynamic symbol, there's
10918
     nothing else to do.   However, if it is a forced local symbol or
10919
     an ifunc symbol we need to give the backend finish_dynamic_symbol
10920
     function a chance to make it dynamic.  */
10921
0
  if (strip
10922
0
      && h->dynindx == -1
10923
0
      && type != STT_GNU_IFUNC
10924
0
      && !h->forced_local)
10925
0
    return true;
10926
10927
0
  sym.st_value = 0;
10928
0
  sym.st_size = h->size;
10929
0
  sym.st_other = h->other;
10930
0
  switch (h->root.type)
10931
0
    {
10932
0
    default:
10933
0
    case bfd_link_hash_new:
10934
0
    case bfd_link_hash_warning:
10935
0
      abort ();
10936
0
      return false;
10937
10938
0
    case bfd_link_hash_undefined:
10939
0
    case bfd_link_hash_undefweak:
10940
0
      input_sec = bfd_und_section_ptr;
10941
0
      sym.st_shndx = SHN_UNDEF;
10942
0
      break;
10943
10944
0
    case bfd_link_hash_defined:
10945
0
    case bfd_link_hash_defweak:
10946
0
      {
10947
0
  input_sec = h->root.u.def.section;
10948
0
  if (input_sec->output_section != NULL)
10949
0
    {
10950
0
      sym.st_shndx =
10951
0
        _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10952
0
             input_sec->output_section);
10953
0
      if (sym.st_shndx == SHN_BAD)
10954
0
        {
10955
0
    _bfd_error_handler
10956
      /* xgettext:c-format */
10957
0
      (_("%pB: could not find output section %pA for input section %pA"),
10958
0
       flinfo->output_bfd, input_sec->output_section, input_sec);
10959
0
    bfd_set_error (bfd_error_nonrepresentable_section);
10960
0
    eoinfo->failed = true;
10961
0
    return false;
10962
0
        }
10963
10964
      /* ELF symbols in relocatable files are section relative,
10965
         but in nonrelocatable files they are virtual
10966
         addresses.  */
10967
0
      sym.st_value = h->root.u.def.value + input_sec->output_offset;
10968
0
      if (!bfd_link_relocatable (flinfo->info))
10969
0
        {
10970
0
    sym.st_value += input_sec->output_section->vma;
10971
0
    if (h->type == STT_TLS)
10972
0
      {
10973
0
        asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10974
0
        if (tls_sec != NULL)
10975
0
          sym.st_value -= tls_sec->vma;
10976
0
      }
10977
0
        }
10978
0
    }
10979
0
  else
10980
0
    {
10981
0
      BFD_ASSERT (input_sec->owner == NULL
10982
0
      || (input_sec->owner->flags & DYNAMIC) != 0);
10983
0
      sym.st_shndx = SHN_UNDEF;
10984
0
      input_sec = bfd_und_section_ptr;
10985
0
    }
10986
0
      }
10987
0
      break;
10988
10989
0
    case bfd_link_hash_common:
10990
0
      input_sec = h->root.u.c.p->section;
10991
0
      sym.st_shndx = bed->common_section_index (input_sec);
10992
0
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
10993
0
      break;
10994
10995
0
    case bfd_link_hash_indirect:
10996
      /* These symbols are created by symbol versioning.  They point
10997
   to the decorated version of the name.  For example, if the
10998
   symbol foo@@GNU_1.2 is the default, which should be used when
10999
   foo is used with no version, then we add an indirect symbol
11000
   foo which points to foo@@GNU_1.2.  We ignore these symbols,
11001
   since the indirected symbol is already in the hash table.  */
11002
0
      return true;
11003
0
    }
11004
11005
0
  if (type == STT_COMMON || type == STT_OBJECT)
11006
0
    switch (h->root.type)
11007
0
      {
11008
0
      case bfd_link_hash_common:
11009
0
  type = elf_link_convert_common_type (flinfo->info, type);
11010
0
  break;
11011
0
      case bfd_link_hash_defined:
11012
0
      case bfd_link_hash_defweak:
11013
0
  if (bed->common_definition (&sym))
11014
0
    type = elf_link_convert_common_type (flinfo->info, type);
11015
0
  else
11016
0
    type = STT_OBJECT;
11017
0
  break;
11018
0
      case bfd_link_hash_undefined:
11019
0
      case bfd_link_hash_undefweak:
11020
0
  break;
11021
0
      default:
11022
0
  abort ();
11023
0
      }
11024
11025
0
  if (h->forced_local)
11026
0
    {
11027
0
      sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
11028
      /* Turn off visibility on local symbol.  */
11029
0
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11030
0
    }
11031
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
11032
0
  else if (h->unique_global && h->def_regular)
11033
0
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
11034
0
  else if (h->root.type == bfd_link_hash_undefweak
11035
0
     || h->root.type == bfd_link_hash_defweak)
11036
0
    sym.st_info = ELF_ST_INFO (STB_WEAK, type);
11037
0
  else
11038
0
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
11039
0
  sym.st_target_internal = h->target_internal;
11040
11041
  /* Give the processor backend a chance to tweak the symbol value,
11042
     and also to finish up anything that needs to be done for this
11043
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
11044
     forced local syms when non-shared is due to a historical quirk.
11045
     STT_GNU_IFUNC symbol must go through PLT.  */
11046
0
  if (((h->type == STT_GNU_IFUNC
11047
0
  && h->def_regular
11048
0
  && !bfd_link_relocatable (flinfo->info))
11049
0
       || ((h->dynindx != -1
11050
0
      || h->forced_local)
11051
0
     && ((bfd_link_pic (flinfo->info)
11052
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11053
0
        || h->root.type != bfd_link_hash_undefweak))
11054
0
         || !h->forced_local)
11055
0
     && elf_hash_table (flinfo->info)->dynamic_sections_created))
11056
0
      && bed->elf_backend_finish_dynamic_symbol != NULL)
11057
0
    {
11058
0
      if (!bed->elf_backend_finish_dynamic_symbol (flinfo->info, h, &sym))
11059
0
  {
11060
0
    eoinfo->failed = true;
11061
0
    return false;
11062
0
  }
11063
      /* If a symbol is in the dynamic symbol table and isn't a
11064
   should-strip symbol, also keep it in the symbol table.  */
11065
0
      if (!should_strip)
11066
0
  strip = false;
11067
0
    }
11068
11069
  /* If we are marking the symbol as undefined, and there are no
11070
     non-weak references to this symbol from a regular object, then
11071
     mark the symbol as weak undefined; if there are non-weak
11072
     references, mark the symbol as strong.  We can't do this earlier,
11073
     because it might not be marked as undefined until the
11074
     finish_dynamic_symbol routine gets through with it.  */
11075
0
  if (sym.st_shndx == SHN_UNDEF
11076
0
      && h->ref_regular
11077
0
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11078
0
    || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11079
0
    {
11080
0
      int bindtype;
11081
0
      type = ELF_ST_TYPE (sym.st_info);
11082
11083
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11084
0
      if (type == STT_GNU_IFUNC)
11085
0
  type = STT_FUNC;
11086
11087
0
      if (h->ref_regular_nonweak)
11088
0
  bindtype = STB_GLOBAL;
11089
0
      else
11090
0
  bindtype = STB_WEAK;
11091
0
      sym.st_info = ELF_ST_INFO (bindtype, type);
11092
0
    }
11093
11094
  /* If this is a symbol defined in a dynamic library, don't use the
11095
     symbol size from the dynamic library.  Relinking an executable
11096
     against a new library may introduce gratuitous changes in the
11097
     executable's symbols if we keep the size.  */
11098
0
  if (sym.st_shndx == SHN_UNDEF
11099
0
      && !h->def_regular
11100
0
      && h->def_dynamic)
11101
0
    sym.st_size = 0;
11102
11103
  /* If a non-weak symbol with non-default visibility is not defined
11104
     locally, it is a fatal error.  */
11105
0
  if (!bfd_link_relocatable (flinfo->info)
11106
0
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11107
0
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
11108
0
      && h->root.type == bfd_link_hash_undefined
11109
0
      && !h->def_regular)
11110
0
    {
11111
0
      const char *msg;
11112
11113
0
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11114
  /* xgettext:c-format */
11115
0
  msg = _("%pB: protected symbol `%s' isn't defined");
11116
0
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11117
  /* xgettext:c-format */
11118
0
  msg = _("%pB: internal symbol `%s' isn't defined");
11119
0
      else
11120
  /* xgettext:c-format */
11121
0
  msg = _("%pB: hidden symbol `%s' isn't defined");
11122
0
      _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11123
0
      bfd_set_error (bfd_error_bad_value);
11124
0
      eoinfo->failed = true;
11125
0
      return false;
11126
0
    }
11127
11128
  /* If this symbol should be put in the .dynsym section, then put it
11129
     there now.  We already know the symbol index.  We also fill in
11130
     the entry in the .hash section.  */
11131
0
  if (h->dynindx != -1
11132
0
      && elf_hash_table (flinfo->info)->dynamic_sections_created
11133
0
      && elf_hash_table (flinfo->info)->dynsym != NULL
11134
0
      && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11135
0
    {
11136
0
      bfd_byte *esym;
11137
11138
      /* Since there is no version information in the dynamic string,
11139
   if there is no version info in symbol version section, we will
11140
   have a run-time problem if not linking executable, referenced
11141
   by shared library, or not bound locally.  */
11142
0
      if (h->verinfo.verdef == NULL
11143
0
    && (!bfd_link_executable (flinfo->info)
11144
0
        || h->ref_dynamic
11145
0
        || !h->def_regular))
11146
0
  {
11147
0
    const char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11148
11149
0
    if (p && p [1] != '\0')
11150
0
      {
11151
0
        _bfd_error_handler
11152
    /* xgettext:c-format */
11153
0
    (_("%pB: no symbol version section for versioned symbol `%s'"),
11154
0
     flinfo->output_bfd, h->root.root.string);
11155
0
        eoinfo->failed = true;
11156
0
        return false;
11157
0
      }
11158
0
  }
11159
11160
0
      sym.st_name = h->dynstr_index;
11161
0
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
11162
0
        + h->dynindx * bed->s->sizeof_sym);
11163
0
      if (!check_dynsym (flinfo->output_bfd, &sym))
11164
0
  {
11165
0
    eoinfo->failed = true;
11166
0
    return false;
11167
0
  }
11168
11169
      /* Inform the linker of the addition of this symbol.  */
11170
11171
0
      if (flinfo->info->callbacks->ctf_new_dynsym)
11172
0
  flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11173
11174
0
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11175
11176
0
      if (flinfo->hash_sec != NULL)
11177
0
  {
11178
0
    size_t hash_entry_size;
11179
0
    bfd_byte *bucketpos;
11180
0
    bfd_vma chain;
11181
0
    size_t bucketcount;
11182
0
    size_t bucket;
11183
11184
0
    bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11185
0
    bucket = h->u.elf_hash_value % bucketcount;
11186
11187
0
    hash_entry_size
11188
0
      = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11189
0
    bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11190
0
           + (bucket + 2) * hash_entry_size);
11191
0
    chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11192
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11193
0
       bucketpos);
11194
0
    bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11195
0
       ((bfd_byte *) flinfo->hash_sec->contents
11196
0
        + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11197
0
  }
11198
11199
0
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11200
0
  {
11201
0
    Elf_Internal_Versym iversym;
11202
0
    Elf_External_Versym *eversym;
11203
11204
0
    if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11205
0
      {
11206
0
        if (h->verinfo.verdef == NULL
11207
0
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11208
0
          & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11209
0
    {
11210
0
      iversym.vs_vers = 1;
11211
0
      if (strchr (h->root.root.string, ELF_VER_CHR) == NULL)
11212
        /* Referenced symbol without ELF_VER_CHR has no
11213
           version.  */
11214
0
        iversym.vs_vers = 0;
11215
0
    }
11216
0
        else
11217
0
    iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11218
0
      }
11219
0
    else
11220
0
      {
11221
0
        if (h->verinfo.vertree == NULL)
11222
0
    iversym.vs_vers = 1;
11223
0
        else
11224
0
    iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11225
0
        if (flinfo->info->create_default_symver)
11226
0
    iversym.vs_vers++;
11227
11228
        /* Turn on VERSYM_HIDDEN only if the hidden versioned
11229
     symbol is defined locally.  */
11230
0
        if (h->versioned == versioned_hidden && h->def_regular)
11231
0
    iversym.vs_vers |= VERSYM_HIDDEN;
11232
0
      }
11233
11234
0
    eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11235
0
    eversym += h->dynindx;
11236
0
    _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11237
0
  }
11238
0
    }
11239
11240
  /* If the symbol is undefined, and we didn't output it to .dynsym,
11241
     strip it from .symtab too.  Obviously we can't do this for
11242
     relocatable output or when needed for --emit-relocs.  */
11243
0
  else if (input_sec == bfd_und_section_ptr
11244
0
     && h->indx != -2
11245
     /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
11246
0
     && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11247
0
     && !bfd_link_relocatable (flinfo->info))
11248
0
    return true;
11249
11250
  /* Also strip others that we couldn't earlier due to dynamic symbol
11251
     processing.  */
11252
0
  if (strip)
11253
0
    return true;
11254
0
  if ((input_sec->flags & SEC_EXCLUDE) != 0)
11255
0
    return true;
11256
11257
  /* Output a FILE symbol so that following locals are not associated
11258
     with the wrong input file.  We need one for forced local symbols
11259
     if we've seen more than one FILE symbol or when we have exactly
11260
     one FILE symbol but global symbols are present in a file other
11261
     than the one with the FILE symbol.  We also need one if linker
11262
     defined symbols are present.  In practice these conditions are
11263
     always met, so just emit the FILE symbol unconditionally.  */
11264
0
  if (eoinfo->localsyms
11265
0
      && !eoinfo->file_sym_done
11266
0
      && eoinfo->flinfo->filesym_count != 0)
11267
0
    {
11268
0
      Elf_Internal_Sym fsym;
11269
11270
0
      memset (&fsym, 0, sizeof (fsym));
11271
0
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11272
0
      fsym.st_shndx = SHN_ABS;
11273
0
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11274
0
              bfd_und_section_ptr, NULL))
11275
0
  return false;
11276
11277
0
      eoinfo->file_sym_done = true;
11278
0
    }
11279
11280
0
  indx = bfd_get_symcount (flinfo->output_bfd);
11281
0
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11282
0
           input_sec, h);
11283
0
  if (ret == 0)
11284
0
    {
11285
0
      eoinfo->failed = true;
11286
0
      return false;
11287
0
    }
11288
0
  else if (ret == 1)
11289
0
    h->indx = indx;
11290
0
  else if (h->indx == -2)
11291
0
    abort();
11292
11293
0
  return true;
11294
0
}
11295
11296
/* Return TRUE if special handling is done for relocs in SEC against
11297
   symbols defined in discarded sections.  */
11298
11299
static bool
11300
elf_section_ignore_discarded_relocs (asection *sec)
11301
0
{
11302
0
  elf_backend_data *bed;
11303
11304
0
  switch (sec->sec_info_type)
11305
0
    {
11306
0
    case SEC_INFO_TYPE_STABS:
11307
0
    case SEC_INFO_TYPE_EH_FRAME:
11308
0
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11309
0
    case SEC_INFO_TYPE_SFRAME:
11310
0
      return true;
11311
0
    default:
11312
0
      break;
11313
0
    }
11314
11315
0
  bed = get_elf_backend_data (sec->owner);
11316
0
  if (bed->elf_backend_ignore_discarded_relocs != NULL
11317
0
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11318
0
    return true;
11319
11320
0
  return false;
11321
0
}
11322
11323
/* Return a mask saying how ld should treat relocations in SEC against
11324
   symbols defined in discarded sections.  If this function returns
11325
   COMPLAIN set, ld will issue a warning message.  If this function
11326
   returns PRETEND set, and the discarded section was link-once and the
11327
   same size as the kept link-once section, ld will pretend that the
11328
   symbol was actually defined in the kept section.  Otherwise ld will
11329
   zero the reloc (at least that is the intent, but some cooperation by
11330
   the target dependent code is needed, particularly for REL targets).  */
11331
11332
unsigned int
11333
_bfd_elf_default_action_discarded (asection *sec)
11334
0
{
11335
0
  if (sec->flags & SEC_DEBUGGING)
11336
0
    return PRETEND;
11337
11338
0
  if (elf_section_type (sec) == SHT_GNU_SFRAME)
11339
0
    return 0;
11340
11341
0
  if (strncmp (sec->name, ".eh_frame", 9) == 0
11342
0
      && (sec->name[9] == 0 || sec->name[9] == '.'))
11343
0
    return 0;
11344
11345
0
  if (strcmp (sec->name, ".gcc_except_table") == 0)
11346
0
    return 0;
11347
11348
0
  return COMPLAIN | PRETEND;
11349
0
}
11350
11351
/* Find a match between a section and a member of a section group.  */
11352
11353
static asection *
11354
match_group_member (asection *sec, asection *group,
11355
        struct bfd_link_info *info)
11356
0
{
11357
0
  asection *first = elf_next_in_group (group);
11358
0
  asection *s = first;
11359
11360
0
  while (s != NULL)
11361
0
    {
11362
0
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
11363
0
  return s;
11364
11365
0
      s = elf_next_in_group (s);
11366
0
      if (s == first)
11367
0
  break;
11368
0
    }
11369
11370
0
  return NULL;
11371
0
}
11372
11373
/* Check if the kept section of a discarded section SEC can be used
11374
   to replace it.  Return the replacement if it is OK.  Otherwise return
11375
   NULL.  */
11376
11377
asection *
11378
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11379
0
{
11380
0
  asection *kept;
11381
11382
0
  kept = sec->kept_section;
11383
0
  if (kept != NULL)
11384
0
    {
11385
0
      if ((kept->flags & SEC_GROUP) != 0)
11386
0
  kept = match_group_member (sec, kept, info);
11387
0
      if (kept != NULL)
11388
0
  {
11389
0
    if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11390
0
        != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11391
0
      kept = NULL;
11392
0
    else
11393
0
      {
11394
        /* Get the real kept section.  */
11395
0
        asection *next;
11396
0
        for (next = kept->kept_section;
11397
0
       next != NULL;
11398
0
       next = next->kept_section)
11399
0
    kept = next;
11400
0
      }
11401
0
  }
11402
0
      sec->kept_section = kept;
11403
0
    }
11404
0
  return kept;
11405
0
}
11406
11407
/* Link an input file into the linker output file.  This function
11408
   handles all the sections and relocations of the input file at once.
11409
   This is so that we only have to read the local symbols once, and
11410
   don't have to keep them in memory.  */
11411
11412
static bool
11413
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11414
0
{
11415
0
  int (*relocate_section)
11416
0
    (struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11417
0
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11418
0
  bfd *output_bfd;
11419
0
  Elf_Internal_Shdr *symtab_hdr;
11420
0
  size_t locsymcount;
11421
0
  size_t extsymoff;
11422
0
  size_t num_sym;
11423
0
  Elf_Internal_Sym *isymbuf;
11424
0
  Elf_Internal_Sym *isym;
11425
0
  Elf_Internal_Sym *isymend;
11426
0
  long *pindex;
11427
0
  asection **ppsection;
11428
0
  asection *o;
11429
0
  elf_backend_data *bed;
11430
0
  struct elf_link_hash_entry **sym_hashes;
11431
0
  bfd_size_type address_size;
11432
0
  bfd_vma r_type_mask;
11433
0
  int r_sym_shift;
11434
0
  bool have_file_sym = false;
11435
11436
0
  output_bfd = flinfo->output_bfd;
11437
0
  bed = get_elf_backend_data (output_bfd);
11438
0
  relocate_section = bed->elf_backend_relocate_section;
11439
11440
  /* If this is a dynamic object, we don't want to do anything here:
11441
     we don't want the local symbols, and we don't want the section
11442
     contents.  */
11443
0
  if ((input_bfd->flags & DYNAMIC) != 0)
11444
0
    return true;
11445
11446
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
11447
0
  num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
11448
0
  if (elf_bad_symtab (input_bfd))
11449
0
    {
11450
0
      locsymcount = num_sym;
11451
0
      extsymoff = 0;
11452
0
    }
11453
0
  else
11454
0
    {
11455
0
      locsymcount = symtab_hdr->sh_info;
11456
0
      extsymoff = symtab_hdr->sh_info;
11457
0
    }
11458
11459
  /* Enable GNU OSABI features in the output BFD that are used in the input
11460
     BFD.  */
11461
0
  if (bed->elf_osabi == ELFOSABI_NONE
11462
0
      || bed->elf_osabi == ELFOSABI_GNU
11463
0
      || bed->elf_osabi == ELFOSABI_FREEBSD)
11464
0
    elf_tdata (output_bfd)->has_gnu_osabi
11465
0
      |= (elf_tdata (input_bfd)->has_gnu_osabi
11466
0
    & (bfd_link_relocatable (flinfo->info)
11467
0
       ? -1 : ~elf_gnu_osabi_retain));
11468
11469
  /* Read the local symbols.  */
11470
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11471
0
  if (isymbuf == NULL && locsymcount != 0)
11472
0
    {
11473
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11474
0
              flinfo->internal_syms,
11475
0
              flinfo->external_syms,
11476
0
              flinfo->locsym_shndx);
11477
0
      if (isymbuf == NULL)
11478
0
  return false;
11479
0
    }
11480
11481
  /* Find local symbol sections and adjust values of symbols in
11482
     SEC_MERGE sections.  Write out those local symbols we know are
11483
     going into the output file.  */
11484
0
  isymend = PTR_ADD (isymbuf, locsymcount);
11485
0
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11486
0
       isym < isymend;
11487
0
       isym++, pindex++, ppsection++)
11488
0
    {
11489
0
      asection *isec;
11490
0
      const char *name;
11491
0
      Elf_Internal_Sym osym;
11492
0
      long indx;
11493
0
      int ret;
11494
11495
0
      *pindex = -1;
11496
11497
0
      if (elf_bad_symtab (input_bfd))
11498
0
  {
11499
0
    if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11500
0
      {
11501
0
        *ppsection = NULL;
11502
0
        continue;
11503
0
      }
11504
0
  }
11505
11506
0
      if (isym->st_shndx == SHN_UNDEF)
11507
0
  isec = bfd_und_section_ptr;
11508
0
      else if (isym->st_shndx == SHN_ABS)
11509
0
  isec = bfd_abs_section_ptr;
11510
0
      else if (isym->st_shndx == SHN_COMMON)
11511
0
  isec = bfd_com_section_ptr;
11512
0
      else
11513
0
  {
11514
0
    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11515
0
    if (isec == NULL)
11516
0
      {
11517
        /* Don't attempt to output symbols with st_shnx in the
11518
     reserved range other than SHN_ABS and SHN_COMMON.  */
11519
0
        isec = bfd_und_section_ptr;
11520
0
      }
11521
0
    else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11522
0
       && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11523
0
      isym->st_value =
11524
0
        _bfd_merged_section_offset (output_bfd, &isec, isym->st_value);
11525
0
  }
11526
11527
0
      *ppsection = isec;
11528
11529
      /* Don't output the first, undefined, symbol.  In fact, don't
11530
   output any undefined local symbol.  */
11531
0
      if (isec == bfd_und_section_ptr)
11532
0
  continue;
11533
11534
0
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11535
0
  {
11536
    /* We never output section symbols.  Instead, we use the
11537
       section symbol of the corresponding section in the output
11538
       file.  */
11539
0
    continue;
11540
0
  }
11541
11542
      /* If we are stripping all symbols, we don't want to output this
11543
   one.  */
11544
0
      if (flinfo->info->strip == strip_all)
11545
0
  continue;
11546
11547
      /* If we are discarding all local symbols, we don't want to
11548
   output this one.  If we are generating a relocatable output
11549
   file, then some of the local symbols may be required by
11550
   relocs; we output them below as we discover that they are
11551
   needed.  */
11552
0
      if (flinfo->info->discard == discard_all)
11553
0
  continue;
11554
11555
      /* If this symbol is defined in a section which we are
11556
   discarding, we don't need to keep it.  */
11557
0
      if (isym->st_shndx < SHN_LORESERVE
11558
0
    && (isec->output_section == NULL
11559
0
        || bfd_section_removed_from_list (output_bfd,
11560
0
            isec->output_section)))
11561
0
  continue;
11562
11563
      /* Get the name of the symbol.  */
11564
0
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11565
0
                isym->st_name);
11566
0
      if (name == NULL)
11567
0
  return false;
11568
11569
      /* See if we are discarding symbols with this name.  */
11570
0
      if ((flinfo->info->strip == strip_some
11571
0
     && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11572
0
         == NULL))
11573
0
    || (((flinfo->info->discard == discard_sec_merge
11574
0
    && (isec->flags & SEC_MERGE)
11575
0
    && !bfd_link_relocatable (flinfo->info))
11576
0
         || flinfo->info->discard == discard_l)
11577
0
        && bfd_is_local_label_name (input_bfd, name)))
11578
0
  continue;
11579
11580
0
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11581
0
  {
11582
0
    if (input_bfd->lto_output)
11583
      /* -flto puts a temp file name here.  This means builds
11584
         are not reproducible.  Discard the symbol.  */
11585
0
      continue;
11586
0
    have_file_sym = true;
11587
0
    flinfo->filesym_count += 1;
11588
0
  }
11589
0
      if (!have_file_sym)
11590
0
  {
11591
    /* In the absence of debug info, bfd_find_nearest_line uses
11592
       FILE symbols to determine the source file for local
11593
       function symbols.  Provide a FILE symbol here if input
11594
       files lack such, so that their symbols won't be
11595
       associated with a previous input file.  It's not the
11596
       source file, but the best we can do.  */
11597
0
    const char *filename;
11598
0
    have_file_sym = true;
11599
0
    flinfo->filesym_count += 1;
11600
0
    memset (&osym, 0, sizeof (osym));
11601
0
    osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11602
0
    osym.st_shndx = SHN_ABS;
11603
0
    if (input_bfd->lto_output)
11604
0
      filename = NULL;
11605
0
    else
11606
0
      filename = lbasename (bfd_get_filename (input_bfd));
11607
0
    if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11608
0
            bfd_abs_section_ptr, NULL))
11609
0
      return false;
11610
0
  }
11611
11612
0
      osym = *isym;
11613
11614
      /* Adjust the section index for the output file.  */
11615
0
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11616
0
               isec->output_section);
11617
0
      if (osym.st_shndx == SHN_BAD)
11618
0
  return false;
11619
11620
      /* ELF symbols in relocatable files are section relative, but
11621
   in executable files they are virtual addresses.  Note that
11622
   this code assumes that all ELF sections have an associated
11623
   BFD section with a reasonable value for output_offset; below
11624
   we assume that they also have a reasonable value for
11625
   output_section.  Any special sections must be set up to meet
11626
   these requirements.  */
11627
0
      osym.st_value += isec->output_offset;
11628
0
      if (!bfd_link_relocatable (flinfo->info))
11629
0
  {
11630
0
    osym.st_value += isec->output_section->vma;
11631
0
    if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11632
0
      {
11633
        /* STT_TLS symbols are relative to PT_TLS segment base.  */
11634
0
        if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11635
0
    osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11636
0
        else
11637
0
    osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11638
0
              STT_NOTYPE);
11639
0
      }
11640
0
  }
11641
11642
0
      indx = bfd_get_symcount (output_bfd);
11643
0
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11644
0
      if (ret == 0)
11645
0
  return false;
11646
0
      else if (ret == 1)
11647
0
  *pindex = indx;
11648
0
    }
11649
11650
0
  elf_sym_hashes_valid (input_bfd) = true;
11651
11652
0
  if (bed->s->arch_size == 32)
11653
0
    {
11654
0
      r_type_mask = 0xff;
11655
0
      r_sym_shift = 8;
11656
0
      address_size = 4;
11657
0
    }
11658
0
  else
11659
0
    {
11660
0
      r_type_mask = 0xffffffff;
11661
0
      r_sym_shift = 32;
11662
0
      address_size = 8;
11663
0
    }
11664
11665
  /* Relocate the contents of each section.  */
11666
0
  sym_hashes = elf_sym_hashes (input_bfd);
11667
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
11668
0
    {
11669
0
      bfd_byte *contents;
11670
11671
0
      if (! o->linker_mark)
11672
0
  {
11673
    /* This section was omitted from the link.  */
11674
0
    continue;
11675
0
  }
11676
11677
0
      if (!flinfo->info->resolve_section_groups
11678
0
    && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11679
0
  {
11680
    /* Deal with the group signature symbol.  */
11681
0
    struct bfd_elf_section_data *sec_data = elf_section_data (o);
11682
0
    unsigned long symndx = sec_data->this_hdr.sh_info;
11683
0
    asection *osec = o->output_section;
11684
11685
0
    BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11686
0
    if (symndx >= locsymcount
11687
0
        || (elf_bad_symtab (input_bfd)
11688
0
      && flinfo->sections[symndx] == NULL))
11689
0
      {
11690
0
        struct elf_link_hash_entry *h;
11691
11692
0
        h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
11693
0
            extsymoff, num_sym);
11694
0
        if (h == NULL)
11695
0
    {
11696
0
      _bfd_error_handler
11697
        /* xgettext:c-format */
11698
0
        (_("error: %pB: unable to create group section symbol"),
11699
0
         input_bfd);
11700
0
      bfd_set_error (bfd_error_bad_value);
11701
0
      return false;
11702
0
    }
11703
11704
        /* Arrange for symbol to be output.  */
11705
0
        h->indx = -2;
11706
0
        elf_section_data (osec)->this_hdr.sh_info = -2;
11707
0
      }
11708
0
    else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11709
0
      {
11710
        /* We'll use the output section target_index.  */
11711
0
        asection *sec = flinfo->sections[symndx]->output_section;
11712
0
        elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11713
0
      }
11714
0
    else
11715
0
      {
11716
0
        if (flinfo->indices[symndx] == -1)
11717
0
    {
11718
      /* Otherwise output the local symbol now.  */
11719
0
      Elf_Internal_Sym sym = isymbuf[symndx];
11720
0
      asection *sec = flinfo->sections[symndx]->output_section;
11721
0
      const char *name;
11722
0
      long indx;
11723
0
      int ret;
11724
11725
0
      name = bfd_elf_string_from_elf_section (input_bfd,
11726
0
                symtab_hdr->sh_link,
11727
0
                sym.st_name);
11728
0
      if (name == NULL)
11729
0
        return false;
11730
11731
0
      sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11732
0
                    sec);
11733
0
      if (sym.st_shndx == SHN_BAD)
11734
0
        return false;
11735
11736
0
      sym.st_value += o->output_offset;
11737
11738
0
      indx = bfd_get_symcount (output_bfd);
11739
0
      ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11740
0
               NULL);
11741
0
      if (ret == 0)
11742
0
        return false;
11743
0
      else if (ret == 1)
11744
0
        flinfo->indices[symndx] = indx;
11745
0
      else
11746
0
        abort ();
11747
0
    }
11748
0
        elf_section_data (osec)->this_hdr.sh_info
11749
0
    = flinfo->indices[symndx];
11750
0
      }
11751
0
  }
11752
11753
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
11754
0
    || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11755
0
  continue;
11756
11757
0
      if ((o->flags & SEC_LINKER_CREATED) != 0)
11758
0
  {
11759
    /* Section was created by bfd_elf_link_create_dynamic_sections()
11760
       or somesuch.  */
11761
0
    continue;
11762
0
  }
11763
11764
      /* Get the contents of the section.  They have been cached by a
11765
   relaxation routine.  Note that o is a section in an input
11766
   file, so the contents field will not have been set by any of
11767
   the routines which work on output files.  */
11768
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
11769
0
  {
11770
0
    contents = elf_section_data (o)->this_hdr.contents;
11771
0
    if (bed->caches_rawsize
11772
0
        && o->rawsize != 0
11773
0
        && o->rawsize < o->size)
11774
0
      {
11775
0
        memcpy (flinfo->contents, contents, o->rawsize);
11776
0
        contents = flinfo->contents;
11777
0
      }
11778
0
  }
11779
0
      else if (!(o->flags & SEC_RELOC)
11780
0
         && !bed->elf_backend_write_section
11781
0
         && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11782
  /* A MERGE section that has no relocations doesn't need the
11783
     contents anymore, they have been recorded earlier.  Except
11784
     if the backend has special provisions for writing sections.  */
11785
0
  contents = NULL;
11786
0
      else
11787
0
  {
11788
0
    contents = flinfo->contents;
11789
0
    if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11790
0
                 &contents))
11791
0
      return false;
11792
0
  }
11793
11794
0
      if ((o->flags & SEC_RELOC) != 0)
11795
0
  {
11796
0
    Elf_Internal_Rela *internal_relocs;
11797
0
    Elf_Internal_Rela *rel, *relend;
11798
0
    int action_discarded;
11799
0
    int ret;
11800
11801
    /* Get the swapped relocs.  */
11802
0
    internal_relocs
11803
0
      = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11804
0
                flinfo->external_relocs,
11805
0
                flinfo->internal_relocs,
11806
0
                false);
11807
0
    if (internal_relocs == NULL
11808
0
        && o->reloc_count > 0)
11809
0
      return false;
11810
11811
0
    action_discarded = -1;
11812
0
    if (!elf_section_ignore_discarded_relocs (o))
11813
0
      action_discarded = (*bed->action_discarded) (o);
11814
11815
    /* Run through the relocs evaluating complex reloc symbols and
11816
       looking for relocs against symbols from discarded sections
11817
       or section symbols from removed link-once sections.
11818
       Complain about relocs against discarded sections.  Zero
11819
       relocs against removed link-once sections.  */
11820
11821
0
    rel = internal_relocs;
11822
0
    relend = rel + o->reloc_count;
11823
0
    for ( ; rel < relend; rel++)
11824
0
      {
11825
0
        unsigned long r_symndx = rel->r_info >> r_sym_shift;
11826
0
        unsigned int s_type;
11827
0
        asection **ps, *sec;
11828
0
        struct elf_link_hash_entry *h = NULL;
11829
0
        const char *sym_name;
11830
11831
0
        if (r_symndx == STN_UNDEF)
11832
0
    continue;
11833
11834
0
        if (r_symndx >= locsymcount
11835
0
      || (elf_bad_symtab (input_bfd)
11836
0
          && flinfo->sections[r_symndx] == NULL))
11837
0
    {
11838
0
      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
11839
0
                extsymoff, num_sym);
11840
11841
      /* Badly formatted input files can contain relocs that
11842
         reference non-existant symbols.  Check here so that
11843
         we do not seg fault.  */
11844
0
      if (h == NULL)
11845
0
        {
11846
0
          _bfd_error_handler
11847
      /* xgettext:c-format */
11848
0
      (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11849
0
         "that references a non-existent global symbol"),
11850
0
       input_bfd, (uint64_t) rel->r_info, o);
11851
0
          bfd_set_error (bfd_error_bad_value);
11852
0
          return false;
11853
0
        }
11854
11855
0
      s_type = h->type;
11856
11857
      /* If a plugin symbol is referenced from a non-IR file,
11858
         mark the symbol as undefined.  Note that the
11859
         linker may attach linker created dynamic sections
11860
         to the plugin bfd.  Symbols defined in linker
11861
         created sections are not plugin symbols.  */
11862
0
      if ((h->root.non_ir_ref_regular
11863
0
           || h->root.non_ir_ref_dynamic)
11864
0
          && (h->root.type == bfd_link_hash_defined
11865
0
        || h->root.type == bfd_link_hash_defweak)
11866
0
          && (h->root.u.def.section->flags
11867
0
        & SEC_LINKER_CREATED) == 0
11868
0
          && h->root.u.def.section->owner != NULL
11869
0
          && (h->root.u.def.section->owner->flags
11870
0
        & BFD_PLUGIN) != 0)
11871
0
        {
11872
0
          h->root.type = bfd_link_hash_undefined;
11873
0
          h->root.u.undef.abfd = h->root.u.def.section->owner;
11874
0
        }
11875
11876
0
      ps = NULL;
11877
0
      if (h->root.type == bfd_link_hash_defined
11878
0
          || h->root.type == bfd_link_hash_defweak)
11879
0
        ps = &h->root.u.def.section;
11880
11881
0
      sym_name = h->root.root.string;
11882
0
    }
11883
0
        else
11884
0
    {
11885
0
      Elf_Internal_Sym *sym = isymbuf + r_symndx;
11886
11887
0
      s_type = ELF_ST_TYPE (sym->st_info);
11888
0
      ps = &flinfo->sections[r_symndx];
11889
0
      sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11890
0
                 sym, *ps);
11891
0
    }
11892
11893
0
        if ((s_type == STT_RELC || s_type == STT_SRELC)
11894
0
      && !bfd_link_relocatable (flinfo->info))
11895
0
    {
11896
0
      bfd_vma val;
11897
0
      bfd_vma dot = (rel->r_offset
11898
0
         + o->output_offset + o->output_section->vma);
11899
#ifdef DEBUG
11900
      printf ("Encountered a complex symbol!");
11901
      printf (" (input_bfd %s, section %s, reloc %ld\n",
11902
        bfd_get_filename (input_bfd), o->name,
11903
        (long) (rel - internal_relocs));
11904
      printf (" symbol: idx  %8.8lx, name %s\n",
11905
        r_symndx, sym_name);
11906
      printf (" reloc : info %8.8lx, addr %8.8lx\n",
11907
        (unsigned long) rel->r_info,
11908
        (unsigned long) rel->r_offset);
11909
#endif
11910
0
      if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11911
0
            isymbuf, locsymcount, s_type == STT_SRELC))
11912
0
        return false;
11913
11914
      /* Symbol evaluated OK.  Update to absolute value.  */
11915
0
      if (!set_symbol_value (input_bfd, isymbuf, locsymcount,
11916
0
           num_sym, r_symndx, val))
11917
0
        return false;
11918
11919
0
      continue;
11920
0
    }
11921
11922
0
        if (action_discarded != -1 && ps != NULL)
11923
0
    {
11924
      /* Complain if the definition comes from a
11925
         discarded section.  */
11926
0
      if ((sec = *ps) != NULL && discarded_section (sec))
11927
0
        {
11928
0
          BFD_ASSERT (r_symndx != STN_UNDEF);
11929
0
          if (action_discarded & COMPLAIN)
11930
0
      (*flinfo->info->callbacks->einfo)
11931
        /* xgettext:c-format */
11932
0
        (_("%X`%s' referenced in section `%pA' of %pB: "
11933
0
           "defined in discarded section `%pA' of %pB\n"),
11934
0
         sym_name, o, input_bfd, sec, sec->owner);
11935
11936
          /* Try to do the best we can to support buggy old
11937
       versions of gcc.  Pretend that the symbol is
11938
       really defined in the kept linkonce section.
11939
       FIXME: This is quite broken.  Modifying the
11940
       symbol here means we will be changing all later
11941
       uses of the symbol, not just in this section.  */
11942
0
          if (action_discarded & PRETEND)
11943
0
      {
11944
0
        asection *kept;
11945
11946
0
        kept = _bfd_elf_check_kept_section (sec,
11947
0
                    flinfo->info);
11948
0
        if (kept != NULL)
11949
0
          {
11950
0
            *ps = kept;
11951
0
            continue;
11952
0
          }
11953
0
      }
11954
0
        }
11955
0
    }
11956
0
      }
11957
11958
    /* Relocate the section by invoking a back end routine.
11959
11960
       The back end routine is responsible for adjusting the
11961
       section contents as necessary, and (if using Rela relocs
11962
       and generating a relocatable output file) adjusting the
11963
       reloc addend as necessary.
11964
11965
       The back end routine does not have to worry about setting
11966
       the reloc address or the reloc symbol index.
11967
11968
       The back end routine is given a pointer to the swapped in
11969
       internal symbols, and can access the hash table entries
11970
       for the external symbols via elf_sym_hashes (input_bfd).
11971
11972
       When generating relocatable output, the back end routine
11973
       must handle STB_LOCAL/STT_SECTION symbols specially.  The
11974
       output symbol is going to be a section symbol
11975
       corresponding to the output section, which will require
11976
       the addend to be adjusted.  */
11977
11978
0
    ret = relocate_section (flinfo->info,
11979
0
          input_bfd, o, contents,
11980
0
          internal_relocs,
11981
0
          isymbuf,
11982
0
          flinfo->sections);
11983
0
    if (!ret)
11984
0
      return false;
11985
11986
0
    if (ret == 2
11987
0
        || bfd_link_relocatable (flinfo->info)
11988
0
        || flinfo->info->emitrelocations)
11989
0
      {
11990
0
        Elf_Internal_Rela *irela;
11991
0
        Elf_Internal_Rela *irelaend, *irelamid;
11992
0
        bfd_vma last_offset;
11993
0
        struct elf_link_hash_entry **rel_hash;
11994
0
        struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11995
0
        Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11996
0
        unsigned int next_erel;
11997
0
        bool rela_normal;
11998
0
        struct bfd_elf_section_data *esdi, *esdo;
11999
12000
0
        esdi = elf_section_data (o);
12001
0
        esdo = elf_section_data (o->output_section);
12002
0
        rela_normal = false;
12003
12004
        /* Adjust the reloc addresses and symbol indices.  */
12005
12006
0
        irela = internal_relocs;
12007
0
        irelaend = irela + o->reloc_count;
12008
0
        rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
12009
        /* We start processing the REL relocs, if any.  When we reach
12010
     IRELAMID in the loop, we switch to the RELA relocs.  */
12011
0
        irelamid = irela;
12012
0
        if (esdi->rel.hdr != NULL)
12013
0
    irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
12014
0
           * bed->s->int_rels_per_ext_rel);
12015
0
        rel_hash_list = rel_hash;
12016
0
        rela_hash_list = NULL;
12017
0
        last_offset = o->output_offset;
12018
0
        if (!bfd_link_relocatable (flinfo->info))
12019
0
    last_offset += o->output_section->vma;
12020
0
        for (next_erel = 0; irela < irelaend; irela++, next_erel++)
12021
0
    {
12022
0
      unsigned long r_symndx;
12023
0
      asection *sec;
12024
0
      Elf_Internal_Sym sym;
12025
12026
0
      if (next_erel == bed->s->int_rels_per_ext_rel)
12027
0
        {
12028
0
          rel_hash++;
12029
0
          next_erel = 0;
12030
0
        }
12031
12032
0
      if (irela == irelamid)
12033
0
        {
12034
0
          rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
12035
0
          rela_hash_list = rel_hash;
12036
0
          rela_normal = bed->rela_normal;
12037
0
        }
12038
12039
0
      irela->r_offset = _bfd_elf_section_offset (output_bfd,
12040
0
                   flinfo->info, o,
12041
0
                   irela->r_offset);
12042
0
      if (irela->r_offset >= (bfd_vma) -2)
12043
0
        {
12044
          /* This is a reloc for a deleted entry or somesuch.
12045
       Turn it into an R_*_NONE reloc, at the same
12046
       offset as the last reloc.  elf_eh_frame.c and
12047
       bfd_elf_discard_info rely on reloc offsets
12048
       being ordered.  */
12049
0
          irela->r_offset = last_offset;
12050
0
          irela->r_info = 0;
12051
0
          irela->r_addend = 0;
12052
0
          continue;
12053
0
        }
12054
12055
0
      irela->r_offset += o->output_offset;
12056
12057
      /* Relocs in an executable have to be virtual addresses.  */
12058
0
      if (!bfd_link_relocatable (flinfo->info))
12059
0
        irela->r_offset += o->output_section->vma;
12060
12061
0
      last_offset = irela->r_offset;
12062
12063
0
      r_symndx = irela->r_info >> r_sym_shift;
12064
0
      if (r_symndx == STN_UNDEF)
12065
0
        continue;
12066
12067
0
      if (r_symndx >= locsymcount
12068
0
          || (elf_bad_symtab (input_bfd)
12069
0
        && flinfo->sections[r_symndx] == NULL))
12070
0
        {
12071
0
          struct elf_link_hash_entry *rh;
12072
12073
          /* This is a reloc against a global symbol.  We
12074
       have not yet output all the local symbols, so
12075
       we do not know the symbol index of any global
12076
       symbol.  We set the rel_hash entry for this
12077
       reloc to point to the global hash table entry
12078
       for this symbol.  The symbol index is then
12079
       set at the end of bfd_elf_final_link.  */
12080
0
          rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
12081
0
               extsymoff, num_sym);
12082
0
          if (rh == NULL)
12083
0
      {
12084
        /* FIXME: Generate an error ?  */
12085
0
        continue;
12086
0
      }
12087
12088
          /* Setting the index to -2 tells elf_link_output_extsym
12089
       that this symbol is used by a reloc.  */
12090
0
          BFD_ASSERT (rh->indx < 0);
12091
0
          rh->indx = -2;
12092
0
          *rel_hash = rh;
12093
12094
0
          continue;
12095
0
        }
12096
12097
      /* This is a reloc against a local symbol.  */
12098
12099
0
      *rel_hash = NULL;
12100
0
      sym = isymbuf[r_symndx];
12101
0
      sec = flinfo->sections[r_symndx];
12102
0
      if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12103
0
        {
12104
          /* I suppose the backend ought to fill in the
12105
       section of any STT_SECTION symbol against a
12106
       processor specific section.  */
12107
0
          r_symndx = STN_UNDEF;
12108
0
          if (bfd_is_abs_section (sec))
12109
0
      ;
12110
0
          else if (sec == NULL || sec->owner == NULL)
12111
0
      {
12112
0
        bfd_set_error (bfd_error_bad_value);
12113
0
        return false;
12114
0
      }
12115
0
          else
12116
0
      {
12117
0
        asection *osec = sec->output_section;
12118
12119
        /* If we have discarded a section, the output
12120
           section will be the absolute section.  In
12121
           case of discarded SEC_MERGE sections, use
12122
           the kept section.  relocate_section should
12123
           have already handled discarded linkonce
12124
           sections.  */
12125
0
        if (bfd_is_abs_section (osec)
12126
0
            && sec->kept_section != NULL
12127
0
            && sec->kept_section->output_section != NULL)
12128
0
          {
12129
0
            osec = sec->kept_section->output_section;
12130
0
            irela->r_addend -= osec->vma;
12131
0
          }
12132
12133
0
        if (!bfd_is_abs_section (osec))
12134
0
          {
12135
0
            r_symndx = osec->target_index;
12136
0
            if (r_symndx == STN_UNDEF)
12137
0
        {
12138
0
          irela->r_addend += osec->vma;
12139
0
          osec = flinfo->info->callbacks->nearby_section
12140
0
            (output_bfd, osec, osec->vma);
12141
0
          irela->r_addend -= osec->vma;
12142
0
          r_symndx = osec->target_index;
12143
0
        }
12144
0
          }
12145
0
      }
12146
12147
          /* Adjust the addend according to where the
12148
       section winds up in the output section.  */
12149
0
          if (rela_normal)
12150
0
      irela->r_addend += sec->output_offset;
12151
0
        }
12152
0
      else
12153
0
        {
12154
0
          if (flinfo->indices[r_symndx] == -1)
12155
0
      {
12156
0
        unsigned long shlink;
12157
0
        const char *name;
12158
0
        asection *osec;
12159
0
        long indx;
12160
12161
0
        if (flinfo->info->strip == strip_all)
12162
0
          {
12163
            /* You can't do ld -r -s.  */
12164
0
            bfd_set_error (bfd_error_invalid_operation);
12165
0
            return false;
12166
0
          }
12167
12168
        /* This symbol was skipped earlier, but
12169
           since it is needed by a reloc, we
12170
           must output it now.  */
12171
0
        shlink = symtab_hdr->sh_link;
12172
0
        name = (bfd_elf_string_from_elf_section
12173
0
          (input_bfd, shlink, sym.st_name));
12174
0
        if (name == NULL)
12175
0
          return false;
12176
12177
0
        osec = sec->output_section;
12178
0
        sym.st_shndx =
12179
0
          _bfd_elf_section_from_bfd_section (output_bfd,
12180
0
                     osec);
12181
0
        if (sym.st_shndx == SHN_BAD)
12182
0
          return false;
12183
12184
0
        sym.st_value += sec->output_offset;
12185
0
        if (!bfd_link_relocatable (flinfo->info))
12186
0
          {
12187
0
            sym.st_value += osec->vma;
12188
0
            if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12189
0
        {
12190
0
          struct elf_link_hash_table *htab
12191
0
            = elf_hash_table (flinfo->info);
12192
12193
          /* STT_TLS symbols are relative to PT_TLS
12194
             segment base.  */
12195
0
          if (htab->tls_sec != NULL)
12196
0
            sym.st_value -= htab->tls_sec->vma;
12197
0
          else
12198
0
            sym.st_info
12199
0
              = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12200
0
                 STT_NOTYPE);
12201
0
        }
12202
0
          }
12203
12204
0
        indx = bfd_get_symcount (output_bfd);
12205
0
        ret = elf_link_output_symstrtab (flinfo, name,
12206
0
                 &sym, sec,
12207
0
                 NULL);
12208
0
        if (ret == 0)
12209
0
          return false;
12210
0
        else if (ret == 1)
12211
0
          flinfo->indices[r_symndx] = indx;
12212
0
        else
12213
0
          abort ();
12214
0
      }
12215
12216
0
          r_symndx = flinfo->indices[r_symndx];
12217
0
        }
12218
12219
0
      irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12220
0
           | (irela->r_info & r_type_mask));
12221
0
    }
12222
12223
        /* Swap out the relocs.  */
12224
0
        input_rel_hdr = esdi->rel.hdr;
12225
0
        if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12226
0
    {
12227
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12228
0
                 input_rel_hdr,
12229
0
                 internal_relocs,
12230
0
                 rel_hash_list))
12231
0
        return false;
12232
0
      internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12233
0
              * bed->s->int_rels_per_ext_rel);
12234
0
      rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12235
0
    }
12236
12237
0
        input_rela_hdr = esdi->rela.hdr;
12238
0
        if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12239
0
    {
12240
0
      if (!bed->elf_backend_emit_relocs (output_bfd, o,
12241
0
                 input_rela_hdr,
12242
0
                 internal_relocs,
12243
0
                 rela_hash_list))
12244
0
        return false;
12245
0
    }
12246
0
      }
12247
0
  }
12248
12249
      /* Write out the modified section contents.  */
12250
0
      if (bed->elf_backend_write_section
12251
0
    && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12252
0
            contents))
12253
0
  {
12254
    /* Section written out.  */
12255
0
  }
12256
0
      else switch (o->sec_info_type)
12257
0
  {
12258
0
  case SEC_INFO_TYPE_STABS:
12259
0
    if (! (_bfd_write_section_stabs
12260
0
     (output_bfd,
12261
0
      &elf_hash_table (flinfo->info)->stab_info, o, contents)))
12262
0
      return false;
12263
0
    break;
12264
0
  case SEC_INFO_TYPE_MERGE:
12265
0
    if (! _bfd_write_merged_section (output_bfd, o))
12266
0
      return false;
12267
0
    break;
12268
0
  case SEC_INFO_TYPE_EH_FRAME:
12269
0
    {
12270
0
      if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12271
0
               o, contents))
12272
0
        return false;
12273
0
    }
12274
0
    break;
12275
0
  case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12276
0
    {
12277
0
      if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12278
0
               flinfo->info,
12279
0
               o, contents))
12280
0
        return false;
12281
0
    }
12282
0
    break;
12283
0
  case SEC_INFO_TYPE_SFRAME:
12284
0
      {
12285
        /* Merge SFrame section into the SFrame encoder context of the
12286
     output_bfd's section.  The final .sframe output section will
12287
     be written out later.  */
12288
0
        if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12289
0
              o, contents))
12290
0
    return false;
12291
0
      }
12292
0
      break;
12293
0
  default:
12294
0
    {
12295
0
      if (! (o->flags & SEC_EXCLUDE))
12296
0
        {
12297
0
    file_ptr offset = (file_ptr) o->output_offset;
12298
0
    bfd_size_type todo = o->size;
12299
12300
0
    offset *= bfd_octets_per_byte (output_bfd, o);
12301
12302
0
    if ((o->flags & SEC_ELF_REVERSE_COPY)
12303
0
        && o->size > address_size)
12304
0
      {
12305
        /* Reverse-copy input section to output.  */
12306
12307
0
        if ((o->size & (address_size - 1)) != 0
12308
0
      || (o->reloc_count != 0
12309
0
          && (o->size * bed->s->int_rels_per_ext_rel
12310
0
        != o->reloc_count * address_size)))
12311
0
          {
12312
0
      _bfd_error_handler
12313
        /* xgettext:c-format */
12314
0
        (_("error: %pB: size of section %pA is not "
12315
0
           "multiple of address size"),
12316
0
         input_bfd, o);
12317
0
      bfd_set_error (bfd_error_bad_value);
12318
0
      return false;
12319
0
          }
12320
12321
0
        do
12322
0
          {
12323
0
      todo -= address_size;
12324
0
      if (! bfd_set_section_contents (output_bfd,
12325
0
              o->output_section,
12326
0
              contents + todo,
12327
0
              offset,
12328
0
              address_size))
12329
0
        return false;
12330
0
      if (todo == 0)
12331
0
        break;
12332
0
      offset += address_size;
12333
0
          }
12334
0
        while (1);
12335
0
      }
12336
0
    else if (! bfd_set_section_contents (output_bfd,
12337
0
                 o->output_section,
12338
0
                 contents,
12339
0
                 offset, todo))
12340
0
      return false;
12341
0
        }
12342
0
    }
12343
0
    break;
12344
0
  }
12345
12346
      /* Munmap the section contents for each input section.  */
12347
0
      _bfd_elf_link_munmap_section_contents (o);
12348
0
    }
12349
12350
0
  return true;
12351
0
}
12352
12353
/* Generate a reloc when linking an ELF file.  This is a reloc
12354
   requested by the linker, and does not come from any input file.  This
12355
   is used to build constructor and destructor tables when linking
12356
   with -Ur.  */
12357
12358
static bool
12359
elf_reloc_link_order (bfd *output_bfd,
12360
          struct bfd_link_info *info,
12361
          asection *output_section,
12362
          struct bfd_link_order *link_order)
12363
0
{
12364
0
  reloc_howto_type *howto;
12365
0
  long indx;
12366
0
  bfd_vma offset;
12367
0
  bfd_vma addend;
12368
0
  struct bfd_elf_section_reloc_data *reldata;
12369
0
  struct elf_link_hash_entry **rel_hash_ptr;
12370
0
  Elf_Internal_Shdr *rel_hdr;
12371
0
  elf_backend_data *bed = get_elf_backend_data (output_bfd);
12372
0
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12373
0
  bfd_byte *erel;
12374
0
  unsigned int i;
12375
0
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12376
12377
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12378
0
  if (howto == NULL)
12379
0
    {
12380
0
      bfd_set_error (bfd_error_bad_value);
12381
0
      return false;
12382
0
    }
12383
12384
0
  addend = link_order->u.reloc.p->addend;
12385
12386
0
  if (esdo->rel.hdr)
12387
0
    reldata = &esdo->rel;
12388
0
  else if (esdo->rela.hdr)
12389
0
    reldata = &esdo->rela;
12390
0
  else
12391
0
    {
12392
0
      reldata = NULL;
12393
0
      BFD_ASSERT (0);
12394
0
    }
12395
12396
  /* Figure out the symbol index.  */
12397
0
  rel_hash_ptr = reldata->hashes + reldata->count;
12398
0
  if (link_order->type == bfd_section_reloc_link_order)
12399
0
    {
12400
0
      indx = link_order->u.reloc.p->u.section->target_index;
12401
0
      BFD_ASSERT (indx != 0);
12402
0
      *rel_hash_ptr = NULL;
12403
0
    }
12404
0
  else
12405
0
    {
12406
0
      struct elf_link_hash_entry *h;
12407
12408
      /* Treat a reloc against a defined symbol as though it were
12409
   actually against the section.  */
12410
0
      h = ((struct elf_link_hash_entry *)
12411
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
12412
0
           link_order->u.reloc.p->u.name,
12413
0
           false, false, true));
12414
0
      if (h != NULL
12415
0
    && (h->root.type == bfd_link_hash_defined
12416
0
        || h->root.type == bfd_link_hash_defweak))
12417
0
  {
12418
0
    asection *section;
12419
12420
0
    section = h->root.u.def.section;
12421
0
    indx = section->output_section->target_index;
12422
0
    *rel_hash_ptr = NULL;
12423
    /* It seems that we ought to add the symbol value to the
12424
       addend here, but in practice it has already been added
12425
       because it was passed to constructor_callback.  */
12426
0
    addend += section->output_section->vma + section->output_offset;
12427
0
  }
12428
0
      else if (h != NULL)
12429
0
  {
12430
    /* Setting the index to -2 tells elf_link_output_extsym that
12431
       this symbol is used by a reloc.  */
12432
0
    h->indx = -2;
12433
0
    *rel_hash_ptr = h;
12434
0
    indx = 0;
12435
0
  }
12436
0
      else
12437
0
  {
12438
0
    (*info->callbacks->unattached_reloc)
12439
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12440
0
    indx = 0;
12441
0
  }
12442
0
    }
12443
12444
  /* If this is an inplace reloc, we must write the addend into the
12445
     object file.  */
12446
0
  if (howto->partial_inplace && addend != 0)
12447
0
    {
12448
0
      bfd_size_type size;
12449
0
      bfd_reloc_status_type rstat;
12450
0
      bfd_byte *buf;
12451
0
      bool ok;
12452
0
      const char *sym_name;
12453
0
      bfd_size_type octets;
12454
12455
0
      size = bfd_get_reloc_size (howto);
12456
0
      buf = bfd_zmalloc (size);
12457
0
      if (buf == NULL && size != 0)
12458
0
  return false;
12459
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12460
0
      switch (rstat)
12461
0
  {
12462
0
  case bfd_reloc_ok:
12463
0
    break;
12464
12465
0
  default:
12466
0
  case bfd_reloc_outofrange:
12467
0
    abort ();
12468
12469
0
  case bfd_reloc_overflow:
12470
0
    if (link_order->type == bfd_section_reloc_link_order)
12471
0
      sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12472
0
    else
12473
0
      sym_name = link_order->u.reloc.p->u.name;
12474
0
    (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12475
0
                howto->name, addend, NULL, NULL,
12476
0
                (bfd_vma) 0);
12477
0
    break;
12478
0
  }
12479
12480
0
      octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12481
0
               output_section);
12482
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
12483
0
             octets, size);
12484
0
      free (buf);
12485
0
      if (! ok)
12486
0
  return false;
12487
0
    }
12488
12489
  /* The address of a reloc is relative to the section in a
12490
     relocatable file, and is a virtual address in an executable
12491
     file.  */
12492
0
  offset = link_order->offset;
12493
0
  if (! bfd_link_relocatable (info))
12494
0
    offset += output_section->vma;
12495
12496
0
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12497
0
    {
12498
0
      irel[i].r_offset = offset;
12499
0
      irel[i].r_info = 0;
12500
0
      irel[i].r_addend = 0;
12501
0
    }
12502
0
  if (bed->s->arch_size == 32)
12503
0
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12504
0
  else
12505
0
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12506
12507
0
  rel_hdr = reldata->hdr;
12508
0
  erel = rel_hdr->contents;
12509
0
  if (rel_hdr->sh_type == SHT_REL)
12510
0
    {
12511
0
      erel += reldata->count * bed->s->sizeof_rel;
12512
0
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12513
0
    }
12514
0
  else
12515
0
    {
12516
0
      irel[0].r_addend = addend;
12517
0
      erel += reldata->count * bed->s->sizeof_rela;
12518
0
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12519
0
    }
12520
12521
0
  ++reldata->count;
12522
12523
0
  return true;
12524
0
}
12525
12526
/* Generate an import library in INFO->implib_bfd from symbols in OBFD.
12527
   Returns TRUE upon success, FALSE otherwise.  */
12528
12529
static bool
12530
elf_output_implib (bfd *obfd, struct bfd_link_info *info)
12531
0
{
12532
0
  bool ret = false;
12533
0
  bfd *implib_bfd;
12534
0
  elf_backend_data *obed;
12535
0
  flagword flags;
12536
0
  enum bfd_architecture arch;
12537
0
  unsigned int mach;
12538
0
  asymbol **sympp = NULL;
12539
0
  long symsize;
12540
0
  long symcount;
12541
0
  long src_count;
12542
0
  elf_symbol_type *osymbuf;
12543
12544
0
  implib_bfd = info->out_implib_bfd;
12545
0
  obed = get_elf_backend_data (obfd);
12546
12547
0
  if (!bfd_set_format (implib_bfd, bfd_object))
12548
0
    return false;
12549
12550
  /* Use flag from executable but make it a relocatable object.  */
12551
0
  flags = bfd_get_file_flags (obfd);
12552
0
  flags &= ~HAS_RELOC;
12553
0
  if (!bfd_set_start_address (implib_bfd, 0)
12554
0
      || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12555
0
    return false;
12556
12557
  /* Copy architecture of output file to import library file.  */
12558
0
  arch = bfd_get_arch (obfd);
12559
0
  mach = bfd_get_mach (obfd);
12560
0
  if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12561
0
      && (obfd->target_defaulted
12562
0
    || bfd_get_arch (obfd) != bfd_get_arch (implib_bfd)))
12563
0
    return false;
12564
12565
  /* Get symbol table size.  */
12566
0
  symsize = bfd_get_symtab_upper_bound (obfd);
12567
0
  if (symsize < 0)
12568
0
    return false;
12569
12570
  /* Read in the symbol table.  */
12571
0
  sympp = bfd_malloc (symsize);
12572
0
  if (sympp == NULL)
12573
0
    return false;
12574
12575
0
  symcount = bfd_canonicalize_symtab (obfd, sympp);
12576
0
  if (symcount < 0)
12577
0
    goto free_sym_buf;
12578
12579
  /* Allow the BFD backend to copy any private header data it
12580
     understands from the output BFD to the import library BFD.  */
12581
0
  if (! bfd_copy_private_header_data (obfd, implib_bfd))
12582
0
    goto free_sym_buf;
12583
12584
  /* Filter symbols to appear in the import library.  */
12585
0
  symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
12586
0
  if (symcount == 0)
12587
0
    {
12588
0
      bfd_set_error (bfd_error_no_symbols);
12589
0
      _bfd_error_handler (_("%pB: no symbol found for import library"),
12590
0
        implib_bfd);
12591
0
      goto free_sym_buf;
12592
0
    }
12593
12594
  /* Make symbols absolute.  */
12595
0
  osymbuf = bfd_alloc (implib_bfd, symcount * sizeof (*osymbuf));
12596
0
  if (osymbuf == NULL)
12597
0
    goto free_sym_buf;
12598
12599
0
  for (src_count = 0; src_count < symcount; src_count++)
12600
0
    {
12601
0
      memcpy (&osymbuf[src_count], sympp[src_count], sizeof (*osymbuf));
12602
0
      osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12603
0
      osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12604
0
      osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12605
0
      osymbuf[src_count].internal_elf_sym.st_value =
12606
0
  osymbuf[src_count].symbol.value;
12607
0
      sympp[src_count] = &osymbuf[src_count].symbol;
12608
0
    }
12609
12610
0
  bfd_set_symtab (implib_bfd, sympp, symcount);
12611
12612
  /* Allow the BFD backend to copy any private data it understands
12613
     from the output BFD to the import library BFD.  This is done last
12614
     to permit the routine to look at the filtered symbol table.  */
12615
0
  if (! bfd_copy_private_bfd_data (obfd, implib_bfd))
12616
0
    goto free_sym_buf;
12617
12618
0
  if (!bfd_close (implib_bfd))
12619
0
    goto free_sym_buf;
12620
12621
0
  ret = true;
12622
12623
0
 free_sym_buf:
12624
0
  free (sympp);
12625
0
  return ret;
12626
0
}
12627
12628
static void
12629
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12630
0
{
12631
0
  asection *o;
12632
12633
0
  if (flinfo->symstrtab != NULL)
12634
0
    _bfd_elf_strtab_free (flinfo->symstrtab);
12635
0
  free (flinfo->contents);
12636
0
  free (flinfo->external_relocs);
12637
0
  free (flinfo->internal_relocs);
12638
0
  free (flinfo->external_syms);
12639
0
  free (flinfo->locsym_shndx);
12640
0
  free (flinfo->internal_syms);
12641
0
  free (flinfo->indices);
12642
0
  free (flinfo->sections);
12643
0
  if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12644
0
    free (flinfo->symshndxbuf);
12645
0
  for (o = obfd->sections; o != NULL; o = o->next)
12646
0
    {
12647
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12648
0
      free (esdo->rel.hashes);
12649
0
      free (esdo->rela.hashes);
12650
0
    }
12651
0
}
12652
12653
/* Do the final step of an ELF link.  */
12654
12655
bool
12656
_bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
12657
0
{
12658
0
  bool dynamic;
12659
0
  bool emit_relocs;
12660
0
  bfd *dynobj;
12661
0
  struct elf_final_link_info flinfo;
12662
0
  asection *o;
12663
0
  struct bfd_link_order *p;
12664
0
  bfd *sub;
12665
0
  bfd_size_type max_contents_size;
12666
0
  bfd_size_type max_external_reloc_size;
12667
0
  bfd_size_type max_internal_reloc_count;
12668
0
  bfd_size_type max_sym_count;
12669
0
  bfd_size_type max_sym_shndx_count;
12670
0
  Elf_Internal_Sym elfsym;
12671
0
  unsigned int i;
12672
0
  Elf_Internal_Shdr *symtab_hdr;
12673
0
  Elf_Internal_Shdr *symtab_shndx_hdr;
12674
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
12675
0
  struct elf_outext_info eoinfo;
12676
0
  bool merged;
12677
0
  size_t relativecount;
12678
0
  size_t relr_entsize;
12679
0
  asection *reldyn = 0;
12680
0
  bfd_size_type amt;
12681
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
12682
0
  bool sections_removed;
12683
12684
0
  if (!is_elf_hash_table (&htab->root))
12685
0
    return false;
12686
12687
0
  if (bfd_link_pic (info))
12688
0
    obfd->flags |= DYNAMIC;
12689
12690
0
  dynamic = htab->dynamic_sections_created;
12691
0
  dynobj = htab->dynobj;
12692
12693
0
  emit_relocs = (bfd_link_relocatable (info)
12694
0
     || info->emitrelocations);
12695
12696
0
  memset (&flinfo, 0, sizeof (flinfo));
12697
0
  flinfo.info = info;
12698
0
  flinfo.output_bfd = obfd;
12699
0
  flinfo.symstrtab = _bfd_elf_strtab_init ();
12700
0
  if (flinfo.symstrtab == NULL)
12701
0
    return false;
12702
12703
0
  if (! dynamic)
12704
0
    {
12705
0
      flinfo.hash_sec = NULL;
12706
0
      flinfo.symver_sec = NULL;
12707
0
    }
12708
0
  else
12709
0
    {
12710
0
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12711
      /* Note that dynsym_sec can be NULL (on VMS).  */
12712
0
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12713
      /* Note that it is OK if symver_sec is NULL.  */
12714
0
    }
12715
12716
0
  if (info->unique_symbol
12717
0
      && !bfd_hash_table_init (&flinfo.local_hash_table,
12718
0
             local_hash_newfunc,
12719
0
             sizeof (struct local_hash_entry)))
12720
0
    return false;
12721
12722
  /* The object attributes have been merged.  Remove the input
12723
     sections from the link, and set the contents of the output
12724
     section.  */
12725
0
  sections_removed = false;
12726
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12727
0
  const char *obj_attrs_section = get_elf_backend_data (obfd)->obj_attrs_section;
12728
0
#endif
12729
0
  for (o = obfd->sections; o != NULL; o = o->next)
12730
0
    {
12731
0
      bool remove_section = false;
12732
12733
0
#ifdef OBJ_MAYBE_ELF_ATTRIBUTES
12734
0
      if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12735
0
    || strcmp (o->name, ".gnu.attributes") == 0)
12736
0
  {
12737
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
12738
0
      {
12739
0
        asection *input_section;
12740
12741
0
        if (p->type != bfd_indirect_link_order)
12742
0
    continue;
12743
0
        input_section = p->u.indirect.section;
12744
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
12745
     elf_link_input_bfd ignores this section.  */
12746
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
12747
0
      }
12748
12749
    /* Skip this section later on.  */
12750
0
    o->map_head.link_order = NULL;
12751
12752
0
    bfd_vma attr_size = bfd_elf_obj_attr_size (obfd);
12753
    /* Once ELF headers have been written, the size of a section is
12754
       frozen. We need to set the size of the attribute section before
12755
       _bfd_elf_compute_section_file_positions.  */
12756
0
    bfd_set_section_size (o, attr_size);
12757
0
    if (attr_size > 0)
12758
0
      elf_obj_object_attributes (obfd) = o;
12759
0
    else
12760
0
      remove_section = true;
12761
0
  }
12762
0
      else
12763
0
#endif /* OBJ_MAYBE_ELF_ATTRIBUTES */
12764
0
      if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12765
0
  {
12766
    /* Remove empty group section from linker output.  */
12767
0
    remove_section = true;
12768
0
  }
12769
0
      if (remove_section)
12770
0
  {
12771
0
    o->flags |= SEC_EXCLUDE;
12772
0
    bfd_section_list_remove (obfd, o);
12773
0
    obfd->section_count--;
12774
0
    sections_removed = true;
12775
0
  }
12776
0
    }
12777
0
  if (sections_removed)
12778
0
    bfd_fix_excluded_sec_syms (info);
12779
12780
  /* Count up the number of relocations we will output for each output
12781
     section, so that we know the sizes of the reloc sections.  We
12782
     also figure out some maximum sizes.  */
12783
0
#ifdef USE_MMAP
12784
0
  if (obed->use_mmap)
12785
0
    {
12786
      /* Mmap is used only if section size >= the minimum mmap section
12787
   size.  The initial max_contents_size value covers all sections
12788
   smaller than the minimum mmap section size.  It may be increased
12789
   for compressed or linker created sections or sections whose
12790
   rawsize != size.  max_external_reloc_size covers all relocation
12791
   sections smaller than the minimum mmap section size.  */
12792
0
      max_contents_size = _bfd_minimum_mmap_size;
12793
0
      max_external_reloc_size = _bfd_minimum_mmap_size;
12794
0
    }
12795
0
  else
12796
0
#endif
12797
0
    {
12798
0
      max_contents_size = 0;
12799
0
      max_external_reloc_size = 0;
12800
0
    }
12801
0
  max_internal_reloc_count = 0;
12802
0
  max_sym_count = 0;
12803
0
  max_sym_shndx_count = 0;
12804
0
  merged = false;
12805
0
  for (o = obfd->sections; o != NULL; o = o->next)
12806
0
    {
12807
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12808
0
      bfd_size_type size_align = 1;
12809
12810
0
      if (o->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
12811
0
  {
12812
    /* eh_frame editing can extend last FDE to cover padding
12813
       between one section and the next.  */
12814
0
    size_align = (((bfd_size_type) 1 << o->alignment_power)
12815
0
      * bfd_octets_per_byte (obfd, o));
12816
0
  }
12817
12818
0
      o->reloc_count = 0;
12819
12820
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
12821
0
  {
12822
0
    unsigned int reloc_count = 0;
12823
0
    unsigned int additional_reloc_count = 0;
12824
0
    struct bfd_elf_section_data *esdi = NULL;
12825
12826
0
    if (p->type == bfd_section_reloc_link_order
12827
0
        || p->type == bfd_symbol_reloc_link_order)
12828
0
      reloc_count = 1;
12829
0
    else if (p->type == bfd_indirect_link_order)
12830
0
      {
12831
0
        asection *sec;
12832
12833
0
        sec = p->u.indirect.section;
12834
12835
        /* Mark all sections which are to be included in the
12836
     link.  This will normally be every section.  We need
12837
     to do this so that we can identify any sections which
12838
     the linker has decided to not include.  */
12839
0
        sec->linker_mark = true;
12840
12841
0
        if (sec->flags & SEC_MERGE)
12842
0
    merged = true;
12843
12844
0
#ifdef USE_MMAP
12845
        /* Mmap is used only on non-compressed, non-linker created
12846
     sections whose rawsize == size.  */
12847
0
        if (!obed->use_mmap
12848
0
      || sec->compress_status != COMPRESS_SECTION_NONE
12849
0
      || (sec->flags & SEC_LINKER_CREATED) != 0
12850
0
      || sec->rawsize != sec->size)
12851
0
#endif
12852
0
    {
12853
0
      bfd_size_type size = (sec->rawsize > sec->size
12854
0
          ? sec->rawsize : sec->size);
12855
0
      size = (size + size_align - 1) & -size_align;
12856
0
      if (max_contents_size < size)
12857
0
        max_contents_size = size;
12858
0
    }
12859
12860
0
        if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12861
0
      && (sec->owner->flags & DYNAMIC) == 0)
12862
0
    {
12863
0
      size_t sym_count;
12864
12865
      /* We are interested in just local symbols, not all
12866
         symbols.  */
12867
0
      if (elf_bad_symtab (sec->owner))
12868
0
        sym_count = (elf_symtab_hdr (sec->owner).sh_size
12869
0
         / obed->s->sizeof_sym);
12870
0
      else
12871
0
        sym_count = elf_symtab_hdr (sec->owner).sh_info;
12872
12873
0
      if (sym_count > max_sym_count)
12874
0
        max_sym_count = sym_count;
12875
12876
0
      if (sym_count > max_sym_shndx_count
12877
0
          && elf_symtab_shndx_list (sec->owner) != NULL)
12878
0
        max_sym_shndx_count = sym_count;
12879
12880
0
      esdi = elf_section_data (sec);
12881
12882
0
      if (esdi->this_hdr.sh_type == SHT_REL
12883
0
          || esdi->this_hdr.sh_type == SHT_RELA)
12884
        /* Some backends use reloc_count in relocation sections
12885
           to count particular types of relocs.  Of course,
12886
           reloc sections themselves can't have relocations.  */
12887
0
        ;
12888
0
      else if (emit_relocs)
12889
0
        {
12890
0
          reloc_count = sec->reloc_count;
12891
0
          if (obed->elf_backend_count_additional_relocs)
12892
0
      {
12893
0
        int c;
12894
0
        c = obed->elf_backend_count_additional_relocs (sec);
12895
0
        additional_reloc_count += c;
12896
0
      }
12897
0
        }
12898
0
      else if (obed->elf_backend_count_relocs)
12899
0
        reloc_count = obed->elf_backend_count_relocs (info, sec);
12900
12901
0
      if ((sec->flags & SEC_RELOC) != 0)
12902
0
        {
12903
0
#ifdef USE_MMAP
12904
0
          if (!obed->use_mmap)
12905
0
#endif
12906
0
      {
12907
0
        size_t ext_size = 0;
12908
12909
0
        if (esdi->rel.hdr != NULL)
12910
0
          ext_size = esdi->rel.hdr->sh_size;
12911
0
        if (esdi->rela.hdr != NULL)
12912
0
          ext_size += esdi->rela.hdr->sh_size;
12913
12914
0
        if (ext_size > max_external_reloc_size)
12915
0
          max_external_reloc_size = ext_size;
12916
0
      }
12917
0
          if (sec->reloc_count > max_internal_reloc_count)
12918
0
      max_internal_reloc_count = sec->reloc_count;
12919
0
        }
12920
0
    }
12921
0
      }
12922
12923
0
    if (reloc_count == 0)
12924
0
      continue;
12925
12926
0
    reloc_count += additional_reloc_count;
12927
0
    o->reloc_count += reloc_count;
12928
12929
0
    if (p->type == bfd_indirect_link_order && emit_relocs)
12930
0
      {
12931
0
        if (esdi->rel.hdr)
12932
0
    {
12933
0
      esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12934
0
      esdo->rel.count += additional_reloc_count;
12935
0
    }
12936
0
        if (esdi->rela.hdr)
12937
0
    {
12938
0
      esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12939
0
      esdo->rela.count += additional_reloc_count;
12940
0
    }
12941
0
      }
12942
0
    else
12943
0
      {
12944
0
        if (o->use_rela_p)
12945
0
    esdo->rela.count += reloc_count;
12946
0
        else
12947
0
    esdo->rel.count += reloc_count;
12948
0
      }
12949
0
  }
12950
12951
0
      if (o->reloc_count > 0)
12952
0
  o->flags |= SEC_RELOC;
12953
0
      else
12954
0
  {
12955
    /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12956
       set it (this is probably a bug) and if it is set
12957
       assign_section_numbers will create a reloc section.  */
12958
0
    o->flags &=~ SEC_RELOC;
12959
0
  }
12960
12961
      /* If the SEC_ALLOC flag is not set, force the section VMA to
12962
   zero.  This is done in elf_fake_sections as well, but forcing
12963
   the VMA to 0 here will ensure that relocs against these
12964
   sections are handled correctly.  */
12965
0
      if ((o->flags & SEC_ALLOC) == 0
12966
0
    && ! o->user_set_vma)
12967
0
  o->vma = 0;
12968
0
    }
12969
12970
0
  if (! bfd_link_relocatable (info) && merged)
12971
0
    elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, obfd);
12972
12973
  /* Figure out the file positions for everything but the symbol table
12974
     and the relocs.  We set symcount to force assign_section_numbers
12975
     to create a symbol table.  */
12976
0
  obfd->symcount = info->strip != strip_all || emit_relocs;
12977
0
  BFD_ASSERT (! obfd->output_has_begun);
12978
0
  if (! _bfd_elf_compute_section_file_positions (obfd, info))
12979
0
    goto error_return;
12980
12981
  /* Set sizes, and assign file positions for reloc sections.  */
12982
0
  for (o = obfd->sections; o != NULL; o = o->next)
12983
0
    {
12984
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
12985
0
      if ((o->flags & SEC_RELOC) != 0)
12986
0
  {
12987
0
    if (esdo->rel.hdr
12988
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rel)))
12989
0
      goto error_return;
12990
12991
0
    if (esdo->rela.hdr
12992
0
        && !(_bfd_elf_link_size_reloc_section (obfd, &esdo->rela)))
12993
0
      goto error_return;
12994
0
  }
12995
12996
      /* _bfd_elf_compute_section_file_positions makes temporary use
12997
   of target_index.  Reset it.  */
12998
0
      o->target_index = 0;
12999
13000
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
13001
   to count upwards while actually outputting the relocations.  */
13002
0
      esdo->rel.count = 0;
13003
0
      esdo->rela.count = 0;
13004
13005
0
      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
13006
0
    && !bfd_section_is_ctf (o))
13007
0
  {
13008
    /* Cache the section contents so that they can be compressed
13009
       later.  Use bfd_malloc since it will be freed by
13010
       bfd_compress_section_contents.  */
13011
0
    unsigned char *contents = esdo->this_hdr.contents;
13012
0
    if (contents != NULL)
13013
0
      abort ();
13014
0
    contents = bfd_malloc (esdo->this_hdr.sh_size);
13015
0
    if (contents == NULL)
13016
0
      goto error_return;
13017
0
    esdo->this_hdr.contents = contents;
13018
0
  }
13019
0
    }
13020
13021
  /* We have now assigned file positions for all the sections except .symtab,
13022
     .strtab, and non-loaded reloc and compressed debugging sections.  We start
13023
     the .symtab section at the current file position, and write directly to it.
13024
     We build the .strtab section in memory.  */
13025
0
  obfd->symcount = 0;
13026
0
  symtab_hdr = &elf_symtab_hdr (obfd);
13027
  /* sh_name is set in prep_headers.  */
13028
0
  symtab_hdr->sh_type = SHT_SYMTAB;
13029
  /* sh_flags, sh_addr and sh_size all start off zero.  */
13030
0
  symtab_hdr->sh_entsize = obed->s->sizeof_sym;
13031
  /* sh_link is set in assign_section_numbers.  */
13032
  /* sh_info is set below.  */
13033
  /* sh_offset is set just below.  */
13034
0
  symtab_hdr->sh_addralign = (bfd_vma) 1 << obed->s->log_file_align;
13035
13036
0
  if (max_sym_count < 20)
13037
0
    max_sym_count = 20;
13038
0
  htab->strtabsize = max_sym_count;
13039
0
  htab->strtab = bfd_malloc (max_sym_count * sizeof (*htab->strtab));
13040
0
  if (htab->strtab == NULL)
13041
0
    goto error_return;
13042
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
13043
0
  flinfo.symshndxbuf
13044
0
    = (elf_numsections (obfd) > (SHN_LORESERVE & 0xFFFF)
13045
0
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
13046
13047
0
  if (info->strip != strip_all || emit_relocs)
13048
0
    {
13049
0
      file_ptr off = elf_next_file_pos (obfd);
13050
13051
0
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
13052
13053
      /* Note that at this point elf_next_file_pos (obfd) is
13054
   incorrect.  We do not yet know the size of the .symtab section.
13055
   We correct next_file_pos below, after we do know the size.  */
13056
13057
      /* Start writing out the symbol table.  The first symbol is always a
13058
   dummy symbol.  */
13059
0
      elfsym.st_value = 0;
13060
0
      elfsym.st_size = 0;
13061
0
      elfsym.st_info = 0;
13062
0
      elfsym.st_other = 0;
13063
0
      elfsym.st_shndx = SHN_UNDEF;
13064
0
      elfsym.st_target_internal = 0;
13065
0
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13066
0
             bfd_und_section_ptr, NULL) != 1)
13067
0
  goto error_return;
13068
13069
      /* Output a symbol for each section if asked or they are used for
13070
   relocs.  These symbols usually have no names.  We store the
13071
   index of each one in the index field of the section, so that
13072
   we can find it again when outputting relocs.  */
13073
13074
0
      if (bfd_keep_unused_section_symbols (obfd) || emit_relocs)
13075
0
  {
13076
0
    bool name_local_sections
13077
0
      = (obed->elf_backend_name_local_section_symbols
13078
0
         && obed->elf_backend_name_local_section_symbols (obfd));
13079
0
    const char *name = NULL;
13080
13081
0
    elfsym.st_size = 0;
13082
0
    elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13083
0
    elfsym.st_other = 0;
13084
0
    elfsym.st_value = 0;
13085
0
    elfsym.st_target_internal = 0;
13086
0
    for (i = 1; i < elf_numsections (obfd); i++)
13087
0
      {
13088
0
        o = bfd_section_from_elf_index (obfd, i);
13089
0
        if (o != NULL)
13090
0
    {
13091
0
      o->target_index = bfd_get_symcount (obfd);
13092
0
      elfsym.st_shndx = i;
13093
0
      if (!bfd_link_relocatable (info))
13094
0
        elfsym.st_value = o->vma;
13095
0
      if (name_local_sections)
13096
0
        name = o->name;
13097
0
      if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13098
0
             NULL) != 1)
13099
0
        goto error_return;
13100
0
    }
13101
0
      }
13102
0
  }
13103
0
    }
13104
13105
  /* On some targets like Irix 5 the symbol split between local and global
13106
     ones recorded in the sh_info field needs to be done between section
13107
     and all other symbols.  */
13108
0
  if (obed->elf_backend_elfsym_local_is_section
13109
0
      && obed->elf_backend_elfsym_local_is_section (obfd))
13110
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13111
13112
  /* Allocate some memory to hold information read in from the input
13113
     files.  */
13114
0
  if (max_contents_size != 0)
13115
0
    {
13116
0
      flinfo.contents = bfd_malloc (max_contents_size);
13117
0
      if (flinfo.contents == NULL)
13118
0
  goto error_return;
13119
0
    }
13120
13121
0
  if (max_external_reloc_size != 0)
13122
0
    {
13123
0
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13124
0
      if (flinfo.external_relocs == NULL)
13125
0
  goto error_return;
13126
0
    }
13127
13128
0
  if (max_internal_reloc_count != 0)
13129
0
    {
13130
0
      amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13131
0
      flinfo.internal_relocs = bfd_malloc (amt);
13132
0
      if (flinfo.internal_relocs == NULL)
13133
0
  goto error_return;
13134
0
    }
13135
13136
0
  if (max_sym_count != 0)
13137
0
    {
13138
0
      amt = max_sym_count * obed->s->sizeof_sym;
13139
0
      flinfo.external_syms = bfd_malloc (amt);
13140
0
      if (flinfo.external_syms == NULL)
13141
0
  goto error_return;
13142
13143
0
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
13144
0
      flinfo.internal_syms = bfd_malloc (amt);
13145
0
      if (flinfo.internal_syms == NULL)
13146
0
  goto error_return;
13147
13148
0
      amt = max_sym_count * sizeof (long);
13149
0
      flinfo.indices = bfd_malloc (amt);
13150
0
      if (flinfo.indices == NULL)
13151
0
  goto error_return;
13152
13153
0
      amt = max_sym_count * sizeof (asection *);
13154
0
      flinfo.sections = bfd_malloc (amt);
13155
0
      if (flinfo.sections == NULL)
13156
0
  goto error_return;
13157
0
    }
13158
13159
0
  if (max_sym_shndx_count != 0)
13160
0
    {
13161
0
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13162
0
      flinfo.locsym_shndx = bfd_malloc (amt);
13163
0
      if (flinfo.locsym_shndx == NULL)
13164
0
  goto error_return;
13165
0
    }
13166
13167
0
  if (htab->tls_sec)
13168
0
    {
13169
0
      bfd_vma base, end = 0;  /* Both bytes.  */
13170
0
      asection *sec;
13171
13172
0
      for (sec = htab->tls_sec;
13173
0
     sec && (sec->flags & SEC_THREAD_LOCAL);
13174
0
     sec = sec->next)
13175
0
  {
13176
0
    bfd_size_type size = sec->size;
13177
0
    unsigned int opb = bfd_octets_per_byte (obfd, sec);
13178
13179
0
    if (size == 0
13180
0
        && (sec->flags & SEC_HAS_CONTENTS) == 0)
13181
0
      {
13182
0
        struct bfd_link_order *ord = sec->map_tail.link_order;
13183
13184
0
        if (ord != NULL)
13185
0
    size = ord->offset * opb + ord->size;
13186
0
      }
13187
0
    end = sec->vma + size / opb;
13188
0
  }
13189
0
      base = htab->tls_sec->vma;
13190
      /* Only align end of TLS section if static TLS doesn't have special
13191
   alignment requirements.  */
13192
0
      if (obed->static_tls_alignment == 1)
13193
0
  end = align_power (end, htab->tls_sec->alignment_power);
13194
0
      htab->tls_size = end - base;
13195
0
    }
13196
13197
0
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
13198
0
    return false;
13199
13200
  /* Finish relative relocations here after regular symbol processing
13201
     is finished if DT_RELR is enabled.  */
13202
0
  if (info->enable_dt_relr
13203
0
      && obed->finish_relative_relocs
13204
0
      && !obed->finish_relative_relocs (info))
13205
0
    info->callbacks->fatal
13206
0
      (_("%P: %pB: failed to finish relative relocations\n"), obfd);
13207
13208
  /* Since ELF permits relocations to be against local symbols, we
13209
     must have the local symbols available when we do the relocations.
13210
     Since we would rather only read the local symbols once, and we
13211
     would rather not keep them in memory, we handle all the
13212
     relocations for a single input file at the same time.
13213
13214
     Unfortunately, there is no way to know the total number of local
13215
     symbols until we have seen all of them, and the local symbol
13216
     indices precede the global symbol indices.  This means that when
13217
     we are generating relocatable output, and we see a reloc against
13218
     a global symbol, we can not know the symbol index until we have
13219
     finished examining all the local symbols to see which ones we are
13220
     going to output.  To deal with this, we keep the relocations in
13221
     memory, and don't output them until the end of the link.  This is
13222
     an unfortunate waste of memory, but I don't see a good way around
13223
     it.  Fortunately, it only happens when performing a relocatable
13224
     link, which is not the common case.  FIXME: If keep_memory is set
13225
     we could write the relocs out and then read them again; I don't
13226
     know how bad the memory loss will be.  */
13227
13228
0
  for (o = obfd->sections; o != NULL; o = o->next)
13229
0
    {
13230
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
13231
0
  {
13232
0
    if (p->type == bfd_indirect_link_order
13233
0
        && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13234
0
      == bfd_target_elf_flavour)
13235
0
        && elf_elfheader (sub)->e_ident[EI_CLASS] == obed->s->elfclass)
13236
0
      {
13237
0
        if (! sub->output_has_begun)
13238
0
    {
13239
0
      sub->output_has_begun = true;
13240
0
      if ((sub->flags & BFD_LINKER_CREATED) == 0
13241
0
          && !elf_link_input_bfd (&flinfo, sub))
13242
0
        goto error_return;
13243
0
    }
13244
0
      }
13245
0
    else if (p->type == bfd_section_reloc_link_order
13246
0
       || p->type == bfd_symbol_reloc_link_order)
13247
0
      {
13248
0
        if (! elf_reloc_link_order (obfd, info, o, p))
13249
0
    goto error_return;
13250
0
      }
13251
0
    else
13252
0
      {
13253
0
        if (! _bfd_default_link_order (obfd, info, o, p))
13254
0
    {
13255
0
      if (p->type == bfd_indirect_link_order
13256
0
          && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13257
0
        == bfd_target_elf_flavour)
13258
0
          && (elf_elfheader (sub)->e_ident[EI_CLASS]
13259
0
        != obed->s->elfclass))
13260
0
        {
13261
0
          const char *iclass, *oclass;
13262
13263
0
          switch (obed->s->elfclass)
13264
0
      {
13265
0
      case ELFCLASS64: oclass = "ELFCLASS64"; break;
13266
0
      case ELFCLASS32: oclass = "ELFCLASS32"; break;
13267
0
      case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13268
0
      default: abort ();
13269
0
      }
13270
13271
0
          switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13272
0
      {
13273
0
      case ELFCLASS64: iclass = "ELFCLASS64"; break;
13274
0
      case ELFCLASS32: iclass = "ELFCLASS32"; break;
13275
0
      case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13276
0
      default: abort ();
13277
0
      }
13278
13279
0
          bfd_set_error (bfd_error_wrong_format);
13280
0
          _bfd_error_handler
13281
      /* xgettext:c-format */
13282
0
      (_("%pB: file class %s incompatible with %s"),
13283
0
       sub, iclass, oclass);
13284
0
        }
13285
13286
0
      goto error_return;
13287
0
    }
13288
0
      }
13289
0
  }
13290
0
    }
13291
  /* Writing of linker created BFDs is left until last, because the
13292
     aarch64 backend wants to copy insns from a relocated section to
13293
     a stub section.  See erratum_843419 code.  */
13294
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13295
0
    if (sub->output_has_begun && (sub->flags & BFD_LINKER_CREATED) != 0)
13296
0
      {
13297
0
  if (!elf_link_input_bfd (&flinfo, sub))
13298
0
    goto error_return;
13299
0
      }
13300
13301
  /* Free symbol buffer if needed.  */
13302
0
  if (!info->reduce_memory_overheads)
13303
0
    {
13304
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13305
0
  if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13306
0
    {
13307
0
      free (elf_tdata (sub)->symbuf);
13308
0
      elf_tdata (sub)->symbuf = NULL;
13309
0
    }
13310
0
    }
13311
13312
  /* Output any global symbols that got converted to local in a
13313
     version script or due to symbol visibility.  We do this in a
13314
     separate step since ELF requires all local symbols to appear
13315
     prior to any global symbols.  FIXME: We should only do this if
13316
     some global symbols were, in fact, converted to become local.
13317
     FIXME: Will this work correctly with the Irix 5 linker?  */
13318
0
  eoinfo.failed = false;
13319
0
  eoinfo.flinfo = &flinfo;
13320
0
  eoinfo.localsyms = true;
13321
0
  eoinfo.file_sym_done = false;
13322
  /* Output non-base symbols first.  */
13323
0
  eoinfo.base_symbol = false;
13324
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13325
0
  if (eoinfo.failed)
13326
0
    goto error_return;
13327
13328
  /* If backend needs to output some local symbols not present in the hash
13329
     table, do it now.  */
13330
0
  if (obed->elf_backend_output_arch_local_syms)
13331
0
    {
13332
0
      if (!obed->elf_backend_output_arch_local_syms (info, &flinfo,
13333
0
                 elf_link_output_symstrtab))
13334
0
  goto error_return;
13335
0
    }
13336
13337
  /* That wrote out all the local symbols.  Finish up the symbol table
13338
     with the global symbols. Even if we want to strip everything we
13339
     can, we still need to deal with those global symbols that got
13340
     converted to local in a version script.  */
13341
13342
  /* The sh_info field records the index of the first non local symbol.  */
13343
0
  if (!symtab_hdr->sh_info)
13344
0
    symtab_hdr->sh_info = bfd_get_symcount (obfd);
13345
13346
0
  if (dynamic
13347
0
      && htab->dynsym != NULL
13348
0
      && htab->dynsym->output_section != bfd_abs_section_ptr)
13349
0
    {
13350
0
      Elf_Internal_Sym sym;
13351
0
      bfd_byte *dynsym = htab->dynsym->contents;
13352
13353
0
      o = htab->dynsym->output_section;
13354
0
      elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13355
13356
      /* Write out the section symbols for the output sections.  */
13357
0
      if (bfd_link_pic (info)
13358
0
    || htab->is_relocatable_executable)
13359
0
  {
13360
0
    asection *s;
13361
13362
0
    sym.st_size = 0;
13363
0
    sym.st_name = 0;
13364
0
    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13365
0
    sym.st_other = 0;
13366
0
    sym.st_target_internal = 0;
13367
13368
0
    for (s = obfd->sections; s != NULL; s = s->next)
13369
0
      {
13370
0
        int indx;
13371
0
        bfd_byte *dest;
13372
0
        long dynindx;
13373
13374
0
        dynindx = elf_section_data (s)->dynindx;
13375
0
        if (dynindx <= 0)
13376
0
    continue;
13377
0
        indx = elf_section_data (s)->this_idx;
13378
0
        BFD_ASSERT (indx > 0);
13379
0
        sym.st_shndx = indx;
13380
0
        if (! check_dynsym (obfd, &sym))
13381
0
    goto error_return;
13382
0
        sym.st_value = s->vma;
13383
0
        dest = dynsym + dynindx * obed->s->sizeof_sym;
13384
13385
        /* Inform the linker of the addition of this symbol.  */
13386
13387
0
        if (info->callbacks->ctf_new_dynsym)
13388
0
    info->callbacks->ctf_new_dynsym (dynindx, &sym);
13389
13390
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13391
0
      }
13392
0
  }
13393
13394
      /* Write out the local dynsyms.  */
13395
0
      if (htab->dynlocal)
13396
0
  {
13397
0
    struct elf_link_local_dynamic_entry *e;
13398
0
    for (e = htab->dynlocal; e ; e = e->next)
13399
0
      {
13400
0
        asection *s;
13401
0
        bfd_byte *dest;
13402
13403
        /* Copy the internal symbol and turn off visibility.
13404
     Note that we saved a word of storage and overwrote
13405
     the original st_name with the dynstr_index.  */
13406
0
        sym = e->isym;
13407
0
        sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13408
0
        sym.st_shndx = SHN_UNDEF;
13409
13410
0
        s = bfd_section_from_elf_index (e->input_bfd,
13411
0
                e->isym.st_shndx);
13412
0
        if (s != NULL
13413
0
      && s->output_section != NULL
13414
0
      && elf_section_data (s->output_section) != NULL)
13415
0
    {
13416
0
      sym.st_shndx =
13417
0
        elf_section_data (s->output_section)->this_idx;
13418
0
      if (! check_dynsym (obfd, &sym))
13419
0
        goto error_return;
13420
0
      sym.st_value = (s->output_section->vma
13421
0
          + s->output_offset
13422
0
          + e->isym.st_value);
13423
0
    }
13424
13425
        /* Inform the linker of the addition of this symbol.  */
13426
13427
0
        if (info->callbacks->ctf_new_dynsym)
13428
0
    info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13429
13430
0
        dest = dynsym + e->dynindx * obed->s->sizeof_sym;
13431
0
        obed->s->swap_symbol_out (obfd, &sym, dest, 0);
13432
0
      }
13433
0
  }
13434
0
    }
13435
13436
  /* We get the global symbols from the hash table.  */
13437
0
  eoinfo.failed = false;
13438
0
  eoinfo.localsyms = false;
13439
0
  eoinfo.flinfo = &flinfo;
13440
0
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13441
0
  if (eoinfo.failed)
13442
0
    goto error_return;
13443
13444
0
  if (htab->has_base_symbols)
13445
0
    {
13446
      /* Output base symbols last in DT_HASH so that they will be picked
13447
   before non-base symbols at run-time.  */
13448
0
      eoinfo.base_symbol = true;
13449
0
      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym,
13450
0
       &eoinfo);
13451
0
      if (eoinfo.failed)
13452
0
  goto error_return;
13453
0
    }
13454
13455
  /* If backend needs to output some symbols not present in the hash
13456
     table, do it now.  */
13457
0
  if (obed->elf_backend_output_arch_syms
13458
0
      && (info->strip != strip_all || emit_relocs))
13459
0
    {
13460
0
      if (!obed->elf_backend_output_arch_syms (info, &flinfo,
13461
0
                 elf_link_output_symstrtab))
13462
0
  goto error_return;
13463
0
    }
13464
13465
  /* Finalize the .strtab section.  */
13466
0
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
13467
13468
  /* Swap out the .strtab section. */
13469
0
  if (!elf_link_swap_symbols_out (&flinfo))
13470
0
    goto error_return;
13471
0
  free (htab->strtab);
13472
0
  htab->strtab = NULL;
13473
13474
  /* Now we know the size of the symtab section.  */
13475
0
  if (bfd_get_symcount (obfd) > 0)
13476
0
    {
13477
      /* Finish up and write out the symbol string table (.strtab)
13478
   section.  */
13479
0
      Elf_Internal_Shdr *symstrtab_hdr = NULL;
13480
0
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13481
13482
0
      if (elf_symtab_shndx_list (obfd))
13483
0
  {
13484
0
    symtab_shndx_hdr = & elf_symtab_shndx_list (obfd)->hdr;
13485
13486
0
    if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13487
0
      {
13488
0
        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13489
0
        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13490
0
        symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13491
0
        amt = bfd_get_symcount (obfd) * sizeof (Elf_External_Sym_Shndx);
13492
0
        symtab_shndx_hdr->sh_size = amt;
13493
13494
0
        off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13495
0
                     off, true, 0);
13496
13497
0
        if (bfd_seek (obfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13498
0
      || (bfd_write (flinfo.symshndxbuf, amt, obfd) != amt))
13499
0
    goto error_return;
13500
0
      }
13501
0
  }
13502
13503
0
      symstrtab_hdr = &elf_tdata (obfd)->strtab_hdr;
13504
0
      symstrtab_hdr->sh_type = SHT_STRTAB;
13505
0
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13506
0
      symstrtab_hdr->sh_addralign = 1;
13507
13508
0
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13509
0
                   off, true, 0);
13510
0
      elf_next_file_pos (obfd) = off;
13511
13512
0
      if (bfd_seek (obfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13513
0
    || ! _bfd_elf_strtab_emit (obfd, flinfo.symstrtab))
13514
0
  goto error_return;
13515
0
    }
13516
13517
0
  if (info->out_implib_bfd && !elf_output_implib (obfd, info))
13518
0
    {
13519
0
      _bfd_error_handler (_("%pB: failed to generate import library"),
13520
0
        info->out_implib_bfd);
13521
0
      goto error_return;
13522
0
    }
13523
13524
  /* Adjust the relocs to have the correct symbol indices.  */
13525
0
  for (o = obfd->sections; o != NULL; o = o->next)
13526
0
    {
13527
0
      struct bfd_elf_section_data *esdo = elf_section_data (o);
13528
0
      bool sort;
13529
13530
0
      if ((o->flags & SEC_RELOC) == 0)
13531
0
  continue;
13532
13533
0
      sort = obed->sort_relocs_p == NULL || obed->sort_relocs_p (o);
13534
0
      if (esdo->rel.hdr != NULL
13535
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rel, sort, info))
13536
0
  goto error_return;
13537
0
      if (esdo->rela.hdr != NULL
13538
0
    && !elf_link_adjust_relocs (obfd, o, &esdo->rela, sort, info))
13539
0
  goto error_return;
13540
13541
      /* Set the reloc_count field to 0 to prevent write_relocs from
13542
   trying to swap the relocs out itself.  */
13543
0
      o->reloc_count = 0;
13544
0
    }
13545
13546
0
  relativecount = 0;
13547
0
  if (dynamic && info->combreloc && dynobj != NULL)
13548
0
    relativecount = elf_link_sort_relocs (obfd, info, &reldyn);
13549
13550
0
  relr_entsize = 0;
13551
0
  if (htab->srelrdyn != NULL
13552
0
      && htab->srelrdyn->output_section != NULL
13553
0
      && htab->srelrdyn->size != 0)
13554
0
    {
13555
0
      asection *s = htab->srelrdyn->output_section;
13556
0
      relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13557
0
      if (relr_entsize == 0)
13558
0
  {
13559
0
    relr_entsize = obed->s->arch_size / 8;
13560
0
    elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13561
0
  }
13562
0
    }
13563
13564
  /* If we are linking against a dynamic object, or generating a
13565
     shared library, finish up the dynamic linking information.  */
13566
0
  if (dynamic)
13567
0
    {
13568
0
      bfd_byte *dyncon, *dynconend;
13569
13570
      /* Fix up .dynamic entries.  */
13571
0
      o = htab->dynamic;
13572
0
      BFD_ASSERT (o != NULL);
13573
13574
0
      dyncon = o->contents;
13575
0
      dynconend = PTR_ADD (o->contents, o->size);
13576
0
      for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13577
0
  {
13578
0
    Elf_Internal_Dyn dyn;
13579
0
    const char *name;
13580
0
    unsigned int type;
13581
0
    bfd_size_type sh_size;
13582
0
    bfd_vma sh_addr;
13583
13584
0
    obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13585
13586
0
    switch (dyn.d_tag)
13587
0
      {
13588
0
      default:
13589
0
        continue;
13590
0
      case DT_NULL:
13591
0
        if (relativecount != 0)
13592
0
    {
13593
0
      switch (elf_section_data (reldyn)->this_hdr.sh_type)
13594
0
        {
13595
0
        case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13596
0
        case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13597
0
        }
13598
0
      if (dyn.d_tag != DT_NULL
13599
0
          && dynconend - dyncon >= obed->s->sizeof_dyn)
13600
0
        {
13601
0
          dyn.d_un.d_val = relativecount;
13602
0
          relativecount = 0;
13603
0
          break;
13604
0
        }
13605
0
      relativecount = 0;
13606
0
    }
13607
0
        if (relr_entsize != 0)
13608
0
    {
13609
0
      if (dynconend - dyncon >= 3 * obed->s->sizeof_dyn)
13610
0
        {
13611
0
          asection *s = htab->srelrdyn;
13612
0
          dyn.d_tag = DT_RELR;
13613
0
          dyn.d_un.d_ptr
13614
0
      = s->output_section->vma + s->output_offset;
13615
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13616
0
          dyncon += obed->s->sizeof_dyn;
13617
13618
0
          dyn.d_tag = DT_RELRSZ;
13619
0
          dyn.d_un.d_val = s->size;
13620
0
          obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13621
0
          dyncon += obed->s->sizeof_dyn;
13622
13623
0
          dyn.d_tag = DT_RELRENT;
13624
0
          dyn.d_un.d_val = relr_entsize;
13625
0
          relr_entsize = 0;
13626
0
          break;
13627
0
        }
13628
0
      relr_entsize = 0;
13629
0
    }
13630
0
        continue;
13631
13632
0
      case DT_INIT:
13633
0
        name = info->init_function;
13634
0
        goto get_sym;
13635
0
      case DT_FINI:
13636
0
        name = info->fini_function;
13637
0
      get_sym:
13638
0
        {
13639
0
    struct elf_link_hash_entry *h;
13640
13641
0
    h = elf_link_hash_lookup (htab, name, false, false, true);
13642
0
    if (h != NULL
13643
0
        && (h->root.type == bfd_link_hash_defined
13644
0
      || h->root.type == bfd_link_hash_defweak))
13645
0
      {
13646
0
        dyn.d_un.d_ptr = h->root.u.def.value;
13647
0
        o = h->root.u.def.section;
13648
0
        if (o->output_section != NULL)
13649
0
          dyn.d_un.d_ptr += (o->output_section->vma
13650
0
           + o->output_offset);
13651
0
        else
13652
0
          {
13653
      /* The symbol is imported from another shared
13654
         library and does not apply to this one.  */
13655
0
      dyn.d_un.d_ptr = 0;
13656
0
          }
13657
0
        break;
13658
0
      }
13659
0
        }
13660
0
        continue;
13661
13662
0
      case DT_PREINIT_ARRAYSZ:
13663
0
        name = ".preinit_array";
13664
0
        goto get_out_size;
13665
0
      case DT_INIT_ARRAYSZ:
13666
0
        name = ".init_array";
13667
0
        goto get_out_size;
13668
0
      case DT_FINI_ARRAYSZ:
13669
0
        name = ".fini_array";
13670
0
      get_out_size:
13671
0
        o = bfd_get_section_by_name (obfd, name);
13672
0
        if (o == NULL)
13673
0
    {
13674
0
      _bfd_error_handler
13675
0
        (_("could not find section %s"), name);
13676
0
      goto error_return;
13677
0
    }
13678
0
        if (o->size == 0)
13679
0
    _bfd_error_handler
13680
0
      (_("warning: %s section has zero size"), name);
13681
0
        dyn.d_un.d_val = o->size;
13682
0
        break;
13683
13684
0
      case DT_PREINIT_ARRAY:
13685
0
        name = ".preinit_array";
13686
0
        goto get_out_vma;
13687
0
      case DT_INIT_ARRAY:
13688
0
        name = ".init_array";
13689
0
        goto get_out_vma;
13690
0
      case DT_FINI_ARRAY:
13691
0
        name = ".fini_array";
13692
0
      get_out_vma:
13693
0
        o = bfd_get_section_by_name (obfd, name);
13694
0
        goto do_vma;
13695
13696
0
      case DT_HASH:
13697
0
        name = ".hash";
13698
0
        goto get_vma;
13699
0
      case DT_GNU_HASH:
13700
0
        name = ".gnu.hash";
13701
0
        goto get_vma;
13702
0
      case DT_STRTAB:
13703
0
        name = ".dynstr";
13704
0
        goto get_vma;
13705
0
      case DT_SYMTAB:
13706
0
        name = ".dynsym";
13707
0
        goto get_vma;
13708
0
      case DT_VERDEF:
13709
0
        name = ".gnu.version_d";
13710
0
        goto get_vma;
13711
0
      case DT_VERNEED:
13712
0
        name = ".gnu.version_r";
13713
0
        goto get_vma;
13714
0
      case DT_VERSYM:
13715
0
        name = ".gnu.version";
13716
0
      get_vma:
13717
0
        o = bfd_get_linker_section (dynobj, name);
13718
0
      do_vma:
13719
0
        if (o == NULL || bfd_is_abs_section (o->output_section))
13720
0
    {
13721
0
      _bfd_error_handler
13722
0
        (_("could not find section %s"), name);
13723
0
      goto error_return;
13724
0
    }
13725
0
        if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13726
0
    {
13727
0
      _bfd_error_handler
13728
0
        (_("warning: section '%s' is being made into a note"), name);
13729
0
      bfd_set_error (bfd_error_nonrepresentable_section);
13730
0
      goto error_return;
13731
0
    }
13732
0
        dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13733
0
        break;
13734
13735
0
      case DT_REL:
13736
0
      case DT_RELA:
13737
0
      case DT_RELSZ:
13738
0
      case DT_RELASZ:
13739
0
        if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13740
0
    type = SHT_REL;
13741
0
        else
13742
0
    type = SHT_RELA;
13743
0
        sh_size = 0;
13744
0
        sh_addr = 0;
13745
0
        for (i = 1; i < elf_numsections (obfd); i++)
13746
0
    {
13747
0
      Elf_Internal_Shdr *hdr;
13748
13749
0
      hdr = elf_elfsections (obfd)[i];
13750
0
      if (hdr->sh_type == type
13751
0
          && (hdr->sh_flags & SHF_ALLOC) != 0)
13752
0
        {
13753
0
          sh_size += hdr->sh_size;
13754
0
          if (sh_addr == 0
13755
0
        || sh_addr > hdr->sh_addr)
13756
0
      sh_addr = hdr->sh_addr;
13757
0
        }
13758
0
    }
13759
13760
0
        if (obed->dtrel_excludes_plt && htab->srelplt != NULL)
13761
0
    {
13762
0
      unsigned int opb = bfd_octets_per_byte (obfd, o);
13763
13764
      /* Don't count procedure linkage table relocs in the
13765
         overall reloc count.  */
13766
0
      sh_size -= htab->srelplt->size;
13767
0
      if (sh_size == 0)
13768
        /* If the size is zero, make the address zero too.
13769
           This is to avoid a glibc bug.  If the backend
13770
           emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13771
           zero, then we'll put DT_RELA at the end of
13772
           DT_JMPREL.  glibc will interpret the end of
13773
           DT_RELA matching the end of DT_JMPREL as the
13774
           case where DT_RELA includes DT_JMPREL, and for
13775
           LD_BIND_NOW will decide that processing DT_RELA
13776
           will process the PLT relocs too.  Net result:
13777
           No PLT relocs applied.  */
13778
0
        sh_addr = 0;
13779
13780
      /* If .rela.plt is the first .rela section, exclude
13781
         it from DT_RELA.  */
13782
0
      else if (sh_addr == (htab->srelplt->output_section->vma
13783
0
               + htab->srelplt->output_offset) * opb)
13784
0
        sh_addr += htab->srelplt->size;
13785
0
    }
13786
13787
0
        if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13788
0
    dyn.d_un.d_val = sh_size;
13789
0
        else
13790
0
    dyn.d_un.d_ptr = sh_addr;
13791
0
        break;
13792
0
      }
13793
0
    obed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13794
0
  }
13795
0
    }
13796
13797
  /* If we have created any dynamic sections, then output them.  */
13798
0
  if (dynobj != NULL)
13799
0
    {
13800
0
      if (!obed->elf_backend_finish_dynamic_sections (info, flinfo.contents))
13801
0
  goto error_return;
13802
13803
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
13804
0
      if (bfd_link_textrel_check (info)
13805
0
    && (o = htab->dynamic) != NULL
13806
0
    && o->size != 0)
13807
0
  {
13808
0
    bfd_byte *dyncon, *dynconend;
13809
13810
0
    dyncon = o->contents;
13811
0
    dynconend = o->contents + o->size;
13812
0
    for (; dyncon < dynconend; dyncon += obed->s->sizeof_dyn)
13813
0
      {
13814
0
        Elf_Internal_Dyn dyn;
13815
13816
0
        obed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13817
13818
0
        if (dyn.d_tag == DT_TEXTREL)
13819
0
    {
13820
0
      if (info->textrel_check == textrel_check_error)
13821
0
        info->callbacks->einfo
13822
0
          (_("%P%X: read-only segment has dynamic relocations\n"));
13823
0
      else if (bfd_link_dll (info))
13824
0
        info->callbacks->einfo
13825
0
          (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13826
0
      else if (bfd_link_pde (info))
13827
0
        info->callbacks->einfo
13828
0
          (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13829
0
      else
13830
0
        info->callbacks->einfo
13831
0
          (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13832
0
      break;
13833
0
    }
13834
0
      }
13835
0
  }
13836
13837
0
      for (o = dynobj->sections; o != NULL; o = o->next)
13838
0
  {
13839
0
    if ((o->flags & SEC_HAS_CONTENTS) == 0
13840
0
        || o->size == 0
13841
0
        || o->output_section == bfd_abs_section_ptr)
13842
0
      continue;
13843
0
    if ((o->flags & SEC_LINKER_CREATED) == 0)
13844
0
      {
13845
        /* At this point, we are only interested in sections
13846
     created by bfd_elf_link_create_dynamic_sections().  */
13847
0
        continue;
13848
0
      }
13849
0
    if (htab->stab_info.stabstr == o)
13850
0
      continue;
13851
0
    if (htab->eh_info.hdr_sec == o)
13852
0
      continue;
13853
0
    if (strcmp (o->name, ".dynstr") != 0)
13854
0
      {
13855
0
        bfd_size_type octets = ((file_ptr) o->output_offset
13856
0
              * bfd_octets_per_byte (obfd, o));
13857
0
        if (!bfd_set_section_contents (obfd, o->output_section,
13858
0
               o->contents, octets, o->size))
13859
0
    goto error_return;
13860
0
      }
13861
0
    else
13862
0
      {
13863
        /* The contents of the .dynstr section are actually in a
13864
     stringtab.  */
13865
0
        file_ptr off;
13866
13867
0
        off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13868
0
        if (bfd_seek (obfd, off, SEEK_SET) != 0
13869
0
      || !_bfd_elf_strtab_emit (obfd, htab->dynstr))
13870
0
    goto error_return;
13871
0
      }
13872
0
  }
13873
0
    }
13874
13875
0
  if (!info->resolve_section_groups)
13876
0
    {
13877
0
      bool failed = false;
13878
13879
0
      BFD_ASSERT (bfd_link_relocatable (info));
13880
0
      bfd_map_over_sections (obfd, bfd_elf_set_group_contents, &failed);
13881
0
      if (failed)
13882
0
  goto error_return;
13883
0
    }
13884
13885
  /* If we have optimized stabs strings, output them.  */
13886
0
  if (htab->stab_info.stabstr != NULL)
13887
0
    {
13888
0
      if (!_bfd_write_stab_strings (obfd, &htab->stab_info))
13889
0
  goto error_return;
13890
0
    }
13891
13892
0
  if (! _bfd_elf_write_section_eh_frame_hdr (obfd, info))
13893
0
    goto error_return;
13894
13895
0
  if (! _bfd_elf_write_section_sframe (obfd, info))
13896
0
    goto error_return;
13897
13898
0
  if (! _bfd_elf_write_section_object_attributes (obfd, info))
13899
0
    goto error_ret2;
13900
13901
0
  if (info->callbacks->emit_ctf)
13902
0
      info->callbacks->emit_ctf ();
13903
13904
0
  elf_final_link_free (obfd, &flinfo);
13905
13906
0
  if (info->unique_symbol)
13907
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13908
0
  return true;
13909
13910
0
 error_return:
13911
0
  free (htab->strtab);
13912
0
  htab->strtab = NULL;
13913
0
  elf_final_link_free (obfd, &flinfo);
13914
0
 error_ret2:
13915
0
  if (info->unique_symbol)
13916
0
    bfd_hash_table_free (&flinfo.local_hash_table);
13917
0
  return false;
13918
0
}
13919

13920
/* Initialize COOKIE for input bfd ABFD.  */
13921
13922
static bool
13923
init_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13924
0
{
13925
0
  Elf_Internal_Shdr *symtab_hdr;
13926
0
  elf_backend_data *bed;
13927
13928
0
  bed = get_elf_backend_data (abfd);
13929
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13930
13931
0
  cookie->abfd = abfd;
13932
0
  cookie->num_sym = symtab_hdr->sh_size / bed->s->sizeof_sym;
13933
0
  if (elf_bad_symtab (abfd))
13934
0
    {
13935
0
      cookie->locsymcount = cookie->num_sym;
13936
0
      cookie->extsymoff = 0;
13937
0
    }
13938
0
  else
13939
0
    {
13940
0
      cookie->locsymcount = symtab_hdr->sh_info;
13941
0
      cookie->extsymoff = symtab_hdr->sh_info;
13942
0
    }
13943
13944
0
  if (bed->s->arch_size == 32)
13945
0
    cookie->r_sym_shift = 8;
13946
0
  else
13947
0
    cookie->r_sym_shift = 32;
13948
13949
0
  return true;
13950
0
}
13951
13952
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
13953
13954
static void
13955
fini_reloc_cookie (struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
13956
       bfd *abfd ATTRIBUTE_UNUSED)
13957
0
{
13958
0
}
13959
13960
/* Initialize the relocation information in COOKIE for input section SEC
13961
   of input bfd ABFD.  */
13962
13963
static bool
13964
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13965
      struct bfd_link_info *info, bfd *abfd,
13966
      asection *sec, bool keep_memory)
13967
0
{
13968
0
  if (sec->reloc_count == 0)
13969
0
    {
13970
0
      cookie->rels = NULL;
13971
0
      cookie->relend = NULL;
13972
0
    }
13973
0
  else
13974
0
    {
13975
0
      keep_memory = keep_memory || _bfd_elf_link_keep_memory (info);
13976
0
      cookie->rels
13977
0
  = _bfd_elf_link_info_read_relocs (abfd, info, sec, NULL, NULL,
13978
0
            keep_memory);
13979
0
      if (cookie->rels == NULL)
13980
0
  return false;
13981
0
      cookie->rel = cookie->rels;
13982
0
      cookie->relend = cookie->rels + sec->reloc_count;
13983
0
    }
13984
0
  cookie->rel = cookie->rels;
13985
0
  return true;
13986
0
}
13987
13988
/* Free the memory allocated by init_reloc_cookie_rels,
13989
   if appropriate.  */
13990
13991
static void
13992
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13993
      asection *sec)
13994
0
{
13995
0
  if (elf_section_data (sec)->relocs != cookie->rels)
13996
0
    free (cookie->rels);
13997
0
}
13998
13999
/* Initialize the whole of COOKIE for input section SEC.  */
14000
14001
static bool
14002
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14003
             struct bfd_link_info *info,
14004
             asection *sec, bool keep_memory)
14005
0
{
14006
0
  if (!init_reloc_cookie (cookie, sec->owner))
14007
0
    goto error1;
14008
0
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
14009
0
             keep_memory))
14010
0
    goto error2;
14011
0
  return true;
14012
14013
0
 error2:
14014
0
  fini_reloc_cookie (cookie, sec->owner);
14015
0
 error1:
14016
0
  return false;
14017
0
}
14018
14019
/* Free the memory allocated by init_reloc_cookie_for_section,
14020
   if appropriate.  */
14021
14022
static void
14023
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
14024
             asection *sec)
14025
0
{
14026
0
  fini_reloc_cookie_rels (cookie, sec);
14027
0
  fini_reloc_cookie (cookie, sec->owner);
14028
0
}
14029

14030
/* Garbage collect unused sections.  */
14031
14032
/* Default gc_mark_hook.  */
14033
14034
asection *
14035
_bfd_elf_gc_mark_hook (asection *sec ATTRIBUTE_UNUSED,
14036
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
14037
           struct elf_reloc_cookie *cookie,
14038
           struct elf_link_hash_entry *h,
14039
           unsigned int symndx)
14040
0
{
14041
0
  if (h == NULL)
14042
0
    return _bfd_get_local_sym_section (cookie, symndx);
14043
14044
0
  switch (h->root.type)
14045
0
    {
14046
0
    case bfd_link_hash_defined:
14047
0
    case bfd_link_hash_defweak:
14048
0
      return h->root.u.def.section;
14049
14050
0
    case bfd_link_hash_common:
14051
0
      return h->root.u.c.p->section;
14052
14053
0
    default:
14054
0
      return NULL;
14055
0
    }
14056
0
}
14057
14058
/* Return the debug definition section.  */
14059
14060
static asection *
14061
elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14062
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
14063
         struct elf_reloc_cookie *cookie,
14064
         struct elf_link_hash_entry *h,
14065
         unsigned int symndx)
14066
0
{
14067
0
  if (h != NULL)
14068
0
    {
14069
      /* Return the global debug definition section.  */
14070
0
      if ((h->root.type == bfd_link_hash_defined
14071
0
     || h->root.type == bfd_link_hash_defweak)
14072
0
    && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14073
0
  return h->root.u.def.section;
14074
0
    }
14075
0
  else
14076
0
    {
14077
      /* Return the local debug definition section.  */
14078
0
      asection *isec = _bfd_get_local_sym_section (cookie, symndx);
14079
0
      if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14080
0
  return isec;
14081
0
    }
14082
14083
0
  return NULL;
14084
0
}
14085
14086
/* COOKIE->rel describes a relocation against section SEC, which is
14087
   a section we've decided to keep.  Return the section that contains
14088
   the relocation symbol, or NULL if no section contains it.  */
14089
14090
asection *
14091
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14092
           elf_gc_mark_hook_fn gc_mark_hook,
14093
           struct elf_reloc_cookie *cookie,
14094
           bool *start_stop)
14095
0
{
14096
0
  unsigned long r_symndx;
14097
0
  struct elf_link_hash_entry *h, *hw;
14098
14099
0
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14100
0
  if (r_symndx == STN_UNDEF)
14101
0
    return NULL;
14102
14103
0
  h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14104
0
  if (h == NULL)
14105
0
    {
14106
      /* A corrupt input file can lead to a situation where the index
14107
   does not reference either a local or an external symbol.  */
14108
0
      if (r_symndx >= cookie->locsymcount)
14109
0
  return NULL;
14110
14111
0
      return (*gc_mark_hook) (sec, info, cookie, NULL, r_symndx);
14112
0
    }
14113
14114
0
  bool was_marked = h->mark;
14115
14116
0
  h->mark = 1;
14117
  /* Keep all aliases of the symbol too.  If an object symbol
14118
     needs to be copied into .dynbss then all of its aliases
14119
     should be present as dynamic symbols, not just the one used
14120
     on the copy relocation.  */
14121
0
  hw = h;
14122
0
  while (hw->is_weakalias)
14123
0
    {
14124
0
      hw = hw->u.alias;
14125
0
      hw->mark = 1;
14126
0
    }
14127
14128
0
  if (!was_marked && h->start_stop && !h->root.ldscript_def)
14129
0
    {
14130
0
      if (info->start_stop_gc)
14131
0
  return NULL;
14132
14133
      /* To work around a glibc bug, mark XXX input sections
14134
   when there is a reference to __start_XXX or __stop_XXX
14135
   symbols.  */
14136
0
      else if (start_stop != NULL)
14137
0
  {
14138
0
    asection *s = h->u2.start_stop_section;
14139
0
    *start_stop = true;
14140
0
    return s;
14141
0
  }
14142
0
    }
14143
14144
0
  return (*gc_mark_hook) (sec, info, cookie, h, 0);
14145
0
}
14146
14147
/* COOKIE->rel describes a relocation against section SEC, which is
14148
   a section we've decided to keep.  Mark the section that contains
14149
   the relocation symbol.  */
14150
14151
bool
14152
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14153
      asection *sec,
14154
      elf_gc_mark_hook_fn gc_mark_hook,
14155
      struct elf_reloc_cookie *cookie)
14156
0
{
14157
0
  asection *rsec;
14158
0
  bool start_stop = false;
14159
14160
0
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14161
0
  while (rsec != NULL)
14162
0
    {
14163
0
      if (!rsec->gc_mark)
14164
0
  {
14165
0
    if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14166
0
        || (rsec->owner->flags & DYNAMIC) != 0)
14167
0
      rsec->gc_mark = 1;
14168
0
    else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14169
0
      return false;
14170
0
  }
14171
0
      if (!start_stop)
14172
0
  break;
14173
0
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14174
0
    }
14175
0
  return true;
14176
0
}
14177
14178
/* The mark phase of garbage collection.  For a given section, mark
14179
   it and any sections in this section's group, and all the sections
14180
   which define symbols to which it refers.  */
14181
14182
bool
14183
_bfd_elf_gc_mark (struct bfd_link_info *info,
14184
      asection *sec,
14185
      elf_gc_mark_hook_fn gc_mark_hook)
14186
0
{
14187
0
  bool ret;
14188
0
  asection *group_sec, *eh_frame, *sframe;
14189
14190
0
  sec->gc_mark = 1;
14191
14192
  /* Mark all the sections in the group.  */
14193
0
  group_sec = elf_section_data (sec)->next_in_group;
14194
0
  if (group_sec && !group_sec->gc_mark)
14195
0
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14196
0
      return false;
14197
14198
  /* Look through the section relocs.  */
14199
0
  ret = true;
14200
0
  eh_frame = elf_eh_frame_section (sec->owner);
14201
0
  sframe = elf_sframe_section (sec->owner);
14202
14203
0
  if ((sec->flags & SEC_RELOC) != 0
14204
0
      && sec->reloc_count > 0
14205
0
      && sec != eh_frame
14206
0
      && sec != sframe)
14207
0
    {
14208
0
      struct elf_reloc_cookie cookie;
14209
14210
0
      if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14211
0
  ret = false;
14212
0
      else
14213
0
  {
14214
0
    for (; cookie.rel < cookie.relend; cookie.rel++)
14215
0
      if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14216
0
        {
14217
0
    ret = false;
14218
0
    break;
14219
0
        }
14220
0
    fini_reloc_cookie_for_section (&cookie, sec);
14221
0
  }
14222
0
    }
14223
14224
0
  if (ret && eh_frame && elf_fde_list (sec))
14225
0
    {
14226
0
      struct elf_reloc_cookie cookie;
14227
14228
      /* NB: When --no-keep-memory is used, the symbol table and
14229
   relocation info for eh_frame are freed after they are retrieved
14230
   for each text section in the input object.  If an input object
14231
   has many text sections, the same data is retrieved and freed
14232
   many times which can take a very long time.  Always keep the
14233
   symbol table and relocation info for eh_frame to avoid it.  */
14234
0
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14235
0
            true))
14236
0
  ret = false;
14237
0
      else
14238
0
  {
14239
0
    if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14240
0
              gc_mark_hook, &cookie))
14241
0
      ret = false;
14242
0
    fini_reloc_cookie_for_section (&cookie, eh_frame);
14243
0
  }
14244
0
    }
14245
14246
0
  eh_frame = elf_section_eh_frame_entry (sec);
14247
0
  if (ret && eh_frame && !eh_frame->gc_mark)
14248
0
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14249
0
      ret = false;
14250
14251
0
  return ret;
14252
0
}
14253
14254
/* Scan and mark sections in a special or debug section group.  */
14255
14256
static void
14257
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14258
0
{
14259
  /* Point to first section of section group.  */
14260
0
  asection *ssec;
14261
  /* Used to iterate the section group.  */
14262
0
  asection *msec;
14263
14264
0
  bool is_special_grp = true;
14265
0
  bool is_debug_grp = true;
14266
14267
  /* First scan to see if group contains any section other than debug
14268
     and special section.  */
14269
0
  ssec = msec = elf_next_in_group (grp);
14270
0
  while (msec != NULL)
14271
0
    {
14272
0
      if ((msec->flags & SEC_DEBUGGING) == 0)
14273
0
  is_debug_grp = false;
14274
14275
0
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14276
0
  is_special_grp = false;
14277
14278
0
      msec = elf_next_in_group (msec);
14279
0
      if (msec == ssec)
14280
0
  break;
14281
0
    }
14282
14283
  /* If this is a pure debug section group or pure special section group,
14284
     keep all sections in this group.  */
14285
0
  if (is_debug_grp || is_special_grp)
14286
0
    {
14287
0
      msec = ssec;
14288
0
      while (msec != NULL)
14289
0
  {
14290
0
    msec->gc_mark = 1;
14291
0
    msec = elf_next_in_group (msec);
14292
0
    if (msec == ssec)
14293
0
      break;
14294
0
  }
14295
0
    }
14296
0
}
14297
14298
/* Keep debug and special sections.  */
14299
14300
bool
14301
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14302
         elf_gc_mark_hook_fn mark_hook)
14303
0
{
14304
0
  bfd *ibfd;
14305
14306
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14307
0
    {
14308
0
      asection *isec;
14309
0
      bool some_kept;
14310
0
      bool debug_frag_seen;
14311
0
      bool has_kept_debug_info;
14312
14313
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14314
0
  continue;
14315
0
      isec = ibfd->sections;
14316
0
      if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14317
0
  continue;
14318
14319
      /* Ensure all linker created sections are kept,
14320
   see if any other section is already marked,
14321
   and note if we have any fragmented debug sections.  */
14322
0
      debug_frag_seen = some_kept = has_kept_debug_info = false;
14323
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14324
0
  {
14325
0
    if ((isec->flags & SEC_LINKER_CREATED) != 0)
14326
0
      isec->gc_mark = 1;
14327
0
    else if (isec->gc_mark
14328
0
       && (isec->flags & SEC_ALLOC) != 0
14329
0
       && elf_section_type (isec) != SHT_NOTE)
14330
0
      some_kept = true;
14331
0
    else
14332
0
      {
14333
        /* Since all sections, except for backend specific ones,
14334
     have been garbage collected, call mark_hook on this
14335
     section if any of its linked-to sections is marked.  */
14336
0
        asection *linked_to_sec;
14337
0
        for (linked_to_sec = elf_linked_to_section (isec);
14338
0
       linked_to_sec != NULL && !linked_to_sec->linker_mark;
14339
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14340
0
    {
14341
0
      if (linked_to_sec->gc_mark)
14342
0
        {
14343
0
          if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14344
0
      return false;
14345
0
          break;
14346
0
        }
14347
0
      linked_to_sec->linker_mark = 1;
14348
0
    }
14349
0
        for (linked_to_sec = elf_linked_to_section (isec);
14350
0
       linked_to_sec != NULL && linked_to_sec->linker_mark;
14351
0
       linked_to_sec = elf_linked_to_section (linked_to_sec))
14352
0
    linked_to_sec->linker_mark = 0;
14353
0
      }
14354
14355
0
    if (!debug_frag_seen
14356
0
        && (isec->flags & SEC_DEBUGGING)
14357
0
        && startswith (isec->name, ".debug_line."))
14358
0
      debug_frag_seen = true;
14359
0
    else if (strcmp (bfd_section_name (isec),
14360
0
         "__patchable_function_entries") == 0
14361
0
       && elf_linked_to_section (isec) == NULL)
14362
0
        info->callbacks->fatal (_("%P: %pB(%pA): error: "
14363
0
          "need linked-to section "
14364
0
          "for --gc-sections\n"),
14365
0
              isec->owner, isec);
14366
0
  }
14367
14368
      /* If no non-note alloc section in this file will be kept, then
14369
   we can toss out the debug and special sections.  */
14370
0
      if (!some_kept)
14371
0
  continue;
14372
14373
      /* Keep debug and special sections like .comment when they are
14374
   not part of a group.  Also keep section groups that contain
14375
   just debug sections or special sections.  NB: Sections with
14376
   linked-to section has been handled above.  */
14377
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14378
0
  {
14379
0
    if ((isec->flags & SEC_GROUP) != 0)
14380
0
      _bfd_elf_gc_mark_debug_special_section_group (isec);
14381
0
    else if (((isec->flags & SEC_DEBUGGING) != 0
14382
0
        || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14383
0
       && elf_next_in_group (isec) == NULL
14384
0
       && elf_linked_to_section (isec) == NULL)
14385
0
      isec->gc_mark = 1;
14386
0
    if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14387
0
      has_kept_debug_info = true;
14388
0
  }
14389
14390
      /* Look for CODE sections which are going to be discarded,
14391
   and find and discard any fragmented debug sections which
14392
   are associated with that code section.  */
14393
0
      if (debug_frag_seen)
14394
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14395
0
    if ((isec->flags & SEC_CODE) != 0
14396
0
        && isec->gc_mark == 0)
14397
0
      {
14398
0
        unsigned int ilen;
14399
0
        asection *dsec;
14400
14401
0
        ilen = strlen (isec->name);
14402
14403
        /* Association is determined by the name of the debug
14404
     section containing the name of the code section as
14405
     a suffix.  For example .debug_line.text.foo is a
14406
     debug section associated with .text.foo.  */
14407
0
        for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14408
0
    {
14409
0
      unsigned int dlen;
14410
14411
0
      if (dsec->gc_mark == 0
14412
0
          || (dsec->flags & SEC_DEBUGGING) == 0)
14413
0
        continue;
14414
14415
0
      dlen = strlen (dsec->name);
14416
14417
0
      if (dlen > ilen
14418
0
          && strncmp (dsec->name + (dlen - ilen),
14419
0
          isec->name, ilen) == 0)
14420
0
        dsec->gc_mark = 0;
14421
0
    }
14422
0
    }
14423
14424
      /* Mark debug sections referenced by kept debug sections.  */
14425
0
      if (has_kept_debug_info)
14426
0
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14427
0
    if (isec->gc_mark
14428
0
        && (isec->flags & SEC_DEBUGGING) != 0)
14429
0
      if (!_bfd_elf_gc_mark (info, isec,
14430
0
           elf_gc_mark_debug_section))
14431
0
        return false;
14432
0
    }
14433
0
  return true;
14434
0
}
14435
14436
static bool
14437
elf_gc_sweep (struct bfd_link_info *info)
14438
0
{
14439
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14440
0
    {
14441
0
      if (!compatible_format (info, sub))
14442
0
  continue;
14443
14444
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14445
0
  {
14446
    /* When any section in a section group is kept, we keep all
14447
       sections in the section group.  If the first member of
14448
       the section group is excluded, we will also exclude the
14449
       group section.  */
14450
0
    if (o->flags & SEC_GROUP)
14451
0
      {
14452
0
        asection *first = elf_next_in_group (o);
14453
0
        if (first != NULL)
14454
0
    o->gc_mark = first->gc_mark;
14455
0
      }
14456
14457
0
    if (o->gc_mark)
14458
0
      continue;
14459
14460
    /* Skip sweeping sections already excluded.  */
14461
0
    if (o->flags & SEC_EXCLUDE)
14462
0
      continue;
14463
14464
    /* Since this is early in the link process, it is simple
14465
       to remove a section from the output.  */
14466
0
    o->flags |= SEC_EXCLUDE;
14467
14468
0
    if (info->print_gc_sections && o->size != 0)
14469
      /* xgettext:c-format */
14470
0
      _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14471
0
        o, sub);
14472
0
  }
14473
0
    }
14474
14475
0
  return true;
14476
0
}
14477
14478
/* Propagate collected vtable information.  This is called through
14479
   elf_link_hash_traverse.  */
14480
14481
static bool
14482
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14483
0
{
14484
  /* Those that are not vtables.  */
14485
0
  if (h->start_stop
14486
0
      || h->u2.vtable == NULL
14487
0
      || h->u2.vtable->parent == NULL)
14488
0
    return true;
14489
14490
  /* Those vtables that do not have parents, we cannot merge.  */
14491
0
  if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14492
0
    return true;
14493
14494
  /* If we've already been done, exit.  */
14495
0
  if (h->u2.vtable->used && h->u2.vtable->used[-1])
14496
0
    return true;
14497
14498
  /* Make sure the parent's table is up to date.  */
14499
0
  elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14500
14501
0
  if (h->u2.vtable->used == NULL)
14502
0
    {
14503
      /* None of this table's entries were referenced.  Re-use the
14504
   parent's table.  */
14505
0
      h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14506
0
      h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14507
0
    }
14508
0
  else
14509
0
    {
14510
0
      size_t n;
14511
0
      bool *cu, *pu;
14512
14513
      /* Or the parent's entries into ours.  */
14514
0
      cu = h->u2.vtable->used;
14515
0
      cu[-1] = true;
14516
0
      pu = h->u2.vtable->parent->u2.vtable->used;
14517
0
      if (pu != NULL)
14518
0
  {
14519
0
    elf_backend_data *bed;
14520
0
    unsigned int log_file_align;
14521
14522
0
    bed = get_elf_backend_data (h->root.u.def.section->owner);
14523
0
    log_file_align = bed->s->log_file_align;
14524
0
    n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14525
0
    while (n--)
14526
0
      {
14527
0
        if (*pu)
14528
0
    *cu = true;
14529
0
        pu++;
14530
0
        cu++;
14531
0
      }
14532
0
  }
14533
0
    }
14534
14535
0
  return true;
14536
0
}
14537
14538
struct link_info_ok
14539
{
14540
  struct bfd_link_info *info;
14541
  bool ok;
14542
};
14543
14544
static bool
14545
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14546
            void *ptr)
14547
0
{
14548
0
  asection *sec;
14549
0
  bfd_vma hstart, hend;
14550
0
  Elf_Internal_Rela *relstart, *relend, *rel;
14551
0
  elf_backend_data *bed;
14552
0
  unsigned int log_file_align;
14553
0
  struct link_info_ok *info = ptr;
14554
14555
  /* Take care of both those symbols that do not describe vtables as
14556
     well as those that are not loaded.  */
14557
0
  if (h->start_stop
14558
0
      || h->u2.vtable == NULL
14559
0
      || h->u2.vtable->parent == NULL)
14560
0
    return true;
14561
14562
0
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
14563
0
        || h->root.type == bfd_link_hash_defweak);
14564
14565
0
  sec = h->root.u.def.section;
14566
0
  hstart = h->root.u.def.value;
14567
0
  hend = hstart + h->size;
14568
14569
0
  relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14570
0
               sec, NULL, NULL, true);
14571
0
  if (!relstart)
14572
0
    return info->ok = false;
14573
0
  bed = get_elf_backend_data (sec->owner);
14574
0
  log_file_align = bed->s->log_file_align;
14575
14576
0
  relend = relstart + sec->reloc_count;
14577
14578
0
  for (rel = relstart; rel < relend; ++rel)
14579
0
    if (rel->r_offset >= hstart && rel->r_offset < hend)
14580
0
      {
14581
  /* If the entry is in use, do nothing.  */
14582
0
  if (h->u2.vtable->used
14583
0
      && (rel->r_offset - hstart) < h->u2.vtable->size)
14584
0
    {
14585
0
      bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14586
0
      if (h->u2.vtable->used[entry])
14587
0
        continue;
14588
0
    }
14589
  /* Otherwise, kill it.  */
14590
0
  rel->r_offset = rel->r_info = rel->r_addend = 0;
14591
0
      }
14592
14593
0
  return true;
14594
0
}
14595
14596
/* Mark sections containing dynamically referenced symbols.  When
14597
   building shared libraries, we must assume that any visible symbol is
14598
   referenced.  */
14599
14600
bool
14601
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14602
0
{
14603
0
  struct bfd_link_info *info = inf;
14604
0
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
14605
14606
0
  if ((h->root.type == bfd_link_hash_defined
14607
0
       || h->root.type == bfd_link_hash_defweak)
14608
0
      && (!h->start_stop
14609
0
    || h->root.ldscript_def
14610
0
    || !info->start_stop_gc)
14611
0
      && ((h->ref_dynamic && !h->forced_local)
14612
0
    || ((h->def_regular || ELF_COMMON_DEF_P (h))
14613
0
        && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14614
0
        && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14615
0
        && (!bfd_link_executable (info)
14616
0
      || info->gc_keep_exported
14617
0
      || info->export_dynamic
14618
0
      || (h->dynamic
14619
0
          && d != NULL
14620
0
          && (*d->match) (&d->head, NULL, h->root.root.string)))
14621
0
        && (h->versioned >= versioned
14622
0
      || !bfd_hide_sym_by_version (info->version_info,
14623
0
                 h->root.root.string)))))
14624
0
    h->root.u.def.section->flags |= SEC_KEEP;
14625
14626
0
  return true;
14627
0
}
14628
14629
/* Keep all sections containing symbols undefined on the command-line,
14630
   and the section containing the entry symbol.  */
14631
14632
void
14633
_bfd_elf_gc_keep (struct bfd_link_info *info)
14634
0
{
14635
0
  struct bfd_sym_chain *sym;
14636
14637
0
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14638
0
    {
14639
0
      struct elf_link_hash_entry *h;
14640
14641
0
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14642
0
        false, false, false);
14643
14644
0
      if (h != NULL
14645
0
    && (h->root.type == bfd_link_hash_defined
14646
0
        || h->root.type == bfd_link_hash_defweak)
14647
0
    && !bfd_is_const_section (h->root.u.def.section))
14648
0
  h->root.u.def.section->flags |= SEC_KEEP;
14649
0
    }
14650
0
}
14651
14652
bool
14653
bfd_elf_parse_eh_frame_entries (struct bfd_link_info *info)
14654
0
{
14655
0
  bfd *ibfd = info->input_bfds;
14656
14657
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14658
0
    {
14659
0
      asection *sec;
14660
0
      struct elf_reloc_cookie cookie;
14661
14662
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14663
0
  continue;
14664
0
      sec = ibfd->sections;
14665
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14666
0
  continue;
14667
14668
0
      if (!init_reloc_cookie (&cookie, ibfd))
14669
0
  return false;
14670
14671
0
      for (sec = ibfd->sections; sec; sec = sec->next)
14672
0
  {
14673
0
    if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14674
0
        && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14675
0
           false))
14676
0
      {
14677
0
        _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14678
0
        fini_reloc_cookie_rels (&cookie, sec);
14679
0
      }
14680
0
  }
14681
0
    }
14682
0
  return true;
14683
0
}
14684
14685
/* Do mark and sweep of unused sections.  */
14686
14687
bool
14688
bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
14689
0
{
14690
0
  bool ok = true;
14691
0
  elf_gc_mark_hook_fn gc_mark_hook;
14692
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
14693
0
  struct elf_link_hash_table *htab;
14694
0
  struct link_info_ok info_ok;
14695
14696
0
  if (!obed->can_gc_sections
14697
0
      || !is_elf_hash_table (info->hash))
14698
0
    {
14699
0
      _bfd_error_handler(_("warning: gc-sections option ignored"));
14700
0
      return true;
14701
0
    }
14702
14703
0
  obed->gc_keep (info);
14704
0
  htab = elf_hash_table (info);
14705
14706
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
14707
     at the .eh_frame section if we can mark the FDEs individually.  */
14708
0
  for (bfd *sub = info->input_bfds;
14709
0
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14710
0
       sub = sub->link.next)
14711
0
    {
14712
0
      asection *sec;
14713
0
      struct elf_reloc_cookie cookie;
14714
14715
0
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
14716
0
  continue;
14717
0
      sec = sub->sections;
14718
0
      if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14719
0
  continue;
14720
0
      sec = bfd_get_section_by_name (sub, ".eh_frame");
14721
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14722
0
               false))
14723
0
  {
14724
0
    _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14725
0
    if (sec->sec_info
14726
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14727
0
      elf_eh_frame_section (sub) = sec;
14728
0
    fini_reloc_cookie_for_section (&cookie, sec);
14729
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14730
0
  }
14731
14732
      /* Handle .sframe section.  */
14733
0
      sec = bfd_get_section_by_name (sub, ".sframe");
14734
0
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14735
0
               false))
14736
0
  {
14737
0
    _bfd_elf_parse_sframe (sub, info, sec, &cookie);
14738
14739
0
    if (sec->sec_info
14740
0
        && (sec->flags & SEC_LINKER_CREATED) == 0)
14741
0
      elf_sframe_section (sub) = sec;
14742
14743
0
    fini_reloc_cookie_for_section (&cookie, sec);
14744
0
    sec = bfd_get_next_section_by_name (NULL, sec);
14745
0
  }
14746
0
    }
14747
14748
  /* Apply transitive closure to the vtable entry usage info.  */
14749
0
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14750
0
  if (!ok)
14751
0
    return false;
14752
14753
  /* Kill the vtable relocations that were not used.  */
14754
0
  info_ok.info = info;
14755
0
  info_ok.ok = true;
14756
0
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14757
0
  if (!info_ok.ok)
14758
0
    return false;
14759
14760
  /* Mark dynamically referenced symbols.  */
14761
0
  if (htab->dynamic_sections_created || info->gc_keep_exported)
14762
0
    elf_link_hash_traverse (htab, obed->gc_mark_dynamic_ref, info);
14763
14764
  /* Grovel through relocs to find out who stays ...  */
14765
0
  gc_mark_hook = obed->gc_mark_hook;
14766
0
  for (bfd *sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14767
0
    {
14768
0
      if (!compatible_format (info, sub))
14769
0
  continue;
14770
14771
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14772
   Also treat note sections as a root, if the section is not part
14773
   of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
14774
   well as FINI_ARRAY sections for ld -r.  */
14775
0
      for (asection *o = sub->sections; o != NULL; o = o->next)
14776
0
  if (!o->gc_mark
14777
0
      && (o->flags & SEC_EXCLUDE) == 0
14778
0
      && ((o->flags & SEC_KEEP) != 0
14779
0
    || (bfd_link_relocatable (info)
14780
0
        && ((elf_section_data (o)->this_hdr.sh_type
14781
0
       == SHT_PREINIT_ARRAY)
14782
0
      || (elf_section_data (o)->this_hdr.sh_type
14783
0
          == SHT_INIT_ARRAY)
14784
0
      || (elf_section_data (o)->this_hdr.sh_type
14785
0
          == SHT_FINI_ARRAY)))
14786
0
    || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14787
0
        && elf_next_in_group (o) == NULL
14788
0
        && elf_linked_to_section (o) == NULL)
14789
0
    || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14790
0
        && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14791
0
    {
14792
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14793
0
        return false;
14794
0
    }
14795
0
    }
14796
14797
  /* Allow the backend to mark additional target specific sections.  */
14798
0
  if (!obed->gc_mark_extra_sections (info, gc_mark_hook))
14799
0
    return false;
14800
14801
  /* ... and mark SEC_EXCLUDE for those that go.  */
14802
0
  return elf_gc_sweep (info);
14803
0
}
14804

14805
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
14806
14807
bool
14808
bfd_elf_gc_record_vtinherit (bfd *abfd,
14809
           asection *sec,
14810
           struct elf_link_hash_entry *h,
14811
           bfd_vma offset)
14812
0
{
14813
0
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14814
0
  struct elf_link_hash_entry **search, *child;
14815
0
  size_t extsymcount;
14816
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14817
14818
  /* The sh_info field of the symtab header tells us where the
14819
     external symbols start.  We don't care about the local symbols at
14820
     this point.  */
14821
0
  extsymcount = elf_symtab_hdr (abfd).sh_size / bed->s->sizeof_sym;
14822
0
  if (!elf_bad_symtab (abfd))
14823
0
    extsymcount -= elf_symtab_hdr (abfd).sh_info;
14824
14825
0
  sym_hashes = elf_sym_hashes (abfd);
14826
0
  sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14827
14828
  /* Hunt down the child symbol, which is in this section at the same
14829
     offset as the relocation.  */
14830
0
  for (search = sym_hashes; search != sym_hashes_end; ++search)
14831
0
    {
14832
0
      if ((child = *search) != NULL
14833
0
    && (child->root.type == bfd_link_hash_defined
14834
0
        || child->root.type == bfd_link_hash_defweak)
14835
0
    && child->root.u.def.section == sec
14836
0
    && child->root.u.def.value == offset)
14837
0
  goto win;
14838
0
    }
14839
14840
  /* xgettext:c-format */
14841
0
  _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14842
0
          abfd, sec, (uint64_t) offset);
14843
0
  bfd_set_error (bfd_error_invalid_operation);
14844
0
  return false;
14845
14846
0
 win:
14847
0
  if (!child->u2.vtable)
14848
0
    {
14849
0
      child->u2.vtable = bfd_zalloc (abfd, sizeof (*child->u2.vtable));
14850
0
      if (!child->u2.vtable)
14851
0
  return false;
14852
0
    }
14853
0
  if (!h)
14854
0
    {
14855
      /* This *should* only be the absolute section.  It could potentially
14856
   be that someone has defined a non-global vtable though, which
14857
   would be bad.  It isn't worth paging in the local symbols to be
14858
   sure though; that case should simply be handled by the assembler.  */
14859
14860
0
      child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14861
0
    }
14862
0
  else
14863
0
    child->u2.vtable->parent = h;
14864
14865
0
  return true;
14866
0
}
14867
14868
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
14869
14870
bool
14871
bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14872
         struct elf_link_hash_entry *h,
14873
         bfd_vma addend)
14874
0
{
14875
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
14876
0
  unsigned int log_file_align = bed->s->log_file_align;
14877
14878
0
  if (!h || addend > 1u << 28)
14879
0
    {
14880
      /* xgettext:c-format */
14881
0
      _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14882
0
        abfd, sec);
14883
0
      bfd_set_error (bfd_error_bad_value);
14884
0
      return false;
14885
0
    }
14886
14887
0
  if (!h->u2.vtable)
14888
0
    {
14889
0
      h->u2.vtable = bfd_zalloc (abfd, sizeof (*h->u2.vtable));
14890
0
      if (!h->u2.vtable)
14891
0
  return false;
14892
0
    }
14893
14894
0
  if (addend >= h->u2.vtable->size)
14895
0
    {
14896
0
      size_t size, bytes, file_align;
14897
0
      bool *ptr = h->u2.vtable->used;
14898
14899
      /* While the symbol is undefined, we have to be prepared to handle
14900
   a zero size.  */
14901
0
      file_align = 1 << log_file_align;
14902
0
      if (h->root.type == bfd_link_hash_undefined)
14903
0
  size = addend + file_align;
14904
0
      else
14905
0
  {
14906
0
    size = h->size;
14907
0
    if (addend >= size)
14908
0
      {
14909
        /* Oops!  We've got a reference past the defined end of
14910
     the table.  This is probably a bug -- shall we warn?  */
14911
0
        size = addend + file_align;
14912
0
      }
14913
0
  }
14914
0
      size = (size + file_align - 1) & -file_align;
14915
14916
      /* Allocate one extra entry for use as a "done" flag for the
14917
   consolidation pass.  */
14918
0
      bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14919
14920
0
      if (ptr)
14921
0
  {
14922
0
    ptr = bfd_realloc (ptr - 1, bytes);
14923
14924
0
    if (ptr != NULL)
14925
0
      {
14926
0
        size_t oldbytes;
14927
14928
0
        oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14929
0
        * sizeof (bool));
14930
0
        memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14931
0
      }
14932
0
  }
14933
0
      else
14934
0
  ptr = bfd_zmalloc (bytes);
14935
14936
0
      if (ptr == NULL)
14937
0
  return false;
14938
14939
      /* And arrange for that done flag to be at index -1.  */
14940
0
      h->u2.vtable->used = ptr + 1;
14941
0
      h->u2.vtable->size = size;
14942
0
    }
14943
14944
0
  h->u2.vtable->used[addend >> log_file_align] = true;
14945
14946
0
  return true;
14947
0
}
14948
14949
/* Map an ELF section header flag to its corresponding string.  */
14950
typedef struct
14951
{
14952
  char *flag_name;
14953
  flagword flag_value;
14954
} elf_flags_to_name_table;
14955
14956
static const elf_flags_to_name_table elf_flags_to_names [] =
14957
{
14958
  { "SHF_WRITE", SHF_WRITE },
14959
  { "SHF_ALLOC", SHF_ALLOC },
14960
  { "SHF_EXECINSTR", SHF_EXECINSTR },
14961
  { "SHF_MERGE", SHF_MERGE },
14962
  { "SHF_STRINGS", SHF_STRINGS },
14963
  { "SHF_INFO_LINK", SHF_INFO_LINK},
14964
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14965
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14966
  { "SHF_GROUP", SHF_GROUP },
14967
  { "SHF_TLS", SHF_TLS },
14968
  { "SHF_MASKOS", SHF_MASKOS },
14969
  { "SHF_EXCLUDE", SHF_EXCLUDE },
14970
};
14971
14972
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
14973
bool
14974
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14975
            struct flag_info *flaginfo,
14976
            asection *section)
14977
0
{
14978
0
  const bfd_vma sh_flags = elf_section_flags (section);
14979
14980
0
  if (!flaginfo->flags_initialized)
14981
0
    {
14982
0
      bfd *obfd = info->output_bfd;
14983
0
      elf_backend_data *obed = get_elf_backend_data (obfd);
14984
0
      struct flag_info_list *tf = flaginfo->flag_list;
14985
0
      int with_hex = 0;
14986
0
      int without_hex = 0;
14987
14988
0
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14989
0
  {
14990
0
    unsigned i;
14991
0
    flagword (*lookup) (char *);
14992
14993
0
    lookup = obed->elf_backend_lookup_section_flags_hook;
14994
0
    if (lookup != NULL)
14995
0
      {
14996
0
        flagword hexval = (*lookup) ((char *) tf->name);
14997
14998
0
        if (hexval != 0)
14999
0
    {
15000
0
      if (tf->with == with_flags)
15001
0
        with_hex |= hexval;
15002
0
      else if (tf->with == without_flags)
15003
0
        without_hex |= hexval;
15004
0
      tf->valid = true;
15005
0
      continue;
15006
0
    }
15007
0
      }
15008
0
    for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
15009
0
      {
15010
0
        if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
15011
0
    {
15012
0
      if (tf->with == with_flags)
15013
0
        with_hex |= elf_flags_to_names[i].flag_value;
15014
0
      else if (tf->with == without_flags)
15015
0
        without_hex |= elf_flags_to_names[i].flag_value;
15016
0
      tf->valid = true;
15017
0
      break;
15018
0
    }
15019
0
      }
15020
0
    if (!tf->valid)
15021
0
      {
15022
0
        info->callbacks->einfo
15023
0
    (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
15024
0
        return false;
15025
0
      }
15026
0
  }
15027
0
      flaginfo->flags_initialized = true;
15028
0
      flaginfo->only_with_flags |= with_hex;
15029
0
      flaginfo->not_with_flags |= without_hex;
15030
0
    }
15031
15032
0
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
15033
0
    return false;
15034
15035
0
  if ((flaginfo->not_with_flags & sh_flags) != 0)
15036
0
    return false;
15037
15038
0
  return true;
15039
0
}
15040
15041
struct alloc_got_off_arg {
15042
  bfd_vma gotoff;
15043
  struct bfd_link_info *info;
15044
};
15045
15046
/* We need a special top-level link routine to convert got reference counts
15047
   to real got offsets.  */
15048
15049
static bool
15050
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15051
0
{
15052
0
  struct alloc_got_off_arg *gofarg = arg;
15053
0
  bfd *obfd = gofarg->info->output_bfd;
15054
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15055
15056
0
  if (h->got.refcount > 0)
15057
0
    {
15058
0
      h->got.offset = gofarg->gotoff;
15059
0
      gofarg->gotoff += obed->got_elt_size (gofarg->info, h, NULL, 0);
15060
0
    }
15061
0
  else
15062
0
    h->got.offset = (bfd_vma) -1;
15063
15064
0
  return true;
15065
0
}
15066
15067
/* And an accompanying bit to work out final got entry offsets once
15068
   we're done.  Should be called from final_link.  */
15069
15070
static bool
15071
bfd_elf_gc_common_finalize_got_offsets (bfd *obfd,
15072
          struct bfd_link_info *info)
15073
0
{
15074
0
  bfd *i;
15075
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15076
0
  bfd_vma gotoff;
15077
0
  struct alloc_got_off_arg gofarg;
15078
15079
0
  BFD_ASSERT (obfd == info->output_bfd);
15080
15081
0
  if (! is_elf_hash_table (info->hash))
15082
0
    return false;
15083
15084
  /* The GOT offset is relative to the .got section, but the GOT header is
15085
     put into the .got.plt section, if the backend uses it.  */
15086
0
  if (obed->want_got_plt)
15087
0
    gotoff = 0;
15088
0
  else
15089
0
    gotoff = obed->got_header_size;
15090
15091
  /* Do the local .got entries first.  */
15092
0
  for (i = info->input_bfds; i; i = i->link.next)
15093
0
    {
15094
0
      bfd_signed_vma *local_got;
15095
0
      size_t j, locsymcount;
15096
0
      Elf_Internal_Shdr *symtab_hdr;
15097
15098
0
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15099
0
  continue;
15100
15101
0
      local_got = elf_local_got_refcounts (i);
15102
0
      if (!local_got)
15103
0
  continue;
15104
15105
0
      symtab_hdr = &elf_symtab_hdr (i);
15106
0
      if (elf_bad_symtab (i))
15107
0
  locsymcount = symtab_hdr->sh_size / obed->s->sizeof_sym;
15108
0
      else
15109
0
  locsymcount = symtab_hdr->sh_info;
15110
15111
0
      for (j = 0; j < locsymcount; ++j)
15112
0
  {
15113
0
    if (local_got[j] > 0)
15114
0
      {
15115
0
        local_got[j] = gotoff;
15116
0
        gotoff += obed->got_elt_size (info, NULL, i, j);
15117
0
      }
15118
0
    else
15119
0
      local_got[j] = (bfd_vma) -1;
15120
0
  }
15121
0
    }
15122
15123
  /* Then the global .got entries.  .plt refcounts are handled by
15124
     adjust_dynamic_symbol  */
15125
0
  gofarg.gotoff = gotoff;
15126
0
  gofarg.info = info;
15127
0
  elf_link_hash_traverse (elf_hash_table (info),
15128
0
        elf_gc_allocate_got_offsets,
15129
0
        &gofarg);
15130
0
  return true;
15131
0
}
15132
15133
/* Many folk need no more in the way of final link than this, once
15134
   got entry reference counting is enabled.  */
15135
15136
bool
15137
_bfd_elf_gc_common_final_link (bfd *obfd, struct bfd_link_info *info)
15138
0
{
15139
0
  if (!bfd_elf_gc_common_finalize_got_offsets (obfd, info))
15140
0
    return false;
15141
15142
  /* Invoke the regular ELF backend linker to do all the work.  */
15143
0
  return _bfd_elf_final_link (obfd, info);
15144
0
}
15145
15146
bool
15147
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15148
0
{
15149
0
  struct elf_reloc_cookie *rcookie = cookie;
15150
15151
0
  if (elf_bad_symtab (rcookie->abfd))
15152
0
    rcookie->rel = rcookie->rels;
15153
15154
0
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15155
0
    {
15156
0
      unsigned long r_symndx;
15157
15158
0
      if (!elf_bad_symtab (rcookie->abfd)
15159
0
    && rcookie->rel->r_offset > offset)
15160
0
  return false;
15161
0
      if (rcookie->rel->r_offset != offset)
15162
0
  continue;
15163
15164
0
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15165
0
      if (r_symndx == STN_UNDEF)
15166
0
  return true;
15167
15168
0
      struct elf_link_hash_entry *h;
15169
15170
0
      h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15171
15172
0
      if (h != NULL)
15173
0
  {
15174
0
    if ((h->root.type == bfd_link_hash_defined
15175
0
         || h->root.type == bfd_link_hash_defweak)
15176
0
        && (h->root.u.def.section->owner != rcookie->abfd
15177
0
      || h->root.u.def.section->kept_section != NULL
15178
0
      || discarded_section (h->root.u.def.section)))
15179
0
      return true;
15180
0
  }
15181
0
      else
15182
0
  {
15183
0
    if (r_symndx >= rcookie->locsymcount)
15184
      /* This can happen with corrupt input.  */
15185
0
      return false;
15186
15187
    /* It's not a relocation against a global symbol,
15188
       but it could be a relocation against a local
15189
       symbol for a discarded section.  */
15190
0
    asection *isec = _bfd_get_local_sym_section (cookie, r_symndx);
15191
0
    if (isec != NULL
15192
0
        && (isec->kept_section != NULL
15193
0
      || discarded_section (isec)))
15194
0
      return true;
15195
0
  }
15196
15197
0
      return false;
15198
0
    }
15199
0
  return false;
15200
0
}
15201
15202
/* Discard unneeded references to discarded sections.
15203
   Returns -1 on error, 1 if any section's size was changed, 0 if
15204
   nothing changed.  This function assumes that the relocations are in
15205
   sorted order, which is true for all known assemblers.  */
15206
15207
int
15208
bfd_elf_discard_info (struct bfd_link_info *info)
15209
0
{
15210
0
  struct elf_reloc_cookie cookie;
15211
0
  asection *o;
15212
0
  bfd *abfd;
15213
0
  int changed = 0;
15214
15215
0
  if (info->traditional_format
15216
0
      || !is_elf_hash_table (info->hash))
15217
0
    return 0;
15218
15219
0
  o = bfd_get_section_by_name (info->output_bfd, ".stab");
15220
0
  if (o != NULL)
15221
0
    {
15222
0
      asection *i;
15223
15224
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15225
0
  {
15226
0
    if (i->size == 0
15227
0
        || i->reloc_count == 0
15228
0
        || i->sec_info_type != SEC_INFO_TYPE_STABS)
15229
0
      continue;
15230
15231
0
    abfd = i->owner;
15232
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15233
0
      continue;
15234
15235
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15236
0
      return -1;
15237
15238
0
    if (_bfd_discard_section_stabs (abfd, i,
15239
0
            bfd_elf_reloc_symbol_deleted_p,
15240
0
            &cookie))
15241
0
      changed = 1;
15242
15243
0
    fini_reloc_cookie_for_section (&cookie, i);
15244
0
  }
15245
0
    }
15246
15247
0
  o = NULL;
15248
0
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15249
0
    o = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
15250
0
  if (o != NULL)
15251
0
    {
15252
0
      asection *i;
15253
0
      int eh_changed = 0;
15254
0
      unsigned int eh_alignment;  /* Octets.  */
15255
15256
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15257
0
  {
15258
0
    int r;
15259
15260
0
    if (i->size == 0)
15261
0
      continue;
15262
15263
0
    abfd = i->owner;
15264
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15265
0
      continue;
15266
15267
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15268
0
      return -1;
15269
15270
0
    _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15271
0
    r = _bfd_elf_discard_section_eh_frame (abfd, info, i,
15272
0
             bfd_elf_reloc_symbol_deleted_p,
15273
0
             &cookie);
15274
0
    if (r)
15275
0
      {
15276
0
        eh_changed = 1;
15277
0
        if (r >= 2)
15278
0
    changed = 1;
15279
0
      }
15280
15281
0
    fini_reloc_cookie_for_section (&cookie, i);
15282
0
  }
15283
15284
0
      eh_alignment = ((1 << o->alignment_power)
15285
0
          * bfd_octets_per_byte (info->output_bfd, o));
15286
      /* Skip over zero terminator, and prevent empty sections from
15287
   adding alignment padding at the end.  */
15288
0
      for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15289
0
  if (i->size == 0)
15290
0
    i->flags |= SEC_EXCLUDE;
15291
0
  else if (i->size > 4)
15292
0
    break;
15293
      /* The last non-empty eh_frame section doesn't need padding.  */
15294
0
      if (i != NULL)
15295
0
  i = i->map_tail.s;
15296
      /* Any prior sections must pad the last FDE out to the output
15297
   section alignment.  Otherwise we might have zero padding
15298
   between sections, which would be seen as a terminator.  */
15299
0
      for (; i != NULL; i = i->map_tail.s)
15300
0
  if (i->size == 4)
15301
    /* All but the last zero terminator should have been removed.  */
15302
0
    BFD_FAIL ();
15303
0
  else
15304
0
    {
15305
0
      bfd_size_type size
15306
0
        = (i->size + eh_alignment - 1) & -eh_alignment;
15307
0
      if (i->size != size)
15308
0
        {
15309
0
    i->size = size;
15310
0
    changed = 1;
15311
0
    eh_changed = 1;
15312
0
        }
15313
0
    }
15314
0
      if (eh_changed)
15315
0
  elf_link_hash_traverse (elf_hash_table (info),
15316
0
        _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15317
0
    }
15318
15319
0
  o = bfd_get_section_by_name (info->output_bfd, ".sframe");
15320
0
  if (o != NULL)
15321
0
    {
15322
0
      asection *i;
15323
15324
0
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15325
0
  {
15326
0
    if (i->size == 0)
15327
0
      continue;
15328
15329
0
    abfd = i->owner;
15330
0
    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15331
0
      continue;
15332
15333
0
    if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15334
0
      return -1;
15335
15336
0
    if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15337
0
      {
15338
0
        if (_bfd_elf_discard_section_sframe (i,
15339
0
               bfd_elf_reloc_symbol_deleted_p,
15340
0
               &cookie))
15341
0
    {
15342
0
      if (i->size != i->rawsize)
15343
0
        changed = 1;
15344
0
    }
15345
0
      }
15346
0
    fini_reloc_cookie_for_section (&cookie, i);
15347
0
  }
15348
      /* Update the reference to the output .sframe section.  Used to
15349
   determine later if PT_GNU_SFRAME segment is to be generated.  */
15350
0
      if (!_bfd_elf_set_section_sframe (info->output_bfd, info))
15351
0
  return -1;
15352
0
    }
15353
15354
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15355
0
    {
15356
0
      elf_backend_data *bed;
15357
0
      asection *s;
15358
15359
0
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15360
0
  continue;
15361
0
      s = abfd->sections;
15362
0
      if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15363
0
  continue;
15364
15365
0
      bed = get_elf_backend_data (abfd);
15366
15367
0
      if (bed->elf_backend_discard_info != NULL)
15368
0
  {
15369
0
    if (!init_reloc_cookie (&cookie, abfd))
15370
0
      return -1;
15371
15372
0
    if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15373
0
      changed = 1;
15374
15375
0
    fini_reloc_cookie (&cookie, abfd);
15376
0
  }
15377
0
    }
15378
15379
0
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15380
0
    _bfd_elf_end_eh_frame_parsing (info);
15381
15382
0
  if (_bfd_elf_discard_section_eh_frame_hdr (info))
15383
0
    changed = 1;
15384
15385
0
  return changed;
15386
0
}
15387
15388
bool
15389
_bfd_elf_section_already_linked (bfd *abfd,
15390
         asection *sec,
15391
         struct bfd_link_info *info)
15392
0
{
15393
0
  flagword flags;
15394
0
  const char *name, *key;
15395
0
  struct bfd_section_already_linked *l;
15396
0
  struct bfd_section_already_linked_hash_entry *already_linked_list;
15397
15398
0
  if (sec->output_section == bfd_abs_section_ptr)
15399
0
    return false;
15400
15401
0
  flags = sec->flags;
15402
15403
  /* Return if it isn't a linkonce section.  A comdat group section
15404
     also has SEC_LINK_ONCE set.  */
15405
0
  if ((flags & SEC_LINK_ONCE) == 0)
15406
0
    return false;
15407
15408
  /* Don't put group member sections on our list of already linked
15409
     sections.  They are handled as a group via their group section.  */
15410
0
  if (elf_sec_group (sec) != NULL)
15411
0
    return false;
15412
15413
  /* For a SHT_GROUP section, use the group signature as the key.  */
15414
0
  name = sec->name;
15415
0
  if ((flags & SEC_GROUP) != 0
15416
0
      && elf_next_in_group (sec) != NULL
15417
0
      && elf_group_name (elf_next_in_group (sec)) != NULL)
15418
0
    key = elf_group_name (elf_next_in_group (sec));
15419
0
  else
15420
0
    {
15421
      /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
15422
0
      if (startswith (name, ".gnu.linkonce.")
15423
0
    && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15424
0
  key++;
15425
0
      else
15426
  /* Must be a user linkonce section that doesn't follow gcc's
15427
     naming convention.  In this case we won't be matching
15428
     single member groups.  */
15429
0
  key = name;
15430
0
    }
15431
15432
0
  already_linked_list = bfd_section_already_linked_table_lookup (key);
15433
15434
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15435
0
    {
15436
      /* We may have 2 different types of sections on the list: group
15437
   sections with a signature of <key> (<key> is some string),
15438
   and linkonce sections named .gnu.linkonce.<type>.<key>.
15439
   Match like sections.  LTO plugin sections are an exception.
15440
   They are always named .gnu.linkonce.t.<key> and match either
15441
   type of section.  */
15442
0
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15443
0
     && ((flags & SEC_GROUP) != 0
15444
0
         || strcmp (name, l->sec->name) == 0))
15445
0
    || (l->sec->owner->flags & BFD_PLUGIN) != 0
15446
0
    || (sec->owner->flags & BFD_PLUGIN) != 0)
15447
0
  {
15448
    /* The section has already been linked.  See if we should
15449
       issue a warning.  */
15450
0
    if (!_bfd_handle_already_linked (sec, l, info))
15451
0
      return false;
15452
15453
0
    if (flags & SEC_GROUP)
15454
0
      {
15455
0
        asection *first = elf_next_in_group (sec);
15456
0
        asection *s = first;
15457
15458
0
        while (s != NULL)
15459
0
    {
15460
0
      s->output_section = bfd_abs_section_ptr;
15461
      /* Record which group discards it.  */
15462
0
      s->kept_section = l->sec;
15463
0
      s = elf_next_in_group (s);
15464
      /* These lists are circular.  */
15465
0
      if (s == first)
15466
0
        break;
15467
0
    }
15468
0
      }
15469
15470
0
    return true;
15471
0
  }
15472
0
    }
15473
15474
  /* A single member comdat group section may be discarded by a
15475
     linkonce section and vice versa.  */
15476
0
  if ((flags & SEC_GROUP) != 0)
15477
0
    {
15478
0
      asection *first = elf_next_in_group (sec);
15479
15480
0
      if (first != NULL && elf_next_in_group (first) == first)
15481
  /* Check this single member group against linkonce sections.  */
15482
0
  for (l = already_linked_list->entry; l != NULL; l = l->next)
15483
0
    if ((l->sec->flags & SEC_GROUP) == 0
15484
0
        && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15485
0
      {
15486
0
        first->output_section = bfd_abs_section_ptr;
15487
0
        first->kept_section = l->sec;
15488
0
        sec->output_section = bfd_abs_section_ptr;
15489
0
        break;
15490
0
      }
15491
0
    }
15492
0
  else
15493
    /* Check this linkonce section against single member groups.  */
15494
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15495
0
      if (l->sec->flags & SEC_GROUP)
15496
0
  {
15497
0
    asection *first = elf_next_in_group (l->sec);
15498
15499
0
    if (first != NULL
15500
0
        && elf_next_in_group (first) == first
15501
0
        && bfd_elf_match_symbols_in_sections (first, sec, info))
15502
0
      {
15503
0
        sec->output_section = bfd_abs_section_ptr;
15504
0
        sec->kept_section = first;
15505
0
        break;
15506
0
      }
15507
0
  }
15508
15509
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15510
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15511
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15512
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
15513
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
15514
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15515
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
15516
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
15517
     The reverse order cannot happen as there is never a bfd with only the
15518
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
15519
     matter as here were are looking only for cross-bfd sections.  */
15520
15521
0
  if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15522
0
    for (l = already_linked_list->entry; l != NULL; l = l->next)
15523
0
      if ((l->sec->flags & SEC_GROUP) == 0
15524
0
    && startswith (l->sec->name, ".gnu.linkonce.t."))
15525
0
  {
15526
0
    if (abfd != l->sec->owner)
15527
0
      sec->output_section = bfd_abs_section_ptr;
15528
0
    break;
15529
0
  }
15530
15531
  /* This is the first section with this name.  Record it.  */
15532
0
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15533
0
    info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15534
0
  return sec->output_section == bfd_abs_section_ptr;
15535
0
}
15536
15537
bool
15538
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15539
0
{
15540
0
  return sym->st_shndx == SHN_COMMON;
15541
0
}
15542
15543
unsigned int
15544
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15545
0
{
15546
0
  return SHN_COMMON;
15547
0
}
15548
15549
asection *
15550
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15551
0
{
15552
0
  return bfd_com_section_ptr;
15553
0
}
15554
15555
bfd_vma
15556
_bfd_elf_default_got_elt_size (struct bfd_link_info *info,
15557
             struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15558
             bfd *ibfd ATTRIBUTE_UNUSED,
15559
             unsigned long symndx ATTRIBUTE_UNUSED)
15560
0
{
15561
0
  elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15562
0
  return obed->s->arch_size / 8;
15563
0
}
15564
15565
/* Routines to support the creation of dynamic relocs.  */
15566
15567
/* Returns the name of the dynamic reloc section associated with SEC.  */
15568
15569
static char *
15570
get_dynamic_reloc_section_name (bfd *abfd,
15571
        asection *sec,
15572
        bool is_rela)
15573
0
{
15574
0
  const char *prefix = is_rela ? ".rela" : ".rel";
15575
0
  size_t plen = is_rela ? 5 : 4;
15576
0
  const char *old_name = bfd_section_name (sec);
15577
0
  size_t nlen = strlen (old_name);
15578
0
  char *name = bfd_alloc (abfd, plen + nlen + 1);
15579
0
  if (name == NULL)
15580
0
    return NULL;
15581
15582
0
  memcpy (name, prefix, plen);
15583
0
  memcpy (name + plen, old_name, nlen + 1);
15584
0
  return name;
15585
0
}
15586
15587
/* Returns the dynamic reloc section associated with SEC.  If the
15588
   section does not exist it is created and attached to the DYNOBJ
15589
   bfd and stored in the SRELOC field of SEC's elf_section_data
15590
   structure.
15591
15592
   ALIGNMENT is the alignment for the newly created section and
15593
   IS_RELA defines whether the name should be .rela.<SEC's name>
15594
   or .rel.<SEC's name>.  The section name is looked up in the
15595
   string table associated with ABFD.  */
15596
15597
asection *
15598
_bfd_elf_make_dynamic_reloc_section (asection *sec,
15599
             bfd *dynobj,
15600
             unsigned int alignment,
15601
             bfd *abfd,
15602
             bool is_rela)
15603
0
{
15604
0
  asection * reloc_sec = elf_section_data (sec)->sreloc;
15605
15606
0
  if (reloc_sec == NULL)
15607
0
    {
15608
0
      bool known = (dynobj != NULL
15609
0
        && (get_elf_backend_data (dynobj)
15610
0
      ->relocs_compatible (dynobj->xvec, abfd->xvec)));
15611
0
      BFD_ASSERT (known);
15612
0
      if (!known
15613
0
    || dynobj->xvec->byteorder != abfd->xvec->byteorder)
15614
0
  {
15615
0
    _bfd_error_handler (_("%s dynamic relocs incompatible with%s%s output"),
15616
0
            bfd_get_target (abfd),
15617
0
            dynobj ? " " : "",
15618
0
            dynobj ? bfd_get_target (dynobj) : "");
15619
0
    return NULL;
15620
0
  }
15621
15622
0
      char *name = get_dynamic_reloc_section_name (dynobj, sec, is_rela);
15623
0
      if (name == NULL)
15624
0
  return NULL;
15625
15626
0
      reloc_sec = bfd_get_linker_section (dynobj, name);
15627
0
      if (reloc_sec != NULL)
15628
0
  bfd_release (dynobj, name);
15629
0
      else
15630
0
  {
15631
0
    flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15632
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15633
0
    if ((sec->flags & SEC_ALLOC) != 0)
15634
0
      flags |= SEC_ALLOC | SEC_LOAD;
15635
15636
0
    reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15637
0
    if (reloc_sec != NULL)
15638
0
      {
15639
        /* _bfd_elf_get_sec_type_attr chooses a section type by
15640
     name.  Override as it may be wrong, eg. for a user
15641
     section named "auto" we'll get ".relauto" which is
15642
     seen to be a .rela section.  */
15643
0
        elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15644
0
        if (!bfd_set_section_alignment (reloc_sec, alignment))
15645
0
    reloc_sec = NULL;
15646
0
      }
15647
0
  }
15648
15649
0
      elf_section_data (sec)->sreloc = reloc_sec;
15650
0
    }
15651
15652
0
  return reloc_sec;
15653
0
}
15654
15655
/* Copy the ELF symbol type and other attributes for a linker script
15656
   assignment from HSRC to HDEST.  Generally this should be treated as
15657
   if we found a strong non-dynamic definition for HDEST (except that
15658
   ld ignores multiple definition errors).  */
15659
void
15660
_bfd_elf_copy_link_hash_symbol_type (bfd *obfd,
15661
             struct bfd_link_hash_entry *hdest,
15662
             struct bfd_link_hash_entry *hsrc)
15663
0
{
15664
0
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15665
0
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15666
15667
0
  ehdest->type = ehsrc->type;
15668
0
  ehdest->target_internal = ehsrc->target_internal;
15669
15670
0
  elf_merge_st_other (obfd, obfd, ehdest, ehsrc->other, NULL,
15671
0
          true, false);
15672
0
}
15673
15674
/* Append a RELA relocation REL to section S.  */
15675
15676
void
15677
_bfd_elf_append_rela (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15678
0
{
15679
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15680
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rela);
15681
0
  BFD_ASSERT (loc + obed->s->sizeof_rela <= s->contents + s->size);
15682
0
  obed->s->swap_reloca_out (obfd, rel, loc);
15683
0
}
15684
15685
/* Append a REL relocation REL to section S.  */
15686
15687
void
15688
_bfd_elf_append_rel (bfd *obfd, asection *s, Elf_Internal_Rela *rel)
15689
0
{
15690
0
  elf_backend_data *obed = get_elf_backend_data (obfd);
15691
0
  bfd_byte *loc = s->contents + (s->reloc_count++ * obed->s->sizeof_rel);
15692
0
  BFD_ASSERT (loc + obed->s->sizeof_rel <= s->contents + s->size);
15693
0
  obed->s->swap_reloc_out (obfd, rel, loc);
15694
0
}
15695
15696
/* Define __start, __stop, .startof. or .sizeof. symbol.  */
15697
15698
struct bfd_link_hash_entry *
15699
bfd_elf_define_start_stop (struct bfd_link_info *info,
15700
         const char *symbol, asection *sec)
15701
0
{
15702
0
  struct elf_link_hash_entry *h;
15703
15704
0
  h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15705
0
          false, false, true);
15706
  /* NB: Common symbols will be turned into definition later.  */
15707
0
  if (h != NULL
15708
0
      && !h->root.ldscript_def
15709
0
      && (h->root.type == bfd_link_hash_undefined
15710
0
    || h->root.type == bfd_link_hash_undefweak
15711
0
    || ((h->ref_regular || h->def_dynamic)
15712
0
        && !h->def_regular
15713
0
        && h->root.type != bfd_link_hash_common)))
15714
0
    {
15715
0
      bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15716
0
      h->verinfo.verdef = NULL;
15717
0
      h->root.type = bfd_link_hash_defined;
15718
0
      h->root.u.def.section = sec;
15719
0
      h->root.u.def.value = 0;
15720
0
      h->def_regular = 1;
15721
0
      h->def_dynamic = 0;
15722
0
      h->start_stop = 1;
15723
0
      h->u2.start_stop_section = sec;
15724
0
      if (symbol[0] == '.')
15725
0
  {
15726
    /* .startof. and .sizeof. symbols are local.  */
15727
0
    elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15728
0
    obed->elf_backend_hide_symbol (info, h, true);
15729
0
  }
15730
0
      else
15731
0
  {
15732
0
    if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15733
0
      h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15734
0
      | info->start_stop_visibility);
15735
0
    if (was_dynamic)
15736
0
      bfd_elf_link_record_dynamic_symbol (info, h);
15737
0
  }
15738
0
      return &h->root;
15739
0
    }
15740
0
  return NULL;
15741
0
}
15742
15743
/* Find dynamic relocs for H that apply to read-only sections.  */
15744
15745
asection *
15746
_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15747
0
{
15748
0
  struct elf_dyn_relocs *p;
15749
15750
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
15751
0
    {
15752
0
      asection *s = p->sec->output_section;
15753
15754
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
15755
0
  return p->sec;
15756
0
    }
15757
0
  return NULL;
15758
0
}
15759
15760
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15761
   read-only sections.  */
15762
15763
bool
15764
_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15765
0
{
15766
0
  asection *sec;
15767
15768
0
  if (h->root.type == bfd_link_hash_indirect)
15769
0
    return true;
15770
15771
0
  sec = _bfd_elf_readonly_dynrelocs (h);
15772
0
  if (sec != NULL)
15773
0
    {
15774
0
      struct bfd_link_info *info = inf;
15775
15776
0
      info->flags |= DF_TEXTREL;
15777
      /* xgettext:c-format */
15778
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15779
0
        "in read-only section `%pA'\n"),
15780
0
            sec->owner, h->root.root.string, sec);
15781
15782
0
      if (bfd_link_textrel_check (info))
15783
  /* xgettext:c-format */
15784
0
  info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15785
0
          "in read-only section `%pA'\n"),
15786
0
        sec->owner, h->root.root.string, sec);
15787
15788
      /* Not an error, just cut short the traversal.  */
15789
0
      return false;
15790
0
    }
15791
0
  return true;
15792
0
}
15793
15794
/* Add dynamic tags.  */
15795
15796
bool
15797
_bfd_elf_add_dynamic_tags (struct bfd_link_info *info, bool need_dynamic_reloc)
15798
0
{
15799
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
15800
15801
0
  if (htab->dynamic_sections_created)
15802
0
    {
15803
      /* Add some entries to the .dynamic section.  We fill in the
15804
   values later, in finish_dynamic_sections, but we must add
15805
   the entries now so that we get the correct size for the
15806
   .dynamic section.  The DT_DEBUG entry is filled in by the
15807
   dynamic linker and used by the debugger.  */
15808
0
#define add_dynamic_entry(TAG, VAL) \
15809
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15810
15811
0
      elf_backend_data *obed = get_elf_backend_data (info->output_bfd);
15812
15813
0
      if (bfd_link_executable (info))
15814
0
  {
15815
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
15816
0
      return false;
15817
0
  }
15818
15819
0
      if (htab->dt_pltgot_required || htab->splt->size != 0)
15820
0
  {
15821
    /* DT_PLTGOT is used by prelink even if there is no PLT
15822
       relocation.  */
15823
0
    if (!add_dynamic_entry (DT_PLTGOT, 0))
15824
0
      return false;
15825
0
  }
15826
15827
0
      if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15828
0
  {
15829
0
    if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15830
0
        || !add_dynamic_entry (DT_PLTREL,
15831
0
             (obed->rela_plts_and_copies_p
15832
0
              ? DT_RELA : DT_REL))
15833
0
        || !add_dynamic_entry (DT_JMPREL, 0))
15834
0
      return false;
15835
0
  }
15836
15837
0
      if (htab->tlsdesc_plt
15838
0
    && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15839
0
        || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15840
0
  return false;
15841
15842
0
      if (need_dynamic_reloc)
15843
0
  {
15844
0
    if (obed->rela_plts_and_copies_p)
15845
0
      {
15846
0
        if (!add_dynamic_entry (DT_RELA, 0)
15847
0
      || !add_dynamic_entry (DT_RELASZ, 0)
15848
0
      || !add_dynamic_entry (DT_RELAENT,
15849
0
           obed->s->sizeof_rela))
15850
0
    return false;
15851
0
      }
15852
0
    else
15853
0
      {
15854
0
        if (!add_dynamic_entry (DT_REL, 0)
15855
0
      || !add_dynamic_entry (DT_RELSZ, 0)
15856
0
      || !add_dynamic_entry (DT_RELENT,
15857
0
           obed->s->sizeof_rel))
15858
0
    return false;
15859
0
      }
15860
15861
    /* If any dynamic relocs apply to a read-only section,
15862
       then we need a DT_TEXTREL entry.  */
15863
0
    if ((info->flags & DF_TEXTREL) == 0)
15864
0
      elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15865
0
            info);
15866
15867
0
    if ((info->flags & DF_TEXTREL) != 0)
15868
0
      {
15869
0
        if (htab->ifunc_resolvers)
15870
0
    info->callbacks->einfo
15871
0
      (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15872
0
         "may result in a segfault at runtime; recompile with %s\n"),
15873
0
       bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15874
15875
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
15876
0
    return false;
15877
0
      }
15878
0
  }
15879
0
    }
15880
0
#undef add_dynamic_entry
15881
15882
0
  return true;
15883
0
}